Download presentation
Presentation is loading. Please wait.
Published byYandi Johan Modified over 6 years ago
2
Verifying a compiler for a simple language with exceptions (MPC 04).
Background Verifying a compiler for a simple language with exceptions (MPC 04). Calculating an abstract machine that is correct by construction (TFP 05).
3
This Talk We show how to calculate a compiler for a simple language of arithmetic expressions; Our new approach builds upon and refines earlier work by Wand and Danvy. We make essential use of dependent types, and our work is formalised in Agda.
4
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
5
Step 1 - Sequencing Rewrite the semantics in combinatory form using a special purpose sequencing operator. Raising a type to a power: a3 a a a a = 3 arguments
6
Sequencing: Example (n = 3, m = 2): = (;) :: an a1+m an+m g f ; g
7
We can now rewrite the semantics:
eval :: Expr Int0 eval (Val n) = return n eval (Add x y) = eval x ; (eval y ; add) where Only uses the powers 0,1,2. return :: Int Int0 return n = n add :: Int2 add = λn m m+n
8
Step 2 - Continuations Generalise the semantics to arbitrary powers by making the use of continuations explicit. Definition: A continuation is a function that is applied to the result of another computation.
9
Aim: define a new semantics
eval’ :: Expr Int1+m Intm such that eval’ e c = eval e ; c and hence halt = λn n eval e = eval’ e halt
10
Case: e = Add x y = = = = = eval’ (Add x y) c eval (Add x y) ; c
(eval x ; (eval y ; add)) ; c = (eval x ; (eval y ; add)) ; c eval x ; (eval y ; (add ; c)) = eval x ; (eval y ; (add ; c)) eval’ x (eval y ; (add ; c)) = eval’ x (eval y ; (add ; c)) eval’ x (eval’ y (add ; c)) =
11
The semantics now uses arbitrary powers.
New semantics: eval :: Expr Int0 eval e = eval’ e halt eval’ :: Expr Int1+m Intm eval’ (Val n) c = return n ; c eval’ (Add x y) c = eval’ x (eval’ y (add ; c)) The semantics now uses arbitrary powers.
12
Step 3 - Defunctionalize
Make the semantics first-order again, by applying the defunctionalization technique. Basic idea: Represent the functions of type Intn we actually need using a datatype Term n.
13
The semantics is now first-order again.
New semantics: eval :: Expr Term 0 eval e = eval’ e Halt eval’ :: Expr Term (1+m) Term m eval’ (Val n) c = Return n c eval’ (Add x y) c = eval’ x (eval’ y (Add c)) The semantics is now first-order again.
14
New datatype: Interpretation: data Term n where Halt :: Term 1
Return :: Int Term (1+n) Term n Add :: Term (1+n) Term (2+n) Interpretation: exec :: Term n -> Intn exec Halt = halt exec (Return n c) = return n ; exec c exec (Add c) = add ; exec c
15
Example Add (Val 1) (Add (Val 2) (Val 3)) Return 1 $ Return 2 $
eval Return 1 $ Return 2 $ Return 3 $ Add $ Halt exec 6
16
Summary Purely calculational development of a compiler for simple arithmetic expressions; Use of dependent types arises naturally during the process to keep track of stack usage; Technique has also been used to calculate a compiler for a language with exceptions.
17
Ongoing and Further Work
Using an explicit stack type; Other control structures; Relationship to other approaches; Further exploiting operads.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.