Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Optional arguments "between" non-optional ones
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Optional arguments "between" non-optional ones
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