@@ -191,9 +191,6 @@ where
191191 (throwError "failed to generate equality theorem {thmName} for `match` expression `{matchDeclName}`\n {MessageData.ofGoal mvarId}" )
192192 subgoals.forM (go · (depth+1 ))
193193
194- private def useGrind : MetaM Bool := do
195- return ((← getEnv).getModuleIdx? `InitGrind).isSome && !debug.Meta.Match.MatchEqs.grindAsSorry.get (← getOptions)
196-
197194private partial def proveCongrEqThm (matchDeclName : Name) (thmName : Name) (mvarId : MVarId) : MetaM Unit := do
198195 withTraceNode `Meta.Match.matchEqs (msg := (return m!"{exceptEmoji ·} proveCondEqThm {thmName}" )) do
199196 let mvarId ← mvarId.deltaTarget (· == matchDeclName)
@@ -233,12 +230,12 @@ where
233230 <|>
234231 (substSomeVar mvarId)
235232 <|>
236- (do if (← useGrind) then
237- let r ← Grind.main mvarId (← Grind.mkParams {})
238- if r.hasFailed then throwError "grind failed"
239- else
233+ (do if debug.Meta.Match.MatchEqs.grindAsSorry.get (← getOptions) then
240234 trace[Meta.Match.matchEqs] "proveCondEqThm.go: grind_as_sorry is enabled, admitting goal"
241235 mvarId.admit (synthetic := true )
236+ else
237+ let r ← Grind.main mvarId (← Grind.mkParams {})
238+ if r.hasFailed then throwError "grind failed"
242239 return #[])
243240 <|>
244241 (throwError "failed to generate equality theorem {thmName} for `match` expression `{matchDeclName}`\n {MessageData.ofGoal mvarId}" )
@@ -408,6 +405,18 @@ def registerMatchCongrEqns (matchDeclName : Name) (eqnNames : Array Name) : Core
408405 modifyEnv fun env => matchCongrEqnsExt.modifyState env fun map =>
409406 map.insert matchDeclName eqnNames
410407
408+ private def _root_.Lean.MVarId.revertAll (mvarId : MVarId) : MetaM MVarId := mvarId.withContext do
409+ mvarId.checkNotAssigned `revertAll
410+ let mut toRevert := #[]
411+ for fvarId in (← getLCtx).getFVarIds do
412+ unless (← fvarId.getDecl).isAuxDecl do
413+ toRevert := toRevert.push fvarId
414+ mvarId.setKind .natural
415+ let (_, mvarId) ← mvarId.revert toRevert
416+ (preserveOrder := true )
417+ (clearAuxDeclsInsteadOfRevert := true )
418+ return mvarId
419+
411420/--
412421Generate the congruence equations for the given match auxiliary declaration.
413422The congruence equations have a completely unrestricted left-hand side (arbitrary discriminants),
@@ -473,7 +482,26 @@ where go baseName :=
473482 let thmType ← unfoldNamedPattern thmType
474483 let thmVal ← mkFreshExprSyntheticOpaqueMVar thmType
475484 let mvarId := thmVal.mvarId!
476- proveCongrEqThm matchDeclName thmName mvarId
485+ let canUseGrind := ((← getEnv).getModuleIdx? `InitGrind).isSome
486+ if canUseGrind then -- TMP
487+ proveCongrEqThm matchDeclName thmName mvarId
488+ else
489+ trace[Meta.Match.matchEqs] "proving congruence equation via normal equation"
490+ let mut mvarId := mvarId
491+ (_, mvarId) ← mvarId.revert (hs_discrs.map Expr.fvarId!) (preserveOrder := true )
492+ (_, mvarId) ← mvarId.revert (heqs.map Expr.fvarId!) (preserveOrder := true )
493+ trace[Meta.Match.matchEqs] "reverted:\n {mvarId}"
494+ -- TODO: Code duplication with below
495+ for _ in [:heqs.size] do
496+ let (fvarId, mvarId') ← mvarId.intro1
497+ -- important to substitute the fvar on the LHS, so do not use `substEq`
498+ let (fvarId, mvarId') ← heqToEq mvarId' fvarId
499+ (_, mvarId) ← substCore (symm := false ) (clearH := true ) mvarId' fvarId
500+ trace[Meta.Match.matchEqs] "subst'ed:\n {mvarId}"
501+ mvarId ← mvarId.revertAll
502+ let thmType ← mvarId.getType'
503+ trace[Meta.Match.matchEqs] "thmType: {thmType}"
504+ mvarId.assign (← proveCondEqThm matchDeclName thmName thmType)
477505 let thmVal ← mkExpectedTypeHint thmVal thmType
478506 let thmVal ← instantiateMVars thmVal
479507 mkLambdaFVars hs_discrs thmVal
0 commit comments