Defunctionalized Interpreters for Call-by-Need Evaluation Olivier - - PowerPoint PPT Presentation

defunctionalized interpreters for call by need evaluation
SMART_READER_LITE
LIVE PREVIEW

Defunctionalized Interpreters for Call-by-Need Evaluation Olivier - - PowerPoint PPT Presentation

Defunctionalized Interpreters for Call-by-Need Evaluation Olivier Danvy, University of Aarhus Kevin Millikin, Google Johan Munk, Arctic Lake Systems Ian Zerny, University of Aarhus Sendai, Japan FLOPS 2010 Motivation Formal semantics: why?


slide-1
SLIDE 1

Defunctionalized Interpreters for Call-by-Need Evaluation

Olivier Danvy, University of Aarhus Kevin Millikin, Google Johan Munk, Arctic Lake Systems Ian Zerny, University of Aarhus Sendai, Japan FLOPS 2010

slide-2
SLIDE 2

Motivation

Formal semantics: why?

◮ Understanding linguistic features ◮ Proving programs correct or equivalent ◮ Proving language properties ◮ Proving implementations, analyses or

transformations correct Formal semantics: which kind?

◮ Denotational? ◮ Operational? Big step? Small step? ◮ Axiomatic?

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 2 / 18

slide-3
SLIDE 3

Foundations of this work

Semantic artifacts can be inter-derived mechanically, and the inter-derivation is worthwhile:

◮ it can yield simpler semantics, and ◮ it can yield new semantics.

Here: call by need.

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 3 / 18

slide-4
SLIDE 4

Call-by-need evaluation

◮ Demand-driven computation ◮ Memoization of intermediate results

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 4 / 18

slide-5
SLIDE 5

Semantics for call-by-need evaluation

◮ Store-based:

results are saved in the global store

◮ Storeless:

results are saved in the term itself

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 5 / 18

slide-6
SLIDE 6

Syntactic theories of call by need

◮ Different opinions — the POPL’95 affair

◮ The call-by-need lambda-calculus

by Ariola and Felleisen (JFP’97)

◮ The call-by-need lambda-calculus

by Maraist, Odersky and Wadler (JFP’98)

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 6 / 18

slide-7
SLIDE 7

Syntactic theories of call by need

◮ Different opinions — the POPL’95 affair

◮ The call-by-need lambda-calculus

by Ariola and Felleisen (JFP’97)

◮ The call-by-need lambda-calculus

by Maraist, Odersky and Wadler (JFP’98)

◮ Appearances can be deceiving:

The standard reduction is common to both

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 6 / 18

slide-8
SLIDE 8

Syntactic theories of call by need

◮ Different opinions — the POPL’95 affair

◮ The call-by-need lambda-calculus

by Ariola and Felleisen (JFP’97)

◮ The call-by-need lambda-calculus

by Maraist, Odersky and Wadler (JFP’98)

◮ Appearances can be deceiving:

The standard reduction is common to both

◮ It is our starting point

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 6 / 18

slide-9
SLIDE 9

Outline

  • 1. The call-by-need λ-calculus
  • 2. Deriving an abstract machine

and a natural semantics

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 7 / 18

slide-10
SLIDE 10

The call-by-name λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 8 / 18

slide-11
SLIDE 11

The call-by-name λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E

(λz.z z) ((λy.y) (λx.x)) →name

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 8 / 18

slide-12
SLIDE 12

The call-by-name λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E

(λz.z z) ((λy.y) (λx.x)) →name let z be (λy.y) (λx.x) in z z →name

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 8 / 18

slide-13
SLIDE 13

The call-by-name λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E

(λz.z z) ((λy.y) (λx.x)) →name let z be (λy.y) (λx.x) in z z →name let z be (λy.y) (λx.x) in ((λy.y) (λx.x)) z →name

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 8 / 18

slide-14
SLIDE 14

The call-by-name λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E

(λz.z z) ((λy.y) (λx.x)) →name let z be (λy.y) (λx.x) in z z →name let z be (λy.y) (λx.x) in ((λy.y) (λx.x)) z →name let z be (λy.y) (λx.x) in (let y be λx.x in y) z →name

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 8 / 18

slide-15
SLIDE 15

The call-by-name λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E

(λz.z z) ((λy.y) (λx.x)) →name let z be (λy.y) (λx.x) in z z →name let z be (λy.y) (λx.x) in ((λy.y) (λx.x)) z →name let z be (λy.y) (λx.x) in (let y be λx.x in y) z →name let z be (λy.y) (λx.x) in let y be λx.x in y z →name

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 8 / 18

slide-16
SLIDE 16

The call-by-name λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E

(λz.z z) ((λy.y) (λx.x)) →name let z be (λy.y) (λx.x) in z z →name let z be (λy.y) (λx.x) in ((λy.y) (λx.x)) z →name let z be (λy.y) (λx.x) in (let y be λx.x in y) z →name let z be (λy.y) (λx.x) in let y be λx.x in y z →name let z be (λy.y) (λx.x) in let y be λx.x in (λx.x) z →name

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 8 / 18

slide-17
SLIDE 17

The call-by-name λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E

(λz.z z) ((λy.y) (λx.x)) →name let z be (λy.y) (λx.x) in z z →name let z be (λy.y) (λx.x) in ((λy.y) (λx.x)) z →name let z be (λy.y) (λx.x) in (let y be λx.x in y) z →name let z be (λy.y) (λx.x) in let y be λx.x in y z →name let z be (λy.y) (λx.x) in let y be λx.x in (λx.x) z →name let z be (λy.y) (λx.x) in let y be λx.x in let x be z in x →name

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 8 / 18

slide-18
SLIDE 18

The call-by-name λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E

(λz.z z) ((λy.y) (λx.x)) →name let z be (λy.y) (λx.x) in z z →name let z be (λy.y) (λx.x) in ((λy.y) (λx.x)) z →name let z be (λy.y) (λx.x) in (let y be λx.x in y) z →name let z be (λy.y) (λx.x) in let y be λx.x in y z →name let z be (λy.y) (λx.x) in let y be λx.x in (λx.x) z →name let z be (λy.y) (λx.x) in let y be λx.x in let x be z in x →name let z be (λy.y) (λx.x) in let y be λx.x in let x be z in z →name

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 8 / 18

slide-19
SLIDE 19

The call-by-name λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E

(λz.z z) ((λy.y) (λx.x)) →name let z be (λy.y) (λx.x) in z z →name let z be (λy.y) (λx.x) in ((λy.y) (λx.x)) z →name let z be (λy.y) (λx.x) in (let y be λx.x in y) z →name let z be (λy.y) (λx.x) in let y be λx.x in y z →name let z be (λy.y) (λx.x) in let y be λx.x in (λx.x) z →name let z be (λy.y) (λx.x) in let y be λx.x in let x be z in x →name let z be (λy.y) (λx.x) in let y be λx.x in let x be z in z →name let z be (λy.y) (λx.x) in let y be λx.x in let x be z in (λy.y) (λx.x) . . .

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 8 / 18

slide-20
SLIDE 20

The call-by-need λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E | let x be E in E[x]

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 9 / 18

slide-21
SLIDE 21

The call-by-need λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E | let x be E in E[x]

(λz.z z) ((λy.y) (λx.x)) →need

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 9 / 18

slide-22
SLIDE 22

The call-by-need λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E | let x be E in E[x]

(λz.z z) ((λy.y) (λx.x)) →need let z be (λy.y) (λx.x) in z z →need

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 9 / 18

slide-23
SLIDE 23

The call-by-need λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E | let x be E in E[x]

(λz.z z) ((λy.y) (λx.x)) →need let z be (λy.y) (λx.x) in z z →need let z be (let y be λx.x in y ) in z z →need

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 9 / 18

slide-24
SLIDE 24

The call-by-need λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E | let x be E in E[x]

(λz.z z) ((λy.y) (λx.x)) →need let z be (λy.y) (λx.x) in z z →need let z be (let y be λx.x in y ) in z z →need let z be (let y be λx.x in λx.x) in z z →need

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 9 / 18

slide-25
SLIDE 25

The call-by-need λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E | let x be E in E[x]

(λz.z z) ((λy.y) (λx.x)) →need let z be (λy.y) (λx.x) in z z →need let z be (let y be λx.x in y ) in z z →need let z be (let y be λx.x in λx.x) in z z →need let y be λx.x in let z be λx.x in z z →need

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 9 / 18

slide-26
SLIDE 26

The call-by-need λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E | let x be E in E[x]

(λz.z z) ((λy.y) (λx.x)) →need let z be (λy.y) (λx.x) in z z →need let z be (let y be λx.x in y ) in z z →need let z be (let y be λx.x in λx.x) in z z →need let y be λx.x in let z be λx.x in z z →need let y be λx.x in let z be λx.x in (λx.x) z →need

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 9 / 18

slide-27
SLIDE 27

The call-by-need λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E | let x be E in E[x]

(λz.z z) ((λy.y) (λx.x)) →need let z be (λy.y) (λx.x) in z z →need let z be (let y be λx.x in y ) in z z →need let z be (let y be λx.x in λx.x) in z z →need let y be λx.x in let z be λx.x in z z →need let y be λx.x in let z be λx.x in (λx.x) z →need let y be λx.x in let z be λx.x in let x be z in x →need

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 9 / 18

slide-28
SLIDE 28

The call-by-need λlet-calculus

Terms ∋ T ::= x | λx.T | T T | let x be T in T Values ∋ V ::= λx.T Eval Cont ∋ E ::= [ ] | E T | let x be T in E | let x be E in E[x]

(λz.z z) ((λy.y) (λx.x)) →need let z be (λy.y) (λx.x) in z z →need let z be (let y be λx.x in y ) in z z →need let z be (let y be λx.x in λx.x) in z z →need let y be λx.x in let z be λx.x in z z →need let y be λx.x in let z be λx.x in (λx.x) z →need let y be λx.x in let z be λx.x in let x be z in x →need let y be λx.x in let z be λx.x in let x be λx.x in x →need let y be λx.x in let z be λx.x in let x be λx.x in λx.x

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 9 / 18

slide-29
SLIDE 29

Accounting for call by need syntactically

On-demand: already present for call by name let x be T in E[x] → let x be T in E[T] Memoization: restricting substitution to values let x be V in E[x] → let x be V in E[V ] and adapting the search (hence the extra context constructor)

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 10 / 18

slide-30
SLIDE 30

Related semantics for call by need

◮ A variety of fascinating semantics exist:

TIM (Fairbairn and Wray, FPCA’87) Lazy Krivine Machine (Sestoft, JFP’97) Maraist et al., POPL’98 Garcia et al., POPL’09 etc.

◮ How do they relate to

the call-by-need λ-calculus?

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 11 / 18

slide-31
SLIDE 31

Our thesis

What: We can constructively calculate the corresponding abstract machine and other semantic artifacts.

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 12 / 18

slide-32
SLIDE 32

Our thesis

What: We can constructively calculate the corresponding abstract machine and other semantic artifacts. How: We use program transformations

  • ff the shelf.

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 12 / 18

slide-33
SLIDE 33

Our sub-thesis

◮ Spell out the reduction semantics in detail ◮ Leads directly to the abstract machine

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 13 / 18

slide-34
SLIDE 34

The reduction semantics

Decomposition Searches for the next redex, if there is one, and its context Contraction Contracts a redex to a contractum Recomposition Reconstructs a term from a contractum and the context

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 14 / 18

slide-35
SLIDE 35

The reduction semantics

Decomposition Searches for the next redex, if there is one, and its context Contraction Contracts a redex to a contractum Recomposition Reconstructs a term from a contractum and the context Hygiene We maintain hygiene explicitly (e.g., a variable convention)

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 14 / 18

slide-36
SLIDE 36

The reduction semantics

One-step reduction Decompose, contract and recompose Reduction-based evaluation Iterate one-step reduction

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 15 / 18

slide-37
SLIDE 37

The syntactic correspondence

Reduction semantics Abstract machine

Danvy and Nielsen’s refocusing

  • inlining
  • Ohori and Sasano’s lightweight fusion
  • transition compression
  • Olivier Danvy (danvy@cs.au.dk)

Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 16 / 18

slide-38
SLIDE 38

The syntactic correspondence

Reduction semantics Abstract machine

Danvy and Nielsen’s refocusing

  • inlining
  • Ohori and Sasano’s lightweight fusion
  • transition compression
  • Result: the simplest storeless abstract machine

for call-by-need evaluation (see paper).

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 16 / 18

slide-39
SLIDE 39

The functional correspondence

Kudos for John Reynolds: Evaluator Abstract machine

closure conversion

  • CPS transformation
  • defunctionalization
  • Olivier Danvy (danvy@cs.au.dk)

Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 17 / 18

slide-40
SLIDE 40

The functional correspondence

Kudos for John Reynolds: Evaluator Abstract machine

closure conversion

  • CPS transformation
  • defunctionalization
  • Result: the first storeless natural semantics

for call-by-need evaluation (see paper).

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 17 / 18

slide-41
SLIDE 41

Our results

◮ Identify what is common to Ariola et al.:

the standard reduction

◮ Calculate the corresponding abstract machine ◮ Calculate the corresponding natural semantics ◮ And also: calculate the corresponding

extended direct semantics

◮ A denotational semantics can be constructed

with a control monad or a state monad

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 18 / 18

slide-42
SLIDE 42

Our results

◮ Identify what is common to Ariola et al.:

the standard reduction

◮ Calculate the corresponding abstract machine ◮ Calculate the corresponding natural semantics ◮ And also: calculate the corresponding

extended direct semantics

◮ A denotational semantics can be constructed

with a control monad or a state monad Thank you.

Olivier Danvy (danvy@cs.au.dk) Defunctionalized Interpreters for Call-by-Need Evaluation FLOPS’10 18 / 18