Chapter 5. Patterns

5.1. Literal Pattern
5.2. Variable Pattern
5.3. Tuple Pattern
5.4. List Pattern
5.5. Record Pattern
5.6. Call Pattern
5.7. Cons Pattern
5.8. As Pattern
5.9. Wildcard Pattern

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.