I took part in the Dagstuhl Seminar on Bidirectional Transformations “BX” earlier this month. It was a meeting of people from four communities—databases, graph transformations, programming languages, and software engineering—discussing their various perspectives—namely the view–update problem in databases, triple graph grammars, lenses, and model synchronization—on the common problem of “BX”.
While there, I reported on a marvellous observation made by Russell O’Connor, that lenses are exactly the coalgebras for the costate comonad. That is, the independently identified notion of a “very well-behaved lens” in the work of Pierce and others coincides exactly with the categorical notion of a “coalgebra” for a particular comonad, the “costate” comonad. I’ll unpack that claim here.
Pierce’s lenses are pairs of functions between “source” and “view” datatypes and : a “get” function and a “put” function . The story is that the view is some projection of the data in the source—perhaps a subset of the data, or the data in a simpler format—and so in order to update the source given a modified view, one needs also a copy of the original source from which to reconstruct the missing information.
For these two functions to capture a “well-behaved” lens, they should satisfy the so-called Get–Put and Put–Get laws:
The Get–Put law says that if you “get” a view of the source, and then “put” it straight back without modifying it, the source remains unmodified: a no-op edit on the view translates into a no-op on the source. The Put–Get law says that if you “put” any view into a source and then “get” it back, you end up with the view you first thought of: nothing is lost from the view when it is put back.
Additionally, for these two functions to capture a “very well-behaved” lens, they must satisfy a third law, the Put–Put law:
In words, “put”ting back two views then is equivalent to “put”ting back just the second; any changes to the source from putting back are completely overwritten when putting back . (This turns out to be rather a strong condition, requiring that the source basically factors into the view and a completely independent “complement”; few real applications of bidirectional transformation satisfy it. But that’s another story.)
The costate comonad
Intuitively, comonads capture “data in context”. A comonad consists of a functor together with two natural transformations and that extract the data from its context and duplicate the context, satisfying the three axioms:
One example of a comonad is the “costate” construction: for fixed , define functor by
so that the “map” function for satisfies . The operations are given by
Verifying that these definitions satisfy the comonad axioms is left as an exercise for the interested reader.
(Incidentally, I think it’s called the “costate” comonad more because it is the dual of the “state” monad , rather than because it has anything to do with stateful computations. However, it does model state in the sense of stored variables; and indeed, Russell O’Connor’s blog posting calls the “store” comonad.)
Coalgebras of a comonad
For a functor , an -coalgebra is a pair of a type and a function . A “coalgebra for a comonad ” is a -coalgebra that interacts well with the operations and of the comonad; that is, the function should also satisfy the laws:
(Another incidentally: I don’t have a feeling for what these laws mean, in the way that I do for the laws of an algebra of a monad. At least for the free monads that represent terms with free variables, an algebra is a pair such that makes sense as an “expression evaluator”—it respects singleton variables and substitution. It’s clear to me that the laws of a coalgebra for a comonad are the obvious duals of those for the algebra of a monad; and that they describe the interesting ways of putting together the coalgebra operation with the comonad operations; but I still don’t have a direct intuition. Any comments gratefully received!)
Lenses are coalgebras of the costate comonad
Now it’s just a matter of putting the pieces together. Curry the “put” function of a lens to obtain , and define a lens to be the fork of the “get” and “put” functions:
Note that now where is the costate comonad. The Get–Put law is equivalent to the counit axiom of the coalgebra:
And the Put–Get and Put–Put laws together are equivalent to the coassociativity axiom: