bifunctors-5: Bifunctors

Copyright(C) 2011 Edward Kmett
LicenseBSD-style (see the file LICENSE)
MaintainerEdward Kmett <ekmett@gmail.com>
Stabilityprovisional
Portabilityportable
Safe HaskellSafe
LanguageHaskell98

Data.Bitraversable

Description

 

Synopsis

Documentation

class (Bifunctor t, Bifoldable t) => Bitraversable t where Source

Minimal complete definition either bitraverse or bisequenceA.

Bitraversable identifies bifunctorial data structures whose elements can be traversed in order, performing Applicative or Monad actions at each element, and collecting a result structure with the same shape.

A definition of traverse must satisfy the following laws:

naturality
bitraverse (t . f) (t . g) ≡ t . bitraverse f g for every applicative transformation t
identity
bitraverse Identity IdentityIdentity
composition
Compose . fmap (bitraverse g1 g2) . bitraverse f1 f2 ≡ traverse (Compose . fmap g1 . f1) (Compose . fmap g2 . f2)

A definition of bisequenceA must satisfy the following laws:

naturality
bisequenceA . bimap t t ≡ t . bisequenceA for every applicative transformation t
identity
bisequenceA . bimap Identity IdentityIdentity
composition
bisequenceA . bimap Compose ComposeCompose . fmap bisequenceA . bisequenceA

where an applicative transformation is a function

t :: (Applicative f, Applicative g) => f a -> g a

preserving the Applicative operations:

t (pure x) = pure x
t (f <*> x) = t f <*> t x

and the identity functor Identity and composition functors Compose are defined as

newtype Identity a = Identity { runIdentity :: a }

instance Functor Identity where
  fmap f (Identity x) = Identity (f x)

instance Applicative Identity where
  pure = Identity
  Identity f <*> Identity x = Identity (f x)

newtype Compose f g a = Compose (f (g a))

instance (Functor f, Functor g) => Functor (Compose f g) where
  fmap f (Compose x) = Compose (fmap (fmap f) x)

instance (Applicative f, Applicative g) => Applicative (Compose f g) where
  pure = Compose . pure . pure
  Compose f <*> Compose x = Compose ((<*>) <$> f <*> x)

Some simple examples are Either and '(,)':

instance Bitraversable Either where
  bitraverse f _ (Left x) = Left <$> f x
  bitraverse _ g (Right y) = Right <$> g y

instance Bitraversable (,) where
  bitraverse f g (x, y) = (,) <$> f x <*> g y

Bitraversable relates to its superclasses in the following ways:

bimap f g ≡ runIdentity . bitraverse (Identity . f) (Identity . g)
bifoldMap f g = getConst . bitraverse (Const . f) (Const . g)

These are available as bimapDefault and bifoldMapDefault respectively.

Minimal complete definition

bitraverse | bisequenceA

Methods

bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> t a b -> f (t c d) Source

Evaluates the relevant functions at each element in the structure, running the action, and builds a new structure with the same shape, using the elements produced from sequencing the actions.

bitraverse f g ≡ bisequenceA . bimap f g

bisequenceA :: Applicative f => t (f a) (f b) -> f (t a b) Source

Sequences all the actions in a structure, building a new structure with the same shape using the results of the actions.

bisequenceAbitraverse id id

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> t a b -> m (t c d) Source

As bitraverse, but uses evidence that m is a Monad rather than an Applicative.

bimapM f g ≡ bisequence . bimap f g
bimapM f g ≡ unwrapMonad . bitraverse (WrapMonad . f) (WrapMonad . g)

bisequence :: Monad m => t (m a) (m b) -> m (t a b) Source

As bisequenceA, but uses evidence that m is a Monad rather than an Applicative.

bisequencebimapM id id
bisequenceunwrapMonad . bisequenceA . bimap WrapMonad WrapMonad

Instances

Bitraversable Either Source 

Methods

bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> Either a b -> f (Either c d) Source

bisequenceA :: Applicative f => Either (f a) (f b) -> f (Either a b) Source

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> Either a b -> m (Either c d) Source

bisequence :: Monad m => Either (m a) (m b) -> m (Either a b) Source

Bitraversable (,) Source 

Methods

bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> (a, b) -> f (c, d) Source

bisequenceA :: Applicative f => (f a, f b) -> f (a, b) Source

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> (a, b) -> m (c, d) Source

bisequence :: Monad m => (m a, m b) -> m (a, b) Source

Bitraversable Const Source 

Methods

bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> Const a b -> f (Const c d) Source

bisequenceA :: Applicative f => Const (f a) (f b) -> f (Const a b) Source

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> Const a b -> m (Const c d) Source

bisequence :: Monad m => Const (m a) (m b) -> m (Const a b) Source

Bitraversable Arg Source 

Methods

bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> Arg a b -> f (Arg c d) Source

bisequenceA :: Applicative f => Arg (f a) (f b) -> f (Arg a b) Source

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> Arg a b -> m (Arg c d) Source

bisequence :: Monad m => Arg (m a) (m b) -> m (Arg a b) Source

Bitraversable ((,,) x) Source 

Methods

bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> (x, a, b) -> f (x, c, d) Source

bisequenceA :: Applicative f => (x, f a, f b) -> f (x, a, b) Source

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> (x, a, b) -> m (x, c, d) Source

bisequence :: Monad m => (x, m a, m b) -> m (x, a, b) Source

Bitraversable (Tagged *) Source 

Methods

bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> Tagged * a b -> f (Tagged * c d) Source

bisequenceA :: Applicative f => Tagged * (f a) (f b) -> f (Tagged * a b) Source

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> Tagged * a b -> m (Tagged * c d) Source

bisequence :: Monad m => Tagged * (m a) (m b) -> m (Tagged * a b) Source

Traversable f => Bitraversable (Clown f) Source 

Methods

bitraverse :: Applicative b => (a -> b c) -> (d -> b e) -> Clown f a d -> b (Clown f c e) Source

bisequenceA :: Applicative a => Clown f (a b) (a c) -> a (Clown f b c) Source

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> Clown f a b -> m (Clown f c d) Source

bisequence :: Monad m => Clown f (m a) (m b) -> m (Clown f a b) Source

Bitraversable p => Bitraversable (Flip p) Source 

Methods

bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> Flip p a b -> f (Flip p c d) Source

bisequenceA :: Applicative f => Flip p (f a) (f b) -> f (Flip p a b) Source

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> Flip p a b -> m (Flip p c d) Source

bisequence :: Monad m => Flip p (m a) (m b) -> m (Flip p a b) Source

Traversable g => Bitraversable (Joker g) Source 

Methods

bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> Joker g a b -> f (Joker g c d) Source

bisequenceA :: Applicative f => Joker g (f a) (f b) -> f (Joker g a b) Source

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> Joker g a b -> m (Joker g c d) Source

bisequence :: Monad m => Joker g (m a) (m b) -> m (Joker g a b) Source

Bitraversable p => Bitraversable (WrappedBifunctor p) Source 

Methods

bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> WrappedBifunctor p a b -> f (WrappedBifunctor p c d) Source

bisequenceA :: Applicative f => WrappedBifunctor p (f a) (f b) -> f (WrappedBifunctor p a b) Source

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> WrappedBifunctor p a b -> m (WrappedBifunctor p c d) Source

bisequence :: Monad m => WrappedBifunctor p (m a) (m b) -> m (WrappedBifunctor p a b) Source

Bitraversable ((,,,) x y) Source 

Methods

bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> (x, y, a, b) -> f (x, y, c, d) Source

bisequenceA :: Applicative f => (x, y, f a, f b) -> f (x, y, a, b) Source

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> (x, y, a, b) -> m (x, y, c, d) Source

bisequence :: Monad m => (x, y, m a, m b) -> m (x, y, a, b) Source

(Bitraversable f, Bitraversable g) => Bitraversable (Product f g) Source 

Methods

bitraverse :: Applicative b => (a -> b c) -> (d -> b e) -> Product f g a d -> b (Product f g c e) Source

bisequenceA :: Applicative a => Product f g (a b) (a c) -> a (Product f g b c) Source

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> Product f g a b -> m (Product f g c d) Source

bisequence :: Monad m => Product f g (m a) (m b) -> m (Product f g a b) Source

(Traversable f, Bitraversable p) => Bitraversable (Tannen f p) Source 

Methods

bitraverse :: Applicative b => (a -> b c) -> (d -> b e) -> Tannen f p a d -> b (Tannen f p c e) Source

bisequenceA :: Applicative a => Tannen f p (a b) (a c) -> a (Tannen f p b c) Source

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> Tannen f p a b -> m (Tannen f p c d) Source

bisequence :: Monad m => Tannen f p (m a) (m b) -> m (Tannen f p a b) Source

Bitraversable ((,,,,) x y z) Source 

Methods

bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> (x, y, z, a, b) -> f (x, y, z, c, d) Source

bisequenceA :: Applicative f => (x, y, z, f a, f b) -> f (x, y, z, a, b) Source

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> (x, y, z, a, b) -> m (x, y, z, c, d) Source

bisequence :: Monad m => (x, y, z, m a, m b) -> m (x, y, z, a, b) Source

(Bitraversable p, Traversable f, Traversable g) => Bitraversable (Biff p f g) Source 

Methods

bitraverse :: Applicative b => (a -> b c) -> (d -> b e) -> Biff p f g a d -> b (Biff p f g c e) Source

bisequenceA :: Applicative a => Biff p f g (a b) (a c) -> a (Biff p f g b c) Source

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> Biff p f g a b -> m (Biff p f g c d) Source

bisequence :: Monad m => Biff p f g (m a) (m b) -> m (Biff p f g a b) Source

Bitraversable ((,,,,,) x y z w) Source 

Methods

bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> (x, y, z, w, a, b) -> f (x, y, z, w, c, d) Source

bisequenceA :: Applicative f => (x, y, z, w, f a, f b) -> f (x, y, z, w, a, b) Source

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> (x, y, z, w, a, b) -> m (x, y, z, w, c, d) Source

bisequence :: Monad m => (x, y, z, w, m a, m b) -> m (x, y, z, w, a, b) Source

Bitraversable ((,,,,,,) x y z w v) Source 

Methods

bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> (x, y, z, w, v, a, b) -> f (x, y, z, w, v, c, d) Source

bisequenceA :: Applicative f => (x, y, z, w, v, f a, f b) -> f (x, y, z, w, v, a, b) Source

bimapM :: Monad m => (a -> m c) -> (b -> m d) -> (x, y, z, w, v, a, b) -> m (x, y, z, w, v, c, d) Source

bisequence :: Monad m => (x, y, z, w, v, m a, m b) -> m (x, y, z, w, v, a, b) Source

bifor :: (Bitraversable t, Applicative f) => t a b -> (a -> f c) -> (b -> f d) -> f (t c d) Source

bifor is bitraverse with the structure as the first argument.

biforM :: (Bitraversable t, Monad m) => t a b -> (a -> m c) -> (b -> m d) -> m (t c d) Source

biforM is bimapM with the structure as the first argument.

bimapAccumL :: Bitraversable t => (a -> b -> (a, c)) -> (a -> d -> (a, e)) -> a -> t b d -> (a, t c e) Source

Traverses a structure from left to right, threading a state of type a and using the given actions to compute new elements for the structure.

bimapAccumR :: Bitraversable t => (a -> b -> (a, c)) -> (a -> d -> (a, e)) -> a -> t b d -> (a, t c e) Source

Traverses a structure from right to left, threading a state of type a and using the given actions to compute new elements for the structure.

bimapDefault :: Bitraversable t => (a -> b) -> (c -> d) -> t a c -> t b d Source

A default definition of bimap in terms of the Bitraversable operations.

bifoldMapDefault :: (Bitraversable t, Monoid m) => (a -> m) -> (b -> m) -> t a b -> m Source

A default definition of bifoldMap in terms of the Bitraversable operations.