Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Andrej Bauer <Andrej.Bauer@fmf.uni-lj.si>
To: Keiko Nakata <keiko@kurims.kyoto-u.ac.jp>, Caml <caml-list@inria.fr>
Subject: Re: [Caml-list] a function for polymorphic and monomorphic objects
Date: Thu, 08 Nov 2007 11:48:27 +0100	[thread overview]
Message-ID: <4732E97B.9080805@fmf.uni-lj.si> (raw)
In-Reply-To: <20071108.180532.68538077.keiko@kurims.kyoto-u.ac.jp>

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


  reply	other threads:[~2007-11-08 10:44 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 ` Andrej Bauer [this message]
2007-11-08 12:09   ` [Caml-list] " Keiko Nakata
2007-11-09  1:35     ` Peng Zang
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=4732E97B.9080805@fmf.uni-lj.si \
    --to=andrej.bauer@fmf.uni-lj.si \
    --cc=Andrej.Bauer@andrej.com \
    --cc=caml-list@inria.fr \
    --cc=keiko@kurims.kyoto-u.ac.jp \
    /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