Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Generic Zero-Cost Reuse for Dependent Types

larrytheliquid
September 26, 2018

Generic Zero-Cost Reuse for Dependent Types

Slides for the talk about the corresponding ICFP'18 paper:
https://dl.acm.org/citation.cfm?id=3236799

larrytheliquid

September 26, 2018
Tweet

More Decks by larrytheliquid

Other Decks in Research

Transcript

  1. Generic Zero-Cost Reuse for Dependent Types Larry Diehl, Denis Firsov,

    and Aaron Stump University of Iowa September 26, 2018 @ ICFP
  2. Outline 1. Manual Reuse (review) 2. Manual Zero-Cost Reuse 3.

    Generic Zero-Cost Reuse (using combinators)
  3. data List (A : Set) : Set where nil :

    List A cons : A ! List A ! List A data Vec (A : Set) : ℕ ! Set where nil : Vec A zero cons : {n : ℕ} ! A ! Vec A n ! Vec A (suc n) v2l : {A : Set} {n : ℕ} ! Vec A n ! List A v2l nil = nil v2l (cons x xs) = cons x (v2l xs) appV2appL : AppV ! AppL appV2appL appV xs ys = v2l (appV (l2v xs) (l2v ys))
  4. data List (A : Set) : Set where nil :

    List A cons : A ! List A ! List A data Vec (A : Set) : ℕ ! Set where nil : Vec A zero cons : {n : ℕ} ! A ! Vec A n ! Vec A (suc n) v2l : {A : Set} {n : ℕ} ! Vec A n ! List A v2l nil = nil v2l (cons x xs) = cons x (v2l xs) appV2appL : AppV ! AppL appV2appL appV xs ys = v2l (appV (l2v xs) (l2v ys)) nothing happening?
  5. data List (A : Set) : Set where nil :

    List A cons : A ! List A ! List A data Vec (A : Set) : ℕ ! Set where nil : Vec A zero cons : {n : ℕ} ! A ! Vec A n ! Vec A (suc n) v2l : {A : Set} {n : ℕ} ! Vec A n ! List A v2l nil = nil v2l (cons x xs) = cons x (v2l xs) appV2appL : AppV ! AppL appV2appL appV xs ys = v2l (appV (l2v xs) (l2v ys)) nothing happening?
  6. v2l : {A : Set} {n : ℕ} ! Vec

    A n ! List A v2l nil = nil v2l (cons x xs) = cons x (v2l xs) Nothing Happening? v2lId : {A : Set} {n : ℕ} (xs : Vec A n) ! v2l xs ≡ xs ??? v2l! : {A : Set} {n : ℕ} ! Vec A n ! List A v2l! xs = xs ???
  7. v2l : {A : Set} {n : ℕ} ! Vec

    A n ! List A v2l nil = nil v2l (cons x xs) = cons x (v2l xs) Nothing Happening? v2lId : {A : Set} {n : ℕ} (xs : Vec A n) ! v2l xs ≡ xs ??? v2l! : {A : Set} {n : ℕ} ! Vec A n ! List A v2l! xs = xs ???
  8. v2l : {A : Set} {n : ℕ} ! Vec

    A n ! List A v2l nil = nil v2l (cons x xs) = cons x (v2l xs) Nothing Happening? v2lId : {A : Set} {n : ℕ} (xs : Vec A n) ! v2l xs ≡ xs ??? v2l! : {A : Set} {n : ℕ} ! Vec A n ! List A v2l! xs = xs ???
  9. v2l : {A : Set} {n : ℕ} ! Vec

    A n ! List A v2l nil = nil v2l (cons x xs) = cons x (v2l xs) Something Happening! v2lId : {A : Set} {n : ℕ} (xs : Vec A n) ! v2l xs ≡ xs v2l! : {A : Set} {n : ℕ} ! Vec A n ! List A v2l! xs = xs
  10. data List (A : Set) : Set where nilL :

    List A consL : A ! List A ! List A data Vec (A : Set) : ℕ ! Set where nilV : Vec A zero consV : {n : ℕ} ! A ! Vec A n ! Vec A (suc n) v2l : {A : Set} {n : ℕ} ! Vec A n ! List A v2l nilV = nilL v2l (consV x xs) = consL x (v2l xs) something happening! qualified constructor names
  11. data List (A : Set) : Set where nilL :

    List A consL : A ! List A ! List A data Vec (A : Set) : ℕ ! Set where nilV : Vec A zero consV : {n : ℕ} ! A ! Vec A n ! Vec A (suc n) v2l : {A : Set} {n : ℕ} ! Vec A n ! List A v2l nilV = nilL v2l (consV x xs) = consL x (v2l xs) something happening! qualified constructor names
  12. data List (A : Set) : Set where nilL :

    List A consL : A ! List A ! List A data Vec (A : Set) : ℕ ! Set where nilV : Vec A zero consV : (n : ℕ) ! A ! Vec A n ! Vec A (suc n) v2l : {A : Set} (n : ℕ) ! Vec A n ! List A v2l zero nilV = nilL v2l (suc n)(consV n x xs) = consL x (v2l n xs) something happening! explicit index args
  13. data List (A : Set) : Set where nilL :

    List A consL : A ! List A ! List A data Vec (A : Set) : ℕ ! Set where nilV : Vec A zero consV : (n : ℕ) ! A ! Vec A n ! Vec A (suc n) v2l : {A : Set} (n : ℕ) ! Vec A n ! List A v2l zero nilV = nilL v2l (suc n)(consV n x xs) = consL x (v2l n xs) something happening! explicit index args
  14. Changing Theories Now we will move from Agda’s type theory

    to another type theory (Cedille’s), where our intuition holds (“nothing happening”).
  15. Changing Theories • Primitive Inductive Types • Predicative Quantification •

    Intrinsically Typed Terms • Definitional Equality defined directly over terms • Derived Inductive Types • Impredicative Quantification • Extrinsically Typed Terms • Definitional Equality defined modulo erasing terms to untyped λ-calculus Agda Cedille
  16. Changing Theories • Primitive Inductive Types • Predicative Quantification •

    Intrinsically Typed Terms • Definitional Equality defined directly over terms • Derived Inductive Types • Impredicative Quantification • Extrinsically Typed Terms • Definitional Equality defined modulo erasing terms to untyped λ-calculus Agda Cedille t1 = t2 |t1| =βη |t2| ≜
  17. Cedille Extrinsic Calculus of Constructions + 3 Primitive Types Erased

    Function Space Heterogenous Equality Dependent Intersection
  18. Erased Function Space Erased Function Space Non-Erased Function Space Π

    x : T. T’ ∀ x : T. T’ abstraction erased
  19. data List (A : Set) : Set where nil :

    List A cons : A ! List A ! List A data Vec (A : Set) : ℕ ! Set where nil : Vec A zero cons : {n : ℕ} ! A ! Vec A n ! Vec A (suc n) Deriving Inductive Types
  20. List : ̣ ➔ ̣ = λ A. ∀ X

    : ̣. X ➔ (A ➔ X ➔ X) ➔ X. Vec : ̣ ➔ ℕ ➔ ̣ = λ A n. ∀ X : ℕ ➔ ̣. X zero ➔ (∀ n : ℕ. A ➔ X n ➔ X (suc n)) ➔ X n. nilL : ∀ A : ̣. List A = Λ A X. λ cN cC. cN. nilV : ∀ A : ̣. Vec A zero = Λ A X. λ cN cC. cN. |nilL| = |nilV| = λ cN cC. cN Church-encoding
  21. nilL : ∀ A : ̣. List A = Λ

    A X. λ cN cC. cN. nilV : ∀ A : ̣. Vec A zero = Λ A X. λ cN cC. cN. |nilL| = |nilV| = λ cN cC. cN Church-encoding Intersection Types for induction principles Mendler-encoding for efficient predecessors Actually… List : ̣ ➔ ̣ = λ A. ∀ X : ̣. X ➔ (A ➔ X ➔ X) ➔ X. Vec : ̣ ➔ ℕ ➔ ̣ = λ A n. ∀ X : ℕ ➔ ̣. X zero ➔ (∀ n : ℕ. A ➔ X n ➔ X (suc n)) ➔ X n.
  22. List : ̣ ➔ ̣ = λ A. ∀ X

    : ̣. X ➔ (A ➔ X ➔ X) ➔ X. Vec : ̣ ➔ ℕ ➔ ̣ = λ A n. ∀ X : ℕ ➔ ̣. X zero ➔ (∀ n : ℕ. A ➔ X n ➔ X (suc n)) ➔ X n. nilL : ∀ A : ̣. List A = Λ A X. λ cN cC. cN. nilV : ∀ A : ̣. Vec A zero = Λ A X. λ cN cC. cN. |nilL| = |nilV| = λ cN cC. cN
  23. List : ̣ ➔ ̣ = λ A. ∀ X

    : ̣. X ➔ (A ➔ X ➔ X) ➔ X. Vec : ̣ ➔ ℕ ➔ ̣ = λ A n. ∀ X : ℕ ➔ ̣. X zero ➔ (∀ n : ℕ. A ➔ X n ➔ X (suc n)) ➔ X n. nilL : ∀ A : ̣. List A = Λ A X. λ cN cC. cN. nilV : ∀ A : ̣. Vec A zero = Λ A X. λ cN cC. cN. A and X erased |nilL| = |nilV| = λ cN cC. cN
  24. List : ̣ ➔ ̣ = λ A. ∀ X

    : ̣. X ➔ (A ➔ X ➔ X) ➔ X. Vec : ̣ ➔ ℕ ➔ ̣ = λ A n. ∀ X : ℕ ➔ ̣. X zero ➔ (∀ n : ℕ. A ➔ X n ➔ X (suc n)) ➔ X n. consL : ∀ A : ̣. A ➔ List A ➔ List A = Λ A. λ x xs. Λ X. λ cN cC. cC x (xs -X cN cC). consV : ∀ A : ̣. ∀ n : ℕ. A ➔ Vec A n ➔ Vec A (suc n) = Λ A n. λ x xs. Λ X. λ cN cC. cC -n x (xs -X cN cC). |consL| = |consV| = λ x xs cN cC. cC x (xs cN cC) application args n and X erased
  25. List : ̣ ➔ ̣ = λ A. ∀ X

    : ̣. X ➔ (A ➔ X ➔ X) ➔ X. Vec : ̣ ➔ ℕ ➔ ̣ = λ A n. ∀ X : ℕ ➔ ̣. X zero ➔ (Π n : ℕ. A ➔ X n ➔ X (suc n)) ➔ X n. consL : ∀ A : ̣. A ➔ List A ➔ List A = Λ A. λ x xs. Λ X. λ cN cC. cC x (xs -X cN cC). consV : ∀ A : ̣. Π n : ℕ. A ➔ Vec A n ➔ Vec A (suc n) = Λ A. λ n. λ x xs. Λ X. λ cN cC. cC n x (xs -X cN cC). NO!!! arg n not erased |consL| ≠ |consV| NO!!!
  26. Heterogenous Equality Heterogenous Equality t1 ≃ t2 nilL : List

    A nilV : Vec A zero |nilL| = |nilV| β : nilL ≃ nilV
  27. Heterogenous Equality Heterogenous Equality t1 ≃ t2 nilL : List

    A nilV : Vec A zero |nilL| = |nilV| β : nilL ≃ nilV
  28. v2l : {A : Set} {n : ℕ} ! Vec

    A n ! List A v2l nil = nil v2l (cons x xs) = cons x (v2l xs) Nothing Happening? v2lId : {A : Set} {n : ℕ} (xs : Vec A n) ! v2l xs ≡ xs ??? v2l! : {A : Set} {n : ℕ} ! Vec A n ! List A v2l! xs = xs ???
  29. v2l : ∀ A : ̣. ∀ n : Nat.

    Vec A n ➔ List A = elimVec nilL (λ x ih. consL x ih). Nothing Happening! v2lId : ∀ A : ̣. ∀ n : Nat. Π xs : Vec A n. v2l xs ≃ xs = elimVec β (λ x ih. ρ ih - β). v2l! : ∀ A : ̣. ∀ n : Nat. Vec A n ➔ List A = Λ A n. λ xs. φ (v2lId xs) - (v2l xs) {xs}. extrinsic het. eq.
  30. v2l : ∀ A : ̣. ∀ n : Nat.

    Vec A n ➔ List A = elimVec nilL (λ x ih. consL x ih). Nothing Happening! v2lId : ∀ A : ̣. ∀ n : Nat. Π xs : Vec A n. v2l xs ≃ xs = elimVec β (λ x ih. ρ ih - β). v2l! : ∀ A : ̣. ∀ n : Nat. Vec A n ➔ List A = Λ A n. λ xs. φ (v2lId xs) - (v2l xs) {xs}. |v2l!| = λ xs. xs
  31. Type of Identity Functions A type abstraction that represents the

    existence of an identity function from A to B (after erasure).
  32. Type of Identity Functions There are many ways to extrinsically

    type the identity function (e.g. v2l!, l2v!, appV2appL!, etc.). The type abstraction Id A B represents the existence of an identity function (after erasure) from A to B. Id A B = Π a : A. Σ b : B. b ≃ a. elimId f : A ➔ B |elimId f| = λ x. x f : Id A B extrinsic het. eq.
  33. Type of Identity Functions There are many ways to extrinsically

    type the identity function (e.g. v2l!, l2v!, appV2appL!, etc.). The type abstraction Id A B represents the existence of an identity function (after erasure) from A to B. Id A B = Π a : A. Σ b : B. b ≃ a. elimId f : A ➔ B |elimId f| = λ x. x f : Id A B
  34. Type of Identity Functions There are many ways to extrinsically

    type the identity function (e.g. v2l!, l2v!, appV2appL!, etc.). The type abstraction Id A B represents the existence of an identity function (after erasure) from A to B. Id A B = Π a : A. Σ b : B. b ≃ a. elimId f : A ➔ B |elimId f| = λ x. x f : Id A B
  35. Type of Identity Functions There are many ways to extrinsically

    type the identity function (e.g. v2l!, l2v!, appV2appL!, etc.). The type abstraction Id A B represents the existence of an identity function (after erasure) from A to B. Id A B = Π a : A. Σ b : B. b ≃ a. elimId f : A ➔ B |elimId f| = λ x. x f : Id A B
  36. Id-Closed Combinators Program (Function Type) Reuse Id (∀ i :

    I. X i ➔ X' i) (Y ➔ Y') Data (Fixpoint Type) Reuse ∀ i : I. Id (IFix I F imapF i) (Fix G imapG)
  37. Id-Closed Combinators Program (Function Type) Reuse Id (∀ i :

    I. X i ➔ X' i) (Y ➔ Y') Data (Fixpoint Type) Reuse ∀ i : I. Id (IFix I F imapF i) (Fix G imapG)
  38. Id-Closed Combinators Program (Function Type) Reuse Id (∀ i :

    I. X i ➔ X' i) (Y ➔ Y') Data (Fixpoint Type) Reuse ∀ i : I. Id (IFix I F imapF i) (Fix G imapG)
  39. Id-Closed Combinators Program (Function Type) Reuse Id (∀ i :

    I. X i ➔ X' i) (Y ➔ Y') Data (Fixpoint Type) Reuse ∀ i : I. Id (IFix I F imapF i) (Fix G imapG) Id-closed combinators allow for data and functions to be reused incrementally, and typing ensures the results of reuse will be zero- cost (i.e., erase to the identity function). Program (Function Type) Reuse Id (∀ i : I. X i ➔ X' i) (Y ➔ Y')
  40. Id-Closed Combinators Program (Function Type) Reuse Id (∀ i :

    I. X i ➔ X' i) (Y ➔ Y') Data (Fixpoint Type) Reuse ∀ i : I. Id (IFix I F imapF i) (Fix G imapG) Id-closed combinators allow for data and functions to be reused incrementally, and typing ensures the results of reuse will be zero- cost (i.e., erase to the identity function). Program (Function Type) Reuse Id (∀ i : I. X i ➔ X' i) (Y ➔ Y') left-component ( ) erased
  41. AppV AppL forget 1 2 3 AppV : ̣ =

    ∀ A : ̣. ∀ n : Nat. Vec A n ➔ ∀ m : Nat. Vec A m ➔ Vec A (add n m). AppL : ̣ = ∀ A : ̣. List A ➔ List A ➔ List A.
  42. AppV AppL forget 1 2 3 AppV : ̣ =

    ∀ A : ̣. ∀ n : Nat. Vec A n ➔ ∀ m : Nat. Vec A m ➔ Vec A (add n m). AppL : ̣ = ∀ A : ̣. List A ➔ List A ➔ List A. // Id (∀ A : ̣. •) (∀ A : ̣. •) copyType (Λ A. // Id (∀ n : Nat. Vec A n ➔ •) (List A ➔ •) allArr2arr len l2v (λ xs. // Id (∀ m : Nat. Vec A m ➔ •) (List A ➔ •) allArr2arr len l2v (λ xs. // Id (Vec A (add (len xs) (len ys))) (List A) v2l)))
  43. AppV AppL forget 1 2 3 AppV : ̣ =

    ∀ A : ̣. ∀ n : Nat. Vec A n ➔ ∀ m : Nat. Vec A m ➔ Vec A (add n m). AppL : ̣ = ∀ A : ̣. List A ➔ List A ➔ List A. // Id (∀ A : ̣. •) (∀ A : ̣. •) copyType (Λ A. // Id (∀ n : Nat. Vec A n ➔ •) (List A ➔ •) allArr2arr len l2v (λ xs. // Id (∀ m : Nat. Vec A m ➔ •) (List A ➔ •) allArr2arr len l2v (λ xs. // Id (Vec A (add (len xs) (len ys))) (List A) v2l)))
  44. AppV AppL forget 1 2 3 AppV : ̣ =

    ∀ A : ̣. ∀ n : Nat. Vec A n ➔ ∀ m : Nat. Vec A m ➔ Vec A (add n m). AppL : ̣ = ∀ A : ̣. List A ➔ List A ➔ List A. // Id (∀ A : ̣. •) (∀ A : ̣. •) copyType (Λ A. // Id (∀ n : Nat. Vec A n ➔ •) (List A ➔ •) allArr2arr len l2v (λ xs. // Id (∀ m : Nat. Vec A m ➔ •) (List A ➔ •) allArr2arr len l2v (λ xs. // Id (Vec A (add (len xs) (len ys))) (List A) v2l)))
  45. AppV AppL forget 1 2 3 AppV : ̣ =

    ∀ A : ̣. ∀ n : Nat. Vec A n ➔ ∀ m : Nat. Vec A m ➔ Vec A (add n m). AppL : ̣ = ∀ A : ̣. List A ➔ List A ➔ List A. // Id (∀ A : ̣. •) (∀ A : ̣. •) copyType (Λ A. // Id (∀ n : Nat. Vec A n ➔ •) (List A ➔ •) allArr2arr len l2v (λ xs. // Id (∀ m : Nat. Vec A m ➔ •) (List A ➔ •) allArr2arr len l2v (λ xs. // Id (Vec A (add (len xs) (len ys))) (List A) v2l)))
  46. AppV AppL forget 1 2 3 AppV : ̣ =

    ∀ A : ̣. ∀ n : Nat. Vec A n ➔ ∀ m : Nat. Vec A m ➔ Vec A (add n m). AppL : ̣ = ∀ A : ̣. List A ➔ List A ➔ List A. // Id (∀ A : ̣. •) (∀ A : ̣. •) copyType (Λ A. // Id (∀ n : Nat. Vec A n ➔ •) (List A ➔ •) allArr2arr len l2v (λ xs. // Id (∀ m : Nat. Vec A m ➔ •) (List A ➔ •) allArr2arr len l2v (λ xs. // Id (Vec A (add (len xs) (len ys))) (List A) v2l)))
  47. We hope that is a step in the right direction

    for making dependently typed languages more usable, via code reuse without runtime penalty. Conclusion Tutorial tomorrow on dependently typed programming in Cedille! … wrapping up my postdoc at University of Iowa, please talk to me about any positions :)
  48. We hope that is a step in the right direction

    for making dependently typed languages more usable, via code reuse without runtime penalty. Conclusion Tutorial tomorrow on dependently typed programming in Cedille! … wrapping up my postdoc at University of Iowa, please talk to me about any positions :)
  49. We hope that is a step in the right direction

    for making dependently typed languages more usable, via code reuse without runtime penalty. Conclusion Tutorial tomorrow on dependently typed programming in Cedille! … wrapping up my postdoc at University of Iowa, please talk to me about any positions :)
  50. • Details on combinators • Background on fixpoint types in

    Cedille • Enriching direction of reuse • Case study: Reuse between untyped and intrinsically typed STLC terms • Comparisons to related work like ornaments and dependent interoperability In The Paper
  51. AppV AppL forget 1 2 3 AppV : ̣ =

    ∀ A : ̣. ∀ n : Nat. Vec A n ➔ ∀ m : Nat. Vec A m ➔ Vec A (add n m). AppL : ̣ = ∀ A : ̣. List A ➔ List A ➔ List A. any f : AppV works!
  52. data List (A : Set) : Set where nil :

    List A cons : A ! List A ! List A data Vec (A : Set) : ℕ ! Set where nil : Vec A zero cons : {n : ℕ} ! A ! Vec A n ! Vec A (suc n) v2l : {A : Set} {n : ℕ} ! Vec A n ! List A v2l nil = nil v2l (cons x xs) = cons x (v2l xs) appV2appL : AppV ! AppL appV2appL appV xs ys = v2l (appV (l2v xs) (l2v ys)) nothing happening?
  53. appV2appL : AppV ➔ AppL = λ appV xs ys.

    v2l (appV (l2v xs) (l2v ys)). Nothing Happening! appV2appLId : Π f : AppV. ∀ A : ̣. Π xs ys : List A. appV2appL f xs ys ≃ f (l2v! xs) (l2v! ys) = λ f xs ys. ρ (l2vId xs) - ρ (l2vId ys) - ρ (v2lId (f xs ys)) - β rewrites
  54. Nothing Happening! appV2appL! : AppV ➔ AppL = λ f

    xs ys. φ (appV2appLId f xs ys) - (appV2appL f xs ys) {f (l2v! xs) (l2v! ys)}. |l2v!| = λ xs. xs |appV2appL!| = λ f xs ys. f xs ys = λ f. f