caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Obj.magic for polymorphic identifiers
Date: Thu, 24 Apr 2014 16:28:37 +0200	[thread overview]
Message-ID: <20140424142837.GA3845@frosties> (raw)
In-Reply-To: <CANhEzE5r_AL8mG968ANHS6aTbxnHKru2B596rs6KyAY-ufs1hw@mail.gmail.com>

On Tue, Apr 22, 2014 at 09:31:13AM +0100, Jeremie Dimino wrote:
> 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

Now that is a good reason to have open types. I've tried (and failed)
to do something like this without resorting to magic and the only way
I found was to declare a big GADT that has a constructor for every
possible type and match all possible equal pairs. That realy doesn't
scale well.


Would it be possible for someone familiar with the patch to create a
pull request for it on git? Maybe that way it could be merged in
faster.

MfG
	Goswin

  reply	other threads:[~2014-04-24 14:28 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
2014-04-24 14:28   ` Goswin von Brederlow [this message]
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=20140424142837.GA3845@frosties \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    /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).