I agree it is very confusing...


On 10/07/2013 15:48, Ivan Gotovchits wrote:
Please, can someone explain the reason behind the following behaviour:


# let f ~a = a;;
val f : a:'a -> 'a = <fun>
You are defining 'f', a function that waits for a labeled argument called 'a' and that will send back itself.

if I apply function f, omiting the label, instead of an error I'll get:

# f 12;;
- : a:(int -> 'a) -> 'a = <fun>
yes, very confusing. 'f 12' is still not applied (see at the end, it is maybe a problem in ocaml). It is still waiting for the label '~a'. once the label is given, 'f ~a' will be able to give back the result, and then apply it to '12'. So the labelled argument should be a function from an int to something, in order to be applied to '12'. That is what you are doing next :

... a function that accepts a labeled arguments, that is a function from int
to 'a, and returns a result of this function:

# f 12 ~a:(fun x -> x + 1);;
- : int = 13

What is confusing is that f is applied to ~a, not to 12 directly (because of labels properties).
f ~a => (fun x -> x+1)
then
(fun x -> x +1) 12 => 13


and even more, if I apply it to more unlabled arguments:

# f 1 2 3 4 5;;
- : a:(int -> int -> int -> int -> int -> 'a) -> 'a = <fun>
This is same behaviour : waiting for a labelled argument that will be able to deal with 5 integers.

It is very confusing...


I agree it is confusing. And there is something strange :

The doc says :

"As an exception to the above parameter matching rules, if an application is total (omitting all optional arguments), labels may be omitted. In practice, many applications are total, so that labels can often be omitted."

# let f ~x ~y = x - y;;
val f : x:int -> y:int -> int = <fun>
# f 3 2;;
- : int = 1

But in your case, when you do "f 12", the application is total, and thus it should be applied and give '12'. But it is not applied. This is an inconsistency in the documentation.

William