TITLE: Why does it seem like you can pattern match in categories?
QUESTION [5 upvotes]: I’ve been reading Bartosz Milewski’s “Category Theory for Programmers”, and I’m very confused by some diagrams around the part about adjunctions and monads. Specifically, on p. 332,  c. 22.3, there’s this diagram:

As I understand, $\mu$ is a morphism in the category of endofunctors for a category $C$, $\mu : T^2 \to T$, meaning that $\mu$ is a natural transformation that transforms the image of $T \circ T$ into the image of $T$. It is explained that $\mu \circ T$ is a shorthand for $\mu \circ I_T$, where $I_T : T \to T = \mathbf{id}_T$. However, $\mu \circ I_T : T^2 \to T$, not $T^3 \to T^2$. How can $\mu$ be used here? It seems like $\mu$ pattern-matches on its argument, but that’s looking at the actual realization of $\mu$, instead of looking at the morphisms, as category theory is apparently supposed to do. What’s going on?

REPLY [7 votes]: Here's a concrete description of what the terms mean:

$T$ is an endofunctor, i.e. a functor $C\to C$ where $C$ is some category. $T^3$ and $T^2$ are the compositions, e.g. $T^3=T\circ T\circ T$, which are also endofunctors on $C.$
$\mu$ is a natural transformation $T^2\to T.$ So if $A$ is an object of $C,$ then $\mu_A$ is an arrow $T^2(A)\to T(A).$
$\mu\circ T$ is the natural transformation $T^3\to T^2,$ whose components are given by $(\mu\circ T)_A =\mu_{T(A)}$ for $A$ an object of $C.$
$T\circ \mu$ is the natural transformation $T^3\to T^2,$ whose components are given by $(T\circ \mu)_A = T(\mu_A).$

Edit
On the matter with $\mu\circ T$ meaning $\mu\circ I_T$, we're not treating $\circ$ as composition of arrows in the category of functors ("vertical composition"), but rather as "horizontal composition", where if we have $\mu:F\to G$ and $\mu':F'\to G'$ with $F\circ F'$ and $G\circ G'$ existing, then $\mu\circ \mu'$ is the natural transformation $F\circ F'\to G\circ G'$ with components $(\mu\circ \mu')_A = \mu_{G'(A)}\circ F(\mu'_A).$
One way think about horizontal composition is as composition in the category whose objects are categories and arrows are natural transformations. So in other words, if $C$ and $D$ are categories, an arrow $C\to D$ consists of two functors $F,G:C\to D$ and a natural transformation $F\to G.$
Another way to think of horizontal composition in this specific case is to think of $\circ$ in its role as the tensor product in the (strict monoidal) category of endofunctors. Then $\mu \circ T := \mu \circ I_T,$ can be written in a different notation as $\mu \otimes I_T.$ This is the tensor product of an arrow $T^2\to T$ and an arrow $T\to T$. which will be an arrow $T^2\otimes T\to T\otimes T,$ i.e. a natural transformation $T^3\to T^2,$ when we go back to the original notation. So, in this guise, horizontal composition is how $\circ$ acts on pairs of natural transformations when we view it as a tensor product (recalling this is a bifunctor $E\times E\to E$, where $E$ is the category of endofunctors).