Table of Contents
1. Introduction
In the context of category theory and functional programming, optics provide a principled approach to accessing and updating parts of complex data structures. While simple optics operate using monoidal actions within a single category framework, recent developments involving polynomial functors necessitate a more general theory. This paper introducesComposite Optics, it generalizes this framework through the action of the double category of profunctors, specifically the Profunctor double category Prof, on the category of copresheaves. This unification elegantly captures the optics arising from natural transformations between polynomial functors.
2. Simple Optics
The foundation of optics is built upon the concept of action categories.
2.1 Action Categories and Monoidal Actions
An actegory is defined by an action of a monoidal category $\mathcal{M}$ on a category $\mathcal{C}$, denoted $\bullet : \mathcal{M} \times \mathcal{C} \to \mathcal{C}$. This can be seen as a monoidal functor $\bullet: \mathcal{M} \to [\mathcal{C}, \mathcal{C}]$.
2.2 Mixed Variation 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}$. Then, the hom-set is extended along these actions, and after averaging over $\mathcal{M}$ using a coend, we obtain the standard form of simple (mixed) optics:
$$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$ represents the "ambient object".
2.3 Optical Category
These optics constitute the hom-set of a category $\mathbf{Opt}$, whose objects are pairs $\langle a, b \rangle$.
3. Double Category
Bicategories generalize category theory by introducing 2-cells (morphisms between 1-cells), relaxing strict equations to coherent isomorphisms.
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)$. A typical example is $\mathbf{Cat}$, where categories are 0-cells, functors are 1-cells, and natural transformations are 2-cells.
3.2 Monoidal Categories as Double Categories
A single-object double category is equivalent to a monoidal category. Its 1-endomorphisms are the objects of the monoidal category, composition is the tensor product, and the 2-cells are the morphisms.
3.3 Pseudofunctors
A pseudofunctor $F: \mathcal{C} \to \mathcal{D}$ between bicategories is a mapping that preserves the bicategory structure up to coherent isomorphisms, rather than strictly.
4. Composite Optics Based on Bicategory Action
The key insight is that the relationship between the focus and the composite object in compound optics is not most appropriately described by a single monoidal action (an action category), but rather by an action of a bicategory. This paper proposes defining compound optics using the action of the bicategory $\mathbf{Prof}$ (composed of categories, profunctors, and natural transformations) on the category of copresheaves. The composition of these optics is explained via Kan extensions, providing a solid categorical foundation for their chaining behavior.
5. Polynomial Optics as a Special Case
The theory of compound optics subsumes polynomial optics. Optics that arise as natural transformations between polynomial functors (previously represented as "lenses") are shown to be a specific instance of the general bicategorical framework. When the acting bicategory is $\mathbf{Prof}$ and the acted-upon category is the category of copresheaves, the resulting compound optics correspond precisely to these polynomial-based optics.
6. Technical Details and Mathematical Framework
La kernan teknikan kontribuon konsistigas la formuligon de optikoj per dukategoria ago kaj Kan-etalonoj. Donita dukategorio $\mathcal{B}$ aganta sur kategorio $\mathcal{X}$, kaj fokusoj grupigitaj en kategoriojn $\mathcal{A}$ kaj $\mathcal{B}$, komponita optiko povas esti difinita kiel certa Kan-etalona integralo:
$$\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))$$
Here, $\alpha$ and $\beta$ represent the two-sided actions of a bicategory. This generalizes the simple optic formula by replacing the monoidal category $\mathcal{M}$ with a bicategory $\mathcal{B}$, and replacing the actions $\bullet_1, \bullet_2$ with the more general bifunctors $\alpha, \beta$ that respect the bicategory structure.
7. Analytical Framework: Core Insights and Logical Threads
Core Insights: Milewski's paper is not merely another incremental adjustment to lens theory; it is a strategic upgrade to the entire categorical operating system for composable data accessors. The shift from monoidal action categories to bicategorical actions is analogous to generalizing from single-threaded processes to concurrent, networked systems. Its core thesis is that the true complexity of modern data structures—such as nested containers, dependent types, or graph schemas—is inherently multidimensional, requiring a framework where the "context" ($\mathcal{M}$ or $\mathcal{B}$) itself is a rich, composable entity. This aligns with trends in applied category theory, seen in work on categorical quantum mechanics or the Coq optics library, where bicategories and Profunctors are becoming the lingua franca for open systems and resource-aware computation.
Logical Structure: The argument proceeds with surgical precision. First, it establishes the limitations of the old paradigm: the simple optics built from monoidal actions encountered a bottleneck when faced with polynomial functors and diverse optic compositions. The diagnosis was that the monoidal category $\mathcal{M}$ was too "flat" to model the independent, interacting contexts of composite foci. The solution is the bicategory, which provides the necessary two-dimensional structure to track these interactions. The proof of concept is elegant: demonstrating that the action of the Profunctor bicategory $\mathbf{Prof}$ on copresheaves naturally yields the previously ad-hoc defined "little eyes" of polynomial optics. The logical climax is unification: what were once seen as different species (lenses, prisms, polynomial optics) are now revealed as manifestations of the same bicategorical genus under different parameters.
8. Strengths, Weaknesses, and Actionable Insights
Advantages:
- Birlashtirilgan imkoniyatlar: Ushbu ramka polinomli optika va turli kombinatsiyalarni muvaffaqiyatli o'z ichiga oladi, tushunchaviy parchalanishni kamaytiradi.
- Matematik mustahkamlik: Using well-established concepts such as bicategories, profunctors, and Kan extensions ensures theoretical reliability and establishes connections with a vast body of knowledge.
- Future-Oriented: The bicategorical formulation is inherently more expressive and is ready to model optics for emerging paradigms of data structures, such as those involving directed type dependencies or effectful contexts.
- Computational Tractability: The paper focuses on existence proofs and universal properties, but provides less insight into algorithmic aspects. How can we efficientlyComputationThese composite optics? The formulation of residual/Kan extensions might be too abstract for implementers. This contrasts with the concrete representation of Van Laarhoven lenses, which can be directly mapped to functional code.
- Lack of empirical validation: No case studies or benchmarks demonstrate that this general framework solves practical software engineering problems that simpler optics cannot. Without this, for practitioners, it might merely be a solution in search of a problem.
- Steep learning curve: The prerequisite knowledge of bicategory theory and coend calculus is substantial, potentially limiting adoption outside academic circles specializing in categorical semantics.
- For library designers: Use this paper as a North Star for designing next-generation optics libraries (e.g., Haskell's `lens` or Scala's `monocle`). Begin prototyping a "bicategorical backend" that gracefully falls back to simple optics for common cases but can natively handle polynomial and composite optics within a general framework.
- For researchers: The most urgent next step isConcretize. Following 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 that representation.
- For practitioners: Focus on this research direction. While it cannot be immediately put into practice, it points the way toward advanced functional programming abstractions. Understanding it now can provide a competitive advantage in designing robust, future-proof systems.
9. Future Applications and Research Directions
The bicategorical framework of optics opens up several promising avenues:
- Dependent optics: Modeling lenses and prisms in dependently typed languages (such as Agda or Idris) is challenging. The Profunctor-based bicategorical approach can provide a clearer semantic foundation for optics in these settings, where types can depend on values.
- Integration with Effect Systems: Action bicategories can be instantiated by computational categories with effects (e.g., monadic bicategories). This may lead to a unified theory of "optics with effects" for handling access and updates in the presence of I/O, state, or nondeterminism.
- Database View Updates: The problem of updating views in databases is a classic application of lenses. Composite optics can model more complex view definitions involving joins across multiple tables (similar to polynomial structures) and provide a categorical correctness proof for update propagation.
- Machine Learning and Differentiable Programming: As seen in frameworks like PyTorch or JAX, accessing and manipulating parts of complex tensors or computational graphs is crucial. A general optics framework can provide a principled, composable API for such operations, where the bicategory captures the structure of the computational graph itself.
- Bidirectional Transformations: BX field studies synchronizers between different data representations, deeply connected to lenses. This composite optical framework can provide new, more composable constructions for multiplex synchronization on complex patterns.
10. References
- Boisseau, G., & Gibbons, J. (2018). What You Needa Know about Yoneda: Profunctor Optics and the Yoneda Lemma. Proceedings of the ACM on Programming Languages.
- Riley, M. (2018). Categories of optics. arXiv preprint arXiv:1809.00738.
- Loregian, F. (2021). Coend Calculus. Cambridge University Press.
- Mac Lane, S. (1998). Categories for the Working Mathematician. Springer Science & Business Media.
- Pickering, M., Gibbons, J., & Wu, N. (2017). Profunctor optics: Modular data accessors. Art Science and Engineering of Programming.
- Spivak, D. I. (2020). Polynomial functors and ommatidia. arXiv preprint arXiv:2006.16941.
- Nester, C. (2022). Bicategories in Functional Programming: A Survey. Journal of Functional Programming.
- Abramsky, S., & Coecke, B. (2004). A categorical semantics of quantum protocols. Proceedings of the 19th Annual IEEE Symposium on Logic in Computer Science.
- Haskell `lens` library documentation and source code. https://hackage.haskell.org/package/lens
- nLab community wiki. Entries on Bicategory, Profunctor, Optic. https://ncatlab.org/nlab/show/HomePage