Lawful State Lenses #
This file specializes polynomial-functor lenses to state lenses. In Poly,
following Spivak and Niu's Polynomial Functors: A Mathematical Theory of
Interaction, a dependent lens p → q is a natural transformation between
polynomial functors, represented by a forward map on positions and a backward
map on directions. For the self-monomial polynomial PFunctor.selfMonomial σ,
both positions and directions are states of type σ. Thus a lens
selfMonomial σ → selfMonomial τ has exactly the shape of a classical state
lens:
get : σ → τ, reading a view/component state;put : τ → σ → σ, writing an updated view/component back into the source.
The law classes below use the standard bidirectional-programming names from
Foster, Greenwald, Moore, Pierce, and Schmitt's lens work: PutGet, GetPut,
and the stronger PutPut law, often called very well-behavedness. We keep
these laws on selfMonomial lenses rather than on arbitrary polynomial lenses:
for a general PFunctor.Lens p q, the backward map returns a direction of p,
not a new position of p, so the usual get/put round-trip equations are not
well-typed without this state-polynomial specialization.
The binary relation IsSeparated captures the non-interference condition for
two lenses with the same source. This is the condition used by Pacheco and
Cunha's point-free lens calculus to justify splitting a source into two
independently updatable views: updating the left and right views should not
change the other view, and the two updates should commute. It is also the
state-lens analogue of the separation-logic intuition behind separation lenses:
two views describe spatially separated portions of a state.
References:
- David I. Spivak and Nelson Niu, Polynomial Functors: A Mathematical Theory of Interaction, Cambridge University Press, 2025.
- J. Nathan Foster, Michael B. Greenwald, Jonathan T. Moore, Benjamin C. Pierce, and Alan Schmitt, Combinators for Bidirectional Tree Transformations: A Linguistic Approach to the View-Update Problem, POPL 2005.
- Hugo Pacheco and Alcino Cunha, Generic Point-free Lenses, MPC 2010.
A state lens from source state σ to view state τ.
This is not a new kind of optic. It is the existing polynomial-functor lens
between the self-monomial polynomials selfMonomial σ and selfMonomial τ.
Its position map is the state get, and its direction map is the state put.
Instances For
Basic operations #
Construct a state lens from a getter and a putter.
Instances For
Read the view/component state selected by a state lens.
Instances For
Write an updated view/component state back into the source state.
The argument order follows the usual bidirectional-programming convention:
L.put t s updates source s with new view t.
Instances For
Compose state lenses.
Instances For
The first projection state lens from a product state.
Instances For
The second projection state lens from a product state.
Instances For
Lens laws #
The PutGet law for a state lens.
In the bidirectional-programming literature this is the consistency law:
after writing an updated view t into a source s, reading the view back
returns exactly t.
Reading immediately after writing returns the written view.
Instances
The GetPut law for a state lens.
In the bidirectional-programming literature this is the stability law: writing back the view already present in a source leaves the source unchanged.
Writing the current view back into the source is a no-op.
Instances
The PutPut law for a state lens.
This is the constant-complement law used in the classical view-update
literature: two consecutive writes are observationally the same as the second
write alone. Lenses satisfying PutGet, GetPut, and PutPut are commonly
called very well-behaved.
Consecutive writes collapse to the last write.
Instances
A well-behaved state lens.
This bundles the two basic round-trip laws, PutGet and GetPut, following
the standard terminology for asymmetric lenses in bidirectional programming.
Instances
A very well-behaved state lens.
This extends well-behavedness with PutPut, the law saying that repeated
updates keep a constant complement and collapse to the last update.
Instances
PutPut is intentionally not given as a generic composition instance here.
Very well-behaved asymmetric lenses are closed under the classical lens
composition operation, but the proof is more naturally developed alongside the
larger bidirectional-lens algebra if and when it is needed.
Separation / non-interference #
Non-interference for two well-behaved state lenses with a common source.
This relation says that the left and right lenses describe separated portions of the same source state. Updating either view leaves the other view unchanged, and left and right updates commute. Pacheco and Cunha use this commutation condition to justify a split combinator for two lenses with a shared concrete domain; in program-logical terminology, it is the lens-level analogue of two spatially separated state views.
Updating the right view does not change the left view.
Updating the left view does not change the right view.
Left and right updates commute.