From: Fabrice Marchant <fabricemarchant@free.fr>
To: caml-list@inria.fr
Subject: Problem with module inclusion
Date: Wed, 4 Jun 2008 14:19:59 +0200 [thread overview]
Message-ID: <20080604141959.6e095093@free.fr> (raw)
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
next reply other threads:[~2008-06-04 14:12 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-06-04 12:19 Fabrice Marchant [this message]
2008-06-05 7:36 ` [Caml-list] " Jean-Christophe Filliâtre
2008-06-05 18:10 ` Fabrice Marchant
-- strict thread matches above, loose matches on Subject: below --
2008-06-04 11:40 Fabrice Marchant
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20080604141959.6e095093@free.fr \
--to=fabricemarchant@free.fr \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox