From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 92FF3BC58 for ; Sat, 27 Nov 2010 10:49:26 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AsgEAJ9i8ExV6aAZc2dsb2JhbACDUJEKjWBSFgwKCwcRAx+ILKUJkCaBIYMzcwSKYYMPgnE X-IronPort-AV: E=Sophos;i="4.59,265,1288566000"; d="scan'208";a="80313207" Received: from outgoing-smtp.namesco.net ([85.233.160.25]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 27 Nov 2010 10:49:26 +0100 Received: from [192.168.0.6] (helo=medusa.hosts.co.uk) by outgoing-smtp.namesco.net with esmtp (Exim 4.67) (envelope-from ) id 1PMHPH-0003XR-GF; Sat, 27 Nov 2010 09:49:24 +0000 Received: from root by medusa.hosts.co.uk with local (Exim 4.69) (envelope-from ) id 1PMHOy-0003Nl-On; Sat, 27 Nov 2010 09:49:04 +0000 To: , Cc: , From: Reply-To: Subject: =?utf-8?q?Re=3a=20=5bCaml=2dlist=5d=20zero=2darity=20constructor?= MIME-Version: 1.0 X-Mailer: Namesco Webmail v3.0 Message-ID: <1290851344516@names.co.uk> Date: Sat, 27 Nov 2010 09:49:04 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-namescosender: 0 2002 X-namesco: 192.168.0.171 X-Spam-Score: -1.0 (-) X-Spam: no; 0.00; syntax:01 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 Surely it's preferable to use a syntactically distinct mechanism for this subtly different concept. Given that we're talking about patterns and not general expressions here, surely there's plenty of space in the syntax. Perhaps something like '*' to mean 0 or more. Or is it already too late because '_' has already been incorporated and backwards compatibility dictates that this cannot be changed? type ty =3D A | B let test =3D function | A * -> () | B -> () Mark Adams on 26/11/10 10:35 PM, bluestorm wrote: > 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=3D4052 ) 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 =3D A | B > > let test =3D 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=3D4675 (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 >> > > > > ---------------------------------------- > _______________________________________________ > 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 > > >