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`.
75 lines
1.1 KiB
Text
75 lines
1.1 KiB
Text
import Lean.Data.PersistentArray
|
|
|
|
def check (x : IO Nat) (expected : IO Nat) : IO Unit := do
|
|
unless (← x) == (← expected) do
|
|
throw $ IO.userError "unexpected result"
|
|
|
|
def f1 (xs : Lean.PArray Nat) (top : Nat) : IO Nat := do
|
|
let mut sum := 0
|
|
for x in xs do
|
|
if x % 2 == 0 then
|
|
IO.println s!"x: {x}"
|
|
sum := sum + x
|
|
if sum > top then
|
|
return sum
|
|
IO.println s!"sum: {sum}"
|
|
return sum
|
|
|
|
/--
|
|
info: x: 2
|
|
x: 4
|
|
x: 10
|
|
---
|
|
info: 16
|
|
-/
|
|
#guard_msgs in
|
|
#eval f1 [1, 2, 3, 4, 5, 10, 20].toPArray' 10
|
|
|
|
/--
|
|
info: x: 2
|
|
x: 4
|
|
x: 10
|
|
-/
|
|
#guard_msgs in
|
|
#eval check (f1 [1, 2, 3, 4, 5, 10, 20].toPArray' 10) (pure 16)
|
|
|
|
def f2 (xs : Lean.PArray Nat) (top : Nat) : IO Nat := do
|
|
let mut sum := 0
|
|
for x in xs do
|
|
if x % 2 == 0 then
|
|
IO.println s!"x: {x}"
|
|
sum := sum + x
|
|
if sum > top then
|
|
break
|
|
IO.println s!"sum: {sum}"
|
|
return sum
|
|
|
|
/--
|
|
info: x: 100
|
|
x: 98
|
|
x: 96
|
|
x: 94
|
|
x: 92
|
|
x: 90
|
|
x: 88
|
|
x: 86
|
|
x: 84
|
|
x: 82
|
|
x: 80
|
|
x: 78
|
|
x: 100
|
|
x: 98
|
|
x: 96
|
|
x: 94
|
|
x: 92
|
|
x: 90
|
|
x: 88
|
|
x: 86
|
|
x: 84
|
|
x: 82
|
|
x: 80
|
|
x: 78
|
|
sum: 1068
|
|
-/
|
|
#guard_msgs in
|
|
#eval check (f1 (List.iota 100).toPArray' 1000) (f2 (List.iota 100).toPArray' 1000)
|