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`.
106 lines
2.4 KiB
Text
106 lines
2.4 KiB
Text
#check @Array.mk
|
|
|
|
def v : Array Nat := Array.mk [1, 2, 3, 4]
|
|
|
|
def w : Array Nat :=
|
|
(mkArray 9 1).push 3
|
|
|
|
#check @Array.casesOn
|
|
|
|
def f : Fin w.size → Nat :=
|
|
w.get
|
|
|
|
def arraySum (a : Array Nat) : Nat :=
|
|
a.foldl Nat.add 0
|
|
|
|
#guard mkArray 4 1 == #[1, 1, 1, 1]
|
|
|
|
#guard Array.map (fun x => x+10) v == #[11, 12, 13, 14]
|
|
|
|
#guard f ⟨1, sorry⟩ == 1
|
|
|
|
#guard f ⟨9, sorry⟩ == 3
|
|
|
|
#guard (((mkArray 1 1).push 2).push 3).foldl (fun x y => x + y) 0 == 6
|
|
|
|
#guard arraySum (mkArray 10 1) == 10
|
|
|
|
axiom axLt {a b : Nat} : a < b
|
|
|
|
|
|
#guard #[1, 2, 3].insertAt 0 10 == #[10, 1, 2, 3]
|
|
#guard #[1, 2, 3].insertAt 1 10 == #[1, 10, 2, 3]
|
|
#guard #[1, 2, 3].insertAt 2 10 == #[1, 2, 10, 3]
|
|
#guard #[1, 2, 3].insertAt 3 10 == #[1, 2, 3, 10]
|
|
#guard #[].insertAt 0 10 == #[10]
|
|
|
|
def tst1 : IO Unit :=
|
|
#[1, 2, 3, 4].forRevM IO.println
|
|
|
|
/--
|
|
info: 4
|
|
3
|
|
2
|
|
1
|
|
-/
|
|
#guard_msgs in
|
|
#eval tst1
|
|
|
|
#guard #[1, 2].extract 0 1 == #[1]
|
|
#guard #[1, 2].extract 0 0 == #[]
|
|
#guard #[1, 2].extract 0 2 == #[1, 2]
|
|
|
|
#guard #[1, 2, 3, 4].filterMap (fun x => if x % 2 == 0 then some (x + 10) else none) == #[12, 14]
|
|
|
|
def tst : IO (List Nat) :=
|
|
[1, 2, 3, 4].filterMapM fun x => do
|
|
IO.println x;
|
|
(if x % 2 == 0 then pure $ some (x + 10) else pure none)
|
|
|
|
/--
|
|
info: 1
|
|
2
|
|
3
|
|
4
|
|
---
|
|
info: [12, 14]
|
|
-/
|
|
#guard_msgs in
|
|
#eval tst
|
|
|
|
#guard #[1, 3, 6, 2].getMax? (fun a b => a < b) == some 6
|
|
#guard #[].getMax? (fun (a b : Nat) => a < b) == none
|
|
#guard #[1, 8].getMax? (fun a b => a < b) == some 8
|
|
#guard #[8, 1].getMax? (fun a b => a < b) == some 8
|
|
|
|
#guard #[1, 6, 5, 3, 8, 2, 0].partition (fun x => x % 2 == 0) == (#[6, 8, 2, 0], #[1, 5, 3])
|
|
|
|
def check (b : Bool) : IO Unit :=
|
|
unless b do throw $ IO.userError "check failed"
|
|
|
|
#guard #[].isPrefixOf #[2, 3]
|
|
#guard (#[] : Array Nat).isPrefixOf #[]
|
|
#guard #[2, 3].isPrefixOf #[2, 3]
|
|
#guard #[2, 3].isPrefixOf #[2, 3, 4, 5]
|
|
#guard ! #[2, 4].isPrefixOf #[2, 3]
|
|
#guard ! #[2, 3, 4].isPrefixOf #[2, 3]
|
|
#guard ! #[2].isPrefixOf #[]
|
|
#guard ! #[4, 3].isPrefixOf #[2, 3, 4, 5]
|
|
|
|
#guard #[1, 2, 3].allDiff
|
|
#guard !#[1, 2, 1, 3].allDiff
|
|
#guard #[1, 2, 4, 3].allDiff
|
|
#guard (#[] : Array Nat).allDiff
|
|
#guard !#[1, 1].allDiff
|
|
#guard !#[1, 2, 3, 4, 5, 1].allDiff
|
|
#guard #[1, 2, 3, 4, 5].allDiff
|
|
#guard !#[1, 2, 3, 4, 5, 5].allDiff
|
|
#guard !#[1, 3, 3, 4, 5].allDiff
|
|
#guard !#[1, 2, 3, 4, 5, 3].allDiff
|
|
#guard !#[1, 2, 3, 4, 5, 4].allDiff
|
|
#guard #[1, 2, 3, 4, 5, 6].allDiff
|
|
|
|
#guard Array.zip #[1, 2] #[3, 4, 6] == #[(1, 3), (2, 4)]
|
|
|
|
theorem ex1 (a b c : Nat) : #[a, b].push c = #[a, 0, c].set! 1 b :=
|
|
rfl
|