DEV Community

Cover image for Easy Category Theory Part I: The Basics
Chris McLeod
Chris McLeod

Posted on • Edited on

Easy Category Theory Part I: The Basics

Category Theory may be important to learn as functional programming is coming back into the mainstream. If you a have a passion for discovering abstractions, CT promises to be the ultimate abstraction. As I learn category theory, I will add an article to this series in which I will explain the concept I have learned in an attempt to further my own understanding and hopefully help you learn this vast subject as well.

There are plenty of articles, books, papers etc for learning CT. My goal with these articles is not to provide the best explanation or an exhaustive explanation that uses all the mathematical notation. I only hope to provide another explanation that may trigger an epiphany for the more guided learning I hope you are doing. My explanations here are tuned to the way I learn best, metaphors, pictures, and simple language.

The strategy I recommend for learning CT is to use an introductory book as a guide to the concepts to learn and the order in which to learn them. Then consult other resources (YouTube, articles like this one, etc) to chip away at each concept until your understanding is satisfactory and internalized enough to move to the next concept.

Disclaimer: I am not a mathematician and I do not have a degree. I am only just beginning to learn Category Theory and anything I say should be independently verified as correct.

What is Category Theory?

A theory in mathematics is the attempt to seek out patterns and suggest possibilities about these patterns. Then assemble these possibilities into a well-defined set of ideas using mathematical concepts, language, and notation to form statements (axioms) that are taken to be true and built upon to form further axioms.

Category theory is a somewhat special mathematical theory in that the axioms attempt to formalize the structure of all mathematics. The patterns this theory seeks to explain apparently underly every branch of mathematics and more. That is to say, if you were to replace concepts in any branch of mathematics with the most abstract notions of those objects and their rules, you would then be dealing with CT.

What is a Category?

A category has two parts and two rules. The two parts are things and indicators. An indicator is some process which can be supplied with a thing and indicate (point out, surface, return, show) the same or another thing. The two rules for a category are that indicators must be composable with associativity and there must be an identity indicator for every thing.

Composable means if an indicator uses type A things to indicate type B things and another indicator uses type B things to indicate type C things, then there is an indicator that can use type A things to indicate type C things, and that indicator is the composition of the two indicators i.e. If we can go from A->B->C, we can go directly from A->C.

Associativity is defined by saying a group of quantities connected by operators gives the same result whatever their grouping, as long as their order remains the same. In CT this means, you can start following arrows at any point as long as you don't change where they point.

Identity means if I have things and give one to my indicator it will indicate to me that exact thing I gave it.

I use this non-specific language intentionally. It gets difficult to separate the abstract nature of a category from specific examples of a category. For example indicators are often thought of as functions, as in a mathematical function like f(x), something that takes an input and produces an output. However, indicators don’t have to be transformations. Again, the only rules for what qualifies as an indicator are composable with associativity and an identity indicator exists. Relations are also indicators. If I have a 5 and I use the relation ≤, what I have is an indicator which takes things of type "set of all numbers" and indicates all other things of type "set of numbers less than or equal to 5".

Simplifying the Language

  • Objects: things such as real numbers, words, other categories or even arrows. Pretty much anything an be an object.
  • Arrows: indicators which are processes that use objects to indicate other objects. They must be composable with associativity.

So using our new language, categories are arrows that use objects of a certain kind to point at other objects of the same kind. These arrows must be associative and composable. The category contains ALL such indicators and objects.

Concept Example

In programming, classes (or more accurately types) are an example of a category. Let’s fill in the blanks:

Category of Types

  • Objects: Types
  • Arrows: Hierarchy (inheritance)
  • Composable: Yes. If type C inherits from type B, and type B inherits from type A, then type C inherits from type A. Notice the order in which these types are defined (grouped, evaluated) does not matter from a hierarchy sense i.e. (C <- (B <- A)). However the order in which they are composed does, i.e. C <- B <- A is not the same as B <- C <- A
  • Identity: Yes. Type A could inherit from type A in which case you would have type A, though since this is trivial no language that I know of supports it.

Category of Metaphors

  • Objects: anything
  • Arrows: mental mapping
  • Composable: Yes. If I imagine a piece of wood represents a boat, and a boat represents travel, I can imagine a piece of wood represents travel.
  • Identity: Yes. I can always imagine a piece of wood represents a piece of wood.

What’s Next?

The next article will discuss what it means for two categories to be close enough to the same to be considered equal for most intents and purposes.

Resources

I recommend this book:
https://www.amazon.com/gp/product/052171916X/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1

And Bartoz Milewski's (free!) youtube channel:
https://www.youtube.com/playlist?list=PLbgaMIhjbmEnaH_LTkxLI7FMa2HsnawM_

Top comments (6)

Collapse
 
kenbellows profile image
Ken Bellows • Edited

Nice one! This is heavy stuff, and you weren't kidding about how abstract it is 😳 But I thought your examples at the end were super helpful for showing how it's deeply abstract nature leads to intense flexibility, such that you can represent about anything! I hope you write more about it 😁

Collapse
 
chrismcleod profile image
Chris McLeod

Thank you, there are limitless articles to be written! It underlies just about all math. Thank you for the encouragement, I might just start the isomorphism article right now...

Collapse
 
tails128 profile image
Tails128

Out of curiosity: How did you get started with the topic?
I didn't know it and it seems a lot interesting!

Collapse
 
chrismcleod profile image
Chris McLeod

I found the topic a long time ago when I was implementing tagging for a CMS. I was googling for design patterns for categories/tags and stumbled on to category theory. I read an introduction and was hooked as it claimed to be the mother of all abstraction. I started studying the topic using the book and videos linked in the article. I just read and re-read, watch, and re-watch until I'm less confused about each chapter haha.

Collapse
 
prathyvsh profile image
Prathyush • Edited

Do you have a link to this article? I'm thinking of implementing a tagging system myself and was thinking that tags themselves should be more like the tagged things, just like how you say a category could itself be an arrow.

Collapse
 
prathyvsh profile image
Prathyush

"Then assemble these possibilities into a well-defined set of ideas using mathematical concepts, language, and notation to form statements (axioms) that are taken to be true and built upon to form further axioms."

Think you should have said further *theorems there.