caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* is this a bug ?
@ 2006-05-09 13:09 Christophe Raffalli
  2006-05-09 13:16 ` [Caml-list] " Remi Vanicat
                   ` (5 more replies)
  0 siblings, 6 replies; 12+ messages in thread
From: Christophe Raffalli @ 2006-05-09 13:09 UTC (permalink / raw)
  To: caml-list


hello,

--------------------------------
let f b l = match l with
   [] | [_] -> 1
| [a;_] | [_;a] when a = b-> 2
| _ -> 3

let _ =
   print_int (f 1 [1;2]);
   print_int (f 1 [2;1]);
   print_newline ()
--------------------------------

Do you think this code should have a useless pattern warning and print "23" instead of "22" ?

If someone tell me this is a bug then I fill a bug report ...


-- 
Christophe Raffalli
Université de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex

tél: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
---------------------------------------------
IMPORTANT: this mail is signed using PGP/MIME
At least Enigmail/Mozilla, mutt or evolution
can check this signature. The public key is
stored on www.keyserver.net
---------------------------------------------


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] is this a bug ?
  2006-05-09 13:09 is this a bug ? Christophe Raffalli
@ 2006-05-09 13:16 ` Remi Vanicat
  2006-05-09 13:19 ` Samuel Mimram
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 12+ messages in thread
From: Remi Vanicat @ 2006-05-09 13:16 UTC (permalink / raw)
  To: Christophe Raffalli; +Cc: caml-list

2006/5/9, Christophe Raffalli <christophe.raffalli@univ-savoie.fr>:
>
> hello,
>
> --------------------------------
> let f b l = match l with
>    [] | [_] -> 1
> | [a;_] | [_;a] when a = b-> 2
> | _ -> 3
>
> let _ =
>    print_int (f 1 [1;2]);
>    print_int (f 1 [2;1]);
>    print_newline ()
> --------------------------------
>
> Do you think this code should have a useless pattern warning ?

Yes. When you wrote a pattern as :  '| [a;_] | [_;a] when a = b' it
first match the pattern '| [a;_] | [_;a]' then try the condition. So
it will match [a;_] and never [_;a]. You have to wrote it as :
| [a;c] when a = b or c = b -> 2


> and print "23" instead of "22" ?

It is the same problem there. Only the first patttern to match is matched.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] is this a bug ?
  2006-05-09 13:09 is this a bug ? Christophe Raffalli
  2006-05-09 13:16 ` [Caml-list] " Remi Vanicat
@ 2006-05-09 13:19 ` Samuel Mimram
  2006-05-09 13:26   ` Christophe Raffalli
  2006-05-09 13:30   ` Yoann Padioleau
  2006-05-09 13:22 ` Christophe Raffalli
                   ` (3 subsequent siblings)
  5 siblings, 2 replies; 12+ messages in thread
From: Samuel Mimram @ 2006-05-09 13:19 UTC (permalink / raw)
  To: Christophe Raffalli; +Cc: caml-list

Hi,

Christophe Raffalli wrote:
> --------------------------------
> let f b l = match l with
>   [] | [_] -> 1
> | [a;_] | [_;a] when a = b-> 2
> | _ -> 3
> 
> let _ =
>   print_int (f 1 [1;2]);
>   print_int (f 1 [2;1]);
>   print_newline ()
> --------------------------------
> 
> Do you think this code should have a useless pattern warning and print
> "23" instead of "22" ?
> 
> If someone tell me this is a bug then I fill a bug report ...

I guess that the priorities says that you should "read" the third line as

| [a;_] | ( [_;a] when a = b ) -> 2

This would justify the warning and explain the behaviour...

Cheers,

Samuel.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] is this a bug ?
  2006-05-09 13:09 is this a bug ? Christophe Raffalli
  2006-05-09 13:16 ` [Caml-list] " Remi Vanicat
  2006-05-09 13:19 ` Samuel Mimram
@ 2006-05-09 13:22 ` Christophe Raffalli
  2006-05-09 13:33 ` Luc Maranget
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 12+ messages in thread
From: Christophe Raffalli @ 2006-05-09 13:22 UTC (permalink / raw)
  To: Christophe Raffalli; +Cc: caml-list

2006/5/9, Christophe Raffalli <christophe.raffalli@univ-savoie.fr>:
>
> hello,
>
> --------------------------------
> let f b l = match l with
>    [] | [_] -> 1
> | [a;_] | [_;a] when a = b-> 2
> | _ -> 3
>
> let _ =
>    print_int (f 1 [1;2]);
>    print_int (f 1 [2;1]);
>    print_newline ()
> --------------------------------
>
> Do you think this code should have a useless pattern warning ?

Yes. When you wrote a pattern as :  '| [a;_] | [_;a] when a = b' it
first match the pattern '| [a;_] | [_;a]' then try the condition. So
it will match [a;_] and never [_;a]. You have to wrote it as :
| [a;c] when a = b or c = b -> 2


> and print "23" instead of "22" ?

It is the same problem there. Only the first patttern to match is matched.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] is this a bug ?
  2006-05-09 13:19 ` Samuel Mimram
@ 2006-05-09 13:26   ` Christophe Raffalli
  2006-05-09 13:33     ` Samuel Mimram
  2006-05-09 13:30   ` Yoann Padioleau
  1 sibling, 1 reply; 12+ messages in thread
From: Christophe Raffalli @ 2006-05-09 13:26 UTC (permalink / raw)
  To: Samuel Mimram; +Cc: caml-list

Samuel Mimram a écrit :
> Hi,
> 
> Christophe Raffalli wrote:
> 
>>--------------------------------
>>let f b l = match l with
>>  [] | [_] -> 1
>>| [a;_] | [_;a] when a = b-> 2
>>| _ -> 3
>>
>>let _ =
>>  print_int (f 1 [1;2]);
>>  print_int (f 1 [2;1]);
>>  print_newline ()
>>--------------------------------
>>
>>Do you think this code should have a useless pattern warning and print
>>"23" instead of "22" ?
>>
>>If someone tell me this is a bug then I fill a bug report ...
> 
> 
> I guess that the priorities says that you should "read" the third line as
> 
> | [a;_] | ( [_;a] when a = b ) -> 2
> 

but the you should be allowed to write

  | ([a;_] when a = b) | ( [_;a] when a = b ) -> 2

but this results in a syntax error ...

> This would justify the warning and explain the behaviour...
> 
> Cheers,
> 
> Samuel.

-- 
Christophe Raffalli
Université de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex

tél: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
---------------------------------------------
IMPORTANT: this mail is signed using PGP/MIME
At least Enigmail/Mozilla, mutt or evolution
can check this signature. The public key is
stored on www.keyserver.net
---------------------------------------------


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] is this a bug ?
  2006-05-09 13:19 ` Samuel Mimram
  2006-05-09 13:26   ` Christophe Raffalli
@ 2006-05-09 13:30   ` Yoann Padioleau
  1 sibling, 0 replies; 12+ messages in thread
From: Yoann Padioleau @ 2006-05-09 13:30 UTC (permalink / raw)
  To: Samuel Mimram; +Cc: Christophe Raffalli, caml-list

Samuel Mimram <samuel.mimram@ens-lyon.org> writes:

> Hi,
>
> Christophe Raffalli wrote:
>> --------------------------------
>> let f b l = match l with
>>   [] | [_] -> 1
>> | [a;_] | [_;a] when a = b-> 2
>> | _ -> 3
>> 
>> let _ =
>>   print_int (f 1 [1;2]);
>>   print_int (f 1 [2;1]);
>>   print_newline ()
>> --------------------------------
>> 
>> Do you think this code should have a useless pattern warning and print
>> "23" instead of "22" ?
>> 
>> If someone tell me this is a bug then I fill a bug report ...
>
> I guess that the priorities says that you should "read" the third line as
>
> | [a;_] | ( [_;a] when a = b ) -> 2

I am not sure because because  | ([a;_] | [_;a]) when a = b    -> 2
issues the same warning.


I think | [a;_] | [_;a] when a = b-> 2    is  parsed  as 

| ( [a;_] | [_;a])  when a = b -> 2

but that the semantic is first to look at the pattern, 
and if one is found, then look at the condition, which is too late.

'when' does not "distribute" over Or-patterns :( 



let f b l = match l with
   [] | [_] -> 1
| [a;_] when a = b-> 2
| [_;a] when a = b-> 2
| _ -> 3

works.

>
> This would justify the warning and explain the behaviour...
>
> Cheers,
>
> Samuel.
>
> _______________________________________________
> 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
>
>

-- 
pad


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] is this a bug ?
  2006-05-09 13:09 is this a bug ? Christophe Raffalli
                   ` (2 preceding siblings ...)
  2006-05-09 13:22 ` Christophe Raffalli
@ 2006-05-09 13:33 ` Luc Maranget
  2006-05-09 13:47 ` Arne Ehrlich
  2006-05-09 13:47 ` Alain Frisch
  5 siblings, 0 replies; 12+ messages in thread
From: Luc Maranget @ 2006-05-09 13:33 UTC (permalink / raw)
  To: Christophe Raffalli; +Cc: caml-list

> 
> hello,
> 
> --------------------------------
> let f b l = match l with
>   [] | [_] -> 1
> | [a;_] | [_;a] when a = b-> 2
            ^^^^
> | _ -> 3
Warning U: this pattern is unused.

> 
> let _ =
>   print_int (f 1 [1;2]);
>   print_int (f 1 [2;1]);
>   print_newline ()
> --------------------------------
23


> 
> Do you think this code should have a useless pattern warning and print "23" 
> instead of "22" ?
> 
> If someone tell me this is a bug then I fill a bug report ...
> 


You may object the semantics, but it is consitent w.r.t. both
documentation (deterministic left-to-right matching of or-patterns,
semantics of when as match first, then test)
and supplied warning (which you omit).

So I am tempted to say not a bug.

However, there is indeed a difficulty, if we expand or-pat as:

let f b l = match l with
| [] -> 1
| [_] -> 1
| [a;_] when a = b -> 2
| [_;a] when a = b -> 2
| _ -> 3

Then, we have the other behavior (no warning, 22).

Oups, another oddity of 'when...'

--Luc


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] is this a bug ?
  2006-05-09 13:26   ` Christophe Raffalli
@ 2006-05-09 13:33     ` Samuel Mimram
  0 siblings, 0 replies; 12+ messages in thread
From: Samuel Mimram @ 2006-05-09 13:33 UTC (permalink / raw)
  To: Christophe Raffalli; +Cc: caml-list

Christophe Raffalli wrote:
> Samuel Mimram a écrit :
>> Christophe Raffalli wrote:
>>
>>> --------------------------------
>>> let f b l = match l with
>>>  [] | [_] -> 1
>>> | [a;_] | [_;a] when a = b-> 2
>>> | _ -> 3
>>>
>>> let _ =
>>>  print_int (f 1 [1;2]);
>>>  print_int (f 1 [2;1]);
>>>  print_newline ()
>>> --------------------------------
>>>
>>> Do you think this code should have a useless pattern warning and print
>>> "23" instead of "22" ?
>>>
>>> If someone tell me this is a bug then I fill a bug report ...
>>
>> I guess that the priorities says that you should "read" the third line as
>>
>> | [a;_] | ( [_;a] when a = b ) -> 2
> 
> but the you should be allowed to write
> 
>  | ([a;_] when a = b) | ( [_;a] when a = b ) -> 2
> 
> but this results in a syntax error ...

Yes, in fact I answered too quickly. The answers would have been 22 in
both cases. Moreover,

let f x = match x with
  | z | y when y = 0 -> 0
  | _ -> 1
;;

results in "Variable y must occur on both sides of this | pattern"...

Samuel.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] is this a bug ?
  2006-05-09 13:09 is this a bug ? Christophe Raffalli
                   ` (3 preceding siblings ...)
  2006-05-09 13:33 ` Luc Maranget
@ 2006-05-09 13:47 ` Arne Ehrlich
  2006-05-09 18:24   ` Jonathan Roewen
  2006-05-09 13:47 ` Alain Frisch
  5 siblings, 1 reply; 12+ messages in thread
From: Arne Ehrlich @ 2006-05-09 13:47 UTC (permalink / raw)
  To: caml-list

Am Dienstag, 9. Mai 2006 15:09 schrieb Christophe Raffalli:
> hello,
>
> --------------------------------
> let f b l = match l with
>    [] | [_] -> 1
>
> | [a;_] | [_;a] when a = b-> 2
> | _ -> 3
>
> let _ =
>    print_int (f 1 [1;2]);
>    print_int (f 1 [2;1]);
>    print_newline ()
> --------------------------------
>
> Do you think this code should have a useless pattern warning and print "23"
> instead of "22" ?
You found an semantic problem in the gramar ^^
or-patterns do not play well with "when"

I think it's a bug in the semantics... 


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] is this a bug ?
  2006-05-09 13:09 is this a bug ? Christophe Raffalli
                   ` (4 preceding siblings ...)
  2006-05-09 13:47 ` Arne Ehrlich
@ 2006-05-09 13:47 ` Alain Frisch
  5 siblings, 0 replies; 12+ messages in thread
From: Alain Frisch @ 2006-05-09 13:47 UTC (permalink / raw)
  To: Christophe Raffalli; +Cc: caml-list

Christophe Raffalli wrote:
> 
> hello,
> 
> --------------------------------
> let f b l = match l with
>   [] | [_] -> 1
> | [a;_] | [_;a] when a = b-> 2
> | _ -> 3
> 
> let _ =
>   print_int (f 1 [1;2]);
>   print_int (f 1 [2;1]);
>   print_newline ()
> --------------------------------
> 
> Do you think this code should have a useless pattern warning and print
> "23" instead of "22" ?

Well, both happens, right?  I don't see any problem.

The pattern [_;a] is useless because [a;_] matches the same values and
the first match (left-to-right) policy is specificed
(http://caml.inria.fr/pub/docs/manual-ocaml/manual014.html).

The guard is checked after the binding, and there is no backtracking.
This explains why the function returns 3 when b=1 l=[2;1].

-- Alain


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] is this a bug ?
  2006-05-09 13:47 ` Arne Ehrlich
@ 2006-05-09 18:24   ` Jonathan Roewen
  0 siblings, 0 replies; 12+ messages in thread
From: Jonathan Roewen @ 2006-05-09 18:24 UTC (permalink / raw)
  To: Arne Ehrlich; +Cc: caml-list

> You found an semantic problem in the gramar ^^
> or-patterns do not play well with "when"
>
> I think it's a bug in the semantics...

I don't believe so. It depends on the type. In fact, it works very
well with sum types (I did a very small inference engine which used or
patterns with when to great effect).

The problem is the two match clauses are not different. They look
different for the fact that the variables are bound in a different
order -- which is why the warning occurs. For matching, variable
naming has no semantic impact, hence the confusion.

Jonathan


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Is this a bug?
@ 2007-05-01 12:59 Thomas Fischbacher
  0 siblings, 0 replies; 12+ messages in thread
From: Thomas Fischbacher @ 2007-05-01 12:59 UTC (permalink / raw)
  To: caml-list


If we look at the following piece of code:

===>
type dof_region_spec = (string * bool * (int option array)) array;;
type opt_field_restriction = dof_region_spec option;;

let make_field ?(name="Field") ?restriction ?constant_value mwe =
   let restriction:opt_field_restriction = restriction in
	failwith "FOO";;
<===

...then make_field types as:

===>
val make_field :
   ?name:string -> ?restriction:<hidden> -> ?constant_value:'a -> 'b -> 'c
<===

The <hidden> actually should not show up here. What is going on?

-- 
best regards,
Thomas Fischbacher
tf@functionality.de


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2007-05-01 12:59 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-09 13:09 is this a bug ? Christophe Raffalli
2006-05-09 13:16 ` [Caml-list] " Remi Vanicat
2006-05-09 13:19 ` Samuel Mimram
2006-05-09 13:26   ` Christophe Raffalli
2006-05-09 13:33     ` Samuel Mimram
2006-05-09 13:30   ` Yoann Padioleau
2006-05-09 13:22 ` Christophe Raffalli
2006-05-09 13:33 ` Luc Maranget
2006-05-09 13:47 ` Arne Ehrlich
2006-05-09 18:24   ` Jonathan Roewen
2006-05-09 13:47 ` Alain Frisch
2007-05-01 12:59 Is this a bug? Thomas Fischbacher

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).