# Understanding Category Theory Using Agda - Part 1

Published:

After a long silence, I decided to blog again. Now, I’ll start a series on implementing Category Theory using Agda.

My focus on this post series will be presenting the main definitions of category theory, implement them in Agda, show some examples of these definitions and prove some basic results. A good introduction to category theory are the posts by Bartosz Milewski. If you don’t know anything about Agda, I recommend this tutorial.

## What’s so special about category theory?

Everyone that starts to use functional programming languages, like Haskell, would hear something about category theory (that’s happened to me). Usually, people talk about category theory with some mistery or fear, due to its abstract content. But, essentially, category theory is about structure preserving transformations and how such transformations can be composed. Since, composition is essential to software design, category theory can be a great source of inspiration for ideas in programming.

Before talking about formal definitions of categories and how to code them in Agda, in this post, we will focus on monoids, that are a very useful algebraic structure that shares some similarities with categories.

## Monoids - A brief, but useful interlude

Before give a definition of what is a category, it’s useful to consider what is a monoid, since categories can be seen as a generalization of monoids.

A monoid consists of a set $A$, a binary operation $\circ : A \times A \to A$ and an element $\epsilon \in A$ which satisfies the following properties:

• $\forall x.\forall y. \forall z. x \circ (y \circ z) = (x \circ y) \circ z$.
• $x \circ \epsilon = x = \epsilon \circ x$

In Agda, we can define a record definition to code a monoid and its properties:

We represent both the requirements for and the algebraic structure of monoid using records. In Agda, records can be opened into scope like modules and it is a nice way to enforce some sort of information hiding, when implement algebraic structures.

Several well known structures are monoids. Addition and multiplication over natural numbers, multiplication over matrices are some examples. Below, we show how addition over natural numbers forms a monoid.

The previous source code piece declares a data type of natural numbers, Nat, its addition operation and some lemmas stating that addition is associative and zero is left and right unit element for it.

Lists and their concatenation operation are other example that forms a monoid. Such monoid is known as free-generated monoid.

## Freely generated monoids

Formally, given a set $S$, a list over $S$ is a pair $(n,f)$, where $n \in\mathbb{N}$ and $f : {1,…,n}\to S$ is a function such that $f(i)$ denotes the i-th list element, $i\in{1,…,n}$.

The empty list is represented by $[ ]$ and it is the unique list $(n,f)$ such that $n = 0$.

Given two lists, $L = (n,f)$ and $L’ = (n’,f’)$, we define their concatenation, $L ++ L’$ to be the list $(n + n’,g)$, where function $g$ is defined as:

• $g(i) = f(i)$; if $1 \leq i \leq n$
• $g(i) = f’(i - n)$; if $n + 1 \leq i \leq n + n’$

The monoid freely generated by a set $S$ is given by the triple $(List\,S, [], ++)$, where $List\,S$ is the set of lists of elements in S, $[] \in List\,S$, and $++$ is the list concatenation operation.

Agda list data type and its concatenation function forms a monoid, in the sense of the previous record definition. The next source code piece proves the monoid properties for lists and their concatenation.

Freely generated structures are almost everywhere in abstract algebra. In next post, we will talk about categories, their definition and how they can be freely generated.

Tags: