typed closure conversion for the calculus of constructions
play

Typed Closure Conversion for the Calculus of Constructions William - PowerPoint PPT Presentation

Typed Closure Conversion for the Calculus of Constructions William J. Bowman , Amal Ahmed Typed Closure Conversion for the Calculus of Constructions Core calculus on which Coq is built Dependent types And high-assurance


  1. Typed Closure Conversion 
 for the 
 Calculus of Constructions William J. Bowman , Amal Ahmed

  2. 
 Typed Closure Conversion 
 for the Calculus of Constructions Core calculus on which Coq is built

  3. Dependent types And high-assurance software

  4. Dependent types High-assurance software using dependent types Verified in Coq! - CompCert - CertiKOS - Vellvm - RustBelt - CertiCrypt 
 …

  5. Story of a verified program Coq e

  6. Story of a verified program Coq OCaml asm e+…+ e e+ …

  7. Story of a verified program Coq ✓ OCaml ? asm ?…? e+…+ e e+ … Compilation 
 can undo verification

  8. Compiler correctness!

  9. A correct compilation story Coq ✓ OCaml ✓ asm ✓ ✓ ✓ e+…+ e e+ … Verify that the program we run is the program we verified

  10. Compiler correctness 
 is not the whole story

  11. Correctness is the 
 “whole program” story

  12. No one* writes whole programs * Okay, well, not most people.

  13. Story of a verified component Coq ✓ OCaml ? OCaml X Unverified 
 e : A e+ e’ component k n i L

  14. Story of a verified component Coq ✓ OCaml ? OCaml X e : A e+ e’ k n i L Linking can undo Compilation 
 verification can undo verification

  15. Story of a verified component Coq ✓ OCaml ? OCaml X asm ? asm X ? asm X e++ e’+ e’’ e : A e+ e’ k k n n i i L L

  16. Story of a verified component Coq ✓ OCaml ? OCaml X asm ? asm X ? asm X e++ e’+ e’’ e : A e+ e’ k k n n i i L L Linking can undo Compilation 
 verification can undo verification

  17. > coqc verified.v 
 > link verified.ml unverified.ml 
 > ocaml verified.ml [1] 43185 segmentation fault (core dumped) ocaml verified.ml

  18. Be careful? > coqc verified.v 
 > link verified.ml unverified.ml 
 > ocaml verified.ml [1] 43185 segmentation fault (core dumped) ocaml verified.ml

  19. No! 
 Be careful? Be well-typed! Coq Dep. Type ASM e : A e+ : A+ e’ : A’ e’’ : A’’ … k k n n i i L L Verified 
 type-preserving Type checking compilers linkers

  20. 
 Typed Closure Conversion 
 for the Calculus of Constructions A standard compiler pass for functional languages

  21. A type-preserving compiler Continuation-Passing Style (CPS) Closure Conversion Allocation Code generation Morrisett, Walker, Crary, Glew 1998

  22. 
 A type-preserving compiler Theorem. (Type Preservation) 
 If e : A then 
 translates to e+ : A+ …

  23. A type-preserving compiler Design typed intermediate language 
 Prove soundness, decidability, etc

  24. A dependent-type-preserving compiler • Move from functional, compositional 
 to global, stateful, instruction based • Which axioms can we use 
 (e.g parametricity, impredicativity) Design typed intermediate language 
 Prove soundness, decidability, etc

  25. Brief history of preserving dependent types Continuation-Passing Style (CPS) 1999 2002 2018 Impossibility result Un-impossibility result

  26. Brief history of preserving dependent types Continuation-Passing Style (CPS) 1999 2002 2018 Impossibility result Un-impossibility result Key insights: Past work doesn’t scale in the obvious way - for dependent type theory

  27. Type-Preserving Closure Conversion Closure Conversion Key problem: Which axioms does past work rely on, and - can we use them?

  28. Type-Preserving Closure Conversion The standard (non-dependent) type-preserving translation Γ ⊢ f : A → B Goal: Translate

  29. Type-Preserving Closure Conversion The standard (non-dependent) type-preserving translation Γ ⊢ f : A → B Goal: Translate Takes 1 argument, of type A

  30. Type-Preserving Closure Conversion The standard (non-dependent) type-preserving translation Γ ⊢ f : A → B Goal: Translate Takes 1 argument, of type A returns result of type B

  31. Type-Preserving Closure Conversion The standard (non-dependent) type-preserving translation Γ ⊢ f : A → B Goal: Translate And can refer to lexical variables

  32. Type-Preserving Closure Conversion The standard (non-dependent) type-preserving translation Γ ⊢ f : A → B Goal: Translate Γ + ⊢ ⟨ Γ + , f + ⟩ : Closure ( A + → B + ) · To: Pair of data and a code pointer . 
 (object)

  33. Type-Preserving Closure Conversion The standard (non-dependent) type-preserving translation Γ ⊢ f : A → B Goal: Translate Γ + ⊢ ⟨ Γ + , f + ⟩ : Closure ( A + → B + ) · To: · ⊢ f + : Γ + → A + → B + ) Where: Code pointers are closed except formal arguments (can be heap allocated).

  34. Type-Preserving Closure Conversion The standard (non-dependent) type-preserving translation How do we implement a typed closure ? Γ + ⊢ ⟨ Γ + , f + ⟩ : Closure ( A + → B + ) ·

  35. Type-Preserving Closure Conversion The standard (non-dependent) type-preserving translation How do we implement a typed closure ? Γ + ⊢ ⟨ Γ + , f + ⟩ : Closure ( A + → B + ) · 1. Not as pairs: Γ + ⊢ ⟨ Γ + , f + ⟩ : Pair ( Γ + , Γ + → A + → B + ) def apply c x = ( snd c ) ( fst c ) x

  36. How do we implement a typed closure ? 1. Not as pairs: Γ + ⊢ ⟨ Γ + , f + ⟩ : Pair ( Γ + , Γ + → A + → B + ) def apply c x = ( snd c ) ( fst c ) x ✓ Equal z : Nat ⊢ f : Nat → Nat ⊢ f ′ : Nat → Nat

  37. How do we implement a typed closure ? 1. Not as pairs: Γ + ⊢ ⟨ Γ + , f + ⟩ : Pair ( Γ + , Γ + → A + → B + ) def apply c x = ( snd c ) ( fst c ) x ✓ Equal z : Nat ⊢ f : Nat → Nat ⊢ f ′ : Nat → Nat X Not equal ⊢ ⟨ z , f + ⟩ : Pair (( z : Nat ) , ( z : Nat ) → Nat → Nat ) ⊢ ⟨ () , f ′ + ⟩ : Pair (() , () → Nat → Nat )

  38. How do we implement a typed closure ? 1. Not as pairs: Γ + ⊢ ⟨ Γ + , f + ⟩ : Pair ( Γ + , Γ + → A + → B + ) def apply c x = ( snd c ) ( fst c ) x def X Not Secure extract_hidden_data c = fst c

  39. Type-Preserving Closure Conversion The standard (non-dependent) type-preserving translation How do we implement a typed closure ? Γ + ⊢ ⟨ Γ + , f + ⟩ : Closure ( A + → B + ) · 1. Not as pairs: Γ + ⊢ ⟨ Γ + , f + ⟩ : Pair ( Γ + , Γ + → A + → B + ) def apply c x = ( snd c ) ( fst c ) x

  40. Type-Preserving Closure Conversion The standard (non-dependent) type-preserving translation How do we implement a typed closure ? Γ + ⊢ ⟨ Γ + , f + ⟩ : Closure ( A + → B + ) · 1. Not as pairs: Γ + ⊢ ⟨ Γ + , f + ⟩ : Pair ( Γ + , Γ + → A + → B + ) def apply c x = ( snd c ) ( fst c ) x 2. As existential pairs: Γ + ⊢ ⟨ Γ + , f + ⟩ : ∃ α . ( α , ( α → A + → B + )) def apply c x = unpack ⟨ α , p ⟩ in ( snd c ) ( fst c ) x

  41. How do we implement a typed closure ? 2. As existential pairs: Γ + ⊢ ⟨ Γ + , f + ⟩ : ∃ α . ( α , ( α → A + → B + )) def apply c x = unpack ⟨ α , p ⟩ in ( snd c ) ( fst c ) x ✓ Equal z : Nat ⊢ f : Nat → Nat ⊢ f ′ : Nat → Nat ✓ Equal ⊢ ⟨ () , f ′ + ⟩ : ∃ α . ( α , α → Nat → Nat ) ⊢ ⟨ z , f + ⟩ : ∃ α . ( α , α → Nat → Nat )

  42. How do we implement a typed closure ? 2. As existential pairs: Γ + ⊢ ⟨ Γ + , f + ⟩ : ∃ α . ( α , ( α → A + → B + )) def apply c x = unpack ⟨ α , p ⟩ in ( snd c ) ( fst c ) x def X extract_hidden_data c = fst c ✓ Secure Not definable

  43. Type-Preserving Closure Conversion The standard (non-dependent) type-preserving translation How do we implement a typed closure ? Γ + ⊢ ⟨ Γ + , f + ⟩ : Closure ( A + → B + ) · As existential pairs: Γ + ⊢ ⟨ Γ + , f + ⟩ : ∃ α . ( α , ( α → A + → B + )) def apply c x = unpack ⟨ α , p ⟩ in ( snd c ) ( fst c ) x

  44. Dependent-Type-Preserving Closure Conversion Goal: Translate Γ ⊢ f : Π x : A . B

  45. Dependent-Type-Preserving Closure Conversion Goal: Translate Γ ⊢ f : Π x : A . B Takes 1 argument, x , of type A .

  46. Dependent-Type-Preserving Closure Conversion Goal: Translate Γ ⊢ f : Π x : A . B Returns result of type B .

  47. Dependent-Type-Preserving Closure Conversion Goal: Translate Γ ⊢ f : Π x : A . B Refer to lexical variables

  48. Dependent-Type-Preserving Closure Conversion Goal: Translate Γ ⊢ f : Π x : A . B And so can types: types can depend on terms

  49. Dependent-Type-Preserving Closure Conversion Goal: Translate Γ ⊢ f : Π x : A . B The term-level 0 div : Π x : Nat . Π y : Nat . Π p : y � 0 . Int The term-level inequality function

  50. Dependent-Type-Preserving Closure Conversion Goal: Translate Γ ⊢ f : Π x : A . B

  51. Dependent-Type-Preserving Closure Conversion Γ ⊢ f : Π x : A . B Goal: Translate Γ + ⊢ ⟨ Γ + , f + ⟩ : Closure ( x : A + → B + ) To: · ⊢ f + : Π Γ + . Π x : A + . B + Where: Code pointers are closed 
 (can be heap allocated)

  52. Dependent-Type-Preserving Closure Conversion How do we implement a dependently typed closure ? Γ + ⊢ ⟨ Γ + , f + ⟩ : Closure ( x : A + → B + ) Hint: not as pairs

  53. Dependent-Type-Preserving Closure Conversion How do we implement a dependently typed closure ? Γ + ⊢ ⟨ Γ + , f + ⟩ : Closure ( x : A + → B + ) Hint: not as pairs Hint: existential pairs don’t work either

  54. Digression on the nature of existence

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend