work on prelims and partiality

This commit is contained in:
Leon Vatthauer 2024-02-27 18:57:32 +01:00
parent be09d767ea
commit 76c145207a
Signed by: leonv
SSH key fingerprint: SHA256:G4+ddwoZmhLPRB1agvXzZMXIzkVJ36dUYZXf5NxT+u8
5 changed files with 194 additions and 224 deletions

View file

@ -60,3 +60,4 @@ epis
isos
Corecursion
subobject
isomorphisms

View file

@ -105,6 +105,16 @@
publisher = {Citeseer}
}
@article{lambek,
title = {A fixpoint theorem for complete categories},
author = {Lambek, Joachim},
journal = {Mathematische Zeitschrift},
volume = {103},
pages = {151--161},
year = {1968},
publisher = {Springer}
}
@inproceedings{Lane1971,
title = {Categories for the Working Mathematician},
author = {Saunders Mac Lane},
@ -256,3 +266,12 @@
biburl = {https://dblp.org/rec/journals/corr/GoncharovSR14.bib},
bibsource = {dblp computer science bibliography, https://dblp.org}
}
@article{manes,
title={Algebraic Theories in a Category},
author={Manes, Ernest G},
journal={Algebraic Theories},
pages={161--279},
year={1976},
publisher={Springer}
}

View file

@ -1,4 +1,10 @@
\chapter{Introduction}
\info[inline]{use introductory example from talk}
\todo[inline]{give overview of thesis}
\section{Motivation}
\info[inline]{Use introductory example from talk}
\section{Type Theory}
\unsure[inline]{Talk about (co-) induction?}
\todo[inline]{Give overview of thesis}

View file

@ -1,7 +1,5 @@
\chapter{Preliminaries}
\todo[inline]{Also note that we require some knowledge with type theory}
We assume familiarity with basic categorical notions, in particular: categories, functors, functor algebras and natural transformations, as well as special objects like (co-)products, terminal and initial objects and special morphisms like isos, epis and monos.
In this chapter we will introduce notation that will be used throughout the thesis and also introduce some notions that are crucial to this thesis in more detail.
We write $\obj{C}$ for the objects of a category $\C$, $id_X$ for the identity morphism on $X$, $(-) \circ (-)$ for the composition of morphisms and $\C(X,Y)$ for the set of morphisms between $X$ and $Y$.
@ -95,62 +93,64 @@ Categories with finite products (i.e.\ binary products and a terminal object) ar
\end{proof}
\begin{proposition}
The distribution morphisms satisfy the following equations:
The distribution morphisms satisfy the following properties:
\begin{alignat}{2}
&dstl \circ (id \times i_1) &&= i_1
\\&dstl \circ (id \times i_2) &&= i_2
\\&[ \pi_1 , \pi_1 ] \circ dstl &&= \pi_1
\\&( \pi_2 + \pi_2 ) \circ dstl &&= \pi_2
\\&dstl \circ swap &&= (swap + swap) \circ dstr
\\&dstr \circ (i_1 \times id) &&= i_1
\\&dstr \circ (i_2 \times id) &&= i_2
\\&(\pi_1 + \pi_1) \circ dstr &&= \pi_1
\\&[ \pi_2 , \pi_2 ] \circ dstr &&= \pi_2
\\&dstr \circ swap &&= (swap + swap) \circ dstl
\end{alignat}
\begin{enumerate}
\item $dstl \circ (id \times i_1) = i_1$
\item $dstl \circ (id \times i_2) = i_2$
\item $[ \pi_1 , \pi_1 ] \circ dstl = \pi_1$
\item $( \pi_2 + \pi_2 ) \circ dstl = \pi_2$
\item $dstl \circ swap = (swap + swap) \circ dstr$
\item $dstr \circ (i_1 \times id) = i_1$
\item $dstr \circ (i_2 \times id) = i_2$
\item $(\pi_1 + \pi_1) \circ dstr = \pi_1$
\item $[ \pi_2 , \pi_2 ] \circ dstr = \pi_2$
\item $dstr \circ swap = (swap + swap) \circ dstl$
\end{enumerate}
\end{proposition}
\begin{proof}
Let us verify the five equations concerning $dstl$, the ones concerning $dstr$ follow symmetrically:
\begin{alignat*}{1}
&dstl \circ (id \times i_1)
\\=\;&dstl \circ [ id \times i_1 , id \times i_2 ] \circ i_1
\\=\;&dstl \circ dstl^{-1} \circ i_1
\\=\;&i_1
\end{alignat*}
\begin{alignat*}{1}
&dstl \circ (id \times i_2)
\\=\;&dstl \circ [ id \times i_1 , id \times i_2 ] \circ i_2
\\=\;&dstl \circ dstl^{-1} \circ i_2
\\=\;&i_2
\end{alignat*}
The next two follow by the fact that $dstl^{-1}$ is epic:
\begin{alignat*}{1}
&\pi_1 \circ dstl^{-1}
\\=\;&[ \pi_1 \circ (id \times i_1) , \pi_1 \circ (id \times i_2) ]
\\=\;&[ \pi_1 , \pi_1 ]
\end{alignat*}
\begin{alignat*}{1}
&\pi_2 \circ dstl^{-1}
\\=\;& [ \pi_2 \circ (id \times i_1) , \pi_2 \circ (id \times i_2) ]
\\=\;& \pi_2 + \pi_2
\end{alignat*}
And by monicity of $dstl^{-1}$:
\begin{alignat*}{1}
&dstl^{-1} \circ (swap + swap) \circ dstl
\\=\;&[ (id \times i_1) \circ swap, (id \times i_2) \circ swap ] \circ dstl
\\=\;&[ swap \circ (i_1 \times id) , swap \circ (i_2 \times id) ] \circ dstl
\\=\;&swap \circ dstl^{-1} \circ dstl
\\=\;&swap
\end{alignat*}
Let us verify the five properties concerning $dstl$, the ones concerning $dstr$ follow symmetrically:
\begin{enumerate}
\item
\begin{alignat*}{1}
&dstl \circ (id \times i_1)
\\=\;&dstl \circ [ id \times i_1 , id \times i_2 ] \circ i_1
\\=\;&dstl \circ dstl^{-1} \circ i_1
\\=\;&i_1
\end{alignat*}
\item
\begin{alignat*}{1}
&dstl \circ (id \times i_2)
\\=\;&dstl \circ [ id \times i_1 , id \times i_2 ] \circ i_2
\\=\;&dstl \circ dstl^{-1} \circ i_2
\\=\;&i_2
\end{alignat*}
\item
\begin{alignat*}{1}
&\pi_1
\\=\;&\pi_1 \circ dstl^{-1} \circ dstl
\\=\;&[ \pi_1 \circ (id \times i_1) , \pi_1 \circ (id \times i_2) ] \circ dstl
\\=\;&[ \pi_1 , \pi_1 ] \circ dstl
\end{alignat*}
\item
\begin{alignat*}{1}
&\pi_2
\\=\;&\pi_2 \circ dstl^{-1} \circ dstl
\\=\;&[ \pi_2 \circ (id \times i_1) , \pi_2 \circ (id \times i_2) ] \circ dstl
\\=\;&(\pi_2 + \pi_2) \circ dstl
\end{alignat*}
\item
\begin{alignat*}{1}
&dstl \circ swap
\\=\;&dstl \circ swap \circ dstr^{-1} \circ dstr
\\=\;&dstl \circ [ swap \circ (i_1 \times id) , swap \circ (i_2 \times id) ] \circ dstr
\\=\;&dstl \circ [ (id \times i_1) \circ swap , (id \times i_2) \circ swap) ] \circ dstr
\\=\;&dstl \circ [ id \times i_1 , id \times i_2 ] \circ (swap + swap) \circ dstr
\\=\;&dstl \circ dstl^{-1} \circ (swap + swap) \circ dstr
\\=\;&(swap + swap) \circ dstr
\end{alignat*}
\end{enumerate}
\end{proof}
\begin{definition}[Exponential Object]
@ -174,7 +174,7 @@ The internal logic of cartesian closed categories is the simply typed $\lambda$-
Let $F : \C \rightarrow \C$ be an endofunctor. Recall that F-algebras are tuples $(X, \alpha : FX \rightarrow X)$ consisting of an object of $\C$ and a morphism out of the functor. Initial F-algebras have been studied extensively as a means of modelling inductive data types together with induction and recursion principles~\cite{inductive}. For this thesis we will be more interested in the dual concept namely terminal coalgebras; let us formally introduce them now.
\begin{definition}[F-Coalgebra]
A tuple $(X \in \obj{\C}, \alpha : X \rightarrow FX)$ is called a terminal F-coalgebra.
A tuple $(X \in \obj{\C}, \alpha : X \rightarrow FX)$ is called an F-coalgebra.
\end{definition}
\begin{definition}[Morphisms between Coalgebras]\label{def:coalgmorph}
@ -225,11 +225,11 @@ The terminal object of $Coalg(F)$ is sometimes called \textit{final F-coalgebra}
\end{tikzcd}\]
\end{definition}
\begin{lemma}[Lambek's Lemma]
\begin{lemma}[Lambek's Lemma~\cite{lambek}]
Let $(T, t : T \rightarrow FT)$ be a terminal F-coalgebra. Then $t$ is an isomorphism.
\end{lemma}
\begin{proof}
Note that $(FT, Ft : FT \rightarrow FFT)$ is also an F-coalgebra. This yields the unique morphism $\coalg{Ft} : FT \rightarrow T$ satisfying:
First note that $(FT, Ft : FT \rightarrow FFT)$ is also an F-coalgebra. This yields the unique morphism $\coalg{Ft} : FT \rightarrow T$ satisfying:
% https://q.uiver.app/#q=WzAsNCxbMCwwLCJGVCJdLFsyLDAsIkZGVCJdLFswLDIsIlQiXSxbMiwyLCJGVCJdLFswLDEsIkZ0Il0sWzIsMywidCJdLFswLDIsIlxcbGxicmFja2V0IEZ0IFxccnJicmFja2V0IiwyLHsic3R5bGUiOnsiYm9keSI6eyJuYW1lIjoiZGFzaGVkIn19fV0sWzEsMywiRlxcbGxicmFja2V0IEZ0IFxccnJicmFja2V0Il1d
\[\begin{tikzcd}[ampersand replacement=\&]
FT \&\& FFT \\
@ -264,98 +264,6 @@ The terminal object of $Coalg(F)$ is sometimes called \textit{final F-coalgebra}
\end{enumerate}
\end{proof}
\section{Adjunctions and Free Objects}
\todo[inline]{Add text}
\begin{definition}[Adjoint Functors]\label{def:adjoint}
Let $\C$ and $\D$ be two categories. Two functors $L : \C \rightarrow \D$ and $R : \D \rightarrow \C$ are called adjoint to each other if there exist natural transformations $\eta : Id \rightarrow R \circ L$ (called unit) and $\epsilon : L \circ R \rightarrow Id$ (called counit) such that the following diagrams commute:
% https://q.uiver.app/#q=WzAsNixbMCwwLCJMWCJdLFsyLDAsIkxSTFgiXSxbMiwxLCJMWCJdLFszLDAsIlJYIl0sWzUsMCwiUkxSWCJdLFs1LDEsIlJYIl0sWzAsMSwiTFxcZXRhIl0sWzEsMiwiXFxlcHNpbG9uIl0sWzAsMiwiaWQiLDJdLFs0LDUsIlJcXGVwc2lsb24iXSxbMyw0LCJcXGV0YSJdLFszLDUsImlkIiwyXV0=
\[\begin{tikzcd}[ampersand replacement=\&]
LX \&\& LRLX \& RX \&\& RLRX \\
\&\& LX \&\&\& RX
\arrow["L\eta", from=1-1, to=1-3]
\arrow["\epsilon", from=1-3, to=2-3]
\arrow["id"', from=1-1, to=2-3]
\arrow["R\epsilon", from=1-6, to=2-6]
\arrow["\eta", from=1-4, to=1-6]
\arrow["id"', from=1-4, to=2-6]
\end{tikzcd}\]
We write $L \dashv R$ and call $L$ the left adjoint of $R$.
\end{definition}
Free objects are constructions capturing the essence of structures in a minimal way, we will rely on free structures in chapter~\ref{chp:iteration} to define a monad in a general setting. We recall the definition to establish some notation:
\begin{definition}[Free Object]\label{def:free}
Let $\C, \D$ be categories and $U : \C \rightarrow \D$ be a forgetful functor (whose construction usually is obvious). A free object on some object $X \in \obj{\D}$ is an object $FX \in \obj{\C}$ together with a morphism $\eta : X \rightarrow UFX$ such that for any $Y \in \obj{\C}$ and $f : X \rightarrow UY$ there exists a unique morphism $\free{f} : FX \rightarrow UY$ satisfying:
% https://q.uiver.app/#q=WzAsMyxbMCwwLCJYIl0sWzEsMCwiVVkiXSxbMCwxLCJGWCJdLFswLDEsImYiXSxbMCwyLCJcXGV0YSIsMl0sWzIsMSwiXFxmcmVle2Z9IiwyLHsic3R5bGUiOnsiYm9keSI6eyJuYW1lIjoiZGFzaGVkIn19fV1d
\[\begin{tikzcd}[ampersand replacement=\&]
X \& UY \\
FX
\arrow["f", from=1-1, to=1-2]
\arrow["\eta"', from=1-1, to=2-1]
\arrow["{\free{f}}"', dashed, from=2-1, to=1-2]
\end{tikzcd}\]
\end{definition}
\begin{theorem}\label{thm:foladj}
Let $\C$ and $\D$ be two categories and let $U : \C \rightarrow \D$ be a forgetful functor between these categories.
Existence of all free objects on $\D$ yields a left adjoint to the forgetful functor that is called the free functor.
\end{theorem}
\begin{proof}
First we define the free functor $F : \D \rightarrow \C$. $F$ maps objects of $\D$ to the corresponding $FX$ in $\C$ (where $FX$ is the underlying object in Definition~\ref{def:free}).
For any morphism $f : X \rightarrow Y$ in $\D$ we take $Ff = \free{\eta \circ f}$.
This is a functor because:
\begin{enumerate}
\item The identity law:
\[Fid = \free{\eta \circ id} = \free{\eta} = id\]
follows by uniqueness of $\free{\eta}$ since $id$ satisfies $id \circ \eta = \eta$.
\item Let $f : X \rightarrow Y$ and $g : Y \rightarrow Z$. The compositional law:
\[F(g \circ f) = \free{\eta \circ g \circ f} = \free{\eta \circ g} \circ \free{\eta \circ f} = Fg \circ Ff \]
follows by uniqueness of $\free{\eta \circ g \circ f}$ since:
\[\free{\eta \circ g} \circ \free{\eta \circ f} \circ \eta = \free{\eta \circ g} \circ \eta \circ f = \eta \circ g \circ f\]
\end{enumerate}
We are left to show that $F$ is left adjoint to $U$. The free object $\eta$ extends to a family of morphisms $(\eta_X : X \rightarrow UFX)_{X \in \obj{D}}$ yielding the unit of the adjunction. For naturality of $\eta$ we need to check:
\[\eta \circ f = \free{\eta \circ f}\circ \eta = UFf \circ \eta\]
which follows by the universal property of free objects.
As the counit take $\epsilon = \free{id}: FU \rightarrow Id$. For naturality of $\epsilon$ we show:
\[\free{id}\circ FU f = \free{Uf}= f \circ \free{id }\]
where $f : X \rightarrow Y$.
Using uniqueness of $\free{Uf }$ we are done by:
\begin{alignat*}{1}
&U (\free{id}\circ FUf) \circ \eta\\
=\;&U \free{id}\circ UFUf \circ \eta\\
=\;&U \free{id}\circ \eta \circ Uf\\
=\;&id \circ Uf\\
=\;&Uf
\end{alignat*}
and
\begin{alignat*}{1}
&U (f \circ \free{id }) \circ \eta\\
=\;&Uf \circ U\free{id}\circ \eta\\
=\;&Uf \circ id\\
=\;&Uf
\end{alignat*}
Let us finally check the triangle identities. The first one can be proven via:
\[\epsilon \circ F\eta = \free{id}\circ \free{\eta \circ \eta}= \free{\eta}= id \]
where the only step missing is $\free{id}\circ \free{\eta \circ \eta}= \free{\eta}$ which holds since:
\begin{alignat*}{1}
&\free{id}\circ \free{\eta \circ \eta}\circ \eta\\
=\;&\free{id}\circ \eta \circ \eta\\
=\;&id \circ \eta\\
=\;&\eta
\end{alignat*}
The second triangle identity is a direct consequence of the universal property of free objects:
\[U\epsilon \circ \eta = U \free{id}\circ \eta = id\]
We have thereby proven that indeed $F \dashv U$.
\end{proof}
\section{Monads}
Monads are widely known among programmers as a way of modelling effects in pure languages and are also central to this thesis. Let us recall the basic definitions\cite{Lane1971}\cite{moggi}.
@ -386,85 +294,64 @@ Monads are widely known among programmers as a way of modelling effects in pure
\end{tikzcd}\]
\end{definition}
The following theorem makes the relationship of monads and adjoint functors apparent:
\begin{theorem}\label{thm:adjmon}
For every adjunction $L \dashv R$ the triple $(R \circ L, \eta, R\epsilon L)$ is a monad.
\end{theorem}
\begin{proof}
We check the monad laws:
\begin{itemize}
\item[\ref{M1}]
Follows by naturality of $\epsilon$:
\[ R\epsilon L \circ RLR\epsilon L = R(\epsilon L \circ LR\epsilon L) = R (\epsilon L \circ \epsilon L) = R\epsilon L \circ R\epsilon L\]
\item[\ref{M2}]
Follows by the second diagram in Definition~\ref{def:adjoint}:
\[R\epsilon L \circ \eta = id\]
\item[\ref{M3}] Follows by the first diagram in Definition~\ref{def:adjoint}:
\[R\epsilon L \circ RL\eta = R(\epsilon L \circ L\eta) = R(id) = id\]
\end{itemize}
\end{proof}
% Morphisms between monads are natural transformations that respect the monad operations:
Morphisms between monads are natural transformations that respect the monad operations:
\change[inline]{
Maybe remove the definition of Mon(C) and just introduce the category of pre-Elgot monads.
}
\begin{definition}[Monad Morphism]
A morphism between two monads $(S, \eta^S, \mu^S)$ and $(T, \eta^T, \mu^T)$ is a natural transformation $\alpha : S \rightarrow T$ between the underlying functors satisfying:
% https://q.uiver.app/#q=WzAsOCxbMCwwLCJYIl0sWzIsMCwiU1giXSxbMiwxLCJUWCJdLFs0LDAsIlNTWCJdLFs2LDAsIlNUWCJdLFs0LDEsIlNYIl0sWzYsMSwiVFRYIl0sWzUsMiwiVFgiXSxbMCwxLCJcXGV0YV5TIl0sWzEsMiwiXFxhbHBoYSJdLFswLDIsIlxcZXRhXlQiLDJdLFszLDQsIlNcXGFscGhhIl0sWzMsNSwiXFxtdV5TIiwyXSxbNCw2LCJcXGFscGhhIl0sWzUsNywiXFxhbHBoYSIsMl0sWzYsNywiXFxtdV5UIl1d
\[\begin{tikzcd}
X && SX && SSX && STX \\
&& TX && SX && TTX \\
&&&&& TX
\arrow["{\eta^S}", from=1-1, to=1-3]
\arrow["\alpha", from=1-3, to=2-3]
\arrow["{\eta^T}"', from=1-1, to=2-3]
\arrow["S\alpha", from=1-5, to=1-7]
\arrow["{\mu^S}"', from=1-5, to=2-5]
\arrow["\alpha", from=1-7, to=2-7]
\arrow["\alpha"', from=2-5, to=3-6]
\arrow["{\mu^T}", from=2-7, to=3-6]
\end{tikzcd}\]
\end{definition}
% \begin{definition}[Monad Morphism]
% A morphism between monads $(S, \eta^S, \mu^S)$ and $(T, \eta^T, \mu^T)$ is a natural transformation $\alpha : S \rightarrow T$ between the underlying functors satisfying:
% % https://q.uiver.app/#q=WzAsOCxbMCwwLCJYIl0sWzIsMCwiU1giXSxbMiwxLCJUWCJdLFszLDAsIlNTWCJdLFs1LDAsIlNUWCJdLFszLDEsIlNYIl0sWzcsMCwiVFRYIl0sWzcsMSwiVFgiXSxbMCwxLCJcXGV0YV5TIl0sWzEsMiwiXFxhbHBoYSJdLFswLDIsIlxcZXRhXlQiLDJdLFszLDQsIlNcXGFscGhhIl0sWzMsNSwiXFxtdV5TIiwyXSxbNCw2LCJcXGFscGhhIl0sWzUsNywiXFxhbHBoYSIsMl0sWzYsNywiXFxtdV5UIl1d
% \[\begin{tikzcd}[ampersand replacement=\&]
% X \&\& SX \& SSX \&\& STX \&\& TTX \\
% \&\& TX \& SX \&\&\&\& TX
% \arrow["{\eta^S}", from=1-1, to=1-3]
% \arrow["\alpha", from=1-3, to=2-3]
% \arrow["{\eta^T}"', from=1-1, to=2-3]
% \arrow["S\alpha", from=1-4, to=1-6]
% \arrow["{\mu^S}"', from=1-4, to=2-4]
% \arrow["\alpha", from=1-6, to=1-8]
% \arrow["\alpha"', from=2-4, to=2-8]
% \arrow["{\mu^T}", from=1-8, to=2-8]
% \end{tikzcd}\]
% \end{definition}
\begin{definition}[The Category of Monads]
Monads on a category $\C$ together with monad morphisms form a category that we call $\monads{\C}$. The identity morphism is the identity natural transformation that trivially respects the monad operations and composition of morphisms is composition of natural transformations.
\end{definition}
% \begin{definition}[The Category of Monads]
% Monads on a category $\C$ together with monad morphisms form a category that we call $\monads{\C}$. The identity morphism is the identity natural transformation that trivially respects the monad operations and composition of morphisms is composition of natural transformations.
% \end{definition}
\change[inline]{Change text}
For programmers a second equivalent definition is more useful:
% \improvement[inline]{Explain benefits of initial monad}
Monads can also be specified in a second equivalent way that is better suited to describe computation.
\begin{definition}[Kleisli Triple]
A Kleisli triple on a category $\C$ is a triple $(F, \eta, (-)^*)$, where $F : \obj{C} \rightarrow \obj{C}$ is a mapping on objects, $(\eta_X : X \rightarrow FX)_{X\in\obj{C}}$ is a family of morphisms and for every morphism $f : X \rightarrow FY$ there exists a morphism $f^* : FX \rightarrow FY$ called the Kleisli lifting, where the following laws hold:
\begin{alignat*}{2}
\begin{alignat*}{3}
&\eta_X^* &&= id_{FX} \tag*{(K1)}\label{K1}\\
&f^* \circ \eta_X &&= f \tag*{(K2)}\label{K2}\\
&f^* \circ g* &&= (f \circ g^*)^* \tag*{(K3)}\label{K3}
&f^* \circ \eta_X &&= f &&\text{ for any } f : X \rightarrow FY \tag*{(K2)}\label{K2}\\
&f^* \circ g* &&= (f^* \circ g)^* &&\text{ for any } f : Y \rightarrow FZ, g : X \rightarrow FY \tag*{(K3)}\label{K3}
\end{alignat*}
\end{definition}
\change[inline]{Rewrite this paragraph, it's not so good}
In functional programming languages like Haskell the Kleisli lifting $(-)^*$ is usually called \textit{bind} or written infix as \mintinline{haskell}{>>=} and $\eta$ is called $return$. Given two programs $f : X \rightarrow MY, g : Y \rightarrow MZ$, where $M$ is a Kleisli triple the composition $g^* \circ f$ can then be (in a pointful manner) written as \mintinline{haskell}{f x >>= g} or using Haskell's do-notation:
% In functional programming languages like Haskell the Kleisli lifting $(-)^*$ is usually called \textit{bind} or written infix as \mintinline{haskell}{>>=} and $\eta$ is called $return$. Given two programs $f : X \rightarrow MY, g : Y \rightarrow MZ$, where $M$ is a Kleisli triple the composition $g^* \circ f$ can then be (in a pointful manner) written as \mintinline{haskell}{f x >>= g} or using Haskell's do-notation:
Let $f : X \rightarrow TY, g : Y \rightarrow TZ$ be two programs, where $T$ is a Kleisli triple. These programs can be composed by taking: $f^* \circ g : X \rightarrow TZ$, which is called Kleisli composition. Haskell's do-notation is a useful tool for writing Kleisli composition in a legible way. $f^* \circ g$ can equivalently be expressed as:
\begin{minted}{haskell}
do y <- f x
g y
\end{minted}
This results in the following:
The possibility of Kleisli composition enables us to define the category of programs for any Kleisli triple $T$.
\begin{definition}[Kleisli Category]
Given a monad $T$ on a category $\C$, the Kleisli category $\C^T$ is defined as:
\begin{itemize}
\item $\vert \C^T \vert = \obj{C}$
\item $\mathcal{C^T}(X, Y) = \C(X, TY)$
\item Composition of $f : X \rightarrow TY$ and $g : Y \rightarrow TZ$ is defined as $f \circ_{\C^T} g = f^* \circ_{\C} g$.
\item Composition of programs is Kleisli composition.
\item The identity morphisms are the unit morphisms of $T$, $id_X = \eta_X : X \rightarrow TX$
\end{itemize}
The laws of categories then follow from the Kleisli triple laws, making this indeed a category.
The laws of categories then follow from the Kleisli triple laws.
\end{definition}
\begin{theorem}[\cite{moggi}] The notions of Kleisli triple and monad are equivalent.
\begin{theorem}[\cite{manes}] The notions of Kleisli triple and monad are equivalent.
\end{theorem}
\begin{proof}
The crux of this proof is defining the triples, the proofs of the corresponding laws (functoriality, naturality, monad and Kleisli triple laws) are left out.
@ -524,3 +411,40 @@ Now we can express the above condition:
\arrow["{\sigma^*}", from=1-3, to=3-3]
\end{tikzcd}\]
\end{definition}
\section{Free Objects}
Free objects are constructions capturing the essence of structures in a minimal way, we will rely
on free structures in \autoref{chp:iteration} to define a monad in a general setting. We recall the definition
to establish some notation and then describe how to obtain a monad via existence of free objects.
\begin{definition}[Free Object]\label{def:free}
Let $\C, \D$ be categories and $U : \C \rightarrow \D$ be a forgetful functor (whose construction usually is obvious). A free object on some object $X \in \obj{\D}$ is an object $FX \in \obj{\C}$ together with a morphism $\eta : X \rightarrow UFX$ such that for any $Y \in \obj{\C}$ and $f : X \rightarrow UY$ there exists a unique morphism $\free{f} : UFX \rightarrow UY$ satisfying:
% https://q.uiver.app/#q=WzAsMyxbMCwwLCJYIl0sWzEsMCwiVVkiXSxbMCwxLCJVRlgiXSxbMCwxLCJmIl0sWzAsMiwiXFxldGEiLDJdLFsyLDEsIlxcZnJlZXtmfSIsMix7InN0eWxlIjp7ImJvZHkiOnsibmFtZSI6ImRhc2hlZCJ9fX1dXQ==
\[\begin{tikzcd}[ampersand replacement=\&]
X \& UY \\
UFX
\arrow["f", from=1-1, to=1-2]
\arrow["\eta"', from=1-1, to=2-1]
\arrow["{\free{f}}"', dashed, from=2-1, to=1-2]
\end{tikzcd}\]
\end{definition}
\begin{theorem}
Let $U : \C \rightarrow \D$ be a forgetful functor.
If for every $X \in \obj{\D}$ a free object $FX \in \obj{C}$ exists then $(X \mapsto UFX, \eta : X \rightarrow UFX, \free{(f : X \rightarrow UFY)} : UFX \rightarrow UFY)$ is a Kleisli triple on $\D$.
\end{theorem}
\begin{proof}
We are left to check the laws of Kleisli triples.
\begin{itemize}
\item[\ref{K1}] $\free{\eta} = id$
By uniqueness of $\free{\eta}$ it suffices to show that $id \circ \eta = \eta$ which holds trivially.
\item[\ref{K2}] $\free{f} \circ \eta = f$ for any $f : X \rightarrow UFY$
This is the universal property concerning $\free{f}$.
\item[\ref{K3}] $\free{f} \circ \free{g} = \free{\freee{f} \circ g}$ for any $f : Y \rightarrow UFZ, g : X \rightarrow UFY$
By uniqueness of $\free{\freee{f} \circ g}$ we are left to show $\free{f} \circ \free{g} \circ \eta = \free{f} \circ g$ which again follows directly by the universal property of $\free{g}$.
\end{itemize}
\end{proof}

View file

@ -1,17 +1,16 @@
\chapter{Partiality Monads}\label{chp:partiality}
Moggi's categorical semantics~\cite{moggi} give us a way to interpret an effectful programming language in a category. For this one needs a (strong) monad $T$ capturing the desired effects, then we can take the elements of $TA$ as denotations for programs of type $A$. The Kleisli category of $T$ can be viewed as the category of programs, which gives us a way of composing programs (Kleisli composition).
Moggi's categorical semantics~\cite{moggi} describe a way to interpret an effectful programming language in a category. For this one needs a (strong) monad $T$ capturing the desired effects, then we can take the elements of $TA$ as denotations for programs of type $A$. The Kleisli category of $T$ can be viewed as the category of programs, which gives us a way of composing programs (Kleisli composition).
For this thesis we will restrict ourselves to monads for modelling partiality, the goal of this chapter is to capture what it means to be a partiality monad and look at two common examples.
\section{Properties of Partiality Monads}
We will now look at how to express the following non-controversial properties of partiality monads categorically:
We will now look at how to express the following non-controversial properties of a minimal partiality monad categorically:
\todo[inline]{Not happy with third point}
\begin{itemize}
\item Irrelevance of execution order
\item Partiality of programs
\item No other effect besides partiality
\item No other effect besides some form of non-termination
\end{itemize}
The first property of course holds for any commutative monad, the other two are more interesting.
@ -31,14 +30,13 @@ To ensure that programs are partial, we recall the following notion by Cockett a
\end{definition}
\begin{lemma}
For any $f : X \rightarrow Y$ the restriction $\tdom f$ is idempotent.
For any $f : X \rightarrow Y$ the restriction morphism $\tdom f$ is idempotent.
\end{lemma}
\begin{proof}
This follows by \ref{R3} and \ref{R1}:
\[\tdom f \circ \tdom f = \tdom (f \circ \tdom f) = \tdom f\]
\end{proof}
The idempotent morphism $\tdom f : X \rightarrow X$ represents the domain of definiteness of $f : X \rightarrow Y$. In the category of partial functions this takes the following form:
\[
@ -74,6 +72,22 @@ Lastly we also recall the following notion by Bucalo et al.~\cite{eqlm} which ca
where $\Delta : X \rightarrow X \times X$ is the diagonal morphism.
\end{definition}
To make the equational lifting property more comprehensible we can alternatively state it using do-notation. The equational lifting property states that the following programs must be equal:
\begin{multicols}{2}
\begin{minted}{haskell}
do x <- p
return (x , p)
\end{minted}
\begin{minted}{haskell}
do x <- p
return (x , return x)
\end{minted}
\end{multicols}
That is, if some computation $p : TX$ terminates with the result $x : X$, then $p = return\;x$ must hold. This of course implies that running $p$ multiple times yields the same result as running $p$ once.
\begin{theorem}[\cite{eqlm}]
If $T$ is an equational lifting monad the Kleisli category $\mathcal{C}^T$ is a restriction category.
\end{theorem}
@ -81,7 +95,7 @@ Lastly we also recall the following notion by Bucalo et al.~\cite{eqlm} which ca
Definition~\ref{def:eqlm} combines all three properties stated above, so when studying partiality monads in this thesis, we ideally expect them to be equational lifting monads. For the rest of this chapter we will use these definitions to compare two monads that are commonly used to model partiality.
\section{The Maybe Monad}
The endofunctor $MX = X + 1$ extends to a monad by taking $\eta_X = i_1 : X \rightarrow X + 1$ and $\mu_X = [ id , i_2 ] : (X + 1) + 1 \rightarrow X + 1$. The monad laws follow easily. This is generally known as the maybe monad and can be viewed as the prototypical example of an equational lifting monad:
The endofunctor $MX = X + 1$ extends to a monad by taking $\eta_X = i_1 : X \rightarrow X + 1$ and $\mu_X = [ id , i_2 ] : (X + 1) + 1 \rightarrow X + 1$. The monad laws follow easily. This is generally known as the maybe monad and can be viewed as the canonical example of an equational lifting monad:
\begin{theorem} M is an equational lifting monad.
\end{theorem}
@ -406,11 +420,17 @@ Let us record some facts that we will use to prove commutativity of $\mathbf{D}$
\begin{corollary}
The following properties hold:
\begin{alignat}{1}
&out \circ Df = (f + Df) \circ out\label{eq:outD}
\\&f^* = [ f , {(later \circ f)}^* ] \circ out\label{eq:f*help}
\\&later \circ f^* = {(later \circ f)}^* = f^* \circ later\label{eq:later*}
\end{alignat}
% \begin{alignat}{1}
% &out \circ Df = (f + Df) \circ out\label{eq:outD}
% \\&f^* = [ f , {(later \circ f)}^* ] \circ out\label{eq:f*help}
% \\&later \circ f^* = {(later \circ f)}^* = f^* \circ later\label{eq:later*}
% \end{alignat}
\change[inline]{Move this to beginning, this is not commutativity specific!}
\begin{enumerate}
\item $out \circ Df = (f + Df) \circ out$
\item $f^* = [ f , {(later \circ f)}^* ] \circ out$
\item $later \circ f^* = {(later \circ f)}^* = f^* \circ later$
\end{enumerate}
As well as these properties of $\tau$ and $\sigma$:
\begin{alignat*}{2}
@ -425,28 +445,28 @@ Let us record some facts that we will use to prove commutativity of $\mathbf{D}$
We prove them one by one:
\begin{itemize}
\item[(\ref{eq:outD})] Note that $Df = {(now \circ f)}^*$ definitionally for any $f : X \rightarrow TY$. This is then simply a consequence of~\ref*{D2}.
\item[1.] Note that $Df = {(now \circ f)}^*$ definitionally for any $f : X \rightarrow TY$. This is then simply a consequence of~\ref*{D2}.
\begin{alignat*}{1}
&out \circ Df
\\=\;&out \circ {(now \circ f)}^*
\\=\;&[ out \circ now \circ f , i_2 \circ {(now \circ f)}^* ] \circ out\tag*{\ref{D2}}
\\=\;&(f + Df) \circ out\tag*{\ref{D1}}
\end{alignat*}
\item[(\ref{eq:f*help})] By uniqueness of $f^*$ it suffices to show:
\item[2.] By uniqueness of $f^*$ it suffices to show:
\begin{alignat*}{1}
&out \circ [ f , {(later \circ f)}^* ] \circ out
\\=\;&[ out \circ f , out \circ {(later \circ f)}^* ] \circ out
\\=\;&[out \circ f , [ out \circ later \circ f , i_2 \circ {(later \circ f)}^* ] \circ out ] \circ out\tag*{\ref{D2}}
\\=\;&[out \circ f , i_2 \circ [ f , {(later \circ f)}^* ] \circ out ] \circ out\tag*{\ref{D1}}
\end{alignat*}
\item[(\ref{eq:later*})]
\item[3.]
The next one follows by monicity of $out$:
\begin{alignat*}{1}
&out \circ {(later \circ f)}^*
\\=\;&[ out \circ later \circ f , i_2 \circ {(later \circ f)}^*] \circ out\tag*{\ref{D2}}
\\=\;&i_2 \circ [ f , {(later \circ f)}^*] \circ out\tag*{\ref{D1}}
\\=\;&i_2 \circ f^*\tag*{(\ref{eq:f*help})}
\\=\;&i_2 \circ f^*
\\=\;&out \circ later \circ f^*\tag*{\ref{D1}}
\\=\;&i_2 \circ f^*\tag*{\ref{D1}}
\\=\;&[ out \circ f , i_2 \circ f^* ] \circ i_2
@ -463,7 +483,7 @@ Let us record some facts that we will use to prove commutativity of $\mathbf{D}$
\begin{alignat*}{1}
&out \circ \sigma
\\=\;&out \circ Dswap \circ \tau \circ swap
\\=\;&(swap + Dswap) \circ out \circ \tau \circ swap\tag*{(\ref{eq:outD})}
\\=\;&(swap + Dswap) \circ out \circ \tau \circ swap
\\=\;&(swap + Dswap) \circ (id + \tau) \circ dstl \circ (id \times out) \circ swap \tag*{\ref{tau1}}
\\=\;&(swap + Dswap) \circ (id + \tau) \circ dstl \circ swap \circ (out \times id)
\\=\;&(swap + Dswap) \circ (id + \tau) \circ (swap + swap) \circ dstr \circ (out \times id)
@ -505,7 +525,7 @@ Let us record some facts that we will use to prove commutativity of $\mathbf{D}$
\\=\; & [ out \circ [ now , f ] , i_2 \circ {[ now , f ]}^* ] \circ [ i_1 + D i_1 \circ \sigma , i_2 \circ [ D i_1 \circ \tau , later \circ now \circ i_2 ] ] \circ w\tag*{\ref{D2}}
\\=\; & [ id + \sigma , i_2 \circ {[ now , f]}^* \circ [ D i_1 \circ \tau , later \circ now \circ i_2 ] ] \circ w\tag*{\ref{D1}}
\\=\; & [ id + \sigma , i_2 \circ [ \tau , {[ now , f]}^* \circ later \circ now \circ i_2 ] ] \circ w
\\=\; & [ id + \sigma , i_2 \circ [ \tau , {[ later \circ now , later \circ f]}^* \circ now \circ i_2 ] ] \circ w\tag*{(\ref{eq:later*})}
\\=\; & [ id + \sigma , i_2 \circ [ \tau , {[ later \circ now , later \circ f]}^* \circ now \circ i_2 ] ] \circ w
\\=\; & [ id + \sigma , i_2 \circ [ \tau , later \circ f ] ] \circ w
\end{alignat*}
@ -527,7 +547,7 @@ Let us record some facts that we will use to prove commutativity of $\mathbf{D}$
\\=\;&[ (id + \sigma), i_2 \circ (out^{-1} \circ (id + \sigma))^* \circ [D i_1 \circ \tau , D i_2 \circ \tau]] \circ (dstr + dstr) \circ dstl \circ (out \times out)
\\=\;&[ (id + \sigma), i_2 \circ [(out^{-1} \circ i_1)^* \circ \tau , (out^{-1} \circ i_2 \circ \sigma)^* \circ \tau]] \circ w
\\=\;&[ (id + \sigma), i_2 \circ [ \tau , (later \circ \sigma)^* \circ \tau]] \circ w \tag*{\ref{K1}}
\\=\;&[ (id + \sigma), i_2 \circ [ \tau , later \circ \sigma^* \circ \tau]] \circ w \tag*{(\ref{eq:later*})}
\\=\;&[ (id + \sigma), i_2 \circ [ \tau , later \circ \sigma^* \circ \tau]] \circ w
\end{alignat*}
where