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: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] stl?
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

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

So you have to create another temporary module from the IntMap one:

# module IntMapMap = Map.Make(struct
                                type t = string IntMap.t
                                let compare =
                                  IntMap.compare compare
                              end);;
module IntMapMap :
  sig
    type key = string IntMap.t
    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

All of that code is completely redundant. In F#, you write nothing at all and 
get the same functionality with more safety.

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e