Show that Function Compositions Are Associative

Usually, when $f\colon X\to Y$ and $g\colon Y\to Z$ are maps, their composition is written $g\circ f$, rather than $f\circ g$: in this way you write $$ g\circ f(x)=g(f(x)) $$ by definition.

You seem to confuse codomain and range. The range, or image, of $f$ is the subset of the codomain $Y$ consisting of the elements $f(x)$, for $x\in X$. The range has no role whatsoever when composition of maps is considered. At least, when maps are supposed to be defined on the whole domain as is the case when talking of surjectivity or bijectivity.

Associativity is almost obvious. If you have another function $h\colon Z\to W$, you have, by definition, that $g\circ f\colon X\to Z$ and $h\circ g\colon Y\to W$. Thus one can consider also the compositions $$ h\circ(g\circ f) \qquad\text{and}\qquad (h\circ g)\circ f $$ and both are maps $X\to W$, so it makes sense to ask if they are equal. They are, because for each $x\in X$ we have $$ h\circ(g\circ f)(x)=h(g\circ f(x))= h(g(f(x))=h\circ g(f(x))=(h\circ g)\circ f(x). $$ If you can't parse this, just set $y=f(x)$, $z=g(y)$, $F=g\circ f$ and $G=h\circ g$, so that $F(x)=g(f(x))=g(y)=z$. Then $$ h\circ(g\circ f)(x)=h\circ F(x)=h(F(x))=h(z) $$ and $$ (h\circ g)\circ f(x)=G\circ f(x)=G(y)=h\circ g(y)=h(g(y))=h(z) $$ so the two elements are the same.


You have $$(f\circ g)\circ h(x) = f\circ g(h(x)) = f(g(h(x)),$$ and $$f\circ(g\circ h(x)) = f(g \circ h (x)) = f(g(h(x)).$$ The associativity you seek now follows.


I found it easier to reason about composition using the following notation and definitions.

Infix notation for functions

$$(x,y)\in f \leftrightarrow x \space \boldsymbol f \space y $$ and let $$(x,b)\in f \wedge (b,y) \in g \leftrightarrow x \space \boldsymbol f \space b \space \boldsymbol g \space y$$ then

Definition of $(g \circ f)$

If $f,g$ are functions, then $(g \circ f)$ is the relation $$(x,y)\in(g\circ f)\leftrightarrow \exists b: x \space \boldsymbol f \space b \space \boldsymbol g \space y$$


Composition ($\circ$) is associative

If $h,g,f$ are functions, then $$(h \circ g) \circ f = h \circ (g \circ f)$$ Proof. $(x,y)\in(h \circ g) \circ f \leftrightarrow \exists b:x\space\boldsymbol f\space b\space\boldsymbol (\boldsymbol h \boldsymbol\circ \boldsymbol g \boldsymbol )\space y$. Where $b\space\boldsymbol (\boldsymbol h \boldsymbol\circ \boldsymbol g \boldsymbol )\space y \leftrightarrow (b,y)\in (h \circ g)\leftrightarrow \exists b': b\space\boldsymbol g \space b' \space\boldsymbol h \space y.$ Then the membership rule becomes $$(x,y)\in(h \circ g) \circ f \leftrightarrow \exists b,b': x\space\boldsymbol f\space b\space\boldsymbol g \space b' \space\boldsymbol h \space y$$ The other direction is again two applications of the definition of composition. $(x,y)\in h\circ (g \circ f) \leftrightarrow \exists b:x\space\boldsymbol (\boldsymbol g \boldsymbol \circ \boldsymbol f \boldsymbol) \space b \space \boldsymbol h \space y$. But $x\space\boldsymbol (\boldsymbol g \boldsymbol \circ \boldsymbol f \boldsymbol) \space b \leftrightarrow (x,b) \in (g \circ f) \leftrightarrow \exists b': x\space\boldsymbol f \space b' \space\boldsymbol g \space b$. Thus, $$(x,y)\in h \circ (g \circ f) \leftrightarrow \exists b,b': x\space\boldsymbol f\space b'\space\boldsymbol g \space b \space\boldsymbol h \space y$$ And so, $$(x,y) \in (h \circ g) \circ f \leftrightarrow (x,y)\in h \circ ( g \circ f)$$ Which implies $$(h \circ g) \circ f=h \circ ( g \circ f)$$