THE CALL-BY-NEED LAMBDA CALCULUS, REVISITED
Stephen Chang and Matthias Felleisen Northeastern University
26/3/2012
Church call-by-name (x.e) e x e{x:=e x - - PowerPoint PPT Presentation
T HE C ALL-BY-NEED L AMBDA C ALCULUS, R EVISITED Stephen Chang and Matthias Felleisen Northeastern University 26/3/2012 Church call-by-name (x.e) e x e{x:=e x } ()
THE CALL-BY-NEED LAMBDA CALCULUS, REVISITED
Stephen Chang and Matthias Felleisen Northeastern University
26/3/2012
Church
call-by-name
(λx.e) ex → e{x:=ex} (β)
call-by-name
E[(λx.e) ex] → E[e{x:=ex}] (β)
"leftmost-outermost"
E @ λx e ex
call-by-value
E[(λx.e) vx] → E[e{x:=vx}] (βv)
"leftmost-outermost"
E @ λx e vx
call-by-need
call-by-need 1) Evaluate argument only when needed.
call-by-need 1) Evaluate argument only when needed. 2) Evaluate argument at most once.
? ?
call-by-need 1) Evaluate argument only when needed. 2) Evaluate argument at most once.
λneed-af
Ariola/Felleisen '94,'95,'97
?
=
?
=
λneed-mow
Maraist/Odersky/Wadler '94,'95,'98
call-by-need 1) Evaluate argument only when needed. 2) Evaluate argument at most once.
λneed-af
Ariola/Felleisen '94,'95,'97
Maraist/Odersky/Wadler '94,'95,'98
call-by-need 1) Evaluate argument only when needed. 2) Evaluate argument at most once.
λneed-af
Ariola/Felleisen '94,'95,'97
Maraist/Odersky/Wadler '94,'95,'98
call-by-need
λneed-af
Ariola/Felleisen '94,'95,'97
Maraist/Odersky/Wadler '94,'95,'98
call-by-need
x
λneed-af
Ariola/Felleisen '94,'95,'97
Maraist/Odersky/Wadler '94,'95,'98
call-by-need
x λx
λneed-af
Ariola/Felleisen '94,'95,'97
Maraist/Odersky/Wadler '94,'95,'98
call-by-need
x λx ex
λneed-af
Ariola/Felleisen '94,'95,'97
Maraist/Odersky/Wadler '94,'95,'98
call-by-need
x λx ex → → @ λx x ex (reshuffle)
λneed-af
Ariola/Felleisen '94,'95,'97
Maraist/Odersky/Wadler '94,'95,'98
call-by-need
@ λx x vx → @ λx vx vx (like β)
call-by-need
x λx ex
call-by-need
x λx ex
call-by-need "demand path"
x λx ex
call-by-need "demand path"
x λx ex (βneed)
OLD λneed: OPERATIONAL OVERVIEW 1) Find the next demanded variable. 2) Find its corresponding argument and evaluate it. 3) Substitute evaluated argument for demanded variable.
OLD λneed: OPERATIONAL OVERVIEW 1) Find the next demanded variable. 2) Find its corresponding argument and evaluate it. 3) Substitute evaluated argument for demanded variable.
OLD λneed: DEMAND CONTEXTS
D = [ ] | D e
OLD λneed: DEMAND CONTEXTS
D = [ ] | D e
OLD λneed: DEMAND CONTEXTS
D = [ ] | D e | (λx.D) e
OLD λneed: DEMAND CONTEXTS
D = [ ] | D e | (λx.D) e
OLD λneed: DEMAND CONTEXTS
D = [ ] | D e | (λx.D) e
OLD λneed: DEMAND CONTEXTS
D = [ ] | D e | (λx.D) e
binding structure
OLD λneed: DEMAND CONTEXTS
D = [ ] | D e | B[D]
binding structure
B = [ ] | (λx.B) e
OLD λneed: BINDING STRUCTURE
B = [ ] | (λx.B) e
(λx.(λy.(λz. ...) ez) ey) ex
OLD λneed: BINDING STRUCTURE
B = [ ] | (λx.B) e
(λx.(λy.(λz. ...) ez) ey) ex
OLD λneed: BINDING STRUCTURE
B = [ ] | (λx.B) e
(λx.(λy.(λz. ...) ez) ey) ex
OLD λneed: BINDING STRUCTURE
B = [ ] | (λx.B) e
(λx.(λy.(λz. ...) ez) ey) ex @ λx @ λy @ λz ... ez ey ex
OLD λneed: RESHUFFLING OF BINDINGS
B = [ ] | (λx.B) e
(((λx.λy.λz. ...) ex) ey) ez @ @ @ λx λy λz ... ex ey ez
OLD λneed: RESHUFFLING OF BINDINGS
B = [ ] | (λx.B) e
(((λx.λy.λz. ...) ex) ey) ez @ @ @ λx λy λz ... ex ey ez
OLD λneed: RESHUFFLING OF BINDINGS
B = [ ] | (λx.B) e
(((λx.λy.λz. ...) ex) ey) ez @ @ @ λx λy λz ... ex ey ez
OLD λneed: RESHUFFLING OF BINDINGS
B = [ ] | (λx.B) e
((λx.(λy.λz. ...) ey) ex) ez @ @ λx @ λy λz ... ey ex ez
OLD λneed: RESHUFFLING OF BINDINGS
B = [ ] | (λx.B) e
((λx.(λy.λz. ...) ey) ex) ez @ @ λx @ λy λz ... ey ex ez
OLD λneed: RESHUFFLING OF BINDINGS
B = [ ] | (λx.B) e
(λx.((λy.λz. ...) ey) ez) ex @ λx @ @ λy λz ... ey ez ex
OLD λneed: RESHUFFLING OF BINDINGS
B = [ ] | (λx.B) e
(λx.((λy.λz. ...) ey) ez) ex @ λx @ @ λy λz ... ey ez ex
OLD λneed: RESHUFFLING OF BINDINGS
B = [ ] | (λx.B) e
(λx.(λy.(λz. ...) ez) ey) ex @ λx @ λy @ λz ... ez ey ex
PROBLEMS WITH OLD CALL-BY-NEED CALCULUS
1) Reshuffling rules.
OLD λneed: OPERATIONAL OVERVIEW 1) Find the next demanded variable. 2) Find its corresponding argument and evaluate it. 3) Substitute evaluated argument for demanded variable.
OLD λneed: OPERATIONAL OVERVIEW 1) Find the next demanded variable. 2) Find its corresponding argument and evaluate it. 3) Substitute evaluated argument for demanded variable.
OLD λneed: OPERATIONAL OVERVIEW 1) Find the next demanded variable. 2) Find its corresponding argument and evaluate it. 3) Substitute evaluated argument for demanded variable.
OLD λneed: DEREFERENCING
(λx.(λy.(λz. ...) ez) ey) ex @ λx @ λy @ λz ... ez ey ex
OLD λneed: DEREFERENCING
(λx.(λy.(λz. y ) ez) ey) ex @ λx @ λy @ λz y ez ey ex
OLD λneed: DEREFERENCING
(λx.(λy.(λz. y ) ez) vy) ex @ λx @ λy @ λz y ez vy ex
OLD λneed: DEREFERENCING
(λx.(λy.(λz. y ) ez) vy) ex @ λx @ λy @ λz y ez vy ex
(λy.D[y]) v → (λy.D[v]) v (deref)
OLD λneed: DEREFERENCING
(λx.(λy.(λz. vy ) ez) vy) ex @ λx @ λy @ λz vy ez vy ex
(λy.D[y]) v → (λy.D[v]) v (deref)
PROBLEMS WITH OLD CALL-BY-NEED CALCULUS
1) Reshuffling rules. 2) Arguments and applications never go away.
NEW λneed: HANDLING ARBITRARY BINDING STRUCTURE
@ λx @ λy @ λz ... ez ey ex
NEW λneed: HANDLING ARBITRARY BINDING STRUCTURE
@ λx @ λy @ λz ... ez ey ex B = [ ] | (λx.B) e
NEW λneed: HANDLING ARBITRARY BINDING STRUCTURE
@ @ @ λx λy λz ... ex ey ez A = [ ] | ???
NEW λneed: HANDLING ARBITRARY BINDING STRUCTURE
@ λx @ @ λy λz ... ey ez ex A = [ ] | ???
NEW λneed: HANDLING ARBITRARY BINDING STRUCTURE
@ @ λx @ λy λz ... ey ex ez A = [ ] | ???
NEW λneed: HANDLING ARBITRARY BINDING STRUCTURE
@ @ λx @ λy λz ... ey ex ez A = [ ] | (λx.A) e
NEW λneed: HANDLING ARBITRARY BINDING STRUCTURE
@ @ λx @ λy λz ... ey ex ez A = [ ] | (λx.A) e
NEW λneed: HANDLING ARBITRARY BINDING STRUCTURE
@ @ λx @ λy λz ... ey ex ez A = [ ] | (λx.A) e
NEW λneed: HANDLING ARBITRARY BINDING STRUCTURE
@ @ λx @ λy λz ... ey ex ez A = [ ] | A[(λx.A)] e
NEW λneed: HANDLING ARBITRARY BINDING STRUCTURE
@ @ λx @ λy λz ... ey ex ez A = [ ] | A[(λx.A)] e D = [ ] | D e | A[D]
PROBLEMS WITH OLD CALL-BY-NEED CALCULUS
1) Reshuffling rules. 2) Arguments and applications never go away.
NEW λneed: SPLITTING CONTEXTS
@ @ @ λx λy λz ... ex ey ez
NEW λneed: SPLITTING CONTEXTS
@ @ @ λx λy λz ... ex ey ez A[ ...]
NEW λneed: SPLITTING CONTEXTS
@ @ @ λx λy λz D[y] ex ey ez A[D[y]]
NEW λneed: SPLITTING CONTEXTS
@ @ @ λx λy λz D[y] ex ey ez A[D[y]]
NEW λneed: SPLITTING CONTEXTS
@ @ @ λx λy λz D[y] ex ey ez ... (λy...D[y])...ey...
NEW λneed: SPLITTING CONTEXTS
@ @ @ λx λy λz D[y] ex ey ez ... (λy...D[y])...ey...
NEW λneed: SPLITTING CONTEXTS
@ @ @ λx λy λz D[y] ex ey ez ...A[λy...D[y] ] ey ...
NEW λneed: SPLITTING CONTEXTS
@ @ @ λx λy λz D[y] ex ey ez ...A[λy...D[y] ] ey ...
NEW λneed: SPLITTING CONTEXTS
@ @ @ λx λy λz D[y] ex ey ez ...A[λy. [D[y]]] ey ... = [ ] | A[λx. ]
NEW λneed: SPLITTING CONTEXTS
@ @ @ λx λy λz D[y] ex ey ez ...A[λy. [D[y]]] ey ... = [ ] | A[λx. ]
NEW λneed: SPLITTING CONTEXTS
@ @ @ λx λy λz D[y] ex ey ez [A[λy. [D[y]]] ey] = [ ] | A[λx. ] = [ ] | A[ ] e
NEW λneed: SPLITTING CONTEXTS
@ @ @ λx λy λz D[y] ex vy ez [A[λy. [D[y]]] vy] = [ ] | A[λx. ] = [ ] | A[ ] e
NEW λneed: βneed
@ @ @ λx λy λz D[y] ex vy ez [A[λy. [D[y]]] vy]
→
[A[ [D[y]]{y:=vy}]] (βneed)
NEW λneed: βneed
@ @ @ λx λy λz D[y] ex vy ez → @ @ λx λz D[y]{y:=vy} ex ez [A[λy. [D[y]]] vy]
→
[A[ [D[y]]{y:=vy}]] (βneed)
PROBLEMS WITH PREVIOUS CALL-BY-NEED CALCULUS
1) Reassociation rules. 2) Function calls not resolved.
NEW λneed: EVALUATING ARGUMENTS
D = [ ] | D e | A[D]
NEW λneed: EVALUATING ARGUMENTS
D = [ ] | D e | A[D] | [A[λy. [D[y]]] D]
OTHER INTERESTING THINGS IN THE PAPER . . . Correspondence to Launchbury's (1993) machine semantics.
OTHER INTERESTING THINGS IN THE PAPER . . . Correspondence to Launchbury's (1993) machine semantics. Confluence, Standardization properties.
OTHER INTERESTING THINGS IN THE PAPER . . . Correspondence to Launchbury's (1993) machine semantics. Confluence, Standardization properties. Soundness with respect to observational equivalence.