From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 01BBFBBAF for ; Fri, 26 Nov 2010 23:32:52 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AusBALHE70zRVdg0kGdsb2JhbACUW4YpAYgDCBUBAQEBCQkMBxEDH4gsnViJZIIYhHEuiFYBAQMFhUIEgVyJBYkt X-IronPort-AV: E=Sophos;i="4.59,263,1288566000"; d="scan'208";a="81065158" Received: from mail-qw0-f52.google.com ([209.85.216.52]) by mail2-smtp-roc.national.inria.fr with ESMTP; 26 Nov 2010 23:32:50 +0100 Received: by qwf7 with SMTP id 7so1938648qwf.39 for ; Fri, 26 Nov 2010 14:32:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:sender:received :in-reply-to:references:from:date:x-google-sender-auth:message-id :subject:to:cc:content-type; bh=r14BLDuHaYFekb5kg3m6yzSEDR8s0vD+lZuf99S6EUU=; b=uZgqyD7rrQ7Cnd+lNq7CpOOfw5cC5ZCHFf4FcoRAAOWu32HMzMWKPNK8DL5+s+8uNB 489yW8qmr/+C8x00G7eyAR6Zc+v9osHmOpjJresXwHjxa3VCkSmWDlaO+g8elzT+lx0P lFZ/C+MmLygo7b/7X/wXTEMggG/aH5ZcSYXB8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; b=U0Ui9MIsbOibYp1BXAgelHuz7nYb8jdzdkQoDaflcDb743kcLhzOJhYcOcXG1RCZMj HJnTpnQjdHLEeThwN6MOcoea4j3cPP1/N0UgEI3BAoV+ESX/2BFPRHQCGTIaxvtxyynO skxz1+2+6Sm+BLng3iuq0WV2krdz7cK4tgTms= Received: by 10.229.246.79 with SMTP id lx15mr2238122qcb.30.1290810769554; Fri, 26 Nov 2010 14:32:49 -0800 (PST) MIME-Version: 1.0 Sender: gabriel.scherer@gmail.com Received: by 10.229.253.135 with HTTP; Fri, 26 Nov 2010 14:32:29 -0800 (PST) In-Reply-To: References: <4CF02BE2.6050500@pps.jussieu.fr> From: bluestorm Date: Fri, 26 Nov 2010 23:32:29 +0100 X-Google-Sender-Auth: gjzG10DAkf6S4wQMjwJNZxLqspM Message-ID: Subject: Re: [Caml-list] zero-arity constructor To: Julia Lawall Cc: Mehdi Dogguy , caml-list@yquem.inria.fr Content-Type: multipart/alternative; boundary=00163628482a643c4b0495fc4d1a X-Spam: no; 0.00; bug:01 frisch:01 ocaml:01 camlp:01 constructors:01 arity:01 arity:01 constructors:01 frisch:01 foo:01 foo:01 non-zero:01 ocaml:01 wildcard:01 wildcard:01 --00163628482a643c4b0495fc4d1a Content-Type: text/plain; charset=ISO-8859-1 A quick summary for those like me that didn't follow the change and were baffled to find out that "it's not a bug, it's a feature". The change was asked for by Alain Frisch in 2006 ( http://caml.inria.fr/mantis/view.php?id=4052 ) and finally added in ocaml 3.11. The rationale is to make it easy to mechanically -- think camlp4 or another preprocessor -- generate pattern clauses to test for the head constructor of a data type, ignoring it's parameter. Before that change, (K _) would work for all constructors K of arity greater than 1, but not for arity 0. After the change, (K _) work even for constant constructors. Generating a match clause that says "looks if it's the constructor K, I don't care about the arguments" is much easier as you don't have to carry arity information around. The downside of this behaviour is that the universal pattern _ has an different meaning in this setting. It does not only matches any value (as the manual says : http://caml.inria.fr/pub/docs/manual-ocaml/patterns.html ), but also "matches any number of arguments, possibly 0". The nice compositional interpretation of patterns -- K (p1, .., pN) matches a value with constructor K and whose N arguments match p1..pN -- is lost. Note that this was already the case before the change suggested by Alain Frisch : _ would work for two-arguments constructors as well, while a named variable wouldn't -- this is well-known subtle difference between (Foo of a * b) and (Foo of (a * b)). The pattern _ ignored any non-zero number of arguments. Note that since ocaml 3.12, there is a warning available for this very error. $ ocaml -warn-help [...] 28 Wildcard pattern given as argument to a constant constructor. [...] $ cat test.ml type ty = A | B let test = function | A _ -> () | B -> () $ ocaml -w +28 test.ml File "test.ml", line 4, characters 4-5: Warning 28: wildcard pattern given as argument to a constant constructor I think than, in the end, it's all a matter of compromise. Thanks to Julia and Mehdi for casting light on the dark corners of the ocaml syntax! PS : I haven't found that behaviour documented anywhere. Maybe it would be good to describe that special behaviour of _ on constructors in the manual? On Fri, Nov 26, 2010 at 11:02 PM, Julia Lawall wrote: > On Fri, 26 Nov 2010, Mehdi Dogguy wrote: > > > On 11/26/2010 10:46 PM, Julia Lawall wrote: > > > The following code compiles in 3.12.0 but doesn't compile in 3.10.2. > > > Is it a bug or a feature? > > > > > > > It's a feature that was implemented in 3.11.0 (iirc). > > > > See: http://caml.inria.fr/mantis/view.php?id=4675 (and other related > > bugreports). > > OK, thanks. I agree wth those that don't like the change... > > julia > > _______________________________________________ > 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 > --00163628482a643c4b0495fc4d1a Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable A quick summary for those like me that didn't follow the change and wer= e baffled to find out that "it's not a bug, it's a feature&quo= t;.

The change was asked for by Alain Frisch in 2006 (= =A0http://caml.i= nria.fr/mantis/view.php?id=3D4052=A0) and finally added in ocaml 3.11. = The rationale is to make it easy to mechanically -- think camlp4 or another= preprocessor -- generate pattern clauses to test for the head constructor = of a data type, ignoring it's parameter.
Before that change, (K _) would work for all constructors K of arity g= reater than 1, but not for arity 0. After the change, (K _) work even for c= onstant constructors. Generating a match clause that says "looks if it= 's the constructor K, I don't care about the arguments" is muc= h easier as you don't have to carry =A0arity information around.

The downside of this behaviour is that the universal pa= ttern _ has an different meaning in this setting. It does not only matches = any value (as the manual says :=A0http://caml.inria.fr/pub/docs/manual-ocaml/patt= erns.html=A0), but also "matches any number of arguments, possibly= 0". The nice compositional interpretation of patterns -- K (p1, .., p= N) matches a value with constructor K and whose N arguments match p1..pN --= is lost.
Note that this was already the case before the change suggested by Ala= in Frisch : _ would work for two-arguments constructors as well, while a na= med variable wouldn't -- this is well-known subtle difference between (= Foo of a * b) and (Foo of (a * b)). The pattern _ ignored any non-zero numb= er of arguments.

Note that since ocaml 3.12, there is a warning availabl= e for this very error.

$ ocaml -warn-help
[...]
=A028 Wildcard pattern given as argument to a consta= nt constructor.
[...]

$ cat test.ml
type ty =3D A | B

= let test =3D function
| A _ -> ()
| B -> ()
=

$ ocaml -w +28 test.ml
File "test.ml", line 4, c= haracters 4-5:
Warning 28: wildcard pattern given as argument to = a constant constructor

I think than, in the end, it's all a matter o= f compromise.

Thanks to Julia and Mehdi for castin= g light on the dark corners of the ocaml syntax!


PS : I haven't found that behaviour documented anywhere.= Maybe it would be good to describe that special behaviour of _ on construc= tors in the manual?

On Fri, Nov 26, 2010 at 11:02 = PM, Julia Lawall <jul= ia@diku.dk> wrote:
On Fri, 26 Nov 2010, Mehdi Dogguy wrote:

> On 11/26/2010 10:46 PM, Julia Lawall wrote:
> > The following code compiles in 3.12.0 but doesn't compile in = 3.10.2.
> > Is it a bug or a feature?
> >
>
> It's a feature that was implemented in 3.11.0 (iirc).
>
> See: http://caml.inria.fr/mantis/view.php?id=3D4675 (and other r= elated
> bugreports).

OK, thanks. =A0I agree wth those that don't like the change...
julia

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list Archives: http://caml.in= ria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

--00163628482a643c4b0495fc4d1a--