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

Fully Generic Programming over Closed Universes...

Fully Generic Programming over Closed Universes of Types

My Ph.D. defense.

larrytheliquid

May 08, 2017
Tweet

More Decks by larrytheliquid

Other Decks in Programming

Transcript

  1. Motivation • Dependently typed languages • Type system works as

    a logic (Curry-Howard) • Types are to propositions as programs are to proofs • Fine-grained types with inherent correctness properties • Must rewrite “standard library” functions for each fine- grained type • Program correctness at the expense of code reuse
  2. Solution • Fully generic programming • Write generic “standard library”

    functions • Generic functions work over all types defined now and all types defined in the future • Generic functions do not need to be edited when a new type is declared • Dependent typing ensures generic functions are well-typed! • By being defined over a closed universe of user-declared types
  3. Dependent Types dependency append : (A : Set) ! List

    A ! List A ! List A append A nil ys = ys append A (cons x xs) ys = cons x (append A xs ys) data List (A : Set) : Set where nil : List A cons : A ! List A ! List A
  4. Dependent Types append : (A : Set) (n m :

    ℕ) ! Vec A n ! Vec A m ! Vec A (n + m) append A zero m nil ys = ys append A (suc n) m (cons x xs) ys = cons x (append A n m xs ys) dependency correctness property data Vec (A : Set) : ℕ ! Set where nil : Vec A zero cons : {n : ℕ} ! A ! Vec A n ! Vec A (suc n)
  5. Proliferation of Types ℕ List Tree • edge labeled •

    node labeled • both labeled • n-ary • cons-based • snoc-based structurally different 7 * 4 = 28
  6. Proliferation of Types ℕ List Tree • cons-based • snoc-based

    • edge labeled • node labeled • both labeled • n-ary • length-indexed • unique • sorted • balanced • searchable • maximum value • minimum value • range of values structurally different semantically constrained 15 * 4 = 60
  7. Generic Programming generic : (A : Set) ! A !

    ꔇ abstract type of types is
  8. Generic Programming generic : (A : ) ! A !

    ꔇ abstract type ≡ Set Bool : Set ℕ : Set Char : Set String : Set List : Set ! Set Pair : Set ! Set ! Set … e.g.
  9. Generic Programming generic : (A : ) ! A !

    ꔇ reify concrete subset data : Set where `Bool : `ℕ : `String : abstract type codes type
  10. Generic Programming generic : (A : ) ! A !

    ꔇ ⟦_⟧ : ! ⟦ `Bool ⟧ = Bool ⟦ `ℕ ⟧ = ℕ ⟦ `String ⟧ = String reflect meaning function reify concrete subset abstract type codes type
  11. Generic Programming generic : (A : ) ! A !

    ꔇ generic : (C : ) ! ⟦ C ⟧ ! ꔇ reflect ⟦_⟧ : ! meaning function reify concrete subset abstract type codes type
  12. Universe : Set + ⟦_⟧ : ! type of codes

    meaning function { A : | C : where ⟦ C ⟧ ≡ A } (a possibly infinite collection of types) image of meaning function
  13. Universe : Set + ⟦_⟧ : ! type of codes

    meaning function { A : | C : where ⟦ C ⟧ ≡ A } (a possibly infinite collection of types) image of meaning function
  14. Generic Programming • Functions defined over values of multiple types

    (i.e. a universe) • Traditional • Functions defined by recursion on inductive arguments of constructors (e.g. append) • Defined over an open (i.e. extendable) universe • An open universe contains a datatype defined in terms of Set • Fully • Functions defined by recursion on inductive & non-inductive arguments of constructors (e.g. marshal) • Defined over a closed (i.e. fixed) universe
  15. Traditional Generic Size Function • Sum of: • Number of

    constructors • Number of non-inductive constructor arguments • Recursive size of inductive constructor arguments size : (A : Set) ! A ! ℕ abstract cons (true , true) (cons (false , false) nil) 1 + 1 + 1 + 1 + 1
  16. Traditional Generic Size Function • Sum of: • Number of

    constructors • Number of non-inductive constructor arguments • Recursive size of inductive constructor arguments size : (A : Set) ! A ! ℕ abstract cons (true , true) (cons (false , false) nil) 1 + 1 + 1 + 1 + 1
  17. Traditional Generic Size Function • Sum of: • Number of

    constructors • Number of non-inductive constructor arguments • Recursive size of inductive constructor arguments size : (A : Set) ! A ! ℕ abstract cons (true , true) (cons (false , false) nil) 1 + 1 + 1 + 1 + 1
  18. Open Built-In Types Universe data Size : Set₁ where `Bool

    : Size `Pair : (A B : Set) ! Size `List : (A : Set) ! Size ⟦_⟧ : Size ! Set ⟦ `Bool ⟧ = Bool ⟦ `Pair A B ⟧ = A × B ⟦ `List A ⟧ = List A codes meaning function collection of types … open Bool List Bool List String List ℕ Bool × Bool String × Char Char × ℕ List (List Bool) List (List String) Bool × List Bool × Bool
  19. Open Built-In Types Universe data Size : Set₁ where `Bool

    : Size `Pair : (A B : Set) ! Size `List : (A : Set) ! Size ⟦_⟧ : Size ! Set ⟦ `Bool ⟧ = Bool ⟦ `Pair A B ⟧ = A × B ⟦ `List A ⟧ = List A codes meaning function collection of types Bool List Bool List String List ℕ Bool × Bool String × Char Char × ℕ List (List Bool) List (List String) Bool × List Bool × Bool … open
  20. data Size : Set₁ where `Bool : Size `Pair :

    (A B : Set) ! Size `List : (A : Set) ! Size ⟦_⟧ : Size ! Set ⟦ `Bool ⟧ = Bool ⟦ `Pair A B ⟧ = A × B ⟦ `List A ⟧ = List A size : (A : Size) ! ⟦ A ⟧ ! ℕ size `Bool b = 1 size (`Pair A B) (a , b) = 3 size (`List A) nil = 1 size (`List A) (cons x xs) = 2 + size (`List A) xs inductive argument Traditional Generic Size Function
  21. Fully Generic Count Function • Sum of: • Number of

    constructors • Recursive count of inductive and non-inductive constructor arguments count : (A : Set) ! A ! ℕ abstract cons (true , true) (cons (false , false) nil) 1 + (1 + 1 + 1) + 1 + (1 + 1 + 1) + 1
  22. Fully Generic Count Function • Sum of: • Number of

    constructors • Recursive count of inductive and non-inductive constructor arguments count : (A : Set) ! A ! ℕ abstract cons (true , true) (cons (false , false) nil) 1 + (1 + 1 + 1) + 1 + (1 + 1 + 1) + 1
  23. Fully Generic Count Function • Sum of: • Number of

    constructors • Recursive count of inductive and non-inductive constructor arguments count : (A : Set) ! A ! ℕ abstract cons (true , true) (cons (false , false) nil) 1 + (1 + 1 + 1) + 1 + (1 + 1 + 1) + 1
  24. data Count : Set where `Bool : Count `Pair :

    (A B : Count) ! Count `List : (A : Count) ! Count ⟦_⟧ : Count ! Set ⟦ `Bool ⟧ = Bool ⟦ `Pair A B ⟧ = ⟦ A ⟧ × ⟦ B ⟧ ⟦ `List A ⟧ = List ⟦ A ⟧ collection of types Bool List Bool Bool × Bool List (List Bool) List (Bool × Bool) Bool × Bool × Bool List Bool × Bool × Bool List Bool × List Bool × Bool … Closed Built-In Types Universe closed codes meaning function
  25. data Count : Set where `Bool : Count `Pair :

    (A B : Count) ! Count `List : (A : Count) ! Count ⟦_⟧ : Count ! Set ⟦ `Bool ⟧ = Bool ⟦ `Pair A B ⟧ = ⟦ A ⟧ × ⟦ B ⟧ ⟦ `List A ⟧ = List ⟦ A ⟧ collection of types Bool List Bool Bool × Bool List (List Bool) List (Bool × Bool) Bool × Bool × Bool List Bool × Bool × Bool List Bool × List Bool × Bool … Closed Built-In Types Universe closed codes meaning function
  26. data Count : Set where `Bool : Count `Pair :

    (A B : Count) ! Count `List : (A : Count) ! Count ⟦_⟧ : Count ! Set ⟦ `Bool ⟧ = Bool ⟦ `Pair A B ⟧ = ⟦ A ⟧ × ⟦ B ⟧ ⟦ `List A ⟧ = List ⟦ A ⟧ count : (A : Count) ! ⟦ A ⟧ ! ℕ count `Bool b = 1 count (`Pair A B) (a , b) = 1 + count A a + count B b count (`List A) nil = 1 count (`List A) (cons x xs) = 1 + count A x + count (`List A) xs non-inductive argument Fully Generic Count Function
  27. Initial Algebra Semantics • Define a datatype as the least

    fixed point of a polynomial functor • Polynomial functor (F : (X : Set) → Set) • Function body has a sum-of-products format • Sum of constructor choices (nested disjoint unions) • Product of arguments per constructor (nested pairs) • Function parameter is a type (X) representing inductive constructor arguments
  28. Sum of Products C1 ⊎ C2 ⊎ … ⊎ Cn

    A1 × A2 × … × Am Sum/Disjoint union/Either type Product/Pair type
  29. Polynomial Functors Type Theory Algebra C1 ⊎ C2 x +

    y A1 × A2 x * y ⊤ 1 Bool ≅ ⊤ ⊎ ⊤ 2 = 1 + 1 Tril ≅ ⊤ ⊎ ⊤ ⊎ ⊤ 3 = 1 + 1 + 1
  30. Polynomial Functors Datatype Type Theory Algebra Natural Numbers F X

    = ⊤ ⊎ X f(x) = 1 + x Lists F X = ⊤ ⊎ (A × X) f(x) = 1 + a*x Binary Trees F X = A ⊎ (X × X) f(x) = a + x*x zero suc nil cons leaf branch
  31. Two-Level Types data List (A : Set) : Set where

    nil : List A cons : A ! List A ! List A ListF : Set ! (Set ! Set) ListF A X = ⊤ ⊎ A × X List : Set ! Set List A = μ (ListF A) functor level fixpoint level fixpoint μ F ≅ F (μ F)
  32. size : (F : Set ! Set) ! μ F

    ! ℕ sizes : (F : Set ! Set) (X : Set) ! F X ! ℕ abstract Traditional Generic Size Function two mutual functions for two-level types ≡ Set ! Set
  33. data Desc : Set₁ where _⊕_ _⊗_ : (D E

    : Desc) ! Desc κ : (A : Set) ! Desc ι : Desc ⟬_⟭ : Desc ! (Set ! Set) ⟬ D ⊕ E ⟭ X = ⟬ D ⟭ X ⊎ ⟬ E ⟭ X ⟬ D ⊗ E ⟭ X = ⟬ D ⟭ X × ⟬ E ⟭ X ⟬ κ A ⟭ X = A ⟬ ι ⟭ X = X codes meaning function Open User-Declared Types Universe ListD : Set ! Desc ListD A = κ ⊤ ⊕ (κ A ⊗ ι) ListF : Set ! (Set ! Set) ListF A X = ⟬ ListD A ⟭ X ListF : Set ! (Set ! Set) ListF A X = ⊤ ⊎ A × X
  34. data Desc : Set₁ where _⊕_ _⊗_ : (D E

    : Desc) ! Desc κ : (A : Set) ! Desc ι : Desc ⟬_⟭ : Desc ! (Set ! Set) ⟬ D ⊕ E ⟭ X = ⟬ D ⟭ X ⊎ ⟬ E ⟭ X ⟬ D ⊗ E ⟭ X = ⟬ D ⟭ X × ⟬ E ⟭ X ⟬ κ A ⟭ X = A ⟬ ι ⟭ X = X codes meaning function ListD : Set ! Desc ListD A = κ ⊤ ⊕ (κ A ⊗ ι) ListF : Set ! (Set ! Set) ListF A X = ⟬ ListD A ⟭ X Open User-Declared Types Universe ListF : Set ! (Set ! Set) ListF A X = ⊤ ⊎ A × X
  35. data Desc : Set₁ where _⊕_ _⊗_ : (D E

    : Desc) ! Desc κ : (A : Set) ! Desc ι : Desc ⟬_⟭ : Desc ! (Set ! Set) ⟬ D ⊕ E ⟭ X = ⟬ D ⟭ X ⊎ ⟬ E ⟭ X ⟬ D ⊗ E ⟭ X = ⟬ D ⟭ X × ⟬ E ⟭ X ⟬ κ A ⟭ X = A ⟬ ι ⟭ X = X data μ (D : Desc) : Set where init : ⟬ D ⟭ (μ D) ! μ D codes meaning function ListD : Set ! Desc ListD A = κ ⊤ ⊕ (κ A ⊗ ι) List : Set ! Set List A = μ (ListD A) data List (A : Set) : Set where nil : List A cons : A ! List A ! List A Open User-Declared Types Universe meaning function F (μ F) ! μ F
  36. data Desc : Set₁ where _⊕_ _⊗_ : (D E

    : Desc) ! Desc κ : (A : Set) ! Desc ι : Desc ⟬_⟭ : Desc ! (Set ! Set) ⟬ D ⊕ E ⟭ X = ⟬ D ⟭ X ⊎ ⟬ E ⟭ X ⟬ D ⊗ E ⟭ X = ⟬ D ⟭ X × ⟬ E ⟭ X ⟬ κ A ⟭ X = A ⟬ ι ⟭ X = X data μ (D : Desc) : Set where init : ⟬ D ⟭ (μ D) ! μ D codes meaning function ListD : Set ! Desc ListD A = κ ⊤ ⊕ (κ A ⊗ ι) List : Set ! Set List A = μ (ListD A) data List (A : Set) : Set where nil : List A cons : A ! List A ! List A Open User-Declared Types Universe meaning function F (μ F) ! μ F
  37. sizes : (F : Set ! Set) (X : Set)

    ! F X ! ℕ abstract Traditional Generic Size Function sizes : (D R : Desc) ! ⟬ D ⟭ (μ R) ! ℕ codes size : (F : Set ! Set) ! μ F ! ℕ size : (D : Desc) ! μ D ! ℕ
  38. sizes : (F : Set ! Set) (X : Set)

    ! F X ! ℕ abstract Traditional Generic Size Function sizes : (D R : Desc) ! ⟬ D ⟭ (μ R) ! ℕ codes size : (F : Set ! Set) ! μ F ! ℕ size : (D : Desc) ! μ D ! ℕ
  39. sizes : (F : Set ! Set) (X : Set)

    ! F X ! ℕ abstract Traditional Generic Size Function sizes : (D R : Desc) ! ⟬ D ⟭ (μ R) ! ℕ codes size : (F : Set ! Set) ! μ F ! ℕ size : (D : Desc) ! μ D ! ℕ
  40. data Desc : Set₁ where _⊕_ _⊗_ : (D E

    : Desc) ! Desc κ : (A : Set) ! Desc ι : Desc ⟬_⟭ : Desc ! (Set ! Set) ⟬ D ⊕ E ⟭ X = ⟬ D ⟭ X ⊎ ⟬ E ⟭ X ⟬ D ⊗ E ⟭ X = ⟬ D ⟭ X × ⟬ E ⟭ X ⟬ κ A ⟭ X = A ⟬ ι ⟭ X = X data μ (D : Desc) : Set where init : ⟬ D ⟭ (μ D) ! μ D size : (D : Desc) ! μ D ! ℕ size D (init xs) = 1 + sizes D D xs inductive argument open Traditional Generic Size Function sizes : (D R : Desc) ! ⟬ D ⟭ (μ R) ! ℕ sizes (D ⊕ E) X (inj₁ xs) = sizes D X xs sizes (D ⊕ E) X (inj₂ ys) = sizes E X ys sizes (D ⊗ E) X (xs , ys) = sizes D X xs + sizes E X ys sizes (κ A) X a = 1 sizes ι R x = size R x constructor arguments
  41. data Desc : Set₁ where _⊕_ _⊗_ : (D E

    : Desc) ! Desc κ : (A : Set) ! Desc ι : Desc ⟬_⟭ : Desc ! (Set ! Set) ⟬ D ⊕ E ⟭ X = ⟬ D ⟭ X ⊎ ⟬ E ⟭ X ⟬ D ⊗ E ⟭ X = ⟬ D ⟭ X × ⟬ E ⟭ X ⟬ κ A ⟭ X = A ⟬ ι ⟭ X = X data μ (D : Desc) : Set where init : ⟬ D ⟭ (μ D) ! μ D size : (D : Desc) ! μ D ! ℕ size D (init xs) = 1 + sizes D D xs inductive argument open Traditional Generic Size Function sizes : (D R : Desc) ! ⟬ D ⟭ (μ R) ! ℕ sizes (D ⊕ E) X (inj₁ xs) = sizes D X xs sizes (D ⊕ E) X (inj₂ ys) = sizes E X ys sizes (D ⊗ E) X (xs , ys) = sizes D X xs + sizes E X ys sizes (κ A) X a = 1 sizes ι R x = size R x constructor arguments
  42. count : (A : Set) ! A ! ℕ count

    : (F : Set ! Set) ! μ F ! ℕ counts : (F : Set ! Set) (X : Set) ! F X ! ℕ abstract ₁ Fully Generic Count Function μ abstract ₂ ₂ ≡ Set ! Set ₁ ≡ Set
  43. Closed User-Declared Types Universe data `Desc : Set where _`⊕_

    _`⊗_ : (D E : `Desc) ! `Desc `κ : (A : `Set) ! `Desc `ι : `Desc ⟪_⟫ : `Desc ! Desc ⟪ D `⊕ E ⟫ = ⟪ D ⟫ ⊕ ⟪ E ⟫ ⟪ D `⊗ E ⟫ = ⟪ D ⟫ ⊗ ⟪ E ⟫ ⟪ `κ A ⟫ = κ ⟦ A ⟧ ⟪ `ι ⟫ = ι codes ₂ meaning function closed data Desc : Set₁ where _⊕_ _⊗_ : (D E : Desc) ! Desc κ : (A : Set) ! Desc ι : Desc ⟬_⟭ : Desc ! (Set ! Set) ⟬ D ⊕ E ⟭ X = ⟬ D ⟭ X ⊎ ⟬ E ⟭ X ⟬ D ⊗ E ⟭ X = ⟬ D ⟭ X × ⟬ E ⟭ X ⟬ κ A ⟭ X = A ⟬ ι ⟭ X = X data μ (D : Desc) : Set where init : ⟬ D ⟭ (μ D) ! μ D open open user-declared types from before
  44. Closed User-Declared Types Universe data `Desc : Set where _`⊕_

    _`⊗_ : (D E : `Desc) ! `Desc `κ : (A : `Set) ! `Desc `ι : `Desc ⟪_⟫ : `Desc ! Desc ⟪ D `⊕ E ⟫ = ⟪ D ⟫ ⊕ ⟪ E ⟫ ⟪ D `⊗ E ⟫ = ⟪ D ⟫ ⊗ ⟪ E ⟫ ⟪ `κ A ⟫ = κ ⟦ A ⟧ ⟪ `ι ⟫ = ι codes ₂ meaning function closed data Desc : Set₁ where _⊕_ _⊗_ : (D E : Desc) ! Desc κ : (A : Set) ! Desc ι : Desc ⟬_⟭ : Desc ! (Set ! Set) ⟬ D ⊕ E ⟭ X = ⟬ D ⟭ X ⊎ ⟬ E ⟭ X ⟬ D ⊗ E ⟭ X = ⟬ D ⟭ X × ⟬ E ⟭ X ⟬ κ A ⟭ X = A ⟬ ι ⟭ X = X data μ (D : Desc) : Set where init : ⟬ D ⟭ (μ D) ! μ D open open user-declared types from before
  45. Closed Built-In Types Universe data `Set : Set where `⊥

    `⊤ : `Set _`⊎_ : (A B : `Set) ! `Set `Σ `Π : (A : `Set) (B : ⟦ A ⟧ ! `Set) ! `Set `Id : (A : `Set) (x y : ⟦ A ⟧) ! `Set ⟦_⟧ : `Set ! Set ⟦ `⊥ ⟧ = ⊥ ⟦ `⊤ ⟧ = ⊤ ⟦ A `⊎ B ⟧ = ⟦ A ⟧ ⊎ ⟦ B ⟧ ⟦ `Σ A B ⟧ = Σ ⟦ A ⟧ (λ a ! ⟦ B a ⟧) ⟦ `Π A B ⟧ = (a : ⟦ A ⟧) ! ⟦ B a ⟧ ⟦ `Id A x y ⟧ = Id ⟦ A ⟧ x y data `Desc : Set where _`⊕_ _`⊗_ : (D E : `Desc) ! `Desc `κ : (A : `Set) ! `Desc `ι : `Desc ⟪_⟫ : `Desc ! Desc ⟪ D `⊕ E ⟫ = ⟪ D ⟫ ⊕ ⟪ E ⟫ ⟪ D `⊗ E ⟫ = ⟪ D ⟫ ⊗ ⟪ E ⟫ ⟪ `κ A ⟫ = κ ⟦ A ⟧ ⟪ `ι ⟫ = ι codes ₁ closed codes ₂ meaning function meaning function closed closed closed `μ : (D : `Desc) ! `Set ⟦ `μ D ⟧ = μ ⟪ D ⟫
  46. Closed Built-In Types Universe data `Set : Set where `⊥

    `⊤ : `Set _`⊎_ : (A B : `Set) ! `Set `Σ `Π : (A : `Set) (B : ⟦ A ⟧ ! `Set) ! `Set `Id : (A : `Set) (x y : ⟦ A ⟧) ! `Set ⟦_⟧ : `Set ! Set ⟦ `⊥ ⟧ = ⊥ ⟦ `⊤ ⟧ = ⊤ ⟦ A `⊎ B ⟧ = ⟦ A ⟧ ⊎ ⟦ B ⟧ ⟦ `Σ A B ⟧ = Σ ⟦ A ⟧ (λ a ! ⟦ B a ⟧) ⟦ `Π A B ⟧ = (a : ⟦ A ⟧) ! ⟦ B a ⟧ ⟦ `Id A x y ⟧ = Id ⟦ A ⟧ x y data `Desc : Set where _`⊕_ _`⊗_ : (D E : `Desc) ! `Desc `κ : (A : `Set) ! `Desc `ι : `Desc ⟪_⟫ : `Desc ! Desc ⟪ D `⊕ E ⟫ = ⟪ D ⟫ ⊕ ⟪ E ⟫ ⟪ D `⊗ E ⟫ = ⟪ D ⟫ ⊗ ⟪ E ⟫ ⟪ `κ A ⟫ = κ ⟦ A ⟧ ⟪ `ι ⟫ = ι codes ₁ closed codes ₂ meaning function meaning function closed closed `μ : (D : `Desc) ! `Set ⟦ `μ D ⟧ = μ ⟪ D ⟫ Closed Fixpoints as a Built-In Type closed
  47. Closed User-Declared Types Universe data `Set : Set where `⊥

    `⊤ : `Set _`⊎_ : (A B : `Set) ! `Set `Σ `Π : (A : `Set) (B : ⟦ A ⟧ ! `Set) ! `Set `Id : (A : `Set) (x y : ⟦ A ⟧) ! `Set ⟦_⟧ : `Set ! Set ⟦ `⊥ ⟧ = ⊥ ⟦ `⊤ ⟧ = ⊤ ⟦ A `⊎ B ⟧ = ⟦ A ⟧ ⊎ ⟦ B ⟧ ⟦ `Σ A B ⟧ = Σ ⟦ A ⟧ (λ a ! ⟦ B a ⟧) ⟦ `Π A B ⟧ = (a : ⟦ A ⟧) ! ⟦ B a ⟧ ⟦ `Id A x y ⟧ = Id ⟦ A ⟧ x y data `Desc : Set where _`⊕_ _`⊗_ : (D E : `Desc) ! `Desc `κ : (A : `Set) ! `Desc `ι : `Desc ⟪_⟫ : `Desc ! Desc ⟪ D `⊕ E ⟫ = ⟪ D ⟫ ⊕ ⟪ E ⟫ ⟪ D `⊗ E ⟫ = ⟪ D ⟫ ⊗ ⟪ E ⟫ ⟪ `κ A ⟫ = κ ⟦ A ⟧ ⟪ `ι ⟫ = ι codes ₁ closed codes ₂ meaning function meaning function closed 2 4 + = 3 closed `μ : (D : `Desc) ! `Set ⟦ `μ D ⟧ = μ ⟪ D ⟫ closed
  48. Closed Codes of Open Codes data `Desc : Set where

    _`⊕_ _`⊗_ : (D E : `Desc) ! `Desc `κ : (A : `Set) ! `Desc `ι : `Desc ⟪_⟫ : `Desc ! Desc ⟪ D `⊕ E ⟫ = ⟪ D ⟫ ⊕ ⟪ E ⟫ ⟪ D `⊗ E ⟫ = ⟪ D ⟫ ⊗ ⟪ E ⟫ ⟪ `κ A ⟫ = κ ⟦ A ⟧ ⟪ `ι ⟫ = ι codes ₂ meaning function closed data Desc : Set₁ where _⊕_ _⊗_ : (D E : Desc) ! Desc κ : (A : Set) ! Desc ι : Desc ⟬_⟭ : Desc ! (Set ! Set) ⟬ D ⊕ E ⟭ X = ⟬ D ⟭ X ⊎ ⟬ E ⟭ X ⟬ D ⊗ E ⟭ X = ⟬ D ⟭ X × ⟬ E ⟭ X ⟬ κ A ⟭ X = A ⟬ ι ⟭ X = X data μ (D : Desc) : Set where init : ⟬ D ⟭ (μ D) ! μ D open codes meaning function
  49. `ListD : `Set ! `Desc `ListD A = `κ `⊤

    `⊕ (`κ A `⊗ `ι) `List : `Set ! `Set `List A = `μ (`ListD A) ListD : Set ! Desc ListD A = κ ⊤ ⊕ (κ A ⊗ ι) List : Set ! Set List A = μ (ListD A) closed open 3 4
  50. counts : (F : Set ! Set) (X : Set)

    ! F X ! ℕ abstract ₁ Fully Generic Count Function codes ₁ counts : (D : `Desc) (X : `Set) ! ⟬ ⟪ D ⟫ ⟭ ⟦ X ⟧ ! ℕ μ `μ abstract ₂ codes ₂ count : (A : Set) ! A ! ℕ count : (A : `Set) ! ⟦ A ⟧ ! ℕ
  51. counts : (F : Set ! Set) (X : Set)

    ! F X ! ℕ Fully Generic Count Function counts : (D : `Desc) (X : `Set) ! ⟬ ⟪ D ⟫ ⟭ ⟦ X ⟧ ! ℕ abstract ₂ codes ₂ count : (A : Set) ! A ! ℕ count : (A : `Set) ! ⟦ A ⟧ ! ℕ abstract ₁ codes ₁
  52. counts : (F : Set ! Set) (X : Set)

    ! F X ! ℕ Fully Generic Count Function counts : (D : `Desc) (X : `Set) ! ⟬ ⟪ D ⟫ ⟭ ⟦ X ⟧ ! ℕ abstract ₂ codes ₂ count : (A : Set) ! A ! ℕ count : (A : `Set) ! ⟦ A ⟧ ! ℕ abstract ₁ codes ₁
  53. count : (A : `Set) ! ⟦ A ⟧ !

    ℕ count (A `⊎ B) (inj₁ a) = 1 + count A a count (A `⊎ B) (inj₂ b) = 1 + count B b count (`Σ A B) (a , b) = 1 + count A a + count (B a) b count (`μ D) (init xs) = 1 + counts D (`μ D) xs count A a = 1 counts : (D : `Desc) (X : `Set) ! ⟬ ⟪ D ⟫ ⟭ ⟦ X ⟧ ! ℕ counts (D `⊕ E) X (inj₁ xs) = counts D X xs counts (D `⊕ E) X (inj₂ ys) = counts E X ys counts (D `⊗ E) X (xs , ys) = counts D X xs + counts E X ys counts (`κ A) X a = count A a counts `ι X x = count X x Fully Generic Count Function non-inductive argument 2
  54. count : (A : `Set) ! ⟦ A ⟧ !

    ℕ count (A `⊎ B) (inj₁ a) = 1 + count A a count (A `⊎ B) (inj₂ b) = 1 + count B b count (`Σ A B) (a , b) = 1 + count A a + count (B a) b count (`μ D) (init xs) = 1 + counts D (`μ D) xs count A a = 1 counts : (D : `Desc) (X : `Set) ! ⟬ ⟪ D ⟫ ⟭ ⟦ X ⟧ ! ℕ counts (D `⊕ E) X (inj₁ xs) = counts D X xs counts (D `⊕ E) X (inj₂ ys) = counts E X ys counts (D `⊗ E) X (xs , ys) = counts D X xs + counts E X ys counts (`κ A) X a = count A a counts `ι X x = count X x Fully Generic Count Function constructor arguments 3
  55. count : (A : `Set) ! ⟦ A ⟧ !

    ℕ count (A `⊎ B) (inj₁ a) = 1 + count A a count (A `⊎ B) (inj₂ b) = 1 + count B b count (`Σ A B) (a , b) = 1 + count A a + count (B a) b count (`μ D) (init xs) = 1 + counts D (`μ D) xs count A a = 1 counts : (D : `Desc) (X : `Set) ! ⟬ ⟪ D ⟫ ⟭ ⟦ X ⟧ ! ℕ counts (D `⊕ E) X (inj₁ xs) = counts D X xs counts (D `⊕ E) X (inj₂ ys) = counts E X ys counts (D `⊗ E) X (xs , ys) = counts D X xs + counts E X ys counts (`κ A) X a = count A a counts `ι X x = count X x inductive argument Fully Generic Count Function 3
  56. count : (A : `Set) ! ⟦ A ⟧ !

    ℕ count (A `⊎ B) (inj₁ a) = 1 + count A a count (A `⊎ B) (inj₂ b) = 1 + count B b count (`Σ A B) (a , b) = 1 + count A a + count (B a) b count (`μ D) (init xs) = 1 + counts D (`μ D) xs count A a = 1 counts : (D : `Desc) (X : `Set) ! ⟬ ⟪ D ⟫ ⟭ ⟦ X ⟧ ! ℕ counts (D `⊕ E) X (inj₁ xs) = counts D X xs counts (D `⊕ E) X (inj₂ ys) = counts E X ys counts (D `⊗ E) X (xs , ys) = counts D X xs + counts E X ys counts (`κ A) X a = count A a counts `ι X x = count X x non-inductive argument Fully Generic Count Function 4
  57. Generic Programming generic : (A : ) ! A !

    ꔇ generic : (C : ) ! ⟦ C ⟧ ! ꔇ reflect ⟦_⟧ : ! meaning function reify concrete subset abstract type codes type
  58. Outline & Contribution Generic Programming Built-In Types User-Declared Types Traditional

    (open) Fully (closed) 1 ⟦_⟧ : Size ! Set 2 ⟦_⟧ : Count ! Set 3 ⟬_⟭ : Desc ! (Set ! Set) μ : Desc ! Set ⟦_⟧ : `Set ! Set ⟪_⟫ : `Desc ! Desc 4
  59. Solution • Fully generic programming • Code reuse & correctness

    in dependently typed programming • Despite proliferation of dependent types • Generic functions work over all types defined now and all types defined in the future • Generic functions do not need to be edited when a new type is declared • Dependent typing ensures generic functions are well-typed! • By being defined over a closed universe of user-declared types • The universe models a closed dependently typed programming language 4
  60. In The Thesis • Intensional model of closed user-declared types

    • extended to dependent, infinitary, and inductive-recursive polynomial functors • Examples of fully generic programming using the model • i.e. count, lookup, ast • Special attention payed to role of types versus role of kinds • Procedure to close any universe of types • Generalization of model of types to kinds, superkinds, etc. • Modeling a closed hierarchy of user-declared types • Modeling a language that internalizes polymorphism, hence internalizes fully generic programming
  61. reflect ⟦_⟧₂ : ! meaning function reify concrete subset abstract

    type codes type generic : (A : ) ! ⟦ A ⟧₁ ! ꔇ ⟦_⟧₁ : ! Set generic : (C : ) ! ⟦ ⟦ C ⟧₂ ⟧₁ ! ꔇ Generic Programming
  62. ListF : Set ! Set ! Set ListF A X

    = ⊤ ⊎ A × X List : Set ! Set List A = μ (ListF A) nil : {A : Set} ! List A nil = init (inj₁ tt) cons : {A : Set} ! A ! List A ! List A cons x xs = init (inj₂ (x , xs))
  63. `ListD : `Set ! `Desc `ListD A = `κ `⊤

    `⊕ (`κ A `⊗ `ι) `List : `Set ! `Set `List A = `μ (`ListD A) nil : {A : `Set} ! ⟦ `List A ⟧ nil = init (inj₁ tt) cons : {A : `Set} ! ⟦ A ⟧ ! ⟦ `List A ⟧ ! ⟦ `List A ⟧ cons x xs = init (inj₂ (x , xs))