Categories For Noobs
© 2020 Vlad Patryshev
In This Crash Course
Pet Database
create type kind as enum (‘cat’, ‘dog’, ‘fly’, ‘hamster’, ‘e.coli’);
create table Person (id bigint, name varchar(80), pet bigint,
primary key (id),
constraint pet_fk foreign key pet references Animal(id));
create table Animal (id bigint, kind kind, name varchar(80), owner bigint,
primary key (id),
constraint owner_pk foreign key owner references Person(id));
“Conceptual Model”
Person |
name |
Animal |
kind |
{cat, dog, fly, hamster, e.coli} |
‘owner’
‘pet’
‘kind’
Can compose!
kind(pet(owner(pet(“John”)))
select kind from Animal a where a.owner in (select id from Person where pet in (select id from Animal where owner in (select id from person where name=’John’)));
Some Types
Int |
toLong
Can compose, but… are they functions?
We don’t care
It’s just a picture
toInt∘toString = identity
toLong∘toString = identity
toString∘toLong = toString
Long |
String |
Unit |
toInt
toLong
toString
toInt
toString
Define: Category
E.g. matrices
(what are the objects?)
Not all objects are equal
How?
Initial Object in a Category
Definition. In a category C, Initial Object is an object 0 with a unique arrow iX:0 → X for any given X. (this is its “universal property”)
Note that if we take X=0, we see that there is just one arrow 0 → 0. Can you name a set (or two) with only one function S→S?
What if there’s more? Say 01 and 02, both having feature.
Then, for 01, we have a unique a:01 → 02; and similarly we have a unique b:02 → 01. Composing a and b either way, we get an identity.
An Initial Object is unique up to an isomorphism.
Examples of Initial Objects
initial object?
They are
isomorphic!
Terminal Object in a Category
Definition. In a category C, Terminal Object is an object 1 with a unique arrow uX:X → 1 for any given X. (this is its “universal property”)
For X=1, we see that there is just one function 1 → 1, same as with 0.
What if there’s more? Say 11 and 12. Then there’s a unique a:11 → 12; and similarly we have a unique b:12 → 11. Composing a and b either way, we get an identity; so they are isomorphisms.
A Terminal Object is unique up to an isomorphism.
Examples of Terminal Objects
Initial and Terminal Object in our DB
terminal
Cartesian Product
Definition. Given a category C, and two objects in it, X and Y, their Cartesian Product is such an object Z=X×Y, together with two arrows (projections), pX:Z→X and pY:Z→Y, that for any pair f:A→X and g:A→Y, f=pX∘h and g=pY∘h for some unique h. (this is its “universal property”)
Cartesian product is unique up to an isomorphism. (proof?)
Examples of Cartesian Products
val intWithString: (Int, String) = (42, “Hello 42”)
Disjoint Union (dual to Product)
Definition. Given a category C, and two objects, X and Y, their Disjoint Union is such an object Z=X+Y, together with two arrows, iX:X→Z and iY:Y→Z, that for any pair f:X→A and g:Y→A, f=h∘iX and g=h∘iY for some unique h. (this is its “universal property”)
Disjoint union is unique up to an isomorphism. (proof?)
Examples of Disjoint Unions
val intOrString: Either[Int, String] = Left(42)// Right(“Hello 42”)
Equalizers
Definition. Given a category C, two objects in it, X and Y, and two arrows, f,g:X→Y, their Equalizer is an object E=Eq(f,g), together with an arrow eq:E→X, such that that f∘eq=g∘eq , and for any m:O→X, such that f∘m=g∘m, m=eq∘u for some unique u. (this is its “universal property”)
An equalizer is unique up to an isomorphism. (proof?)
select * from X where X.f=X.g;
In this case, the arrow eq is the inclusion of this selection into the whole X.
Pullbacks
Definition. Given a category C, and three objects, X,Y and Z, and two arrows, f:X→Z and g:Y→Z, their Pullback is such an object X×ZY, together with arrows (projections) pX:X×ZY→X and pY:X×ZY→Y, such that g∘pY = f∘pX, and for any pair x:U→X, y:U→Y,such that gy=fx, the following holds:
x=pX∘h and y=pY∘h for some unique h. (this is its “universal property”)
A pullback is unique up to an isomorphism. (proof?)
Examples of Pullbacks
e.g. select * from Person A, Person B where A.pet=B.pet;
Functor
It is just an arrow in the category of all categories
Functor
Given categories A and B, a functor F: A → B
consists of mappings Fo: Objects(A) → Objects(B)
and Fa: Arrows(A) → Arrows(B),
so that
F(idX)=idF(X)
and F(f∘g)=F(f)∘F(g)
Example: Parametric Class
trait List[A] extends Iterable[A] {
def map[A](f:A=>B):List[B]
}
Examples
Have to define: sets F(0), F(1), F(2); functions F(f), F(g), F(gf)
Cartesian Product is a Functor
Choose an object A. X ↦ X×A is a functor.
How does it work on arrows? f:X→Y ↦ f×idA: X×A → Y×A
+1 is a Functor
In Sets, take X ↦ X+1. f: X→Y ↦ (f+1): X+1 → Y+1
This functor is called Option[T].
Category of Categories
Take functors F:A→B, G:B→C; their composition H=G∘F is also a functor.
H(idX) = G(F(idX)) = G(idF(X)) = idG(F(X))
H(p∘q) = G(F(p∘q)) = G(F(p)∘F(q)) = G(F(p))∘G(F(q)) = H(p)∘H(q)
Identity functor IdA:A→A Id(f) = f
Cat - a category of all categories.
No barber problems, nobody shaves nobody.
Exponential Functor
A, B - objects in category C. BA - an object of functions from A to B, if such exists.
E.g. in Sets, {f:A→B}.
In Scala | In old Java (Guava) (https://code.google.com/p/guava-libraries/wiki/FunctionalExplained) |
trait Function[X,Y] { def apply(x:X): Y } | interface Function<X,Y> { Y apply(X x); } |
val len:Function[String, Int] = _.length | Function<String, Integer> lengthFunction = new Function<String, Integer>() { public Integer apply(String string) { return string.length(); } }; |
Define Exponential via Currying
In Sets, f:A→CB ≡ f’:A×B→C
f(x)(b) = f’(x,b)
Very similar to P⊢(Q→R) ≡ P∧Q ⊢ R
Actually… it’s the same thing.
Take a partial order, for example, a Boolean lattice,
where a×b ≡ min(a,b) ≡ a∧b
a∧b ≤ c ≡ a ≤ (b→c)
Currying: Yoneda Lemma
Arrows(A×B, C) ≡ Arrows(A, CB)
(Caveat: what is Arrows? A set? It’s not always available as a set)
Monad
* f:F(X)->G(X) is natural if for all p:X→ Y
Given a category C, an endofunctor M: C → C
is called a Monad if it has two features:
These two arrows must be natural*
Example with Lists
Category Scala
Take functor List.
We see two properties:
Do you see a monad?
Example with Numbers
Category ℤ = (ℤ,≤); category ℚ = (ℚ,≤)
Inclusion iz: ℤ ↣ ℚ - preserves order, so is a functor.
How about ℚ → ℤ? Upb: q ↦ Upb(q)
Upb(q) ≤ n /* this is in Z */ ≡ q ≤ n /* this is in Q */
Take composition q ↦ iZ(Upb(q)), call it Int.
We see two properties:
References
David I. Spivak, Category Theory for the Sciences - good examples from databases
Saunders Mac lane, Categories for the Working Mathematician, Second Edition
Benjamin C. Pierce, Basic Category Theory for Computer Scientists
http://fundeps.com/tables/FromSemigroupToMonads.pdf
https://ncatlab.org/nlab/show/HomePage
Thanks for your Patience!