+ All Categories
Home > Documents > Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack...

Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack...

Date post: 11-Mar-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
88
Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1
Transcript
Page 1: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

Evaluating Call-by-need on the Control Stack

Stephen Chang, David Van Horn, Matthias Felleisen

Northeastern University

1

Page 2: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

Lazy Abstract Machines

Sharing implemented with:heap

2

Page 3: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

Lazy Abstract Machines

Sharing implemented with:heap

stack operations(alternative approach)

3

Page 4: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

Lazy Abstract Machines

Sharing implemented with:heap

stack operations(alternative approach)

[Garcia et al. 2009]

4

Page 5: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

Our Paper

• New way to resolve variable references in the stack

5

Page 6: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

Our Paper

• New way to resolve variable references in the stack

• Reorganize stack structure to allow indexing

6

Page 7: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

Call-by-need λ-Calculus

[Ariola et al. 1995][Ariola and Felleisen 1997]

7

Page 8: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

Call-by-need λ-Calculus

[Ariola et al. 1995][Ariola and Felleisen 1997]

• Delay evaluation of argument until needed

8

Page 9: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

Call-by-need λ-Calculus

[Ariola et al. 1995][Ariola and Felleisen 1997]

• Delay evaluation of argument until needed

• Evaluate each argument only once

9

Page 10: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 11: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 12: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 13: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 14: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 15: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 16: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 17: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 18: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

An Initial Abstract Machine

18

Page 19: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

An Initial Abstract Machine

Standard Reduction = abstract machine

E[M] SR E[N]

if M N

19

Page 20: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 21: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 22: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 23: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 24: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 25: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 26: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 27: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 28: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 29: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

Example (Garcia Machine)

(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5

29

Page 30: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

Example (Garcia Machine)

(λx.(λy.(λz.(y M)) M0 M1 M2) M3 M4) M5

30

Page 31: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 32: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 33: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 34: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 35: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 36: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 37: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 38: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 39: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 40: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 41: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 42: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 43: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 44: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 45: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 46: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 47: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 48: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 49: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 50: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 51: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

CK+ Machine: Stack Structure

• Reorganize stack to be stack of stacks

bind continuations on top

51

Page 52: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 53: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 54: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 55: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

CK+ Machine: Lexical Addresses

• Replace variables with lexical addresses

[De Bruijn 1972]

55

Page 56: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

CK+ Machine: Lexical Addresses

• Replace variables with lexical addresses

[De Bruijn 1972]

M = x | M M | λx.M

56

Page 57: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

CK+ Machine: Lexical Addresses

• Replace variables with lexical addresses

[De Bruijn 1972]

M = x | M M | λx.MM = n | M M | λ.M

57

Page 58: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 59: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 60: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 61: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 62: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 63: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

CK+ Machine: Example

(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5

63

Page 64: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

CK+ Machine: Example

(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5

64

Page 65: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

CK+ Machine: Example

(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5

CCCC = (λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5KKKK = mt

65

Page 66: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

CK+ Machine: Example

(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5

CCCC = (λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4)KKKK = (arg M5)

mt

66

Page 67: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

CK+ Machine: Example

(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5

CCCC = (λ.(λ.(1 M)) M0 M1 M2) M3 M4KKKK = mt (bind M5)

mt

67

Page 68: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 69: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 70: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 71: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 72: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 73: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 74: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 75: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 76: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 77: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 78: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 79: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

Stack Compaction

79

Page 80: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

Stack Compaction

((λx.M) N) Mwhere x ∉ FV(M)

80

Page 81: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

Stack Compaction

((λx.M) N) Mwhere x ∉ FV(M)

(λ.(λ.(λ.(1 M)) M0 M1 M2) M3 M4) M5where

No variables reference M0 or M5

81

Page 82: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 83: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 84: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 85: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 86: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 87: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

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

Page 88: Evaluating Call-by-need on the Control Stack · Evaluating Call-by-need on the Control Stack Stephen Chang, David Van Horn, Matthias Felleisen Northeastern University 1. Lazy Abstract

Thanks!

88


Recommended