Skip to main content

leanfran_runtime/
thunk.rs

1//! `Thunk` (evaluated on first `get`) and `Task` (run synchronously when created; there are
2//! no worker threads).
3
4use crate::closure::lean_apply_1;
5use crate::object::*;
6use crate::panic::lean_internal_panic;
7use core::mem::size_of;
8use core::ptr;
9
10#[inline]
11unsafe fn to_thunk(o: Obj) -> *mut ThunkObject {
12    o as *mut ThunkObject
13}
14
15#[inline]
16unsafe fn to_task(o: Obj) -> *mut TaskObject {
17    o as *mut TaskObject
18}
19
20/// `Thunk.mk` from a closure `Unit → α` (consumed).
21#[inline]
22pub unsafe fn lean_mk_thunk(c: Obj) -> Obj {
23    let o = lean_alloc_small_object(size_of::<ThunkObject>());
24    lean_set_st_header(o, LEAN_THUNK as u32, 0);
25    (*to_thunk(o)).m_value = ptr::null_mut();
26    (*to_thunk(o)).m_closure = c;
27    o
28}
29
30/// `Thunk.pure` (consumes `v`).
31#[inline]
32pub unsafe fn lean_thunk_pure(v: Obj) -> Obj {
33    let o = lean_alloc_small_object(size_of::<ThunkObject>());
34    lean_set_st_header(o, LEAN_THUNK as u32, 0);
35    (*to_thunk(o)).m_value = v;
36    (*to_thunk(o)).m_closure = ptr::null_mut();
37    o
38}
39
40/// Evaluates the closure of `t` and stores the value; returns it borrowed.
41pub unsafe fn lean_thunk_get_core(t: Obj) -> Obj {
42    let c = (*to_thunk(t)).m_closure;
43    if c.is_null() {
44        lean_internal_panic("thunk forced while it is being evaluated");
45    }
46    (*to_thunk(t)).m_closure = ptr::null_mut();
47    let r = lean_apply_1(c, lean_box(0));
48    (*to_thunk(t)).m_value = r;
49    r
50}
51
52/// The value of `t` (borrowed in and out).
53#[inline]
54pub unsafe fn lean_thunk_get(t: Obj) -> Obj {
55    debug_check_live(t, "thunk read");
56    let r = (*to_thunk(t)).m_value;
57    if !r.is_null() {
58        return r;
59    }
60    lean_thunk_get_core(t)
61}
62
63/// `Thunk.get` (borrows `t`, returns an owned value).
64#[inline]
65pub unsafe fn lean_thunk_get_own(t: Obj) -> Obj {
66    let r = lean_thunk_get(t);
67    lean_inc(r);
68    r
69}
70
71/* Tasks */
72
73/// `Task.pure` (consumes `a`).
74pub unsafe fn lean_task_pure(a: Obj) -> Obj {
75    let o = lean_alloc_small_object(size_of::<TaskObject>());
76    lean_set_st_header(o, LEAN_TASK as u32, 0);
77    (*to_task(o)).m_value = a;
78    (*to_task(o)).m_imp = ptr::null_mut();
79    o
80}
81
82/// Runs the closure `c : Unit → α` (consumed) now and returns the finished task.
83pub unsafe fn lean_task_spawn_core(c: Obj, _prio: u32, _keep_alive: bool) -> Obj {
84    lean_task_pure(lean_apply_1(c, lean_box(0)))
85}
86
87/// `Task.spawn` (`c` and `prio` consumed).
88#[inline]
89pub unsafe fn lean_task_spawn(c: Obj, prio: Obj) -> Obj {
90    lean_dec(prio);
91    lean_task_spawn_core(c, 0, false)
92}
93
94/// The value of a task (borrowed in and out).
95#[inline]
96pub unsafe fn lean_task_get(t: Obj) -> Obj {
97    debug_check_live(t, "task read");
98    let v = (*to_task(t)).m_value;
99    if v.is_null() {
100        lean_internal_panic(
101            "the result of a promise that is not resolved is read (tasks run synchronously, so nothing can resolve it later)",
102        );
103    }
104    v
105}
106
107/// `Task.get` (consumes `t`).
108#[inline]
109pub unsafe fn lean_task_get_own(t: Obj) -> Obj {
110    let r = lean_task_get(t);
111    lean_inc(r);
112    lean_dec(t);
113    r
114}
115
116pub unsafe fn lean_task_map_core(
117    f: Obj,
118    t: Obj,
119    _prio: u32,
120    _sync: bool,
121    _keep_alive: bool,
122) -> Obj {
123    let v = lean_task_get(t);
124    lean_inc(v);
125    lean_dec(t);
126    lean_task_pure(lean_apply_1(f, v))
127}
128
129/// `Task.map f t prio sync` (`f`, `t`, `prio` consumed).
130#[inline]
131pub unsafe fn lean_task_map(f: Obj, t: Obj, prio: Obj, sync: u8) -> Obj {
132    lean_dec(prio);
133    lean_task_map_core(f, t, 0, sync != 0, false)
134}
135
136pub unsafe fn lean_task_bind_core(
137    x: Obj,
138    f: Obj,
139    _prio: u32,
140    _sync: bool,
141    _keep_alive: bool,
142) -> Obj {
143    let v = lean_task_get(x);
144    lean_inc(v);
145    lean_dec(x);
146    lean_apply_1(f, v)
147}
148
149/// `Task.bind x f prio sync` (`x`, `f`, `prio` consumed).
150#[inline]
151pub unsafe fn lean_task_bind(x: Obj, f: Obj, prio: Obj, sync: u8) -> Obj {
152    lean_dec(prio);
153    lean_task_bind_core(x, f, 0, sync != 0, false)
154}
155
156/// `IO.checkCanceled`: tasks are never canceled.
157pub unsafe fn lean_io_check_canceled_core() -> bool {
158    false
159}
160
161/// `IO.cancel`: no-op, tasks are already finished.
162pub unsafe fn lean_io_cancel_core(_t: Obj) {}
163
164/// `IO.getTaskState`: finished (2), or running (1) for the result of an unresolved promise, as the C runtime reports it.
165pub unsafe fn lean_io_get_task_state_core(t: Obj) -> u8 {
166    if (*to_task(t)).m_value.is_null() {
167        1
168    } else {
169        2
170    }
171}
172
173/* Tasks of `BaseIO` actions */
174
175/// `BaseIO.asTask act prio` (`act`, `prio` consumed): runs `act` now.
176pub unsafe fn lean_io_as_task(act: Obj, prio: Obj) -> Obj {
177    lean_dec(prio);
178    lean_task_pure(lean_apply_1(act, lean_box(0)))
179}
180
181/// `BaseIO.mapTask f t prio sync` (`f`, `t`, `prio` consumed).
182pub unsafe fn lean_io_map_task(f: Obj, t: Obj, prio: Obj, _sync: u8) -> Obj {
183    lean_dec(prio);
184    let v = lean_task_get(t);
185    lean_inc(v);
186    lean_dec(t);
187    lean_task_pure(crate::closure::lean_apply_2(f, v, lean_box(0)))
188}
189
190/// `BaseIO.bindTask t f prio sync` (`t`, `f`, `prio` consumed).
191pub unsafe fn lean_io_bind_task(t: Obj, f: Obj, prio: Obj, _sync: u8) -> Obj {
192    lean_dec(prio);
193    let v = lean_task_get(t);
194    lean_inc(v);
195    lean_dec(t);
196    crate::closure::lean_apply_2(f, v, lean_box(0))
197}
198
199/// `IO.checkCanceled : BaseIO Bool`.
200pub unsafe fn lean_io_check_canceled() -> u8 {
201    lean_io_check_canceled_core() as u8
202}
203
204/// `IO.cancel : @& Task α → BaseIO Unit`.
205pub unsafe fn lean_io_cancel(t: Obj) -> Obj {
206    lean_io_cancel_core(t);
207    lean_box(0)
208}
209
210/// `IO.getTaskState : @& Task α → BaseIO TaskState`.
211pub unsafe fn lean_io_get_task_state(t: Obj) -> u8 {
212    lean_io_get_task_state_core(t)
213}
214
215/// `IO.wait : Task α → BaseIO α` (consumes `t`).
216pub unsafe fn lean_io_wait(t: Obj) -> Obj {
217    lean_task_get_own(t)
218}
219
220/// `IO.waitAny : @& List (Task α) → BaseIO α`: the value of the first task.
221pub unsafe fn lean_io_wait_any(tasks: Obj) -> Obj {
222    let v = lean_task_get(lean_ctor_get(tasks, 0));
223    lean_inc(v);
224    v
225}
226
227/* Promises */
228
229unsafe fn promise_finalize(p: *mut core::ffi::c_void) {
230    let t = *Box::from_raw(p as *mut Obj);
231    if (*to_task(t)).m_value.is_null() {
232        (*to_task(t)).m_value = lean_box(0);
233    }
234    lean_dec(t);
235}
236
237unsafe fn promise_foreach(_p: *mut core::ffi::c_void, _f: Obj) {}
238
239static PROMISE_CLASS: ExternalClass = ExternalClass {
240    m_finalize: promise_finalize,
241    m_foreach: promise_foreach,
242};
243
244unsafe fn promise_task(p: Obj) -> Obj {
245    *(lean_get_external_data(p) as *const Obj)
246}
247
248/// `IO.Promise.new : BaseIO (Promise α)`: a promise whose task `Task (Option α)` holds no value
249/// until `resolve` (`some v`) or until the promise is dropped (`none`).
250pub unsafe fn lean_io_promise_new() -> Obj {
251    let t = lean_alloc_small_object(size_of::<TaskObject>());
252    lean_set_st_header(t, LEAN_TASK as u32, 0);
253    (*to_task(t)).m_value = ptr::null_mut();
254    (*to_task(t)).m_imp = ptr::null_mut();
255    lean_alloc_external(
256        &PROMISE_CLASS,
257        Box::into_raw(Box::new(t)) as *mut core::ffi::c_void,
258    )
259}
260
261/// `IO.Promise.resolve : α → (@& Promise α) → BaseIO Unit` (consumes `value`); a second
262/// `resolve` keeps the first value.
263pub unsafe fn lean_io_promise_resolve(value: Obj, p: Obj) -> Obj {
264    let t = promise_task(p);
265    if (*to_task(t)).m_value.is_null() {
266        let s = lean_alloc_ctor(1, 1, 0);
267        lean_ctor_set(s, 0, value);
268        (*to_task(t)).m_value = s;
269    } else {
270        lean_dec(value);
271    }
272    lean_box(0)
273}
274
275/// `IO.Promise.result? : (@& Promise α) → Task (Option α)`.
276pub unsafe fn lean_io_promise_result_opt(p: Obj) -> Obj {
277    let t = promise_task(p);
278    lean_inc(t);
279    t
280}
281
282/// `Option.getOrBlock! : Option α → α` (consumes `o`): the value, or a panic for a promise
283/// dropped without a value.
284pub unsafe fn lean_option_get_or_block(o: Obj) -> Obj {
285    if lean_is_scalar(o) {
286        crate::panic::lean_panic(
287            "PANIC: Promise.result!: promise has been dropped without ever being resolved",
288            true,
289        );
290        lean_internal_panic(
291            "Promise.result!: promise has been dropped without ever being resolved",
292        );
293    }
294    let v = lean_ctor_get(o, 0);
295    lean_inc(v);
296    lean_dec(o);
297    v
298}