liveBook · Manning

Category theory

As explained in Fabulous Adventures in Data Structures and Algorithms

Category theory is a mathematical framework that studies objects and structure-preserving mappings between them.

How can two very different programming problems share a useful structure? Category theory looks past what things are and studies the relationships between them. Its practical appeal is composition: solve smaller problems, then connect their solutions. section 7.0 section 7.1

A category contains objects and morphisms—arrows from one object to another. Here, “object” does not mean an object in the object-oriented programming sense. Morphisms are reflexive and transitive: each object has an arrow to itself, and arrows from A to B and B to C imply one from A to C. section 7.1 section 7.4

Figure 7.1 A small category with three objects, represented by the nodes, and six morphisms, represented by the arrows

The nodes and arrows make the distinction visible: objects are the things being related; morphisms are the relationships. section 7.1

Mappings between categories introduce a second question: what happens to the arrows?

  • A covariant functor maps objects to objects while preserving morphisms and their direction. section 7.4
  • A contravariant functor preserves the relationships but reverses their direction. A functor whose source and destination are the same category is an endofunctor. section 7.4

Types as objects

Take types as objects and assignment compatibility as an arrow: X→Y means an expression of type X may be assigned to a variable of type Y. This makes covariance and contravariance of generic interfaces questions about whether a type-to-type mapping preserves or reverses those arrows. Posetal categories and semilattices provide further ways to examine such type relationships. section 7.4

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Defines categories, morphisms, and functors, then connects them to type relationships; stops short of a deep treatment.
Examples
Uses arrow diagrams for small categories and assignment compatibility between types as a programming example.
What it assumes
Assumes familiarity with programming abstractions and generic interfaces while defining category-theory terms from the ground up.
What you can do after
Evaluate/understand how generic-interface variance relates to mappings that preserve or reverse assignment-compatibility arrows.
How it connects
Connects functors to covariance, contravariance, generic interfaces, assignment compatibility, posetal categories, and semilattices.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.