On Tue, Apr 22, 2014 at 9:03 AM, Romain Bardou <romain@cryptosense.com> wrote:
I want to implement the trick with GADTs where you test equality of
unique identifiers, and if they match this adds an equality constraint
on types. I want this code to be small and well abstracted in a module
so that if this module is safe, then using this module cannot cause a
seg fault.

Here is the signature of my module:
[...]

We do exactly this at Jane Street in our type_equal module:
  https://github.com/janestreet/core_kernel/blob/master/lib/type_equal.ml#L87

Note that using the open_types branch of ocaml [1] there is a cool way to do this:

(************************************************************************)
type 'a key = ..

type (_, _) equal =
  | Equal: ('a, 'a) equal
  | Different: ('a, 'b) equal

module type S = sig
  type t
  type 'a key += T : t key
end

type 'a t = (module S with type t = 'a)

let fresh (type a) () =
  let module M = struct
    type t = a
    type 'a key += T : t key
  end in
  (module M : S with type t = a)

let equal (type a) (type b)
      (module A : S with type t = a)
      (module B : S with type t = b)
  : (a, b) equal =
  match A.T with
  | B.T -> Equal
  | _   -> Different

let to_int = Hashtbl.hash
(************************************************************************)

  [1] http://caml.inria.fr/mantis/view.php?id=5584
 
--
Jeremie