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

Fully Generic Programming over Closed Universes of Types

Fully Generic Programming over Closed Universes of Types

My Ph.D. defense.

72003cf388f6f7f95b433de3df9ccd38?s=128

larrytheliquid

May 08, 2017
Tweet

Transcript

  1. Fully Generic Programming over Closed Universes of Types Larry Diehl

    Thesis Defense May 8, 2017
  2. 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
  3. 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
  4. 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
  5. 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)
  6. Generic Programming ℕ List Tree count lookup marshal append universe

    of types generic functions 3 * 4 = 12
  7. Proliferation of Types ℕ List Tree 3 * 4 =

    12
  8. Proliferation of Types ℕ List Tree • edge labeled •

    node labeled • both labeled • n-ary • cons-based • snoc-based structurally different 7 * 4 = 28
  9. 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
  10. Generic Programming generic : (A : Set) ! A !

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

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

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

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

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

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

    meaning function { A : | C : where ⟦ C ⟧ ≡ A } (a possibly infinite collection of types) image of meaning function
  17. 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
  18. Outline & Contribution Generic Programming Built-In Types User-Declared Types Traditional

    (open) Fully (closed) 1 2 3 4
  19. Traditional Generic Programming over Open Built-In Types 1

  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. Fully Generic Programming over Closed Built-In Types 2

  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. Traditional Generic Programming over Open User-Declared Types 3

  34. 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
  35. Sum of Products C1 ⊎ C2 ⊎ … ⊎ Cn

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

    y A1 × A2 x * y ⊤ 1 Bool ≅ ⊤ ⊎ ⊤ 2 = 1 + 1 Tril ≅ ⊤ ⊎ ⊤ ⊎ ⊤ 3 = 1 + 1 + 1
  37. 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
  38. 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)
  39. 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
  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 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
  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 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
  42. 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
  43. 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
  44. 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 ! ℕ
  45. 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 ! ℕ
  46. 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 ! ℕ
  47. 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
  48. 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
  49. Fully Generic Programming over Closed User-Declared Types 4

  50. 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
  51. 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
  52. 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
  53. 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 ⟫
  54. 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
  55. 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
  56. 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
  57. `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
  58. 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 ⟧ ! ℕ
  59. 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 ₁
  60. 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 ₁
  61. 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
  62. 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
  63. 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
  64. 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
  65. Conclusion

  66. Generic Programming generic : (A : ) ! A !

    ꔇ generic : (C : ) ! ⟦ C ⟧ ! ꔇ reflect ⟦_⟧ : ! meaning function reify concrete subset abstract type codes type
  67. 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
  68. 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
  69. 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
  70. Questions?

  71. reflect ⟦_⟧₂ : ! meaning function reify concrete subset abstract

    type codes type generic : (A : ) ! ⟦ A ⟧₁ ! ꔇ ⟦_⟧₁ : ! Set generic : (C : ) ! ⟦ ⟦ C ⟧₂ ⟧₁ ! ꔇ Generic Programming
  72. 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))
  73. `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))