Select Language

Compound Optics: A Bicategorical Framework for Profunctor Actions

This paper presents a generalization of simple optics to compound optics using the action of bicategories, specifically Prof, on co-presheaves, unifying polynomial functors and Kan extensions.
rgbcw.net | PDF Size: 0.1 MB
Rating: 4.5/5
Your Rating
You have already rated this document
PDF Document Cover - Compound Optics: A Bicategorical Framework for Profunctor Actions

Table of Contents

1. Introduction

Optics, in the context of category theory and functional programming, provide a principled way to access and update parts of complex data structures. While simple optics operate within a single categorical framework using monoidal actions, recent developments involving polynomial functors necessitate a more general theory. This paper introduces compound optics, which generalize the framework by employing the action of a bicategory, specifically the bicategory of profunctors (Prof), on categories of co-presheaves. This unification elegantly captures optics arising from natural transformations between polynomial functors.

2. Simple Optics

The foundation of optics is built on the concept of actegories.

2.1 Actegories and Monoidal Actions

An actegory is defined by the action of a monoidal category $\mathcal{M}$ on a category $\mathcal{C}$, denoted by $\bullet : \mathcal{M} \times \mathcal{C} \to \mathcal{C}$. This can be viewed as a monoidal functor $\bullet: \mathcal{M} \to [\mathcal{C}, \mathcal{C}]$.

2.2 Mixed-Variance Actions and Coends

Given two actions $\bullet_1: \mathcal{M} \to [\mathcal{C}, \mathcal{C}]$ and $\bullet_2: \mathcal{M} \to [\mathcal{D}, \mathcal{D}]$, they can be combined into a mixed-variance action on the product category $\mathcal{C}^{\text{op}} \times \mathcal{D}$. The hom-sets are then extended along these actions, and after averaging over $\mathcal{M}$ using a coend, we obtain the standard form of a simple (mixed) optic:

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

Here, $\langle a, b \rangle$ represents the "focus" and $\langle s, t \rangle$ the "whole object".

2.3 The Category of Optics

These optics form the hom-sets of a category $\mathbf{Opt}$, where objects are pairs $\langle a, b \rangle$.

3. Bicategories

A bicategory generalizes category theory by introducing 2-cells (morphisms between 1-cells), relaxing strict equality to coherent isomorphism.

3.1 Definition and Examples

A bicategory $\mathcal{B}$ consists of 0-cells (objects), 1-cells (arrows between objects), and 2-cells (arrows between 1-cells). For any pair of 0-cells $i, j$, the 1-cells form a hom-category $\mathcal{B}(i, j)$. The canonical example is $\mathbf{Cat}$, with categories as 0-cells, functors as 1-cells, and natural transformations as 2-cells.

3.2 Monoidal Categories as Bicategories

A single-object bicategory is equivalent to a monoidal category. Its 1-endocells are the objects of the monoidal category, composition is the tensor product, and 2-cells are the morphisms.

3.3 Pseudofunctors

A pseudofunctor $F: \mathcal{C} \to \mathcal{D}$ between bicategories is a mapping that preserves the bicategorical structure up to coherent isomorphism, rather than strictly.

4. Compound Optics via Bicategory Actions

The key insight is that the relationship between foci and composite objects in compound optics is not best described by a single monoidal action (an actegory) but by the action of a bicategory. The paper proposes defining compound optics using the action of the bicategory $\mathbf{Prof}$ (of categories, profunctors, and natural transformations) on categories of co-presheaves. Composition of these optics is explained through Kan extensions, providing a robust categorical foundation for their chaining behavior.

5. Polynomial Optics as a Special Case

The theory of compound optics subsumes polynomial optics. Optics arising as natural transformations between polynomial functors, represented as "ommatidia" in prior work, are shown to be a specific instance of the general bicategorical framework. When the acting bicategory is $\mathbf{Prof}$ and the acted-upon categories are co-presheaf categories, the resulting compound optics correspond precisely to these polynomial-based optics.

6. Technical Details & Mathematical Framework

The core technical contribution is the formulation of the optic using a bicategory action and Kan extensions. Given a bicategory $\mathcal{B}$ acting on a category $\mathcal{X}$, and foci grouped into categories $\mathcal{A}$ and $\mathcal{B}$, a compound optic can be defined as a certain Kan extension integral:

$$\text{Optic}((A,B), (S,T)) \cong \int^{M \in \mathcal{B}} \mathcal{X}(\alpha(M, A), S) \times \mathcal{X}(T, \beta(M, B))$$

where $\alpha$ and $\beta$ represent the two-sided action of the bicategory. This generalizes the simple optic formula by replacing the monoidal category $\mathcal{M}$ with a bicategory $\mathcal{B}$ and the actions $\bullet_1, \bullet_2$ with more general bifunctors $\alpha, \beta$ that respect the bicategorical structure.

7. Analytical Framework: Core Insight & Logical Flow

Core Insight: Milewski's paper isn't just another incremental tweak to lens theory; it's a strategic upgrade of the entire categorical operating system for composable data accessors. The move from monoidal actegories to bicategory actions is analogous to generalizing from a single-threaded process to a concurrent, networked system. The core bet is that the true complexity of modern data structures—think nested containers, dependent types, or graph schemas—is inherently multi-dimensional and requires a framework where "context" (the $\mathcal{M}$ or $\mathcal{B}$) is itself a rich, compositional entity. This aligns with trends in applied category theory, where bicategories and profunctors are becoming the lingua franca for open systems and resource-aware computation, as seen in the work on categorical quantum mechanics or the Coq library for optics.

Logical Flow: The argument proceeds with surgical precision. First, it establishes the limitations of the old regime: simple optics, built on monoidal actions, hit a wall with polynomial functors and disparate optic compositions. The diagnosis is that the monoidal category $\mathcal{M}$ is too "flat" to model the separate, interacting contexts of compound foci. The prescription is bicategories, which provide the necessary 2-dimensional structure to track these interactions. The proof of concept is elegant: demonstrate that the bicategory of profunctors, $\mathbf{Prof}$, acting on co-presheaves, naturally yields the previously ad-hoc "ommatidia" of polynomial optics. The logical climax is the unification: what were seen as different species (lenses, prisms, polynomial optics) are now revealed as manifestations of the same bicategorical genus under different parameters.

8. Strengths, Flaws & Actionable Insights

Strengths:

  1. Unifying Power: The framework successfully subsumes polynomial optics and disparate compositions, reducing conceptual fragmentation.
  2. Mathematical Robustness: Leveraging well-established concepts like bicategories, profunctors, and Kan extensions ensures theoretical soundness and connects to a vast body of knowledge.
  3. Future-Proofing: The bicategorical formulation is inherently more expressive, ready to model optics for emerging data structure paradigms (e.g., those involving directed type dependencies or effectful contexts).
Flaws & Critical Gaps:
  1. Computational Tractability: The paper is heavy on existence proofs and universal properties but light on algorithmic insights. How do we compute with these compound optics efficiently? The coend/Kan extension formulation can be prohibitively abstract for implementers. Contrast this with the concrete representation of Van Laarhoven lenses, which map directly to functional code.
  2. Empirical Validation Missing: There is no case study or benchmark showing that this generalized framework solves a real-world software engineering problem that simpler optics cannot. Without this, it risks being a solution in search of a problem for practitioners.
  3. Steep Learning Curve: The prerequisite knowledge in bicategory theory and coend calculus is significant, potentially limiting adoption outside academic circles specializing in categorical semantics.
Actionable Insights:
  1. For Library Designers: Use this paper as a north star for designing the next generation of optics libraries (e.g., in Haskell's `lens` or Scala's `monocle`). Start prototyping a "bicategorical backend" that can gracefully fall back to simple optics for common cases but can handle polynomial and composed optics natively in the general framework.
  2. For Researchers: The most urgent next step is concretization. Follow the path of the original "lens" paper, which gave rise to practical libraries. Develop a canonical, concrete representation for compound optics (perhaps a generalized van Laarhoven form) and provide a compiler from the bicategorical specification to this representation.
  3. For Practitioners: Monitor this line of research. While not immediately actionable, it signals the direction of advanced functional programming abstractions. Understanding it now provides a competitive advantage in designing robust, future-compatible systems.

9. Future Applications & Research Directions

The bicategorical framework for optics opens several promising avenues:

  1. Optics for Dependent Types: Modeling lenses and prisms in dependently typed languages (like Agda or Idris) is challenging. The profunctor-based, bicategorical approach could provide a cleaner semantic foundation for optics in these settings, where types can depend on values.
  2. Integration with Effect Systems: The acting bicategory could be instantiated with categories of effectful computations (e.g., the bicategory of monads). This could lead to a unified theory of "effectful optics" that handle access and update in the presence of I/O, state, or non-determinism.
  3. Database View Updates: The view-update problem in databases is a classic application of lenses. Compound optics could model more complex view definitions involving joins across multiple tables (polynomial-like structures) and provide a categorical proof of correctness for update propagation.
  4. Machine Learning & Differentiable Programming: As seen in frameworks like PyTorch or JAX, accessing and manipulating parts of complex tensors or computational graphs is crucial. A generalized optics framework could provide a principled, composable API for such manipulations, with the bicategory capturing the structure of the computational graph itself.
  5. Bidirectional Transformations (BX): The field of BX, which studies synchronizers between different data representations, has deep connections to lenses. This compound optics framework could offer new, more composable constructions for multi-way synchronizations over complex schemas.

10. References

  1. Boisseau, G., & Gibbons, J. (2018). What You Needa Know about Yoneda: Profunctor Optics and the Yoneda Lemma. Proceedings of the ACM on Programming Languages.
  2. Riley, M. (2018). Categories of optics. arXiv preprint arXiv:1809.00738.
  3. Loregian, F. (2021). Coend Calculus. Cambridge University Press.
  4. Mac Lane, S. (1998). Categories for the Working Mathematician. Springer Science & Business Media.
  5. Pickering, M., Gibbons, J., & Wu, N. (2017). Profunctor optics: Modular data accessors. Art Science and Engineering of Programming.
  6. Spivak, D. I. (2020). Polynomial functors and ommatidia. arXiv preprint arXiv:2006.16941.
  7. Nester, C. (2022). Bicategories in Functional Programming: A Survey. Journal of Functional Programming.
  8. Abramsky, S., & Coecke, B. (2004). A categorical semantics of quantum protocols. Proceedings of the 19th Annual IEEE Symposium on Logic in Computer Science.
  9. Haskell `lens` library documentation and source code. https://hackage.haskell.org/package/lens
  10. nLab community wiki. Entries on Bicategory, Profunctor, Optic. https://ncatlab.org/nlab/show/HomePage