Skip to content

Instantly share code, notes, and snippets.

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 lyricallogical/3476389 to your computer and use it in GitHub Desktop.
Save lyricallogical/3476389 to your computer and use it in GitHub Desktop.
なんか rank2-polymorphism と existential type が欲しかったけど無かったので first-class module を使った結果がこれだよ
type 'a node =
| A: int -> int node
| B: string -> string node
module type Exists = sig
type t
val ret: t node
end
module type PolyFun = sig
val f: 'a node -> (module Exists)
end
let f: type a. a node -> (module Exists) = function
| A _ -> let module Ret = struct type t = string let ret = B "hoge" end in (module Ret)
| B _ -> let module Ret = struct type t = int let ret = A 0 end in (module Ret)
let g: type a. a node -> (module PolyFun) -> (module Exists) = fun n pf ->
let module PF = (val pf: PolyFun) in
PF.f n
let result = g (A 1) (module struct let f = f end: PolyFun)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment