Optics¶
Optics and lenses in enriched category theory.
optics
¶
Optics: composable bidirectional transformations in Rel(V).
Optics generalize lenses and prisms to provide composable "accessors" that can get, set, and transform parts of a structure. In a V-enriched setting, optics are formulated via profunctors or as pairs of morphisms with specific laws.
The general optic from (S, T) to (A, B) in a monoidal category is a coend:
Optic(S, T, A, B) = ∫^M S → M ⊗ A × M ⊗ B → T
which factors a transformation through a residual M.
This module provides concrete optic types:
Optic (abstract)
├── Lens — get/put on product structures
├── Prism — match/build on coproduct structures
├── Adapter — invertible optics (isomorphisms)
└── Grate — closed-structure optics
compose_optics() — compose two optics sequentially
These are V-enriched optics: forward() and backward() return
Morphism (tensor-shaped fuzzy relations) and backward()
joins over the complement axis using the algebra's join. This is a
distinct abstraction from didactic.Lens (instance-level, with an
explicit complement value) and from didactic.DependentLens (schema-
level, wrapping panproto.ProtolensChain). Didactic 0.15.0 does not
expose a V-enriched lens abstraction, so this module retains its own
implementation.
Optic
¶
Optic(source: SetObject, target: SetObject, focus_source: SetObject, focus_target: SetObject, algebra: Algebra | None = None)
Bases: ABC
Abstract optic from (S, T) to (A, B).
An optic provides a bidirectional transformation between a "whole" (S/T) and a "part" (A/B). S is the source whole type, T is the target whole type, A is the source part type, and B is the target part type.
For simple (non-polymorphic) optics, S = T and A = B.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
The source whole S.
TYPE:
|
target
|
The target whole T.
TYPE:
|
focus_source
|
The source part A.
TYPE:
|
focus_target
|
The target part B.
TYPE:
|
algebra
|
The enrichment algebra.
TYPE:
|
Source code in src/quivers/enriched/optics.py
forward
abstractmethod
¶
forward() -> Morphism
The forward (get/match) morphism.
| RETURNS | DESCRIPTION |
|---|---|
Morphism
|
A morphism extracting the focus from the source. |
backward
abstractmethod
¶
backward() -> Morphism
The backward (put/build) morphism.
| RETURNS | DESCRIPTION |
|---|---|
Morphism
|
A morphism putting the focus back into the whole. |
as_profunctor
¶
as_profunctor() -> Profunctor
View this optic as a profunctor S ↛ T.
Computes the profunctor representation by composing forward and backward through the focus.
| RETURNS | DESCRIPTION |
|---|---|
Profunctor
|
The profunctor representation of this optic. |
Source code in src/quivers/enriched/optics.py
Lens
¶
Lens(whole: ProductSet, focus_index: int, algebra: Algebra | None = None)
Bases: Optic
A lens focusing on a component of a product.
A lens from (S, S) to (A, A) on a ProductSet S = A × C consists of:
get: S → A (extract the focus)
put: A × C → S (replace the focus, keeping complement)
In the V-enriched setting, get is a projection morphism and put reconstructs the product.
| PARAMETER | DESCRIPTION |
|---|---|
whole
|
The whole product S = A × C.
TYPE:
|
focus_index
|
Index of the focus component A in the product.
TYPE:
|
algebra
|
The enrichment algebra.
TYPE:
|
Source code in src/quivers/enriched/optics.py
forward
¶
forward() -> Morphism
get: S → A (projection to focus component).
| RETURNS | DESCRIPTION |
|---|---|
ObservedMorphism
|
The projection morphism. |
Source code in src/quivers/enriched/optics.py
backward
¶
backward() -> Morphism
put: A → S (embed focus back, averaging over complement).
In the V-enriched setting, the "put" creates a morphism A → S where fixing the focus component and joining over the complement gives a fuzzy relation.
| RETURNS | DESCRIPTION |
|---|---|
ObservedMorphism
|
The embedding morphism. |
Source code in src/quivers/enriched/optics.py
Prism
¶
Prism(whole: CoproductSet, focus_index: int, algebra: Algebra | None = None)
Bases: Optic
A prism focusing on a component of a coproduct.
A prism from (S, S) to (A, A) on a CoproductSet S = A + C consists of:
match: S → A + C (attempt to extract the focus)
build: A → S (embed the focus into the whole)
| PARAMETER | DESCRIPTION |
|---|---|
whole
|
The whole coproduct S = A + C.
TYPE:
|
focus_index
|
Index of the focus component A in the coproduct.
TYPE:
|
algebra
|
The enrichment algebra.
TYPE:
|
Source code in src/quivers/enriched/optics.py
forward
¶
forward() -> Morphism
match: S → A (partial extraction, zero on non-matching).
| RETURNS | DESCRIPTION |
|---|---|
ObservedMorphism
|
The matching morphism. |
Source code in src/quivers/enriched/optics.py
backward
¶
backward() -> Morphism
build: A → S (inject focus into the coproduct).
| RETURNS | DESCRIPTION |
|---|---|
ObservedMorphism
|
The injection morphism. |
Source code in src/quivers/enriched/optics.py
Adapter
¶
Bases: Optic
An adapter (isomorphism optic).
An adapter between (S, T) and (A, B) consists of an isomorphism pair from: S → A and to: B → T. Every adapter is both a lens and a prism.
| PARAMETER | DESCRIPTION |
|---|---|
from_morph
|
The forward isomorphism S → A.
TYPE:
|
to_morph
|
The backward isomorphism B → T.
TYPE:
|
algebra
|
The enrichment algebra.
TYPE:
|
Source code in src/quivers/enriched/optics.py
verify_isomorphism
¶
Verify that forward and backward form an isomorphism.
Checks from >> to ≈ id_S and to >> from ≈ id_A (when S=T, A=B).
| PARAMETER | DESCRIPTION |
|---|---|
atol
|
Absolute tolerance.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the adapter is an isomorphism. |
Source code in src/quivers/enriched/optics.py
Grate
¶
Grate(source: SetObject, focus: SetObject, index: SetObject, cotraverse_tensor: Tensor, algebra: Algebra | None = None)
Bases: Optic
A grate optic for closed/exponential structures.
A grate from S to A through a "coindexing" object I encapsulates the pattern:
cotraverse: (I → A) → S
In the V-enriched setting, the grate is represented by a morphism from the internal hom [I, A] to S.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
The whole S.
TYPE:
|
focus
|
The focus A.
TYPE:
|
index
|
The coindex I.
TYPE:
|
cotraverse_tensor
|
The tensor for (I → A) → S.
TYPE:
|
algebra
|
The enrichment algebra.
TYPE:
|
Source code in src/quivers/enriched/optics.py
forward
¶
forward() -> Morphism
Extract focus by evaluating at each index.
Produces S → A by marginalizing over the index.
Source code in src/quivers/enriched/optics.py
backward
¶
backward() -> Morphism
Rebuild whole from focus via cotraverse.
Source code in src/quivers/enriched/optics.py
compose_optics
¶
Compose two optics sequentially.
Given outer: (S, T) → (M, N) and inner: (M, N) → (A, B), produces the composed optic (S, T) → (A, B).
The composition is returned as an Adapter wrapping the composed forward and backward morphisms.
| PARAMETER | DESCRIPTION |
|---|---|
outer
|
The outer optic (S, T) → (M, N).
TYPE:
|
inner
|
The inner optic (M, N) → (A, B).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Adapter
|
The composed optic. |