[Haskell-cafe] Lenses and records and ecosystem

Anthony Clayden anthony_clayden at clear.net.nz
Tue Feb 14 09:36:13 UTC 2017


> On Sat, Feb 11, 2017 at 21:04, David Menendez wrote:

> > On Fri, Feb 10, 2017 at 7:53 PM, Anthony Clayden wrote:
> >
> > Certainly (.) for lenses works 'backwards'
> > (i.e. suffix style)
> > compared to function-prefix style.
> > (As any Lens tutorial will say.)
> >
> 
> Those tutorials are oversimplifying.

Thank you David,
ho hum I'm not getting much feedback about ecosystem/
co-existence; nor a specialised composition operator.

>  Lenses (and their relatives) transform operations, not
values.

So it must be a stubborn superstition on my part
that getting/putting from/to structures is about values.

I'm not here to 'knock' lenses (of whichever model),
but every time I scratch the surface,
the answers wander off into category theory
and higher-order types.

I'll draw a comparison with another place in Haskell
where category theory has huge benefits: monads.

There's less than a handful of operators for monads,
and those are conveniently handled behind some elegant
syntactic sugar (do-notation).

So a newbie doesn't really need to grok monads.
They can get on and use them; learn the theory later.

Can that be said of lenses?
I guess part of the trouble is that H98 record operations
have gobbled up so much of the syntactic design space.

Nevertheless I don't see what syntactic sugar could help
with lenses.

> Composition of lenses is exactly like composition of
functions, with
> the inner-most operation on the right.

That doesn't seem to say more than "it's composition".
If I want to get a foo out of a bar out of a baz in a s, 
I'd (perhaps naievely) go

    foo (bar (baz s)) -- i.e.
    (foo . bar . baz) s

And I'm using function application all the way through.

I'd only think of doing

    s.baz.bar.foo

If I was trying to mimic OO style methods.
And anyway with lenses, that's exactly what
I _can't_ do.

Lenses do not use function application of the lens to the
structure.
You need an extra operator (view, over).

Then I'm not seeing the syntactic merit of using
composition (.) for lenses.

It's certainly making the mental machinery incompatible
for lenses vs records, even if they can co-exist
semantically.


> A lens foo :: Lens
> O I transforms an operation on I into an operation on O. A
> composition outer.inner turns an operation on the target
> of inner into an operation on the source of outer.
> 
> This is demonstrated by the way over distributes with (.):
> 
>     over (outer . inner) = over outer . over inner
> 
> What confuses people is view, which is contravariant:
> 
>     view (outer . inner) = view inner . view outer
> 
> but you can't avoid having one of over and view be
> contravariant.

OK. (Why did it need so much category theory to explain
that?)

And why do those nifty equivalences have to intrude so much
into the surface syntax, just to get a value out of a
structure?


AntC


More information about the Haskell-Cafe mailing list