Continuing on my intense tangent of universal types in OCaml, digging around the...

@andnull@social.nouveau.community

Continuing on my intense tangent of universal types in OCaml, digging around the internet archive and finding this implementation:

module type Univ' = sig
type t
module Embed (M : sig type t end) : sig
val inj : M.t -> t
val proj : t -> M.t option
end
end

module Univ' : Univ' = struct
type t = exn

module Embed (M : sig type t end) = struct
exception E of M.t
let inj m = E m
let proj = function E m -> Some m | _ -> None
end
end

module EmbeddedInt = Univ'.Embed(Int)
module EmbeddedString = Univ'.Embed(String)

let x = EmbeddedInt.inj 10
let () = match EmbeddedString.proj x with
| None -> print_endline "not a string"
| Some s -> print_endline s

I do not understand anything about modules and local exceptions enough to gork any of this beyond parroting some stuff, but I at least want to surface it from the web archive. May just make a wiki page for these snippets I'm finding.