# Is Category Theory useful for learning functional programming?

I'm learning Haskell and I'm fascinated by the language. However I have no serious math or CS background. But I am an experienced software programmer.

I want to learn category theory so I can become better at Haskell.

Which topics in category theory should I learn to provide a good basis for understanding Haskell?

-
– Kaveh Aug 13 '12 at 5:25

## migrated from cstheory.stackexchange.comAug 3 '12 at 19:48

In a previous answer in the Theoretical Computer Science site, I said that category theory is the "foundation" for type theory. Here, I would like to say something stronger. Category theory is type theory. Conversely, type theory is category theory. Let me expand on these points.

Category theory is type theory

In any typed formal language, and even in normal mathematics using informal notation, we end up declaring functions with types $f : A \to B$. Implicit in writing that is the idea that $A$ and $B$ are some things called "types" and $f$ is a "function" from one type to another. Category theory is the algebraic theory of such "types" and "functions". (Officially, category theory calls them "objects" and "morphisms" so as to avoid treading on the set-theoretic toes of the traditionalists, but increasingly I see category theorists throwing such caution to the wind and using the more intuitive terms: "type" and "function". But, be prepared for protests from the traditionalists when you do so.)

We have all been brought up on set theory from high school onwards. So, we are used to thinking of types such as $A$ and $B$ as sets, and functions such as $f$ as set-theoretic mappings. If you never thought of them that way, you are in good shape. You have escaped set-theoretic brain-washing. Category theory says that there are many kinds of types and many kinds of functions. So, the idea of types as sets is limiting. Instead, category theory axiomatizes types and functions in an algebraic way. Basically, that is what category theory is. A theory of types and functions. It does get quite sophisticated, involving high levels of abstraction. But, if you can learn it, you will acquire a deep understanding of types and functions.

Type theory is category theory

By "type theory," I mean any kind of typed formal language, based on rigid rules of term-formation which make sure that everything type checks. It turns out that, whenever we work in such a language, we are working in a category-theoretic structure. Even if we use set-theoretic notations and think set-theoretically, still we end up writing stuff that makes sense categorically. That is an amazing fact.

Historically, Dana Scott may have been the first to realize this. He worked on producing semantic models of programming languages based on typed (and untyped) lambda calculus. The traditional set-theoretic models were inadequate for this purpose, because programming languages involve unrestricted recursion which set theory lacks. Scott invented a series of semantic models that captured programming phenomena, and came to the realization that typed lambda calculus exactly represented a class of categories called cartesian closed categories. There are plenty of cartesian closed categories that are not "set-theoretic". But typed lambda calculus applies to all of them equally. Scott wrote a nice essay called "Relating theories of lambda calculus" explaining what is going on, parts of which seem to be available on the web. The original article was published in a volume called "To H. B. Curry: Essays on Combinatory Logic, Lambda Calculus and Formalism", Academic Press, 1980. Berry and Curien came to the same realization, probably independently. They defined a categorical abstract machine (CAM) to use these ideas in implementing functional languages, and the language they implemented was called "CAML" which is the underlying framework of Microsoft's F#.

Standard type constructors like $\times$, $\to$, $List$ etc. are functors. That means that they not only map types to types, but also functions between types to functions between types. Polymorphic functions preserve all such functions resulting from functor actions. Category theory was invented in 1950's by Eilenberg and MacLane precisely to formalize the concept of polymorphic functions. They called them "natural transformations", "natural" because they are the only ones that you can write in a type-correct way using type variables. So, one might say that category theory was invented precisely to formalize polymorphic programming languages, even before programming languages came into being!

A set-theoretic traditionalist has no knowledge of the functors and natural transformations that are going on under the surface when he uses set-theoretic notations. But, as long as he is using the type system faithfully, he is really doing categorical constructions without being aware of them.

All said and done, category theory is the quintessential mathematical theory of types and functions. So, all programmers can benefit from learning a bit of category theory, especially functional programmers. Unfortunately, there do not seem to be any text books on category theory targeted at programmers specifically. The "category theory for computer science" books are typically targeted at theoretical computer science students/researchers. The book by Benjamin Pierce, Basic category theory for computer scientists is perhaps the most readable of them.

However, there are plenty of resources on the web, which are targeted at programmers. The Haskellwiki page can be a good starting point. At the Midlands Graduate School, we have lectures on category theory (among others). Graham Hutton's course was pegged as a "beginner" course, and mine was pegged as an "advanced" course. But both of them cover essentially the same content, going to different depths. University of Chalmers has a nice resource page on books and lecture notes from around the world. The enthusiastic blog site of "sigfpe" also provides a lot of good intuitions from a programmer's point of view.

The basic topics you would want to learn are:

• definition of categories, and some examples of categories
• functors, and examples of them
• natural transformations, and examples of them
• definitions of products, coproducts and exponents (function spaces), initial and terminal objects.
• monads, algebras and Kleisli categories

My own lecture notes in the Midlands Graduate School covers all these topics except for the last one (monads). There are plenty of other resources available for monads these days. So that is not a big loss.

The more mathematics you know, the easier it would be to learn category theory. Because category theory is a general theory of mathematical structures, it is helpful to know some examples to appreciate what the definitions mean. (When I learnt category theory, I had to make up my own examples using my knowledge of programming language semantics, because the standard text books only had mathematical examples, which I didn't know anything about.) Then came the brilliant book by Lambek and Scott called "Introduction to categorical logic" which related category theory to type systems (what they call "logic"). It is now possible to understand category theory just by relating it to type systems even without knowing a lot of examples. A lot of the resources I mentioned above use this approach to explain category theory.

-
i thought that if category theory were to make sense to a student, they should have some familiarity with abstract algebra. i.e. you should've seen an isomorphism to appreciate what this categories thing is about. and afaik there are some modern algebra texts that teach categories and algebra hand in hand from the start. are you saying that there are programming language examples, understandable to most programmers, that work just as well as the algebra examples in making category theory concrete? – Sasho Nikolov Aug 19 '12 at 18:53
@SashoNikolov Yes, the topics I listed in my post, viz., categories, functors, natural transformations, adjunctions and monads can be understood using just the category Set without any further knowledge of algebraic structures. Graham Hutton's and my notes from the Midlands Graduate School show how it can be done. Of course, knowing more mathematics helps. But it is not strictly necessary. I think we understand the connections with programming languages much better now. So we can explain it in our own terms. – Uday Reddy Aug 19 '12 at 21:58
(a) What is unrestricted recursion or why is it a problem for set-theoreticans? Can't you apply functions with defined domain and codomain as often as you want? Where is the restriction? (b) Is there a real reason why the category business gets mentioned with functional programming languages? Or is it just because it's conceptually closer to the definition of algorithms/lambda calculus? (c) Naive question: How are whole functional programming expressions (concatenations of functions) represented in category theory? Is it that one says this long concatenation equals some other morphism? – Nick Kidman Oct 17 '12 at 9:58
@UdayReddy I strongly disagree with your identification of category theory with type theory. Modern type theory is sustantially about types for concurrency processes, e.g. the theory tradition of session types. To the best of my knowledge there is no categorical understanding of such typing systems. – Martin Berger Jan 9 at 3:34
@MartinBerger I think your interpretation of "type theory" is a bit narrow. However, I agree that a proper type-theoretic and category-theoretic understanding of session types is currently a good research challenge, one that I intend to spend time on. – Uday Reddy Jan 9 at 7:07
show 16 more comments

I'm going to try and keep it short and sweet. There is an informal correspondence between Haskell programs and certain classes of categories, which can be made more formal with some work. This correspondence is known as the Curry-Howard-Lambek correspondence and relates:

1. Haskell types with objects of the category
2. Terms of type $A\rightarrow B$ with morphisms $f\colon A\rightarrow B$ (note the similar notations)
3. Algebraic datatypes with initial objects
4. Type constructors with functors
5. etc

The list goes on and on, but one crucial point is that you can define things like monads and algebras in category theory and come up with notions that are both useful to mathematicians but also pervasive in the practice of Haskell programming.

I'm not sure which book to recommend, as I haven't found a completely satisfactory introductory book on categories for computer scientists. You can try Categories, Types and Structues by Asperti and Longo. The idea is to learn basic definitions up to adjunctions, and then maybe try and read some of the excellent blogs out there to try and understand these concepts.

-
"come up with notions that are both useful to mathematicians but also pervasive in the practice of Haskell programming" -- can you give an example, or would that require too much prior knowledge? – Raphael Aug 11 '12 at 11:08
@Raphael: Monads. Arrows. Algebras. Coalgebras. – Dave Clarke Aug 11 '12 at 12:10
Functors, duality, the Kleisli category, the Yoneda lemma... – cody Aug 12 '12 at 9:25
Cartesion closed categories. Currying. – Dave Clarke Aug 12 '12 at 18:04
Point taken. I was thinking more of a presentation that would be accessible to somebody not familiar with category theory, though, that is explicitly laying out the construction/analogy. (The mere statement of the things does not show the layman anything.) – Raphael Aug 16 '12 at 8:46
show 1 more comment

A short answer: no [but this is only an opinion]

Dont go to Category Theory or any other theoretical domain to become good in Haskell. Learn functional programming techniques, such as tail recursion, map, reduce, and others. Read as much code as you can. Implement as many ideas as you can. If you have issues, read and read.

If you want a good theoritical reference to learn Haskell and other functional programming paradaigms then have a look at: An Introduction to Functional Programming Through Lambda Calculus, Greg Michaelson (available online). ... There are other similar books.

-

Echoing @AJed advice, I recommend to turn your statement

I want to learn category theory so I can become better at Haskell.


on its head: learn Haskell, building on your programming intuition. Once you are an FP guru, it might be easier to pick up category theory (if you still care).

Category theory is simple for somebody with broad mathematical education (groups, rings, modules, vector spaces, topology etc). Lacking this background, category theory is nearly impenetrable. The beauty of category theory is that it unifies a lot of seemingly unrelated things (e.g. left adjoints of forgetful functors include free groups, universal enveloping algebras, Stone-Cech compactifications, abelianisations of groups, ...), and so reduces complexity. But if you are not familiar with the multiple examples that category theory unifies, category theory is just an additional layer of complexity that makes your life harder.