SlideShare a Scribd company logo
1 of 67
Download to read offline
lazy evaluation
hey, i’m fronx.

berlin compiler meetup, feb 2014
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
not
- user-code performance optimization
- how and why to avoid capture
- supercombinators / λ-lifting
- representation in memory
- garbage collection
- compiler optimizations
http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/
semantics
strict

non-strict
vs.

evaluation strategy
eager

lazy
as a language property
non-strict
expressions can

have a value

even if
some of their
subexpressions

do not
http://www.haskell.org/haskellwiki/Non-strict_semantics
strict
expressions can not

have a value

even if
some of their
subexpressions

do not
http://www.haskell.org/haskellwiki/Non-strict_semantics
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
“not have a value”?
loop :: Int -> Int
loop n = 1 + loop n
loop :: Int -> Int
loop n = 1 + loop n
repeat :: Int -> [Int]
repeat n = n : repeat n
!

> repeat 1
[1,1,1,1,1,1,1,1,1,1,1,1,…
how is this useful?
infinite lists are awesome.
nat :: [Int]
nat = nat' 1
where nat' n = n : nat' (n + 1)
!

take 5 (map sqr nat) == [1,4,9,16,25]
http://mybroadband.co.za/news/wp-content/uploads/2013/03/Barack-Obama-Michelle-Obama-not-bad.jpg
⊥
”bottom”
“a computation which
never completes successfully”
http://www.haskell.org/haskellwiki/Bottom
⊥
bottom :: a
bottom = bottom
undefined (throws error)
take 0 _ = []
take n (x : xs) = x : take (n - 1) xs
take n [] = []
!

take 2 (1:1:1:undefined)
== take 2 (1:1:undefined)
== [1,1]
repeat n = n : repeat n
!

repeat 1
=> 1 : ⊥
=> 1 : 1 : ⊥
=> 1 : 1 : 1 : ⊥
=> 1 : 1 : 1 : 1 : ⊥

…
repeat n = n : repeat n
!

repeat 1
=> 1 : ⊥
=> 1 : 1 : ⊥

Terms
approximation,
more defined

=> 1 : 1 : 1 : ⊥
=> 1 : 1 : 1 : 1 : ⊥

…
http://en.wikibooks.org/wiki/Haskell/Denotational_semantics
strict / non-strict
functions
a function is strict iff

f⊥=⊥
strictness of functions
id = λ x . x
(λx.x)⊥=⊥
strict
strictness of functions
one = λ x . 1
(λx.1)⊥=1
non-strict
strictness of functions
second = λ x y . y
(λxy.y)⊥ 2=2
strict in its 2nd argument
non-strict in its 1st argument
repeat n = n : repeat n

Question:

!

repeat 1
=> 1 : ⊥
=> 1 : 1 : ⊥
=> 1 : 1 : 1 : ⊥

Is repeat strict
or non-strict
(“lazy”)?

=> 1 : 1 : 1 : 1 : ⊥

…
repeat n = n : repeat n
!

repeat ⊥ = ⊥ : repeat ⊥
repeat n = n : repeat n
!

repeat ⊥ = ⊥ : repeat ⊥

strict
one x = 1
one ⊥ = 1

non-strict
evaluation
strategies
first x y = x

lazy

eager
first 1 (loop 2)

first 1 (loop 2)

=>

=>
first x y = x

eager

lazy

first 1 (loop 2)

first 1 (loop 2)

=> first 1 (2 + (loop 2))

=1

=> first 1 (2 + (2 + (loop 2)))
=

⊥
first x y = x

lazy

eager
first 1 (loop 2)

first 1 (loop 2)

=> first 1 (2 + (loop 2))

=1

=> first 1 (2 + (2 + (loop 2)))
=

⊥

redex: reducible expression
first x y = x

lazy

eager
first (loop 1) 2

first (loop 1) 2

=> first (1 + (loop 1)) 2

=> loop 1

=> first (1 + (1 + (loop 1))) 2

=> 1 + (loop 1)

=

⊥

=> 1 + (1 + (loop 1))
=

⊥
nat n = n : nat (n + 1)

lazy

eager
nat 1

nat 1

=>

=>
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.
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?
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/
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/
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/
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/
graph reduction
a way to model / implement
lazy evaluation
graph reduction
first = λ x y . x
first = λ x . λ y . x
first 1 ⊥

@
@
λx
λy
x

⊥
1
graph reduction
first = λ x y . x
first = λ x . λ y . x
first 1 ⊥
leftmost outermost
reduction
=
normal-order
reduction

@
@
λx
λy
x

⊥
1
graph reduction
first = λ x y . x
first = λ x . λ y . x
first 1 ⊥

instantiate
the lambda
body

@
@
λx
λy
x

⊥
1
@
λy
1

⊥
1
graph reduction
sqr = λ x . x * x
sqr 2

@
2

λx

@

@
@
*

@

x
x

*

2
2

4
graph reduction
sqr (6 + 1)

@
@

λx
@

@
@
*

x
x

+ 6

1
graph reduction
sqr (6 + 1)

call by name

@
@

λx
@

@
@
*

@

x

1

@

@

@

+ 6
*

x

@
+

+

@
1
6

1
6
…
graph reduction
sqr (6 + 1)

sharing
call by need

@
@

λx
@

@
@
*

x
x

+ 6

@
1

@
*

@
@

1

+ 6

@
@

*

7
49
+ call by name
+ sharing
+ ——————
+ call by need
shadowing
(λx.λx.x)1 2

@
2

@
λx
λx
x

1

@
λx
x

2
2
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/
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/
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/
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/
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
so how does take work?
take 0 _ = []
take n (x : xs) = x : take (n - 1) xs
take n [] = []
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
product-matching
= zeroPair (x,y) = 0
!

= Eval [[ zeroPair ]] ⊥ = ⊥

strict product-matching

= Eval [[ zeroPair ]] ⊥ = 0

lazy product-matching

http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/
lazy product-matching
= zeroPair (x,y) = 0
!

= Eval [[ zeroPair ]] ⊥
= Eval [[ λ(Pair x y). 0 ]] ⊥
= Eval [[ λx. λy. 0 ]] (SEL-PAIR-1 ⊥) (SEL-PAIR-2 ⊥)
= Eval [[ λy. 0 ]] (SEL-PAIR-2 ⊥)
=0

http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/
lazy product-matching
SEL-PAIR-1 (Pair x y) = x
SEL-PAIR-1 ⊥

=⊥

SEL-PAIR-2 (Pair x y) = y
SEL-PAIR-2 ⊥

=⊥

http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/
lazy product-matching
SEL-PAIR-1 (Pair x y) = x
SEL-PAIR-1 ⊥

=⊥

SEL-PAIR-2 (Pair x y) = y
SEL-PAIR-2 ⊥

=⊥

SEL-constr-i (constr a1 … ai … an) = ai
SEL-constr-i ⊥

=⊥

http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/
summary
- f is strict

f⊥=⊥

- graph reduction
- sharing
- lazy pattern-matching
- go and read those papers/books
okay.
that’s it for today.
“Laziness kept us pure.”
Simon Peyton Jones

http://www.techworld.com.au/article/261007/a-z_programming_languages_haskell/?pp=7
“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

More Related Content

Similar to Lazy evaluation

Presentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingPresentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional Programming
Filip De Sutter
 
Lesson 4A - Inverses of Functions.ppt
Lesson 4A - Inverses of Functions.pptLesson 4A - Inverses of Functions.ppt
Lesson 4A - Inverses of Functions.ppt
ssuser78a386
 
Haskell retrospective
Haskell retrospectiveHaskell retrospective
Haskell retrospective
chenge2k
 
Threading Is Not A Model
Threading Is Not A ModelThreading Is Not A Model
Threading Is Not A Model
guest2a5acfb
 
How to herd cat statues and make awesome things
How to herd cat statues and make awesome thingsHow to herd cat statues and make awesome things
How to herd cat statues and make awesome things
meldra
 

Similar to Lazy evaluation (20)

Presentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingPresentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional Programming
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 
Lesson 4A - Inverses of Functions.ppt
Lesson 4A - Inverses of Functions.pptLesson 4A - Inverses of Functions.ppt
Lesson 4A - Inverses of Functions.ppt
 
LISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesLISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love Parantheses
 
Haskell retrospective
Haskell retrospectiveHaskell retrospective
Haskell retrospective
 
Python to scala
Python to scalaPython to scala
Python to scala
 
Python Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionPython Puzzlers - 2016 Edition
Python Puzzlers - 2016 Edition
 
Lesson 6: The derivative as a function
Lesson 6: The derivative as a functionLesson 6: The derivative as a function
Lesson 6: The derivative as a function
 
Introduction to Functional Languages
Introduction to Functional LanguagesIntroduction to Functional Languages
Introduction to Functional Languages
 
Using Language Oriented Programming to Execute Computations on the GPU
Using Language Oriented Programming to Execute Computations on the GPUUsing Language Oriented Programming to Execute Computations on the GPU
Using Language Oriented Programming to Execute Computations on the GPU
 
Functions
FunctionsFunctions
Functions
 
Intro to threp
Intro to threpIntro to threp
Intro to threp
 
Threading Is Not A Model
Threading Is Not A ModelThreading Is Not A Model
Threading Is Not A Model
 
6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
code4lib 2011 preconference: What's New in Solr (since 1.4.1)
code4lib 2011 preconference: What's New in Solr (since 1.4.1)code4lib 2011 preconference: What's New in Solr (since 1.4.1)
code4lib 2011 preconference: What's New in Solr (since 1.4.1)
 
How to herd cat statues and make awesome things
How to herd cat statues and make awesome thingsHow to herd cat statues and make awesome things
How to herd cat statues and make awesome things
 
Beyond Ruby (RubyConf Argentina 2011)
Beyond Ruby (RubyConf Argentina 2011)Beyond Ruby (RubyConf Argentina 2011)
Beyond Ruby (RubyConf Argentina 2011)
 
A brief introduction to lisp language
A brief introduction to lisp languageA brief introduction to lisp language
A brief introduction to lisp language
 
F# Ignite - DNAD2010
F# Ignite - DNAD2010F# Ignite - DNAD2010
F# Ignite - DNAD2010
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 

Lazy evaluation

  • 1. lazy evaluation hey, i’m fronx. berlin compiler meetup, feb 2014
  • 2.
  • 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
  • 7. as a language property
  • 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
  • 11. “not have a value”?
  • 12. loop :: Int -> Int loop n = 1 + loop n
  • 13. loop :: Int -> Int loop n = 1 + loop n repeat :: Int -> [Int] repeat n = n : repeat n ! > repeat 1 [1,1,1,1,1,1,1,1,1,1,1,1,…
  • 14. how is this useful?
  • 15. infinite lists are awesome. nat :: [Int] nat = nat' 1 where nat' n = n : nat' (n + 1) ! take 5 (map sqr nat) == [1,4,9,16,25]
  • 17. ⊥ ”bottom” “a computation which never completes successfully” http://www.haskell.org/haskellwiki/Bottom
  • 18. ⊥ bottom :: a bottom = bottom undefined (throws error)
  • 19. take 0 _ = [] take n (x : xs) = x : take (n - 1) xs take n [] = [] ! take 2 (1:1:1:undefined) == take 2 (1:1:undefined) == [1,1]
  • 20. repeat n = n : repeat n ! repeat 1 => 1 : ⊥ => 1 : 1 : ⊥ => 1 : 1 : 1 : ⊥ => 1 : 1 : 1 : 1 : ⊥ …
  • 21. repeat n = n : repeat n ! repeat 1 => 1 : ⊥ => 1 : 1 : ⊥ Terms approximation, more defined => 1 : 1 : 1 : ⊥ => 1 : 1 : 1 : 1 : ⊥ … http://en.wikibooks.org/wiki/Haskell/Denotational_semantics
  • 23. a function is strict iff f⊥=⊥
  • 24. strictness of functions id = λ x . x (λx.x)⊥=⊥ strict
  • 25. strictness of functions one = λ x . 1 (λx.1)⊥=1 non-strict
  • 26. strictness of functions second = λ x y . y (λxy.y)⊥ 2=2 strict in its 2nd argument non-strict in its 1st argument
  • 27. repeat n = n : repeat n Question: ! repeat 1 => 1 : ⊥ => 1 : 1 : ⊥ => 1 : 1 : 1 : ⊥ Is repeat strict or non-strict (“lazy”)? => 1 : 1 : 1 : 1 : ⊥ …
  • 28. repeat n = n : repeat n ! repeat ⊥ = ⊥ : repeat ⊥
  • 29. repeat n = n : repeat n ! repeat ⊥ = ⊥ : repeat ⊥ strict
  • 30. one x = 1 one ⊥ = 1 non-strict
  • 32. first x y = x lazy eager first 1 (loop 2) first 1 (loop 2) => =>
  • 33. first x y = x eager lazy first 1 (loop 2) first 1 (loop 2) => first 1 (2 + (loop 2)) =1 => first 1 (2 + (2 + (loop 2))) = ⊥
  • 34. first x y = x lazy eager first 1 (loop 2) first 1 (loop 2) => first 1 (2 + (loop 2)) =1 => first 1 (2 + (2 + (loop 2))) = ⊥ redex: reducible expression
  • 35. first x y = x lazy eager first (loop 1) 2 first (loop 1) 2 => first (1 + (loop 1)) 2 => loop 1 => first (1 + (1 + (loop 1))) 2 => 1 + (loop 1) = ⊥ => 1 + (1 + (loop 1)) = ⊥
  • 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/
  • 43. graph reduction a way to model / implement lazy evaluation
  • 44. graph reduction first = λ x y . x first = λ x . λ y . x first 1 ⊥ @ @ λx λy x ⊥ 1
  • 45. graph reduction first = λ x y . x first = λ x . λ y . x first 1 ⊥ leftmost outermost reduction = normal-order reduction @ @ λx λy x ⊥ 1
  • 46. graph reduction first = λ x y . x first = λ x . λ y . x first 1 ⊥ instantiate the lambda body @ @ λx λy x ⊥ 1 @ λy 1 ⊥ 1
  • 47. graph reduction sqr = λ x . x * x sqr 2 @ 2 λx @ @ @ * @ x x * 2 2 4
  • 48. graph reduction sqr (6 + 1) @ @ λx @ @ @ * x x + 6 1
  • 49. graph reduction sqr (6 + 1) call by name @ @ λx @ @ @ * @ x 1 @ @ @ + 6 * x @ + + @ 1 6 1 6 …
  • 50. graph reduction sqr (6 + 1) sharing call by need @ @ λx @ @ @ * x x + 6 @ 1 @ * @ @ 1 + 6 @ @ * 7 49
  • 51. + call by name + sharing + —————— + call by need
  • 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
  • 60. product-matching = zeroPair (x,y) = 0 ! = Eval [[ zeroPair ]] ⊥ = ⊥ strict product-matching = Eval [[ zeroPair ]] ⊥ = 0 lazy product-matching http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/
  • 61. lazy product-matching = zeroPair (x,y) = 0 ! = Eval [[ zeroPair ]] ⊥ = Eval [[ λ(Pair x y). 0 ]] ⊥ = Eval [[ λx. λy. 0 ]] (SEL-PAIR-1 ⊥) (SEL-PAIR-2 ⊥) = Eval [[ λy. 0 ]] (SEL-PAIR-2 ⊥) =0 http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/
  • 62. lazy product-matching SEL-PAIR-1 (Pair x y) = x SEL-PAIR-1 ⊥ =⊥ SEL-PAIR-2 (Pair x y) = y SEL-PAIR-2 ⊥ =⊥ http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/
  • 63. lazy product-matching SEL-PAIR-1 (Pair x y) = x SEL-PAIR-1 ⊥ =⊥ SEL-PAIR-2 (Pair x y) = y SEL-PAIR-2 ⊥ =⊥ SEL-constr-i (constr a1 … ai … an) = ai SEL-constr-i ⊥ =⊥ http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/
  • 64. summary - f is strict f⊥=⊥ - graph reduction - sharing - lazy pattern-matching - go and read those papers/books
  • 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