5.3 W-types
Inductive types are very general, which is excellent for their usefulness and applicability, but makes them difficult to study as a whole.
Fortunately, they can all be formally reduced to a few special cases.
It is beyond the scope of this book to discuss this reduction β which is anyway irrelevant to the mathematician using type theory
in practice β but we will take a little time to discuss the one of the basic special cases that we have not yet met.
These are Martin-LΓΆfβs W-types, also known as the types of well-founded trees.
πΆ-types are a generalization
of such types as natural numbers
, lists, and binary trees
, which are sufficiently general to encapsulate the βrecursionβ aspect of any inductive type.
A particular πΆ-type is specified by giving two parameters A:π° and B:Aβπ°, in which case the resulting πΆ-type is written πΆ(a:A)B(a).
The type A represents the type of labels for πΆ(a:A)B(a), which function as constructors (however, we reserve that word for the actual functions which arise in inductive definitions). For instance, when defining natural numbers as a πΆ-type,
the type A would be the type π inhabited by the two elements 0π and 1π, since there are precisely two ways to obtain a natural number β either it will be zero or a successor of another natural number.
The type family B:Aβπ° is used to record the arity of labels: a label a:A will take a family of inductive arguments, indexed over B(a). We can therefore think of the βB(a)-manyβ arguments of a. These arguments are represented by a function f:B(a)βπΆ(a:A)B(a), with the understanding that for any b:B(a), f(b) is the βb-thβ argument to the label a. The πΆ-type πΆ(a:A)B(a) can thus be thought of as the type of well-founded trees, where nodes are labeled by elements of A and each node labeled by a:A has B(a)-many branches.
In the case of natural numbers, the label 0π has arity 0, since it constructs the constant zero; the label 1π has arity 1, since it constructs the successor of its argument. We can capture this by using simple elimination on π to define a function ππΎπΌπ(π°,π,π) into a universe of types; this function returns the empty type π for 0π and the unit type π for 1π. We can thus define
ππ°:β‘πΆ(b:π)ππΎπΌπ(π°,π,π) |
where the superscript π° serves to distinguish this version of natural numbers from the previously used one. Similarly, we can define the type of lists over A as a πΆ-type with π+A many labels: one nullary label for the empty list, plus one unary label for each a:A, corresponding to appending a to the head of a list:
π«πππ(A):β‘πΆ(x:π+A)ππΎπΌπ+A(π°,β0,Ξ»a.β1). |
In general, the πΆ-type πΆ(x:A)B(x) specified by A:π° and B:Aβπ° is the inductive type generated by the following constructor:
-
β’
πππ:β(a:A)(B(a)βπΆ(x:A)B(x))βπΆ(x:A)B(x).
The constructor πππ (short for supremum) takes a label a:A and a function f:B(a)βπΆ(x:A)B(x) representing the arguments to a, and constructs a new element of πΆ(x:A)B(x). Using our previous encoding of natural numbers as πΆ-types, we can for instance define
0π°:β‘πππ(0π,Ξ»x.ππΎπΌπ(ππ°,x)). |
Put differently, we use the label 0π to construct 0π°. Then, ππΎπΌπ(π°,π,π,0π) evaluates to π, as it should since 0π is a nullary label. Thus, we need to construct a function f:πβππ°, which represents the (zero) arguments supplied to 0π. This is of course trivial, using simple elimination on π as shown. Similarly, we can define
1π° | :β‘πππ(1π,Ξ»x.β0π°) | ||
2π° | :β‘πππ(1π,Ξ»x.β1π°) |
and so on.
We have the following induction principle for πΆ-types:
-
β’
When proving a statement E:(πΆ(x:A)B(x))βπ° about all elements of the πΆ-type πΆ(x:A)B(x), it suffices to prove it for πππ(a,f), assuming it holds for all f(b) with b:B(a). In other words, it suffices to give a proof
e:β(a:A)β(f:B(a)βπΆ(x:A)B(x))β(g:β(b:B(a))E(f(b)))E(πππ(a,f))
The variable g represents our inductive hypothesis, namely that all arguments of a satisfy E. To state this, we quantify over all elements of type B(a), since each b:B(a) corresponds to one argument f(b) of a.
How would we define the function π½πππ»π πΎ on natural numbers encoded as a πΆ-type? We would like to use the recursion principle of ππ° with a codomain of ππ° itself. We thus need to construct a suitable function
e:β(a:π)β(f:B(a)βππ°)β(g:B(a)βππ°)ππ° |
which will represent the recurrence for the π½πππ»π πΎ function; for simplicity we denote the type family ππΎπΌπ(π°,π,π) by B.
Clearly, e will be a function taking a:π as its first argument. The next step is to perform case analysis on a and proceed based on whether it is 0π or 1π. This suggests the following form
e:β‘Ξ»a.ππΎπΌπ(C,e0,e1,a) |
where
C:β‘β(f:B(a)βππ°)β(g:B(a)βππ°)ππ° |
If a is 0π, the type B(a) becomes π. Thus, given f:πβππ° and g:πβππ°, we want to construct an element of ππ°. Since the label 0π represents π, it needs zero inductive arguments and the variables f and g are irrelevant. We return 0π° as a result:
e0:β‘Ξ»f.Ξ»g.β0π° |
Analogously, if a is 1π, the type B(a) becomes π. Since the label 1π represents the successor operator, it needs one inductive argument β the predecessor β which is represented by the variable f:πβππ°. The value of the recursive call on the predecessor is represented by the variable g:πβππ°. Thus, taking this value (namely g(β)) and applying the successor operator twice thus yields the desired result:
e1:β‘Ξ»f.Ξ»g.πππ(1π,(Ξ»x.πππ(1π,(Ξ»y.g(β))))). |
Putting this together, we thus have
π½πππ»π πΎ:β‘ππΎπΌππ°(ππ°,e) |
with e as defined above.
The associated computation rule for the function ππΎπΌπΆ(x:A)B(x)(E,e):β(w:πΆ(x:A)B(x))E(w) is as follows.
-
β’
For any a:A and f:B(a)βπΆ(x:A)B(x) we have
ππΎπΌπΆ(x:A)B(x)(E,e,πππ(a,f))β‘e(a,f,(Ξ»b.ππΎπΌπΆ(x:A)B(x)(E,f(b)))).
In other words, the function ππΎπΌπΆ(x:A)B(x)(E,e) satisfies the recurrence e.
By the above computation rule, the function π½πππ»π πΎ behaves as expected:
π½πππ»π πΎ(0π°) | β‘ππΎπΌππ°(ππ°,e,πππ(0π,Ξ»x.ππΎπΌπ(ππ°,x))) | |||
β‘e(0π,(Ξ»x.ππΎπΌπ(ππ°,x)),(Ξ»x.π½πππ»π πΎ(ππΎπΌπ(ππ°,x)))) | ||||
β‘et((Ξ»x.ππΎπΌπ(ππ°,x)),(Ξ»x.π½πππ»π πΎ(ππΎπΌπ(ππ°,x)))) | ||||
β‘0π° | ||||
and | ||||
π½πππ»π πΎ(1π°) | β‘ππΎπΌππ°(ππ°,e,πππ(1π,Ξ»x.β0π°)) | |||
β‘e(1π,(Ξ»x.β0π°),(Ξ»x.π½πππ»π πΎ(0π°))) | ||||
β‘ef((Ξ»x.β0π°),(Ξ»x.π½πππ»π πΎ(0π°))) | ||||
β‘πππ(1π,(Ξ»x.πππ(1π,(Ξ»y.π½πππ»π πΎ(0π°))))) | ||||
β‘πππ(1π,(Ξ»x.πππ(1π,(Ξ»y.β0π°)))) | ||||
β‘2π° |
and so on.
Just as for natural numbers, we can prove a uniqueness theorem for πΆ-types:
Theorem 5.3.1.
Let g,h:β(w:W(x:A)B(x))E(w) be two functions which satisfy the recurrence
e:βa,f(βb:B(a)E(f(b)))βE(πππ(a,f)), |
i.e., such that
βa,fg(πππ(a,f))=e(a,f,Ξ»b.g(f(b))), | ||
βa,fh(πππ(a,f))=e(a,f,Ξ»b.h(f(b))). |
Then g and h are equal.
Title | 5.3 W-types |
---|---|
\metatable |