is an attribute that can be assigned to a symbol f to indicate that all expressions involving nested functions f should be flattened out. This property is accounted for in pattern matching.
Flat
is an attribute that can be assigned to a symbol f to indicate that all expressions involving nested functions f should be flattened out. This property is accounted for in pattern matching.
Details
- Flat corresponds to the mathematical property of associativity.
- For a symbol f with attribute Flat, f[f[a,b],f[c]] is automatically reduced to f[a,b,c].
- Functions like Plus, Times, and Dot are Flat.
- For a Flat function f, the variables x and y in the pattern f[x_,y_] can correspond to any sequence of arguments.
- The Flat attribute must be assigned before defining any values for a Flat function.
Examples
open all close allBasic Examples (3)
Nested expressions with flat functions are flattened out:
SetAttributes[f, Flat]f[f[a, b], f[c, f[d, e]]]Flat implements the notion of associativity:
Attributes[Plus]a + (b + c)Flat allows the pattern matcher to use associativity:
a + b + c /. x_ + y_ -> {x, y}Scope (3)
Nested expressions with associative functions are flattened out:
Attributes[NonCommutativeMultiply](a**(b**c))**(d)In pattern matching, Flat allows sequences of elements to be replaced:
SetAttributes[f, Flat]f[a, b, c, d, e] /. f[b, c, d] -> xf[a, b, c, d, e] /. f[b, c, d] -> f[x, y]f[a, b, c, d, e] /. f[b] -> xFor flat and orderless functions, any subset of the arguments may match:
SetAttributes[f, {Flat, Orderless}]f[a, b, c, d, e] /. f[d, b] -> xProperties & Relations (5)
Nested expressions with flat functions are automatically flattened:
f[f[a, b], f[c, f[d, e]]]Flatten[%]SetAttributes[g, Flat]g[g[a, b], g[c, g[d, e]]]For flat functions, a definition for the two-argument case is normally sufficient:
SetAttributes[g, {Flat, OneIdentity}]g[x_, y_] := x + yg[a, b, c, d]An expression with a flat head is considered matched in its entirety when any sequence is matched:
a + b + Sin[a + b] /. a + b -> 5{a + b, Sin[a + b]} /. a + b -> 5ReplaceRepeated may be helpful in ensuring all occurrences are replaced:
a + b + Sin[a + b] //. a + b -> 5When Blank matches a sequence inside a Flat function f, it will maintain the head f:
SetAttributes[f, Flat]f[a, b, c] /. f[a, x_] -> {x}f[a, b, c] /. f[x_] -> {x}Sequential patterns like BlankSequence and BlankNullSequence are not affected by Flat:
f[a, b, c] /. f[a, z__] :> {z}f[a, b, c] /. f[a, z___] :> {z}For a flat function f that is not OneIdentity, when f[x_] is compared with f[expr], the pattern matcher will attempt to bind x_ to f[expr] first, and only if that fails to expr:
SetAttributes[f, Flat]f[3] /. f[z_] /; IntegerQ[Echo[z]] :> zIf the function also has attribute OneIdentity, the first attempt is skipped:
SetAttributes[fo, {Flat, OneIdentity}]fo[3] /. fo[z_] /; IntegerQ[Echo[z]] :> zPossible Issues (4)
For flat and orderless functions, pattern matching may have to try a large number of cases:
SetAttributes[f, {Flat, OneIdentity, Orderless}]ReplaceList[f[a, b, c, d], f[x_, y_] :> {x, y}]Longest can be used to prevent additional matches for shorter subexpressions:
SetAttributes[f, {Flat, OneIdentity}]Replace[f[a, b, c, d], f[x_, y__, z_] :> {y}]Replace[f[a, b, c, d], f[x_, Longest[y__], z_] :> {y}]For a flat function f, the pattern f[_] matches f with one or more arguments:
SetAttributes[f, Flat]MatchQ[f[1, 2], f[_]]Repeated can be used as a pattern that only matches a single argument, even inside a flat function:
MatchQ[f[1, 2], f[Repeated[_, {1}]]]Restrictions may behave unexpectedly because x_ matches together with the head of a flat function:
Replace[a + b + c , Plus[a, x_] :> x]The following restriction works because x_Plus matches Plus[b,c]:
Replace[a + b + c , Plus[a, x_Plus] :> x]However, this one will fail because x_ cannot have both head Plus and Symbol:
Replace[a + b + c , Plus[a, x_Symbol] :> x]Using BlankSequence on the left-hand side produces more consistent results:
Replace[a + b + c, Plus[a, x__Symbol] :> Plus[x]]See Also
Tech Notes
Related Guides
History
Introduced in 1988 (1.0)
Text
Wolfram Research (1988), Flat, Wolfram Language function, https://reference.wolfram.com/language/ref/Flat.html.
CMS
Wolfram Language. 1988. "Flat." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Flat.html.
APA
Wolfram Language. (1988). Flat. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Flat.html
BibTeX
@misc{reference.wolfram_2026_flat, author="Wolfram Research", title="{Flat}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/Flat.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_flat, organization={Wolfram Research}, title={Flat}, year={1988}, url={https://reference.wolfram.com/language/ref/Flat.html}, note=[Accessed: 13-June-2026]}