English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Announce: xsetxmap, unfunctorized, Sexp-lib aware versions of Set and Map
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-04-23 (13:41)
From: Berke Durak <berke.durak@e...>
Subject: Re: [Caml-list] Announce: xsetxmap, unfunctorized, Sexp-lib aware versions of Set and Map
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?