Chapter 7  The OCaml language

Foreword

This document is intended as a reference manual for the OCaml language. It lists the language constructs, and gives their precise syntax and informal semantics. It is by no means a tutorial introduction to the language: there is not a single example. A good working knowledge of OCaml is assumed.

No attempt has been made at mathematical rigor: words are employed with their intuitive meaning, without further definition. As a consequence, the typing rules have been left out, by lack of the mathematical framework required to express them, while they are definitely part of a full formal definition of the language.

Notations

The syntax of the language is given in BNF-like notation. Terminal symbols are set in typewriter font (like this). Non-terminal symbols are set in italic font (like  that). Square brackets […] denote optional components. Curly brackets {…} denotes zero, one or several repetitions of the enclosed components. Curly brackets with a trailing plus sign {…}+ denote one or several repetitions of the enclosed components. Parentheses (…) denote grouping.

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

See also the following language extensions: lazy patterns, local opens, first-class modules, attributes, extension nodes and exception cases in pattern matching.

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

OperatorAssociativity
..
lazy (see section 8.3)
Constructor application, Tag applicationright
::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 pattern1patternn, 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 field1fieldn; 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.