A Verified Compiler for an Impure Functional Language

Slides:



Advertisements
Similar presentations
Substitution & Evaluation Order cos 441 David Walker.
Advertisements

Static and User-Extensible Proof Checking Antonis StampoulisZhong Shao Yale University POPL 2012.
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Certified Typechecking in Foundational Certified Code Systems Susmit Sarkar Carnegie Mellon University.
Proofs and Programs Wei Hu 11/01/2007. Outline  Motivation  Theory  Lambda calculus  Curry-Howard Isomorphism  Dependent types  Practice  Coq Wei.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
1 Dependent Types for Termination Verification Hongwei Xi University of Cincinnati.
Formal Semantics of Programming Languages 虞慧群 Topic 5: Axiomatic Semantics.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Typed Assembly Languages COS 441, Fall 2004 Frances Spalding Based on slides from Dave Walker and Greg Morrisett.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Twelf: The Quintessential Proof Assistant for Language Metatheory Karl Crary Carnegie Mellon University Joint work with Robert Harper and Michael Ashley-Rollman.
1 A Dependently Typed Assembly Language Hongwei Xi University of Cincinnati and Robert Harper Carnegie Mellon University.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Catriel Beeri Pls/Winter 2004/5 inductive-revisited 1 Inductive definitions revisited  Generated and Freely generated sets oPattern match, unification.
Closure and Environment Compiler Baojian Hua
Midterm & Concept Review CS 510, Fall 2002 David Walker.
Have Your Verified Compiler And Extend It Too Zachary Tatlock Sorin Lerner UC San Diego.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 11: Compiler II: Code Generation slide 1www.idc.ac.il/tecs.
A Locally Nameless Theory of Objects 1.Introduction:  -calculus and De Bruijn notation 2.locally nameless technique 3.formalization in Isabelle and proofs.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
COMPILER CONSTRUCTION Lesson 1 – TDDD16 TDDB44 Compiler Construction 2010 Kristian Stavåker (Erik Hansson.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
Mechanizing Metatheory without Typing Contexts TYPES 2011 September 10, 2011 Jonghyun Park, Jeongbong Seo, Sungwoo Park, Gyesik Lee* Pohang University.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
Interpreters and Higher-Order Functions CSE 413 Autumn 2008 Credit: CSE341 notes by Dan Grossman.
A Mechanized Model of the Theory of Objects 1.Functional  -calculus in Isabelle 2.Confluence Proof in Isabelle 3.Ongoing Work, Applications, Conclusion.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Faithful mapping of model classes to mathematical structures Ádám Darvas ETH Zürich Switzerland Peter Müller Microsoft Research Redmond, WA, USA SAVCBS.
CSE-321 Programming Languages Abstract Machine E POSTECH May 1, 2006 박성우.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Cooperative Integration of an Interactive Proof Assistant and an Automated Prover Adam Chlipala and George C. Necula University of California, Berkeley.
1 A Certified Type-Preserving Compiler from Lambda Calculus to Assembly Language Adam Chlipala University of California, Berkeley An experiment with variable.
Mostly-Automated Verification of Low-Level Programs in Computational Separation Logic Adam Chlipala Harvard University PLDI 2011.
1 Interactive Computer Theorem Proving CS294-9 October 5, 2006 Adam Chlipala UC Berkeley Lecture 7: Programming with Proofs.
1 Interactive Computer Theorem Proving CS294-9 November 30, 2006 Adam Chlipala UC Berkeley Lecture 14: Twelf.
The Bedrock Structured Programming System Combining Generative Metaprogramming and Hoare Logic in an Extensible Program Verifier Adam Chlipala MIT CSAIL.
Engineering a Verified Functional Language Compiler Adam Chlipala Harvard University WMM 2009.
1 Parametric Higher-Order Abstract Syntax for Mechanized Semantics Adam Chlipala Harvard University ICFP 2008.
Generic Programming and Proving for Programming Language Metatheory
Type Checking and Type Inference
CSE-321 Programming Languages Simply Typed -Calculus
Compiler Construction (CS-636)
CSE341: Programming Languages Lecture 15 Macros
The DE Language Erik Reeber 6/30/04.
Intermediate code generation Jakub Yaghob
Corky Cartwright January 18, 2017
SSA in Scheme Static single assignment (SSA) :
CSE341: Programming Languages Lecture 15 Macros
Engineering Aspects of Formal Metatheory
Continuations and Compilation
CSE341: Programming Languages Lecture 12 Equivalence
This Lecture Substitution model
CSE341: Programming Languages Lecture 15 Macros
CSE341: Programming Languages Lecture 12 Equivalence
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
CSE341: Programming Languages Lecture 15 Macros
CSE341: Programming Languages Lecture 15 Macros
Intermediate Code Generation
CSE341: Programming Languages Lecture 12 Equivalence
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
This Lecture Substitution model
Programming Languages and Compilers (CS 421)
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 15 Macros
CSE341: Programming Languages Lecture 15 Macros
Presentation transcript:

A Verified Compiler for an Impure Functional Language Adam Chlipala Harvard University POPL 2010

What are the engineering principles that make compiler verification worth doing in the real world? In particular, for higher-order languages, which have tricky binder issues

From Mini-ML to Assembly Source language e ::= c | e = e | x | e e | fix f(x). e | let x = e in e | () | (e, e) | fst(e) | snd(e) | inl(e) | inr(e) | case e of inl(x) => e | inr(x) => e | ref(e) | !e | e := e | raise(e) | e handle x => e Target language Lvalues L ::= r | [r + n] | [n] Rvalues R ::= n | r | [r + n] | [n] Instructions I ::= L := R | L := R == R | r += n | jnz R, n Jumps J ::= halt | fail | jmp R Basic blocks B ::= (I*, J) Programs P ::= (B*, B)

Two Main Ideas It's possible to encode syntax and semantics in a way that avoids all auxiliary operations and lemmas about variables. Proofs about this encoding can be automated effectively enough that it is not hard to evolve a compiler and its proof over time.

Phase Structure ~7000 LoC (w/ proofs) ~2000 lines of proof Translations with interesting binding in both source and target languages ~7000 LoC (w/ proofs) ~2000 lines of proof First-order source The one and only inductive theorem about substitution Conversion to higher-order syntax Source CPS conversion CPS Closure conversion Common subexpression elimination Closed Flattening Register allocation/dead code elim. Three-address code Code generation Assembly

Overall Compiler Correctness 1 3 Data Layout Convention inr((), 3) Source Assembly Compiler Result Result Data Layout Convention

Operational Semantics (x. e) v [x/v]e compile([x/e2]e1) = [x/compile(e2)]compile(e1) To verify compile, need to prove:

No explicit substitution! Hiding Substitution? (x. x) 1 Encode (Higher-Order Abstract Syntax) App (Lam (fn x => x)) (Const 1) App (Lam f) v ⇒ f(v) No explicit substitution! Adding HOAS to general-purpose proof assistants creates unsoundness!

Closure Semantics n x. e1 Closure Heap x. e1 #n #n #m [x/#m]e1

Automating Proofs ... Theorem “By induction on....” Case Propositional simplification, partial evaluation, rewriting, ... Perform all useful inversions on hypotheses. Choose IHes to instantiate with unification variables. Finish with higher-order logic programming over rules of operational semantics and a few additional lemmas.

Lines of code added or changed to add new language features Proof Script Re-use Lines of code added or changed to add new language features Definitions Theorems & Proofs Time let 30 ½ hour Constants & = 150 10 ½ day fix 70 350 1 day Almost all has to do with a new binding pattern, not the semantics of fix.

Code available in the latest Lambda Tamer distribution: http://ltamer.sourceforge.net/

Backup Slides

Manipulating Binders let x = ... in let y = ... x ... in Which variables does the new expression mention? Are they available in scope? let x = ... in let y = ... x ... in let u = ... x ... in let z = ... x ... y ... in ... z ... u ... Does the new binding shadow a variable needed here?

De Bruijn Indices let x = ... in let y = ... 0 ... in Exactly which variables does this expression expect? let x = ... in let y = ... 0 ... in let u = ... 1 ... in let z = ... 1 ... 0 ... in ... 0 ... 2 1 1 ... Did we adjust this index properly?

Higher-Order Syntax let (...) (x. let (... x ...) (y. let (... x ...) (u. let (... x ... y ...) (z. ... z ... ))) u ... )

Weak Higher-Order Syntax let (...) (x : var. let (... #x ...) (y : var. let (... #x ...) (u : var. let (... #x ... #y ...) (z. ... #z ... #u ...

Parametric Higher-Order Syntax ∀ var: let (...) (x : var. let (... #x ...) (y : var. let (... #x ...) (u : var. let (... #x ... #y ...) (z. ... #z ... #u ... A piece of syntax is a first-class polymorphic function.