calculating correct compilers

Calculating Correct Compilers Patrick Bahr 1 Graham Hutton 2 1 - PowerPoint PPT Presentation

u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Faculty of Science Calculating Correct Compilers Patrick Bahr 1 Graham Hutton 2 1 University of Copenhagen, Department of Computer Science


  1. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Faculty of Science Calculating Correct Compilers Patrick Bahr 1 Graham Hutton 2 1 University of Copenhagen, Department of Computer Science paba@diku.dk 2 University of Nottingham, Functional Programming Laboratory graham.hutton@nottingham.ac.uk 10th January, 2014 Slide 1

  2. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Introduction Goals • Derive compiler implementation from denotational semantics • Derivation by formal calculations Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 2

  3. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Introduction Goals • Derive compiler implementation from denotational semantics • Derivation by formal calculations • Result: compiler + virtual machine + correctness proof Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 2

  4. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Introduction Goals • Derive compiler implementation from denotational semantics • Derivation by formal calculations • Result: compiler + virtual machine + correctness proof Our approach • simple, goal-oriented calculations • little prior knowledge needed (e.g. “Target machine has a stack.”) • full correctness proof as a byproduct • wide variety of language features: arithmetic, exceptions, state, lambda calculi, loops, non-determinism, interrupts Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 2

  5. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Calculate a Compiler in 3 Steps 1 Define evaluation function in compositional manner. Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 3

  6. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Calculate a Compiler in 3 Steps 1 Define evaluation function in compositional manner. 2 Calculate a version that uses a stack and continuations. Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 3

  7. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Calculate a Compiler in 3 Steps 1 Define evaluation function in compositional manner. 2 Calculate a version that uses a stack and continuations. 3 Defunctionalise to produce a compiler and a virtual machine. Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 3

  8. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Toy Example: Simple Arithmetic Language Step 1: Semantics of the language Syntax data Expr = Val Int | Add Expr Expr Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 4

  9. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Toy Example: Simple Arithmetic Language Step 1: Semantics of the language Syntax data Expr = Val Int | Add Expr Expr Semantics eval :: Expr → Int eval ( Val n ) = n eval ( Add x y ) = eval x + eval y Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 4

  10. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Step 2: Transformation into CPS Type Definitions type Stack = [ Int ] type Cont = Stack → Stack Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 5

  11. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Step 2: Transformation into CPS Type Definitions type Stack = [ Int ] type Cont = Stack → Stack eval C :: Expr → Cont → Cont Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 5

  12. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Step 2: Transformation into CPS Type Definitions type Stack = [ Int ] type Cont = Stack → Stack eval C :: Expr → Cont → Cont Specification eval C e c s = c ( eval e : s ) Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 5

  13. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Step 2: Transformation into CPS Type Definitions type Stack = [ Int ] type Cont = Stack → Stack eval C :: Expr → Cont → Cont Specification eval C e c s = c ( eval e : s ) Constructive induction: “prove” specification by induction on e Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 5

  14. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e Step 2: Transformation into CPS Type Definitions type Stack = [ Int ] type Cont = Stack → Stack eval C :: Expr → Cont → Cont Specification eval C e c s = c ( eval e : s ) Constructive induction: “prove” specification by induction on e � definition of eval C Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 5

  15. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e The easy case: Val eval C ( Val n ) c s Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 6

  16. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e The easy case: Val eval C ( Val n ) c s = { specification of eval C } c ( eval ( Val n ) : s ) Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 6

  17. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e The easy case: Val eval C e c s = c ( eval e : s ) eval C ( Val n ) c s = { specification of eval C } c ( eval ( Val n ) : s ) Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 6

  18. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e The easy case: Val eval C ( Val n ) c s = { specification of eval C } c ( eval ( Val n ) : s ) = { definition of eval } c ( n : s ) Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 6

  19. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e The easy case: Val eval C ( Val n ) c s = { specification of eval C } eval ( Val n ) = n c ( eval ( Val n ) : s ) = { definition of eval } c ( n : s ) Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 6

  20. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e The easy case: Val eval C ( Val n ) c s = { specification of eval C } c ( eval ( Val n ) : s ) = { definition of eval } c ( n : s ) = { define: push n c s = c ( n : s ) } push n c s Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 6

  21. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e The interesting case: Add eval C ( Add x y ) c s Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 7

  22. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e The interesting case: Add eval C ( Add x y ) c s = { specification of eval C } c ( eval ( Add x y ) : s ) Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 7

  23. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e The interesting case: Add eval C e c s = c ( eval e : s ) eval C ( Add x y ) c s = { specification of eval C } c ( eval ( Add x y ) : s ) Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 7

  24. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e The interesting case: Add eval C ( Add x y ) c s = { specification of eval C } c ( eval ( Add x y ) : s ) = { definition of eval } c (( eval x + eval y ) : s ) Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 7

  25. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e The interesting case: Add eval C ( Add x y ) c s = { specification of eval C } eval ( Add x y ) = eval x + eval y c ( eval ( Add x y ) : s ) = { definition of eval } c (( eval x + eval y ) : s ) Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 7

  26. u n i v e r s i t y o f c o p e n h a g e n d e p a r t m e n t o f c o m p u t e r s c i e n c e The interesting case: Add Induction Hypothesis For all c ′ and s ′ : eval C ( Add x y ) c s eval C x c ′ s ′ = c ′ ( eval x : s ′ ) = { specification of eval C } eval C y c ′ s ′ = c ′ ( eval y : s ′ ) c ( eval ( Add x y ) : s ) = { definition of eval } c (( eval x + eval y ) : s ) Patrick Bahr, Graham Hutton — Calculating Correct Compilers — 10th January, 2014 Slide 7

Recommend


More recommend