From: Peng Zang <peng.zang@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] a function for polymorphic and monomorphic objects
Date: Thu, 8 Nov 2007 20:35:57 -0500 [thread overview]
Message-ID: <200711082036.07469.peng.zang@gmail.com> (raw)
In-Reply-To: <20071108.210939.68538729.keiko@kurims.kyoto-u.ac.jp>
-----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-----
next prev parent reply other threads:[~2007-11-09 1:36 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-11-08 9:05 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 [this message]
2007-11-09 2:50 ` Jacques Garrigue
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=200711082036.07469.peng.zang@gmail.com \
--to=peng.zang@gmail.com \
--cc=caml-list@inria.fr \
/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