You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tex/data.tex
+99-65Lines changed: 99 additions & 65 deletions
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,13 @@ \chapter{Inductive Datatypes and Initial Algebras}
10
10
\section{Examples}
11
11
\label{sec:examples}
12
12
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
+
13
20
\begin{exer}\label{exer:nat-initial}
14
21
Consider the datatype
15
22
\begin{lstlisting}[mathescape=true]
@@ -19,7 +26,14 @@ \section{Examples}
19
26
\end{lstlisting}
20
27
%
21
28
%
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$,
23
37
\[
24
38
\binaryRule
25
39
{z \in X}
@@ -31,7 +45,13 @@ \section{Examples}
31
45
\begin{equation}\label{eq:computation-rules-nat}
32
46
\rec(z,s)(\Zero) = z \quad\text{ and }\quad\rec(z,s)(\Succ(n)) = s (\rec(z,s)(n)).
33
47
\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:
35
55
\[
36
56
\binaryRule
37
57
{P(\Zero)}
@@ -40,7 +60,9 @@ \section{Examples}
40
60
{ind}
41
61
\]
42
62
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:
44
66
\begin{itemize}
45
67
\item Objects are triples $(X, z \in X, s : X \to X)$ with $X$ a set;
46
68
\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}
160
182
\section{Datatypes as Initial Algebras}
161
183
\label{sec:datatypes-as-initial}
162
184
163
-
164
-
165
185
\begin{reading*}
166
186
This chapter is strongly inspired by Varmo Vene's Ph.D.\ thesis \cite[Chapter 2]{vene_phd}.
167
-
168
187
A good explanation of recursion on lists is given in Graham Hutton's tutorial paper \cite{DBLP:journals/jfp/Hutton99}.
169
-
170
188
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.
171
189
\end{reading*}
172
190
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.
174
200
175
201
\begin{dfn} Let $F:\CC\to\CC$ be an endofunctor. An \textbf{$F$-algebra} consists of the following data:
176
202
\begin{enumerate}
@@ -284,7 +310,7 @@ \section{Datatypes as Initial Algebras}
284
310
285
311
We are interested in \textbf{initial objects of $\ALG{F}$}, if they exist.
286
312
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;
288
314
but for many interesting choices of $F$, such an initial object does exist.
289
315
Before coming to the general definition (see \cref{dfn:initial-alg}),
290
316
we consider an example.
@@ -320,6 +346,11 @@ \section{Datatypes as Initial Algebras}
320
346
The morphism $\catam{\phi}$ is called the \emph{catamorphism} generated by $\phi$.
321
347
\end{dfn}
322
348
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.
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:
342
375
\begin{lstlisting}
@@ -369,45 +402,6 @@ \section{Datatypes as Initial Algebras}
369
402
form a $\Maybe$-algebra. However, show that $(\mathbb{N}^{c},zero^{c},succ^{c})$ is not an initial $\Maybe$-algebra.
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
-
411
405
\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$.
412
406
\end{exer}
413
407
@@ -458,21 +452,6 @@ \section{Datatypes as Initial Algebras}
458
452
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}.
459
453
\end{exer}
460
454
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},
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},
0 commit comments