logical relations for a manifest contract calculus
play

Logical Relations for a Manifest Contract Calculus Taro Sekiyama - PowerPoint PPT Presentation

Logical Relations for a Manifest Contract Calculus Taro Sekiyama Atsushi Igarashi Kyoto University Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus Manifest Contract Calculus [1] A typed lambda calculus with


  1. Logical Relations for a Manifest Contract Calculus Taro Sekiyama Atsushi Igarashi Kyoto University Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus

  2. Manifest Contract Calculus [1] A typed lambda calculus with (higher-order) software contracts hybrid checking of software contracts Static type system: refinement type { x : T | e } e.g. { x :int | 0 < x } Dynamic checking: cast � T 1 ⇒ T 2 � � e.g. � int ⇒ { x :int | x < 0 }� � [1] Knowles and Flanagan, 2010 Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus

  3. Programming in Manifest Contract Calculus div : int → { x :int | 0 � = x } → int div “abc” 2 ( ∗ Compiler error ∗ ) ( ∗ Compiler error ∗ ) div 6 0 ( ∗ Compiler doesn’t know that y is non-zero ∗ ) ( fun y : int . div 6 y ) Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus

  4. Programming in Manifest Contract Calculus div : int → { x :int | 0 � = x } → int div “abc” 2 ( ∗ Compiler error ∗ ) ( ∗ Compiler error ∗ ) div 6 0 ( ∗ Compiler inserts a cast ∗ ) ( fun y : int . div 6 ( � int ⇒ { x :int | 0 � = x }� � y )) Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus

  5. Previous Work: Upcast Elimination Upcast Elimination [1,2] An upcast and an identity function are contextually equivalent An upcast is a cast from a type to its supertype �{ x :int | 0 < x } ⇒ int � � �{ x :int | is square x } ⇒ { x :int | 0 < x }� � Upcast elimination is useful for optimization [1] Knowles and Flanagan, 2010 [2] Belo et al., 2011 Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus

  6. Previous Work: Correctness of Proofs Previous work tried to prove upcast elimination by using logical relations didn’t really prove soundness of the logical relations w.r.t contextual equivalence λ [1] F H [2] H � T 1 ⇒ T 2 � � � fun x . x proved proved � ⊆ ≈ flawed not proved � T 1 ⇒ T 2 � � ≈ fun x . x not proved not proved ≈ : contextual equivalence � : logical relation [1] Knowles and Flanagan, 2010 [2] Belo et al., 2011 Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus

  7. Logical Relations for a Manifest Contract Calculus, Fixed Taro Sekiyama Atsushi Igarashi Kyoto University Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

  8. This Work This work fixes the flaws of previous work introduces F fix H a polymorphic manifest contract calculus with fixed -point operator non-termination is only effect in F fix H F fix λ H F H H Subsumption rule × × � Polymorphic types × � � × × Fixed-point operator � Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

  9. Contribution Semi-typed contextual equivalence A sound logical relation w.r.t semi-typed contextual equivalence Proof of upcast elimination by using the logical relation above We believe correctness of our proof :-) F fix λ H F H H � T 1 ⇒ T 2 � � � fun x . x proved proved proved proved � ⊆ ≈ flawed not proved � T 1 ⇒ T 2 � � ≈ fun x . x proved not proved not proved Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

  10. Contents A Manifest Contract Calculus: F fix 1 H Semi-Typed Contextual Equivalence 2 Logical Relation 3 Upcast Elimination 4 Discussion 5 Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

  11. Contents A Manifest Contract Calculus: F fix 1 H Semi-Typed Contextual Equivalence 2 Logical Relation 3 Upcast Elimination 4 Discussion 5 Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

  12. Overview of F fix H F fix is a typed lambda calculus with H polymorphic types, refinement types { x : T | e } , dependent function types x : T 1 → T 2 , casts � T 1 ⇒ T 2 � � , and fixed-point operator (recursive functions) λ H F fix F H H × × Subsumption rule � Polymorphic types × � � Recursive functions × × � Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

  13. Types Refinement types: { x : T | e } denote a set of values which are in T satisfy the contract (boolean expression) e e.g. { x :int | 0 < x } = { 1 , 2 , 3 , ... } Dependent function types: x : T 1 → T 2 denote a set of functions which accept values v of T 1 return values of T 2 [ v / x ] e.g. x :int → { y :int | x < y } Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

  14. Dynamic Checking: Cast Casts: � T 1 ⇒ T 2 � � accept values v of T 1 check whether v can behave as T 2 If the checking fails, the cast is blamed with label � e.g. � int ⇒ { x :int | 0 < x }� � � int ⇒ { x :int | 0 < x }� � 0 � ∗ ⇑ � � int ⇒ { x :int | 0 < x }� � 2 � ∗ 2 Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

  15. Digression: Pitfall of A-Normal Form At first, we gave A-normal form as syntax following [3] which uses A-normal form to simplify the definition and the proof e ::= v 1 v 2 | let x = e 1 in e 2 | · · · It is difficult to prove even type soundness to require substitution of terms A-normal form is not closed under substitution of terms Γ � e 1 : T 1 Γ , x : T 1 � e 2 : T 2 Γ � let x = e 1 in e 2 : T 2 [ e 1 / x ] [3] Pitts, 2005 Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

  16. Contents A Manifest Contract Calculus: F fix 1 H Semi-Typed Contextual Equivalence 2 Logical Relation 3 Upcast Elimination 4 Discussion 5 Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

  17. Review: (Typed) Contextual Equivalence e 1 ≈ typed e 2 : T e 1 and e 2 have the same observable result under any contexts which are well-typed and accept any terms of T e 1 and e 2 are typed at the same type T ( fun x : int . 0) ≈ typed ( fun x : int . x ∗ 0) : int → int ( fun x : int . 0) �≈ typed ( fun x : int . x + 2) : int → int ( fun x : int . 0) �≈ typed ( fun x : bool . 0) : int → int Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

  18. Problem Upcast elimination doesn’t hold in typed contextual equivalence An upcast and an identity function may have different types Note lack of a subsumption rule � T 1 ⇒ T 2 � � fun x : T 1 . x fun x : T 2 . x T 1 → T 2 T 1 → T 1 T 2 → T 2 We must relax typed contextual equivalence Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

  19. Semi-Typed Contextual Equivalence e 1 ≈ e 2 : T e 1 and e 2 have the same observable result under any well-typed contexts Only e 1 is typed at T e 2 can even be ill-typed ( fun x : int . 0) ≈ ( fun x : int . x ∗ 0) : int → int ( fun x : int . 0) �≈ ( fun x : int . x + 2) : int → int ( fun x : int . 0) ≈ ( fun x : bool . 0) : int → int Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

  20. Formal Definition Definition Semi-typed contextual equivalence ≈ is the largest set satisfying the following: If Γ � e 1 ≈ e 2 : T , then Γ � e 1 : T 1 If ∅ � e 1 ≈ e 2 : T , then e 1 and e 2 have the 2 same observable result Reflexivity, Transitivity, (Typed) Symmetry 3 Compatibility 4 Substitutivity 5 Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

  21. Compatibility and Substitutivity Rules Choose typed terms for substitution on types so that the type after the substitution is well-formed E.g. Compatibility: term application Γ � e 11 ≈ e 21 : ( x : T 1 → T 2 ) Γ � e 12 ≈ e 22 : T 1 Γ � e 11 e 12 ≈ e 21 e 22 : T 2 [ e 12 / x ] Substitutivity: value substitution Γ , x : T 1 , Γ � � e 1 ≈ e 2 : T 2 Γ � v 1 ≈ v 2 : T 1 Γ , Γ � [ v 1 / x ] � e 1 [ v 1 / x ] ≈ e 2 [ v 2 / x ] : T 2 [ v 1 / x ] Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

  22. Contents A Manifest Contract Calculus: F fix 1 H Semi-Typed Contextual Equivalence 2 Logical Relation 3 Upcast Elimination 4 Discussion 5 Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

  23. Overview of Logical Relation e 1 � e 2 : T � is defined by using basic ideas of the logical relation for F H [2] �� -closure[3] A method to give a logical relation to a lambda calculus with recursive functions Only e 1 is typed similarly to semi-typed contextual equivalence [2] Belo et al., 2011 [3] Pitts, 2005 Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

  24. How to Define Logical Relation by �� Define value relations for base types 1 bool: { (true,true), (false,false) } int: { ...,(-1,-1),(0,0),(1,1),... } Taro Sekiyama Atsushi Igarashi Logical Relations for a Manifest Contract Calculus, Fixed

Recommend


More recommend