Commit graph

14391 commits

Author SHA1 Message Date
Leonardo de Moura
261dc999d0 refactor(frontends/lean/elaborator): mark thunk as opaque, and thunk A to A is now a coercion
@kha I was working in the new declaration type and using tasks there.
Since we don't have tasks yet in Lean, I decided to start refactoring
the `thunk` type. I defined it as:

```
-- TODO(Leo): mark as opaque, it is implemented by the new runtime
structure thunk (α : Type u) : Type u :=
(fn : unit → α)

def thunk.pure {α : Type u} (a : α) : thunk α :=
⟨λ _, a⟩

def thunk.get {α : Type u} (t : thunk α) : α :=
t.fn ()
```

The idea is to use the runtime primitives to implement them.
Then, I realized the support for `thunk`s in the elaborator are quite
hacky. Given `f x`, if `f`'s domain has type `thunk A`, we elaborate
`f x` as `f (fun _, x)` even if `x` has type `thunk A`.
This is quite bad, for example, suppose we have
```
def f (x : thunk A) := ...
```
Then, the following definition is type incorrect.
```
def g (x : thunk A) := f x
```
and we are forced to write
```
def g (x : thunk A) := f (x ())
```
The term `f (x ())` will be elaborated as `f (fun _, x ())` and an
unnecessary closure is created at runtime.

This mechanism inherited from Lean 3 is also incompatible with the
new thunk definition. Given `x : thunk A`, I want to write `x.get`
to retrieve the value instead of `x ()` as in Lean 3.
However, `x.get` expands into the nonsensical `(fun _, x).get`.

So, I decided to view the mapping `A` to `thunk A` as a "coercion".
I used double quotes, because it is a macro instead of a function.
If it were a coercion, then we would be using `thunk.pure` to coerce
values but this is not we want most of the time.
For example, given `f : thunk A -> B` and a term `t : A`, when we write
`f t`, we want it to be converted into `f (fun _, t)` instead of
`f (thunk.pure t)` which would eagerly compute `t`. The transformation
`t` into `fun _, t` is syntactic.
We cannot implement it using type classes. I implemented it as
a hard-coded extra case like the one from `Prop` to `bool`.
We can also add a coercion from `thunk A` to `A` to avoid the `.get`.

That being said, I had a few breakages in the code base since we only
use coercions when the given and expected type do not contain
metavariables.
2018-08-21 15:27:51 -07:00
Leonardo de Moura
dc477db71e chore(kernel/environment): remove environment::is_descendant
We will remove certified_declaration since we are adding tasks to the
kernel. We have removed `environment::replace`. We also don't need it
for checking the consistency of thread local caches since they will be
removed.
2018-08-21 13:25:58 -07:00
Leonardo de Moura
f6684b24c6 chore(kernel): cleanup 2018-08-21 13:09:27 -07:00
Leonardo de Moura
d9399924c9 chore(kernel/type_checker): hide aux procedure 2018-08-21 12:59:12 -07:00
Leonardo de Moura
027ce06f89 chore(kernel/environment): remove environment::replace
We added tasks to the runtime.
2018-08-21 12:47:08 -07:00
Leonardo de Moura
682cfa14e8 chore(library/init/lean/declaration): add new declaration type 2018-08-21 10:13:16 -07:00
Leonardo de Moura
47d74e1a5a chore(kernel/declaration): minor cleanup 2018-08-21 10:11:23 -07:00
Leonardo de Moura
6473bd1294 chore(frontends/lean/builtin_cmds): fix warning 2018-08-21 10:09:39 -07:00
Leonardo de Moura
04fbbbb0d6 refactor(library/init/lean/declaration): declaration -> constant_info
This is the first step in the declaration vs constant_info refactor.
Here is the design notes:

In Lean3, we use the `declaration` objects to represent new declarations
that are sent to the kernel, and to store information for all constants
declared in an environment.
This design decision was done when we did not have support for
mutual (meta) declarations, and information about inductive datatypes,
constructors and recursors were stored in an environment extension.
This design now seems weird since we have four different methods for
adding declarations into the environment:
```
    environment add(certified_declaration const & d) const;
    environment add_meta(buffer<declaration> const & ds, bool check = true) const;
    environment add(inductive_decl const & decl) const;
    environment add_quot() const;
```
Moreover, we use `mk_constant_assumption` to represent inductive
datatype, constructors, recursors, and `quot` primitives.
Since inductive datatypes, constructors, recursors and `quot` primitives
are not considered axioms, we have the method:
```
    bool environment::is_builtin(name const & n) const;
```

We can avoid these hacks by having a type for representing
declarations (i.e., objects that are sent to the kernel) and
objects for storing information of the constant declarations stored
in an environment object.

A `declaration` object is now of the form
```
inductive declaration
| defn_decl        (val : definition_val)
| axiom_decl       (val : axiom_val)
| thm_decl         (val : theorem_val)
| quot_decl
| mutual_defn_decl (defns : list definition_val) -- All definitions must be marked as `meta`
| induct_decl      (lparams : list name) (nparams : nat) (types : list inductive_type) (is_meta : bool)

/-
If we want, we can let users specify their own names for `quot`,
`quot.mk`, `quot.lift` and `quot.ind`. We just need to add them
as fields of `declaration.quot_decl`.
-/
```

When we check a declaration, one or more `constant_info` objects are
created for each new constant in the declaration.
```
inductive constant_info
| assump_info   (val : assumption_val)
| defn_info     (val : definition_val)
| axiom_info    (val : axiom_val)
| thm_info      (val : theorem_val)
| quot_info     (val : quot_val)
| induct_info   (val : inductive_val)
| cnstr_info    (val : constructor_val)
| rec_info      (val : recursor_val)
```
For simple declarations `constant` (aka `assumption`), `definition`,
`theorem` and `axiom`, the information stored in the `constant_info` is
identical to the information in the `declaration` object. This is
expected since these are the original Lean3 declarations.

The `environment` object stores a mapping from `name` to
`constant_info`. The function `check` validates a declaration
and produces a `certified_declaration`. A `certified_declaration` is
a pair `(declaration, list constant_info)`. The `list` here makes it
explicit that a declaration may add one or more new constants into the
environment. Finally, the `environment` object has a single `add`
method and a single `get` and `find`:
```
    environment add(certified_declaration const & d) const;

    /** \brief Return info for the constant with name \c n (if it is defined in this environment). */
    optional<constant_info> find(name const & n) const;

    /** \brief Return info for the constant with name \c n. Throws an
        exception if has not been declared in this environment. */
    constant_info get(name const & n) const;
```

Moreover, the method `environment::builtin` is not necessary anymore.
If `environment::get(n)` returns an `axiom_info` or an `assump_info`, then
we know for sure the constant named `n` has been postulated.

This commit only defined the new types in Lean. I still need to make
the changes to the C++ code base.
2018-08-21 09:59:30 -07:00
Leonardo de Moura
0b349f1abf chore(*): fix style 2018-08-21 09:32:01 -07:00
Leonardo de Moura
4314617a85 chore(library/init/io): remove dead code 2018-08-21 09:00:14 -07:00
Sebastian Ullrich
6dbe54a01b chore(library/init/io): rename io.print_ln to io.println 2018-08-21 08:43:10 -07:00
Sebastian Ullrich
3516c37ec9 feat(library/init/control/coroutine_io): coroutine_io 2018-08-21 08:43:10 -07:00
Sebastian Ullrich
a42fb533f4 fix(library/vm/vm_io): move all primitives into io
While `cmdline_args` has no side-effects, it is certainly not a pure function.
The `stdin` etc. should have been safe since all accessors are in `io`, but
better be safe than sorry.
2018-08-21 08:43:10 -07:00
Sebastian Ullrich
fb2ee24ba6 refactor(library/init/io): replace ioe with eio
Old MacDonald had a monad stack
eio = except_t io.error io
And in his stack he had I/O, io = state io.real_world
With a monad here and a monad there
Here a monad there a monad
Everywhere a monad!
2018-08-21 08:43:10 -07:00
Sebastian Ullrich
6b55e24ab7 feat(library/init/{io,control/except}): use lift_t to automatically upcast io and other errors 2018-08-21 08:43:09 -07:00
Sebastian Ullrich
9db688f4c2 fix(library/{vm/vm_io,init/io}): fix bugs and tests 2018-08-21 08:43:09 -07:00
Sebastian Ullrich
0936d4d81e feat(library/init/io): introduce has_eval class to customize #eval output 2018-08-21 08:43:09 -07:00
Sebastian Ullrich
37e5f03351 refactor(library/system/io): move into init 2018-08-21 08:43:09 -07:00
Sebastian Ullrich
a260bf91d4 refactor(library/vm/vm_io,library/system/io): remove io classes, make errors explicit 2018-08-21 08:43:09 -07:00
Sebastian Ullrich
a02fd7fb4c feat(library/noncomputable): sorts are computable
i.e.

constant io.real_world : Type
2018-08-21 08:43:09 -07:00
Sebastian Ullrich
6e3a6ff40f feat(library/noncomputable): mark all VM builtins as computable 2018-08-21 08:43:09 -07:00
Leonardo de Moura
fd9bc9e15b feat(frontends/lean/builtin_cmds): write/read compacted region file 2018-08-20 16:35:37 -07:00
Leonardo de Moura
8944cb6951 fix(frontends/lean/builtin_cmds): do not include time spent copying memory
We will read the data from the disk and give it directly to the
compacted_region object
2018-08-20 15:49:54 -07:00
Leonardo de Moura
38b23431a3 chore(runtime/compact): add inline 2018-08-20 15:30:21 -07:00
Leonardo de Moura
c75fb04322 feat(frontends/lean/builtin_cmds): add option for increasing the number of copies being compacted/serialized 2018-08-20 15:21:21 -07:00
Leonardo de Moura
7f9d131a1f chore(runtime/compact): one alloc per object 2018-08-20 14:46:06 -07:00
Leonardo de Moura
b8e4e94c91 feat(frontends/lean): add command for testing compacted regions 2018-08-20 14:39:15 -07:00
Leonardo de Moura
474a0c40c7 fix(runtime/compact): missing memcpy 2018-08-20 10:33:04 -07:00
Leonardo de Moura
dc1f5c0aa6 feat(runtime/object): task API functions can take thunks as arguments 2018-08-20 09:13:35 -07:00
Leonardo de Moura
a27aa53e88 refactor(runtime/compact): save task objects as thunks
TODO: modify task API and make sure all functions there can take thunks
instead of tasks as arguments.
2018-08-20 08:52:35 -07:00
Leonardo de Moura
db98397cc0 feat(runtime): object compactor
We need more testing and performance testing.
We also need to compare serializer and compacted_region.
2018-08-19 17:10:18 -07:00
Leonardo de Moura
ce504b4c21 feat(runtime/serializer): support for tasks 2018-08-18 14:52:29 -07:00
Leonardo de Moura
684085d93f refactor(runtime/object): delete data needed to execute task after it finishes 2018-08-18 14:33:27 -07:00
Leonardo de Moura
a0b5502821 fix(runtime/object): memory leak and simplify task_object
We remove per task condition_variable and use m_task_finished_cv.
The same condition_variable used to implement `wait_any`.
2018-08-18 10:29:12 -07:00
Leonardo de Moura
d0bc663f0d chore(runtime/object): avoid ugly handle_finished_rec
Users should not rely on the order the dependencies have beed inserted.
If the order matters, priorities should be used instead.
2018-08-17 18:15:58 -07:00
Leonardo de Moura
d52507c4b2 fix(runtime/object): memory leak 2018-08-17 18:12:17 -07:00
Leonardo de Moura
861592fe6a chore(runtime/object): cleanup 2018-08-17 15:43:01 -07:00
Leonardo de Moura
4bc8414d2b feat(runtime/object): use "weak pointers" in the task manager, and interrupt tasks at GC time 2018-08-17 15:35:00 -07:00
Leonardo de Moura
24444d65c4 refactor(runtime/object): do not use Lean runtime lists to implement the reverse dependency list in task objects 2018-08-17 14:42:43 -07:00
Leonardo de Moura
1d5411f455 feat(runtime/object): add support for io.wait_any 2018-08-17 13:04:06 -07:00
Leonardo de Moura
5f78087b08 feat(runtime/object): add support for io.has_finished 2018-08-17 12:36:48 -07:00
Leonardo de Moura
5e63e7806c chore(runtime/object): cleanup 2018-08-17 12:32:47 -07:00
Leonardo de Moura
ae9eac6781 feat(runtime/object): simplify and more tests 2018-08-17 09:41:22 -07:00
Leonardo de Moura
c863e86429 feat(runtime/object): primitives for interrupting threads 2018-08-17 09:25:40 -07:00
Leonardo de Moura
7a7d443ad5 test(tests/util/object): improve tests 2018-08-17 09:04:08 -07:00
Leonardo de Moura
0a2e9c109f fix(runtime/object): memory leak and violation at task_bind 2018-08-17 09:03:45 -07:00
Leonardo de Moura
510a5ffeaa chore(tests/util/object): adjust test 2018-08-17 08:48:11 -07:00
Leonardo de Moura
f5ecd8477f fix(runtime/object): memory leak 2018-08-17 08:47:29 -07:00
Leonardo de Moura
168eaefff5 fix(runtime/object): finalization and avoid leak 2018-08-17 08:32:33 -07:00