lean4-htt/tests/lean/run/finally.lean
Kyle Miller fdd5aec172
feat: better #eval command (#5627)
This refactors and improves the `#eval` command, introducing some new
features.
* Now evaluated results can be represented using `ToExpr` and pretty
printing. This means **hoverable output**. If `ToExpr` fails, it then
tries `Repr` and then `ToString`. The `eval.pp` option controls whether
or not to try `ToExpr`.
* There is now **auto-derivation** of `Repr` instances, enabled with the
`pp.derive.repr` option (default to **true**). For example:
  ```lean
  inductive Baz
    | a | b

  #eval Baz.a
  -- Baz.a
  ```
It simply does `deriving instance Repr for Baz` when there's no way to
represent `Baz`. If core Lean gets `ToExpr` derive handlers, they could
be used here as well.
* The option `eval.type` controls whether or not to include the type in
the output. For now the default is false.
* Now things like `#eval do return 2` work. It tries using
`CommandElabM`, `TermElabM`, or `IO` when the monad is unknown.
* Now there is no longer `Lean.Eval` or `Lean.MetaEval`. These each used
to be responsible for both adapting monads and printing results. The
concerns have been split into two. (1) The `MonadEval` class is
responsible for adapting monads for evaluation (it is similar to
`MonadLift`, but instances are allowed to use default data when
initializing state) and (2) finding a way to represent results is
handled separately.
* Error messages about failed instance synthesis are now more precise.
Once it detects that a `MonadEval` class applies, then the error message
will be specific about missing `ToExpr`/`Repr`/`ToString` instances.
* Fixes a bug where `Repr`/`ToString` instances can't be found by
unfolding types "under the monad". For example, this works now:
  ```lean
  def Foo := List Nat
  def Foo.mk (l : List Nat) : Foo := l
  #eval show Lean.CoreM Foo from do return Foo.mk [1,2,3]
  ```
* Elaboration errors now abort evaluation. This eliminates some
not-so-relevant error messages.
* Now evaluating a value of type `m Unit` never prints a blank message.
* Fixes bugs where evaluating `MetaM` and `CoreM` wouldn't collect log
messages.

The `run_cmd`, `run_elab`, and `run_meta` commands are now frontends for
`#eval`.
2024-10-08 20:51:46 +00:00

123 lines
2.5 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

def checkM (b : IO Bool) : IO Unit :=
unless (← b) do throw $ IO.userError "failed"
abbrev M := ExceptT String $ StateRefT Nat IO
def f1 : M Nat :=
throw "error 1"
def f2 : M Nat :=
throwThe IO.Error $ IO.userError "error 2"
def tst1 : M Nat := do
try
try f1 finally set 100; IO.println "finisher executed"
catch _ =>
get
def checkE {α ε : Type} [BEq α] (x : IO (Except ε α)) (expected : α) : IO Unit := do
let r ← x;
match r with
| Except.ok a => unless a == expected do throw $ IO.userError "unexpected result"
| Except.error _ => throw $ IO.userError "unexpected error"
/--
info: finisher executed
---
info: Except.ok 100
-/
#guard_msgs in
#eval (tst1.run).run' 0
/-- info: finisher executed -/
#guard_msgs in
#eval checkE ((tst1.run).run' 0) 100
def tst2 : M Nat :=
tryCatchThe IO.Error
(tryFinally f2 (do set 100; IO.println "finisher executed"))
(fun _ => get)
/--
info: finisher executed
---
info: Except.ok 100
-/
#guard_msgs in
#eval (tst2.run).run' 0
/-- info: finisher executed -/
#guard_msgs in
#eval checkE ((tst2.run).run' 0) 100
def tst3 : M Nat :=
tryCatchThe IO.Error
(tryFinally
(tryFinally f1 (do set 100; IO.println "inner finisher executed"; discard $ f2; pure ()))
(do modify Nat.succ; IO.println "outer finisher executed"))
(fun _ => get)
/--
info: inner finisher executed
outer finisher executed
---
info: Except.ok 101
-/
#guard_msgs in
#eval (tst3.run).run' 0
/--
info: inner finisher executed
outer finisher executed
-/
#guard_msgs in
#eval checkE ((tst3.run).run' 0) 101
def tst4 : M Nat := do
let a ← tryFinally
(tryFinally (pure 42) (do set 100; IO.println "inner finisher executed"; pure ()))
(do modify Nat.succ; IO.println "outer finisher executed");
let s ← get;
pure (a + s)
/--
info: inner finisher executed
outer finisher executed
---
info: Except.ok 143
-/
#guard_msgs in
#eval (tst4.run).run' 0
/--
info: inner finisher executed
outer finisher executed
-/
#guard_msgs in
#eval checkE ((tst4.run).run' 0) 143
def tst5 : M Nat := do
let (a, _) ← tryFinally' (pure 42) (fun a? => do IO.println ("finalizer received: " ++ toString a?));
pure a
/--
info: finalizer received: (some 42)
---
info: Except.ok 42
-/
#guard_msgs in
#eval (tst5.run).run' 0
def tst6 : M Nat := do
let (a, _) ← tryFinally' f2 (fun a? => do IO.println ("finalizer received: " ++ toString a?));
pure a
def tst7 : IO Unit :=
tryCatchThe IO.Error (do discard $ (tst6.run).run' 0; pure ()) (fun _ => IO.println "failed as expected")
/--
info: finalizer received: none
failed as expected
-/
#guard_msgs in
#eval tst7