Skip to content

Conversation

@zrho
Copy link
Contributor

@zrho zrho commented Jan 28, 2016

The solution for indexed optics used by lens does not work with profunctor optics. Indexed optics are very useful however, even if their index can't be implicitly forgotten like in lens. Here, I experimented with the following compromise:

For each profunctor p there is a profunctor Indexed p i with which we can define indexed optics by

type IndexedOptic p i s t a b = Indexed p i a b -> p s t

This does not compose to the right with Optics or other IndexedOptics, but composes to the left with Optics. If one has an IndexedOptic and wishes to compose it to the right with another Optic or IndexedOptic, one can use

unIndex
  :: forall p i s t a b. (Profunctor p)
  => IndexedOptic p i s t a b -> Optic p s t a b

to explicity convert an IndexedOptic to an Optic by forgetting indices. Despite of a bit of syntactic inconvenience, I reckon that this is a small price to pay. What do you think?

On that note, what would be a sensible place to put an IndexedTraversable type class like the following in the purescript ecosystem?

class (Traversable t) <= IndexedTraversable i t where
  itraverse :: forall f a b. (Applicative f) => (i -> a -> f b) -> t a -> f (t b)

Not as flexible as the `lens` variant, since indices can
not be forgotten implicitly. Use `unIndex` for that.
@paf31
Copy link
Contributor

paf31 commented Jan 28, 2016

This looks nice, and I think it's a good compromise until I implement equality constraints.

I think we could make a new package for the indexed variants of the Functor hierarchy. Or perhaps they should go into foldable-traversable.

paf31 added a commit that referenced this pull request Jan 28, 2016
@paf31 paf31 merged commit d7f198c into purescript-contrib:master Jan 28, 2016
@paf31
Copy link
Contributor

paf31 commented Jan 28, 2016

👍 Thanks!

@paf31
Copy link
Contributor

paf31 commented Mar 4, 2016

I had a bit more of a think about this. Could something like this work:

type IndexedOptic p i s t a b = Indexing i (p a b) (p s t)

data Indexing i x y = Indexing ((i -> x) -> y)
instance Category Indexing

? I haven't thought through the details but I wanted to make a note.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants