Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Evaluating Call-by-need on the Control Stack
Stephen Chang, David Van Horn, Matthias Felleisen
Northeastern University
1
Lazy Abstract Machines
Sharing implemented with:heap
2
Lazy Abstract Machines
Sharing implemented with:heap
stack operations(alternative approach)
3
Lazy Abstract Machines
Sharing implemented with:heap
stack operations(alternative approach)
[Garcia et al. 2009]
4
Our Paper
• New way to resolve variable references in the stack
5
Our Paper
• New way to resolve variable references in the stack
• Reorganize stack structure to allow indexing
6
Call-by-need λ-Calculus
[Ariola et al. 1995][Ariola and Felleisen 1997]
7
Call-by-need λ-Calculus
[Ariola et al. 1995][Ariola and Felleisen 1997]
• Delay evaluation of argument until needed
8
Call-by-need λ-Calculus
[Ariola et al. 1995][Ariola and Felleisen 1997]
• Delay evaluation of argument until needed
• Evaluate each argument only once
9
Call-by-need λ-Calculus
[Ariola et al. 1995][Ariola and Felleisen 1997]
• Delay evaluation of argument until needed
• Evaluate each argument only once
M = x | M M | λx.M
10
Call-by-need λ-Calculus
[Ariola et al. 1995][Ariola and Felleisen 1997]
• Delay evaluation of argument until needed
• Evaluate each argument only once
M = x | M M | λx.M
E = [ ] | E M | (λx.E) M | (λx.E[x]) E
11
Call-by-need λ-Calculus
[Ariola et al. 1995][Ariola and Felleisen 1997]
• Delay evaluation of argument until needed
• Evaluate each argument only once
M = x | M M | λx.M
E = [ ] | E M[ ] | E M[ ] | E M[ ] | E M | (λx.E) M | (λx.E[x]) E
12
Call-by-need λ-Calculus
[Ariola et al. 1995][Ariola and Felleisen 1997]
• Delay evaluation of argument until needed
• Evaluate each argument only once
M = x | M M | λx.M
E = [ ] | E M | (λx.E) M(λx.E) M(λx.E) M(λx.E) M | (λx.E[x]) E
13
Call-by-need λ-Calculus
[Ariola et al. 1995][Ariola and Felleisen 1997]
• Delay evaluation of argument until needed
• Evaluate each argument only once
M = x | M M | λx.M
E = [ ] | E M | (λx.E) M | (λx.E[x]) E(λx.E[x]) E(λx.E[x]) E(λx.E[x]) E
14
Call-by-need λ-Calculus
[Ariola et al. 1995][Ariola and Felleisen 1997]
• Delay evaluation of argument until needed
• Evaluate each argument only once
M = x | M M | λx.M
E = [ ] | E M | (λx.E) M | (λx.E[x]) E
deref (β alternative):
(λx.E[x]) V (λx.E[VVVV]) V
15
Call-by-need λ-Calculus
[Ariola et al. 1995][Ariola and Felleisen 1997]
• Delay evaluation of argument until needed
• Evaluate each argument only once
M = x | M M | λx.M
E = [ ] | E M | (λx.E) M | (λx.E[x]) E
deref (β alternative):
(λx.E[x]) V (λx.E[VVVV]) V
• One-at-a-time substitution (only when needed)
16
Call-by-need λ-Calculus
[Ariola et al. 1995][Ariola and Felleisen 1997]
• Delay evaluation of argument until needed
• Evaluate each argument only once
M = x | M M | λx.M
E = [ ] | E M | (λx.E) M | (λx.E[x]) E
deref (β alternative):
(λx.E[x]) V (λx.E[VVVV]) V
• One-at-a-time substitution (only when needed)
• Argument not removed (may need it again)
17
An Initial Abstract Machine
18
An Initial Abstract Machine
Standard Reduction = abstract machine
E[M] SR E[N]
if M N
19
An Initial Abstract Machine
Standard Reduction = abstract machine
E[M] SR E[N]
if M N
• Re-partition into E and M after every reduction
20
CK Machine
[Felleisen 1986]
(For by-value λ calculus)
• Separate program into two registers:
CCCC = Current subterm being evaluated
KKKK = Continuation (equiv. to eval. context)
21
CK Machine
[Felleisen 1986]
(For by-value λ calculus)
• Separate program into two registers:
CCCC = Current subterm being evaluated
KKKK = Continuation (equiv. to eval. context)
Don't need to re-partition program after every reduction
22
CK Machine
[Felleisen 1986]
(For by-value λ calculus)
• Separate program into two registers:
CCCC = Current subterm being evaluated
KKKK = Continuation (equiv. to eval. context)
Don't need to re-partition program after every reduction
[Garcia et al. 2009]: lazy CK machine
23
Evaluation Contexts (E) vs Continuations (K)
[ ] ~ mt
E[[ ] M] ~ (arg M K)E ~ K
E[(λx.[ ]) M] ~ (bind x M K)E ~ K
E[(λx.E'[x]) [ ]] ~ (op x K' K)K' ~ E', K ~ E
24
Evaluation Contexts (E) vs Continuations (K)
[ ] ~ mt[ ] ~ mt[ ] ~ mt[ ] ~ mt
E[[ ] M] ~ (arg M K)E ~ K
E[(λx.[ ]) M] ~ (bind x M K)E ~ K
E[(λx.E'[x]) [ ]] ~ (op x K' K)K' ~ E', K ~ E
25
Evaluation Contexts (E) vs Continuations (K)
[ ] ~ mt
E[[ ] M] ~ (arg M K)E[[ ] M] ~ (arg M K)E[[ ] M] ~ (arg M K)E[[ ] M] ~ (arg M K)E ~ K
E[(λx.[ ]) M] ~ (bind x M K)E ~ K
E[(λx.E'[x]) [ ]] ~ (op x K' K)K' ~ E', K ~ E
26
Evaluation Contexts (E) vs Continuations (K)
[ ] ~ mt
E[[ ] M] ~ (arg M K)E ~ K
E[(λx.[ ]) M] ~ (bind x M K)E[(λx.[ ]) M] ~ (bind x M K)E[(λx.[ ]) M] ~ (bind x M K)E[(λx.[ ]) M] ~ (bind x M K)E ~ K
E[(λx.E'[x]) [ ]] ~ (op x K' K)K' ~ E', K ~ E
27
Evaluation Contexts (E) vs Continuations (K)
[ ] ~ mt
E[[ ] M] ~ (arg M K)E ~ K
E[(λx.[ ]) M] ~ (bind x M K)E ~ K
E[(λx.E'[x]) [ ]] ~ (op x K' K)E[(λx.E'[x]) [ ]] ~ (op x K' K)E[(λx.E'[x]) [ ]] ~ (op x K' K)E[(λx.E'[x]) [ ]] ~ (op x K' K)K' ~ E', K ~ E
28
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
29
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
30
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = (λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5KKKK = mt
31
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = (λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4)KKKK = (arg M5) mt
32
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = (λy.(λz.(y M)) M0 M1 M2) M3 M4KKKK = (bind x M5) mt
33
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = (λy.(λz.(y M)) M0 M1 M2) M3KKKK = (arg M4) (bind x M5) mt
34
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = (λy.(λz.(y M)) M0 M1 M2)KKKK = (arg M3) (arg M4) (bind x M5) mt
35
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = (λz.(y M)) M0 M1 M2KKKK = (bind y M3) (arg M4) (bind x M5) mt
36
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = (λz.(y M)) M0 M1KKKK = (arg M2) (bind y M3) (arg M4) (bind x M5) mt
37
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = (λz.(y M)) M0KKKK = (arg M1) (arg M2) (bind y M3) (arg M4) (bind x M5) mt
38
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = (λz.(y M))KKKK = (arg M0) (arg M1) (arg M2) (bind y M3) (arg M4) (bind x M5) mt
39
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = (y M)KKKK = (bind z M0) (arg M1) (arg M2) (bind y M3) (arg M4) (bind x M5) mt
40
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = yKKKK = (arg M) (bind z M0) (arg M1) (arg M2) (bind y M3) (arg M4) (bind x M5) mt
41
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = yKKKK = (arg M)(arg M)(arg M)(arg M) (bind z M0) (arg M1) (arg M2) (bind y M3) (arg M4) (bind x M5) mt
42
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = yKKKK = (arg M) (bind z M0)(bind z M0)(bind z M0)(bind z M0) (arg M1) (arg M2) (bind y M3) (arg M4) (bind x M5) mt
43
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = yKKKK = (arg M) (bind z M0) (arg M1)(arg M1)(arg M1)(arg M1) (arg M2) (bind y M3) (arg M4) (bind x M5) mt
44
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = yKKKK = (arg M) (bind z M0) (arg M1) (arg M2)(arg M2)(arg M2)(arg M2) (bind y M3) (arg M4) (bind x M5) mt
45
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = yKKKK = (arg M) (bind z M0) (arg M1) (arg M2) (bind y M3)(bind y M3)(bind y M3)(bind y M3) (arg M4) (bind x M5) mt
46
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = yKKKK = (arg M) (bind z M0) (arg M1) (arg M2) (bind y M3)(bind y M3)(bind y M3)(bind y M3) (arg M4) (bind x M5) mt
47
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = yKKKK = (arg M) (bind z M0) (arg M1) (arg M2) (bind y M3)(bind y M3)(bind y M3)(bind y M3) (arg M4) (bind x M5) mt
48
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = yKKKK = (arg M) (bind z M0) (arg M1) (arg M2) (bind y M3)(bind y M3)(bind y M3)(bind y M3) (arg M4) (bind x M5) mt
49
Example (Garcia Machine)
(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5
CCCC = yKKKK = (arg M) (bind z M0) (arg M1) (arg M2) (bind y M3)(bind y M3)(bind y M3)(bind y M3) (arg M4) (bind x M5) mt
• Linear search to find argument
50
CK+ Machine: Stack Structure
• Reorganize stack to be stack of stacks
bind continuations on top
51
CK+ Machine: Stack Structure
• Reorganize stack to be stack of stacks
bind continuations on top
(arg M) (bind z M0) (arg M1) (arg M2) (bind y M3) (arg M4) (bind x M5) mt
52
CK+ Machine: Stack Structure
• Reorganize stack to be stack of stacks
bind continuations on top
(arg M) (bind z M0)(bind z M0)(bind z M0)(bind z M0) (arg M1) (arg M2) (bind y M3)(bind y M3)(bind y M3)(bind y M3) (arg M4) (bind x M5)(bind x M5)(bind x M5)(bind x M5) mt
53
CK+ Machine: Stack Structure
• Reorganize stack to be stack of stacks
bind continuations on top
(arg M) (bind z M0)(bind z M0)(bind z M0)(bind z M0) (arg M1) (arg M2) (bind y M3)(bind y M3)(bind y M3)(bind y M3) (arg M4) (bind x M5)(bind x M5)(bind x M5)(bind x M5) mt
(arg M)
mt
(bind M0)(bind M0)(bind M0)(bind M0)
(arg M1)
(arg M2)
mt
(bind M3)(bind M3)(bind M3)(bind M3)
(arg M4)
mt
(bind M5)(bind M5)(bind M5)(bind M5)
mt
54
CK+ Machine: Lexical Addresses
• Replace variables with lexical addresses
[De Bruijn 1972]
55
CK+ Machine: Lexical Addresses
• Replace variables with lexical addresses
[De Bruijn 1972]
M = x | M M | λx.M
56
CK+ Machine: Lexical Addresses
• Replace variables with lexical addresses
[De Bruijn 1972]
M = x | M M | λx.MM = n | M M | λ.M
57
CK+ Machine: Lexical Addresses
• Replace variables with lexical addresses
[De Bruijn 1972]
M = x | M M | λx.MM = n | M M | λ.M
K = mt | (arg M K) | (bind x M K) | (op x K K)
58
CK+ Machine: Lexical Addresses
• Replace variables with lexical addresses
[De Bruijn 1972]
M = x | M M | λx.MM = n | M M | λ.M
K = mt | (arg M K) | (bind x M K) | (op x K K)K = mt | (arg M K) | (bind M K) | (op K K)
59
CK+ Machine: Lexical Addresses
• Replace variables with lexical addresses
[De Bruijn 1972]
M = x | M M | λx.MM = n | M M | λ.M
K = mt | (arg M K) | (bind x M K) | (op x K K)K = mt | (arg M K) | (bind M K) | (op K K)
λx.(x λy.(x y))
60
CK+ Machine: Lexical Addresses
• Replace variables with lexical addresses
[De Bruijn 1972]
M = x | M M | λx.MM = n | M M | λ.M
K = mt | (arg M K) | (bind x M K) | (op x K K)K = mt | (arg M K) | (bind M K) | (op K K)
λx.(x λy.(x y))λ.(0 λ.(1 0))
61
CK+ Machine: Lexical Addresses
• Replace variables with lexical addresses
[De Bruijn 1972]
M = x | M M | λx.MM = n | M M | λ.M
K = mt | (arg M K) | (bind x M K) | (op x K K)K = mt | (arg M K) | (bind M K) | (op K K)
λx.(x λy.(x y))λ.(0 λ.(1 0))
62
CK+ Machine: Example
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5
63
CK+ Machine: Example
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5
64
CK+ Machine: Example
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5
CCCC = (λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5KKKK = mt
65
CK+ Machine: Example
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5
CCCC = (λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4)KKKK = (arg M5)
mt
66
CK+ Machine: Example
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5
CCCC = (λ.(λ.(1 M)) M0 M1 M2) M3 M4KKKK = mt (bind M5)
mt
67
CK+ Machine: Example
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5
CCCC = (λ.(λ.(1 M)) M0 M1 M2) M3KKKK = (arg M4)
mt
(bind M5)
mt
68
CK+ Machine: Example
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5
CCCC = (λ.(λ.(1 M)) M0 M1 M2)KKKK = (arg M3)
(arg M4)
mt
(bind M5)
mt
69
CK+ Machine: Example
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5
CCCC = (λ.(1 M)) M0 M1 M2KKKK = mt (bind M3)
(arg M4)
mt
(bind M5)
mt
70
CK+ Machine: Example
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5
CCCC = (λ.(1 M)) M0 M1KKKK = (arg M2)
mt
(bind M3)
(arg M4)
mt
(bind M5)
mt
71
CK+ Machine: Example
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5
CCCC = (λ.(1 M)) M0KKKK = (arg M1)
(arg M2)
mt
(bind M3)
(arg M4)
mt
(bind M5)
mt
72
CK+ Machine: Example
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5
CCCC = (λ.(1 M))KKKK = (arg M0)
(arg M1)
(arg M2)
mt
(bind M3)
(arg M4)
mt
(bind M5)
mt
73
CK+ Machine: Example
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5
CCCC = (1 M)KKKK = mt (bind M0)
(arg M1)
(arg M2)
mt
(bind M3)
(arg M4)
mt
(bind M5)
mt
74
CK+ Machine: Example
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5
CCCC = 1KKKK = (arg M)
mt
(bind M0)
(arg M1)
(arg M2)
mt
(bind M3)
(arg M4)
mt
(bind M5)
mt
75
CK+ Machine: Example
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5
CCCC = 1KKKK = (arg M)
mt
(bind M0)
(arg M1)
(arg M2)
mt0
(bind M3)
(arg M4)
mt1
(bind M5)
mt2
76
CK+ Machine: Example
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5
CCCC = 1KKKK = (arg M)
mt
(bind M0)
(arg M1)
(arg M2)
mt0
(bind M3)
(arg M4)
mt1
(bind M5)
mt2
77
CK+ Machine: Example
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5
CCCC = 1KKKK = (arg M)
mt
(bind M0)
(arg M1)
(arg M2)
mt0
(bind M3)
(arg M4)
mt1
(bind M5)
mt2
• Direct index instead of search
78
Stack Compaction
79
Stack Compaction
((λx.M) N) Mwhere x ∉ FV(M)
80
Stack Compaction
((λx.M) N) Mwhere x ∉ FV(M)
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5where
No variables reference M0 or M5
81
Stack Compaction
((λx.M) N) Mwhere x ∉ FV(M)
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5where
No variables reference M0 or M5
CCCC = 1
KKKK = (arg M)
mt
(bind M0)
(arg M1)
(arg M2)
mt
(bind M3)
(arg M4)
mt
(bind M5)
mt
82
Stack Compaction
((λx.M) N) Mwhere x ∉ FV(M)
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5where
No variables reference M0 or M5
CCCC = 1
KKKK = (arg M)
mt
(bind M0)(bind M0)(bind M0)(bind M0)
(arg M1)
(arg M2)
mt
(bind M3)
(arg M4)
mt
(bind M5)
mt
83
Stack Compaction
((λx.M) N) Mwhere x ∉ FV(M)
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5where
No variables reference M0 or M5
CCCC = 1
KKKK = (arg M)
(arg M1)
(arg M2)
mt
(bind M3)
(arg M4)
mt
(bind M5)
mt
84
Stack Compaction
((λx.M) N) Mwhere x ∉ FV(M)
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5where
No variables reference M0 or M5
CCCC = 1
KKKK = (arg M)
(arg M1)
(arg M2)
mt
(bind M3)(bind M3)(bind M3)(bind M3)
(arg M4)
mt
(bind M5)
mt
85
Stack Compaction
((λx.M) N) Mwhere x ∉ FV(M)
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5where
No variables reference M0 or M5
CCCC = 1
KKKK = (arg M)
(arg M1)
(arg M2)
mt
(bind M3)
(arg M4)
mt
(bind M5)(bind M5)(bind M5)(bind M5)
mt
86
Stack Compaction
((λx.M) N) Mwhere x ∉ FV(M)
(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5where
No variables reference M0 or M5
CCCC = 1
KKKK = (arg M)
(arg M1)
(arg M2)
mt
(bind M3)
(arg M4)
mt
87
Thanks!
88