Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MATH ALL THE THINGS #56

Merged
merged 15 commits into from
Oct 4, 2017
12 changes: 12 additions & 0 deletions src/category.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
\newcommand{\cat}{%
\mathbf%
}
\newcommand{\idarrow}[1][]{%
\mathbf{id}_{#1}%
}
\newcommand{\Set}{\cat{Set}}
\newcommand{\Rel}{\cat{Rel}}
\newcommand{\Cat}{\cat{Cat}}
\newcommand{\id}{\mathbf{id}}

\newcommand{\stimes}{{\times}}
66 changes: 31 additions & 35 deletions src/content/1.1/Category - The Essence of Composition.tex
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,49 @@
for variety, I will occasionally draw objects as piggies and arrows as
fireworks.) But the essence of a category is \newterm{composition}. Or, if you
prefer, the essence of composition is a category. Arrows compose, so
if you have an arrow from object A to object B, and another arrow from
object B to object C, then there must be an arrow --- their composition
--- that goes from A to C.
if you have an arrow from object $A$ to object $B$, and another arrow from
object $B$ to object $C$, then there must be an arrow --- their composition
--- that goes from $A$ to $C$.

\begin{figure}
\centering
\includegraphics[width=\textwidth]{images/img_1330.jpg}
\caption{In a category, if there is an arrow going from A to B and an arrow going from B to C then there must also be a direct arrow from A to C that is their composition. This diagram is not a full category because it’s missing identity morphisms (see later).}
\caption{In a category, if there is an arrow going from $A$ to $B$ and an arrow going from $B$ to $C$
then there must also be a direct arrow from $A$ to $C$ that is their composition. This diagram is not a full
category because it’s missing identity morphisms (see later).}
\end{figure}

\section{Arrows as Functions}\label{arrows-as-functions}

Is this already too much abstract nonsense? Do not despair. Let's talk
concretes. Think of arrows, which are also called \newterm{morphisms}, as
functions. You have a function \code{f} that takes an argument of type A and
returns a B. You have another function \code{g} that takes a B and returns a C.
You can compose them by passing the result of \code{f} to \code{g}. You have just
defined a new function that takes an A and returns a C.
functions. You have a function \code{f} that takes an argument of type $A$ and
returns a $B$. You have another function \code{g} that takes a $B$ and returns a $C$.
You can compose them by passing the result of $f$ to $g$. You have just
defined a new function that takes an $A$ and returns a $C$.

In math, such composition is denoted by a small circle between
functions: \ensuremath{g \circ f}. Notice the right to left order of composition. For some
people this is confusing. You may be familiar with the pipe notation in
Unix, as in:

\begin{Verbatim}[commandchars=\\\{\}]
\begin{Verbatim}
lsof | grep Chrome
\end{Verbatim}
or the chevron \code{>>} in F\#, which both
go from left to right. But in mathematics and in Haskell functions
compose right to left. It helps if you read \code{g◦f} as ``g \emph{after} f.''
compose right to left. It helps if you read \(g \circ f\) as ``g \emph{after} f.''

Let's make this even more explicit by writing some C code. We have one
function \code{f} that takes an argument of type \code{A} and
returns a value of type \code{B}:

\begin{Verbatim}[commandchars=\\\{\}]
\begin{Verbatim}
B f(A a);
\end{Verbatim}
and another:

\begin{Verbatim}[commandchars=\\\{\}]
\begin{Verbatim}
C g(B b);
\end{Verbatim}
Their composition is:
Expand All @@ -64,25 +66,25 @@ \section{Arrows as Functions}\label{arrows-as-functions}
there isn't one. So let's try some Haskell for a change. Here's the
declaration of a function from A to B:

\begin{Verbatim}[commandchars=\\\{\}]
\begin{Verbatim}
f :: A -> B
\end{Verbatim}
Similarly:

\begin{Verbatim}[commandchars=\\\{\}]
\begin{Verbatim}
g :: B -> C
\end{Verbatim}
Their composition is:

\begin{Verbatim}[commandchars=\\\{\}]
\begin{Verbatim}
g . f
\end{Verbatim}
Once you see how simple things are in Haskell, the inability to express
straightforward functional concepts in C++ is a little embarrassing. In
fact, Haskell will let you use Unicode characters so you can write
composition as:

\begin{Verbatim}[commandchars=\\\{\}]
\begin{Verbatim}
g ◦ f
\end{Verbatim}
You can even use Unicode double colons and arrows:
Expand All @@ -102,17 +104,14 @@ \section{Properties of Composition}\label{properties-of-composition}

\begin{enumerate}
\item
Composition is associative. If you have three morphisms, f, g, and h,
Composition is associative. If you have three morphisms, $f$, $g$, and $h$,
that can be composed (that is, their objects match end-to-end), you
don't need parentheses to compose them. In math notation this is
expressed as:

\begin{Verbatim}[commandchars=\\\{\}]
h◦(g◦f) = (h◦g)◦f = h◦g◦f
\end{Verbatim}
\[h\circ{}(g\circ{}f) = (h\circ{}g)\circ{}f = h\circ{}g\circ{}f\]
In (pseudo) Haskell:

\begin{Verbatim}[commandchars=\\\{\}]
\begin{Verbatim}
f :: A -> B
g :: B -> C
h :: C -> D
Expand All @@ -124,18 +123,15 @@ \section{Properties of Composition}\label{properties-of-composition}
be not as obvious in other categories.

\item
For every object A there is an arrow which is a unit of composition.
For every object $A$ there is an arrow which is a unit of composition.
This arrow loops from the object to itself. Being a unit of composition
means that, when composed with any arrow that either starts at A or ends
at A, respectively, it gives back the same arrow. The unit arrow for
object A is called \code{id\textsubscript{A}} (\newterm{identity} on A). In math
notation, if \code{f} goes from A to B then

\code{f◦id\textsubscript{A} = f}

means that, when composed with any arrow that either starts at $A$ or ends
at $A$, respectively, it gives back the same arrow. The unit arrow for
object A is called $\idarrow[A]$ (\newterm{identity} on $A$). In math
notation, if \code{f} goes from $A$ to $B$ then
\[f \circ \idarrow[A] = f\]
and

\code{id\textsubscript{B}◦f = f}
\[\idarrow[B] \circ f = f\]
\end{enumerate}
When dealing with functions, the identity arrow is implemented as the
identity function that just returns back its argument. The
Expand All @@ -152,7 +148,7 @@ \section{Properties of Composition}\label{properties-of-composition}
In Haskell, the identity function is part of the standard library
(called Prelude). Here's its declaration and definition:

\begin{Verbatim}[commandchars=\\\{\}]
\begin{Verbatim}
id :: a -> a
id x = x
\end{Verbatim}
Expand Down Expand Up @@ -180,7 +176,7 @@ \section{Properties of Composition}\label{properties-of-composition}

The identity conditions can be written (again, in pseudo-Haskell) as:

\begin{Verbatim}[commandchars=\\\{\}]
\begin{Verbatim}
f . id == f
id . f == f
\end{Verbatim}
Expand Down Expand Up @@ -229,7 +225,7 @@ \section{Composition is the Essence of
time. One of the most cited papers in psychology,
\href{http://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two}{The
Magical Number Seven, Plus or Minus Two}, postulated that we can only
keep \ensuremath{7 \pm 2} ``chunks'' of information in our minds. The details of our
keep $7 \pm 2$ ``chunks'' of information in our minds. The details of our
understanding of the human short-term memory might be changing, but we
know for sure that it's limited. The bottom line is that we are unable
to deal with the soup of objects or the spaghetti of code. We need
Expand Down
Loading