chapter seven

7 First abstract nonsense interlude: Category theory

 

This chapter covers

  • What is category theory, and why is it called generalized abstract nonsense?
  • What are categories, objects, morphisms, and functors?
  • What are covariance and contravariance?
  • 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 in which 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, and 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! At various points throughout this book, we’ll take short breaks 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 Category theory is generalized abstract nonsense

7.2 Covariant endofunctors in the category of types

7.3 Contravariant endofunctors in the category of types

Summary