* a function for polymorphic and monomorphic objects
@ 2007-11-08 9:05 Keiko Nakata
2007-11-08 10:48 ` [Caml-list] " Andrej Bauer
2007-11-09 2:50 ` Jacques Garrigue
0 siblings, 2 replies; 5+ messages in thread
From: Keiko Nakata @ 2007-11-08 9:05 UTC (permalink / raw)
To: caml-list
Hello.
Can I define a function taking as argument
an object with a method, say, "map" whose type is instantiatable to
(int -> int) -> int list?
I have two object;
one with a polymorphic map, the other with a monomorphic map.
let p = object
method map : 'a.(int -> 'a) -> 'a list = fun f -> List.map f [1;2;3]
end;;
let q = object
method map : (int -> int) -> int list = fun f -> List.map f [1;2;3]
end;;
I want to define a function like double below,
which is applicable to both p and q.
let double o = o#map (fun x -> x*2);;
The only workaround I came up with is
let double2 f = f (fun x -> x*2);;
double2 p#map;;
double2 q#map;;
Yet this is not very nice....
With best regards,
Keiko
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] a function for polymorphic and monomorphic objects
2007-11-08 9:05 a function for polymorphic and monomorphic objects Keiko Nakata
@ 2007-11-08 10:48 ` Andrej Bauer
2007-11-08 12:09 ` Keiko Nakata
2007-11-09 2:50 ` Jacques Garrigue
1 sibling, 1 reply; 5+ messages in thread
From: Andrej Bauer @ 2007-11-08 10:48 UTC (permalink / raw)
To: Keiko Nakata, Caml
Keiko Nakata wrote:
> Can I define a function taking as argument
> an object with a method, say, "map" whose type is instantiatable to
> (int -> int) -> int list?
Don't write too many type annotations by hand and everything will be
fine. In your case you got it wrong when you quantified 'a in the object p.
Objective Caml version 3.09.3
# let p = object method map = fun f -> List.map f [1;2;3] end;;
val p : < map : (int -> 'a) -> 'a list > = <obj>
# let q = object method map = fun (f : int -> int) -> List.map f [1;2;3]
end;;
val q : < map : (int -> int) -> int list > = <obj>
# let double o = o#map (fun x -> x*2);;
val double : < map : (int -> int) -> 'a; .. > -> 'a = <fun>
# double p;;
- : int list = [2; 4; 6]
# double q;;
- : int list = [2; 4; 6]
I think the general hint is this: first try without type annotations.
Then, if you're not happy (say because the function is "too
polymorphic") add as few type annotations as possible, e.g., annotate f
above rather than map.
Best regards,
Andrej
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] a function for polymorphic and monomorphic objects
2007-11-08 10:48 ` [Caml-list] " Andrej Bauer
@ 2007-11-08 12:09 ` Keiko Nakata
2007-11-09 1:35 ` Peng Zang
0 siblings, 1 reply; 5+ messages in thread
From: Keiko Nakata @ 2007-11-08 12:09 UTC (permalink / raw)
To: Andrej.Bauer; +Cc: caml-list
> In your case you got it wrong when you quantified 'a in the object p.
> # let p = object method map = fun f -> List.map f [1;2;3] end;;
> val p : < map : (int -> 'a) -> 'a list > = <obj>
Perhaps I oversimplified my example.
Indeed I want to make p a function like
let p1 l = object
method map : 'a.(int -> 'a) -> 'a list = fun f -> List.map f l
end;;
let o1 = p1 [1;2;3];;
- val o1 : < map : 'a. (int -> 'a) -> 'a list > = <obj>
Then I need type annotations
let p2 l = object
method map = fun f -> List.map f l
end;;
let o2 = p2 [1;2;3];;
- val o2 : < map : (int -> '_a) -> '_a list > = <obj>
With best,
keiko
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] a function for polymorphic and monomorphic objects
2007-11-08 12:09 ` Keiko Nakata
@ 2007-11-09 1:35 ` Peng Zang
0 siblings, 0 replies; 5+ messages in thread
From: Peng Zang @ 2007-11-09 1:35 UTC (permalink / raw)
To: caml-list
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
So my take is that since o2 is a completely different beast, ie. monomorphic,
it really is of a different type. And should not be confused with
polymorphic cousins. Functions that take them as an argument will
fundamentally have different types. Eg. one monomorphic, the other poly.
You want this distinction normally as it protects you from running o2 on a
(int -> int) and later run it on a (int -> string).
However I can see how especially when the monomorphic version is
still "unbound", ie. still _'a, one might want to write one function to deal
with both. I don't know of a nice way to do this as the type sigs are
fundamentally different. That said, one can always resort to black magic
when the type system gets in the way assuming you know what you're doing:
# let foo arg = (arg:<map : 'a. (int -> 'a) -> 'a list; ..>)#map
string_of_int;;
val foo : < map : 'a. (int -> 'a) -> 'a list; .. > -> string list = <fun>
# foo o1;;
- - : string list = ["1"; "2"; "3"]
# foo (Obj.magic o2);;
- - : string list = ["1"; "2"; "3"]
Peng
On Thursday 08 November 2007 07:09:39 am Keiko Nakata wrote:
> > In your case you got it wrong when you quantified 'a in the object p.
> >
> > # let p = object method map = fun f -> List.map f [1;2;3] end;;
> > val p : < map : (int -> 'a) -> 'a list > = <obj>
>
> Perhaps I oversimplified my example.
> Indeed I want to make p a function like
>
> let p1 l = object
> method map : 'a.(int -> 'a) -> 'a list = fun f -> List.map f l
> end;;
>
> let o1 = p1 [1;2;3];;
> - val o1 : < map : 'a. (int -> 'a) -> 'a list > = <obj>
>
> Then I need type annotations
>
> let p2 l = object
> method map = fun f -> List.map f l
> end;;
> let o2 = p2 [1;2;3];;
> - val o2 : < map : (int -> '_a) -> '_a list > = <obj>
>
>
> With best,
> keiko
>
> _______________________________________________
> 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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)
iD8DBQFHM7mHfIRcEFL/JewRAvCYAKCBDF0GoWWpeQTRtb/0ZTNWlCTBXgCg0HO/
VYPxEIQU88Fmctql0Cw910U=
=fcz+
-----END PGP SIGNATURE-----
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] a function for polymorphic and monomorphic objects
2007-11-08 9:05 a function for polymorphic and monomorphic objects Keiko Nakata
2007-11-08 10:48 ` [Caml-list] " Andrej Bauer
@ 2007-11-09 2:50 ` Jacques Garrigue
1 sibling, 0 replies; 5+ messages in thread
From: Jacques Garrigue @ 2007-11-09 2:50 UTC (permalink / raw)
To: keiko; +Cc: caml-list
From: Keiko Nakata <keiko@kurims.kyoto-u.ac.jp>
> Can I define a function taking as argument
> an object with a method, say, "map" whose type is instantiatable to
> (int -> int) -> int list?
>
> I have two object;
> one with a polymorphic map, the other with a monomorphic map.
>
> let p = object
> method map : 'a.(int -> 'a) -> 'a list = fun f -> List.map f [1;2;3]
> end;;
>
> let q = object
> method map : (int -> int) -> int list = fun f -> List.map f [1;2;3]
> end;;
>
> I want to define a function like double below,
> which is applicable to both p and q.
>
> let double o = o#map (fun x -> x*2);;
>
> The only workaround I came up with is
>
> let double2 f = f (fun x -> x*2);;
> double2 p#map;;
> double2 q#map;;
>
> Yet this is not very nice....
Currently, this is the only workaround available.
A possibility once considered was to allow subtyping between the type
of p and the type of q. This way you could use your function double,
after applying a coercion for p.
However this is not so easy technically (mixing subtyping and
polymorphism...), and there has never been much demand for such
subtyping. Indeed, if one is required to do a (double) coercion
anyway, then double2 is not much worse (actually it is even shorter
here...)
Cheers,
Jacques Garrigue
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2007-11-09 2:50 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-08 9:05 a function for polymorphic and monomorphic objects Keiko Nakata
2007-11-08 10:48 ` [Caml-list] " Andrej Bauer
2007-11-08 12:09 ` Keiko Nakata
2007-11-09 1:35 ` Peng Zang
2007-11-09 2:50 ` Jacques Garrigue
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox