Objects Used in Pattern Matching

There are a number of objects used in the pattern matching process, depending on what type of match is required.

The objects used for matching include:

All of these objects are defined in the Code namespace. All of these objects match an expression against a pattern except for the MatchParameters and MatchParamCommutative objects. These objects are used to for matching against the parameters passed into a function. During execution, the MatchParameters object is replaced by a Match object with the first parameter of the Match object containing a parameter passed into the function call and the second parameter of the Match object containing the pattern that was in the MatchParameters object. For example, if the clause is:

and the function call is MyFn[x + y], then the executed clause will be:

The same processing occurs for clauses that have the MatchParamCommutative object except that it's replaced by a MatchCommutative object.

In addition to the Match objects themselves, there are several other objects used in the pattern matching process. These include:

The three list variable objects are most often used when the pattern contains a monoid. The ParameterList object is used as a container in some cases when multiple terms are bound to a single variable.

There are And, Or, and Not objects defined in the Code namespace and are used with pattern matching. These objects can take either Boolean or PatternMatch objects as parameters. More complex pattern matches can be built using these objects. For more information, see:

Each of the pattern match objects (Match, MatchParmeters, etc.) is typed as PatternMatch. The Assert objects are also typed as PatternMatch → U → U. Boolean is defined to be a subtype of PatternMatch so that clauses like:

are type valid. A Boolean is basically a PatternMatch with a empty binding list.