well-typed / optics

Optics as an abstract interface
371 stars 23 forks source link

Coindexed optics? #23

Open adamgundry opened 5 years ago

adamgundry commented 5 years ago

Is there anything stopping us adding coindexed optics, apart from wanting to keep down the number of type variables? Perhaps there are tricks we can play here, e.g. see https://github.com/well-typed/optics/pull/19#issuecomment-444459782.

arybczak commented 3 years ago

@phadej explored that in https://oleg.fi/gists/posts/2021-01-04-coindexed-optics.html.

I think we could fit it into optics by representing coindexes similar to indexes, but instead of having i -> j -> k -> ... -> r it would be (i -> r) -> (j -> r) -> ... -> r and then at the end you'd supply data constructors of the error type (id for one coindex, for two it could be Left Right etc.).

It looks like the biggest issue would be getting types right. Ideally we'd want all prisms etc. to be coindexed, the problem seems to be the choice of the error type. Should it be String? Good for printing, not very good for control flow. Should we have a custom type for every prism etc.? Sounds messy.