nLab lens (in computer science)

Contents

Contents

Idea

In computer science, originally in database theory, a data structure called lenses [Bohannon, Pierce & Vaughan (2006, §3), Foster, Greenwald, Moore, Pierce & Schmitt (2007, §3)] is used to formally capture situations where from some data(-base) may be extracted a specific view (say one field in a record structure) in such a way that changes made to the view can be reflected as updates to the original data.

The same construction has been devised on numerous occasions (cf. Hedges (2018)).

In particular, “well-behaved lenses” (BPV06, Def. 3.2those for which updates of the “view” completely overwrite any previous such changes) turn out to equivalently be just the coalgebras of the costate comonad (cf. monads in computer science), an observation due to O’Connor (2010), (2011); see Prop. below.

More generally, there are two different approaches to lenses:

  • Lawful lenses are an algebraic structure which axiomatize product projections. The laws govern the ways views and updates relate. These are generalized into Delta lenses, which are more flexible lawful lenses.

  • Lawless lenses, and in particular bi-directional or polymorphic lenses, separate the two directions of view and update so that the laws no longer type-check. These lawless lenses are used to organize bi-directional flow of data, and are generalized into optics in the functional programming literature.

An alternate generalization of lawless lenses is put forward in Spivak 19 as the Grothendieck construction of the fiberwise dual of an indexed category. This notion of lawless lens has been adopted in the context of categorical systems theory because they represent a bidirectional stateful computation which describes the way some systems expose and update their internal state. For instance, see Myers, Spivak & Niu or Hedges (2021).

In this sense, lawless lenses are applications of two mathematical frameworks which are interesting in their own right: fibrations and optics (thus Tambara modules).

Definition

Let (C,1,×)(\mathbf{C}, 1, \times) be a category with finite products.

Definition

Let S,VS,V be objects of C\mathbf C. A (lawful) lens LL, with source SS and view VV, is a pair of arrows get:SV\mathrm{get} : S \to V and put:V×SS\mathrm{put} : V \times S \to S, often taken to satisfy the following equations, or lens laws:

  1. (PutGet) the get of a put is the projection: getput=π 0\mathrm{get} \mathrm{put} = \pi_0.
  2. (GetPut) the put for a trivially updated state is trivial: putget,1 S=1 S\mathrm{put} \langle \mathrm{get}, 1_S \rangle = 1_S.
  3. (PutPut) composing puts does not depend on the first view update: put(1 V×put)=putπ 0,2\mathrm{put}(1_V \times \mathrm{put}) = \mathrm{put} \pi_{0,2}.

Remark

The two morphisms comprising a lens can also be called ‘view’ and ‘update’. More generally, they are referred to as the ‘forward’ and ‘backward’ parts of the lens.

Remark

Sometimes a lens satisfying all three laws is said to be lawful. Sometimes it is said that a well-behaved lens satisfies (1) and (2) and a very well-behaved lens satisfies also (3).

The category of lenses

Lenses (regardless of their lawfulness) organize in a category Lens(C)\mathrm{Lens}(\mathbf C) whose objects are the same as C\mathbf C and whose morphisms XYX \to Y are lenses with states XX and views YY. The identity lens is given by (1 X,π 1):XX(1_X, \pi_1) :X \to X. Composition of (get 1,put 1):XY(\mathrm{get}_1, \mathrm{put}_{1}):X \to Y and (get 2,put 2):YZ(\mathrm{get}_2, \mathrm{put}_{2}):Y \to Z is given by:

get 12=get 2get 1 \mathrm{get}_{12} = \mathrm{get}_2 \circ \mathrm{get}_1
put 12=put 1((put 2(1 Z×get 1))×1 X)(1 Z×Δ X) \mathrm{put}_{12} = \mathrm{put}_1 \circ ((\mathrm{put}_2 \circ (1_Z \times \mathrm{get}_1)) \times 1_X) \circ (1_Z \times \Delta_X)

The put 12\mathrm{put}_{12} morphism is probably easier to describe using generalized elements:

put 12:(z,x)put 1(put 2(z,get 1(x)),x) \mathrm{put}_{12} : (z,x) \mapsto \mathrm{put}_1(\mathrm{put}_2(z, \mathrm{get}_1(x)), x)

Remark

Crucially, associativity of this composition relies on naturality of the diagonals, which is a given in cartesian categories but not in more general monoidal categories. Optics are a sweeping generalization of lenses which overcomes this obstacle.

Moreover, the cartesian product of C\mathbf C endows Lens(C)\mathrm{Lens}(\mathbf{C}) of a monoidal product.

Properties

Proposition

Lenses are algebras for a monad generated by the adjunction:

Proof

See (Johnson-Rosebrugh-Wood 2010, Proposition 9). See also the possibility operator.

Proposition

Let C=Set\mathbf{C} = Set. Then every lens L=(S,V,get,put)L = (S, V, \mathrm{get}, \mathrm{put}) is equivalent a “constant complement” lens whose Get is a product projection π 1:C×VV\pi_{1} : C \times V \to V and whose Put is the function π 0,2:C×V×VC×V\pi_{0,2} : C \times V \times V \to C \times V for some set CC.

Proposition

(well-behaved lenses are the costate coalgebras)

For C\mathbf{C} a cartesian closed category, the well-behaved lenses in C\mathbf{C} are equivalently the coalgebras of the CoState comonad, i.e. that induced by the internal hom-adjunction:

This is due to O’Connor (2010), O’Connor (2011), for further discussion see Gibbons & Johnson (2012), Section 3.2.

Generalizations

There are many generalizations of lenses which have been proposed, however they can be broadly classified into those which satisfy analogues of the lens laws, and those without any axioms or laws.

Lenses without laws

  • One generalization considers the lenses from the previous section as monomorphic by contrast to polymorphic lens which go between pairs of types, λ:(S,T)(A,B)\lambda: (S, T) \to (A, B), consisting of a view function, v λ:SAv_{\lambda}: S \to A, and an update function u λ:S×BTu_{\lambda}: S \times B \to T. Without further conditions, these are known as bimorphic lenses. To impose conditions comparable to the lens laws above requires that the types be related.

These sorts of lenses are generalized by Spivak 19. For a quick explanation of how these sorts of generalized lenses are of use in systems theory, see Myers20; for a longer explanation, see Chapter 2 of Myers.

  • An optic generalizes the way lenses ‘remember’ state from the forward part to the backward part, avoiding the necessity of a cartesian structure by swapping it with a sufficiently rich actegorical context.

Lenses with laws

  • Delta lenses are a generalization which does satisfy laws. Here we have categories SS and VV called the source and view, together with a Get functor g:SVg : S \to V and a function φ:S 0× V 0V 1S 1\varphi \colon S_{0} \times_{V_{0}} V_{1} \to S_{1} which takes a pair (sS,u:gsvV)(s \in S, u : g s \to v \in V) to a morphism

    φ(s,u):sp(s,u)\varphi(s, u) : s \to p(s, u) in SS where p(s,u)=cod(φ(a,u))p(s, u) = cod(\varphi(a, u)) is the Put function. The function φ\varphi must also satisfy three lens laws. When SS and VV are codiscrete categories, delta lenses are equivalent to a lens in Set; see (Johnson-Rosebrugh 2016, Proposition 4).

  • A morphism between directed containers is another kind of generalised lens satisfying laws called update-update lenses; see (Ahman-Uustalu 2017, Section 5). These are equivalent to cofunctors.

References

General

Relation to the costate comonad

The observation that lenses are equivalently nothing but the coalgebras of the costate comonad (cf. monads in computer science) is due to:

Early review:

Further details:

Further review:

Blog posts, talk slides, and other material

Last revised on January 26, 2023 at 14:47:55. See the history of this page for a list of all contributions to it.