{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
module Data.Foldable.Constrained
( CFoldable(..)
, cfoldrM
, cfoldlM
, ctraverse_
, cfor_
, cmapM_
, cforM_
, csequenceA_
, csequence_
, casum
, cmsum
, cconcat
, cconcatMap
, cand
, cor
, cany
, call
, cmaximumBy
, cminimumBy
, cnotElem
, cfind
, module Data.Constrained
) where
import Prelude
(Bool(..), id, (.), Int, Eq(..), Num(..), ($), ($!), flip, errorWithoutStackTrace, not)
import Control.Applicative
import Control.Monad hiding (mapM_)
import Data.Coerce
import Data.Either
import qualified Data.Foldable as F
import Data.Functor.Compose (Compose(..))
import Data.Functor.Identity (Identity(..))
import Data.Functor.Product as Product
import Data.Functor.Sum as Sum
import Data.List.NonEmpty (NonEmpty(..))
import Data.Maybe
import Data.Monoid
import qualified Data.Monoid as Monoid
import Data.Ord
import Data.Semigroup (Max(..), Min(..), Option(..))
import qualified Data.Semigroup as Semigroup
import GHC.Base (build)
import Data.Constrained (Constrained(..))
class Constrained f => CFoldable f where
{-# MINIMAL cfoldMap | cfoldr #-}
cfold :: (Monoid m, Constraints f m) => f m -> m
cfold = cfoldMap id
{-# INLINABLE cfold #-}
cfoldMap :: (Monoid m, Constraints f a) => (a -> m) -> f a -> m
cfoldMap f = cfoldr (mappend . f) mempty
{-# INLINE cfoldMap #-}
cfoldr :: Constraints f a => (a -> b -> b) -> b -> f a -> b
cfoldr f z t = appEndo (cfoldMap (Endo . f) t) z
cfoldr' :: Constraints f a => (a -> b -> b) -> b -> f a -> b
cfoldr' f z0 xs = cfoldl f' id xs z0
where
f' k x z = k $! f x z
cfoldl :: Constraints f a => (b -> a -> b) -> b -> f a -> b
cfoldl f z t = appEndo (getDual (cfoldMap (Dual . Endo . flip f) t)) z
cfoldl' :: Constraints f a => (b -> a -> b) -> b -> f a -> b
cfoldl' f z0 xs = cfoldr f' id xs z0
where
f' x k z = k $! f z x
cfoldr1 :: Constraints f a => (a -> a -> a) -> f a -> a
cfoldr1 f xs =
fromMaybe (errorWithoutStackTrace "foldr1: empty structure")
(cfoldr mf Nothing xs)
where
mf x m = Just $ case m of
Nothing -> x
Just y -> f x y
cfoldl1 :: Constraints f a => (a -> a -> a) -> f a -> a
cfoldl1 f xs =
fromMaybe (errorWithoutStackTrace "foldl1: empty structure")
(cfoldl mf Nothing xs)
where
mf m y = Just $ case m of
Nothing -> y
Just x -> f x y
ctoList :: Constraints f a => f a -> [a]
ctoList t = build (\ c n -> cfoldr c n t)
{-# INLINE ctoList #-}
cnull :: Constraints f a => f a -> Bool
cnull = cfoldr (\_ _ -> False) True
{-# INLINE cnull #-}
clength :: Constraints f a => f a -> Int
clength = cfoldl' (\c _ -> c + 1) 0
celem :: (Eq a, Constraints f a) => a -> f a -> Bool
celem = cany . (==)
{-# INLINE celem #-}
cmaximum :: forall a. (Ord a, Constraints f a) => f a -> a
cmaximum
= maybe (errorWithoutStackTrace "maximum: empty structure") getMax
. getOption
. cfoldMap (Option . Just . Max)
{-# INLINABLE cmaximum #-}
cminimum :: forall a. (Ord a, Constraints f a) => f a -> a
cminimum
= maybe (errorWithoutStackTrace "maximum: empty structure") getMin
. getOption
. cfoldMap (Option . Just . Min)
{-# INLINABLE cminimum #-}
csum :: (Num a, Constraints f a) => f a -> a
csum = getSum . cfoldMap Sum
{-# INLINABLE csum #-}
cproduct :: (Num a, Constraints f a) => f a -> a
cproduct = getProduct . cfoldMap Product
{-# INLINABLE cproduct #-}
cfoldrM :: (CFoldable f, Monad m, Constraints f a) => (a -> b -> m b) -> b -> f a -> m b
cfoldrM f z0 xs = cfoldl f' return xs z0
where
f' k x z = f x z >>= k
cfoldlM :: (CFoldable f, Monad m, Constraints f a) => (b -> a -> m b) -> b -> f a -> m b
cfoldlM f z0 xs = cfoldr f' return xs z0
where
f' x k z = f z x >>= k
ctraverse_ :: (CFoldable f, Applicative f, Constraints f a) => (a -> f b) -> f a -> f ()
ctraverse_ f = cfoldr ((*>) . f) (pure ())
cfor_ :: (CFoldable f, Applicative f, Constraints f a) => f a -> (a -> f b) -> f ()
{-# INLINE cfor_ #-}
cfor_ = flip ctraverse_
cmapM_ :: (CFoldable f, Monad m, Constraints f a) => (a -> m b) -> f a -> m ()
cmapM_ f = cfoldr ((>>) . f) (return ())
cforM_ :: (CFoldable f, Monad m, Constraints f a) => f a -> (a -> m b) -> m ()
{-# INLINE cforM_ #-}
cforM_ = flip cmapM_
csequenceA_ :: (CFoldable f, Applicative m, Constraints f (m a)) => f (m a) -> m ()
csequenceA_ = cfoldr (*>) (pure ())
csequence_ :: (CFoldable f, Monad m, Constraints f a, Constraints f (m a)) => f (m a) -> m ()
csequence_ = cfoldr (>>) (return ())
casum :: (CFoldable f, Alternative m, Constraints f (m a)) => f (m a) -> m a
{-# INLINE casum #-}
casum = cfoldr (<|>) empty
cmsum :: (CFoldable f, MonadPlus m, Constraints f (m a)) => f (m a) -> m a
{-# INLINE cmsum #-}
cmsum = casum
cconcat :: (CFoldable f, Constraints f [a]) => f [a] -> [a]
cconcat xs = build (\c n -> cfoldr (\x y -> cfoldr c y x) n xs)
{-# INLINE cconcat #-}
cconcatMap :: (CFoldable f, Constraints f a) => (a -> [b]) -> f a -> [b]
cconcatMap f xs = build (\c n -> cfoldr (\x b -> cfoldr c b (f x)) n xs)
{-# INLINE cconcatMap #-}
cand :: (CFoldable f, Constraints f Bool) => f Bool -> Bool
cand = getAll . cfoldMap All
cor :: (CFoldable f, Constraints f Bool) => f Bool -> Bool
cor = getAny . cfoldMap Any
cany :: (CFoldable f, Constraints f a) => (a -> Bool) -> f a -> Bool
cany p = getAny . cfoldMap (Any . p)
call :: (CFoldable f, Constraints f a) => (a -> Bool) -> f a -> Bool
call p = getAll . cfoldMap (All . p)
cmaximumBy :: (CFoldable f, Constraints f a) => (a -> a -> Ordering) -> f a -> a
cmaximumBy cmp = cfoldl1 max'
where
max' x y = case cmp x y of
GT -> x
_ -> y
cminimumBy :: (CFoldable f, Constraints f a) => (a -> a -> Ordering) -> f a -> a
cminimumBy cmp = cfoldl1 min'
where
min' x y = case cmp x y of
GT -> y
_ -> x
cnotElem :: (CFoldable f, Eq a, Constraints f a) => a -> f a -> Bool
cnotElem x = not . celem x
cfind :: (CFoldable f, Constraints f a) => (a -> Bool) -> f a -> Maybe a
cfind p = getFirst . cfoldMap (\ x -> First (if p x then Just x else Nothing))
instance CFoldable [] where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
{-# INLINE ctoList #-}
{-# INLINE cnull #-}
{-# INLINE clength #-}
{-# INLINE celem #-}
{-# INLINE cmaximum #-}
{-# INLINE cminimum #-}
{-# INLINE csum #-}
{-# INLINE cproduct #-}
cfold = F.fold
cfoldMap = F.foldMap
cfoldr = F.foldr
cfoldr' = F.foldr'
cfoldl = F.foldl
cfoldl' = F.foldl'
cfoldr1 = F.foldr1
cfoldl1 = F.foldl1
ctoList = F.toList
cnull = F.null
clength = F.length
celem = F.elem
cmaximum = F.maximum
cminimum = F.minimum
csum = F.sum
cproduct = F.product
instance CFoldable NonEmpty where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
{-# INLINE ctoList #-}
{-# INLINE cnull #-}
{-# INLINE clength #-}
{-# INLINE celem #-}
{-# INLINE cmaximum #-}
{-# INLINE cminimum #-}
{-# INLINE csum #-}
{-# INLINE cproduct #-}
cfold = F.fold
cfoldMap = F.foldMap
cfoldr = F.foldr
cfoldr' = F.foldr'
cfoldl = F.foldl
cfoldl' = F.foldl'
cfoldr1 = F.foldr1
cfoldl1 = F.foldl1
ctoList = F.toList
cnull = F.null
clength = F.length
celem = F.elem
cmaximum = F.maximum
cminimum = F.minimum
csum = F.sum
cproduct = F.product
instance CFoldable Identity where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
{-# INLINE ctoList #-}
{-# INLINE cnull #-}
{-# INLINE clength #-}
{-# INLINE celem #-}
{-# INLINE cmaximum #-}
{-# INLINE cminimum #-}
{-# INLINE csum #-}
{-# INLINE cproduct #-}
cfold = F.fold
cfoldMap = F.foldMap
cfoldr = F.foldr
cfoldr' = F.foldr'
cfoldl = F.foldl
cfoldl' = F.foldl'
cfoldr1 = F.foldr1
cfoldl1 = F.foldl1
ctoList = F.toList
cnull = F.null
clength = F.length
celem = F.elem
cmaximum = F.maximum
cminimum = F.minimum
csum = F.sum
cproduct = F.product
instance CFoldable ((,) a) where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
{-# INLINE ctoList #-}
{-# INLINE cnull #-}
{-# INLINE clength #-}
{-# INLINE celem #-}
{-# INLINE cmaximum #-}
{-# INLINE cminimum #-}
{-# INLINE csum #-}
{-# INLINE cproduct #-}
cfold = F.fold
cfoldMap = F.foldMap
cfoldr = F.foldr
cfoldr' = F.foldr'
cfoldl = F.foldl
cfoldl' = F.foldl'
cfoldr1 = F.foldr1
cfoldl1 = F.foldl1
ctoList = F.toList
cnull = F.null
clength = F.length
celem = F.elem
cmaximum = F.maximum
cminimum = F.minimum
csum = F.sum
cproduct = F.product
instance CFoldable Maybe where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
{-# INLINE ctoList #-}
{-# INLINE cnull #-}
{-# INLINE clength #-}
{-# INLINE celem #-}
{-# INLINE cmaximum #-}
{-# INLINE cminimum #-}
{-# INLINE csum #-}
{-# INLINE cproduct #-}
cfold = F.fold
cfoldMap = F.foldMap
cfoldr = F.foldr
cfoldr' = F.foldr'
cfoldl = F.foldl
cfoldl' = F.foldl'
cfoldr1 = F.foldr1
cfoldl1 = F.foldl1
ctoList = F.toList
cnull = F.null
clength = F.length
celem = F.elem
cmaximum = F.maximum
cminimum = F.minimum
csum = F.sum
cproduct = F.product
instance CFoldable (Either a) where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
{-# INLINE ctoList #-}
{-# INLINE cnull #-}
{-# INLINE clength #-}
{-# INLINE celem #-}
{-# INLINE cmaximum #-}
{-# INLINE cminimum #-}
{-# INLINE csum #-}
{-# INLINE cproduct #-}
cfold = F.fold
cfoldMap = F.foldMap
cfoldr = F.foldr
cfoldr' = F.foldr'
cfoldl = F.foldl
cfoldl' = F.foldl'
cfoldr1 = F.foldr1
cfoldl1 = F.foldl1
ctoList = F.toList
cnull = F.null
clength = F.length
celem = F.elem
cmaximum = F.maximum
cminimum = F.minimum
csum = F.sum
cproduct = F.product
instance CFoldable (Const a) where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
{-# INLINE ctoList #-}
{-# INLINE cnull #-}
{-# INLINE clength #-}
{-# INLINE celem #-}
{-# INLINE cmaximum #-}
{-# INLINE cminimum #-}
{-# INLINE csum #-}
{-# INLINE cproduct #-}
cfold = F.fold
cfoldMap = F.foldMap
cfoldr = F.foldr
cfoldr' = F.foldr'
cfoldl = F.foldl
cfoldl' = F.foldl'
cfoldr1 = F.foldr1
cfoldl1 = F.foldl1
ctoList = F.toList
cnull = F.null
clength = F.length
celem = F.elem
cmaximum = F.maximum
cminimum = F.minimum
csum = F.sum
cproduct = F.product
instance CFoldable ZipList where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
{-# INLINE ctoList #-}
{-# INLINE cnull #-}
{-# INLINE clength #-}
{-# INLINE celem #-}
{-# INLINE cmaximum #-}
{-# INLINE cminimum #-}
{-# INLINE csum #-}
{-# INLINE cproduct #-}
cfold = F.fold
cfoldMap = F.foldMap
cfoldr = F.foldr
cfoldr' = F.foldr'
cfoldl = F.foldl
cfoldl' = F.foldl'
cfoldr1 = F.foldr1
cfoldl1 = F.foldl1
ctoList = F.toList
cnull = F.null
clength = F.length
celem = F.elem
cmaximum = F.maximum
cminimum = F.minimum
csum = F.sum
cproduct = F.product
instance CFoldable Semigroup.Min where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
{-# INLINE ctoList #-}
{-# INLINE cnull #-}
{-# INLINE clength #-}
{-# INLINE celem #-}
{-# INLINE cmaximum #-}
{-# INLINE cminimum #-}
{-# INLINE csum #-}
{-# INLINE cproduct #-}
cfold = F.fold
cfoldMap = F.foldMap
cfoldr = F.foldr
cfoldr' = F.foldr'
cfoldl = F.foldl
cfoldl' = F.foldl'
cfoldr1 = F.foldr1
cfoldl1 = F.foldl1
ctoList = F.toList
cnull = F.null
clength = F.length
celem = F.elem
cmaximum = F.maximum
cminimum = F.minimum
csum = F.sum
cproduct = F.product
instance CFoldable Semigroup.Max where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
{-# INLINE ctoList #-}
{-# INLINE cnull #-}
{-# INLINE clength #-}
{-# INLINE celem #-}
{-# INLINE cmaximum #-}
{-# INLINE cminimum #-}
{-# INLINE csum #-}
{-# INLINE cproduct #-}
cfold = F.fold
cfoldMap = F.foldMap
cfoldr = F.foldr
cfoldr' = F.foldr'
cfoldl = F.foldl
cfoldl' = F.foldl'
cfoldr1 = F.foldr1
cfoldl1 = F.foldl1
ctoList = F.toList
cnull = F.null
clength = F.length
celem = F.elem
cmaximum = F.maximum
cminimum = F.minimum
csum = F.sum
cproduct = F.product
instance CFoldable Semigroup.First where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
{-# INLINE ctoList #-}
{-# INLINE cnull #-}
{-# INLINE clength #-}
{-# INLINE celem #-}
{-# INLINE cmaximum #-}
{-# INLINE cminimum #-}
{-# INLINE csum #-}
{-# INLINE cproduct #-}
cfold = F.fold
cfoldMap = F.foldMap
cfoldr = F.foldr
cfoldr' = F.foldr'
cfoldl = F.foldl
cfoldl' = F.foldl'
cfoldr1 = F.foldr1
cfoldl1 = F.foldl1
ctoList = F.toList
cnull = F.null
clength = F.length
celem = F.elem
cmaximum = F.maximum
cminimum = F.minimum
csum = F.sum
cproduct = F.product
instance CFoldable Semigroup.Last where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
{-# INLINE ctoList #-}
{-# INLINE cnull #-}
{-# INLINE clength #-}
{-# INLINE celem #-}
{-# INLINE cmaximum #-}
{-# INLINE cminimum #-}
{-# INLINE csum #-}
{-# INLINE cproduct #-}
cfold = F.fold
cfoldMap = F.foldMap
cfoldr = F.foldr
cfoldr' = F.foldr'
cfoldl = F.foldl
cfoldl' = F.foldl'
cfoldr1 = F.foldr1
cfoldl1 = F.foldl1
ctoList = F.toList
cnull = F.null
clength = F.length
celem = F.elem
cmaximum = F.maximum
cminimum = F.minimum
csum = F.sum
cproduct = F.product
instance CFoldable Semigroup.Dual where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
{-# INLINE ctoList #-}
{-# INLINE cnull #-}
{-# INLINE clength #-}
{-# INLINE celem #-}
{-# INLINE cmaximum #-}
{-# INLINE cminimum #-}
{-# INLINE csum #-}
{-# INLINE cproduct #-}
cfold = F.fold
cfoldMap = F.foldMap
cfoldr = F.foldr
cfoldr' = F.foldr'
cfoldl = F.foldl
cfoldl' = F.foldl'
cfoldr1 = F.foldr1
cfoldl1 = F.foldl1
ctoList = F.toList
cnull = F.null
clength = F.length
celem = F.elem
cmaximum = F.maximum
cminimum = F.minimum
csum = F.sum
cproduct = F.product
instance CFoldable Semigroup.Sum where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
{-# INLINE ctoList #-}
{-# INLINE cnull #-}
{-# INLINE clength #-}
{-# INLINE celem #-}
{-# INLINE cmaximum #-}
{-# INLINE cminimum #-}
{-# INLINE csum #-}
{-# INLINE cproduct #-}
cfold = F.fold
cfoldMap = F.foldMap
cfoldr = F.foldr
cfoldr' = F.foldr'
cfoldl = F.foldl
cfoldl' = F.foldl'
cfoldr1 = F.foldr1
cfoldl1 = F.foldl1
ctoList = F.toList
cnull = F.null
clength = F.length
celem = F.elem
cmaximum = F.maximum
cminimum = F.minimum
csum = F.sum
cproduct = F.product
instance CFoldable Semigroup.Product where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
{-# INLINE ctoList #-}
{-# INLINE cnull #-}
{-# INLINE clength #-}
{-# INLINE celem #-}
{-# INLINE cmaximum #-}
{-# INLINE cminimum #-}
{-# INLINE csum #-}
{-# INLINE cproduct #-}
cfold = F.fold
cfoldMap = F.foldMap
cfoldr = F.foldr
cfoldr' = F.foldr'
cfoldl = F.foldl
cfoldl' = F.foldl'
cfoldr1 = F.foldr1
cfoldl1 = F.foldl1
ctoList = F.toList
cnull = F.null
clength = F.length
celem = F.elem
cmaximum = F.maximum
cminimum = F.minimum
csum = F.sum
cproduct = F.product
#if MIN_VERSION_base(4,12,0)
instance CFoldable f => CFoldable (Monoid.Ap f) where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
{-# INLINE ctoList #-}
{-# INLINE cnull #-}
{-# INLINE clength #-}
{-# INLINE celem #-}
{-# INLINE cmaximum #-}
{-# INLINE cminimum #-}
{-# INLINE csum #-}
{-# INLINE cproduct #-}
cfold :: forall a. (Monoid a, Constraints (Monoid.Ap f) a) => Monoid.Ap f a -> a
cfold = coerce (cfold :: f a -> a)
cfoldMap :: forall a b. (Monoid b, Constraints (Monoid.Ap f) a) => (a -> b) -> Monoid.Ap f a -> b
cfoldMap = coerce (cfoldMap :: (a -> b) -> f a -> b)
cfoldr :: forall a b. Constraints (Monoid.Ap f) a => (a -> b -> b) -> b -> Monoid.Ap f a -> b
cfoldr = coerce (cfoldr :: (a -> b -> b) -> b -> f a -> b)
cfoldr' :: forall a b. Constraints (Monoid.Ap f) a => (a -> b -> b) -> b -> Monoid.Ap f a -> b
cfoldr' = coerce (cfoldr' :: (a -> b -> b) -> b -> f a -> b)
cfoldl :: forall a b. Constraints (Monoid.Ap f) a => (b -> a -> b) -> b -> Monoid.Ap f a -> b
cfoldl = coerce (cfoldl :: (b -> a -> b) -> b -> f a -> b)
cfoldl' :: forall a b. Constraints (Monoid.Ap f) a => (b -> a -> b) -> b -> Monoid.Ap f a -> b
cfoldl' = coerce (cfoldl' :: (b -> a -> b) -> b -> f a -> b)
cfoldr1 :: forall a. Constraints (Monoid.Ap f) a => (a -> a -> a) -> Monoid.Ap f a -> a
cfoldr1 = coerce (cfoldr1 :: (a -> a -> a) -> f a -> a)
cfoldl1 :: forall a. Constraints (Monoid.Ap f) a => (a -> a -> a) -> Monoid.Ap f a -> a
cfoldl1 = coerce (cfoldl1 :: (a -> a -> a) -> f a -> a)
ctoList :: forall a. Constraints (Monoid.Ap f) a => Monoid.Ap f a -> [a]
ctoList = coerce (ctoList :: f a -> [a])
cnull :: forall a. Constraints (Monoid.Ap f) a => Monoid.Ap f a -> Bool
cnull = coerce (cnull :: f a -> Bool)
clength :: forall a. Constraints (Monoid.Ap f) a => Monoid.Ap f a -> Int
clength = coerce (clength :: f a -> Int)
celem :: forall a. (Eq a, Constraints (Monoid.Ap f) a) => a -> Monoid.Ap f a -> Bool
celem = coerce (celem :: a -> f a -> Bool)
cmaximum :: forall a. (Ord a, Constraints (Monoid.Ap f) a) => Monoid.Ap f a -> a
cmaximum = coerce (cmaximum :: f a -> a)
cminimum :: forall a. (Ord a, Constraints (Monoid.Ap f) a) => Monoid.Ap f a -> a
cminimum = coerce (cminimum :: f a -> a)
csum :: forall a. (Num a, Constraints (Monoid.Ap f) a) => Monoid.Ap f a -> a
csum = coerce (csum :: f a -> a)
cproduct :: forall a. (Num a, Constraints (Monoid.Ap f) a) => Monoid.Ap f a -> a
cproduct = coerce (cproduct :: f a -> a)
#endif
instance CFoldable f => CFoldable (Monoid.Alt f) where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
{-# INLINE ctoList #-}
{-# INLINE cnull #-}
{-# INLINE clength #-}
{-# INLINE celem #-}
{-# INLINE cmaximum #-}
{-# INLINE cminimum #-}
{-# INLINE csum #-}
{-# INLINE cproduct #-}
cfold :: forall a. (Monoid a, Constraints (Monoid.Alt f) a) => Monoid.Alt f a -> a
cfold = coerce (cfold :: f a -> a)
cfoldMap :: forall a b. (Monoid b, Constraints (Monoid.Alt f) a) => (a -> b) -> Monoid.Alt f a -> b
cfoldMap = coerce (cfoldMap :: (a -> b) -> f a -> b)
cfoldr :: forall a b. Constraints (Monoid.Alt f) a => (a -> b -> b) -> b -> Monoid.Alt f a -> b
cfoldr = coerce (cfoldr :: (a -> b -> b) -> b -> f a -> b)
cfoldr' :: forall a b. Constraints (Monoid.Alt f) a => (a -> b -> b) -> b -> Monoid.Alt f a -> b
cfoldr' = coerce (cfoldr' :: (a -> b -> b) -> b -> f a -> b)
cfoldl :: forall a b. Constraints (Monoid.Alt f) a => (b -> a -> b) -> b -> Monoid.Alt f a -> b
cfoldl = coerce (cfoldl :: (b -> a -> b) -> b -> f a -> b)
cfoldl' :: forall a b. Constraints (Monoid.Alt f) a => (b -> a -> b) -> b -> Monoid.Alt f a -> b
cfoldl' = coerce (cfoldl' :: (b -> a -> b) -> b -> f a -> b)
cfoldr1 :: forall a. Constraints (Monoid.Alt f) a => (a -> a -> a) -> Monoid.Alt f a -> a
cfoldr1 = coerce (cfoldr1 :: (a -> a -> a) -> f a -> a)
cfoldl1 :: forall a. Constraints (Monoid.Alt f) a => (a -> a -> a) -> Monoid.Alt f a -> a
cfoldl1 = coerce (cfoldl1 :: (a -> a -> a) -> f a -> a)
ctoList :: forall a. Constraints (Monoid.Alt f) a => Monoid.Alt f a -> [a]
ctoList = coerce (ctoList :: f a -> [a])
cnull :: forall a. Constraints (Monoid.Alt f) a => Monoid.Alt f a -> Bool
cnull = coerce (cnull :: f a -> Bool)
clength :: forall a. Constraints (Monoid.Alt f) a => Monoid.Alt f a -> Int
clength = coerce (clength :: f a -> Int)
celem :: forall a. (Eq a, Constraints (Monoid.Alt f) a) => a -> Monoid.Alt f a -> Bool
celem = coerce (celem :: a -> f a -> Bool)
cmaximum :: forall a. (Ord a, Constraints (Monoid.Alt f) a) => Monoid.Alt f a -> a
cmaximum = coerce (cmaximum :: f a -> a)
cminimum :: forall a. (Ord a, Constraints (Monoid.Alt f) a) => Monoid.Alt f a -> a
cminimum = coerce (cminimum :: f a -> a)
csum :: forall a. (Num a, Constraints (Monoid.Alt f) a) => Monoid.Alt f a -> a
csum = coerce (csum :: f a -> a)
cproduct :: forall a. (Num a, Constraints (Monoid.Alt f) a) => Monoid.Alt f a -> a
cproduct = coerce (cproduct :: f a -> a)
instance (CFoldable f, CFoldable g) => CFoldable (Compose f g) where
{-# INLINABLE cfold #-}
{-# INLINABLE cfoldMap #-}
{-# INLINABLE cfoldr #-}
cfold = cfoldMap cfold . getCompose
cfoldMap f = cfoldMap (cfoldMap f) . getCompose
cfoldr f z = cfoldr (\ga acc -> cfoldr f acc ga) z . getCompose
instance (CFoldable f, CFoldable g) => CFoldable (Product.Product f g) where
{-# INLINABLE cfold #-}
{-# INLINABLE cfoldMap #-}
{-# INLINABLE cfoldr #-}
{-# INLINABLE cfoldr' #-}
{-# INLINABLE cfoldl #-}
{-# INLINABLE cfoldl' #-}
{-# INLINABLE cfoldr1 #-}
{-# INLINABLE cfoldl1 #-}
cfold (Pair x y) = cfold x <> cfold y
cfoldMap f (Pair x y) = cfoldMap f x <> cfoldMap f y
cfoldr f z (Pair x y) = cfoldr f (cfoldr f z y) x
cfoldr' f z (Pair x y) = cfoldr' f y' x
where
!y' = cfoldr' f z y
cfoldl f z (Pair x y) = cfoldl f (cfoldl f z y) x
cfoldl' f z (Pair x y) = cfoldl' f x' x
where
!x' = cfoldl' f z y
cfoldr1 f (Pair x y) = cfoldl1 f x `f` cfoldl1 f y
cfoldl1 f (Pair x y) = cfoldr1 f y `f` cfoldr1 f x
instance (CFoldable f, CFoldable g) => CFoldable (Sum.Sum f g) where
{-# INLINE cfold #-}
{-# INLINE cfoldMap #-}
{-# INLINE cfoldr #-}
{-# INLINE cfoldr' #-}
{-# INLINE cfoldl #-}
{-# INLINE cfoldl' #-}
{-# INLINE cfoldr1 #-}
{-# INLINE cfoldl1 #-}
cfold (InL x) = cfold x
cfold (InR y) = cfold y
cfoldMap f (InL x) = cfoldMap f x
cfoldMap f (InR y) = cfoldMap f y
cfoldr f z (InL x) = cfoldr f z x
cfoldr f z (InR y) = cfoldr f z y
cfoldr' f z (InL x) = cfoldr' f z x
cfoldr' f z (InR y) = cfoldr' f z y
cfoldl f z (InL x) = cfoldl f z x
cfoldl f z (InR y) = cfoldl f z y
cfoldl' f z (InL x) = cfoldl' f z x
cfoldl' f z (InR y) = cfoldl' f z y
cfoldr1 f (InL x) = cfoldr1 f x
cfoldr1 f (InR y) = cfoldr1 f y
cfoldl1 f (InL x) = cfoldl1 f x
cfoldl1 f (InR y) = cfoldl1 f y