CALCULA ULATING NG A COM OMPILE LER Graham Hutton and Nils - - PowerPoint PPT Presentation

calcula ulating ng a com ompile ler
SMART_READER_LITE
LIVE PREVIEW

CALCULA ULATING NG A COM OMPILE LER Graham Hutton and Nils - - PowerPoint PPT Presentation

CALCULA ULATING NG A COM OMPILE LER Graham Hutton and Nils Anders Danielsson Background Verifying a compiler for a simple language with exceptions (MPC 04). Calculating an abstract machine that is correct by construction (TFP 05). 1


slide-1
SLIDE 1

CALCULA ULATING NG A COM OMPILE LER

Graham Hutton and Nils Anders Danielsson

slide-2
SLIDE 2

1

Background

Verifying a compiler for a simple language with exceptions (MPC 04). Calculating an abstract machine that is correct by construction (TFP 05).

slide-3
SLIDE 3

2

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.

slide-4
SLIDE 4

3

Arithmetic Expressions

data Expr = Val Int | Add Expr Expr eval :: Expr → Int eval (Val n) = n eval (Add x y) = eval x + eval y

Syntax: Semantics:

slide-5
SLIDE 5

4

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

slide-6
SLIDE 6

5

Sequencing: Example (n = 3, m = 2):

(;) :: an → a1+m → an+m f ; g

=

f g

slide-7
SLIDE 7

6

We can now rewrite the semantics:

eval :: Expr → Int0 eval (Val n) = return n eval (Add x y) = eval x ; (eval y ; add)

where

return :: Int → Int0 return n = n add :: Int2 add = λn m → m+n

Only uses the powers 0,1,2.

slide-8
SLIDE 8

7

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.

slide-9
SLIDE 9

8

Aim: define a new semantics and hence

eval’ :: Expr → Int1+m → Intm eval e = eval’ e halt eval’ e c = eval e ; c

such that halt = λn → n

slide-10
SLIDE 10

9

eval’ (Add x y) c

Case: e = Add x y

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 (Add x y) ; c (eval x ; (eval y ; add)) ; c eval’ x (eval y ; (add ; c)) eval x ; (eval y ; (add ; c))

slide-11
SLIDE 11

10

New semantics: The semantics now uses arbitrary powers.

eval’ :: Expr → Int1+m → Intm eval’ (Val n) c = return n ; c eval’ (Add x y) c = eval’ x (eval’ y (add ; c)) eval :: Expr → Int0 eval e = eval’ e halt

slide-12
SLIDE 12

11

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.

slide-13
SLIDE 13

12

New semantics:

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)) eval :: Expr → Term 0 eval e = eval’ e Halt

The semantics is now first-order again.

slide-14
SLIDE 14

13

data Term n where Halt :: Term 1 Return :: Int → Term (1+n) → Term n Add :: Term (1+n) → Term (2+n)

New datatype: Interpretation:

exec :: Term n -> Intn exec Halt = halt exec (Return n c) = return n ; exec c exec (Add c) = add ; exec c

slide-15
SLIDE 15

14

Example

Add (Val 1) (Add (Val 2) (Val 3)) Return 1 $ Return 2 $ Return 3 $ Add $ Add $ Halt 6

eval exec

slide-16
SLIDE 16

15

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.

slide-17
SLIDE 17

16

Ongoing and Further Work

 Using an explicit stack type;  Other control structures;  Relationship to other approaches;  Further exploiting operads.