2.6 Function composition

2.6.1 Definition of function composition

Suppose you have two functions f:X→Y and g:Y→Z:

X→fY→gZ

Then you can make a new function X→Z whose rule is “do f, then do g”.

Definition 2.6.1.

Let f:X→Y and g:Y→Z. The composition of g and f, written g∘f or g⁢f, is the function X→Z with rule (g∘f)⁢(x)=g⁢(f⁢(x)).

This makes sense because f⁢(x) is an element of Y and g has domain Y so we can use any element of Y as an input to g.

It’s important to remember that g∘f is the function whose rule is “do f, then do g”.

Proposition 2.6.1.

If f:X→Y then f∘idX=idY∘f=f.

Proof.

For any x∈X we have (f∘idX)⁢(x)=f⁢(idX⁡(x))=f⁢(x) and (idY∘f)⁢(x)=idY⁡(f⁢(x))=f⁢(x). ∎

2.6.2 Associativity

Functions f and g such that the codomain of f equals the domain of g, in other words, functions such that g∘f makes sense, are called composable. Suppose that f and g are composable and g and h are also composable, so that we can draw a diagram

X→fY→gZ→hW.

It seems there are two different ways to compose these three functions: you could first compose f and g, then compose the result with h, or you could compose g with h and then compose the result with f. But they both give the same result, because function composition is associative.

Lemma 2.6.2.

Let f:X→Y,g:Y→Z,h:Z→W. Then h∘(g∘f)=(h∘g)∘f.

Proof.

Both h∘(g∘f) and (h∘g)∘f have the same domain X, same codomain W, and same rule that sends x to h⁢(g⁢(f⁢(x))). ∎

The associativity property says that a composition like h∘g∘f doesn’t need any brackets to make it unambiguous: however you bracket it, the result is the same. In fact we can omit brackets from a composition of any length without ambiguity.