pattern:ident|`_`

|pattern`as`

ident|`(`

pattern`)`

|`(`

pattern`:`

typexpr`)`

|pattern`|`

pattern|constant|ncconstrpattern|pattern`,`

pattern{`,`

pattern}|`{`

label`=`

pattern{`;`

label`=`

pattern}`}`

|`[`

`]`

|`[`

pattern{`;`

pattern}`]`

|pattern`::`

pattern

The table below shows the relative precedences and associativity of operators and non-closed pattern constructions. The constructions with higher precedences come first.

Operator | Associativity |
---|---|

Constructor application | -- |

:: | right |

, | -- |

| | left |

as | -- |

Patterns are templates that allow selecting data structures of a given shape, and binding identifiers to components of the data structure. This selection operation is called pattern matching; its outcome is either ``this value does not match this pattern'', or ``this value matches this pattern, resulting in the following bindings of identifiers to values''.

A pattern that consists in an identifier matches any value,
binding the identifier to the value. The pattern `_`

also matches
any value, but does not bind any identifier.

The pattern *pattern*_{1} `as`

*ident* matches the same values as
*pattern*_{1}. If the matching against *pattern*_{1} is successful,
the identifier *ident* is bound to the matched value, in addition to the
bindings performed by the matching against *pattern*_{1}.

The pattern `(`

*pattern*_{1} `)`

matches the same values as
*pattern*_{1}. A type constraint can appear in a
parenthesized patterns, as in `(`

*pattern*_{1} `:`

*typexpr* `)`

. This
constraint forces the type of *pattern*_{1} to be compatible with
*type*.

The pattern *pattern*_{1} `|`

*pattern*_{2} represents the logical ``or'' of
the two patterns *pattern*_{1} and *pattern*_{2}. A value matches
*pattern*_{1} `|`

*pattern*_{2} either if it matches *pattern*_{1} or if it
matches *pattern*_{2}. The two sub-patterns *pattern*_{1} and *pattern*_{2}
must contain no identifiers. Hence no bindings are returned by
matching against an ``or'' pattern.

A pattern consisting in a constant matches the values that are equal to this constant.

The pattern *ncconstr* *pattern*_{1} matches all variants whose
constructor is equal to *ncconstr*, and whose argument matches
*pattern*_{1}.

The pattern *pattern*_{1} `::`

*pattern*_{2} matches non-empty lists whose
heads match *pattern*_{1}, and whose tails match *pattern*_{2}. This
pattern behaves like `prefix`

`::`

`(`

*pattern*_{1} `,`

*pattern*_{2} `)`

.

The pattern `[`

*pattern*_{1} `;`

...`;`

*pattern*_{n} `]`

matches lists
of length n whose elements match *pattern*_{1} ... *pattern*_{n},
respectively. This pattern behaves like
*pattern*_{1} `::`

...`::`

*pattern*_{n} `::`

`[]`

.

The pattern *pattern*_{1} `,`

...`,`

*pattern*_{n} matches n-tuples
whose components match the patterns *pattern*_{1} through *pattern*_{n}. That
is, the pattern matches the tuple values *(**v*_{1},...,*v*_{n}*)* such that
*pattern*_{i} matches *v*_{i} for *i* = 1, ..., *n*.

The pattern `{`

*label*_{1} `=`

*pattern*_{1} `;`

...`;`

*label*_{n} `=`

*pattern*_{n} `}`

matches records that define at least the labels
*label*_{1} through *label*_{n}, and such that the value associated to
*label*_{i} match the pattern *pattern*_{i}, for *i* = 1, ..., *n*.
The record value can define more labels than *label*_{1} ...
*label*_{n}; the values associated to these extra labels are not taken
into account for matching.