|
|
|
|
|
by sweetsocks21
16 days ago
|
|
I think the parent may be getting at the continuation aspect of effects? Effect systems make the stack a first class object you can reuse, I think a standard example is implementing a scheduler. I'm not familiar with your Bluefin library so maybe it already handles this: effect Sched =
yield : unit -> unit
fork : (unit -> unit) -> unit
end
let mut run_queue = []
let enqueue t = run_queue := List.concat run_queue [t]
let dequeue () =
match run_queue with
| [] -> ()
| t :: rest ->
run_queue := rest;
t ()
let rec spawn task =
handle
task ()
with
| return _ -> dequeue ()
| yield () k ->
enqueue (fn () -> resume k ());
dequeue ()
| fork f k ->
enqueue (fn () -> resume k ());
spawn f
let run main = spawn main
let worker name steps =
let rec loop i =
if i > steps do ()
else do
print $"{name}: step {i}";
perform yield ();
loop (i + 1)
end
in
loop 1
let () =
run (fn () ->
print "main: starting";
perform fork (fn () -> worker "A" 3);
perform fork (fn () -> worker "B" 3);
print "main: forked workers, now yielding";
perform yield ();
print "main: done")
output: main: starting
A: step 1
B: step 1
A: step 2
main: forked workers, now yielding
B: step 2
A: step 3
main: done
B: step 3
|
|
On the other hand, I don't see any difficulty with implementing a scheduler using Await/Yield. I don't think it needs access to the full continuation.