ingolemo / python-lenses

A python lens library for manipulating deeply nested immutable structures
GNU General Public License v3.0
310 stars 19 forks source link

Parts should focus a tuple #30

Closed Gurkenglas closed 3 years ago

Gurkenglas commented 3 years ago

Lists are supposed to have arbitrary length, tuples are supposed to have constant length.

ingolemo commented 3 years ago

Tuples are also supposed to be heterogeneous, while lists are homogeneous. Parts focuses something that is homogeneous but constant length, so it's not obvious which of the two it should return.

The Haskell equivalent partsOf also returns a list, probably because trying to type this as a tuple in Haskell is... non-trivial. That's not an issue in python since we have tuple[T, ...], but then you lose the "constant length" part which kinda defeats the point.

Gurkenglas commented 3 years ago

Does Parts focus something that is homogenous? Instance means that the four type parameters of a lens don't need to be homogenous. It is clear how to have Parts handle types changing, but not how to have it handle length changing.

ingolemo commented 3 years ago

Yes. To be well-typed, an optic's foci should all be of a single type (ignoring polymorphism). Now, obviously, python is a dynamic language, and it would be unpythonic to insist that your optics always be well-typed. If you want to have Parts select heterogeneous foci then go ahead. It's no worse than having a heterogeneous list in regular python code.

Instance is not a good example of a well-typed optic. It's a compromise that fills the hole left by python's lack of sum-types. I'm not sure that the four type parameters are relevant here. Yeah, there are four different type parameters to all optics, but they have nothing to do with allowing for heterogeneous data.

I can certainly see your point, and if I were writing it over again then I might make a different choice; list may be theoretically correct , but tuple is probably more practical in python. I'm not sure if it's enough to justify changing the existing api and introducing extra conversion overhead.