caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: Adrien <camaradetux@gmail.com>
Cc: bluestorm <bluestorm.dylc@gmail.com>,
	Caml Mailing List <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] Optional arguments "between" non-optional ones
Date: Fri, 24 Sep 2010 10:32:44 +0900	[thread overview]
Message-ID: <00FF2AA5-C0EF-4835-BCB4-326DA78B8665@gmail.com> (raw)
In-Reply-To: <AANLkTimzf5ossL=eELdVk+q=Sckg0QfYaGdtK2uCR8Zr@mail.gmail.com>

On 2010/09/24, at 0:05, Adrien wrote:
> On 23/09/2010, bluestorm <bluestorm.dylc@gmail.com> wrote:
>> (sorry for any double-posting)
>> 
>> The problem is that in your declaration of h, the inferred type for f
>> is of the form (unit -> unit -> ...), and you use it with the
>> different type (unit -> ?a:'a -> unit -> ...).
>> 
>> Changing ?a to be the first parameter of f change f's type to (?a:'a
>> -> unit -> unit -> ...). OCaml knows that it can implicitly coerce
>> functions when the optional parameters appear in the first position.
> 
> I actually expected types (?a:'a -> unit -> unit -> ...) and (unit ->
> ?a:'a -> unit -> ...) to work alike. I'm wondering why the arguments
> are "dropped" from the first case but not the second.

The answer is that this way of coercing functions by applying optional
arguments when they are passed is a kind of hack. The formal semantics
defines optional argument discarding as happening only when a function
is applied. However, one often wants this discarding to occur when
the function is passed as argument, to avoid manual eta-expansion.
This is actually done through an eta-expansion (i.e., (fun x -> f x) gets
passed to the function instead of f, to be sure that side-effects happen at
the right time). Since only cases with a single eta-expansion are considered,
optional arguments should come first.

Of course we are very careful of not breaking the formal semantics
(where there is no eta-expansion, and discarding will only occur when
the function is applied). Whenever the eta-expansion approach might
introduce a discrepancy, you will get a type error.

Jacques Garrigue


      reply	other threads:[~2010-09-24  1:32 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-23 13:20 Adrien
2010-09-23 14:32 ` bluestorm
2010-09-23 15:05   ` Adrien
2010-09-24  1:32     ` Jacques Garrigue [this message]

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=00FF2AA5-C0EF-4835-BCB4-326DA78B8665@gmail.com \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=bluestorm.dylc@gmail.com \
    --cc=camaradetux@gmail.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).