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
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-03-04 (22:42)
From: Yoann Padioleau <padator@w...>
Subject: Re: [Caml-list] stl?
Brian Hurt <bhurt@spnz.org> writes:

> On Wed, 4 Mar 2009, Yoann Padioleau wrote:
>> Again, just imagine one second that 'a list were not present in OCaml,
>> and that the only way you had to make a list would be to use
>> a functorized interface of a List module. Would you like that ?
>> (that's what we are forced to do when using Map and that's why
>> I always use Hashtbl instead).
> Humorously enough, I'm doing exactly this.  In a bunch of code I'm
> playing with, I've implemented an NeList module- nothing fancy, just a
> few dozen lines of code and the basic list operations, only the lists
> can not be empty.  They always have to contain at least one element.
> But seriously, you hate functors that much?  

With a passion :) I don't like functors for generic data structures
such as Map.

If you have some code that you want to parametrize over multiple types,
and each of this type has some complex constraints, then I like functors.
But when the only constraints on your type is that you want a Ord, or a Eq
(or a Show), then I think functors are overkill, and I am fine with the default

Also I tend to find harder to understand code using functors ...
You can get many signatures, functors taking functors, ... I am not
evolved enough to handle that. 

> The overhead of doing:
> module StringMap = Map.Make(String);;
> is so high to you, that you simply don't do it?

I do that sometimes, and I also have a IntMap and IntIntMap.

The problem is when your code has some 'a involved, and that
your key is for instance a pair, or triple of stuff having
a polymorphic type. Then, before, I had a function taking
some 'a and returning some 'b, and internally it was using
a list and everything was fine. Then one day I want to optimize
this and use internally a Map and then everything goes down ...
I have to change the signature of my function taking a 'a
into a functor now taking a T, and also do that for its caller,
etc, etc. With typeclass, most of this tedious boilerplate
work would be done behind the scene for me thanks to type inference.
Haskell will just propagate the need to have a    Ord a => 
in addition to the original signature of  'a -> ...

In a similar way, you have some complex code polymorphic in a 'a
and at some point, deep inside a call chain, you want to print
some debugging information about this tiny 'a,  so you really
want to do a      generic_print x, but you can not.
Or if you want to do that you have to turn that into a functor
taking a T where the T is contrained to have a 'print' method/function. 
With haskell you just write 'show x', and haskell inference will
do the work to add the Show a =>. So right now in OCaml
I tend to use the Dumper module of Richard Jones. When I can
avoid functors, I do so.

> Mind if I ask why?
> Brian