@@ -19,10 +19,10 @@ def newTactic (x : Lean.Elab.Tactic.TacticM α) : Lean.Elab.Tactic.TacticM α :=
19
19
private def castToProp (e: Lean.Expr) : Lean.Elab.Tactic.TacticM (Option Q(Prop )) := do
20
20
Qq.checkTypeQ (u := Lean.levelOne) e q(Prop )
21
21
22
- -- getHypotheses returns the hypotheses as an array of tuples of (Hypothesis name, Q(Prop))
22
+ -- getHypothesesProp returns the hypotheses as an array of tuples of (Hypothesis name, Q(Prop))
23
23
-- This way the hypothesis Q(Prop) can be used in a pattern match and
24
24
-- the name can be used to refer to the hypothesis in other tactics
25
- def getHypotheses : Lean.Elab.Tactic.TacticM (Array (Lean.Syntax.Ident × Q(Prop ))) := do
25
+ def getHypothesesProp : Lean.Elab.Tactic.TacticM (Array (Lean.Syntax.Ident × Q(Prop ))) := do
26
26
let mut res := #[]
27
27
for localDecl in ← Lean.MonadLCtx.getLCtx do
28
28
if let some typ ← castToProp localDecl.type then
@@ -32,7 +32,7 @@ def getHypotheses : Lean.Elab.Tactic.TacticM (Array (Lean.Syntax.Ident × Q(Prop
32
32
33
33
-- a tactic that prints the hypotheses and their types
34
34
elab "print_hypotheses" : tactic => do
35
- let hyps ← getHypotheses
35
+ let hyps ← getHypothesesProp
36
36
for (name, ty) in hyps do
37
37
Lean.logInfo m!"{name}: {ty}"
38
38
@@ -41,14 +41,37 @@ example (_H1: 1 = 1) (_H2: 2 = 2): True := by
41
41
simp
42
42
43
43
-- a tactic that prints the hypotheses, but only if they match a pattern
44
- local elab "example_print_rfl_hypotheses " : tactic => do
45
- let hyps ← getHypotheses
44
+ local elab "example_print_rfl_hypotheses_prop " : tactic => do
45
+ let hyps ← getHypothesesProp
46
46
for (name, ty) in hyps do
47
47
if let ~q($x = $y) := ty then
48
48
if ← Lean.Meta.isExprDefEq x y then
49
49
Lean.logInfo m!"{name} is rfl"
50
50
51
51
example (_H1 : x = 5 ) (_H2 : 2 = 2 ) (_H3: y = y) (H4: 5 = 4 ): False := by
52
+ example_print_rfl_hypotheses_prop
53
+ contradiction
54
+
55
+ def getHypotheses : Lean.Elab.Tactic.TacticM (Array (Lean.Syntax.Ident × Lean.Expr)) := do
56
+ let mut res := #[]
57
+ for localDecl in ← Lean.MonadLCtx.getLCtx do
58
+ let name := Lean.mkIdent localDecl.userName
59
+ res := res.push (name, localDecl.type)
60
+ return res
61
+
62
+ -- use match_expr instead of Qq
63
+ -- see examples in https://github.com/leanprover/lean4/blob/a5162ca7489bfdbf1a2851cffd8fdcca9d2b9b56/src/Lean/Elab/Tactic/Omega/Frontend.lean#L431
64
+ local elab "example_print_rfl_hypotheses" : tactic => do
65
+ let hyps ← getHypotheses
66
+ for (name, ty) in hyps do
67
+ match_expr ty with
68
+ | Eq _ x y =>
69
+ if ← Lean.Meta.isExprDefEq x y then
70
+ Lean.logInfo m!"{name} is rfl"
71
+ | _ =>
72
+ continue
73
+
74
+ example (_G1 : x > 5 ) (_G2 : 2 = 2 ) (_G3: y = y) (G4: 5 = 4 ): False := by
52
75
example_print_rfl_hypotheses
53
76
contradiction
54
77
@@ -59,7 +82,7 @@ def run (t: Lean.Elab.Tactic.TacticM (Lean.TSyntax `tactic)): Lean.Elab.Tactic.T
59
82
60
83
-- an example tactic that applies a hypothesis to the goal if it matches a pattern
61
84
local elab "example_apply_hypothesis" : tactic => do
62
- let hyps ← getHypotheses
85
+ let hyps ← getHypothesesProp
63
86
for (name, ty) in hyps do
64
87
if let ~q((((($a : Prop )) → $b) : Prop )) := ty then
65
88
run `(tactic| apply $name )
@@ -78,7 +101,7 @@ def getGoalProp : Lean.Elab.Tactic.TacticM Q(Prop) := do
78
101
-- An example to check whether the goal is already an hypothesis
79
102
local elab "example_assumption_tactic" : tactic => do
80
103
let goal ← getGoalProp
81
- let hyps ← getHypotheses
104
+ let hyps ← getHypothesesProp
82
105
for (name, ty) in hyps do
83
106
if ← Lean.Meta.isExprDefEq ty goal then
84
107
run `(tactic| exact $name )
@@ -129,7 +152,7 @@ def mkHyp (suggestion: String) (t: Lean.Elab.Tactic.TacticM (Lean.TSyntax `term)
129
152
-- an example that tries to apply a bunch to tactics to specific patterns
130
153
local elab "example_combo_tactic" : tactic => do
131
154
let goal ← getGoalProp
132
- let hyps ← getHypotheses
155
+ let hyps ← getHypothesesProp
133
156
if let ~q($x /\ $y) := goal then
134
157
if ← Lean.Meta.isExprDefEq x y then
135
158
-- x /\ x -> x
@@ -167,7 +190,7 @@ def applyIn (name: Lean.Ident) (t: Lean.Elab.Tactic.TacticM (Lean.TSyntax `term)
167
190
return ()
168
191
169
192
local elab "example_applyin_tactic" : tactic => do
170
- let hyps ← getHypotheses
193
+ let hyps ← getHypothesesProp
171
194
for (name, ty) in hyps do
172
195
match ty with
173
196
| ~q((((($a : Prop )) → $b)) /\ ($a')) =>
0 commit comments