Proposal: merge Data.Functor.Coproduct into transformers

Sjoerd Visscher sjoerd at
Sun Dec 16 13:43:35 CET 2012

If you're going to update transformers, could you perhaps export fstP and sndP from Product? I keep having to redefine them. And perhaps also a function dual to coproduct of type (a -> f b) -> (a -> g b) -> a -> Product f g b.

Here's an example where they are very useful:

{-# LANGUAGE MultiParamTypeClasses, UndecidableInstances #-}
import Data.Functor.Product
import Data.Functor.Coproduct
import Data.Functor.Adjunction

fstP :: Product f g a -> f a
fstP (Pair x _) = x

sndP :: Product f g a -> g a
sndP (Pair _ x) = x

productP :: (a -> f b) -> (a -> g b) -> a -> Product f g b
productP f g a = Pair (f a) (g a)

instance (Adjunction f g, Adjunction f' g') => Adjunction (Coproduct f f') (Product g g') where
  unit   = productP  (leftAdjunct  left) (leftAdjunct right)
  counit = coproduct (rightAdjunct fstP) (rightAdjunct sndP)

  leftAdjunct  f = productP  (leftAdjunct  (f . left)) (leftAdjunct  (f . right))
  rightAdjunct f = coproduct (rightAdjunct (fstP . f)) (rightAdjunct (sndP . f))


More information about the Libraries mailing list