caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremie Dimino <jdimino@janestreet.com>
To: Romain Bardou <romain@cryptosense.com>
Cc: Ocaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Obj.magic for polymorphic identifiers
Date: Tue, 22 Apr 2014 09:31:13 +0100	[thread overview]
Message-ID: <CANhEzE5r_AL8mG968ANHS6aTbxnHKru2B596rs6KyAY-ufs1hw@mail.gmail.com> (raw)
In-Reply-To: <5356225B.1090305@cryptosense.com>

[-- Attachment #1: Type: text/plain, Size: 1403 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 2457 bytes --]

  reply	other threads:[~2014-04-22  8:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-22  8:03 Romain Bardou
2014-04-22  8:31 ` Jeremie Dimino [this message]
2014-04-24 14:28   ` Goswin von Brederlow
2014-05-04 23:46     ` Leo White
2014-04-24 15:30 ` Dmitry Grebeniuk
2014-04-28  7:36   ` Goswin von Brederlow
2014-04-28  8:13     ` Dmitry Grebeniuk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CANhEzE5r_AL8mG968ANHS6aTbxnHKru2B596rs6KyAY-ufs1hw@mail.gmail.com \
    --to=jdimino@janestreet.com \
    --cc=caml-list@inria.fr \
    --cc=romain@cryptosense.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).