Category Theory for Java Programmers

October 27, 2024

Summary

The simple rules of set theory were helpful when I was working on a new Java application.

Understanding how sets and their functions work is a useful tool to have in your programming tool kit.

Start with defining the sets of things that define your domain; for example, HttpRequest → DomainView → HttpResponse. These are pretty easy to define and allow you to reason about the problem you are solving at a more general level.

Then, define the functions that map from one set to another. Note that in set theory, a valid function must map

  1. from a source set to a target set such that
  2. every element in source set maps to
  3. one and only one element in the target set.

This rules out one-to-many and many-to-may relationships.

The rest of this blog gives a summary of set theory, as To get the PDF, you can either click the “Get PDF” link in the on-line book or build the PDF yourself from category-theory-illustrated repository on github. described in Category Theory Illustrated by Jencel Panic (previously known as Boris Marinov), with a brief hat tip to Bartosz Milewski’s book in the final section.

I hope to write up a follow-up post that gives more technical detail of how this approach worked with Java when tackling a specific problem.

Sets

Start on page 21 of the PDF. Category Theory Illustrated does not have a table of contents, so I’ll note the PDF page number where the topic starts.

When these notes are direct quotes, I’ll put them in quotes.

Functions

Start on page 27 of the PDF.

A function “is a relationship between two sets.”

A function “matches each element in one set … to exactly one element in another set.”

There are lots of names for these two sets:

source -> target

domain -> codomain

input -> output

argument type -> return type    programming

premise -> conclusion           logic

as well as for functions themselves; a function can

go from one set to another,

connect one set to another, or

convert a value from one set to another.

Types of functions:

  1. one-to-one: each source element maps to exactly one target element, and the reverse is true.

  2. many-to-one: maps many source elements to one target element, and the reverse is not true. These types of functions categorize the elements in the source set.

A couple rules about functions:

  1. “You cannot have a source element that is not mapped to anything.”

  2. You cannot have a source element “that is mapped to more than one target element”.

Rule #2 means that a function cannot represent a many-to-many relationship.

Real world examples of functions:

How far are we from New York?

    This function connects

        all places in the world (source set)

    to

        all positive numbers (target set)


Who is my father?

    This function connects

        all people in the world

    to

        a singleton set.

There are a number of special functions:

There is an interesting quote in this section that suggests that a set itself can be represented by a function: “You can think of the IDG as a function which represents the set G in the realm of functions.” (I don’t understand here what he means by the “realm of functions.”)

A note on Bartosz Milewski’s book

Bartosz Milewski’s book Category Theory For Programmers You can build a PDF of this book from the github project milewski-ctfp-pdf . is another Google hit when searching for category theory for programmers.

The first chapter of Category Theory for Programmers gets right to business, defining a category as a thing with objects and arrows, whose fundamental property is composition. His presentation is both a clear and concise introduction to a category, and is copied below.

Given:

    a function f that takes an "A" and returns a "B"
    that is,

           f :: A -> B

    and a function g that takes a "B" and returns an "C"

           g :: B -> C


Then if A, B and C belong to the same category you can define
a new function h that composes these two:

          h :: A -> C

          h a = g(f(a))

or, more simply,

          h = g . f

The notation here is the Haskell programming language. You should read the period in that last line as “after”, as in “apply g after f”.

As a beginner, I found the graphical, set-based introduction of Category Theory Illustrated easier to understand.

Tags: functional