1 / 17

Background

Background. Verifying a compiler for a simple language with exceptions (MPC 04). Calculating an abstract machine that is correct by construction (TFP 05). This Talk. We show how to calculate a compiler for a simple language of arithmetic expressions;

Télécharger la présentation

Background

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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.

  3. 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

  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

  5. Sequencing: (;) :: an a1+m an+m Example (n = 3, m = 2): g f ; g = f

  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 Only uses the powers 0,1,2. return :: Int  Int0 return n = n add :: Int2 add = λn m  m+n

  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.

  8. 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

  9. = 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)) 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))

  10. 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.

  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.

  12. New semantics: eval :: Expr Term0 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.

  13. New datatype: 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

  14. Example Add (Val 1) (Add (Val 2) (Val 3)) eval Return 1 $ Return 2 $ Return 3 $ Add $ Add $ Halt exec 6

  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.

  16. Ongoing and Further Work • Using an explicit stack type; • Other control structures; • Relationship to other approaches; • Further exploiting operads.

More Related