Skip to content

Commit

Permalink
Adding category theory macros
Browse files Browse the repository at this point in the history
This fixes #53 and enables converting \code blocks to e.g. \cat
  • Loading branch information
hmemcpy committed Oct 2, 2017
1 parent 1c799ad commit 3cf8be9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 54 deletions.
14 changes: 14 additions & 0 deletions src/category.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
\newcommand{\cat}{%
\mathbf%
}
\newcommand{\domain}[1]{%
\mathrm{dom}(#1)%
}
\newcommand{\codomain}[1]{%
\mathrm{cod}(#1)%
}
\newcommand{\idarrow}[1][]{%
\mathbf{id}_{#1}%
}
\newcommand{\Set}{\cat{Set}}
\newcommand{\Rel}{\cat{Rel}}
97 changes: 43 additions & 54 deletions src/content/1.3/Categories Great and Small.tex
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,27 @@ \section{Orders}\label{orders}
is a relation between objects: the relation of being less than or equal.
Let's check if it indeed is a category. Do we have identity morphisms?
Every object is less than or equal to itself: check! Do we have
composition? If \code{a <= b} and \code{b <= c} then \code{a
<= c}: check! Is composition associative? Check! A set with a
composition? If $a \leq b$ and $b \leq c$ then $a \leq c$: check! Is composition associative? Check! A set with a
relation like this is called a \newterm{preorder}, so a preorder is indeed
a category.

You can also have a stronger relation, that satisfies an additional
condition that, if \code{a <= b} and \code{b <= a} then \code{a} must be
the same as \code{b}. That's called a \newterm{partial order}.
condition that, if $a \leq b$ and $b \leq a$ then $a$ must be
the same as $b$. That's called a \newterm{partial order}.

Finally, you can impose the condition that any two objects are in a
relation with each other, one way or another; and that gives you a
\newterm{linear order} or \newterm{total order}.

Let's characterize these ordered sets as categories. A preorder is a
category where there is at most one morphism going from any object a to
any object b. Another name for such a category is ``thin.'' A preorder
category where there is at most one morphism going from any object $a$ to
any object $b$. Another name for such a category is ``thin.'' A preorder
is a thin category.

A set of morphisms from object a to object b in a category C is called a
\newterm{hom-set} and is written as \code{C(a, b)} (or, sometimes,
\code{Hom\textsubscript{C}(a, b)}). So every hom-set in a preorder is either
empty or a singleton. That includes the hom-set \code{C(a, a)}, the set of
A set of morphisms from object a to object b in a category $\cat{C}$ is called a
\newterm{hom-set} and is written as $\cat{C}(a, b)$ (or, sometimes,
$\cat{Hom_C}(a, b)$). So every hom-set in a preorder is either
empty or a singleton. That includes the hom-set $\cat{C}(a, a)$, the set of
morphisms from a to a, which must be a singleton, containing only the
identity, in any preorder. You may, however, have cycles in a preorder.
Cycles are forbidden in a partial order.
Expand All @@ -86,24 +85,15 @@ \section{Monoid as Set}\label{monoid-as-set}

For instance, natural numbers with zero form a monoid under addition.
Associativity means that:

\begin{Verbatim}[commandchars=\\\{\}]
(a + b) + c = a + (b + c)
\end{Verbatim}
\[(a + b) + c = a + (b + c)\]
(In other words, we can skip parentheses when adding numbers.)

The neutral element is zero, because:

\begin{Verbatim}[commandchars=\\\{\}]
0 + a = a
\end{Verbatim}
\[0 + a = a\]
and

\begin{Verbatim}[commandchars=\\\{\}]
a + 0 = a
\end{Verbatim}
The second equation is redundant, because addition is commutative \code{(a + b
= b + a)}, but commutativity is not part of the definition of a monoid.
\[a + 0 = a\]
The second equation is redundant, because addition is commutative $(a + b
= b + a)$, but commutativity is not part of the definition of a monoid.
For instance, string concatenation is not commutative and yet it forms a
monoid. The neutral element for string concatenation, by the way, is an
empty string, which can be attached to either side of a string without
Expand All @@ -113,7 +103,7 @@ \section{Monoid as Set}\label{monoid-as-set}
there is a neutral element called \code{mempty} and a binary operation
called \code{mappend}:

\begin{Verbatim}[commandchars=\\\{\}]
\begin{Verbatim}
class Monoid m where
mempty :: m
mappend :: m -> m -> m
Expand Down Expand Up @@ -143,7 +133,7 @@ \section{Monoid as Set}\label{monoid-as-set}
\code{mappend} (this is, in fact, done for you in the standard
Prelude):

\begin{Verbatim}[commandchars=\\\{\}]
\begin{Verbatim}
instance Monoid String where
mempty = ""
mappend = (++)
Expand All @@ -155,12 +145,12 @@ \section{Monoid as Set}\label{monoid-as-set}
two-argument function by surrounding it with parentheses. Given two
strings, you can concatenate them by inserting \code{++} between them:

\begin{Verbatim}[commandchars=\\\{\}]
\begin{Verbatim}
"Hello " ++ "world!"
\end{Verbatim}
or by passing them as two arguments to the parenthesized \code{(++)}:

\begin{Verbatim}[commandchars=\\\{\}]
\begin{Verbatim}
(++) "Hello " "world!"
\end{Verbatim}
Notice that arguments to a function are not separated by commas or
Expand All @@ -170,13 +160,13 @@ \section{Monoid as Set}\label{monoid-as-set}
It's worth emphasizing that Haskell lets you express equality of
functions, as in:

\begin{Verbatim}[commandchars=\\\{\}]
\begin{Verbatim}
mappend = (++)
\end{Verbatim}
Conceptually, this is different than expressing the equality of values
produced by functions, as in:

\begin{Verbatim}[commandchars=\\\{\}]
\begin{Verbatim}
mappend s1 s2 = (++) s1 s2
\end{Verbatim}
The former translates into equality of morphisms in the category
Expand All @@ -186,7 +176,7 @@ \section{Monoid as Set}\label{monoid-as-set}
is called \newterm{extensional} equality, and states the fact that for any
two input strings, the outputs of \code{mappend} and \code{(++)} are
the same. Since the values of arguments are sometimes called
\newterm{points} (as in: the value of f at point x), this is called
\newterm{points} (as in: the value of $f$ at point $x$), this is called
point-wise equality. Function equality without specifying the arguments
is described as \newterm{point-free}. (Incidentally, point-free equations
often involve composition of functions, which is symbolized by a point,
Expand Down Expand Up @@ -243,8 +233,8 @@ \section{Monoid as Category}\label{monoid-as-category}
For instance, there is the operation of adding 5 to every natural
number. It maps 0 to 5, 1 to 6, 2 to 7, and so on. That's a function
defined on the set of natural numbers. That's good: we have a function
and a set. In general, for any number n there is a function of adding n
--- the ``adder'' of n.
and a set. In general, for any number n there is a function of adding $n$
--- the ``adder'' of $n$.

How do adders compose? The composition of the function that adds 5 with
the function that adds 7 is a function that adds 12. So the composition
Expand All @@ -267,21 +257,20 @@ \section{Monoid as Category}\label{monoid-as-category}
tells us that \code{mappend} maps an element of a monoid set to a
function acting on that set.

\begin{wrapfigure}[11]{R}{0pt}
\raisebox{0pt}[\dimexpr\height-0.75\baselineskip\relax]{
\includegraphics[width=40mm]{images/monoid.jpg}}
\end{wrapfigure}

Now I want you to forget that you are dealing with the set of natural
numbers and just think of it as a single object, a blob with a bunch of
morphisms --- the adders. A monoid is a single object category. In fact
the name monoid comes from Greek \newterm{mono}, which means single. Every
the name monoid comes from Greek \emph{mono}, which means single. Every
monoid can be described as a single object category with a set of
morphisms that follow appropriate rules of composition.


\begin{figure}
\centering
\fbox{\includegraphics[width=2.45833in]{images/monoid.jpg}}
\end{figure}

String concatenation is an interesting case, because we have a choice of
defining right appenders and left appenders (or \newterm{prependers}, if
defining right appenders and left appenders (or \emph{prependers}, if
you will). The composition tables of the two models are a mirror reverse
of each other. You can easily convince yourself that appending ``bar''
after ``foo'' corresponds to prepending ``foo'' after prepending
Expand All @@ -291,21 +280,21 @@ \section{Monoid as Category}\label{monoid-as-category}
one-object category --- defines a unique set-with-binary-operator
monoid. It turns out that we can always extract a set from a
single-object category. This set is the set of morphisms --- the adders
in our example. In other words, we have the hom-set \code{M(m, m)} of the
single object \code{m} in the category \code{M}. We can easily define a binary
in our example. In other words, we have the hom-set $\cat{M}(m, m)$ of the
single object $m$ in the category $\cat{M}$. We can easily define a binary
operator in this set: The monoidal product of two set-elements is the
element corresponding to the composition of the corresponding morphisms.
If you give me two elements of \code{M(m, m)} corresponding to \code{f} and
\code{g}, their product will correspond to the composition
\code{g◦f}. The composition always exists, because the source and the
If you give me two elements of $\cat{M}(m, m)$ corresponding to $f$ and
$g$, their product will correspond to the composition
$f \circ g$. The composition always exists, because the source and the
target for these morphisms are the same object. And it's associative by
the rules of category. The identity morphism is the neutral element of
this product. So we can always recover a set monoid from a category
monoid. For all intents and purposes they are one and the same.

\begin{figure}
\centering
\fbox{\includegraphics[width=60mm]{images/monoidhomset.jpg}}
\includegraphics[width=60mm]{images/monoidhomset.jpg}
\caption{Monoid hom-set seen as morphisms and as points in a set.}
\end{figure}

Expand All @@ -319,7 +308,7 @@ \section{Monoid as Category}\label{monoid-as-category}
fact that elements of a hom-set can be seen both as morphisms, which
follow the rules of composition, and as points in a set. Here,
composition of morphisms in M translates into monoidal product in the
set \code{M(m, m)}.
set $\cat{M}(m, m)$.

\section{Challenges}\label{challenges}

Expand Down Expand Up @@ -347,17 +336,17 @@ \section{Challenges}\label{challenges}
\begin{enumerate}
\tightlist
\item
A set of sets with the inclusion relation: A is included in B if
every element of A is also an element of B.
A set of sets with the inclusion relation: $A$ is included in $B$ if
every element of $A$ is also an element of $B$.
\item
C++ types with the following subtyping relation: T1 is a subtype of
T2 if a pointer to T1 can be passed to a function that expects a
pointer to T2 without triggering a compilation error.
C++ types with the following subtyping relation: \code{T1} is a subtype of
\code{T2} if a pointer to \code{T1} can be passed to a function that expects a
pointer to \code{T2} without triggering a compilation error.
\end{enumerate}
\item
Considering that Bool is a set of two values True and False, show that
it forms two (set-theoretical) monoids with respect to, respectively,
operator \code{\&\&} (AND) and \code{\textbar{}\textbar{}} (OR).
operator \code{\&\&} (AND) and \code{||} (OR).
\item
Represent the Bool monoid with the AND operator as a category: List
the morphisms and their rules of composition.
Expand Down
1 change: 1 addition & 0 deletions src/ctfp.tex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
% Almost verbatim translation of the blog posts. Slight adjustments to images and text.

\input{preamble.tex}
\input{category}

\frontmatter

Expand Down
1 change: 1 addition & 0 deletions src/preamble.tex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
\setdefaultlanguage[variant=american]{english}
\setotherlanguages{greek}
\usemintedstyle{emacs}
\usepackage[all]{nowidow}

% To be able to use '\-/' in place of '-' inside \code{}
% so that long function names containing hyphens
Expand Down

0 comments on commit 3cf8be9

Please sign in to comment.