Compiling Dependent Types Much recent focus on verified compilation - - PowerPoint PPT Presentation

compiling dependent types
SMART_READER_LITE
LIVE PREVIEW

Compiling Dependent Types Much recent focus on verified compilation - - PowerPoint PPT Presentation

CPS Translation of Dependent Types Amal Ahmed Northeastern University Work in progress, with Nick Rioux and William Bowman Compiling Dependent Types Much recent focus on verified compilation of dependently typed languages: Coqonut, CertiCoq


slide-1
SLIDE 1

CPS Translation of Dependent Types

Amal Ahmed

Northeastern University Work in progress, with Nick Rioux and William Bowman

slide-2
SLIDE 2

Compiling Dependent Types

  • Much recent focus on verified compilation of dependently

typed languages: Coqonut, CertiCoq

  • Our goal: type-preserving, compositional verified

compilation of Coq/Agda

  • Types at target level can be used to provide protection

from target contexts/attackers (fully abstract compilation)

slide-3
SLIDE 3

CPS Translation of Dependent Types

Prior work

  • CPS Translations and Applications: The Cube and Beyond

[Barthe, Hatcliff, Sorenson HOSC’99]

  • [Barthe & Uustalu PEPM’02]
  • Good news: “CPS translations… generalize for

dependently typed calculi”

  • Bad news: “No translation is possible along the same lines

for small -types and sum types with dependent case” Σ

slide-4
SLIDE 4

CPS Translation of Dependent Types

Prior work

  • CPS Translations and Applications: The Cube and Beyond

[Barthe, Hatcliff, Sorenson HOSC’99]

  • [Barthe & Uustalu PEPM’02]
  • Good news: “CPS translations… generalize for

dependently typed calculi”

  • Bad news: “No translation is possible along the same lines

for small -types and sum types with dependent case” Σ

slide-5
SLIDE 5

CPS Translation of Dependent Types

Prior work

  • CPS Translations and Applications: The Cube and Beyond

[Barthe, Hatcliff, Sorenson HOSC’99]

  • [Barthe & Uustalu PEPM’02]
  • Good news: “CPS translations… generalize for

dependently typed calculi”

  • Bad news: “No translation is possible along the same lines

for small -types and sum types with dependent case” Σ

slide-6
SLIDE 6

This Talk: CPS-ing CoC with

Kinds κ ::= ∗ | Π x : X. κ | Π α : κ1. κ2 Types A, X ::= α | Π x : X. Y | Π α : κ. X | Σ x : X. Y | λ x : X. A | A e | λ α : κ. A | A B | e1 =X e2 Terms e ::= x | λ x : X. e | λ α : κ. e | e1 e2 | e A | ⟨e1, e2⟩ | fst e | snd e | refl Values v ::= x | λ x : X. e | ⟨e1, e2⟩ | refl Environments Γ ::= · | Γ, x : X | Γ, α : κ

Σ

slide-7
SLIDE 7

This Talk: CPS-ing CoC with

Kinds κ ::= ∗ | Π x : X. κ | Π α : κ1. κ2 Types A, X ::= α | Π x : X. Y | Π α : κ. X | Σ x : X. Y | λ x : X. A | A e | λ α : κ. A | A B | e1 =X e2 Terms e ::= x | λ x : X. e | λ α : κ. e | e1 e2 | e A | ⟨e1, e2⟩ | fst e | snd e | refl Values v ::= x | λ x : X. e | ⟨e1, e2⟩ | refl Environments Γ ::= · | Γ, x : X | Γ, α : κ

denote types of kind X,Y ∗

Σ

slide-8
SLIDE 8

Typed CPS: STLC (call by name)

Computation translation Value translation

τ ÷

τ + τ ÷ = (τ + → ⊥) → ⊥

bool+ = bool (τ1 → τ2)+ = τ ÷

1 → τ ÷ 2

slide-9
SLIDE 9

Typed CPS: STLC (call by name)

Computation translation Value translation

τ ÷

τ + τ ÷ = (τ + → ⊥) → ⊥

bool+ = bool (τ1 → τ2)+ = τ ÷

1 → τ ÷ 2

τ ÷

1 → (τ + 2 → ⊥) → ⊥

slide-10
SLIDE 10

Computation translation Value translation

Typed CPS: Dependent Types (cbn)

X÷ = (X+ → ⊥) → ⊥

α+ = α (Π x : X. Y )+ = Π x : X÷. Y ÷ (Σ x : X. Y )+ = Σ x : X÷. Y ÷

X+

. . .

slide-11
SLIDE 11

Computation translation Value translation

Typed CPS: Dependent Types (cbn)

X÷ = (X+ → ⊥) → ⊥

α+ = α (Π x : X. Y )+ = Π x : X÷. Y ÷ (Σ x : X. Y )+ = Σ x : X÷. Y ÷

X+

. . . Γ ⊢ X : ∗

÷

X′

slide-12
SLIDE 12

Computation translation Value translation

Typed CPS: Dependent Types (cbn)

X÷ = (X+ → ⊥) → ⊥

α+ = α (Π x : X. Y )+ = Π x : X÷. Y ÷ (Σ x : X. Y )+ = Σ x : X÷. Y ÷

X+

. . . Γ ⊢ X : ∗

÷

X′ Γ ⊢ A : κ

+

A′

slide-13
SLIDE 13

Typed CPS: pair … warm up

k

1 , e÷ 2

  • Γ ⊢ e1 : X

÷

1

Γ ⊢ e2 : Y [e1/x]

÷

2

Γ ⊢ ⟨e1, e2⟩ : Σ x : X. Y

÷

λ k : (Σ x : X÷. Y ÷) → ⊥. : X÷ : Y ÷[e÷

1 /x]

slide-14
SLIDE 14

Typed CPS: pair … warm up

k

1 , e÷ 2

  • Γ ⊢ e1 : X

÷

1

Γ ⊢ e2 : Y [e1/x]

÷

2

Γ ⊢ ⟨e1, e2⟩ : Σ x : X. Y

÷

λ k : (Σ x : X÷. Y ÷) → ⊥. : X÷ : Y ÷[e÷

1 /x]

slide-15
SLIDE 15

Typed CPS: pair … warm up

k

1 , e÷ 2

  • Γ ⊢ e1 : X

÷

1

Γ ⊢ e2 : Y [e1/x]

÷

2

Γ ⊢ ⟨e1, e2⟩ : Σ x : X. Y

÷

λ k : (Σ x : X÷. Y ÷) → ⊥. : X÷ : Y ÷[e÷

1 /x]

slide-16
SLIDE 16

Typed CPS: pair … warm up

k

1 , e÷ 2

  • Γ ⊢ e1 : X

÷

1

Γ ⊢ e2 : Y [e1/x]

÷

2

Γ ⊢ ⟨e1, e2⟩ : Σ x : X. Y

÷

λ k : (Σ x : X÷. Y ÷) → ⊥. : X÷ : Y ÷[e÷

1 /x]

slide-17
SLIDE 17

Typed CPS: pair … warm up

k

1 , e÷ 2

  • Γ ⊢ e1 : X

÷

1

Γ ⊢ e2 : Y [e1/x]

÷

2

Γ ⊢ ⟨e1, e2⟩ : Σ x : X. Y

÷

λ k : (Σ x : X÷. Y ÷) → ⊥. : X÷ : Y ÷[e÷

1 /x]

slide-18
SLIDE 18

Typed CPS: pair … warm up

k

1 , e÷ 2

  • Γ ⊢ e1 : X

÷

1

Γ ⊢ e2 : Y [e1/x]

÷

2

Γ ⊢ ⟨e1, e2⟩ : Σ x : X. Y

÷

λ k : (Σ x : X÷. Y ÷) → ⊥. : X÷ : Y ÷[e÷

1 /x]

slide-19
SLIDE 19

Typed CPS: fst … all’s well

e÷ (λ p : (Σ x : X÷. Y ÷). let z = fst p in z k) : (Σ x : X÷. Y ÷ → ⊥) → ⊥ Γ ⊢ e : Σ x : X. Y

÷

e÷ Γ ⊢ fst e : X

÷

λ k : X+ → ⊥.

slide-20
SLIDE 20

Typed CPS: fst … all’s well

e÷ (λ p : (Σ x : X÷. Y ÷). let z = fst p in z k) : (Σ x : X÷. Y ÷ → ⊥) → ⊥ Γ ⊢ e : Σ x : X. Y

÷

e÷ Γ ⊢ fst e : X

÷

λ k : X+ → ⊥.

slide-21
SLIDE 21

Typed CPS: fst … all’s well

e÷ (λ p : (Σ x : X÷. Y ÷). let z = fst p in z k) : (Σ x : X÷. Y ÷ → ⊥) → ⊥ Γ ⊢ e : Σ x : X. Y

÷

e÷ Γ ⊢ fst e : X

÷

λ k : X+ → ⊥.

slide-22
SLIDE 22

Typed CPS: fst … all’s well

e÷ (λ p : (Σ x : X÷. Y ÷). let z = fst p in z k) : (Σ x : X÷. Y ÷ → ⊥) → ⊥ Γ ⊢ e : Σ x : X. Y

÷

e÷ Γ ⊢ fst e : X

÷

λ k : X+ → ⊥.

slide-23
SLIDE 23

Typed CPS: snd … the evil case!

Γ ⊢ e : Σ x : X. Y

÷

e÷ Γ ⊢ snd e : Y [fst e/x]

÷

λ k : (Y [fst e/x])+ → ⊥. : (Σ x : X÷. Y ÷ → ⊥) → ⊥ e÷ (λ p : (Σ x : X÷. Y ÷). let y = snd p in y k)

slide-24
SLIDE 24

Typed CPS: snd … the evil case!

Γ ⊢ e : Σ x : X. Y

÷

e÷ Γ ⊢ snd e : Y [fst e/x]

÷

λ k : (Y [fst e/x])+ → ⊥. : (Σ x : X÷. Y ÷ → ⊥) → ⊥ e÷ (λ p : (Σ x : X÷. Y ÷). let y = snd p in y k)

slide-25
SLIDE 25

Typed CPS: snd … the evil case!

Γ ⊢ e : Σ x : X. Y

÷

e÷ Γ ⊢ snd e : Y [fst e/x]

÷

λ k : (Y [fst e/x])+ → ⊥. : (Σ x : X÷. Y ÷ → ⊥) → ⊥ e÷ (λ p : (Σ x : X÷. Y ÷). let y = snd p in y k)

slide-26
SLIDE 26

Typed CPS: snd … the evil case!

Γ ⊢ e : Σ x : X. Y

÷

e÷ Γ ⊢ snd e : Y [fst e/x]

÷

λ k : (Y [fst e/x])+ → ⊥. : (Σ x : X÷. Y ÷ → ⊥) → ⊥ e÷ (λ p : (Σ x : X÷. Y ÷). let y = snd p in y k) Y ÷[fst p/x] Y +[(fst e)÷/x] → ⊥

slide-27
SLIDE 27

Typed CPS: snd … the evil case!

Γ ⊢ e : Σ x : X. Y

÷

e÷ Γ ⊢ snd e : Y [fst e/x]

÷

λ k : (Y [fst e/x])+ → ⊥. : (Σ x : X÷. Y ÷ → ⊥) → ⊥ e÷ (λ p : (Σ x : X÷. Y ÷). let y = snd p in y k) (Y +[fst p/x] → ⊥) → ⊥ Y +[(fst e)÷/x] → ⊥

slide-28
SLIDE 28

Typed CPS: snd … the evil case!

Γ ⊢ e : Σ x : X. Y

÷

e÷ Γ ⊢ snd e : Y [fst e/x]

÷

λ k : (Y [fst e/x])+ → ⊥. : (Σ x : X÷. Y ÷ → ⊥) → ⊥ e÷ (λ p : (Σ x : X÷. Y ÷). let y = snd p in y k) (Y +[fst p/x] → ⊥) → ⊥ Y +[(fst e)÷/x] → ⊥

slide-29
SLIDE 29

Reasoning about value passed to cont.

Want to extract the content of type inside Idea: change the type translation Now, we can extract via: e : (X+ → ⊥) → ⊥ e : Π α : ∗ . (X+ → α) → α e X+ id X+ e X÷ = Π α : ∗ . (X+ → α) → α

slide-30
SLIDE 30

Reasoning about value passed to cont.

Want to extract the content of type inside Idea: change the type translation Now, we can extract via: e : (X+ → ⊥) → ⊥ e : Π α : ∗ . (X+ → α) → α e X+ id X+ e X÷ = Π α : ∗ . (X+ → α) → α

slide-31
SLIDE 31

Reasoning about value passed to cont.

Want to extract the content of type inside Idea: change the type translation Now, we can extract via: e : (X+ → ⊥) → ⊥ e : Π α : ∗ . (X+ → α) → α e X+ id X+ e X÷ = Π α : ∗ . (X+ → α) → α

slide-32
SLIDE 32

Reasoning about value passed to cont.

Want to extract the content of type inside Idea: change the type translation Now, we can extract via: e : (X+ → ⊥) → ⊥ e : Π α : ∗ . (X+ → α) → α e X+ id X+ e X÷ = Π α : ∗ . (X+ → α) → α

slide-33
SLIDE 33

Typed CPS: snd again

Γ ⊢ e : Σ x : X. Y

÷

e÷ Γ ⊢ snd e : Y [fst e/x]

÷

λ α : ∗ . λ k : (Y [fst e/x])+ → α. e÷ α (λ p : (Σ x : X÷. Y ÷). let y = snd p in y α k)

slide-34
SLIDE 34

Typed CPS: snd again

Γ ⊢ e : Σ x : X. Y

÷

e÷ Γ ⊢ snd e : Y [fst e/x]

÷

λ α : ∗ . λ k : (Y [fst e/x])+ → α. e÷ α (λ p : (Σ x : X÷. Y ÷). let y = snd p in y α k) e÷ (Σ x : X÷. Y ÷) id

slide-35
SLIDE 35

Typed CPS: snd again

Γ ⊢ e : Σ x : X. Y

÷

e÷ Γ ⊢ snd e : Y [fst e/x]

÷

λ α : ∗ . λ k : (Y [fst e/x])+ → α. e÷ α (λ p : (Σ x : X÷. Y ÷). let y = snd p in y α k) e÷ (Σ x : X÷. Y ÷) id (Y +[fst p/x] → α) → α Y +[(fst e)÷/x] → α

slide-36
SLIDE 36

Typed CPS: snd again

Γ ⊢ e : Σ x : X. Y

÷

e÷ Γ ⊢ snd e : Y [fst e/x]

÷

λ α : ∗ . λ k : (Y [fst e/x])+ → α. e÷ α (λ p : (Σ x : X÷. Y ÷). let y = snd p in y α k) e÷ (Σ x : X÷. Y ÷) id (Y +[fst p/x] → α) → α Y +[(fst e)÷/x] → α

2 issues:

  • how to typecheck above continuation
  • how to prove fst p ≡ (fst e)÷
slide-37
SLIDE 37

For Issue 1: New Typing Rule

Γ ⊢ e : Π α : ∗ . (X → α) → α Γ ⊢ Y : ∗ Γ, x : X, u : x =X e X id ⊢ eb : Y Γ ⊢ e Y (λ x : X. eb) : Y

slide-38
SLIDE 38

For Issue 1: New Typing Rule

Γ ⊢ e : Π α : ∗ . (X → α) → α Γ ⊢ Y : ∗ Γ, x : X, u : x =X e X id ⊢ eb : Y Γ ⊢ e Y (λ x : X. eb) : Y

Note: may apply continuation many times…

e

slide-39
SLIDE 39

Γ ⊢ e : Π α : ∗ . (X → α) ⊸ α Γ ⊢ Y : ∗ Γ, x : X, u : x =X e X id ⊢ eb : Y Γ ⊢ e Y (λ x : X. eb) : Y

For Issue 1: New Typing Rule

Need linearity to ensure safe execution of eb

slide-40
SLIDE 40

Γ ⊢ e : Π α : ∗ . (X → α) ⊸ α Γ ⊢ Y : ∗ Γ, x : X, u : x =X e X id ⊢ eb : Y Γ ⊢ e Y (λ x : X. eb) : Y

For Issue 1: New Typing Rule

Need linearity to ensure safe execution of eb

Need to prove soundness of the above rule — requires internalizing parametricity:

  • [Krishnaswami-Dreyer, CSL’13]
  • [Bernardy et al., ICFP’10, JFP’12]
slide-41
SLIDE 41

For Issue 2: Parametricity Condition

Given that we can prove if the parametricity condition holds. Parametricity Condition: [Wadler’89] If and then fst p ≡ (fst e)÷ p ≡ e÷ (Σ x : X÷. Y ÷) id e : Π α : ∗ . (X → α) ⊸ α k : X → Y e Y k ≡ k (e X id)

slide-42
SLIDE 42

Type-preserving CPS for Coq/Agda

  • do the same issues arise if we translate to ANF?

What is the “right” type for a CPS’d term?

  • reversibility of CPS and linear use of continuation seem

critical (e.g., for fully abstract compilation, interoperability with direct-style languages)

Concluding thoughts…

τ ÷ = ∀α. (τ + → α) ⊸ α τ ÷ = (τ + → ⊥) ⊸ ⊥ τ ÷ = (τ + → ⊥) → ⊥

slide-43
SLIDE 43

Questions?

slide-44
SLIDE 44

Γ ⊢ e1 : X

÷

1

Γ ⊢ e2 : Y [e1/x]

÷

2

Γ ⊢ ⟨e1, e2⟩ : Σ x : X. Y

÷

λ α : ∗ . λ k : (Π : (Σ x : X÷. Y ÷). α). k

1