News for hackers. Stuff that matters.
Re:Lenses: Composable Getters and Setters for Functional Pro... by kccqzy on Sunday December 23, 2018 @08:02PM (#18749541)
I love lenses and other optics like prisms and folds, but only in a statically typed language like Haskell. It seems to me that it would be very hard to get things right in a dynamically typed language:

* First, it's possible that different elements of an array or different properties of an object has different shapes. I can't imagine how lenses can deal with that in an idiomatic way.

* Second, it's just difficult to get things right without a compiler's help. Forget a `traverse`? Now you are operating nonsense on the wrong level of your nested data structure. Trying to modify an inner structure but you only have a Fold instead of a Traversal? You won't know it won't work without running it to get some cryptic error.

* Furthermore, without types, we obscure the fact that setters and getters are unified: if `a` stands for the part and `s` for the whole, we just have

    (a -> f a) -> (s -> f s)
that is, given a way to transform some inner part, we can transform the whole as well. Getters and setters are just supplying a concrete choice of f, namely the const functor and the identity functor. If we pick f to be the identity functor, then with some unwrapping we have

    (a -> a) -> (s -> s)
so if there is a function that can modify the part, we have a function that can modify the whole by keeping the rest the same and modify that part. If we pick f to be the const functor, then with some unwrapping we have

    (a -> r) -> (s -> r)
so if there is a function that can extract the value from the part, we can extract the value from the whole as well. Without types, this rather elegant unification of getter and setter seems to be lost.
Re:Lenses: Composable Getters and Setters for Functional Pro... by lilactown on Monday December 24, 2018 @02:04AM (#18750864)
Everything you said has nothing to do with lenses and everything to do with trying to satisfy some statically-typed aesthetics.

> First, it's possible that different elements of an array or different properties of an object has different shapes

I can't imagine how this would introduce any sort of difficulties w.r.t. lenses that aren't already present with normal getting and setting of nested structures (aka probably not much).

> Second, it's just difficult to get things right without a compiler's help

This has nothing to do with lenses and everything to do with you preferring the feel of static types. You go on to talk about a bunch of issues with complex constructs that really only exist in static typing.

Sometimes you write a wrong thing and get a runtime error. This happens in all dynamically typed languages (and many statically typed as well).

> Furthermore, without types... this rather elegant unification of getter and setter seems to be lost.

Like ya do. I don't see how this has any impact at all on developing applications.

Re:Lenses: Composable Getters and Setters for Functional Pro... by freshhawk on Monday December 24, 2018 @05:07AM (#18751390)
I use the lentes[1] library in Clojure all the time, doing all these things. With dynamic types lenses/prisms are the same thing, which I find nice. The library exposes a function called `id_setter` to go along with the core function `identity` to make use of the last bit of lens behaviour you describe.

I'm pretty sure it isn't types that are causing the aesthetic and usability differences you are seeing. I feel the same way about looking at js and typescript lens use. I thought it was just the result of using a concept from one paradigm in a language designed for another, which caused some awkwardness.

[1] http://funcool.github.io/lentes/latest/

Re:Lenses: Composable Getters and Setters for Functional Pro... by whateveracct on Monday December 24, 2018 @03:03AM (#18751051)
> Furthermore, without types, we obscure the fact that setters and getters are unified.

This is way beyond what most people need to know to benefit from lens though.

Haskell itself has a counterexample: aeson-lens. It gives you lenses and prisms for working with JSON ASTs. Any unityped language could benefit from lenses for their unityped structures (JS and Clojure come to mind.)

Re:Lenses: Composable Getters and Setters for Functional Pro... by beezee on Tuesday December 25, 2018 @10:34AM (#18757751)
Does unification of getter and setter have to be lost?

Assuming you are in a unityped context, hkts are gone, functor is not a thing. so

  (a -> f a) -> (s -> f s)
is not meaningful. If we pretend that unityped means everything is parametrically polymorphic, for the purpose of this discussion, can't we leverage parametricity to choose a representation that provides this unification?

for example, if i provide laws that show for a given

  lens :: (s -> a, s -> a -> s)
that

  (snd lens) s (fst lens $ s) == id s
doesn't that provide the same assurance, even if less elegant and not typechecked (the latter of which is much broader than just applicability of lenses)