Browse thread
A (Silly?) Question About Universal Type Quantification
-
Will M Farr
- Alan Schmitt
- Martin Jambon
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2009-09-10 (15:56) |
From: | Martin Jambon <martin.jambon@e...> |
Subject: | Re: [Caml-list] A (Silly?) Question About Universal Type Quantification |
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/