## Fractals and Monads — Part 1

A while back, I posted a bunch of fractal artwork that I did for fun, based on variations of the Koch snowflake; like this one, for example:

This is composed entirely of equilateral triangles, but it’s just one step in an iterated process leading to a fractal consisting of infinitely many Koch snowflakes at ever smaller scales.

I had a lot of fun coming up with designs like this, but actually I didn’t start out wanting to do new things with the Koch snowflake—that was just a fun tangent.  Originally, I was thinking about monads!

It’s high time I got back to what I was originally working on and explained what these fractals have to do with the “power set monad.”  To do that, I should probably start by explaining what the power set monad is.

For any set $X$ the power set $\mathcal{P} X$ is the set of all its subsets.  If you’ve ever studied any set theory, this should be familiar.  Unfortunately, a typical first course in set theory may not go into the how power sets interact with functions; this is what the “monad” structure of power sets is all about. This structure has three parts, each corresponding to a very common and important mathematical construction, but also so simple enough that we often use it without even thinking about it.

Let me finally say what the monad structure of power sets actually is.

First, mapping sets to their power sets is actually a functor:  For any function $f\colon X \to Y$, there corresponds a function

$\mathcal{P}f\colon \mathcal{P}X \to \mathcal{P}Y$

which sends each element $S \in \mathcal{P}X$, i.e. each subset $S \subseteq X$, to its image:

$(\mathcal{P}f)(S) = \{ f(s) : s \in S\}$.

This makes $\mathcal{P}$ a functor because the mapping of functions gets along nicely with identity functions and composition.   This functor is the first part of the power set monad.

You might be accustomed to writing the image of the set $S$ under $f$ simply as $\text{}f(S)\text{''}$, rather than $\mathcal{P}f(S)$.  But here, we need to be more careful: despite its frequent use in mathematics, $\text{}f(S)\text{''}$ is a notational abuse for which $\mathcal{P}f(S)$ is the proper form.

To motivate the next piece in the power set monad, notice that there’s a strong sense in which $\mathcal{P}f$ is just an extension of $f$, namely, on singleton sets, it essentially “is” $f$:

$\mathcal{P}f(\{ x \}) = \{f(x)\}$.

This might make it tempting write $\mathcal{P}f( x ) = f(x)$ for $x \in X$, as sort of “dual” version of the previous abuse of notation, though I don’t think anyone does that.

A better way to express how $\mathcal{P}f$ extends $f$ is to introduce the canonical map sending elements of $X$ to the corresponding singleton sets:

$\displaystyle \begin{array}{rcl} \eta_X \colon X & \to & \mathcal{P}X \\ x & \mapsto & \{x\} \end{array}$

Then, the previous equation can be written $\mathcal{P}f \circ \eta_X = \eta_Y \circ f$, which is precisely the equation that makes $\eta$ a natural transformation. This natural transformation is the second piece in the monad structure.

Anyone who’s taught elementary set theory can attest that some students have a hard time at first making with conceptual distinction between elements and singleton subsets of a set.  Indeed, conflating the two is a very “natural” thing to do, which is why this is a natural transformation.

The last piece of the power set monad is another natural transformation. Much like $\eta$, which formalizes a way to think of elements as being subsets, there is a natural transformation that lets us think of sets of subsets as being subsets.  This is given by, for each set $X$, a map:

$\displaystyle \begin{array}{rcl} \mu_X \colon \mathcal{P}^2X & \to & \mathcal{P}X \\ S & \mapsto & \bigcup_{U\in S} U \end{array}$

Here $\mathcal{P}^2X$ is the power set of the power set of $X$, so an element of it is a sets of subsets of $X$, and we turn this into a single subset by taking the union.

That’s it!  Or, almost.  To form a monad, the data $\mathcal{P}, \eta, \mu$ must be compatible in such a way that these diagrams commute:

It’s a simple exercise to check that this is indeed the case.  You can extract the general definition of monad from what I’ve written here: you need an endofunctor on some category, and two natural transformations such that the diagrams analogous to those above commute.

Now let’s come back to fractals:

Many kinds of fractals are created by taking a simple shape, making multiple copies, transforming them somehow—for example, by shrinking or rotating—and then repeating.

For example, here’s one way to build a Sierpinski triangle, or rather its complement, staring from a single triangle and a rule for how triangles should “reproduce”:

If we look closely, we see that this process naturally involves ingredients from the power set monad.

In particular, the basic building block is the self-replication rule that we can write as a function

$f\colon T \to \mathcal{P}T$

where $T$ is the set of triangles in the plane.  We won’t need an explicit formula for the function, since a picture suffices to explain what it does:

We can get all of the triangular holes in the Sierpinski triangle by “iterating” f. But, what does this mean exactly?  It would be nice if we could just do $\text{}f(f(t))\text{''}$, but, strictly speaking, this doesn’t parse: we can’t feed a set of triangles into $f$, since this is a function that expects a single triangle as input.

Fortunately, we know the solution to this problem: since $f(t) \in \mathcal{P}T$, we can’t feed it into $f$, but we can feed it into $\mathcal{P}f$.  Good so far.

The problem with this is that $\mathcal{P}f(f(t))$ again doesn’t live in $T$, nor does it live in $\mathcal{P}T$—it lives in $\mathcal{P}^2T$:

$\mathcal{P}f \colon \mathcal{P}T \to \mathcal{P}^2 T$

It seems we would be doomed to march into ever more deeply-nested sets, were it not for the natural transformation $\mu$.  Fortunately, using $\mu_T$ we get

$\mu_T \circ \mathcal{P}f \colon \mathcal{P}T \to \mathcal{P} T$

So, now we can see the process of building a fractal as just repeatedly applying this function $\mu_T \circ \mathcal{P}f$.

One last problem: What do we apply it to?  We want to apply it to our initial triangle $t$, but that’s an element of $T$, not $\mathcal{P}T$.  Good thing we can think of this as an element of $\mathcal{P}T$ using the natural transformation $\eta$!

So, here’s a recipe to build a wide variety of fractals, like some of the ones I’ve posted on this blog:

but also tons of others, using the power set monad:

1. Start with a replication rule $f\colon X \to \mathcal{P}X$ on some set $X$ of “shapes.”
2. Pick a shape $x\in X$.
3. Feed your initial shape into $\eta_X$ to get the initial state.
4. Iterate feeding the current state into $\mu_X \circ \mathcal{P} T$ to get a new state, as many times as you want.

In fact, in calling this a “recipe,” I mean more than just an abstract mathematical recipe: this is essentially how I really drew all of those fractal pictures.  I generated them using Haskell—a functional programming language that knows about monads.  I didn’t use the power set monad, but rather Haskell’s built in “list monad,” which works very similarly.

Tags: ,

### 3 Responses to “Fractals and Monads — Part 1”

1. Fractals and Monads – Part 2 | Simplicity Says:

[…] I discussed the power set monad and how it shows up in certain methods of building fractals.  This time I’ll go a bit further, and also explain some Haskell code I actually use to generate fractal pictures […]

2. Michael Says:

I’m not sure whether your equivocation on the word ‘natural’ is endearing or like saying ‘like’, like whenever.

• Derek Wise Says:

Hi Michael. I suppose you are referring to where I wrote that a certain natural transformation was a very “natural” thing to do. I was only half joking. The word “natural” has a very precise meaning in category theory, and I was conflating that technical meaning with the colloquial meaning. Sometimes having tons of adjectives with technical definitions in mathematics can be annoying, because you’re not free to use the corresponding colloquial adjectives without alerting the reader. On the other hand, in the case of “natural transformations” I do at least think the adjective is very well chosen: The definition very often encapsulates the most natural thing one could do. (Although, perhaps I’ve been biased by my upbringing.)