## ﻿7.6 Patterns

pattern | ::= | value-name
| ∣ | _
| ∣ | constant
| ∣ | pattern as value-name
| ∣ | ( pattern )
| ∣ | ( pattern : typexpr )
| ∣ | pattern | pattern
| ∣ | constr pattern
| ∣ | `tag-name pattern
| ∣ | #typeconstr
| ∣ | pattern{, pattern}+
| ∣ | { field[: typexpr]  [= pattern] {; field[: typexpr]  [= pattern] }  [; _] [;]}
| ∣ | [ pattern{; pattern}  [;]]
| ∣ | pattern :: pattern
| ∣ | [| pattern{; pattern}  [;]|]
| ∣ | char-literal .. char-literal
| ∣ | lazy pattern
| ∣ | exception pattern
| ∣ | module-path .( pattern )
| ∣ | module-path .[ pattern ]
| ∣ | module-path .[| pattern |]
| ∣ | module-path .{ pattern }

See also the following language extensions: first-class modules, attributes and extension nodes.

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
.. | –
lazy (see section ‍7.6) | –
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”.

#### ﻿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 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).

#### ﻿Constant patterns

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

#### ﻿Alias patterns

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

#### ﻿Parenthesized patterns

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

#### ﻿“Or” patterns

The pattern pattern1 | pattern2 represents the logical “or” of the two patterns pattern1 and pattern2. A value matches pattern1 | pattern2 if it matches pattern1 or pattern2. The two sub-patterns pattern1 and pattern2 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 pattern1 | pattern2, the bindings performed are those of pattern1 when v matches pattern1. Otherwise, value ‍v matches pattern2 whose bindings are performed.

#### ﻿Variant patterns

The pattern constr ( pattern1 , … , patternn ) matches all variants whose constructor is equal to constr, and whose arguments match pattern1 …patternn. 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 pattern1 :: pattern2 matches non-empty lists whose heads match pattern1, and whose tails match pattern2.

The pattern [ pattern1 ; … ; patternn ] matches lists of length n whose elements match pattern1 …patternn, respectively. This pattern behaves like pattern1 :: … :: patternn :: [].

#### ﻿Polymorphic variant patterns

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

#### ﻿Polymorphic variant abbreviation patterns

If the type [('a,'b,…)] typeconstr = [ ` tag-name1 typexpr1 | … | ` tag-namen typexprn] is defined, then the pattern #typeconstr is a shorthand for the following or-pattern: ( `tag-name1(_ : typexpr1) | … | ` tag-namen(_ : typexprn)). It matches all values of type [< typeconstr ].

#### ﻿Tuple patterns

The pattern pattern1 , … , patternn matches n-tuples whose components match the patterns pattern1 through patternn. That is, the pattern matches the tuple values (v1, …, vn) such that patterni matches vi for i = 1,… , n.

#### ﻿Record patterns

The pattern { field1 [= pattern1] ; … ; fieldn [= patternn] } matches records that define at least the fields field1 through fieldn, and such that the value associated to fieldi matches the pattern patterni, for i = 1,… , n. A single identifier fieldk stands for fieldk = fieldk , and a single qualified identifier module-path . fieldk stands for module-path . fieldk = fieldk . The record value can define more fields than field1 …fieldn; 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 { field1 : typexpr1 = pattern1 ;… ; fieldn : typexprn = patternn } to force the type of fieldk to be compatible with typexprk.

#### ﻿Array patterns

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

#### ﻿Range patterns

The pattern ' c ' .. ' d ' is a shorthand for the pattern

' c ' | ' c1 ' | ' c2 ' | … | ' cn ' | ' d '

where c1, c2, …, cn 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.

#### ﻿Lazy patterns

(Introduced in Objective Caml 3.11)

pattern | ::= | ...

The pattern lazy pattern matches a value v of type Lazy.t, provided pattern matches the result of forcing v with Lazy.force. A successful match of a pattern containing lazy sub-patterns forces the corresponding parts of the value being matched, even those that imply no test such as lazy value-name or lazy _. Matching a value with a pattern-matching where some patterns contain lazy sub-patterns may imply forcing parts of the value, even when the pattern selected in the end has no lazy sub-pattern.

For more information, see the description of module Lazy in the standard library (module Lazy).

#### ﻿Exception patterns

(Introduced in OCaml 4.02)

A new form of exception pattern, exception pattern , is allowed only as a toplevel pattern or inside a toplevel or-pattern under a match...with pattern-matching (other occurrences are rejected by the type-checker).

Cases with such a toplevel pattern are called “exception cases”, as opposed to regular “value cases”. Exception cases are applied when the evaluation of the matched expression raises an exception. The exception value is then matched against all the exception cases and re-raised if none of them accept the exception (as with a try...with block). Since the bodies of all exception and value cases are outside the scope of the exception handler, they are all considered to be in tail-position: if the match...with block itself is in tail position in the current function, any function call in tail position in one of the case bodies results in an actual tail call.

A pattern match must contain at least one value case. It is an error if all cases are exceptions, because there would be no code to handle the return of a value.

#### ﻿Local opens for patterns

(Introduced in OCaml 4.04)

For patterns, local opens are limited to the module-path.( pattern) construction. This construction locally opens the module referred to by the module path module-path in the scope of the pattern pattern.

When the body of a local open pattern is delimited by [ ], [| |], or { }, the parentheses can be omitted. For example, module-path.[ pattern] is equivalent to module-path.([ pattern]), and module-path.[| pattern |] is equivalent to module-path.([| pattern |]).