caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Berke Durak <berke.durak@exalead.com>
To: Jon Harrop <jon@ffconsultancy.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Announce: xsetxmap, unfunctorized,	Sexp-lib aware versions of Set and Map
Date: Wed, 23 Apr 2008 15:41:29 +0200	[thread overview]
Message-ID: <480F3C89.5080203@exalead.com> (raw)
In-Reply-To: <200804231335.25708.jon@ffconsultancy.com>

Jon Harrop wrote:
> On Monday 21 April 2008 12:19:09 Berke Durak wrote:
>> Yes you are right, I should have put a warning.  These modules are for
>> peculiar cases.
> 
> Actually I would say that your style is more useful than the built-in Set and 
> Map modules because you don't have to jump through hoops defining your 
> own "Int" module with its own "int" type and its own comparison function over 
> ints every time you want a set of integers. I would put the comparison 
> function in the set itself though.

Well, thanks.

> Your modules cover 99% of use cases with the lowest syntactic overhead 
> possible in OCaml. However, you have inherited a couple of design flaws from 
> OCaml's standard library:
> 
> . The "height" function has been inlined which has no significant effect on 
> performance but makes it harder to refactor the code.
> 
> . If you special case Node(Empty, v, Empty) to a new Node1(v) type constructor 
> then you can improve performance by 30% by alleviating the GC.

That's a good idea.

> As Jean-Christophe says, it is dangerous.

Have you checked out the second version where I use phantom types to prevent
the more severe cases of data corruption?  Yes, you can still end up with
multiple copies of the same "set", but at least the tree won't get
inconsistent because you mistakenly called add with different comparison functions.
So it's as safe/unsafe as using List.assoc or the generic hash table to
store sets or hash tables.

That's still a problem, but it's not that much worse than with
functors.  After all, nothing prevents you from incorrectly writing

   module StringSet = Set.Make(String)
   module SetSet = Set.Make(struct type t = StringSet.t let compare = compare)

 > However, the whole language is
> already dangerous in this sense because it is so easy to erroneously apply 
> polymorphic comparison or equality to the existing sets and maps and get 
> silent data corruption. The only solution is to fix the language itself by 
> allowing types to override comparison, equality and hashing.

Overriding equality etc. would require run-time type-based dispatch
or implicitly carrying the comparison function (as Haskell apparently does
with type classes) as an extra argument.  Could be expensive.

> Another inherent problem is the inefficiency of performing comparisons in this 
> way in OCaml. Type specialization (e.g. using a JIT) greatly accelerates this 
> kind of code by allowing the comparison function (which is almost always 
> trivial) to be inlined and optimized.

Did anyone try the GNU lightning-based ocamljit in the Ocaml CVS?
-- 
Berke DURAK


      parent reply	other threads:[~2008-04-23 13:41 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-21  9:39 Berke Durak
2008-04-21 10:59 ` [Caml-list] " Jean-Christophe Filliâtre
2008-04-21 11:19   ` Berke Durak
2008-04-23 12:35     ` Jon Harrop
2008-04-23 13:25       ` Brian Hurt
2008-04-23 13:56         ` [Caml-list] Announce: xsetxmap, unfunctorized, Sexp-lib awareversions " David Allsopp
2008-04-23 14:22           ` Berke Durak
2008-04-26 14:44           ` Alexandre Pilkiewicz
2008-04-23 13:41       ` Berke Durak [this message]

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=480F3C89.5080203@exalead.com \
    --to=berke.durak@exalead.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=jon@ffconsultancy.com \
    /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).