Top Banner
CALCULATING CORRECT COMPILERS Graham Hutton and Patrick Bahr
30

How To Make Sausages

Jan 02, 2016

Download

Documents

Jane Cunningham

How To Make Sausages. How To Make Compilers?. language. compiler. This Talk. A new approach to the problem of calculating compilers from high-level semantics; Only requires simple techniques, and all the calculations have been formalised in Coq; - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: How To Make Sausages

CALCULATINGCORRECT COMPILERS

Graham Hutton and Patrick Bahr

Page 2: How To Make Sausages

2

How To Make Sausages

Page 3: How To Make Sausages

3

How To Make Compilers?

language compiler

Page 4: How To Make Sausages

4

This Talk

A new approach to the problem of calculating compilers from high-level semantics;

Only requires simple techniques, and all the calculations have been formalised in Coq;

Scales to exceptions, state, variable binding, loops, non-determinism, interrupts, etc.

Page 5: How To Make Sausages

5

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:

Page 6: How To Make Sausages

Example

6

1 + (2 + 3)

Add (Val 1) (Add (Val 2) (Val 3))

6

Page 7: How To Make Sausages

7

Step 1 – Stacks

Aim: define a new semantics

evalS :: Expr Stack Stack

evalS e s = eval e : s

such that

Make the manipulation of arguments explicit by transforming the semantics to use a stack.

Stack = [Int]

Page 8: How To Make Sausages

8

evalS (Add x y) s

Case for addition:

eval (Add x y) : s

=

(eval x + eval y) : s=

add (evalS y (evalS x s))=

add (eval y : evalS x s)=

add (eval y : eval x : s)=

add (n:m:s) = m+n : s

Page 9: How To Make Sausages

9

New semantics:

evalS :: Expr Stack Stack

evalS (Val n) s = push n s

evalS (Add x y) s = add (evalS y (evalS x s))

Stack operations:

push n s = n : s

add (n:m:s) = m+n : s

Page 10: How To Make Sausages

10

Step 2 – 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.

Page 11: How To Make Sausages

11

Aim: define a new semantics

evalC e c s = c (evalS e s)

such that

evalC :: Expr Cont Cont

Cont = Stack Stack

Page 12: How To Make Sausages

12

New semantics:

evalC :: Expr Cont Cont

evalC (Val n) c s = c (push n s)

evalC (Add x y) c s = evalC x (evalC y (c . add)) s

Previous semantics:

evalS :: Expr Stack Stack

evalS e = evalC e (λs s)

Page 13: How To Make Sausages

13

Step 3 - Defunctionalise

Basic idea:

Represent the continuations that we actually need using a

datatype.

Make the semantics first-order again by applying the technique of defunctionalisation.

Page 14: How To Make Sausages

14

comp :: Expr Code

comp e = comp’ e HALT

New semantics:

comp’ :: Expr Code Code

comp’ (Val n) c = PUSH n c

comp’ (Add x y) c = comp’ x (comp’ y (ADD c))

A compiler for arithmetic expressions!

Page 15: How To Make Sausages

15

1 + (2 + 3)

Example

PUSH 1PUSH 2PUSH 3ADD ADDHALT

Page 16: How To Make Sausages

16

data Code = PUSH Int Code | ADD Code | HALT

New datatype and its interpretation:

exec :: Code Stack Stack

exec (PUSH n c) s = exec c (n:s)

exec (ADD c) (n:m:s) = exec c (m+n : s)

exec HALT s = s

A virtual machine for arithmetic expressions!

Page 17: How To Make Sausages

17

1 + (2 + 3)

Example

PUSH 1PUSH 2PUSH 3ADD ADDHALT

Stack

Code

Page 18: How To Make Sausages

18

1 + (2 + 3)

Example

PUSH 2PUSH 3ADD ADDHALT

Stack

Code

1

Page 19: How To Make Sausages

19

1 + (2 + 3)

Example

PUSH 3ADD ADDHALT

Stack

Code

21

Page 20: How To Make Sausages

20

1 + (2 + 3)

Example

ADD ADDHALT

Stack

Code

321

Page 21: How To Make Sausages

21

1 + (2 + 3)

Example

ADDHALT

Stack

Code

51

Page 22: How To Make Sausages

22

1 + (2 + 3)

Example

HALT

Stack

Code

6

Page 23: How To Make Sausages

23

1 + (2 + 3)

Example

Stack

Code

6

Page 24: How To Make Sausages

Compiler Correctness

24

Is captured by the following two equations:

These follow from defunctionalisation, or can be

verified by simple inductive proofs.

exec c (eval e : s)

exec (comp e) s eval e : s=

exec (comp’ e c) s =

Page 25: How To Make Sausages

25

Reflection

We now have a three step process for calculating a correct compiler from a high-level semantics:

Can the steps be combined?

1 - Add a stack2 - Add a continuation3 - Remove the continuations

Page 26: How To Make Sausages

The Trick

26

Start directly with the correctness equations:

Aim to calculate definitions for comp, comp’, exec and Code that satisfy

these equations.

exec c (eval e : s)

exec (comp e) s eval e : s=

exec (comp’ e c) s =

Page 27: How To Make Sausages

27

In Practice

Calculating four 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 has the same structure as our stepwise version.

Page 28: How To Make Sausages

28

Summary

Purely calculational approach to developing compilers that are correct by construction;

Only requires simple techniques, and scales to a wide variety of language features;

More sophisticated languages also introduce the idea of using partial specifications.

Page 29: How To Make Sausages

29

Further Work

Register-based machines;

Real source/target languages;

Mechanical assistance;

EPSRC application.

Page 30: How To Make Sausages

CALCULATINGCORRECT COMPILERS

Graham Hutton and Patrick Bahr