Add section on functors and natural transformations

Also do not use ugly overbar
This commit is contained in:
Frederik Hanghøj Iversen 2018-05-22 13:45:52 +02:00
parent 2fce963072
commit 01159930de
4 changed files with 176 additions and 67 deletions

View file

@ -1261,6 +1261,55 @@ That in any category:
\prod_{A\ B \tp \Object} \isProp\ (\var{Product}\ \bC\ A\ B) \prod_{A\ B \tp \Object} \isProp\ (\var{Product}\ \bC\ A\ B)
\end{align} \end{align}
% %
\section{Functors and natural transformations}
For the sake of completeness I will mention the definition of functors
and natural transformations. Please refer to the implementation for
the full details.
%
\subsection{Functors}
Given two categories $\bC$ and $\bD$ a functor consists of the
following data:
%
\begin{align*}
\omapF & \tp .\Object𝔻.\Object \\
\fmap & \tp .\Arrow\ A\ B → 𝔻.\Arrow\ (\omapF\ A)\ (\omapF\ B)
\end{align*}
%
And the following laws:
\begin{align*}
\fmap\ .\identity &𝔻.identity \\
\fmap\ (g \clll f) &\fmap\ g \dlll \fmap\ f
\end{align*}
%
The implementation can be found here:
%
\begin{center}
\sourcelink{Cat.Category.Functor}
\end{center}
\subsection{Natural Transformation}
Given two functors between categories $\bC$ and $\bD$. Name them
$\FunF$ and $\FunG$. A natural transformation is a family of arrows:
%
\begin{align*}
\prod_{C \tp .\Object} \bD.\Arrow\ (\omapF\ C)\ (\omapG\ C)
\end{align*}
%
This family of arrows can be seen as the data. If $\theta$ is a
natural transformation $\theta\ C$ will be called the component (of
$\theta$) at $C$. The laws of this family of morphism is the
naturality condition:
%
\begin{align*}
\prod_{f \tp .\Arrow\ A\ B}
\ B) \dlll (\FunF.\fmap\ f) ≡ (\FunG.\fmap\ f) \dlll\ A)
\end{align*}
%
The implementation can be found here:
%
\begin{center}
\sourcelink{Cat.Category.NaturalTransformation}
\end{center}
\section{Monads} \section{Monads}
\label{sec:monads} \label{sec:monads}
In this section I present two formulations of monads. The two representations In this section I present two formulations of monads. The two representations
@ -1269,8 +1318,14 @@ simply monoidal monads and Kleisli monads for short. We then show that the two
formulations are equivalent, which due to univalence gives us a path between the formulations are equivalent, which due to univalence gives us a path between the
two types. two types.
Let a category $\bC$ be given. In the remainder of this sections all objects and Let a category $\bC$ be given. In the remainder of this sections all
arrows will implicitly refer to objects and arrows in this category. objects and arrows will implicitly refer to objects and arrows in this
category. I will also use the notation $\EndoR$ to refer to an
endofunctor on this category. Its map on objects will be denoted
$\omapR$ and its map on arrows will be denoted $\fmap$. Likewise I
will use the notation $\pureNT$ to refer to a natural transformation
and its component at a given (implicit) object will be denoted
$\pure$.
% %
\subsection{Monoidal formulation} \subsection{Monoidal formulation}
The monoidal formulation of monads consists of the following data: The monoidal formulation of monads consists of the following data:
@ -1278,7 +1333,7 @@ The monoidal formulation of monads consists of the following data:
\begin{align} \begin{align}
\label{eq:monad-monoidal-data} \label{eq:monad-monoidal-data}
\begin{split} \begin{split}
\EndoR & \tp \Endo \\ \EndoR & \tp \Functor\ \ \bC \\
\pureNT & \tp \NT{\EndoR^0}{\EndoR} \\ \pureNT & \tp \NT{\EndoR^0}{\EndoR} \\
\joinNT & \tp \NT{\EndoR^2}{\EndoR} \joinNT & \tp \NT{\EndoR^2}{\EndoR}
\end{split} \end{split}
@ -1321,8 +1376,15 @@ The Kleisli-formulation consists of the following data:
\end{split} \end{split}
\end{align} \end{align}
% %
The objects $X$ and $Y$ are implicitly universally quantified. The objects $X$ and $Y$ are implicitly universally quantified. With this data we can construct the \nomenindex{Kleisli arrow}:
%
\begin{align*}
\fish & \tp \Arrow\ A\ (\omapR\ B)
\to \Arrow\ B\ (\omapR\ C)
\to \Arrow\ A\ (\omapR\ C) \\
f \fish g & \defeq f \rrr (\bind\ g)
\end{align*}
%
It is interesting to note here that this formulation does not talk about natural It is interesting to note here that this formulation does not talk about natural
transformations or other such constructs from category theory. All we have here transformations or other such constructs from category theory. All we have here
is a regular maps on objects and a pair of arrows. is a regular maps on objects and a pair of arrows.
@ -1339,11 +1401,10 @@ This data must satisfy:
\end{align} \end{align}
\newcommand\kleislilaws{\ref{eq:monad-kleisli-laws-0}, \ref{eq:monad-kleisli-laws-1} and \ref{eq:monad-kleisli-laws-2}}% \newcommand\kleislilaws{\ref{eq:monad-kleisli-laws-0}, \ref{eq:monad-kleisli-laws-1} and \ref{eq:monad-kleisli-laws-2}}%
% %
Here likewise the arrows $f \tp \Arrow\ X\ (\EndoR\ Y)$ and $g \tp Here likewise the arrows $f \tp \Arrow\ X\ (\omapR\ Y)$ and $g \tp
\Arrow\ Y\ (\EndoR\ Z)$ are universally quantified (as well as the objects they \Arrow\ Y\ (\omapR\ Z)$ are universally quantified as well as the
range over). $\fish$ is the Kleisli-arrow which is defined as $f \fish g \defeq objects they range over.
f \rrr (\bind\ g)$ . (\TODO{Better way to typeset $\fish$?}) %
\subsection{Equivalence of formulations} \subsection{Equivalence of formulations}
% %
The notation I have chosen here in the report The notation I have chosen here in the report
@ -1376,27 +1437,27 @@ show that $(\omapR, \pure, \bind)$ is indeed a monad in the Kleisli
form. In the second part we will show the other direction. form. In the second part we will show the other direction.
\subsubsection{Monoidal to Kleisli} \subsubsection{Monoidal to Kleisli}
Let $(\EndoR, \pure, \join)$ be given as in \ref{eq:monad-monoidal-data} Let $(\EndoR, \pureNT, \joinNT)$ be given as in \ref{eq:monad-monoidal-data}
satisfying the laws \monoidallaws. For the data of the Kleisli satisfying the laws \monoidallaws. For the data of the Kleisli
formulation we pick: formulation we pick:
% %
\begin{align} \begin{align}
\begin{split} \begin{split}
\EndoR & \defeq \EndoRX \\ \omapR & \defeq \omapR \\
\pure & \defeq \pureX \\ \pure & \defeq \pure \\
\bind\ f & \tp \joinX \lll \fmapX\ f \bind\ f & \defeq \join \lll \fmap\ f
\end{split} \end{split}
\end{align} \end{align}
% %
$\EndoRX$ is the object map of the endo-functor $\EndoR$, $\pureX$ and Again $\omapR$ is the object map of the endo-functor $\EndoR$, $\pure$
$\joinX$ are the arrows from the natural transformations $\pure$ and and $\join$ are the arrows from the natural transformations $\pureNT$
$\join$ respectively. The term $\fmapX$ is the arrow map of the and $\joinNT$ respectively and $\fmap$ is the map on arrows of the
endo-functor $\EndoR$. It now just remains to verify the laws endofunctor $\EndoR$. It now just remains to verify the laws
\kleislilaws. For \ref{eq:monad-kleisli-laws-0}: \kleislilaws. For \ref{eq:monad-kleisli-laws-0}:
% %
\begin{align*} \begin{align*}
\bind\ \pure & \bind\ \pure & =
\join \lll (\fmap\ \pure) && \text{By definition} \\ \join \lll (\fmap\ \pure) \\
&\identity && \text{By \ref{eq:monad-monoidal-laws-2}} &\identity && \text{By \ref{eq:monad-monoidal-laws-2}}
\end{align*} \end{align*}
% %
@ -1404,57 +1465,89 @@ For \ref{eq:monad-kleisli-laws-1}:
% %
\begin{align*} \begin{align*}
\pure \fish f \pure \fish f
& \equiv %%% & = %%%
\pure \ggg \bind\ f && \text{By definition} \\ & \pure \ggg \bind\ f \\ & =
\bind\ f \lll \pure && \text{By definition} \\ & \bind\ f \lll \pure \\ & =
\joinX \lll \fmapX\ f \lll \pureX && \text{By definition} \\ & \join \lll \fmap\ f \lll \pure \\ &
\joinX \lll \pureX \lll f && \text{$\pure$ is a natural transformation} \\ & \join \lll \pure \lll f && \text{$\pure$ is a natural transformation} \\ &
\identity \lll f && \text{By \ref{eq:monad-monoidal-laws-1}} \\ & \identity \lll f && \text{By \ref{eq:monad-monoidal-laws-1}} \\ &
f && \text{Left identity} f && \text{Left identity}
\end{align*} \end{align*}
% %
For \ref{eq:monad-kleisli-laws-2}: For \ref{eq:monad-kleisli-laws-2}:
\begin{align*} \begin{align*}
\bind\ g \rrr \bind\ f & \bind\ g \rrr \bind\ f & =
\bind\ f \lll \bind\ g \bind\ f \lll \bind\ g
\\ & \\ & =
%% %%%% %% %%%%
\joinX \lll \fmapX\ g \lll \joinX \lll \fmapX\ f \join \lll \fmap\ g \lll \join \lll \fmap\ f
&& \text{\dots} \\ & \\ &
\joinX \lll \joinX \lll \fmapX^2\ g \lll \fmapX\ f \join \lll \join \lll (\fmap \comp \fmap)\ f \lll \fmap\ g
&& \text{$\join$ is a natural transformation} \\ & && \text{$\join$ is a natural transformation} \\ &
\joinX\ \lll \fmapX\ \joinX \lll \fmapX^2\ g \lll \fmapX\ f \join \lll \fmap\ \join \lll (\fmap \comp \fmap)\ f \lll \fmap\ g
&& \text{By \ref{eq:monad-monoidal-laws-0}} \\ & && \text{By \ref{eq:monad-monoidal-laws-0}} \\ &
\joinX\ \lll \fmapX\ \joinX\ \lll \fmapX\ (\fmapX\ g) \lll \fmapX\ f \join \lll \fmap\ \join \lll \fmap\ (\fmap\ f) \lll \fmap\ g
&& \text{} \\ & && \text{} \\ &
\joinX \lll \fmapX\ (\joinX \lll \fmapX\ g \lll f) \join \lll \fmap\ (\join \lll \fmap\ f \lll g)
&& \text{Distributive law for functors} \\ & \equiv && \text{Distributive law for functors} \\ & =
\join \lll \fmap\ (\join \lll \fmap\ f \lll g) \\ & =
%%%% %%%%
\bind\ (\bind\ f \lll g) \\ & =
\bind\ (g \rrr \bind\ f) \\ & =
\bind\ (g \fish f) \bind\ (g \fish f)
\end{align*} \end{align*}
%
The construction can be found in the module:
\begin{center}
\sourcelink{Cat.Category.Monad.Monoidal}
\end{center}
%
\subsubsection{Kleisli to Monoidal} \subsubsection{Kleisli to Monoidal}
For the other direction we are given $(\EndoR, \pure, \bind)$ as in For the other direction we are given $(\omapR, \pure, \bind)$ as in
\ref{eq:monad-kleisli-data} satisfying the laws \kleislilaws. For the data of \ref{eq:monad-kleisli-data} satisfying the laws \kleislilaws. For the data of
the monoidal formulation we pick: the monoidal formulation we pick:
% %
\begin{align} \begin{align}
\begin{split} \begin{split}
\EndoR & \defeq \EndoRX \\ \EndoR & \defeq (\omapR, \bind\ (\pure \lll f)) \\
\pure & \defeq \pureX \\ \pure & \defeq \pure \\
\join & \defeq \bind\ \identity \join & \defeq \bind\ \identity
\end{split} \end{split}
\end{align} \end{align}
% %
Where $\EndoRX \defeq (\bind\ (\pure \lll f), \EndoR)$ and $\pureX \defeq We must now not only show the monad laws given for the monoidal
\bind\ \identity$. We must now show the laws \monoidallaws, but we must also formulation (\monoidallaws), we must also verify that $\EndoR$ is a
verify that our choice of $\EndoRX$ actually is a functor. I will ommit this functor and that $\pure$ and $\join$ are natural transformations. I
here. In stead we shall see how these two mappings are indeed inverses. will ommit this here. In stead we shall see how these two mappings are
indeed inverses. The full construction can be found in the module:
\begin{center}
\mbox{\sourcelink{Cat.Category.Monad.Kleisli}}
\end{center}
%
\subsubsection{Equivalence} \subsubsection{Equivalence}
To prove that the two formulations are equivalent we must demonstrate that the To prove that the two formulations are equivalent we must demonstrate
two mappings sketched above are indeed inverses of each other. If we name the that the two mappings sketched above are indeed inverses of each
first mapping $\toKleisli$ and it's proposed inverse $\toMonoidal$ other. To recap, these maps are:
then we must show: %
\begin{align*}
\toKleisli & \tp \var{Kleisli} \to \var{Monoidal} \\
\toKleisli & \defeq \lambda\ (\omapR, \pure, \bind)
\to (\EndoR, \pure, \bind\ \identity)
\end{align*}
%
Where $\EndoR \defeq (\omapR, \bind\ (\pure \lll f))$. The proof that
this is indeed a functor is left implicit as well as the monad laws.
Likewise the proof that $\pure$ and $\bind\ \identity$ are natural
transformations are left implicit. The inverse map will be:
%
\begin{align*}
\toMonoidal & \tp \var{Monoidal} \to \var{Kleisli} \\
\toMonoidal & \defeq \lambda\ (\EndoR, \pureNT, \joinNT)
\to (\omapR, \pure, \bind)
\end{align*}
%
Where $\bind\ f \defeq \join \lll \fmap\ f$. Again the monad laws are
left implicit. Now we must show:
% %
\begin{align} \begin{align}
\label{eq:monad-forwards} \label{eq:monad-forwards}
@ -1463,30 +1556,37 @@ then we must show:
\toMonoidal \comp \toKleisli &\identity \toMonoidal \comp \toKleisli &\identity
\end{align} \end{align}
% %
For \ref{eq:monad-forwards} let $(\EndoR, \pure, \join)$ be a monad in the For \ref{eq:monad-forwards} let $(\omapR, \pure, \bind)$ be a monad in
monoidal form. In my formulation the proof that being-a-monad is a proposition the Kleisli form. Since being-a-monad is a proposition\footnote{The
can be found. With this result in place we get an equality principle for proof can be found in the implementation.} we get an
kleisli-monads that say that to equate two such monads it suffices to equate equality-principle for kleisli-monads that say that to equate two such
their data-part. So it suffices to equate the data-parts of the monads it suffices to equate their data-part. So it suffices to equate
\ref{eq:monad-forwards}. Such a proof is a triple equation the three projections the data-parts of the \ref{eq:monad-forwards}. Such a proof is a
of \ref{eq:monad-kleisli-data}. The first two hold definitionally -- essentially triple equating the three projections of \ref{eq:monad-kleisli-data}.
one just wraps and unwraps the morphism in a functor. For the last equation a The first two hold definitionally -- essentially one just wraps and
little more work is required: unwraps the morphism in a functor. For the last equation a little more
work is required:
% %
\begin{align*} \begin{align*}
\join \lll \fmap\ f & \join \lll \fmap\ f & =
\fmap\ f \rrr \join \\ & \fmap\ f \rrr \join \\ & =
\bind\ (f \rrr \pure) \rrr \bind\ \identity \bind\ (f \rrr \pure) \rrr \bind\ \identity
&& \text{By definition of $\fmap$ and $\join$} \\ & && \text{By definition of $\fmap$ and $\join$} \\ &
\bind\ (f \rrr \pure \fish \identity) \bind\ (f \rrr \pure \fish \identity)
&& \text{By \ref{eq:monad-kleisli-laws-2}} \\ & && \text{By \ref{eq:monad-kleisli-laws-2}} \\ &
\bind\ (f \rrr \identity) \bind\ (f \rrr \identity)
&& \text{By \ref{eq:monad-kleisli-laws-1}} \\ & && \text{By \ref{eq:monad-kleisli-laws-1}} \\ & =
\bind\ f \bind\ f
\end{align*} \end{align*}
% %
For the other direction we can likewise verify that the maps $\EndoR$, $\bind$, For the other direction (\ref{eq:monad-backwards}) we are given a
$\join$, and $\fmap$ are equal. The equality principle for functors gives us monad in the monoidal form; $(\EndoR, \pureNT, \joinNT)$. The various
that this is enough to show that the the functor $\EndoR$ we construct is equality-principles again give us that it is sufficient to equate the
identical. Similarly for the natural transformations we have that the naturality data-part of the above. That is, we only need to verify that the
condition is a proposition so the paths between the maps are sufficient. following pieces of data: $\omapR$, $\fmap$, $\pure$ and $\join$ get
mapped correctly. To see the full details check the implementation in
the module:
%
\begin{center}
\sourcelink{Cat.Category.Monad}
\end{center}

View file

@ -9,8 +9,9 @@
%% Alternatively: %% Alternatively:
%% \newcommand{\defeq}{} %% \newcommand{\defeq}{}
\newcommand{\bN}{\mathbb{N}} \newcommand{\bN}{\mathbb{N}}
\newcommand{\bC}{\mathbb{C}} \newcommand{\bC}{}
\newcommand{\bX}{\mathbb{X}} \newcommand{\bD}{𝔻}
\newcommand{\bX}{𝕏}
% \newcommand{\to}{\rightarrow} % \newcommand{\to}{\rightarrow}
%% \newcommand{\mto}{\mapsto} %% \newcommand{\mto}{\mapsto}
\newcommand{\mto}{\rightarrow} \newcommand{\mto}{\rightarrow}
@ -99,6 +100,10 @@
\newcommand\Endo[1]{\varindex{Endo}\ #1} \newcommand\Endo[1]{\varindex{Endo}\ #1}
\newcommand\EndoR{\functor{\mathcal{R}}} \newcommand\EndoR{\functor{\mathcal{R}}}
\newcommand\omapR{\mathcal{R}} \newcommand\omapR{\mathcal{R}}
\newcommand\omapF{\mathcal{F}}
\newcommand\omapG{\mathcal{G}}
\newcommand\FunF{\functor{\omapF}}
\newcommand\FunG{\functor{\omapG}}
\newcommand\funExt{\varindex{funExt}} \newcommand\funExt{\varindex{funExt}}
\newcommand{\suc}[1]{\varindex{suc}\ #1} \newcommand{\suc}[1]{\varindex{suc}\ #1}
\newcommand{\trans}{\varindex{trans}} \newcommand{\trans}{\varindex{trans}}
@ -117,3 +122,5 @@
} }
\newcommand{\gitlink}{\hrefsymb{\docbasepath}{\texttt{\docbasepath}}} \newcommand{\gitlink}{\hrefsymb{\docbasepath}{\texttt{\docbasepath}}}
\newcommand{\doclink}{\hrefsymb{\sourcebasepath}{\texttt{\sourcebasepath}}} \newcommand{\doclink}{\hrefsymb{\sourcebasepath}{\texttt{\sourcebasepath}}}
\newcommand{\clll}{\mathrel{\bC.\mathord{\lll}}}
\newcommand{\dlll}{\mathrel{\bD.\mathord{\lll}}}

View file

@ -140,3 +140,4 @@
-- (1, 1) -- (1, 0.6); -- (1, 1) -- (1, 0.6);
} }
} }
\usepackage{ dsfont }

View file

@ -63,7 +63,8 @@ module _ {a b : Level} ( : Category a b) where
f f
Kleisli.IsMonad.isDistributive toKleisliIsMonad f g = begin Kleisli.IsMonad.isDistributive toKleisliIsMonad f g = begin
bind g >>> bind f ≡⟨⟩ bind g >>> bind f ≡⟨⟩
(join <<< fmap g) >>> (join <<< fmap f) ≡⟨ isDistributive f g (join <<< fmap f) <<< (join <<< fmap g) ≡⟨ isDistributive f g
join <<< fmap (join <<< fmap f <<< g) ≡⟨⟩
bind (g >=> f) bind (g >=> f)
-- Kleisli.IsMonad.isDistributive toKleisliIsMonad = isDistributive -- Kleisli.IsMonad.isDistributive toKleisliIsMonad = isDistributive