In my previous post I worked on stretching the intuition of what a container is. I proposed that, in Haskell, any functor may be interpreted as some kind of container, including the hard cases like the state functor or the IO functor. I also talked about natural transformations between functors as “repackaging” schemes for containers, which work without “breaking the eggs” — not looking inside the elements stored in the container. Continuing with this analogy: Algebras are like recipes for making omelets.

The intuition is that an algebra provides a way to combine elements stored inside a container. This cannot be done for arbitrary types because there is no generic notion of “combining.” So an algebra is always defined for a specific type. For instance, you can define an algebra for numbers because you know how to add or multiply them, or for strings because you can concatenate them, and so on. The way elements are combined by an algebra is in general driven by the structure of the container itself.

For example, think of an expression tree as a container.

data Expr a = Const a | Add (Expr a) (Expr a) | Mul (Expr a) (Expr a)

We could define many algebras for it. An integer algebra would work on an expression tree that stores integers. A complex algebra would work on a tree that stores complex numbers. A Boolean algebra would work on Boolean expressions using, for instance, logical OR to evaluate the `Add`

node and logical AND for the `Mul`

node. You could even define an algebra of sets with union and intersection for `Add`

and `Mul`

. In fact, in the absence of any additional requirements, any pair of binary functions acting on a given type will do.

The definition of an algebra for a given functor `f`

consists of a type `t`

called the carrier type and a function called the action. Any Haskell algebra is therefore of the type:

newtype Algebra f t = Algebra (f t -> t)

More abstractly, in category theory, an algebra (or, more precisely, an F-algebra) for an endofunctor F is a pair (A, alg) of an object A and a morphism alg : F A -> A. As always, the standard translation from category theory to Haskell replaces objects with types and morphisms with functions.

Let’s have a look at a simple example of an algebra. Let’s pick the list functor and define an `Int`

algebra for it, for instance:

sumAlg :: Algebra [] Int sumAlg = Algebra (foldr (+) 0)

Despite its simplicity, this example leads to some interesting observations.

First, the use of `foldr`

tells us that it’s possible to handle recursion separately from evaluation. The evaluation is really parameterized here by the function `(+)`

and the value, zero. The algebra is type-specific. On the other hand, `foldr`

is fully polymorphic. It turns out that there is another algebra hidden in this example, and it’s determined just by `(+)`

and zero. We’ll see that more clearly when we talk about fixed points of functors.

The second observation is that a list is not only a functor but also a monad. Is there something special about algebras for a monad? We’ll see.

# Algebras and Fixed Points

I wrote a whole blog post about F-algebras with a more categorical slant. Here I’ll elaborate on the Haskell aspects of algebras and develop some more intuitions.

A recursive container is not only a functor but it can also be defined as a fixed point of a functor. So, really, we should start with a double functor, parameterized by two types, `a`

and `b`

:

data ExprF a b = Const a | Add b b | Mul b b deriving Functor

We can then find its fixed point: a type that, when substituted for `b`

, will give back itself. Think of a functor as a TV camera (sorry for switching metaphors). When you point it at some type `b`

, its image appears in all the little monitors where `b`

is on the right hand side of the definition. We all know what happens when you point the camera back at the monitors — you get the ever receding image within image within image… That’s your fixed point.

This “pointing of the camera at the monitors” can be abstracted into a Haskell data structure. It is parameterized by a functor `f`

, which provides the camera and the monitors. The fixed point is given by the ever receding:

newtype Fix f = In (f (Fix f))

Notice that, on the left hand side, `f`

appears without an argument. If `f a`

is a container of `a`

then `f`

by itself is a recipe for creating a container from any type. `Fix`

takes such a recipe and applies it to itself — to `(Fix f)`

.

Later we’ll also need the deconstructor, `unIn`

:

unIn :: Fix f -> f (Fix f) unIn (In x) = x

Going back to our earlier functor, we can apply `Fix`

to it and get back the recursive version of `Expr`

:

type Expr a = Fix (ExprF a)

Here, `(ExprF a)`

is a recipe for stuffing any type `b`

into a simple (non-recursive) container defined by `ExprF`

.

Creating actual expressions using the above definition of `Expr`

is a little awkward, but possible. Here’s one:

testExpr :: Expr Int testExpr = In $ (In $ (In $ Const 2) `Add` (In $ Const 3)) `Mul` (In $ Const 4)

Knowing that a recursive data type such as `(Expr a)`

is defined in terms of a simpler functor `(ExprF a b)`

means that any recursive algebra for it can be defined in terms of a simpler algebra. For instance, we can define a simple algebra for `(ExprF Int)`

by picking the carrier type `Double`

and the following action:

alg :: ExprF Int Double -> Double alg (Const i) = fromIntegral i alg (Add x y) = x + y alg (Mul x y) = x * y

We can extend this algebra to work on arbitrary recursive expressions of type `Expr Int`

. We’ll call this new recursive algebra `alg'`

. When given an `(Expr Int)`

it will do the following:

- Extract the contents of the outer
`Fix`

by pattern matching on the consturctor`In`

. The contents is of the type`ExprF`

acting on`(Expr Int)`

. - Apply
`alg'`

(the recursive one we are just defininig) to this contents. Do this using`fmap`

. Here we are taking advantage of the fact that`ExprF`

is a functor. This application of`alg'`

replaces the children of the expression`ExprF`

with`Double`

s — the results of their evaluation. - Apply
`alg`

to the result of the previous step, which is of the type`(ExprF Int Double)`

.

Here’s the code that implements these steps:

alg' :: Fix (ExprF Int) -> Double alg' (In expr) = alg (fmap alg' expr)

Notice that this code does not depend on the details of the functor. In fact it will work for any functor and any algebra:

cata :: Functor f => (f a -> a) -> Fix f -> a cata alg = alg . fmap (cata alg) . unIn

This generic function is called a catamorphism. It lets you apply an algebra to the contents of a recursively defined container.

My first example of an algebra was acting on a list. A list can also be defined as a fixed point of a functor:

data ListF a b = Empty | Cons a b deriving Functor

If you work out the details, you can convince yourself that the `sumAlg`

I defined earlier is nothing else but the catamorphism for the functor `ListF Int`

applied to the following simple algebra:

alg :: ListF Int Int -> Int alg Empty = 0 alg (Cons a b) = a + b

Now we understand why any list catamorphism is parameterized by one value and one function of two arguments.

# Monads and Algebras

As I said in the beginning, a list is not only a functor but also a monad. A monad adds two special abilities to a functor/container. It lets you create a default container that contains just a given value: The function that does it is called `return`

. And it lets you collapse a container of containers into a single container: That function is called `join`

(and I explained before how it relates to the more commonly used bind, `>>=`

).

When we define an algebra for a functor that happens to be a monad, it would be nice for this algebra to interact sensibly with `return`

and `join`

. For instance, you can apply `return`

to a value of the algebra’s carrier type to obtain a default container of that type. Evaluating such a container should be trivial — it should give you back the same value:

(1) alg . return == id

For instance, in the list monad `return`

creates a singleton list, so we want the algebra to extract the value from a singleton without modifying it in any way.

alg [a] = (alg . return) a = id a = a

Now let’s consider a container of containers of the carrier type. We have two ways of collapsing it: we can `fmap`

our algebra over it — in other words, evaluate all the sub-containers — or we can `join`

it. Expecting to get the same result in both cases would be asking a lot (but we get something like this in the Kleisli category later). We can demand though that, for an algebra to be compatible with a monad, the two resulting containers at least evaluate to the same thing:

(2) alg . fmap alg == alg . join

Let’s see what this condition means for lists, where `join`

is concatenation. We start with a list of lists and we apply two evaluation strategies to it: We can evaluate the sub-lists and then evaluate the resulting list of results, or we can concatenate the sub-lists and then evaluate the concatenated list.

Guess what, our condition is equivalent to imposing associativity on the algebra. Think of the action of the algebra on a two-element list as some kind of “multiplication.” Since the concatenation of `[a, [b, c]]`

is the same as the concatenation of `[[a, b], c]`

, these two must evaluate to the same value. But that’s just associativity of our “multiplication.”

How much can we extend this analogy with multiplication? Can we actually produce a unit element? Of course: The action of the algebra on an empty list:

e = alg []

Let’s check it: Apply our compatibility conditions to the list `[[a], []]`

. This is the left hand side:

(alg . fmap alg) [[a], []] = alg [alg [a], alg []] = alg [a, e]

And this is the right hand side:

(alg . join) [[a], []] = alg [a] = a

So, indeed, `e`

is the right unit of our “multiplication.” You can do the same calculation for `[[], [a]]`

to show that it’s also the left unit.

We have an associative operation equipped with a unit — that’s called a monoid. So any list algebra compatible with the list’s monadic structure defines a monoid.

## T-Algebras

An F-algebra that’s compatible with a monad (conditions (1) and (2) above), both built on the same functor, is called a T-algebra. I guess that’s because mathematicians replace F with T when they talk about monads. There may be many T-algebras for a given monad and in fact they form a category of their own.

This is not saying much, because requirements for a category are pretty minimal. You have to define arrows: here it would be homomorphisms of T-algebras. A homomorphism of algebras maps one carrier into another in such a way as to preserve the action.

In Haskell, a homomorphism of algebras would just be a function `h`

from one carrier type to another such that:

h :: A -> B alg :: F A -> A alg' :: F B -> B h . alg == alg' . fmap h

Here, `alg`

and `alg'`

are the two actions with carrier types `A`

and `B`

, respectively, and `F`

is the functor. What this means is that, if you have a container of As you can evaluate it using `alg`

and then apply `h`

to it and get a B, or you can apply `h`

to the contents of the container using `fmap`

and then evaluate the resulting container of Bs using `alg'`

. The result should be the same in both cases.

This is a pretty standard way of defining homomorphisms for any structure, not just an algebra. Homomorphisms behave like functions: they are composable and there always is an identity homomorphism for every algebra, so they indeed turn T-algebras into a category — the so called Eilenberg-Moore category.

Remember what I said about the compatibility between `join`

and `alg`

? They both take down one layer of containment. Other than that, they are very different: `join`

is a polymorphic natural transformation — it operates on the structure of the container, not its contents. An F-algebra operates on the contents and is defined only for a specific type.

And yet we can use `join`

to define a T-algebra. Just consider using a container as a carrier type. A container is an image of some type `a`

under a functor `m`

which, for our purposes also happens to be a monad. Apply `m`

to it one more time and you get a container of containers. You can “evaluate” this container of containers down to a single container using `join`

.

You have just defined an algebra for the functor `m`

whose carrier type is `(m a)`

and the action is `join`

. In fact, you have defined a whole family of algebras parameterized by the type `a`

. Keep in mind that `a`

is *not* the carrier type of this algebra, `(m a)`

is. These algebras are called *free algebras* for the monad `m`

. Guess what, they also form a category — the so called Kleisli category — which is a subcategory of the Eilenberg-Moore category.

Why are these two categories important? Well, it’s a topic for another blog post, but here’s the idea: Suppose you have two functors, F and G, one going from category C to D and the other going back. If G were the inverse of F, we would say that C and D are isomorphic. But what if they were “almost” inverse? For instance, their composition instead of being the identity were somehow mappable to identity. This kind of relationship between functors can be formalized into an *adjunction*. It so happens that the composition of two adjoint functors forms a monad (or a comonad, if you compose them the other way around). Not only that — *any* monad may be decomposed into a pair of adjoint functors. There are many ways to perform this decomposition and there are many choices for the intermediate category — the target of F and the source of G. The Kleisli category is the smallest such category and the Eilenberg-Moore category is the largest one.

January 30, 2014 at 6:40 am

Thanks for the great post! The TV camera metaphor is enlightening. Though, the Fix f definition is still pretty hard to understand. How and when are the parts in the type-level function evaluated? Is it all done at compile time? What part does lazy evaluation play if any? Heck, what part is math and what is just unnecessary Haskell trickery?

January 30, 2014 at 5:00 pm

Your comment made me change the definition of Fix from “data” to “newtype”. I just wasn’t thinking about performance, but a “data” definition incur some small runtime overhead, whereas “newtype” doesn’t. This is now all compile-time. It’s like a template in C++.

February 11, 2014 at 8:31 am

[…] You Can't Make an Algebra without Breaking a Few Eggs | Bartosz … https://bartoszmilewski.com/A Boolean algebra would work on Boolean expressions using, for instance, logical OR to evaluate the Add node and logical AND for the Mul node. You could … Let's have a look at a simple example of an algebra. Let's pick … […]

March 12, 2014 at 3:17 pm

You might want to take a look on my post about virtual constructors in C++ here:

http://blog.panqnik.pl/dev/co-variant-and-virtual-constructor-in-c/

Pozdrawiam 🙂

February 8, 2017 at 7:12 am

Is there any explanation for the constructor name “In”?

February 8, 2017 at 10:32 am

@Harendra Kumar: If you think of a functor as a container, we are stacking containers inside containers, inside containers, and so on. So if

`Fix f`

is this stack of containers, we are putting it inside another container`f`

.