-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInduction.v
More file actions
397 lines (330 loc) · 9.18 KB
/
Induction.v
File metadata and controls
397 lines (330 loc) · 9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
From LF Require Export Basics.
Theorem add_0_l_firsttry: forall n:nat,
0 + n = n.
Proof. simpl. reflexivity. Qed.
(** it can be done simply with reflexivity.**)
Theorem add_0_r_firsttry: forall n:nat,
n + 0 = n.
Proof.
intros n.
destruct n as [|n'] eqn:E.
- simpl. reflexivity.
- simpl. Abort.
(** If we try to use reflexivity
above, we get Unable to unify "S n" with "S (n + 0)"
because S n is a inductive type, we would need to
destruct it again. **)
(** Given a proposition P, we need to:
1 - show that P(O) holds;
2 - show that, for any n', if P(n') holds, then so does P(S n');
conclude that P(n) holds for all n.
**)
Theorem add_0_r : forall n: nat, n + 0 = n.
Proof.
intros n. induction n as [| n' IHn']. (*n: the arbitrary natural number, IHn': the inductive hypothesis.*)
- reflexivity.
- simpl.
rewrite IHn'. reflexivity. Qed.
(**
the inductive tactics here replaces n' + 0 = n' by S n' + 0 = S n',
what is: we are effectively applying the second criteria we defined
from the principle of Induction.
**)
Theorem minus_n_n : forall n,
minus n n = 0.
Proof.
intros n. induction n as [| n' IHn'].
- reflexivity.
- rewrite <- IHn'. simpl. reflexivity. Qed.
(** Exercise 1 - basic induction, 2 stars **)
Theorem mul_0_r: forall n: nat, n * 0 = 0.
Proof.
intros n. induction n as [| n' IHn'].
- reflexivity.
- simpl. rewrite IHn'. reflexivity. Qed.
Theorem plus_n_Sm : forall n m : nat,
S (n + m) = n + (S m).
Proof.
intros n m.
induction n as [| n' IHn'].
- induction m as [| m' IHm'].
+ simpl. reflexivity.
+ simpl. reflexivity.
- simpl. rewrite <- IHn'.
reflexivity.
Qed.
Theorem add_comm : forall n m : nat,
n + m = m + n.
Proof.
intros m n.
induction n as [| n' IHn'].
- rewrite add_0_r. reflexivity.
- simpl.
rewrite <- plus_n_Sm.
rewrite IHn'.
reflexivity.
Qed.
Theorem add_assoc : forall n m p: nat,
n + (m + p) = (n + m) + p.
Proof.
intros n m p.
induction n as [| n' IHn'].
- simpl. reflexivity.
- simpl. rewrite IHn'. reflexivity.
Qed.
(** Exercise 2 - double plus, 2 stars**)
Fixpoint double (n: nat) :=
match n with
| O => O
| S n' => S (S (double n'))
end.
(** Use induction to prove this fact **)
Lemma double_plus: forall n, double n = n + n.
Proof.
induction n as [| n' IHn'].
- reflexivity.
- simpl. rewrite IHn'. rewrite plus_n_Sm. reflexivity.
Qed.
(**
Fixpoint even (n:nat) : bool :=
match n with
| O => true
| S O => false
| S (S n') => even n'
end.
**)
(** Optional exercise - using induction to prove a lemma about even function **)
Theorem even_S : forall n : nat,
even (S n) = negb (even n).
Proof.
induction n as [| n' IHn'].
- reflexivity.
- rewrite IHn'.
rewrite negb_involutive.
simpl. reflexivity.
Qed.
(** Differences destruct and induction **)
(**
TL;DR Their main difference is the subgoal hypothesis.
The destruct tactic gives us a subgoal for each constructor of
an inductive type. It means that we have to prove that a proposition
holds for both O or S n' for a nat type, for example.
This creates as many goals as there are constructors in type T.
On the other hand, induction generates subgoals as many as
there are constructors, and adds the inductive hypotheses in
the contexts.
**)
(* ################################################################# *)
(** * Proofs Within Proofs *)
(** Assert is a way to create a quick proof
about some statement, that is used in the goal
we are trying to prove.
Assert generates:
1 - A subgoal where we must prove the asserted fact
2 - A second subgoal where we can use the asserted
fact to make progress on whatever we were trying to
prove in the first place.
**)
Theorem mult_0_plus' : forall n m : nat,
(0 + n) * m = n * m.
Proof.
intros n m.
assert (H: 0 + n = n). { reflexivity. }
rewrite -> H.
reflexivity. Qed.
Theorem plus_rearrange_firsttry : forall n m p q : nat,
(n + m) + (p + q) = (m + n) + (p + q).
Proof.
intros n m p q.
(* We just need to swap (n + m) for (m + n)... seems
like add_comm should do the trick! *)
rewrite add_comm.
(* Doesn't work... Coq rewrites the wrong plus! :-( *)
Abort.
(** To use [add_comm] at the point where we need it, we can introduce
a local lemma stating that [n + m = m + n] (for the _particular_ [m]
and [n] that we are talking about here), prove this lemma using
[add_comm], and then use it to do the desired rewrite. *)
Theorem plus_rearrange : forall n m p q : nat,
(n + m) + (p + q) = (m + n) + (p + q).
Proof.
intros n m p q.
assert (H: n + m = m + n).
{ rewrite add_comm. reflexivity. }
rewrite H. reflexivity. Qed.
(* ################################################################# *)
(** * Formal vs. Informal Proof *)
(* TODO *)
(* ################################################################# *)
(** * More Exercises *)
(** In the theorem below, assert helps us
to change the order of elements; after
the two rewrites, we get n + m + p = m + n + p
what is, we only need to change the positions
of m and n, so assert does it for us here.
Basically we use the same hypothesis defined
previously, but we are kind of telling
Coq how to replace stuff properly.**)
Theorem add_shuffle3: forall n m p: nat,
n + (m + p) = m + (n + p).
Proof.
intros n m p.
rewrite add_assoc.
rewrite add_assoc.
assert (H: n + m = m + n).
{ rewrite add_comm. reflexivity. }
rewrite H. reflexivity.
Qed.
(** Commutativity of multiplication **)
(**
Before proving the commutativity of multiplication, we have
to prove that the multiplication distributes over addition.
**)
Theorem mul_dist: forall n m: nat,
n * (S m) = n + n * m.
intros n m.
induction n as [| n' IHn'].
- reflexivity.
- simpl. rewrite IHn'.
rewrite add_shuffle3.
reflexivity.
Qed.
Theorem mul_comm: forall n m: nat,
m * n = n * m.
Proof.
intros m n.
induction n as [| n' IHn'].
- simpl. rewrite mul_0_r. reflexivity.
- simpl.
rewrite IHn'.
rewrite mul_dist.
reflexivity.
Qed.
(**
The goal for the exercises below is to
think about the necessity for the use of
simplification and rewriting, case analysis
(destruct) or if it also requires induction.
My hypothesis: when the case is covered in the
definition, it can be checked with simpl, which
does an "unfolding".
**)
Check leb.
(** Requires induction bc is an arbitrary number **)
Theorem leb_refl: forall n: nat,
(n <=? n) = true.
Proof.
induction n as [| n' IHn'].
- simpl. reflexivity.
- simpl. rewrite IHn'. reflexivity.
Qed.
(** simpl, because this case is defined
**)
Theorem zero_neqb_S : forall n:nat,
0 =? (S n) = false.
Proof. (*simp: same as unfold eqb here*)
simpl. reflexivity.
Qed.
(** case analysis bc the match takes
the first argument first **)
Theorem andb_false_r : forall b : bool,
andb b false = false.
Proof.
destruct b.
- reflexivity.
- reflexivity.
Qed.
(* Induction and rewrite (?) *)
Theorem plus_leb_compat_l : forall n m p : nat,
n <=? m = true -> (p + n) <=? (p + m) = true.
Proof.
intros n m p H.
induction p as [| p' IHp'].
- simpl. rewrite H. reflexivity.
- simpl. rewrite IHp'. reflexivity.
Qed.
(* simpl, because this case is already defined in eqb *)
Theorem S_neqb_0 : forall n:nat,
(S n) =? 0 = false.
Proof.
simpl. reflexivity. Qed.
(**
Needs induction, to create a hypothesis that
holds for both 0 and S n.
**)
Theorem mult_1_l : forall n:nat, 1 * n = n.
Proof.
induction n as [| n' IHn'].
- reflexivity.
- simpl. rewrite add_comm. reflexivity.
Qed.
(* Case analysis *)
Theorem all3_spec : forall b c : bool,
orb
(andb b c)
(orb (negb b)
(negb c))
= true.
Proof.
intros b c.
destruct b.
- destruct c.
+ reflexivity.
+ reflexivity.
- destruct c.
+ reflexivity.
+ reflexivity.
Qed.
(* Rewrite with mult_dist after induction on n *)
Theorem mult_plus_distr_r : forall n m p : nat,
(n + m) * p = (n * p) + (m * p).
Proof.
intros n m p.
induction n as [| n' IHn'].
- simpl. reflexivity.
- simpl. rewrite IHn'.
rewrite add_assoc.
reflexivity.
Qed.
(* Induction on n', rewrite *)
Theorem mult_assoc : forall n m p : nat,
n * (m * p) = (n * m) * p.
Proof.
intros n m p.
induction n as [| n' IHn'].
- reflexivity.
- simpl. rewrite IHn'.
rewrite mult_plus_distr_r.
reflexivity.
Qed.
(** [] *)
Theorem eqb_refl: forall n: nat,
(n =? n) = true.
Proof.
induction n as [| n' IHn'].
- reflexivity.
- simpl. rewrite IHn'. reflexivity.
Qed.
(**
We can specify a particular subterm to
perform the rewrite using replace (t) with (u).
Previously, we have defined that n + m = m + n
with the use of the keyword "assert". Assert
generates a hypothesis and a subgoal to prove
that hypothesis.
By using replace, we edit the goal directly and
have to prove that this rewriting is possible
as a subgoal.
**)
Theorem add_shuffle3': forall n m p: nat,
n + (m + p) = m + (n + p).
Proof.
intros n m p.
rewrite add_assoc.
rewrite add_assoc.
replace (n + m) with (m + n).
- reflexivity.
- rewrite add_comm. reflexivity.
Qed.
(** **** Exercise: 3 stars, standard, especially useful (binary_commute) **)
(* TODO *)