lean4-htt/tests/lean/run/doNotation6.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

105 lines
1.7 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.

abbrev M := StateRefT Nat IO
def testM {α} [ToString α] [BEq α] (init : Nat) (expected : α) (x : M α): IO Unit := do
let v ← x.run' init
IO.println ("result " ++ toString v)
unless v == expected do
throw $ IO.userError "unexpected"
def dec (x : Nat) : M Unit := do
if (← get) == 0 then
throw $ IO.userError "value is zero"
modify (· - x)
def f1 (x : Nat) : M Nat := do
let v ←
try
dec x
return x
catch _ =>
return 1
def f2 (xs : List Nat) : M Nat := do
let mut sum := 0
for x in xs do
try
dec x
sum := sum + x
if sum > 100 then
break
continue
catch _ =>
break
return sum
/-- info: result 6 -/
#guard_msgs in
#eval testM 100 6 $ f2 [1, 2, 3]
/-- info: result 101 -/
#guard_msgs in
#eval testM 200 101 $ f2 [1, 100, 200, 300]
/-- info: result 1 -/
#guard_msgs in
#eval testM 1 1 $ f2 [1, 100, 200, 300]
def f3 (xs : List Nat) : M Nat := do
let mut sum := 0
for x in xs do
try
dec x
sum := sum + x
if sum > 100 then
return sum
continue
catch _ =>
return sum
return sum
/-- info: result 6 -/
#guard_msgs in
#eval testM 100 6 $ f3 [1, 2, 3]
/-- info: result 101 -/
#guard_msgs in
#eval testM 200 101 $ f3 [1, 100, 200, 300]
/-- info: result 1 -/
#guard_msgs in
#eval testM 1 1 $ f3 [1, 100, 200, 300]
def f4 (xs : Array Nat) : IO Nat := do
let mut sum := 0
for x in xs do
sum := sum + x
IO.println x
return sum
/--
info: 1
2
3
---
info: 6
-/
#guard_msgs in
#eval f4 #[1, 2, 3]
def f5 (xs : Array Nat) : IO Nat := do
let mut sum := 0
for x in xs[1 : xs.size - 1] do
sum := sum + x
IO.println x
return sum
/--
info: 2
3
4
5
---
info: 14
-/
#guard_msgs in
#eval f5 #[1, 2, 3, 4, 5, 6]