* [Caml-list] Forcing a Map type: setting the value type
@ 2011-05-02 13:49 Hugo Ferreira
[not found] ` <BANLkTikn+xxaBeBC0nW0=XFibbcwbC-VFQ@mail.gmail.com>
0 siblings, 1 reply; 5+ messages in thread
From: Hugo Ferreira @ 2011-05-02 13:49 UTC (permalink / raw)
To: caml-list users
Hello,
I would like to make a Map with the key = int and
and the value also equal to int. Is this possible?
The example below does not work.
TIA,
Hugo F.
# module Domains : Map.S with type key = int
with type (+'a) t = int = Map.Make( Int );;
Error: Signature mismatch:
Modules do not match:
sig
type key = Int.t
type 'a t = 'a Map.Make(Int).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
sig
type key = int
type 'a t = int
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
Type declarations do not match:
type 'a t = 'a Map.Make(Int).t
is not included in
type 'a t = int
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Forcing a Map type: setting the value type
[not found] ` <BANLkTikn+xxaBeBC0nW0=XFibbcwbC-VFQ@mail.gmail.com>
@ 2011-05-02 14:18 ` Hugo Ferreira
2011-05-02 14:46 ` David MENTRE
0 siblings, 1 reply; 5+ messages in thread
From: Hugo Ferreira @ 2011-05-02 14:18 UTC (permalink / raw)
To: Çagdas Bozman; +Cc: caml-list users
On 05/02/2011 03:11 PM, Çagdas Bozman wrote:
> I would use something like this :
>
> module IntMap = Map.Make (struct
> type t = int
> let compare = Pervasives.compare
> end);;
>
> # let m = IntMap.empty;;
> val m : 'a IntMap.t = <abstr>
> # let m = IntMap.add 1 42 m;;
> val m : int IntMap.t = <abstr>
> # IntMap.find 1 m ;;
> - : int = 42
>
Maybe I did not express myself correctly.
The above would also work with any other type for the
parameter 'a. I want only integers for it. So:
# let m = IntMap.empty;;
# let m = IntMap.add 1 "42" m;;
should not work.
R,
Hugo F.
> 2011/5/2 Hugo Ferreira <hmf@inescporto.pt <mailto:hmf@inescporto.pt>>
>
> Hello,
>
> I would like to make a Map with the key = int and
> and the value also equal to int. Is this possible?
> The example below does not work.
>
> TIA,
> Hugo F.
>
>
> # module Domains : Map.S with type key = int
> with type (+'a) t = int = Map.Make( Int );;
> Error: Signature mismatch:
> Modules do not match:
> sig
> type key = Int.t
> type 'a t = 'a Map.Make(Int).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
> sig
> type key = int
> type 'a t = int
> 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
> Type declarations do not match:
> type 'a t = 'a Map.Make(Int).t
> is not included in
> type 'a t = int
>
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
>
> Best regards,
> --
> Çagdas Bozman <cagdas@bozman.fr <mailto:cagdas@bozman.fr>>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Forcing a Map type: setting the value type
2011-05-02 14:18 ` Hugo Ferreira
@ 2011-05-02 14:46 ` David MENTRE
2011-05-02 15:15 ` Gabriel Scherer
0 siblings, 1 reply; 5+ messages in thread
From: David MENTRE @ 2011-05-02 14:46 UTC (permalink / raw)
To: Hugo Ferreira; +Cc: Çagdas Bozman, caml-list users
Hello Hugo,
2011/5/2 Hugo Ferreira <hmf@inescporto.pt>:
> Maybe I did not express myself correctly.
> The above would also work with any other type for the
> parameter 'a. I want only integers for it. So:
>
> # let m = IntMap.empty;;
Just do:
# let m : int IntMap.t = IntMap.empty;;
The " : int IntMap.t" type annotation restricts the default "'a
IntMap.t" of IntMap.empty.
> # let m = IntMap.add 1 "42" m;;
Then:
# let m = IntMap.add 1 "42" m;;
Characters 26-27:
let m = IntMap.add 1 "42" m;;
^
This expression has type int IntMap.t but is here used with type
string IntMap.t
Best regards,
david
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Forcing a Map type: setting the value type
2011-05-02 14:46 ` David MENTRE
@ 2011-05-02 15:15 ` Gabriel Scherer
2011-05-02 15:33 ` Hugo Ferreira
0 siblings, 1 reply; 5+ messages in thread
From: Gabriel Scherer @ 2011-05-02 15:15 UTC (permalink / raw)
To: David MENTRE; +Cc: Hugo Ferreira, Çagdas Bozman, caml-list users
[-- Attachment #1: Type: text/plain, Size: 1470 bytes --]
My basic idea was to re-export Map using a constrained type. With David
Mentre's insight that only map-producing functions need to be coerced, I
have the following proposal:
module type MonoMap = sig
include Map.S
type elt
val empty : elt t
val singleton : key -> elt -> elt t
val of_enum : (key * elt) Enum.t -> elt t
end
module IntMap
: MonoMap with type key = int with type elt = int
= struct
include Map.Make(Int)
type elt = int
end
On Mon, May 2, 2011 at 4:46 PM, David MENTRE <dmentre@linux-france.org>wrote:
> Hello Hugo,
>
> 2011/5/2 Hugo Ferreira <hmf@inescporto.pt>:
> > Maybe I did not express myself correctly.
> > The above would also work with any other type for the
> > parameter 'a. I want only integers for it. So:
> >
> > # let m = IntMap.empty;;
>
> Just do:
>
> # let m : int IntMap.t = IntMap.empty;;
>
> The " : int IntMap.t" type annotation restricts the default "'a
> IntMap.t" of IntMap.empty.
>
> > # let m = IntMap.add 1 "42" m;;
>
> Then:
>
> # let m = IntMap.add 1 "42" m;;
> Characters 26-27:
> let m = IntMap.add 1 "42" m;;
> ^
> This expression has type int IntMap.t but is here used with type
> string IntMap.t
>
>
> Best regards,
> david
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> 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: 2402 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Forcing a Map type: setting the value type
2011-05-02 15:15 ` Gabriel Scherer
@ 2011-05-02 15:33 ` Hugo Ferreira
0 siblings, 0 replies; 5+ messages in thread
From: Hugo Ferreira @ 2011-05-02 15:33 UTC (permalink / raw)
To: Gabriel Scherer; +Cc: David MENTRE, Çagdas Bozman, caml-list users
David and Gabriel,
On 05/02/2011 04:15 PM, Gabriel Scherer wrote:
> My basic idea was to re-export Map using a constrained type. With David
> Mentre's insight that only map-producing functions need to be coerced, I
> have the following proposal:
>
> module type MonoMap = sig
> include Map.S
> type elt
> val empty : elt t
> val singleton : key -> elt -> elt t
> val of_enum : (key * elt) Enum.t -> elt t
> end
>
> module IntMap
> : MonoMap with type key = int with type elt = int
> = struct
> include Map.Make(Int)
> type elt = int
> end
>
Thank you,
Hugo F.
> On Mon, May 2, 2011 at 4:46 PM, David MENTRE <dmentre@linux-france.org
> <mailto:dmentre@linux-france.org>> wrote:
>
> Hello Hugo,
>
> 2011/5/2 Hugo Ferreira <hmf@inescporto.pt <mailto:hmf@inescporto.pt>>:
> > Maybe I did not express myself correctly.
> > The above would also work with any other type for the
> > parameter 'a. I want only integers for it. So:
> >
> > # let m = IntMap.empty;;
>
> Just do:
>
> # let m : int IntMap.t = IntMap.empty;;
>
> The " : int IntMap.t" type annotation restricts the default "'a
> IntMap.t" of IntMap.empty.
>
> > # let m = IntMap.add 1 "42" m;;
>
> Then:
>
> # let m = IntMap.add 1 "42" m;;
> Characters 26-27:
> let m = IntMap.add 1 "42" m;;
> ^
> This expression has type int IntMap.t but is here used with type
> string IntMap.t
>
>
> Best regards,
> david
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2011-05-02 15:33 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-02 13:49 [Caml-list] Forcing a Map type: setting the value type Hugo Ferreira
[not found] ` <BANLkTikn+xxaBeBC0nW0=XFibbcwbC-VFQ@mail.gmail.com>
2011-05-02 14:18 ` Hugo Ferreira
2011-05-02 14:46 ` David MENTRE
2011-05-02 15:15 ` Gabriel Scherer
2011-05-02 15:33 ` Hugo Ferreira
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox