How to learn enough category theory to be good at Haskell

A lot of the most fascinating parts of both practical (and deeply, deeply magical) Haskell programming take at least their names from concepts in category theory. So, maybe you have to learn some CT in order to be good at Haskell? Or, maybe at least you could learn some CT in order to be better at understanding Haskell? Or, at least then I’d understand what the heck a monad is, right?

(Or the parts of other language ecosystems which have followed this trend. I’m looking at you Scala, OCaml, Javascript, etc)

I have to say, resoundingly, that none of those are true. At least not inherently and directly. If your goal is to better understand some of these perhaps-we-can-call-them advanced FP techniques then CT is not the most important thing for you to learn. I’ll explain, and then talk about what you might want to learn instead.

Category theory, the study of abstract names

Category theory, among other things, is uniquely good at discovering universal properties. CT has a lot to say about what that means formally, but practically it means that it’s pretty good at naming things. 

People from Camp A have invented the really powerful technology they call the Florb and people from Camp B discovered the powerful technique they call the Whizbang. Annoyingly, everyone who learns from both camps is pretty sure that the Florb and Whizbang are nearly the same idea, but it’s not entirely clear how to get them 100% lined up. And even if you did, what would you call it?

Well, it’s often the case that people from Camp Category Theory accidentally invented that 10 years ago in a vastly more general fashion and didn’t even notice. They call it the Supine Half-Lax Braided Applicatioid and would have never on their own realized how its useful in its Whizbang and Florb specializations.

Then someone clever writes a very powerful library that uses that insight to cover cases from Camp A and B, calls it Supine, and writes 10 fascinating and inpenetrable blog posts. The Haskell blogosphere goes crazy and we end up with 100 “Supine is just like Potato Soup but different” blog posts and 10 “Supine is just the Supine Half-Lax Braided Applicatioid what’s the big deal” ones.

Ahh, the internet.

Study the Florb

Honestly, it’s not surprising after all this hubub to get the impression that you will unlock cool comprehensional superpowers by being able to decode what the Supine Half-Lax Braided Applicatioid actually means. Honestly, that’s even true. Category theory is rich with powerful techniques for attacking and organizing complex ideas and, I believe, forms a sort of “mother domain” for understanding much of mathematics.

It’s just that this is a very, very, wildly indirect method of learning how to solve the more practical problems that drove the invention of the Florb and Whizbang. Learning CT can be like using a rocket ship and a parachute to skip a pleasant nature hike.

My advice is absolutely to study the Florb. Not only will you likely relate to the problems its solving more easily, but you’ll also get an intuitive sense for the “feel” of that solution. If you then go and study the Whizbang you’ll recognize that “feel” again and be perhaps 80% of the way to seeing how the Supine is successfully generalizing them both.

And, to be totally honest, when you think about the nature of the Haskell Pyramid, it’s quite possible that you never even really need to understand the Supine.

Study abstract algebra

Let’s also get a little more concrete, too. If learning CT is like using a rocket ship and a parachute to climb a mountain, you actually do sometimes need a middle ground. It’s useful to train at things like rock climbing, maybe buy a tent and some ropes.

Much more pratical and useful while still being tremendously powerful is the study of abstract algebra and universal algebra. Algebra, generally speaking, is the mathematics of things which can be composed together and sometimes, later, taken back apart. It’s something very familiar to programmers and has direct relations to many kinds of data structures and APIs we use regularly.

Much of the terminology that sometimes gets attributed to category theory is actually from abstract algebra, too. Things like monoid, semigroup, ring, group show up in very cool generalized algorithms. Much of linear algebra—the mathematics which underlies machine learning and vision and so much else—can be given an insightful depth through its abstract algebraic presentation (also, consider this one). Finally, some major ideas like injection, surjection, epimorphism, monomorphism, isomorphism, section, retraction, etc., etc. all find an initial, more easily grokked explanation in abstract algebra.

So, if you want to level up in these ways, consider an “intermediate” topic like abstract algebra instead. I strongly recommend Aluffi’s Algebra: Chapter 0, especially if you think you’d like to learn more category theory later.

Study category theory, too!

That all may feel like a real downer. In order to write it I had to embody the spirit of a old crumudgeon. Don’t do the fun thing, do the boring and straightforward thing!

So, let me be clear: totally do the fun thing, too! Just do it for fun, do it alongside the practical thing, and don’t expect that you’re going to get immediate gains from it. I absolutely encourage people to study Category Theory if they have the interest. It’s fascinating, large, and opens doors to gathering some big insights and values which are useful for programming.

If this seems like exactly what you’re after, then I’ve written another post that describes my recommended resources for learning category theory.

Study everything

Finally, it’s important to note that computation and specification, these key components of the practice we like to call coding, are deep and widely applicable ideas. Long before computers were invented have people been exploring these ideas seriously and they’ve truly blown up ever since.

The particular fields of category theory and abstract algebra play a certain kind of center stage in the “typed FP” world nowadays. People have found great success mining them for highly effective tools of abstraction and organization.

They’re far from the only topic of study that can help you level up your programming skills from an oblique angle. Here are several other areas of direct interest that I’ve found fruitful

  • Type theories and logic more directly. This can be thought of as the engineering discipline of making type systems as much as the theoretical discipline of studying languages with power. You can dig into constructivismand intuitionism to learn a powerful approach to thinking about computation.
  • Linear algebra on its own, as an applied domain, is hugely useful as a tool of geometry and data processing. It’s a great onramp to studying machine learning and optimization.
  • Formal methods often come up in relation to type theory, but they also live in their own space. Leslie Lamport’s TLA+ and related literature is a treasure trove here.
  • Compilers, running all the way down to machine bytecode, are often a rather detailed engineering discipline. At the same time, they have a lot of theoretical techniques based on analysis of language or abstract interpretation which fuel optimizations.
  • Topology, and perhaps especially its relation to logic via “pointless topology” provides a powerful tool for thinking about information and how it flows from place to place.

This is enough to chew on for years and years if you like. Beyond that, each one of these bullets could be a bottomless pit of new things to learn and use to influence your programming chops. 

And then, perhaps most amazingly, each and everyone one of these topics can find itself in category theory, too. The more you learn about each of these (and abstract algebra), the more prepared you’ll be to make the leap into category theory.

Good luck!