Last active
March 7, 2023 09:37
-
-
Save mheiber/b0fe9b96cacf6fd61abc392c7bd24d41 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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