module Data.Bifunctor.Product
( Product(..)
) where
import Control.Applicative
import Data.Biapplicative
import Data.Functor.Apply
import Data.Bifoldable
import Data.Bitraversable
import Data.Monoid hiding (Product, (<>))
import Data.Semigroup hiding (Product)
import Data.Semigroup.Bifoldable
import Data.Semigroup.Bitraversable
data Product f g a b = Pair (f a b) (g a b) deriving (Eq,Ord,Show,Read)
instance (Bifunctor f, Bifunctor g) => Bifunctor (Product f g) where
first f (Pair x y) = Pair (first f x) (first f y)
second g (Pair x y) = Pair (second g x) (second g y)
bimap f g (Pair x y) = Pair (bimap f g x) (bimap f g y)
instance (Biapplicative f, Biapplicative g) => Biapplicative (Product f g) where
bipure a b = Pair (bipure a b) (bipure a b)
Pair w x <<*>> Pair y z = Pair (w <<*>> y) (x <<*>> z)
instance (Bifoldable f, Bifoldable g) => Bifoldable (Product f g) where
bifoldMap f g (Pair x y) = bifoldMap f g x `mappend` bifoldMap f g y
instance (Bitraversable f, Bitraversable g) => Bitraversable (Product f g) where
bitraverse f g (Pair x y) = Pair <$> bitraverse f g x <*> bitraverse f g y
instance (Bifoldable1 f, Bifoldable1 g) => Bifoldable1 (Product f g) where
bifoldMap1 f g (Pair x y) = bifoldMap1 f g x <> bifoldMap1 f g y
instance (Bitraversable1 f, Bitraversable1 g) => Bitraversable1 (Product f g) where
bitraverse1 f g (Pair x y) = Pair <$> bitraverse1 f g x <.> bitraverse1 f g y