@@ -528,16 +528,34 @@ private def assignGeneralizedPatternProof (mvarId : MVarId) (eqProof : Expr) (or
528528 reportEMatchIssue! "invalid generalized pattern at `{origin.pp}`\n failed to assign {mkMVar mvarId}\n with{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}`\n when processing argument{indentExpr lhs}"
545+ failure
546+
531547/-- Helper function for `applyAssignment. -/
532548private 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 | _ =>
0 commit comments