{-# LANGUAGE Safe #-}
{-# LANGUAGE ScopedTypeVariables #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Bifoldable
-- 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.Bifoldable
  ( Bifoldable(..)
  , bifoldr'
  , bifoldr1
  , bifoldrM
  , bifoldl'
  , bifoldl1
  , bifoldlM
  , bitraverse_
  , bifor_
  , bimapM_
  , biforM_
  , bimsum
  , bisequenceA_
  , bisequence_
  , biasum
  , biList
  , binull
  , bilength
  , bielem
  , bimaximum
  , biminimum
  , bisum
  , biproduct
  , biconcat
  , biconcatMap
  , biand
  , bior
  , biany
  , biall
  , bimaximumBy
  , biminimumBy
  , binotElem
  , bifind
  ) where

import Control.Applicative
import Data.Functor.Utils (Max(..), Min(..), (#.))
import Data.Maybe (fromMaybe)
import Data.Monoid
import GHC.Generics (K1(..))

-- | '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 'Data.Bifunctor.Bifunctor' instance, it should satisfy:
--
-- @
-- 'bifoldMap' f g ≡ 'bifold' . 'Data.Bifunctor.bimap' f g
-- @
--
-- which implies that
--
-- @
-- 'bifoldMap' f g . 'Data.Bifunctor.bimap' h i ≡ 'bifoldMap' (f . h) (g . i)
-- @
--
-- @since 4.10.0.0
class Bifoldable p where
  {-# MINIMAL bifoldr | bifoldMap #-}

  -- | Combines the elements of a structure using a monoid.
  --
  -- @'bifold' ≡ 'bifoldMap' 'id' 'id'@
  --
  -- @since 4.10.0.0
  bifold :: Monoid m => p m m -> m
  bifold = (m -> m) -> (m -> m) -> p m m -> m
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap m -> m
forall a. a -> a
id m -> m
forall a. a -> a
id

  -- | Combines the elements of a structure, given ways of mapping them to a
  -- common monoid.
  --
  -- @'bifoldMap' f g
  --     ≡ 'bifoldr' ('mappend' . f) ('mappend' . g) 'mempty'@
  --
  -- @since 4.10.0.0
  bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> p a b -> m
  bifoldMap f :: a -> m
f g :: b -> m
g = (a -> m -> m) -> (b -> m -> m) -> m -> p a b -> m
forall (p :: * -> * -> *) a c b.
Bifoldable p =>
(a -> c -> c) -> (b -> c -> c) -> c -> p a b -> c
bifoldr (m -> m -> m
forall a. Monoid a => a -> a -> a
mappend (m -> m -> m) -> (a -> m) -> a -> m -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m
f) (m -> m -> m
forall a. Monoid a => a -> a -> a
mappend (m -> m -> m) -> (b -> m) -> b -> m -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> m
g) m
forall a. Monoid a => a
mempty

  -- | 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@
  --
  -- @since 4.10.0.0
  bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> p a b -> c
  bifoldr f :: a -> c -> c
f g :: b -> c -> c
g z :: c
z t :: p a b
t = Endo c -> c -> c
forall a. Endo a -> a -> a
appEndo ((a -> Endo c) -> (b -> Endo c) -> p a b -> Endo c
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap ((c -> c) -> Endo c
forall a. (a -> a) -> Endo a
Endo ((c -> c) -> Endo c) -> (a -> c -> c) -> a -> Endo c
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. a -> c -> c
f) ((c -> c) -> Endo c
forall a. (a -> a) -> Endo a
Endo ((c -> c) -> Endo c) -> (b -> c -> c) -> b -> Endo c
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. b -> c -> c
g) p a b
t) c
z

  -- | 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.
  --
  -- @since 4.10.0.0
  bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> p a b -> c
  bifoldl f :: c -> a -> c
f g :: c -> b -> c
g z :: c
z t :: p a b
t = Endo c -> c -> c
forall a. Endo a -> a -> a
appEndo (Dual (Endo c) -> Endo c
forall a. Dual a -> a
getDual ((a -> Dual (Endo c))
-> (b -> Dual (Endo c)) -> p a b -> Dual (Endo c)
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap (Endo c -> Dual (Endo c)
forall a. a -> Dual a
Dual (Endo c -> Dual (Endo c)) -> (a -> Endo c) -> a -> Dual (Endo c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> c) -> Endo c
forall a. (a -> a) -> Endo a
Endo ((c -> c) -> Endo c) -> (a -> c -> c) -> a -> Endo c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> a -> c) -> a -> c -> c
forall a b c. (a -> b -> c) -> b -> a -> c
flip c -> a -> c
f)
                                                (Endo c -> Dual (Endo c)
forall a. a -> Dual a
Dual (Endo c -> Dual (Endo c)) -> (b -> Endo c) -> b -> Dual (Endo c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> c) -> Endo c
forall a. (a -> a) -> Endo a
Endo ((c -> c) -> Endo c) -> (b -> c -> c) -> b -> Endo c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> b -> c) -> b -> c -> c
forall a b c. (a -> b -> c) -> b -> a -> c
flip c -> b -> c
g) p a b
t)) c
z

-- | @since 4.10.0.0
instance Bifoldable (,) where
  bifoldMap :: (a -> m) -> (b -> m) -> (a, b) -> m
bifoldMap f :: a -> m
f g :: b -> m
g ~(a :: a
a, b :: b
b) = a -> m
f a
a m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` b -> m
g b
b

-- | @since 4.10.0.0
instance Bifoldable Const where
  bifoldMap :: (a -> m) -> (b -> m) -> Const a b -> m
bifoldMap f :: a -> m
f _ (Const a :: a
a) = a -> m
f a
a

-- | @since 4.10.0.0
instance Bifoldable (K1 i) where
  bifoldMap :: (a -> m) -> (b -> m) -> K1 i a b -> m
bifoldMap f :: a -> m
f _ (K1 c :: a
c) = a -> m
f a
c

-- | @since 4.10.0.0
instance Bifoldable ((,,) x) where
  bifoldMap :: (a -> m) -> (b -> m) -> (x, a, b) -> m
bifoldMap f :: a -> m
f g :: b -> m
g ~(_,a :: a
a,b :: b
b) = a -> m
f a
a m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` b -> m
g b
b

-- | @since 4.10.0.0
instance Bifoldable ((,,,) x y) where
  bifoldMap :: (a -> m) -> (b -> m) -> (x, y, a, b) -> m
bifoldMap f :: a -> m
f g :: b -> m
g ~(_,_,a :: a
a,b :: b
b) = a -> m
f a
a m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` b -> m
g b
b

-- | @since 4.10.0.0
instance Bifoldable ((,,,,) x y z) where
  bifoldMap :: (a -> m) -> (b -> m) -> (x, y, z, a, b) -> m
bifoldMap f :: a -> m
f g :: b -> m
g ~(_,_,_,a :: a
a,b :: b
b) = a -> m
f a
a m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` b -> m
g b
b

-- | @since 4.10.0.0
instance Bifoldable ((,,,,,) x y z w) where
  bifoldMap :: (a -> m) -> (b -> m) -> (x, y, z, w, a, b) -> m
bifoldMap f :: a -> m
f g :: b -> m
g ~(_,_,_,_,a :: a
a,b :: b
b) = a -> m
f a
a m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` b -> m
g b
b

-- | @since 4.10.0.0
instance Bifoldable ((,,,,,,) x y z w v) where
  bifoldMap :: (a -> m) -> (b -> m) -> (x, y, z, w, v, a, b) -> m
bifoldMap f :: a -> m
f g :: b -> m
g ~(_,_,_,_,_,a :: a
a,b :: b
b) = a -> m
f a
a m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` b -> m
g b
b

-- | @since 4.10.0.0
instance Bifoldable Either where
  bifoldMap :: (a -> m) -> (b -> m) -> Either a b -> m
bifoldMap f :: a -> m
f _ (Left a :: a
a) = a -> m
f a
a
  bifoldMap _ g :: b -> m
g (Right b :: b
b) = b -> m
g b
b

-- | As 'bifoldr', but strict in the result of the reduction functions at each
-- step.
--
-- @since 4.10.0.0
bifoldr' :: Bifoldable t => (a -> c -> c) -> (b -> c -> c) -> c -> t a b -> c
bifoldr' :: (a -> c -> c) -> (b -> c -> c) -> c -> t a b -> c
bifoldr' f :: a -> c -> c
f g :: b -> c -> c
g z0 :: c
z0 xs :: t a b
xs = ((c -> c) -> a -> c -> c)
-> ((c -> c) -> b -> c -> c) -> (c -> c) -> t a b -> c -> c
forall (p :: * -> * -> *) c a b.
Bifoldable p =>
(c -> a -> c) -> (c -> b -> c) -> c -> p a b -> c
bifoldl (c -> c) -> a -> c -> c
forall b. (c -> b) -> a -> c -> b
f' (c -> c) -> b -> c -> c
forall b. (c -> b) -> b -> c -> b
g' c -> c
forall a. a -> a
id t a b
xs c
z0 where
  f' :: (c -> b) -> a -> c -> b
f' k :: c -> b
k x :: a
x z :: c
z = c -> b
k (c -> b) -> c -> b
forall a b. (a -> b) -> a -> b
$! a -> c -> c
f a
x c
z
  g' :: (c -> b) -> b -> c -> b
g' k :: c -> b
k x :: b
x z :: c
z = c -> b
k (c -> b) -> c -> b
forall a b. (a -> b) -> a -> b
$! b -> c -> c
g b
x c
z

-- | A variant of 'bifoldr' that has no base case,
-- and thus may only be applied to non-empty structures.
--
-- @since 4.10.0.0
bifoldr1 :: Bifoldable t => (a -> a -> a) -> t a a -> a
bifoldr1 :: (a -> a -> a) -> t a a -> a
bifoldr1 f :: a -> a -> a
f xs :: t a a
xs = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe ([Char] -> a
forall a. HasCallStack => [Char] -> a
error "bifoldr1: empty structure")
                  ((a -> Maybe a -> Maybe a)
-> (a -> Maybe a -> Maybe a) -> Maybe a -> t a a -> Maybe a
forall (p :: * -> * -> *) a c b.
Bifoldable p =>
(a -> c -> c) -> (b -> c -> c) -> c -> p a b -> c
bifoldr a -> Maybe a -> Maybe a
mbf a -> Maybe a -> Maybe a
mbf Maybe a
forall a. Maybe a
Nothing t a a
xs)
  where
    mbf :: a -> Maybe a -> Maybe a
mbf x :: a
x m :: Maybe a
m = a -> Maybe a
forall a. a -> Maybe a
Just (case Maybe a
m of
                      Nothing -> a
x
                      Just y :: a
y  -> a -> a -> a
f a
x a
y)

-- | Right associative monadic bifold over a structure.
--
-- @since 4.10.0.0
bifoldrM :: (Bifoldable t, Monad m)
         => (a -> c -> m c) -> (b -> c -> m c) -> c -> t a b -> m c
bifoldrM :: (a -> c -> m c) -> (b -> c -> m c) -> c -> t a b -> m c
bifoldrM f :: a -> c -> m c
f g :: b -> c -> m c
g z0 :: c
z0 xs :: t a b
xs = ((c -> m c) -> a -> c -> m c)
-> ((c -> m c) -> b -> c -> m c) -> (c -> m c) -> t a b -> c -> m c
forall (p :: * -> * -> *) c a b.
Bifoldable p =>
(c -> a -> c) -> (c -> b -> c) -> c -> p a b -> c
bifoldl (c -> m c) -> a -> c -> m c
forall b. (c -> m b) -> a -> c -> m b
f' (c -> m c) -> b -> c -> m c
forall b. (c -> m b) -> b -> c -> m b
g' c -> m c
forall (m :: * -> *) a. Monad m => a -> m a
return t a b
xs c
z0 where
  f' :: (c -> m b) -> a -> c -> m b
f' k :: c -> m b
k x :: a
x z :: c
z = a -> c -> m c
f a
x c
z m c -> (c -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= c -> m b
k
  g' :: (c -> m b) -> b -> c -> m b
g' k :: c -> m b
k x :: b
x z :: c
z = b -> c -> m c
g b
x c
z m c -> (c -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= c -> m b
k

-- | 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').
--
-- @since 4.10.0.0
bifoldl':: Bifoldable t => (a -> b -> a) -> (a -> c -> a) -> a -> t b c -> a
bifoldl' :: (a -> b -> a) -> (a -> c -> a) -> a -> t b c -> a
bifoldl' f :: a -> b -> a
f g :: a -> c -> a
g z0 :: a
z0 xs :: t b c
xs = (b -> (a -> a) -> a -> a)
-> (c -> (a -> a) -> a -> a) -> (a -> a) -> t b c -> a -> a
forall (p :: * -> * -> *) a c b.
Bifoldable p =>
(a -> c -> c) -> (b -> c -> c) -> c -> p a b -> c
bifoldr b -> (a -> a) -> a -> a
forall b. b -> (a -> b) -> a -> b
f' c -> (a -> a) -> a -> a
forall b. c -> (a -> b) -> a -> b
g' a -> a
forall a. a -> a
id t b c
xs a
z0 where
  f' :: b -> (a -> b) -> a -> b
f' x :: b
x k :: a -> b
k z :: a
z = a -> b
k (a -> b) -> a -> b
forall a b. (a -> b) -> a -> b
$! a -> b -> a
f a
z b
x
  g' :: c -> (a -> b) -> a -> b
g' x :: c
x k :: a -> b
k z :: a
z = a -> b
k (a -> b) -> a -> b
forall a b. (a -> b) -> a -> b
$! a -> c -> a
g a
z c
x

-- | A variant of 'bifoldl' that has no base case,
-- and thus may only be applied to non-empty structures.
--
-- @since 4.10.0.0
bifoldl1 :: Bifoldable t => (a -> a -> a) -> t a a -> a
bifoldl1 :: (a -> a -> a) -> t a a -> a
bifoldl1 f :: a -> a -> a
f xs :: t a a
xs = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe ([Char] -> a
forall a. HasCallStack => [Char] -> a
error "bifoldl1: empty structure")
                  ((Maybe a -> a -> Maybe a)
-> (Maybe a -> a -> Maybe a) -> Maybe a -> t a a -> Maybe a
forall (p :: * -> * -> *) c a b.
Bifoldable p =>
(c -> a -> c) -> (c -> b -> c) -> c -> p a b -> c
bifoldl Maybe a -> a -> Maybe a
mbf Maybe a -> a -> Maybe a
mbf Maybe a
forall a. Maybe a
Nothing t a a
xs)
  where
    mbf :: Maybe a -> a -> Maybe a
mbf m :: Maybe a
m y :: a
y = a -> Maybe a
forall a. a -> Maybe a
Just (case Maybe a
m of
                      Nothing -> a
y
                      Just x :: a
x  -> a -> a -> a
f a
x a
y)

-- | Left associative monadic bifold over a structure.
--
-- @since 4.10.0.0
bifoldlM :: (Bifoldable t, Monad m)
         => (a -> b -> m a) -> (a -> c -> m a) -> a -> t b c -> m a
bifoldlM :: (a -> b -> m a) -> (a -> c -> m a) -> a -> t b c -> m a
bifoldlM f :: a -> b -> m a
f g :: a -> c -> m a
g z0 :: a
z0 xs :: t b c
xs = (b -> (a -> m a) -> a -> m a)
-> (c -> (a -> m a) -> a -> m a) -> (a -> m a) -> t b c -> a -> m a
forall (p :: * -> * -> *) a c b.
Bifoldable p =>
(a -> c -> c) -> (b -> c -> c) -> c -> p a b -> c
bifoldr b -> (a -> m a) -> a -> m a
forall b. b -> (a -> m b) -> a -> m b
f' c -> (a -> m a) -> a -> m a
forall b. c -> (a -> m b) -> a -> m b
g' a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return t b c
xs a
z0 where
  f' :: b -> (a -> m b) -> a -> m b
f' x :: b
x k :: a -> m b
k z :: a
z = a -> b -> m a
f a
z b
x m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m b
k
  g' :: c -> (a -> m b) -> a -> m b
g' x :: c
x k :: a -> m b
k z :: a
z = a -> c -> m a
g a
z c
x m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m b
k

-- | 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 'Data.Bitraversable.bitraverse'.
--
-- @since 4.10.0.0
bitraverse_ :: (Bifoldable t, Applicative f)
            => (a -> f c) -> (b -> f d) -> t a b -> f ()
bitraverse_ :: (a -> f c) -> (b -> f d) -> t a b -> f ()
bitraverse_ f :: a -> f c
f g :: b -> f d
g = (a -> f () -> f ()) -> (b -> f () -> f ()) -> f () -> t a b -> f ()
forall (p :: * -> * -> *) a c b.
Bifoldable p =>
(a -> c -> c) -> (b -> c -> c) -> c -> p a b -> c
bifoldr (f c -> f () -> f ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>) (f c -> f () -> f ()) -> (a -> f c) -> a -> f () -> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> f c
f) (f d -> f () -> f ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>) (f d -> f () -> f ()) -> (b -> f d) -> b -> f () -> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> f d
g) (() -> f ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())

-- | As 'bitraverse_', but with the structure as the primary argument. For a
-- version that doesn't ignore the results, see 'Data.Bitraversable.bifor'.
--
-- >>> > bifor_ ('a', "bc") print (print . reverse)
-- 'a'
-- "cb"
--
-- @since 4.10.0.0
bifor_ :: (Bifoldable t, Applicative f)
       => t a b -> (a -> f c) -> (b -> f d) -> f ()
bifor_ :: t a b -> (a -> f c) -> (b -> f d) -> f ()
bifor_ t :: t a b
t f :: a -> f c
f g :: b -> f d
g = (a -> f c) -> (b -> f d) -> t a b -> f ()
forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bifoldable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f ()
bitraverse_ a -> f c
f b -> f d
g t a b
t

-- | Alias for 'bitraverse_'.
--
-- @since 4.10.0.0
bimapM_ :: (Bifoldable t, Applicative f)
        => (a -> f c) -> (b -> f d) -> t a b -> f ()
bimapM_ :: (a -> f c) -> (b -> f d) -> t a b -> f ()
bimapM_ = (a -> f c) -> (b -> f d) -> t a b -> f ()
forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bifoldable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f ()
bitraverse_

-- | Alias for 'bifor_'.
--
-- @since 4.10.0.0
biforM_ :: (Bifoldable t, Applicative f)
        => t a b ->  (a -> f c) -> (b -> f d) -> f ()
biforM_ :: t a b -> (a -> f c) -> (b -> f d) -> f ()
biforM_ = t a b -> (a -> f c) -> (b -> f d) -> f ()
forall (t :: * -> * -> *) (f :: * -> *) a b c d.
(Bifoldable t, Applicative f) =>
t a b -> (a -> f c) -> (b -> f d) -> f ()
bifor_

-- | Alias for 'bisequence_'.
--
-- @since 4.10.0.0
bisequenceA_ :: (Bifoldable t, Applicative f) => t (f a) (f b) -> f ()
bisequenceA_ :: t (f a) (f b) -> f ()
bisequenceA_ = t (f a) (f b) -> f ()
forall (t :: * -> * -> *) (f :: * -> *) a b.
(Bifoldable t, Applicative f) =>
t (f a) (f b) -> f ()
bisequence_

-- | Evaluate each action in the structure from left to right, and ignore the
-- results. For a version that doesn't ignore the results, see
-- 'Data.Bitraversable.bisequence'.
--
-- @since 4.10.0.0
bisequence_ :: (Bifoldable t, Applicative f) => t (f a) (f b) -> f ()
bisequence_ :: t (f a) (f b) -> f ()
bisequence_ = (f a -> f () -> f ())
-> (f b -> f () -> f ()) -> f () -> t (f a) (f b) -> f ()
forall (p :: * -> * -> *) a c b.
Bifoldable p =>
(a -> c -> c) -> (b -> c -> c) -> c -> p a b -> c
bifoldr f a -> f () -> f ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>) f b -> f () -> f ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>) (() -> f ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())

-- | The sum of a collection of actions, generalizing 'biconcat'.
--
-- @since 4.10.0.0
biasum :: (Bifoldable t, Alternative f) => t (f a) (f a) -> f a
biasum :: t (f a) (f a) -> f a
biasum = (f a -> f a -> f a)
-> (f a -> f a -> f a) -> f a -> t (f a) (f a) -> f a
forall (p :: * -> * -> *) a c b.
Bifoldable p =>
(a -> c -> c) -> (b -> c -> c) -> c -> p a b -> c
bifoldr f a -> f a -> f a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) f a -> f a -> f a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) f a
forall (f :: * -> *) a. Alternative f => f a
empty

-- | Alias for 'biasum'.
--
-- @since 4.10.0.0
bimsum :: (Bifoldable t, Alternative f) => t (f a) (f a) -> f a
bimsum :: t (f a) (f a) -> f a
bimsum = t (f a) (f a) -> f a
forall (t :: * -> * -> *) (f :: * -> *) a.
(Bifoldable t, Alternative f) =>
t (f a) (f a) -> f a
biasum

-- | Collects the list of elements of a structure, from left to right.
--
-- @since 4.10.0.0
biList :: Bifoldable t => t a a -> [a]
biList :: t a a -> [a]
biList = (a -> [a] -> [a]) -> (a -> [a] -> [a]) -> [a] -> t a a -> [a]
forall (p :: * -> * -> *) a c b.
Bifoldable p =>
(a -> c -> c) -> (b -> c -> c) -> c -> p a b -> c
bifoldr (:) (:) []

-- | Test whether the structure is empty.
--
-- @since 4.10.0.0
binull :: Bifoldable t => t a b -> Bool
binull :: t a b -> Bool
binull = (a -> Bool -> Bool) -> (b -> Bool -> Bool) -> Bool -> t a b -> Bool
forall (p :: * -> * -> *) a c b.
Bifoldable p =>
(a -> c -> c) -> (b -> c -> c) -> c -> p a b -> c
bifoldr (\_ _ -> Bool
False) (\_ _ -> Bool
False) Bool
True

-- | Returns the size/length of a finite structure as an 'Int'.
--
-- @since 4.10.0.0
bilength :: Bifoldable t => t a b -> Int
bilength :: t a b -> Int
bilength = (Int -> a -> Int) -> (Int -> b -> Int) -> Int -> t a b -> Int
forall (t :: * -> * -> *) a b c.
Bifoldable t =>
(a -> b -> a) -> (a -> c -> a) -> a -> t b c -> a
bifoldl' (\c :: Int
c _ -> Int
cInt -> Int -> Int
forall a. Num a => a -> a -> a
+1) (\c :: Int
c _ -> Int
cInt -> Int -> Int
forall a. Num a => a -> a -> a
+1) 0

-- | Does the element occur in the structure?
--
-- @since 4.10.0.0
bielem :: (Bifoldable t, Eq a) => a -> t a a -> Bool
bielem :: a -> t a a -> Bool
bielem x :: a
x = (a -> Bool) -> (a -> Bool) -> t a a -> Bool
forall (t :: * -> * -> *) a b.
Bifoldable t =>
(a -> Bool) -> (b -> Bool) -> t a b -> Bool
biany (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x) (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x)

-- | Reduces a structure of lists to the concatenation of those lists.
--
-- @since 4.10.0.0
biconcat :: Bifoldable t => t [a] [a] -> [a]
biconcat :: t [a] [a] -> [a]
biconcat = t [a] [a] -> [a]
forall (p :: * -> * -> *) m. (Bifoldable p, Monoid m) => p m m -> m
bifold

-- | The largest element of a non-empty structure.
--
-- @since 4.10.0.0
bimaximum :: forall t a. (Bifoldable t, Ord a) => t a a -> a
bimaximum :: t a a -> a
bimaximum = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe ([Char] -> a
forall a. HasCallStack => [Char] -> a
error "bimaximum: empty structure") (Maybe a -> a) -> (t a a -> Maybe a) -> t a a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Max a -> Maybe a
forall a. Max a -> Maybe a
getMax (Max a -> Maybe a) -> (t a a -> Max a) -> t a a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Max a) -> (a -> Max a) -> t a a -> Max a
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap a -> Max a
mj a -> Max a
mj
  where mj :: a -> Max a
mj = Maybe a -> Max a
forall a. Maybe a -> Max a
Max (Maybe a -> Max a) -> (a -> Maybe a) -> a -> Max a
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (a -> Maybe a
forall a. a -> Maybe a
Just :: a -> Maybe a)

-- | The least element of a non-empty structure.
--
-- @since 4.10.0.0
biminimum :: forall t a. (Bifoldable t, Ord a) => t a a -> a
biminimum :: t a a -> a
biminimum = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe ([Char] -> a
forall a. HasCallStack => [Char] -> a
error "biminimum: empty structure") (Maybe a -> a) -> (t a a -> Maybe a) -> t a a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Min a -> Maybe a
forall a. Min a -> Maybe a
getMin (Min a -> Maybe a) -> (t a a -> Min a) -> t a a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Min a) -> (a -> Min a) -> t a a -> Min a
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap a -> Min a
mj a -> Min a
mj
  where mj :: a -> Min a
mj = Maybe a -> Min a
forall a. Maybe a -> Min a
Min (Maybe a -> Min a) -> (a -> Maybe a) -> a -> Min a
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (a -> Maybe a
forall a. a -> Maybe a
Just :: a -> Maybe a)

-- | The 'bisum' function computes the sum of the numbers of a structure.
--
-- @since 4.10.0.0
bisum :: (Bifoldable t, Num a) => t a a -> a
bisum :: t a a -> a
bisum = Sum a -> a
forall a. Sum a -> a
getSum (Sum a -> a) -> (t a a -> Sum a) -> t a a -> a
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (a -> Sum a) -> (a -> Sum a) -> t a a -> Sum a
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap a -> Sum a
forall a. a -> Sum a
Sum a -> Sum a
forall a. a -> Sum a
Sum

-- | The 'biproduct' function computes the product of the numbers of a
-- structure.
--
-- @since 4.10.0.0
biproduct :: (Bifoldable t, Num a) => t a a -> a
biproduct :: t a a -> a
biproduct = Product a -> a
forall a. Product a -> a
getProduct (Product a -> a) -> (t a a -> Product a) -> t a a -> a
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (a -> Product a) -> (a -> Product a) -> t a a -> Product a
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap a -> Product a
forall a. a -> Product a
Product a -> Product a
forall a. a -> Product a
Product

-- | Given a means of mapping the elements of a structure to lists, computes the
-- concatenation of all such lists in order.
--
-- @since 4.10.0.0
biconcatMap :: Bifoldable t => (a -> [c]) -> (b -> [c]) -> t a b -> [c]
biconcatMap :: (a -> [c]) -> (b -> [c]) -> t a b -> [c]
biconcatMap = (a -> [c]) -> (b -> [c]) -> t a b -> [c]
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap

-- | '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.
--
-- @since 4.10.0.0
biand :: Bifoldable t => t Bool Bool -> Bool
biand :: t Bool Bool -> Bool
biand = All -> Bool
getAll (All -> Bool) -> (t Bool Bool -> All) -> t Bool Bool -> Bool
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (Bool -> All) -> (Bool -> All) -> t Bool Bool -> All
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap Bool -> All
All Bool -> All
All

-- | '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.
--
-- @since 4.10.0.0
bior :: Bifoldable t => t Bool Bool -> Bool
bior :: t Bool Bool -> Bool
bior = Any -> Bool
getAny (Any -> Bool) -> (t Bool Bool -> Any) -> t Bool Bool -> Bool
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (Bool -> Any) -> (Bool -> Any) -> t Bool Bool -> Any
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap Bool -> Any
Any Bool -> Any
Any

-- | Determines whether any element of the structure satisfies its appropriate
-- predicate argument.
--
-- @since 4.10.0.0
biany :: Bifoldable t => (a -> Bool) -> (b -> Bool) -> t a b -> Bool
biany :: (a -> Bool) -> (b -> Bool) -> t a b -> Bool
biany p :: a -> Bool
p q :: b -> Bool
q = Any -> Bool
getAny (Any -> Bool) -> (t a b -> Any) -> t a b -> Bool
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (a -> Any) -> (b -> Any) -> t a b -> Any
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap (Bool -> Any
Any (Bool -> Any) -> (a -> Bool) -> a -> Any
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
p) (Bool -> Any
Any (Bool -> Any) -> (b -> Bool) -> b -> Any
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Bool
q)

-- | Determines whether all elements of the structure satisfy their appropriate
-- predicate argument.
--
-- @since 4.10.0.0
biall :: Bifoldable t => (a -> Bool) -> (b -> Bool) -> t a b -> Bool
biall :: (a -> Bool) -> (b -> Bool) -> t a b -> Bool
biall p :: a -> Bool
p q :: b -> Bool
q = All -> Bool
getAll (All -> Bool) -> (t a b -> All) -> t a b -> Bool
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (a -> All) -> (b -> All) -> t a b -> All
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap (Bool -> All
All (Bool -> All) -> (a -> Bool) -> a -> All
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
p) (Bool -> All
All (Bool -> All) -> (b -> Bool) -> b -> All
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Bool
q)

-- | The largest element of a non-empty structure with respect to the
-- given comparison function.
--
-- @since 4.10.0.0
bimaximumBy :: Bifoldable t => (a -> a -> Ordering) -> t a a -> a
bimaximumBy :: (a -> a -> Ordering) -> t a a -> a
bimaximumBy cmp :: a -> a -> Ordering
cmp = (a -> a -> a) -> t a a -> a
forall (t :: * -> * -> *) a.
Bifoldable t =>
(a -> a -> a) -> t a a -> a
bifoldr1 a -> a -> a
max'
  where max' :: a -> a -> a
max' x :: a
x y :: a
y = case a -> a -> Ordering
cmp a
x a
y of
                        GT -> a
x
                        _  -> a
y

-- | The least element of a non-empty structure with respect to the
-- given comparison function.
--
-- @since 4.10.0.0
biminimumBy :: Bifoldable t => (a -> a -> Ordering) -> t a a -> a
biminimumBy :: (a -> a -> Ordering) -> t a a -> a
biminimumBy cmp :: a -> a -> Ordering
cmp = (a -> a -> a) -> t a a -> a
forall (t :: * -> * -> *) a.
Bifoldable t =>
(a -> a -> a) -> t a a -> a
bifoldr1 a -> a -> a
min'
  where min' :: a -> a -> a
min' x :: a
x y :: a
y = case a -> a -> Ordering
cmp a
x a
y of
                        GT -> a
y
                        _  -> a
x

-- | 'binotElem' is the negation of 'bielem'.
--
-- @since 4.10.0.0
binotElem :: (Bifoldable t, Eq a) => a -> t a a-> Bool
binotElem :: a -> t a a -> Bool
binotElem x :: a
x =  Bool -> Bool
not (Bool -> Bool) -> (t a a -> Bool) -> t a a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> t a a -> Bool
forall (t :: * -> * -> *) a.
(Bifoldable t, Eq a) =>
a -> t a a -> Bool
bielem a
x

-- | 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.
--
-- @since 4.10.0.0
bifind :: Bifoldable t => (a -> Bool) -> t a a -> Maybe a
bifind :: (a -> Bool) -> t a a -> Maybe a
bifind p :: a -> Bool
p = First a -> Maybe a
forall a. First a -> Maybe a
getFirst (First a -> Maybe a) -> (t a a -> First a) -> t a a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> First a) -> (a -> First a) -> t a a -> First a
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap a -> First a
finder a -> First a
finder
  where finder :: a -> First a
finder x :: a
x = Maybe a -> First a
forall a. Maybe a -> First a
First (if a -> Bool
p a
x then a -> Maybe a
forall a. a -> Maybe a
Just a
x else Maybe a
forall a. Maybe a
Nothing)