<spanclass="tomenu"><ahref="#navigation">↓ Menu ↓</a></span>
<spanclass="flush"></span>
</div>
</div>
<divid="titre">
<h1>Category Theory Presentation</h1>
</div>
<divclass="flush"></div>
<divid="afterheader"class="article">
<divclass="corps">
<imgsrc="http://yogsototh.github.com/Category-Theory-Presentation/categories/img/mp/cat-hask-endofunctor.png"alt="Cateogry of Hask's endofunctors"/>
<p>Yesterday I was happy to make a presentation about Category Theory at <ahref="http://www.meetup.com/riviera-scala-clojure">Riviera Scala Clojure Meetup</a> (note I used only Haskell for my examples).</p>
<ul><li><ahref="http://yogsototh.github.com/Category-Theory-Presentation/categories.html">Click here to go to the HTML presentation.</a>
</li><li><ahref="http://yogsototh.github.com/Category-Theory-Presentation/categories.pdf">Click Here to download the PDF slides (<spanstyle="text-transform: uppercase">L<supstyle="vertical-align: 0.15em; margin-left: -0.36em; margin-right: -0.15em; font-size: .85em">a</sup>T<substyle="vertical-align: -0.5ex; margin-left: -0.1667em; margin-right: -0.125em; font-size: 1em">e</sub>X</span> not rendered properly)</a>
</li></ul>
<p>If you don't want to read them through an HTML presentations framework or downloading a big PDF
just continue to read as a standard web page.
</p>
<divstyle="display:none">
\(\newcommand{\F}{\mathbf{F}}\)
\(\newcommand{\E}{\mathbf{E}}\)
\(\newcommand{\C}{\mathcal{C}}\)
\(\newcommand{\D}{\mathcal{D}}\)
\(\newcommand{\id}{\mathrm{id}}\)
\(\newcommand{\ob}[1]{\mathrm{ob}(#1)}\)
\(\newcommand{\hom}[1]{\mathrm{hom}(#1)}\)
\(\newcommand{\Set}{\mathbf{Set}}\)
\(\newcommand{\Mon}{\mathbf{Mon}}\)
\(\newcommand{\Vec}{\mathbf{Vec}}\)
\(\newcommand{\Grp}{\mathbf{Grp}}\)
\(\newcommand{\Rng}{\mathbf{Rng}}\)
\(\newcommand{\ML}{\mathbf{ML}}\)
\(\newcommand{\Hask}{\mathbf{Hask}}\)
\(\newcommand{\Cat}{\mathbf{Cat}}\)
\(\newcommand{\fmap}{\mathtt{fmap}}\)
</div>
<sectionclass="slide"style="text-align: center;">
<h1style="position: relative;">Category Theory <spanclass="and">&</span> Programming</h1>
<div><emclass="base01">for</em><ahref="http://www.meetup.com/riviera-scala-clojure">Rivieria Scala Clojure</a> (Note this presentation uses Haskell)</div>
<imgsrc="http://yogsototh.github.com/Category-Theory-Presentation/categories/img/categlory.jpg"alt="Cat n glory"/><figcaption>credit to Tokuhiro Kawai (川井徳寛)</figcaption>
</figure>
<divclass="flush"></div></section>
<sectionclass="slide">
<h2id="general-overview">General Overview</h2>
<divstyle="float:right; width: 18%">
<imgsrc="http://yogsototh.github.com/Category-Theory-Presentation/categories/img/eilenberg.gif"alt="Samuel Eilenberg"/><imgsrc="http://yogsototh.github.com/Category-Theory-Presentation/categories/img/maclaine.jpg"alt="Saunders Mac Lane"/>
</div>
<p><em>Recent Math Field</em><br/>1942-45, Samuel Eilenberg <spanclass="and">&</span> Saunders Mac Lane</p>
<p>Certainly one of the more abstract branches of math</p>
<ul>
<li><em>New math foundation</em><br/> formalism abstraction, package entire theory<sup>★</sup></li>
<li><em>Bridge between disciplines</em><br/> Physics, Quantum Physics, Topology, Logic, Computer Science<sup>☆</sup></li>
★: <ahref="http://www.math.harvard.edu/~mazur/preprints/when_is_one.pdf">When is one thing equal to some other thing?, Barry Mazur, 2007</a><br/> ☆: <ahref="http://math.ucr.edu/home/baez/rosetta.pdf">Physics, Topology, Logic and Computation: A Rosetta Stone, John C. Baez, Mike Stay, 2009</a>
</p>
<divclass="flush"></div></section>
<sectionclass="slide">
<h2id="from-a-programmer-perspective">From a Programmer perspective</h2>
<blockquote>
<p>Category Theory is a new language/framework for Math</p>
<imgclass="right"style="max-width:17%"src="http://yogsototh.github.com/Category-Theory-Presentation/categories/img/mp/strings.png"alt="Monoids are one object categories"/>
<ul>
<li> \(\ob{Str}\) is a singleton </li>
<li> \(\hom{Str}\) each string </li>
<li> ∘ is concatenation <code>(++)</code></li>
</ul>
<ul>
<li><code>"" ++ u = u = u ++ ""</code></li>
<li><code>(u ++ v) ++ w = u ++ (v ++ w)</code></li>
</ul>
<divclass="flush"></div></section>
<sectionclass="slide">
<h2>Finite Example?</h2>
<h3>Graph</h3>
<figureclass="right"style="max-width:40%">
<imgsrc="http://yogsototh.github.com/Category-Theory-Presentation/categories/img/mp/graph-category.png"alt="Each graph is a category"/>
</figure>
<ul>
<li> \(\ob{G}\) are vertices</li>
<li> \(\hom{G}\) each path</li>
<li> ∘ is path concatenation</li>
</ul>
<ul><li>\(\ob{G}=\{X,Y,Z\}\),
</li><li>\(\hom{G}=\{ε,α,β,γ,αβ,βγ,...\}\)
</li><li>\(αβ∘γ=αβγ\)
</li></ul>
<divclass="flush"></div></section>
<sectionclass="slide">
<h2>Number construction</h2>
<h3>Each Numbers as a whole category</h3>
<imgsrc="http://yogsototh.github.com/Category-Theory-Presentation/categories/img/mp/numbers.png"alt="Each number as a category"/>
<divclass="flush"></div></section>
<sectionclass="slide">
<h2>Degenerated Categories: Monoids</h2>
<imgclass="right"style="max-width:17%"src="http://yogsototh.github.com/Category-Theory-Presentation/categories/img/mp/monoid.png"alt="Monoids are one object categories"/>
</li><li><emclass="orange">Terminal</em>: \(T\in\ob{\C}\) s.t. \(T\) is initial in the dual of \(\C\)
</li><li><emclass="orange">Functor</em>: structure preserving mapping between categories
</li><li> ...
</li></ul>
<divclass="flush"></div></section>
<sectionclass="slide">
<h2id="isomorph">Isomorph</h2>
<p><imgclass="right"alt="isomorph cats"src="http://yogsototh.github.com/Category-Theory-Presentation/categories/img/isomorph-cats.jpg"/><emclass="orange">isomorphism</em>: \(f:A→B\) which can be "undone"<em>i.e.</em><br/>\(∃g:B→A\), \(g∘f=id_A\) <spanclass="and">&</span> \(f∘g=id_B\)<br/>in this case, \(A\) <spanclass="and">&</span> \(B\) are <emclass="orange">isomorphic</em>.</p>
<p><spanclass="orange">A≌B</span> means A and B are essentially the same.<br/>In Category Theory, <spanclass="orange">=</span> is in fact mostly <spanclass="orange">≌</span>.<br/>For example in commutative diagrams.</p>
<divclass="flush"></div></section>
<sectionclass="slide">
<h2>Functor</h2>
<p> A functor is a mapping between two categories.
Let \(\C\) and \(\D\) be two categories.
A <em>functor</em><spanclass="orange">\(\F\)</span> from <spanclass="blue">\(\C\)</span> to <spanclass="green">\(\D\)</span>:</p>
<ul>
<li> Associate objects: <spanclass="backblue">\(A\in\ob{\C}\)</span> to <spanclass="backgreen">\(\F(A)\in\ob{\D}\)</span></li>
<p>Forget glitches because of <code>undefined</code>.</p>
<divclass="flush"></div></section>
<sectionclass="slide">
<h2id="haskell-kinds">Haskell Kinds</h2>
<p>In Haskell some types can take type variable(s). Typically: <code>[a]</code>.</p>
<p>Types have <em>kinds</em>; The kind is to type what type is to function. Kind are the types for types (so meta).</p>
<pre><code>Int, Char :: *
[], Maybe :: * -> *
(,), (->) :: * -> * -> *
[Int], Maybe Char, Maybe [Int] :: *</code></pre>
<divclass="flush"></div></section>
<sectionclass="slide">
<h2id="haskell-types">Haskell Types</h2>
<p>Sometimes, the type determine a lot about the function<sup>★</sup>:</p>
<preclass="haskell"><code>fst :: (a,b) -> a -- Only one choice
snd :: (a,b) -> b -- Only one choice
f :: a -> [a] -- Many choices
-- Possibilities: f x=[], or [x], or [x,x] or [x,...,x]
? :: [a] -> [a] -- Many choices
-- can only rearrange: duplicate/remove/reorder elements
-- for example: the type of addOne isn't [a] -> [a]
addOne l = map <spanclass="red">(+1)</span> l
-- The (+1) force 'a' to be a Num.</code></pre>
<p>
<p><spanclass="small base01">★:<ahref="http://ttic.uchicago.edu/~dreyer/course/papers/wadler.pdf">Theorems for free!, Philip Wadler, 1989</a></span></p>
<divclass="flush"></div></section>
<sectionclass="slide">
<h2>Haskell Functor vs \(\Hask\) Functor</h2>
<p>A Haskell Functor is a type <code>F :: * -> *</code> which belong to the type class <code>Functor</code> ; thus instantiate
<h2id="category-of-hask-endofunctors">Category of \(\Hask\) Endofunctors</h2>
<imgwidth="50%"src="http://yogsototh.github.com/Category-Theory-Presentation/categories/img/mp/cat-hask-endofunctor.png"alt="Category of Hask endofunctors"/>
<divclass="flush"></div></section>
<sectionclass="slide">
<h2id="category-of-functors">Category of Functors</h2>
<p>If \(\C\) is <em>small</em> (\(\hom{\C}\) is a set). All functors from \(\C\) to some category \(\D\) form the category \(\mathrm{Func}(\C,\D)\).</p>
<pre><codeclass="haskell small">data List a = Nil | Cons a (List a)
toList :: [a] -> List a
toList [] = Nil
toList (x:xs) = Cons x (toList xs)</code></pre>
<p><code>toList</code> is a natural transformation. It is also a morphism from <code>[]</code> to <code>List</code> in the Category of \(\Hask\) endofunctors.</p>
<pre><codeclass="haskell small">data List a = Nil | Cons a (List a)
toHList :: List a -> [a]
toHList Nil = []
toHList (Cons x xs) = x:toHList xs</code></pre>
<p><code>toHList</code> is a natural transformation. It is also a morphism from <code>List</code> to <code>[]</code> in the Category of \(\Hask\) endofunctors.</p>
<pre><codeclass="haskell small">toMaybe :: [a] -> Maybe a
toMaybe [] = Nothing
toMaybe (x:xs) = Just x</code></pre>
<p><code>toMaybe</code> is a natural transformation. It is also a morphism from <code>[]</code> to <code>Maybe</code> in the Category of \(\Hask\) endofunctors.</p>
<pre><codeclass="haskell small">mToList :: Maybe a -> [a]
mToList Nothing = []
mToList Just x = [x]</code></pre>
<p><code>toMaybe</code> is a natural transformation. It is also a morphism from <code>[]</code> to <code>Maybe</code> in the Category of \(\Hask\) endofunctors.</p>
<imgstyle="width:40%"src="http://yogsototh.github.com/Category-Theory-Presentation/categories/img/mp/maybe-list-endofunctor-morphsm.png"alt="relation between [] and Maybe"/><figcaption>There is <spanclass="red">no isomorphism</span>.<br/> Hint: <code>Bool</code> lists longer than 1. </figcaption>
<p>Goal, find: <code>◎ :: (b -> F c) -> (a -> F b) -> (a -> F c)</code><br/><code>f :: a -> F b</code>, <code>g :: b -> F c</code>:</p>
<ul>
<li><code>(g ◎ f) x</code> ???</li>
<li>First apply <code>f</code> to <code>x</code> ⇒ <code>f x :: F b</code></li>
<li>Then how to apply <code>g</code> properly to an element of type <code>F b</code>?</li>
<p>Goal, find: <code>◎ :: (b -> F c) -> (a -> F b) -> (a -> F c)</code><br/><code>f :: a -> F b</code>, <code>g :: b -> F c</code>, <spanclass="orange"><code>f x :: F b</code></span>:</p>
<ul>
<li>Use <code>fmap :: (t -> u) -> (F t -> F u)</code>!</li>
<li><code>(fmap g) :: F b -> F (F c)</code> ; (<code>t=b</code>, <code>u=F c</code>)</li>
<li><code>(fmap g) (f x) :: F (F c)</code> it almost WORKS!</li>
<li>We lack an important component, <code>join :: F (F c) -> F c</code></li>
<li><code>(g ◎ f) x = join ((fmap g) (f x))</code> ☺<br/>◎ is the Kleisli composition; in Haskell: <code><=<</code> (in <code>Control.Monad</code>).</li>
</ul>
<divclass="flush"></div></section>
<sectionclass="slide">
<h2id="necessary-laws">Necessary laws</h2>
<p>For ◎ to work like composition, we need join to hold the following properties:</p>
<li>abusing notations denoting <code>join</code> by ⊙; this is equivalent to<br/><spanclass="orange"><code>(F ⊙ F) ⊙ F = F ⊙ (F ⊙ F)</code></span></li>
<li>There exists <code>η :: a -> F a</code> s.t.<br/><spanclass="orange"><code>η⊙F=F=F⊙η</code></span></li>
<p>A monad is a triplet <code>(M,⊙,η)</code> where</p>
<ul>
<li>\(M\) an <spanclass="orange">Endofunctor</span> (to type <code>a</code> associate <code>M a</code>)</li>
<li>\(⊙:M×M→M\) a <spanclass="orange">nat. trans.</span> (i.e. <code>⊙::M (M a) → M a</code> ; <code>join</code>)</li>
<li>\(η:I→M\) a <spanclass="orange">nat. trans.</span> (\(I\) identity functor ; <code>η::a → M a</code>)</li>
</ul>
<p>Satisfying</p>
<ul>
<li>\(M ⊙ (M ⊙ M) = (M ⊙ M) ⊙ M\)</li>
<li>\(η ⊙ M = M = M ⊙ η\)</li>
</ul>
<divclass="flush"></div></section>
<sectionclass="slide">
<h2id="compare-with-monoid">Compare with Monoid</h2>
<p>A Monoid is a triplet \((E,∙,e)\) s.t.</p>
<ul>
<li>\(E\) a set</li>
<li>\(∙:E×E→E\)</li>
<li>\(e:1→E\)</li>
</ul>
<p>Satisfying</p>
<ul>
<li>\(x∙(y∙z) = (x∙y)∙z, ∀x,y,z∈E\)</li>
<li>\(e∙x = x = x∙e, ∀x∈E\)</li>
</ul>
<divclass="flush"></div></section>
<sectionclass="slide">
<h2id="monads-are-just-monoids">Monads are just Monoids</h2>
<blockquote>
<p>A Monad is just a monoid in the category of endofunctors, what's the problem?</p>
</blockquote>
<p>The real sentence was:</p>
<blockquote>
<p>All told, a monad in X is just a monoid in the category of endofunctors of X, with product × replaced by composition of endofunctors and unit set by the identity endofunctor.</p>
</blockquote>
<divclass="flush"></div></section>
<sectionclass="slide">
<h2id="example-list">Example: List</h2>
<ul>
<li><code>[] :: * -> *</code> an <spanclass="orange">Endofunctor</span></li>
<li>\(⊙:M×M→M\) a nat. trans. (<code>join :: M (M a) -> M a</code>)</li>
<li>\(η:I→M\) a nat. trans.</li>
</ul>
<preclass="haskell"><code>-- In Haskell ⊙ is "join" in "Control.Monad"
join :: [[a]] -> [a]
join = concat
-- In Haskell the "return" function (unfortunate name)
η :: a -> [a]
η x = [x]</code></pre>
<divclass="flush"></div></section>
<sectionclass="slide">
<h2id="example-list-law-verification">Example: List (law verification)</h2>
<p>Example: <code>List</code> is a functor (<code>join</code> is ⊙)</p>