-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTactics.lean
More file actions
440 lines (385 loc) · 9.93 KB
/
Tactics.lean
File metadata and controls
440 lines (385 loc) · 9.93 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
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
-- import «Poly»
import Basics
import Lean
-- THE APPLY TACTIC
theorem silly1 : forall (n m : nat), n = m -> n = m := by
intro n m eq
apply eq
theorem silly2 : forall (n m o p : Nat),
n = m -> (n = m -> [n,o]=[m,p]) -> [n,o]=[m,p] := by
intro n m o p eq1 eq2
apply eq2
apply eq1
theorem silly2a : forall (n m : Nat),
(n, n) = (m, m) ->
(forall (q r : Nat), (q, q) = (r, r) -> [q] = [r]) ->
[n] = [m] := by
intro n m eq1 eq2
apply eq2
apply eq1
theorem silly_ex : forall p : Nat,
(forall n, even n = true -> even (Nat.succ n) = false) ->
(forall n, even n = false -> odd n = true) ->
even p = true ->
odd (Nat.succ p) = true := by
intro p h1 h2 hp
apply h2
apply h1
exact hp
theorem silly3 : forall (n m : Nat),
n = m ->
m = n := by
intro n m h
exact h.symm
theorem rev_exercise1 : forall (l l' : List Nat),
l = List.reverse l' ->
l' = List.reverse l := by
intro l l' h
rw[h]
apply Eq.symm
exact List.reverse_reverse l'
-- THE APPLY WITH TACTIC
theorem trans_eq_example : forall (a b c d e f : Nat),
[a, b] = [c, d] ->
[c, d] = [e, f] ->
[a, b] = [e, f] := by
intro a b c d e f h1 h2
rewrite [h1]
exact h2
theorem trans_eq : forall {X : Type} (x y z : X),
x = y -> y = z -> x = z := by
intro X x y z h1 h2
rewrite [h1]
exact h2
theorem trans_eq_example' : forall (a b c d e f : Nat),
[a, b] = [c, d] ->
[c, d] = [e, f] ->
[a, b] = [e, f] := by
intro a b c d e f h1 h2
-- Coq: apply trans_eq with (y := [c; d]).
apply trans_eq (X := List Nat) (x := [a, b]) (y := [c, d]) (z := [e, f])
· exact h1
· exact h2
theorem trans_eq_example'' : forall (a b c d e f : Nat),
[a, b] = [c, d] ->
[c, d] = [e, f] ->
[a, b] = [e, f] := by
intro a b c d e f eq1 eq2
apply trans_eq (y := [c, d])
· exact eq1
· exact eq2
theorem trans_eq_exercise : forall (n m o p : Nat),
m = minustwo o ->
(n + p) = m ->
(n + p) = (minustwo o) := by
intro n m o p h hm
apply Eq.trans hm h
-- THE INJECTION AND DISCRIMINATE TACTICS
theorem S_injective : forall (n m : Nat),
Nat.succ n = Nat.succ m ->
n = m := by
intro n m h1
have h2 : n = Nat.pred (Nat.succ n) := by rfl
rewrite [h2]
rewrite [h1]
simp
theorem S_injective' : forall (n m : Nat),
Nat.succ n = Nat.succ m ->
n = m := by
intro n m h
cases h
rfl
theorem injection_ex1 : forall (n m o : Nat),
([n, m] : List Nat) = [o, o] ->
n = m := by
intro n m o h
cases h
rfl
theorem injection_ex3 :
forall (X : Type) (x y z : X) (l j : List X),
x :: y :: l = z :: j ->
j = z :: l ->
x = y := by
intro X x y z l j h hj
cases h
-- now z := x and j := y :: l
-- hj : y :: l = x :: l
cases hj
rfl
theorem discriminate_ex1 : forall (n m : Nat),
(false = true) ->
n = m := by
intro n m h
cases h
theorem discriminate_ex2 : forall (n : Nat),
Nat.succ n = 0 ->
2 + 2 = 5 := by
intro n h
cases h
theorem discriminate_ex3 :
forall (X : Type) (x y z : X) (l _j : List X),
x :: y :: l = [] ->
x = z := by
intro X x y z l j h
cases h
theorem eqb_0_l : forall n,
(0 =? n) = true -> n = 0 := by
intro n
cases n with
| zero =>
intro h
rfl
| succ n' =>
intro h
cases h
theorem f_equal :
forall {A B : Type} (f : A -> B) (x y : A),
x = y -> f x = f y := by
intro A B f x y h
cases h
rfl
theorem eq_implies_succ_equal : forall (n m : Nat),
n = m -> Nat.succ n = Nat.succ m := by
intro n m h
apply f_equal Nat.succ
exact h
theorem eq_implies_succ_equal' : forall (n m : Nat),
n = m -> Nat.succ n = Nat.succ m := by
intro n m h
rw[h]
-- USING TACTICS ON HYPOTHESES
theorem S_inj : forall (n m : Nat) (b : Bool),
((Nat.succ n =? Nat.succ m) = b) ->
((n =? m) = b) := by
intro n m b h
dsimp at h
exact h
-- simpa using h -- ( works perfectly fine)
theorem silly4 : forall (n m p q : Nat),
(n = m -> p = q) ->
m = n ->
q = p := by
intro n m p q eq h
have h' : n = m := h.symm
have hpq : p = q := eq h'
exact hpq.symm
theorem silly4' : forall (n m p q : Nat),
(n = m -> p = q) ->
m = n ->
q = p := by
intro n m p q eq h
exact (eq h.symm).symm
/- SPECIALIZING HYPOTHESES -/
theorem specialize_example : forall n,
(forall m, m * n = 0) ->
n = 0 := by
intro n h
-- have h1 : 1 * n = 0 := h 1 -- > this works as well, just tried to make it more coq native
-- specialize h 1 --> and this as well, but lean allows to pass arguments directly to h as well
simpa [Nat.one_mul] using (h 1)
theorem trans_eq_example''' : forall (a b c d e f : Nat),
[a, b] = [c, d] ->
[c, d] = [e, f] ->
[a, b] = [e, f] := by
intro a b c d e f eq1 eq2
exact trans_eq _ _ _ eq1 eq2
-- VARYING THE INDUCTION HYPOTHESIS
def double (n : Nat) : Nat :=
match n with
| Nat.zero => 0
| Nat.succ n' => Nat.succ (Nat.succ (double n'))
theorem double_injective : ∀ n m : Nat, double n = double m → n = m := by
intro n m
revert n
induction m with
| zero =>
intro n h
cases n with
| zero => rfl
| succ n' => cases h
| succ m' ih =>
intro n h
cases n with
| zero => cases h
| succ n' =>
have hs :
Nat.succ (Nat.succ (double n')) =
Nat.succ (Nat.succ (double m')) := by
simpa [double] using h
have h1 : Nat.succ (double n') = Nat.succ (double m') :=
Nat.succ.inj hs
have h2 : double n' = double m' :=
Nat.succ.inj h1
have hn : n' = m' := ih n' h2
rw[hn]
theorem plus_n_n_injective : forall n m : Nat, n + n = m + m -> n = m := by
intro n
induction n with
| zero =>
intro m h
cases m
· rfl
· contradiction -- 0 = succ... impossible
| succ n' ih =>
intro m h
cases m with
| zero => contradiction
| succ m' =>
rw [Nat.succ_add, Nat.succ_add] at h
injection h with h
injection h with h
specialize ih m' h
rw [ih]
theorem double_injective_take2 : forall n m, double n = double m -> n = m := by
intro n m h
-- "generalizing n" replaces "generalize dependent n"
induction m generalizing n with
| zero =>
cases n
· rfl
· contradiction
| succ m' ih =>
cases n with
| zero => contradiction
| succ n' =>
dsimp [double] at h
injection h with h
injection h with h
specialize ih n' h
rw [ih]
-- theorem nth_error_after_last :
-- forall (n : Nat) (X : Type) (l : List X),
-- List.length l = n ->
-- nth_error l n = none := by
-- intro n X l hlen
-- revert n
-- induction l with
-- | nil =>
-- intro n hlen
-- cases n with
-- | zero =>
-- simp [nth_error] at *
-- | succ n' =>
-- have : (0 : Nat) = Nat.succ n' := by simp at hlen
-- cases this
-- | cons x l' ih =>
-- intro n hlen
-- cases n with
-- | zero =>
-- have : Nat.succ (List.length l') = 0 := by simp at hlen
-- cases this
-- | succ n' =>
-- have hlen' : List.length l' = n' := by
-- have : Nat.succ (List.length l') = Nat.succ n' := by simpa using hlen
-- exact Nat.succ.inj this
-- -- nth_error (x::l') (succ n') = nth_error l' n'
-- simpa [nth_error] using ih n' hlen'
theorem sub_add_leb : forall n m, (Nat.ble n m) = true -> (m - n) + n = m := by
intro n
induction n with
| zero =>
intro m h
simp
| succ n' ih =>
intro m h
cases m with
| zero =>
simp [Nat.ble] at h
| succ m' =>
rw [Nat.succ_sub_succ]
rw [Nat.add_succ]
simp
rw [ih m' h]
/- UNFOLDING DEFINITIONS -/
def square (n : Nat) : Nat := n * n
theorem mul_succ_r : forall n m : Nat,
n * (Nat.succ m) = n + n * m := by
intro n m
induction n with
| zero =>
simp
| succ n' ih =>
simp [Nat.mul_succ, Nat.mul_succ, Nat.add_assoc, Nat.add_comm]
theorem mul_comm : forall m n : Nat,
m * n = n * m := by
intro m n
induction m with
| zero =>
simp
| succ m' ih =>
simp [Nat.succ_mul, ih, mul_succ_r]
rw[<- ih]
exact Nat.add_comm (m' * n) n
theorem square_mult : forall n m : Nat, square (n * m) = square n * square m := by
intro n m
-- expand square
unfold square
calc
(n * m) * (n * m)
= n * (m * (n * m)) := by
simp [Nat.mul_assoc]
_ = n * ((m * n) * m) := by
simp [Nat.mul_assoc]
_ = n * ((n * m) * m) := by
simp [mul_comm]
_ = (n * n) * (m * m) := by
simp [Nat.mul_assoc]
def bar (x : Nat) : Nat :=
match x with
| 0 => 5
| Nat.succ _ => 5
def 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 := by
intro n
unfold sillyfun
cases h1 : (n =? 3) with
| true =>
simp
| false =>
cases h2 : (n =? 5) with
| true =>
simp
| false =>
simp
def combine' {X Y : Type} : List X -> List Y -> List (X × Y)
| [], _ => []
| _, [] => []
| x :: xs, y :: ys => (x, y) :: combine' xs ys
def 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)
theorem combine_split : ∀ X Y (l : List (X × Y)) l1 l2,
split' l = (l1, l2) →
combine' l1 l2 = l := by
intro X Y l
induction l with
| nil =>
intro l1 l2 h
dsimp [split'] at h
cases h
dsimp [combine']
| cons p l' ih =>
intro l1 l2 h
cases p with
| mk x y =>
cases hst : split' l' with
| mk lx ly =>
-- extract l1,l2 from h
have hpair : x :: lx = l1 ∧ y :: ly = l2 := by
simpa [split', hst] using h
rcases hpair with ⟨h1, h2⟩
cases h1
cases h2
have ht : combine' lx ly = l' := ih lx ly hst
simp [combine', ht]
theorem bool_fn_applied_thrice :
forall (f : Bool -> Bool) (b : Bool),
f (f (f b)) = f b := by
intro f b
cases b <;> cases hff : f false <;> cases hft : f true <;> simp [hff, hft]