annotate src/InductiveTypes.v @ 27:8788249c7d3a

bool
author Adam Chlipala <adamc@hcoop.net>
date Mon, 08 Sep 2008 14:28:55 -0400
parents 65314ca099ed
children 0543bbd62843
rev   line source
adamc@26 1 (* Copyright (c) 2008, Adam Chlipala
adamc@26 2 *
adamc@26 3 * This work is licensed under a
adamc@26 4 * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
adamc@26 5 * Unported License.
adamc@26 6 * The license text is available at:
adamc@26 7 * http://creativecommons.org/licenses/by-nc-nd/3.0/
adamc@26 8 *)
adamc@26 9
adamc@26 10 (* begin hide *)
adamc@26 11 Require Import List.
adamc@26 12
adamc@26 13 Require Import Tactics.
adamc@26 14
adamc@26 15 Set Implicit Arguments.
adamc@26 16 (* end hide *)
adamc@26 17
adamc@26 18
adamc@26 19 (** %\chapter{Inductive Types}% *)
adamc@26 20
adamc@26 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. *)
adamc@26 22
adamc@26 23
adamc@26 24 (** * Enumerations *)
adamc@26 25
adamc@26 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.
adamc@26 27
adamc@26 28 The singleton type [unit] is an inductive type: *)
adamc@26 29
adamc@26 30 Inductive unit : Set :=
adamc@26 31 | tt.
adamc@26 32
adamc@26 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: *)
adamc@26 34
adamc@26 35 Check unit.
adamc@26 36 (** [[
adamc@26 37
adamc@26 38 unit : Set
adamc@26 39 ]] *)
adamc@26 40 Check tt.
adamc@26 41 (** [[
adamc@26 42
adamc@26 43 tt : unit
adamc@26 44 ]] *)
adamc@26 45
adamc@26 46 (** We can prove that [unit] is a genuine singleton type. *)
adamc@26 47
adamc@26 48 Theorem unit_singleton : forall x : unit, x = tt.
adamc@26 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]. *)
adamc@26 50 induction x.
adamc@26 51 (** The goal changes to: [[
adamc@26 52
adamc@26 53 tt = tt
adamc@26 54 ]] *)
adamc@26 55 (** ...which we can discharge trivially. *)
adamc@26 56 reflexivity.
adamc@26 57 Qed.
adamc@26 58
adamc@26 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: [[
adamc@26 60
adamc@26 61 destruct x.
adamc@26 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.
adamc@26 63
adamc@26 64 What exactly %\textit{%#<i>#is#</i>#%}% the induction principle for [unit]? We can ask Coq: *)
adamc@26 65
adamc@26 66 Check unit_ind.
adamc@26 67 (** [[
adamc@26 68
adamc@26 69 unit_ind : forall P : unit -> Prop, P tt -> forall u : unit, P u
adamc@26 70 ]]
adamc@26 71
adamc@26 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.
adamc@26 73
adamc@26 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.
adamc@26 75
adamc@26 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)].
adamc@26 77
adamc@26 78 %\medskip%
adamc@26 79
adamc@26 80 We can define an inductive type even simpler than [unit]: *)
adamc@26 81
adamc@26 82 Inductive Empty_set : Set := .
adamc@26 83
adamc@26 84 (** [Empty_set] has no elements. We can prove fun theorems about it: *)
adamc@26 85
adamc@26 86 Theorem the_sky_is_falling : forall x : Empty_set, 2 + 2 = 5.
adamc@26 87 destruct 1.
adamc@26 88 Qed.
adamc@26 89
adamc@26 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.)
adamc@26 91
adamc@26 92 We can see the induction principle that made this proof so easy: *)
adamc@26 93
adamc@26 94 Check Empty_set_ind.
adamc@26 95 (** [[
adamc@26 96
adamc@26 97 Empty_set_ind : forall (P : Empty_set -> Prop) (e : Empty_set), P e
adamc@26 98 ]]
adamc@26 99
adamc@26 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)].
adamc@26 101
adamc@26 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]: *)
adamc@26 103
adamc@26 104 Definition e2u (e : Empty_set) : unit := match e with end.
adamc@26 105
adamc@26 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.
adamc@26 107
adamc@26 108 %\medskip%
adamc@26 109
adamc@26 110 Moving up the ladder of complexity, we can define the booleans: *)
adamc@26 111
adamc@26 112 Inductive bool : Set :=
adamc@26 113 | true
adamc@26 114 | false.
adamc@26 115
adamc@26 116 (** We can use less vacuous pattern matching to define boolean negation. *)
adamc@26 117
adamc@26 118 Definition not (b : bool) : bool :=
adamc@26 119 match b with
adamc@26 120 | true => false
adamc@26 121 | false => true
adamc@26 122 end.
adamc@26 123
adamc@27 124 (** An alternative definition desugars to the above: *)
adamc@27 125
adamc@27 126 Definition not' (b : bool) : bool :=
adamc@27 127 if b then false else true.
adamc@27 128
adamc@26 129 (** We might want to prove that [not] is its own inverse operation. *)
adamc@26 130
adamc@26 131 Theorem not_inverse : forall b : bool, not (not b) = b.
adamc@26 132 destruct b.
adamc@26 133
adamc@26 134 (** After we case analyze on [b], we are left with one subgoal for each constructor of [bool].
adamc@26 135
adamc@26 136 [[
adamc@26 137
adamc@26 138 2 subgoals
adamc@26 139
adamc@26 140 ============================
adamc@26 141 not (not true) = true
adamc@26 142 ]]
adamc@26 143
adamc@26 144 [[
adamc@26 145 subgoal 2 is:
adamc@26 146 not (not false) = false
adamc@26 147 ]]
adamc@26 148
adamc@26 149 The first subgoal follows by Coq's rules of computation, so we can dispatch it easily: *)
adamc@26 150
adamc@26 151 reflexivity.
adamc@26 152
adamc@26 153 (** Likewise for the second subgoal, so we can restart the proof and give a very compact justification. *)
adamc@26 154
adamc@26 155 Restart.
adamc@26 156 destruct b; reflexivity.
adamc@26 157 Qed.
adamc@27 158
adamc@27 159 (** Another theorem about booleans illustrates another useful tactic. *)
adamc@27 160
adamc@27 161 Theorem not_ineq : forall b : bool, not b <> b.
adamc@27 162 destruct b; discriminate.
adamc@27 163 Qed.
adamc@27 164
adamc@27 165 (** [discriminate] is used to prove that two values of an inductive type are not equal, whenever the values are formed with different constructors. In this case, the different constructors are [true] and [false].
adamc@27 166
adamc@27 167 At this point, it is probably not hard to guess what the underlying induction principle for [bool] is. *)
adamc@27 168
adamc@27 169 Check bool_ind.
adamc@27 170 (** [[
adamc@27 171
adamc@27 172 bool_ind : forall P : bool -> Prop, P true -> P false -> forall b : bool, P b
adamc@27 173 ]] *)