defunctionalized interpreters for call by need
play

Defunctionalized Interpreters for Call-by-Need Programming Languages - PowerPoint PPT Presentation

Defunctionalized Interpreters for Call-by-Need Programming Languages a functional pearl with hygiene Olivier Danvy (Aarhus University) Kevin Millikin (Google) Johan Munk (Arctic Lake Systems) IFIP WG 2.8 12 June 2009 1 The


  1. Defunctionalized Interpreters for Call-by-Need Programming Languages — a functional pearl with hygiene — Olivier Danvy (Aarhus University) Kevin Millikin (Google) Johan Munk (Arctic Lake Systems) IFIP WG 2.8 12 June 2009 1

  2. The contributions • A hygienic standard call-by-need reduction for the λ -calculus. • The notion of explicit evaluation contexts. • Towards an abstract machine and a natural semantics for call by need through refocusing, refunctionalization, etc. 2

  3. The starting point The standard call-by-need reduction of • Ariola and Felleisen, 1997 JFP 7(3):265-301 • Maraist, Odersky and Wadler, 1998 JFP 8(3):275-317 3

  4. The starting point The standard call-by-need reduction of • Ariola and Felleisen, 1997 JFP 7(3):265-301 • Maraist, Odersky and Wadler, 1998 JFP 8(3):275-317 The goal: to extract a computational content. 4

  5. Syntax T ::= x | λx.T | T T | let x be T in T A ::= λx.T | let x be T in A E ::= [ ] | E T | let x be T in E | let x be E in E [ x ] 5

  6. Axioms ( λx.T ) T 1 → let x be T 1 in T let x be λx.T in E [ x ] → let x be λx.T in E [ λx.T ] ( let x be T 1 in A ) T 2 → let x be T 1 in A T 2 let x 2 be let x 1 be T → let x 1 be T in A in let x 2 be A in E [ x 2 ] in E [ x 2 ] 6

  7. In practice ...too hard to test! 7

  8. La mˆ eme chose, with integers Syntax: T ::= � � | succ T | x | . . . n A ::= � � | λx.T | let x be T in A n E ::= [ ] | succ E | E T | . . . 8

  9. La mˆ eme chose, with integers Three extra axioms: n ′ � succ � n � → � where n ′ = n + 1 � in E [ x ] → let x be � � in E [ � � ] let x be � n n n succ ( let x be T in A ) → let x be T in succ A 9

  10. Some exegesis 1. The potential redexes 2. Barendregt’s variable convention 3. The evaluation contexts 10

  11. 1. The potential redexes A helpful grammar: R ::= succ A | A T | let x be A in E [ x ] where A ::= � � | λx.T | let x be T in A n 11

  12. 2. Barendregt’s variable convention (1/3) It is assumed, e.g., in ( let x be T 1 in A ) T 2 → let x be T 1 in A T 2 let x be λx.T in E [ x ] → let x be λx.T in E [ λx.T ] 12

  13. 2. Barendregt’s variable convention (2/3) One axiom, however, yields terms that do not satisfy the convention: let x be λx.T in E [ x ] → let x be λx.T in E [ λx.T ] 13

  14. 2. Barendregt’s variable convention (3/3) Simple fix: let x be λx.T → let x be λx.T in E [ x ] in E [ λx ′ .T ′ ] where λx ′ .T ′ = freshen up ( λx.T ) 14

  15. 3. The evaluation contexts The grammar of contexts is unusual because it includes identifiers within (delimited) contexts. 15

  16. 3. The evaluation contexts The grammar of contexts is unusual because it includes identifiers within (delimited) contexts. • These contexts are constructed outside in. • All the others are constructed inside out. 16

  17. Towards explicit evaluation contexts Analogy with explicit substitutions: delay the actual substitution. Here: delay the recomposition, i.e., keep E instead of having λx.E [ x ] . Joint work with Kristoffer Rose 17

  18. Contexts as lists of frames ::= succ � | F � T | let x be � in C oi [ x ] | let x be T in � C oi ::= • | F ◦ C oi C io ::= • | F ◦ C io 18

  19. Recomposition of outside-in contexts �• , T � oi ↑ rec T � C oi , T � oi ↑ rec T 0 � ( � T 1 ) ◦ C oi , T � oi ↑ rec T 0 T 1 . . . 19

  20. Recomposition of inside-out contexts �• , T � io ↑ rec T � ( � T 1 ) ◦ C io , T � io ↑ rec � C io , T T 1 � io . . . 20

  21. Decomposition A convenient format: as a transition system. Accepting states: � T, C io � term � C io , A � context � C io , ( C oi , x ) � reroot Final states: � A � answer � R, C io � decomposition 21

  22. One-step reduction   � T, •� term ↓ ∗ dec � R, C io � decomposition     T � → let T ′ if ( R, R ′ ) ∈ ...the axioms...      � C io , R ′ � io ↑ ∗ rec T ′ 22

  23. Reduction-based evaluation T � → ∗ let A 23

  24. Good news The rest is (essentially) mechanical. Reference: Defunctionalized Interpreters for Programming Languages, ICFP’08. 24

  25. The syntactic correspondence • Refocusing: from reduction semantics to small-step abstract machine • Lightweight fusion: from small-step abstract machine to big-step abstract machine • Transition compression: from big-step abstract machine to big-step abstract machine 25

  26. The functional correspondence • Refunctionalization: from abstract machine to continuation-passing interpreter • Back to direct style: from continuation-passing interpreter to first-order natural semantics • Refunctionalization: from first-order natural semantics to higher-order natural semantics 26

  27. Main results • A readable, hygienic abstract machine. • A readable, hygienic natural semantics. 27

  28. Orthogonal issues • Adding a garbage-collection rule • Introducing a heap • Introducing a store 28

  29. Variants Ensuring hygiene. 29

  30. Latest news More aggressive transition compression (using a global invariant) makes outside-in contexts unnecessary. Good news for Simon’s head: a continuation-free account of lazy evaluation. Work in progress. 30

  31. Conclusion • The standard call-by-need reduction of the lambda-calculus, plus hygiene, can be uniformly mirrored into an abstract machine and a natural semantics that make sense. • Further transition compression leads to a continuation-free account of call by need. Thank you. 31

Recommend


More recommend