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

Towards a Dependent Type Theory with First-Class Datatype Definitions

Towards a Dependent Type Theory with First-Class Datatype Definitions

Today's dependently typed languages that emphasize correct-by-construction programming (e.g. Agda, Epigram, Idris) are "both a blessing and a curse". On one hand, creating specialized indexed datatypes allows functions to be written over the minimal and exact cases a function is expected to handle. On the other hand, constantly creating new datatypes comes with the cost of constantly rewriting routine functions. Fortunately, a beautiful solution to this problem exists in the form of datatype Ornaments (McBride?). Unfortunately, no existing programming language supports them. I will present a dependent type theory that makes datatype definitions first-class, hence supporting Ornaments and other features as user-libraries (rather than being baked into the meta theory). This is the idea behind Levitation (Chapman et al), but presented as a stratified denotational semantics for the types of such a language.

Agda code used in this talk:
https://github.com/larrytheliquid/plclub-oct-2012

72003cf388f6f7f95b433de3df9ccd38?s=128

larrytheliquid

October 26, 2012
Tweet

Transcript

  1. Towards a Dependent Type Theory with First-Class Datatype Definitions Larry

    Diehl Friday, March 29, 13
  2. “chalk talks are fine!” - Nathan Collins, PSU PLClub Wiki

    Friday, March 29, 13
  3. Outline 1. Definition of “first-class datatype”. 2.Motivation for dependent type

    theories with first-class datatypes. 3.Denotation of types for a dependent type theory with first-class datatypes. Friday, March 29, 13
  4. Part 1: Definition Friday, March 29, 13

  5. First-Class X Can be constructed at run-time. Can be assigned

    into a variable. Can be passed as a parameter. Can be returned from a subroutine. Friday, March 29, 13
  6. First-Class Type Can be constructed at run-time. Can be assigned

    into a variable. Can be passed as a parameter. Can be returned from a subroutine. Bool× : Set Bool× = Bool ×  Bool× : Set ! Set Bool× = _×_ Bool Friday, March 29, 13
  7. First-Class Datatype Definition Can be constructed at run-time. Can be

    assigned into a variable. Can be passed as a parameter. Can be returned from a subroutine. Friday, March 29, 13
  8. Part 2: Motivation Friday, March 29, 13

  9. DTPL Problems Datatype and library reuse. Little deriving support. Generic

    programming 㱺 universes galore. New solutions 㱺 new languages/patches. Friday, March 29, 13
  10. Datatype Reuse Trees bloom in Spring. Friday, March 29, 13

  11. data Tree (A : Set) :  ! Set where

    node : ∀{n} ! A ! Tree A n ! Tree A n ! Tree A (suc n) leaf : Tree A zero tree : Tree Bool 2 tree = node true (node false leaf leaf) (node true leaf leaf) postulate foldTree : ∀{A n} {B : Set} ! B ! (A ! B ! B) ! Tree A n ! B Friday, March 29, 13
  12. data Tree (A : Set) :  ! Set where

    node : ∀{n} ! A ! Tree A n ! Tree A n ! Tree A (suc n) leaf : Tree A zero tree : Tree Bool 2 tree = node true (node false leaf leaf) (node true leaf leaf) postulate foldTree : ∀{A n} {B : Set} ! B ! (A ! B ! B) ! Tree A n ! B What about leaf data? Friday, March 29, 13
  13. data Desc (I : Set) : Set₁ where `⊤ :

    Desc I `X : (i : I) ! Desc I _`⊎_ _`×_ : (δ δ′ : Desc I) ! Desc I `Σ `Π : (A : Set) (δ : A ! Desc I) ! Desc I Friday, March 29, 13
  14. TreeD : Set !  ! Desc  TreeD A

    zero = `⊤ TreeD A (suc n) = `Σ A λ _ ! `X n `× `X n TreeD′ : Set !  ! Desc  TreeD′ A m = (`Σ (m ≡ 0) λ _ ! `⊤) `⊎ (`Σ  λ n ! `Σ (suc n ≡ m) λ _ ! `Σ A λ _ ! `X n `× `X n) Friday, March 29, 13
  15. bloom : ∀{I} ! Desc I ! Set ! Desc

    I bloom `⊤ X = `Σ X λ _ ! `⊤ -- Interesting Case bloom (`X i) X = `X i bloom (δ `⊎ δ′) X = bloom δ X `⊎ bloom δ′ X bloom (δ `× δ′) X = bloom δ X `× bloom δ′ X bloom (`Σ A δ) X = `Σ A (λ a ! bloom (δ a) X) bloom (`Π A δ) X = `Π A (λ a ! bloom (δ a) X) “ornamentation” Friday, March 29, 13
  16. ⟦_⟧ : {I : Set} ! Desc I ! (I

    ! Set) ! Set ⟦ `⊤ ⟧ X = ⊤ ⟦ `X i ⟧ X = X i ⟦ T `× T' ⟧ X = ⟦ T ⟧ X × ⟦ T' ⟧ X ⟦ T `⊎ T' ⟧ X = ⟦ T ⟧ X ⊎ ⟦ T' ⟧ X ⟦ `Σ S T ⟧ X = Σ S λ s ! ⟦ T s ⟧ X ⟦ `Π S T ⟧ X = (s : S) ! ⟦ T s ⟧ X data μ {I : Set} (R : I ! Desc I)(i : I) : Set where con : ⟦ R i ⟧ (μ R) ! μ R i Friday, March 29, 13
  17. μTree : Set !  ! Set μTree A n

    = μ (TreeD A) n μBloomingTree : Set !  ! Set μBloomingTree A n = μ (λ m ! bloom (TreeD A m) Bool) n μTreeBool⊎3+ : Set !  ! Set μTreeBool⊎3+ A n = μTree (Bool ⊎ A) (3 + n) Friday, March 29, 13
  18. μTree : Set !  ! Set μTree A n

    = μ (TreeD A) n μBloomingTree : Set !  ! Set μBloomingTree A n = μ (λ m ! bloom (TreeD A m) Bool) n μTreeBool⊎3+ : Set !  ! Set μTreeBool⊎3+ A n = μTree (Bool ⊎ A) (3 + n) Look ma, no staging! Friday, March 29, 13
  19. Deriving / Generic Functions One universe to rule them all.

    Friday, March 29, 13
  20. `leaf : ∀{A} ! μTree A 0 `leaf = con

    tt `node : ∀{A n} ! A ! μTree A n ! μTree A n ! μTree A (suc n) `node x xs xs′ = con (x , xs , xs′) `sapling : μTree Bool 1 `sapling = `node true `leaf `leaf Friday, March 29, 13
  21. eq : ∀{I} ! {R : I ! Desc I}

    {i : I} ! μ R i ! μ R i ! Maybe Bool eq′ : ∀{I} ! {R : I ! Desc I} (D : Desc I) ! ⟦ D ⟧ (μ R) ! ⟦ D ⟧ (μ R) ! Maybe Bool eq {R = R} {i = i} (con x) (con y) = eq′ (R i) x y eq′ `⊤ tt tt = just true eq′ (`X i) x y = eq x y eq′ (δ `⊎ δ′) (inj₁ x) (inj₁ y) = eq′ δ x y eq′ (δ `⊎ δ′) (inj₁ x) (inj₂ y) = just false eq′ (δ `⊎ δ′) (inj₂ x) (inj₁ y) = just false eq′ (δ `⊎ δ′) (inj₂ x) (inj₂ y) = eq′ δ′ x y eq′ (δ `× δ′) (x , x′) (y , y′) with eq′ δ x y ... | nothing = nothing ... | just false = just false ... | just true = eq′ δ′ x′ y′ eq′ (`Σ A δ) (x , x′) (y , y′) = nothing eq′ (`Π A δ) x y = nothing Friday, March 29, 13
  22. leaf≡leaf : ∀{A} ! eq (`leaf {A}) `leaf ≡ just

    true leaf≡leaf = refl sapling≟sapling : eq `sapling `sapling ≡ nothing sapling≟sapling = refl Friday, March 29, 13
  23. Future Proof Enhance datatypes with “fairy dust”. Deriving “show3d”. Friday,

    March 29, 13
  24. Motivation Summary Agda datatype code whose interpretation is any indexed

    family of types. Reusable libraries of dependent types via ornaments. Deriving mechanism via generic functions over code + interpretation of any datatype. Future proof for datatype “fairy dust” and deriving “show3d”. Friday, March 29, 13
  25. Agda Deep Embedding Limitations Desc universe distinct from types defined

    with “data” keyword. Would like all datatypes in language to be translated into this format. Cannot generate new datatypes programmatically due to external positivity checker. Friday, March 29, 13
  26. Part 3: Denotation Friday, March 29, 13

  27. ⟦ "chalk talks are fine!" ⟧ = this Friday, March

    29, 13
  28. Pop Quiz! Foo = Bool ⊎  Bar = Set

    ⊎  VS Friday, March 29, 13
  29. Pop Quiz! Foo : Set₀ Foo = Bool ⊎ 

    Bar : Set₁ Bar = Set ⊎  VS Friday, March 29, 13
  30. Type₀ Where the rubber meets the road. Friday, March 29,

    13
  31. data Type′ : Set ⟦_⟧′ : Type′ ! Set data

    Type′ where `⊥ `⊤ `Bool : Type′ `Π `Σ : (τ : Type′) (τ′ : ⟦ τ ⟧′ ! Type′) ! Type′ ⟦ `⊥ ⟧′ = ⊥ ⟦ `⊤ ⟧′ = ⊤ ⟦ `Bool ⟧′ = Bool ⟦ `Π τ τ′ ⟧′ = (v : ⟦ τ ⟧′) ! ⟦ τ′ v ⟧′ ⟦ `Σ τ τ′ ⟧′ = Σ ⟦ τ ⟧′ λ v ! ⟦ τ′ v ⟧′ Friday, March 29, 13
  32. _`∧_ : ⟦ `Bool `! `Bool `! `Bool ⟧′ true

    `∧ b = b false `∧ b = false `tru : ⟦ `Π `Bool (λ b ! if b then `⊤ else `Bool) ⟧′ `tru true = tt `tru false = true Friday, March 29, 13
  33. Type₁ ≤ Diligence is the only size bound on a

    Russian Doll. Friday, March 29, 13
  34. data Type′ (U : Set) (El : U ! Set)

    : Set ⟦_⟧′ : ∀{U El} ! Type′ U El ! Set data Type′ U El where `⊥ `⊤ `Bool : Type′ U El `Π `Σ : (τ : Type′ U El) (τ′ : ⟦ τ ⟧′ ! Type′ U El) ! Type′ U El ⟦ `⊥ ⟧′ = ⊥ ⟦ `⊤ ⟧′ = ⊤ ⟦ `Bool ⟧′ = Bool ⟦ `Π τ τ′ ⟧′ = (v : ⟦ τ ⟧′) ! ⟦ τ′ v ⟧′ ⟦ `Σ τ τ′ ⟧′ = Σ ⟦ τ ⟧′ λ v ! ⟦ τ′ v ⟧′ Friday, March 29, 13
  35. data Type′ U El where `Type : Type′ U El

    `⟦_⟧ : U ! Type′ U El ⟦_⟧′ {U = U} `Type = U ⟦_⟧′ {El = El} `⟦ τ ⟧ = El τ Friday, March 29, 13
  36. Type₀ : Set Type₀ = Type′ ⊥ ⊥-elim Friday, March

    29, 13
  37. Type : (n : ) ! Set _⟦_⟧ : (n

    : ) ! Type n ! Set Type zero = Type₀ Type (suc n) = Type′ (Type n) (_⟦_⟧ n) zero ⟦ e ⟧ = ⟦ e ⟧′ suc n ⟦ e ⟧ = ⟦ e ⟧′ Friday, March 29, 13
  38. Type₁ : Set Type₁ = Type 1 ⟦_⟧₁ : Type₁

    ! Set ⟦_⟧₁ = _⟦_⟧ 1 Friday, March 29, 13
  39. `id : ⟦ `Π `Type (λ A ! `⟦ A

    ⟧ `! `⟦ A ⟧) ⟧₁ `id A x = x Friday, March 29, 13
  40. Desc : Set₀ Fitting in. Friday, March 29, 13

  41. data Desc (I : Set) : Set₁ where `⊤ :

    Desc I `X : (i : I) ! Desc I _`⊎_ _`×_ : (δ δ′ : Desc I) ! Desc I `Σ `Π : (A : Set) (δ : A ! Desc I) ! Desc I Friday, March 29, 13
  42. data Desc (U : Set) (El : U ! Set)

    (I : Set) : Set where `⊤ : Desc U El I `X : (i : I) ! Desc U El I _`⊎_ _`×_ : (δ δ′ : Desc U El I) ! Desc U El I `Σ `Π : (u : U) (δ : El u ! Desc U El I) ! Desc U El I Friday, March 29, 13
  43. ⟦_⟧ᵈ : ∀{U El I} ! Desc U El I

    ! (I ! Set) ! Set ⟦ `⊤ ⟧ᵈ X = ⊤ ⟦ `X i ⟧ᵈ X = X i ⟦ δ `⊎ δ′ ⟧ᵈ X = ⟦ δ ⟧ᵈ X ⊎ ⟦ δ′ ⟧ᵈ X ⟦ δ `× δ′ ⟧ᵈ X = ⟦ δ ⟧ᵈ X × ⟦ δ′ ⟧ᵈ X ⟦_⟧ᵈ {El = El} (`Σ τ δ) X = Σ (El τ) λ e ! ⟦ δ e ⟧ᵈ X ⟦_⟧ᵈ {El = El} (`Π τ δ) X = (e : El τ) ! ⟦ δ e ⟧ᵈ X Friday, March 29, 13
  44. data Type′ U El where `Desc : (τ : Type′

    U El) ! Type′ U El `μ : (τ : Type′ U El) (δ : ⟦ τ ⟧′ ! Desc U El ⟦ τ ⟧′) (i : ⟦ τ ⟧′) ! Type′ U El ⟦_⟧′ {U} {El} (`Desc τ) = Desc U El ⟦ τ ⟧′ ⟦_⟧′ {U} {El} (`μ τ δ i) = μ δ i Friday, March 29, 13
  45. ` : Type₁ ` = `μ `⊤ (λ { tt

    ! `⊤ `⊎ `X tt }) tt `zero : ⟦ ` ⟧₁ `zero = con (inj₁ tt) `suc : ⟦ ` `! ` ⟧₁ `suc n = con (inj₂ n) Friday, March 29, 13
  46. TreeD : ⟦ `Type `! ` `! `Desc ` ⟧₁

    TreeD A (con (inj₁ tt)) = `⊤ TreeD A (con (inj₂ n)) = `Σ A λ _ ! `X n `× `X n `leaf : ⟦ `Π `Type (λ A ! `μ ` (TreeD A) `zero) ⟧₁ `leaf A = con tt `node : ⟦ `Π `Type (λ A ! `Π ` (λ n ! `⟦ A ⟧ `! `μ ` (TreeD A) n `! `μ ` (TreeD A) n `! `μ ` (TreeD A) (`suc n))) ⟧₁ `node A n x xs xs′ = con (x , xs , xs′) Friday, March 29, 13
  47. `bloom : ⟦ `Π `Type (λ I ! `Desc `⟦

    I ⟧ `! `Type `! `Desc `⟦ I ⟧) ⟧₁ `bloom I `⊤ X = `Σ X λ _ ! `⊤ `bloom I (`X i) X = `X i `bloom I (δ `⊎ δ′) X = `bloom I δ X `⊎ `bloom I δ′ X `bloom I (δ `× δ′) X = `bloom I δ X `× `bloom I δ′ X `bloom I (`Σ A δ) X = `Σ A (λ a ! `bloom I (δ a) X) `bloom I (`Π A δ) X = `Π A (λ a ! `bloom I (δ a) X) Friday, March 29, 13
  48. First-Class Datatype Definition can be constructed at run-time can be

    assigned into a variable can be passed as a parameter can be returned from a subroutine `bloom : ⟦ `Π `Type (λ I ! `Desc `⟦ I ⟧ `! `Type `! `Desc `⟦ I ⟧) ⟧₁ Friday, March 29, 13
  49. Summary Denotation of dependent types for a theory with a

    hierarchy of universes (Conor McBride). A stratified embedding of a Martin-Löf universe whose interpretation is any indexed family of types (Larry Diehl). Object language can “meta-program” over datatype definitions, and write generic functions like “deriving” over interpretations of all universe codes (Conor McBride). Friday, March 29, 13
  50. Future Work Lift’ing types one universe level up is trivial

    via `⟦_⟧, but can we also Lower? Impact of non-parametric datatypes on “minimally complete” Type size. A (e.g. denotational) semantics of Terms indexed by these Types. Friday, March 29, 13
  51. References Conor McBride. Ornamental algebras, algebraic ornaments. Pierre-Evariste Dagand, Conor

    McBride. Transporting Functions across Ornaments. James Chapman , Pierre-Evariste Dagand , Conor Mcbride , Peter Morris. The Gentle Art of Levitation. Pierre-Evariste Dagand, Conor McBride. Elaborating Inductive Definitions. Inductive-recursive universe hierarchy thanks to Hier.agda by Conor McBride. Friday, March 29, 13
  52. Questions? Friday, March 29, 13