Traverses only over the first argument.
firstA f ≡ bitraverse f pure:: a typeCtrl KGHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05
Modulebase-compat-batteries-0.14.1Haskell2010
Traverses only over the first argument.
firstA f ≡ bitraverse f pureTraverses only over the second argument.
secondA f ≡ bitraverse pure fBasic usage:
secondA (find odd) (Left [])Just (Left [])
secondA (find odd) (Left [1, 2, 3])Just (Left [1,2,3])
secondA (find odd) (Right [4, 5])Just (Right 5)
secondA (find odd) ([1, 2, 3], [4, 5])Just ([1,2,3],5)
secondA (find odd) ([1,2,3], [4])Nothing
A default definition of bifoldMap in terms of the Bitraversable operations.
bifoldMapDefault f g ≡
getConst . bitraverse (Const . f) (Const . g)bifor is bitraverse with the structure as the first argument. For a version that ignores the results, see bifor_.
Basic usage:
bifor (Left []) listToMaybe (find even)Nothing
bifor (Left [1, 2, 3]) listToMaybe (find even)Just (Left 1)
bifor (Right [4, 5]) listToMaybe (find even)Just (Right 4)
bifor ([1, 2, 3], [4, 5]) listToMaybe (find even)Just (1,4)
bifor ([], [4, 5]) listToMaybe (find even)Nothing
Alias for bifor.
The bimapAccumL function behaves like a combination of bimap and
bifoldl; it 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.
Basic usage:
bimapAccumL (\acc bool -> (acc + 1, show bool)) (\acc string -> (acc * 2, reverse string)) 3 (True, "foo")(8,("True","oof"))
The bimapAccumR function behaves like a combination of bimap and
bifoldr; it 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.
Basic usage:
bimapAccumR (\acc bool -> (acc + 1, show bool)) (\acc string -> (acc * 2, reverse string)) 3 (True, "foo")(7,("True","oof"))
A default definition of bimap in terms of the Bitraversable operations.
bimapDefault f g ≡
runIdentity . bitraverse (Identity . f) (Identity . g)Alias for bitraverse.
Sequences all the actions in a structure, building a new structure with the same shape using the results of the actions. For a version that ignores the results, see bisequence_.
bisequence ≡ bitraverse id idBasic usage:
bisequence (Just 4, Nothing)Nothing
bisequence (Just 4, Just 5)Just (4,5)
bisequence ([1, 2, 3], [4, 5])[(1,4),(1,5),(2,4),(2,5),(3,4),(3,5)]
Alias for bisequence.
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.
As opposed to Traversable data structures, which have one variety of element on which an action can be performed, Bitraversable data structures have two such varieties of elements.
A definition of bitraverse must satisfy the following laws:
bitraverse (t . f) (t . g) ≡ t . bitraverse f g
for every applicative transformation
t
Compose .
fmap (bitraverse g1 g2) .
bitraverse f1 f2
≡ bitraverse (Compose . fmap g1 . f1)
(Compose . fmap g2 . f2)
where an applicative transformation is a function
t :: (Applicative f, Applicative g) => f a -> g apreserving 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 from Data.Functor.Identity and Data.Functor.Compose.
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 yBitraversable 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.
If the type is also an instance of Traversable, then it must satisfy (up to laziness):
traverse ≡ bitraverse pure
bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> t a b -> f (t c d)Evaluates the relevant functions at each element in the structure, running the action, and builds a new structure with the same shape, using the results produced from sequencing the actions.
bitraverse f g ≡ bisequenceA . bimap f gFor a version that ignores the results, see bitraverse_.
Basic usage:
bitraverse listToMaybe (find odd) (Left [])Nothing
bitraverse listToMaybe (find odd) (Left [1, 2, 3])Just (Left 1)
bitraverse listToMaybe (find odd) (Right [4, 5])Just (Right 5)
bitraverse listToMaybe (find odd) ([1, 2, 3], [4, 5])Just (1,5)
bitraverse listToMaybe (find odd) ([], [4, 5])Nothing
Bitraversable ArgDefined in base-4.20.2.0 · Data.SemigroupBitraversable EitherDefined in base-4.20.2.0 · Data.BitraversableBitraversable Tuple2Defined in base-4.20.2.0 · Data.BitraversableClass laws for tuples hold only up to laziness. The
Bitraversable methods are lazier than their Traversable counterparts.
For example the law bitraverse pure ≡ traverse does
not hold for tuples if laziness is exploited:
(bitraverse pure pure undefined :: IO (Int, Word)) `seq` ()()(traverse pure undefined :: IO (Int, Word)) `seq` ()*** Exception: Prelude.undefined
Bitraversable ConstDefined in base-4.20.2.0 · Data.BitraversableBitraversable ConstantDefined in transformers-0.6.1.1 · Data.Functor.ConstantBitraversable (Tuple3 x)Defined in base-4.20.2.0 · Data.BitraversableBitraversable (K1 i)Defined in base-4.20.2.0 · Data.BitraversableBitraversable (Tuple4 x y)Defined in base-4.20.2.0 · Data.BitraversableBitraversable (Tuple5 x y z)Defined in base-4.20.2.0 · Data.BitraversableBitraversable (Tuple6 x y z w)Defined in base-4.20.2.0 · Data.BitraversableBitraversable (Tuple7 x y z w v)Defined in base-4.20.2.0 · Data.BitraversableTraverses only over the first argument.
firstA f ≡ bitraverse f pureTraverses only over the second argument.
secondA f ≡ bitraverse pure fBasic usage:
secondA (find odd) (Left [])Just (Left [])
secondA (find odd) (Left [1, 2, 3])Just (Left [1,2,3])
secondA (find odd) (Right [4, 5])Just (Right 5)
secondA (find odd) ([1, 2, 3], [4, 5])Just ([1,2,3],5)
secondA (find odd) ([1,2,3], [4])Nothing