2 Sets and functions

2.7 Function composition

2.7.1 Definition of function composition

Suppose you have two functions f:XY and g:YZ:

XfYgZ

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

Definition 2.7.1.

Let f:XY and g:YZ. The composition of g and f, written gf or gf, is the function XZ with rule (gf)(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 gf is the function whose rule is “do f, then do g”.

Proposition 2.7.1.

If f:XY then fidX=idYf=f.

Proof.

For any xX we have (fidX)(x)=f(idX(x))=f(x) and (idYf)(x)=idY(f(x))=f(x). ∎

2.7.2 Associativity

Functions f and g such that the codomain of f equals the domain of g, in other words, functions such that gf 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

XfYgZhW.

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.7.2.

Let f:XY,g:YZ,h:ZW. Then h(gf)=(hg)f.

Proof.

Both h(gf) and (hg)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 hgf 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.