@@ -3,83 +3,27 @@ Copyright (c) 2022 Newell Jensen. All rights reserved.
3
3
Released under Apache 2.0 license as described in the file LICENSE.
4
4
Authors: Newell Jensen
5
5
-/
6
- import Lean
7
6
import Mathlib.Lean.Meta
7
+ import Std.Tactic.Relation.Rfl
8
8
9
9
/-!
10
- # `rfl` tactic extension for reflexive relations
10
+ # `Lean.MVarId.liftReflToEq`
11
11
12
- This extends the `rfl` tactic so that it works on any reflexive relation,
13
- provided the reflexivity lemma has been marked as `@[refl]`.
12
+ Convert a goal of the form `x ~ y` into the form `x = y`, where `~` is a reflexive
13
+ relation, that is, a relation which has a reflexive lemma tagged with the attribute `[refl]`.
14
+ If this can't be done, returns the original `MVarId`.
14
15
-/
15
16
16
17
namespace Mathlib.Tactic
17
18
18
- open Lean Meta
19
-
20
- /-- Environment extensions for `refl` lemmas -/
21
- initialize reflExt :
22
- SimpleScopedEnvExtension (Name × Array (DiscrTree.Key true )) (DiscrTree Name true ) ←
23
- registerSimpleScopedEnvExtension {
24
- addEntry := fun dt (n, ks) ↦ dt.insertCore ks n
25
- initial := {}
26
- }
27
-
28
- initialize registerBuiltinAttribute {
29
- name := `refl
30
- descr := "reflexivity relation"
31
- add := fun decl _ kind ↦ MetaM.run' do
32
- let declTy := (← getConstInfo decl).type
33
- let (_, _, targetTy) ← withReducible <| forallMetaTelescopeReducing declTy
34
- let fail := throwError
35
- "@[refl] attribute only applies to lemmas proving x ∼ x, got {declTy}"
36
- let .app (.app rel lhs) rhs := targetTy | fail
37
- unless ← withNewMCtxDepth <| isDefEq lhs rhs do fail
38
- let key ← DiscrTree.mkPath rel
39
- reflExt.add (decl, key) kind
40
- }
41
-
42
- open Elab Tactic
43
-
44
- /-- Closes the goal if the target has the form `x ~ x`, where `~` is a reflexive
45
- relation, that is, a relation which has a reflexive lemma tagged with the attribute `[refl]`.
46
- Otherwise throws an error.
47
-
48
- See also `Lean.MVarId.refl`, which is for `x = x` specifically.
49
-
50
- This is the `MetaM` implementation of the `rfl` tactic.
51
- -/
52
- def _root_.Lean.MVarId.rfl (goal : MVarId) : MetaM Unit := do
53
- let .app (.app rel _) _ ← withReducible goal.getType'
54
- | throwError "reflexivity lemmas only apply to binary relations, not
55
- {indentExpr (← goal.getType)}"
56
- let s ← saveState
57
- let mut ex? := none
58
- for lem in ← (reflExt.getState (← getEnv)).getMatch rel do
59
- try
60
- let gs ← goal.apply (← mkConstWithFreshMVarLevels lem)
61
- if gs.isEmpty then return () else
62
- logError <| MessageData.tagged `Tactic.unsolvedGoals <| m!"unsolved goals\n
63
- {goalsToMessageData gs}"
64
- catch e =>
65
- ex? := ex? <|> (some (← saveState, e)) -- stash the first failure of `apply`
66
- s.restore
67
- if let some (sErr, e) := ex? then
68
- sErr.restore
69
- throw e
70
- else
71
- throwError "rfl failed, no lemma with @[refl] applies"
19
+ open Lean Meta Elab Tactic
72
20
73
21
/--
74
22
This tactic applies to a goal whose target has the form `x ~ x`, where `~` is a reflexive
75
23
relation, that is, a relation which has a reflexive lemma tagged with the attribute [ refl ] .
76
24
-/
77
25
def rflTac : TacticM Unit :=
78
- withMainContext do liftMetaFinishingTactic (·.rfl)
79
-
80
- @[inherit_doc rflTac]
81
- elab_rules : tactic
82
- | `(tactic| rfl) => rflTac
26
+ withMainContext do liftMetaFinishingTactic (·.applyRfl)
83
27
84
28
/-- Helper theorem for `Lean.MVar.liftReflToEq`. -/
85
29
private theorem rel_of_eq_and_refl {α : Sort _} {R : α → α → Prop }
@@ -97,7 +41,7 @@ def _root_.Lean.MVarId.liftReflToEq (mvarId : MVarId) : MetaM MVarId := do
97
41
if rel.isAppOf `Eq then
98
42
-- No need to lift Eq to Eq
99
43
return mvarId
100
- for lem in ← (reflExt.getState (← getEnv)).getMatch rel do
44
+ for lem in ← (Std.Tactic. reflExt.getState (← getEnv)).getMatch rel do
101
45
let res ← observing? do
102
46
-- First create an equality relating the LHS and RHS
103
47
-- and reduce the goal to proving that LHS is related to LHS.
0 commit comments