@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.
43 lines
1.6 KiB
Text
43 lines
1.6 KiB
Text
/-
|
||
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
|
||
Released under Apache 2.0 license as described in the file LICENSE.
|
||
Authors: Leonardo de Moura
|
||
-/
|
||
prelude
|
||
import init.meta.format
|
||
universes u
|
||
|
||
/-- This function has a native implementation that tracks time. -/
|
||
def timeit {α : Type u} (s : string) (f : thunk α) : α :=
|
||
f.get
|
||
|
||
/-- This function has a native implementation that displays the given string in the regular output stream. -/
|
||
def trace {α : Type u} (s : string) (f : thunk α) : α :=
|
||
f.get
|
||
|
||
meta def trace_val {α : Type u} [has_to_format α] (f : α) : α :=
|
||
trace (to_fmt f).to_string f
|
||
|
||
/-- This function has a native implementation that shows the VM call stack. -/
|
||
def trace_call_stack {α : Type u} (f : thunk α) : α :=
|
||
f.get
|
||
|
||
/-- This function has a native implementation that displays in the given position all trace messages used in f.
|
||
The arguments line and col are filled by the elaborator. -/
|
||
def scope_trace {α : Type u} {line col: nat} (f : thunk α) : α :=
|
||
f.get
|
||
|
||
/--
|
||
This function has a native implementation where
|
||
the thunk is interrupted if it takes more than 'max' "heartbeats" to compute it.
|
||
The heartbeat is approx. the maximum number of memory allocations (in thousands) performed by 'f ()'.
|
||
This is a deterministic way of interrupting long running tasks. -/
|
||
meta def try_for {α : Type u} (max : nat) (f : thunk α) : option α :=
|
||
some f.get
|
||
|
||
meta constant undefined_core {α : Sort u} (message : string) : α
|
||
|
||
meta def undefined {α : Sort u} : α := undefined_core "undefined"
|
||
|
||
meta def unchecked_cast {α : Sort u} {β : Sort u} : α → β :=
|
||
cast undefined
|