Sub topic 6
Associativity: regrouping a chain of compositions
This chapter assumes you can evaluate a composite such as (f∘g)(x), you know that the order of composition matters, and you can find the domain of a composite. Here we look at what you can change in a chain of three or more functions (the grouping) and what you cannot change (the order).
Chains of three functions
So far you have composed two functions at a time. Real problems often chain three or more. Suppose you have f, g and h and want to apply h first, then g, then f. There are two natural ways to write that with the ∘ symbol:
- (f∘g)∘h: first build the composite f∘g, then compose it with h.
- f∘(g∘h): first build the composite g∘h, then compose f with it.
Associativity says both give the same function:
(f∘g)∘h = f∘(g∘h)
Because of this, we usually drop the brackets and write f∘g∘h. Reading right to left, the input goes into h, then g, then f.
Why the two groupings agree
Check the definition at a single input x.
- By definition, ((f∘g)∘h)(x) = (f∘g)(h(x)) = f(g(h(x))).
- By definition, (f∘(g∘h))(x) = f((g∘h)(x)) = f(g(h(x))).
Both reduce to f(g(h(x))). The brackets only decide which pair you bundle into a single function first. They do not change which function is applied to which value, or in what sequence.
The diagram shows one pipeline. Grouping A bundles the last two stages into one box, and grouping B bundles the first two. Either way, the data takes the same path.
A worked example
Let f(x) = 2x + 3, g(x) = x², and h(x) = x − 1. Compute both groupings symbolically.
Grouping A, (f∘g)∘h. First, (f∘g)(x) = f(x²) = 2x² + 3. Then compose with h:
((f∘g)∘h)(x) = (f∘g)(x − 1) = 2(x − 1)² + 3
Grouping B, f∘(g∘h). First, (g∘h)(x) = g(x − 1) = (x − 1)². Then:
(f∘(g∘h))(x) = f((x − 1)²) = 2(x − 1)² + 3
The two results are identical. Expanding gives 2x² − 4x + 5.
The code below confirms this numerically and builds the chain for any number of functions.
from functools import reduce
def f(x):
return 2 * x + 3
def g(x):
return x ** 2
def h(x):
return x - 1
def compose(outer, inner):
return lambda x: outer(inner(x))
group_a = compose(compose(f, g), h) # (f∘g)∘h
group_b = compose(f, compose(g, h)) # f∘(g∘h)
for x in [-3, 0, 1, 4, 10]:
assert group_a(x) == group_b(x) == 2 * x**2 - 4 * x + 5
print(group_a(4), group_b(4)) # 21 21
def chain(*funcs):
"""chain(f, g, h) means f∘g∘h: the last function is applied first."""
return reduce(compose, funcs)
fgh = chain(f, g, h)
print(fgh(4)) # h(4)=3, g(3)=9, f(9)=21reduce(compose, funcs) groups from the left, producing (f∘g)∘h. Grouping from the right would give the same function, which is the practical meaning of associativity: a library can choose whichever grouping is convenient.
What associativity does not say
Associativity is easy to confuse with commutativity, which is the claim that order does not matter. Composition has the first property and lacks the second.
| Property | Statement | Holds for composition? |
|---|---|---|
| Associative | (f∘g)∘h = f∘(g∘h) | Yes |
| Commutative | f∘g = g∘f | No, in general |
Using the functions above, f∘g∘h gives 2(x − 1)² + 3. Reordering to h∘g∘f gives (2x + 3)² − 1. At x = 4 the first is 21 and the second is 120. Moving the brackets is always safe. Moving the functions is not.
Associativity and domains
The equality (f∘g)∘h = f∘(g∘h) holds as functions, including their domains. An input x is allowed in either grouping exactly when both of these are true:
- x is in the domain of h,
- h(x) is in the domain of g, and g(h(x)) is in the domain of f.
Both groupings impose the same three conditions, just in a different bundled form. So you never need to worry that regrouping will quietly add or remove an allowed input.
Take f(x) = √x, g(x) = x − 4, h(x) = x². The chain f∘g∘h gives √(x² − 4). Working through the conditions:
- h accepts every real x.
- g accepts every real value of h(x), so no extra limit here.
- f needs g(h(x)) = x² − 4 ≥ 0, so |x| ≥ 2.
The domain is x ≤ −2 or x ≥ 2, and it is the same whichever way you group.
A related trap is simplifying before checking the domain. The chain below shows why you should record the restrictions from the original chain.
import math
def f(x):
return x ** 2
def g(x):
return math.sqrt(x)
# (f∘g)(x) = (sqrt(x))^2, which looks like it simplifies to x.
fg = lambda x: f(g(x))
print(fg(9)) # 9.0
try:
print(fg(-9))
except ValueError as e:
print("error:", e) # math domain error: -9 is not allowedThe simplified form x would accept −9, but the original chain does not. The domain of a composite is read from the unsimplified chain.
Why associativity matters in practice
Associativity is what makes several later ideas work.
- Decomposition. When you break a function such as 3(x + 2)² − 1 into simple steps, you can describe it as a chain of "add 2, square, multiply by 3, subtract 1" without specifying brackets. Any grouping describes the same function.
- Inverses of chains. To undo a chain you reverse the order of the steps. Showing that the reversed inverses cancel pairwise relies on regrouping, as the next sections of the tutorial use.
- Programming. Pipelines, middleware stacks and function-chaining utilities rely on it. You can precompose any adjacent stages into one function without changing the program's behaviour.
Common mistakes
- Thinking "associative" means "any order." Only the brackets move. The sequence of functions stays fixed.
- Reading the chain left to right. In f∘g∘h, h acts first. A chain read left to right will give the reverse order.
- Dropping domain restrictions after simplifying. Keep the restrictions from the unsimplified chain.
- Confusing ∘ with multiplication. (f∘g)(x) = f(g(x)) is not f(x)·g(x). Composition is associative like multiplication of numbers, but it is a different operation and is not commutative.
Summary
Composition of functions is associative: (f∘g)∘h = f∘(g∘h), both as formulas and as domains. You can write f∘g∘h without brackets, group the stages as convenient, and evaluate by applying the rightmost function first. You cannot reorder the functions.
Exercise
Let p(x) = x + 5, q(x) = 3x, and r(x) = x². (a) Compute (p∘q)∘r and p∘(q∘r) symbolically and show they are equal. (b) Evaluate p∘q∘r and r∘q∘p at x = 2. (c) Give the domain of s∘p∘q, where s(x) = 1/x.
Show answer
(a) (p∘q)(x) = 3x + 5, so ((p∘q)∘r)(x) = 3x² + 5. Also (q∘r)(x) = 3x², so (p∘(q∘r))(x) = 3x² + 5. They are equal. (b) p∘q∘r at 2: r(2) = 4, q(4) = 12, p(12) = 17. r∘q∘p at 2: p(2) = 7, q(7) = 21, r(21) = 441. The orders give different values, so the functions differ even though regrouping is allowed. (c) (s∘p∘q)(x) = 1/(3x + 5). We need 3x + 5 ≠ 0, so x ≠ −5/3. The domain is all real numbers except −5/3.
Exercise
A programmer writes pipeline = compose(compose(clean, parse), validate) and a colleague rewrites it as compose(clean, compose(parse, validate)). Does the behaviour change? What if the colleague rewrites it as compose(compose(parse, clean), validate)?
Show answer
The first rewrite does not change behaviour. It only moves brackets, so by associativity both apply validate first, then parse, then clean, and they accept the same inputs. The second rewrite swaps clean and parse, so parse now runs after clean instead of before it. That changes the function, and the results (and the accepted inputs) can differ.