HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulererebase-1.21.2Haskell2010

Data.Bifoldable

  • 1 class
  • 32 values
  • Packagererebase-1.21.2
  • Exports33
  • LanguageHaskell2010
  • LicenceMIT
  • SourceBifoldable.hs
classclass Bifoldable (p :: Type -> Type -> Type) where
#

Bifoldable identifies foldable structures with two different varieties of elements (as opposed to Foldable, which has one variety of element). Common examples are Either and (,):

instance Bifoldable Either where
  bifoldMap f _ (Left  a) = f a
  bifoldMap _ g (Right b) = g b

instance Bifoldable (,) where
  bifoldr f g z (a, b) = f a (g b z)

Some examples below also use the following BiList to showcase empty Bifoldable behaviors when relevant (Either and (,) containing always exactly resp. 1 and 2 elements):

data BiList a b = BiList [a] [b]

instance Bifoldable BiList where
  bifoldr f g z (BiList as bs) = foldr f (foldr g z bs) as

A minimal Bifoldable definition consists of either bifoldMap or bifoldr. When defining more than this minimal set, one should ensure that the following identities hold:

bifold ≡ bifoldMap id id
bifoldMap f g ≡ bifoldr (mappend . f) (mappend . g) mempty
bifoldr f g z t ≡ appEndo (bifoldMap (Endo . f) (Endo . g) t) z

If the type is also an instance of Foldable, then it must satisfy (up to laziness):

bifoldl const ≡ foldl
bifoldr (flip const) ≡ foldr
bifoldMap (const mempty) ≡ foldMap

If the type is also a Bifunctor instance, it should satisfy:

bifoldMap f g ≡ bifold . bimap f g

which implies that

bifoldMap f g . bimap h i ≡ bifoldMap (f . h) (g . i)

Methods

  • bifold :: Monoid m => p m m -> m

    Combines the elements of a structure using a monoid.

    bifold ≡ bifoldMap id id
    Examples

    Basic usage:

    Example1 expression
    bifold (Right [1, 2, 3])[1,2,3]
    Example1 expression
    bifold (Left [5, 6])[5,6]
    Example1 expression
    bifold ([1, 2, 3], [4, 5])[1,2,3,4,5]
    Example1 expression
    bifold (Product 6, Product 7)Product {getProduct = 42}
    Example1 expression
    bifold (Sum 6, Sum 7)Sum {getSum = 13}
  • bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> p a b -> m

    Combines the elements of a structure, given ways of mapping them to a common monoid.

    bifoldMap f g ≡ bifoldr (mappend . f) (mappend . g) mempty
    Examples

    Basic usage:

    Example1 expression
    bifoldMap (take 3) (fmap digitToInt) ([1..], "89")[1,2,3,8,9]
    Example1 expression
    bifoldMap (take 3) (fmap digitToInt) (Left [1..])[1,2,3]
    Example1 expression
    bifoldMap (take 3) (fmap digitToInt) (Right "89")[8,9]
  • bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> p a b -> c

    Combines the elements of a structure in a right associative manner. Given a hypothetical function toEitherList :: p a b -> [Either a b] yielding a list of all elements of a structure in order, the following would hold:

    bifoldr f g z ≡ foldr (either f g) z . toEitherList
    Examples

    Basic usage:

    > bifoldr (+) (*) 3 (5, 7)
    26 -- 5 + (7 * 3)
    
    > bifoldr (+) (*) 3 (7, 5)
    22 -- 7 + (5 * 3)
    
    > bifoldr (+) (*) 3 (Right 5)
    15 -- 5 * 3
    
    > bifoldr (+) (*) 3 (Left 5)
    8 -- 5 + 3
    
  • bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> p a b -> c

    Combines the elements of a structure in a left associative manner. Given a hypothetical function toEitherList :: p a b -> [Either a b] yielding a list of all elements of a structure in order, the following would hold:

    bifoldl f g z
         ≡ foldl (acc -> either (f acc) (g acc)) z . toEitherList

    Note that if you want an efficient left-fold, you probably want to use bifoldl' instead of bifoldl. The reason is that the latter does not force the "inner" results, resulting in a thunk chain which then must be evaluated from the outside-in.

    Examples

    Basic usage:

    > bifoldl (+) (*) 3 (5, 7)
    56 -- (5 + 3) * 7
    
    > bifoldl (+) (*) 3 (7, 5)
    50 -- (7 + 3) * 5
    
    > bifoldl (+) (*) 3 (Right 5)
    15 -- 5 * 3
    
    > bifoldl (+) (*) 3 (Left 5)
    8 -- 5 + 3
    
Instances27Bifoldable, …
valuebiList :: Bifoldable t => t a a -> [a]
#

Collects the list of elements of a structure, from left to right.

Examples

Basic usage:

Example1 expression
biList (18, 42)[18,42]
Example1 expression
biList (Left 18)[18]
valuebiall :: Bifoldable t => (a -> Bool) -> (b -> Bool) -> t a b -> Bool
#

Determines whether all elements of the structure satisfy their appropriate predicate argument. Empty structures yield True.

Examples

Basic usage:

Example1 expression
biall even isDigit (27, 't')False
Example1 expression
biall even isDigit (26, '8')True
Example1 expression
biall even isDigit (Left 27)False
Example1 expression
biall even isDigit (Left 26)True
Example1 expression
biall even isDigit (BiList [26, 52] ['3', '8'])True

Empty structures yield True:

Example1 expression
biall even isDigit (BiList [] [])True
valuebiand :: Bifoldable t => t Bool Bool -> Bool
#

biand returns the conjunction of a container of Bools. For the result to be True, the container must be finite; False, however, results from a False value finitely far from the left end.

Examples

Basic usage:

Example1 expression
biand (True, False)False
Example1 expression
biand (True, True)True
Example1 expression
biand (Left True)True

Empty structures yield True:

Example1 expression
biand (BiList [] [])True

A False value finitely far from the left end yields False (short circuit):

Example1 expression
biand (BiList [True, True, False, True] (repeat True))False

A False value infinitely far from the left end hangs:

> biand (BiList (repeat True) [False])
* Hangs forever *

An infinitely True value hangs:

> biand (BiList (repeat True) [])
* Hangs forever *
valuebiany :: Bifoldable t => (a -> Bool) -> (b -> Bool) -> t a b -> Bool
#

Determines whether any element of the structure satisfies its appropriate predicate argument. Empty structures yield False.

Examples

Basic usage:

Example1 expression
biany even isDigit (27, 't')False
Example1 expression
biany even isDigit (27, '8')True
Example1 expression
biany even isDigit (26, 't')True
Example1 expression
biany even isDigit (Left 27)False
Example1 expression
biany even isDigit (Left 26)True
Example1 expression
biany even isDigit (BiList [27, 53] ['t', '8'])True

Empty structures yield False:

Example1 expression
biany even isDigit (BiList [] [])False
valuebiasum :: (Bifoldable t, Alternative f) => t (f a) (f a) -> f a
#

The sum of a collection of actions, generalizing biconcat.

Examples

Basic usage:

Example1 expression
biasum (Nothing, Nothing)Nothing
Example1 expression
biasum (Nothing, Just 42)Just 42
Example1 expression
biasum (Just 18, Nothing)Just 18
Example1 expression
biasum (Just 18, Just 42)Just 18
valuebiconcat :: Bifoldable t => t [a] [a] -> [a]
#

Reduces a structure of lists to the concatenation of those lists.

Examples

Basic usage:

Example1 expression
biconcat ([1, 2, 3], [4, 5])[1,2,3,4,5]
Example1 expression
biconcat (Left [1, 2, 3])[1,2,3]
Example1 expression
biconcat (BiList [[1, 2, 3, 4, 5], [6, 7, 8]] [[9]])[1,2,3,4,5,6,7,8,9]
valuebiconcatMap :: Bifoldable t => (a -> [c]) -> (b -> [c]) -> t a b -> [c]
#

Given a means of mapping the elements of a structure to lists, computes the concatenation of all such lists in order.

Examples

Basic usage:

Example1 expression
biconcatMap (take 3) (fmap digitToInt) ([1..], "89")[1,2,3,8,9]
Example1 expression
biconcatMap (take 3) (fmap digitToInt) (Left [1..])[1,2,3]
Example1 expression
biconcatMap (take 3) (fmap digitToInt) (Right "89")[8,9]
valuebielem :: (Bifoldable t, Eq a) => a -> t a a -> Bool
#

Does the element occur in the structure?

Examples

Basic usage:

Example1 expression
bielem 42 (17, 42)True
Example1 expression
bielem 42 (17, 43)False
Example1 expression
bielem 42 (Left 42)True
Example1 expression
bielem 42 (Right 13)False
Example1 expression
bielem 42 (BiList [1..5] [1..100])True
Example1 expression
bielem 42 (BiList [1..5] [1..41])False
valuebifind :: Bifoldable t => (a -> Bool) -> t a a -> Maybe a
#

The bifind function takes a predicate and a structure and returns the leftmost element of the structure matching the predicate, or Nothing if there is no such element.

Examples

Basic usage:

Example1 expression
bifind even (27, 53)Nothing
Example1 expression
bifind even (27, 52)Just 52
Example1 expression
bifind even (26, 52)Just 26

Empty structures always yield Nothing:

Example1 expression
bifind even (BiList [] [])Nothing
valuebifoldl'
  1. :: Bifoldable t
  2. => a -> b -> a
  3. -> a -> c -> a
  4. -> a
  5. -> t b c
  6. -> a
#

As bifoldl, but strict in the result of the reduction functions at each step.

This ensures that each step of the bifold is forced to weak head normal form before being applied, avoiding the collection of thunks that would otherwise occur. This is often what you want to strictly reduce a finite structure to a single, monolithic result (e.g., bilength).

valuebifoldl1 :: Bifoldable t => (a -> a -> a) -> t a a -> a
#

A variant of bifoldl that has no base case, and thus may only be applied to non-empty structures.

Examples

Basic usage:

Example1 expression
bifoldl1 (+) (5, 7)12
Example1 expression
bifoldl1 (+) (Right 7)7
Example1 expression
bifoldl1 (+) (Left 5)5
> bifoldl1 (+) (BiList [1, 2] [3, 4])
10 -- ((1 + 2) + 3) + 4
Example1 expression
bifoldl1 (+) (BiList [1, 2] [])3

On empty structures, this function throws an exception:

Example1 expression
bifoldl1 (+) (BiList [] [])*** Exception: bifoldl1: empty structure...
valuebifoldlM
  1. :: (Bifoldable t, Monad m)
  2. => a -> b -> m a
  3. -> a -> c -> m a
  4. -> a
  5. -> t b c
  6. -> m a
#

Left associative monadic bifold over a structure.

Examples

Basic usage:

Example1 expression
bifoldlM (\a b -> print b >> pure a) (\a c -> print (show c) >> pure a) 42 ("Hello", True)"Hello""True"42
Example1 expression
bifoldlM (\a b -> print b >> pure a) (\a c -> print (show c) >> pure a) 42 (Right True)"True"42
Example1 expression
bifoldlM (\a b -> print b >> pure a) (\a c -> print (show c) >> pure a) 42 (Left "Hello")"Hello"42
valuebifoldr'
  1. :: Bifoldable t
  2. => a -> c -> c
  3. -> b -> c -> c
  4. -> c
  5. -> t a b
  6. -> c
#

As bifoldr, but strict in the result of the reduction functions at each step.

valuebifoldr1 :: Bifoldable t => (a -> a -> a) -> t a a -> a
#

A variant of bifoldr that has no base case, and thus may only be applied to non-empty structures.

Examples

Basic usage:

Example1 expression
bifoldr1 (+) (5, 7)12
Example1 expression
bifoldr1 (+) (Right 7)7
Example1 expression
bifoldr1 (+) (Left 5)5
> bifoldr1 (+) (BiList [1, 2] [3, 4])
10 -- 1 + (2 + (3 + 4))
Example1 expression
bifoldr1 (+) (BiList [1, 2] [])3

On empty structures, this function throws an exception:

Example1 expression
bifoldr1 (+) (BiList [] [])*** Exception: bifoldr1: empty structure...
valuebifoldrM
  1. :: (Bifoldable t, Monad m)
  2. => a -> c -> m c
  3. -> b -> c -> m c
  4. -> c
  5. -> t a b
  6. -> m c
#

Right associative monadic bifold over a structure.

valuebifor_
  1. :: (Bifoldable t, Applicative f)
  2. => t a b
  3. -> a -> f c
  4. -> b -> f d
  5. -> f ()
#

As bitraverse_, but with the structure as the primary argument. For a version that doesn't ignore the results, see bifor.

Examples

Basic usage:

Example1 expression
bifor_ ("Hello", True) print (print . show)"Hello""True"
Example1 expression
bifor_ (Right True) print (print . show)"True"
Example1 expression
bifor_ (Left "Hello") print (print . show)"Hello"
valuebilength :: Bifoldable t => t a b -> Int
#

Returns the size/length of a finite structure as an Int.

Examples

Basic usage:

Example1 expression
bilength (True, 42)2
Example1 expression
bilength (Right 42)1
Example1 expression
bilength (BiList [1,2,3] [4,5])5
Example1 expression
bilength (BiList [] [])0

On infinite structures, this function hangs:

> bilength (BiList [1..] [])
* Hangs forever *
valuebimaximum :: (Bifoldable t, Ord a) => t a a -> a
#

The largest element of a non-empty structure.

Examples

Basic usage:

Example1 expression
bimaximum (42, 17)42
Example1 expression
bimaximum (Right 42)42
Example1 expression
bimaximum (BiList [13, 29, 4] [18, 1, 7])29
Example1 expression
bimaximum (BiList [13, 29, 4] [])29

On empty structures, this function throws an exception:

Example1 expression
bimaximum (BiList [] [])*** Exception: bimaximum: empty structure...
valuebimaximumBy :: Bifoldable t => (a -> a -> Ordering) -> t a a -> a
#

The largest element of a non-empty structure with respect to the given comparison function.

Examples

Basic usage:

Example1 expression
bimaximumBy compare (42, 17)42
Example1 expression
bimaximumBy compare (Left 17)17
Example1 expression
bimaximumBy compare (BiList [42, 17, 23] [-5, 18])42

On empty structures, this function throws an exception:

Example1 expression
bimaximumBy compare (BiList [] [])*** Exception: bifoldr1: empty structure...
valuebiminimum :: (Bifoldable t, Ord a) => t a a -> a
#

The least element of a non-empty structure.

Examples

Basic usage:

Example1 expression
biminimum (42, 17)17
Example1 expression
biminimum (Right 42)42
Example1 expression
biminimum (BiList [13, 29, 4] [18, 1, 7])1
Example1 expression
biminimum (BiList [13, 29, 4] [])4

On empty structures, this function throws an exception:

Example1 expression
biminimum (BiList [] [])*** Exception: biminimum: empty structure...
valuebiminimumBy :: Bifoldable t => (a -> a -> Ordering) -> t a a -> a
#

The least element of a non-empty structure with respect to the given comparison function.

Examples

Basic usage:

Example1 expression
biminimumBy compare (42, 17)17
Example1 expression
biminimumBy compare (Left 17)17
Example1 expression
biminimumBy compare (BiList [42, 17, 23] [-5, 18])-5

On empty structures, this function throws an exception:

Example1 expression
biminimumBy compare (BiList [] [])*** Exception: bifoldr1: empty structure...
valuebinotElem :: (Bifoldable t, Eq a) => a -> t a a -> Bool
#

binotElem is the negation of bielem.

Examples

Basic usage:

Example1 expression
binotElem 42 (17, 42)False
Example1 expression
binotElem 42 (17, 43)True
Example1 expression
binotElem 42 (Left 42)False
Example1 expression
binotElem 42 (Right 13)True
Example1 expression
binotElem 42 (BiList [1..5] [1..100])False
Example1 expression
binotElem 42 (BiList [1..5] [1..41])True
valuebinull :: Bifoldable t => t a b -> Bool
#

Test whether the structure is empty.

Examples

Basic usage:

Example1 expression
binull (18, 42)False
Example1 expression
binull (Right 42)False
Example1 expression
binull (BiList [] [])True
valuebior :: Bifoldable t => t Bool Bool -> Bool
#

bior returns the disjunction of a container of Bools. For the result to be False, the container must be finite; True, however, results from a True value finitely far from the left end.

Examples

Basic usage:

Example1 expression
bior (True, False)True
Example1 expression
bior (False, False)False
Example1 expression
bior (Left True)True

Empty structures yield False:

Example1 expression
bior (BiList [] [])False

A True value finitely far from the left end yields True (short circuit):

Example1 expression
bior (BiList [False, False, True, False] (repeat False))True

A True value infinitely far from the left end hangs:

> bior (BiList (repeat False) [True])
* Hangs forever *

An infinitely False value hangs:

> bior (BiList (repeat False) [])
* Hangs forever *
valuebiproduct :: (Bifoldable t, Num a) => t a a -> a
#

The biproduct function computes the product of the numbers of a structure.

Examples

Basic usage:

Example1 expression
biproduct (42, 17)714
Example1 expression
biproduct (Right 42)42
Example1 expression
biproduct (BiList [13, 29, 4] [18, 1, 7])190008
Example1 expression
biproduct (BiList [13, 29, 4] [])1508
Example1 expression
biproduct (BiList [] [])1
valuebisequence_ :: (Bifoldable t, Applicative f) => t (f a) (f b) -> f ()
#

Evaluate each action in the structure from left to right, and ignore the results. For a version that doesn't ignore the results, see bisequence.

Examples

Basic usage:

Example1 expression
bisequence_ (print "Hello", print "World")"Hello""World"
Example1 expression
bisequence_ (Left (print "Hello"))"Hello"
Example1 expression
bisequence_ (Right (print "World"))"World"
valuebisum :: (Bifoldable t, Num a) => t a a -> a
#

The bisum function computes the sum of the numbers of a structure.

Examples

Basic usage:

Example1 expression
bisum (42, 17)59
Example1 expression
bisum (Right 42)42
Example1 expression
bisum (BiList [13, 29, 4] [18, 1, 7])72
Example1 expression
bisum (BiList [13, 29, 4] [])46
Example1 expression
bisum (BiList [] [])0
valuebitraverse_
  1. :: (Bifoldable t, Applicative f)
  2. => a -> f c
  3. -> b -> f d
  4. -> t a b
  5. -> f ()
#

Map each element of a structure using one of two actions, evaluate these actions from left to right, and ignore the results. For a version that doesn't ignore the results, see bitraverse.

Examples

Basic usage:

Example1 expression
bitraverse_ print (print . show) ("Hello", True)"Hello""True"
Example1 expression
bitraverse_ print (print . show) (Right True)"True"
Example1 expression
bitraverse_ print (print . show) (Left "Hello")"Hello"