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 IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 Goal Calculate a compiler that is correct by construction Patrick Bahr, Graham Hutton — Calculating Correct Compilers — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 Goal Calculate a compiler that is correct by construction: • Derive compiler implementation from denotational semantics • Derivation by formal calculations Patrick Bahr, Graham Hutton — Calculating Correct Compilers — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 Goal Calculate a compiler that is correct by construction: • Derive compiler implementation from denotational semantics • Derivation by formal calculations • Result: compiler + virtual machine + correctness proof Patrick Bahr, Graham Hutton — Calculating Correct Compilers — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 Background Reasoning about compilers, Hutton & Wright • Verifying a compiler for a simple language with exceptions (MPC ’04) • Calculating an abstract machine that is correct by construction (TFP ’05) Patrick Bahr, Graham Hutton — Calculating Correct Compilers — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 Background Reasoning about compilers, Hutton & Wright • Verifying a compiler for a simple language with exceptions (MPC ’04) • Calculating an abstract machine that is correct by construction (TFP ’05) Last 2.1 meeting, Hutton & Danielsson • Calculating a compiler for a simple language with exceptions • Use of dependent types during the calculation Patrick Bahr, Graham Hutton — Calculating Correct Compilers — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 This Talk: A Simplified Approach • simple calculations without the need for dependent types • little prior knowledge needed (e.g. “Target machine has a stack.”) • scales to wide variety of language features Patrick Bahr, Graham Hutton — Calculating Correct Compilers — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 This Talk: A Simplified Approach • simple calculations without the need for dependent types • little prior knowledge needed (e.g. “Target machine has a stack.”) • scales to wide variety of language features: • arithmetic expressions • exceptions (synchronous and asynchronous) • state (global and local) • lambda calculi (call-by-value, call-by-name, call-by-need) • loops (bounded and unbounded) • non-determinism Patrick Bahr, Graham Hutton — Calculating Correct Compilers — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 This Talk: A Simplified Approach • simple calculations without the need for dependent types • little prior knowledge needed (e.g. “Target machine has a stack.”) • scales to wide variety of language features: • arithmetic expressions • exceptions (synchronous and asynchronous) • state (global and local) • lambda calculi (call-by-value, call-by-name, call-by-need) • loops (bounded and unbounded) • non-determinism • Underlying techniques: continuation-passing style & defunctionalisation (Reynolds, 1972) Patrick Bahr, Graham Hutton — Calculating Correct Compilers — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 How Does it Work? Calculate a Compiler in 3 Steps: Semantics 1 Define evaluation function in compositional manner. CPS + Stack Compiler VM Patrick Bahr, Graham Hutton — Calculating Correct Compilers — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 How Does it Work? Calculate a Compiler in 3 Steps: Semantics 1 Define evaluation function in compositional manner. 2 Calculate a version that uses a stack CPS + Stack and continuations. Compiler VM Patrick Bahr, Graham Hutton — Calculating Correct Compilers — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 How Does it Work? Calculate a Compiler in 3 Steps: Semantics 1 Define evaluation function in compositional manner. 2 Calculate a version that uses a stack CPS + Stack and continuations. 3 Defunctionalise to produce a compiler Compiler VM & virtual machine. Patrick Bahr, Graham Hutton — Calculating Correct Compilers — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 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 — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 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 — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 Step 2: Transformation into CPS Type Definitions type Stack = [ Int ] type Cont = Stack → Stack Patrick Bahr, Graham Hutton — Calculating Correct Compilers — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 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 — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 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 — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 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 — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 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 — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 The easy case: Val eval C ( Val n ) c s Patrick Bahr, Graham Hutton — Calculating Correct Compilers — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 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 — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 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 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 — IFIP Working Group 2.1 Meeting, 24th-28th March, 2014 Slide 8
Recommend
More recommend