[Haskell-cafe] Representation of lenses

David Turner dct25-561bs at mythic-beasts.com
Tue Jan 27 17:18:41 UTC 2015


Hi,

I'm planning on discussing lenses with some colleagues shortly so I'm
consolidating some of my notes into a coherent story. At one point I
found myself describing lenses as a way of packaging up a getter and a
setter into a single thing. However, this raises the question: why not
just use a pair (getter, setter)? More precisely, I believe that the
types (a -> s, s -> a -> a) and (forall f. Functor f => (s -> f s) ->
a -> f a) are isomorphic. Is that right?

I see that one advantage of the lens type is that you can use (.) to
compose them since they're just functions, but that doesn't bother me
much and it seems I could define another operator to compose (getter,
setter) lenses and the rest of the machinery would work either way.

It's also possible that you can't get the full generality of (forall
f. (s -> f t) -> a -> f b) lenses with getter/setter pairs, although I
haven't worked through the details yet so don't know either way.

So, my question is: what is the advantage of representing lenses in
the way that they are?

Many thanks,

David


More information about the Haskell-Cafe mailing list