Patterns are used in combinators, in val
declarations, and in clausal definitions of functions, to select values and bind variables to values.
A pattern is given by a shape and a set of variables. A shape is either a tuple, a list,
a record, a call, a literal value,
or wildcard (written as _
). If the shape describes a structured value (such as a tuple), its components may
also be shapes. For example, the shape (_,3)
describes all pairs whose second element is 3
,
and the pattern (x,3)
binds x
to the first element of all such pairs.
Note that a pattern may fail to match a value, if it does not have the same shape as that value. When this occurs, the unmatched value is simply discarded.
A pattern such as (x,y)
may bind multiple variables.
However, patterns are linear, meaning that a pattern may mention a variable name at most once.
For example, (x,y,x)
is not a valid pattern.
During typechecking, a pattern is matched against a type instead of a value. This match produces a typing context, which associates a type with each variable that occurs in the pattern.