caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* How can I set a type parameter of Map.Make(X) ?
@ 2010-09-20 14:35 Dumitru Potop-Butucaru
  2010-09-20 14:57 ` [Caml-list] " Ashish Agarwal
  2010-09-20 14:59 ` How can I set a type parameter of Map.Make(X) ? Sylvain Le Gall
  0 siblings, 2 replies; 9+ messages in thread
From: Dumitru Potop-Butucaru @ 2010-09-20 14:35 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 373 bytes --]


Hello,

I'm certain most users here will consider the question trivially simple, 
but I browsed the documentation without finding a solution.

The question is quite general: Given a polymorphic definition like 
Map.Make(X), where
X is some module, how can I specialize its 'a type parameter, e.g. by 
setting it to Y, so that
I have maps from X to Y ?

Yours,
Jacky Potop

[-- Attachment #2: dumitru_potop_butucaru.vcf --]
[-- Type: text/x-vcard, Size: 335 bytes --]

begin:vcard
fn:Dumitru Potop-Butucaru
n:Potop-Butucaru;Dumitru
org:INRIA Rocquencourt;Project AOSTE
adr:;;Domaine de Voluceau, BP 105;Le Chesnay;;F-78153;France
email;internet:dumitru.potop_butucaru@inria.fr
tel;work:+33-139.63.55.80
tel;fax:+33-139.63.51.93
x-mozilla-html:FALSE
url:http://www.DumitruPotop.net
version:2.1
end:vcard


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

* Re: [Caml-list] How can I set a type parameter of Map.Make(X) ?
  2010-09-20 14:35 How can I set a type parameter of Map.Make(X) ? Dumitru Potop-Butucaru
@ 2010-09-20 14:57 ` Ashish Agarwal
  2010-09-20 17:25   ` Dumitru Potop-Butucaru
  2010-09-20 14:59 ` How can I set a type parameter of Map.Make(X) ? Sylvain Le Gall
  1 sibling, 1 reply; 9+ messages in thread
From: Ashish Agarwal @ 2010-09-20 14:57 UTC (permalink / raw)
  To: Dumitru Potop-Butucaru; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1059 bytes --]

module M = Map.Make(String)

type t = int M.t

Type t is the type of maps from string's to int's. Or alternatively write a
function that assumes 'a is some specific type:

# let f m = M.fold (fun _ x y -> x + y) m 0;;
val f : int M.t -> int = <fun>


On Mon, Sep 20, 2010 at 10:35 AM, Dumitru Potop-Butucaru <
dumitru.potop_butucaru@inria.fr> wrote:

>
> Hello,
>
> I'm certain most users here will consider the question trivially simple,
> but I browsed the documentation without finding a solution.
>
> The question is quite general: Given a polymorphic definition like
> Map.Make(X), where
> X is some module, how can I specialize its 'a type parameter, e.g. by
> setting it to Y, so that
> I have maps from X to Y ?
>
> Yours,
> Jacky Potop
>
> _______________________________________________
> 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
>
>

[-- Attachment #2: Type: text/html, Size: 1811 bytes --]

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

* Re: How can I set a type parameter of Map.Make(X) ?
  2010-09-20 14:35 How can I set a type parameter of Map.Make(X) ? Dumitru Potop-Butucaru
  2010-09-20 14:57 ` [Caml-list] " Ashish Agarwal
@ 2010-09-20 14:59 ` Sylvain Le Gall
  1 sibling, 0 replies; 9+ messages in thread
From: Sylvain Le Gall @ 2010-09-20 14:59 UTC (permalink / raw)
  To: caml-list

On 20-09-2010, Dumitru Potop-Butucaru <dumitru.potop_butucaru@inria.fr> wrote:
>
> I'm certain most users here will consider the question trivially simple, 
> but I browsed the documentation without finding a solution.
>

There is an ocaml-beginner list, if you feel the question is trivial:
http://tech.groups.yahoo.com/group/ocaml_beginners/

> The question is quite general: Given a polymorphic definition like 
> Map.Make(X), where
> X is some module, how can I specialize its 'a type parameter, e.g. by 
> setting it to Y, so that
> I have maps from X to Y ?
>

module M = Map.Make(X)

type y_map = y M.t 

(* N.B. Y is syntactically incorrect for a type identifier, you need to
use y *)

Regards
Sylvain Le Gall


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

* Re: [Caml-list] How can I set a type parameter of Map.Make(X) ?
  2010-09-20 14:57 ` [Caml-list] " Ashish Agarwal
@ 2010-09-20 17:25   ` Dumitru Potop-Butucaru
  2010-09-20 17:31     ` Ashish Agarwal
  2010-09-20 17:58     ` Martin Jambon
  0 siblings, 2 replies; 9+ messages in thread
From: Dumitru Potop-Butucaru @ 2010-09-20 17:25 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1409 bytes --]


Actually, I was looking for a way to specialize a whole module,
not just the associated type (**this** I knew how to do).
I would like to write something like:

     include module type of Set.Make(String) with 'a= int

Is this possible?

Yours,
Jacky Potop




On 20/09/2010 16:57, Ashish Agarwal wrote:
> module M = Map.Make(String)
>
> type t = int M.t
>
> Type t is the type of maps from string's to int's. Or alternatively write a
> function that assumes 'a is some specific type:
>
> # let f m = M.fold (fun _ x y ->  x + y) m 0;;
> val f : int M.t ->  int =<fun>
>
>
> On Mon, Sep 20, 2010 at 10:35 AM, Dumitru Potop-Butucaru<
> dumitru.potop_butucaru@inria.fr>  wrote:
>
>> Hello,
>>
>> I'm certain most users here will consider the question trivially simple,
>> but I browsed the documentation without finding a solution.
>>
>> The question is quite general: Given a polymorphic definition like
>> Map.Make(X), where
>> X is some module, how can I specialize its 'a type parameter, e.g. by
>> setting it to Y, so that
>> I have maps from X to Y ?
>>
>> Yours,
>> Jacky Potop
>>
>> _______________________________________________
>> 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
>>
>>


[-- Attachment #2: dumitru_potop_butucaru.vcf --]
[-- Type: text/x-vcard, Size: 335 bytes --]

begin:vcard
fn:Dumitru Potop-Butucaru
n:Potop-Butucaru;Dumitru
org:INRIA Rocquencourt;Project AOSTE
adr:;;Domaine de Voluceau, BP 105;Le Chesnay;;F-78153;France
email;internet:dumitru.potop_butucaru@inria.fr
tel;work:+33-139.63.55.80
tel;fax:+33-139.63.51.93
x-mozilla-html:FALSE
url:http://www.DumitruPotop.net
version:2.1
end:vcard


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

* Re: [Caml-list] How can I set a type parameter of Map.Make(X) ?
  2010-09-20 17:25   ` Dumitru Potop-Butucaru
@ 2010-09-20 17:31     ` Ashish Agarwal
  2010-09-20 17:58     ` Martin Jambon
  1 sibling, 0 replies; 9+ messages in thread
From: Ashish Agarwal @ 2010-09-20 17:31 UTC (permalink / raw)
  To: Dumitru Potop-Butucaru; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 2003 bytes --]

Why do you want to do this? What benefit does it bring that cannot be
achieved by leaving the polymorphic type?

On Mon, Sep 20, 2010 at 1:25 PM, Dumitru Potop-Butucaru <
dumitru.potop_butucaru@inria.fr> wrote:

>
> Actually, I was looking for a way to specialize a whole module,
> not just the associated type (**this** I knew how to do).
> I would like to write something like:
>
>    include module type of Set.Make(String) with 'a= int
>
> Is this possible?
>
> Yours,
> Jacky Potop
>
>
>
>
>
> On 20/09/2010 16:57, Ashish Agarwal wrote:
>
>> module M = Map.Make(String)
>>
>> type t = int M.t
>>
>> Type t is the type of maps from string's to int's. Or alternatively write
>> a
>> function that assumes 'a is some specific type:
>>
>> # let f m = M.fold (fun _ x y ->  x + y) m 0;;
>> val f : int M.t ->  int =<fun>
>>
>>
>> On Mon, Sep 20, 2010 at 10:35 AM, Dumitru Potop-Butucaru<
>> dumitru.potop_butucaru@inria.fr>  wrote:
>>
>>  Hello,
>>>
>>> I'm certain most users here will consider the question trivially simple,
>>> but I browsed the documentation without finding a solution.
>>>
>>> The question is quite general: Given a polymorphic definition like
>>> Map.Make(X), where
>>> X is some module, how can I specialize its 'a type parameter, e.g. by
>>> setting it to Y, so that
>>> I have maps from X to Y ?
>>>
>>> Yours,
>>> Jacky Potop
>>>
>>> _______________________________________________
>>> 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
>>>
>>>
>>>
>
> _______________________________________________
> 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
>
>

[-- Attachment #2: Type: text/html, Size: 3340 bytes --]

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

* Re: [Caml-list] How can I set a type parameter of Map.Make(X) ?
  2010-09-20 17:25   ` Dumitru Potop-Butucaru
  2010-09-20 17:31     ` Ashish Agarwal
@ 2010-09-20 17:58     ` Martin Jambon
  2010-09-20 19:02       ` Dumitru Potop-Butucaru
  1 sibling, 1 reply; 9+ messages in thread
From: Martin Jambon @ 2010-09-20 17:58 UTC (permalink / raw)
  To: Dumitru Potop-Butucaru; +Cc: caml-list

Dumitru Potop-Butucaru wrote:
> 
> Actually, I was looking for a way to specialize a whole module,
> not just the associated type (**this** I knew how to do).
> I would like to write something like:
> 
>     include module type of Set.Make(String) with 'a= int
> 
> Is this possible?

I don't know about such a shortcut, but the following works and the interface
is easier to use for a human:

(* foo.mli *)
type key = string
type value = string
type map
val empty : map
val is_empty : map -> bool
val add : key -> value -> map -> map
val find : key -> map -> value
val remove : key -> map -> map
val mem : key -> map -> bool
val iter : (key -> value -> unit) -> map -> unit
val map : (value -> value) -> map -> map
val mapi : (key -> value -> value) -> map -> map
val fold : (key -> value -> 'a -> 'a) -> map -> 'a -> 'a
val compare : (value -> value -> int) -> map -> map -> int
val equal : (value -> value -> bool) -> map -> map -> bool


(* foo.ml *)
module M = Map.Make (String)
include M
type value = string
type map = string M.t



Martin


> Yours,
> Jacky Potop
> 
> 
> 
> 
> On 20/09/2010 16:57, Ashish Agarwal wrote:
>> module M = Map.Make(String)
>>
>> type t = int M.t
>>
>> Type t is the type of maps from string's to int's. Or alternatively
>> write a
>> function that assumes 'a is some specific type:
>>
>> # let f m = M.fold (fun _ x y ->  x + y) m 0;;
>> val f : int M.t ->  int =<fun>
>>
>>
>> On Mon, Sep 20, 2010 at 10:35 AM, Dumitru Potop-Butucaru<
>> dumitru.potop_butucaru@inria.fr>  wrote:
>>
>>> Hello,
>>>
>>> I'm certain most users here will consider the question trivially simple,
>>> but I browsed the documentation without finding a solution.
>>>
>>> The question is quite general: Given a polymorphic definition like
>>> Map.Make(X), where
>>> X is some module, how can I specialize its 'a type parameter, e.g. by
>>> setting it to Y, so that
>>> I have maps from X to Y ?
>>>
>>> Yours,
>>> Jacky Potop
>>>
>>> _______________________________________________
>>> 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
>>>
>>>
> 
> _______________________________________________
> 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


-- 
http://mjambon.com/


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

* Re: [Caml-list] How can I set a type parameter of Map.Make(X) ?
  2010-09-20 17:58     ` Martin Jambon
@ 2010-09-20 19:02       ` Dumitru Potop-Butucaru
  2010-09-20 19:32         ` Ashish Agarwal
  2010-09-20 19:35         ` code duplication (was Re: [Caml-list] How can I set a type parameter of Map.Make(X) ?) Martin Jambon
  0 siblings, 2 replies; 9+ messages in thread
From: Dumitru Potop-Butucaru @ 2010-09-20 19:02 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 3127 bytes --]


Yes, but this involves duplicating code, and I really hate to duplicate 
code, even in interfaces.

To also answer Ashish: I want to define an interface to modules where I 
use a very specific kind of map. Of course, I could leave Map.Make 
polymorphic, but that is a different module type than the one I want to 
use to represent my theory.

Yours,
Jacky


On 20/09/2010 19:58, Martin Jambon wrote:
> Dumitru Potop-Butucaru wrote:
>> Actually, I was looking for a way to specialize a whole module,
>> not just the associated type (**this** I knew how to do).
>> I would like to write something like:
>>
>>      include module type of Set.Make(String) with 'a= int
>>
>> Is this possible?
> I don't know about such a shortcut, but the following works and the interface
> is easier to use for a human:
>
> (* foo.mli *)
> type key = string
> type value = string
> type map
> val empty : map
> val is_empty : map ->  bool
> val add : key ->  value ->  map ->  map
> val find : key ->  map ->  value
> val remove : key ->  map ->  map
> val mem : key ->  map ->  bool
> val iter : (key ->  value ->  unit) ->  map ->  unit
> val map : (value ->  value) ->  map ->  map
> val mapi : (key ->  value ->  value) ->  map ->  map
> val fold : (key ->  value ->  'a ->  'a) ->  map ->  'a ->  'a
> val compare : (value ->  value ->  int) ->  map ->  map ->  int
> val equal : (value ->  value ->  bool) ->  map ->  map ->  bool
>
>
> (* foo.ml *)
> module M = Map.Make (String)
> include M
> type value = string
> type map = string M.t
>
>
>
> Martin
>
>
>> Yours,
>> Jacky Potop
>>
>>
>>
>>
>> On 20/09/2010 16:57, Ashish Agarwal wrote:
>>> module M = Map.Make(String)
>>>
>>> type t = int M.t
>>>
>>> Type t is the type of maps from string's to int's. Or alternatively
>>> write a
>>> function that assumes 'a is some specific type:
>>>
>>> # let f m = M.fold (fun _ x y ->   x + y) m 0;;
>>> val f : int M.t ->   int =<fun>
>>>
>>>
>>> On Mon, Sep 20, 2010 at 10:35 AM, Dumitru Potop-Butucaru<
>>> dumitru.potop_butucaru@inria.fr>   wrote:
>>>
>>>> Hello,
>>>>
>>>> I'm certain most users here will consider the question trivially simple,
>>>> but I browsed the documentation without finding a solution.
>>>>
>>>> The question is quite general: Given a polymorphic definition like
>>>> Map.Make(X), where
>>>> X is some module, how can I specialize its 'a type parameter, e.g. by
>>>> setting it to Y, so that
>>>> I have maps from X to Y ?
>>>>
>>>> Yours,
>>>> Jacky Potop
>>>>
>>>> _______________________________________________
>>>> 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
>>>>
>>>>
>> _______________________________________________
>> 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
>


[-- Attachment #2: dumitru_potop_butucaru.vcf --]
[-- Type: text/x-vcard, Size: 335 bytes --]

begin:vcard
fn:Dumitru Potop-Butucaru
n:Potop-Butucaru;Dumitru
org:INRIA Rocquencourt;Project AOSTE
adr:;;Domaine de Voluceau, BP 105;Le Chesnay;;F-78153;France
email;internet:dumitru.potop_butucaru@inria.fr
tel;work:+33-139.63.55.80
tel;fax:+33-139.63.51.93
x-mozilla-html:FALSE
url:http://www.DumitruPotop.net
version:2.1
end:vcard


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

* Re: [Caml-list] How can I set a type parameter of Map.Make(X) ?
  2010-09-20 19:02       ` Dumitru Potop-Butucaru
@ 2010-09-20 19:32         ` Ashish Agarwal
  2010-09-20 19:35         ` code duplication (was Re: [Caml-list] How can I set a type parameter of Map.Make(X) ?) Martin Jambon
  1 sibling, 0 replies; 9+ messages in thread
From: Ashish Agarwal @ 2010-09-20 19:32 UTC (permalink / raw)
  To: Dumitru Potop-Butucaru; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 4033 bytes --]

How about this:

--- a.mli ---
type map

val fold : (string -> int -> 'a -> 'a) -> map -> 'a -> 'a
val iter : (string -> int -> unit) -> map -> unit

--- a.ml ---
module M = Map.Make(String)
type map = int M.t
include M

You avoid boilerplate in the implementation, but I don't know how to avoid
writing out the restricted types in the signature.




On Mon, Sep 20, 2010 at 3:02 PM, Dumitru Potop-Butucaru <
dumitru.potop_butucaru@inria.fr> wrote:

>
> Yes, but this involves duplicating code, and I really hate to duplicate
> code, even in interfaces.
>
> To also answer Ashish: I want to define an interface to modules where I use
> a very specific kind of map. Of course, I could leave Map.Make polymorphic,
> but that is a different module type than the one I want to use to represent
> my theory.
>
> Yours,
> Jacky
>
>
>
> On 20/09/2010 19:58, Martin Jambon wrote:
>
>> Dumitru Potop-Butucaru wrote:
>>
>>> Actually, I was looking for a way to specialize a whole module,
>>> not just the associated type (**this** I knew how to do).
>>> I would like to write something like:
>>>
>>>     include module type of Set.Make(String) with 'a= int
>>>
>>> Is this possible?
>>>
>> I don't know about such a shortcut, but the following works and the
>> interface
>> is easier to use for a human:
>>
>> (* foo.mli *)
>> type key = string
>> type value = string
>> type map
>> val empty : map
>> val is_empty : map ->  bool
>> val add : key ->  value ->  map ->  map
>> val find : key ->  map ->  value
>> val remove : key ->  map ->  map
>> val mem : key ->  map ->  bool
>> val iter : (key ->  value ->  unit) ->  map ->  unit
>> val map : (value ->  value) ->  map ->  map
>> val mapi : (key ->  value ->  value) ->  map ->  map
>> val fold : (key ->  value ->  'a ->  'a) ->  map ->  'a ->  'a
>> val compare : (value ->  value ->  int) ->  map ->  map ->  int
>> val equal : (value ->  value ->  bool) ->  map ->  map ->  bool
>>
>>
>> (* foo.ml *)
>> module M = Map.Make (String)
>> include M
>> type value = string
>> type map = string M.t
>>
>>
>>
>> Martin
>>
>>
>>  Yours,
>>> Jacky Potop
>>>
>>>
>>>
>>>
>>> On 20/09/2010 16:57, Ashish Agarwal wrote:
>>>
>>>> module M = Map.Make(String)
>>>>
>>>> type t = int M.t
>>>>
>>>> Type t is the type of maps from string's to int's. Or alternatively
>>>> write a
>>>> function that assumes 'a is some specific type:
>>>>
>>>> # let f m = M.fold (fun _ x y ->   x + y) m 0;;
>>>> val f : int M.t ->   int =<fun>
>>>>
>>>>
>>>> On Mon, Sep 20, 2010 at 10:35 AM, Dumitru Potop-Butucaru<
>>>> dumitru.potop_butucaru@inria.fr>   wrote:
>>>>
>>>>  Hello,
>>>>>
>>>>> I'm certain most users here will consider the question trivially
>>>>> simple,
>>>>> but I browsed the documentation without finding a solution.
>>>>>
>>>>> The question is quite general: Given a polymorphic definition like
>>>>> Map.Make(X), where
>>>>> X is some module, how can I specialize its 'a type parameter, e.g. by
>>>>> setting it to Y, so that
>>>>> I have maps from X to Y ?
>>>>>
>>>>> Yours,
>>>>> Jacky Potop
>>>>>
>>>>> _______________________________________________
>>>>> 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
>>>>>
>>>>>
>>>>>  _______________________________________________
>>> 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
>>>
>>
>>
>
> _______________________________________________
> 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
>
>

[-- Attachment #2: Type: text/html, Size: 7213 bytes --]

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

* code duplication (was Re: [Caml-list] How can I set a type parameter of Map.Make(X) ?)
  2010-09-20 19:02       ` Dumitru Potop-Butucaru
  2010-09-20 19:32         ` Ashish Agarwal
@ 2010-09-20 19:35         ` Martin Jambon
  1 sibling, 0 replies; 9+ messages in thread
From: Martin Jambon @ 2010-09-20 19:35 UTC (permalink / raw)
  To: Dumitru Potop-Butucaru; +Cc: caml-list

Dumitru Potop-Butucaru wrote:
> 
> Yes, but this involves duplicating code, and I really hate to duplicate
> code, even in interfaces.

We could also argue that writing interfaces in general is code duplication.

Duplicating source code or data is bad only if future bugs and enhancements
are not guaranteed to be applied to all the copies.  It's not the case here
because if the Map module of the standard library ever changes in an
incompatible way, your .ml will no longer be compatible with your .mli and the
compiler will not let this pass.

Duplicating the comments of the standard map.mli may however not such a good
idea because as always the compiler ignores them.  Adding something like (*
See the documentation of the standard Map module *) seems reasonable to me.


Martin

> To also answer Ashish: I want to define an interface to modules where I
> use a very specific kind of map. Of course, I could leave Map.Make
> polymorphic, but that is a different module type than the one I want to
> use to represent my theory.
> 
> Yours,
> Jacky
> 
> 
> On 20/09/2010 19:58, Martin Jambon wrote:
>> Dumitru Potop-Butucaru wrote:
>>> Actually, I was looking for a way to specialize a whole module,
>>> not just the associated type (**this** I knew how to do).
>>> I would like to write something like:
>>>
>>>      include module type of Set.Make(String) with 'a= int
>>>
>>> Is this possible?
>> I don't know about such a shortcut, but the following works and the
>> interface
>> is easier to use for a human:
>>
>> (* foo.mli *)
>> type key = string
>> type value = string
>> type map
>> val empty : map
>> val is_empty : map ->  bool
>> val add : key ->  value ->  map ->  map
>> val find : key ->  map ->  value
>> val remove : key ->  map ->  map
>> val mem : key ->  map ->  bool
>> val iter : (key ->  value ->  unit) ->  map ->  unit
>> val map : (value ->  value) ->  map ->  map
>> val mapi : (key ->  value ->  value) ->  map ->  map
>> val fold : (key ->  value ->  'a ->  'a) ->  map ->  'a ->  'a
>> val compare : (value ->  value ->  int) ->  map ->  map ->  int
>> val equal : (value ->  value ->  bool) ->  map ->  map ->  bool
>>
>>
>> (* foo.ml *)
>> module M = Map.Make (String)
>> include M
>> type value = string
>> type map = string M.t
>>
>>
>>
>> Martin
>>
>>
>>> Yours,
>>> Jacky Potop
>>>
>>>
>>>
>>>
>>> On 20/09/2010 16:57, Ashish Agarwal wrote:
>>>> module M = Map.Make(String)
>>>>
>>>> type t = int M.t
>>>>
>>>> Type t is the type of maps from string's to int's. Or alternatively
>>>> write a
>>>> function that assumes 'a is some specific type:
>>>>
>>>> # let f m = M.fold (fun _ x y ->   x + y) m 0;;
>>>> val f : int M.t ->   int =<fun>
>>>>
>>>>
>>>> On Mon, Sep 20, 2010 at 10:35 AM, Dumitru Potop-Butucaru<
>>>> dumitru.potop_butucaru@inria.fr>   wrote:
>>>>
>>>>> Hello,
>>>>>
>>>>> I'm certain most users here will consider the question trivially
>>>>> simple,
>>>>> but I browsed the documentation without finding a solution.
>>>>>
>>>>> The question is quite general: Given a polymorphic definition like
>>>>> Map.Make(X), where
>>>>> X is some module, how can I specialize its 'a type parameter, e.g. by
>>>>> setting it to Y, so that
>>>>> I have maps from X to Y ?
>>>>>
>>>>> Yours,
>>>>> Jacky Potop
>>>>>
>>>>> _______________________________________________
>>>>> 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
>>>>>
>>>>>
>>> _______________________________________________
>>> 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
>>
> 
> _______________________________________________
> 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


-- 
http://mjambon.com/


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

end of thread, other threads:[~2010-09-20 19:36 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-20 14:35 How can I set a type parameter of Map.Make(X) ? Dumitru Potop-Butucaru
2010-09-20 14:57 ` [Caml-list] " Ashish Agarwal
2010-09-20 17:25   ` Dumitru Potop-Butucaru
2010-09-20 17:31     ` Ashish Agarwal
2010-09-20 17:58     ` Martin Jambon
2010-09-20 19:02       ` Dumitru Potop-Butucaru
2010-09-20 19:32         ` Ashish Agarwal
2010-09-20 19:35         ` code duplication (was Re: [Caml-list] How can I set a type parameter of Map.Make(X) ?) Martin Jambon
2010-09-20 14:59 ` How can I set a type parameter of Map.Make(X) ? Sylvain Le Gall

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