Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
zero-arity constructor
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: bluestorm <bluestorm.dylc@g...>
Subject: Re: [Caml-list] zero-arity constructor
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 ( ) 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 : ),
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

Note that since ocaml 3.12, there is a warning available for this very

$ ocaml -warn-help
 28 Wildcard pattern given as argument to a constant constructor.

$ cat
type ty = A | B

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

$ ocaml -w +28
File "", 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

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: (and other related
> > bugreports).
> OK, thanks.  I agree wth those that don't like the change...
> julia
> _______________________________________________
> Caml-list mailing list. Subscription management:
> Archives:
> Beginner's list:
> Bug reports: