Pattern calculus bases all computation on pattern matching of a very general kind. Like lambda calculus, it supports auniform treatment of function evaluation. Also, it allows functions to bepassed as arguments and returned as results. In addition, pattern calculus supportsuniform access to the internal structure of arguments, be they pairsor lists or trees. Also, it allows patterns to be passed as arguments andreturned as results. Uniform access is illustrated by apattern-matching function that computes the size of anarbitrary data structure. In the notation of the programming languagebondi, it is given by the recursive function
The second, or default case matches the pattern against the argument and returns . Thiscase is used only if the matching failed in the first case. Thefirst, or special case matches against any compound, suchas a non-empty list, or pair. Matching binds to the left componentand to the right component. Then the body of the case adds thesizes of these components together.
Similar techniques yield generic queries for searching and updating. Combining recursion and decomposition in this way yields path polymorphism.
The ability to pass patterns as parameters (pattern polymorphism) is illustrated by defining a generic eliminator. Suppose given constructors for creatingthe leaves of a tree, and for converting numbers intocounters. The corresponding eliminators are then
For example, evaluates to as does .
These examples can be produced by applying the generic eliminator to the constructors in question. It is defined by
Now evaluates to | {y} Leaf y -> y
which is equivalent to . Also is equivalent to .
In general, the curly braces contain the bound variables of thepattern, so that is free and is bound in | {y} x y -> y
.