5.1 Introduction to inductive types
An inductive type X can be intuitively understood as a type βfreely generatedβ by a certain finite collection of constructors, each of which is a function (of some number of arguments) with codomain X.
This includes functions of zero arguments, which are simply elements of X.
When describing a particular inductive type, we list the constructors with bullets. For instance, the type π from Β§1.8 (http://planetmath.org/18thetypeofbooleans) is inductively generated by the following constructors:
-
β’
0π:π
-
β’
1π:π
Similarly, π is inductively generated by the constructor:
-
β’
β:π
while π is inductively generated by no constructors at all.
An example where the constructor functions take arguments is the coproduct A+B, which is generated by the two constructors
-
β’
πππ :AβA+B
-
β’
πππ:BβA+B.
And an example with a constructor taking multiple arguments is the cartesian product AΓB, which is generated by one constructor
-
β’
(β,β):AβBβAΓB.
Crucially, we also allow constructors of inductive types that take arguments from the inductive type being defined.
For instance, the type β of natural numbers has constructors
-
β’
0:β
-
β’
πππΌπΌ:βββ.
Another useful example is the type π«πππ(A) of finite lists of elements of some type A, which has constructors
-
β’
πππ :π«πππ(A)
-
β’
πΌπππ:Aβπ«πππ(A)βπ«πππ(A).
Intuitively, we should understand an inductive type as being freely generated by its constructors. That is, the elements of an inductive type are exactly what can be obtained by starting from nothing and applying the constructors repeatedly. (We will see in Β§5.8 (http://planetmath.org/58identitytypesandidentitysystems),http://planetmath.org/node/87579Chapter 6 that this conception has to be modified slightly for more general kinds of inductive definitions, but for now it is sufficient.) For instance, in the case of π, we should expect that the only elements are 0π and 1π. Similarly, in the case of β, we should expect that every element is either 0 or obtained by applying πππΌπΌ to some βpreviously constructedβ natural number.
Rather than assert properties such as this directly, however, we express them by means of an induction principle, also called a (dependent) elimination rule.
We have seen these principles already in http://planetmath.org/node/87533Chapter 1.
For instance, the induction principle for π is:
-
β’
When proving a statement E:πβπ° about all inhabitants of π, it suffices to prove it for 0π and 1π, i.e., to give proofs e0:E(0π) and e1:E(1π).
Furthermore, the resulting proof πππ½π(E,e0,e1):β(b:π)E(b) behaves as expected when applied to the constructors 0π and 1π; this principle is expressed by the computation rules:
-
β’
We have πππ½π(E,e0,e1,0π)β‘e0.
-
β’
We have πππ½π(E,e0,e1,1π)β‘e1.
Thus, the induction principle for the type π of booleans allow us to reason by case analysis. Since neither of the two constructors takes any arguments, this is all we need for booleans.
For natural numbers, however, case analysis is generally not sufficient: in the case corresponding to the inductive step πππΌπΌ(n), we also want to presume that the statement being proven has already been shown for n. This gives us the following induction principle:
-
β’
When proving a statement E:ββπ° about all natural numbers, it suffices to prove it for 0 and for πππΌπΌ(n), assuming it holds for n, i.e., we construct ez:E(0) and es:β(n:β)β(y:E(n))E(πππΌπΌ(n)).
The variable y represents our inductive hypothesis. As in the case of booleans, we also have the associated computation rules for the function πππ½β(E,ez,es):β(x:β)E(x):
-
β’
πππ½β(E,ez,es,0)β‘ez.
-
β’
πππ½β(E,ez,es,πππΌπΌ(n))β‘es(n,πππ½β(E,ez,es,n)) for any n:β.
The dependent function πππ½β(E,ez,es) can thus be understood as being defined recursively on the argument x:β, via the functions ez and es which we call the recurrences.
When x is zero, the function simply returns ez.
When x is the successor of another natural number n, the result is obtained by taking the recurrence es and substituting the specific predecessor n and the recursive call value πππ½β(E,ez,es,n).
The induction principles for all the examples mentioned above share this family resemblance. In Β§5.6 (http://planetmath.org/56thegeneralsyntaxofinductivedefinitions) we will discuss a general notion of βinductive definitionβ and how to derive an appropriate induction principle for it, but first we investigate various commonalities between inductive definitions.
For instance, we have remarked in every case in http://planetmath.org/node/87533Chapter 1 that from the induction principle we can derive a recursion principle in which the codomain is a simple type (rather than a family).
Both induction and recursion principles may seem odd, since they yield only the existence of a function without seeming to characterize it uniquely.
However, in fact the induction principle is strong enough also to prove its own uniqueness principle, as in the following theorem.
Theorem 5.1.1.
Let f,g:β(x:N)E(x) be two functions which satisfy the recurrences
ez:E(0)ββ |
up to propositional equality, i.e., such that
as well as
Then and are equal.
Proof.
We use induction on the type family . For the base case, we have
For the inductive case, assume such that . Then
The first and last equality follow from the assumptions on and . The middle equality follows from the inductive hypothesis and the fact that application preserves equality. This gives us pointwise equality between and ; invoking function extensionality finishes the proof.
β
Note that the uniqueness principle applies even to functions that only satisfy the recurrences up to propositional equality, i.e. a path. Of course, the particular function obtained from the induction principle satisfies these recurrences judgmentally; we will return to this point in Β§5.5 (http://planetmath.org/55homotopyinductivetypes). On the other hand, the theorem itself only asserts a propositional equality between functions (see also http://planetmath.org/node/87832Exercise 5.2). From a homotopical viewpoint it is natural to ask whether this path is coherent, i.e. whether the equality is unique up to higher paths; in Β§5.4 (http://planetmath.org/54inductivetypesareinitialalgebras) we will see that this is in fact the case.
Of course, similar uniqueness theorems for functions can generally be formulated and shown for other inductive types as well.
In the next section, we show how this uniqueness property, together with univalence, implies that an inductive type such as the natural numbers is completely characterized by its introduction, elimination, and computation rules.
Title | 5.1 Introduction to inductive types |
---|---|
\metatable |