caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Problem with module inclusion
@ 2008-06-04 11:40 Fabrice Marchant
  0 siblings, 0 replies; 2+ messages in thread
From: Fabrice Marchant @ 2008-06-04 11:40 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] 2+ messages in thread

* Problem with module inclusion
@ 2008-06-04 12:19 Fabrice Marchant
  0 siblings, 0 replies; 2+ 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] 2+ messages in thread

end of thread, other threads:[~2008-06-04 14:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-04 11:40 Problem with module inclusion Fabrice Marchant
2008-06-04 12:19 Fabrice Marchant

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).