adam@394
|
1 (* Copyright (c) 2008-2012, Adam Chlipala
|
adamc@45
|
2 *
|
adamc@45
|
3 * This work is licensed under a
|
adamc@45
|
4 * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
|
adamc@45
|
5 * Unported License.
|
adamc@45
|
6 * The license text is available at:
|
adamc@45
|
7 * http://creativecommons.org/licenses/by-nc-nd/3.0/
|
adamc@45
|
8 *)
|
adamc@45
|
9
|
adamc@45
|
10 (* begin hide *)
|
adamc@45
|
11 Require Import List.
|
adamc@45
|
12
|
adam@314
|
13 Require Import CpdtTactics.
|
adamc@45
|
14
|
adamc@45
|
15 Set Implicit Arguments.
|
adam@322
|
16
|
adam@322
|
17 (* Extra definitions to get coqdoc to choose the right fonts. *)
|
adam@322
|
18
|
adam@323
|
19 (* begin thide *)
|
adam@322
|
20 Inductive unit := tt.
|
adam@322
|
21 Inductive Empty_set := .
|
adam@322
|
22 Inductive bool := true | false.
|
adam@322
|
23 Inductive sum := .
|
adam@322
|
24 Inductive prod := .
|
adam@322
|
25 Inductive and := conj.
|
adam@322
|
26 Inductive or := or_introl | or_intror.
|
adam@322
|
27 Inductive ex := ex_intro.
|
adam@322
|
28 Inductive eq := refl_equal.
|
adam@322
|
29 Reset unit.
|
adam@323
|
30 (* end thide *)
|
adamc@45
|
31 (* end hide *)
|
adamc@45
|
32
|
adamc@45
|
33 (** %\chapter{Inductive Predicates}% *)
|
adamc@45
|
34
|
adam@322
|
35 (** The so-called %\index{Curry-Howard correspondence}``%#"#Curry-Howard correspondence#"#%''~\cite{Curry,Howard}% states a formal connection between functional programs and mathematical proofs. In the last chapter, we snuck in a first introduction to this subject in Coq. Witness the close similarity between the types [unit] and [True] from the standard library: *)
|
adamc@45
|
36
|
adamc@45
|
37 Print unit.
|
adam@322
|
38 (** [[
|
adamc@209
|
39 Inductive unit : Set := tt : unit
|
adam@302
|
40 ]]
|
adam@302
|
41 *)
|
adamc@45
|
42
|
adamc@45
|
43 Print True.
|
adam@322
|
44 (** [[
|
adamc@209
|
45 Inductive True : Prop := I : True
|
adam@322
|
46 ]]
|
adam@302
|
47 *)
|
adamc@45
|
48
|
adam@350
|
49 (** Recall that [unit] is the type with only one value, and [True] is the proposition that always holds. Despite this superficial difference between the two concepts, in both cases we can use the same inductive definition mechanism. The connection goes further than this. We see that we arrive at the definition of [True] by replacing [unit] by [True], [tt] by [I], and [Set] by [Prop]. The first two of these differences are superficial changes of names, while the third difference is the crucial one for separating programs from proofs. A term [T] of type [Set] is a type of programs, and a term of type [T] is a program. A term [T] of type [Prop] is a logical proposition, and its proofs are of type [T]. Chapter 12 goes into more detail about the theoretical differences between [Prop] and [Set]. For now, we will simply follow common intuitions about what a proof is.
|
adamc@45
|
50
|
adam@398
|
51 The type [unit] has one value, [tt]. The type [True] has one proof, [I]. Why distinguish between these two types? Many people who have read about Curry-Howard in an abstract context and not put it to use in proof engineering answer that the two types in fact _should not_ be distinguished. There is a certain aesthetic appeal to this point of view, but I want to argue that it is best to treat Curry-Howard very loosely in practical proving. There are Coq-specific reasons for preferring the distinction, involving efficient compilation and avoidance of paradoxes in the presence of classical math, but I will argue that there is a more general principle that should lead us to avoid conflating programming and proving.
|
adamc@45
|
52
|
adam@401
|
53 The essence of the argument is roughly this: to an engineer, not all functions of type [A -> B] are created equal, but all proofs of a proposition [P -> Q] are. This idea is known as%\index{proof irrelevance}% _proof irrelevance_, and its formalizations in logics prevent us from distinguishing between alternate proofs of the same proposition. Proof irrelevance is compatible with, but not derivable in, Gallina. Apart from this theoretical concern, I will argue that it is most effective to do engineering with Coq by employing different techniques for programs versus proofs. Most of this book is organized around that distinction, describing how to program, by applying standard functional programming techniques in the presence of dependent types; and how to prove, by writing custom Ltac decision procedures.
|
adamc@45
|
54
|
adam@421
|
55 With that perspective in mind, this chapter is sort of a mirror image of the last chapter, introducing how to define predicates with inductive definitions. We will point out similarities in places, but much of the effective Coq user's bag of tricks is disjoint for predicates versus "datatypes." This chapter is also a covert introduction to dependent types, which are the foundation on which interesting inductive predicates are built, though we will rely on tactics to build dependently typed proof terms for us for now. A future chapter introduces more manual application of dependent types. *)
|
adamc@45
|
56
|
adamc@45
|
57
|
adamc@48
|
58 (** * Propositional Logic *)
|
adamc@45
|
59
|
adamc@45
|
60 (** Let us begin with a brief tour through the definitions of the connectives for propositional logic. We will work within a Coq section that provides us with a set of propositional variables. In Coq parlance, these are just terms of type [Prop.] *)
|
adamc@45
|
61
|
adamc@45
|
62 Section Propositional.
|
adamc@46
|
63 Variables P Q R : Prop.
|
adamc@45
|
64
|
adamc@45
|
65 (** 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.
|
adamc@45
|
66
|
adamc@45
|
67 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. *)
|
adamc@45
|
68
|
adamc@45
|
69 Theorem obvious : True.
|
adamc@55
|
70 (* begin thide *)
|
adamc@45
|
71 apply I.
|
adamc@55
|
72 (* end thide *)
|
adamc@45
|
73 Qed.
|
adamc@45
|
74
|
adam@401
|
75 (** 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:%\index{tactics!constructor}% *)
|
adamc@45
|
76
|
adamc@55
|
77 (* begin thide *)
|
adamc@45
|
78 Theorem obvious' : True.
|
adamc@45
|
79 constructor.
|
adamc@45
|
80 Qed.
|
adamc@45
|
81
|
adamc@55
|
82 (* end thide *)
|
adamc@55
|
83
|
adamc@45
|
84 (** There is also a predicate [False], which is the Curry-Howard mirror image of [Empty_set] from the last chapter. *)
|
adamc@45
|
85
|
adamc@45
|
86 Print False.
|
adam@322
|
87 (** [[
|
adamc@209
|
88 Inductive False : Prop :=
|
adamc@209
|
89
|
adamc@209
|
90 ]]
|
adamc@45
|
91
|
adamc@209
|
92 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. *)
|
adamc@45
|
93
|
adamc@45
|
94 Theorem False_imp : False -> 2 + 2 = 5.
|
adamc@55
|
95 (* begin thide *)
|
adamc@45
|
96 destruct 1.
|
adamc@55
|
97 (* end thide *)
|
adamc@45
|
98 Qed.
|
adamc@45
|
99
|
adamc@45
|
100 (** 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]. *)
|
adamc@45
|
101
|
adamc@45
|
102 Theorem arith_neq : 2 + 2 = 5 -> 9 + 9 = 835.
|
adamc@55
|
103 (* begin thide *)
|
adamc@45
|
104 intro.
|
adamc@45
|
105
|
adam@322
|
106 (** At this point, we have an inconsistent hypothesis [2 + 2 = 5], so the specific conclusion is not important. We use the %\index{tactics!elimtype}%[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]. *)
|
adamc@45
|
107
|
adamc@45
|
108 elimtype False.
|
adamc@45
|
109 (** [[
|
adamc@45
|
110 H : 2 + 2 = 5
|
adamc@45
|
111 ============================
|
adamc@45
|
112 False
|
adamc@209
|
113
|
adamc@209
|
114 ]]
|
adamc@45
|
115
|
adamc@209
|
116 For now, we will leave the details of this proof about arithmetic to [crush]. *)
|
adamc@45
|
117
|
adamc@45
|
118 crush.
|
adamc@55
|
119 (* end thide *)
|
adamc@45
|
120 Qed.
|
adamc@45
|
121
|
adamc@45
|
122 (** A related notion to [False] is logical negation. *)
|
adamc@45
|
123
|
adam@421
|
124 (* begin hide *)
|
adam@421
|
125 Definition foo := not.
|
adam@421
|
126 (* end hide *)
|
adam@421
|
127
|
adamc@45
|
128 Print not.
|
adamc@209
|
129 (** %\vspace{-.15in}% [[
|
adamc@209
|
130 not = fun A : Prop => A -> False
|
adamc@209
|
131 : Prop -> Prop
|
adamc@209
|
132
|
adamc@209
|
133 ]]
|
adamc@45
|
134
|
adam@280
|
135 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]. *)
|
adamc@45
|
136
|
adamc@45
|
137 Theorem arith_neq' : ~ (2 + 2 = 5).
|
adamc@55
|
138 (* begin thide *)
|
adamc@45
|
139 unfold not.
|
adamc@45
|
140 (** [[
|
adamc@45
|
141 ============================
|
adamc@45
|
142 2 + 2 = 5 -> False
|
adam@302
|
143 ]]
|
adam@302
|
144 *)
|
adamc@45
|
145
|
adamc@45
|
146 crush.
|
adamc@55
|
147 (* end thide *)
|
adamc@45
|
148 Qed.
|
adamc@45
|
149
|
adamc@45
|
150 (** We also have conjunction, which we introduced in the last chapter. *)
|
adamc@45
|
151
|
adamc@45
|
152 Print and.
|
adam@401
|
153 (** [[
|
adam@322
|
154 Inductive and (A : Prop) (B : Prop) : Prop := conj : A -> B -> A /\ B
|
adamc@209
|
155
|
adamc@209
|
156 ]]
|
adamc@209
|
157
|
adam@322
|
158 The interested reader can check that [and] has a Curry-Howard equivalent called %\index{Gallina terms!prod}%[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. The operator [/\] is an infix shorthand for [and]. *)
|
adamc@45
|
159
|
adamc@45
|
160 Theorem and_comm : P /\ Q -> Q /\ P.
|
adamc@209
|
161
|
adamc@55
|
162 (* begin thide *)
|
adamc@45
|
163 (** We start by case analysis on the proof of [P /\ Q]. *)
|
adamc@45
|
164
|
adamc@45
|
165 destruct 1.
|
adamc@45
|
166 (** [[
|
adamc@45
|
167 H : P
|
adamc@45
|
168 H0 : Q
|
adamc@45
|
169 ============================
|
adamc@45
|
170 Q /\ P
|
adamc@209
|
171
|
adamc@209
|
172 ]]
|
adamc@45
|
173
|
adam@322
|
174 Every proof of a conjunction provides proofs for both conjuncts, so we get a single subgoal reflecting that. We can proceed by splitting this subgoal into a case for each conjunct of [Q /\ P].%\index{tactics!split}% *)
|
adamc@45
|
175
|
adamc@45
|
176 split.
|
adam@322
|
177 (** %\vspace{.1in} \noindent 2 \coqdockw{subgoals}\vspace{-.1in}%#<tt>2 subgoals</tt>#
|
adam@322
|
178 [[
|
adamc@45
|
179
|
adamc@45
|
180 H : P
|
adamc@45
|
181 H0 : Q
|
adamc@45
|
182 ============================
|
adamc@45
|
183 Q
|
adam@322
|
184 ]]
|
adam@322
|
185 %\noindent \coqdockw{subgoal} 2 \coqdockw{is}:%#<tt>subgoal 2 is</tt>#
|
adam@322
|
186 [[
|
adam@322
|
187 P
|
adamc@209
|
188
|
adamc@209
|
189 ]]
|
adamc@45
|
190
|
adam@322
|
191 In each case, the conclusion is among our hypotheses, so the %\index{tactics!assumption}%[assumption] tactic finishes the process. *)
|
adamc@45
|
192
|
adamc@45
|
193 assumption.
|
adamc@45
|
194 assumption.
|
adamc@55
|
195 (* end thide *)
|
adamc@45
|
196 Qed.
|
adamc@45
|
197
|
adam@322
|
198 (** Coq disjunction is called %\index{Gallina terms!or}%[or] and abbreviated with the infix operator [\/]. *)
|
adamc@45
|
199
|
adamc@45
|
200 Print or.
|
adam@401
|
201 (** [[
|
adamc@209
|
202 Inductive or (A : Prop) (B : Prop) : Prop :=
|
adamc@209
|
203 or_introl : A -> A \/ B | or_intror : B -> A \/ B
|
adamc@209
|
204
|
adamc@209
|
205 ]]
|
adamc@45
|
206
|
adam@322
|
207 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 %\index{Gallina terms!sum}%[sum] type. We can demonstrate the main tactics here with another proof of commutativity. *)
|
adamc@45
|
208
|
adamc@45
|
209 Theorem or_comm : P \/ Q -> Q \/ P.
|
adamc@55
|
210
|
adamc@55
|
211 (* begin thide *)
|
adamc@45
|
212 (** As in the proof for [and], we begin with case analysis, though this time we are met by two cases instead of one. *)
|
adamc@209
|
213
|
adamc@45
|
214 destruct 1.
|
adam@322
|
215 (** %\vspace{.1in} \noindent 2 \coqdockw{subgoals}\vspace{-.1in}%#<tt>2 subgoals</tt>#
|
adam@322
|
216 [[
|
adamc@45
|
217
|
adamc@45
|
218 H : P
|
adamc@45
|
219 ============================
|
adamc@45
|
220 Q \/ P
|
adam@322
|
221 ]]
|
adam@322
|
222 %\noindent \coqdockw{subgoal} 2 \coqdockw{is}:%#<tt>subgoal 2 is</tt>#
|
adam@322
|
223 [[
|
adamc@45
|
224 Q \/ P
|
adamc@209
|
225
|
adamc@209
|
226 ]]
|
adamc@45
|
227
|
adam@401
|
228 We can see that, in the first subgoal, we want to prove the disjunction by proving its second disjunct. The %\index{tactics!right}%[right] tactic telegraphs this intent. *)
|
adam@322
|
229
|
adamc@45
|
230 right; assumption.
|
adamc@45
|
231
|
adam@322
|
232 (** The second subgoal has a symmetric proof.%\index{tactics!left}%
|
adamc@45
|
233
|
adamc@45
|
234 [[
|
adamc@45
|
235 1 subgoal
|
adamc@45
|
236
|
adamc@45
|
237 H : Q
|
adamc@45
|
238 ============================
|
adamc@45
|
239 Q \/ P
|
adam@302
|
240 ]]
|
adam@302
|
241 *)
|
adamc@45
|
242
|
adamc@45
|
243 left; assumption.
|
adam@322
|
244
|
adamc@55
|
245 (* end thide *)
|
adamc@45
|
246 Qed.
|
adamc@45
|
247
|
adamc@46
|
248
|
adamc@46
|
249 (* begin hide *)
|
adamc@46
|
250 (* In-class exercises *)
|
adamc@46
|
251
|
adamc@46
|
252 Theorem contra : P -> ~P -> R.
|
adamc@52
|
253 (* begin thide *)
|
adamc@52
|
254 unfold not.
|
adamc@52
|
255 intros.
|
adamc@52
|
256 elimtype False.
|
adamc@52
|
257 apply H0.
|
adamc@52
|
258 assumption.
|
adamc@52
|
259 (* end thide *)
|
adamc@46
|
260 Admitted.
|
adamc@46
|
261
|
adamc@46
|
262 Theorem and_assoc : (P /\ Q) /\ R -> P /\ (Q /\ R).
|
adamc@52
|
263 (* begin thide *)
|
adamc@52
|
264 intros.
|
adamc@52
|
265 destruct H.
|
adamc@52
|
266 destruct H.
|
adamc@52
|
267 split.
|
adamc@52
|
268 assumption.
|
adamc@52
|
269 split.
|
adamc@52
|
270 assumption.
|
adamc@52
|
271 assumption.
|
adamc@52
|
272 (* end thide *)
|
adamc@46
|
273 Admitted.
|
adamc@46
|
274
|
adamc@46
|
275 Theorem or_assoc : (P \/ Q) \/ R -> P \/ (Q \/ R).
|
adamc@52
|
276 (* begin thide *)
|
adamc@52
|
277 intros.
|
adamc@52
|
278 destruct H.
|
adamc@52
|
279 destruct H.
|
adamc@52
|
280 left.
|
adamc@52
|
281 assumption.
|
adamc@52
|
282 right.
|
adamc@52
|
283 left.
|
adamc@52
|
284 assumption.
|
adamc@52
|
285 right.
|
adamc@52
|
286 right.
|
adamc@52
|
287 assumption.
|
adamc@52
|
288 (* end thide *)
|
adamc@46
|
289 Admitted.
|
adamc@46
|
290
|
adamc@46
|
291 (* end hide *)
|
adamc@46
|
292
|
adamc@46
|
293
|
adam@421
|
294 (** 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 %\index{tactics!tauto}%[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. *)
|
adamc@46
|
295
|
adamc@46
|
296 Theorem or_comm' : P \/ Q -> Q \/ P.
|
adamc@55
|
297 (* begin thide *)
|
adamc@46
|
298 tauto.
|
adamc@55
|
299 (* end thide *)
|
adamc@46
|
300 Qed.
|
adamc@46
|
301
|
adam@401
|
302 (** Sometimes propositional reasoning forms important plumbing for the proof of a theorem, but we still need to apply some other smarts about, say, arithmetic. The tactic %\index{tactics!intuition}%[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. *)
|
adamc@46
|
303
|
adamc@46
|
304 Theorem arith_comm : forall ls1 ls2 : list nat,
|
adamc@46
|
305 length ls1 = length ls2 \/ length ls1 + length ls2 = 6
|
adamc@46
|
306 -> length (ls1 ++ ls2) = 6 \/ length ls1 = length ls2.
|
adamc@55
|
307 (* begin thide *)
|
adamc@46
|
308 intuition.
|
adamc@46
|
309
|
adamc@46
|
310 (** 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. *)
|
adamc@46
|
311
|
adamc@46
|
312 (** [[
|
adamc@46
|
313 ls1 : list nat
|
adamc@46
|
314 ls2 : list nat
|
adamc@46
|
315 H0 : length ls1 + length ls2 = 6
|
adamc@46
|
316 ============================
|
adamc@46
|
317 length (ls1 ++ ls2) = 6 \/ length ls1 = length ls2
|
adamc@209
|
318
|
adamc@209
|
319 ]]
|
adamc@46
|
320
|
adamc@209
|
321 We can see that we need a theorem about lengths of concatenated lists, which we proved last chapter and is also in the standard library. *)
|
adamc@46
|
322
|
adamc@46
|
323 rewrite app_length.
|
adamc@46
|
324 (** [[
|
adamc@46
|
325 ls1 : list nat
|
adamc@46
|
326 ls2 : list nat
|
adamc@46
|
327 H0 : length ls1 + length ls2 = 6
|
adamc@46
|
328 ============================
|
adamc@46
|
329 length ls1 + length ls2 = 6 \/ length ls1 = length ls2
|
adamc@209
|
330
|
adamc@209
|
331 ]]
|
adamc@46
|
332
|
adamc@209
|
333 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. *)
|
adamc@46
|
334
|
adamc@46
|
335 tauto.
|
adamc@55
|
336 (* end thide *)
|
adamc@46
|
337 Qed.
|
adamc@46
|
338
|
adam@322
|
339 (** The [intuition] tactic 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. *)
|
adamc@46
|
340
|
adamc@55
|
341 (* begin thide *)
|
adamc@46
|
342 Theorem arith_comm' : forall ls1 ls2 : list nat,
|
adamc@46
|
343 length ls1 = length ls2 \/ length ls1 + length ls2 = 6
|
adamc@46
|
344 -> length (ls1 ++ ls2) = 6 \/ length ls1 = length ls2.
|
adam@375
|
345 Hint Rewrite app_length.
|
adamc@46
|
346
|
adamc@46
|
347 crush.
|
adamc@46
|
348 Qed.
|
adamc@55
|
349 (* end thide *)
|
adamc@46
|
350
|
adamc@45
|
351 End Propositional.
|
adamc@45
|
352
|
adam@322
|
353 (** Ending the section here has the same effect as always. Each of our propositional theorems becomes universally quantified over the propositional variables that we used. *)
|
adam@322
|
354
|
adamc@46
|
355
|
adamc@47
|
356 (** * What Does It Mean to Be Constructive? *)
|
adamc@46
|
357
|
adam@401
|
358 (** One potential point of confusion in the presentation so far is the distinction between [bool] and [Prop]. The datatype [bool] is built from two values [true] and [false], while [Prop] is a more primitive type that includes among its members [True] and [False]. Why not collapse these two concepts into one, and why must there be more than two states of mathematical truth?
|
adamc@46
|
359
|
adam@421
|
360 The answer comes from the fact that Coq implements%\index{constructive logic}% _constructive_ or%\index{intuitionistic logic|see{constructive logic}}% _intuitionistic_ logic, in contrast to the%\index{classical logic}% _classical_ logic that you may be more familiar with. In constructive logic, classical tautologies like [~ ~ P -> P] and [P \/ ~ P] do not always hold. In general, we can only prove these tautologies when [P] is%\index{decidability}% _decidable_, in the sense of %\index{computability|see{decidability}}%computability theory. The Curry-Howard encoding that Coq uses for [or] allows us to extract either a proof of [P] or a proof of [~ P] from any proof of [P \/ ~ P]. Since our proofs are just functional programs which we can run, a general %\index{law of the excluded middle}%law of the excluded middle would give us a decision procedure for the halting problem, where the instantiations of [P] would be formulas like "this particular Turing machine halts."
|
adamc@47
|
361
|
adam@421
|
362 Hence the distinction between [bool] and [Prop]. Programs of type [bool] are computational by construction; we can always run them to determine their results. Many [Prop]s are undecidable, and so we can write more expressive formulas with [Prop]s than with [bool]s, but the inevitable consequence is that we cannot simply "run a [Prop] to determine its truth."
|
adamc@47
|
363
|
adam@401
|
364 Constructive logic lets us define all of the logical connectives in an aesthetically appealing way, with orthogonal inductive definitions. That is, each connective is defined independently using a simple, shared mechanism. Constructivity also enables a trick called%\index{program extraction}% _program extraction_, where we write programs by phrasing them as theorems to be proved. Since our proofs are just functional programs, we can extract executable programs from our final proofs, which we could not do as naturally with classical proofs.
|
adamc@47
|
365
|
adamc@47
|
366 We will see more about Coq's program extraction facility in a later chapter. However, I think it is worth interjecting another warning at this point, following up on the prior warning about taking the Curry-Howard correspondence too literally. It is possible to write programs by theorem-proving methods in Coq, but hardly anyone does it. It is almost always most useful to maintain the distinction between programs and proofs. If you write a program by proving a theorem, you are likely to run into algorithmic inefficiencies that you introduced in your proof to make it easier to prove. It is a shame to have to worry about such situations while proving tricky theorems, and it is a happy state of affairs that you almost certainly will not need to, with the ideal of extracting programs from proofs being confined mostly to theoretical studies. *)
|
adamc@48
|
367
|
adamc@48
|
368
|
adamc@48
|
369 (** * First-Order Logic *)
|
adamc@48
|
370
|
adam@421
|
371 (** The %\index{Gallina terms!forall}%[forall] connective of first-order logic, which we have seen in many examples so far, is built into Coq. Getting ahead of ourselves a bit, we can see it as the dependent function type constructor. In fact, implication and universal quantification are just different syntactic shorthands for the same Coq mechanism. A formula [P -> Q] is equivalent to [forall x : P, Q], where [x] does not appear in [Q]. That is, the "real" type of the implication says "for every proof of [P], there exists a proof of [Q]."
|
adamc@48
|
372
|
adam@322
|
373 %\index{existential quantification}\index{Gallina terms!exists}\index{Gallina terms!ex}%Existential quantification is defined in the standard library. *)
|
adamc@48
|
374
|
adam@322
|
375 Print ex.
|
adam@401
|
376 (** [[
|
adamc@209
|
377 Inductive ex (A : Type) (P : A -> Prop) : Prop :=
|
adamc@209
|
378 ex_intro : forall x : A, P x -> ex P
|
adamc@209
|
379
|
adamc@209
|
380 ]]
|
adamc@48
|
381
|
adam@322
|
382 The family [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. We use the equality operator [=], which, depending on the settings in which they learned logic, different people will say either is or is not part of first-order logic. For our purposes, it is. *)
|
adamc@48
|
383
|
adamc@48
|
384 Theorem exist1 : exists x : nat, x + 1 = 2.
|
adamc@55
|
385 (* begin thide *)
|
adamc@67
|
386 (** remove printing exists *)
|
adam@421
|
387 (** We can start this proof with a tactic %\index{tactics!exists}%[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.) *)
|
adamc@209
|
388
|
adamc@48
|
389 exists 1.
|
adamc@48
|
390
|
adamc@209
|
391 (** The conclusion is replaced with a version using the existential witness that we announced.
|
adamc@48
|
392
|
adamc@209
|
393 [[
|
adamc@48
|
394 ============================
|
adamc@48
|
395 1 + 1 = 2
|
adam@302
|
396 ]]
|
adam@302
|
397 *)
|
adamc@48
|
398
|
adamc@48
|
399 reflexivity.
|
adamc@55
|
400 (* end thide *)
|
adamc@48
|
401 Qed.
|
adamc@48
|
402
|
adamc@48
|
403 (** printing exists $\exists$ *)
|
adamc@48
|
404
|
adamc@48
|
405 (** We can also use tactics to reason about existential hypotheses. *)
|
adamc@48
|
406
|
adamc@48
|
407 Theorem exist2 : forall n m : nat, (exists x : nat, n + x = m) -> n <= m.
|
adamc@55
|
408 (* begin thide *)
|
adamc@48
|
409 (** We start by case analysis on the proof of the existential fact. *)
|
adamc@209
|
410
|
adamc@48
|
411 destruct 1.
|
adamc@48
|
412 (** [[
|
adamc@48
|
413 n : nat
|
adamc@48
|
414 m : nat
|
adamc@48
|
415 x : nat
|
adamc@48
|
416 H : n + x = m
|
adamc@48
|
417 ============================
|
adamc@48
|
418 n <= m
|
adamc@209
|
419
|
adamc@209
|
420 ]]
|
adamc@48
|
421
|
adamc@209
|
422 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. *)
|
adamc@48
|
423
|
adamc@48
|
424 crush.
|
adamc@55
|
425 (* end thide *)
|
adamc@48
|
426 Qed.
|
adamc@48
|
427
|
adamc@48
|
428
|
adamc@48
|
429 (* begin hide *)
|
adamc@48
|
430 (* In-class exercises *)
|
adamc@48
|
431
|
adamc@48
|
432 Theorem forall_exists_commute : forall (A B : Type) (P : A -> B -> Prop),
|
adamc@48
|
433 (exists x : A, forall y : B, P x y) -> (forall y : B, exists x : A, P x y).
|
adamc@52
|
434 (* begin thide *)
|
adamc@52
|
435 intros.
|
adamc@52
|
436 destruct H.
|
adamc@52
|
437 exists x.
|
adamc@52
|
438 apply H.
|
adamc@52
|
439 (* end thide *)
|
adamc@48
|
440 Admitted.
|
adamc@48
|
441
|
adamc@48
|
442 (* end hide *)
|
adamc@48
|
443
|
adamc@48
|
444
|
adam@322
|
445 (** The tactic [intuition] has a first-order cousin called %\index{tactics!firstorder}%[firstorder], which proves many formulas when only first-order reasoning is needed, and it tries to perform first-order simplifications in any case. First-order reasoning is much harder than propositional reasoning, so [firstorder] is much more likely than [intuition] to get stuck in a way that makes it run for long enough to be useless. *)
|
adamc@49
|
446
|
adamc@49
|
447
|
adamc@49
|
448 (** * Predicates with Implicit Equality *)
|
adamc@49
|
449
|
adamc@49
|
450 (** We start our exploration of a more complicated class of predicates with a simple example: an alternative way of characterizing when a natural number is zero. *)
|
adamc@49
|
451
|
adamc@49
|
452 Inductive isZero : nat -> Prop :=
|
adamc@49
|
453 | IsZero : isZero 0.
|
adamc@49
|
454
|
adamc@49
|
455 Theorem isZero_zero : isZero 0.
|
adamc@55
|
456 (* begin thide *)
|
adamc@49
|
457 constructor.
|
adamc@55
|
458 (* end thide *)
|
adamc@49
|
459 Qed.
|
adamc@49
|
460
|
adam@401
|
461 (** We can call [isZero] a%\index{judgment}% _judgment_, in the sense often used in the semantics of programming languages. Judgments are typically defined in the style of%\index{natural deduction}% _natural deduction_, where we write a number of%\index{inference rules}% _inference rules_ 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.
|
adamc@49
|
462
|
adam@398
|
463 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 _before_ 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.
|
adamc@49
|
464
|
adam@322
|
465 For instance, our definition [isZero] makes the predicate provable only when the argument is [0]. We can see that the concept of equality is somehow implicit in the inductive definition mechanism. The way this is accomplished is similar to the way that logic variables are used in %\index{Prolog}%Prolog, and it is a very powerful mechanism that forms a foundation for formalizing all of mathematics. In fact, though it is natural to think of inductive types as folding in the functionality of equality, in Coq, the true situation is reversed, with equality defined as just another inductive type!%\index{Gallina terms!eq}\index{Gallina terms!refl\_equal}% *)
|
adamc@49
|
466
|
adamc@49
|
467 Print eq.
|
adam@401
|
468 (** [[
|
adamc@209
|
469 Inductive eq (A : Type) (x : A) : A -> Prop := refl_equal : x = x
|
adamc@209
|
470
|
adamc@209
|
471 ]]
|
adamc@49
|
472
|
adam@398
|
473 Behind the scenes, uses of infix [=] are expanded to instances of [eq]. We see that [eq] has both a parameter [x] that is fixed and an extra unnamed argument of the same type. The type of [eq] allows us to state any equalities, even those that are provably false. However, examining the type of equality's sole constructor [refl_equal], we see that we can only _prove_ 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. Another way of stating that definition is: equality is defined as the least reflexive relation.
|
adamc@49
|
474
|
adam@322
|
475 Returning to the example of [isZero], we can see how to work with hypotheses that use this predicate. *)
|
adamc@49
|
476
|
adamc@49
|
477 Theorem isZero_plus : forall n m : nat, isZero m -> n + m = n.
|
adamc@55
|
478 (* begin thide *)
|
adamc@49
|
479 (** We want to proceed by cases on the proof of the assumption about [isZero]. *)
|
adamc@209
|
480
|
adamc@49
|
481 destruct 1.
|
adamc@49
|
482 (** [[
|
adamc@49
|
483 n : nat
|
adamc@49
|
484 ============================
|
adamc@49
|
485 n + 0 = n
|
adamc@209
|
486
|
adamc@209
|
487 ]]
|
adamc@49
|
488
|
adamc@209
|
489 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. *)
|
adamc@49
|
490
|
adamc@49
|
491 crush.
|
adamc@55
|
492 (* end thide *)
|
adamc@49
|
493 Qed.
|
adamc@49
|
494
|
adamc@49
|
495 (** 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. *)
|
adamc@49
|
496
|
adamc@49
|
497 Theorem isZero_contra : isZero 1 -> False.
|
adamc@55
|
498 (* begin thide *)
|
adamc@49
|
499 (** Let us try a proof by cases on the assumption, as in the last proof. *)
|
adamc@209
|
500
|
adamc@49
|
501 destruct 1.
|
adamc@49
|
502 (** [[
|
adamc@49
|
503 ============================
|
adamc@49
|
504 False
|
adamc@209
|
505
|
adamc@209
|
506 ]]
|
adamc@49
|
507
|
adamc@209
|
508 It seems that case analysis has not helped us much at all! Our sole hypothesis disappears, leaving us, if anything, worse off than we were before. What went wrong? We have met an important restriction in tactics like [destruct] and [induction] when applied to types with arguments. If the arguments are not already free variables, they will be replaced by new free variables internally before doing the case analysis or induction. Since the argument [1] to [isZero] is replaced by a fresh variable, we lose the crucial fact that it is not equal to [0].
|
adamc@49
|
509
|
adam@421
|
510 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 %\index{tactics!inversion}%[inversion], which corresponds to the concept of inversion that is frequently used with natural deduction proof systems. *)
|
adamc@49
|
511
|
adamc@49
|
512 Undo.
|
adamc@49
|
513 inversion 1.
|
adamc@55
|
514 (* end thide *)
|
adamc@49
|
515 Qed.
|
adamc@49
|
516
|
adamc@49
|
517 (** 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.
|
adamc@49
|
518
|
adamc@49
|
519 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. *)
|
adamc@49
|
520
|
adamc@49
|
521 Theorem isZero_contra' : isZero 1 -> 2 + 2 = 5.
|
adamc@49
|
522 destruct 1.
|
adamc@49
|
523 (** [[
|
adamc@49
|
524 ============================
|
adamc@49
|
525 1 + 1 = 4
|
adamc@209
|
526
|
adamc@209
|
527 ]]
|
adamc@49
|
528
|
adam@280
|
529 What on earth happened here? Internally, [destruct] replaced [1] with a fresh variable, and, trying to be helpful, it also replaced the occurrence of [1] within the unary representation of each number in the goal. This has the net effect of decrementing each of these numbers. *)
|
adamc@209
|
530
|
adamc@49
|
531 Abort.
|
adamc@49
|
532
|
adam@280
|
533 (** To see more clearly what is happening, we can consider the type of [isZero]'s induction principle. *)
|
adam@280
|
534
|
adam@280
|
535 Check isZero_ind.
|
adam@280
|
536 (** %\vspace{-.15in}% [[
|
adam@280
|
537 isZero_ind
|
adam@280
|
538 : forall P : nat -> Prop, P 0 -> forall n : nat, isZero n -> P n
|
adam@280
|
539
|
adam@280
|
540 ]]
|
adam@280
|
541
|
adam@401
|
542 In our last proof script, [destruct] chose to instantiate [P] as [fun n => S n + S n = S (S (S (S n)))]. You can verify for yourself that this specialization of the principle applies to the goal and that the hypothesis [P 0] then matches the subgoal we saw generated. If you are doing a proof and encounter a strange transmutation like this, there is a good chance that you should go back and replace a use of [destruct] with [inversion]. *)
|
adam@280
|
543
|
adamc@49
|
544
|
adamc@49
|
545 (* begin hide *)
|
adamc@49
|
546 (* In-class exercises *)
|
adamc@49
|
547
|
adamc@49
|
548 (* EX: Define an inductive type capturing when a list has exactly two elements. Prove that your predicate does not hold of the empty list, and prove that, whenever it holds of a list, the length of that list is two. *)
|
adamc@49
|
549
|
adamc@52
|
550 (* begin thide *)
|
adamc@52
|
551 Section twoEls.
|
adamc@52
|
552 Variable A : Type.
|
adamc@52
|
553
|
adamc@52
|
554 Inductive twoEls : list A -> Prop :=
|
adamc@52
|
555 | TwoEls : forall x y, twoEls (x :: y :: nil).
|
adamc@52
|
556
|
adamc@52
|
557 Theorem twoEls_nil : twoEls nil -> False.
|
adamc@52
|
558 inversion 1.
|
adamc@52
|
559 Qed.
|
adamc@52
|
560
|
adamc@52
|
561 Theorem twoEls_two : forall ls, twoEls ls -> length ls = 2.
|
adamc@52
|
562 inversion 1.
|
adamc@52
|
563 reflexivity.
|
adamc@52
|
564 Qed.
|
adamc@52
|
565 End twoEls.
|
adamc@52
|
566 (* end thide *)
|
adamc@52
|
567
|
adamc@49
|
568 (* end hide *)
|
adamc@49
|
569
|
adamc@50
|
570
|
adamc@50
|
571 (** * Recursive Predicates *)
|
adamc@50
|
572
|
adamc@50
|
573 (** We have already seen all of the ingredients we need to build interesting recursive predicates, like this predicate capturing even-ness. *)
|
adamc@50
|
574
|
adamc@50
|
575 Inductive even : nat -> Prop :=
|
adamc@50
|
576 | EvenO : even O
|
adamc@50
|
577 | EvenSS : forall n, even n -> even (S (S n)).
|
adamc@50
|
578
|
adam@401
|
579 (** Think of [even] as another judgment defined by natural deduction rules. The rule [EvenO] has 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.
|
adamc@50
|
580
|
adamc@50
|
581 The proof techniques of the last section are easily adapted. *)
|
adamc@50
|
582
|
adamc@50
|
583 Theorem even_0 : even 0.
|
adamc@55
|
584 (* begin thide *)
|
adamc@50
|
585 constructor.
|
adamc@55
|
586 (* end thide *)
|
adamc@50
|
587 Qed.
|
adamc@50
|
588
|
adamc@50
|
589 Theorem even_4 : even 4.
|
adamc@55
|
590 (* begin thide *)
|
adamc@50
|
591 constructor; constructor; constructor.
|
adamc@55
|
592 (* end thide *)
|
adamc@50
|
593 Qed.
|
adamc@50
|
594
|
adam@375
|
595 (** 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, with a new [Hint] variant that asks to consider all constructors of an inductive type during proof search. The tactic %\index{tactics!auto}%[auto] performs exhaustive proof search up to a fixed depth, considering only the proof steps we have registered as hints. *)
|
adamc@50
|
596
|
adamc@55
|
597 (* begin thide *)
|
adamc@50
|
598 Hint Constructors even.
|
adamc@50
|
599
|
adamc@50
|
600 Theorem even_4' : even 4.
|
adamc@50
|
601 auto.
|
adamc@50
|
602 Qed.
|
adamc@50
|
603
|
adamc@55
|
604 (* end thide *)
|
adamc@55
|
605
|
adam@322
|
606 (** We may also use [inversion] with [even]. *)
|
adam@322
|
607
|
adamc@50
|
608 Theorem even_1_contra : even 1 -> False.
|
adamc@55
|
609 (* begin thide *)
|
adamc@50
|
610 inversion 1.
|
adamc@55
|
611 (* end thide *)
|
adamc@50
|
612 Qed.
|
adamc@50
|
613
|
adamc@50
|
614 Theorem even_3_contra : even 3 -> False.
|
adamc@55
|
615 (* begin thide *)
|
adamc@50
|
616 inversion 1.
|
adamc@50
|
617 (** [[
|
adamc@50
|
618 H : even 3
|
adamc@50
|
619 n : nat
|
adamc@50
|
620 H1 : even 1
|
adamc@50
|
621 H0 : n = 1
|
adamc@50
|
622 ============================
|
adamc@50
|
623 False
|
adamc@209
|
624
|
adamc@209
|
625 ]]
|
adamc@50
|
626
|
adam@322
|
627 The [inversion] tactic 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. More complex inductive definitions and theorems can cause [inversion] to generate equalities where neither side is a variable. *)
|
adamc@50
|
628
|
adamc@50
|
629 inversion H1.
|
adamc@55
|
630 (* end thide *)
|
adamc@50
|
631 Qed.
|
adamc@50
|
632
|
adamc@50
|
633 (** We can also do inductive proofs about [even]. *)
|
adamc@50
|
634
|
adamc@50
|
635 Theorem even_plus : forall n m, even n -> even m -> even (n + m).
|
adamc@55
|
636 (* begin thide *)
|
adamc@50
|
637 (** It seems a reasonable first choice to proceed by induction on [n]. *)
|
adamc@209
|
638
|
adamc@50
|
639 induction n; crush.
|
adamc@50
|
640 (** [[
|
adamc@50
|
641 n : nat
|
adamc@50
|
642 IHn : forall m : nat, even n -> even m -> even (n + m)
|
adamc@50
|
643 m : nat
|
adamc@50
|
644 H : even (S n)
|
adamc@50
|
645 H0 : even m
|
adamc@50
|
646 ============================
|
adamc@50
|
647 even (S (n + m))
|
adamc@209
|
648
|
adamc@209
|
649 ]]
|
adamc@50
|
650
|
adamc@209
|
651 We will need to use the hypotheses [H] and [H0] somehow. The most natural choice is to invert [H]. *)
|
adamc@50
|
652
|
adamc@50
|
653 inversion H.
|
adamc@50
|
654 (** [[
|
adamc@50
|
655 n : nat
|
adamc@50
|
656 IHn : forall m : nat, even n -> even m -> even (n + m)
|
adamc@50
|
657 m : nat
|
adamc@50
|
658 H : even (S n)
|
adamc@50
|
659 H0 : even m
|
adamc@50
|
660 n0 : nat
|
adamc@50
|
661 H2 : even n0
|
adamc@50
|
662 H1 : S n0 = n
|
adamc@50
|
663 ============================
|
adamc@50
|
664 even (S (S n0 + m))
|
adamc@209
|
665
|
adamc@209
|
666 ]]
|
adamc@50
|
667
|
adamc@209
|
668 Simplifying the conclusion brings us to a point where we can apply a constructor. *)
|
adamc@209
|
669
|
adamc@50
|
670 simpl.
|
adamc@50
|
671 (** [[
|
adamc@50
|
672 ============================
|
adamc@50
|
673 even (S (S (n0 + m)))
|
adam@302
|
674 ]]
|
adam@302
|
675 *)
|
adamc@50
|
676
|
adamc@50
|
677 constructor.
|
adam@322
|
678
|
adam@401
|
679 (** [[
|
adamc@50
|
680 ============================
|
adamc@50
|
681 even (n0 + m)
|
adamc@209
|
682
|
adamc@209
|
683 ]]
|
adamc@50
|
684
|
adamc@209
|
685 At this point, we would like to apply the inductive hypothesis, which is:
|
adamc@209
|
686
|
adamc@209
|
687 [[
|
adamc@50
|
688 IHn : forall m : nat, even n -> even m -> even (n + m)
|
adamc@209
|
689 ]]
|
adamc@50
|
690
|
adam@421
|
691 Unfortunately, the goal mentions [n0] where it would need to mention [n] to match [IHn]. We could keep looking for a way to finish this proof from here, but it turns out that we can make our lives much easier by changing our basic strategy. Instead of inducting on the structure of [n], we should induct _on the structure of one of the [even] proofs_. This technique is commonly called%\index{rule induction}% _rule induction_ in programming language semantics. In the setting of Coq, we have already seen how predicates are defined using the same inductive type mechanism as datatypes, so the fundamental unity of rule induction with "normal" induction is apparent.
|
adamc@50
|
692
|
adam@322
|
693 Recall that tactics like [induction] and [destruct] may be passed numbers to refer to unnamed lefthand sides of implications in the conclusion, where the argument [n] refers to the [n]th such hypothesis. *)
|
adam@322
|
694
|
adamc@50
|
695 Restart.
|
adamc@50
|
696
|
adamc@50
|
697 induction 1.
|
adamc@50
|
698 (** [[
|
adamc@50
|
699 m : nat
|
adamc@50
|
700 ============================
|
adamc@50
|
701 even m -> even (0 + m)
|
adam@322
|
702 ]]
|
adamc@50
|
703
|
adam@322
|
704 %\noindent \coqdockw{subgoal} 2 \coqdockw{is}:%#<tt>subgoal 2 is</tt>#
|
adam@322
|
705 [[
|
adamc@50
|
706 even m -> even (S (S n) + m)
|
adamc@209
|
707
|
adamc@209
|
708 ]]
|
adamc@50
|
709
|
adamc@209
|
710 The first case is easily discharged by [crush], based on the hint we added earlier to try the constructors of [even]. *)
|
adamc@50
|
711
|
adamc@50
|
712 crush.
|
adamc@50
|
713
|
adamc@50
|
714 (** Now we focus on the second case: *)
|
adamc@209
|
715
|
adamc@50
|
716 intro.
|
adamc@50
|
717 (** [[
|
adamc@50
|
718 m : nat
|
adamc@50
|
719 n : nat
|
adamc@50
|
720 H : even n
|
adamc@50
|
721 IHeven : even m -> even (n + m)
|
adamc@50
|
722 H0 : even m
|
adamc@50
|
723 ============================
|
adamc@50
|
724 even (S (S n) + m)
|
adamc@209
|
725
|
adamc@209
|
726 ]]
|
adamc@50
|
727
|
adamc@209
|
728 We simplify and apply a constructor, as in our last proof attempt. *)
|
adamc@50
|
729
|
adamc@50
|
730 simpl; constructor.
|
adam@322
|
731
|
adam@401
|
732 (** [[
|
adamc@50
|
733 ============================
|
adamc@50
|
734 even (n + m)
|
adamc@209
|
735
|
adamc@209
|
736 ]]
|
adamc@50
|
737
|
adamc@209
|
738 Now we have an exact match with our inductive hypothesis, and the remainder of the proof is trivial. *)
|
adamc@50
|
739
|
adamc@50
|
740 apply IHeven; assumption.
|
adamc@50
|
741
|
adamc@50
|
742 (** In fact, [crush] can handle all of the details of the proof once we declare the induction strategy. *)
|
adamc@50
|
743
|
adamc@50
|
744 Restart.
|
adam@322
|
745
|
adamc@50
|
746 induction 1; crush.
|
adamc@55
|
747 (* end thide *)
|
adamc@50
|
748 Qed.
|
adamc@50
|
749
|
adamc@50
|
750 (** Induction on recursive predicates has similar pitfalls to those we encountered with inversion in the last section. *)
|
adamc@50
|
751
|
adamc@50
|
752 Theorem even_contra : forall n, even (S (n + n)) -> False.
|
adamc@55
|
753 (* begin thide *)
|
adamc@50
|
754 induction 1.
|
adamc@50
|
755 (** [[
|
adamc@50
|
756 n : nat
|
adamc@50
|
757 ============================
|
adamc@50
|
758 False
|
adam@322
|
759 ]]
|
adamc@50
|
760
|
adam@322
|
761 %\noindent \coqdockw{subgoal} 2 \coqdockw{is}:%#<tt>subgoal 2 is</tt>#
|
adam@322
|
762 [[
|
adamc@50
|
763 False
|
adamc@209
|
764
|
adamc@209
|
765 ]]
|
adamc@50
|
766
|
adam@280
|
767 We are already sunk trying to prove the first subgoal, since the argument to [even] was replaced by a fresh variable internally. This time, we find it easier to prove this theorem by way of a lemma. Instead of trusting [induction] to replace expressions with fresh variables, we do it ourselves, explicitly adding the appropriate equalities as new assumptions. *)
|
adamc@209
|
768
|
adamc@50
|
769 Abort.
|
adamc@50
|
770
|
adamc@50
|
771 Lemma even_contra' : forall n', even n' -> forall n, n' = S (n + n) -> False.
|
adamc@50
|
772 induction 1; crush.
|
adamc@50
|
773
|
adamc@54
|
774 (** At this point, it is useful to consider all cases of [n] and [n0] being zero or nonzero. Only one of these cases has any trickiness to it. *)
|
adamc@209
|
775
|
adamc@50
|
776 destruct n; destruct n0; crush.
|
adamc@50
|
777
|
adamc@50
|
778 (** [[
|
adamc@50
|
779 n : nat
|
adamc@50
|
780 H : even (S n)
|
adamc@50
|
781 IHeven : forall n0 : nat, S n = S (n0 + n0) -> False
|
adamc@50
|
782 n0 : nat
|
adamc@50
|
783 H0 : S n = n0 + S n0
|
adamc@50
|
784 ============================
|
adamc@50
|
785 False
|
adamc@209
|
786
|
adamc@209
|
787 ]]
|
adamc@50
|
788
|
adam@280
|
789 At this point it is useful to use a theorem from the standard library, which we also proved with a different name in the last chapter. We can search for a theorem that allows us to rewrite terms of the form [x + S y]. *)
|
adamc@209
|
790
|
adam@280
|
791 SearchRewrite (_ + S _).
|
adam@322
|
792
|
adam@401
|
793 (** [[
|
adam@280
|
794 plus_n_Sm : forall n m : nat, S (n + m) = n + S m
|
adam@302
|
795 ]]
|
adam@302
|
796 *)
|
adamc@50
|
797
|
adamc@50
|
798 rewrite <- plus_n_Sm in H0.
|
adamc@50
|
799
|
adam@322
|
800 (** The induction hypothesis lets us complete the proof, if we use a variant of [apply] that has a %\index{tactics!with}%[with] clause to give instantiations of quantified variables. *)
|
adamc@209
|
801
|
adamc@50
|
802 apply IHeven with n0; assumption.
|
adamc@50
|
803
|
adam@322
|
804 (** 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. We use the notation [<-] to request a hint that does right-to-left rewriting, just like we can with the [rewrite] tactic. *)
|
adamc@209
|
805
|
adamc@209
|
806 Restart.
|
adam@322
|
807
|
adam@375
|
808 Hint Rewrite <- plus_n_Sm.
|
adamc@50
|
809
|
adamc@50
|
810 induction 1; crush;
|
adamc@50
|
811 match goal with
|
adamc@50
|
812 | [ H : S ?N = ?N0 + ?N0 |- _ ] => destruct N; destruct N0
|
adamc@50
|
813 end; crush; eauto.
|
adamc@50
|
814 Qed.
|
adamc@50
|
815
|
adam@322
|
816 (** We write the proof in a way that avoids the use of local variable or hypothesis names, using the %\index{tactics!match}%[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, %\index{tactics!eauto}%[eauto].
|
adamc@50
|
817
|
adam@421
|
818 The [crush] tactic 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. The [auto] tactic attempts %\index{Prolog}%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. This restriction is relaxed for [eauto], 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.
|
adamc@50
|
819
|
adamc@50
|
820 The original theorem now follows trivially from our lemma. *)
|
adamc@50
|
821
|
adamc@50
|
822 Theorem even_contra : forall n, even (S (n + n)) -> False.
|
adamc@52
|
823 intros; eapply even_contra'; eauto.
|
adamc@50
|
824 Qed.
|
adamc@52
|
825
|
adam@398
|
826 (** We use a variant %\index{tactics!apply}%[eapply] of [apply] which has the same relationship to [apply] as [eauto] has to [auto]. An invocation of [apply] only succeeds if all arguments to the rule being used can be determined from the form of the goal, whereas [eapply] will introduce unification variables for undetermined arguments. In this case, [eauto] is able to determine the right values for those unification variables, using (unsurprisingly) a variant of the classic algorithm for _unification_ %\cite{unification}%.
|
adamc@52
|
827
|
adamc@52
|
828 By considering an alternate attempt at proving the lemma, we can see another common pitfall of inductive proofs in Coq. Imagine that we had tried to prove [even_contra'] with all of the [forall] quantifiers moved to the front of the lemma statement. *)
|
adamc@52
|
829
|
adamc@52
|
830 Lemma even_contra'' : forall n' n, even n' -> n' = S (n + n) -> False.
|
adamc@52
|
831 induction 1; crush;
|
adamc@52
|
832 match goal with
|
adamc@52
|
833 | [ H : S ?N = ?N0 + ?N0 |- _ ] => destruct N; destruct N0
|
adamc@52
|
834 end; crush; eauto.
|
adamc@52
|
835
|
adamc@209
|
836 (** One subgoal remains:
|
adamc@52
|
837
|
adamc@209
|
838 [[
|
adamc@52
|
839 n : nat
|
adamc@52
|
840 H : even (S (n + n))
|
adamc@52
|
841 IHeven : S (n + n) = S (S (S (n + n))) -> False
|
adamc@52
|
842 ============================
|
adamc@52
|
843 False
|
adamc@209
|
844
|
adamc@209
|
845 ]]
|
adamc@52
|
846
|
adam@398
|
847 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] _appeared after the thing we are inducting on_ 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. *)
|
adamc@52
|
848
|
adam@322
|
849 Abort.
|
adam@322
|
850
|
adam@322
|
851 (** 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. *)
|
adamc@55
|
852 (* end thide *)
|
adamc@209
|
853
|
adam@322
|
854
|
adamc@51
|
855
|
adamc@52
|
856
|
adamc@52
|
857 (* begin hide *)
|
adamc@52
|
858 (* In-class exercises *)
|
adamc@52
|
859
|
adam@421
|
860 (* EX: Define a type [prop] of simple boolean formulas made up only of truth, falsehood, binary conjunction, and binary disjunction. Define an inductive predicate [holds] that captures when [prop]s are valid, and define a predicate [falseFree] that captures when a [prop] does not contain the "false" formula. Prove that every false-free [prop] is valid. *)
|
adamc@52
|
861
|
adamc@52
|
862 (* begin thide *)
|
adamc@52
|
863 Inductive prop : Set :=
|
adamc@52
|
864 | Tru : prop
|
adamc@52
|
865 | Fals : prop
|
adamc@52
|
866 | And : prop -> prop -> prop
|
adamc@52
|
867 | Or : prop -> prop -> prop.
|
adamc@52
|
868
|
adamc@52
|
869 Inductive holds : prop -> Prop :=
|
adamc@52
|
870 | HTru : holds Tru
|
adamc@52
|
871 | HAnd : forall p1 p2, holds p1 -> holds p2 -> holds (And p1 p2)
|
adamc@52
|
872 | HOr1 : forall p1 p2, holds p1 -> holds (Or p1 p2)
|
adamc@52
|
873 | HOr2 : forall p1 p2, holds p2 -> holds (Or p1 p2).
|
adamc@52
|
874
|
adamc@52
|
875 Inductive falseFree : prop -> Prop :=
|
adamc@52
|
876 | FFTru : falseFree Tru
|
adamc@52
|
877 | FFAnd : forall p1 p2, falseFree p1 -> falseFree p2 -> falseFree (And p1 p2)
|
adamc@52
|
878 | FFNot : forall p1 p2, falseFree p1 -> falseFree p2 -> falseFree (Or p1 p2).
|
adamc@52
|
879
|
adamc@52
|
880 Hint Constructors holds.
|
adamc@52
|
881
|
adamc@52
|
882 Theorem falseFree_holds : forall p, falseFree p -> holds p.
|
adamc@52
|
883 induction 1; crush.
|
adamc@52
|
884 Qed.
|
adamc@52
|
885 (* end thide *)
|
adamc@52
|
886
|
adamc@52
|
887
|
adamc@52
|
888 (* EX: Define an inductive type [prop'] that is the same as [prop] but omits the possibility for falsehood. Define a proposition [holds'] for [prop'] that is analogous to [holds]. Define a function [propify] for translating [prop']s to [prop]s. Prove that, for any [prop'] [p], if [propify p] is valid, then so is [p]. *)
|
adamc@52
|
889
|
adamc@52
|
890 (* begin thide *)
|
adamc@52
|
891 Inductive prop' : Set :=
|
adamc@52
|
892 | Tru' : prop'
|
adamc@52
|
893 | And' : prop' -> prop' -> prop'
|
adamc@52
|
894 | Or' : prop' -> prop' -> prop'.
|
adamc@52
|
895
|
adamc@52
|
896 Inductive holds' : prop' -> Prop :=
|
adamc@52
|
897 | HTru' : holds' Tru'
|
adamc@52
|
898 | HAnd' : forall p1 p2, holds' p1 -> holds' p2 -> holds' (And' p1 p2)
|
adamc@52
|
899 | HOr1' : forall p1 p2, holds' p1 -> holds' (Or' p1 p2)
|
adamc@52
|
900 | HOr2' : forall p1 p2, holds' p2 -> holds' (Or' p1 p2).
|
adamc@52
|
901
|
adamc@52
|
902 Fixpoint propify (p : prop') : prop :=
|
adamc@52
|
903 match p with
|
adamc@52
|
904 | Tru' => Tru
|
adamc@52
|
905 | And' p1 p2 => And (propify p1) (propify p2)
|
adamc@52
|
906 | Or' p1 p2 => Or (propify p1) (propify p2)
|
adamc@52
|
907 end.
|
adamc@52
|
908
|
adamc@52
|
909 Hint Constructors holds'.
|
adamc@52
|
910
|
adamc@52
|
911 Lemma propify_holds' : forall p', holds p' -> forall p, p' = propify p -> holds' p.
|
adamc@52
|
912 induction 1; crush; destruct p; crush.
|
adamc@52
|
913 Qed.
|
adamc@52
|
914
|
adamc@52
|
915 Theorem propify_holds : forall p, holds (propify p) -> holds' p.
|
adamc@52
|
916 intros; eapply propify_holds'; eauto.
|
adamc@52
|
917 Qed.
|
adamc@52
|
918 (* end thide *)
|
adamc@52
|
919
|
adamc@52
|
920 (* end hide *)
|