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

typed closure conversion for the calculus of constructions
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Typed Closure Conversion 
 for the 
 Calculus of Constructions

William J. Bowman, Amal Ahmed

slide-2
SLIDE 2


 Calculus of Constructions Typed Closure Conversion 
 for the

Core calculus on which Coq is built

slide-3
SLIDE 3

Dependent types

And high-assurance software

slide-4
SLIDE 4

Dependent types

High-assurance software using dependent types

  • CompCert
  • CertiKOS
  • Vellvm
  • RustBelt
  • CertiCrypt


… Verified in Coq!

slide-5
SLIDE 5

Story of a verified program

Coq

e

slide-6
SLIDE 6

Coq OCaml

e e+

… asm

e+…+

Story of a verified program

slide-7
SLIDE 7

Coq ✓ OCaml ?

e e+

… asm ?…?

e+…+

Compilation
 can undo verification

Story of a verified program

slide-8
SLIDE 8

Compiler correctness!

slide-9
SLIDE 9

A correct compilation story

Coq ✓ OCaml ✓

e e+

… asm ✓

e+…+

✓ ✓ Verify that the program we run is the program we verified

slide-10
SLIDE 10

Compiler correctness
 is not the whole story

slide-11
SLIDE 11

Correctness is the 
 “whole program” story

slide-12
SLIDE 12

No one* writes whole programs

* Okay, well, not most people.

slide-13
SLIDE 13

Story of a verified component

OCaml X

e : A e+ e’

L i n k Coq ✓ OCaml ? Unverified 
 component

slide-14
SLIDE 14

OCaml X

e : A e+ e’

L i n k Coq ✓ OCaml ? Compilation
 can undo verification Linking can undo verification

Story of a verified component

slide-15
SLIDE 15

e : A e+ e’ e++ e’+ e’’

asm X ? L i n k Coq ✓ OCaml X OCaml ? asm ? asm X L i n k

Story of a verified component

slide-16
SLIDE 16

e : A e+ e’ e++ e’+ e’’

asm X ? L i n k Coq ✓ OCaml X OCaml ? asm ? asm X Compilation
 can undo verification Linking can undo verification L i n k

Story of a verified component

slide-17
SLIDE 17

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

  • caml verified.ml
slide-18
SLIDE 18

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

  • caml verified.ml

Be careful?

slide-19
SLIDE 19

Coq

e : A

e+ : A+

e’ : A’ e’’ : A’’

  • Dep. Type ASM

Be careful? No!
 Be well-typed!

Verified 
 type-preserving compilers Type checking linkers L i n k L i n k

slide-20
SLIDE 20


 Calculus of Constructions Typed Closure Conversion 


A standard compiler pass for functional languages

for the

slide-21
SLIDE 21

A type-preserving compiler

Continuation-Passing Style (CPS) Closure Conversion Allocation Code generation

Morrisett, Walker, Crary, Glew 1998

slide-22
SLIDE 22

A type-preserving compiler

  • Theorem. (Type Preservation)


If then
 


e

translates to

: A

e+ : A+

slide-23
SLIDE 23

A type-preserving compiler

Design typed intermediate language
 Prove soundness, decidability, etc

slide-24
SLIDE 24

A dependent-type-preserving compiler

Design typed intermediate language
 Prove soundness, decidability, etc

  • Move from functional, compositional


to global, stateful, instruction based

  • Which axioms can we use


(e.g parametricity, impredicativity)

slide-25
SLIDE 25

Impossibility result

Brief history of preserving dependent types

1999 2002 2018 Continuation-Passing Style (CPS)

Un-impossibility result

slide-26
SLIDE 26

Impossibility result

Brief history of preserving dependent types

1999 2002 2018 Continuation-Passing Style (CPS)

Un-impossibility result

Key insights:

  • Past work doesn’t scale in the obvious way

for dependent type theory

slide-27
SLIDE 27

Type-Preserving Closure Conversion

Closure Conversion

Key problem:

  • Which axioms does past work rely on, and

can we use them?

slide-28
SLIDE 28

Type-Preserving Closure Conversion

The standard (non-dependent) type-preserving translation

Γ ⊢ f : A → B

Goal: Translate

slide-29
SLIDE 29

Type-Preserving Closure Conversion

The standard (non-dependent) type-preserving translation

Γ ⊢ f : A → B

Goal: Translate Takes 1 argument, of type A

slide-30
SLIDE 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

slide-31
SLIDE 31

Type-Preserving Closure Conversion

The standard (non-dependent) type-preserving translation

Γ ⊢ f : A → B

Goal: Translate And can refer to lexical variables

slide-32
SLIDE 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)

slide-33
SLIDE 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).

slide-34
SLIDE 34

Type-Preserving Closure Conversion

The standard (non-dependent) type-preserving translation

How do we implement a typed closure?

Γ+ ⊢ ⟨Γ+, f+⟩ : Closure(A+ → B+) ·

slide-35
SLIDE 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:

apply c x

def

= (snd c) (fst c) x

Γ+ ⊢ ⟨Γ+, f+⟩ : Pair(Γ+, Γ+ → A+ → B+)

slide-36
SLIDE 36
  • 1. Not as pairs:

apply c x

def

= (snd c) (fst c) x

How do we implement a typed closure?

z : Nat ⊢ f : Nat → Nat ⊢ f′ : Nat → Nat

Γ+ ⊢ ⟨Γ+, f+⟩ : Pair(Γ+, Γ+ → A+ → B+)

✓ Equal

slide-37
SLIDE 37
  • 1. Not as pairs:

apply c x

def

= (snd c) (fst c) x

How do we implement a typed closure?

z : Nat ⊢ f : Nat → Nat ⊢ f′ : Nat → Nat

✓ X Equal Not equal

⊢ ⟨(), f′+⟩ : Pair((), () → Nat → Nat)

⊢ ⟨z, f+⟩ : Pair((z : Nat), (z : Nat) → Nat → Nat)

Γ+ ⊢ ⟨Γ+, f+⟩ : Pair(Γ+, Γ+ → A+ → B+)

slide-38
SLIDE 38
  • 1. Not as pairs:

apply c x

def

= (snd c) (fst c) x

How do we implement a typed closure? X Not Secure

extract_hidden_data c

def

= fst c

Γ+ ⊢ ⟨Γ+, f+⟩ : Pair(Γ+, Γ+ → A+ → B+)

slide-39
SLIDE 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:

apply c x

def

= (snd c) (fst c) x

Γ+ ⊢ ⟨Γ+, f+⟩ : Pair(Γ+, Γ+ → A+ → B+)

slide-40
SLIDE 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:

apply c x

def

= (snd c) (fst c) x

  • 2. As existential pairs:

Γ+ ⊢ ⟨Γ+, f+⟩ : ∃α.(α, (α → A+ → B+))

Γ+ ⊢ ⟨Γ+, f+⟩ : Pair(Γ+, Γ+ → A+ → B+)

apply c x

def

= unpack⟨α, p⟩in (snd c) (fst c) x

slide-41
SLIDE 41

How do we implement a typed closure?

z : Nat ⊢ f : Nat → Nat ⊢ f′ : Nat → Nat

✓ Equal

  • 2. As existential pairs:

Γ+ ⊢ ⟨Γ+, f+⟩ : ∃α.(α, (α → A+ → B+))

⊢ ⟨(), f′+⟩ : ∃α.(α,α → Nat → Nat) ⊢ ⟨z, f+⟩ : ∃α.(α,α → Nat → Nat)

✓ Equal

apply c x

def

= unpack⟨α, p⟩in (snd c) (fst c) x

slide-42
SLIDE 42

How do we implement a typed closure?

  • 2. As existential pairs:

Γ+ ⊢ ⟨Γ+, f+⟩ : ∃α.(α, (α → A+ → B+))

apply c x

def

= unpack⟨α, p⟩in (snd c) (fst c) x

Not definable

extract_hidden_data c

def

= fst c

✓Secure X

slide-43
SLIDE 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+))

apply c x

def

= unpack⟨α, p⟩in (snd c) (fst c) x

slide-44
SLIDE 44

Dependent-Type-Preserving Closure Conversion

Goal: Translate Γ ⊢ f : Π x : A. B

slide-45
SLIDE 45

Takes 1 argument, x, of type A.

Dependent-Type-Preserving Closure Conversion

Goal: Translate Γ ⊢ f : Π x : A. B

slide-46
SLIDE 46

Returns result of type B.

Dependent-Type-Preserving Closure Conversion

Goal: Translate Γ ⊢ f : Π x : A. B

slide-47
SLIDE 47

Refer to lexical variables

Dependent-Type-Preserving Closure Conversion

Goal: Translate Γ ⊢ f : Π x : A. B

slide-48
SLIDE 48

And so can types: types can depend on terms

Dependent-Type-Preserving Closure Conversion

Goal: Translate Γ ⊢ f : Π x : A. B

slide-49
SLIDE 49

Dependent-Type-Preserving Closure Conversion

Goal: Translate Γ ⊢ f : Π x : A. B

div : Π x : Nat. Π y : Nat. Π p : y 0. Int

The term-level inequality function The term-level 0

slide-50
SLIDE 50

Dependent-Type-Preserving Closure Conversion

Goal: Translate Γ ⊢ f : Π x : A. B

slide-51
SLIDE 51

Dependent-Type-Preserving Closure Conversion

Goal: Translate To: Where: Code pointers are closed
 (can be heap allocated)

Γ ⊢ f : Π x : A. B Γ+ ⊢ ⟨Γ+, f+⟩ : Closure(x : A+ → B+)

· ⊢ f+ : ΠΓ+.Π x : A+. B+

slide-52
SLIDE 52

How do we implement a dependently typed closure? Hint: not as pairs

Dependent-Type-Preserving Closure Conversion

Γ+ ⊢ ⟨Γ+, f+⟩ : Closure(x : A+ → B+)

slide-53
SLIDE 53

How do we implement a dependently typed closure? Hint: not as pairs

Dependent-Type-Preserving Closure Conversion

Γ+ ⊢ ⟨Γ+, f+⟩ : Closure(x : A+ → B+)

Hint: existential pairs don’t work either

slide-54
SLIDE 54

Digression on the nature of existence

slide-55
SLIDE 55

On existence

Γ,α : Type ⊢ A : Type Γ ⊢ ∃α : Type. A : Type

Key problem:

  • Which axioms does past work rely on, and

can we use them?

slide-56
SLIDE 56

On existence

Γ,α : Type ⊢ A : Type Γ ⊢ ∃α : Type. A : Type

If A is a Type, 
 under the assumption that α is a Type Then “there exists an α such that A holds of α” is a Type.

slide-57
SLIDE 57

On existence

Γ,α : Type ⊢ A : Type Γ ⊢ ∃α : Type. A : Type

If A is a Type, 
 under the assumption that α is a Type Then “there exists an α such that A holds of α” is a Type.

slide-58
SLIDE 58

On existence

Γ,α : Type ⊢ A : Type Γ ⊢ ∃α : Type. A : Type

If A is a Type, 
 under the assumption that α is a Type Then “there exists an α such that A holds of α” is a Type.

slide-59
SLIDE 59

On existence

Properties:

  • 1. Existence is impredicative


(formed by quantifying over Types including itself)

  • 2. Existence is computationally relevant


(terms of this type can be used when running a program)

  • 3. Existence is parametric in α


(terms cannot make inspect α at run-time)

  • 4. Existence can be used in large elimination


(we can compute types from term of existential types)

Γ,α : Type ⊢ A : Type Γ ⊢ ∃α : Type. A : Type

slide-60
SLIDE 60

On existence

Properties:

  • 1. Existence is impredicative


(formed by quantifying over Types including itself)

  • 2. Existence is computationally relevant


(terms of this type can be used when running a program)

  • 3. Existence is parametric in α


(terms cannot make inspect α at run-time)

  • 4. Existence can be used in large elimination


(we can compute types from term of existential types)

Γ,α : Type ⊢ A : Type Γ ⊢ ∃α : Type. A : Type

slide-61
SLIDE 61

On existence

Properties:

  • 1. Existence is impredicative


(formed by quantifying over Types including itself)

  • 2. Existence is computationally relevant


(terms of this type can be used when running a program)

  • 3. Existence is parametric in α


(terms cannot make inspect α at run-time)

  • 4. Existence can be used in large elimination


(we can compute types from term of existential types)

Γ,α : Type ⊢ A : Type Γ ⊢ ∃α : Type. A : Type

slide-62
SLIDE 62

On existence

Properties:

  • 1. Existence is impredicative


(formed by quantifying over Types including itself)

  • 2. Existence is computationally relevant


(terms of this type can be used when running a program)

  • 3. Existence is parametric in α


(terms cannot make inspect α at run-time)

  • 4. Existence can be used in large elimination


(we can compute types from term of existential types)

Γ,α : Type ⊢ A : Type Γ ⊢ ∃α : Type. A : Type

slide-63
SLIDE 63

On existence

Properties:

  • 1. Existence is impredicative


(formed by quantifying over Types including itself)

  • 2. Existence is computationally relevant


(terms of this type can be used when running a program)

  • 3. Existence is parametric in α


(terms cannot make inspect α at run-time)

  • 4. Existence can be used in large elimination


(we can compute types from term of existential types)

Γ,α : Type ⊢ A : Type Γ ⊢ ∃α : Type. A : Type

slide-64
SLIDE 64

Γ,α : Type ⊢ A : Type Γ ⊢ ∃α : Type. A : Type

Existence is incompatible with reality

We could add this axiom, in theory, but in reality:

slide-65
SLIDE 65
  • 1. Some type theories do not admit impredicativity


(Agda, Coq for computational Types (by default))

  • 2. Some type theories do not admit parametricity


(Agda, Coq by default)

  • 3. These properties are incompatible with set theory:
  • 1. impredicativity + large elimination + excluded middle


implies False

  • 2. impredicativity + relevance + excluded middle


implies False

Γ,α : Type ⊢ A : Type Γ ⊢ ∃α : Type. A : Type

Existence is incompatible with reality

We could add this axiom, in theory, but in reality:

slide-66
SLIDE 66
  • 1. Some type theories do not admit impredicativity


(Agda, Coq for computational Types (by default))

  • 2. Some type theories do not admit parametricity


(Agda, Coq by default)

  • 3. These properties are incompatible with set theory:
  • 1. impredicativity + large elimination + excluded middle


implies False

  • 2. impredicativity + relevance + excluded middle


implies False

Γ,α : Type ⊢ A : Type Γ ⊢ ∃α : Type. A : Type

Existence is incompatible with reality

We could add this axiom, in theory, but in reality:

slide-67
SLIDE 67
  • 1. Some type theories do not admit impredicativity


(Agda, Coq for computational Types (by default))

  • 2. Some type theories do not admit parametricity


(Agda, Coq by default)

  • 3. Incompatible with set theory:
  • 1. impredicativity + large elimination + excluded middle


implies False

  • 2. impredicativity + relevance + excluded middle


implies False

Γ,α : Type ⊢ A : Type Γ ⊢ ∃α : Type. A : Type

Existence is incompatible with reality

We could add this axiom, in theory, but in reality:

slide-68
SLIDE 68

How do we implement a dependently typed closure?

  • 1. not as pairs
  • 2. not as existential pairs

Dependent-Type-Preserving Closure Conversion

Γ+ ⊢ ⟨Γ+, f+⟩ : Closure(x : A+ → B+)

slide-69
SLIDE 69

How do we implement a dependently typed closure?

  • 1. not as pairs
  • 2. not as existential pairs
  • 3. as …. closures

Dependent-Type-Preserving Closure Conversion

Γ+ ⊢ ⟨Γ+, f+⟩ : Closure(x : A+ → B+)

Pros:

  • Works

Cons:

  • Require proof of consistency
  • Unclear how past work (optimizations?) applies
slide-70
SLIDE 70

Closure Axioms

slide-71
SLIDE 71

Closure Axioms

Well-typed code pointer

·, n : A′, x : A ⊢ e : B Γ ⊢ λ n : A′, x : A. e : Code (n : A′, x : A). B [Code]

slide-72
SLIDE 72

Closure Axioms

Well-typed code pointer

·, n : A′, x : A ⊢ e : B Γ ⊢ λ n : A′, x : A. e : Code (n : A′, x : A). B [Code]

A code pointer, 
 pointing to e, 
 which expects two arguments, n and x

slide-73
SLIDE 73

Closure Axioms

Well-typed code pointer

·, n : A′, x : A ⊢ e : B Γ ⊢ λ n : A′, x : A. e : Code (n : A′, x : A). B [Code]

Has a dependent code type (A can depend on n, B can depend on n and x)

slide-74
SLIDE 74

Closure Axioms

Well-typed code pointer

·, n : A′, x : A ⊢ e : B Γ ⊢ λ n : A′, x : A. e : Code (n : A′, x : A). B [Code]

When e is a well-typed block of code with references only to declared arguments n and x.

slide-75
SLIDE 75

Closure Axioms

Well-typed code pointer

·, n : A′, x : A ⊢ e : B Γ ⊢ λ n : A′, x : A. e : Code (n : A′, x : A). B [Code]

When e is a well-typed block of code with references only to declared arguments n and x.

slide-76
SLIDE 76

Closure Axioms

Well-typed closure

Γ ⊢ e : Code (n : A′, x : A). B Γ ⊢ e′ : A′ Γ ⊢ ⟨⟨e, e′⟩⟩ : Π x : A[e′/n]. B[e′/n] [Clo]

A closure has code pointer e, 
 data e’

slide-77
SLIDE 77

Closure Axioms

Well-typed closure

Γ ⊢ e : Code (n : A′, x : A). B Γ ⊢ e′ : A′ Γ ⊢ ⟨⟨e, e′⟩⟩ : Π x : A[e′/n]. B[e′/n] [Clo]

With the data e’ substituted for n in the types
 (dependent closure type)

slide-78
SLIDE 78

Closure Axioms

Well-typed code pointer

·, n : A′, x : A ⊢ e : B Γ ⊢ λ n : A′, x : A. e : Code (n : A′, x : A). B [Code]

Well-typed closure

Γ ⊢ e : Code (n : A′, x : A). B Γ ⊢ e′ : A′ Γ ⊢ ⟨⟨e, e′⟩⟩ : Π x : A[e′/n]. B[e′/n] [Clo]

slide-79
SLIDE 79

Well-typed code pointer

·, n : A′, x : A ⊢ e : B Γ ⊢ λ n : A′, x : A. e : Code (n : A′, x : A). B [Code]

Well-typed closure

Γ ⊢ e : Code (n : A′, x : A). B Γ ⊢ e′ : A′ Γ ⊢ ⟨⟨e, e′⟩⟩ : Π x : A[e′/n]. B[e′/n] [Clo]

Closure Axioms

We show

  • 1. Logical Consistency


Orthogonal to impredicativity, relevance, parametricity, large elimination, and set theory

  • 2. Type safety


IR programs type and memory safe; validate compiler

  • utput and enforce safe linking via type checking
slide-80
SLIDE 80

Closure Conversion

(Π x : A. B)+ def = Π x : A+. B+ (λ x : A. e)+ def = ⟨⟨(λ (n : Σ (xi : A+

i . . . ), x : let ⟨xi . . .⟩ = n in A+).

let ⟨xi . . .⟩ = n in e), ⟨xi . . .⟩⟩⟩ where xi : Ai . . . are the free variables of e and A

Pair code pointer with free variables, as explicit data
 (standard)

slide-81
SLIDE 81

Closure Conversion

(Π x : A. B)+ def = Π x : A+. B+ (λ x : A. e)+ def = ⟨⟨(λ (n : Σ (xi : A+

i . . . ), x : let ⟨xi . . .⟩ = n in A+).

let ⟨xi . . .⟩ = n in e), ⟨xi . . .⟩⟩⟩ where xi : Ai . . . are the free variables of e and A

Bind free variables in term by projecting from data argument (standard)

slide-82
SLIDE 82

Closure Conversion

(Π x : A. B)+ def = Π x : A+. B+ (λ x : A. e)+ def = ⟨⟨(λ (n : Σ (xi : A+

i . . . ), x : let ⟨xi . . .⟩ = n in A+).

let ⟨xi . . .⟩ = n in e), ⟨xi . . .⟩⟩⟩ where xi : Ai . . . are the free variables of e and A

Bind free variables in type annotation (dependency)

slide-83
SLIDE 83

Closure Conversion

(Π x : A. B)+ def = Π x : A+. B+ (λ x : A. e)+ def = ⟨⟨(λ (n : Σ (xi : A+

i . . . ), x : let ⟨xi . . .⟩ = n in A+).

let ⟨xi . . .⟩ = n in e), ⟨xi . . .⟩⟩⟩ where xi : Ai . . . are the free variables of e and A

Bind each xi in the type annotation for xi+1 (dependency)

slide-84
SLIDE 84
  • Theorem. (Type Preservation)


If then
 


e

translates to

: A

e+ : A+

Type Preservation

slide-85
SLIDE 85
  • Theorem. (Correctness of Separate Compilation)


If then

e γ v e+ γ+ v’ v+

Type Preservation

slide-86
SLIDE 86

Future Work

Closure Conversion Allocation Code generation ✓ ✓

slide-87
SLIDE 87

Future Work

✓ ✓ Scaling source feature set:

  • recursion (optimizing)
  • universe hierarchy
slide-88
SLIDE 88

Future Work

✓ ✓ Foundational challenges:
 linear + dependent types

slide-89
SLIDE 89

Typed Closure Conversion of the 
 Calculus of Constructions

https://williamjbowman.com/#cccc Dependently typed closures require abandoning past work on type preservation (existential types) to be practical. ✓ ✓ ✓ ✓ ✓

Link