Natural transformations
In the previous section, we discussed functors, but even more important than functors in category theory are natural transformations. Informally, a natural transformation is a mapping between functors; through a natural transformation, one functor is transformed into another.
Definition: Natural transformation
Given two categories , and two functors , , a natural transformation is a collection of morphisms such that for , , called the component of at , such that for any morphism , the following diagram commutes:
When we say that this diagram commutes, we mean that the pairs of morphisms compose diagonally into the same morphism, thus satisfying the following relation:
Composition
Natural transformations can also be composed just like morphisms and functors. Given the functors , , and the natural transformations , , there will always exist a natural transformation with components , .
Connection with programming
Natural transformations are often used in programming, although they are not known by this name. The most common natural transformation used in programming is the map/fmap/select function of generic types, which imposes a natural transformation from the identity functor to the application of a generic type that implements this function. For example, we can see how this applies to the generic class for lists in the following diagram:
The vertical morphisms, which are components of the natural transformation, represent the application of the generic type over the initial types, and the map function is the one that ensures the existence of horizontal morphisms so that the diagram commutes.