caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Hurt <bhurt@spnz.org>
To: Jon Harrop <jon@ffconsultancy.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] stl?
Date: Tue, 10 Mar 2009 23:16:19 -0400 (EDT)	[thread overview]
Message-ID: <alpine.DEB.2.00.0903102301030.10051@beast> (raw)
In-Reply-To: <200903042303.07629.jon@ffconsultancy.com>


Sorry for the late reply- I was out of town over the weekend.

On Wed, 4 Mar 2009, Jon Harrop wrote:

> On Wednesday 04 March 2009 21:59:51 Brian Hurt wrote:
>> But seriously, you hate functors that much?  The overhead of doing:
>>
>> module StringMap = Map.Make(String);;
>>
>> is so high to you, that you simply don't do it?
>>
>> Mind if I ask why?
>
> Your example is fragile: it doesn't work with Int and Float because they were
> never written:
>
> $ ocaml
>        Objective Caml version 3.09.1
>
> # module IntMap = Map.Make(Int);;
> Unbound module Int
> # module FloatMap = Map.Make(Float);;
> Unbound module Float

This isn't a limitation of the language, this is simply a short comming of 
the standard libraries.


>
> So you have to define a temporary module by hand in general:
>
> # module IntMap = Map.Make(struct type t = int let compare = compare end);;
> module IntMap :
>  sig
>    type key = int
>    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
>  end
>
> You want a mapping to IntMaps but IntMap is parameterised and the Map.Make
> functor cannot handle that:
>
> # module IntMapMap = Map.Make(IntMap);;
> Signature mismatch:
> Modules do not match:
>  sig
>    type key = int
>    type 'a t = 'a IntMap.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
> is not included in
>  Map.OrderedType
> Type declarations do not match:
>  type 'a t = 'a IntMap.t
> is not included in
>  type t

How do you know how to compare two generic types?  Yes, you need another 
functor here, to give a comparison function for the types being held. 
Note that the functors propogate in exactly the same way as type class 
dependencies propogate.

Here is one place where (small) changes to the language, the addition of 
some syntactic sugar, could make things a lot more usefull.  I'm thinking 
of something like making => a special operator, so that:

Ord t => let foo (x: t) (y : t) = ...

is the same as:

module Foo(X: Ord) = struct
 	type t = X.t;;
 	open X;;
 	let foo (x: t) (y: t) = ...
end;;

or something.  And some similar bit of syntactic sugar to make 
instantiating a functor lower cost as well.  Obviously this idea has some 
problems.  My point is that is that it should be possible to come up with 
some sort of reasonable extension of the language to allow functors to be 
more "type-class like".

Brian


  reply	other threads:[~2009-03-11  3:16 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-03 21:40 stl? Raoul Duke
2009-03-03 22:31 ` [Caml-list] stl? Yoann Padioleau
2009-03-03 22:42   ` Till Varoquaux
2009-03-03 23:36   ` Jon Harrop
2009-03-04  0:13     ` Peng Zang
2009-03-04  0:58     ` Yoann Padioleau
2009-03-04  1:10       ` Raoul Duke
2009-03-04  1:19         ` Pal-Kristian Engstad
2009-03-04  1:21         ` Yoann Padioleau
2009-03-04  1:29       ` Jon Harrop
2009-03-04 14:26     ` Kuba Ober
2009-03-04 14:24   ` Kuba Ober
2009-03-03 23:42 ` Jon Harrop
2009-03-04  0:11   ` Brian Hurt
2009-03-04  1:05     ` Yoann Padioleau
2009-03-04  4:56       ` Brian Hurt
2009-03-04 20:11         ` Yoann Padioleau
2009-03-04 21:59           ` Brian Hurt
2009-03-04 22:42             ` Yoann Padioleau
2009-03-04 23:19               ` Jon Harrop
2009-03-04 23:03             ` Jon Harrop
2009-03-11  3:16               ` Brian Hurt [this message]
2009-03-11  5:57                 ` David Rajchenbach-Teller
2009-03-11  6:11                   ` David Rajchenbach-Teller
2009-03-04  1:59     ` Jon Harrop
2009-03-04  6:11       ` Brian Hurt
2009-03-04 14:08         ` Christophe TROESTLER
2009-03-04 14:19         ` Peng Zang
2009-03-04 16:14           ` Brian Hurt
2009-03-04 16:35             ` Andreas Rossberg
2009-03-04 16:40             ` Peng Zang
2009-03-04 21:43             ` Nicolas Pouillard
2009-03-05 11:24             ` Wolfgang Lux
2009-03-04 19:45         ` Jon Harrop
2009-03-04 21:23           ` Brian Hurt
2009-03-04 23:17             ` Jon Harrop
2009-03-05  2:26             ` stl? Stefan Monnier
2009-03-04  3:10     ` [Caml-list] stl? Martin Jambon
2009-03-04  6:18       ` Brian Hurt
2009-03-04 16:35 ` Mikkel Fahnøe Jørgensen
2009-03-04 16:48   ` Yoann Padioleau
2009-03-04 20:07     ` Jon Harrop
2009-03-04 20:31       ` Richard Jones
2009-03-04 20:49       ` Yoann Padioleau
2009-03-04 21:20         ` Andreas Rossberg
2009-03-04 21:51         ` Pal-Kristian Engstad
2009-03-04 22:50           ` Jon Harrop
2009-03-04 23:18             ` Pal-Kristian Engstad
2009-03-05  1:31               ` Jon Harrop
2009-03-05  2:15                 ` Pal-Kristian Engstad
2009-03-05  3:26                   ` Jon Harrop
2009-03-05  6:22                     ` yoann padioleau
2009-03-05  7:02                       ` Raoul Duke
2009-03-05  8:07                         ` Erick Tryzelaar
2009-03-05  9:06                       ` Richard Jones
2009-03-05  9:34                         ` malc
2009-03-05  9:56                           ` Richard Jones
2009-03-05 10:49                             ` malc
2009-03-05 11:16                               ` Richard Jones
2009-03-05 12:39                                 ` malc
2009-03-05 19:39                       ` Jon Harrop
2009-03-05 21:10                       ` Pal-Kristian Engstad
2009-03-05 22:41                         ` Richard Jones
2009-03-05 22:53                         ` malc
2009-03-05  8:59                   ` Richard Jones
2009-03-05 17:50                     ` Raoul Duke
2009-03-05  8:17             ` Kuba Ober
2009-03-05  1:06         ` Jon Harrop
2009-03-05  9:09           ` Richard Jones
2009-03-05 20:44             ` Jon Harrop
2009-03-05 20:50               ` Jake Donham
2009-03-05 21:28                 ` [Caml-list] OCaml's intermediate representations Jon Harrop

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.2.00.0903102301030.10051@beast \
    --to=bhurt@spnz.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=jon@ffconsultancy.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).