On this page:
Category Theory in Programming
8.16.0.1

Category Theory in Programming🔗ℹ

Welcome to Category Theory in Programming, a journey into the conceptual world where mathematics meets software development. This tutorial is designed for Racket programmers who are curious about the mathematical ideas underlying computational systems. It offers insights into how familiar programming concepts can be reinterpreted through the lens of category theory, and even goes further to directly borrow from category theory, using programming language constructs to describe these abstract concepts.

In addition to this tutorial, you may find the following resources helpful for further exploration of category theory:

  • Category Theory for Computing Science by Michael Barr & Charles Wells

  • Computational Category Theory by D.E. Rydeheard & R.M. Burstall

  • Category Theory in Context by Emily Riehl

  • Category Theory by Steve Awodey

  • Categories for the Working Mathematician by Saunders Mac Lane

  • nLab

  • TheCatsters YouTube Channel

Category theory, a branch of mathematics that deals with abstract structures and relationships, may seem esoteric at first glance. However, its principles are deeply intertwined with the concepts and patterns we encounter in programming. Through this tutorial, we aim to bridge the gap between these two worlds, offering a unique perspective that enriches the programmer’s toolkit with new ways of thinking, problem-solving, and system design.

In the following chapters, we will explore the core concepts of category theoryobjects, morphisms, categories, functors, natural transformations, Yoneda Lemma, 2-categories, (co)limits, sketches, Cartesion closed categories & typed lambda, Curry–Howard–Lambek corresponding, adjunctions, (co)monads, kan-extensions, toposes, and more — and how these can be represented and utilized within the Racket programming language. The goal is not to exhaustively cover category theory or to transform you into a category theorist. Instead, we will focus on mapping these abstract concepts into programming constructs, providing a foundation that you, the reader, can build upon and apply in your work.

Why study category theory as a programmer? The answer lies in the abstraction and generalization capabilities provided by category theory. It allows us to see beyond the specifics of a particular programming language, problem, or system, revealing the underlying structures that are common across different domains. By identifying connections between a system and the constructs of category theory, you can leverage existing categorical results and structures to expand and improve the system, applying well-established theories and techniques to refine and extend your design. This tutorial aims to open the door to this broader perspective, enriching your approach to programming.

As you embark on this journey, keep in mind that the real value of understanding category theory in the context of programming is not merely in acquiring new knowledge but in developing a new way of thinking about problems or systems. We encourage you to approach the material with an open mind and to explore how the concepts presented here can be applied or extended in your programming endeavors.

Category Theory in Programming is an invitation to explore, to question, and to discover. It is a starting point for a deeper inquiry into the vast and fascinating intersection of mathematics and programming. We hope this tutorial will inspire you to delve further into both fields, exploring new ideas and forging connections that will enhance your work as a programmer.

Let the journey begin.

    1 Category

      1.1 Category

        1.1.1 Discrete Category

        1.1.2 One-Object Category

        1.1.3 Thin Category

        1.1.4 Cartesian Product

        1.1.5 Disjoint Union

        1.1.6 Hom Set

        1.1.7 Relation

          1.1.7.1 Binary Relation

          1.1.7.2 Equivalence Relation

          1.1.7.3 Congruence Relation

        1.1.8 Commutative Diagram

          1.1.8.1 Commutative Triangle

          1.1.8.2 Commutative Square

          1.1.8.3 Commutative Cube

      1.2 Mapping Category to Programming

        1.2.1 Category Examples

          1.2.1.1 Category of Natural Numbers

          1.2.1.2 Category of Lists

          1.2.1.3 Category of Strings

          1.2.1.4 Category of Relations

          1.2.1.5 Category of Pairs

          1.2.1.6 Category of Matrices

          1.2.1.7 Category of Sets

          1.2.1.8 Category of Procedures

        1.2.2 Constructions on Categories

          1.2.2.1 Opposite Category

          1.2.2.2 Subcategory

          1.2.2.3 Quotient Category

          1.2.2.4 Product Category

          1.2.2.5 Sum Category

          1.2.2.6 Arrow Category

          1.2.2.7 (Co)Slice Category

      1.3 Categories of Structured Sets

        1.3.1 Category of Monoids

        1.3.2 Category of Groups

        1.3.3 Category of Prosets

        1.3.4 Category of Posets

        1.3.5 Category of Tosets

        1.3.6 Category of Digraphs

      1.4 Categorical Definitions

        1.4.1 Parallel Morphism

        1.4.2 Endomorphism

          1.4.2.1 Idempotent

        1.4.3 Monomorphism and Epimorphism

        1.4.4 Split Morphism

        1.4.5 Isomorphism

          1.4.5.1 Automorphism

          1.4.5.2 Representative Subcategory

          1.4.5.3 Replete Subcategory

        1.4.6 Groupoid

        1.4.7 Initial Object and Terminal Object

          1.4.7.1 Global Element and Variable Element

          1.4.7.2 Pointed Object

        1.4.8 Subobject and Quotient Object

        1.4.9 Factorization System

          1.4.9.1 Orthogonal Factorization System

          1.4.9.2 Weak Factorization System

    2 Functor

      2.1 Functor

        2.1.1 Category of Categories

          2.1.1.1 Isomorphism of Categories

        2.1.2 Constant Functor

        2.1.3 Opposite Functor

        2.1.4 Essentially Injective Functor

        2.1.5 Essentially Surjective Functor

        2.1.6 Inclusion Functor

        2.1.7 Projection Functor

        2.1.8 Forgetful Functor

        2.1.9 Free Monoid Functor

        2.1.10 Free Category Functor

          2.1.10.1 Category of Trees

          2.1.10.2 Category of Forests

        2.1.11 Composition Functor

        2.1.12 (Co)Slice Functor

      2.2 Diagram

      2.3 𝐒𝐞𝐭-Valued Functor

        2.3.1 Category of Elements of a Functor

        2.3.2 Powerset Functor

          2.3.2.1 Direct Image Functor

          2.3.2.2 Preimage Functor

          2.3.2.3 Universal Image Functor

        2.3.3 Hom Functor

          2.3.3.1 Covariant Hom Functor

          2.3.3.2 Contravariant Hom Functor

          2.3.3.3 Two-Variable Hom Functor

        2.3.4 Cayley’s Theorem

        2.3.5 Action

          2.3.5.1 Monoid Actions as Functors

          2.3.5.2 𝐒𝐞𝐭-Valued Functors as Typed Actions

      2.4 Finite Automaton

        2.4.1 Deterministic Finite Automaton

        2.4.2 Typed Deterministic Finite Automaton

        2.4.3 Nondeterministic Finite Automaton

    3 Natural Transformation

      3.1 Natural Transformation

        3.1.1 Composition

          3.1.1.1 Horizontal Composition

          3.1.1.2 Vertical Composition

          3.1.1.3 Interchange Law

        3.1.2 Structure of 𝐂𝐚𝐭

          3.1.2.1 Horizontal Category

          3.1.2.2 Vertical Category

      3.2 Comma Category

      3.3 Yoneda Lemma

        3.3.1 Yoneda Embedding

        3.3.2 Universal Element

      3.4 2-Category

        3.4.1 Strict Monoidal Category

          3.4.1.1 Strict Symmetric Monoidal Category

        3.4.2 String Diagram

        3.4.3 Equivalence

    4 Appendix

    Index