# Category theory/Natural transformation

 map even $maybeToList$ Just 5


yields the same as

 maybeToList $fmap even$ Just 5


yields: both yield

 [False]


In the followings, this example will be used to illustrate the notion of natural transformation. If the examples are exaggerated and/or the definitions are incomprehensible, try #External links.

## Definition

• Let $\mathcal C$, $\mathcal D$ denote categories.
• Let $\Phi, \Psi : \mathcal C \to \mathcal D$ be functors.
• Let $X, Y \in \mathbf{Ob}(\mathcal C)$. Let $f \in \mathrm{Hom}_{\mathcal C}(X, Y)$.

Let us define the $\eta : \Phi \to \Psi$ natural transformation. It associates to each object of $\mathcal{C}$ a morphism of $\mathcal{D}$ in the following way (usually, not sets are discussed here, but proper classes, so I do not use term “function” for this $\mathbf{Ob}(\mathcal C) \to \mathbf{Mor}(\mathcal D)$ mapping):

• $\forall A \in \mathbf{Ob}(\mathcal C) \longmapsto \eta_A \in \mathrm{Hom}_{\mathcal D}(\Phi(A), \Psi(A))$. We call $\eta_A$ the component of $\eta$ at A.
• $\eta_Y \cdot \Phi(f) = \Psi(f) \cdot \eta_X$

Thus, the following diagram commutes (in $\mathcal D$):

## Example: maybeToList

 map even $maybeToList$ Just 5


yields the same as

 maybeToList $fmap even$ Just 5


yields: both yield

 [False]


This example will be shown in the light of the above definition in the followings.

### Vertical arrows: sides of objects

… showing how the natural transformation works. $\eta : \Phi \to \Psi$
maybeToList :: Maybe a -> [a]


#### Left: side of X object

 maybeToList :: Maybe Int -> [Int] Nothing [] Just 0  Just 1 

#### Right: side of Y object

 maybeToList :: Maybe Bool -> [Bool] Nothing [] Just True [True] Just False [False]

### Horizontal arrows: sides of functors $f : X \to Y$
 even :: Int -> Bool


#### Side of $\Phi$ functor

 fmap even:: Maybe Int -> Maybe Bool Nothing Nothing Just 0 Just True Just 1 Just False

#### Side of $\Psi$ functor

 map even:: [Int] -> [Bool] [] []  [True]  [False]

### Commutativity of the diagram $\Psi(f) \cdot \eta_X = \eta_Y \cdot \Phi(f)$

both paths span between $\Phi(X) \to \Psi(Y)$
 Maybe Int -> [Bool] map even . maybeToList maybeToList . fmap even Nothing [] [] Just 0 [True] [True] Just 1 [False] [False]

### Remarks

• even has a more general type (Integral a => a -> Bool) than described here
• Words “side”, “horizontal”, “vertical”, “left”, “right” serve here only to point to the discussed parts of a diagram, thus, they are not part of the scientific terminology.
• If You want to modify the commutative diagram, see its source code (in LaTeX using amscd).

## Operations

### Mixed

The “mixed” operations described below will be important also in understanding the definition of “monad” concept in category theory.

#### Functor and natural transformation

Let us imagine a parser library, which contains functions for parsing a form. There are two kinds of cells:

• containing data which are optional (e.g. name of spouse)
• containing data which consist of an enumeration of items (e.g. names of acquired languages)
 spouse :: Parser (Maybe String)
languages :: Parser [String]


Let us imagine we have any processing (storing, archiving etc.) function which processes lists (or any other reason which forces us to convert our results to list format and exclude any Maybe's). (Perhaps, all this example is impractical and exaggerated, because in real life we should solve the whole thing in other ways.)

Thus, we want to build a parser combinator (we could notate it graphically with something like $\mathrm?\!\!\to\!\!\mathrm*$) which converts a “zero-ore-one-occurrence” like parser to a “zero-or-one-or-many-occurrences” like parser.

We can convert Maybe to list with maybeToList But if we want to do something similar with a parser on Maybe's to achieve a parser on list, then maybeToList is not enough alone, we must fmap it. E.g. if we want to convert a parser like spouse to be of the same type as languages:

 fmap maybeToList spouse


Let us see the types: We start with

 spouse :: Parser (Maybe String) $\Lambda(\Phi(X))$

or using notion of composing functors $(\Lambda \Phi)(X)$

We want to achieve

 fmap maybeToList spouse :: Parser [String] $\Lambda(\Psi(X))$ $(\Lambda \Psi)(X)$

thus we can infer

 fmap maybeToList :: Parser (Maybe [String]) -> Parser [String] $(\Lambda\eta)_X \in \mathrm{Hom}_{\mathcal D}((\Lambda\Phi)(X),\;(\Lambda\Psi)(X))$

In fact, we have a new “datatype converter”: converting not Maybe's to lists, but parser on Maybe to Parser on list. Let us notate the corresponding natural transformation with $\Lambda\eta$:

To each $X \in \mathbf{Ob}(\mathcal C)$ we associate $(\Lambda\eta)_X \in \mathrm{Hom}_{\mathcal D}((\Lambda\Phi)(X),\;(\Lambda\Psi)(X))$ $\Lambda\eta : \Lambda\Phi \to \Lambda\Psi$ $(\Lambda\eta)_X = \Lambda(\eta_X)$

Summary:

Let $\mathcal C, \mathcal D, \mathcal E$ be categories $\Phi, \Psi : \mathcal C \to \mathcal D$ functors $\Lambda : \mathcal D \to \mathcal E$ functor $\eta : \Phi \to \Psi$ natural transformation

Then let us define a new natural transformation: $\Lambda\eta : \Lambda\Phi \to \Lambda\Psi$ $(\Lambda\eta)_X = \Lambda(\eta_X)$

#### Natural transformation and functor

Let $\mathcal C, \mathcal D, \mathcal E$ be categories $\Delta : \mathcal C \to \mathcal D$ functor $\Phi, \Psi : \mathcal D \to \mathcal E$ functors $\eta : \Phi \to \Psi$ natural transformation

Then let us define a new natural transformation: $\eta\Delta : \Phi\Delta \to \Psi\Delta$ $(\eta\Delta)_X = \eta_{\Delta(X)}$

It can be illustrated by Haskell examples, too. Understanding it is made harder (easier?) by the fact that Haskell's type inference “(dis)solves” the main point, thus there is no “materialized” manifestation of it.

convert :: Maybe (Term a) -> [Term a]


Unlike seen at $\mathrm?\!\!\to\!\!\mathrm*$, the definition of this converter will not show much novelty:

convert = maybeToList


the most interesting thing is done automatically by type inference.