Download presentation
Presentation is loading. Please wait.
Published byΚλείτος Μιαούλης Modified over 6 years ago
2
Background In his classic 1972 paper on definitional interpreters, John Reynolds introduced two key techniques: Continuation-passing style Makes control flow explicit Defunctionalisation Makes programs first-order
3
Background These techniques are often used together:
language compiler CPS DEFUN Can the two steps be fused together !? Introduce continuations Remove continuations
4
This Talk A new approach to transforming high-level semantics into low-level implementations; Only requires simple calculation techniques, and avoids the use of continuations; Scales to exceptions, state, variable binding, loops, non-determinism, interrupts, etc.
5
Arithmetic Expressions
Syntax: data Expr = Val Int | Add Expr Expr Semantics: eval :: Expr Int eval (Val n) = n eval (Add x y) = eval x + eval y
6
Step 1 – Add Continuations
Make the flow of control explicit by transforming the semantics into continuation-passing style. Definition: A continuation is a function that is applied to the result of another computation.
7
Specification Aim: define a new semantics such that
eval’ :: Expr Cont Int Cont = Int Int such that eval’ e c = c (eval e)
8
Control flow now explicit.
New semantics: eval’ :: Expr Cont Int eval’ (Val n) c = c n eval’ (Add x y) c = eval’ x (n eval’ y (m c (n + m))) Original semantics: Control flow now explicit. eval e = eval’ e (n n)
9
Step 2 - Defunctionalise
Make the semantics first-order again by applying the technique of defunctionalisation. Basic idea: Represent the continuations that we actually need using a datatype.
10
Specification Aim: define a new semantics such that
eval’’ :: Expr CONT Int such that exec :: CONT Cont eval’’ e c = eval’ e (exec c)
11
An abstract machine for evaluating expressions.
New semantics: eval’’ :: Expr CONT Int eval’’ (Val n) c = exec c n eval’’ (Add x y) c = eval’’ x (NEXT y c) exec :: CONT Int Int exec (NEXT y c) n = eval’’ y (ADD n c) exec (ADD n c) m = exec c (n + m) exec HALT n = n An abstract machine for evaluating expressions.
12
Reflection We now have a two step process for calculating an abstract machine from a high-level semantics: 1 – Add a continuation 2 – Remove the continuations Can the steps be combined?
13
The Trick Start directly with the combined specification: =
eval’’ e c exec c (eval e) = Aim to calculate definitions for eval’’, exec and CONT that satisfy these equations.
14
In Practice Calculating three interlinked definitions at the same time seems like an impossible task; But... with experience gained from our stepwise approach, it turns out to be straightforward; New calculation is simpler, more direct, and eliminates the use of continuations.
15
Now we appear to be stuck, as no further definitions can be applied.
Case: e = Add x y eval’’ (Add x y) c exec c (eval (Add x y)) = exec c (eval x + eval y) = Now we appear to be stuck, as no further definitions can be applied. But we are using induction, so we have induction hypotheses!
16
We start by generalising to:
To use the hypothesis for y, we need a term of form exec c’ (eval y) for some c’, i.e. we must solve: exec c (eval x + eval y) exec c’ (eval y) = We start by generalising to: exec c (n + m) exec c’ m = But we can’t simply use this as a definition for exec, because c and n are unbound.
17
Solution Package the free variables up in the argument c’ by adding a new constructor to the CONT type, ADD :: Int CONT CONT and a new equation for the exec function: exec (ADD n c) m = exec c (n + m)
18
Now we can continue: = = = exec c (eval x + eval y)
exec (ADD (eval x) c) (eval y) = eval’’ y (ADD (eval x) c) = . =
19
Summary The rest of the calculation of the abstract machine proceeds in a similar manner; The driving force for the calculation is the desire to apply induction hypotheses; We solve the resulting equations by adding new constructors to CONT on demand.
20
Final Result data CONT where NEXT :: Expr CONT CONT
ADD :: Int CONT CONT HALT :: CONT eval’’ (Val n) c = exec c n eval’’ (Add x y) c = eval’’ x (NEXT y c) exec (NEXT y c) n = eval’’ y (ADD n c) exec (ADD n c) m = exec c (n + m) exec HALT n = n
21
Conclusion Purely calculational approach to cutting out the intermediate use of continuations; Only requires simple techniques, and scales to a wide variety of language features; Can also be used to calculate compilers, and everything has been formalised in Coq.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.