5.1 Introduction to inductive types
An inductive type 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 . This includes functions of zero arguments, which are simply elements of .
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:
-
β’
-
β’
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 , which is generated by the two constructors
-
β’
-
β’
.
And an example with a constructor taking multiple arguments is the cartesian product , which is generated by one constructor
-
β’
.
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
-
β’
-
β’
.
Another useful example is the type of finite lists of elements of some type , which has constructors
-
β’
-
β’
.
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 and . Similarly, in the case of , we should expect that every element is either 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 about all inhabitants of , it suffices to prove it for and , i.e., to give proofs and .
Furthermore, the resulting proof behaves as expected when applied to the constructors and ; this principle is expressed by the computation rules:
-
β’
We have .
-
β’
We have .
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 , we also want to presume that the statement being proven has already been shown for . This gives us the following induction principle:
-
β’
When proving a statement about all natural numbers, it suffices to prove it for and for , assuming it holds for , i.e., we construct and .
The variable represents our inductive hypothesis. As in the case of booleans, we also have the associated computation rules for the function :
-
β’
.
-
β’
for any .
The dependent function can thus be understood as being defined recursively on the argument , via the functions and which we call the recurrences. When is zero, the function simply returns . When is the successor of another natural number , the result is obtained by taking the recurrence and substituting the specific predecessor and the recursive call value .
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 be two functions which satisfy the recurrences
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 |