Skip to content

Instantly share code, notes, and snippets.

@mheiber
Last active March 7, 2023 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mheiber/b0fe9b96cacf6fd61abc392c7bd24d41 to your computer and use it in GitHub Desktop.
Save mheiber/b0fe9b96cacf6fd61abc392c7bd24d41 to your computer and use it in GitHub Desktop.
module type Univ = sig
type t
val embed: unit -> ('a -> t) * (t -> 'a option)
end
module U1 = struct
type t = exn
let embed (type a) () =
let open struct
exception E of a
end in
let inject e = E e in
let project e =
match e with
| E a -> Some a
| _ -> None
in
(inject, project)
end
module Test (U : Univ) = struct
let (of_int, to_int) = U.embed ()
let (of_string, to_string) = U.embed ()
let r : U.t ref = ref (of_int 13)
let () = begin
assert (to_int !r = Some 13);
assert (to_string !r = None);
r := of_string "foo";
assert (to_int !r = None);
assert (to_string !r = Some "foo");
end
end
module _ = Test(U1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment