
See also the following language extensions: lazy patterns, local opens, firstclass modules, attributes, extension nodes and exception cases in pattern matching.
The table below shows the relative precedences and associativity of operators and nonclosed pattern constructions. The constructions with higher precedences come first.
Operator  Associativity 
..  – 
lazy (see section 8.3)  – 
Constructor application, Tag application  right 
::  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”.
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 be bound several times by 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).
A pattern consisting in a constant matches the values that are equal to this constant.
The pattern pattern_{1} as valuename matches the same values as pattern_{1}. If the matching against pattern_{1} is successful, the name valuename 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 pattern, as in ( pattern_{1} : typexpr ). This constraint forces the type of pattern_{1} to be compatible with typexpr.
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} if it matches pattern_{1} or pattern_{2}. The two subpatterns pattern_{1} and pattern_{2} must bind exactly the same identifiers to values having the same types. Matching is performed from left to right. More precisely, in case some value v matches pattern_{1}  pattern_{2}, the bindings performed are those of pattern_{1} when v matches pattern_{1}. Otherwise, value v matches pattern_{2} whose bindings are performed.
The pattern constr ( pattern_{1} , … , pattern_{n} ) matches all variants whose constructor is equal to constr, and whose arguments match pattern_{1} … pattern_{n}. It is a type error if n is not the number of arguments expected by the constructor.
The pattern constr _ matches all variants whose constructor is constr.
The pattern pattern_{1} :: pattern_{2} matches nonempty lists whose heads match pattern_{1}, and whose tails match 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 `tagname pattern_{1} matches all polymorphic variants whose tag is equal to tagname, and whose argument matches pattern_{1}.
If the type [('a,'b,…)] typeconstr = [ ` tagname_{1} typexpr_{1}  …  ` tagname_{n} typexpr_{n}] is defined, then the pattern #typeconstr is a shorthand for the following orpattern: ( `tagname_{1}(_ : typexpr_{1})  …  ` tagname_{n}(_ : typexpr_{n})). It matches all values of type [< typeconstr ].
The pattern pattern_{1} , … , pattern_{n} matches ntuples 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 { 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. A single identifier field_{k} stands for field_{k} = field_{k} , and a single qualified identifier modulepath . field_{k} stands for modulepath . field_{k} = field_{k} . 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. Optionally, a record pattern can be terminated by ; _ to convey the fact that not all fields of the record type are listed in the record pattern and that it is intentional. Optional type constraints can be added field by field with { field_{1} : typexpr_{1} = pattern_{1} ;… ; field_{n} : typexpr_{n} = pattern_{n} } to force the type of field_{k} to be compatible with typexpr_{k}.
The pattern [ pattern_{1} ; … ; pattern_{n} ] matches arrays of length n such that the ith array element matches the pattern pattern_{i}, for i = 1,… , n.
The pattern ' c ' .. ' d ' is a shorthand for the pattern
where c_{1}, c_{2}, …, c_{n} are the characters that occur between c and d in the ASCII character set. For instance, the pattern '0'..'9' matches all characters that are digits.