From: Martin Jambon <martin.jambon@ens-lyon.org>
To: Will M Farr <farr@MIT.EDU>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] A (Silly?) Question About Universal Type Quantification
Date: Thu, 10 Sep 2009 17:48:54 +0200 [thread overview]
Message-ID: <4AA91FE6.4050102@ens-lyon.org> (raw)
In-Reply-To: <CFC43205-B409-43C9-B796-91BC386BCC9E@MIT.EDU>
Will M Farr wrote:
> Hello,
>
> I recently encountered a situation where I had (effectively) the
> following polymorphic type:
>
> type 'a record = { id : int; data : 'a }
You could do this:
type record = { id : int; data : 'a . 'a }
The only minor problem is that you can't create values of such type :-)
> and the following compare function
>
> let compare {id = id1} {id = id2} = Pervasives.compare id1 id2
>
> and wanted to put such records into a set. However, I could not figure
> out how to make the polymorphic 'a in the type definition "disappear" in
> the module argument to the Set.Make functor. For example, the obvious
>
> Set.Make(struct
> type t = 'a record
> let compare = compare
> end)
>
> fails because the 'a in the type definition for t is unbound. Is there
> no way to do this? I'm thinking of some sort of "forall" designation,
> which universally quantifies the type parameter, like
>
> Set.Make(struct
> type t = forall 'a : 'a record
> let compare = compare
> end)
>
> (I'm sure that there is better terminology for this---please pardon my
> ignorance about types and type theory.)
>
> I ended up solving my problem by placing the record type into a functor,
> whose argument specified the concrete type for data, but I'm curious if
> other solutions exist.
Looks like the right approach.
You could also used a defunctorized version of Set, at the cost of losing the
static guarantee that you won't mix sets using inconsistent comparison functions.
Martin
--
http://mjambon.com/
next prev parent reply other threads:[~2009-09-10 15:56 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-09-10 15:00 Will M Farr
2009-09-10 15:48 ` Martin Jambon [this message]
2009-09-10 15:51 ` [Caml-list] " Alan Schmitt
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=4AA91FE6.4050102@ens-lyon.org \
--to=martin.jambon@ens-lyon.org \
--cc=caml-list@inria.fr \
--cc=farr@MIT.EDU \
/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