Version française
Home     About     Download     Resources     Contact us    
Browse thread
stl?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Brian Hurt <bhurt@s...>
Subject: Re: [Caml-list] stl?

Sorry for the late reply- I was out of town over the weekend.

On Wed, 4 Mar 2009, Jon Harrop wrote:

> On Wednesday 04 March 2009 21:59:51 Brian Hurt wrote:
>> But seriously, you hate functors that much?  The overhead of doing:
>>
>> module StringMap = Map.Make(String);;
>>
>> is so high to you, that you simply don't do it?
>>
>> Mind if I ask why?
>
> Your example is fragile: it doesn't work with Int and Float because they were
> never written:
>
> $ ocaml
>        Objective Caml version 3.09.1
>
> # module IntMap = Map.Make(Int);;
> Unbound module Int
> # module FloatMap = Map.Make(Float);;
> Unbound module Float

This isn't a limitation of the language, this is simply a short comming of 
the standard libraries.


>
> So you have to define a temporary module by hand in general:
>
> # module IntMap = Map.Make(struct type t = int let compare = compare end);;
> module IntMap :
>  sig
>    type key = int
>    type +'a t
>    val empty : 'a t
>    val is_empty : 'a t -> bool
>    val add : key -> 'a -> 'a t -> 'a t
>    val find : key -> 'a t -> 'a
>    val remove : key -> 'a t -> 'a t
>    val mem : key -> 'a t -> bool
>    val iter : (key -> 'a -> unit) -> 'a t -> unit
>    val map : ('a -> 'b) -> 'a t -> 'b t
>    val mapi : (key -> 'a -> 'b) -> 'a t -> 'b t
>    val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
>    val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
>    val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
>  end
>
> You want a mapping to IntMaps but IntMap is parameterised and the Map.Make
> functor cannot handle that:
>
> # module IntMapMap = Map.Make(IntMap);;
> Signature mismatch:
> Modules do not match:
>  sig
>    type key = int
>    type 'a t = 'a IntMap.t
>    val empty : 'a t
>    val is_empty : 'a t -> bool
>    val add : key -> 'a -> 'a t -> 'a t
>    val find : key -> 'a t -> 'a
>    val remove : key -> 'a t -> 'a t
>    val mem : key -> 'a t -> bool
>    val iter : (key -> 'a -> unit) -> 'a t -> unit
>    val map : ('a -> 'b) -> 'a t -> 'b t
>    val mapi : (key -> 'a -> 'b) -> 'a t -> 'b t
>    val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
>    val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
>    val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
>  end
> is not included in
>  Map.OrderedType
> Type declarations do not match:
>  type 'a t = 'a IntMap.t
> is not included in
>  type t

How do you know how to compare two generic types?  Yes, you need another 
functor here, to give a comparison function for the types being held. 
Note that the functors propogate in exactly the same way as type class 
dependencies propogate.

Here is one place where (small) changes to the language, the addition of 
some syntactic sugar, could make things a lot more usefull.  I'm thinking 
of something like making => a special operator, so that:

Ord t => let foo (x: t) (y : t) = ...

is the same as:

module Foo(X: Ord) = struct
 	type t = X.t;;
 	open X;;
 	let foo (x: t) (y: t) = ...
end;;

or something.  And some similar bit of syntactic sugar to make 
instantiating a functor lower cost as well.  Obviously this idea has some 
problems.  My point is that is that it should be possible to come up with 
some sort of reasonable extension of the language to allow functors to be 
more "type-class like".

Brian