SLIDE 1
CPS Translation of Dependent Types
Amal Ahmed
Northeastern University Work in progress, with Nick Rioux and William Bowman
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 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 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 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
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
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 Typed CPS: STLC (call by name)
Computation translation Value translation
τ ÷
τ + τ ÷ = (τ + → ⊥) → ⊥
bool+ = bool (τ1 → τ2)+ = τ ÷
1 → τ ÷ 2
SLIDE 9 Typed CPS: STLC (call by name)
Computation translation Value translation
τ ÷
τ + τ ÷ = (τ + → ⊥) → ⊥
bool+ = bool (τ1 → τ2)+ = τ ÷
1 → τ ÷ 2
τ ÷
1 → (τ + 2 → ⊥) → ⊥
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÷
X+
. . .
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 : ∗
÷
X′
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 : ∗
÷
X′ Γ ⊢ A : κ
+
A′
SLIDE 13 Typed CPS: pair … warm up
k
1 , e÷ 2
÷
e÷
1
Γ ⊢ e2 : Y [e1/x]
÷
e÷
2
Γ ⊢ ⟨e1, e2⟩ : Σ x : X. Y
÷
λ k : (Σ x : X÷. Y ÷) → ⊥. : X÷ : Y ÷[e÷
1 /x]
SLIDE 14 Typed CPS: pair … warm up
k
1 , e÷ 2
÷
e÷
1
Γ ⊢ e2 : Y [e1/x]
÷
e÷
2
Γ ⊢ ⟨e1, e2⟩ : Σ x : X. Y
÷
λ k : (Σ x : X÷. Y ÷) → ⊥. : X÷ : Y ÷[e÷
1 /x]
SLIDE 15 Typed CPS: pair … warm up
k
1 , e÷ 2
÷
e÷
1
Γ ⊢ e2 : Y [e1/x]
÷
e÷
2
Γ ⊢ ⟨e1, e2⟩ : Σ x : X. Y
÷
λ k : (Σ x : X÷. Y ÷) → ⊥. : X÷ : Y ÷[e÷
1 /x]
SLIDE 16 Typed CPS: pair … warm up
k
1 , e÷ 2
÷
e÷
1
Γ ⊢ e2 : Y [e1/x]
÷
e÷
2
Γ ⊢ ⟨e1, e2⟩ : Σ x : X. Y
÷
λ k : (Σ x : X÷. Y ÷) → ⊥. : X÷ : Y ÷[e÷
1 /x]
SLIDE 17 Typed CPS: pair … warm up
k
1 , e÷ 2
÷
e÷
1
Γ ⊢ e2 : Y [e1/x]
÷
e÷
2
Γ ⊢ ⟨e1, e2⟩ : Σ x : X. Y
÷
λ k : (Σ x : X÷. Y ÷) → ⊥. : X÷ : Y ÷[e÷
1 /x]
SLIDE 18 Typed CPS: pair … warm up
k
1 , e÷ 2
÷
e÷
1
Γ ⊢ e2 : Y [e1/x]
÷
e÷
2
Γ ⊢ ⟨e1, e2⟩ : Σ x : X. Y
÷
λ k : (Σ x : X÷. Y ÷) → ⊥. : X÷ : Y ÷[e÷
1 /x]
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 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 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 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 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 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 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 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 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 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
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
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
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
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 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 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 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 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
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
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
Γ ⊢ 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 Γ ⊢ 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
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 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
Questions?
SLIDE 44 Γ ⊢ e1 : X
÷
e÷
1
Γ ⊢ e2 : Y [e1/x]
÷
e÷
2
Γ ⊢ ⟨e1, e2⟩ : Σ x : X. Y
÷
λ α : ∗ . λ k : (Π : (Σ x : X÷. Y ÷). α). k
1