Skip to content

Commit db4206f

Browse files
authored
fix: instantiate metavariables in hypotheses in grind (#11315)
This PR fixes an issue affecting `grind -revert`. In this mode, assigned metavariables in hypotheses were not being instantiated. This issue was affecting two files in Mathlib.
1 parent a0772dc commit db4206f

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

src/Lean/Meta/Tactic/Grind/Arith/Cutsat/MBTC.lean

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ private def getAssignmentExt? (e : Expr) : GoalM (Option Rat) := do
1616
if let some val ← getAssignment? (← get) e then
1717
-- Easy case when `e : Int`
1818
return some val
19+
/-
20+
**Note**: The following code assumes that instantiated mvars occurring in types
21+
have been instantiated.
22+
-/
1923
let type ← inferType e
2024
if type == Int.mkType then
2125
-- It should have been handled in the previous getAssignment?

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ See issue #11806 for a motivating example.
270270
def withProtectedMCtx [Monad m] [MonadControlT MetaM m] [MonadLiftT MetaM m]
271271
[MonadExcept Exception m] [MonadRuntimeException m]
272272
(abstractProof : Bool) (mvarId : MVarId) (k : MVarId → m α) : m α := do
273+
/-
274+
**Note**: `instantiateGoalMVars` here also instantiates mvars occurring in hypotheses.
275+
This is particularly important when using `grind -revert`.
276+
-/
277+
let mvarId ← mvarId.instantiateGoalMVars
273278
let mvarId ← mvarId.abstractMVars
274279
let mvarId ← mvarId.clearImplDetails
275280
tryCatchRuntimeEx (main mvarId) fun ex => do

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ def _root_.Lean.MVarId.ensureNoMVar (mvarId : MVarId) : MetaM Unit := do
1919
if type.hasExprMVar then
2020
throwTacticEx `grind mvarId "goal contains metavariables"
2121

22+
/--
23+
Instantiates metavariables occurring in the target and hypotheses.
24+
-/
25+
def _root_.Lean.MVarId.instantiateGoalMVars (mvarId : MVarId) : MetaM MVarId := do
26+
mvarId.checkNotAssigned `grind
27+
let mvarDecl ← mvarId.getDecl
28+
let lctx ← instantiateLCtxMVars mvarDecl.lctx
29+
let type ← Lean.instantiateMVars mvarDecl.type
30+
let mvarNew ← mkFreshExprMVarAt lctx mvarDecl.localInstances type .syntheticOpaque mvarDecl.userName
31+
mvarId.assign mvarNew
32+
return mvarNew.mvarId!
33+
2234
/-- Abstracts metavariables occurring in the target. -/
2335
def _root_.Lean.MVarId.abstractMVars (mvarId : MVarId) : MetaM MVarId := do
2436
mvarId.checkNotAssigned `grind
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace Nat
2+
3+
def ascFactorial (n : Nat) : Nat → Nat
4+
| 0 => 1
5+
| k + 1 => (n + k) * ascFactorial n k
6+
7+
def descFactorial (n : Nat) : Nat → Nat
8+
| 0 => 1
9+
| k + 1 => (n - k) * descFactorial n k
10+
11+
theorem descFactorial_of_lt {n : Nat} : ∀ {k : Nat}, n < k → n.descFactorial k = 0 := by sorry
12+
13+
theorem add_descFactorial_eq_ascFactorial' (n : Nat) :
14+
∀ k : Nat, (n + k - 1).descFactorial k = n.ascFactorial k := by sorry
15+
16+
def ascFactorialBinary (n k : Nat) : Nat :=
17+
match k with
18+
| 0 => 1
19+
| 1 => n
20+
| k@(_ + 2) => ascFactorialBinary n (k / 2) * ascFactorialBinary (n + k / 2) ((k + 1) / 2)
21+
22+
theorem ascFactorial_eq_ascFactorialBinary : ascFactorial = ascFactorialBinary := sorry
23+
24+
def descFactorialBinary (n k : Nat) : Nat :=
25+
if n < k then 0
26+
else ascFactorialBinary (n - k + 1) k
27+
28+
theorem descFactorial_eq_descFactorialBinary : descFactorial = descFactorialBinary := by
29+
ext n k
30+
rw [descFactorialBinary]
31+
split <;> rename_i h
32+
· rw [descFactorial_of_lt h]
33+
· rw [← ascFactorial_eq_ascFactorialBinary, ← add_descFactorial_eq_ascFactorial']
34+
grind
35+
36+
end Nat

0 commit comments

Comments
 (0)