Zulip Chat Archive

Stream: metaprogramming / tactics

Topic: Are commands automatically rolled back on error?


Jannis Limperg (Feb 22 2024 at 15:09):

I'm writing a command which can throw an error after it's already modified the environment. Does anyone know if these modifications are automatically rolled back when an error occurs? Usually I would use commitIfNoEx for this, but CommandElabM doesn't seem to implement MonadBacktrack.

Adam Topaz (Feb 22 2024 at 15:41):

looks like the answer is no:

import Lean

open Lean Elab Command

#eval show CommandElabM Unit from do
  elabCommand <|  `(def $(mkIdent `foo) : Nat := 37)
  throwError "Error"

#eval foo
-- 37

Adam Topaz (Feb 22 2024 at 15:48):

Is there any reason not to add the following instance?

instance : MonadBacktrack Command.State CommandElabM where
  saveState := get
  restoreState := set

Last updated: May 02 2025 at 03:31 UTC