{-# LANGUAGE Trustworthy #-}{-# LANGUAGE ScopedTypeVariables #-}------------------------------------------------------------------------------- |-- Module : Data.Bitraversable-- Copyright : (C) 2011-2016 Edward Kmett-- License : BSD-style (see the file LICENSE)---- Maintainer : libraries@haskell.org-- Stability : provisional-- Portability : portable---- @since 4.10.0.0----------------------------------------------------------------------------module Data.Bitraversable ( Bitraversable(..) , bisequenceA , bisequence , bimapM , firstA , secondA , bifor , biforM , bimapAccumL , bimapAccumR , bimapDefault , bifoldMapDefault ) whereimport Control.Applicativeimport Data.Bifunctorimport Data.Bifoldableimport Data.Coerceimport Data.Functor.Identity(Identity(..))import Data.Foldable.Internal(StateL(..), runStateL, StateR(..), runStateR)import Data.Monoid.Internal(Arg(..))class (Bifunctor t, Bifoldable t) => Bitraversable t where bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> t a b -> f (t c d)bisequenceA :: (Bitraversable t, Applicative f) => t (f a) (f b) -> f (t a b)bisequenceA = bisequencebimapM :: (Bitraversable t, Applicative f) => (a -> f c) -> (b -> f d) -> t a b -> f (t c d)bimapM = bitraversebisequence :: (Bitraversable t, Applicative f) => t (f a) (f b) -> f (t a b)bisequence = bitraverse id idfirstA :: Bitraversable t => Applicative f => (a -> f c) -> t a b -> f (t c b)firstA f = bitraverse f puresecondA :: Bitraversable t => Applicative f => (b -> f c) -> t a b -> f (t a c)secondA f = bitraverse pure f{-instance Bitraversable (,) where bitraverse f g ~(a, b) = liftA2 (,) (f a) (g b)instance Bitraversable ((,,) x) where bitraverse f g ~(x, a, b) = liftA2 ((,,) x) (f a) (g b)instance Bitraversable ((,,,) x y) where bitraverse f g ~(x, y, a, b) = liftA2 ((,,,) x y) (f a) (g b)instance Bitraversable ((,,,,) x y z) where bitraverse f g ~(x, y, z, a, b) = liftA2 ((,,,,) x y z) (f a) (g b)instance Bitraversable ((,,,,,) x y z w) where bitraverse f g ~(x, y, z, w, a, b) = liftA2 ((,,,,,) x y z w) (f a) (g b)instance Bitraversable ((,,,,,,) x y z w v) where bitraverse f g ~(x, y, z, w, v, a, b) = liftA2 ((,,,,,,) x y z w v) (f a) (g b)-}instance Bitraversable Either where bitraverse f _ (Left a) = Left <$> f a bitraverse _ g (Right b) = Right <$> g binstance Bitraversable Const where bitraverse f _ (Const a) = Const <$> f ainstance Bitraversable Arg where bitraverse f g (Arg a b) = liftA2 Arg (f a) (g b)bifor :: (Bitraversable t, Applicative f) => t a b -> (a -> f c) -> (b -> f d) -> f (t c d)bifor t f g = bitraverse f g tbiforM :: (Bitraversable t, Applicative f) => t a b -> (a -> f c) -> (b -> f d) -> f (t c d)biforM = biforbimapAccumL :: Bitraversable t => (a -> b -> (a, c)) -> (a -> d -> (a, e)) -> a -> t b d -> (a, t c e)bimapAccumL f g s t = runStateL (bitraverse (StateL . flip f) (StateL . flip g) t) sbimapAccumR :: Bitraversable t => (a -> b -> (a, c)) -> (a -> d -> (a, e)) -> a -> t b d -> (a, t c e)bimapAccumR f g s t = runStateR (bitraverse (StateR . flip f) (StateR . flip g) t) sbimapDefault :: forall t a b c d . Bitraversable t => (a -> b) -> (c -> d) -> t a c -> t b dbimapDefault = coerce (bitraverse :: (a -> Identity b) -> (c -> Identity d) -> t a c -> Identity (t b d))bifoldMapDefault :: forall t m a b . (Bitraversable t, Monoid m) => (a -> m) -> (b -> m) -> t a b -> mbifoldMapDefault = coerce (bitraverse :: (a -> Const m ()) -> (b -> Const m ()) -> t a b -> Const m (t () ()))