3. infinite lists
graph reduction
bottom
normal order
redex
call by need
constant subexpression
strictness in
an argument
sharing
leftmost outermost
full laziness
approximation
eager
instantiation
WHNF
thunk
4. not
- user-code performance optimization
- how and why to avoid capture
- supercombinators / λ-lifting
- representation in memory
- garbage collection
- compiler optimizations
8. non-strict
expressions can
have a value
even if
some of their
subexpressions
do not
http://www.haskell.org/haskellwiki/Non-strict_semantics
9. strict
expressions can not
have a value
even if
some of their
subexpressions
do not
http://www.haskell.org/haskellwiki/Non-strict_semantics
10. strict
expressions can not
have a value
even if
some of their
subexpressions
do not … have a value
http://www.haskell.org/haskellwiki/Non-strict_semantics
36. nat n = n : nat (n + 1)
lazy
eager
nat 1
nat 1
=>
=>
37. nat n = n : nat (n + 1)
eager
lazy
nat 1
nat 1
=> 1 : nat (1+1)
= 1 : nat (1 + 1)
=> 1 : nat 2
=> 1 : 2 : nat (2 + 1)
=
⊥
the function is still strict!
lazy evaluation allows
you to look at intermediate
results, though.
38. nat n = n : nat (n + 1)
eager
lazy
nat 1
nat 1
=> 1 : nat (1+1)
= 1 : nat (1 + 1)
=> 1 : nat 2
=> 1 : 2 : nat (2 + 1)
=
⊥
Question:
How do you define the
point where it has
to stop?
39. weak head normal form
expression head is one of:
-
variable
data object
built-in function
lambda abstraction
http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/
40. weak head normal form
An expression has
no top-level redex
iff
it is in
weak head normal form.
http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/
41. weak head normal form
examples:
3
1 : (2 : …) == CONS 1 (CONS 2 …)
+ (- 4 3)
(λx. + 5 1)
http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/
42. weak head normal form
result:
Ignore inner redexes
as long as you can.
http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/
53. full laziness
λy. + y (sqrt 4)
@
λx
λy
“x(1), x(2)”
@
@
+
constant
subexpression
y
@
sqrt
4
http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/
54. maximal free expression
a free expression
which is not a subexpression
of another free expression
http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/
55. full laziness
λy. + y (sqrt 4)
@
λx
λy $
“x(1), x(2)”
@
@
+
y
maximal free
expression in $
@
sqrt
4
free subexpression
http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/
56. full laziness
λy. + y (sqrt 4)
@
λx
λy
“x(1), x(2)”
@
@
+
y
sqrt4
@
sqrt
4
Extract all the
maximal free
expressions!
!
|o/
http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/
57. full laziness
fully lazy λ-lifting SPJ87
extract MFEs via closures Sestoft97
sharing via labelling Blanc/Levy/Maranget07
variants
(sorry, no time for details.)
A Unified Approach to Fully Lazy Sharing, Balabonski 2012
58. so how does take work?
take 0 _ = []
take n (x : xs) = x : take (n - 1) xs
take n [] = []
59. so how does take work?
take 0 _ = []
take n (x : xs) = x : take (n - 1) xs
take n [] = []
patterns
-
variable
constant
sum constructor
product constructor
66. “Laziness kept us pure.”
Simon Peyton Jones
http://www.techworld.com.au/article/261007/a-z_programming_languages_haskell/?pp=7
67. “If your program is slow, don't bother
profiling. Just scatter bang patterns
throughout your code like confetti.
They're magic!”
https://twitter.com/evilhaskelltips/status/429788191613140992