caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Question on polymorphic typing for curried functions
Date: Fri, 24 Aug 2007 21:25:00 +0100	[thread overview]
Message-ID: <200708242125.00613.jon@ffconsultancy.com> (raw)
In-Reply-To: <46CF34DE.301@cs.umn.edu>

On Friday 24 August 2007 20:43:26 Christopher Kauffman wrote:
> I am looking for a bit of information on the behavior of curried functions
> wrt polymorphic arguments. For instance, in the following example, using a
> curried function seems to lose the nice polymorphism that I desire.
>
> # let genf f a b = f a b;;
> val genf : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c = <fun>
> # let specf = genf (<);;
> val specf : '_a -> '_a -> bool = <fun>
> # specf 1 2;;
> - : bool = true
> # specf;;
> - : int -> int -> bool = <fun>
>
> An alternative definition for the specific 'specf' maintains polymorphism
> of its arguments.
>
> # let specf a b = genf (<) a b;;
> val specf : 'a -> 'a -> bool = <fun>
> # specf 1 2;;
> - : bool = true
> # specf 1.0 2.0;;
> - : bool = true
> # specf;;
> - : 'a -> 'a -> bool = <fun>
>
> Is there a set of rules or guidelines that determine when argument types
> are specialized versus staying polymorphic?

Yes. If you partially apply then you get a monomorphic result:

# genf (<);;
- : '_a -> '_a -> bool = <fun>

If you then wrap that in any kind of closure then it becomes polymorphic 
again:

# (fun a -> genf (<));;
- : 'a -> 'b -> 'b -> bool = <fun>

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists/?e


  reply	other threads:[~2007-08-24 20:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-24 19:43 Christopher Kauffman
2007-08-24 20:25 ` Jon Harrop [this message]
2007-08-24 21:04 ` [Caml-list] " Julien Moutinho
2007-08-24 23:32 ` Harrison, John R

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=200708242125.00613.jon@ffconsultancy.com \
    --to=jon@ffconsultancy.com \
    --cc=caml-list@yquem.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;
as well as URLs for NNTP newsgroup(s).