# Flat and Orderless Functions

Although *Mathematica* 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 *Mathematica* considers the expressions and equivalent for the purposes of pattern matching. As a result, a pattern like can match not only , but also .

In[1]:= |

Out[1]= |

In[2]:= |

Out[2]= |

Whenever *Mathematica* 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, *Mathematica* just uses the first ordering it finds. For example, could match with , , or with , , . *Mathematica* tries the case , , first, and so uses this match.

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

As discussed in "Attributes", *Mathematica* 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.

Orderless | commutative function: , etc., are equivalent to |

Flat | associative 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.

In[5]:= |

Out[5]= |

In[6]:= |

In[7]:= |

Out[7]= |

In[8]:= |

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.

*Mathematica* takes account of flatness in matching patterns. As a result, a pattern like can match , with and .

In[9]:= |

Out[9]= |

In[10]:= |

Out[10]= |

In[11]:= |

Out[11]= |

In[12]:= |

Out[12]= |

*Mathematica* 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.

In[13]:= |

Out[13]= |

In[14]:= |

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.

In[15]:= |

Out[15]= |

In[16]:= |

In[17]:= |

Out[17]= |

In[18]:= |

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 . *Mathematica* chooses the first of these cases if the function carries the attribute OneIdentity, and chooses the second case otherwise.

In[19]:= |

In[20]:= |

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.