From: Berke Durak <berke.durak@exalead.com>
To: Caml-list List <caml-list@inria.fr>
Subject: How to make a value uncomparable
Date: Wed, 23 Apr 2008 15:49:06 +0200 [thread overview]
Message-ID: <480F3E52.60300@exalead.com> (raw)
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
next reply other threads:[~2008-04-23 13:49 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-04-23 13:49 Berke Durak [this message]
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
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=480F3E52.60300@exalead.com \
--to=berke.durak@exalead.com \
--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