Skip to content

Commit c1df5ab

Browse files
Merge pull request #61 from KobeWullaert/Inductive
Refactoring and elaboration on inductive types
2 parents cb1adca + 30fc6a8 commit c1df5ab

File tree

7 files changed

+1893
-1861
lines changed

7 files changed

+1893
-1861
lines changed

tex/CT4P.tex

Lines changed: 6 additions & 1796 deletions
Large diffs are not rendered by default.

tex/categories.tex

Lines changed: 467 additions & 0 deletions
Large diffs are not rendered by default.

tex/data.tex

Lines changed: 99 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ \chapter{Inductive Datatypes and Initial Algebras}
1010
\section{Examples}
1111
\label{sec:examples}
1212

13+
In \cref{sec:universal}, it was discussed how the empty type, the unit type, the product type, and the sum type can be interpreted in a category.
14+
Those type constructors were completely determined by how they interact with the other objects in the category.
15+
Furthermore, as \cref{exer:coproduct_iff_initial_in_subcategory} illustrates, the aforementioned type constructors can all be described as initial objects.
16+
It turns out that these observations are not specific to those type constructors, but this principle applies to many data types one uses in a functional language.
17+
That this is indeed the case will be illustrated in this section with a couple of examples.
18+
First, we look at the type of natural numbers (\cref{exer:nat-initial}), then we will see that the same can be done for more complex types such as those used in the development of programming languages (\cref{exer:aexp}).
19+
1320
\begin{exer}\label{exer:nat-initial}
1421
Consider the datatype
1522
\begin{lstlisting}[mathescape=true]
@@ -19,7 +26,14 @@ \section{Examples}
1926
\end{lstlisting}
2027
%
2128
%
22-
This inductive datatype comes with a recursion principle to define functions from the natural numbers to any other type $X$,
29+
This type is defined recursively.
30+
First, $\NN$ contains an element called $\Zero$.
31+
Second, for every element (natural number) $n$ there is a new element $\Succ(n)$, which corresponds to $n + 1$.
32+
That is, $\NN$ is closed under the successor.
33+
Alternatively, we can say that $\NN$ is the minimal type/set which is closed under these constructors.
34+
35+
Since $\NN$ is defined recursively, this implies that functions out of $\NN$ can also be defined recursively.
36+
That is, this inductive datatype comes with a recursion principle to define functions from the natural numbers to any other type $X$,
2337
\[
2438
\binaryRule
2539
{z \in X}
@@ -31,7 +45,13 @@ \section{Examples}
3145
\begin{equation}\label{eq:computation-rules-nat}
3246
\rec(z,s)(\Zero) = z \quad\text{ and }\quad \rec(z,s)(\Succ(n)) = s (\rec(z,s)(n)).
3347
\end{equation}
34-
Furthermore, it satisfies the following induction principle:
48+
49+
The recursion principle can be stated more generally.
50+
Indeed, $z \in X$ is a proposition $P$ dependent on $z$, that is $P(z) := z \in X$.
51+
Furthermore, the function $s : X \to X$ is used to witness that if $n \in \NN$ is mapped to $x_n \in X$, then $\Succ(n)$ is mapped to $x_{\Succ(n)} \in X$.
52+
Hence, $s : X \to X$ can be rewritten as $\forall n \in \NN, P(n) \Rightarrow P(\Succ(n))$.
53+
Hence, the recursion principle can be stated more generally via its induction principle:
54+
%Furthermore, it satisfies the following induction principle:
3555
\[
3656
\binaryRule
3757
{P(\Zero)}
@@ -40,7 +60,9 @@ \section{Examples}
4060
{ind}
4161
\]
4262

43-
and the following category:
63+
The recursion principle can be summarized as saying that for every set $X$ together with a chosen element $z \in X$ and a function $s : X \to X$, there is a unique function $f$ from $\NN$ to $X$ such that $f(\Zero) = z$ and $f(\Succ(n)) = s(f(n))$.
64+
The unique existence means precisely that $\NN$, together with its constructors, is initial among those triples $(X, z, s)$.
65+
This is made precise by considering the following category:
4466
\begin{itemize}
4567
\item Objects are triples $(X, z \in X, s : X \to X)$ with $X$ a set;
4668
\item Morphisms from $(X, z \in X, s : X \to X)$ to $(X', z' \in X', s' : X' \to X')$ are functions
@@ -160,17 +182,21 @@ \section{Examples}
160182
\section{Datatypes as Initial Algebras}
161183
\label{sec:datatypes-as-initial}
162184

163-
164-
165185
\begin{reading*}
166186
This chapter is strongly inspired by Varmo Vene's Ph.D.\ thesis \cite[Chapter 2]{vene_phd}.
167-
168187
A good explanation of recursion on lists is given in Graham Hutton's tutorial paper \cite{DBLP:journals/jfp/Hutton99}.
169-
170188
The tutorial on (co)algebras and (co)induction by Jacobs and Rutten \cite{jacobs-rutten-tutorial} provides an excellent overview to the categorical view on inductive and coinductive datatypes.
171189
\end{reading*}
172190

173-
In this section we introduce (initial) algebras which allows us to define inductive data types.
191+
In \cref{sec:examples}, it was discussed how inductively defined data types naturally give rise to a category where the data type is the initial object in that category, and that the initiality correspond to the recursion principle.
192+
Even though different types lead to different categories, those categories are all tuples consisting of a set together with morphisms (modeling the constructor) into the set.
193+
In this section, we generalize the construction of those categories.
194+
In particular, this generalization allows us to compute the recursion principle for inductively defined data types.
195+
196+
To model the constructors, observe that multiple functions $A_0 \to X, \cdots, A_n \to X$ with the same codomain can be equivalently described as one function $(A_0 + \cdots + A_n) \to X$ (see \cref{exer:coproduct-represent}).
197+
For example, an object in the category which models the natural numbers is equivalently a pair $(X \in \SET, [z,s] : 1 + X \to X)$.
198+
199+
%In this section we introduce (initial) algebras which allows us to define inductive data types.
174200

175201
\begin{dfn} Let $F:\CC\to \CC$ be an endofunctor. An \textbf{$F$-algebra} consists of the following data:
176202
\begin{enumerate}
@@ -284,7 +310,7 @@ \section{Datatypes as Initial Algebras}
284310

285311
We are interested in \textbf{initial objects of $\ALG{F}$}, if they exist.
286312
We call these ``initial $F$-algebras''.
287-
For a general endofunctor $F$, an initial $F$-algebra does not exist;
313+
For a general endofunctor $F$, an initial $F$-algebra does not need to exist;
288314
but for many interesting choices of $F$, such an initial object does exist.
289315
Before coming to the general definition (see \cref{dfn:initial-alg}),
290316
we consider an example.
@@ -320,6 +346,11 @@ \section{Datatypes as Initial Algebras}
320346
The morphism $\catam{\phi}$ is called the \emph{catamorphism} generated by $\phi$.
321347
\end{dfn}
322348

349+
\begin{exer}
350+
Let $F : \CC \to \CC$ be an endofunctor.
351+
The initial $F$-algebra, if it exists, is unique up to isomorphism.
352+
\end{exer}
353+
323354
\begin{exer}[\cref{sol:in_catamorphism_id}]\label{exer:in_catamorphism_id}
324355
Let $F:\CC\to\CC$ be an endofunctor and let $(\Initalg F, \In)$ be an initial algebra.
325356
Show that
@@ -337,6 +368,8 @@ \section{Datatypes as Initial Algebras}
337368
Moreover, show that $(\mathsf{Bool}, \mathsf{True}, \mathsf{False})$ is an initial object in $\ALG{F}$.
338369
\end{exer}
339370

371+
Observe that $\mathbf{Bool}$ is the coproduct $1 + 1$.
372+
Hence, more generally:
340373
\begin{exer}\label{exer:coproduct_as_initial_algebra}
341374
The disjoint union (i.e., the coproduct) of two sets $X$ and $Y$ can also be described as an inductive data type; indeed, it is generated by the following two constructors:
342375
\begin{lstlisting}
@@ -369,45 +402,6 @@ \section{Datatypes as Initial Algebras}
369402
form a $\Maybe$-algebra. However, show that $(\mathbb{N}^{c},zero^{c},succ^{c})$ is not an initial $\Maybe$-algebra.
370403
\end{exer}
371404

372-
\begin{exer}[\textbf{Fusion property}, \cref{sol:fusion-property}]\label{exer:fusion-property}
373-
Let $F:\CC\to\CC$ be an endofunctor and let $(\Initalg F, \In)$ be an initial algebra. Show that
374-
for $F$-algebras $(X,\phi)$ and $(Y,\psi)$ and $f\in\CHom{\CC}{X}{Y}$, we have
375-
\[
376-
\co{\phi}{f} = \co{F(f)}{\psi} \implies \co{\catam{\phi}}{f} = \catam{\psi}.
377-
\]
378-
This is summarized in the following diagram:
379-
\begin{equation}\label{eq:initial-f-alg-composition}
380-
\begin{tikzcd}
381-
F\Initalg{F}
382-
\arrow[r, "\In"]
383-
\arrow[d,swap, "F\catam{\phi}"]
384-
\ar[dd, bend right=60, "F\catam{\psi}"']
385-
&
386-
\Initalg{F}
387-
\arrow[d, "\catam{\phi}"]
388-
\ar[dd, bend left=60, "\catam{\psi}"]
389-
\\
390-
FX
391-
\arrow[r, swap, "\phi"]
392-
\ar[d, "Ff"']
393-
&
394-
X \ar[d, "f"]
395-
\\
396-
FY \ar[r, "\psi"']
397-
&
398-
Y
399-
\end{tikzcd}
400-
\end{equation}
401-
\end{exer}
402-
403-
\begin{thm}[\textbf{Lambek's theorem}]
404-
Let $F:\CC\to\CC$ be an endofunctor and let $(\mu^F, \In)$ be an initial algebra. Then, $\In$ is an isomorphism whose inverse is given by $\Inv{\In} = \catam{F(\In)}$.
405-
\end{thm}
406-
407-
\begin{exer}
408-
Prove Lambek's theorem.
409-
\end{exer}
410-
411405
\begin{exer}[\cref{sol:initialalg_for_idfun_with_initialob}]\label{exer:initialalg_for_idfun_with_initialob} Let $\CC$ be a category with an initial object $\bot$. Show that $(\bot, \Id[\bot])$ is the initial algebra for the identity (endo)functor on $\CC$.
412406
\end{exer}
413407

@@ -458,21 +452,6 @@ \section{Datatypes as Initial Algebras}
458452
Hint: a systematic approach to reformulating functions on lists defined by explicit recursion in terms of |fold| is described in \cite[\S3.3]{DBLP:journals/jfp/Hutton99}.
459453
\end{exer}
460454

461-
\begin{exer}[\cref{sol:list-concat-nil}, see also \cite{DBLP:journals/scp/Malcolm90}]
462-
\label{exer:list-concat-nil}
463-
Consider the function |(++) :: [a] → [a] → [a]| defined in \cref{exer:list-functions-as-fold},
464-
and |l :: [a]|.
465-
\begin{enumerate}
466-
\item Show that |nil ++ l = l|.
467-
468-
\item Show that |l ++ nil = l|.
469-
\end{enumerate}
470-
\end{exer}
471-
472-
\begin{exer}[{\cite[\S3.1]{DBLP:journals/jfp/Hutton99}}]
473-
Show that |(+1) . sum = fold (+) 1|, by showing that |(+1) . sum| makes Diagram~\ref{eq:initial-f-alg} of \cref{dfn:initial-alg} commute.
474-
\end{exer}
475-
476455
% \begin{exer}
477456
% An exercise about (the limits of) representing functions as catamorphisms (or rewriting functions using `fold`):
478457
% \begin{enumerate}
@@ -529,6 +508,60 @@ \section{Fusion Property}\label{sec:fusion}
529508
Recall that $(\NN, [\Zero,\Succ])$ is the initial $\Maybe$-algebra.
530509
We also write $(+1)$ for $\Succ$.
531510

511+
\begin{exer}[\textbf{Fusion property}, \cref{sol:fusion-property}]\label{exer:fusion-property}
512+
Let $F:\CC\to\CC$ be an endofunctor and let $(\Initalg F, \In)$ be an initial algebra. Show that
513+
for $F$-algebras $(X,\phi)$ and $(Y,\psi)$ and $f\in\CHom{\CC}{X}{Y}$, we have
514+
\[
515+
\co{\phi}{f} = \co{F(f)}{\psi} \implies \co{\catam{\phi}}{f} = \catam{\psi}.
516+
\]
517+
This is summarized in the following diagram:
518+
\begin{equation}\label{eq:initial-f-alg-composition}
519+
\begin{tikzcd}
520+
F\Initalg{F}
521+
\arrow[r, "\In"]
522+
\arrow[d,swap, "F\catam{\phi}"]
523+
\ar[dd, bend right=60, "F\catam{\psi}"']
524+
&
525+
\Initalg{F}
526+
\arrow[d, "\catam{\phi}"]
527+
\ar[dd, bend left=60, "\catam{\psi}"]
528+
\\
529+
FX
530+
\arrow[r, swap, "\phi"]
531+
\ar[d, "Ff"']
532+
&
533+
X \ar[d, "f"]
534+
\\
535+
FY \ar[r, "\psi"']
536+
&
537+
Y
538+
\end{tikzcd}
539+
\end{equation}
540+
\end{exer}
541+
542+
\begin{thm}[\textbf{Lambek's theorem}]
543+
Let $F:\CC\to\CC$ be an endofunctor and let $(\mu^F, \In)$ be an initial algebra. Then, $\In$ is an isomorphism whose inverse is given by $\Inv{\In} = \catam{F(\In)}$.
544+
\end{thm}
545+
546+
\begin{exer}
547+
Prove Lambek's theorem.
548+
\end{exer}
549+
550+
\begin{exer}[\cref{sol:list-concat-nil}, see also \cite{DBLP:journals/scp/Malcolm90}]
551+
\label{exer:list-concat-nil}
552+
Consider the function |(++) :: [a] → [a] → [a]| defined in \cref{exer:list-functions-as-fold},
553+
and |l :: [a]|.
554+
\begin{enumerate}
555+
\item Show that |nil ++ l = l|.
556+
557+
\item Show that |l ++ nil = l|.
558+
\end{enumerate}
559+
\end{exer}
560+
561+
\begin{exer}[{\cite[\S3.1]{DBLP:journals/jfp/Hutton99}}]
562+
Show that |(+1) . sum = fold (+) 1|, by showing that |(+1) . sum| makes Diagram~\ref{eq:initial-f-alg} of \cref{dfn:initial-alg} commute.
563+
\end{exer}
564+
532565
\begin{reading*}
533566
The content of this section is very much inspired by \cite[\S3.2]{DBLP:journals/jfp/Hutton99}.
534567
You are strongly encouraged to read that section before reading the present section.
@@ -571,8 +604,9 @@ \chapter{Terminal Coalgebras and Coinductive Datatypes}\label{sec:coinductive}
571604
The paper \cite{DBLP:journals/scp/Malcolm90} by Malcolm contains further examples.
572605
\end{reading*}
573606

574-
575-
In this section we introduce (terminal) coalgebras which allows us to define coinductive data types.
607+
In \cref{sec:initial-algs}, we introduce (initial) algebras to model inductive types.
608+
In this section, we introduce the dual notion thereof.
609+
That is, we introduce (terminal) coalgebras which allows us to define coinductive data types.
576610

577611
\begin{dfn} Let $F:\CC\to \CC$ be an endofunctor. An \textbf{$F$-coalgebra} consists of the following data:
578612
\begin{itemize}

0 commit comments

Comments
 (0)