Skip to content

Commit 2a2a3d8

Browse files
committed
feat: MonadBacktrack for GrindM
1 parent d00d3aa commit 2a2a3d8

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ structure State where
219219
-/
220220
anchors : PHashMap ExprPtr UInt64 := {}
221221

222+
instance : Nonempty State :=
223+
.intro {}
224+
222225
private opaque MethodsRefPointed : NonemptyType.{0}
223226
def MethodsRef : Type := MethodsRefPointed.type
224227
instance : Nonempty MethodsRef := by exact MethodsRefPointed.property
@@ -228,6 +231,26 @@ abbrev GrindM := ReaderT MethodsRef $ ReaderT Context $ StateRefT State MetaM
228231
@[inline] def mapGrindM [MonadControlT GrindM m] [Monad m] (f : {α : Type} → GrindM α → GrindM α) {α} (x : m α) : m α :=
229232
controlAt GrindM fun runInBase => f <| runInBase x
230233

234+
/--
235+
Backtrackable state for the `GrindM` monad.
236+
-/
237+
structure SavedState where
238+
«meta» : Meta.SavedState
239+
grind : State
240+
deriving Nonempty
241+
242+
protected def saveState : GrindM SavedState :=
243+
return { «meta» := (← Meta.saveState), grind := (← get) }
244+
245+
/-- Restore backtrackable parts of the state. -/
246+
def SavedState.restore (b : SavedState) : GrindM Unit := do
247+
b.meta.restore
248+
set b.grind
249+
250+
instance : MonadBacktrack SavedState GrindM where
251+
saveState := Grind.saveState
252+
restoreState s := s.restore
253+
231254
/--
232255
`withoutReportingMVarIssues x` executes `x` without reporting metavariables found during internalization.
233256
See comment at `Grind.Context.reportMVarIssue` for additional details.

0 commit comments

Comments
 (0)