bifunctors-5.4.1: Bifunctors

Copyright | (C) 2011-2015 Edward Kmett
License | BSD-style (see the file LICENSE)
Maintainer | Edward Kmett <ekmett@gmail.com>
Stability | provisional
Portability | portable
Safe Haskell | Trustworthy
Language | Haskell98

Data.Bifoldable

Description

Synopsis

- class Bifoldable p where
- bifoldr' :: Bifoldable t => (a -> c -> c) -> (b -> c -> c) -> c -> t a b -> c
- bifoldr1 :: Bifoldable t => (a -> a -> a) -> t a a -> a
- bifoldrM :: (Bifoldable t, Monad m) => (a -> c -> m c) -> (b -> c -> m c) -> c -> t a b -> m c
- bifoldl' :: Bifoldable t => (a -> b -> a) -> (a -> c -> a) -> a -> t b c -> a
- bifoldl1 :: Bifoldable t => (a -> a -> a) -> t a a -> a
- bifoldlM :: (Bifoldable t, Monad m) => (a -> b -> m a) -> (a -> c -> m a) -> a -> t b c -> m a
- bitraverse_ :: (Bifoldable t, Applicative f) => (a -> f c) -> (b -> f d) -> t a b -> f ()
- bifor_ :: (Bifoldable t, Applicative f) => t a b -> (a -> f c) -> (b -> f d) -> f ()
- bimapM_ :: (Bifoldable t, Monad m) => (a -> m c) -> (b -> m d) -> t a b -> m ()
- biforM_ :: (Bifoldable t, Monad m) => t a b -> (a -> m c) -> (b -> m d) -> m ()
- bimsum :: (Bifoldable t, MonadPlus m) => t (m a) (m a) -> m a
- bisequenceA_ :: (Bifoldable t, Applicative f) => t (f a) (f b) -> f ()
- bisequence_ :: (Bifoldable t, Monad m) => t (m a) (m b) -> m ()
- biasum :: (Bifoldable t, Alternative f) => t (f a) (f a) -> f a
- biList :: Bifoldable t => t a a -> [a]
- binull :: Bifoldable t => t a b -> Bool
- bilength :: Bifoldable t => t a b -> Int
- bielem :: (Bifoldable t, Eq a) => a -> t a a -> Bool
- bimaximum :: forall t a. (Bifoldable t, Ord a) => t a a -> a
- biminimum :: forall t a. (Bifoldable t, Ord a) => t a a -> a
- bisum :: (Bifoldable t, Num a) => t a a -> a
- biproduct :: (Bifoldable t, Num a) => t a a -> a
- biconcat :: Bifoldable t => t [a] [a] -> [a]
- biconcatMap :: Bifoldable t => (a -> [c]) -> (b -> [c]) -> t a b -> [c]
- biand :: Bifoldable t => t Bool Bool -> Bool
- bior :: Bifoldable t => t Bool Bool -> Bool
- biany :: Bifoldable t => (a -> Bool) -> (b -> Bool) -> t a b -> Bool
- biall :: Bifoldable t => (a -> Bool) -> (b -> Bool) -> t a b -> Bool
- bimaximumBy :: Bifoldable t => (a -> a -> Ordering) -> t a a -> a
- biminimumBy :: Bifoldable t => (a -> a -> Ordering) -> t a a -> a
- binotElem :: (Bifoldable t, Eq a) => a -> t a a -> Bool
- bifind :: Bifoldable t => (a -> Bool) -> t a a -> Maybe a

# Documentation

class Bifoldable p 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)

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 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)

Minimal complete definition

bifoldr | bifoldMap

Methods

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

Combines the elements of a structure using a monoid.

    bifold ≡ bifoldMap id id

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

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

bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> p a b -> c #

Combines the elments 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.

Instances

Bifoldable Either # |
Methods bifold :: Monoid m => Either m m -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> Either a b -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> Either a b -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> Either a b -> c #
Bifoldable (,) # |
Methods bifold :: Monoid m => (m, m) -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> (a, b) -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> (a, b) -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> (a, b) -> c #
Bifoldable Arg # |
Methods bifold :: Monoid m => Arg m m -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> Arg a b -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> Arg a b -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> Arg a b -> c #
Bifoldable (K1 i) # |
Methods bifold :: Monoid m => K1 i m m -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> K1 i a b -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> K1 i a b -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> K1 i a b -> c #
Bifoldable ((,,) x) # |
Methods bifold :: Monoid m => (x, m, m) -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> (x, a, b) -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> (x, a, b) -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> (x, a, b) -> c #
Bifoldable (Const *) # |
Methods bifold :: Monoid m => Const * m m -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> Const * a b -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> Const * a b -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> Const * a b -> c #
Bifoldable (Tagged *) # |
Methods bifold :: Monoid m => Tagged * m m -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> Tagged * a b -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> Tagged * a b -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> Tagged * a b -> c #
Bifoldable (Constant *) # |
Methods bifold :: Monoid m => Constant * m m -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> Constant * a b -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> Constant * a b -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> Constant * a b -> c #
Bifoldable ((,,,) x y) # |
Methods bifold :: Monoid m => (x, y, m, m) -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> (x, y, a, b) -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> (x, y, a, b) -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> (x, y, a, b) -> c #
Bifoldable ((,,,,) x y z) # |
Methods bifold :: Monoid m => (x, y, z, m, m) -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> (x, y, z, a, b) -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> (x, y, z, a, b) -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> (x, y, z, a, b) -> c #
Foldable f => Bifoldable (Clown * * f) # |
Methods bifold :: Monoid m => Clown * * f m m -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> Clown * * f a b -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> Clown * * f a b -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> Clown * * f a b -> c #
Bifoldable p => Bifoldable (Flip * * p) # |
Methods bifold :: Monoid m => Flip * * p m m -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> Flip * * p a b -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> Flip * * p a b -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> Flip * * p a b -> c #
Foldable g => Bifoldable (Joker * * g) # |
Methods bifold :: Monoid m => Joker * * g m m -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> Joker * * g a b -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> Joker * * g a b -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> Joker * * g a b -> c #
Bifoldable p => Bifoldable (WrappedBifunctor * * p) # |
Methods bifold :: Monoid m => WrappedBifunctor * * p m m -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> WrappedBifunctor * * p a b -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> WrappedBifunctor * * p a b -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> WrappedBifunctor * * p a b -> c #
Bifoldable ((,,,,,) x y z w) # |
Methods bifold :: Monoid m => (x, y, z, w, m, m) -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> (x, y, z, w, a, b) -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> (x, y, z, w, a, b) -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> (x, y, z, w, a, b) -> c #
(Bifoldable p, Bifoldable q) => Bifoldable (Sum * * p q) # |
Methods bifold :: Monoid m => Sum * * p q m m -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> Sum * * p q a b -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> Sum * * p q a b -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> Sum * * p q a b -> c #
(Bifoldable f, Bifoldable g) => Bifoldable (Product * * f g) # |
Methods bifold :: Monoid m => Product * * f g m m -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> Product * * f g a b -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> Product * * f g a b -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> Product * * f g a b -> c #
Bifoldable ((,,,,,,) x y z w v) # |
Methods bifold :: Monoid m => (x, y, z, w, v, m, m) -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> (x, y, z, w, v, a, b) -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> (x, y, z, w, v, a, b) -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> (x, y, z, w, v, a, b) -> c #
(Foldable f, Bifoldable p) => Bifoldable (Tannen * * * f p) # |
Methods bifold :: Monoid m => Tannen * * * f p m m -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> Tannen * * * f p a b -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> Tannen * * * f p a b -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> Tannen * * * f p a b -> c #
(Bifoldable p, Foldable f, Foldable g) => Bifoldable (Biff * * * * p f g) # |
Methods bifold :: Monoid m => Biff * * * * p f g m m -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> Biff * * * * p f g a b -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> Biff * * * * p f g a b -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> Biff * * * * p f g a b -> c #

bifoldr' :: Bifoldable t => (a -> c -> c) -> (b -> c -> c) -> c -> t a b -> c #

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

bifoldr1 :: 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.

bifoldrM :: (Bifoldable t, Monad m) => (a -> c -> m c) -> (b -> c -> m c) -> c -> t a b -> m c #

Right associative monadic bifold over a structure.

bifoldl' :: Bifoldable t => (a -> b -> a) -> (a -> c -> a) -> a -> t b c -> 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`).

bifoldl1 :: 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.

bifoldlM :: (Bifoldable t, Monad m) => (a -> b -> m a) -> (a -> c -> m a) -> a -> t b c -> m a #

Left associative monadic bifold over a structure.

bitraverse_ :: (Bifoldable t, Applicative f) => (a -> f c) -> (b -> f d) -> t a b -> 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`.

bifor_ :: (Bifoldable t, Applicative f) => t a b -> (a -> f c) -> (b -> f d) -> f () #

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

    >>> > bifor_ ('a', "bc") print (print . reverse)
    'a'
    "cb"

bimapM_ :: (Bifoldable t, Monad m) => (a -> m c) -> (b -> m d) -> t a b -> m () #

As `bimapM`, but ignores the results of the functions, merely performing the "actions".

biforM_ :: (Bifoldable t, Monad m) => t a b -> (a -> m c) -> (b -> m d) -> m () #

As `bimapM_`, but with the structure as the primary argument.

bimsum :: (Bifoldable t, MonadPlus m) => t (m a) (m a) -> m a #

The sum of a collection of actions, generalizing `biconcat`.

bisequenceA_ :: (Bifoldable t, Applicative f) => t (f a) (f b) -> f () #

As `bisequenceA`, but ignores the results of the actions.

bisequence_ :: (Bifoldable t, Monad m) => t (m a) (m b) -> m () #

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`.

biasum :: (Bifoldable t, Alternative f) => t (f a) (f a) -> f a #

The sum of a collection of actions, generalizing `biconcat`.

biList :: Bifoldable t => t a a -> [a] #

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

binull :: Bifoldable t => t a b -> Bool #

Test whether the structure is empty.

bilength :: Bifoldable t => t a b -> Int #

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

bielem :: (Bifoldable t, Eq a) => a -> t a a -> Bool #

Does the element occur in the structure?

bimaximum :: forall t a. (Bifoldable t, Ord a) => t a a -> a #

The largest element of a non-empty structure.

biminimum :: forall t a. (Bifoldable t, Ord a) => t a a -> a #

The least element of a non-empty structure.

bisum :: (Bifoldable t, Num a) => t a a -> a #

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

biproduct :: (Bifoldable t, Num a) => t a a -> a #

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

biconcat :: Bifoldable t => t [a] [a] -> [a] #

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

biconcatMap :: 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.

biand :: 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.

bior :: 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.

biany :: Bifoldable t => (a -> Bool) -> (b -> Bool) -> t a b -> Bool #

Determines whether any element of the structure satisfies the appropriate predicate.

biall :: Bifoldable t => (a -> Bool) -> (b -> Bool) -> t a b -> Bool #

Determines whether all elements of the structure satisfy the appropriate predicate.

bimaximumBy :: Bifoldable t => (a -> a -> Ordering) -> t a a -> a #

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

biminimumBy :: Bifoldable t => (a -> a -> Ordering) -> t a a -> a #

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

binotElem :: (Bifoldable t, Eq a) => a -> t a a -> Bool #

`binotElem` is the negation of `bielem`.

bifind :: 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.