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`.
138 lines
3.7 KiB
Text
138 lines
3.7 KiB
Text
prelude
|
||
import Init.System.IO
|
||
import Init.Data.List.Control
|
||
import Init.Data.ToString
|
||
|
||
|
||
open IO.FS
|
||
|
||
def check_eq {α} [BEq α] [Repr α] (tag : String) (expected actual : α) : IO Unit :=
|
||
unless (expected == actual) do
|
||
throw $ IO.userError $
|
||
s!"assertion failure \"{tag}\":\n expected: {repr expected}\n actual: {repr actual}"
|
||
|
||
def test : IO Unit := do
|
||
let xs : ByteArray := ⟨#[1,2,3,4]⟩;
|
||
let fn := "foo.txt";
|
||
withFile fn Mode.write fun h => do
|
||
h.write xs;
|
||
h.write xs;
|
||
pure ();
|
||
let ys ← withFile "foo.txt" Mode.read $ fun h => h.read 10;
|
||
check_eq "1" (xs.toList ++ xs.toList) ys.toList;
|
||
withFile fn Mode.append fun h => do
|
||
h.write ⟨#[5,6,7,8]⟩;
|
||
pure ();
|
||
withFile "foo.txt" Mode.read fun h => do
|
||
let ys ← h.read 10
|
||
check_eq "2" [1,2,3,4,1,2,3,4,5,6] ys.toList
|
||
let ys ← h.read 2
|
||
check_eq "3" [7,8] ys.toList
|
||
let ys ← h.read 2
|
||
check_eq "5" [] ys.toList
|
||
pure ()
|
||
|
||
#guard_msgs in
|
||
#eval test
|
||
|
||
def test2 : IO Unit := do
|
||
let fn1 := "bar2.txt";
|
||
let fn2 := "foo2.txt";
|
||
let xs₀ : String := "⟨[₂,α]⟩";
|
||
let xs₁ := "⟨[6,8,@]⟩";
|
||
let xs₂ := "/* Handle.getLine : Handle → IO Unit */" ++
|
||
"/* The line returned by `lean_io_prim_handle_get_line` */" ++
|
||
"/* is truncated at the first \'\\0\' character and the */" ++
|
||
"/* rest of the line is discarded. */";
|
||
-- multi-buffer line
|
||
withFile fn1 Mode.write $ fun _h => pure ();
|
||
|
||
withFile fn1 Mode.write $ fun h => do
|
||
h.putStr xs₀
|
||
h.putStrLn xs₀
|
||
h.putStrLn xs₂
|
||
h.putStrLn xs₁
|
||
withFile fn2 Mode.write $ fun h => h.putStr "overwritten"
|
||
rename fn1 fn2
|
||
let ys ← withFile fn2 Mode.read $ fun h => h.getLine;
|
||
IO.println ys;
|
||
check_eq "1" (xs₀ ++ xs₀ ++ "\n") ys;
|
||
IO.println ys;
|
||
withFile fn2 Mode.append $ fun h => do
|
||
{ h.putStrLn xs₁;
|
||
pure () };
|
||
let ys ← withFile fn2 Mode.read $ fun h => do
|
||
{ let ys ← (List.iota 4).mapM $ fun i => do
|
||
{ let ln ← h.getLine;
|
||
IO.println i;
|
||
IO.println ∘ repr $ ln;
|
||
pure ln };
|
||
pure ys };
|
||
IO.println ys;
|
||
let rs := [xs₀ ++ xs₀ ++ "\n", xs₂ ++ "\n", xs₁ ++ "\n", xs₁ ++ "\n"];
|
||
check_eq "2" rs ys;
|
||
let ys ← readFile fn2;
|
||
check_eq "3" (String.join rs) ys;
|
||
pure ()
|
||
|
||
/--
|
||
info: ⟨[₂,α]⟩⟨[₂,α]⟩
|
||
|
||
⟨[₂,α]⟩⟨[₂,α]⟩
|
||
|
||
4
|
||
"⟨[₂,α]⟩⟨[₂,α]⟩\n"
|
||
3
|
||
"/* Handle.getLine : Handle → IO Unit *//* The line returned by `lean_io_prim_handle_get_line` *//* is truncated at the first '\\0' character and the *//* rest of the line is discarded. */\n"
|
||
2
|
||
"⟨[6,8,@]⟩\n"
|
||
1
|
||
"⟨[6,8,@]⟩\n"
|
||
[⟨[₂,α]⟩⟨[₂,α]⟩
|
||
, /* Handle.getLine : Handle → IO Unit *//* The line returned by `lean_io_prim_handle_get_line` *//* is truncated at the first '\0' character and the *//* rest of the line is discarded. */
|
||
, ⟨[6,8,@]⟩
|
||
, ⟨[6,8,@]⟩
|
||
]
|
||
-/
|
||
#guard_msgs in
|
||
#eval test2
|
||
|
||
def test3 : IO Unit := do
|
||
let fn3 := "foo3.txt"
|
||
let xs₀ := "abc"
|
||
let xs₁ := ""
|
||
let xs₂ := "hello"
|
||
let xs₃ := "world"
|
||
withFile fn3 Mode.write $ fun _h => do {
|
||
pure ()
|
||
}
|
||
let ys ← lines fn3
|
||
IO.println $ repr ys
|
||
check_eq "1" ys #[]
|
||
withFile fn3 Mode.write $ fun h => do
|
||
h.putStrLn xs₀
|
||
h.putStrLn xs₁
|
||
h.putStrLn xs₂
|
||
h.putStrLn xs₃
|
||
let ys ← lines fn3
|
||
IO.println $ repr ys
|
||
check_eq "2" ys #[xs₀, xs₁, xs₂, xs₃]
|
||
pure ()
|
||
|
||
/--
|
||
info: #[]
|
||
#["abc", "", "hello", "world"]
|
||
-/
|
||
#guard_msgs in
|
||
#eval test3
|
||
|
||
def test4 : IO Unit := do
|
||
let fn4 := "foo4.txt"
|
||
withFile fn4 Mode.write fun _h => do pure ();
|
||
let ys ← withFile fn4 Mode.read $ fun h => h.read 1;
|
||
check_eq "1" [] ys.toList
|
||
let ys ← withFile fn4 Mode.read $ fun h => h.read 1;
|
||
check_eq "2" [] ys.toList
|
||
|
||
#guard_msgs in
|
||
#eval test4
|