annotate src/InductiveTypes.v @ 488:31258618ef73

Incorporate feedback from Nathan Collins
author Adam Chlipala <adam@chlipala.net>
date Tue, 08 Jan 2013 14:38:56 -0500
parents 1fd4109f7b31
children 4a663981b699
rev   line source
adam@398 1 (* Copyright (c) 2008-2012, 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
adam@314 13 Require Import CpdtTactics.
adamc@26 14
adamc@26 15 Set Implicit Arguments.
adamc@26 16 (* end hide *)
adamc@26 17
adamc@26 18
adamc@74 19 (** %\part{Basic Programming and Proving}
adamc@74 20
adamc@74 21 \chapter{Introducing Inductive Types}% *)
adamc@26 22
adam@475 23 (** The logical foundation of Coq is the Calculus of Inductive Constructions, or CIC. In a sense, CIC is built from just two relatively straightforward features: function types and inductive types. From this modest foundation, we can prove essentially all of the theorems of math and carry out effectively all program verifications, with enough effort expended. This chapter introduces induction and recursion for functional programming in Coq. Most of our examples reproduce functionality from the Coq standard library, and I have tried to copy the standard library's choices of identifiers, where possible, so many of the definitions here are already available in the default Coq environment.
adam@315 24
adam@315 25 The last chapter took a deep dive into some of the more advanced Coq features, to highlight the unusual approach that I advocate in this book. However, from this point on, we will rewind and go back to basics, presenting the relevant features of Coq in a more bottom-up manner. A useful first step is a discussion of the differences and relationships between proofs and programs in Coq. *)
adam@315 26
adam@315 27
adam@315 28 (** * Proof Terms *)
adam@315 29
adam@465 30 (** Mainstream presentations of mathematics treat proofs as objects that exist outside of the universe of mathematical objects. However, for a variety of reasoning tasks, it is convenient to encode proofs, traditional mathematical objects, and programs within a single formal language. Validity checks on mathematical objects are useful in any setting, to catch typos and other uninteresting errors. The benefits of static typing for programs are widely recognized, and Coq brings those benefits to both mathematical objects and programs via a uniform mechanism. In fact, from this point on, we will not bother to distinguish between programs and mathematical objects. Many mathematical formalisms are most easily encoded in terms of programs.
adam@315 31
adam@400 32 Proofs are fundamentally different from programs, because any two proofs of a theorem are considered equivalent, from a formal standpoint if not from an engineering standpoint. However, we can use the same type-checking technology to check proofs as we use to validate our programs. This is the%\index{Curry-Howard correspondence}% _Curry-Howard correspondence_ %\cite{Curry,Howard}%, an approach for relating proofs and programs. We represent mathematical theorems as types, such that a theorem's proofs are exactly those programs that type-check at the corresponding type.
adam@315 33
adam@315 34 The last chapter's example already snuck in an instance of Curry-Howard. We used the token [->] to stand for both function types and logical implications. One reasonable conclusion upon seeing this might be that some fancy overloading of notations is at work. In fact, functions and implications are precisely identical according to Curry-Howard! That is, they are just two ways of describing the same computational phenomenon.
adam@315 35
adam@315 36 A short demonstration should explain how this can be. The identity function over the natural numbers is certainly not a controversial program. *)
adam@315 37
adam@315 38 Check (fun x : nat => x).
adam@315 39 (** [: nat -> nat] *)
adam@315 40
adam@439 41 (** %\smallskip{}%Consider this alternate program, which is almost identical to the last one. *)
adam@315 42
adam@315 43 Check (fun x : True => x).
adam@315 44 (** [: True -> True] *)
adam@315 45
adam@439 46 (** %\smallskip{}%The identity program is interpreted as a proof that %\index{Gallina terms!True}%[True], the always-true proposition, implies itself! What we see is that Curry-Howard interprets implications as functions, where an input is a proposition being assumed and an output is a proposition being deduced. This intuition is not too far from a common one for informal theorem proving, where we might already think of an implication proof as a process for transforming a hypothesis into a conclusion.
adam@315 47
adam@315 48 There are also more primitive proof forms available. For instance, the term %\index{Gallina terms!I}%[I] is the single proof of [True], applicable in any context. *)
adam@315 49
adam@315 50 Check I.
adam@315 51 (** [: True] *)
adam@315 52
adam@439 53 (** %\smallskip{}%With [I], we can prove another simple propositional theorem. *)
adam@315 54
adam@315 55 Check (fun _ : False => I).
adam@315 56 (** [: False -> True] *)
adam@315 57
adam@439 58 (** %\smallskip{}%No proofs of %\index{Gallina terms!False}%[False] exist in the top-level context, but the implication-as-function analogy gives us an easy way to, for example, show that [False] implies itself. *)
adam@315 59
adam@315 60 Check (fun x : False => x).
adam@315 61 (** [: False -> False] *)
adam@315 62
adam@439 63 (** %\smallskip{}%Every one of these example programs whose type looks like a logical formula is a%\index{proof term}% _proof term_. We use that name for any Gallina term of a logical type, and we will elaborate shortly on what makes a type logical.
adam@315 64
adam@469 65 In the rest of this chapter, we will introduce different ways of defining types. Every example type can be interpreted alternatively as a type of programs or proofs.
adam@317 66
adam@471 67 One of the first types we introduce will be [bool], with constructors [true] and [false]. Newcomers to Coq often wonder about the distinction between [True] and [true] and the distinction between [False] and [false]. One glib answer is that [True] and [False] are types, but [true] and [false] are not. A more useful answer is that Coq's metatheory guarantees that any term of type [bool] _evaluates_ to either [true] or [false]. This means that we have an _algorithm_ for answering any question phrased as an expression of type [bool]. Conversely, most propositions do not evaluate to [True] or [False]; the language of inductively defined propositions is much richer than that. We ought to be glad that we have no algorithm for deciding our formalized version of mathematical truth, since otherwise it would be clear that we could not formalize undecidable properties, like almost any interesting property of general-purpose programs. *)
adamc@26 68
adamc@26 69
adamc@26 70 (** * Enumerations *)
adamc@26 71
adam@419 72 (** Coq inductive types generalize the %\index{algebraic datatypes}%algebraic datatypes found in %\index{Haskell}%Haskell and %\index{ML}%ML. Confusingly enough, inductive types also generalize %\index{generalized algebraic datatypes}%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 73
adam@315 74 The singleton type [unit] is an inductive type:%\index{Gallina terms!unit}\index{Gallina terms!tt}% *)
adamc@26 75
adamc@26 76 Inductive unit : Set :=
adamc@26 77 | tt.
adamc@26 78
adam@471 79 (** This vernacular command defines a new inductive type [unit] whose only value is [tt]. We can verify the types of the two identifiers we introduce: *)
adamc@26 80
adamc@26 81 Check unit.
adamc@208 82 (** [unit : Set] *)
adamc@26 83
adamc@26 84 Check tt.
adamc@208 85 (** [tt : unit] *)
adamc@26 86
adam@439 87 (** %\smallskip{}%We can prove that [unit] is a genuine singleton type. *)
adamc@26 88
adamc@26 89 Theorem unit_singleton : forall x : unit, x = tt.
adamc@208 90
adam@315 91 (** 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].%\index{tactics!induction}% *)
adamc@208 92
adamc@41 93 (* begin thide *)
adamc@26 94 induction x.
adamc@26 95
adamc@208 96 (** The goal changes to:
adamc@208 97 [[
adamc@26 98 tt = tt
adam@302 99 ]]
adam@302 100 *)
adamc@208 101
adam@448 102 (** %\noindent{}%...which we can discharge trivially. *)
adamc@208 103
adamc@26 104 reflexivity.
adamc@26 105 Qed.
adamc@41 106 (* end thide *)
adamc@26 107
adam@315 108 (** 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:%\index{tactics!destruct}% [[
adamc@26 109 destruct x.
adamc@205 110 ]]
adamc@205 111
adam@420 112 %\noindent%...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 113
adam@398 114 What exactly _is_ the %\index{induction principles}%induction principle for [unit]? We can ask Coq: *)
adamc@26 115
adamc@26 116 Check unit_ind.
adamc@208 117 (** [unit_ind : forall P : unit -> Prop, P tt -> forall u : unit, P u] *)
adamc@26 118
adam@439 119 (** %\smallskip{}%Every [Inductive] command defining a type [T] also defines an induction principle named [T_ind]. Recall from the last section that 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 %\index{Gallina terms!Prop}%[Prop], which appears in our induction principle; and the type %\index{Gallina terms!Set}%[Set], which we have seen a few times already.
adamc@26 120
adam@315 121 The convention goes like this: [Set] is the type of normal types used in programming, 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 122
adam@400 123 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)].
adam@315 124
adam@315 125 The definition of [unit] places the type in [Set]. By replacing [Set] with [Prop], [unit] with [True], and [tt] with [I], we arrive at precisely the definition of [True] that the Coq standard library employs! The program type [unit] is the Curry-Howard equivalent of the proposition [True]. We might make the tongue-in-cheek claim that, while philosophers have expended much ink on the nature of truth, we have now determined that truth is the [unit] type of functional programming.
adamc@26 126
adamc@26 127 %\medskip%
adamc@26 128
adam@315 129 We can define an inductive type even simpler than [unit]:%\index{Gallina terms!Empty\_set}% *)
adamc@26 130
adamc@26 131 Inductive Empty_set : Set := .
adamc@26 132
adamc@26 133 (** [Empty_set] has no elements. We can prove fun theorems about it: *)
adamc@26 134
adamc@26 135 Theorem the_sky_is_falling : forall x : Empty_set, 2 + 2 = 5.
adamc@41 136 (* begin thide *)
adamc@26 137 destruct 1.
adamc@26 138 Qed.
adamc@41 139 (* end thide *)
adamc@26 140
adam@317 141 (** 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 implication. At least within Coq's logical foundation of %\index{constructive logic}%constructive logic, which we elaborate on more in the next chapter, an implication is just a quantification over a proof, where the quantified variable is never used. It generally makes more sense to refer to implication hypotheses by number than by name, and Coq treats our quantifier over an unused variable as an implication in determining the proper behavior.)
adamc@26 142
adamc@26 143 We can see the induction principle that made this proof so easy: *)
adamc@26 144
adamc@26 145 Check Empty_set_ind.
adam@400 146 (** [Empty_set_ind : forall (P : Empty_set -> Prop) (e : Empty_set), P e] *)
adamc@26 147
adam@439 148 (** %\smallskip{}%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 149
adamc@26 150 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 151
adamc@26 152 Definition e2u (e : Empty_set) : unit := match e with end.
adamc@26 153
adam@315 154 (** 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. This idiom may look familiar; we employed it with proofs of [False] in the last section. In fact, [Empty_set] is the Curry-Howard equivalent of [False]. As for why [Empty_set] starts with a capital letter and not a lowercase letter like [unit] does, we must refer the reader to the authors of the Coq standard library, to which we try to be faithful.
adamc@26 155
adamc@26 156 %\medskip%
adamc@26 157
adam@448 158 Moving up the ladder of complexity, we can define the Booleans:%\index{Gallina terms!bool}\index{Gallina terms!true}\index{Gallina terms!false}% *)
adamc@26 159
adamc@26 160 Inductive bool : Set :=
adamc@26 161 | true
adamc@26 162 | false.
adamc@26 163
adam@448 164 (** We can use less vacuous pattern matching to define Boolean negation.%\index{Gallina terms!negb}% *)
adamc@26 165
adam@279 166 Definition negb (b : bool) : bool :=
adamc@26 167 match b with
adamc@26 168 | true => false
adamc@26 169 | false => true
adamc@26 170 end.
adamc@26 171
adam@317 172 (** An alternative definition desugars to the above, thanks to an %\index{Gallina terms!if}%[if] notation overloaded to work with any inductive type that has exactly two constructors: *)
adamc@27 173
adam@279 174 Definition negb' (b : bool) : bool :=
adamc@27 175 if b then false else true.
adamc@27 176
adam@279 177 (** We might want to prove that [negb] is its own inverse operation. *)
adamc@26 178
adam@279 179 Theorem negb_inverse : forall b : bool, negb (negb b) = b.
adamc@41 180 (* begin thide *)
adamc@26 181 destruct b.
adamc@26 182
adamc@208 183 (** After we case-analyze on [b], we are left with one subgoal for each constructor of [bool].
adam@439 184 [[
adam@439 185 2 subgoals
adamc@26 186
adamc@26 187 ============================
adam@279 188 negb (negb true) = true
adam@439 189
adam@439 190 subgoal 2 is
adam@439 191
adam@279 192 negb (negb false) = false
adamc@26 193 ]]
adamc@26 194
adamc@26 195 The first subgoal follows by Coq's rules of computation, so we can dispatch it easily: *)
adamc@26 196
adamc@26 197 reflexivity.
adamc@26 198
adam@315 199 (** Likewise for the second subgoal, so we can restart the proof and give a very compact justification.%\index{Vernacular commands!Restart}% *)
adamc@26 200
adamc@26 201 Restart.
adam@315 202
adamc@26 203 destruct b; reflexivity.
adamc@26 204 Qed.
adamc@41 205 (* end thide *)
adamc@27 206
adam@448 207 (** Another theorem about Booleans illustrates another useful tactic.%\index{tactics!discriminate}% *)
adamc@27 208
adam@279 209 Theorem negb_ineq : forall b : bool, negb b <> b.
adamc@41 210 (* begin thide *)
adamc@27 211 destruct b; discriminate.
adamc@27 212 Qed.
adamc@41 213 (* end thide *)
adamc@27 214
adam@448 215 (** The [discriminate] tactic 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 216
adamc@27 217 At this point, it is probably not hard to guess what the underlying induction principle for [bool] is. *)
adamc@27 218
adamc@27 219 Check bool_ind.
adamc@208 220 (** [bool_ind : forall P : bool -> Prop, P true -> P false -> forall b : bool, P b] *)
adamc@28 221
adam@439 222 (** %\smallskip{}%That is, to prove that a property describes all [bool]s, prove that it describes both [true] and [false].
adam@315 223
adam@392 224 There is no interesting Curry-Howard analogue of [bool]. Of course, we can define such a type by replacing [Set] by [Prop] above, but the proposition we arrive at is not very useful. It is logically equivalent to [True], but it provides two indistinguishable primitive proofs, [true] and [false]. In the rest of the chapter, we will skip commenting on Curry-Howard versions of inductive definitions where such versions are not interesting. *)
adam@315 225
adamc@28 226
adamc@28 227 (** * Simple Recursive Types *)
adamc@28 228
adam@315 229 (** The natural numbers are the simplest common example of an inductive type that actually deserves the name.%\index{Gallina terms!nat}\index{Gallina terms!O}\index{Gallina terms!S}% *)
adamc@28 230
adamc@28 231 Inductive nat : Set :=
adamc@28 232 | O : nat
adamc@28 233 | S : nat -> nat.
adamc@28 234
adam@439 235 (** The constructor [O] is zero, and [S] is the successor function, so that [0] is syntactic sugar for [O], [1] for [S O], [2] for [S (S O)], and so on.
adamc@28 236
adam@316 237 Pattern matching works as we demonstrated in the last chapter:%\index{Gallina terms!pred}% *)
adamc@28 238
adamc@28 239 Definition isZero (n : nat) : bool :=
adamc@28 240 match n with
adamc@28 241 | O => true
adamc@28 242 | S _ => false
adamc@28 243 end.
adamc@28 244
adamc@28 245 Definition pred (n : nat) : nat :=
adamc@28 246 match n with
adamc@28 247 | O => O
adamc@28 248 | S n' => n'
adamc@28 249 end.
adamc@28 250
adam@469 251 (** We can prove theorems by case analysis with [destruct] as for simpler inductive types, but we can also now get into genuine inductive theorems. First, we will need a recursive function, to make things interesting.%\index{Gallina terms!plus}% *)
adamc@28 252
adamc@208 253 Fixpoint plus (n m : nat) : nat :=
adamc@28 254 match n with
adamc@28 255 | O => m
adamc@28 256 | S n' => S (plus n' m)
adamc@28 257 end.
adamc@28 258
adamc@208 259 (** Recall that [Fixpoint] is Coq's mechanism for recursive function definitions. Some theorems about [plus] can be proved without induction. *)
adamc@28 260
adamc@28 261 Theorem O_plus_n : forall n : nat, plus O n = n.
adamc@41 262 (* begin thide *)
adamc@28 263 intro; reflexivity.
adamc@28 264 Qed.
adamc@41 265 (* end thide *)
adamc@28 266
adamc@208 267 (** Coq's computation rules automatically simplify the application of [plus], because unfolding the definition of [plus] gives us a [match] expression where the branch to be taken is obvious from syntax alone. If we just reverse the order of the arguments, though, this no longer works, and we need induction. *)
adamc@28 268
adamc@28 269 Theorem n_plus_O : forall n : nat, plus n O = n.
adamc@41 270 (* begin thide *)
adamc@28 271 induction n.
adamc@28 272
adam@398 273 (** Our first subgoal is [plus O O = O], which _is_ trivial by computation. *)
adamc@28 274
adamc@28 275 reflexivity.
adamc@28 276
adam@469 277 (** Our second subgoal requires more work and also demonstrates our first inductive hypothesis.
adamc@28 278
adamc@28 279 [[
adamc@28 280 n : nat
adamc@28 281 IHn : plus n O = n
adamc@28 282 ============================
adamc@28 283 plus (S n) O = S n
adamc@208 284
adamc@28 285 ]]
adamc@28 286
adam@315 287 We can start out by using computation to simplify the goal as far as we can.%\index{tactics!simpl}% *)
adamc@28 288
adamc@28 289 simpl.
adamc@28 290
adam@400 291 (** Now the conclusion is [S (plus n O) = S n]. Using our inductive hypothesis: *)
adamc@28 292
adamc@28 293 rewrite IHn.
adamc@28 294
adam@448 295 (** %\noindent{}%...we get a trivial conclusion [S n = S n]. *)
adamc@28 296
adamc@28 297 reflexivity.
adamc@28 298
adam@315 299 (** Not much really went on in this proof, so the [crush] tactic from the [CpdtTactics] module can prove this theorem automatically. *)
adamc@28 300
adamc@28 301 Restart.
adam@315 302
adamc@28 303 induction n; crush.
adamc@28 304 Qed.
adamc@41 305 (* end thide *)
adamc@28 306
adamc@28 307 (** We can check out the induction principle at work here: *)
adamc@28 308
adamc@28 309 Check nat_ind.
adamc@208 310 (** %\vspace{-.15in}% [[
adamc@208 311 nat_ind : forall P : nat -> Prop,
adamc@208 312 P O -> (forall n : nat, P n -> P (S n)) -> forall n : nat, P n
adamc@208 313 ]]
adamc@28 314
adam@442 315 Each of the two cases of our last proof came from the type of one of the arguments to [nat_ind]. We chose [P] to be [(fun n : nat => plus n O = n)]. The first proof case corresponded to [P O] and the second case to [(forall n : nat, P n -> P (S n))]. The free variable [n] and inductive hypothesis [IHn] came from the argument types given here.
adamc@28 316
adam@315 317 Since [nat] has a constructor that takes an argument, we may sometimes need to know that that constructor is injective.%\index{tactics!injection}\index{tactics!trivial}% *)
adamc@28 318
adamc@28 319 Theorem S_inj : forall n m : nat, S n = S m -> n = m.
adamc@41 320 (* begin thide *)
adamc@28 321 injection 1; trivial.
adamc@28 322 Qed.
adamc@41 323 (* end thide *)
adamc@28 324
adam@471 325 (** The [injection] tactic refers to a premise by number, adding new equalities between the corresponding arguments of equated terms that are formed with the same constructor. We end up needing to prove [n = m -> n = m], so it is unsurprising that a tactic named [trivial] is able to finish the proof. This tactic attempts a variety of single proof steps, drawn from a user-specified database that we will later see how to extend.
adamc@28 326
adam@417 327 There is also a very useful tactic called %\index{tactics!congruence}%[congruence] that can prove this theorem immediately. The [congruence] tactic generalizes [discriminate] and [injection], and it also adds reasoning about the general properties of equality, such as that a function returns equal results on equal arguments. That is, [congruence] is a%\index{theory of equality and uninterpreted functions}% _complete decision procedure for the theory of equality and uninterpreted functions_, plus some smarts about inductive types.
adamc@29 328
adamc@29 329 %\medskip%
adamc@29 330
adamc@29 331 We can define a type of lists of natural numbers. *)
adamc@29 332
adamc@29 333 Inductive nat_list : Set :=
adamc@29 334 | NNil : nat_list
adamc@29 335 | NCons : nat -> nat_list -> nat_list.
adamc@29 336
adam@470 337 (** Recursive definitions over [nat_list] are straightforward extensions of what we have seen before. *)
adamc@29 338
adamc@29 339 Fixpoint nlength (ls : nat_list) : nat :=
adamc@29 340 match ls with
adamc@29 341 | NNil => O
adamc@29 342 | NCons _ ls' => S (nlength ls')
adamc@29 343 end.
adamc@29 344
adamc@208 345 Fixpoint napp (ls1 ls2 : nat_list) : nat_list :=
adamc@29 346 match ls1 with
adamc@29 347 | NNil => ls2
adamc@29 348 | NCons n ls1' => NCons n (napp ls1' ls2)
adamc@29 349 end.
adamc@29 350
adamc@29 351 (** Inductive theorem proving can again be automated quite effectively. *)
adamc@29 352
adamc@29 353 Theorem nlength_napp : forall ls1 ls2 : nat_list, nlength (napp ls1 ls2)
adamc@29 354 = plus (nlength ls1) (nlength ls2).
adamc@41 355 (* begin thide *)
adamc@29 356 induction ls1; crush.
adamc@29 357 Qed.
adamc@41 358 (* end thide *)
adamc@29 359
adamc@29 360 Check nat_list_ind.
adamc@208 361 (** %\vspace{-.15in}% [[
adamc@208 362 nat_list_ind
adamc@29 363 : forall P : nat_list -> Prop,
adamc@29 364 P NNil ->
adamc@29 365 (forall (n : nat) (n0 : nat_list), P n0 -> P (NCons n n0)) ->
adamc@29 366 forall n : nat_list, P n
adamc@29 367 ]]
adamc@29 368
adamc@29 369 %\medskip%
adamc@29 370
adam@471 371 In general, we can implement any "tree" type as an inductive type. For example, here are binary trees of naturals. *)
adamc@29 372
adamc@29 373 Inductive nat_btree : Set :=
adamc@29 374 | NLeaf : nat_btree
adamc@29 375 | NNode : nat_btree -> nat -> nat_btree -> nat_btree.
adamc@29 376
adam@471 377 (** Here are two functions whose intuitive explanations are not so important. The first one computes the size of a tree, and the second performs some sort of splicing of one tree into the leftmost available leaf node of another. *)
adam@471 378
adamc@29 379 Fixpoint nsize (tr : nat_btree) : nat :=
adamc@29 380 match tr with
adamc@35 381 | NLeaf => S O
adamc@29 382 | NNode tr1 _ tr2 => plus (nsize tr1) (nsize tr2)
adamc@29 383 end.
adamc@29 384
adamc@208 385 Fixpoint nsplice (tr1 tr2 : nat_btree) : nat_btree :=
adamc@29 386 match tr1 with
adamc@35 387 | NLeaf => NNode tr2 O NLeaf
adamc@29 388 | NNode tr1' n tr2' => NNode (nsplice tr1' tr2) n tr2'
adamc@29 389 end.
adamc@29 390
adamc@29 391 Theorem plus_assoc : forall n1 n2 n3 : nat, plus (plus n1 n2) n3 = plus n1 (plus n2 n3).
adamc@41 392 (* begin thide *)
adamc@29 393 induction n1; crush.
adamc@29 394 Qed.
adamc@41 395 (* end thide *)
adamc@29 396
adamc@29 397 Theorem nsize_nsplice : forall tr1 tr2 : nat_btree, nsize (nsplice tr1 tr2)
adamc@29 398 = plus (nsize tr2) (nsize tr1).
adamc@41 399 (* begin thide *)
adam@375 400 Hint Rewrite n_plus_O plus_assoc.
adamc@29 401
adamc@29 402 induction tr1; crush.
adamc@29 403 Qed.
adamc@41 404 (* end thide *)
adamc@29 405
adam@471 406 (** It is convenient that these proofs go through so easily, but it is still useful to look into the details of what happened, by checking the statement of the tree induction principle. *)
adam@315 407
adamc@29 408 Check nat_btree_ind.
adamc@208 409 (** %\vspace{-.15in}% [[
adamc@208 410 nat_btree_ind
adamc@29 411 : forall P : nat_btree -> Prop,
adamc@29 412 P NLeaf ->
adamc@29 413 (forall n : nat_btree,
adamc@29 414 P n -> forall (n0 : nat) (n1 : nat_btree), P n1 -> P (NNode n n0 n1)) ->
adamc@29 415 forall n : nat_btree, P n
adam@302 416 ]]
adam@315 417
adam@315 418 We have the usual two cases, one for each constructor of [nat_btree]. *)
adamc@30 419
adamc@30 420
adamc@30 421 (** * Parameterized Types *)
adamc@30 422
adam@316 423 (** We can also define %\index{polymorphism}%polymorphic inductive types, as with algebraic datatypes in Haskell and ML.%\index{Gallina terms!list}\index{Gallina terms!Nil}\index{Gallina terms!Cons}\index{Gallina terms!length}\index{Gallina terms!app}% *)
adamc@30 424
adamc@30 425 Inductive list (T : Set) : Set :=
adamc@30 426 | Nil : list T
adamc@30 427 | Cons : T -> list T -> list T.
adamc@30 428
adamc@30 429 Fixpoint length T (ls : list T) : nat :=
adamc@30 430 match ls with
adamc@30 431 | Nil => O
adamc@30 432 | Cons _ ls' => S (length ls')
adamc@30 433 end.
adamc@30 434
adamc@208 435 Fixpoint app T (ls1 ls2 : list T) : list T :=
adamc@30 436 match ls1 with
adamc@30 437 | Nil => ls2
adamc@30 438 | Cons x ls1' => Cons x (app ls1' ls2)
adamc@30 439 end.
adamc@30 440
adamc@30 441 Theorem length_app : forall T (ls1 ls2 : list T), length (app ls1 ls2)
adamc@30 442 = plus (length ls1) (length ls2).
adamc@41 443 (* begin thide *)
adamc@30 444 induction ls1; crush.
adamc@30 445 Qed.
adamc@41 446 (* end thide *)
adamc@30 447
adam@420 448 (** There is a useful shorthand for writing many definitions that share the same parameter, based on Coq's%\index{sections}\index{Vernacular commands!Section}\index{Vernacular commands!Variable}% _section_ mechanism. The following block of code is equivalent to the above: *)
adamc@30 449
adamc@30 450 (* begin hide *)
adamc@30 451 Reset list.
adamc@30 452 (* end hide *)
adamc@30 453
adamc@30 454 Section list.
adamc@30 455 Variable T : Set.
adamc@30 456
adamc@30 457 Inductive list : Set :=
adamc@30 458 | Nil : list
adamc@30 459 | Cons : T -> list -> list.
adamc@30 460
adamc@30 461 Fixpoint length (ls : list) : nat :=
adamc@30 462 match ls with
adamc@30 463 | Nil => O
adamc@30 464 | Cons _ ls' => S (length ls')
adamc@30 465 end.
adamc@30 466
adamc@208 467 Fixpoint app (ls1 ls2 : list) : list :=
adamc@30 468 match ls1 with
adamc@30 469 | Nil => ls2
adamc@30 470 | Cons x ls1' => Cons x (app ls1' ls2)
adamc@30 471 end.
adamc@30 472
adamc@30 473 Theorem length_app : forall ls1 ls2 : list, length (app ls1 ls2)
adamc@30 474 = plus (length ls1) (length ls2).
adamc@41 475 (* begin thide *)
adamc@30 476 induction ls1; crush.
adamc@30 477 Qed.
adamc@41 478 (* end thide *)
adamc@30 479 End list.
adamc@30 480
adamc@35 481 Implicit Arguments Nil [T].
adamc@35 482
adam@469 483 (** After we end the section, the [Variable]s we used are added as extra function parameters for each defined identifier, as needed. With an [Implicit Arguments]%~\index{Vernacular commands!Implicit Arguments}% command, we ask that [T] be inferred when we use [Nil]; Coq's heuristics already decided to apply a similar policy to [Cons], because of the [Set Implicit Arguments]%~\index{Vernacular commands!Set Implicit Arguments}% command elided at the beginning of this chapter. We verify that our definitions have been saved properly using the [Print] command, a cousin of [Check] which shows the definition of a symbol, rather than just its type. *)
adamc@30 484
adamc@202 485 Print list.
adamc@208 486 (** %\vspace{-.15in}% [[
adamc@208 487 Inductive list (T : Set) : Set :=
adam@316 488 Nil : list T | Cons : T -> list T -> list T
adamc@202 489 ]]
adamc@30 490
adam@442 491 The final definition is the same as what we wrote manually before. The other elements of the section are altered similarly, turning out exactly as they were before, though we managed to write their definitions more succinctly. *)
adamc@30 492
adamc@30 493 Check length.
adamc@208 494 (** %\vspace{-.15in}% [[
adamc@208 495 length
adamc@30 496 : forall T : Set, list T -> nat
adamc@30 497 ]]
adamc@30 498
adam@442 499 The parameter [T] is treated as a new argument to the induction principle, too. *)
adamc@30 500
adamc@30 501 Check list_ind.
adamc@208 502 (** %\vspace{-.15in}% [[
adamc@208 503 list_ind
adamc@30 504 : forall (T : Set) (P : list T -> Prop),
adamc@30 505 P (Nil T) ->
adamc@30 506 (forall (t : T) (l : list T), P l -> P (Cons t l)) ->
adamc@30 507 forall l : list T, P l
adamc@30 508 ]]
adamc@30 509
adam@488 510 Thus, despite a very real sense in which the type [T] is an argument to the constructor [Cons], the inductive case in the type of [list_ind] (i.e., the third line of the type) includes no quantifier for [T], even though all of the other arguments are quantified explicitly. Parameters in other inductive definitions are treated similarly in stating induction principles. *)
adamc@31 511
adamc@31 512
adamc@31 513 (** * Mutually Inductive Types *)
adamc@31 514
adamc@31 515 (** We can define inductive types that refer to each other: *)
adamc@31 516
adamc@31 517 Inductive even_list : Set :=
adamc@31 518 | ENil : even_list
adamc@31 519 | ECons : nat -> odd_list -> even_list
adamc@31 520
adamc@31 521 with odd_list : Set :=
adamc@31 522 | OCons : nat -> even_list -> odd_list.
adamc@31 523
adamc@31 524 Fixpoint elength (el : even_list) : nat :=
adamc@31 525 match el with
adamc@31 526 | ENil => O
adamc@31 527 | ECons _ ol => S (olength ol)
adamc@31 528 end
adamc@31 529
adamc@31 530 with olength (ol : odd_list) : nat :=
adamc@31 531 match ol with
adamc@31 532 | OCons _ el => S (elength el)
adamc@31 533 end.
adamc@31 534
adamc@208 535 Fixpoint eapp (el1 el2 : even_list) : even_list :=
adamc@31 536 match el1 with
adamc@31 537 | ENil => el2
adamc@31 538 | ECons n ol => ECons n (oapp ol el2)
adamc@31 539 end
adamc@31 540
adamc@208 541 with oapp (ol : odd_list) (el : even_list) : odd_list :=
adamc@31 542 match ol with
adamc@31 543 | OCons n el' => OCons n (eapp el' el)
adamc@31 544 end.
adamc@31 545
adamc@31 546 (** Everything is going roughly the same as in past examples, until we try to prove a theorem similar to those that came before. *)
adamc@31 547
adamc@31 548 Theorem elength_eapp : forall el1 el2 : even_list,
adamc@31 549 elength (eapp el1 el2) = plus (elength el1) (elength el2).
adamc@41 550 (* begin thide *)
adamc@31 551 induction el1; crush.
adamc@31 552
adamc@31 553 (** One goal remains: [[
adamc@31 554
adamc@31 555 n : nat
adamc@31 556 o : odd_list
adamc@31 557 el2 : even_list
adamc@31 558 ============================
adamc@31 559 S (olength (oapp o el2)) = S (plus (olength o) (elength el2))
adamc@31 560 ]]
adamc@31 561
adamc@31 562 We have no induction hypothesis, so we cannot prove this goal without starting another induction, which would reach a similar point, sending us into a futile infinite chain of inductions. The problem is that Coq's generation of [T_ind] principles is incomplete. We only get non-mutual induction principles generated by default. *)
adamc@31 563
adamc@31 564 Abort.
adamc@31 565 Check even_list_ind.
adamc@208 566 (** %\vspace{-.15in}% [[
adamc@208 567 even_list_ind
adamc@31 568 : forall P : even_list -> Prop,
adamc@31 569 P ENil ->
adamc@31 570 (forall (n : nat) (o : odd_list), P (ECons n o)) ->
adamc@31 571 forall e : even_list, P e
adamc@31 572 ]]
adamc@31 573
adam@442 574 We see that no inductive hypotheses are included anywhere in the type. To get them, we must ask for mutual principles as we need them, using the %\index{Vernacular commands!Scheme}%[Scheme] command. *)
adamc@31 575
adamc@31 576 Scheme even_list_mut := Induction for even_list Sort Prop
adamc@31 577 with odd_list_mut := Induction for odd_list Sort Prop.
adamc@31 578
adam@316 579 (** This invocation of [Scheme] asks for the creation of induction principles [even_list_mut] for the type [even_list] and [odd_list_mut] for the type [odd_list]. The [Induction] keyword says we want standard induction schemes, since [Scheme] supports more exotic choices. Finally, [Sort Prop] establishes that we really want induction schemes, not recursion schemes, which are the same according to Curry-Howard, save for the [Prop]/[Set] distinction. *)
adam@316 580
adamc@31 581 Check even_list_mut.
adamc@208 582 (** %\vspace{-.15in}% [[
adamc@208 583 even_list_mut
adamc@31 584 : forall (P : even_list -> Prop) (P0 : odd_list -> Prop),
adamc@31 585 P ENil ->
adamc@31 586 (forall (n : nat) (o : odd_list), P0 o -> P (ECons n o)) ->
adamc@31 587 (forall (n : nat) (e : even_list), P e -> P0 (OCons n e)) ->
adamc@31 588 forall e : even_list, P e
adamc@31 589 ]]
adamc@31 590
adam@471 591 This is the principle we wanted in the first place.
adam@471 592
adam@471 593 The [Scheme] command is for asking Coq to generate particular induction schemes that are mutual among a set of inductive types (possibly only one such type, in which case we get a normal induction principle). In a sense, it generalizes the induction scheme generation that goes on automatically for each inductive definition. Future Coq versions might make that automatic generation smarter, so that [Scheme] is needed in fewer places. In a few sections, we will see how induction principles are derived theorems in Coq, so that there is not actually any need to build in _any_ automatic scheme generation.
adam@471 594
adam@471 595 There is one more wrinkle left in using the [even_list_mut] induction principle: the [induction] tactic will not apply it for us automatically. It will be helpful to look at how to prove one of our past examples without using [induction], so that we can then generalize the technique to mutual inductive types.%\index{tactics!apply}% *)
adamc@31 596
adamc@31 597 Theorem n_plus_O' : forall n : nat, plus n O = n.
adam@471 598 apply nat_ind.
adam@471 599 (** Here we use [apply], which is one of the most essential basic tactics. When we are trying to prove fact [P], and when [thm] is a theorem whose conclusion can be made to match [P] by proper choice of quantified variable values, the invocation [apply thm] will replace the current goal with one new goal for each premise of [thm].
adam@471 600
adam@471 601 This use of [apply] may seem a bit _too_ magical. To better see what is going on, we use a variant where we partially apply the theorem [nat_ind] to give an explicit value for the predicate that gives our induction hypothesis. *)
adam@471 602
adam@471 603 Undo.
adamc@31 604 apply (nat_ind (fun n => plus n O = n)); crush.
adamc@31 605 Qed.
adamc@31 606
adam@471 607 (** From this example, we can see that [induction] is not magic. It only does some bookkeeping for us to make it easy to apply a theorem, which we can do directly with the [apply] tactic.
adamc@31 608
adamc@31 609 This technique generalizes to our mutual example: *)
adamc@31 610
adamc@31 611 Theorem elength_eapp : forall el1 el2 : even_list,
adamc@31 612 elength (eapp el1 el2) = plus (elength el1) (elength el2).
adamc@41 613
adamc@31 614 apply (even_list_mut
adamc@31 615 (fun el1 : even_list => forall el2 : even_list,
adamc@31 616 elength (eapp el1 el2) = plus (elength el1) (elength el2))
adamc@31 617 (fun ol : odd_list => forall el : even_list,
adamc@31 618 olength (oapp ol el) = plus (olength ol) (elength el))); crush.
adamc@31 619 Qed.
adamc@41 620 (* end thide *)
adamc@31 621
adam@475 622 (** We simply need to specify two predicates, one for each of the mutually inductive types. In general, it is not a good idea to assume that a proof assistant can infer extra predicates, so this way of applying mutual induction is about as straightforward as we may hope for. *)
adamc@33 623
adamc@33 624
adamc@33 625 (** * Reflexive Types *)
adamc@33 626
adam@469 627 (** A kind of inductive type called a _reflexive type_ includes at least one constructor that takes as an argument _a function returning the same type we are defining_. One very useful class of examples is in modeling variable binders. Our example will be an encoding of the syntax of first-order logic. Since the idea of syntactic encodings of logic may require a bit of acclimation, let us first consider a simpler formula type for a subset of propositional logic. We are not yet using a reflexive type, but later we will extend the example reflexively. *)
adam@316 628
adam@316 629 Inductive pformula : Set :=
adam@316 630 | Truth : pformula
adam@316 631 | Falsehood : pformula
adam@316 632 | Conjunction : pformula -> pformula -> pformula.
adam@316 633
adam@448 634 (* begin hide *)
adam@448 635 (* begin thide *)
adam@448 636 Definition prod' := prod.
adam@448 637 (* end thide *)
adam@448 638 (* end hide *)
adam@448 639
adam@475 640 (** A key distinction here is between, for instance, the _syntax_ [Truth] and its _semantics_ [True]. We can make the semantics explicit with a recursive function. This function uses the infix operator %\index{Gallina operators!/\textbackslash}%[/\], which desugars to instances of the type family %\index{Gallina terms!and}%[and] from the standard library. The family [and] implements conjunction, the [Prop] Curry-Howard analogue of the usual pair type from functional programming (which is the type family %\index{Gallina terms!prod}%[prod] in Coq's standard library). *)
adam@316 641
adam@316 642 Fixpoint pformulaDenote (f : pformula) : Prop :=
adam@316 643 match f with
adam@316 644 | Truth => True
adam@316 645 | Falsehood => False
adam@316 646 | Conjunction f1 f2 => pformulaDenote f1 /\ pformulaDenote f2
adam@316 647 end.
adam@316 648
adam@392 649 (** This is just a warm-up that does not use reflexive types, the new feature we mean to introduce. When we set our sights on first-order logic instead, it becomes very handy to give constructors recursive arguments that are functions. *)
adamc@33 650
adamc@33 651 Inductive formula : Set :=
adamc@33 652 | Eq : nat -> nat -> formula
adamc@33 653 | And : formula -> formula -> formula
adamc@33 654 | Forall : (nat -> formula) -> formula.
adamc@33 655
adam@470 656 (** Our kinds of formulas are equalities between naturals, conjunction, and universal quantification over natural numbers. We avoid needing to include a notion of "variables" in our type, by using Coq functions to encode the syntax of quantification. For instance, here is the encoding of [forall x : nat, x = x]:%\index{Vernacular commands!Example}% *)
adamc@33 657
adamc@33 658 Example forall_refl : formula := Forall (fun x => Eq x x).
adamc@33 659
adamc@33 660 (** We can write recursive functions over reflexive types quite naturally. Here is one translating our formulas into native Coq propositions. *)
adamc@33 661
adamc@33 662 Fixpoint formulaDenote (f : formula) : Prop :=
adamc@33 663 match f with
adamc@33 664 | Eq n1 n2 => n1 = n2
adamc@33 665 | And f1 f2 => formulaDenote f1 /\ formulaDenote f2
adamc@33 666 | Forall f' => forall n : nat, formulaDenote (f' n)
adamc@33 667 end.
adamc@33 668
adamc@33 669 (** We can also encode a trivial formula transformation that swaps the order of equality and conjunction operands. *)
adamc@33 670
adamc@33 671 Fixpoint swapper (f : formula) : formula :=
adamc@33 672 match f with
adamc@33 673 | Eq n1 n2 => Eq n2 n1
adamc@33 674 | And f1 f2 => And (swapper f2) (swapper f1)
adamc@33 675 | Forall f' => Forall (fun n => swapper (f' n))
adamc@33 676 end.
adamc@33 677
adamc@33 678 (** It is helpful to prove that this transformation does not make true formulas false. *)
adamc@33 679
adamc@33 680 Theorem swapper_preserves_truth : forall f, formulaDenote f -> formulaDenote (swapper f).
adamc@41 681 (* begin thide *)
adamc@33 682 induction f; crush.
adamc@33 683 Qed.
adamc@41 684 (* end thide *)
adamc@33 685
adamc@33 686 (** We can take a look at the induction principle behind this proof. *)
adamc@33 687
adamc@33 688 Check formula_ind.
adamc@208 689 (** %\vspace{-.15in}% [[
adamc@208 690 formula_ind
adamc@33 691 : forall P : formula -> Prop,
adamc@33 692 (forall n n0 : nat, P (Eq n n0)) ->
adamc@33 693 (forall f0 : formula,
adamc@33 694 P f0 -> forall f1 : formula, P f1 -> P (And f0 f1)) ->
adamc@33 695 (forall f1 : nat -> formula,
adamc@33 696 (forall n : nat, P (f1 n)) -> P (Forall f1)) ->
adamc@33 697 forall f2 : formula, P f2
adamc@208 698 ]]
adamc@33 699
adam@442 700 Focusing on the [Forall] case, which comes third, we see that we are allowed to assume that the theorem holds _for any application of the argument function [f1]_. That is, Coq induction principles do not follow a simple rule that the textual representations of induction variables must get shorter in appeals to induction hypotheses. Luckily for us, the people behind the metatheory of Coq have verified that this flexibility does not introduce unsoundness.
adamc@33 701
adamc@33 702 %\medskip%
adamc@33 703
adam@469 704 Up to this point, we have seen how to encode in Coq more and more of what is possible with algebraic datatypes in %\index{Haskell}%Haskell and %\index{ML}%ML. This may have given the inaccurate impression that inductive types are a strict extension of algebraic datatypes. In fact, Coq must rule out some types allowed by Haskell and ML, for reasons of soundness. Reflexive types provide our first good example of such a case; only some of them are legal.
adamc@33 705
adam@400 706 Given our last example of an inductive type, many readers are probably eager to try encoding the syntax of %\index{lambda calculus}%lambda calculus. Indeed, the function-based representation technique that we just used, called%\index{higher-order abstract syntax}\index{HOAS|see{higher-order abstract syntax}}% _higher-order abstract syntax_ (HOAS)%~\cite{HOAS}%, is the representation of choice for lambda calculi in %\index{Twelf}%Twelf and in many applications implemented in Haskell and ML. Let us try to import that choice to Coq: *)
adam@400 707 (* begin hide *)
adam@437 708 (* begin thide *)
adam@400 709 Inductive term : Set := App | Abs.
adam@400 710 Reset term.
adam@420 711 Definition uhoh := O.
adam@437 712 (* end thide *)
adam@400 713 (* end hide *)
adamc@33 714 (** [[
adamc@33 715 Inductive term : Set :=
adamc@33 716 | App : term -> term -> term
adamc@33 717 | Abs : (term -> term) -> term.
adamc@33 718 ]]
adamc@33 719
adam@316 720 <<
adam@316 721 Error: Non strictly positive occurrence of "term" in "(term -> term) -> term"
adam@316 722 >>
adam@316 723
adam@400 724 We have run afoul of the%\index{strict positivity requirement}\index{positivity requirement}% _strict positivity requirement_ for inductive definitions, which says that the type being defined may not occur to the left of an arrow in the type of a constructor argument. It is important that the type of a constructor is viewed in terms of a series of arguments and a result, since obviously we need recursive occurrences to the lefts of the outermost arrows if we are to have recursive occurrences at all. Our candidate definition above violates the positivity requirement because it involves an argument of type [term -> term], where the type [term] that we are defining appears to the left of an arrow. The candidate type of [App] is fine, however, since every occurrence of [term] is either a constructor argument or the final result type.
adamc@33 725
adamc@33 726 Why must Coq enforce this restriction? Imagine that our last definition had been accepted, allowing us to write this function:
adamc@33 727
adam@439 728 %\vspace{-.15in}%[[
adamc@33 729 Definition uhoh (t : term) : term :=
adamc@33 730 match t with
adamc@33 731 | Abs f => f t
adamc@33 732 | _ => t
adamc@33 733 end.
adamc@205 734 ]]
adamc@205 735
adamc@33 736 Using an informal idea of Coq's semantics, it is easy to verify that the application [uhoh (Abs uhoh)] will run forever. This would be a mere curiosity in OCaml and Haskell, where non-termination is commonplace, though the fact that we have a non-terminating program without explicit recursive function definitions is unusual.
adamc@33 737
adam@316 738 %\index{termination checking}%For Coq, however, this would be a disaster. The possibility of writing such a function would destroy all our confidence that proving a theorem means anything. Since Coq combines programs and proofs in one language, we would be able to prove every theorem with an infinite loop.
adamc@33 739
adam@439 740 Nonetheless, the basic insight of HOAS is a very useful one, and there are ways to realize most benefits of HOAS in Coq. We will study a particular technique of this kind in the final chapter, on programming language syntax and semantics. *)
adamc@34 741
adamc@34 742
adam@317 743 (** * An Interlude on Induction Principles *)
adamc@34 744
adam@317 745 (** As we have emphasized a few times already, Coq proofs are actually programs, written in the same language we have been using in our examples all along. We can get a first sense of what this means by taking a look at the definitions of some of the %\index{induction principles}%induction principles we have used. A close look at the details here will help us construct induction principles manually, which we will see is necessary for some more advanced inductive definitions. *)
adamc@34 746
adam@470 747 Print nat_ind.
adam@437 748 (** %\vspace{-.15in}%[[
adam@470 749 nat_ind =
adam@470 750 fun P : nat -> Prop => nat_rect P
adam@470 751 : forall P : nat -> Prop,
adam@470 752 P O -> (forall n : nat, P n -> P (S n)) -> forall n : nat, P n
adamc@34 753 ]]
adamc@34 754
adam@470 755 We see that this induction principle is defined in terms of a more general principle, [nat_rect]. The <<rec>> stands for "recursion principle," and the <<t>> at the end stands for [Type]. *)
adamc@34 756
adam@470 757 Check nat_rect.
adamc@208 758 (** %\vspace{-.15in}% [[
adam@470 759 nat_rect
adam@470 760 : forall P : nat -> Type,
adam@470 761 P O -> (forall n : nat, P n -> P (S n)) -> forall n : nat, P n
adamc@34 762 ]]
adamc@34 763
adam@470 764 The principle [nat_rect] gives [P] type [nat -> Type] instead of [nat -> Prop]. This [Type] is another universe, like [Set] and [Prop]. In fact, it is a common supertype of both. Later on, we will discuss exactly what the significances of the different universes are. For now, it is just important that we can use [Type] as a sort of meta-universe that may turn out to be either [Set] or [Prop]. We can see the symmetry inherent in the subtyping relationship by printing the definition of another principle that was generated for [nat] automatically: *)
adamc@34 765
adam@470 766 Print nat_rec.
adam@437 767 (** %\vspace{-.15in}%[[
adam@470 768 nat_rec =
adam@470 769 fun P : nat -> Set => nat_rect P
adam@470 770 : forall P : nat -> Set,
adam@470 771 P O -> (forall n : nat, P n -> P (S n)) -> forall n : nat, P n
adamc@34 772 ]]
adamc@34 773
adam@470 774 This is identical to the definition for [nat_ind], except that we have substituted [Set] for [Prop]. For most inductive types [T], then, we get not just induction principles [T_ind], but also %\index{recursion principles}%recursion principles [T_rec]. We can use [T_rec] to write recursive definitions without explicit [Fixpoint] recursion. For instance, the following two definitions are equivalent: *)
adamc@34 775
adam@470 776 Fixpoint plus_recursive (n : nat) : nat -> nat :=
adam@470 777 match n with
adam@470 778 | O => fun m => m
adam@470 779 | S n' => fun m => S (plus_recursive n' m)
adamc@34 780 end.
adamc@34 781
adam@470 782 Definition plus_rec : nat -> nat -> nat :=
adam@472 783 nat_rec (fun _ : nat => nat -> nat) (fun m => m) (fun _ r m => S (r m)).
adam@472 784
adam@472 785 Theorem plus_equivalent : plus_recursive = plus_rec.
adam@472 786 reflexivity.
adam@472 787 Qed.
adamc@34 788
adam@470 789 (** Going even further down the rabbit hole, [nat_rect] itself is not even a primitive. It is a functional program that we can write manually. *)
adamc@34 790
adam@470 791 Print nat_rect.
adam@437 792 (** %\vspace{-.15in}%[[
adam@470 793 nat_rect =
adam@470 794 fun (P : nat -> Type) (f : P O) (f0 : forall n : nat, P n -> P (S n)) =>
adam@470 795 fix F (n : nat) : P n :=
adam@470 796 match n as n0 return (P n0) with
adam@470 797 | O => f
adam@470 798 | S n0 => f0 n0 (F n0)
adamc@208 799 end
adam@470 800 : forall P : nat -> Type,
adam@470 801 P O -> (forall n : nat, P n -> P (S n)) -> forall n : nat, P n
adamc@34 802 ]]
adamc@34 803
adam@470 804 The only new wrinkle heres are, first, an anonymous recursive function definition, using the %\index{Gallina terms!fix}%[fix] keyword of Gallina (which is like [fun] with recursion supported); and, second, the annotations on the [match] expression. This is a%\index{dependent pattern matching}% _dependently typed_ pattern match, because the _type_ of the expression depends on the _value_ being matched on. We will meet more involved examples later, especially in Part II of the book.
adam@317 805
adam@470 806 %\index{type inference}%Type inference for dependent pattern matching is undecidable, which can be proved by reduction from %\index{higher-order unification}%higher-order unification%~\cite{HOU}%. Thus, we often find ourselves needing to annotate our programs in a way that explains dependencies to the type checker. In the example of [nat_rect], we have an %\index{Gallina terms!as}%[as] clause, which binds a name for the discriminee; and a %\index{Gallina terms!return}%[return] clause, which gives a way to compute the [match] result type as a function of the discriminee.
adamc@34 807
adam@470 808 To prove that [nat_rect] is nothing special, we can reimplement it manually. *)
adamc@34 809
adam@470 810 Fixpoint nat_rect' (P : nat -> Type)
adam@470 811 (HO : P O)
adam@470 812 (HS : forall n, P n -> P (S n)) (n : nat) :=
adam@470 813 match n return P n with
adam@470 814 | O => HO
adam@470 815 | S n' => HS n' (nat_rect' P HO HS n')
adamc@34 816 end.
adamc@34 817
adam@470 818 (** We can understand the definition of [nat_rect] better by reimplementing [nat_ind] using sections. *)
adamc@34 819
adam@317 820 Section nat_ind'.
adamc@208 821 (** First, we have the property of natural numbers that we aim to prove. *)
adamc@34 822
adam@317 823 Variable P : nat -> Prop.
adamc@34 824
adam@317 825 (** Then we require a proof of the [O] case, which we declare with the command %\index{Vernacular commands!Hypothesis}%[Hypothesis], which is a synonym for [Variable] that, by convention, is used for variables whose types are propositions. *)
adamc@34 826
adam@317 827 Hypothesis O_case : P O.
adamc@34 828
adamc@208 829 (** Next is a proof of the [S] case, which may assume an inductive hypothesis. *)
adamc@34 830
adam@317 831 Hypothesis S_case : forall n : nat, P n -> P (S n).
adamc@34 832
adamc@208 833 (** Finally, we define a recursive function to tie the pieces together. *)
adamc@34 834
adam@317 835 Fixpoint nat_ind' (n : nat) : P n :=
adam@317 836 match n with
adam@317 837 | O => O_case
adam@317 838 | S n' => S_case (nat_ind' n')
adam@317 839 end.
adam@317 840 End nat_ind'.
adamc@34 841
adam@400 842 (** Closing the section adds the [Variable]s and [Hypothesis]es as new [fun]-bound arguments to [nat_ind'], and, modulo the use of [Prop] instead of [Type], we end up with the exact same definition that was generated automatically for [nat_rect].
adamc@34 843
adam@317 844 %\medskip%
adamc@34 845
adam@317 846 We can also examine the definition of [even_list_mut], which we generated with [Scheme] for a mutually recursive type. *)
adamc@34 847
adam@317 848 Print even_list_mut.
adam@439 849 (** %\vspace{-.15in}%[[
adam@317 850 even_list_mut =
adam@317 851 fun (P : even_list -> Prop) (P0 : odd_list -> Prop)
adam@317 852 (f : P ENil) (f0 : forall (n : nat) (o : odd_list), P0 o -> P (ECons n o))
adam@317 853 (f1 : forall (n : nat) (e : even_list), P e -> P0 (OCons n e)) =>
adam@317 854 fix F (e : even_list) : P e :=
adam@317 855 match e as e0 return (P e0) with
adam@317 856 | ENil => f
adam@317 857 | ECons n o => f0 n o (F0 o)
adam@317 858 end
adam@317 859 with F0 (o : odd_list) : P0 o :=
adam@317 860 match o as o0 return (P0 o0) with
adam@317 861 | OCons n e => f1 n e (F e)
adam@317 862 end
adam@317 863 for F
adam@317 864 : forall (P : even_list -> Prop) (P0 : odd_list -> Prop),
adam@317 865 P ENil ->
adam@317 866 (forall (n : nat) (o : odd_list), P0 o -> P (ECons n o)) ->
adam@317 867 (forall (n : nat) (e : even_list), P e -> P0 (OCons n e)) ->
adam@317 868 forall e : even_list, P e
adam@317 869 ]]
adamc@34 870
adam@442 871 We see a mutually recursive [fix], with the different functions separated by %\index{Gallina terms!with}%[with] in the same way that they would be separated by <<and>> in ML. A final %\index{Gallina terms!for}%[for] clause identifies which of the mutually recursive functions should be the final value of the [fix] expression. Using this definition as a template, we can reimplement [even_list_mut] directly. *)
adamc@208 872
adam@317 873 Section even_list_mut'.
adam@317 874 (** First, we need the properties that we are proving. *)
adamc@208 875
adam@317 876 Variable Peven : even_list -> Prop.
adam@317 877 Variable Podd : odd_list -> Prop.
adamc@208 878
adam@317 879 (** Next, we need proofs of the three cases. *)
adamc@208 880
adam@317 881 Hypothesis ENil_case : Peven ENil.
adam@317 882 Hypothesis ECons_case : forall (n : nat) (o : odd_list), Podd o -> Peven (ECons n o).
adam@317 883 Hypothesis OCons_case : forall (n : nat) (e : even_list), Peven e -> Podd (OCons n e).
adamc@208 884
adam@317 885 (** Finally, we define the recursive functions. *)
adamc@208 886
adam@317 887 Fixpoint even_list_mut' (e : even_list) : Peven e :=
adam@317 888 match e with
adam@317 889 | ENil => ENil_case
adam@317 890 | ECons n o => ECons_case n (odd_list_mut' o)
adam@317 891 end
adam@317 892 with odd_list_mut' (o : odd_list) : Podd o :=
adam@317 893 match o with
adam@317 894 | OCons n e => OCons_case n (even_list_mut' e)
adam@317 895 end.
adamc@34 896 End even_list_mut'.
adamc@34 897
adamc@34 898 (** Even induction principles for reflexive types are easy to implement directly. For our [formula] type, we can use a recursive definition much like those we wrote above. *)
adamc@34 899
adamc@34 900 Section formula_ind'.
adamc@34 901 Variable P : formula -> Prop.
adamc@38 902 Hypothesis Eq_case : forall n1 n2 : nat, P (Eq n1 n2).
adamc@38 903 Hypothesis And_case : forall f1 f2 : formula,
adamc@34 904 P f1 -> P f2 -> P (And f1 f2).
adamc@38 905 Hypothesis Forall_case : forall f : nat -> formula,
adamc@34 906 (forall n : nat, P (f n)) -> P (Forall f).
adamc@34 907
adamc@34 908 Fixpoint formula_ind' (f : formula) : P f :=
adamc@208 909 match f with
adamc@34 910 | Eq n1 n2 => Eq_case n1 n2
adamc@34 911 | And f1 f2 => And_case (formula_ind' f1) (formula_ind' f2)
adamc@34 912 | Forall f' => Forall_case f' (fun n => formula_ind' (f' n))
adamc@34 913 end.
adamc@34 914 End formula_ind'.
adamc@34 915
adam@317 916 (** It is apparent that induction principle implementations involve some tedium but not terribly much creativity. *)
adam@317 917
adamc@35 918
adamc@35 919 (** * Nested Inductive Types *)
adamc@35 920
adamc@35 921 (** Suppose we want to extend our earlier type of binary trees to trees with arbitrary finite branching. We can use lists to give a simple definition. *)
adamc@35 922
adamc@35 923 Inductive nat_tree : Set :=
adamc@35 924 | NNode' : nat -> list nat_tree -> nat_tree.
adamc@35 925
adam@465 926 (** This is an example of a%\index{nested inductive type}% _nested_ inductive type definition, because we use the type we are defining as an argument to a parameterized type family. Coq will not allow all such definitions; it effectively pretends that we are defining [nat_tree] mutually with a version of [list] specialized to [nat_tree], checking that the resulting expanded definition satisfies the usual rules. For instance, if we replaced [list] with a type family that used its parameter as a function argument, then the definition would be rejected as violating the positivity restriction.
adamc@35 927
adam@475 928 As we encountered with mutual inductive types, we find that the automatically generated induction principle for [nat_tree] is too weak. *)
adamc@35 929
adam@471 930 (* begin hide *)
adam@471 931 (* begin thide *)
adam@471 932 Check Forall.
adam@471 933 (* end thide *)
adam@471 934 (* end hide *)
adam@471 935
adamc@35 936 Check nat_tree_ind.
adamc@208 937 (** %\vspace{-.15in}% [[
adamc@208 938 nat_tree_ind
adamc@35 939 : forall P : nat_tree -> Prop,
adamc@35 940 (forall (n : nat) (l : list nat_tree), P (NNode' n l)) ->
adamc@35 941 forall n : nat_tree, P n
adamc@35 942 ]]
adamc@35 943
adam@471 944 There is no command like [Scheme] that will implement an improved principle for us. In general, it takes creativity to figure out _good_ ways to incorporate nested uses of different type families. Now that we know how to implement induction principles manually, we are in a position to apply just such creativity to this problem.
adamc@35 945
adam@471 946 Many induction principles for types with nested used of [list] could benefit from a unified predicate capturing the idea that some property holds of every element in a list. By defining this generic predicate once, we facilitate reuse of library theorems about it. (Here, we are actually duplicating the standard library's [Forall] predicate, with a different implementation, for didactic purposes.) *)
adamc@35 947
adamc@35 948 Section All.
adamc@35 949 Variable T : Set.
adamc@35 950 Variable P : T -> Prop.
adamc@35 951
adamc@35 952 Fixpoint All (ls : list T) : Prop :=
adamc@35 953 match ls with
adamc@35 954 | Nil => True
adamc@35 955 | Cons h t => P h /\ All t
adamc@35 956 end.
adamc@35 957 End All.
adamc@35 958
adam@317 959 (** It will be useful to review the definitions of [True] and [/\], since we will want to write manual proofs of them below. *)
adamc@35 960
adamc@35 961 Print True.
adam@439 962 (** %\vspace{-.15in}%[[
adamc@208 963 Inductive True : Prop := I : True
adamc@208 964 ]]
adamc@35 965
adam@442 966 That is, [True] is a proposition with exactly one proof, [I], which we may always supply trivially.
adamc@35 967
adam@400 968 Finding the definition of [/\] takes a little more work. Coq supports user registration of arbitrary parsing rules, and it is such a rule that is letting us write [/\] instead of an application of some inductive type family. We can find the underlying inductive type with the %\index{Vernacular commands!Locate}%[Locate] command, whose argument may be a parsing token.%\index{Gallina terms!and}% *)
adamc@35 969
adamc@35 970 Locate "/\".
adam@439 971 (** %\vspace{-.15in}%[[
adam@317 972 "A /\ B" := and A B : type_scope (default interpretation)
adam@302 973 ]]
adam@302 974 *)
adamc@35 975
adamc@35 976 Print and.
adam@439 977 (** %\vspace{-.15in}%[[
adamc@208 978 Inductive and (A : Prop) (B : Prop) : Prop := conj : A -> B -> A /\ B
adam@317 979 ]]
adam@317 980 %\vspace{-.1in}%
adam@317 981 <<
adamc@208 982 For conj: Arguments A, B are implicit
adam@317 983 >>
adamc@35 984
adam@400 985 In addition to the definition of [and] itself, we get information on %\index{implicit arguments}%implicit arguments (and some other information that we omit here). The implicit argument information tells us that we build a proof of a conjunction by calling the constructor [conj] on proofs of the conjuncts, with no need to include the types of those proofs as explicit arguments.
adamc@35 986
adamc@35 987 %\medskip%
adamc@35 988
adam@448 989 Now we create a section for our induction principle, following the same basic plan as in the previous section of this chapter. *)
adamc@35 990
adamc@35 991 Section nat_tree_ind'.
adamc@35 992 Variable P : nat_tree -> Prop.
adamc@35 993
adamc@38 994 Hypothesis NNode'_case : forall (n : nat) (ls : list nat_tree),
adamc@35 995 All P ls -> P (NNode' n ls).
adamc@35 996
adam@420 997 (* begin hide *)
adam@437 998 (* begin thide *)
adam@420 999 Definition list_nat_tree_ind := O.
adam@437 1000 (* end thide *)
adam@420 1001 (* end hide *)
adam@420 1002
adamc@35 1003 (** A first attempt at writing the induction principle itself follows the intuition that nested inductive type definitions are expanded into mutual inductive definitions.
adamc@35 1004
adam@439 1005 %\vspace{-.15in}%[[
adamc@35 1006 Fixpoint nat_tree_ind' (tr : nat_tree) : P tr :=
adamc@208 1007 match tr with
adamc@35 1008 | NNode' n ls => NNode'_case n ls (list_nat_tree_ind ls)
adamc@35 1009 end
adamc@35 1010
adamc@35 1011 with list_nat_tree_ind (ls : list nat_tree) : All P ls :=
adamc@208 1012 match ls with
adamc@35 1013 | Nil => I
adamc@35 1014 | Cons tr rest => conj (nat_tree_ind' tr) (list_nat_tree_ind rest)
adamc@35 1015 end.
adamc@205 1016 ]]
adamc@205 1017
adam@442 1018 Coq rejects this definition, saying
adam@317 1019 <<
adam@317 1020 Recursive call to nat_tree_ind' has principal argument equal to "tr"
adam@317 1021 instead of rest.
adam@317 1022 >>
adam@317 1023
adam@475 1024 There is no deep theoretical reason why this program should be rejected; Coq applies incomplete termination-checking heuristics, and it is necessary to learn a few of the most important rules. The term "nested inductive type" hints at the solution to this particular problem. Just as mutually inductive types require mutually recursive induction principles, nested types require nested recursion. *)
adamc@35 1025
adamc@35 1026 Fixpoint nat_tree_ind' (tr : nat_tree) : P tr :=
adamc@208 1027 match tr with
adamc@35 1028 | NNode' n ls => NNode'_case n ls
adamc@35 1029 ((fix list_nat_tree_ind (ls : list nat_tree) : All P ls :=
adamc@208 1030 match ls with
adamc@35 1031 | Nil => I
adam@471 1032 | Cons tr' rest => conj (nat_tree_ind' tr') (list_nat_tree_ind rest)
adamc@35 1033 end) ls)
adamc@35 1034 end.
adamc@35 1035
adam@398 1036 (** We include an anonymous [fix] version of [list_nat_tree_ind] that is literally _nested_ inside the definition of the recursive function corresponding to the inductive definition that had the nested use of [list]. *)
adamc@35 1037
adamc@35 1038 End nat_tree_ind'.
adamc@35 1039
adam@475 1040 (** We can try our induction principle out by defining some recursive functions on [nat_tree] and proving a theorem about them. First, we define some helper functions that operate on lists. *)
adamc@35 1041
adamc@35 1042 Section map.
adamc@35 1043 Variables T T' : Set.
adam@317 1044 Variable F : T -> T'.
adamc@35 1045
adamc@35 1046 Fixpoint map (ls : list T) : list T' :=
adamc@35 1047 match ls with
adamc@35 1048 | Nil => Nil
adam@317 1049 | Cons h t => Cons (F h) (map t)
adamc@35 1050 end.
adamc@35 1051 End map.
adamc@35 1052
adamc@35 1053 Fixpoint sum (ls : list nat) : nat :=
adamc@35 1054 match ls with
adamc@35 1055 | Nil => O
adamc@35 1056 | Cons h t => plus h (sum t)
adamc@35 1057 end.
adamc@35 1058
adamc@35 1059 (** Now we can define a size function over our trees. *)
adamc@35 1060
adamc@35 1061 Fixpoint ntsize (tr : nat_tree) : nat :=
adamc@35 1062 match tr with
adamc@35 1063 | NNode' _ trs => S (sum (map ntsize trs))
adamc@35 1064 end.
adamc@35 1065
adamc@35 1066 (** Notice that Coq was smart enough to expand the definition of [map] to verify that we are using proper nested recursion, even through a use of a higher-order function. *)
adamc@35 1067
adamc@208 1068 Fixpoint ntsplice (tr1 tr2 : nat_tree) : nat_tree :=
adamc@35 1069 match tr1 with
adamc@35 1070 | NNode' n Nil => NNode' n (Cons tr2 Nil)
adamc@35 1071 | NNode' n (Cons tr trs) => NNode' n (Cons (ntsplice tr tr2) trs)
adamc@35 1072 end.
adamc@35 1073
adamc@35 1074 (** We have defined another arbitrary notion of tree splicing, similar to before, and we can prove an analogous theorem about its relationship with tree size. We start with a useful lemma about addition. *)
adamc@35 1075
adamc@41 1076 (* begin thide *)
adamc@35 1077 Lemma plus_S : forall n1 n2 : nat,
adamc@35 1078 plus n1 (S n2) = S (plus n1 n2).
adamc@35 1079 induction n1; crush.
adamc@35 1080 Qed.
adamc@41 1081 (* end thide *)
adamc@35 1082
adamc@35 1083 (** Now we begin the proof of the theorem, adding the lemma [plus_S] as a hint. *)
adamc@35 1084
adamc@35 1085 Theorem ntsize_ntsplice : forall tr1 tr2 : nat_tree, ntsize (ntsplice tr1 tr2)
adamc@35 1086 = plus (ntsize tr2) (ntsize tr1).
adamc@41 1087 (* begin thide *)
adam@375 1088 Hint Rewrite plus_S.
adamc@35 1089
adam@317 1090 (** We know that the standard induction principle is insufficient for the task, so we need to provide a %\index{tactics!using}%[using] clause for the [induction] tactic to specify our alternate principle. *)
adamc@208 1091
adamc@35 1092 induction tr1 using nat_tree_ind'; crush.
adamc@35 1093
adamc@35 1094 (** One subgoal remains: [[
adamc@35 1095 n : nat
adamc@35 1096 ls : list nat_tree
adamc@35 1097 H : All
adamc@35 1098 (fun tr1 : nat_tree =>
adamc@35 1099 forall tr2 : nat_tree,
adamc@35 1100 ntsize (ntsplice tr1 tr2) = plus (ntsize tr2) (ntsize tr1)) ls
adamc@35 1101 tr2 : nat_tree
adamc@35 1102 ============================
adamc@35 1103 ntsize
adamc@35 1104 match ls with
adamc@35 1105 | Nil => NNode' n (Cons tr2 Nil)
adamc@35 1106 | Cons tr trs => NNode' n (Cons (ntsplice tr tr2) trs)
adamc@35 1107 end = S (plus (ntsize tr2) (sum (map ntsize ls)))
adamc@208 1108
adamc@35 1109 ]]
adamc@35 1110
adamc@35 1111 After a few moments of squinting at this goal, it becomes apparent that we need to do a case analysis on the structure of [ls]. The rest is routine. *)
adamc@35 1112
adamc@35 1113 destruct ls; crush.
adamc@35 1114
adam@317 1115 (** We can go further in automating the proof by exploiting the hint mechanism.%\index{Vernacular commands!Hint Extern}% *)
adamc@35 1116
adamc@35 1117 Restart.
adam@317 1118
adamc@35 1119 Hint Extern 1 (ntsize (match ?LS with Nil => _ | Cons _ _ => _ end) = _) =>
adamc@35 1120 destruct LS; crush.
adamc@35 1121 induction tr1 using nat_tree_ind'; crush.
adamc@35 1122 Qed.
adamc@41 1123 (* end thide *)
adamc@35 1124
adamc@35 1125 (** We will go into great detail on hints in a later chapter, but the only important thing to note here is that we register a pattern that describes a conclusion we expect to encounter during the proof. The pattern may contain unification variables, whose names are prefixed with question marks, and we may refer to those bound variables in a tactic that we ask to have run whenever the pattern matches.
adamc@35 1126
adam@317 1127 The advantage of using the hint is not very clear here, because the original proof was so short. However, the hint has fundamentally improved the readability of our proof. Before, the proof referred to the local variable [ls], which has an automatically generated name. To a human reading the proof script without stepping through it interactively, it was not clear where [ls] came from. The hint explains to the reader the process for choosing which variables to case analyze, and the hint can continue working even if the rest of the proof structure changes significantly. *)
adamc@36 1128
adamc@36 1129
adamc@36 1130 (** * Manual Proofs About Constructors *)
adamc@36 1131
adam@317 1132 (** It can be useful to understand how tactics like %\index{tactics!discriminate}%[discriminate] and %\index{tactics!injection}%[injection] work, so it is worth stepping through a manual proof of each kind. We will start with a proof fit for [discriminate]. *)
adamc@36 1133
adamc@36 1134 Theorem true_neq_false : true <> false.
adamc@208 1135
adamc@41 1136 (* begin thide *)
adam@420 1137 (** We begin with the tactic %\index{tactics!red}%[red], which is short for "one step of reduction," to unfold the definition of logical negation. *)
adamc@36 1138
adamc@36 1139 red.
adam@439 1140 (** %\vspace{-.15in}%[[
adamc@36 1141 ============================
adamc@36 1142 true = false -> False
adamc@36 1143 ]]
adamc@36 1144
adam@442 1145 The negation is replaced with an implication of falsehood. We use the tactic %\index{tactics!intro}%[intro H] to change the assumption of the implication into a hypothesis named [H]. *)
adamc@36 1146
adamc@36 1147 intro H.
adam@439 1148 (** %\vspace{-.15in}%[[
adamc@36 1149 H : true = false
adamc@36 1150 ============================
adamc@36 1151 False
adamc@36 1152 ]]
adamc@36 1153
adam@442 1154 This is the point in the proof where we apply some creativity. We define a function whose utility will become clear soon. *)
adamc@36 1155
adam@317 1156 Definition toProp (b : bool) := if b then True else False.
adamc@36 1157
adam@448 1158 (** It is worth recalling the difference between the lowercase and uppercase versions of truth and falsehood: [True] and [False] are logical propositions, while [true] and [false] are Boolean values that we can case-analyze. We have defined [toProp] such that our conclusion of [False] is computationally equivalent to [toProp false]. Thus, the %\index{tactics!change}%[change] tactic will let us change the conclusion to [toProp false]. The general form [change e] replaces the conclusion with [e], whenever Coq's built-in computation rules suffice to establish the equivalence of [e] with the original conclusion. *)
adamc@36 1159
adam@317 1160 change (toProp false).
adam@439 1161 (** %\vspace{-.15in}%[[
adamc@36 1162 H : true = false
adamc@36 1163 ============================
adam@317 1164 toProp false
adamc@36 1165 ]]
adamc@36 1166
adam@448 1167 Now the righthand side of [H]'s equality appears in the conclusion, so we can rewrite, using the notation [<-] to request to replace the righthand side of the equality with the lefthand side.%\index{tactics!rewrite}% *)
adamc@36 1168
adamc@36 1169 rewrite <- H.
adam@439 1170 (** %\vspace{-.15in}%[[
adamc@36 1171 H : true = false
adamc@36 1172 ============================
adam@317 1173 toProp true
adamc@36 1174 ]]
adamc@36 1175
adam@442 1176 We are almost done. Just how close we are to done is revealed by computational simplification. *)
adamc@36 1177
adamc@36 1178 simpl.
adam@439 1179 (** %\vspace{-.15in}%[[
adamc@36 1180 H : true = false
adamc@36 1181 ============================
adamc@36 1182 True
adam@302 1183 ]]
adam@302 1184 *)
adamc@36 1185
adamc@36 1186 trivial.
adamc@36 1187 Qed.
adamc@41 1188 (* end thide *)
adamc@36 1189
adamc@36 1190 (** I have no trivial automated version of this proof to suggest, beyond using [discriminate] or [congruence] in the first place.
adamc@36 1191
adamc@36 1192 %\medskip%
adamc@36 1193
adamc@36 1194 We can perform a similar manual proof of injectivity of the constructor [S]. I leave a walk-through of the details to curious readers who want to run the proof script interactively. *)
adamc@36 1195
adamc@36 1196 Theorem S_inj' : forall n m : nat, S n = S m -> n = m.
adamc@41 1197 (* begin thide *)
adamc@36 1198 intros n m H.
adamc@36 1199 change (pred (S n) = pred (S m)).
adamc@36 1200 rewrite H.
adamc@36 1201 reflexivity.
adamc@36 1202 Qed.
adamc@41 1203 (* end thide *)
adamc@36 1204
adam@400 1205 (** The key piece of creativity in this theorem comes in the use of the natural number predecessor function [pred]. Embodied in the implementation of [injection] is a generic recipe for writing such type-specific functions.
adam@317 1206
adam@317 1207 The examples in this section illustrate an important aspect of the design philosophy behind Coq. We could certainly design a Gallina replacement that built in rules for constructor discrimination and injectivity, but a simpler alternative is to include a few carefully chosen rules that enable the desired reasoning patterns and many others. A key benefit of this philosophy is that the complexity of proof checking is minimized, which bolsters our confidence that proved theorems are really true. *)