A Functional Correspondence between Evaluators and Abstract Machines - - PowerPoint PPT Presentation

a functional correspondence between evaluators and
SMART_READER_LITE
LIVE PREVIEW

A Functional Correspondence between Evaluators and Abstract Machines - - PowerPoint PPT Presentation

A Functional Correspondence between Evaluators and Abstract Machines Mads Sig Ager, Dariusz Biernacki, Olivier Danvy , and Jan Midtgaard BRICS, University of Aarhus, Denmark BRICS Nottingham, March 28, 2003 1 Our message A correspondence


slide-1
SLIDE 1

A Functional Correspondence between Evaluators and Abstract Machines

Mads Sig Ager, Dariusz Biernacki, Olivier Danvy , and Jan Midtgaard BRICS, University of Aarhus, Denmark

BRICS

Nottingham, March 28, 2003

1

slide-2
SLIDE 2

Our message

A correspondence between evaluators and abstract machines by

  • closure conversion,
  • CPS transformation, and
  • defunctionalization.

2

slide-3
SLIDE 3

Our results

  • Krivine’s machine,
  • the CEK machine and variants,
  • the CLS machine,
  • the SECD machine,
  • the Categorical Abstract Machine,

and more.

3

slide-4
SLIDE 4

My presentational problem

Continuations.

4

slide-5
SLIDE 5

This talk

A new attempt to explain continuations.

5

slide-6
SLIDE 6

BRICS

The Two Disguises of Continuations

Olivier Danvy and Lasse R. Nielsen BRICS, University of Aarhus, Denmark (danvy@brics.dk) Nottingham, March 28, 2003

6

slide-7
SLIDE 7

What are continuations?

Take 1: A functional representation

  • f the evaluation context.

7

slide-8
SLIDE 8

What are continuations?

Take 2: A functional representation

  • f the rest of the computation.

8

slide-9
SLIDE 9

Together

Take 1: A functional representation

  • f the evaluation context.

Take 2: A functional representation

  • f the rest of the computation.

9

slide-10
SLIDE 10

Goal of this talk

To reconcile these two views:

  • continuations as evaluation contexts, and
  • continuations as the rest of the computation.

10

slide-11
SLIDE 11

Tool: Syntactic Theories

A small-step operational semantics with an explicit representation

  • f the evaluation context.

Ideal to specify control operators.

11

slide-12
SLIDE 12
  • Notion of term, notion of value.
  • Evaluation contexts: decomposition, plugging.
  • Unique-decomposition lemma

(for deterministic languages).

  • Redexes and reduction rules.
  • One-step reduction:

decompose, contract, and plug.

12

slide-13
SLIDE 13

Syntactic theories: Quo Vadis?

  • A small-step operational semantics with

evaluation contexts.

  • Origin: Felleisen’s PhD thesis (1987).
  • Largely used since.
  • Practical challenges: evaluation contexts

and unique-decomposition lemma.

13

slide-14
SLIDE 14

A programming experiment (1/2)

Write a one-step reduction function:

exp → val + exp

  • Compositional, first order, direct style.
  • Recursive descent:

calls decompose, returns (re)construct.

14

slide-15
SLIDE 15

A programming experiment (2/2)

  • CPS-transform the one-step reducer:

exp × (val + exp → α) → α

  • Defunctionalize the continuation into:

– a data type, and – an apply function.

15

slide-16
SLIDE 16

And then a miracle happens

  • The data type is that of evaluation contexts.

(And in hindsight what else could it be?)

  • The apply function is the plug function.

16

slide-17
SLIDE 17

And then a miracle happens

  • The data type is that of evaluation contexts.

(And in hindsight what else could it be?)

  • The apply function is the plug function.

So continuations do represent evaluation contexts.

17

slide-18
SLIDE 18

Byproducts

  • Mechanically deriving a syntactic theory

from a one-step reduction function.

  • If the reduction function is compositional

then the unique-decomposition lemma is guaranteed to hold.

18

slide-19
SLIDE 19

Evaluation

Evaluation: The transitive closure

  • f one-step reduction.

(decompose, contract, plug)∗

19

slide-20
SLIDE 20

Deforestation

Replace decompose, contract, plug,

decompose, contract, plug, ...

by decompose, contract,

refocus, contract, refocus, contract, ...

20

slide-21
SLIDE 21

Reference

Olivier Danvy and Lasse R. Nielsen “Syntactic Theories in Practice” BRICS RS-02-04

21

slide-22
SLIDE 22

And then another miracle happens

The data type of evaluation contexts and the refocus function are in defunctionalized form.

22

slide-23
SLIDE 23

And then another miracle happens

The data type of evaluation contexts and the refocus function are in defunctionalized form. The corresponding higher-order program is a compositional evaluator in CPS .

23

slide-24
SLIDE 24

And then another miracle happens

The data type of evaluation contexts and the refocus function are in defunctionalized form. The corresponding higher-order program is a compositional evaluator in CPS . So continuations do represent the rest of the computation.

24

slide-25
SLIDE 25

Byproducts

  • Mechanically deriving a syntactic theory

from an evaluation function.

  • Mechanically deriving an abstract machine

from an evaluation function.

25

slide-26
SLIDE 26

Syntactic theories for the λ-calculus

  • 1. Pick your favorite monad.
  • 2. Write the corresponding functional evaluator

(by inlining the monad in an evaluator for the computational λ-calculus).

  • 3. Mechanically construct the corresponding

syntactic theory and its abstract machine.

26

slide-27
SLIDE 27

Conclusions

  • Continuations represent evaluation contexts

for one-step reduction.

  • Continuations represent the rest of the

computation for evaluation.

  • Syntactic theories can be constructed

from one-step reduction functions and from evaluation functions.

27