[Haskell-cafe] Representation of lenses
tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Tue Jan 27 17:25:35 UTC 2015
On Tue, Jan 27, 2015 at 05:18:41PM +0000, David Turner wrote:
> 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?
Yes (with the caveat that I don't actually know how to prove this).
> 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
No, they are equivalent.
> So, my question is: what is the advantage of representing lenses in
> the way that they are?
The advantage that's most obvious to me is polymorphism. You can use a lens
forall f. Functor f => (s -> f s) -> a -> f a
where the callee expects a traversal
forall f. Applicative f => (s -> f s) -> a -> f a
This is very convenient in practice. Perhaps there are other practical
advantages that someone else can explain.
More information about the Haskell-Cafe