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.0 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 9E6D8BB84 for ; Wed, 4 Jun 2008 16:12:29 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AosAAMs/RkjUGyobmmdsb2JhbACBVZAvAQEBAQEGBwgHEQOWfIcJ X-IronPort-AV: E=Sophos;i="4.27,589,1204498800"; d="scan'208";a="13221544" Received: from concorde.inria.fr ([192.93.2.39]) by mail1-smtp-roc.national.inria.fr with ESMTP; 04 Jun 2008 16:12:29 +0200 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id m54ECQ0d024155 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Wed, 4 Jun 2008 16:12:29 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AosAAMs/RkjUGyobmmdsb2JhbACBVZAvAQEBAQEGBwgHEQOWfIcJ X-IronPort-AV: E=Sophos;i="4.27,589,1204498800"; d="scan'208";a="13221543" Received: from smtp1-g19.free.fr ([212.27.42.27]) by mail1-smtp-roc.national.inria.fr with ESMTP; 04 Jun 2008 16:12:29 +0200 Received: from smtp1-g19.free.fr (localhost.localdomain [127.0.0.1]) by smtp1-g19.free.fr (Postfix) with ESMTP id CA3DC1AB2E2 for ; Wed, 4 Jun 2008 16:12:28 +0200 (CEST) Received: from localhost.localdomain (c5850-a2-3-62-147-8-79.dial.proxad.net [62.147.8.79]) by smtp1-g19.free.fr (Postfix) with ESMTP id A47FF1AB2BB for ; Wed, 4 Jun 2008 16:12:27 +0200 (CEST) Date: Wed, 4 Jun 2008 14:19:59 +0200 From: Fabrice Marchant To: caml-list@inria.fr Subject: Problem with module inclusion Message-ID: <20080604141959.6e095093@free.fr> X-Mailer: Claws Mail 3.2.0 (GTK+ 2.12.9; i486-pc-linux-gnu) X-Face: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 4846A2CA.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 functor:01 orderedtype:01 sig:01 sig:01 val:01 val:01 bool:01 bool:01 struct:01 printf:01 printf:01 functor:01 struct:01 orderedtype:01 Hi senior list ! Another question about learning OCaml : apologize. It lands here unchanged from Beginners list : Here is a counter functor : *) module Counters ( X : Map.OrderedType ) : sig module XMap : sig type 'a t = 'a Map.Make(X).t val empty : 'a t val add : X.t -> 'a -> 'a t -> 'a t val find : X.t -> 'a t -> 'a val fold : (X.t -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool end type t = int XMap.t val equal : t -> t -> bool val zeroes : t val incr : t -> X.t -> t val to_list : t -> (X.t * int) list end = struct module XMap = Map.Make( X ) type t = int XMap.t let equal = XMap.equal ( = ) let zeroes = XMap.empty let incr map e = (XMap.add e (try succ (XMap.find e map) with Not_found -> 1)) map let to_list map = XMap.fold (fun k d a -> (k, d) :: a ) map [] end module StringCounters = Counters ( String ) let _ = let l= ["7"; "8192"; "7"; "199"; "199"; "7"; "7"] in Printf.printf "[\"%s\"] -> %s\n" (String.concat "\"; \"" l) (String.concat " + " (List.map (fun (k, d) -> (string_of_int d) ^ "x\"" ^ k ^ "\"") (StringCounters.to_list (List.fold_left StringCounters.incr StringCounters.zeroes l)))) (* -> 1x"8192" + 4x"7" + 2x"199" *) (* My problem is I want to reject the 'to_list' function (that isn't specific to counting) from Counters functor to an extension of Map : So I tried to define : *) module MapPlus ( Map : Map.S ) = struct include Map let to_list map = Map.fold (fun k d a -> (k, d) :: a ) map [] end (* before Counters, that would be modified this way : *) module Counters ( X : Map.OrderedType ) : sig module XMap : sig type 'a t = 'a Map.Make(X).t val empty : 'a t val add : X.t -> 'a -> 'a t -> 'a t val find : X.t -> 'a t -> 'a val fold : (X.t -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool end type t = int XMap.t val equal : t -> t -> bool val zeroes : t val incr : t -> X.t -> t end = struct module XMap = MapPlus ( Map.Make( X ) ) type t = int XMap.t let equal = XMap.equal ( = ) let zeroes = XMap.empty let incr map e = (XMap.add e (try succ (XMap.find e map) with Not_found -> 1)) map end (* unfortunately this doesn't work : how to access 'to_list' function through the Counters module ? With StringCounters.XMap.to_list ? I'm confused. Should the Counters signature be modified ? Fabrice