Skip to content

Instantly share code, notes, and snippets.

@hcarty
Last active August 29, 2020 16:48
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 hcarty/9f4e7fe0d6e7310c9376cd46be752c4f to your computer and use it in GitHub Desktop.
Save hcarty/9f4e7fe0d6e7310c9376cd46be752c4f to your computer and use it in GitHub Desktop.
GADT typing
module One = struct
type t
end
module Two = struct
type t
end
module Tag_incomplete_match = struct
type one = One.t
type two = Two.t
type (_, _) t =
| A : (one, string) t
| B : (two, int) t
let get_one (type v) (x : (one, v) t) : v =
match x with
| A -> "a"
let get_two (type v) (x : (two, v) t) : v =
match x with
| B -> 2
let get (type s v) (x : (s, v) t) : v =
match x with
| A as x' -> get_one x'
| B as x' -> get_two x'
end
module Tag_complete_match = struct
type one
type two
type (_, _) t =
| A : (one, string) t
| B : (two, int) t
let get_one (type v) (x : (one, v) t) : v =
match x with
| A -> "a"
let get_two (type v) (x : (two, v) t) : v =
match x with
| B -> 2
let get (type s v) (x : (s, v) t) : v =
match x with
| A as x' -> get_one x'
| B as x' -> get_two x'
end
module One : sig
type t
val get : t -> string
end = struct
type t = string
let get x = x
end
module Two : sig
type t
val get : t -> int
end = struct
type t = int
let get x = x
end
module Tag_incomplete_match = struct
type one = One.t
type two = Two.t
type (_, _) t =
| A : (one, string) t
| B : (two, int) t
let get_one (type v) (x : (one, v) t) : One.t -> v =
match x with
| A -> One.get
let get_two (type v) (x : (two, v) t) : Two.t -> v =
match x with
| B -> Two.get
let get (type s v) (x : (s, v) t) : s -> v =
match x with
| A as x' -> get_one x'
| B as x' -> get_two x'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment