* Problem with module inclusion
@ 2008-06-04 12:19 Fabrice Marchant
2008-06-05 7:36 ` [Caml-list] " Jean-Christophe Filliâtre
0 siblings, 1 reply; 3+ messages in thread
From: Fabrice Marchant @ 2008-06-04 12:19 UTC (permalink / raw)
To: caml-list
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] Problem with module inclusion
2008-06-04 12:19 Problem with module inclusion Fabrice Marchant
@ 2008-06-05 7:36 ` Jean-Christophe Filliâtre
2008-06-05 18:10 ` Fabrice Marchant
0 siblings, 1 reply; 3+ messages in thread
From: Jean-Christophe Filliâtre @ 2008-06-05 7:36 UTC (permalink / raw)
To: Fabrice Marchant; +Cc: caml-list
Fabrice Marchant a écrit :
> Here is a counter functor : *)
> [...]
> (* 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 ?
You need
- either to export to_list in signature of XMap above as
val to_list : 'a t -> (X.t * 'a) list
and then you'll be able to write StringCounters.XMap.to_list in your
example
- or to re-export it in module Counters, as
let to_list = XMap.to_list
and to declare it in signature of module Counters, as
val to_list : t -> (X.t * int) list
and then to use it as StringCounters.to_list in your example.
Hope this helps,
--
Jean-Christophe
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] Problem with module inclusion
2008-06-05 7:36 ` [Caml-list] " Jean-Christophe Filliâtre
@ 2008-06-05 18:10 ` Fabrice Marchant
0 siblings, 0 replies; 3+ messages in thread
From: Fabrice Marchant @ 2008-06-05 18:10 UTC (permalink / raw)
To: caml-list
On Thu, 05 Jun 2008 09:36:09 +0200
Jean-Christophe Filliâtre <Jean-Christophe.Filliatre@lri.fr> wrote:
> You need
>
> - either to export to_list in signature of XMap above as
>
> val to_list : 'a t -> (X.t * 'a) list
>
> and then you'll be able to write StringCounters.XMap.to_list in your
> example
>
> - or to re-export it in module Counters, as
>
> let to_list = XMap.to_list
>
> and to declare it in signature of module Counters, as
>
> val to_list : t -> (X.t * int) list
>
> and then to use it as StringCounters.to_list in your example.
>
> Hope this helps,
It perfectly does ! Thanks a lot.
Among the 2 ways you explained, I prefer the former because we haven't to worry over what is the enclosing module. But maybe specifying 'int' type in the later would cause a speeder code ?
Regards,
Fabrice
http://fabrice.marchant.free.fr/siteOCaml/OCaml.html
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-06-05 20:02 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-04 12:19 Problem with module inclusion Fabrice Marchant
2008-06-05 7:36 ` [Caml-list] " Jean-Christophe Filliâtre
2008-06-05 18:10 ` Fabrice Marchant
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox