caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: bluestorm <bluestorm.dylc@gmail.com>
To: Julia Lawall <julia@diku.dk>
Cc: Mehdi Dogguy <mehdi.dogguy@pps.jussieu.fr>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] zero-arity constructor
Date: Fri, 26 Nov 2010 23:32:29 +0100	[thread overview]
Message-ID: <AANLkTimOUt5DmuUkBrNsVRFe1BoOsbegQ13tQWKk1gTX@mail.gmail.com> (raw)
In-Reply-To: <Pine.LNX.4.64.1011262300090.12757@ask.diku.dk>

[-- Attachment #1: Type: text/plain, Size: 3040 bytes --]

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 <julia@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=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
>

[-- Attachment #2: Type: text/html, Size: 4585 bytes --]

  reply	other threads:[~2010-11-26 22:32 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-26 21:46 Julia Lawall
2010-11-26 21:51 ` [Caml-list] " Mehdi Dogguy
2010-11-26 22:02   ` Julia Lawall
2010-11-26 22:32     ` bluestorm [this message]
2010-11-27  9:49 mark
2010-11-27 10:15 ` David Allsopp
2010-11-27 10:31 ` bluestorm
2010-11-27 12:23   ` Julia Lawall
2010-11-28  7:40     ` Martin Jambon
2010-11-27 13:38   ` bluestorm
2010-11-28 11:13   ` Alain Frisch
     [not found]   ` <1221507223.79937.1290942833987.JavaMail.root@zmbs1.inria.fr>
2010-12-01 14:22     ` Damien Doligez
     [not found] ` <118427264.71248.1290853319356.JavaMail.root@zmbs1.inria.fr>
2010-12-01 14:29   ` Damien Doligez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AANLkTimOUt5DmuUkBrNsVRFe1BoOsbegQ13tQWKk1gTX@mail.gmail.com \
    --to=bluestorm.dylc@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=julia@diku.dk \
    --cc=mehdi.dogguy@pps.jussieu.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).