A general optic can be defined as a set of bidirectional transformations:
It is buit from two actegories, and
; each with its own monoidal action by a monoidal category
(the two actions are represented by two different triangles).
A product of two hom-sets may be interpreted as a hom-set in a product category. The reversal of one of the hom-set may be accomplished by invoking the opposite category.
If we use for a pair of objects
in
, and
for
, we may want to encode such an optic as:
with the action .
This is a slight abuse of notation. Normally, a coend variable should occur in the integrand twice; once in a covariant and once in a contravariant position. To justify this new notation, let’s dig deeper into the definition of a coend.
A coend is a giant sum over all the the sets formed by taking the diagonal elements of the profunctor
— modulo certain identifications. These identifications are crucial, because they probe the structure of the categories in question; they tell us that we are overcounting certain set elements because they are reachable through underlying morphisms.
The identification is done through cowedges. A cowedge with a vertex is a family of morphisms (injections)
such that for any morphism
the following diagram commutes:
We read the commuting condition as:
In a sum (disjoint union) of all these sets , we identify the elements that can be reached through such composites. One composite contains the lifting of the pair
; and the other, the lifting of
.
Cowedges form a category, and the terminal cowedge for a given profunctor is called the coend.
Twisted Arrow Category
A giant sum with identifications can also be described as a colimit, if we can find the appropriate category to define the cocones. We will want to match cocones with cowedges.
Each cowedge corresponds to a family of injections and an arrow
in
. This suggests that the appropriate category for constructing the colimit is the category of arrows. An object is this category is an arrow
or, more precisely, a triple
.
The tricky part is to define morphisms between arrows. Since we are working with profunctors, it makes sense to define a morphism between and
as a pair of arrows going in opposite directions:
More precisely, a morphism between arrows is a pair of morphisms satisfying the commuting condition
:
(Incidentally, without this commuting condition, all arrows between a fixed pair of objects would be isomorphic.)
Such a category is called a twisted-arrow category .
It can be shown that an end of a profunctor is isomorphic to a limit over .
Here we are interested in a coend, so we’ll have to slightly modify the proof. We’ll show that a coend is isomorphic to a colimit over the opposite catoegory . An object in this category is a triple
, in which the direction of the arrow is reversed. Morphisms are similarly reversed:
They satisfy the commuting condition:
Cocones
A colimit is defined in terms of cocones. A cocone with a vertex is a functor:
together with a family of injections:
.
Let’s define the functor using our profunctor
. On objects, we have the obvious candidate:
The tricky part is to define the action of on morphisms. The image, under
, of a morphism
is a function between sets:
We define it as:
The action of is illustrated in this picture:
From Cocones to Cowedges
We can now establish a one-to-one correspondence between cocones and cowedges.
First of all, a cocone with a vertex is built from a family of injections:
It’s easy to build the diagonal injections, coming from . We make them equal to the cowedge injections:
A general injection is given by either of the two formulas:
which are equal by the wedge condition. This is illustrated by the following diagram:
Next we have to show that the triangles forming the sides of the cocone commute. Here’s one of such triangles:
The trick is to factorize the top morphism:
where . Here, the identity morphisms are written as their corresponding objects,
, etc. This decomposition is illustrated in the following diagram:
Keep in mind the commuting condition in the definition of a twisted morphism:
We can now factorize the top arrow of our triangle as follows:
We then decompose our triangle into a pair of triangles and the diamond. Translating between the functor and the profunctor
, we build the following diagram:
The two triangles commute by functoriality, and the diamond is the cowedge for . The mapping goes both ways: given a cocone
we can reconstruct the cowedge.
Finally, just like cowedges form a category, so do cocones; and we have just sketched the equivalence of the two categories. It follows that the terminal objects in the two categories are equivalent, therefore coends are isomorphic to colimits.
Twisted Optics
We can now give a precise meaning to the shorthand notation for optics:
This is a colimit over the appropriate twisted arrow category:
The advantage of this definition is that it can be generalized to a double-categorical setting, where colimits have a precise meaning.
If you want to learn more about ends and coends, I recommend reading Coend Calculus by Fosco Loregian.









































