Skip to content

Commit 3359da6

Browse files
committed
fix: panic during the processing of generalized patterns in grind
This PR fixes a panic that occurred during the processing of generalized E-matching patterns in `grind`. Closes #10983
1 parent 4a11150 commit 3359da6

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,16 +528,34 @@ private def assignGeneralizedPatternProof (mvarId : MVarId) (eqProof : Expr) (or
528528
reportEMatchIssue! "invalid generalized pattern at `{origin.pp}`\nfailed to assign {mkMVar mvarId}\nwith{indentExpr eqProof}"
529529
failure
530530

531+
/--
532+
At `processDelayed`, `lhs` is extracted using `instantiateMVars`. We know it is structurally equal to a term in `assignment`, but
533+
it is not necessarily pointer equal. This can happen when the type of `lhs` depends on previous parameters, and we have a
534+
metavariable application.
535+
If the `lhs` has already been internalized, nothing needs to be done. Otherwise, we traverse the assignment looking for
536+
a term that is structurally equal.
537+
**Note**: If this solution is not robust enough, we should store the original `lhs` when we perform an delayed assignment.
538+
-/
539+
private def findOriginalGeneralizedPatternLhs (lhs : Expr) : OptionT (StateT Choice M) Expr := do
540+
if (← alreadyInternalized lhs) then return lhs
541+
for val in (← get).assignment do
542+
if val == lhs then
543+
return val
544+
reportEMatchIssue! "invalid generalized pattern at `{(← read).thm.origin.pp}`\nwhen processing argument{indentExpr lhs}"
545+
failure
546+
531547
/-- Helper function for `applyAssignment. -/
532548
private def processDelayed (mvars : Array Expr) (i : Nat) (h : i < mvars.size) : OptionT (StateT Choice M) Unit := do
533549
let thm := (← read).thm
534550
let mvarId := mvars[i].mvarId!
535551
let mvarIdType ← instantiateMVars (← mvarId.getType)
536552
match_expr mvarIdType with
537553
| Eq α lhs rhs =>
554+
let lhs ← findOriginalGeneralizedPatternLhs lhs
538555
let rhs ← preprocessGeneralizedPatternRHS lhs rhs thm.origin mvarIdType
539556
assignGeneralizedPatternProof mvarId (← mkEqProof lhs rhs) thm.origin
540557
| HEq α lhs β rhs =>
558+
let lhs ← findOriginalGeneralizedPatternLhs lhs
541559
let rhs ← preprocessGeneralizedPatternRHS lhs rhs thm.origin mvarIdType
542560
assignGeneralizedPatternProof mvarId (← mkHEqProof lhs rhs) thm.origin
543561
| _ =>

tests/lean/run/grind_10983.lean

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
structure Equiv (α : Type _) (β : Type _) where
2+
toFun : α → β
3+
invFun : β → α
4+
left_inv : ∀ x, invFun (toFun x) = x
5+
6+
def sumEquivSigmaBool (α β) : Equiv (α ⊕ β) (Σ b, bif b then β else α) where
7+
toFun s := s.elim (fun x => ⟨false, x⟩) fun x => ⟨true, x⟩
8+
invFun s :=
9+
match s with
10+
| ⟨false, a⟩ => .inl a
11+
| ⟨true, b⟩ => .inr b
12+
left_inv := fun s => by
13+
fail_if_success grind
14+
sorry

0 commit comments

Comments
 (0)