module type Dyn = sig type 'a brand type t val make_brand : unit -> 'a brand val pack : 'a brand -> 'a -> t val unpack : 'a brand -> t -> 'a option val null : t end module Dyn : Dyn = struct type 'a brand = ('a -> exn) * (exn -> 'a option) type t = exn let make_brand : unit -> 'a brand = function () -> let module X = struct exception E of 'a let pack x = E x let unpack = function | E x -> Some x | _ -> None end in X.pack, X.unpack let pack = fst let unpack = snd exception Null let null = Null end module Dyn : Dyn = struct type 'a brand = ('a option) ref type t = unit -> unit let make_brand () = ref None let pack b x () = b := Some x let unpack b f = b := None; f(); !b let null () = () end