caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* How to make a value uncomparable
@ 2008-04-23 13:49 Berke Durak
  2008-04-23 15:36 ` [Caml-list] " Christophe Raffalli
  0 siblings, 1 reply; 6+ messages in thread
From: Berke Durak @ 2008-04-23 13:49 UTC (permalink / raw)
  To: Caml-list List

Well, as comparing non-canonical datastructures such as Sets and Maps with Pervasives.compare is unsafe,
I was thinking of ways to make sure that you don't do such a thing.

Our resident type experts could tell us if uniqueness types could be used for that purpose, but if you're
willing to settle for run-time safety, here is a little solution.

Pervasives.compare will refuse to run on heap values wrapped in an Abstract_tag, whose purpose is to stop the
GC from nosing below the tag.  So if you have a value that's not Pervasives.compare-comparables, you can wrap
it inside such a tag, and any call to Pervasives.compare will raise an invalid_arg.

But how?  You could of course write a piece of native code, but it happens that the Abstract_tag is used by
the Weak module.  So here's an Uncomparable module, that makes any value run-time Pervasives-uncomparable,
at a cost of six extra words per value (if I'm correct).

module Uncomparable :
   sig
     type 'a t
     val make : 'a -> 'a t
     val get : 'a t -> 'a
   end
   =
   struct
     type 'a t = 'a * 'a Weak.t

     let make x =
       let w = Weak.create 1 in
       Weak.set w 0 (Some x);
       (x, w)

     let get (x, _) = x
   end

# #use "mkuncmp.ml";;
module Uncomparable :
   sig type 'a t val make : 'a -> 'a t val get : 'a t -> 'a end
# Uncomparable.make 33;;
- : int Uncomparable.t = <abstr>
# let x = Uncomparable.make 33;;
val x : int Uncomparable.t = <abstr>
# let y = Uncomparable.make 33;;
val y : int Uncomparable.t = <abstr>
# compare x y;;
Exception: Invalid_argument "equal: abstract value".
# Uncomparable.get x;;
- : int = 33
# Uncomparable.get y;;
- : int = 33

-- 
Berke DURAK


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-04-23 17:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-23 13:49 How to make a value uncomparable Berke Durak
2008-04-23 15:36 ` [Caml-list] " Christophe Raffalli
2008-04-23 16:14   ` Berke Durak
2008-04-23 16:49     ` Christophe Raffalli
2008-04-23 17:43       ` Berke Durak
2008-04-23 17:46         ` Jon Harrop

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).