Mercurial > cpdt > repo
comparison src/Predicates.v @ 55:8d7a97b3bb91
Chapter read-through
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Sun, 28 Sep 2008 13:50:21 -0400 |
parents | 31622083ad5d |
children | 1946586b9da7 |
comparison
equal
deleted
inserted
replaced
54:31622083ad5d | 55:8d7a97b3bb91 |
---|---|
51 (** In Coq, the most basic propositional connective is implication, written [->], which we have already used in almost every proof. Rather than being defined inductively, implication is built into Coq as the function type constructor. | 51 (** In Coq, the most basic propositional connective is implication, written [->], which we have already used in almost every proof. Rather than being defined inductively, implication is built into Coq as the function type constructor. |
52 | 52 |
53 We have also already seen the definition of [True]. For a demonstration of a lower-level way of establishing proofs of inductive predicates, we turn to this trivial theorem. *) | 53 We have also already seen the definition of [True]. For a demonstration of a lower-level way of establishing proofs of inductive predicates, we turn to this trivial theorem. *) |
54 | 54 |
55 Theorem obvious : True. | 55 Theorem obvious : True. |
56 (* begin thide *) | |
56 apply I. | 57 apply I. |
58 (* end thide *) | |
57 Qed. | 59 Qed. |
58 | 60 |
59 (** We may always use the [apply] tactic to take a proof step based on applying a particular constructor of the inductive predicate that we are trying to establish. Sometimes there is only one constructor that could possibly apply, in which case a shortcut is available: *) | 61 (** We may always use the [apply] tactic to take a proof step based on applying a particular constructor of the inductive predicate that we are trying to establish. Sometimes there is only one constructor that could possibly apply, in which case a shortcut is available: *) |
60 | 62 |
63 (* begin thide *) | |
61 Theorem obvious' : True. | 64 Theorem obvious' : True. |
62 constructor. | 65 constructor. |
63 Qed. | 66 Qed. |
64 | 67 |
68 (* end thide *) | |
69 | |
65 (** There is also a predicate [False], which is the Curry-Howard mirror image of [Empty_set] from the last chapter. *) | 70 (** There is also a predicate [False], which is the Curry-Howard mirror image of [Empty_set] from the last chapter. *) |
66 | 71 |
67 Print False. | 72 Print False. |
68 (** [[ | 73 (** [[ |
69 | 74 |
71 ]] *) | 76 ]] *) |
72 | 77 |
73 (** We can conclude anything from [False], doing case analysis on a proof of [False] in the same way we might do case analysis on, say, a natural number. Since there are no cases to consider, any such case analysis succeeds immediately in proving the goal. *) | 78 (** We can conclude anything from [False], doing case analysis on a proof of [False] in the same way we might do case analysis on, say, a natural number. Since there are no cases to consider, any such case analysis succeeds immediately in proving the goal. *) |
74 | 79 |
75 Theorem False_imp : False -> 2 + 2 = 5. | 80 Theorem False_imp : False -> 2 + 2 = 5. |
81 (* begin thide *) | |
76 destruct 1. | 82 destruct 1. |
83 (* end thide *) | |
77 Qed. | 84 Qed. |
78 | 85 |
79 (** In a consistent context, we can never build a proof of [False]. In inconsistent contexts that appear in the courses of proofs, it is usually easiest to proceed by demonstrating that inconsistency with an explicit proof of [False]. *) | 86 (** In a consistent context, we can never build a proof of [False]. In inconsistent contexts that appear in the courses of proofs, it is usually easiest to proceed by demonstrating that inconsistency with an explicit proof of [False]. *) |
80 | 87 |
81 Theorem arith_neq : 2 + 2 = 5 -> 9 + 9 = 835. | 88 Theorem arith_neq : 2 + 2 = 5 -> 9 + 9 = 835. |
89 (* begin thide *) | |
82 intro. | 90 intro. |
83 | 91 |
84 (** At this point, we have an inconsistent hypothesis [2 + 2 = 5], so the specific conclusion is not important. We use the [elimtype] tactic to state a proposition, telling Coq that we wish to construct a proof of the new proposition and then prove the original goal by case analysis on the structure of the new auxiliary proof. Since [False] has no constructors, [elimtype False] simply leaves us with the obligation to prove [False]. *) | 92 (** At this point, we have an inconsistent hypothesis [2 + 2 = 5], so the specific conclusion is not important. We use the [elimtype] tactic to state a proposition, telling Coq that we wish to construct a proof of the new proposition and then prove the original goal by case analysis on the structure of the new auxiliary proof. Since [False] has no constructors, [elimtype False] simply leaves us with the obligation to prove [False]. *) |
85 | 93 |
86 elimtype False. | 94 elimtype False. |
92 ]] *) | 100 ]] *) |
93 | 101 |
94 (** For now, we will leave the details of this proof about arithmetic to [crush]. *) | 102 (** For now, we will leave the details of this proof about arithmetic to [crush]. *) |
95 | 103 |
96 crush. | 104 crush. |
105 (* end thide *) | |
97 Qed. | 106 Qed. |
98 | 107 |
99 (** A related notion to [False] is logical negation. *) | 108 (** A related notion to [False] is logical negation. *) |
100 | 109 |
101 Print not. | 110 Print not. |
106 ]] *) | 115 ]] *) |
107 | 116 |
108 (** We see that [not] is just shorthand for implication of [False]. We can use that fact explicitly in proofs. The syntax [~P] expands to [not P]. *) | 117 (** We see that [not] is just shorthand for implication of [False]. We can use that fact explicitly in proofs. The syntax [~P] expands to [not P]. *) |
109 | 118 |
110 Theorem arith_neq' : ~ (2 + 2 = 5). | 119 Theorem arith_neq' : ~ (2 + 2 = 5). |
120 (* begin thide *) | |
111 unfold not. | 121 unfold not. |
112 | 122 |
113 (** [[ | 123 (** [[ |
114 | 124 |
115 ============================ | 125 ============================ |
116 2 + 2 = 5 -> False | 126 2 + 2 = 5 -> False |
117 ]] *) | 127 ]] *) |
118 | 128 |
119 crush. | 129 crush. |
130 (* end thide *) | |
120 Qed. | 131 Qed. |
121 | 132 |
122 (** We also have conjunction, which we introduced in the last chapter. *) | 133 (** We also have conjunction, which we introduced in the last chapter. *) |
123 | 134 |
124 Print and. | 135 Print and. |
128 ]] *) | 139 ]] *) |
129 | 140 |
130 (** The interested reader can check that [and] has a Curry-Howard doppleganger called [prod], the type of pairs. However, it is generally most convenient to reason about conjunction using tactics. An explicit proof of commutativity of [and] illustrates the usual suspects for such tasks. [/\] is an infix shorthand for [and]. *) | 141 (** The interested reader can check that [and] has a Curry-Howard doppleganger called [prod], the type of pairs. However, it is generally most convenient to reason about conjunction using tactics. An explicit proof of commutativity of [and] illustrates the usual suspects for such tasks. [/\] is an infix shorthand for [and]. *) |
131 | 142 |
132 Theorem and_comm : P /\ Q -> Q /\ P. | 143 Theorem and_comm : P /\ Q -> Q /\ P. |
144 (* begin thide *) | |
133 (** We start by case analysis on the proof of [P /\ Q]. *) | 145 (** We start by case analysis on the proof of [P /\ Q]. *) |
134 | 146 |
135 destruct 1. | 147 destruct 1. |
136 (** [[ | 148 (** [[ |
137 | 149 |
158 | 170 |
159 (** In each case, the conclusion is among our hypotheses, so the [assumption] tactic finishes the process. *) | 171 (** In each case, the conclusion is among our hypotheses, so the [assumption] tactic finishes the process. *) |
160 | 172 |
161 assumption. | 173 assumption. |
162 assumption. | 174 assumption. |
175 (* end thide *) | |
163 Qed. | 176 Qed. |
164 | 177 |
165 (** Coq disjunction is called [or] and abbreviated with the infix operator [\/]. *) | 178 (** Coq disjunction is called [or] and abbreviated with the infix operator [\/]. *) |
166 | 179 |
167 Print or. | 180 Print or. |
172 ]] *) | 185 ]] *) |
173 | 186 |
174 (** We see that there are two ways to prove a disjunction: prove the first disjunct or prove the second. The Curry-Howard analogue of this is the Coq [sum] type. We can demonstrate the main tactics here with another proof of commutativity. *) | 187 (** We see that there are two ways to prove a disjunction: prove the first disjunct or prove the second. The Curry-Howard analogue of this is the Coq [sum] type. We can demonstrate the main tactics here with another proof of commutativity. *) |
175 | 188 |
176 Theorem or_comm : P \/ Q -> Q \/ P. | 189 Theorem or_comm : P \/ Q -> Q \/ P. |
190 | |
191 (* begin thide *) | |
177 (** As in the proof for [and], we begin with case analysis, though this time we are met by two cases instead of one. *) | 192 (** As in the proof for [and], we begin with case analysis, though this time we are met by two cases instead of one. *) |
178 destruct 1. | 193 destruct 1. |
179 (** [[ | 194 (** [[ |
180 | 195 |
181 2 subgoals | 196 2 subgoals |
202 ============================ | 217 ============================ |
203 Q \/ P | 218 Q \/ P |
204 ]] *) | 219 ]] *) |
205 | 220 |
206 left; assumption. | 221 left; assumption. |
222 (* end thide *) | |
207 Qed. | 223 Qed. |
208 | 224 |
209 | 225 |
210 (* begin hide *) | 226 (* begin hide *) |
211 (* In-class exercises *) | 227 (* In-class exercises *) |
253 | 269 |
254 | 270 |
255 (** It would be a shame to have to plod manually through all proofs about propositional logic. Luckily, there is no need. One of the most basic Coq automation tactics is [tauto], which is a complete decision procedure for constructive propositional logic. (More on what "constructive" means in the next section.) We can use [tauto] to dispatch all of the purely propositional theorems we have proved so far. *) | 271 (** It would be a shame to have to plod manually through all proofs about propositional logic. Luckily, there is no need. One of the most basic Coq automation tactics is [tauto], which is a complete decision procedure for constructive propositional logic. (More on what "constructive" means in the next section.) We can use [tauto] to dispatch all of the purely propositional theorems we have proved so far. *) |
256 | 272 |
257 Theorem or_comm' : P \/ Q -> Q \/ P. | 273 Theorem or_comm' : P \/ Q -> Q \/ P. |
274 (* begin thide *) | |
258 tauto. | 275 tauto. |
276 (* end thide *) | |
259 Qed. | 277 Qed. |
260 | 278 |
261 (** Sometimes propositional reasoning forms important plumbing for the proof of a theorem, but we still need to apply some other smarts about, say, arithmetic. [intuition] is a generalization of [tauto] that proves everything it can using propositional reasoning. When some goals remain, it uses propositional laws to simplify them as far as possible. Consider this example, which uses the list concatenation operator [++] from the standard library. *) | 279 (** Sometimes propositional reasoning forms important plumbing for the proof of a theorem, but we still need to apply some other smarts about, say, arithmetic. [intuition] is a generalization of [tauto] that proves everything it can using propositional reasoning. When some goals remain, it uses propositional laws to simplify them as far as possible. Consider this example, which uses the list concatenation operator [++] from the standard library. *) |
262 | 280 |
263 Theorem arith_comm : forall ls1 ls2 : list nat, | 281 Theorem arith_comm : forall ls1 ls2 : list nat, |
264 length ls1 = length ls2 \/ length ls1 + length ls2 = 6 | 282 length ls1 = length ls2 \/ length ls1 + length ls2 = 6 |
265 -> length (ls1 ++ ls2) = 6 \/ length ls1 = length ls2. | 283 -> length (ls1 ++ ls2) = 6 \/ length ls1 = length ls2. |
284 (* begin thide *) | |
266 intuition. | 285 intuition. |
267 | 286 |
268 (** A lot of the proof structure has been generated for us by [intuition], but the final proof depends on a fact about lists. The remaining subgoal hints at what cleverness we need to inject. *) | 287 (** A lot of the proof structure has been generated for us by [intuition], but the final proof depends on a fact about lists. The remaining subgoal hints at what cleverness we need to inject. *) |
269 | 288 |
270 (** [[ | 289 (** [[ |
289 ]] *) | 308 ]] *) |
290 | 309 |
291 (** Now the subgoal follows by purely propositional reasoning. That is, we could replace [length ls1 + length ls2 = 6] with [P] and [length ls1 = length ls2] with [Q] and arrive at a tautology of propositional logic. *) | 310 (** Now the subgoal follows by purely propositional reasoning. That is, we could replace [length ls1 + length ls2 = 6] with [P] and [length ls1 = length ls2] with [Q] and arrive at a tautology of propositional logic. *) |
292 | 311 |
293 tauto. | 312 tauto. |
313 (* end thide *) | |
294 Qed. | 314 Qed. |
295 | 315 |
296 (** [intuition] is one of the main bits of glue in the implementation of [crush], so, with a little help, we can get a short automated proof of the theorem. *) | 316 (** [intuition] is one of the main bits of glue in the implementation of [crush], so, with a little help, we can get a short automated proof of the theorem. *) |
297 | 317 |
318 (* begin thide *) | |
298 Theorem arith_comm' : forall ls1 ls2 : list nat, | 319 Theorem arith_comm' : forall ls1 ls2 : list nat, |
299 length ls1 = length ls2 \/ length ls1 + length ls2 = 6 | 320 length ls1 = length ls2 \/ length ls1 + length ls2 = 6 |
300 -> length (ls1 ++ ls2) = 6 \/ length ls1 = length ls2. | 321 -> length (ls1 ++ ls2) = 6 \/ length ls1 = length ls2. |
301 Hint Rewrite app_length : cpdt. | 322 Hint Rewrite app_length : cpdt. |
302 | 323 |
303 crush. | 324 crush. |
304 Qed. | 325 Qed. |
326 (* end thide *) | |
305 | 327 |
306 End Propositional. | 328 End Propositional. |
307 | 329 |
308 | 330 |
309 (** * What Does It Mean to Be Constructive? *) | 331 (** * What Does It Mean to Be Constructive? *) |
333 ]] *) | 355 ]] *) |
334 | 356 |
335 (** [ex] is parameterized by the type [A] that we quantify over, and by a predicate [P] over [A]s. We prove an existential by exhibiting some [x] of type [A], along with a proof of [P x]. As usual, there are tactics that save us from worrying about the low-level details most of the time. *) | 357 (** [ex] is parameterized by the type [A] that we quantify over, and by a predicate [P] over [A]s. We prove an existential by exhibiting some [x] of type [A], along with a proof of [P x]. As usual, there are tactics that save us from worrying about the low-level details most of the time. *) |
336 | 358 |
337 Theorem exist1 : exists x : nat, x + 1 = 2. | 359 Theorem exist1 : exists x : nat, x + 1 = 2. |
360 (* begin thide *) | |
338 (** remove printing exists*) | 361 (** remove printing exists*) |
339 (** We can start this proof with a tactic [exists], which should not be confused with the formula constructor shorthand of the same name. (In the PDF version of this document, the reverse 'E' appears instead of the text "exists.") *) | 362 (** We can start this proof with a tactic [exists], which should not be confused with the formula constructor shorthand of the same name. (In the PDF version of this document, the reverse 'E' appears instead of the text "exists" in formulas.) *) |
340 exists 1. | 363 exists 1. |
341 | 364 |
342 (** The conclusion is replaced with a version using the existential witness that we announced. *) | 365 (** The conclusion is replaced with a version using the existential witness that we announced. *) |
343 | 366 |
344 (** [[ | 367 (** [[ |
346 ============================ | 369 ============================ |
347 1 + 1 = 2 | 370 1 + 1 = 2 |
348 ]] *) | 371 ]] *) |
349 | 372 |
350 reflexivity. | 373 reflexivity. |
374 (* end thide *) | |
351 Qed. | 375 Qed. |
352 | 376 |
353 (** printing exists $\exists$ *) | 377 (** printing exists $\exists$ *) |
354 | 378 |
355 (** We can also use tactics to reason about existential hypotheses. *) | 379 (** We can also use tactics to reason about existential hypotheses. *) |
356 | 380 |
357 Theorem exist2 : forall n m : nat, (exists x : nat, n + x = m) -> n <= m. | 381 Theorem exist2 : forall n m : nat, (exists x : nat, n + x = m) -> n <= m. |
382 (* begin thide *) | |
358 (** We start by case analysis on the proof of the existential fact. *) | 383 (** We start by case analysis on the proof of the existential fact. *) |
359 destruct 1. | 384 destruct 1. |
360 (** [[ | 385 (** [[ |
361 | 386 |
362 n : nat | 387 n : nat |
368 ]] *) | 393 ]] *) |
369 | 394 |
370 (** The goal has been replaced by a form where there is a new free variable [x], and where we have a new hypothesis that the body of the existential holds with [x] substituted for the old bound variable. From here, the proof is just about arithmetic and is easy to automate. *) | 395 (** The goal has been replaced by a form where there is a new free variable [x], and where we have a new hypothesis that the body of the existential holds with [x] substituted for the old bound variable. From here, the proof is just about arithmetic and is easy to automate. *) |
371 | 396 |
372 crush. | 397 crush. |
398 (* end thide *) | |
373 Qed. | 399 Qed. |
374 | 400 |
375 | 401 |
376 (* begin hide *) | 402 (* begin hide *) |
377 (* In-class exercises *) | 403 (* In-class exercises *) |
398 | 424 |
399 Inductive isZero : nat -> Prop := | 425 Inductive isZero : nat -> Prop := |
400 | IsZero : isZero 0. | 426 | IsZero : isZero 0. |
401 | 427 |
402 Theorem isZero_zero : isZero 0. | 428 Theorem isZero_zero : isZero 0. |
429 (* begin thide *) | |
403 constructor. | 430 constructor. |
431 (* end thide *) | |
404 Qed. | 432 Qed. |
405 | 433 |
406 (** We can call [isZero] a %\textit{%#<i>#judgment#</i>#%}%, in the sense often used in the semantics of programming languages. Judgments are typically defined in the style of %\textit{%#<i>#natural deduction#</i>#%}%, where we write a number of %\textit{%#<i>#inference rules#</i>#%}% with premises appearing above a solid line and a conclusion appearing below the line. In this example, the sole constructor [IsZero] of [isZero] can be thought of as the single inference rule for deducing [isZero], with nothing above the line and [isZero 0] below it. The proof of [isZero_zero] demonstrates how we can apply an inference rule. | 434 (** We can call [isZero] a %\textit{%#<i>#judgment#</i>#%}%, in the sense often used in the semantics of programming languages. Judgments are typically defined in the style of %\textit{%#<i>#natural deduction#</i>#%}%, where we write a number of %\textit{%#<i>#inference rules#</i>#%}% with premises appearing above a solid line and a conclusion appearing below the line. In this example, the sole constructor [IsZero] of [isZero] can be thought of as the single inference rule for deducing [isZero], with nothing above the line and [isZero 0] below it. The proof of [isZero_zero] demonstrates how we can apply an inference rule. |
407 | 435 |
408 The definition of [isZero] differs in an important way from all of the other inductive definitions that we have seen in this and the previous chapter. Instead of writing just [Set] or [Prop] after the colon, here we write [nat -> Prop]. We saw examples of parameterized types like [list], but there the parameters appeared with names %\textit{%#<i>#before#</i>#%}% the colon. Every constructor of a parameterized inductive type must have a range type that uses the same parameter, whereas the form we use here enables us to use different arguments to the type for different constructors. | 436 The definition of [isZero] differs in an important way from all of the other inductive definitions that we have seen in this and the previous chapter. Instead of writing just [Set] or [Prop] after the colon, here we write [nat -> Prop]. We saw examples of parameterized types like [list], but there the parameters appeared with names %\textit{%#<i>#before#</i>#%}% the colon. Every constructor of a parameterized inductive type must have a range type that uses the same parameter, whereas the form we use here enables us to use different arguments to the type for different constructors. |
418 (** [eq] is the type we get behind the scenes when uses of infix [=] are expanded. We see that [eq] has both a parameter [x] that is fixed and an extra unnamed argument of the same type. It is crucial that the second argument is untied to the parameter in the type of [eq]; otherwise, we would have to prove that two values are equal even to be able to state the possibility of equality, which would more or less defeat the purpose of having an equality proposition. However, examining the type of equality's sole constructor [refl_equal], we see that we can only %\textit{%#<i>#prove#</i>#%}% equality when its two arguments are syntactically equal. This definition turns out to capture all of the basic properties of equality, and the equality-manipulating tactics that we have seen so far, like [reflexivity] and [rewrite], are implemented treating [eq] as just another inductive type with a well-chosen definition. | 446 (** [eq] is the type we get behind the scenes when uses of infix [=] are expanded. We see that [eq] has both a parameter [x] that is fixed and an extra unnamed argument of the same type. It is crucial that the second argument is untied to the parameter in the type of [eq]; otherwise, we would have to prove that two values are equal even to be able to state the possibility of equality, which would more or less defeat the purpose of having an equality proposition. However, examining the type of equality's sole constructor [refl_equal], we see that we can only %\textit{%#<i>#prove#</i>#%}% equality when its two arguments are syntactically equal. This definition turns out to capture all of the basic properties of equality, and the equality-manipulating tactics that we have seen so far, like [reflexivity] and [rewrite], are implemented treating [eq] as just another inductive type with a well-chosen definition. |
419 | 447 |
420 Returning to the example of [isZero], we can see how to make use of hypotheses that use this predicate. *) | 448 Returning to the example of [isZero], we can see how to make use of hypotheses that use this predicate. *) |
421 | 449 |
422 Theorem isZero_plus : forall n m : nat, isZero m -> n + m = n. | 450 Theorem isZero_plus : forall n m : nat, isZero m -> n + m = n. |
451 (* begin thide *) | |
423 (** We want to proceed by cases on the proof of the assumption about [isZero]. *) | 452 (** We want to proceed by cases on the proof of the assumption about [isZero]. *) |
424 destruct 1. | 453 destruct 1. |
425 (** [[ | 454 (** [[ |
426 | 455 |
427 n : nat | 456 n : nat |
430 ]] *) | 459 ]] *) |
431 | 460 |
432 (** Since [isZero] has only one constructor, we are presented with only one subgoal. The argument [m] to [isZero] is replaced with that type's argument from the single constructor [IsZero]. From this point, the proof is trivial. *) | 461 (** Since [isZero] has only one constructor, we are presented with only one subgoal. The argument [m] to [isZero] is replaced with that type's argument from the single constructor [IsZero]. From this point, the proof is trivial. *) |
433 | 462 |
434 crush. | 463 crush. |
464 (* end thide *) | |
435 Qed. | 465 Qed. |
436 | 466 |
437 (** Another example seems at first like it should admit an analogous proof, but in fact provides a demonstration of one of the most basic gotchas of Coq proving. *) | 467 (** Another example seems at first like it should admit an analogous proof, but in fact provides a demonstration of one of the most basic gotchas of Coq proving. *) |
438 | 468 |
439 Theorem isZero_contra : isZero 1 -> False. | 469 Theorem isZero_contra : isZero 1 -> False. |
470 (* begin thide *) | |
440 (** Let us try a proof by cases on the assumption, as in the last proof. *) | 471 (** Let us try a proof by cases on the assumption, as in the last proof. *) |
441 destruct 1. | 472 destruct 1. |
442 (** [[ | 473 (** [[ |
443 | 474 |
444 ============================ | 475 ============================ |
449 | 480 |
450 Why does Coq use this restriction? We will discuss the issue in detail in a future chapter, when we see the dependently-typed programming techniques that would allow us to write this proof term manually. For now, we just say that the algorithmic problem of "logically complete case analysis" is undecidable when phrased in Coq's logic. A few tactics and design patterns that we will present in this chapter suffice in almost all cases. For the current example, what we want is a tactic called [inversion], which corresponds to the concept of inversion that is frequently used with natural deduction proof systems. *) | 481 Why does Coq use this restriction? We will discuss the issue in detail in a future chapter, when we see the dependently-typed programming techniques that would allow us to write this proof term manually. For now, we just say that the algorithmic problem of "logically complete case analysis" is undecidable when phrased in Coq's logic. A few tactics and design patterns that we will present in this chapter suffice in almost all cases. For the current example, what we want is a tactic called [inversion], which corresponds to the concept of inversion that is frequently used with natural deduction proof systems. *) |
451 | 482 |
452 Undo. | 483 Undo. |
453 inversion 1. | 484 inversion 1. |
485 (* end thide *) | |
454 Qed. | 486 Qed. |
455 | 487 |
456 (** What does [inversion] do? Think of it as a version of [destruct] that does its best to take advantage of the structure of arguments to inductive types. In this case, [inversion] completed the proof immediately, because it was able to detect that we were using [isZero] with an impossible argument. | 488 (** What does [inversion] do? Think of it as a version of [destruct] that does its best to take advantage of the structure of arguments to inductive types. In this case, [inversion] completed the proof immediately, because it was able to detect that we were using [isZero] with an impossible argument. |
457 | 489 |
458 Sometimes using [destruct] when you should have used [inversion] can lead to confusing results. To illustrate, consider an alternate proof attempt for the last theorem. *) | 490 Sometimes using [destruct] when you should have used [inversion] can lead to confusing results. To illustrate, consider an alternate proof attempt for the last theorem. *) |
506 (** Think of [even] as another judgment defined by natural deduction rules. [EvenO] is a rule with nothing above the line and [even O] below the line, and [EvenSS] is a rule with [even n] above the line and [even (S (S n))] below. | 538 (** Think of [even] as another judgment defined by natural deduction rules. [EvenO] is a rule with nothing above the line and [even O] below the line, and [EvenSS] is a rule with [even n] above the line and [even (S (S n))] below. |
507 | 539 |
508 The proof techniques of the last section are easily adapted. *) | 540 The proof techniques of the last section are easily adapted. *) |
509 | 541 |
510 Theorem even_0 : even 0. | 542 Theorem even_0 : even 0. |
543 (* begin thide *) | |
511 constructor. | 544 constructor. |
545 (* end thide *) | |
512 Qed. | 546 Qed. |
513 | 547 |
514 Theorem even_4 : even 4. | 548 Theorem even_4 : even 4. |
549 (* begin thide *) | |
515 constructor; constructor; constructor. | 550 constructor; constructor; constructor. |
551 (* end thide *) | |
516 Qed. | 552 Qed. |
517 | 553 |
518 (** It is not hard to see that sequences of constructor applications like the above can get tedious. We can avoid them using Coq's hint facility. *) | 554 (** It is not hard to see that sequences of constructor applications like the above can get tedious. We can avoid them using Coq's hint facility. *) |
519 | 555 |
556 (* begin thide *) | |
520 Hint Constructors even. | 557 Hint Constructors even. |
521 | 558 |
522 Theorem even_4' : even 4. | 559 Theorem even_4' : even 4. |
523 auto. | 560 auto. |
524 Qed. | 561 Qed. |
525 | 562 |
563 (* end thide *) | |
564 | |
526 Theorem even_1_contra : even 1 -> False. | 565 Theorem even_1_contra : even 1 -> False. |
566 (* begin thide *) | |
527 inversion 1. | 567 inversion 1. |
568 (* end thide *) | |
528 Qed. | 569 Qed. |
529 | 570 |
530 Theorem even_3_contra : even 3 -> False. | 571 Theorem even_3_contra : even 3 -> False. |
572 (* begin thide *) | |
531 inversion 1. | 573 inversion 1. |
532 (** [[ | 574 (** [[ |
533 | 575 |
534 H : even 3 | 576 H : even 3 |
535 n : nat | 577 n : nat |
540 ]] *) | 582 ]] *) |
541 | 583 |
542 (** [inversion] can be a little overzealous at times, as we can see here with the introduction of the unused variable [n] and an equality hypothesis about it. For more complicated predicates, though, adding such assumptions is critical to dealing with the undecidability of general inversion. *) | 584 (** [inversion] can be a little overzealous at times, as we can see here with the introduction of the unused variable [n] and an equality hypothesis about it. For more complicated predicates, though, adding such assumptions is critical to dealing with the undecidability of general inversion. *) |
543 | 585 |
544 inversion H1. | 586 inversion H1. |
587 (* end thide *) | |
545 Qed. | 588 Qed. |
546 | 589 |
547 (** We can also do inductive proofs about [even]. *) | 590 (** We can also do inductive proofs about [even]. *) |
548 | 591 |
549 Theorem even_plus : forall n m, even n -> even m -> even (n + m). | 592 Theorem even_plus : forall n m, even n -> even m -> even (n + m). |
593 (* begin thide *) | |
550 (** It seems a reasonable first choice to proceed by induction on [n]. *) | 594 (** It seems a reasonable first choice to proceed by induction on [n]. *) |
551 induction n; crush. | 595 induction n; crush. |
552 (** [[ | 596 (** [[ |
553 | 597 |
554 n : nat | 598 n : nat |
646 | 690 |
647 (** In fact, [crush] can handle all of the details of the proof once we declare the induction strategy. *) | 691 (** In fact, [crush] can handle all of the details of the proof once we declare the induction strategy. *) |
648 | 692 |
649 Restart. | 693 Restart. |
650 induction 1; crush. | 694 induction 1; crush. |
695 (* end thide *) | |
651 Qed. | 696 Qed. |
652 | 697 |
653 (** Induction on recursive predicates has similar pitfalls to those we encountered with inversion in the last section. *) | 698 (** Induction on recursive predicates has similar pitfalls to those we encountered with inversion in the last section. *) |
654 | 699 |
655 Theorem even_contra : forall n, even (S (n + n)) -> False. | 700 Theorem even_contra : forall n, even (S (n + n)) -> False. |
701 (* begin thide *) | |
656 induction 1. | 702 induction 1. |
657 (** [[ | 703 (** [[ |
658 | 704 |
659 n : nat | 705 n : nat |
660 ============================ | 706 ============================ |
695 rewrite <- plus_n_Sm in H0. | 741 rewrite <- plus_n_Sm in H0. |
696 | 742 |
697 (** The induction hypothesis lets us complete the proof. *) | 743 (** The induction hypothesis lets us complete the proof. *) |
698 apply IHeven with n0; assumption. | 744 apply IHeven with n0; assumption. |
699 | 745 |
700 (** As usual, we can rewrite the proof to avoid referencing any locally-generated names, which makes our proof script more robust to changes in the theorem statement. *) | 746 (** As usual, we can rewrite the proof to avoid referencing any locally-generated names, which makes our proof script more readable and more robust to changes in the theorem statement. *) |
701 Restart. | 747 Restart. |
702 Hint Rewrite <- plus_n_Sm : cpdt. | 748 Hint Rewrite <- plus_n_Sm : cpdt. |
703 | 749 |
704 induction 1; crush; | 750 induction 1; crush; |
705 match goal with | 751 match goal with |
707 end; crush; eauto. | 753 end; crush; eauto. |
708 Qed. | 754 Qed. |
709 | 755 |
710 (** We write the proof in a way that avoids the use of local variable or hypothesis names, using the [match] tactic form to do pattern-matching on the goal. We use unification variables prefixed by question marks in the pattern, and we take advantage of the possibility to mention a unification variable twice in one pattern, to enforce equality between occurrences. The hint to rewrite with [plus_n_Sm] in a particular direction saves us from having to figure out the right place to apply that theorem, and we also take critical advantage of a new tactic, [eauto]. | 756 (** We write the proof in a way that avoids the use of local variable or hypothesis names, using the [match] tactic form to do pattern-matching on the goal. We use unification variables prefixed by question marks in the pattern, and we take advantage of the possibility to mention a unification variable twice in one pattern, to enforce equality between occurrences. The hint to rewrite with [plus_n_Sm] in a particular direction saves us from having to figure out the right place to apply that theorem, and we also take critical advantage of a new tactic, [eauto]. |
711 | 757 |
712 [crush] uses the tactic [intuition], which, when it runs out of tricks to try using only propositional logic, by default tries the tactic [auto], which we saw in an earlier example. [auto] attempts Prolog-style logic programming, searching through all proof trees up to a certain depth that are built only out of hints that have been registered with [Hint] commands. Compared to Prolog, [auto] places an important restriction: it never introduces new unification variables during search. That is, every time a rule is applied during proof search, all of its arguments must be deducible by studying the form of the goal. [eauto] relaxes this restriction, at the cost of possibly exponentially greater running time. In this particular case, we know that [eauto] has only a small space of proofs to search, so it makes sense to run it. It is common in effectively-automated Coq proofs to see a bag of standard tactics applied to pick off the "easy" subgoals, finishing off with [eauto] to handle the tricky parts that can benefit from ad-hoc exhaustive search. | 758 [crush] uses the tactic [intuition], which, when it runs out of tricks to try using only propositional logic, by default tries the tactic [auto], which we saw in an earlier example. [auto] attempts Prolog-style logic programming, searching through all proof trees up to a certain depth that are built only out of hints that have been registered with [Hint] commands. Compared to Prolog, [auto] places an important restriction: it never introduces new unification variables during search. That is, every time a rule is applied during proof search, all of its arguments must be deducible by studying the form of the goal. [eauto] relaxes this restriction, at the cost of possibly exponentially greater running time. In this particular case, we know that [eauto] has only a small space of proofs to search, so it makes sense to run it. It is common in effectively-automated Coq proofs to see a bag of standard tactics applied to pick off the "easy" subgoals, finishing with [eauto] to handle the tricky parts that can benefit from ad-hoc exhaustive search. |
713 | 759 |
714 The original theorem now follows trivially from our lemma. *) | 760 The original theorem now follows trivially from our lemma. *) |
715 | 761 |
716 Theorem even_contra : forall n, even (S (n + n)) -> False. | 762 Theorem even_contra : forall n, even (S (n + n)) -> False. |
717 intros; eapply even_contra'; eauto. | 763 intros; eapply even_contra'; eauto. |
739 ]] *) | 785 ]] *) |
740 | 786 |
741 (** We are out of luck here. The inductive hypothesis is trivially true, since its assumption is false. In the version of this proof that succeeded, [IHeven] had an explicit quantification over [n]. This is because the quantification of [n] %\textit{%#<i>#appeared after the thing we are inducting on#</i>#%}% in the theorem statement. In general, quantified variables and hypotheses that appear before the induction object in the theorem statement stay fixed throughout the inductive proof. Variables and hypotheses that are quantified after the induction object may be varied explicitly in uses of inductive hypotheses. | 787 (** We are out of luck here. The inductive hypothesis is trivially true, since its assumption is false. In the version of this proof that succeeded, [IHeven] had an explicit quantification over [n]. This is because the quantification of [n] %\textit{%#<i>#appeared after the thing we are inducting on#</i>#%}% in the theorem statement. In general, quantified variables and hypotheses that appear before the induction object in the theorem statement stay fixed throughout the inductive proof. Variables and hypotheses that are quantified after the induction object may be varied explicitly in uses of inductive hypotheses. |
742 | 788 |
743 Why should Coq implement [induction] this way? One answer is that it avoids burdening this basic tactic with additional heuristic smarts, but that is not the whole picture. Imagine that [induction] analyzed dependencies among variables and reordered quantifiers to preserve as much freedom as possible in later uses of inductive hypotheses. This could make the inductive hypotheses more complex, which could in turn cause particular automation machinery to fail when it would have succeeded before. In general, we want to avoid quantifiers in our proofs whenever we can, and that goal is furthered by the refactoring that the [induction] tactic forces us to do. *) | 789 Why should Coq implement [induction] this way? One answer is that it avoids burdening this basic tactic with additional heuristic smarts, but that is not the whole picture. Imagine that [induction] analyzed dependencies among variables and reordered quantifiers to preserve as much freedom as possible in later uses of inductive hypotheses. This could make the inductive hypotheses more complex, which could in turn cause particular automation machinery to fail when it would have succeeded before. In general, we want to avoid quantifiers in our proofs whenever we can, and that goal is furthered by the refactoring that the [induction] tactic forces us to do. *) |
744 | 790 (* end thide *) |
745 Abort. | 791 Abort. |
746 | 792 |
747 | 793 |
748 (* begin hide *) | 794 (* begin hide *) |
749 (* In-class exercises *) | 795 (* In-class exercises *) |