This is cumbersome and prone to mistakes. Instead we use pattern matching. A function $A -> C$ is completely specified if it's specified on the *canonical elements* of $A$.
$ isZero& : Nat -> Bool \
isZero& zero defeq tru \
isZero& suc (n) defeq fls \
$
=== Pairs
#let pair = $sans("pair")$
#typeIntroTable(
[If $A$ and $B$ are types, then $A times B$ is a type],
[If $a : A$ and $b : B$, then $pair(a, b) : A times B$],
[If $C$ is a type, and $p : A -> B -> C$ and $t : A times B$, then $rec_times (A, B, C, p, t) : C$],
[...],
)
#let fst = $sans("fst")$
#let snd = $sans("snd")$
#let swap = $sans("swap")$
#let assoc = $sans("assoc")$
*Exercise.* Define $snd : A times B -> A$ and $snd : A times B -> B$.
*Exercise.* Given types $A$ and $B$, write a function $swap$ of type $A times B -> B times A$.
*Exercise.* What is the type of $swap @ pair (tt , fls)$?
*Exercise.* Write a function $assoc$ of type $(A times B) times C -> A times (B times C)$
=== Type dependency
In particular: dependent type $B$ over $A$. (read "family $B$ of types indexed by $A$")
Example: type of vectors.
$ n : Nat tack.r Vect(n) $
=== Universes
#let Type = $sans("Type")$
There is a type $Type$. Its elements are types ($A : Type$). The dependent function $x : A tack.r B$ can be considered a function $lambda x . B : A -> Type$.
What is the type of $Type$? $Type$ is in an indexed hierarchy to avoid type-in-type paradox. We usually omit the index: $Type_i : Type_(i + 1)$
=== Dependent functions $product_(x : A) B$
#typeIntroTable(
[If $A$ is a type, and $B$ is a type family indexed by $A$, then there is a type $product_(x : A) B$],
[If $(x : A) tack.r b : B$, then $lambda (x : A) . b : product_(x : A) B$],
[If $f : product_(x : A) B$ and $a : A$, then $f(a) : subst(x, a, B)$],
[$(lambda (x : A) . b) ( a) defeq subst(x, a, b)$
- The case $A -> B$ is a special case of the dependent function, where $B$ doesn't depend on $x : A$],
)
=== Dependent pairs $Sigma_(x : A) B$
#typeIntroTable(
[If $x : A tack.r B$, then there is a type $Sigma_(x : A) B$],
[If $a : A$ and $b : B(a)$, then $pair (a, b) : Sigma_(x : A) B$],
[...],
[...
- The case $A times B$ is a special case of the dependent function, where $B$ doesn't depend on $x : A$],
)
=== Identity type
#let Id = $sans("Id")$
#let refl = $sans("refl")$
#let propeq = $=$
#typeIntroTable(
[If $a : A$ and $b : A$, then $Id_A (a, b)$ is a type],
[If $a : A$, then $refl(a) : Id_A (a, a)$],
[
- If $(x, y : A), (p : Id_A (x, y)) tack.r C (x, y, p)$
- and $(x : A) tack.r t(x) : C(x, x, refl(x))$
- then $(x, y : A), (p : Id_A (x, y)) tack.r ind_Id (t; x, y, p) : C(x, y, p)$],
[...],
)
- (There are dependent versions of each of the previous elimination rules that resulted in $C$)
- $ind$ "induction" is used more when the $C$ is dependent, while $rec$ is better for non-dependent $C$'s.
For example, to define:
#let sym = $sans("sym")$
$ sym : product_(x, y : A) Id(x, y) -> Id(y, x) $
it suffices to just specify its image on $(x, x, refl)$
#prooftree(
axiom($x : A tack.r refl(x) : Id(x, x)$),
rule($(x, y : A) , (p : Id(x, y)) tack.r Id(y, x)$),
rule($x, y : A tack.r Id(x, y) -> Id(y, x)$),
rule($sym : product_(x, y : A) Id(x, y) -> Id(y, x)$)
)
So $sym(x, x, refl(x)) :defeq refl(x)$
#let transport = $sans("transport")$
*Exercise.* Define $transport^B : product_(x, y : A) Id(x, y) -> B(x) -> B(y)$.
*Exercise: swap is involutive.* Given types $A$ and $B$, write a function $product_(t : A times B) Id(swap(swap(t)), t)$
=== Disjoint sum
#let inl = $sans("inl")$
#let inr = $sans("inr")$
#typeIntroTable(
[If $A$ and $B$ are types, then $A + B$ is a type],
[
- If $a : A$, then $inl(a) : A + B$
- If $b : B$, then $inr(b) : A + B$
],
[If $f : A -> C$ and $g : B ->C $ then $rec_+ (C, f, g) : A + B -> C$],