chapter seven

7 Basic category theory for programmers

 

This chapter covers

  • What is category theory, and why is it generalized abstract nonsense?
  • What are categories, objects, morphisms and functors?
  • What are covariance and contravariance? Why do language designers use this jargon?
  • How do all these things relate to the type systems of modern programming languages?

Fundamentally, programming computers is all about creating and using abstractions. At the “physics” level, computers are human-scale boxes containing billions of tiny cages where we control the comings and goings of small numbers of electrons. Chip designers have freed us from having to think about those electrons at all; at the “chip” level we think about bits and bytes, instructions, caches, interrupts and so on. Compiler developers add even more levels of abstraction such as variables, objects, functions, loops. When we write programs in high-level languages, we’re building algorithms and data structures that provide yet more abstractions on top of the fundamental ones provided by the language.

Can we be even more abstract in our thinking about computer programming? Absolutely we can! At various points throughout this book, we’ll take a short break from specific data structures and algorithms to look at some of the mathematical structures from category theory that underlie modern programming languages. There are lots of reasons to pursue this fabulous side quest:

7.1 Covariant endofunctors in the category of types

7.2 Contravariant endofunctors in the category of types

7.3 Summary