ekmett / profunctors

Haskell 98 Profunctors
http://hackage.haskell.org/package/profunctors
Other
70 stars 43 forks source link

Contravariant instances for Forget and Star #67

Closed Solonarv closed 5 years ago

Solonarv commented 5 years ago

We can write the following Contravariant instances:

instance Contravariant (Forget r a) where
  contramap _ = coerce
  -- or: contramap _ (Forget ra) = Forget ra

and:

instance Contravariant f => Contravariant (Star f a) where
  contramap f (Star g) = Star (contramap f . g)

profunctors already depends on contravariant, so this does not change the dependency footprint at all.

These are useful when implementing profunctor optics (example using the new QuantifiedConstraints extension):

type Getter s t a b = forall p. (Profunctor p, forall x. Contravariant (p x)) => p a b -> p s t

to :: (s -> a) -> Getter s t a b
to get = (() >$) . dimap get (const ())

type Getting r s t a b = Forget r a b -> Forget r s t

view :: Getting a s t a b -> s -> a
view l = runForget (l (Forget id))

I will create a PR adding these instances tomorrow, unless they are intentionally left out.

RyanGlScott commented 5 years ago

Fixed in #68.