# Abstract

Co-presheaf optic is a new kind of optic that generalizes the polynomial lens. Its distinguishing feature is that it’s not based on the action of a monoidal category. Instead the action is parameterized by functors between different co-presheaves. The composition of these actions corresponds to composition of functors rather than the more traditional tensor product. These functors and their composition have a representation in terms of profunctors.

# Motivation

A lot of optics can be defined using the existential, or coend, representation:

$\mathcal{O}\langle a, b\rangle \langle s, t \rangle = \int^{m \colon \mathcal M} \mathcal C (s, m \bullet a) \times \mathcal D ( m \bullet b, t)$

Here $\mathcal M$ is a monoidal category with an action on objects of two categories $\mathcal C$ and $\mathcal D$ (I’ll use the same notation for both actions). The actions are composed using the tensor product in $\mathcal M$:

$n \bullet (m \bullet a) = (n \otimes m) \bullet a$

The idea of this optic is that we have a pair of morphisms, one decomposing the source $s$ into the action of some $m$ on $a$, and the other recomposing the target $t$ from the action of the same $m$ on $b$. In most applications we pick $\mathcal D$ to be the same category as $\mathcal C$.

Recently, there has been renewed interest in polynomial functors. Morphisms between polynomial functors form a new kind of optic that doesn’t neatly fit this mold. They do, however, admit an existential representation or the form:

$\int^{c_{k i}} \prod_{k \in K} \mathbf{Set} \left(s_k, \sum_{n \in N} a_n \times c_{n k} \right) \times \prod_{i \in K} \mathbf{Set} \left(\sum_{m \in N} b_m \times c_{m i}, t_i \right)$

Here the sets $s_k$ and $t_i$ can be treated as fibers over the set $K$, while the sets $a_n$ and $b_m$ are fibers over a different set $N$.

Alternatively, we can treat these fibrations as functors from discrete categories to $\mathbf{Set}$, that is co-presheaves. For instance $a_n$ is the result of a co-presheaf $a$ acting on an object $n$ of a discrete category $\mathcal N$. The products over $K$ can be interpreted as ends that define natural transformations between co-presheaves. The interesting part is that the matrices $c_{n k}$ are fibrated over two different sets. I have previously interpreted them as profunctors:

$c \colon \mathcal N^{op} \times \mathcal K \to \mathbf{Set}$

In this post I will elaborate on this interpretation.

# Co-presheaves

A co-presheaf category $[\mathcal C, Set ]$ behaves, in many respects, like a vector space. For instance, it has a “basis” consisting of representable functors $\mathcal C (r, -)$; in the sense that any co-presheaf is as a colimit of representables. Moreover, colimit-preserving functors between co-presheaf categories are very similar to linear transformations between vector spaces. Of particular interest are functors that are left adjoint to some other functors, since left adjoints preserve colimits.

The polynomial lens formula has a form suggestive of vector-space interpretation. We have one vector space with vectors $\vec{s}$ and $\vec{t}$ and another with $\vec{a}$ and $\vec{b}$. Rectangular matrices $c_{n k}$ can be seen as components of a linear transformation between these two vector spaces. We can, for instance, write:

$\sum_{n \in N} a_n \times c_{n k} = c^T a$

where $c^T$ is the transposed matrix. Transposition here serves as an analog of adjunction.

We can now re-cast the polynomial lens formula in terms of co-presheaves. We no longer intepret $\mathcal N$ and $\mathcal K$ as discrete categories. We have:

$a, b \colon [\mathcal N, \mathbf{Set}]$

$s, t \colon [\mathcal K, \mathbf{Set}]$

In this interpretation $c$ is a functor between categories of co-presheaves:

$c \colon [\mathcal N, \mathbf{Set}] \to [\mathcal K, \mathbf{Set}]$

We’ll write the action of this functor on a presheaf $a$ as $c \bullet a$.

We assume that this functor has a right adjoint and therefore preserves colimits.

$[\mathcal K, \mathbf{Set}] (c \bullet a, t) \cong [\mathcal N, \mathbf{Set}] (a, c^{\dagger} \bullet t)$

where:

$c^{\dagger} \colon [\mathcal K, \mathbf{Set}] \to [\mathcal N, \mathbf{Set}]$

We can now generalize the polynomial optic formula to:

$\mathcal{O}\langle a, b\rangle \langle s, t \rangle = \int^{c} [\mathcal K, \mathbf{Set}] \left(s, c \bullet a \right) \times [\mathcal K, \mathbf{Set}] \left(c \bullet b, t \right)$

The coend is taken over all functors that have a right adjoint. Fortunately there is a better representation for such functors. It turns out that colimit preserving functors:

$c \colon [\mathcal N, \mathbf{Set}] \to [\mathcal K, \mathbf{Set}]$

are equivalent to profunctors (see the Appendix for the proof). Such a profunctor:

$p \colon \mathcal N^{op} \times \mathcal K \to \mathbf{Set}$

is given by the formula:

$p \langle n, k \rangle = c ( \mathcal N(n, -)) k$

where $\mathcal N(n, -)$ is a representable co-presheaf.

The action of $c$ can be expressed as a coend:

$(c \bullet a) k = \int^{n} a(n) \times p \langle n, k \rangle$

The co-presheaf optic is then a coend over all profunctors $p \colon \mathcal N^{op} \times \mathcal K \to \mathbf{Set}$:

$\int^{p} [\mathcal K, \mathbf{Set}] \left(s, \int^{n} a(n) \times p \langle n, - \rangle \right) \times [\mathcal K, \mathbf{Set}] \left(\int^{n'} b(n') \times p \langle n', - \rangle, t \right)$

# Composition

We have defined the action $c \bullet a$ as the action of a functor on a co-presheaf. Given two composable functors:

$c \colon [\mathcal N, \mathbf{Set}] \to [\mathcal K, \mathbf{Set}]$

and:

$c' \colon [\mathcal K, \mathbf{Set}] \to [\mathcal M, \mathbf{Set}]$

we automatically get the associativity law:

$c' \bullet (c \bullet a) = (c' \circ c) a$

The composition of functors between co-presheaves translates directly to profunctor composition. Indeed, the profunctor $p' \diamond p$ corresponding to $c' \circ c$ is given by:

$(p' \diamond p) \langle n, m \rangle = (c' \circ c) ( \mathcal N(n, -)) m$

and can be evaluated to:

$(c' ( c ( \mathcal N(n, -))) m \cong \int^{k} c ( \mathcal N(n, -)) k \times p' \langle k, m \rangle$

$\cong \int^{k} p \langle n, k \rangle \times p' \langle k, m \rangle$

which is the standard definition of profunctor composition.

Consider two composable co-presheaf optics, $\mathcal{O}\langle a, b\rangle \langle s, t \rangle$ and $\mathcal{O}\langle a', b' \rangle \langle a, b \rangle$. The first one tells us that there exists a $c$ and a pair of natural transformations:

$l_c (s, a ) = [\mathcal K, \mathbf{Set}] \left(s, c \bullet a \right)$

$r_c (b, t) = [\mathcal K, \mathbf{Set}] \left(c \bullet b, t \right)$

The second tells us that there exists a $c'$ and a pair:

$l'_{c'} (a, a' ) = [\mathcal K, \mathbf{Set}] \left(a, c' \bullet a' \right)$

$r'_{c'} (b', b) = [\mathcal K, \mathbf{Set}] \left(c' \bullet b', b \right)$

The composition of the two should be an optic of the type $\mathcal{O}\langle a', b'\rangle \langle s, t \rangle$. Indeed, we can construct such an optic using the composition $c' \circ c$ and a pair of natural transformations:

$s \xrightarrow{l_c (s, a )} c \bullet a \xrightarrow{c \,\circ \, l'_{c'} (a, a')} c \bullet (c' \bullet a') \xrightarrow{assoc} (c \circ c') \bullet a'$

$(c \circ c') \bullet b' \xrightarrow{assoc^{-1}} c \bullet (c' \bullet b') \xrightarrow{c \, \circ \, r'_{c'} (b', b)} c \bullet b \xrightarrow{r_c (b, t)} t$

# Generalizations

By duality, there is a corresponding optic based on presheaves. Also, (co-) presheaves can be naturally generalized to enriched categories, where the correspondence between left adjoint functors and enriched profunctors holds as well.

# Appendix

I will show that a functor between two co-presheaves that has a right adjoint and therefore preserves colimits:

$c \colon [\mathcal N, \mathbf{Set}] \to [\mathcal K, \mathbf{Set}]$

is equivalent to a profunctor:

$p \colon \mathcal N^{op} \times \mathcal K \to \mathbf{Set}$

The profunctor is given by:

$p \langle n, k \rangle = c ( \mathcal N(n, -)) k$

and the functor $c$ can be recovered using the formula:

$c (a) k = \int^{n'} a (n') \times p \langle n', k \rangle$

where:

$a \colon [\mathcal N, \mathbf{Set}]$

I’ll show that these formulas are the inverse of each other. First, inserting the formula for $c$ into the definition of $p$ should gives us $p$ back:

$\int^{n'} \mathcal N(n, -) (n') \times p\langle n', k \rangle \cong p \langle n, k \rangle$

which follows from the co-Yoneda lemma.

Second, inserting the formula for $p$ into the definition of $c$ should give us $c$ back:

$\int^{n'} a n' \times c(\mathcal N(n', -)) k \cong c (a) k$

Since $c$ preserves all colimits, and any co-presheaf is a colimit of representables, it’s enough that we prove this identity for a representable:

$a (n) = \mathcal N (r, n)$

We have to show that:

$\int^{n'} \mathcal N (r, n') \times c(\mathcal N(n', -)) k \cong c ( \mathcal N (r, -) ) k$

and this follows from the co-Yoneda lemma.