Решил посмотреть многоядерный окамль https://github.com/ocamllabs/ocaml-multicore/wiki. В нём добавили новый механизм - эффекты. Код взял отсюда http://kcsrk.info/ocaml/multicore/effects/2015/05/27/more-effects/ и добавил принтов.
Не понятно что в этом коде возвращает (continue k s). Я так понимаю после этого вызова, код выполняется с места вызова эффекта, с полученным значением s, а дальше? Какая функция присваивается ret?
open Printf
module type STATE = sig
type t
val put : t -> unit
val get : unit -> t
val run : (unit -> 'a) -> init:t -> t * 'a
end
module State (S : sig type t end) : STATE with type t = S.t = struct
type t = S.t
effect Put : t -> unit
let put v = perform (Put v)
effect Get : t
let get () = perform Get
let run f ~init =
let comp =
(printf "running comp\n");
match (f ()) with
| x -> (printf "handling x \n" );
(fun s -> (s, x))
| effect (Put s_new) k ->
(printf "handling new \n" );
(fun s ->
(printf "inside new, before continue \n");
let ret = (continue k ()) in
(printf "inside new, after continue \n");
(ret s_new))
| effect Get k ->
(printf "handling get \n" );
fun s ->
(printf "inside get, before continue \n" );
let ret = (continue k s) in
(printf "inside get, after continue \n" );
(ret s)
in
(printf "before (comp) init \n" );
let some_v = (comp) in
(printf "before init \n" );
(some_v init)
end
module IS = State (struct type t = int end)
let foo () : unit =
printf "running foo \n";
printf "%d\n" (IS.get ());
printf "running foo \n";
IS.put 42;
printf "running foo \n";
printf "%d\n" (IS.get ());
printf "running foo \n";
IS.put 21
let _ = (IS.run foo 4)
Результат запуска:
running comp
running foo
handling get
before (comp) init
before init
inside get, before continue
4
running foo
handling new
inside get, after continue
inside new, before continue
running foo
handling get
inside new, after continue
inside get, before continue
42
running foo
handling new
inside get, after continue
inside new, before continue
handling x
inside new, after continue