Skip to content

Commit 135e7e7

Browse files
authored
fix: instance tactic generation at finish? (#10846)
This PR fixes a few issues on `instance only [...]` tactic generation at `finish?`.
1 parent 58a884e commit 135e7e7

File tree

8 files changed

+360
-18
lines changed

8 files changed

+360
-18
lines changed

src/Lean/Elab/Tactic/Grind/BuiltinTactic.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def logAnchor (numDigits : Nat) (anchorPrefix : UInt64) (e : Expr) : TermElabM U
264264
-/
265265
Term.addTermInfo' stx e
266266
logAt (severity := .information) (isSilent := true) stx
267-
m!"#{anchorToString numDigits anchorPrefix} := {e}"
267+
m!"#{anchorPrefixToString numDigits anchorPrefix} := {e}"
268268

269269
@[builtin_grind_tactic cases] def evalCases : GrindTactic := fun stx => do
270270
let `(grind| cases #$anchor:hexnum) := stx | throwUnsupportedSyntax

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,9 @@ If `s?` is `none` just returns `true`.
276276
-/
277277
def checkSeqAt (s? : Option SavedState) (goal : Goal) (seq : List TGrind) : GrindM Bool := do
278278
let some s := s? | return true
279-
let tac ← mkGrindParen seq
280279
Lean.withoutModifyingState do
281280
s.restore
281+
let tac ← mkGrindParen seq
282282
-- **Note**: Ensure tracing is disabled.
283283
withTheReader Grind.Context (fun ctx => { ctx with config.trace := false }) do
284284
try

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ def hashName (n : Name) : UInt64 :=
2323
else if isPrivateName n then
2424
hash (privateToUserName n)
2525
else if n.isInternal then
26-
match n with
27-
| .str p _ | .num p _ => hashName p
28-
| _ => 0
26+
/-
27+
**Note** We consider hashing the prefix, but it was not good enough.
28+
For example, an internal name may depend on the name of theorem being defined.
29+
Moreover, changing a `theorem` to an `example` would break anchors.
30+
-/
31+
0
2932
else
3033
hash n
3134

@@ -45,8 +48,10 @@ public partial def getAnchor (e : Expr) : GrindM UInt64 := do
4548
"instability". Recall that `match` auxiliary declarations are reused.
4649
-/
4750
if (← isMatcher declName) then pure 0
48-
else pure <| hash declName
49-
| .fvar fvarId => pure <| hashName (← fvarId.getDecl).userName
51+
else pure <| hashName declName
52+
| .fvar fvarId =>
53+
let userName := (← fvarId.getDecl).userName
54+
pure <| hashName userName
5055
| .mdata _ b => getAnchor b
5156
| .letE n v t b _ =>
5257
pure <| mix (hashName n) <| mix (← getAnchor t) <| mix (← getAnchor v) (← getAnchor b)

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,12 @@ where
481481
let mut proof := proof
482482
let mut prop := prop
483483
if (← getConfig).trace then
484-
let uniqueId ← mkFreshId
484+
/-
485+
**Note**: It is incorrect to use `mkFreshId` here because we use `withFreshNGen` at
486+
`instantiateTheorem`. So, we generate an unique id by using the number of instances generated so far.
487+
The code relies on the fact that `addTheoremInstance` bumps this counter.
488+
-/
489+
let uniqueId := Name.num `_grind_inst (← getNumTheoremInstances)
485490
proof := markTheoremInstanceProof proof uniqueId
486491
modify fun s => { s with instanceMap := s.instanceMap.insert uniqueId thm }
487492
if (← isMatchEqLikeDeclName thm.origin.key) then

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def getAllTheorems (map : EMatch.InstanceMap) : Array EMatchTheorem :=
8686
map.toArray.map (·.2)
8787

8888
public def instantiate' : Action := fun goal kna kp => do
89-
let s ← saveStateIfTracing
89+
let saved? ← saveStateIfTracing
9090
let ((progress, map), goal') ← GoalM.run goal ematch'
9191
if progress then
9292
match (← kp goal') with
@@ -95,7 +95,7 @@ public def instantiate' : Action := fun goal kna kp => do
9595
let proof ← instantiateMVars (mkMVar goal.mvarId)
9696
let usedThms := collect proof map
9797
let newSeq ← mkNewSeq goal usedThms seq (approx := false)
98-
if (← checkSeqAt s goal newSeq) then
98+
if (← checkSeqAt saved? goal newSeq) then
9999
return .closed newSeq
100100
else
101101
let allThms := getAllTheorems map

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,10 @@ def addNewRawFact (proof : Expr) (prop : Expr) (generation : Nat) (splitSource :
902902
which is not definitionally equal with `reducible` transparency setting}"
903903
modify fun s => { s with newRawFacts := s.newRawFacts.enqueue { proof, prop, generation, splitSource } }
904904

905+
/-- Returns the number of theorem instances generated so far. -/
906+
def getNumTheoremInstances : GoalM Nat := do
907+
return (← get).ematch.numInstances
908+
905909
/-- Adds a new theorem instance produced using E-matching. -/
906910
def addTheoremInstance (thm : EMatchTheorem) (proof : Expr) (prop : Expr) (generation : Nat) : GoalM Unit := do
907911
saveEMatchTheorem thm

0 commit comments

Comments
 (0)