lean4-htt/tests/lean/run/backtrackable_estate.lean
Leonardo de Moura e4a3b434d7 chore: moving tests to new frontend
@Kha The transition has begun :)
I found and fixed a few bugs, but it is going well so far.
2020-09-10 18:00:34 -07:00

30 lines
700 B
Text

import Init.System.IO
new_frontend
structure MyState :=
(bs : Nat := 0) -- backtrackable state
(ps : Nat := 0) -- non backtrackable state
instance : HasRepr MyState :=
⟨fun s => repr (s.bs, s.ps)⟩
instance : EStateM.Backtrackable Nat MyState :=
{ save := fun s => s.bs,
restore := fun s d => { s with bs := d } }
abbrev M := EStateM String MyState
def bInc : M Unit := -- increment backtrackble counter
modify $ fun s => { s with bs := s.bs + 1 }
def pInc : M Unit := -- increment nonbacktrackable counter
modify $ fun s => { s with ps := s.ps + 1 }
def tst : M MyState :=
do bInc;
pInc;
((bInc *> throw "failed") <|> pInc);
pInc;
get
#eval tst.run' {} -- (some (1, 3))