From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 7F5AABBC4 for ; Wed, 4 Mar 2009 23:57:45 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AiIHAIOWrknUnwdkZGdsb2JhbACCHZJOGgkFBwcPBsMehAgG X-IronPort-AV: E=Sophos;i="4.38,304,1233529200"; d="scan'208";a="25080569" Received: from relay.pcl-ipout02.plus.net ([212.159.7.100]) by mail1-smtp-roc.national.inria.fr with ESMTP; 04 Mar 2009 23:57:45 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: An4FAEeWrknUnw4T/2dsb2JhbACCHdY0hAgG Received: from pih-relay06.plus.net ([212.159.14.19]) by relay.pcl-ipout02.plus.net with ESMTP; 04 Mar 2009 22:57:44 +0000 Received: from [87.112.234.120] (helo=leper.local) by pih-relay06.plus.net with esmtp (Exim) id 1Lf024-0004CD-7z for caml-list@yquem.inria.fr; Wed, 04 Mar 2009 22:57:44 +0000 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] stl? Date: Wed, 4 Mar 2009 23:03:07 +0000 User-Agent: KMail/1.9.9 References: <91a2ba3e0903031340wcdc976cp52522eb35f7ccb73@mail.gmail.com> <87ab81yrog.fsf@aryx.cs.uiuc.edu> In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200903042303.07629.jon@ffconsultancy.com> X-Plusnet-Relay: 1a24d3fd3934fb47835db1af6215db19 X-Spam: no; 0.00; stl:01 functors:01 ocaml:01 struct:01 sig:01 val:01 val:01 bool:01 bool:01 iter:01 functor:01 mismatch:01 sig:01 iter:01 orderedtype:01 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