# Category theory

Category theory can be helpful in understanding Haskell's type system. There exists a "Haskell category", of which the objects are Haskell types, and the morphisms from types a to b are Haskell functions of type a -> b.

The Haskell wikibooks has an introduction to Category theory, written specifically with Haskell programmers in mind.

## Definition of a category

A category ${\displaystyle {\mathcal {C}}}$consists of two collections:

Ob${\displaystyle ({\mathcal {C}})}$, the objects of ${\displaystyle {\mathcal {C}}}$

Ar${\displaystyle ({\mathcal {C}})}$, the arrows of ${\displaystyle {\mathcal {C}}}$ (which are not the same as Arrows defined in GHC)

Each arrow ${\displaystyle f}$ in Ar${\displaystyle ({\mathcal {C}})}$ has a domain, dom ${\displaystyle f}$, and a codomain, cod ${\displaystyle f}$, each chosen from Ob${\displaystyle ({\mathcal {C}})}$. The notation ${\displaystyle f\colon A\to B}$ means ${\displaystyle f}$ is an arrow with domain ${\displaystyle A}$ and codomain ${\displaystyle B}$. Further, there is a function ${\displaystyle \circ }$ called composition, such that ${\displaystyle g\circ f}$ is defined only when the codomain of ${\displaystyle f}$ is the domain of ${\displaystyle g}$, and in this case, ${\displaystyle g\circ f}$ has the domain of ${\displaystyle f}$ and the codomain of ${\displaystyle g}$.

In symbols, if ${\displaystyle f\colon A\to B}$ and ${\displaystyle g\colon B\to C}$, then ${\displaystyle g\circ f\colon A\to C}$.

Also, for each object ${\displaystyle A}$, there is an arrow ${\displaystyle \mathrm {id} _{A}\colon A\to A}$, (often simply denoted as ${\displaystyle 1}$ or ${\displaystyle \mathrm {id} }$, when there is no chance of confusion).

### Axioms

The following axioms must hold for ${\displaystyle {\mathcal {C}}}$ to be a category:

1. If ${\displaystyle f\colon A\to B}$ then ${\displaystyle f\circ \mathrm {id} _{A}=\mathrm {id} _{B}\circ f=f}$ (left and right identity)
2. If ${\displaystyle f\colon A\to B}$ and ${\displaystyle g\colon B\to C}$ and ${\displaystyle h\colon C\to D}$, then ${\displaystyle h\circ (g\circ f)=(h\circ g)\circ f}$ (associativity)

### Examples of categories

• Set, the category of sets and set functions.
• Mon, the category of monoids and monoid morphisms.
• Monoids are themselves one-object categories.
• Grp, the category of groups and group morphisms.
• Rng, the category of rings and ring morphisms.
• Grph, the category of graphs and graph morphisms.
• Top, the category of topological spaces and continuous maps.
• Preord, the category of preorders and order preserving maps.
• CPO, the category of complete partial orders and continuous functions.
• Cat, the category of categories and functors.
• the category of data types and functions on data structures
• the category of functions and data flows (~ data flow diagram)
• the category of stateful objects and dependencies (~ object diagram)
• the category of values and value constructors
• the category of states and messages (~ state diagram)

### Further definitions

With examples in Haskell at:

## Categorical programming

Catamorphisms and related concepts, categorical approach to functional programming, categorical programming. Many materials cited here refer to category theory, so as an introduction to this discipline see the #See also section.

## Haskell libraries and tools

• Originally, there was a package, Category extras, by David Menendez: libraries for e.g. comonads, infinite data types. This package has been superseded by other more-focused and self-contained packages, as documented in the category-extras metapackage in Hackage.

## Books

• Michael Barr and Charles Wells: Toposes, Triples and Theories. The online, freely available book is both an introductory and a detailed description of category theory. It also contains a category-theoretical description of the concept of monad (but calling it a triple instead of monad).
• R. F. C. Walters: Categories and Computer Science. Category Theory has, in recent years, become increasingly important and popular in computer science, and many universities now introduce Category Theory as part of the curriculum for undergraduate computer science students. Here, the theory is developed in a straightforward way, and is enriched with many examples from computer science.
• Arbib&Manes: Arrow, Structures and Functors - The Categorical Imperative. (c)1975 Academic Press, ISBN 0-12-059060-3. Sadly now out of print but very little prerequisite knowledge is needed. It covers monads and the Yoneda lemma.