Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Lens combinator reference

A lens in panproto is a triple of functions over a source S, a view V, and a complement C:

get        : S -> V
put        : S × V × C -> S
complement : S -> C

Every constructor in panproto-lens produces a lens whose round-trip laws (GetPut, PutGet, PutPut) are verified by the property tests in panproto-lens/src/laws.rs. The complement carries the data discarded by get so that put can restore it.

For the model behind these combinators, see Lenses and round-trip laws and Lens DSL: denotational semantics.

Optic kinds

Each protolens or protolens chain is classified into an OpticKind. The classification is structural: it follows from the shape of the underlying TheoryTransform, which in turn reflects the schema edge a combinator is applied at.

OpticKindTypical schema edgeComplement
Isobijective relabeling (e.g. edge rename)Unit
Lensprop (single-value projection)dropped data
Prismsum / variant injectionvariant tag
AffineLens composed with Prism(variant tag, dropped data)
Traversalitem (collection, multi-focus)per-position complements

Composition follows the standard optics lattice: Iso is the identity, Traversal is absorbing, Lens + Prism collapses to Affine. The constructor functions for these kinds live in protolens::elementary (atomic steps: add_edge, drop_edge, rename_edge_name, …) and protolens::combinators (composite builders: pipeline, map_items, …). Browse the panproto_lens module index for full signatures.

Combinator families

FamilyModulePurpose
Asymmetric lensesasymmetricClassical S → V transforms with put.
Symmetric lensessymmetricA ↔ B transforms with shared complement.
CompositioncomposeSequential and parallel composition of lenses.
OpticsopticOpticKind enum (Iso, Lens, Prism, Affine, Traversal) and the composition lattice.
FibrationfibrationThe Grothendieck fibration of lenses over schemas.
ProtolensprotolensSchema-parameterized lens families with vertical and sequential composition.
Enrichment registryenrichment_registryCross-crate LayoutEnricher trait and registry for schema-level enrichment fibres (e.g. the parse / decorate / emit lens; see Layout enrichment).
LawslawsProperty-test harness for the three lens laws.

Complement composition

Complement::compose is a partial commutative monoid:

  • It returns Result<Complement, LensError>.
  • It refuses to merge complements whose source-schema fingerprints disagree (ComplementFingerprintMismatch).
  • It refuses to merge complements that disagree on a key (ComplementConflict).

A pre-flight check is available as Complement::is_compatible. The full discussion is in Lenses and round-trip laws.

Protolens composition

Protolenses are natural transformations between schema endofunctors. protolens_composable requires structural equality of the intermediate endofunctor (same precondition, same transform) before vertical_compose will run; otherwise CompositionMismatch. Two instantiation modes are available:

ModeFunctionUse
FusedinstantiateSingle morphism preserving migration metadata. Default for production.
Sequentialinstantiate_sequentialStep-by-step folding through intermediate schemas. Used by property tests to inspect each step.

Both satisfy the lens laws.

See also