Effect handlers in OCaml
KC Sivaramakrishnan1 & Stephen Dolan1 Leo White2, Jeremy Yallop1,3, Armaël Guéneau4, Anil Madhavapeddy1,3
1 2 3 4
E ff ect handlers in OCaml KC Sivaramakrishnan 1 & Stephen Dolan - - PowerPoint PPT Presentation
E ff ect handlers in OCaml KC Sivaramakrishnan 1 & Stephen Dolan 1 Leo White 2 , Jeremy Yallop 1,3 , Armal Guneau 4 , Anil Madhavapeddy 1,3 1 2 3 4 Concurrency Parallelism Concurrency Programming technique Overlapped
KC Sivaramakrishnan1 & Stephen Dolan1 Leo White2, Jeremy Yallop1,3, Armaël Guéneau4, Anil Madhavapeddy1,3
1 2 3 4
effect Foo : int -> int let f () = (perform (Foo 3)) (* 3 + 1 *) + (perform (Foo 3)) (* 3 + 1 *) let r = try f () with effect (Foo i) k -> continue k (i + 1)
let dynamic_wind before_thunk thunk after_thunk = before_thunk (); let res = match thunk () with | v -> v | exception e -> after_thunk (); raise e | effect e k -> after_thunk (); let res' = perform e in before_thunk (); continue k res' in after_thunk (); res
lying around...)
[1] https://github.com/kayceesrk/ocaml15-eff/tree/master/chameneos-redux
[1] https://github.com/kayceesrk/ocaml15-eff/blob/master/generator.ml
(* val to_gen : 'a t -> (unit -> 'a option) *) let to_gen (type a) (t : a t) = let module M = struct effect Next : a -> unit end in let open M in let step = ref (fun () -> assert false) in let first_step () = try iter (fun x -> perform (Next x)) t; None with effect (Next v) k -> step := continue k; Some v in step := first_step; fun () -> !step () let rec iter f = function | Leaf -> () | Node (l, x, r) -> iter f l; f x; iter f r type 'a t = | Leaf | Node of 'a t * 'a * 'a t
handle / continue
handler sp
call chain reference
handle / continue handle / continue
sp handler
call chain reference
perform
sp
handle / continue
handler
call chain reference
OCaml start program C call OCaml callback C call OCaml callback
C OCaml C OCaml C OCaml
C
system stack
OCaml heap
OCaml start program C call handle OCaml callback C call
C C
Normalised time (lower is better)
Effects Vanilla
Time (S) 0.45 0.9 1.35 1.8 Iterations (X100,000) 1 2 3 4 5 6 7 8 9 10
Lwt Concurrency Monad GHC Fibers
Time (S) 1 2 3 4 Binary tree depth 15 16 17 18 19 20 21 22 23 24 25
Iterator Fiber Generator H/W Generator
Examples: https://github.com/kayceesrk/ocaml-eff-example Multicore OCaml: https://github.com/ocamllabs/ocaml-multicore