Version franēaise
Home     About     Download     Resources     Contact us    
Browse thread
Problem with module inclusion
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Fabrice Marchant <fabricemarchant@f...>
Subject: Problem with module inclusion
  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