Skip to content

Commit 36bf8d2

Browse files
committed
feat(tactic/elim_cast): remove cast expressions from goal
1 parent 2560c4b commit 36bf8d2

File tree

5 files changed

+48
-21
lines changed

5 files changed

+48
-21
lines changed

data/prod.lean

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Authors: Johannes Hölzl
66
Extends theory on products
77
-/
88

9+
import tactic.ext
10+
911
universes u v
1012
variables {α : Type u} {β : Type v}
1113

@@ -23,6 +25,7 @@ namespace prod
2325
lemma ext_iff {p q : α × β} : p = q ↔ p.1 = q.1 ∧ p.2 = q.2 :=
2426
by rw [← @mk.eta _ _ p, ← @mk.eta _ _ q, mk.inj_iff]
2527

28+
@[extensionality]
2629
lemma ext {α β} {p q : α × β} : p.1 = q.1 → p.2 = q.2 → p = q :=
2730
by rw [ext_iff] ; intros ; split ; assumption
2831

@@ -60,4 +63,3 @@ theorem lex_def (r : α → α → Prop) (s : β → β → Prop)
6063
end
6164

6265
end prod
63-

logic/basic.lean

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,3 +631,15 @@ lemma classical.nonempty_pi {α : Sort u} {β : α → Sort v} :
631631
iff.intro (assume ⟨f⟩ a, ⟨f a⟩) (assume f, ⟨assume a, classical.choice $ f a⟩)
632632

633633
end nonempty
634+
635+
lemma cast_eq_of_heq {α : Sort*} {β : Sort*} {x : α} {y : β}
636+
(h : α = β)
637+
(h' : x == y) :
638+
cast h x = y :=
639+
by cc
640+
641+
lemma heq_of_cast_eq {α β} {x : α} {y : β}
642+
(h : α = β)
643+
(h' : cast h x = y) :
644+
x == y :=
645+
by cc

tactic/cache.lean

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11

22
import tactic.basic
33

4+
open lean
45
open lean.parser
56

67
local postfix `?`:9001 := optional
78
local postfix *:9001 := many
89

910
namespace tactic
1011
namespace interactive
11-
open interactive interactive.types
12+
open interactive interactive.types expr
1213

1314
/-- Unfreeze local instances, which allows us to revert
1415
instances in the context. -/

tactic/ext.lean

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/--
2+
Tag lemmas of the form:
3+
4+
```
5+
lemma my_collection.ext (a b : my_collection)
6+
(h : ∀ x, a.lookup x = b.lookup y) :
7+
a = b := ...
8+
```
9+
-/
10+
@[user_attribute]
11+
meta def extensional_attribute : user_attribute :=
12+
{ name := `extensionality,
13+
descr := "lemmas usable by `ext` tactic" }
14+
15+
attribute [extensionality] _root_.funext array.ext

tactic/interactive.lean

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Authors: Mario Carneiro
55
-/
66
import data.dlist data.dlist.basic data.prod category.basic
77
tactic.basic tactic.rcases tactic.generalize_proofs
8-
tactic.split_ifs meta.expr logic.basic
8+
tactic.split_ifs meta.expr tactic.ext
9+
logic.basic
910

1011
open lean
1112
open lean.parser
@@ -197,7 +198,6 @@ do let h := h.get_or_else `this,
197198

198199
meta def symm_apply (e : expr) (cfg : apply_cfg := {}) : tactic (list (name × expr)) :=
199200
tactic.apply e cfg <|> (symmetry >> tactic.apply e cfg)
200-
201201
/--
202202
`apply_assumption` looks for an assumption of the form `... → ∀ _, ... → head`
203203
where `head` matches the current goal.
@@ -280,7 +280,7 @@ repeat (do
280280
try (refine ``( or_iff_not_imp_left.mpr _)),
281281
try (refine ``( or_iff_not_imp_right.mpr _)),
282282
gs' ← get_goals,
283-
guard (gs ≠ gs') ) ;
283+
guard (gs ≠ gs') );
284284
repeat
285285
(reflexivity <|> solve_by_elim <|>
286286
constructor_matching none [``(_ ∧ _),``(_ ↔ _),``(Exists _)] ) ;
@@ -289,22 +289,6 @@ done
289289
/-- Shorter name for the tactic `tautology`. -/
290290
meta def tauto := tautology
291291

292-
/--
293-
Tag lemmas of the form:
294-
295-
```
296-
lemma my_collection.ext (a b : my_collection)
297-
(h : ∀ x, a.lookup x = b.lookup y) :
298-
a = b := ...
299-
```
300-
-/
301-
@[user_attribute]
302-
meta def extensional_attribute : user_attribute :=
303-
{ name := `extensionality,
304-
descr := "lemmas usable by `ext` tactic" }
305-
306-
attribute [extensionality] _root_.funext array.ext prod.ext
307-
308292
/--
309293
`ext1 id` selects and apply one extensionality lemma (with attribute
310294
`extensionality`), using `id`, if provided, to name a local constant
@@ -537,5 +521,18 @@ meta def apply_field : tactic unit :=
537521
propagate_tags $
538522
get_current_field >>= applyc
539523

524+
/-- `elim_cast e with x` matches on `cast _ e` in the goal and replaces it with
525+
`x`. It also adds `Hx : e == x` as an assumption.
526+
527+
`elim_cast! e with x` acts similarly but reverts `Hx`.
528+
-/
529+
meta def elim_cast (rev : parse (tk "!")?) (e : parse texpr) (n : parse (tk "with" *> ident)) : tactic unit :=
530+
do h ← get_unused_name ("H" ++ n.to_string : string),
531+
interactive.generalize h () (``(cast _ %%e), n),
532+
asm ← get_local h,
533+
to_expr ``(heq_of_cast_eq _ %%asm) >>= note h none,
534+
tactic.clear asm,
535+
when rev.is_some (interactive.revert [n])
536+
540537
end interactive
541538
end tactic

0 commit comments

Comments
 (0)