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

159 lines
3.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 tstGetLine (str : String) : IO Unit := do
let path := "tmp_file";
IO.FS.withFile path IO.FS.Mode.write $ λ (h : IO.FS.Handle) =>
h.putStrLn str;
IO.FS.withFile path IO.FS.Mode.read $ λ (h : IO.FS.Handle) => do
let str' ← h.getLine;
IO.println str.length;
IO.println str'.length;
IO.print str';
unless (str'.length == str.length + 1) do
throw (IO.userError ("unexpected length: " ++ toString str'.trim.length));
unless (str'.trim == str) do
throw (IO.userError ("unexpected result: " ++ str'))
def tstGetLine2 (str1 str2 : String) : IO Unit := do
let path := "tmp_file";
IO.FS.withFile path IO.FS.Mode.write $ λ (h : IO.FS.Handle) => do {
h.putStrLn str1; h.putStr str2
};
IO.FS.withFile path IO.FS.Mode.read $ λ (h : IO.FS.Handle) => do
let str1' ← h.getLine;
let str2' ← h.getLine;
unless (str1'.length == str1.length + 1) do
throw (IO.userError ("unexpected length: " ++ toString str1'.trim.length));
unless (str1'.trim == str1) do
throw (IO.userError ("unexpected result: " ++ str1'));
unless (str2'.length == str2.length) do
throw (IO.userError ("unexpected length: " ++ toString str2'.trim.length));
unless (str2'.trim == str2) do
throw (IO.userError ("unexpected result: " ++ str2'))
def tstGetLine3 (str : String) : IO Unit := do
let path := "tmp_file";
IO.FS.withFile path IO.FS.Mode.write $ λ (h : IO.FS.Handle) => do {
h.putStrLn str
};
IO.FS.withFile path IO.FS.Mode.read $ λ (h : IO.FS.Handle) => do
(h.getLine >>= IO.println);
(h.getLine >>= IO.println);
(h.getLine >>= IO.println);
IO.print "done";
pure ()
/--
info: abc
done
-/
#guard_msgs in
#eval tstGetLine3 "abc"
/--
info: 40
41
αααααααααααααααααααααααααααααααααααααααα
-/
#guard_msgs in
#eval tstGetLine ("".pushn 'α' 40)
/--
info: 1
2
a
-/
#guard_msgs in
#eval tstGetLine "a"
/--
info: 0
1
-/
#guard_msgs in
#eval tstGetLine ""
/--
info: 20
21
αααααααααααααααααααα
-/
#guard_msgs in
#eval tstGetLine ("".pushn 'α' 20)
/--
info: 61
62
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
-/
#guard_msgs in
#eval tstGetLine ("".pushn 'a' 61)
/--
info: 61
62
ααααααααααααααααααααααααααααααααααααααααααααααααααααααααααααα
-/
#guard_msgs in
#eval tstGetLine ("".pushn 'α' 61)
/--
info: 62
63
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
-/
#guard_msgs in
#eval tstGetLine ("".pushn 'a' 62)
/--
info: 63
64
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
-/
#guard_msgs in
#eval tstGetLine ("".pushn 'a' 63)
/--
info: 64
65
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
-/
#guard_msgs in
#eval tstGetLine ("".pushn 'a' 64)
/--
info: 65
66
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
-/
#guard_msgs in
#eval tstGetLine ("".pushn 'a' 65)
/--
info: 66
67
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
-/
#guard_msgs in
#eval tstGetLine ("".pushn 'a' 66)
/--
info: 128
129
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
-/
#guard_msgs in
#eval tstGetLine ("".pushn 'a' 128)
#guard_msgs in
#eval tstGetLine2 ("".pushn 'α' 20) ("".pushn 'β' 20)
#guard_msgs in
#eval tstGetLine2 ("".pushn 'α' 40) ("".pushn 'β' 40)
#guard_msgs in
#eval tstGetLine2 ("".pushn 'a' 61) ("".pushn 'b' 61)
#guard_msgs in
#eval tstGetLine2 ("".pushn 'a' 61) ("".pushn 'b' 62)