1.8 The type of booleans
The type of booleans π:π° is intended to have exactly two elements
0π,1π:π. It is clear that we could construct this
type out of coproduct
and unit types as π+π. However,
since it is used frequently, we give the explicit rules here.
Indeed, we are going to observe that we can also go the other way
and derive binary coproducts from Ξ£-types and π.
To derive a function f:πβC we need c0,c1:C and add the defining equations
f(0π) | :β‘c0, | ||
f(1π) | :β‘c1. |
The recursor corresponds to the if-then-else construct in functional programming:
ππΎπΌπ:βC:π°CβCβπβC |
with the defining equations
ππΎπΌπ(C,c0,c1,0π) | :β‘c0, | ||
ππΎπΌπ(C,c0,c1,1π) | :β‘c1. |
Given C:πβπ°, to derive a dependent function f:β(x:π)C(x) we need c0:C(0π) and c1:C(1π), in which case we can give the defining equations
f(0π) | :β‘c0, | ||
f(1π) | :β‘c1. |
We package this up into the induction principle
πππ½π:β(C:πβπ°)C(0π)βC(1π)ββ(x:π)C(x) |
with the defining equations
πππ½π(C,c0,c1,0π) | :β‘c0, | ||
πππ½π(C,c0,c1,1π) | :β‘c1. |
As an example, using the induction principle we can prove that, as we expect, every element of π is either 1π or 0π. As before, we use the equality types which we have not yet introduced, but we need only the fact that everything is equal to itself by ππΎπΏπ x:x=x.
Theorem 1.8.1.
We have
βx:π(x=0π)+(x=1π). |
Proof.
We use the induction principle with C(x):β‘(x=0π)+(x=1π). The two inputs are πππ (ππΎπΏπ 0π):C(0π) and πππ(ππΎπΏπ 1π):C(1π). β
We have remarked that Ξ£-types can be regarded as analogous to indexed disjoint unions, while coproducts are binary disjoint unions.
It is natural to expect that a binary disjoint union A+B could be constructed as an indexed one over the two-element type π.
For this we need a type family P:πβπ° such that P(0π)β‘A and P(1π)β‘B.
Indeed, we can obtain such a family precisely by the recursion principle for π.
(The ability to define type families by induction and recursion, using the fact that the universe
π° is itself a type, is a subtle and important aspect of type theory
.)
Thus, we could have defined
A+B:β‘βx:πππΎπΌπ(π°,A,B,x). |
with
πππ (a) | :β‘(0π,a), | ||
πππ(b) | :β‘(1π,b). |
We leave it as an exercise to derive the induction principle of a coproduct type from this definition. (See also PMlinkexternalExercise 1.5http://planetmath.org/node/87558,Β§5.2 (http://planetmath.org/52uniquenessofinductivetypes).)
We can apply the same idea to products and Ξ -types: we could have defined
AΓB:β‘βx:πππΎπΌπ(π°,A,B,x) |
Pairs could then be constructed using induction for π:
(a,b):β‘πππ½π(ππΎπΌπ(π°,A,B),a,b) |
while the projections are straightforward applications
ππ1(p) | :β‘p(0π), | ||
ππ2(p) | :β‘p(1π). |
The derivation of the induction principle for binary products defined in this way is a bit more involved, and requires function extensionality, which we will introduce in Β§2.9 (http://planetmath.org/29pitypesandthefunctionextensionalityaxiom).
Moreover, we do not get the same judgmental equalities; see http://planetmath.org/node/87559Exercise 1.6.
This is a recurrent issue when encoding one type as another; we will return to it in Β§5.5 (http://planetmath.org/55homotopyinductivetypes).
We may occasionally refer to the elements 0π and 1π of π as βfalseβ and βtrueβ respectively.
However, note that unlike in classical mathematics, we do not use elements of π as truth values
or as propositions.
(Instead we identify propositions with types; see Β§1.11 (http://planetmath.org/111propositionsastypes).)
In particular, the type Aβπ is not generally the power set
of A; it represents only the βdecidableβ subsets of A (see http://planetmath.org/node/87576Chapter 3).
Title | 1.8 The type of booleans |
---|---|
\metatable |