Encoding Hoare Logic in Typed Certified Code Nikolaos S. Papaspyrou Michalis A. Papakyriakou Angelos Manousaridis National Technical University of Athens School of Electrical and Computer Engineering Software Engineering Laboratory {nickie, mpapakyr, amanous}@softlab.ntua.gr 5th Panhellenic Logic Symposium Athens, July 25-28, 2005 N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Outline Motivation Hoare logic Typed certified code Can we combine the two? Our approach The type language The computation language Encoding Hoare logic Problems with Hoare Logic And their Solution Example Conclusions N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Hoare Logic (i) ◮ Introduced the strength of formal logic in computer programming ◮ A tool to: ◮ reason about program properties and prove correctness ◮ derive programs from their specifications C. A. R. Hoare, “An axiomatic basis for computer programming”, Communications of the ACM , vol. 12, no. 10, pp. 576–585, 1969. N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Hoare Logic (ii) ◮ Hoare triples represent program specifications { P } program { Q } N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Hoare Logic (ii) ◮ Hoare triples represent program specifications { P } program { Q } ◮ Example: greatest common divisor { n + m > 0 } a : = n ; b : = m ; while a > 0 and b > 0 do if a > b then a : = a mod b else b : = b mod a ; r : = a + b { r > 0 ∧ r \ n ∧ r \ m ∧ ( ∀ r ′ ∈ N . r ′ \ n ∧ r ′ \ m ⇒ r ′ ≤ r ) } N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Typed Certified Code (i) Methodology: ◮ a sound formal logic is used ◮ combined with the programming language ◮ program specifications are expressed as propositions in this logic ◮ proofs of these propositions are embedded in programs ◮ either explicitly given by the programmer ◮ or automatically constructed by the compiler N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Typed Certified Code (ii) Proposed solutions: ◮ Typed Intermediate Language (TIL); Harper and Morrisett, 1995 ◮ Typed Assembly Language (TAL); Morrisett, Walker, Crary and Glew, 1998 ◮ Proof-Carrying Code (PCC); Necula, 1998 ◮ Foundational Proof-Carrying Code, Appel, 2001 ◮ Shao, Saha, Trifonov and Papaspyrou, 2002, 2005 ◮ Crary and Vanderwaart, 2002 N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Typed Certified Code (iii) ◮ Example: greatest common divisor ◮ gcd : nat ։ nat ։ nat “A function taking two naturals and returning some natural.” N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Typed Certified Code (iii) ◮ Example: greatest common divisor ◮ gcd : nat ։ nat ։ nat “A function taking two naturals and returning some natural.” ◮ gcd : ∀ n : Nat . ∀ m : Nat . ∀ p ∗ : ( n + m > 0 ) . snat n ։ snat m ։ nat “One of the arguments shall not be zero.” N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Typed Certified Code (iii) ◮ Example: greatest common divisor ◮ gcd : nat ։ nat ։ nat “A function taking two naturals and returning some natural.” ◮ gcd : ∀ n : Nat . ∀ m : Nat . ∀ p ∗ : ( n + m > 0 ) . snat n ։ snat m ։ nat “One of the arguments shall not be zero.” ◮ Singleton type snat n A data type whose elements are representations of the single integer value n : Nat N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Typed Certified Code (iii) ◮ Example: greatest common divisor ◮ gcd : nat ։ nat ։ nat “A function taking two naturals and returning some natural.” ◮ gcd : ∀ n : Nat . ∀ m : Nat . ∀ p ∗ : ( n + m > 0 ) . snat n ։ snat m ։ nat “One of the arguments shall not be zero.” ◮ Singleton type snat n A data type whose elements are representations of the single integer value n : Nat ◮ (Syntactic sugar) nat ≡ ∃ r : Nat . snat r N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Typed Certified Code (iv) ◮ Example (continued) ◮ gcd : ∀ n : Nat . ∀ m : Nat . ∀ p ∗ : ( n + m > 0 ) . snat n ։ snat m ։ ∃ r : Nat . ∃ q ∗ : ( r > 0 ) . snat r “The result shall not be zero.” N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Typed Certified Code (iv) ◮ Example (continued) ◮ gcd : ∀ n : Nat . ∀ m : Nat . ∀ p ∗ : ( n + m > 0 ) . snat n ։ snat m ։ ∃ r : Nat . ∃ q ∗ : ( r > 0 ) . snat r “The result shall not be zero.” ◮ gcd : ∀ n : Nat . ∀ m : Nat . ∀ p ∗ : ( n + m > 0 ) . snat n ։ snat m ։ ∃ r : Nat . ∃ q ∗ : ( r > 0 ∧ r \ n ∧ r \ m ) . snat r “The result shall not be zero and shall divide both arguments.” N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Typed Certified Code (v) ◮ Example (continued) ◮ gcd : ∀ n : Nat . ∀ m : Nat . ∀ p ∗ : ( n + m > 0 ) . snat n ։ snat m ։ ∃ r : Nat . ∃ q ∗ 1 : ( r > 0 ∧ r \ n ∧ r \ m ) . 2 : ( Π r ′ : Nat . r ′ \ n ∧ r ′ \ m → r ′ ≤ r ) . ∃ q ∗ snat r “The result shall be the greatest common divisor of the two arguments.” N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Can we combine the two? ◮ Hoare Logic + long studied, large body of scientific knowledge + simple axioms and rules + works with variables and destructive update − does not work well with (higher-order) functions − proofs of specifications cannot be automatically verified N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Can we combine the two? ◮ Hoare Logic + long studied, large body of scientific knowledge + simple axioms and rules + works with variables and destructive update − does not work well with (higher-order) functions − proofs of specifications cannot be automatically verified ◮ Typed Certified Code − relatively new approach − highly complex type system − does not work well with variables and destructive update + works well with (higher-order) functions + proofs of specifications can be automatically verified N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Overview of the Type Language ◮ A variation of the Calculus of Inductive Constructions ◮ Incorporates higher-order predicate logic ◮ Complete grammar: A , B :: = Set | Type | Ext | X | Π X : A . B | λ X : A . B | AB Ind ( X : A ) { � A } | Constr ( n , A ) | Elim [ A ′ ]( A : B � B ) { � | A } A → B ≡ Π X new : A . B Papaspyrou, Vytiniotis and Koutavas, “Logic-Enhanced Type Systems”, PLS 4 , 2003. Shao, Trifonov, Saha and Papaspyrou, “A Type System for Certified Binaries”, ACM TOPLAS , vol. 27, no. 1, pp. 1-45, 2005. N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
The Computation Language (i) ◮ The simple imperative language WHILE x : Var variables :: = n | b | x | ⋄ e | e ⋆ e e : Expr c : Comm :: = skip | x : = e | c ; c | if e then c else c | while e do c ⋄ : UnOp :: = − | ¬ ⋆ : BinOp :: = + | − | ∗ | div | mod | = | � = | < | > | ≤ | ≥ | and | or N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
The Computation Language (ii) Typing ◮ Types τ : Ω :: = int | bool Γ : Env = Var → Ω ◮ Type environments Γ ⊢ e : τ ◮ Typing of expressions Γ ⊢ c ◮ Typing of commands N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
The Computation Language (ii) Typing ◮ Types τ : Ω :: = int | bool Γ : Env = Var → Ω ◮ Type environments Γ ⊢ e : τ ◮ Typing of expressions Γ ⊢ c ◮ Typing of commands Semantics ◮ Meaning of types [ [ int ] ] = Int , [ [ bool ] ] = Bool s : Store Γ = Π x : Var . [ [ Γ x ] ◮ Stores ] ◮ Meaning of expressions [ [ e ] ] s ⇓ v ] s ⇓ s ′ ◮ Meaning of commands [ [ c ] N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Encoding Hoare Logic (i) P , Q , R : Pred Γ = Store Γ → Set ◮ Predicates N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Encoding Hoare Logic (i) P , Q , R : Pred Γ = Store Γ → Set ◮ Predicates ◮ Specification of commands { P } c { Q } Γ ⊢ c P , Q : Pred Γ ◮ { P } c { Q } is valid if for all s : Store Γ , ] s ⇓ s ′ , for some s ′ : Store Γ , if Ps and [ [ c ] then Qs ′ N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis Encoding Hoare Logic in Typed Certified Code
Recommend
More recommend