Skip to content

Commit 43171d1

Browse files
committed
More hacks for experiemnts
1 parent 0e53664 commit 43171d1

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

src/Lean/Meta/Match/MatchEqs.lean

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
197194
private 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
/--
412421
Generate the congruence equations for the given match auxiliary declaration.
413422
The 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

tests/lean/run/matchCongrEqns.lean

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ info: myTest.match_1.splitter.{u_1, u_2} {α : Type u_1} (motive : List α → S
2121
#guard_msgs(pass trace, all) in
2222
#check myTest.match_1.splitter
2323
/--
24-
info: myTest.match_1.congr_eq_1.{u_1, u_2} {α : Type u_1} (motive : List α → Sort u_2) (x : List α)
25-
(h_1 : (a : α) → (dc : List α) → x = a :: dc → motive (a :: dc)) (h_2 : (x' : List α) → x = x' → motive x') (a : α)
26-
(dc : List α) (heq : x = a :: dc) :
27-
(match h : x with
24+
info: private theorem myTest.match_1.congr_eq_1.{u_1, u_2} : ∀ {α : Type u_1} (motive : List α → Sort u_2) (x : List α)
25+
(h_1 : (a : α) → (dc : List α) → x = a :: dc → motive (a :: dc)) (h_2 : (x' : List α) → x = x' → motive x') (a : α)
26+
(dc : List α) (heq : x = a :: dc),
27+
(match h : x with
2828
| a :: dc => h_1 a dc h
2929
| x' => h_2 x' h) ≍
3030
h_1 a dc heq
3131
-/
3232
#guard_msgs(pass trace, all) in
33-
#check myTest.match_1.congr_eq_1
33+
#print sig myTest.match_1.congr_eq_1
3434

3535
/--
3636
info: myTest.match_1.congr_eq_2.{u_1, u_2} {α : Type u_1} (motive : List α → Sort u_2) (x✝ : List α)

0 commit comments

Comments
 (0)