well-typed / optics

Optics as an abstract interface
375 stars 24 forks source link

Constrained traversals #429

Open adamgundry opened 3 years ago

adamgundry commented 3 years ago

See discussion starting from https://discord.com/channels/701454007775920128/855385315942203433/855825282833711115

type TraversalC c s t = (forall a b. c a b => p a b) -> p s t

This allows traversing structures containing multiple different types, satisfying a constraint. Pick c ~ Equals a b where class (a1 ~ a, b1 ~ b) => Equals a b a1 b1 to recover existing traversals.

Can/should we generalise optics to support this?

phadej commented 2 years ago

That discussion link needs login.

phadej commented 2 years ago

You have a typo, missing p:

type TraversalC p c s t = (forall a b. c a b => p a b) -> p s t

It would be more correct to speak about constrained optics:

type OpticC p c s t = (forall a b. c a b => p a b) -> p s t

then constrainted traversal would be

type TraversalC c s t = forall p. Traversing p => OpticC p c s t

and similarly constrained lens, prisms, setters, ...


Can/should we generalise optics to support this?

Can: probably yes. Should: I'd rather see someone else try this first.