{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeSynonymInstances #-}

#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE DefaultSignatures #-}
#endif

#if __GLASGOW_HASKELL__ >= 705
{-# LANGUAGE PolyKinds #-}
#endif

#if __GLASGOW_HASKELL__ >= 710
{-# LANGUAGE Safe #-}
#elif __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif

module Generics.Deriving.Foldable (
  -- * Generic Foldable class
    GFoldable(..)

  -- * Default method
  , gfoldMapdefault

  -- * Derived functions
  , gtoList
  , gconcat
  , gconcatMap
  , gand
  , gor
  , gany
  , gall
  , gsum
  , gproduct
  , gmaximum
  , gmaximumBy
  , gminimum
  , gminimumBy
  , gelem
  , gnotElem
  , gfind

  -- * Internal Foldable class
  , GFoldable'(..)
  ) where

import           Control.Applicative (Const, ZipList)

import           Data.Maybe
import qualified Data.Monoid as Monoid (First, Last, Product(..), Sum(..))
import           Data.Monoid (All(..), Any(..), Dual(..), Endo(..))
#if !(MIN_VERSION_base(4,8,0))
import           Data.Monoid (Monoid(..))
#endif

import           Generics.Deriving.Base

#if MIN_VERSION_base(4,4,0)
import           Data.Complex (Complex)
#endif

#if MIN_VERSION_base(4,6,0)
import           Data.Ord (Down)
#else
import           GHC.Exts (Down)
#endif

#if MIN_VERSION_base(4,7,0)
import           Data.Proxy (Proxy)
#endif

#if MIN_VERSION_base(4,8,0)
import           Data.Functor.Identity (Identity)
#endif

#if MIN_VERSION_base(4,9,0)
import qualified Data.Functor.Product as Functor (Product)
import qualified Data.Functor.Sum as Functor (Sum)
import           Data.List.NonEmpty (NonEmpty)
import qualified Data.Semigroup as Semigroup (First, Last)
import           Data.Semigroup (Arg, Max, Min, WrappedMonoid)
#endif

--------------------------------------------------------------------------------
-- Generic fold
--------------------------------------------------------------------------------

class GFoldable' t where
  gfoldMap' :: Monoid m => (a -> m) -> t a -> m

instance GFoldable' V1 where
  gfoldMap' :: forall m a. Monoid m => (a -> m) -> V1 a -> m
gfoldMap' a -> m
_ V1 a
_ = forall a. Monoid a => a
mempty

instance GFoldable' U1 where
  gfoldMap' :: forall m a. Monoid m => (a -> m) -> U1 a -> m
gfoldMap' a -> m
_ U1 a
U1 = forall a. Monoid a => a
mempty

instance GFoldable' Par1 where
  gfoldMap' :: forall m a. Monoid m => (a -> m) -> Par1 a -> m
gfoldMap' a -> m
f (Par1 a
a) = a -> m
f a
a

instance GFoldable' (K1 i c) where
  gfoldMap' :: forall m a. Monoid m => (a -> m) -> K1 i c a -> m
gfoldMap' a -> m
_ (K1 c
_) = forall a. Monoid a => a
mempty

instance (GFoldable f) => GFoldable' (Rec1 f) where
  gfoldMap' :: forall m a. Monoid m => (a -> m) -> Rec1 f a -> m
gfoldMap' a -> m
f (Rec1 f a
a) = forall (t :: * -> *) m a.
(GFoldable t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap a -> m
f f a
a

instance (GFoldable' f) => GFoldable' (M1 i c f) where
  gfoldMap' :: forall m a. Monoid m => (a -> m) -> M1 i c f a -> m
gfoldMap' a -> m
f (M1 f a
a) = forall (t :: * -> *) m a.
(GFoldable' t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap' a -> m
f f a
a

instance (GFoldable' f, GFoldable' g) => GFoldable' (f :+: g) where
  gfoldMap' :: forall m a. Monoid m => (a -> m) -> (:+:) f g a -> m
gfoldMap' a -> m
f (L1 f a
a) = forall (t :: * -> *) m a.
(GFoldable' t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap' a -> m
f f a
a
  gfoldMap' a -> m
f (R1 g a
a) = forall (t :: * -> *) m a.
(GFoldable' t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap' a -> m
f g a
a

instance (GFoldable' f, GFoldable' g) => GFoldable' (f :*: g) where
  gfoldMap' :: forall m a. Monoid m => (a -> m) -> (:*:) f g a -> m
gfoldMap' a -> m
f (f a
a :*: g a
b) = forall a. Monoid a => a -> a -> a
mappend (forall (t :: * -> *) m a.
(GFoldable' t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap' a -> m
f f a
a) (forall (t :: * -> *) m a.
(GFoldable' t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap' a -> m
f g a
b)

instance (GFoldable f, GFoldable' g) => GFoldable' (f :.: g) where
  gfoldMap' :: forall m a. Monoid m => (a -> m) -> (:.:) f g a -> m
gfoldMap' a -> m
f (Comp1 f (g a)
x) = forall (t :: * -> *) m a.
(GFoldable t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap (forall (t :: * -> *) m a.
(GFoldable' t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap' a -> m
f) f (g a)
x

instance GFoldable' UAddr where
  gfoldMap' :: forall m a. Monoid m => (a -> m) -> UAddr a -> m
gfoldMap' a -> m
_ (UAddr Addr#
_) = forall a. Monoid a => a
mempty

instance GFoldable' UChar where
  gfoldMap' :: forall m a. Monoid m => (a -> m) -> UChar a -> m
gfoldMap' a -> m
_ (UChar Char#
_) = forall a. Monoid a => a
mempty

instance GFoldable' UDouble where
  gfoldMap' :: forall m a. Monoid m => (a -> m) -> UDouble a -> m
gfoldMap' a -> m
_ (UDouble Double#
_) = forall a. Monoid a => a
mempty

instance GFoldable' UFloat where
  gfoldMap' :: forall m a. Monoid m => (a -> m) -> UFloat a -> m
gfoldMap' a -> m
_ (UFloat Float#
_) = forall a. Monoid a => a
mempty

instance GFoldable' UInt where
  gfoldMap' :: forall m a. Monoid m => (a -> m) -> UInt a -> m
gfoldMap' a -> m
_ (UInt Int#
_) = forall a. Monoid a => a
mempty

instance GFoldable' UWord where
  gfoldMap' :: forall m a. Monoid m => (a -> m) -> UWord a -> m
gfoldMap' a -> m
_ (UWord Word#
_) = forall a. Monoid a => a
mempty

class GFoldable t where
  gfoldMap :: Monoid m => (a -> m) -> t a -> m
#if __GLASGOW_HASKELL__ >= 701
  default gfoldMap :: (Generic1 t, GFoldable' (Rep1 t), Monoid m)
                   => (a -> m) -> t a -> m
  gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault
#endif

  gfold :: Monoid m => t m -> m
  gfold = forall (t :: * -> *) m a.
(GFoldable t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap forall a. a -> a
id

  gfoldr :: (a -> b -> b) -> b -> t a -> b
  gfoldr a -> b -> b
f b
z t a
t = forall a. Endo a -> a -> a
appEndo (forall (t :: * -> *) m a.
(GFoldable t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap (forall a. (a -> a) -> Endo a
Endo forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b -> b
f) t a
t) b
z

  gfoldr' :: (a -> b -> b) -> b -> t a -> b
  gfoldr' a -> b -> b
f b
z0 t a
xs = forall (t :: * -> *) a b.
GFoldable t =>
(a -> b -> a) -> a -> t b -> a
gfoldl forall {b}. (b -> b) -> a -> b -> b
f' forall a. a -> a
id t a
xs b
z0
    where f' :: (b -> b) -> a -> b -> b
f' b -> b
k a
x b
z = b -> b
k forall a b. (a -> b) -> a -> b
$! a -> b -> b
f a
x b
z

  gfoldl :: (a -> b -> a) -> a -> t b -> a
  gfoldl a -> b -> a
f a
z t b
t = forall a. Endo a -> a -> a
appEndo (forall a. Dual a -> a
getDual (forall (t :: * -> *) m a.
(GFoldable t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap (forall a. a -> Dual a
Dual forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a) -> Endo a
Endo forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> b -> a
f) t b
t)) a
z

  gfoldl' :: (a -> b -> a) -> a -> t b -> a
  gfoldl' a -> b -> a
f a
z0 t b
xs = forall (t :: * -> *) a b.
GFoldable t =>
(a -> b -> b) -> b -> t a -> b
gfoldr forall {b}. b -> (a -> b) -> a -> b
f' forall a. a -> a
id t b
xs a
z0
    where f' :: b -> (a -> b) -> a -> b
f' b
x a -> b
k a
z = a -> b
k forall a b. (a -> b) -> a -> b
$! a -> b -> a
f a
z b
x

  gfoldr1 :: (a -> a -> a) -> t a -> a
  gfoldr1 a -> a -> a
f t a
xs = forall a. a -> Maybe a -> a
fromMaybe (forall a. HasCallStack => [Char] -> a
error [Char]
"gfoldr1: empty structure")
                   (forall (t :: * -> *) a b.
GFoldable t =>
(a -> b -> b) -> b -> t a -> b
gfoldr a -> Maybe a -> Maybe a
mf forall a. Maybe a
Nothing t a
xs)
    where
      mf :: a -> Maybe a -> Maybe a
mf a
x Maybe a
Nothing = forall a. a -> Maybe a
Just a
x
      mf a
x (Just a
y) = forall a. a -> Maybe a
Just (a -> a -> a
f a
x a
y)

  gfoldl1 :: (a -> a -> a) -> t a -> a
  gfoldl1 a -> a -> a
f t a
xs = forall a. a -> Maybe a -> a
fromMaybe (forall a. HasCallStack => [Char] -> a
error [Char]
"foldl1: empty structure")
                   (forall (t :: * -> *) a b.
GFoldable t =>
(a -> b -> a) -> a -> t b -> a
gfoldl Maybe a -> a -> Maybe a
mf forall a. Maybe a
Nothing t a
xs)
    where
      mf :: Maybe a -> a -> Maybe a
mf Maybe a
Nothing a
y = forall a. a -> Maybe a
Just a
y
      mf (Just a
x) a
y = forall a. a -> Maybe a
Just (a -> a -> a
f a
x a
y)

gfoldMapdefault :: (Generic1 t, GFoldable' (Rep1 t), Monoid m)
                => (a -> m) -> t a -> m
gfoldMapdefault :: forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault a -> m
f t a
x = forall (t :: * -> *) m a.
(GFoldable' t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap' a -> m
f (forall k (f :: k -> *) (a :: k). Generic1 f => f a -> Rep1 f a
from1 t a
x)

-- Base types instances
instance GFoldable ((,) a) where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> (a, a) -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault

instance GFoldable [] where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> [a] -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault

#if MIN_VERSION_base(4,9,0)
instance GFoldable (Arg a) where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> Arg a a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault
#endif

#if MIN_VERSION_base(4,4,0)
instance GFoldable Complex where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> Complex a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault
#endif

instance GFoldable (Const m) where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> Const m a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault

instance GFoldable Down where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> Down a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault

instance GFoldable Dual where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> Dual a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault

instance GFoldable (Either a) where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> Either a a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault

instance GFoldable Monoid.First where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> First a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault

#if MIN_VERSION_base(4,9,0)
instance GFoldable (Semigroup.First) where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> First a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault
#endif

#if MIN_VERSION_base(4,8,0)
instance GFoldable Identity where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> Identity a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault
#endif

instance GFoldable Monoid.Last where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> Last a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault

#if MIN_VERSION_base(4,9,0)
instance GFoldable Semigroup.Last where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> Last a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault

instance GFoldable Max where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> Max a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault
#endif

instance GFoldable Maybe where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> Maybe a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault

#if MIN_VERSION_base(4,9,0)
instance GFoldable Min where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> Min a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault

instance GFoldable NonEmpty where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault
#endif

instance GFoldable Monoid.Product where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> Product a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault

#if MIN_VERSION_base(4,9,0)
instance (GFoldable f, GFoldable g) => GFoldable (Functor.Product f g) where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> Product f g a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault
#endif

#if MIN_VERSION_base(4,7,0)
instance GFoldable Proxy where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> Proxy a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault
#endif

instance GFoldable Monoid.Sum where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> Sum a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault

#if MIN_VERSION_base(4,9,0)
instance (GFoldable f, GFoldable g) => GFoldable (Functor.Sum f g) where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> Sum f g a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault

instance GFoldable WrappedMonoid where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> WrappedMonoid a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault
#endif

instance GFoldable ZipList where
  gfoldMap :: forall m a. Monoid m => (a -> m) -> ZipList a -> m
gfoldMap = forall (t :: * -> *) m a.
(Generic1 t, GFoldable' (Rep1 t), Monoid m) =>
(a -> m) -> t a -> m
gfoldMapdefault

gtoList :: GFoldable t => t a -> [a]
gtoList :: forall (t :: * -> *) a. GFoldable t => t a -> [a]
gtoList = forall (t :: * -> *) a b.
GFoldable t =>
(a -> b -> b) -> b -> t a -> b
gfoldr (:) []

gconcat :: GFoldable t => t [a] -> [a]
gconcat :: forall (t :: * -> *) a. GFoldable t => t [a] -> [a]
gconcat = forall (t :: * -> *) m. (GFoldable t, Monoid m) => t m -> m
gfold

gconcatMap :: GFoldable t => (a -> [b]) -> t a -> [b]
gconcatMap :: forall (t :: * -> *) a b. GFoldable t => (a -> [b]) -> t a -> [b]
gconcatMap = forall (t :: * -> *) m a.
(GFoldable t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap

gand :: GFoldable t => t Bool -> Bool
gand :: forall (t :: * -> *). GFoldable t => t Bool -> Bool
gand = All -> Bool
getAll forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) m a.
(GFoldable t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap Bool -> All
All

gor :: GFoldable t => t Bool -> Bool
gor :: forall (t :: * -> *). GFoldable t => t Bool -> Bool
gor = Any -> Bool
getAny forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) m a.
(GFoldable t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap Bool -> Any
Any

gany :: GFoldable t => (a -> Bool) -> t a -> Bool
gany :: forall (t :: * -> *) a. GFoldable t => (a -> Bool) -> t a -> Bool
gany a -> Bool
p = Any -> Bool
getAny forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) m a.
(GFoldable t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap (Bool -> Any
Any forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
p)

gall :: GFoldable t => (a -> Bool) -> t a -> Bool
gall :: forall (t :: * -> *) a. GFoldable t => (a -> Bool) -> t a -> Bool
gall a -> Bool
p = All -> Bool
getAll forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) m a.
(GFoldable t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap (Bool -> All
All forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
p)

gsum :: (GFoldable t, Num a) => t a -> a
gsum :: forall (t :: * -> *) a. (GFoldable t, Num a) => t a -> a
gsum = forall a. Sum a -> a
Monoid.getSum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) m a.
(GFoldable t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap forall a. a -> Sum a
Monoid.Sum

gproduct :: (GFoldable t, Num a) => t a -> a
gproduct :: forall (t :: * -> *) a. (GFoldable t, Num a) => t a -> a
gproduct = forall a. Product a -> a
Monoid.getProduct forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) m a.
(GFoldable t, Monoid m) =>
(a -> m) -> t a -> m
gfoldMap forall a. a -> Product a
Monoid.Product

gmaximum :: (GFoldable t, Ord a) => t a -> a
gmaximum :: forall (t :: * -> *) a. (GFoldable t, Ord a) => t a -> a
gmaximum = forall (t :: * -> *) a. GFoldable t => (a -> a -> a) -> t a -> a
gfoldr1 forall a. Ord a => a -> a -> a
max

gmaximumBy :: GFoldable t => (a -> a -> Ordering) -> t a -> a
gmaximumBy :: forall (t :: * -> *) a.
GFoldable t =>
(a -> a -> Ordering) -> t a -> a
gmaximumBy a -> a -> Ordering
cmp = forall (t :: * -> *) a. GFoldable t => (a -> a -> a) -> t a -> a
gfoldr1 a -> a -> a
max'
  where max' :: a -> a -> a
max' a
x a
y = case a -> a -> Ordering
cmp a
x a
y of
                        Ordering
GT -> a
x
                        Ordering
_  -> a
y

gminimum :: (GFoldable t, Ord a) => t a -> a
gminimum :: forall (t :: * -> *) a. (GFoldable t, Ord a) => t a -> a
gminimum = forall (t :: * -> *) a. GFoldable t => (a -> a -> a) -> t a -> a
gfoldr1 forall a. Ord a => a -> a -> a
min

gminimumBy :: GFoldable t => (a -> a -> Ordering) -> t a -> a
gminimumBy :: forall (t :: * -> *) a.
GFoldable t =>
(a -> a -> Ordering) -> t a -> a
gminimumBy a -> a -> Ordering
cmp = forall (t :: * -> *) a. GFoldable t => (a -> a -> a) -> t a -> a
gfoldr1 a -> a -> a
min'
  where min' :: a -> a -> a
min' a
x a
y = case a -> a -> Ordering
cmp a
x a
y of
                        Ordering
GT -> a
y
                        Ordering
_  -> a
x

gelem :: (GFoldable t, Eq a) => a -> t a -> Bool
gelem :: forall (t :: * -> *) a. (GFoldable t, Eq a) => a -> t a -> Bool
gelem = forall (t :: * -> *) a. GFoldable t => (a -> Bool) -> t a -> Bool
gany forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => a -> a -> Bool
(==)

gnotElem :: (GFoldable t, Eq a) => a -> t a -> Bool
gnotElem :: forall (t :: * -> *) a. (GFoldable t, Eq a) => a -> t a -> Bool
gnotElem a
x = Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. (GFoldable t, Eq a) => a -> t a -> Bool
gelem a
x

gfind :: GFoldable t => (a -> Bool) -> t a -> Maybe a
gfind :: forall (t :: * -> *) a.
GFoldable t =>
(a -> Bool) -> t a -> Maybe a
gfind a -> Bool
p = forall a. [a] -> Maybe a
listToMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a b. GFoldable t => (a -> [b]) -> t a -> [b]
gconcatMap (\ a
x -> if a -> Bool
p a
x then [a
x] else [])