On 2010/09/24, at 0:05, Adrien wrote:
> On 23/09/2010, bluestorm <bluestorm.d...@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

_______________________________________________
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

Reply via email to