Patterns and Transformation Rules
Patterns stand for classes of expressions. They contain pattern objects that represent sets of possible expressions.
any expression, given the name x
a pattern, given the name x
a pattern that yields True when test is applied to its value
any expression with head h
any expression with head h, given the name x
any sequence of one or more expressions
any sequence of zero or more expressions
sequences of expressions, given the name x
sequences of expressions, each with head h
sequences of expressions with head h, given the name x
a sequence of patterns
an orderless sequence of patterns
an orderless sequence of keyivali pairs
an expression with default value v
an expression with head h and default value v
an expression with a globally defined default value
an expression that must have head h, and has a globally defined default value
any expression except one that matches c
any expression matching pattern, except one that matches c
a pattern repeated one or more times
a pattern repeated zero or more times
a pattern repeated according to spec
a pattern that matches at least one of the patterni
a pattern for which cond evaluates to True
a pattern not evaluated
an expression to be matched verbatim
a sequence of options
the longest sequence consistent with pattern
the shortest sequence consistent with pattern
When several pattern objects with the same name occur in a single pattern, all the objects must stand for the same expression. Thus f[x_,x_] can stand for f[2,2] but not f[2,3].
A pattern object such as x__ stands for a sequence of expressions. So, for example, f[x__] can stand for f[a,b,c], with x being Sequence[a,b,c]. If you use x, say in the result of a transformation rule, the sequence will be spliced into the function in which x appears. Thus g[u,x,u] would become g[u,a,b,c,u].
When the pattern objects x_:v and x_. appear as arguments of functions, they represent arguments which may be omitted. When the argument corresponding to x_:v is omitted, x is taken to have value v. When the argument corresponding to x_. is omitted, x is taken to have a default value that is associated with the function in which it appears. You can specify this default value by making assignments for Default[f] and so on.
default value for x_. when it appears as any argument of the function f
default value for x_. when it appears as the n th argument (negative n count from the end)
default value for the n th argument when there are a total of tot arguments
A pattern like f[x__,y__,z__] can match an expression like f[a,b,c,d,e] with several different choices of x, y, and z. The choices with x and y of minimum length are tried first. In general, when there are multiple __ or ___ in a single function, the case that is tried first takes all the __ and ___ to stand for sequences of minimum length, except the last one, which stands for "the rest" of the arguments.
When x_:v or x_. are present, the case that is tried first is the one in which none of them correspond to omitted arguments. Cases in which later arguments are dropped are tried next.
f[x,y] and f[y,x] are equivalent
f[f[x],y] and f[x,y] are equivalent
f[x] and x are equivalent
Pattern objects like x_ can represent any sequence of arguments in a function f with attribute Flat. The value of x in this case is f applied to the sequence of arguments. If f has the attribute OneIdentity, then e is used instead of f[e] when x corresponds to a sequence of just one argument.
immediate assignment: rhs is evaluated at the time of assignment
delayed assignment: rhs is evaluated when the value of lhs is requested
Assignments in the Wolfram Language specify transformation rules for expressions. Every assignment that you make must be associated with a particular Wolfram Language symbol.
assignment is associated with f (downvalue)
assignment is associated with t (upvalue)
assignment is associated with g (upvalue)
In the case of an assignment like f[args]=rhs, the Wolfram Language looks at f, then the head of f, then the head of that, and so on, until it finds a symbol with which to associate the assignment.
When you make an assignment like lhs^=rhs, the Wolfram Language will set up transformation rules associated with each distinct symbol that occurs either as an argument of lhs, or as the head of an argument of lhs.
The transformation rules associated with a particular symbol s are always stored in a definite order, and are tested in that order when they are used. Each time you make an assignment, the corresponding transformation rule is inserted at the end of the list of transformation rules associated with s, except in the following cases:
- The left‐hand side of the transformation rule is identical to a transformation rule that has already been stored, and any /; conditions on the right‐hand side are also identical. In this case, the new transformation rule is inserted in place of the old one.
- The Wolfram Language determines that the new transformation rule is more specific than a rule already present, and would never be used if it were placed after this rule. In this case, the new rule is placed before the old one. Note that in many cases it is not possible to determine whether one rule is more specific than another; in such cases, the new rule is always inserted at the end.
attributes of f
default values for arguments of f
values for f[…], f[…][…], etc.
print forms associated with f
messages associated with f
numerical values associated with f
defaults for options associated with f
values for f itself
values for …[…,f[…],…]
clear a value defined for expr
clear a value associated with f defined for expr
clear all values for the symbols si, except for attributes, messages, and defaults
clear all values for the si, including attributes, messages, and defaults
clear all values, and then remove the names of the si
In Clear, ClearAll, and Remove, each argument can be either a symbol or the name of a symbol as a string. String arguments can contain the metacharacters * and @ to specify action on all symbols whose names match the pattern.
immediate rule: rhs is evaluated when the rule is first given
delayed rule: rhs is evaluated when the rule is used
Replacements for pattern variables that appear in transformation rules are effectively done using ReplaceAll (the /. operator).