Flat and Orderless Functions

Although the Wolfram Language matches patterns in a purely structural fashion, its notion of structural equivalence is quite sophisticated. In particular, it takes account of properties such as commutativity and associativity in functions like Plus and Times.

This means, for example, that the Wolfram Language considers the expressions and equivalent for the purposes of pattern matching. As a result, a pattern like can match not only , but also .

This expression has exactly the same form as the pattern.
In[1]:=
Click for copyable input
Out[1]=
In this case, the expression has to be put in the form in order to have the same structure as the pattern.
In[2]:=
Click for copyable input
Out[2]=

Whenever the Wolfram Language encounters an orderless or commutative function such as Plus or Times in a pattern, it effectively tests all the possible orders of arguments to try and find a match. Sometimes, there may be several orderings that lead to matches. In such cases, the Wolfram Language just uses the first ordering it finds. For example, could match with , , or with , , . The Wolfram Language tries the case , , first, and so uses this match.

This can match either with or with . The Wolfram Language tries first, and so uses this match.
In[3]:=
Click for copyable input
Out[3]=
ReplaceList shows both possible matches.
In[4]:=
Click for copyable input
Out[4]=

As discussed in "Attributes", the Wolfram Language allows you to assign certain attributes to functions, which specify how those functions should be treated in evaluation and pattern matching. Functions can for example be assigned the attribute Orderless, which specifies that they should be treated as commutative or symmetric, and allows their arguments to be rearranged in trying to match patterns.

Orderlesscommutative function: , etc., are equivalent to
Flatassociative function: , etc., are equivalent to
OneIdentity, etc., are equivalent to a
Attributes[f]give the attributes assigned to f
SetAttributes[f,attr]add attr to the attributes of f
ClearAttributes[f,attr]remove attr from the attributes of f

Some attributes that can be assigned to functions.

Plus has attributes Orderless and Flat, as well as others.
In[5]:=
Click for copyable input
Out[5]=
This defines to be an orderless or commutative function.
In[6]:=
Click for copyable input
The arguments of are automatically sorted into order.
In[7]:=
Click for copyable input
Out[7]=
the Wolfram Language rearranges the arguments of functions to find a match.
In[8]:=
Click for copyable input
Out[8]=

In addition to being orderless, functions like Plus and Times also have the property of being flat or associative. This means that you can effectively "parenthesize" their arguments in any way, so that, for example, is equivalent to , and so on.

The Wolfram Language takes account of flatness in matching patterns. As a result, a pattern like can match , with and .

The argument of is written as so as to match the pattern.
In[9]:=
Click for copyable input
Out[9]=
If there are no other constraints, the Wolfram Language will match to the first element of the sum.
In[10]:=
Click for copyable input
Out[10]=
This shows all the possible matches.
In[11]:=
Click for copyable input
Out[11]=
Here is forced to match .
In[12]:=
Click for copyable input
Out[12]=

The Wolfram Language can usually apply a transformation rule to a function only if the pattern in the rule covers all the arguments in the function. However, if you have a flat function, it is sometimes possible to apply transformation rules even though not all the arguments are covered.

This rule applies even though it does not cover all the terms in the sum.
In[13]:=
Click for copyable input
Out[13]=
This combines two of the terms in the sum.
In[14]:=
Click for copyable input
Out[14]=

Functions like Plus and Times are both flat and orderless. There are, however, some functions, such as Dot, which are flat, but not orderless.

Both and can match any sequence of terms in the dot product.
In[15]:=
Click for copyable input
Out[15]=
This assigns the attribute Flat to the function .
In[16]:=
Click for copyable input
The Wolfram Language writes the expression in the form to match the pattern.
In[17]:=
Click for copyable input
Out[17]=
The Wolfram Language writes this expression in the form to match the pattern.
In[18]:=
Click for copyable input
Out[18]=

In an ordinary function that is not flat, a pattern such as matches an individual argument of the function. But in a function that is flat, can match objects such as which effectively correspond to a sequence of arguments. However, in the case where matches a single argument in a flat function, the question comes up as to whether the object it matches is really just the argument a itself, or . The Wolfram Language chooses the first of these cases if the function carries the attribute OneIdentity, and chooses the second case otherwise.

This adds the attribute OneIdentity to the function .
In[19]:=
Click for copyable input
Now matches individual arguments, without wrapped around them.
In[20]:=
Click for copyable input
Out[20]=

The functions Plus, Times, and Dot all have the attribute OneIdentity, reflecting the fact that Plus[x] is equivalent to x, and so on. However, in representing mathematical objects, it is often convenient to deal with flat functions that do not have the attribute OneIdentity.