6.6 | Patterns

`pattern` | ::= | `value-name`
| | | `_`
| | | `constant`
| | | `pattern` `as` `value-name`
| | | `(` `pattern` `)`
| | | `(` `pattern` `:` `typexpr` `)`
| | | `pattern` `|` `pattern`
| | | `ncconstr` `pattern`
| | | ````tag-name` `pattern`
| | | `#``typeconstr-name`
| | | `pattern` { `,` `pattern` }
| | | `{` `field` `=` `pattern` { `;` `field` `=` `pattern` } `}`
| | | `[` `pattern` { `;` `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 names to values''.

Variable patterns

A pattern that consists in a value name matches any value, binding the name to the value. The pattern `_` also matches any value, but does not bind any name.
Patterns are linear: a variable cannot appear several times in a given pattern. In particular, there is no way to test for equality between two parts of a data structure using only a pattern (but `when` guards can be used for this purpose).

Constant patterns

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

Alias patterns

The pattern `pattern`1 `as` `value-name` matches the same values as `pattern`1. If the matching against `pattern`1 is successful, the name `name` is bound to the matched value, in addition to the bindings performed by the matching against `pattern`1.

Parenthesized patterns

The pattern `(` `pattern`1 `)` matches the same values as `pattern`1. A type constraint can appear in a parenthesized pattern, as in `(` `pattern`1 `:` `typexpr` `)`. This constraint forces the type of `pattern`1 to be compatible with `type`.

``Or'' patterns

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 bind exactly the same identifiers to values having the same types. The bindings performed by matching against an ``or'' pattern are either those performed by the matching against `pattern`1, if it succeeds, or those performed by the matching against `pattern`2, if it succeeds. If both matchings succeed, it is undefined which set of bindings is selected.

Variant patterns

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 `(` `::` `)` `(` `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 `::` `[]`.

Polymorphic variant patterns

The pattern ``tag-name` `pattern`1 matches all polymorphic variants whose tag is equal to `tag-name`, and whose argument matches `pattern`1.

Variant abbreviation patterns

If the type [`('a,'b,...)`] `typeconstr` = `[`tag`1 `t`1 `|` ... `|` ``tag`n `t`n`]` is defined, then the pattern #`typeconstr` is a shorthand for the or-pattern `(`tag`1`(_` `:` `t`1`)` `|` ... `|` ``tag`n`(_` `:` `t`n`))`. It matches all values of type #`typeconstr`.

Tuple patterns

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.

Record patterns

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

Array patterns

The pattern `[|` `pattern`1 `;` ... `;` `pattern`n `|]` matches arrays of length `n` such that the `i`-th array element matches the pattern `pattern`i, for i = 1,... , n.