contravariant icon indicating copy to clipboard operation
contravariant copied to clipboard

Add `Contraversable`

Open masaeedu opened this issue 4 years ago • 3 comments
trafficstars

Hello there. I find it very nice to have an analog for Applicative for Contravariant functors. But it feels like something is missing; where are our nice Traversables?

It seems like there is room for a typeclass like this:

class Functor t => Contraversable t
  where
  {-# MINIMAL contraverse | consequence #-}
  contraverse :: Decidable f => (a -> f b) -> t a -> f (t b)
  consequence :: Decidable f => t (f a) -> f (t b)

A simple application is to note that finite products (e.g. Vec n) are Contraversable. Thus the consequence of a container-ful of Predicates conditions :: Vec n (Predicate a) is consequence conditions :: Predicate (Vec n a), a predicate that gives the conjunction of the result of each predicate in conditions. Similarly, a finite productful of flipped folds can be consequenced to a flipped fold over the product. etc. etc.

Would you be open to a PR that adds this typeclass?

masaeedu avatar Oct 20 '21 22:10 masaeedu

Should it be rather

contraverse :: Divisible f => (a -> f b) -> t a -> f (t b)

etc?

sofia-m-a avatar Mar 29 '22 18:03 sofia-m-a

Is this collect from distributive?

endgame avatar Jun 30 '22 05:06 endgame