OneIdentity
Usage Message: OneIdentity is an attribute that can be assigned to a symbol f to indicate that f[x], f[f[x]], etc.
are all equivalent to x for the purpose of pattern matching. Attributes[OneIdentity] = {Protected}
Related Symbols: Attributes
ClearAttributes
Flat
SetAttributes
Notes: The OneIdentity attribute is used in pattern matching. It is meaningful only when used with the Flat
attribute, or in connection with Default
patterns. When used with the Flat
attribute, OneIdentity causes individual expression elements to be left unchanged for the purpose of pattern matching, instead of as elements in a
subexpression with the same head as the enclosing expression. For example, if f has both the Flat
attribute and the OneIdentity attribute, then f[1,2,3] is treated as equivalent to f[1,2,3], or f[f[1,2],3], or f[1,f[2,3]], and so forth, for the purpose of pattern matching. Without the OneIdentity attribute, f[1,2,3] is instead treated as equivalent to expressions such as f[f[1],f[2],f[3]], or f[f[1,2],f[3]], or and f[f[1],f[2,3]], where the elements are always enclosed in subexpressions with a head of f. This effect of the OneIdentity attribute can be seen by observing that the rule in
In[1]:= Attributes[f] = Flat;
In[2]:= f[1, 2, 3] /. f[p_Integer, q_] -> g[p, q]
Out[2]= f[1, 2, 3]
does not match the expression, and so is not applied while the rule in
In[3]:= Attributes[f] = {Flat, OneIdentity} ;
In[4]:= f[1, 2, 3] /. f[p_Integer, q_] -> g[p, q]
Out[4]= g[1, f[2, 3]]
is applied. The match in the second rule succeeds because, with the addition of the OneIdentity attribute, the elements in f[1,2,3] are
not treated as if they are individuallly enclosed in an expression with a head of f. The second use of the OneIdentity attribute is in
connection with Default
patterns. If f has the OneIdentity attribute, and is used as the head of a pattern with Default
patterns as elements, then any expression expr will be treated like f[expr] for the purpose of pattern matching. For example:
In[5]:= Attributes[f] = OneIdentity ;
In[6]:= expr /. f[p_, q_:def] -> g[p, q]
Out[6]= g[expr, def]
Without the OneIdentity attribute, the rule in the example above would not have been applied.
Additional Online Documentation:
Mathematica 3.0
http://documents.wolfram.com/v3/RefGuide/OneIdentity.html
Mathematica 4.0
http://documents.wolfram.com/v4/RefGuide/OneIdentity.html
Questions or comments? Send email to support@wolfram.com.
| |