* [Caml-list] Labelled parameter bug?
@ 2011-08-24 14:49 Dmitry Bely
2011-08-24 15:01 ` Philippe Veber
0 siblings, 1 reply; 4+ messages in thread
From: Dmitry Bely @ 2011-08-24 14:49 UTC (permalink / raw)
To: Caml List
The following fragment compiles without a warning but produces strange results:
let f ?(p1="p1") ~p2 p3 =
Printf.printf "p1=%s, p2=%s, p3=%s\n" p1 p2 p3
let _ =
f "p2" "p3"; (* 1 *)
let f2 = f "p2" in
f2 "p3" (* 2 *)
Outputs:
p1=p1, p2=p2, p3=p3 (1)
p1=p1, p2=p3, p3=p2 (2)
Why (1) and (2) are different? I assume f "p2" takes p3 instead of p2
but then the compiler should issue at least a warning...
- Dmitry Bely
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Labelled parameter bug?
2011-08-24 14:49 [Caml-list] Labelled parameter bug? Dmitry Bely
@ 2011-08-24 15:01 ` Philippe Veber
2011-08-26 9:20 ` Dmitry Bely
0 siblings, 1 reply; 4+ messages in thread
From: Philippe Veber @ 2011-08-24 15:01 UTC (permalink / raw)
To: Dmitry Bely; +Cc: Caml List
[-- Attachment #1: Type: text/plain, Size: 1672 bytes --]
Hello,
2011/8/24 Dmitry Bely <dmitry.bely@gmail.com>
> The following fragment compiles without a warning but produces strange
> results:
>
> let f ?(p1="p1") ~p2 p3 =
> Printf.printf "p1=%s, p2=%s, p3=%s\n" p1 p2 p3
>
> let _ =
> f "p2" "p3"; (* 1 *)
> let f2 = f "p2" in
> f2 "p3" (* 2 *)
>
The type of f is
val f : ?p1:string -> p2:string -> string -> unit = <fun>
so f "p2" applies to the only anonymous parameter (third one) because it
cannot be applied to the first or second without label :
# f "p2";;
- : p2:string -> unit = <fun>
This first application also applies optional arguments situated before the
anoymous argument, so it remains the second (labeled) argument only.
There is indeed a special case where you can drop labels if you provide the
exact number of arguments. This means that f "p2" "p3" is equivalent to f
~p2:"p2" "p3". This is written in the manual (
http://caml.inria.fr/pub/docs/manual-ocaml/manual006.html) :
"As an exception to the above parameter matching rules, if an application is
total, labels may be omitted. In practice, most applications are total, so
that labels can be omitted in applications. "
So this is actually the intended behavior, AFAIU
Philippe.
>
> Outputs:
>
> p1=p1, p2=p2, p3=p3 (1)
> p1=p1, p2=p3, p3=p2 (2)
>
> Why (1) and (2) are different? I assume f "p2" takes p3 instead of p2
> but then the compiler should issue at least a warning...
>
> - Dmitry Bely
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
[-- Attachment #2: Type: text/html, Size: 2767 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Labelled parameter bug?
2011-08-24 15:01 ` Philippe Veber
@ 2011-08-26 9:20 ` Dmitry Bely
2011-08-26 9:58 ` Jacques Garrigue
0 siblings, 1 reply; 4+ messages in thread
From: Dmitry Bely @ 2011-08-26 9:20 UTC (permalink / raw)
To: Philippe Veber; +Cc: Caml List
On Wed, Aug 24, 2011 at 7:01 PM, Philippe Veber
<philippe.veber@gmail.com> wrote:
>> The following fragment compiles without a warning but produces strange
>> results:
>>
>> let f ?(p1="p1") ~p2 p3 =
>> Printf.printf "p1=%s, p2=%s, p3=%s\n" p1 p2 p3
>>
>> let _ =
>> f "p2" "p3"; (* 1 *)
>> let f2 = f "p2" in
>> f2 "p3" (* 2 *)
(...)
> This first application also applies optional arguments situated before the
> anoymous argument, so it remains the second (labeled) argument only.
>
> There is indeed a special case where you can drop labels if you provide the
> exact number of arguments. This means that f "p2" "p3" is equivalent to f
> ~p2:"p2" "p3". This is written in the manual
> (http://caml.inria.fr/pub/docs/manual-ocaml/manual006.html) :
>
> "As an exception to the above parameter matching rules, if an application is
> total, labels may be omitted. In practice, most applications are total, so
> that labels can be omitted in applications. "
>
> So this is actually the intended behavior, AFAIU
Indeed. Thanks for the detailed explanation. But shouldn't the
compiler decide that the partial application skipping labeled
parameter is suspicious and generate a warning?
- Dmitry Bely
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Labelled parameter bug?
2011-08-26 9:20 ` Dmitry Bely
@ 2011-08-26 9:58 ` Jacques Garrigue
0 siblings, 0 replies; 4+ messages in thread
From: Jacques Garrigue @ 2011-08-26 9:58 UTC (permalink / raw)
To: Dmitry Bely; +Cc: Caml List
On 2011/08/26, at 18:20, Dmitry Bely wrote:
> On Wed, Aug 24, 2011 at 7:01 PM, Philippe Veber
> <philippe.veber@gmail.com> wrote:
>
>>> The following fragment compiles without a warning but produces strange
>>> results:
>>>
>>> let f ?(p1="p1") ~p2 p3 =
>>> Printf.printf "p1=%s, p2=%s, p3=%s\n" p1 p2 p3
>>>
>>> let _ =
>>> f "p2" "p3"; (* 1 *)
>>> let f2 = f "p2" in
>>> f2 "p3" (* 2 *)
>
> (...)
>
>> This first application also applies optional arguments situated before the
>> anoymous argument, so it remains the second (labeled) argument only.
>>
>> There is indeed a special case where you can drop labels if you provide the
>> exact number of arguments. This means that f "p2" "p3" is equivalent to f
>> ~p2:"p2" "p3". This is written in the manual
>> (http://caml.inria.fr/pub/docs/manual-ocaml/manual006.html) :
>>
>> "As an exception to the above parameter matching rules, if an application is
>> total, labels may be omitted. In practice, most applications are total, so
>> that labels can be omitted in applications. "
>>
>> So this is actually the intended behavior, AFAIU
>
> Indeed. Thanks for the detailed explanation. But shouldn't the
> compiler decide that the partial application skipping labeled
> parameter is suspicious and generate a warning?
The golden rule here is no ambiguity.
Since this function accepts only two non-optional arguments (as the return type is unit),
if you pass them all at once without labels then this is accepted as a valid full application,
and cannot be confused with another interpretation.
Note that if you don't like this behaviour, you can manually activate warning 6 (Label omitted in function application)
which requires you to write all labels.
Jacques Garrigue
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2011-08-26 9:58 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-24 14:49 [Caml-list] Labelled parameter bug? Dmitry Bely
2011-08-24 15:01 ` Philippe Veber
2011-08-26 9:20 ` Dmitry Bely
2011-08-26 9:58 ` Jacques Garrigue
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox