Skip to content

Commit e7f4fc9

Browse files
leodemourakim-em
andauthored
fix: theorems without parameters in grind E-matching (#11604)
This PR fixes how theorems without parameters are handled in `grind`. This is a better fix than #11579 --------- Co-authored-by: Kim Morrison <[email protected]>
1 parent d145b9f commit e7f4fc9

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

src/Lean/Meta/Tactic/Grind/EMatch.lean

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -865,9 +865,15 @@ private def matchEqBwdPat (p : Expr) : M Unit := do
865865
if isSameExpr n.next false then return ()
866866
curr := n.next
867867

868+
def instantiateGroundTheorem (thm : EMatchTheorem) : M Unit := do
869+
if (← markTheoremInstance thm.proof #[]) then
870+
addNewInstance thm thm.proof 0 []
871+
868872
def ematchTheorem (thm : EMatchTheorem) : M Unit := do
869873
if (← checkMaxInstancesExceeded) then return ()
870-
withReader (fun ctx => { ctx with thm }) do
874+
if thm.numParams == 0 then
875+
instantiateGroundTheorem thm
876+
else withReader (fun ctx => { ctx with thm }) do
871877
let ps := thm.patterns
872878
match ps, (← read).useMT with
873879
| [p], _ => if isEqBwdPattern p then matchEqBwdPat p else main p []
@@ -909,7 +915,8 @@ private def ematchCore (extraThms : Array EMatchTheorem) : GoalM InstanceMap :=
909915
else
910916
let (_, s) ← go (← get).ematch.thms (← get).ematch.newThms |>.run
911917
modify fun s => { s with
912-
ematch.thms := s.ematch.thms ++ s.ematch.newThms
918+
-- **Note**: Ground theorems should be instantiated once. So, we filter them.
919+
ematch.thms := s.ematch.thms ++ (s.ematch.newThms.filter fun thm => thm.numParams > 0)
913920
ematch.newThms := {}
914921
ematch.gmt := s.ematch.gmt + 1
915922
ematch.num := s.ematch.num + 1

tests/lean/run/grind_ctor_ematch.lean

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ h : ¬Even 16
2424
[facts] Asserted facts
2525
[prop] ¬Even 16
2626
[prop] Even 14 → Even 16
27+
[prop] Even 0
2728
[prop] Even 12 → Even 14
2829
[prop] Even 10 → Even 12
2930
[prop] Even 8 → Even 10
3031
[prop] Even 6 → Even 8
3132
[eqc] True propositions
33+
[prop] Even 0
3234
[prop] Even 14 → Even 16
3335
[prop] Even 12 → Even 14
3436
[prop] Even 10 → Even 12
@@ -43,12 +45,12 @@ h : ¬Even 16
4345
[prop] Even 6
4446
[ematch] E-matching patterns
4547
[thm] Even.plus_two: [Even (#1 + 2)]
46-
[thm] Even.zero: [Even `[0]]
4748
[limits] Thresholds reached
4849
[limit] maximum number of E-matching rounds has been reached, threshold: `(ematch := 5)`
4950
[grind] Diagnostics
5051
[thm] E-Matching instances
5152
[thm] Even.plus_two ↦ 5
53+
[thm] Even.zero ↦ 1
5254
-/
5355
#guard_msgs (error) in
5456
example : Even 16 := by

tests/lean/run/grind_eq.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ set_option trace.grind.assert true
1818
trace: [grind.assert] f (y + 1) = a
1919
[grind.assert] ¬a = g (f y)
2020
[grind.ematch.instance] f.eq_2: f y.succ = g (f y)
21+
[grind.ematch.instance] f.eq_1: f 0 = 10
2122
[grind.assert] f (y + 1) = g (f y)
2223
-/
2324
#guard_msgs (trace) in
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
opaque π : Rat
2+
axiom pi_pos : 0 < π
3+
4+
theorem two_pi_pos : 0 < 2 * π := by
5+
grind [pi_pos]
6+
7+
/--
8+
info: Try these:
9+
[apply] grind only [pi_pos]
10+
[apply] grind =>
11+
instantiate only [pi_pos]
12+
linarith
13+
-/
14+
#guard_msgs in
15+
theorem two_pi_pos' : 0 < 2 * π := by
16+
grind? [pi_pos]
17+
18+
attribute [grind .] pi_pos
19+
20+
theorem two_pi_pos'' : 0 < 2 * π := by
21+
grind
22+
23+
/--
24+
info: Try these:
25+
[apply] grind only [pi_pos]
26+
[apply] grind =>
27+
instantiate only [pi_pos]
28+
linarith
29+
-/
30+
#guard_msgs in
31+
theorem two_pi_pos''' : 0 < 2 * π := by
32+
grind?
33+
34+
def f (x : Nat) := x
35+
36+
-- Should not instantiate/activate pi_pos
37+
/--
38+
trace: [grind.ematch] activated `f.eq_1`, [f #0]
39+
[grind.ematch.instance] f.eq_1: f x = x
40+
-/
41+
#guard_msgs (drop error, trace) in
42+
example : 1 < x → 2 < f x := by
43+
set_option trace.grind.ematch true in
44+
set_option trace.grind.ematch.instance true in
45+
grind [f]
46+
47+
-- Should instantiate/activate pi_pos
48+
/-- trace: [grind.ematch] activated `pi_pos`, [@LT.lt `[Rat] `[Rat.instLT] `[0] `[π]] -/
49+
#guard_msgs in
50+
example : π < x → 0 < x := by
51+
set_option trace.grind.ematch true in
52+
grind

0 commit comments

Comments
 (0)