\(\newcommand{\F}{\mathbf{F}}\) \(\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}}\)

Category Theory & Programming
by Yann Esposito
@yogsototh, +yogsototh
HTML presentation: use arrows, space to navigate.

Plan

Abstraction

A common concept you see often in multiple instances.

Numbers: 1,2,3,... 3400 BC, real numbers 760 BC

Aboriginal Tally System
Aboriginal Tally System
amelioration ⇒
Mesopotamian Numbers
Mesopotamian base 60 system
Operators: =, <, >, +, ×, ...

Generalization

Egyptian Fractions Negative Numbers (Chinese)

More things can be understood as numbers
& More operator to manipulate them.

Numbers ⇒ Sets

Numbers Set Theory (∞)/Abstract Algebra/Topology
\(\mathbb{N}\): \((+,0)\) Semigroups
\(\mathbb{Z}\): \((+,0,\times,1)\) Rings
\(\mathbb{Q}\) Fields
\(\mathbb{R}\) Complete Fields (topology)
\(\mathbb{C}\) Algebræ
Modules,Vector Spaces, Monoids, ...

More general: more things are sets.
& More precise: clear distinction between concepts.

Sets ⇒? Categories

Numbers Sets Categories
\(\mathbb{N}\): \((+,0)\) Semigroups ?
\(\mathbb{Z}\): \((+,0,\times,1)\) Rings ?
\(\mathbb{Q}\) Fields ?
\(\mathbb{R}\) Complete Fields (topology) ?
\(\mathbb{C}\) Algebræ ?
Modules,Vector Spaces, Monoids, ...?

/.*/ ⇒? Category Theory

Gate between different scientific fields

More general: more things are Categories.
& More precise: better distinction between concepts.

Young field: 1942–45, Samuel Eilenberg & Saunders Mac Lane

Programming & Abstraction

Impure programming

Natural Abstractions: pointers, variables, loop, Objects, Classes...

Representation: a data structure changing other time.

Untyped Pure Programming

Natural abstraction: higher level functions & equations

Strongly Typed Pure Programming

Natural abstraction: Polymorphic higher level functions.

Polymorphism: mappend

(<>) = `mappend`

"abc" <>  "def" = "abcdef"
("abc","xyz") <> ("ABC","XYZ") = ("abcABC","xyzXYZ")
3 <> 4 = ERROR which law? + or *
-- Monoid (N,+)
type Sum = Sum {getSum :: a}
(<>+) = getSum (Sum x <> Sum y)
3 <>+ 4 = 7
-- Monoid (N,*)
type Product = Product {getProduct :: a}
(<>*) = getProduct (Product x <> Product y)
3 <>* 4 = 12

Polymorphism: (>>=)

Example: (>>=) with [a] and Maybe a

data Maybe a = Just a | Nothing
-- Maybe : Maybe Int >>= Int -> Maybe (Int -> Int) >>= (Int -> Int) -> Maybe Int
(Just 2) >>= \x -> (Just (\z->z*z)) >>= \f -> Just (f x) = Just 4
 Nothing >>= \x -> (Just (\z->z*z)) >>= \f -> Just (f x) = Nothing

-- Lists: [a] : [Int] >>= Int -> [Int -> Int] >>= (Int -> Int) -> [Int]
[1,2] >>= \x -> [(+10),(+20)] >>= \f -> [f x] = [11,21,12,22]
   [] >>= \x -> [(+10),(+20)] >>= \f -> [f x] = []

Programming Paradigms

ImpureChoose the data structure, find an algorithm.
Untyped PureChoose the data structure, find an equation.
Typed PureChoose the Types and their laws, find the right operator

Type Theory ⇒ Categories

Plan

Category

A way of representing things and ways to go between things.

A Category \(\mathcal{C}\) is defined by:

Category: Objects

objects

\(\ob{\mathcal{C}}\) is a collection

Category: Morphisms

morphisms

\(\hom{A,B}\) is a collection

Category: Composition

Composition (∘): \(f:A→B, g:B→C\) $$g∘f:A\rightarrow C$$

composition

Category laws: neutral element

for all \(X\), there is an \(\id_X\), s.t. for all \(f:A→B\):

identity

Category laws: Associativity

Composition is associative:

associative composition

Commutative diagrams

Two path with the same source and destination are equal.

Commutative Diagram (Associativity)
\((h∘g)∘f = h∘(g∘f) \)
Commutative Diagram (Identity law)
\(id_B∘f = f = f∘id_A \)

Can this be a category?

Category example 1
YES
Category example 2
no candidate for \(g∘f\)
NO
Category example 3
YES

Can this be a category?

Category example 4
no candidate for \(f:C→B\)
NO
Category example 5
\((h∘g)∘f=\id_B∘f=f\)
\(h∘(g∘f)=h∘\id_A=h\)
but \(h≠f\)
NO

Categories Everywhere?

\(\Set\)

Categories Everywhere?

Smaller Examples

Strings

Monoids are one object categories

Finite Example?

Graph

Each graph is a category
  • \(\ob{G}={X,Y,Z}\),
  • \(\hom{G}=\{ε,α,β,γ,αβ,βγ,...\}\)
    \(\phantom{\hom{G}}=(βγ?)?(αβγ)^*(αβ?)?\),
  • \(αβ∘γ=αβγ\)

Degenerated Categories

Monoids are one object categories

Monoids

each Monoid \((M,e,◎): \ob{M}=\{∙\},\hom{M}=M,\circ = ◎\)

one object

Examples: (Integer,0,+), (Integer,1,*), (Strings,"",++), (Lists,[],++), ...

Number construction

Each Numbers as a whole category

Each number as a category

Degenerated Categories

Preorders

each preorder \((P,≤): \ob{P}={P},\hom{x,y}=\{{x≤y}\} ⇔ x≤y,f_{y,z} \circ f_{x,y} = f_{x,z} \)

At most one morphism between two objects. preorder category

Degenerated Categories

Any set can be a category

Any Set

Any set \(E: \ob{E}=E, \hom{x,y}=\{x\} ⇔ x=y \)

Only identities ; not so interesting

Functor

A functor is a mapping between two categories. Let \(\C\) and \(\D\) be two categories. A functor \(\F\) from \(\C\) to \(\D\):

Functor Example (ob → ob)

Functor

Functor Example (hom → hom)

Functor

Functor Example

Functor

Plan

Hask

Category \(\Hask\):

Haskell Category Representation

Forget glitches because of undefined.

Haskell Functors

Haskell Functors Example: Maybe


data Maybe a = Just a | Nothing
instance Functor Maybe where
    fmap :: (a -> b) -> (Maybe a -> Maybe b)
    fmap f (Just a) = f a
    fmap f Nothing = Nothing

fmap (+1) (Just 1) == 2
fmap (+1) Nothing  == Nothing
fmap head (Just [1,2,3]) == Just 1

Haskell Functors Example: List


instance Functor ([]) where
    fmap :: (a -> b) -> [a] -> [b]
    fmap = map

fmap (+1) [1,2,3]           == [2,3,4]
fmap (+1) []                == []
fmap head [[1,2,3],[4,5,6]] == [1,4]

String like type


data F a = Cons Char a | Nil
-- examples :
--  Cons 'c' 32 :: F Int
--  Cons 'c' (\x -> x*x) :: F (Int -> Int)
--  Cons 'c' (Cons 'a' (\x -> x*x)) :: F (F (Int -> Int))
--  Cons 'c' (Cons 'c' Nil) :: F (F (F))
-- note String is the fixed point of F: F(F(F(...)))
instance Functor F where
    fmap :: (a -> b) -> [a] -> [b]
    fmap f (Cons c x) = Cons c (f x)
    fmap f Nil = Nil

fmap (+1) (Cons 'c' 3)      == Cons 'c' 4
fmap (+1) Nil               == Nil
fmap head (Cons 'c' [1,2,3])== Cons 'c' 1

Functor as boxes

Haskell functor can be seen as boxes containing all Haskell types and functions. Haskell types is fractal:

Haskell functor representation

/

#