Contents

1 Algebraic objects in categories

We recall the definition of a monoid:

Definition A monoid is a set M equipped with an operation that we call composition and an operation that we call the identity, such that

(associativity)

(unity)

If we have a monoidal category - a category C with a bifunctor called the tensor product which is associative (up to natural isomorphisms) and has an object I acting as a unit (up to natural isomorphisms) for the tensor product.

The product in a category certainly works as a tensor product, with a terminal object acting as a unit. However, there is often reason to have a non-commutative tensor product for the monoidal structure of a category. This makes the category a cartesian monoidal category.

For, say, abelian groups, or for vector spaces, we have the tensor product forming a non-cartesian monoidal category structure. And it is important that we do.

And for the category of endofunctors on a category, we have a monoidal structure induced by composition of endofunctors: . The unit is the identity functor.

Now, we can move the definition of a monoid out of the category of sets, and define a generic monoid object in a monoidal category:

Definition A monoid object in a monoidal category C is an object M equipped with morphisms and such that

(associativity)

(unity)

As an example, a monoid object in the cartesian monoidal category Set is just a monoid. A monoid object in the category of abelian groups is a ring.

A monoid object in the category of abelian groups, with the tensor product for the monoidal structure is a ring.

And the composition UF for an adjoint pair is a monoid object in the category of endofunctors on the category.

The same kind of construction can be made translating familiar algebraic definitions into categorical constructions with many different groups of definitions. For groups, the corresponding definition introduces a diagonal map , and an inversion map to codify the entire definition.

One framework that formalizes the whole thing, in such a way that the definitions themselves form a category is the theory of Sketches by Charles Wells. In one formulation we get the following definition:

Definition A sketchS = (G,D,L,K) consists of a graph G, a set of diagrams D, a set L of cones in G and a set K of cocones in G.

A model of a sketch S in a category C is a graph homomorphism such that the image of each diagram in D is commutative, each of the cones is a limit cone and each of the cocones is a colimit cocone.

A homomorphism of models is just a natural transformation between the models.

We thus define a monad in a category C to be a monoid object in the category of endofunctors on that category.

Specifically, this means:

Definition A monad in a category C is an endofunctor equipped with natural transformations and such that the following diagrams commute:

Those of you used to Haskell will notice that this is not the same as the

Monad

typeclass. That type class calls for a natural transformation

(>>=):: m a ->(a -> m b)-> m b

(or

bind

).

The secret of the connection between the two lies in the Kleisli category, and a way to build adjunctions out of monads as well as monads out of adjunctions.

2 Kleisli category

We know that an adjoint pair will give us a monad. But what about getting an adjoint pair out of a monad? Can we reverse the process that got us the monad in the first place?

There are several different ways to do this. Awodey uses the Eilenberg-Moore category which has as objects the algebras of the monad T: morphisms . A morphism is just some morphism in the category C such that .

We require of T-algebras two additional conditions:

(unity)

(associativity)

There is a forgetful functor that takes some h to t(h), picking up the object of the T-algebra. Thus , and U(f) = f.

We shall construct a left adjoint F to this from the data of the monad T by setting , making TC the corresponding object. And plugging the corresponding data into the equations, we get:

which we recognize as the axioms of unity and associativity for the monad.

By working through the details of proving this to be an adjunction, and examining the resulting composition, it becomes clear that this is in fact the original monad T. However - while the Eilenberg-Moore construction is highly enlightening for constructing formal systems for algebraic theories, and even for the fixpoint definitions of data types, it is less enlightening to understand Haskell's monad definition.

To get to terms with the Haskell approach, we instead look to a different construction aiming to fulfill the same aim: the Kleisli category:

Given a monad T over a category C, equipped with unit η and concatenation μ, we shall construct a new category K(T), and an adjoint pair of functors U,F factorizing the monad into T = UF.

We first define K(T)0 = C0, keeping the objects from the original category.

Then, we set K(T)1 to be the collection of arrows, in C, on the form .

The composition of with is given by the sequence

The identity is the arrow . The identity property follows directly from the unity axiom for the monad, since ηA composing with μA is the identity.

Given this category, we next define the functors:

U(A) = TA

F(A) = A

This definition makes U,F an adjoint pair. Furthermore, we get

UF(A) = U(A) = TA

, and by naturality of μ, we can rewrite this as

by unitality of η.

We've really just chased through this commutative diagram:

Hence, the composite UF really is just the original monad functor T.

But what's the big deal with this? you may ask. The big deal is that we now have a monad specification with a different signature. Indeed, the Kleisli arrow for an arrow

f :: a -> b

and a monad

Monad m

is something on the shape

fk :: a -> m b

. And the Kleisli factorization tells us that the Haskell monad specification and the Haskell monad laws are equivalent to their categorical counterparts.

3.1 The List monad

As it turns out, the lists monad can be found by considering the free and forgetful functors between sets and monoids. Indeed, the lists are what we get from the Kleene star operation, which is the monad we acquire by composing the free monoid functor with the forgetful functor.

3.2 Error handling

We can put a monadic structure on a coproduct A + B so that the monadic bind operation performs computations until some computation fails, returning an error, typed B, after which we bypass any further computations, just carrying the error out of the entire computation.

The endofunctor here is + B. So the monad is given from a way to go from . Doing this is easy: in Haskell terms, we just remove the constructor differences between the two copies of B floating around. Mathematically, this is just using the functoriality of the coproduct construction on the inclusion maps into A + B.

For our example, we shall return the first value of B to ever occur, thus making our join operator look like this: