Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Category Theory Primer for Beginners

Avatar for Anupam Anupam
September 15, 2025
2

Category Theory Primer for Beginners

A Category Theory primer, presented at the FPIndia Bangalore Sept 2025 Meetup

Avatar for Anupam

Anupam

September 15, 2025
Tweet

Transcript

  1. WHAT WE WILL ACCOMPLISH TODAY • Understand what category theory

    even is • Learn basics to build intuition. • Map concepts to functional programming • Do something useful with it 2 / 52
  2. CATEGORY THEORY IS THE MATHEMATICS OF DEFINING CATEGORIES AND FOCUSING

    ON THEM Precisely, formally, define - • the operations that can be performed on things • the properties that we can access on things That’s what programming is 8 / 52
  3. WHAT IS PROGRAMMING • Coming up with the algorithmic steps

    to accomplish something • Operations and properties • Do this. Do this. Check this. Do that. 9 / 52
  4. BUT ALSO • Defining the structure on which we can

    do these ops • Some structure is usually provided • Some you create your own • Usually they are at different levels 10 / 52
  5. THE STRUCTURE YOU CARE ABOUT CHANGES FOR DIFFERENT PARTS OF

    A PROGRAM • Dog.bark • Person.converse • LivingThing.makeSound Levels may change 11 / 52
  6. BUT STRUCTURE DOESN’T CHANGE IN ISOLATION If you were focusing

    on LivingThings, perhaps you divide the world into living and non-living. cat dog whale chair car sun 12 / 52
  7. DECIDING WHAT YOU WANT TO FOCUS ON WILL TELL YOU

    HOW TO SLICE UP THE WORLD • German Shepherd, Golden Retriever, Pug, ... • My German Shepherd, Your Golden Retriever, ... • Me, You, Them, ... 13 / 52
  8. SO WHAT IS CATEGORY THEORY? The Big Idea Category theory

    is a very abstract branch of mathematics, concerned with: • Objects and their context • Their relationships with other objects • And how we can derive properties and operations on them • It gives us a framework to manage this complexity • Allows us to analyse the connections between multiple groups and levels at the same time • It’s very general, so applies in a lot of situations 14 / 52
  9. CATEGORY THEORY IS VERY SIMPLE • However, extremely simple things,

    can be the very complicated • Because simple things generalise to a lot of usecases 15 / 52
  10. SO IT’S IMPORTANT TO REMEMBER • Maths is made up

    and the points don’t matter • It’s supposed to be fun. emergent complexity • The human brain is good at this stuff. visual, patterns 16 / 52
  11. A PROPERTY! • a ∼ b ∧ b ∼ c

    ⇒ a ∼ c • Transitivity a b c 20 / 52
  12. A PROPERTY! • a ∼ b ∧ b ∼ c

    ⇒ a ∼ c • Transitivity a b c 20 / 52
  13. MORE STRUCTURE How do we represent relations in programming? 1.

    A common way to relate A and B is via a predicate f(A,B) 10 11 12 ≤ ≤ ≤ 22 / 52
  14. MORE STRUCTURE How do we represent relations in programming? 1.

    A common way to relate A and B is via a predicate f(A,B) 10 11 12 ≤ ≤ ≤ ≤ 23 / 52
  15. MORE STRUCTURE 1. Perhaps a more useful way to relate

    A and B is via a function A → B 10 11 12 +1 +1 24 / 52
  16. MORE STRUCTURE 1. Perhaps a more useful way to relate

    A and B is via a function A → B 10 11 12 +1 +1 +1? 25 / 52
  17. MORE STRUCTURE 1. Perhaps a more useful way to relate

    A and B is via a function A → B 10 11 12 +1 +1 +1? +1? 26 / 52
  18. PROBLEM • Seems like we have lost associativity • And

    reflexivity • But remember the part about it all being made up? • We can just make up new structure that fits! 27 / 52
  19. PROBLEM • Seems like we have lost associativity • And

    reflexivity • But remember the part about it all being made up? • We can just make up new structure that fits! 27 / 52
  20. PROBLEM • Seems like we have lost associativity • And

    reflexivity • But remember the part about it all being made up? • We can just make up new structure that fits! 27 / 52
  21. COMPOSITION OF FUNCTIONS 1. Composition of two functions f and

    g is written f ◦ g 2. Pronounced after 3. Composition of +1 and +1 is +1 ◦ +1 = +2 28 / 52
  22. THROW IT IN THE MIX! Axiom 1: Rules of Composition

    If you have a f : A → B and g : B → C, there must be an arrow g ◦ f : A → C. A f → B g → C And composition must be associative. If f : A → B, g : B → C, and h : C → D: h ◦ (g ◦ f) = (h ◦ g) ◦ f A f → B g → C h → D 29 / 52
  23. IDENTITY FUNCTIONS 1. An identity function is one that leaves

    its argument unchanged 2. The identity function for integers could be +0 30 / 52
  24. THROW IT IN THE MIX! Axiom 2: Identity For every

    object A, there must exist a special identity morphism, idA : A → A. A idA→ A Or more commonly: A idA This identity morphism acts as a neutral element for composition. For any f : A → B: f ◦ idA = f and idB ◦ f = f 31 / 52
  25. RECOVERY • Transitivity • In our structure we must have

    an arrow from 10 to 12, and that arrow must be the same as +1 ◦ +1 = +2, 10 11 12 +1 +1 +2 32 / 52
  26. RECOVERY • Reflexivity • In our structure we must have

    an arrow from 10 to 10, and 11 to 11, and so on, • And that arrow must act as the identity element for function composition 10 11 12 +1 +1 +2 id10 33 / 52
  27. A CATEGORY AT ITS CORE A category C is -

    1. A bunch of Objects: denoted with capital letters A,B,C,... 2. A bunch of Arrows: connections between objects. Denoted with lower case letters f,g,h,... • A • B • C • D • E • F • G • H i j f g h k l 34 / 52
  28. THE CORE COMPONENTS: MORPHISMS • The Arrows are more formally

    called Morphisms. • Categories focus more on morphisms rather than objects. • A morphism represents a relationship between any two objects A and B in a category. • There could be any number of distinct morphisms between any two objects. • A morphism from A to B is written as f : A → B. 35 / 52
  29. THE CORE COMPONENTS: MORPHISMS We can represent objects and morphisms

    diagramatically as follows: A f → B Morphisms compose. The composition is denoted by the symbol (◦). So the composition of g after f is written as: g ◦ f Diagramatically: A B C f g 36 / 52
  30. THE RULES: AXIOMS OF A CATEGORY (1/2) Morphisms must obey

    two rules (axioms): Axiom 1: Rules of Composition If you have a f : A → B and g : B → C, there must be an arrow g ◦ f : A → C. A f → B g → C And composition must be associative. If f : A → B, g : B → C, and h : C → D: h ◦ (g ◦ f) = (h ◦ g) ◦ f A f → B g → C h → D 37 / 52
  31. THE RULES: AXIOMS OF A CATEGORY (2/2) Axiom 2: Identity

    For every object A, there must exist a special identity morphism, idA : A → A. A idA→ A Or more commonly: A idA This identity morphism acts as a neutral element for composition. For any f : A → B: f ◦ idA = f and idB ◦ f = f 38 / 52
  32. EXAMPLE: EMPTY CATEGORY (0) • The empty category, denoted as

    0, is the unique category with: • No objects. • No morphisms. • It vacuously satisfies all the category axioms because there is nothing to check! 39 / 52
  33. EXAMPLE: THE TERMINAL CATEGORY (1) • The terminal or final

    category, denoted as 1, consists of: • One object, let’s call it •. • One morphism, which must be the identity morphism id• : • → •. • id• 40 / 52
  34. EXAMPLE: THE TERMINAL CATEGORY (1) • The terminal or final

    category, denoted as 1, consists of: • One object, let’s call it •. • One morphism, which must be the identity morphism id• : • → •. • id• 40 / 52
  35. EXAMPLE: THE TERMINAL CATEGORY (1) • The terminal or final

    category, denoted as 1, consists of: • One object, let’s call it •. • One morphism, which must be the identity morphism id• : • → •. • id• 40 / 52
  36. EXAMPLE: TWO CATEGORY Let’s define a category with just two

    objects, A and B. • Objects: {A,B} • Morphisms: {idA,idB,f : A → B,g : A → B} A f,g→ B • Composition is not defined for f and g since their domains and codomains don’t align. The identity morphisms are also omitted from the diagram for clarity. • All axioms hold! 41 / 52
  37. EXAMPLE: THE CATEGORY SET This is the most common example

    of a category. • Objects: All sets. (e.g., {1,2,3}, R, {cat,dog}) • Morphisms: All functions between sets. • Composition: Standard function composition. • Identity: The identity function for each set, idA (x) = x. 42 / 52
  38. EXAMPLES: PREORDER CATEGORY Any preordered set (P,≤) can be viewed

    as a thin category (at most one morphism between any two objects). • Objects: The elements of the set P. • Morphisms: A unique morphism exists from x to y if and only if x ≤ y. ∣Hom(x, y)∣ ≤ 1 for all x, y ∈ P • Composition: If x ≤ y and y ≤ z, the composite morphism exists because transitivity ensures x ≤ z. • Identity: For any object x, the identity morphism exists because reflexivity ensures x ≤ x. 43 / 52
  39. EXAMPLES: PREORDER CATEGORY Any preordered set (P,≤) can be viewed

    as a thin category (at most one morphism between any two objects). • Objects: The elements of the set P. • Morphisms: A unique morphism exists from x to y if and only if x ≤ y. ∣Hom(x, y)∣ ≤ 1 for all x, y ∈ P • Composition: If x ≤ y and y ≤ z, the composite morphism exists because transitivity ensures x ≤ z. • Identity: For any object x, the identity morphism exists because reflexivity ensures x ≤ x. 43 / 52
  40. EXAMPLES: PREORDER CATEGORY Any preordered set (P,≤) can be viewed

    as a thin category (at most one morphism between any two objects). • Objects: The elements of the set P. • Morphisms: A unique morphism exists from x to y if and only if x ≤ y. ∣Hom(x, y)∣ ≤ 1 for all x, y ∈ P • Composition: If x ≤ y and y ≤ z, the composite morphism exists because transitivity ensures x ≤ z. • Identity: For any object x, the identity morphism exists because reflexivity ensures x ≤ x. 43 / 52
  41. LET’S IDENTIFY TWO OBJECTS Initial Object (I) An object I

    is initial if for every object X in the category, there exists a unique morphism from I to X. I X Y Z Terminal Object (T) An object T is terminal if for every object X in the category, there exists a unique morphism from X to T. T X Z Y 45 / 52
  42. PRODUCT OF OBJECTS Definition Given two objects A and B,

    their product 1. Is written A × B 2. Has two morphisms (projections): – π1 : A × B → A – π2 : A × B → B A × B A π1 π2 46 / 52
  43. PRODUCT OF OBJECTS Construction For any other object Z with

    morphisms f : Z → A and g : Z → B, there exists a unique morphism h : Z → A × B such that the diagram commutes. π1 ◦ h = f and π2 ◦ h = g A × B A Z π1 π2 f g h 47 / 52
  44. SUM OF OBJECTS Definition: Sum of A and B Given

    two objects A and B, their sum (or coproduct): 1. Is written A + B 2. Has two morphisms (injections): – ι1 : A → A + B – ι2 : B → A + B A + B A ι1 ι2 48 / 52
  45. SUM OF OBJECTS Construction For any other object Z with

    morphisms f : A → Z and g : B → Z, there exists a unique morphism h : A + B → Z such that the diagram commutes. h ◦ ι1 = f and h ◦ ι2 = g A + B A Z ι1 ι2 f g h 49 / 52
  46. EXERCISE Given the definitions of SUM, PRODUCT, INITIAL, and TERMINAL

    objects? PROVE INITIAL is an identity for SUM TERMINAL is an identity for PRODUCT 50 / 52