comparison src/InductiveTypes.v @ 26:65314ca099ed

Start of Inductive Types
author Adam Chlipala <adamc@hcoop.net>
date Mon, 08 Sep 2008 14:19:50 -0400
parents
children 8788249c7d3a
comparison
equal deleted inserted replaced
25:26ad686e68f2 26:65314ca099ed
1 (* Copyright (c) 2008, Adam Chlipala
2 *
3 * This work is licensed under a
4 * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
5 * Unported License.
6 * The license text is available at:
7 * http://creativecommons.org/licenses/by-nc-nd/3.0/
8 *)
9
10 (* begin hide *)
11 Require Import List.
12
13 Require Import Tactics.
14
15 Set Implicit Arguments.
16 (* end hide *)
17
18
19 (** %\chapter{Inductive Types}% *)
20
21 (** In a sense, CIC is built from just two relatively straightforward features: function types and inductive types. From this modest foundation, we can prove effectively all of the theorems of math and carry out effectively all program verifications, with enough effort expended. This chapter introduces induction and recursion in Coq and shares some "design patterns" for overcoming common pitfalls with them. *)
22
23
24 (** * Enumerations *)
25
26 (** Coq inductive types generalize the algebraic datatypes found in Haskell and ML. Confusingly enough, inductive types also generalize generalized algebraic datatypes (GADTs), by adding the possibility for type dependency. Even so, it is worth backing up from the examples of the last chapter and going over basic, algebraic datatype uses of inductive datatypes, because the chance to prove things about the values of these types adds new wrinkles beyond usual practice in Haskell and ML.
27
28 The singleton type [unit] is an inductive type: *)
29
30 Inductive unit : Set :=
31 | tt.
32
33 (** This vernacular command defines a new inductive type [unit] whose only value is [tt], as we can see by checking the types of the two identifiers: *)
34
35 Check unit.
36 (** [[
37
38 unit : Set
39 ]] *)
40 Check tt.
41 (** [[
42
43 tt : unit
44 ]] *)
45
46 (** We can prove that [unit] is a genuine singleton type. *)
47
48 Theorem unit_singleton : forall x : unit, x = tt.
49 (** The important thing about an inductive type is, unsurprisingly, that you can do induction over its values, and induction is the key to proving this theorem. We ask to proceed by induction on the variable [x]. *)
50 induction x.
51 (** The goal changes to: [[
52
53 tt = tt
54 ]] *)
55 (** ...which we can discharge trivially. *)
56 reflexivity.
57 Qed.
58
59 (** It seems kind of odd to write a proof by induction with no inductive hypotheses. We could have arrived at the same result by beginning the proof with: [[
60
61 destruct x.
62 ...which corresponds to "proof by case analysis" in classical math. For non-recursive inductive types, the two tactics will always have identical behavior. Often case analysis is sufficient, even in proofs about recursive types, and it is nice to avoid introducing unneeded induction hypotheses.
63
64 What exactly %\textit{%#<i>#is#</i>#%}% the induction principle for [unit]? We can ask Coq: *)
65
66 Check unit_ind.
67 (** [[
68
69 unit_ind : forall P : unit -> Prop, P tt -> forall u : unit, P u
70 ]]
71
72 Every [Inductive] command defining a type [T] also defines an induction principle named [T_ind]. Coq follows the Curry-Howard correspondence and includes the ingredients of programming and proving in the same single syntactic class. Thus, our type, operations over it, and principles for reasoning about it all live in the same language and are described by the same type system. The key to telling what is a program and what is a proof lies in the distinction between the type [Prop], which appears in our induction principle; and the type [Set], which we have seen a few times already.
73
74 The convention goes like this: [Set] is the type of normal types, and the values of such types are programs. [Prop] is the type of logical propositions, and the values of such types are proofs. Thus, an induction principle has a type that shows us that it is a function for building proofs.
75
76 Specifically, [unit_ind] quantifies over a predicate [P] over [unit] values. If we can present a proof that [P] holds of [tt], then we are rewarded with a proof that [P] holds for any value [u] of type [unit]. In our last proof, the predicate was [(fun u : unit => u = tt)].
77
78 %\medskip%
79
80 We can define an inductive type even simpler than [unit]: *)
81
82 Inductive Empty_set : Set := .
83
84 (** [Empty_set] has no elements. We can prove fun theorems about it: *)
85
86 Theorem the_sky_is_falling : forall x : Empty_set, 2 + 2 = 5.
87 destruct 1.
88 Qed.
89
90 (** Because [Empty_set] has no elements, the fact of having an element of this type implies anything. We use [destruct 1] instead of [destruct x] in the proof because unused quantified variables are relegated to being referred to by number. (There is a good reason for this, related to the unity of quantifiers and dependent function types.)
91
92 We can see the induction principle that made this proof so easy: *)
93
94 Check Empty_set_ind.
95 (** [[
96
97 Empty_set_ind : forall (P : Empty_set -> Prop) (e : Empty_set), P e
98 ]]
99
100 In other words, any predicate over values from the empty set holds vacuously of every such element. In the last proof, we chose the predicate [(fun _ : Empty_set => 2 + 2 = 5)].
101
102 We can also apply this get-out-of-jail-free card programmatically. Here is a lazy way of converting values of [Empty_set] to values of [unit]: *)
103
104 Definition e2u (e : Empty_set) : unit := match e with end.
105
106 (** We employ [match] pattern matching as in the last chapter. Since we match on a value whose type has no constructors, there is no need to provide any branches.
107
108 %\medskip%
109
110 Moving up the ladder of complexity, we can define the booleans: *)
111
112 Inductive bool : Set :=
113 | true
114 | false.
115
116 (** We can use less vacuous pattern matching to define boolean negation. *)
117
118 Definition not (b : bool) : bool :=
119 match b with
120 | true => false
121 | false => true
122 end.
123
124 (** We might want to prove that [not] is its own inverse operation. *)
125
126 Theorem not_inverse : forall b : bool, not (not b) = b.
127 destruct b.
128
129 (** After we case analyze on [b], we are left with one subgoal for each constructor of [bool].
130
131 [[
132
133 2 subgoals
134
135 ============================
136 not (not true) = true
137 ]]
138
139 [[
140 subgoal 2 is:
141 not (not false) = false
142 ]]
143
144 The first subgoal follows by Coq's rules of computation, so we can dispatch it easily: *)
145
146 reflexivity.
147
148 (** Likewise for the second subgoal, so we can restart the proof and give a very compact justification. *)
149
150 Restart.
151 destruct b; reflexivity.
152 Qed.