-
Notifications
You must be signed in to change notification settings - Fork 297
/
tactics.lean
579 lines (487 loc) · 15.9 KB
/
tactics.lean
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon, Scott Morrison
-/
import tactic.interactive
import tactic.finish
import tactic.ext
import tactic.lift
import tactic.apply
import tactic.reassoc_axiom
import tactic.tfae
import tactic.elide
import tactic.ring_exp
import tactic.clear
import tactic.simp_rw
example (m n p q : nat) (h : m + n = p) : true :=
begin
have : m + n = q,
{ generalize_hyp h' : m + n = x at h,
guard_hyp h' : m + n = x,
guard_hyp h : x = p,
guard_target m + n = q,
admit },
have : m + n = q,
{ generalize_hyp h' : m + n = x at h ⊢,
guard_hyp h' : m + n = x,
guard_hyp h : x = p,
guard_target x = q,
admit },
trivial
end
example (α : Sort*) (L₁ L₂ L₃ : list α)
(H : L₁ ++ L₂ = L₃) : true :=
begin
have : L₁ ++ L₂ = L₂,
{ generalize_hyp h : L₁ ++ L₂ = L at H,
induction L with hd tl ih,
case list.nil
{ tactic.cleanup,
change list.nil = L₃ at H,
admit },
case list.cons
{ change list.cons hd tl = L₃ at H,
admit } },
trivial
end
example (x y : ℕ) (p q : Prop) (h : x = y) (h' : p ↔ q) : true :=
begin
symmetry' at h,
guard_hyp' h : y = x,
guard_hyp' h' : p ↔ q,
symmetry' at *,
guard_hyp' h : x = y,
guard_hyp' h' : q ↔ p,
trivial
end
section h_generalize
variables {α β γ φ ψ : Type} (f : α → α → α → φ → γ)
(x y : α) (a b : β) (z : φ)
(h₀ : β = α) (h₁ : β = α) (h₂ : φ = β)
(hx : x == a) (hy : y == b) (hz : z == a)
include f x y z a b hx hy hz
example : f x y x z = f (eq.rec_on h₀ a) (cast h₀ b) (eq.mpr h₁.symm a) (eq.mpr h₂ a) :=
begin
guard_hyp_nums 16,
h_generalize hp : a == p with hh,
guard_hyp_nums 19,
guard_hyp' hh : β = α,
guard_target f x y x z = f p (cast h₀ b) p (eq.mpr h₂ a),
h_generalize hq : _ == q,
guard_hyp_nums 21,
guard_target f x y x z = f p q p (eq.mpr h₂ a),
h_generalize _ : _ == r,
guard_hyp_nums 23,
guard_target f x y x z = f p q p r,
casesm* [_ == _, _ = _], refl
end
end h_generalize
section h_generalize
variables {α β γ φ ψ : Type} (f : list α → list α → γ)
(x : list α) (a : list β) (z : φ)
(h₀ : β = α) (h₁ : list β = list α)
(hx : x == a)
include f x z a hx h₀ h₁
example : true :=
begin
have : f x x = f (eq.rec_on h₀ a) (cast h₁ a),
{ guard_hyp_nums 11,
h_generalize : a == p with _,
guard_hyp_nums 13,
guard_hyp' h : β = α,
guard_target f x x = f p (cast h₁ a),
h_generalize! : a == q ,
guard_hyp_nums 13,
guard_target ∀ q, f x x = f p q,
casesm* [_ == _, _ = _],
success_if_fail { refl },
admit },
trivial
end
end h_generalize
section tfae
example (p q r s : Prop)
(h₀ : p ↔ q)
(h₁ : q ↔ r)
(h₂ : r ↔ s) :
p ↔ s :=
begin
scc,
end
example (p' p q r r' s s' : Prop)
(h₀ : p' → p)
(h₀ : p → q)
(h₁ : q → r)
(h₁ : r' → r)
(h₂ : r ↔ s)
(h₂ : s → p)
(h₂ : s → s') :
p ↔ s :=
begin
scc,
end
example (p' p q r r' s s' : Prop)
(h₀ : p' → p)
(h₀ : p → q)
(h₁ : q → r)
(h₁ : r' → r)
(h₂ : r ↔ s)
(h₂ : s → p)
(h₂ : s → s') :
p ↔ s :=
begin
scc',
assumption
end
example : tfae [true, ∀ n : ℕ, 0 ≤ n * n, true, true] := begin
tfae_have : 3 → 1, { intro h, constructor },
tfae_have : 2 → 3, { intro h, constructor },
tfae_have : 2 ← 1, { intros h n, apply nat.zero_le },
tfae_have : 4 ↔ 2, { tauto },
tfae_finish,
end
example : tfae [] := begin
tfae_finish,
end
variables P Q R : Prop
example (pq : P → Q) (qr : Q → R) (rp : R → P) : tfae [P, Q, R] :=
begin
tfae_finish
end
example (pq : P ↔ Q) (qr : Q ↔ R) : tfae [P, Q, R] :=
begin
tfae_finish -- the success or failure of this tactic is nondeterministic!
end
example (p : unit → Prop) : tfae [p (), p ()] :=
begin
tfae_have : 1 ↔ 2, from iff.rfl,
tfae_finish
end
end tfae
section clear_aux_decl
example (n m : ℕ) (h₁ : n = m) (h₂ : ∃ a : ℕ, a = n ∧ a = m) : 2 * m = 2 * n :=
let ⟨a, ha⟩ := h₂ in
begin
clear_aux_decl, -- subst will fail without this line
subst h₁
end
example (x y : ℕ) (h₁ : ∃ n : ℕ, n * 1 = 2) (h₂ : 1 + 1 = 2 → x * 1 = y) : x = y :=
let ⟨n, hn⟩ := h₁ in
begin
clear_aux_decl, -- finish produces an error without this line
finish
end
end clear_aux_decl
section swap
example {α₁ α₂ α₃ : Type} : true :=
by {have : α₁, have : α₂, have : α₃, swap, swap,
rotate, rotate, rotate, rotate 2, rotate 2, triv, recover}
end swap
section lift
example (n m k x z u : ℤ) (hn : 0 < n) (hk : 0 ≤ k + n) (hu : 0 ≤ u)
(h : k + n = 2 + x) (f : false) :
k + n = m + x :=
begin
lift n to ℕ using le_of_lt hn,
guard_target (k + ↑n = m + x), guard_hyp hn : (0 : ℤ) < ↑n,
lift m to ℕ,
guard_target (k + ↑n = ↑m + x), tactic.swap, guard_target (0 ≤ m), tactic.swap,
tactic.num_goals >>= λ n, guard (n = 2),
lift (k + n) to ℕ using hk with l hl,
guard_hyp l : ℕ, guard_hyp hl : ↑l = k + ↑n, guard_target (↑l = ↑m + x),
tactic.success_if_fail (tactic.get_local `hk),
lift x to ℕ with y hy,
guard_hyp y : ℕ, guard_hyp hy : ↑y = x, guard_target (↑l = ↑m + x),
lift z to ℕ with w,
guard_hyp w : ℕ, tactic.success_if_fail (tactic.get_local `z),
lift u to ℕ using hu with u rfl hu,
guard_hyp hu : (0 : ℤ) ≤ ↑u,
all_goals { exfalso, assumption },
end
-- test lift of functions
example (α : Type*) (f : α → ℤ) (hf : ∀ a, 0 ≤ f a) (hf' : ∀ a, f a < 1) (a : α) : 0 ≤ 2 * f a :=
begin
lift f to α → ℕ using hf,
guard_target ((0:ℤ) ≤ 2 * (λ i : α, (f i : ℤ)) a),
guard_hyp hf' : ∀ a, ((λ i : α, (f i:ℤ)) a) < 1,
trivial
end
instance can_lift_unit : can_lift unit unit :=
⟨id, λ x, true, λ x _, ⟨x, rfl⟩⟩
/- test whether new instances of `can_lift` are added as simp lemmas -/
run_cmd do l ← can_lift_attr.get_cache, guard (`can_lift_unit ∈ l)
/- test error messages -/
example (n : ℤ) (hn : 0 < n) : true :=
begin
success_if_fail_with_msg {lift n to ℕ using hn} "lift tactic failed. The type of\n hn\nis
0 < n\nbut it is expected to be\n 0 ≤ n",
success_if_fail_with_msg {lift (n : option ℤ) to ℕ}
"Failed to find a lift from option ℤ to ℕ. Provide an instance of\n can_lift (option ℤ) ℕ",
trivial
end
example (n : ℤ) : ℕ :=
begin
success_if_fail_with_msg {lift n to ℕ}
"lift tactic failed. Tactic is only applicable when the target is a proposition.",
exact 0
end
end lift
private meta def get_exception_message (t : lean.parser unit) : lean.parser string
| s := match t s with
| result.success a s' := result.success "No exception" s
| result.exception none pos s' := result.success "Exception no msg" s
| result.exception (some msg) pos s' := result.success (msg ()).to_string s
end
@[user_command] meta def test_parser1_fail_cmd
(_ : interactive.parse (lean.parser.tk "test_parser1")) : lean.parser unit :=
do
let msg := "oh, no!",
let t : lean.parser unit := tactic.fail msg,
s ← get_exception_message t,
if s = msg then tactic.skip
else interaction_monad.fail "Message was corrupted while being passed through `lean.parser.of_tactic`"
.
-- Due to `lean.parser.of_tactic'` priority, the following *should not* fail with
-- a VM check error, and instead catch the error gracefully and just
-- run and succeed silently.
test_parser1
section category_theory
open category_theory
variables {C : Type} [category.{1} C]
example (X Y Z W : C) (x : X ⟶ Y) (y : Y ⟶ Z) (z z' : Z ⟶ W) (w : X ⟶ Z)
(h : x ≫ y = w)
(h' : y ≫ z = y ≫ z') :
x ≫ y ≫ z = w ≫ z' :=
begin
rw [h',reassoc_of h],
end
end category_theory
section is_eta_expansion
/- test the is_eta_expansion tactic -/
open function tactic
structure my_equiv (α : Sort*) (β : Sort*) :=
(to_fun : α → β)
(inv_fun : β → α)
(left_inv : left_inverse inv_fun to_fun)
(right_inv : right_inverse inv_fun to_fun)
infix ` my≃ `:25 := my_equiv
protected def my_rfl {α} : α my≃ α :=
⟨id, λ x, x, λ x, rfl, λ x, rfl⟩
def eta_expansion_test : ℕ × ℕ := ((1,0).1,(1,0).2)
run_cmd do e ← get_env, x ← e.get `eta_expansion_test,
let v := (x.value.get_app_args).drop 2,
let nms := [`prod.fst, `prod.snd],
guard $ expr.is_eta_expansion_test (nms.zip v) = some `((1, 0))
def eta_expansion_test2 : ℕ my≃ ℕ :=
⟨my_rfl.to_fun, my_rfl.inv_fun, λ x, rfl, λ x, rfl⟩
run_cmd do e ← get_env, x ← e.get `eta_expansion_test2,
let v := (x.value.get_app_args).drop 2,
projs ← e.structure_fields_full `my_equiv,
b ← expr.is_eta_expansion_aux x.value (projs.zip v),
guard $ b = some `(@my_rfl ℕ)
run_cmd do e ← get_env, x1 ← e.get `eta_expansion_test, x2 ← e.get `eta_expansion_test2,
b1 ← expr.is_eta_expansion x1.value,
b2 ← expr.is_eta_expansion x2.value,
guard $ b1 = some `((1, 0)) ∧ b2 = some `(@my_rfl ℕ)
structure my_str (n : ℕ) := (x y : ℕ)
def dummy : my_str 3 := ⟨1, 1⟩
def wrong_param : my_str 2 := ⟨dummy.1, dummy.2⟩
def right_param : my_str 3 := ⟨dummy.1, dummy.2⟩
run_cmd do e ← get_env,
x ← e.get `wrong_param, o ← x.value.is_eta_expansion,
guard o.is_none,
x ← e.get `right_param, o ← x.value.is_eta_expansion,
guard $ o = some `(dummy)
end is_eta_expansion
section elide
variables {x y z w : ℕ}
variables (h : x + y + z ≤ w)
(h' : x ≤ y + z + w)
include h h'
example : x + y + z ≤ w :=
begin
elide 0 at h,
elide 2 at h',
guard_hyp h : @hidden _ (x + y + z ≤ w),
guard_hyp h' : x ≤ @has_add.add (@hidden Type nat) (@hidden (has_add nat) nat.has_add)
(@hidden ℕ (y + z)) (@hidden ℕ w),
unelide at h,
unelide at h',
guard_hyp h' : x ≤ y + z + w,
exact h, -- there was a universe problem in `elide`. `exact h` lets the kernel check
-- the consistency of the universes
end
end elide
section struct_eq
@[ext]
structure foo (α : Type*) :=
(x y : ℕ)
(z : {z // z < x})
(k : α)
(h : x < y)
example {α : Type*} : Π (x y : foo α), x.x = y.x → x.y = y.y → x.z == y.z → x.k = y.k → x = y :=
foo.ext
example {α : Type*} : Π (x y : foo α), x = y ↔ x.x = y.x ∧ x.y = y.y ∧ x.z == y.z ∧ x.k = y.k :=
foo.ext_iff
example {α} (x y : foo α) (h : x = y) : y = x :=
begin
ext,
{ guard_target' y.x = x.x, rw h },
{ guard_target' y.y = x.y, rw h },
{ guard_target' y.z == x.z, rw h },
{ guard_target' y.k = x.k, rw h },
end
end struct_eq
section ring_exp
example (a b : ℤ) (n : ℕ) : (a + b)^(n + 2) = (a^2 + 2 * a * b + b^2) * (a + b)^n := by ring_exp
end ring_exp
section clear'
example (a : ℕ) (b : fin a) : unit :=
begin
success_if_fail { clear a b }, -- fails since `b` depends on `a`
success_if_fail { clear' a }, -- fails since `b` depends on `a`
clear' a b,
guard_hyp_nums 0,
exact ()
end
example (a : ℕ) : fin a → unit :=
begin
success_if_fail { clear' a }, -- fails since the target depends on `a`
success_if_fail { clear_dependent a }, -- ditto
exact λ _, ()
end
example (a : unit) : unit :=
begin
-- Check we fail with an error (but don't segfault) if hypotheses are repeated.
success_if_fail { clear' a a },
success_if_fail { clear_dependent a a },
exact ()
end
example (a a a : unit) : unit :=
begin
-- If there are multiple hypotheses with the same name,
-- `clear'`/`clear_dependent` currently clears only the last.
clear' a,
clear_dependent a,
guard_hyp_nums 1,
exact ()
end
end clear'
section clear_dependent
example (a : ℕ) (b : fin a) : unit :=
begin
success_if_fail { clear' a }, -- fails since `b` depends on `a`
clear_dependent a,
guard_hyp_nums 0,
exact ()
end
end clear_dependent
section simp_rw
example {α β : Type} {f : α → β} {t : set β} :
(∀ s, f '' s ⊆ t) = ∀ s : set α, ∀ x ∈ s, x ∈ f ⁻¹' t :=
by simp_rw [set.image_subset_iff, set.subset_def]
end simp_rw
section local_definitions
/- Some tactics about local definitions.
Testing revert_deps, revert_after, generalize', clear_value. -/
open tactic
example {A : ℕ → Type} {n : ℕ} : let k := n + 3, l := k + n, f : A k → A k := id in
∀(x : A k) (y : A (n + k)) (z : A n) (h : k = n + n), unit :=
begin
intros, guard_target unit,
do { e ← get_local `k, e1 ← tactic.local_def_value e, e2 ← to_expr ```(n + 3), guard $ e1 = e2 },
do { e ← get_local `n, success_if_fail_with_msg (tactic.local_def_value e)
"Variable n is not a local definition." },
do { success_if_fail_with_msg (tactic.local_def_value `(1 + 2))
"No such hypothesis 1 + 2." },
revert_deps k, tactic.intron 5, guard_target unit,
revert_after n, tactic.intron 7, guard_target unit,
do { e ← get_local `k, tactic.revert_deps e, l ← local_context, guard $ e ∈ l, intros },
exact unit.star
end
example {A : ℕ → Type} {n : ℕ} : let k := n + 3, l := k + n, f : A k → A (n+3) := id in
∀(x : A k) (y : A (n + k)) (z : A n) (h : k = n + n), unit :=
begin
intros,
success_if_fail_with_msg {generalize : n + k = x}
"generalize tactic failed, failed to find expression in the target",
generalize' : n + k = x,
generalize' h : n + k = y,
exact unit.star
end
example {A : ℕ → Type} {n : ℕ} : let k := n + 3, l := k + n, f : A k → A (n+3) := id in
∀(x : A k) (y : A (n + k)) (z : A n) (h : k = n + n), unit :=
begin
intros,
tactic.to_expr ```(n + n) >>= λ e, tactic.generalize' e `xxx,
success_if_fail_with_msg {clear_value n}
"Cannot clear the body of n. It is not a local definition.",
success_if_fail_with_msg {clear_value k}
"Cannot clear the body of k. The resulting goal is not type correct.",
clear_value k f,
get_local `k, -- test that `k` is not renamed.
exact unit.star
end
example {A : ℕ → Type} {n : ℕ} : let k := n + 3, l := k + n, f : A k → A k := id in
∀(x : A k) (y : A (n + k)) (z : A n) (h : k = n + n), unit :=
begin
intros,
clear_value k f,
exact unit.star
end
/-- test `clear_value` and the preservation of naming -/
example : ∀ x y : ℤ, let z := x + y in x = z - y → x = y - z → true :=
begin
introv h h,
guard_hyp x : ℤ,
guard_hyp y : ℤ,
guard_hyp z : ℤ := x + y,
guard_hyp h : x = y - z,
suffices : true, -- test the type of the second assumption named `h`
{ clear h,
guard_hyp h : x = z - y,
assumption },
do { to_expr ```(z) >>= is_local_def },
clear_value z,
guard_hyp z : ℤ,
success_if_fail { do { to_expr ```(z) >>= is_local_def } },
guard_hyp h : x = y - z,
suffices : true,
{ clear h,
guard_hyp h : x = z - y,
assumption },
trivial
end
/- Test whether generalize' always uses the exact name stated by the user, even if that name already
exists. -/
example (n : Type) (k : ℕ) : k = 5 → unit :=
begin
generalize' : 5 = n,
guard_target (k = n → unit),
intro, constructor
end
/- Test that `generalize'` works correctly with argument `h`, when the expression occurs in the
target -/
example (n : Type) (k : ℕ) : k = 5 → unit :=
begin
generalize' h : 5 = n,
guard_target (k = n → unit),
intro, constructor
end
end local_definitions
section set_attribute
open tactic
@[user_attribute] meta def my_user_attribute : user_attribute unit bool :=
{ name := `my_attr,
descr := "",
parser := return ff }
run_cmd do nm ← get_user_attribute_name `library_note, guard $ nm = `library_note_attr
run_cmd do nm ← get_user_attribute_name `higher_order, guard $ nm = `tactic.higher_order_attr
run_cmd do success_if_fail $ get_user_attribute_name `zxy.xzy
run_cmd set_attribute `norm `prod.map tt
run_cmd success_if_fail $ set_attribute `higher_order `prod.map tt
run_cmd success_if_fail $ set_attribute `my_attr `prod.map
run_cmd success_if_fail $ set_attribute `norm `xyz.zxy
run_cmd success_if_fail $ set_attribute `zxy.xyz `prod.map
end set_attribute