Marcin Jahn | Dev Notebook
  • Home
  • Programming
  • Technologies
  • Projects
  • About
  • Home
  • Programming
  • Technologies
  • Projects
  • About
  • An icon of the Core section Core
    • Programs Execution
    • Stack and Heap
    • Asynchronous Programming
      • Overview
      • Event Queues
      • Fibers
      • Stackless Coroutines
  • An icon of the .NET section .NET
    • HTTPClient
    • Async
      • How Async Works
      • TAP Tips
    • Equality
    • Comparisons
    • Enumerables
    • Unit Tests
    • Generic Host
    • Logging
    • Configuration
    • Records
    • Nullability
    • Garbage Collector
    • IL and Allocations
    • gRPC
    • Source Generators
    • Platform Invoke
    • ASP.NET Core
      • Overview
      • Middleware
      • Razor Pages
      • Routing in Razor Pages
      • Web APIs
      • Filters
      • Identity
      • Validation
      • Tips
    • Entity Framework Core
      • Overview
      • Testing
      • Tips
  • An icon of the Angular section Angular
    • Overview
    • Components
    • Directives
    • Services and DI
    • Routing
    • Observables (RxJS)
    • Forms
    • Pipes
    • HTTP
    • Modules
    • NgRx
    • Angular Universal
    • Tips
    • Standalone Components
  • An icon of the JavaScript section JavaScript
    • OOP
    • JavaScript - The Weird Parts
    • JS Functions
    • ES Modules
    • Node.js
    • Axios Tips
    • TypeScript
      • TypeScript Environment Setup
      • TypeScript Tips
    • React
      • React Routing
      • MobX
    • Advanced Vue.js Features
  • An icon of the Rust section Rust
    • Overview
    • Cargo
    • Basics
    • Ownership
    • Structures
    • Enums
    • Organization
    • Collections
    • Error Handling
    • Generics
    • Traits
    • Lifetimes
    • Closures
    • Raw Pointers
    • Smart Pointers
    • Concurrency
    • Testing
    • Tips
  • An icon of the C/C++ section C/C++
    • Compilation
    • Structures
    • OOP in C
    • Pointers
    • Strings
    • Dynamic Memory
    • argc and argv Visualization
  • An icon of the GTK section GTK
    • Overview
    • GObject
    • GJS
  • An icon of the CSS section CSS
    • Responsive Design
    • CSS Tips
    • CSS Pixel
  • An icon of the Unity section Unity
    • Unity
  • An icon of the Functional Programming section Functional Programming
    • Fundamentals of Functional Programming
    • .NET Functional Features
    • Signatures
    • Function Composition
    • Error Handling
    • Partial Application
    • Modularity
    • Category Theory
      • Overview
      • Monoid
      • Other Magmas
      • Functors
  • An icon of the Algorithms section Algorithms
    • Big O Notation
    • Array
    • Linked List
    • Queue
    • Hash Table and Set
    • Tree
    • Sorting
    • Searching
  • An icon of the Architecture section Architecture
    • What is architecture?
    • Domain-Driven Design
    • ASP.NET Core Projects
  • An icon of the Core section Core
    • Programs Execution
    • Stack and Heap
    • Asynchronous Programming
      • Overview
      • Event Queues
      • Fibers
      • Stackless Coroutines
  • An icon of the .NET section .NET
    • HTTPClient
    • Async
      • How Async Works
      • TAP Tips
    • Equality
    • Comparisons
    • Enumerables
    • Unit Tests
    • Generic Host
    • Logging
    • Configuration
    • Records
    • Nullability
    • Garbage Collector
    • IL and Allocations
    • gRPC
    • Source Generators
    • Platform Invoke
    • ASP.NET Core
      • Overview
      • Middleware
      • Razor Pages
      • Routing in Razor Pages
      • Web APIs
      • Filters
      • Identity
      • Validation
      • Tips
    • Entity Framework Core
      • Overview
      • Testing
      • Tips
  • An icon of the Angular section Angular
    • Overview
    • Components
    • Directives
    • Services and DI
    • Routing
    • Observables (RxJS)
    • Forms
    • Pipes
    • HTTP
    • Modules
    • NgRx
    • Angular Universal
    • Tips
    • Standalone Components
  • An icon of the JavaScript section JavaScript
    • OOP
    • JavaScript - The Weird Parts
    • JS Functions
    • ES Modules
    • Node.js
    • Axios Tips
    • TypeScript
      • TypeScript Environment Setup
      • TypeScript Tips
    • React
      • React Routing
      • MobX
    • Advanced Vue.js Features
  • An icon of the Rust section Rust
    • Overview
    • Cargo
    • Basics
    • Ownership
    • Structures
    • Enums
    • Organization
    • Collections
    • Error Handling
    • Generics
    • Traits
    • Lifetimes
    • Closures
    • Raw Pointers
    • Smart Pointers
    • Concurrency
    • Testing
    • Tips
  • An icon of the C/C++ section C/C++
    • Compilation
    • Structures
    • OOP in C
    • Pointers
    • Strings
    • Dynamic Memory
    • argc and argv Visualization
  • An icon of the GTK section GTK
    • Overview
    • GObject
    • GJS
  • An icon of the CSS section CSS
    • Responsive Design
    • CSS Tips
    • CSS Pixel
  • An icon of the Unity section Unity
    • Unity
  • An icon of the Functional Programming section Functional Programming
    • Fundamentals of Functional Programming
    • .NET Functional Features
    • Signatures
    • Function Composition
    • Error Handling
    • Partial Application
    • Modularity
    • Category Theory
      • Overview
      • Monoid
      • Other Magmas
      • Functors
  • An icon of the Algorithms section Algorithms
    • Big O Notation
    • Array
    • Linked List
    • Queue
    • Hash Table and Set
    • Tree
    • Sorting
    • Searching
  • An icon of the Architecture section Architecture
    • What is architecture?
    • Domain-Driven Design
    • ASP.NET Core Projects

Category Theory

The point of Category Theory is to unify mathematical theories (like algebra, geometry, set theory, etc.) into terms that could be common among all the mathematics. It can also be seen as a higher level of abstraction above other mathematical constructs.

Category Theory can be brought down to:

  • Composition
  • Identity

A Category is a bunch of objects. It’s not a set of objects though, since a set is already quite a loaded term, with its own palette of rules. For simplicity, it’s then just easier to say that a category is a bunch of objects.

An object is a primitive - it has no properties nor structure. It’s like an atom, or a point, the lowest level of abstraction that we can go down to.

There might be morphisms between objects. Here’s an example of a morphism f between objects a and b:

f
a
b

A morphism is an arrow that has a beginning and an end. Between object a and object b, there can be:

  • -1 morphisms
  • 0 morphism
  • many morphisms

An arrow can point from a to b and from b to a at the same time. Or, there could be arrow(s) starting and ending at the same object.

f
g
h
i
j
k
l
a
b

Objects

The only reason why objects exist is to be able to mark the beginnings and the ends of morphisms’ arrows.

So, a Category is a bunch of objects with morphisms between pairs of these objects. A category could be seen as a kind of graph, loosely speaking - there could be an inifinite number of arrows.

Axioms

Composition and Identity are the fundamental rules of a category.

Composition

If there’s a morphism taking us from a to b and a morphism taking us from b to c, then there has to be a morphism taking us from a to c defined as: g ○ f. The ○ symbol mens that g is applied after f (g ○ f = g after f).

The g ○ f syntax might appear counterintutive, but it actually comes from the way how functions are applied in maths:

g ○ f = g(f(x))

f
g
g ○ f
a
b
c

The g ○ f arrow is a composition of arrows f and g. It’s sometimes called multiplication.

There can also be other arrows going from a to c.

The whole category can be defined by a table of all compositions (not morphisms?) within that category.

Identity

Every object in a category has its identity morphism:

id_a
id_b
a
a

Composition with Identity

Composition of any morphism with identity returns the original morphism:

f
id_b
a
b
id_b ○ f = f

That’s the right identity.

We can also reverse the order of the morphisms:

id_b
g
a
b
g ○ id_b = g

That’s the left identity.

Identity is then like a 0 in multiplication: a * 1 = a.

Associativity

h ○ (g ○ f)
g ○ f
f
g
h
h ○ g
(h ○ g) ○ f
a
d
c
b
h ○ (g ○ f) = (h ○ g) ○ f
# Just like in multiplication:
a(bc) = (ab)c

Example

An example of applying category theory could be the following. Let’s imagine that we have sets A and B:

B
A
b0
b1
b2
a0
a1
a2
b3

There is a mapping between A and B, so there exists a function that transforms elements from A to elements from B.

Category theory is supposed to unify various mathematical terms or theories under one umbrella. Therefore, we can transform the sets into category representation:

f
g
h
id_a
id_b
A
B

A set becomes an object. We have objects A and B. Between them, three morphisms have been drawn, because the sets A and B had three functions between them.

We can also see that there is an identity defined for each object. In sets, such identity would be a function that takes a set, and returns the same set.

If there was another set - C - and there was a function defined between some element of B and C, we could draw another object - C - with a morphism between B and C. Possibly, we could also have a composition consisting of morphisms between A and B, and B and C.

After creating the set category, we can forget about the origin of that data, i.e. that it was a bunch of sets. Now we work on that category using category theory. It’s an abstraction making it possible to look at a set (a “low-level thing”) from a higher level of abstraction. It’s kinda like moving from assembler to some high-level language. Instead of a set consisting of some elements, we have a table of morphisms that define our new category. They define the interface of this category. The sets themselves have been “shrunk” down to points A and B.

Programming

In programming, we have types and functions. Types can be seen as sets, and functions can be seen as functions mapping one set to another.

Functions

In set theory, functions are defined as relations. Relation is a set of tuples (a,b), where a ∈ A and b ∈ B. We have sets:

  • domain
  • codomain (which is a superset of a range)

A function takes elements from a domain and maps it to some element from a codomain.

B
A
b0
b1
b2
a0
a1
a2
b3

A relation is a subset of A x B. The domain is the set of elements in A and the codomain is the set of elements in B.

Isomorphism

A function may be invertible. A function y = f(x) is invertible when there is a way to transform it into x = g(y). Such a function f has to be one-to-one (which is a synonym of injection) and onto (which is a synonym of surjection).

  • one-to-one- there are no two arguments that have the same result.
  • surjective - the range and the codomain are the same

In other words, g is a reverse of f, if g ○ f = id (and f ○ g = id). A function that is invertible is called an isomorphism. Here is an example of an isomorphic mapping (it’s the same illustration as up above):

B
A
b0
b1
b2
a0
a1
a2
b3

This is not an isomorphic function:

false

Given b1, we cannot resolve it to a single element from A since both a1 and a2 map to b2.


Getting back to Category Theory, ismorphism cannot be defined in terms of injective or surjective functions, because these terms have different names in that context.

Injection (one-to-one) becomes epimorphism (epic).

Surjection (onto) becomes monomorphism (monic).

In Category Theory, if a given morphism is both an epimorphism and monomorphism, it doesn’t necessarily constitute an isomorphism! It may be a fact for some categories (like a Set), but it is not true in general.

Resources

  • A realy good Set Theory guide on libretexts.org
  • Set Theory Primer
Monoid  →
© 2023 Marcin Jahn | Dev Notebook | All Rights Reserved. | Built with Astro.