One of the tropes of detective movies is the almost miraculous ability to reconstruct an image from a blurry photograph. You just scan the picture, say “enhance!”, and voila, the face of the suspect or the registration number of their car appear on your computer screen.

Computer, enhance!

With constant improvements in deep learning, we might eventually get there. In category theory, though, we do this all the time. We recover lost information. The procedure is based on the basic tenet of category theory: an object is defined by its interactions with the rest of the world. This is the basis of all universal constructions, the Yoneda lemma, Grothendieck fibration, Kan extensions, and practically everything else.

An iconic example is the construction of the left adjoint to a given functor, and that’s what we are going to study here. But first let me explain why I decided to pick this subject, and how it’s related to programming. I wanted to write a blog post about CPS (continuation passing style) and defunctionalization, and I stumbled upon an article in nLab that related defunctionalization to Freyd’s Adjoint Functor Theorem; in particular to the Solution Set Condition. Such an unexpected connection peaked my interest and I decided to dig deeper into it.

Consider a functor $R$ from some category $\mathcal D$ to another category $\mathcal C$.

$R \colon D \to C$

A functor, in general, loses some data, so it’s normally impossible to invert it. It produces a “blurry” image of $\mathcal D$ inside $\mathcal C$. Its left adjoint is a functor from $\mathcal C$ to $\mathcal D$

$L \colon C \to D$

that attempts to reconstruct lost information, to the best of its ability. Often the functor $R$ is forgetful, which means that it purposefully forgets some information. Its left adjoint is then called free, because it freely ad-libs the forgotten information.

Of course it’s not always possible, but under certain conditions such left adjoint exists. These conditions are spelled out in the Freyd’s General Adjoint Functor Theorem.

To understand them, we have to talk a little about size issues.

# Size issues

A lot of interesting categories are large. It means that there are so many objects in the category that they don’t even form a set. The category of all sets, for instance, is large (there is no set of all sets). It’s also possible that morphisms between two objects don’t form a set.

A category in which objects form a set is called small, and a category in which hom-sets are sets is called locally small.

A lot of complexities in Freyd’s theorem are related to size issues, so it’s important to precisely spell out all the assumptions.

We assume that the source of the functor $R$, the category $\mathcal D$, is locally small. It must also be small-complete, that is, every small diagram in $\mathcal D$ must have a limit. (A small diagram is a functor from a small category.) We also want the functor $R$ to be continuous, that is, to preserve all small limits.

If it weren’t for size issues, this would be enough to guarantee the existence of the left adjoint, and we’ll first sketch the proof for this simplified case. In the general case, there is one more condition, the Solution Set Condition, which we’ll discuss later.

# Left adjoint and the comma category

Here’s the problem we are trying to solve. We have a functor $R$ that maps objects and morphisms from $\mathcal D$ to $\mathcal C$. We want to define another functor $L$ that goes in the opposite direction. We’re not looking for the inverse, so we’re not expecting the composition of this functor with $R$ to be identity, but we want it to be related to identity by two natural transformations called unit and counit. Their components are, respectively:

$\eta_c : c \to R L c$

$\epsilon_d : L R d \to d$

and, as long as they satisfy some additional triangle identities, they will establish the adjunction $L \dashv R$.

We are going to define $L$ point-wise, so let’s pick an object $c$ in $\mathcal C$ and try to propagate it back to $\mathcal D$. To do that, we have to gather as much information about $c$ as possible. We will propagate all this information back to $\mathcal D$ and find an object in $\mathcal D$ that “looks the same.” Think of this as creating a hologram of $c$ and shipping it back to $\mathcal D$.

All information about $c$ is encoded in morphisms so, in order to generate our hologram, we’ll gather all morphisms that originate in $c$. These morphisms form a category called the coslice category $c/C$.

The objects in $c/C$ are pairs $(x, f \colon c \to x)$. In other words, these are all the arrows that emanate from $c$, indexed by their target objects $x$. But what really defines the structure of this category are morphisms between these arrows. A morphism in $c/C$ from $(x, f)$ to $(y, g)$ is a morphism $h \colon x \to y$ that makes the following triangle commute:

We now have complete information about $c$ encoded in the slice category, but we have no way to propagate it back to $\mathcal D$. This is because, in general, the image of $\mathcal D$ doesn’t cover the whole of $\mathcal C$. Even more importantly, not all morphisms in $\mathcal C$ have corresponding morphisms in $\mathcal D$. We have to scale down our expectations, and define a partial hologram that does not capture all the information about $c$; only this part which can be back-propagated to $\mathcal D$ using the functor $R$. Such partial hologram is called a comma category $c/R$.

The objects of $c/R$ are pairs $(d, f \colon c \to R d)$, where $d$ is an object in $\mathcal D$. In other words, these are all the arrows emanating from $c$ whose target is in the image of $R$. Again, the important structure is encoded in the morphisms of $c/R$. These are the arrows in $\mathcal D$, $h \colon d \to d'$ that make the following diagram commute in $\mathcal C$

Notice an interesting fact: we can interpret these triangles as commutation conditions in a cone whose apex is $c$ and whose base is formed by objects and morphisms in the image of $R$. But not all objects or morphism in the image of $R$ are included. Only those morphisms that make the appropriate triangle commute–and these are exactly the morphisms that satisfy the cone condition. So the comma category builds a cone in $\mathcal C$.

# Constructing the limit

We can now take all this information about $c$ that’s been encoded in $c/R$ and move it back to $\mathcal D$. We define a projection functor $\pi_c \colon c/R \to D$ that maps $(d, f)$ to $d$, thus forgetting the morphism $f$. What’s important, though, is that this functor keeps the information encoded in the morphisms of $c/R$, because these are morphisms in $\mathcal D$.

The image of $\pi_c$ doesn’t necessarily cover the whole of $\mathcal D$, because not every $R d$ has arrows coming from $c$. Similarly, only some morphisms, the ones that make the appropriate triangle in $\mathcal C$ commute, are picked by $\pi_c$. But those objects and morphisms that are in the image of $\pi_c$ form a diagram in $\mathcal C$. This diagram is our partial hologram, and we can use it to pick an object in $\mathcal D$ that looks almost exactly like $c$. That object is the limit of this diagram. We pick the limit of this diagram as the definition of $L c$: the left adjoint of $R$ acting on $c$.

Here’s the tricky part: we assumed that $\mathcal D$ was small-complete, so every small diagram has a limit; but the diagram defined by $\pi_c$ is not necessarily small. Let’s ignore this problem for a moment, and continue sketching the proof. We want to show that the mapping that assigns the limit of $\pi_c$ to every $c$ is left adjoint to $R$.

Let’s see if we can define the unit of the adjunction:

$\eta_c : c \to R L c$

Since we have defined $L c$ as the limit of the diagram $\pi_c$ and $R$ preserves limits (small limits, really; but we are ignoring size problems for the moment) then $R L c$ must be the limit of the diagram $R \pi_c$ in $\mathcal C$. But, as we noted before, the diagram $R \pi_c$ is exactly the base of the cone with the apex $c$ that we used to define the comma category $c/R$. Since $R L c$ is the limit of this diagram, there must be a unique morphism from any other cone to it. In particular there must be a morphism from $c$ to it, because $c$ is an apex of the cone defined by the comma category. And that’s the morphism we’ll chose as our $\eta_c$.

Incidentally, we can interpret $\eta_c$ itself as an object of the comma category $c/R$, namely the one defined by the pair $(Lc, \eta_c \colon c \to R L c)$. In fact, this is the initial object in that category. If you pick any other object, say, $(d, g \colon c \to R d)$, you can always find a morphism $h \colon L c \to d$, which is just a leg, a projection, in the limiting cone that defines $L c$. It is automatically a morphism in $c/R$ because the following triangle commutes:

This is the triangle that defines $\eta_c$ as a morphism of cones, from the top cone with the apex $c$, to the bottom (limiting) cone with the apex $R L c$. We’ll use this interpretation later, when discussing the full version of the Freyd’s theorem.

We can also define the counit of the adjunction. Its component at $c$ is a morphism

$\epsilon_d : L R d \to d$

First, we repeat our construction starting with $c = R d$. We define the comma category $R d / R$ and use $\pi_{R d}$ to create the diagram whose limit is $L R d$. We pick $\epsilon_d$ to be a projection in the limiting cone. We are guaranteed that $d$ is in the base of the cone, because it’s the image of $(d, id \colon R d \to R d)$ under $\pi_{R d}$.

To complete this proof, one should show that the unit and counit are natural transformations and that they satisfy triangle identities.

# End of a comma category

An interesting insight into this construction can be gained using the end calculus. In my previous post, I talked about (weighted) colimits as coends, but the same argument can be dualized to limits and ends. For instance, this is our comma category as a category of elements in the coend notation:

$c/R \cong \mathcal{D} \int^d \mathcal{C} (c, R d)$

The limit of of the projection functor $\pi_c$ over the comma category can be written in the end notation as

$\lim_{c/R} \pi_c \cong \int_{(d, f)\colon c/R} \pi_c (d, f) \cong \int_{(d, f)\colon c/R} d$

This, in turn, can be rewritten as a weighted limit, with every $d$ weighted by the set $\mathcal{C}(c, R d)$:

$\mbox{lim}^{\mathcal{C}(c, R -)} \mbox{Id} \cong \int_{d \colon \mathcal{D}} \mathcal{C}(c, R d) \pitchfork d$

The pitchfork here is the power (cotensor) defined by the equation

$\mathcal{D}\big(d', s \pitchfork d\big) \cong Set\big(s, \mathcal{D}(d', d)\big)$

You may think of $s \pitchfork d$ as the product of $s$ copies of the object $d$, where $s$ is a set. The name power conveys the idea of iterated multiplication. Or, since power is a special case of exponentiation, you may think of $s \pitchfork d$ as a function object imitating mappings from $s$ to $d$.

To continue, if the left adjoint $L$ exists, the weighted limit in question can be replaced by

$\int_{d \colon \mathcal{D}} \mathcal{D}(L c, d) \pitchfork d$

which, using standard calculus of ends (see Appendix), can be shown to be isomorphic to $L c$. We end up with:

$\lim_{c/R} \pi_c \cong L c$

# Solution set condition

So what about those pesky size issues? It’s one thing to demand the existence of all small limits, and a completely different thing to demand the existence of large limits (such requirement may narrow down the available categories to preorders). Since the comma category may be too large, maybe we can cut it down to size by carefully picking up a (small) set of objects out of all objects of $\mathcal D$. We may take some indexing set $I$ and construct a family $d_i$ of objects of $\mathcal D$ indexed by elements of $I$. It doesn’t have to be one family for all—we may pick a different family for every object $c$ for which we are performing our construction.

Instead of using the whole comma category $c/R$, we’ll limit ourselves to a set of arrows $f_i \colon c \to R d_i$. But in a comma category we also have morphisms between arrows. In fact they are the essential carriers of the structure of the comma category. Let’s have another look at these morphisms.

This commuting condition can be re-interpreted as a factorization of $g$ through $f$. It so happens that every morphism $g$ can be trivially factorized through some $f$ by picking $d = d'$ and $h = id_d$. But if we restrict the factors $f$ to be members of the family $f_i$ then not every $g \colon c \to R d$ (for arbitrary $d$) can be automatically factorized. We have to demand it. That gives us the following:

Solution Set Condition: For every object $c$ there exists a small set $I$ with an $I$-indexed family of objects $d_i$ in $\mathcal D$ and a family of morphisms $f_i \colon c \to R d_i$, such that every morphism $g \colon c \to R d$ can be factored through one of $f_i$. That is, there exists a morphism $h \colon d_i \to d$ such that

$g = R h \circ f_i$

There is a shorthand for this statement: All comma categories $c/R$ admit weakly initial families of objects. We’ll come back to it later.

# Freyd’s theorem

We can now formulate:

Freyd’s Adjoint Functor Theorem: If $\mathcal D$ is a locally small and small-complete category, and the functor $R \colon D \to C$ is continuous (small-limit preserving), and it satisfies the solution set condition, then $R$ has a left adjoint.

We’ve seen before that the key to defining the point-wise left adjoint was to find the initial object in the comma category $c/R$. The problem is that this comma category may be large. So the trick is to split the proof into two parts: first defining a weakly initial object, and then constructing the actual initial object using equalizers. A weakly initial object has morphisms to every object in the category but, unlike its strong version, these morphisms don’t have to be unique.

An even weaker notion is that of a weakly initial set of objects. These are objects that among themselves have arrows to every object in the category, but it’s possible that no individual object has all the arrows. The solution set in Freyd’s theorem is such a weakly initial set in the comma category $c/R$. Since we assumed that $\mathcal C$ is small-complete, we can take a product of these objects and show that it’s weakly initial. The proof then proceeds with the construction of the initial object.

The details of the proof can be found in any category theory text or in nLab.

Next we’ll see the application of these results to the problem of defunctionalization of computer programs.

# Appendix

To show that

$\int_d \mathcal{D}(L c, d) \pitchfork d \cong L c$

it’s enough to show that the hom-functors from an arbitrary object $d'$ are isomorphic

\begin{aligned} & \mathcal{D}\big(d', \int_d \mathcal{D}(L c, d) \pitchfork d\big) \\ \cong & \int_d \mathcal{D}\big(d', \mathcal{D}(L c, d) \pitchfork d\big) \\ \cong & \int_d Set\big( \mathcal{D}(L c, d), \mathcal{D}(d', d) \big) \\ \cong & \; \mathcal{D}(d', L c) \end{aligned}

I used the continuity of the hom-functor, the definition of the power (cotensor) and the ninja Yoneda lemma.

It’s funny how similar ideas pop up in different branches of mathematics. Calculus, for instance, is built around metric spaces (or, more generally, Banach spaces) and measures. A limit of a sequence is defined by points getting closer and closer together. An integral is an area under a curve. In category theory, though, we don’t talk about distances or areas (except for Lawvere’s take on metric spaces), and yet we have the abstract notion of a limit, and we use integral notation for ends. The similarities are uncanny.

This blog post was inspired by my trying to understand the idea behind the Freyd’s adjoint functor theorem. It can be expressed as a colimit over a comma category, which is a special case of a Grothendieck fibration. To understand it, though, I had to get a better handle on weighted colimits which, as I learned, were even more general than Kan extensions.

# Category of elements as coend

Grothendieck fibration is like splitting a category in two orthogonal directions, the base and the fiber. Fiber may vary from object to object (as in dependent types, which are indeed modeled as fibrations).

The simplest example of a Grothendieck fibration is the category of elements, in which fibers are simply sets. Of course, a set is also a category—a discrete category with no morphisms between elements, except for compulsory identity morphisms. A category of elements is built on top of a category $\mathcal{C}$ using a functor

$F \colon \mathcal{C} \to Set$

Such a functor is traditionally called a copresheaf (this construction works also on presheaves, $\mathcal{C}^{op} \to Set$). Objects in the category of elements are pairs $(c, x)$ where $c$ is an object in $\mathcal{C}$, and $x \in F c$ is an element of a set.

A morphism from $(c, x)$ to $(c', x')$ is a morphism $f \colon c \to c'$ in $\mathcal{C}$, such that $(F f) x = x'$.

There is an obvious projection functor that forgets the second component of the pair

$\Pi \colon (c, x) \mapsto c$

(In fact, a general Grothendieck fibration starts with a projection functor.)

You will often see the category of elements written using integral notation. An integral, after all, is a gigantic sum of tiny slices. Similarly, objects of the category of elements form a gigantic sum (disjoint union) of sets $F c$. This is why you’ll see it written as an integral

$\int^{c \colon \mathcal{C}} F c$

However, this notation conflicts with the one for conical colimits so, following Fosco Loregian, I’ll write the category of elements as

$\mathcal{C}\int^{c} F c$

An interesting specialization of a category of elements is a comma category. It’s the category $L/d$ of arrows originating in the image of the functor $L \colon \mathcal{C} \to \mathcal{D}$ and terminating at a fixed object $d$ in $\mathcal{D}$. The objects of $L/d$ are pairs $(c, f)$ where $c$ is an object in $\mathcal{C}$ and $f \colon L c \to d$ is a morphism in $\mathcal{D}$. These morphisms are elements of the hom-set $\mathcal{D}(L c , d)$, so the comma category is just a category of elements for the functor $\mathcal{D}(L-, d) \colon \mathcal{C}^{op} \to Set$

$L/d \cong \mathcal{C}\int^{c} \mathcal{D}(L c, d)$

You’ll mostly see integral notation in the context of ends and coends. A coend of a profunctor is like a trace of a matrix: it’s a sum (a coproduct) of diagonal elements. But (co-)end notation may also be used for (co-)limits. Using the trace analogy, if you fill rows of a matrix with copies of the same vector, the trace will be the sum of the components of the vector. Similarly, you can construct a profunctor from a functor by repeating the same functor for every value of the first argument $c'$:

$P(c', c) = F c$

The coend over this profunctor is the colimit of the functor, a colimit being a generalization of the sum. By slight abuse of notation we write it as

$\mbox{colim}\, F = \int^{c \colon \mathcal{C}} F c$

This kind of colimit is called conical, as opposed to what we are going to discuss next.

# Weighted colimit as coend

A colimit is a universal cocone under a diagram. A diagram is a bunch of objects and morphisms in $\mathcal{C}$ selected by a functor $D \colon \mathcal{J} \to \mathcal{C}$ from some indexing category $\mathcal{J}$. The legs of a cocone are morphisms that connect the vertices of the diagram to the apex $c$ of the cocone.

For any given indexing object $j \colon \mathcal{J}$, we select an element of the hom-set $\mathcal{C}(D j, c)$, as a wire of the cocone. This is a selection of an element of a set (the hom-set) and, as such, can be described by a function from the singleton set $*$. In other words, a wire is a member of $Set(*, \mathcal{C}(D j, c))$. In fact, we can describe the whole cocone as a natural transformation between two functors, one of them being the constant functor $1 \colon j \mapsto *$. The set of cocones is then the set of natural transformations:

$[\mathcal{J}^{op}, Set](1, \mathcal{C}(D -, c))$

Here, $[J^{op}, Set]$ is the category of presheaves, that is functors from $\mathcal{J}^{op}$ to $Set$, with natural transformations as morphisms. As a bonus, we get the cocone triangle commuting conditions from naturality.

Using singleton sets to pick morphisms doesn’t generalize very well to enriched categories. For conical limits, we are building cocones from zero-thickness wires. What we need instead is what Max Kelly calls cylinders obtained by replacing the constant functor $1\colon \mathcal{J}^{op} \to Set$ with a more general functor $W \colon \mathcal{J}^{op} \to Set$. The result is a weighted colimit (or an indexed colimit, as Kelly calls it), $\mbox{colim}^W D$. The set of weighted cocones is defined by natural transformations

$[\mathcal{J}^{op}, Set](W, \mathcal{C}(D -, c))$

and the weighted colimit is the universal one of these. This definition generalizes nicely to the enriched setting (which I won’t be discussing here).

Universality can be expressed as a natural isomorphism

$[\mathcal{J}^{op}, Set](W, \mathcal{C}(D -, c)) \cong \mathcal{C}(\mbox{colim}^W D, c)$

We interpret this formula as a one-to-one correspondence: for every weighted cocone with the apex $c$ there is a unique morphism from the colimit to $c$. Naturality conditions guarantee that the appropriate triangles commute.

A weighted colimit can be expressed as a coend (see Appendix 1)

$\mbox{colim}^W D \cong \int^{j \colon \mathcal{J}} W j \cdot D j$

The dot here stands for the tensor product of a set by an object. It’s defined by the formula

$\mathcal{C}(s \cdot c, c') \cong Set(s, \mathcal{C}(c, c'))$

If you think of $s \cdot c$ as the sum of $s$ copies of the object $c$, then the above asserts that the set of functions from a sum (coproduct) is equivalent to a product of functions, one per element of the set $s$,

$(\coprod_s c) \to c' \cong \prod_s (c \to c')$

# Right adjoint as a colimit

A fibration is like a two-dimensional category. Or, if you’re familiar with bundles, it’s like a fiber bundle, which is locally isomorphic to a cartesian product of two spaces, the base and the fiber. In particular, the category of elements $\mathcal{C} \int W$ is, roughly speaking, like a bundle whose base is the category $\mathcal{C}$, and the fiber is a ($c$-dependent) set $W c$.

We also have a projection functor on the category of elements $\mathcal{C} \int W$ that ignores the $W c$ component

$\Pi \colon (c, x) \mapsto c$

The coend of this functor is the (conical) colimit

$\int^{(c, x) \colon \mathcal{C}\int W} \Pi (c, x) \cong \underset{\mathcal{C} \int W}{\mbox{colim}} \; \Pi$

But this functor is constant along the fiber, so we can “integrate over it.” Since fibers depends on $c$, different objects end up weighted differently. The result is a coend over the base category, with objects $c$ weighted by sets $W c$

$\int^{(c, x) \colon \mathcal{C}\int W} \Pi (c, x) \cong \int^{(c, x) \colon \mathcal{C}\int W} c \cong \int^{c \colon \mathcal{C}} W c \cdot c$

Using a more traditional notation, this is the formula that relates a (conical) colimit over the category of elements and a weighted colimit of the identity functor

$\underset{\mathcal{C} \int W}{\mbox{colim}} \; \Pi \cong \mbox{colim}^W Id$

There is a category of elements that will be of special interest to us when discussing adjunctions: the comma category for the functor $L \colon \mathcal{C} \to \mathcal{D}$, in which the weight functor is the hom-functor $\mathcal{D}(L-, d)$

$L/d \cong \mathcal{C}\int^{c} \mathcal{D}(L c, d)$

If we plug it into the last formula, we get

$\underset{L/d}{\mbox{colim}} \; \Pi \cong \underset{C \int \mathcal{D}(L-, d)}{\mbox{colim}} \; \Pi \cong \int^{c \colon \mathcal{C}} \mathcal{D}(L c, d) \cdot c$

If the functor $L$ has a right adjoint

$\mathcal{D}(L c, d) \cong \mathcal{C}(c, R d)$

we can rewrite this as

$\underset{L/d}{\mbox{colim}} \; \Pi \cong \int^{c \colon \mathcal{C}} \mathcal{C}(c, R d) \cdot c$

and useing the ninja Yoneda lemma (see Appendix 2) we get a formula for the right adjoint in terms of a colimit of a comma category

$\underset{L/d}{\mbox{colim}} \; \Pi \cong R d$

Incidentally, this is the left Kan extension of the identity functor along $L$. (In fact, it can be used to define the right adjoint as long as it preserves the functor $L$.)

We’ll come back to this formula when discussing the Freyd’s adjoint functor theorem.

# Appendix 1

I’m going to prove the following identity using some of the standard tricks of coend calculus

$\mbox{colim}^W D \cong \int^{j \colon \mathcal{J}} W j \cdot D j$

To show that two objects are isomorphic, it’s enough to show that their hom-sets to any object $c'$ are isomorphic (this follows from the Yoneda lemma)

\begin{aligned} \mathcal{C}(\mbox{colim}^W D, c') & \cong [\mathcal{J}^{op}, Set]\big(W-, \mathcal{C}(D -, c')\big) \\ &\cong \int_j Set \big(W j, \mathcal{C}(D j, c')\big) \\ &\cong \int_j \mathcal{C}(W j \cdot D j, c') \\ &\cong \mathcal{C}(\int^j W j \cdot D j, c') \end{aligned}

I first used the universal property of the colimit, then rewrote the set of natural transformations as an end, used the definition of the tensor product of a set and an object, and replaced an end of a hom-set by a hom-set of a coend (continuity of the hom-set functor).

# Appendix 2

The proof of

$\int^{c \colon \mathcal{C}} \mathcal{C}(c, R d) \cdot c \cong R d$

follows the same pattern

\begin{aligned} &\mathcal{C}\Big( \big(\int^{c} \mathcal{C}(c, R d) \cdot c\big) , c'\Big)\\ \cong &\int_c \mathcal{C}\big( \mathcal{C}(c, R d) \cdot c , c'\big) \\ \cong &\int_c Set\big( \mathcal{C}(c, R d) , \mathcal{C}(c, c')\big) \\ \cong & \; \mathcal{C}(R d, c') \end{aligned}

I used the fact that a hom-set from a coend is isomorphic to an end of a hom-set (continuity of hom-sets). Then I applied the definition of a tensor. Finally, I used the Yoneda lemma for contravariant functors, in which the set of natural transformations is written as an end.

$[ \mathcal{C}^{op}, Set]\big(\mathcal{C}(-, x), H \big) \cong \int_c Set \big( \mathcal{C}(c, x), H c \big) \cong H x$

I have recently watched a talk by Gabriel Gonzalez about folds, which caught my attention because of my interest in both recursion schemes and optics. A Fold is an interesting abstraction. It encapsulates the idea of focusing on a monoidal contents of some data structure. Let me explain.

Suppose you have a data structure that contains, among other things, a bunch of values from some monoid. You might want to summarize the data by traversing the structure and accumulating the monoidal values in an accumulator. You may, for instance, concatenate strings, or add integers. Because we are dealing with a monoid, which is associative, we could even parallelize the accumulation.

In practice, however, data structures are rarely filled with monoidal values or, if they are, it’s not clear which monoid to use (e.g., in case of numbers, additive or multiplicative?). Usually monoidal values have to be extracted from the container. We need a way to convert the contents of the container to monoidal values, perform the accumulation, and then convert the result to some output type. This could be done, for instance by fist applying fmap, and then traversing the result to accumulate monoidal values. For performance reasons, we might prefer the two actions to be done in a single pass.

Here’s a data structure that combines two functions, one converting a to some monoidal value m and the other converting the final result to b. The traversal itself should not depend on what monoid is being used so, in Haskell, we use an existential type.

data Fold a b = forall m. Monoid m => Fold (a -> m) (m -> b)


The data constructor of Fold is polymorphic in m, so it can be instantiated for any monoid, but the client of Fold will have no idea what that monoid was. (In actual implementation, the client is secretly passed a table of functions: one to retrieve the unit of the monoid, and another to perform the mappend.)

The simplest container to traverse is a list and, indeed, we can use a Fold to fold a list. Here’s the less efficient, but easy to understand implementation

fold :: Fold a b -> [a] -> b
fold (Fold s g) = g . mconcat . fmap s


See Gabriel’s blog post for a more efficient implementation.

A Fold is a functor

instance Functor (Fold a) where
fmap f (Fold scatter gather) = Fold scatter (f . gather)


In fact it’s a Monoidal functor (in category theory, it’s called a lax monoidal functor)

class Monoidal f where
init :: f ()
combine :: f a -> f b -> f (a, b)


You can visualize a monoidal functor as a container with two additional properties: you can initialize it with a unit, and you can coalesce a pair of containers into a container of pairs.

instance Monoidal (Fold a) where
-- Fold a ()
init = Fold bang id
-- Fold a b -> Fold a c -> Fold a (b, c)
combine (Fold s g) (Fold s' g') = Fold (tuple s s') (bimap g g')


where we used the following helper functions

bang :: a -> ()
bang _ = ()

tuple :: (c -> a) -> (c -> b) -> (c -> (a, b))
tuple f g = \c -> (f c, g c)


This property can be used to easily aggregate Folds.

In Haskell, a monoidal functor is equivalent to the more common applicative functor.

A list is the simplest example of a recursive data structure. The immediate question is, can we use Fold with other recursive data structures? The generalization of folding for recursively-defined data structures is called a catamorphism. What we need is a monoidal catamorphism.

# Algebras and catamorphisms

Here’s a very short recap of simple recursion schemes (for more, see my blog). An algebra for a functor f with the carrier a is defined as

type Algebra f a = f a -> a


Think of the functor f as defining a node in a recursive data structure (often, this functor is defined as a sum type, so we have more than one type of node). An algebra extracts the contents of this node and summarizes it. The type a is called the carrier of the algebra.

A fixed point of a functor is the carrier of its initial algebra

newtype Fix f = Fix { unFix :: f (Fix f) }


Think of it as a node that contains other nodes, which contain nodes, and so on, recursively.

A catamorphism generalizes a fold

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


It’s a recursively defined function. It’s first applied using fmap to all the children of the node. Then the node is evaluated using the algebra.

# Monoidal algebras

We would like to use a Fold to fold an arbitrary recursive data structure. We are interested in data structures that store values of type a which can be converted to monoidal values. Such structures are generated by functors of two arguments (bifunctors).

class Bifunctor f where
bimap :: (a -> a') -> (b -> b') -> f a b -> f a' b'


In our case, the first argument will be the payload and the second, the placeholder for recursion and the carrier for the algebra.

We start by defining a monoidal algebra for such a functor by assuming that it has a monoidal payload, and that the child nodes have already been evaluated to a monoidal value

type MAlgebra f = forall m. Monoid m => f m m -> m


A monoidal algebra is polymorphic in the monoid m reflecting the requirement that the evaluation should only be allowed to use monoidal unit and monoidal multiplication.

A bifunctor is automatically a functor in its second argument

instance Bifunctor f => Functor (f a) where
fmap g = bimap id g


We can apply the fixed point to this functor to define a recursive data structure Fix (f a).

We can then use Fold to convert the payload of this data structure to monoidal values, and then apply a catamorphism to fold it

cat :: Bifunctor f => MAlgebra f -> Fold a b -> Fix (f a) -> b
cat malg (Fold s g) = g . cata alg
where
alg = malg . bimap s id


Here’s this process in more detail. This is the monoidal catamorphism that we are defining:

We first apply cat, recursively, to all the children. This replaces the children with monoidal values. We also convert the payload of the node to the same monoid using the first component of Fold. We can then use the monoidal algebra to combine the payload with the results of folding the children.

Finally, we convert the result to the target type.

We have factorized the original problem in three orthogonal directions: the monoidal algebra, the Fold, and the traversal of the particular recursive data structure.

# Example

Here’s a simple example. We define a bifunctor that generates a binary tree with arbitrary payload a stored at the leaves

data TreeF a r = Leaf a | Node r r


It is indeed a bifunctor

instance Bifunctor TreeF where
bimap f g (Leaf a) = Leaf (f a)
bimap f g (Node r r') = Node (g r) (g r')


The recursive tree is generated as its fixed point

type Tree a = Fix (TreeF a)


Here’s an example of a tree

We define two smart constructors to simplify the construction of trees

leaf :: a -> Tree a
leaf a = Fix (Leaf a)

node :: Tree a -> Tree a -> Tree a
node t t' = Fix (Node t t')


We can define a monoidal algebra for this functor. Notice that it only uses monoidal operations (we don’t even need the monoidal unit here, since values are stored in the leaves). It will therefore work for any monoid

myAlg :: MAlgebra TreeF
myAlg (Leaf m) = m
myAlg (Node m m') = m <> m'


Separately, we define a Fold whose internal monoid is Sum Int. It converts Double values to this monoid using floor, and converts the result to a String using show

myFold :: Fold Double String
myFold = Fold floor' show'
where
floor' :: Double -> Sum Int
floor' = Sum . floor
show' :: Sum Int -> String
show' = show . getSum


This Fold has no knowledge of the data structure we’ll be traversing. It’s only interested in its payload.

Here’s a small tree containing three Doubles

myTree :: Tree Double
myTree = node (node (leaf 2.3) (leaf 10.3)) (leaf 1.1)


We can monoidally fold this tree and display the resulting String

Notice that we can use the same monoidal catamorphism with any monoidal algebra and any Fold.

The following pragmas were used in this program

{-# language ExistentialQuantification #-}
{-# language RankNTypes #-}
{-# language FlexibleInstances #-}
{-# language IncoherentInstances #-}


# Relation to Optics

A Fold can be seen as a form of optic. It takes a source type, extracts a monoidal value from it, and maps a monoidal value to the target type; all the while keeping the monoid existential. Existential types are represented in category theory as coends—here we are dealing with a coend over the category of monoids $\mathbf{Mon}(\mathbf{C})$ in some monoidal category $\mathbf C$. There is an obvious forgetful functor $U$ that forgets the monoidal structure and produces an object of $\mathbf C$. Here’s the categorical formula that corresponds to Fold

$\int^{m \in Mon(C)} C(s, U m)\times C(U m, t)$

This coend is taken over a profunctor in the category of monoids

$P n m = C(s, U m) \times C(U n, t)$

The coend is defined as a disjoint union of sets $P m m$ in which we identify some of the elements. Given a monoid homomorphism $f \colon m \to n$, and a pair of morphisms

$u \colon s \to U m$

$v \colon U n \to t$

we identify the pairs

$((U f) \circ u, v) \sim (u, v \circ (U f))$

This is exactly what we need to make our monoidal catamorphism work. This condition ensures that the following two scenarios are equivalent:

• Use the function $u$ to extract monoidal values, transform these values to another monoid using $f$, do the folding in the second monoid, and translate the result using $v$
• Use the function $u$ to extract monoidal values, do the folding in the first monoid, use $f$ to transform the result to the second monoid, and translate the result using $v$

Since the monoidal catamorphism only uses monoidal operations and $f$ is a monoid homomorphism, this condition is automatically satisfied.

Previously we discussed ninth chords, which are the first in a series of extension chords. Extensions are the notes that go beyond the first octave. Since we build chords by stacking thirds on top of each other, the next logical step, after the ninth chord, is the eleventh and the thirteenth chords. And that’s it: there is no fifteenth chord, because the fifteenth would be the same as the root (albeit two octaves higher).

This strange musical arithmetic is best understood if we translate all intervals into their semitone equivalents in equal temperament. Since we started by constructing the E major chord, let’s work with the E major scale, which consists of the following notes:

|E |  |F#|  |G#|A  |  |B |  |C#|  |D#|E |

Let’s chart the chord tones taking E as the root.

We see the clash of several naming conventions. Letter names have their origin is the major diatonic scale, as implemented by the white keys on the piano starting from C.

|C |  |D |  |E |F |  |G |  |A |  |B |C |

They go in alphabetical order, wrapping around after G. On the guitar we don’t have white and black keys, so this convention seems rather arbitrary.

The names of intervals (here, marked by digits, with occasional accidental symbols) are also based on the diatonic scale. They essentially count the number of letters from the root (including the root). So the distance from E to B is 5, because you count E, F, G, A, B — five letters. For a mathematician this convention makes little sense, but it is what it is.

After 12 semitones, we wrap around, as far as note names are concerned. With intervals the situation is a bit more nuanced. The ninth can be, conceptually, identified with the second; the eleventh with the fourth; and the thirteenth with the sixth. But how we name the intervals depends on their harmonic function. For instance, the same note, C#, is called the sixth in the E6 chord, and the thirteenth in E13. The difference is that E13 also contains the (dominant) seventh and the ninth.

A full thirteenth chord contains seven notes (root, third, fifth, seventh, ninth, eleventh, and thirteenth), so it cannot possibly be voiced on a six-string guitar. We usually drop the eleventh (as you can see above). The ninth and the fifth can be omitted as well. The root is very important, since it defines the chord, but when you’re playing in a band, it can be taken over by the bass instrument. The third is important because it distinguishes between major and minor modes (but then again, you have power chords that skip the third). The seventh is somewhat important in defining the dominant role of the chord.

Notice that a thirteenth chord can be seen as two separate chords on top of each other. E13 can be decomposed into E7 with F#m on top (try to spot these two shapes in this grip). Seen this way, the major/minor clash is another argument to either drop the eleventh (which serves as the minor third of F#m) or sharp it.

Alternatively, one could decompose E13 into E with DΔ7 on top. The latter shape is also easily recognized in this grip.

I decided against listing eleventh chords because they are awkward to voice on the guitar and because they are rarely used. Thirteenth chords are more frequent, especially in jazz. You’ve seen E13, here’s G13:

It skips the 11th and the 5th; and the 9th at the top is optional.

## The Role of Harmonics

It might be worth explaining why omitting the fifth in G13 doesn’t change the character of the chord. The reason is that, when you play the root note, you are also producing harmonics. One of the strongest harmonics is the fifth, more precisely, the fifth over the octave. So, even if you don’t voice it, you can hear it. In fact, a lot of the quality of a given chord voicing depends on the way the harmonics interact with each other, especially in the bass. When you strum the E chord on the guitar, you get a strong root sound E, and the B on the next thickest string amplifies its harmonic fifth. Compare this with the G shape, which also starts with the root, but the next string voices the third, B, which sounds okay, but not great, so some people mute it.

Inverted chords, even though they contain the same notes (up to octave equivalence) may sound dissonant, depending on the context (in particular, voice leading in the bass). This is why we don’t usually play the lowest string in C and A shapes, or the two lowest strings in the D shape.

In the C shape, the third in the bass clashes with the root and is usually muted. That’s because the strongest harmonic of E is B, which makes C/E sound like CΔ7.

On the other hand, when you play the CΔ7 chord, the E in the bass sounds great, for exactly the same reason.

You can also play C with the fifth in the bass, as C/G, and it sounds good, probably because the harmonic D of G gives it the ninth flavor. This harmonic is an octave and a fifth above G, so it corresponds to the D that would be voiced on the third fret of the B string.

The same reasoning doesn’t quite work for the A shape. Firstly, because all four lower strings in A/E voice the very strong power chord (two of them open strings) drowning out the following third. Also the fifth above E is the B that’s just two semitones below the third C# voiced on the B string. (Theoretically, C/G has a third doubled on the thinest string but that doesn’t seem to clash as badly with the D harmonic of G. Again, the ear beats theory!)

Next: Altered chords.

We have already discussed several kinds of seventh chords. But if you can extend the chord by adding a third above it, why not top it with yet another third? This way we arrive at the ninth chord. But a ninth is one whole step above the octave. So far we’ve been identifying notes that cross the octave with their counterparts that are 12 semitones lower. A mathematician would say that we are doing arithmetic modulo 12. But this is just a useful fiction. A lot of things in music theory can be motivated using modular arithmetic, but ultimately, we have to admit that if something doesn’t sound right, it’s not right.

A ninth is 14 semitones above the root (if you don’t flat or sharp it), so it should be identified with the second, which is 2 semitones up from the root. That puts it smack in the middle between the root and the third: a pretty jarring dissonance. We’ve seen a second used in a chord before, but it was playing the role of a suspended third. In a ninth chord, you keep the third, and move the second to the next octave, where it becomes a ninth and cannot do as much damage. Instead it provides color and tension, making things more interesting.

To construct E9, we start with E7. It has the root duplicated on the thinnest string, so it’s easy to raise it by two semitones to produce the ninth.

There are many variations of the ninth chord. There is a minor version, with the third lowered; the seventh can be raised to a major seventh; and the ninth itself can be flatted or sharped. We won’t cover all these.

Following the same pattern, C9 can be constructed from C7 by raising the root by two semitones.

We get a highly movable shape, especially if we put the fifth on the thinnest string. In particular, it can be moved one fret towards the nut to produce B9–a slight modification of the B7 grip we’ve seen before.

If you look carefully at this shape, you might recognize parts of Gm in it (the three thinnest strings). This is no coincidence. The fifth, the seventh, and the ninth of any ninth chord form a minor triad.

Here is the E9 grip obtained by transposing C9 down the fretboard. It’s used a lot in funk:

The same chord with a sharped ninth is called the Hendrix chord, after Jimi Hendrix who popularized it:

The E9 shape is not only movable, but it’s also easy to mutate. This is the minor version:

and this is the major seventh version:

Such chords are quite common in Bossa Nova.

A9 is obtained by raising the root of A7 by two semitones:

Can you spot the Dm shape raised by two frets?

Similarly, G9 is constructed from G7, and it conceals a Dm as part of it.

Next: Extension chords.

Previously we talked about dominant seventh chords, which are constructed by adding a minor seventh to a chord. Adding a major seventh instead is a very “jazzy” thing. With it, you can jazz up any chord, not just the dominant.

A major seventh is one semitone below the octave, so it forms a highly dissonant minor second (a single semitone) against it. This adds a lot of tension but, unlike the dominant seven, major dominant seventh doesn’t have an obvious resolution, so it provides an element of excitement and unpredictability.

Major-seventh chords are usually voiced in such a way as to put distance between the seventh and the root. But you can try this slightly unusual grip, in which there is a semitone interval between the two highest strings (although the third of the triad is missing, so it’s a variation of a power chord).

The notation for major-seventh chords varies–in jazz, the major seventh is often marked with a triangle, as in $\Delta 7$. It’s also common to see Maj in front of 7.

You may think of major-seventh chords as constructed either by lowering the root by a semitone, or raising the seventh of the corresponding dominant seventh chord.

Here’s the E major-seventh grip, together with its less common minor version:

When transposing these chords down the fretboard, we often skip the fifth in the bass as well as the root on the highest string. We either mute these strings or finger-pick the remaining four strings. Here’s the G major-seventh chord constructed this way:

You might be wondering at the resemblance of this grip to A minor. This is no coincidence–the major-seventh chord contains a minor triad. Check this out: there is a minor third between 3 and 5, and a major third between 5 and $\Delta 7$. In fact, every four-note chord contains two triads (the dominant seventh chord contained a diminished triad built inside a tritone, and the minor major-seventh chord contains an augmented triad).

Here are, similarly constructed, major-seventh versions of A chords. They are also easy to transpose down the fretboard. (Can you spot a flatted Dm shape in the first one?)

And these are the D chords:

C major-seventh is an odd one (that’s because there is an open string between the minor seventh and the root), but it’s very easy to grip:

If you squint hard enough, you can see the elements of E minor in it.

Here’s the open-string version of G major-seventh:

Squint again, and you can see the elements of B minor.

Previously, we discussed chord construction by mutating the third (we’ll come back to the topic of mutating the fifth later). Another important mutation is adding more notes to a chord. Traditionally, the most common addition is that of the seventh. There are two versions of the seventh: minor, consisting of 10 semitones; and major, 11 semitones. We’ll start with the minor mutation, because it plays a very important role in functional harmony.

Classical music was built around the idea of tension and resolution, and the perfect expression of it was the authentic cadence, a two chord progression from the dominant chord to the tonic. The dominant is the fifth above the tonic so, for instance, E dominates A. You could build a whole song with just these two chords, and you’d probably end it on the cadence from E to A (the word cadence is derived from cadere, which means to fall).

To add more tension to a dominant chord, it is customary to extend it with a minor seventh. This creates a very dissonant interval between it and the third of the chord. The major third is 4 semitones above the root, the minor seventh is 10 semitones from the root, and 10 – 4 = 6. Six semitones is a diminished fifth, or the cursed tritone. The resolution of the tension created by this dissonance is very satisfying to our ears. A chord with a minor seventh is called the dominant seventh chord.

Let’s go back to the basic E shape and see how we can add a seventh to it.

We have two possibilities: we can either raise one of the fifth, or we can lower one of the roots. Luckily, both are duplicated, so we are not losing any triad tones.

Raising the fifth by three semitones (7 + 3 = 10) produces this grip:

Notice how smoothly it resolves down to the A chord by small movements of notes in opposite directions.

Here’s the alternative grip of E7, obtained by lowering the root (or the octave: 12 – 2 = 10) by two semitones:

This is a two-finger shape, and it’s easily transposed to any position on the fretboard. Alternatively, if you’re willing to skip the repeated fifth and the duplicated root, you may use this “jazzy” movable shape– here to voice a G7 at the third fret:

We can also add the minor seventh to minor chords to obtain minor seventh chords (sometimes called minor minor seventh chords). These are not as dissonant as their major counterparts (no tritone).

Again, we have two options, one of them easily movable across the fretboard

You can also mute the fifth in the second grip and obtain this strange chord:

This grip is often used in jazz, transposed across the fretboard, with either the top E string muted, or with both bass strings muted.

The story for the dominant seventh versions of the A chord is very similar:

And these are its two minor versions:

The D chords have only one dominant seven version each, obtained by lowering the root (octave):

And here’s the G7 chord in its usual shape, together with the truncated version which, incidentally, could be identified with the A7 chord shifted by two frets in the “wrong” direction:

We’ve seen the minor version of G seventh, Gm7, earlier.

There is only one version of C7:

Interestingly, it’s variation (the one with the fifth on the E string) can be shifted by one fret towards the nut to produce the B7 chord:

This new shape can, in turn, undergo further mutations, giving rise to other interesting extended chords.

As I said, dominant seventh chords have a strong tendency to resolve to their tonic chords, so it pays to learn at least part of the so called circle of fifths. Each chord in this list dominates the one to its right: B, E, A, D, G, C, F (followed by the sharped/flatted chords, to make the full circle of 12). The neighbor to the right of a given chord is called its subdominant. For instance E7 is the dominant of A, whose subdominant is D. The whole 12 bar blues can be played with any three consecutive chords picked from the circle of fifths.

Next time: major seventh chords.

So far we’ve been discussing chord transformations that didn’t change the chord’s function. We’ve been essentially dealing with just one major chord and its transpositions. You can think of a major chord as a rigid shape rotating through the circle of pitches: a four-semitone step topped with a three-semitone step, combining to seven semitones of the perfect fifth. We’ll keep the fifth for the time being, because it’s such a nice consonance, but we’ll be mutating the third.

The first such transformation is to lower the third by one semitone to obtain a minor triad. For some reason, we perceive minor chords as sad or melancholy, sometimes a bit whiny.

They are not very common in pop or rock music, but they are popular in folk and jazz (especially with added sevenths, which makes them edgy).

The first three shapes we’ve seen so far are perfect for this transformation: they have a single third, and it sits under a finger. Lifting or shifting this finger is easy. That changes the major third to a minor third, which we will notate as 3♭, since you can think of it as a flatted third. In jazz, flatting is often represented by a minus sign, so you might see Em notated as E-, and so on.

Here are the three basic minor shapes constructed this way:

When playing Dm, we usually mute the two lowest strings. I also listed the inversion Dm/F, as it’s sometimes used in chord progressions (e.g., a walking bass).

The C shape has the third voiced on an open string, and it’s doubled, so it’s humanly impossible to lower it. G shape is also tricky, although with the “folk” G you could cheat and mute the third, which results in a power chord. A power cord can be substituted, in a pinch, for either a major or a minor chord.

Normally, though, you produce all other minor chords by transposing (barring) either Em or Am.

It’s possible to move the third one more semitone step down or up. This results in suspended chords. Twice lowered third becomes a second, so that chord is called the suspended second. A raised third becomes a fourth, so that chord is called the suspended fourth. They are often used as embellishments, or in progression, with a regular major or minor chord in between (George Harrison’s Something or John Lennon’s Happy Xmas (War Is Over) use this riff extensively).

Suspended fourth works with the E shape as well

But suspended second in E is not very practical, since you have to use the adjacent string at the fourth fret, and you end up with the fifth in triplicate (of course, you can mute one).

Interestingly enough, the second can be reinterpreted as the ninth, an octave over. So, instead of using a suspended second, you may replace it with a better sounding add-nine chord. This one keeps the major third, but adds a ninth:

I’ll let you figure out suspensions for the C and G shapes.

Next: Dominant seventh chords.

Previously, we’ve seen how to transform the shapes of major chords by transposing them down the fretboard and across the fretboard. All these transformations can be composed. A mathematician would say that they form a group. Strictly speaking, one should introduce the identity transformation, which is just holding the same shape in place, and the inverse transformation. The latter is always possible, because the fretboard is cyclic: it repeats itself after the twelfth fret. So subtracting two frets is the same as adding ten frets.

Obviously, we can combine sideways shifts with vertical shifts and, for instance, produce the C chord by shifting down the A shape.

Even though all our shapes require only three fingers, the barred versions become progressively harder to grip as they require wider stretching of fingers. So the most common barred chords are either built from the E and A shapes, or use fewer than six strings (either by muting, or by finger picking).

And then there are some hybrid grips that merge multiple shapes. That’s because there is one more type of transformation, which I call “crawling,” where you move to a different triad note on the same string. We’ve already seen examples of creating variations of the G chord and the C chord by replacing the third by the fifth or vice versa. The third and the fifth are only separated by three frets, so they are often within easy reach. You have to be careful with replacing the third, though, if it wasn’t duplicated in the first place. For instance, here’s a version of the D chord that misses the third:

Such chords that only contain the root and the fifth are called power chords and are used a lot in heavy metal.

It’s also possible to replace the fifth by the root, as in this grip:

This is an interesting case of a hybrid grip. It started off as the A shape, but you may also see the beginnings of the G shape transposed down three frets, especially if you mute the two bass strings. The A shape has the ability to crawl down to a G shape.

Similarly, the D shape can crawl down to C shape (see the D triangle at the top?):

These two shapes (with the bass strings left out) can be easily transposed and are pretty useful in practice.

In fact, all chord shapes can be unified in one diagram, if you mark all triad tones across the fretboard. Here’s such a chart for the E chord. You can recognize, in order, the E shape, followed by the D shape, followed (and partially overlapped) by the C shape, which transitions into the A shape, which morphs into G, and finally goes back to E. You can use this diagram to play the same E chord in five different positions (after which it repeats itself).

You can, of course, produce such a chart starting from any of the five shapes, just by cycling it. Or you can cut this diagram out and glue it into a ring.

If you’re an astronomer, you might recognize some common constellations in this chart, like the Orion, or Draco, but that’s just pure coincidence.

Next time we’ll talk about minor chords.

Previously, we talked about transforming the basic E chord shape by transposing it up the fretboard. You may be aware that there are other chord shapes, sometimes grouped into the so called CAGED system. I’ll show you how to derive this system “scientifically.” Scientists arrive at new theories by looking at patterns. Sometimes a pattern doesn’t fit exactly, but it makes sense to temporarily ignore the discrepancy, forge ahead with incorrect assumptions, and then introduce subtle corrections to fix them. I know, this is not what they teach you in school, but that’s how it’s done in real life.

Let’s make some simplifying assumptions about guitar tuning. The first is that the top string can be identified with the bottom string: they are both E strings. Strictly speaking, this is not true: they are two octaves apart, and sometimes you can finger them differently, but we are playing scientists who ignore such distinctions. So we’ll consider the bottom E string a duplicate of the top E string. Second assumption is more outrageous: strings are tuned in fourths. Well, this is true in 80% of the cases. The 20% exception is the interval of a major third between the G string and the B string. We’ll just ignore it for the moment. With these assumptions in place, we can think of the strings forming a circle: we glue together the two E strings, and we get a circle of five strings a fourth apart.

In this imaginary world, we can now shift any chord shape sideways, around the circle, without changing its function. Granted, it will shift all the pitches up by a fourth, so shifting the E chord to the right would result in the A chord, another shift would produce the D chord, and so on. Lets try it!

We’ll start with the E chord and shift it to the right.

We get this:

Hurray! Within experimental error, it worked! Granted, this is the A minor chord, not the A major that we were expecting, but still, considering that our assumptions were partially wrong, that’s close enough.

The question is, how can we modify our theory to produce the A major chord? Our problem has its source in the anomaly between the G string and the B string. When we shift a finger between the two, keeping it on the same fret, we are not moving the pitch up by a fourth, we’re moving it by a third. If all shifts were by a fourth, relative intervals wouldn’t change, and we would just transpose a major chord into another major chord.

But not all is lost. It just means that we have to introduce a correction to our theory. In order to preserve relative intervals, when shifting from the G string to the B string, we have to move the finger one fret up the fretboard (or down, in the diagram). It works:

This is indeed the A major chord.

Of course, theory aside, the chord has to also sound right. This one is okay, except that the fifth of the triad is in the bass, which makes it an inverted chord. In guitar notation inversions are often written as slash chords; here it would be A/E, because of the E in the bass. Inverted chords don’t always work in a chord progressions so, in practice, people try to mute the low E and emphasize the root A.

To test our theory further, let’s apply the right shift to A. The fourth above A is D and, indeed we get the D major. Notice the adjustment when moving between the G and the B strings.

Again, the bass part is a little tricky. Here, I just duplicated the high E string grip on the lower E string, which resulted in another inversion D/F#. In practice, people usually mute both the E and the A strings and emphasize the root D.

Another shift to the right and we get the G chord (again, correcting the move from the G string to the B string and mirroring the E string):

This particular variant is often used in folk music. The more popular variant gets rid of the fifth on the B string, since the open B is the third of the triad.

This change leads to the duplication of the third, so people often mute the third in the bass.

Another shift, and we get the C major. Here, the move from the open G string is corrected by pressing the B string at the first fret. All according to our theory.

The third in the bass doesn’t sound good, so it’s usually muted. Another inversion, with the fifth in the bass, sounds better in many contexts, so here’s C/G:

Here’s another variant with, the fifth on the highest E string

We’ll see this variant modified by extension notes (the sevenths and the ninths).

We have just covered all the shapes in the CAGED system (the letters stand for the five major chords). And, indeed, we went full circle, because the next shift produces the F chord (the open G string turns into first fret press on the B string).

This makes perfect sense. If it weren’t for the anomaly, the fifth shift should bring us back to the same shape. But every time a black dot crosses the anomaly, it drops down so, after a full circle, the whole shape drops down one fret. Therefore five shifts to the right equal one shift down. We have just proven a theorem.

Notice that in the first three iterations a triangle shape is formed by three fingers. This shape consists of the fifth, the root, and the third of the triad. This information will come in handy when we discuss chord modifications. To the left of the triangle, we have the root, and to the right, another fifth (except in the D chord, where it’s pushed off the edge). In the G chord, we start seeing part of this triangle peeking on the left (the root and the third), shifted down because of the anomaly. In C/G the triangle is fully reconstituted, albeit one fret down. If you can spot these shapes, you’ll have no problem remembering where to find the third (and, for instance, lower it to make a minor chord) or where to insert the seventh.

This theory can be also visualized by arranging the strings in a radial pattern, the frets forming a spiral. Here’s the diagram for the E chord:

If you rotate the dots counterclockwise, you’ll get the A chord, and so on. The anomaly adjustment happens automatically, because the B string is offset by one step. Also, if no dot moves into the B string, a new dot at the first fret is produced.

Next time, combining the transformations.