-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTactics.v
529 lines (470 loc) · 12.3 KB
/
Tactics.v
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
From LF Require Export Poly.
Theorem silly_ex : forall p,
(forall n, even n = true -> even (S n) = false)
-> (forall n, even n = false -> odd n = true)
-> even p = true
-> odd (S p) = true.
Proof.
intros.
apply H0.
apply H.
apply H1.
Qed.
Search rev.
Theorem rev_exercise1 : forall (l l': list nat), l = rev l' -> l' = rev l.
Proof.
intros.
symmetry.
rewrite H.
apply rev_involutive.
Qed.
Theorem trans_eq : forall (X: Type) (n m o: X), n = m -> m = o -> n = o.
Proof.
intros.
rewrite -> H.
rewrite -> H0.
reflexivity.
Qed.
Example trans_eq_example' : forall (a b c d e f : nat),
[a;b] = [c;d] ->
[c;d] = [e;f] ->
[a;b] = [e;f].
Proof.
intros.
apply trans_eq with (m:=[c;d]).
apply H.
apply H0.
Qed.
Example trans_eq_example'' : forall (a b c d e f : nat),
[a;b] = [c;d] ->
[c;d] = [e;f] ->
[a;b] = [e;f].
Proof.
intros.
transitivity [c;d].
apply H.
apply H0.
Qed.
(* should be in basic.v *)
Definition minustwo (n: nat) : nat :=
match n with
| O => O
| S O => O
| S (S n') => n'
end.
Example trans_eq_exercise: forall (n m o p: nat),
m = (minustwo o) -> (n + p) = m ->
(n+p) = (minustwo o).
Proof.
intros.
transitivity m.
- apply H0.
- apply H.
Qed.
(* Injecttion and discriminate *)
Theorem S_injective : forall (n m: nat), S n = S m -> n = m.
Proof.
intros.
assert (H2 : n = pred (S n)). { reflexivity. }
rewrite H2.
rewrite H.
reflexivity.
Qed.
Theorem S_injective' : forall (n m: nat), S n = S m -> n = m.
Proof.
intros n m H.
injection H as Hnm.
apply Hnm.
Qed.
Theorem injection_ex1 : forall (n m o : nat), [n; m] = [o; o] -> n = m.
Proof.
intros.
injection H as H1 H2.
rewrite H1.
rewrite H2.
reflexivity.
Qed.
Example injection_ex2: forall (X: Type) (x y z: X) (l j : list X),
x :: y :: l = z :: j -> j = z :: l -> x = y.
Proof.
intros.
rewrite H0 in H.
injection H as H1 H2.
rewrite -> H1.
rewrite -> H2.
reflexivity.
Qed.
(* principle of explosion asserts that a contradictory hypothesis entails anything (even manifestly false things!) *)
Theorem discriminate_ex1 : forall (n m: nat), false = true -> n = m.
Proof.
intros.
discriminate H.
Qed.
Example discriminate_ex3: forall (X: Type) (x y z:X) (l j: list X),
x :: y :: l = [] -> x = z.
Proof.
intros.
discriminate H.
Qed.
(* using tactics on hypothesises *)
Theorem S_inj : forall (n m: nat) (b: bool), ((S n) =? (S m)) = b -> (n =? m) = b.
Proof.
intros.
simpl in H.
apply H.
Qed.
Theorem silly4 : forall (n m p q: nat), (n = m -> p = q) -> m = n -> q = p.
Proof.
intros.
symmetry in H0.
apply H in H0.
symmetry in H0.
apply H0.
Qed.
(* specializing hypothesises *)
Theorem specialize_example: forall n, (forall m, m * n = 0) -> n = 0.
Proof.
intros.
specialize H with (m := 1).
simpl in H.
rewrite add_comm in H.
simpl in H.
apply H.
Qed.
Theorem trans_eq_example''' : forall (a b c d e f: nat), [a;b] = [c;d] -> [c;d] = [e;f] -> [a;b] = [e;f].
Proof.
intros.
specialize trans_eq with (m:=[c;d]) as H1.
apply H1.
- apply H.
- apply H0.
Qed.
Locate f_equal.
Theorem double_injective : forall n m, double n = double m -> n = m.
Proof.
intros n.
induction n as [| n' IHn'].
- simpl.
intros m eq.
destruct m as [| m'] eqn:E.
+ reflexivity.
+ discriminate eq.
- intros m eq.
destruct m as [| m'] eqn:E.
+ discriminate eq.
+ f_equal.
apply IHn'.
simpl in eq.
injection eq as goal.
apply goal.
Qed.
Theorem eqb_true : forall n m, n =? m = true -> n = m.
Proof.
intros n.
induction n as [| n' IHn'].
- intros m eq.
destruct m as [| m'] eqn:E.
+ reflexivity.
+ discriminate eq.
- intros m eq.
destruct m as [| m'] eqn:E.
+ discriminate eq.
+ f_equal.
apply IHn'.
simpl in eq.
apply eq.
Qed.
Print plus_n_Sm.
Theorem plus_n_n_injective : forall n m, n + n = m + m -> n = m.
Proof.
intros n.
induction n as [| n'].
induction m.
- intros.
reflexivity.
- intros.
inversion H.
- intros.
simpl in H.
rewrite <- plus_n_Sm in H.
destruct m.
+ inversion H.
+ f_equal.
simpl in H.
rewrite <- plus_n_Sm in H.
injection H as goal.
apply IHn' in goal.
apply goal.
Qed.
Theorem plus_n_n_injective' : forall n m, n + n = m + m -> n = m.
Proof.
intros n.
induction n.
- intros.
destruct m.
+ reflexivity.
+ discriminate H.
- intros.
destruct m.
+ discriminate H.
+ f_equal.
simpl in H.
rewrite <- plus_n_Sm in H.
injection H as goal.
rewrite <- plus_n_Sm in goal.
inversion goal.
apply IHn in H0.
apply H0.
Qed.
Theorem double_injective_take2 : forall n m, double n = double m -> n = m.
Proof.
intros n m.
generalize dependent n.
induction m as [|m' IHm'].
- simpl.
intros.
destruct n.
+ reflexivity.
+ discriminate H.
- intros.
destruct n.
+ discriminate H.
+ f_equal.
apply IHm'.
injection H as goal.
apply goal.
Qed.
Locate nth_error.
Theorem nth_error_after_last : forall (n: nat) (X: Type) (l: list X),
length l = n -> OptionPlayground.nth_error l n = OptionPlayground.None.
Proof.
intros.
generalize dependent n.
induction l.
- simpl.
destruct n.
+ intros.
reflexivity.
+ intros.
discriminate H.
- destruct n.
+ intros.
discriminate H.
+ intros.
simpl in H.
simpl.
injection H as goal.
apply IHl in goal.
apply goal.
Qed.
(* unfolding definitions *)
Definition square n := n * n.
Lemma square_mult : forall n m, square (n * m) = square n * square m.
Proof.
intros.
unfold square.
rewrite mult_assoc.
assert (H : n * m * n = n * n * m).
{ rewrite mul_comm. apply mult_assoc. }
rewrite H. rewrite mult_assoc. reflexivity.
Qed.
Definition foo (x: nat) := 5.
Fact silly_fact_1: forall m, foo m + 1 = foo (m + 1) + 1.
Proof. intros. simpl. reflexivity. Qed.
Definition bar x :=
match x with
| O => 5
| S _ => 5
end.
Fact silly_fact_2_failed: forall m, bar m + 1 = bar (m + 1) + 1.
Proof.
intros.
simpl. (* Does nothing *)
Abort.
Fact silly_fact_2 : forall m, bar m + 1 = bar (m + 1) + 1.
Proof.
intros.
destruct m eqn:E.
- simpl. reflexivity.
- simpl. reflexivity.
Qed.
Fact silly_fact_2' : forall m, bar m + 1 = bar (m + 1) + 1.
Proof.
intros.
unfold bar.
destruct m.
- reflexivity.
- reflexivity.
Qed.
(* using destruct on compound expressions *)
Definition sillyfun (n: nat) : bool :=
if n =? 3 then false
else if n =? 5 then false
else false.
Theorem sillyfun_false : forall (n: nat), sillyfun n = false.
Proof.
intros.
unfold sillyfun.
destruct (n =? 3) eqn:E1.
- reflexivity.
- destruct (n =? 5) eqn:E2.
+ reflexivity.
+ reflexivity.
Qed.
(* exercise combine_split *)
Fixpoint split {X Y:Type} (l : list (X * Y)) : (list X) * (list Y) :=
match l with
| [] => ([], [])
| (x, y) :: t => match split t with
| (lx, ly) => (x :: lx, y :: ly)
end
end.
Theorem combine_split : forall X Y (l : list (X * Y)) l1 l2, split l = (l1, l2) -> combine l1 l2 = l.
Proof.
intros.
induction l as [| (x, y) l'].
- intros.
inversion H.
reflexivity.
- intros.
generalize dependent l2.
generalize dependent l1.
destruct (split l') eqn:E.
+ simpl.
Abort.
(* eqn: qualifier can carry important information to prove the goal *)
Theorem bool_fn_applied_thrice : forall (f: bool -> bool) (b:bool), f (f (f b)) = f b.
Proof.
intros.
destruct b.
- destruct (f true) eqn:Eftrue.
+ rewrite Eftrue. apply Eftrue.
+ destruct (f false) eqn:Effalse.
++ apply Eftrue.
++ apply Effalse.
- destruct (f false) eqn:Effalse.
+ destruct (f true) eqn:Eftrue.
++ apply Eftrue.
++ apply Effalse.
+ rewrite Effalse. apply Effalse.
Qed.
(* Review
intros: move hypothesises/variables from goal to context.
reflexivity: finish the proof(when the goal looks like e=e)
apply: prove goal using a hypothesis, lemma, or contructor
apply ... in H: apply a hypothesis, lemma, or contructor to a hypothesis in the context (forward reasoning)
apply ... with ... : explicity specify values for variables that cannot be determined by pattern matching
simpl: simplify computations in the goal.
simpl in H. simplify computations in a hypothesis.
rewrite: use an equality hypothesis (or lemma) to rewrite the goal.
rewrite ... in H: ... or a hypothesis.
symmetry: changes a goal of the form t = u into u = t.
symmetry in H. changes a hypothesis of form t = u into u = t.
transitivity y: prove a goal x = z by proving two new subgoals, x = y and y = z.
unfold: replace a defined constant by its right-hand side in the goal.
unfold ... in H: ... or a hypothesis.
destruct ... as ...: case analysis on values of inductively defined types.
destruct ... eqn:... : specify the name of an equation to be added to the context, recording the result of the case analysis.
induction ... as ...: induction on values of inductively defined types.
injection ... as ...: reason by injectivity on equalitues between values of inductively defined types.
discriminate: reason by disjointness of contructors on equalitues between values of inductively defined types.
assert (H:e) (or assert (e) as H): introduce a 'local lemma' e and call it H.
generalize dependent x: move the variable x (and anything else that depends on it) from the context back to a explicit hypothesis in the goal formula.
f_equal: change a goal of the form f x = f y into x = y.
*)
Check eqb_true.
Theorem eqb_sym : forall (n m: nat), (n =? m) = (m =? n).
Proof.
intros.
generalize dependent m.
induction n.
- intros.
destruct m.
+ reflexivity.
+ reflexivity.
- intros.
induction m.
+ reflexivity.
+ simpl.
apply IHn.
Qed.
Theorem eqb_trans: forall n m p, n =? m = true -> m =? p = true -> n =? p = true.
Proof.
intros.
apply eqb_true in H.
apply eqb_true in H0.
rewrite H.
rewrite H0.
apply eqb_refl.
Qed.
(* exercise split_combine *)
Definition split_combine_statement : Prop
(* (": Prop" means that we are giving a name to a
logical proposition here.) *)
(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.
Theorem split_combine : split_combine_statement.
Proof.
(* FILL IN HERE *) Admitted.
Print filter.
(* exercise filter_exercise *)
Theorem filter_exercise : forall (X : Type) (test : X -> bool) (x : X) (l lf : list X),
filter test l = x :: lf -> test x = true.
Proof.
intros.
destruct l.
- discriminate.
- destruct (test x).
+ reflexivity.
+ generalize dependent x.
intros.
inversion H.
Abort.
(* forall_exists_challenge *)
Fixpoint forallb {X : Type} (f : X -> bool) (l : list X) : bool :=
match l with
| [] => true
| x :: l' => f x && forallb f l'
end.
Fixpoint existsb {X: Type} (f : X -> bool) (l : list X) : bool :=
match l with
| [] => false
| x :: l' => f x || existsb f l'
end.
Example test_forallb_1 : forallb odd [1;3;5;7;9] = true.
Proof. reflexivity. Qed.
Example test_forallb_2 : forallb negb [false;false] = true.
Proof. reflexivity. Qed.
Example test_forallb_3 : forallb even [0;2;4;5] = false.
Proof. reflexivity. Qed.
Example test_forallb_4 : forallb (eqb 5) [] = true.
Proof. reflexivity. Qed.
Example test_existsb_1 : existsb (eqb 5) [0;2;3;6] = false.
Proof. reflexivity. Qed.
Example test_existsb_2 : existsb (andb true) [true;true;false] = true.
Proof. reflexivity. Qed.
Example test_existsb_3 : existsb odd [1;0;0;0;0;3] = true.
Proof. reflexivity. Qed.
Example test_existsb_4 : existsb even [] = false.
Proof. reflexivity. Qed.
Definition existsb' {X : Type} (f : X -> bool) (l : list X) : bool := negb (forallb negb (map f l)).
Example test_existsb'_1 : existsb' (eqb 5) [0;2;3;6] = false.
Proof. reflexivity. Qed.
Example test_existsb'_2 : existsb' (andb true) [true;true;false] = true.
Proof. reflexivity. Qed.
Example test_existsb'_3 : existsb' odd [1;0;0;0;0;3] = true.
Proof. reflexivity. Qed.
Example test_existsb'_4 : existsb' even [] = false.
Proof. reflexivity. Qed.
Theorem existsb_existb' : forall (X:Type) (f: X-> bool) (l: list X),
existsb f l = existsb' f l.
Proof.
intros.
induction l.
- unfold existsb'.
simpl.
reflexivity.
- simpl.
rewrite -> IHl.
unfold existsb'.
destruct (map f (x :: l)) eqn:Efx.
Abort.