Version française
Home     About     Download     Resources     Contact us    
Browse thread
module interface?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Christopher L Conway <cconway@c...>
Subject: Re: [Caml-list] module interface?
On 7/30/07, skaller <skaller@users.sourceforge.net> wrote:
> On Mon, 2007-07-30 at 09:03 +0200, Benedikt Grundmann wrote:
> > module Drules : Map.S with type key = string
>
> So I discovered -- thanks! But hmm, this totally counter intuitive!
>
> You call Map.Make, but refer to Map.S.
> The supplied key is with
>
>         type t = string
>
> but in the sig you use
>
>         type key = string
>
> and finally the
>
>         let compare = compare
>
> in the module is simply dropped.
>
> There is no example of this in the tutorial..
> how would I every guess at the right solution?

Map.Make has functor type (Map.OrderedType -> Map.S), so a module
Map.Make( Ord ) will have module type Map.S. Further, the type of
Map.Make has the restriction that Map.Make( Ord ).key = Ord.t. Since
the identity of Ord is hidden in your .mli, you have to expose it with
your own restriction (if it matters, which it probably does), thus:

module Drules : Map.S with type key = string

The function Ord.compare is "dropped" (hidden, really) because it's
not part of the module type Map.S. You could write your own functor
that exposes it (note that "compare" is bound in Map.S to the function
which compares two maps):

# module type MyMapS = sig
    include Map.S
    val compare_keys : key -> key -> int
  end ;;
      module type MyMapS =
  sig
    type key
    type +'a t
    val empty : 'a t
    val is_empty : 'a t -> bool
    val add : key -> 'a -> 'a t -> 'a t
    val find : key -> 'a t -> 'a
    val remove : key -> 'a t -> 'a t
    val mem : key -> 'a t -> bool
    val iter : (key -> 'a -> unit) -> 'a t -> unit
    val map : ('a -> 'b) -> 'a t -> 'b t
    val mapi : (key -> 'a -> 'b) -> 'a t -> 'b t
    val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
    val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
    val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
    val compare_keys : key -> key -> int
  end
# module MyMap( Ord : Map.OrderedType ) = struct
    module M = Map.Make( Ord )
    open M
    let compare_keys = Ord.compare
  end ;;
        module MyMap :
  functor (Ord : Map.OrderedType) ->
    sig
      module M :
        sig
          type key = Ord.t
          type 'a t = 'a Map.Make(Ord).t
          val empty : 'a t
          val is_empty : 'a t -> bool
          val add : key -> 'a -> 'a t -> 'a t
          val find : key -> 'a t -> 'a
          val remove : key -> 'a t -> 'a t
          val mem : key -> 'a t -> bool
          val iter : (key -> 'a -> unit) -> 'a t -> unit
          val map : ('a -> 'b) -> 'a t -> 'b t
          val mapi : (key -> 'a -> 'b) -> 'a t -> 'b t
          val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
          val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
          val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
        end
      val compare_keys : Ord.t -> Ord.t -> int
    end

Regards,
Chris