-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTactics.v
More file actions
397 lines (339 loc) · 7.43 KB
/
Copy pathTactics.v
File metadata and controls
397 lines (339 loc) · 7.43 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
Set Warnings "-notation-overridden".
From LF Require Export Poly.
Theorem silly1 : forall (n m : nat),
n = m -> n =m.
Proof.
intros n m eq.
apply eq. Qed.
Theorem silly2 : forall (n m o p: nat ),
n = m ->
(n = m -> [n;o]=[m;p]) ->
[n;o] = [m;p].
Proof.
intros n m o p eq1 eq2.
apply eq2.
apply eq1.
Qed.
Theorem silly2a : forall (n m : nat),
(n,n) = (m,m) ->
(forall (q r : nat), (q,q) = (r,r) -> [q]=[r]) ->
[n] = [m].
Proof.
intros n m eq1 eq2.
apply eq2. apply eq1. Qed.
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.
Theorem silly3 : forall (n m : nat),
n = m ->
m = n.
Proof.
intros.
symmetry. apply H. Qed.
Theorem rev_exercise1 : forall (l l' : list nat),
l = rev l' ->
l' = rev l.
Proof.
intros. rewrite H. symmetry. apply rev_involutive. Qed.
(* The apply with Tactic *)
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.
rewrite H.
apply H0.
Qed.
Theorem trans_eq : forall (X : Type) (x y z : X),
x = y -> y = z -> x = z.
Proof.
intros.
rewrite 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.
apply trans_eq with (y:=[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 a b c d e f eq1 eq2.
transitivity [c;d].
apply eq1. apply eq2. Qed.
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.
(* The Injection and discriminate Tactics *)
Theorem S_injective : forall (n m : nat),
S n = S m ->
n = m.
Proof.
intros n m H1.
assert ( H2 : n = pred (S n)). { reflexivity. }
rewrite H2. rewrite H1. simpl. 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 n m o H.
injection H as H1 H2.
rewrite H1. symmetry. apply H2.
Qed.
Example injection_ex3 : forall (X : Type)(x y z : X ) (l j : list X),
x :: y :: l = z :: j ->
j = z :: l ->
x = y.
Proof.
intros.
injection H as H1 H2.
rewrite H0 in H2.
injection H2 as H3.
rewrite H1. symmetry. apply H3.
Qed.
Theorem discriminate_ex1 : forall (n m : nat),
false = true ->
n = m.
Proof.
intros.
discriminate H.
Qed.
Theorem discriminate_ex2 : forall (n : nat),
S n = O ->
2 + 2 = 5.
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.
Theorem eqb_0_l : forall n,
0 =? n = true -> n = 0.
Proof.
intros n.
destruct n as [ | n'] eqn:E.
- intros H. reflexivity.
- intros H. discriminate H.
Qed.
Theorem f_equal : forall (A B : Type) (f : A -> B) ( x y : A),
x = y -> f x = f y.
Proof.
intros A B f x y eq.
rewrite eq.
reflexivity.
Qed.
Theorem eq_implies_succ_equal : forall (n m : nat),
n = m -> S n = S m.
Proof.
intros. apply f_equal.
apply H.
Qed.
Theorem eq_implies_succ_equal' : forall ( n m : nat),
n = m -> S n = S m.
Proof.
intros n m H. f_equal. apply H.
Qed.
(* Using Tactics on Hypotheses *)
Theorem S_inj : forall (n m : nat )(b : bool),
((S n) =? (S m)) = b ->
(n =? m) = b.
Proof.
intros n m b H. simpl in H. apply H.
Qed.
Theorem silly4 : forall (n m p q : nat),
(n = m -> p = q) ->
m = n ->
q = p.
Proof.
intros n m p q EQ H.
symmetry in H. apply EQ in H. symmetry in H.
apply H.
Qed.
Theorem specialize_example : forall n,
(forall m, m*n=0)
-> n =0.
Proof.
intros n H.
specialize H with (m := 1).
rewrite mult_1_l in H.
apply H.
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 a b c d e f eq1 eq2.
specialize trans_eq with (y:=[c;d]) as H.
apply H.
apply eq1.
apply eq2.
Qed.
(* Varying the Induction Hypothesis *)
Theorem double_injective_FAILED : forall n m,
double n = double m ->
n = m.
Proof.
intros n m. induction n as [ | n' IHn' ].
- simpl. intros eq. destruct m as [ | m'] eqn:E.
+ reflexivity.
+ discriminate eq.
- intros eq. destruct m as [ | m'] eqn:E.
+ discriminate eq.
+ f_equal.
Abort.
(* 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.
- *)
Theorem eqb_true : forall n m,
n =? m = true -> n = m.
Proof.
intros n.
induction n as [ | n' IHn'].
- intros m H.
destruct m as [| m'].
+ reflexivity.
+ discriminate H.
- intros m H.
destruct m as [ | m'].
+ discriminate H.
+ apply IHn' in H. rewrite H. reflexivity.
Qed.
(* Rewriting with conditional statements *)
Lemma sub_add_leb : forall n m, n <=? m = true -> (m-n)+n = m.
Proof.
intros n.
induction n as [ | n' IHn'].
- intros m H.
rewrite add_0_r.
destruct m as [ | m'].
+ reflexivity.
+ reflexivity.
- intros m H. destruct m as [ | m'].
+ discriminate.
+ simpl in H. simpl. rewrite <- plus_n_Sm. rewrite IHn'. reflexivity. apply H.
Qed.
Theorem nth_error_after_last : forall (n : nat)(X : Type) (l : list X),
length l = n ->
nth_error l n = None.
Proof.
intros n X l H.
revert n H.
induction l as [ | x l' IHl'].
- intros n H.
destruct n as [ | n'].
+ reflexivity.
+ simpl. reflexivity.
- intros n H.
destruct n as [ | n'].
+ simpl in H. discriminate H.
+ simpl in H.
injection H as Hlen.
simpl.
apply IHl'.
apply Hlen.
Qed.
(* Unfolding Definitions *)
Definition square n := n*n.
Lemma mul_succ_r : forall n m : nat,
n * (S m) = n + n * m.
Proof.
intros n m.
induction n as [| n' IHn'].
- simpl. reflexivity.
- simpl.
rewrite IHn'.
rewrite add_assoc.
rewrite add_assoc.
rewrite (add_comm m n').
reflexivity.
Qed.
Theorem mul_comm : forall m n : nat,
m*n = n*m.
Proof.
intros n m.
induction n as [ | n' IHn'].
- simpl. rewrite mult_0_r. reflexivity.
- simpl. rewrite mul_succ_r. rewrite IHn'. reflexivity.
Qed.
Lemma square_mult : forall n m, square (n*m) = square n * square m.
Proof.
intros n m.
simpl.
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 m.
simpl. reflexivity.
Qed.
Definition bar x :=
match x with
| O => 5
| S _ => 5
end.
Fact silly_fact_2 : forall m, bar m + 1 = bar ( m + 1) +1.
Proof.
intros m.
unfold bar.
destruct m eqn:E.
- reflexivity.
- reflexivity.
Abort.
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 n. unfold sillyfun.
destruct (n =? 3) eqn:E1.
- reflexivity.
- destruct (n =? 5) eqn:E2.
+ reflexivity.
+ reflexivity. Qed.