caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* module interface?
@ 2007-07-30  6:45 skaller
  2007-07-30  7:03 ` [Caml-list] " Benedikt Grundmann
  0 siblings, 1 reply; 8+ messages in thread
From: skaller @ 2007-07-30  6:45 UTC (permalink / raw)
  To: caml-list

Erhm .. how to I declare this in mli file ..
without listing the instantiated Map signature by hand?

module Drules = Map.Make(struct
  type t = string
  let compare = compare
end)


-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] module interface?
  2007-07-30  6:45 module interface? skaller
@ 2007-07-30  7:03 ` Benedikt Grundmann
  2007-07-30  7:55   ` skaller
  0 siblings, 1 reply; 8+ messages in thread
From: Benedikt Grundmann @ 2007-07-30  7:03 UTC (permalink / raw)
  To: skaller; +Cc: caml-list

module Drules : Map.S with type key = string

2007/7/30, skaller <skaller@users.sourceforge.net>:
> Erhm .. how to I declare this in mli file ..
> without listing the instantiated Map signature by hand?
>
> module Drules = Map.Make(struct
>   type t = string
>   let compare = compare
> end)
>
>
> --
> John Skaller <skaller at users dot sf dot net>
> Felix, successor to C++: http://felix.sf.net
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>


-- 
Calvin: I try to make everyone's day a little more
surreal.

(From Calvin & Hobbes)


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] module interface?
  2007-07-30  7:03 ` [Caml-list] " Benedikt Grundmann
@ 2007-07-30  7:55   ` skaller
  2007-07-30  8:02     ` Benedikt Grundmann
  2007-07-30 16:43     ` Christopher L Conway
  0 siblings, 2 replies; 8+ messages in thread
From: skaller @ 2007-07-30  7:55 UTC (permalink / raw)
  To: Benedikt Grundmann; +Cc: caml-list

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?

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] module interface?
  2007-07-30  7:55   ` skaller
@ 2007-07-30  8:02     ` Benedikt Grundmann
  2007-07-30  8:16       ` skaller
  2007-07-30 16:43     ` Christopher L Conway
  1 sibling, 1 reply; 8+ messages in thread
From: Benedikt Grundmann @ 2007-07-30  8:02 UTC (permalink / raw)
  To: skaller; +Cc: caml-list

Easy in map.mli you find:

module Make (Ord : OrderedType) : S with type key = Ord.t

After that it is obvious (at least if you have seen it before :-) )

Cheers,

Bene



2007/7/30, skaller <skaller@users.sourceforge.net>:
> 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?
>
> --
> John Skaller <skaller at users dot sf dot net>
> Felix, successor to C++: http://felix.sf.net
>


-- 
Calvin: I try to make everyone's day a little more
surreal.

(From Calvin & Hobbes)


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] module interface?
  2007-07-30  8:02     ` Benedikt Grundmann
@ 2007-07-30  8:16       ` skaller
  0 siblings, 0 replies; 8+ messages in thread
From: skaller @ 2007-07-30  8:16 UTC (permalink / raw)
  To: Benedikt Grundmann; +Cc: caml-list

On Mon, 2007-07-30 at 10:02 +0200, Benedikt Grundmann wrote:
> Easy in map.mli you find:
> 
> module Make (Ord : OrderedType) : S with type key = Ord.t
> 
> After that it is obvious (at least if you have seen it before :-) )

Looking the *.mli file is a last resort when you have an 
bookmarked online manual  :)


-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] module interface?
  2007-07-30  7:55   ` skaller
  2007-07-30  8:02     ` Benedikt Grundmann
@ 2007-07-30 16:43     ` Christopher L Conway
  2007-07-30 17:13       ` skaller
  1 sibling, 1 reply; 8+ messages in thread
From: Christopher L Conway @ 2007-07-30 16:43 UTC (permalink / raw)
  To: skaller; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] module interface?
  2007-07-30 16:43     ` Christopher L Conway
@ 2007-07-30 17:13       ` skaller
  2007-07-30 19:24         ` Jon Harrop
  0 siblings, 1 reply; 8+ messages in thread
From: skaller @ 2007-07-30 17:13 UTC (permalink / raw)
  To: Christopher L Conway; +Cc: caml-list, Jon Harrop

On Mon, 2007-07-30 at 12:43 -0400, Christopher L Conway wrote:
> 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

Yes, it is quite logical I'm sure, but that wasn't the issue
so much as how someone would learn to use it, i.e. documentation
and tutorial. The tutorial on functors skips over this by
elaborating the signature of the resulting module.

This looks cute in the tutorial, but is out of the question
for even simple functors like Map.Make because they have
far too many methods.

If you try ocamlc -i .. it lists all those methods. Some years
ago I actually used that in the *.mli file. It turned me off
using functors. Later I learned:

	module Drules : Map.S with type key = string

(and in fact already have several instances of that in my program,
which I forgot about).

Maybe Jon Harrop covers this well in his book?



-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] module interface?
  2007-07-30 17:13       ` skaller
@ 2007-07-30 19:24         ` Jon Harrop
  0 siblings, 0 replies; 8+ messages in thread
From: Jon Harrop @ 2007-07-30 19:24 UTC (permalink / raw)
  To: caml-list

On Monday 30 July 2007 18:13:12 skaller wrote:
> Maybe Jon Harrop covers this well in his book?

OCaml for Scientists and the latest article in the OCaml Journal describe how 
to consume functors (most notably the Set.Make and Map.Make functors) but not 
how to create functors. Future OCaml Journal articles will cover functors in 
more detail.

Also, OCaml for Scientists describes performance considerations and 
optimizations in great deal, with some discussion of defunctorization and 
measurements and discussion of the performance of the Set and Map modules 
(and Hashtbl).

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists/?e


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2007-07-30 19:34 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-30  6:45 module interface? skaller
2007-07-30  7:03 ` [Caml-list] " Benedikt Grundmann
2007-07-30  7:55   ` skaller
2007-07-30  8:02     ` Benedikt Grundmann
2007-07-30  8:16       ` skaller
2007-07-30 16:43     ` Christopher L Conway
2007-07-30 17:13       ` skaller
2007-07-30 19:24         ` Jon Harrop

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).