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

defunctionalized interpreters for call by need
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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

slide-2
SLIDE 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

slide-3
SLIDE 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

slide-4
SLIDE 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

slide-5
SLIDE 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

slide-6
SLIDE 6

Axioms

(λx.T) T1 → let x be T1 in T

let x be λx.T in E[x] → let x be λx.T in E[λx.T]

(let x be T1 in A) T2 → let x be T1 in A T2

let x2 be let x1 be T in A in E[x2]

→ let x1 be T

in let x2 be A in E[x2]

6

slide-7
SLIDE 7

In practice

...too hard to test!

7

slide-8
SLIDE 8

La mˆ eme chose, with integers

Syntax:

T ::= n | succ T | x | . . . A ::= n | λx.T | let x be T in A E ::= [ ] | succ E | E T | . . .

8

slide-9
SLIDE 9

La mˆ eme chose, with integers

Three extra axioms:

succ n → n′

where n′ = n + 1 let x be

n in E[x] → let x be n in E[ n ] succ (let x be T in A) → let x be T in succ A

9

slide-10
SLIDE 10

Some exegesis

  • 1. The potential redexes
  • 2. Barendregt’s variable convention
  • 3. The evaluation contexts

10

slide-11
SLIDE 11
  • 1. The potential redexes

A helpful grammar:

R ::= succ A | A T | let x be A in E[x]

where

A ::= n | λx.T | let x be T in A

11

slide-12
SLIDE 12
  • 2. Barendregt’s variable convention (1/3)

It is assumed, e.g., in

(let x be T1 in A) T2 → let x be T1 in A T2

let x be λx.T in E[x] → let x be λx.T in E[λx.T]

12

slide-13
SLIDE 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

slide-14
SLIDE 14
  • 2. Barendregt’s variable convention (3/3)

Simple fix: let x be λx.T in E[x]

→ let x be λx.T

in E[λx′.T ′] where λx′.T ′ = freshen up(λx.T)

14

slide-15
SLIDE 15
  • 3. The evaluation contexts

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

15

slide-16
SLIDE 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

slide-17
SLIDE 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

slide-18
SLIDE 18

Contexts as lists of frames

F ::= succ | T |

let x be in Coi[x] | let x be T in

Coi ::= • | F ◦ Coi Cio ::= • | F ◦ Cio

18

slide-19
SLIDE 19

Recomposition of outside-in contexts

  • , Toi ↑rec T

Coi, Toi ↑rec T0 ( T1) ◦ Coi, Toi ↑rec T0 T1 . . .

19

slide-20
SLIDE 20

Recomposition of inside-out contexts

  • , Tio ↑rec T

( T1) ◦ Cio, Tio ↑rec Cio, T T1io . . .

20

slide-21
SLIDE 21

Decomposition

A convenient format: as a transition system. Accepting states: T, Cioterm

Cio, Acontext Cio, (Coi, x)reroot

Final states: Aanswer

R, Ciodecomposition

21

slide-22
SLIDE 22

One-step reduction

T →let T ′ if            T, •term ↓∗

dec R, Ciodecomposition

(R, R′) ∈ ...the axioms... Cio, R′io ↑∗

rec T ′

22

slide-23
SLIDE 23

Reduction-based evaluation

T →∗

let A

23

slide-24
SLIDE 24

Good news

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

24

slide-25
SLIDE 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

slide-26
SLIDE 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

slide-27
SLIDE 27

Main results

  • A readable, hygienic abstract machine.
  • A readable, hygienic natural semantics.

27

slide-28
SLIDE 28

Orthogonal issues

  • Adding a garbage-collection rule
  • Introducing a heap
  • Introducing a store

28

slide-29
SLIDE 29

Variants

Ensuring hygiene.

29

slide-30
SLIDE 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

slide-31
SLIDE 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