A Mechanized Proof of Type Safety for the Polymorphic λ -Calculus with References Michalis A. Papakyriakou Prodromos E. Gerakios Nikolaos S. Papaspyrou National Technical University of Athens School of Electrical and Computer Engineering Software Engineering Laboratory {mpapakyr, pgerakios, nickie}@softlab.ntua.gr 6th Panhellenic Logic Symposium Volos, Greece, 5-8 July 2007 Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
Outline Introduction Type systems and type safety Polymorphic λ -calculus References Mechanized proof The language λ ∀ , ref Encoding λ ∀ , ref in Isabelle/HOL A tour of the proof Conclusions and future work Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
What is this paper about? ◮ The language Polymorphic λ -calculus with references ◮ The goal A proof of type safety ◮ The method Mechanized proof Using Isabelle/HOL Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
Type systems ◮ A type system defines: ◮ how a programming language classifies values and expressions into types ◮ how elements of these types can be manipulated ◮ how these types can interact ◮ A type indicates a set of values that have the same generic meaning or intended purpose ◮ The purpose of type systems: to prevent certain forms of erroneous or undesirable program behaviour Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
Type safety ◮ If a program is free of static type errors, then its execution is free of dynamic type errors ◮ Kinds of dynamic errors that can be avoided: ◮ programs can only access appropriate memory locations (memory safety) ◮ programs can only transfer control to appropriate program points (control safety) Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
Type safety ◮ If a program is free of static type errors, then its execution is free of dynamic type errors ◮ Kinds of dynamic errors that can be avoided: ◮ programs can only access appropriate memory locations (memory safety) ◮ programs can only transfer control to appropriate program points (control safety) ◮ The standard procedure ◮ Syntax ◮ Operational semantics ◮ Typing rules ◮ Safety = preservation + progress Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
Polymorphic λ -calculus ◮ System F , F 2 ◮ Girard, 1971; Reynolds, 1974 ◮ First-class polymorphism ◮ Useful for code reuse and modular type checking Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
Polymorphic λ -calculus ◮ System F , F 2 ◮ Girard, 1971; Reynolds, 1974 ◮ First-class polymorphism ◮ Useful for code reuse and modular type checking ◮ Polymorphic types and functions ∀ α . α → α id : = Λ α . λ x : α . x Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
Polymorphic λ -calculus ◮ System F , F 2 ◮ Girard, 1971; Reynolds, 1974 ◮ First-class polymorphism ◮ Useful for code reuse and modular type checking ◮ Polymorphic types and functions ∀ α . α → α id : = Λ α . λ x : α . x ◮ Explicit type application ∀ α . list α → list α → list α append : . . . append [ int ] [ 1, 2, 3 ] [ 4, 5 ] . . . Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
ML-style references Imperative programming in functional style ◮ Reference allocation let r = new 7 . . . r : Ref int Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
ML-style references Imperative programming in functional style ◮ Reference allocation let r = new 7 . . . r : Ref int ◮ Assignment . . . in r : = 42; . . . destructive update! Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
ML-style references Imperative programming in functional style ◮ Reference allocation let r = new 7 . . . r : Ref int ◮ Assignment . . . in r : = 42; . . . destructive update! ◮ Dereference . . . print ( deref r ) ; prints 42 Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
ML-style references Imperative programming in functional style ◮ Reference allocation let r = new 7 . . . r : Ref int ◮ Assignment . . . in r : = 42; . . . destructive update! ◮ Dereference . . . print ( deref r ) ; prints 42 ◮ No reference deallocation! . . . free r use garbage collection! Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
Polymorphic references ◮ The problem let r = Λ α . new ( λ x : α . x ) in r : ∀ α . Ref ( α → α ) r [ int ] : = succ ; deref ( r [ bool ]) true dynamic type error Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
Polymorphic references ◮ The problem let r = Λ α . new ( λ x : α . x ) in r : ∀ α . Ref ( α → α ) r [ int ] : = succ ; deref ( r [ bool ]) true dynamic type error ◮ A solution: value restriction In Λ α . v , the term v must be a value Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
Mechanized proof (i) ◮ Why not with pencil and paper? ◮ easy to make a mistake ◮ easy to “fix” a mistake ◮ if one is willing to spend time and effort to write a thorough proof with pencil and paper, why not use a proof assistant? Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
Mechanized proof (i) ◮ Why not with pencil and paper? ◮ easy to make a mistake ◮ easy to “fix” a mistake ◮ if one is willing to spend time and effort to write a thorough proof with pencil and paper, why not use a proof assistant? ◮ Proof assistants ◮ tools to develop formal proofs by man-machine collaboration ◮ interactive proof editor, with which a human can guide the search for proofs ◮ some steps of the proofs can be provided by the computer ◮ not (necessarily) automatic theorem proving! Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
Mechanized proof (ii) ◮ Some available proof assistants: Isabelle/HOL, Coq, Twelf, NuPRL, PVS, PhoX, MINLOG, . . . ◮ Isabelle/HOL ◮ Larry Paulson, Cambridge University ◮ Tobias Nipkow, TU München ◮ ❤tt♣✿✴✴✐s❛❜❡❧❧❡✳✐♥✳t✉♠✳❞❡✴ Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
Syntax of λ ∀ , ref τ :: = Unit | α | τ → τ | ∀ α . τ | Ref τ e :: = unit | x | λ x : τ . e | Λ α . e | e 1 e 2 | e [ τ ] | new e | deref e | e 1 : = e 2 | loc l v :: = unit | λ x : τ . e | Λ α . v | loc l Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
Typing rules of λ ∀ , ref Γ ; ∆ ; M ⊢ unit : Unit Γ , x : τ ; ∆ ; M ⊢ x : τ Γ , x : τ ; ∆ ; M ⊢ e : τ ′ ∆ | = τ Γ ; ∆ , α ; M ⊢ v : τ Γ ; ∆ ; M ⊢ λ x : τ . e : τ → τ ′ Γ ; ∆ ; M ⊢ Λ α . v : ∀ α . τ Γ ; ∆ ; M ⊢ e 1 : τ → τ ′ Γ ; ∆ ; M ⊢ e 2 : τ Γ ; ∆ ; M ⊢ e 1 e 2 : τ ′ = τ ′ Γ ; ∆ ; M ⊢ e : ∀ α . τ ∆ | Γ ; ∆ ; M ⊢ e : τ Γ ; ∆ ; M ⊢ e [ τ ′ ] : τ { α �→ τ ′ } Γ ; ∆ ; M ⊢ new e : Ref τ Γ ; ∆ ; M ⊢ e : Ref τ Γ ; ∆ ; M ⊢ deref e : τ Γ ; ∆ ; M ⊢ e 1 : Ref τ Γ ; ∆ ; M ⊢ e 2 : τ Γ ; ∆ ; M ⊢ e 1 : = e 2 : Unit Γ ; ∆ ; M, l : τ ⊢ loc l : Ref τ Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
Operational semantics of λ ∀ , ref → S ′ ; e ′ → S ′ ; e ′ S ; e 1 − S ; e 2 − 1 2 → S ′ ; e ′ → S ′ ; v 1 e ′ S ; e 1 e 2 − S ; v 1 e 2 − 1 e 2 2 → S ′ ; e ′ S ; e − → S ′ ; e ′ [ τ ] S ; e [ τ ] − → S ′ ; e ′ → S ′ ; e ′ S ; e − S ; e − → S ′ ; new e ′ → S ′ ; deref e ′ S ; new e − S ; deref e − → S ′ ; e ′ → S ′ ; e ′ S ; e 1 − S ; e 2 − 1 2 → S ′ ; e ′ → S ′ ; v 1 : = e ′ S ; e 1 : = e 2 − 1 : = e 2 S ; v 1 : = e 2 − 2 S ; ( λ x : τ . e ) v − → S ; e { x �→ v } S ; ( Λ α . v ) [ τ ] − → S ; v { α �→ τ } S ; new v − → S , l �→ v ; loc l S , l �→ v ; deref ( loc l ) − → S , l �→ v ; v S , l �→ v ′ ; loc l : = v − → S , l �→ v ; v Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
Encoding λ ∀ , ref in Isabelle/HOL ◮ Main problems ◮ The representation of bound variables ◮ The representation of type environments ◮ The details are usually ignored in pencil and paper proofs Mechanized Proof of Type Safety for λ ∀ , ref M. A. Papakyriakou, P . E. Gerakios, N. S. Papaspyrou
Recommend
More recommend