-- |
-- Copyright: Edward Kmett, Oleg Grenrus
-- License: BSD-3-Clause
--

{-# LANGUAGE FlexibleInstances          #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NoImplicitPrelude          #-}
{-# LANGUAGE PolyKinds                  #-}
{-# LANGUAGE ScopedTypeVariables        #-}
{-# LANGUAGE StandaloneDeriving         #-}
{-# LANGUAGE Trustworthy                #-}
{-# LANGUAGE TypeOperators              #-}

-- | A class of non-empty data structures that can be folded to a summary value.
module Data.Foldable1 (
    Foldable1(..),
    foldr1, foldr1',
    foldl1, foldl1',
    intercalate1,
    foldrM1,
    foldlM1,
    foldrMapM1,
    foldlMapM1,
    maximumBy,
    minimumBy,
    ) where

import Data.Foldable      (Foldable, foldlM, foldr)
import Data.List          (foldl, foldl')
import Data.List.NonEmpty (NonEmpty (..))
import Data.Semigroup
       (Dual (..), First (..), Last (..), Max (..), Min (..), Product (..),
       Semigroup (..), Sum (..))
import Prelude
       (Maybe (..), Monad (..), Ord, Ordering (..), id, seq, ($!), ($), (.),
       (=<<), flip, const, error)

import qualified Data.List.NonEmpty as NE

import Data.Complex (Complex (..))
import GHC.Generics
       (M1 (..), Par1 (..), Rec1 (..), V1, (:*:) (..), (:+:) (..), (:.:) (..))

import Data.Ord (Down (..))

import qualified Data.Monoid as Mon

-- Instances
import Data.Functor.Compose          (Compose (..))
import Data.Functor.Identity         (Identity (..))

import qualified Data.Functor.Product as Functor
import qualified Data.Functor.Sum     as Functor

-- coerce
import Data.Coerce (Coercible, coerce)

-- $setup
-- >>> import Prelude hiding (foldr1, foldl1, head, last, minimum, maximum)

-------------------------------------------------------------------------------
-- Foldable1 type class
-------------------------------------------------------------------------------

-- | Non-empty data structures that can be folded.
class Foldable t => Foldable1 t where
    {-# MINIMAL foldMap1 | foldrMap1 #-}

    -- At some point during design it was possible to define this class using
    -- only 'toNonEmpty'. But it seems a bad idea in general.
    --
    -- So currently we require either foldMap1 or foldrMap1
    --
    -- * foldMap1 defined using foldrMap1
    -- * foldrMap1 defined using foldMap1
    --
    -- One can always define an instance using the following pattern:
    --
    --     toNonEmpty = ...
    --     foldMap f     = foldMap f     . toNonEmpty
    --     foldrMap1 f g = foldrMap1 f g . toNonEmpty

    -- | Combine the elements of a structure using a semigroup.
    fold1 :: Semigroup m => t m -> m
    fold1 = (m -> m) -> t m -> m
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 m -> m
forall a. a -> a
id

    -- | Map each element of the structure to a semigroup,
    -- and combine the results.
    --
    -- >>> foldMap1 Sum (1 :| [2, 3, 4])
    -- Sum {getSum = 10}
    --
    foldMap1 :: Semigroup m => (a -> m) -> t a -> m
    foldMap1 a -> m
f = (a -> m) -> (a -> m -> m) -> t a -> m
forall a b. (a -> b) -> (a -> b -> b) -> t a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1 a -> m
f (\a
a m
m -> a -> m
f a
a m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
m)

    -- | A variant of 'foldMap1' that is strict in the accumulator.
    --
    -- >>> foldMap1' Sum (1 :| [2, 3, 4])
    -- Sum {getSum = 10}
    --
    foldMap1' :: Semigroup m => (a -> m) -> t a -> m
    foldMap1' a -> m
f = (a -> m) -> (m -> a -> m) -> t a -> m
forall a b. (a -> b) -> (b -> a -> b) -> t a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (b -> a -> b) -> t a -> b
foldlMap1' a -> m
f (\m
m a
a -> m
m m -> m -> m
forall a. Semigroup a => a -> a -> a
<> a -> m
f a
a)

    -- | List of elements of a structure, from left to right.
    --
    -- >>> toNonEmpty (Identity 2)
    -- 2 :| []
    --
    toNonEmpty :: t a -> NonEmpty a
    toNonEmpty = NonEmptyDList a -> NonEmpty a
forall a. NonEmptyDList a -> NonEmpty a
runNonEmptyDList (NonEmptyDList a -> NonEmpty a)
-> (t a -> NonEmptyDList a) -> t a -> NonEmpty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> NonEmptyDList a) -> t a -> NonEmptyDList a
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> NonEmptyDList a
forall a. a -> NonEmptyDList a
singleton

    -- | The largest element of a non-empty structure.
    --
    -- >>> maximum (32 :| [64, 8, 128, 16])
    -- 128
    --
    maximum :: Ord a => t a -> a
    maximum = Max a -> a
forall a. Max a -> a
getMax (Max a -> a) -> (t a -> Max a) -> t a -> a
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (a -> Max a) -> t a -> Max a
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1' a -> Max a
forall a. a -> Max a
Max

    -- | The least element of a non-empty structure.
    --
    -- >>> minimum (32 :| [64, 8, 128, 16])
    -- 8
    --
    minimum :: Ord a => t a -> a
    minimum = Min a -> a
forall a. Min a -> a
getMin (Min a -> a) -> (t a -> Min a) -> t a -> a
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (a -> Min a) -> t a -> Min a
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1' a -> Min a
forall a. a -> Min a
Min

    -- | The first element of a non-empty structure.
    --
    -- >>> head (1 :| [2, 3, 4])
    -- 1
    --
    head :: t a -> a
    head = First a -> a
forall a. First a -> a
getFirst (First a -> a) -> (t a -> First a) -> t a -> a
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (a -> First a) -> t a -> First a
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> First a
forall a. a -> First a
First

    -- | The last element of a non-empty structure.
    --
    -- >>> last (1 :| [2, 3, 4])
    -- 4
    --
    last :: t a -> a
    last = Last a -> a
forall a. Last a -> a
getLast (Last a -> a) -> (t a -> Last a) -> t a -> a
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. (a -> Last a) -> t a -> Last a
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> Last a
forall a. a -> Last a
Last

    -- | Generalized 'foldr1'.
    foldrMap1 :: (a -> b) -> (a -> b -> b) -> t a -> b
    foldrMap1 a -> b
f a -> b -> b
g t a
xs =
        FromMaybe b -> Maybe b -> b
forall b. FromMaybe b -> Maybe b -> b
appFromMaybe ((a -> FromMaybe b) -> t a -> FromMaybe b
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 ((Maybe b -> b) -> FromMaybe b
forall b. (Maybe b -> b) -> FromMaybe b
FromMaybe ((Maybe b -> b) -> FromMaybe b)
-> (a -> Maybe b -> b) -> a -> FromMaybe b
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. a -> Maybe b -> b
h) t a
xs) Maybe b
forall a. Maybe a
Nothing
      where
        h :: a -> Maybe b -> b
h a
a Maybe b
Nothing  = a -> b
f a
a
        h a
a (Just b
b) = a -> b -> b
g a
a b
b

    -- | Generalized 'foldl1''.
    foldlMap1' :: (a -> b) -> (b -> a -> b) -> t a -> b
    foldlMap1' a -> b
f b -> a -> b
g t a
xs =
        (a -> SMaybe b -> b)
-> (a -> (SMaybe b -> b) -> SMaybe b -> b) -> t a -> SMaybe b -> b
forall a b. (a -> b) -> (a -> b -> b) -> t a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1 a -> SMaybe b -> b
f' a -> (SMaybe b -> b) -> SMaybe b -> b
forall {b}. a -> (SMaybe b -> b) -> SMaybe b -> b
g' t a
xs SMaybe b
forall a. SMaybe a
SNothing
      where
        -- f' :: a -> SMaybe b -> b
        f' :: a -> SMaybe b -> b
f' a
a SMaybe b
SNothing  = a -> b
f a
a
        f' a
a (SJust b
b) = b -> a -> b
g b
b a
a

        -- g' :: a -> (SMaybe b -> b) -> SMaybe b -> b
        g' :: a -> (SMaybe b -> b) -> SMaybe b -> b
g' a
a SMaybe b -> b
x SMaybe b
SNothing  = SMaybe b -> b
x (SMaybe b -> b) -> SMaybe b -> b
forall a b. (a -> b) -> a -> b
$! b -> SMaybe b
forall a. a -> SMaybe a
SJust (a -> b
f a
a)
        g' a
a SMaybe b -> b
x (SJust b
b) = SMaybe b -> b
x (SMaybe b -> b) -> SMaybe b -> b
forall a b. (a -> b) -> a -> b
$! b -> SMaybe b
forall a. a -> SMaybe a
SJust (b -> a -> b
g b
b a
a)

    -- | Generalized 'foldl1'.
    foldlMap1 :: (a -> b) -> (b -> a -> b) -> t a -> b
    foldlMap1 a -> b
f b -> a -> b
g t a
xs =
        FromMaybe b -> Maybe b -> b
forall b. FromMaybe b -> Maybe b -> b
appFromMaybe (Dual (FromMaybe b) -> FromMaybe b
forall a. Dual a -> a
getDual ((a -> Dual (FromMaybe b)) -> t a -> Dual (FromMaybe b)
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 ((FromMaybe b -> Dual (FromMaybe b)
forall a. a -> Dual a
Dual (FromMaybe b -> Dual (FromMaybe b))
-> ((Maybe b -> b) -> FromMaybe b)
-> (Maybe b -> b)
-> Dual (FromMaybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe b -> b) -> FromMaybe b
forall b. (Maybe b -> b) -> FromMaybe b
FromMaybe) ((Maybe b -> b) -> Dual (FromMaybe b))
-> (a -> Maybe b -> b) -> a -> Dual (FromMaybe b)
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. a -> Maybe b -> b
h) t a
xs)) Maybe b
forall a. Maybe a
Nothing
      where
        h :: a -> Maybe b -> b
h a
a Maybe b
Nothing  = a -> b
f a
a
        h a
a (Just b
b) = b -> a -> b
g b
b a
a

    -- | Generalized 'foldr1''.
    foldrMap1' :: (a -> b) -> (a -> b -> b) -> t a -> b
    foldrMap1' a -> b
f a -> b -> b
g t a
xs =
        (a -> SMaybe b -> b)
-> ((SMaybe b -> b) -> a -> SMaybe b -> b) -> t a -> SMaybe b -> b
forall a b. (a -> b) -> (b -> a -> b) -> t a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (b -> a -> b) -> t a -> b
foldlMap1 a -> SMaybe b -> b
f' (SMaybe b -> b) -> a -> SMaybe b -> b
forall {b}. (SMaybe b -> b) -> a -> SMaybe b -> b
g' t a
xs SMaybe b
forall a. SMaybe a
SNothing
      where
        f' :: a -> SMaybe b -> b
f' a
a SMaybe b
SNothing  = a -> b
f a
a
        f' a
a (SJust b
b) = a -> b -> b
g a
a b
b

        g' :: (SMaybe b -> b) -> a -> SMaybe b -> b
g' SMaybe b -> b
bb a
a SMaybe b
SNothing  = SMaybe b -> b
bb (SMaybe b -> b) -> SMaybe b -> b
forall a b. (a -> b) -> a -> b
$! b -> SMaybe b
forall a. a -> SMaybe a
SJust (a -> b
f a
a)
        g' SMaybe b -> b
bb a
a (SJust b
b) = SMaybe b -> b
bb (SMaybe b -> b) -> SMaybe b -> b
forall a b. (a -> b) -> a -> b
$! b -> SMaybe b
forall a. a -> SMaybe a
SJust (a -> b -> b
g a
a b
b)

-------------------------------------------------------------------------------
-- Combinators
-------------------------------------------------------------------------------

-- | Right-associative fold of a structure.
--
-- In the case of lists, 'foldr1', when applied to a binary operator,
-- and a list, reduces the list using the binary operator,
-- from right to left:
--
-- > foldr1 f [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn1 `f` xn )...)
--
-- Note that, since the head of the resulting expression is produced by
-- an application of the operator to the first element of the list,
-- 'foldr1' can produce a terminating expression from an infinite list.
--
-- For a general 'Foldable1' structure this should be semantically identical
-- to,
--
-- @foldr1 f = foldr1 f . 'toNonEmpty'@
--
foldr1 :: Foldable1 t => (a -> a -> a) -> t a -> a
foldr1 :: forall (t :: * -> *) a. Foldable1 t => (a -> a -> a) -> t a -> a
foldr1 = (a -> a) -> (a -> a -> a) -> t a -> a
forall a b. (a -> b) -> (a -> b -> b) -> t a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1 a -> a
forall a. a -> a
id
{-# INLINE foldr1 #-}

-- | Right-associative fold of a structure, but with strict application of
-- the operator.
--
foldr1' :: Foldable1 t => (a -> a -> a) -> t a -> a
foldr1' :: forall (t :: * -> *) a. Foldable1 t => (a -> a -> a) -> t a -> a
foldr1' = (a -> a) -> (a -> a -> a) -> t a -> a
forall a b. (a -> b) -> (a -> b -> b) -> t a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1' a -> a
forall a. a -> a
id
{-# INLINE foldr1' #-}

-- | Left-associative fold of a structure.
--
-- In the case of lists, 'foldl1', when applied to a binary
-- operator, and a list, reduces the list using the binary operator,
-- from left to right:
--
-- > foldl1 f [x1, x2, ..., xn] == (...((x1 `f` x2) `f`...) `f` xn
--
-- Note that to produce the outermost application of the operator the
-- entire input list must be traversed. This means that 'foldl1' will
-- diverge if given an infinite list.
--
-- Also note that if you want an efficient left-fold, you probably want to
-- use 'foldl1'' instead of 'foldl1'. The reason for this is that latter does
-- not force the "inner" results (e.g. @x1 \`f\` x2@ in the above example)
-- before applying them to the operator (e.g. to @(\`f\` x3)@). This results
-- in a thunk chain \(\mathcal{O}(n)\) elements long, which then must be
-- evaluated from the outside-in.
--
-- For a general 'Foldable1' structure this should be semantically identical
-- to,
--
-- @foldl1 f z = foldl1 f . 'toNonEmpty'@
--
foldl1 :: Foldable1 t => (a -> a -> a) -> t a -> a
foldl1 :: forall (t :: * -> *) a. Foldable1 t => (a -> a -> a) -> t a -> a
foldl1 = (a -> a) -> (a -> a -> a) -> t a -> a
forall a b. (a -> b) -> (b -> a -> b) -> t a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (b -> a -> b) -> t a -> b
foldlMap1 a -> a
forall a. a -> a
id
{-# INLINE foldl1 #-}

-- | Left-associative fold of a structure but with strict application of
-- the operator.
--
-- This ensures that each step of the fold 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
-- list to a single, monolithic result (e.g. 'length').
--
-- For a general 'Foldable1' structure this should be semantically identical
-- to,
--
-- @foldl1' f z = foldl1 f . 'toNonEmpty'@
--
foldl1' :: Foldable1 t => (a -> a -> a) -> t a -> a
foldl1' :: forall (t :: * -> *) a. Foldable1 t => (a -> a -> a) -> t a -> a
foldl1' = (a -> a) -> (a -> a -> a) -> t a -> a
forall a b. (a -> b) -> (b -> a -> b) -> t a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (b -> a -> b) -> t a -> b
foldlMap1' a -> a
forall a. a -> a
id
{-# INLINE foldl1' #-}

-- | Insert an @m@ between each pair of @t m@.
--
-- >>> intercalate1 ", " $ "hello" :| ["how", "are", "you"]
-- "hello, how, are, you"
--
-- >>> intercalate1 ", " $ "hello" :| []
-- "hello"
--
-- >>> intercalate1 mempty $ "I" :| ["Am", "Fine", "You?"]
-- "IAmFineYou?"
--
intercalate1 :: (Foldable1 t, Semigroup m) => m -> t m -> m
intercalate1 :: forall (t :: * -> *) m. (Foldable1 t, Semigroup m) => m -> t m -> m
intercalate1 = (m -> (m -> m) -> t m -> m) -> (m -> m) -> m -> t m -> m
forall a b c. (a -> b -> c) -> b -> a -> c
flip m -> (m -> m) -> t m -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
m -> (a -> m) -> t a -> m
intercalateMap1 m -> m
forall a. a -> a
id

intercalateMap1 :: (Foldable1 t, Semigroup m) => m -> (a -> m) -> t a -> m
intercalateMap1 :: forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
m -> (a -> m) -> t a -> m
intercalateMap1 m
j a -> m
f = (JoinWith m -> m -> m) -> m -> JoinWith m -> m
forall a b c. (a -> b -> c) -> b -> a -> c
flip JoinWith m -> m -> m
forall a. JoinWith a -> a -> a
joinee m
j (JoinWith m -> m) -> (t a -> JoinWith m) -> t a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> JoinWith m) -> t a -> JoinWith m
forall m a. Semigroup m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 ((m -> m) -> JoinWith m
forall a. (a -> a) -> JoinWith a
JoinWith ((m -> m) -> JoinWith m) -> (a -> m -> m) -> a -> JoinWith m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m -> m -> m
forall a b. a -> b -> a
const (m -> m -> m) -> (a -> m) -> a -> m -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m
f)

-- | Monadic fold over the elements of a non-empty structure,
-- associating to the right, i.e. from right to left.
foldrM1 :: (Foldable1 t, Monad m) => (a -> a -> m a) -> t a -> m a
foldrM1 :: forall (t :: * -> *) (m :: * -> *) a.
(Foldable1 t, Monad m) =>
(a -> a -> m a) -> t a -> m a
foldrM1 = (a -> m a) -> (a -> a -> m a) -> t a -> m a
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable1 t, Monad m) =>
(a -> m b) -> (a -> b -> m b) -> t a -> m b
foldrMapM1 a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return

-- | Map variant of 'foldrM1'.
foldrMapM1 :: (Foldable1 t, Monad m) => (a -> m b) -> (a -> b -> m b) -> t a -> m b
foldrMapM1 :: forall (t :: * -> *) (m :: * -> *) a b.
(Foldable1 t, Monad m) =>
(a -> m b) -> (a -> b -> m b) -> t a -> m b
foldrMapM1 a -> m b
g a -> b -> m b
f = NonEmpty a -> m b
go (NonEmpty a -> m b) -> (t a -> NonEmpty a) -> t a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> NonEmpty a
forall a. t a -> NonEmpty a
forall (t :: * -> *) a. Foldable1 t => t a -> NonEmpty a
toNonEmpty
  where
    go :: NonEmpty a -> m b
go (a
e:|[a]
es) =
      case [a]
es of
        []   -> a -> m b
g a
e
        a
x:[a]
xs -> a -> b -> m b
f a
e (b -> m b) -> m b -> m b
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< NonEmpty a -> m b
go (a
xa -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:|[a]
xs)

-- | Monadic fold over the elements of a non-empty structure,
-- associating to the left, i.e. from left to right.
foldlM1 :: (Foldable1 t, Monad m) => (a -> a -> m a) -> t a -> m a
foldlM1 :: forall (t :: * -> *) (m :: * -> *) a.
(Foldable1 t, Monad m) =>
(a -> a -> m a) -> t a -> m a
foldlM1 = (a -> m a) -> (a -> a -> m a) -> t a -> m a
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable1 t, Monad m) =>
(a -> m b) -> (b -> a -> m b) -> t a -> m b
foldlMapM1 a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return

-- | Map variant of 'foldlM1'.
foldlMapM1 :: (Foldable1 t, Monad m) => (a -> m b) -> (b -> a -> m b) -> t a -> m b
foldlMapM1 :: forall (t :: * -> *) (m :: * -> *) a b.
(Foldable1 t, Monad m) =>
(a -> m b) -> (b -> a -> m b) -> t a -> m b
foldlMapM1 a -> m b
g b -> a -> m b
f t a
t = a -> m b
g a
x m b -> (b -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \b
y -> (b -> a -> m b) -> b -> [a] -> m b
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldlM b -> a -> m b
f b
y [a]
xs
  where a
x:|[a]
xs = t a -> NonEmpty a
forall a. t a -> NonEmpty a
forall (t :: * -> *) a. Foldable1 t => t a -> NonEmpty a
toNonEmpty t a
t

-- | The largest element of a non-empty structure with respect to the
-- given comparison function.
maximumBy :: Foldable1 t => (a -> a -> Ordering) -> t a -> a
maximumBy :: forall (t :: * -> *) a.
Foldable1 t =>
(a -> a -> Ordering) -> t a -> a
maximumBy a -> a -> Ordering
cmp = (a -> a -> a) -> t a -> a
forall (t :: * -> *) a. Foldable1 t => (a -> a -> a) -> t a -> a
foldl1' 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

-- | The least element of a non-empty structure with respect to the
-- given comparison function.
minimumBy :: Foldable1 t => (a -> a -> Ordering) -> t a -> a
minimumBy :: forall (t :: * -> *) a.
Foldable1 t =>
(a -> a -> Ordering) -> t a -> a
minimumBy a -> a -> Ordering
cmp = (a -> a -> a) -> t a -> a
forall (t :: * -> *) a. Foldable1 t => (a -> a -> a) -> t a -> a
foldl1' 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

-------------------------------------------------------------------------------
-- Auxiliary types
-------------------------------------------------------------------------------

-- | Used for default toNonEmpty implementation.
newtype NonEmptyDList a = NEDL { forall a. NonEmptyDList a -> [a] -> NonEmpty a
unNEDL :: [a] -> NonEmpty a }

instance Semigroup (NonEmptyDList a) where
  NonEmptyDList a
xs <> :: NonEmptyDList a -> NonEmptyDList a -> NonEmptyDList a
<> NonEmptyDList a
ys = ([a] -> NonEmpty a) -> NonEmptyDList a
forall a. ([a] -> NonEmpty a) -> NonEmptyDList a
NEDL (NonEmptyDList a -> [a] -> NonEmpty a
forall a. NonEmptyDList a -> [a] -> NonEmpty a
unNEDL NonEmptyDList a
xs ([a] -> NonEmpty a) -> ([a] -> [a]) -> [a] -> NonEmpty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
NE.toList (NonEmpty a -> [a]) -> ([a] -> NonEmpty a) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmptyDList a -> [a] -> NonEmpty a
forall a. NonEmptyDList a -> [a] -> NonEmpty a
unNEDL NonEmptyDList a
ys)
  {-# INLINE (<>) #-}

-- | Create dlist with a single element
singleton :: a -> NonEmptyDList a
singleton :: forall a. a -> NonEmptyDList a
singleton = ([a] -> NonEmpty a) -> NonEmptyDList a
forall a. ([a] -> NonEmpty a) -> NonEmptyDList a
NEDL (([a] -> NonEmpty a) -> NonEmptyDList a)
-> (a -> [a] -> NonEmpty a) -> a -> NonEmptyDList a
forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
(:|)

-- | Convert a dlist to a non-empty list
runNonEmptyDList :: NonEmptyDList a -> NonEmpty a
runNonEmptyDList :: forall a. NonEmptyDList a -> NonEmpty a
runNonEmptyDList = (([a] -> NonEmpty a) -> [a] -> NonEmpty a
forall a b. (a -> b) -> a -> b
$ []) (([a] -> NonEmpty a) -> NonEmpty a)
-> (NonEmptyDList a -> [a] -> NonEmpty a)
-> NonEmptyDList a
-> NonEmpty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmptyDList a -> [a] -> NonEmpty a
forall a. NonEmptyDList a -> [a] -> NonEmpty a
unNEDL
{-# INLINE runNonEmptyDList #-}

-- | Used for foldrMap1 and foldlMap1 definitions
newtype FromMaybe b = FromMaybe { forall b. FromMaybe b -> Maybe b -> b
appFromMaybe :: Maybe b -> b }

instance Semigroup (FromMaybe b) where
    FromMaybe Maybe b -> b
f <> :: FromMaybe b -> FromMaybe b -> FromMaybe b
<> FromMaybe Maybe b -> b
g = (Maybe b -> b) -> FromMaybe b
forall b. (Maybe b -> b) -> FromMaybe b
FromMaybe (Maybe b -> b
f (Maybe b -> b) -> (Maybe b -> Maybe b) -> Maybe b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Maybe b
forall a. a -> Maybe a
Just (b -> Maybe b) -> (Maybe b -> b) -> Maybe b -> Maybe b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe b -> b
g)

-- | Strict maybe, used to implement default foldlMap1' etc.
data SMaybe a = SNothing | SJust !a

-- | Used to implement intercalate1/Map
newtype JoinWith a = JoinWith {forall a. JoinWith a -> a -> a
joinee :: (a -> a)}

instance Semigroup a => Semigroup (JoinWith a) where
  JoinWith a -> a
a <> :: JoinWith a -> JoinWith a -> JoinWith a
<> JoinWith a -> a
b = (a -> a) -> JoinWith a
forall a. (a -> a) -> JoinWith a
JoinWith ((a -> a) -> JoinWith a) -> (a -> a) -> JoinWith a
forall a b. (a -> b) -> a -> b
$ \a
j -> a -> a
a a
j a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
j a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a -> a
b a
j

-------------------------------------------------------------------------------
-- Instances for misc base types
-------------------------------------------------------------------------------

instance Foldable1 NonEmpty where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> NonEmpty a -> m
foldMap1 a -> m
f (a
x :| [a]
xs) = m -> [a] -> m
go (a -> m
f a
x) [a]
xs where
        go :: m -> [a] -> m
go m
y [] = m
y
        go m
y (a
z : [a]
zs) = m
y m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m -> [a] -> m
go (a -> m
f a
z) [a]
zs

    foldMap1' :: forall m a. Semigroup m => (a -> m) -> NonEmpty a -> m
foldMap1' a -> m
f (a
x :| [a]
xs) = (m -> a -> m) -> m -> [a] -> m
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\m
m a
y -> m
m m -> m -> m
forall a. Semigroup a => a -> a -> a
<> a -> m
f a
y) (a -> m
f a
x) [a]
xs

    toNonEmpty :: forall a. NonEmpty a -> NonEmpty a
toNonEmpty = NonEmpty a -> NonEmpty a
forall a. a -> a
id

    foldrMap1 :: forall a b. (a -> b) -> (a -> b -> b) -> NonEmpty a -> b
foldrMap1 a -> b
g a -> b -> b
f (a
x :| [a]
xs) = a -> [a] -> b
go a
x [a]
xs where
        go :: a -> [a] -> b
go a
y [] = a -> b
g a
y
        go a
y (a
z : [a]
zs) = a -> b -> b
f a
y (a -> [a] -> b
go a
z [a]
zs)

    foldlMap1 :: forall a b. (a -> b) -> (b -> a -> b) -> NonEmpty a -> b
foldlMap1  a -> b
g b -> a -> b
f (a
x :| [a]
xs) = (b -> a -> b) -> b -> [a] -> b
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl b -> a -> b
f (a -> b
g a
x) [a]
xs
    foldlMap1' :: forall a b. (a -> b) -> (b -> a -> b) -> NonEmpty a -> b
foldlMap1' a -> b
g b -> a -> b
f (a
x :| [a]
xs) = let gx :: b
gx = a -> b
g a
x in b
gx b -> b -> b
forall a b. a -> b -> b
`seq` (b -> a -> b) -> b -> [a] -> b
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' b -> a -> b
f b
gx [a]
xs

    head :: forall a. NonEmpty a -> a
head = NonEmpty a -> a
forall a. NonEmpty a -> a
NE.head
    last :: forall a. NonEmpty a -> a
last = NonEmpty a -> a
forall a. NonEmpty a -> a
NE.last

instance Foldable1 Down where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> Down a -> m
foldMap1 = (a -> m) -> Down a -> m
forall a b. Coercible a b => a -> b
coerce

instance Foldable1 Complex where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> Complex a -> m
foldMap1 a -> m
f (a
x :+ a
y) = a -> m
f a
x m -> m -> m
forall a. Semigroup a => a -> a -> a
<> a -> m
f a
y

    toNonEmpty :: forall a. Complex a -> NonEmpty a
toNonEmpty (a
x :+ a
y) = a
x a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| a
y a -> [a] -> [a]
forall a. a -> [a] -> [a]
: []

-------------------------------------------------------------------------------
-- Instances for tuples
-------------------------------------------------------------------------------

-- 3+ tuples are not Foldable/Traversable

instance Foldable1 ((,) a) where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> (a, a) -> m
foldMap1 a -> m
f (a
_, a
y) = a -> m
f a
y
    toNonEmpty :: forall a. (a, a) -> NonEmpty a
toNonEmpty (a
_, a
x) = a
x a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| []
    minimum :: forall a. Ord a => (a, a) -> a
minimum (a
_, a
x) = a
x
    maximum :: forall a. Ord a => (a, a) -> a
maximum (a
_, a
x) = a
x
    head :: forall a. (a, a) -> a
head (a
_, a
x) = a
x
    last :: forall a. (a, a) -> a
last (a
_, a
x) = a
x

-------------------------------------------------------------------------------
-- Monoid / Semigroup instances
-------------------------------------------------------------------------------

instance Foldable1 Dual where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> Dual a -> m
foldMap1 = (a -> m) -> Dual a -> m
forall a b. Coercible a b => a -> b
coerce

instance Foldable1 Sum where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> Sum a -> m
foldMap1 = (a -> m) -> Sum a -> m
forall a b. Coercible a b => a -> b
coerce

instance Foldable1 Product where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> Product a -> m
foldMap1 = (a -> m) -> Product a -> m
forall a b. Coercible a b => a -> b
coerce

instance Foldable1 Min where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> Min a -> m
foldMap1 = (a -> m) -> Min a -> m
forall a b. Coercible a b => a -> b
coerce

instance Foldable1 Max where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> Max a -> m
foldMap1 = (a -> m) -> Max a -> m
forall a b. Coercible a b => a -> b
coerce

instance Foldable1 First where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> First a -> m
foldMap1 = (a -> m) -> First a -> m
forall a b. Coercible a b => a -> b
coerce

instance Foldable1 Last where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> Last a -> m
foldMap1 = (a -> m) -> Last a -> m
forall a b. Coercible a b => a -> b
coerce

deriving instance (Foldable1 f) => Foldable1 (Mon.Alt f)

deriving instance (Foldable1 f) => Foldable1 (Mon.Ap f)

-------------------------------------------------------------------------------
-- GHC.Generics instances
-------------------------------------------------------------------------------

instance Foldable1 V1 where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> V1 a -> m
foldMap1 a -> m
_ V1 a
x = V1 a
x V1 a -> m -> m
forall a b. a -> b -> b
`seq` [Char] -> m
forall a. HasCallStack => [Char] -> a
error [Char]
"foldMap1 @V1"

instance Foldable1 Par1 where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> Par1 a -> m
foldMap1 = (a -> m) -> Par1 a -> m
forall a b. Coercible a b => a -> b
coerce

deriving instance Foldable1 f => Foldable1 (Rec1 f)

deriving instance Foldable1 f => Foldable1 (M1 i c f)

instance (Foldable1 f, Foldable1 g) => Foldable1 (f :+: g) where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> (:+:) f g a -> m
foldMap1 a -> m
f (L1 f a
x) = (a -> m) -> f a -> m
forall m a. Semigroup m => (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f f a
x
    foldMap1 a -> m
f (R1 g a
y) = (a -> m) -> g a -> m
forall m a. Semigroup m => (a -> m) -> g a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f g a
y

instance (Foldable1 f, Foldable1 g) => Foldable1 (f :*: g) where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> (:*:) f g a -> m
foldMap1 a -> m
f (f a
x :*: g a
y) = (a -> m) -> f a -> m
forall m a. Semigroup m => (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f f a
x m -> m -> m
forall a. Semigroup a => a -> a -> a
<> (a -> m) -> g a -> m
forall m a. Semigroup m => (a -> m) -> g a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f g a
y

instance (Foldable1 f, Foldable1 g) => Foldable1 (f :.: g) where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> (:.:) f g a -> m
foldMap1 a -> m
f = (g a -> m) -> f (g a) -> m
forall m a. Semigroup m => (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 ((a -> m) -> g a -> m
forall m a. Semigroup m => (a -> m) -> g a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f) (f (g a) -> m) -> ((:.:) f g a -> f (g a)) -> (:.:) f g a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (:.:) f g a -> f (g a)
forall k2 k1 (f :: k2 -> *) (g :: k1 -> k2) (p :: k1).
(:.:) f g p -> f (g p)
unComp1

-------------------------------------------------------------------------------
-- Extra instances
-------------------------------------------------------------------------------

instance Foldable1 Identity where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> Identity a -> m
foldMap1      = (a -> m) -> Identity a -> m
forall a b. Coercible a b => a -> b
coerce

    foldrMap1 :: forall a b. (a -> b) -> (a -> b -> b) -> Identity a -> b
foldrMap1  a -> b
g a -> b -> b
_ = (a -> b) -> Identity a -> b
forall a b. Coercible a b => a -> b
coerce a -> b
g
    foldrMap1' :: forall a b. (a -> b) -> (a -> b -> b) -> Identity a -> b
foldrMap1' a -> b
g a -> b -> b
_ = (a -> b) -> Identity a -> b
forall a b. Coercible a b => a -> b
coerce a -> b
g
    foldlMap1 :: forall a b. (a -> b) -> (b -> a -> b) -> Identity a -> b
foldlMap1  a -> b
g b -> a -> b
_ = (a -> b) -> Identity a -> b
forall a b. Coercible a b => a -> b
coerce a -> b
g
    foldlMap1' :: forall a b. (a -> b) -> (b -> a -> b) -> Identity a -> b
foldlMap1' a -> b
g b -> a -> b
_ = (a -> b) -> Identity a -> b
forall a b. Coercible a b => a -> b
coerce a -> b
g

    toNonEmpty :: forall a. Identity a -> NonEmpty a
toNonEmpty (Identity a
x) = a
x a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| []

    last :: forall a. Identity a -> a
last    = Identity a -> a
forall a b. Coercible a b => a -> b
coerce
    head :: forall a. Identity a -> a
head    = Identity a -> a
forall a b. Coercible a b => a -> b
coerce
    minimum :: forall a. Ord a => Identity a -> a
minimum = Identity a -> a
forall a b. Coercible a b => a -> b
coerce
    maximum :: forall a. Ord a => Identity a -> a
maximum = Identity a -> a
forall a b. Coercible a b => a -> b
coerce

-- | It would be enough for either half of a product to be 'Foldable1'.
-- Other could be 'Foldable'.
instance (Foldable1 f, Foldable1 g) => Foldable1 (Functor.Product f g) where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> Product f g a -> m
foldMap1 a -> m
f (Functor.Pair f a
x g a
y)    = (a -> m) -> f a -> m
forall m a. Semigroup m => (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f f a
x m -> m -> m
forall a. Semigroup a => a -> a -> a
<> (a -> m) -> g a -> m
forall m a. Semigroup m => (a -> m) -> g a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f g a
y
    foldrMap1 :: forall a b. (a -> b) -> (a -> b -> b) -> Product f g a -> b
foldrMap1 a -> b
g a -> b -> b
f (Functor.Pair f a
x g a
y) = (a -> b -> b) -> b -> f a -> b
forall a b. (a -> b -> b) -> b -> f a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> b -> b
f ((a -> b) -> (a -> b -> b) -> g a -> b
forall a b. (a -> b) -> (a -> b -> b) -> g a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1 a -> b
g a -> b -> b
f g a
y) f a
x

    head :: forall a. Product f g a -> a
head (Functor.Pair f a
x g a
_) = f a -> a
forall a. f a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
head f a
x
    last :: forall a. Product f g a -> a
last (Functor.Pair f a
_ g a
y) = g a -> a
forall a. g a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
last g a
y

instance (Foldable1 f, Foldable1 g) => Foldable1 (Functor.Sum f g) where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> Sum f g a -> m
foldMap1 a -> m
f (Functor.InL f a
x) = (a -> m) -> f a -> m
forall m a. Semigroup m => (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f f a
x
    foldMap1 a -> m
f (Functor.InR g a
y) = (a -> m) -> g a -> m
forall m a. Semigroup m => (a -> m) -> g a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f g a
y

    foldrMap1 :: forall a b. (a -> b) -> (a -> b -> b) -> Sum f g a -> b
foldrMap1 a -> b
g a -> b -> b
f (Functor.InL f a
x) = (a -> b) -> (a -> b -> b) -> f a -> b
forall a b. (a -> b) -> (a -> b -> b) -> f a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1 a -> b
g a -> b -> b
f f a
x
    foldrMap1 a -> b
g a -> b -> b
f (Functor.InR g a
y) = (a -> b) -> (a -> b -> b) -> g a -> b
forall a b. (a -> b) -> (a -> b -> b) -> g a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1 a -> b
g a -> b -> b
f g a
y

    toNonEmpty :: forall a. Sum f g a -> NonEmpty a
toNonEmpty (Functor.InL f a
x) = f a -> NonEmpty a
forall a. f a -> NonEmpty a
forall (t :: * -> *) a. Foldable1 t => t a -> NonEmpty a
toNonEmpty f a
x
    toNonEmpty (Functor.InR g a
y) = g a -> NonEmpty a
forall a. g a -> NonEmpty a
forall (t :: * -> *) a. Foldable1 t => t a -> NonEmpty a
toNonEmpty g a
y

    head :: forall a. Sum f g a -> a
head (Functor.InL f a
x) = f a -> a
forall a. f a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
head f a
x
    head (Functor.InR g a
y) = g a -> a
forall a. g a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
head g a
y
    last :: forall a. Sum f g a -> a
last (Functor.InL f a
x) = f a -> a
forall a. f a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
last f a
x
    last (Functor.InR g a
y) = g a -> a
forall a. g a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
last g a
y

    minimum :: forall a. Ord a => Sum f g a -> a
minimum (Functor.InL f a
x) = f a -> a
forall a. Ord a => f a -> a
forall (t :: * -> *) a. (Foldable1 t, Ord a) => t a -> a
minimum f a
x
    minimum (Functor.InR g a
y) = g a -> a
forall a. Ord a => g a -> a
forall (t :: * -> *) a. (Foldable1 t, Ord a) => t a -> a
minimum g a
y
    maximum :: forall a. Ord a => Sum f g a -> a
maximum (Functor.InL f a
x) = f a -> a
forall a. Ord a => f a -> a
forall (t :: * -> *) a. (Foldable1 t, Ord a) => t a -> a
maximum f a
x
    maximum (Functor.InR g a
y) = g a -> a
forall a. Ord a => g a -> a
forall (t :: * -> *) a. (Foldable1 t, Ord a) => t a -> a
maximum g a
y

instance (Foldable1 f, Foldable1 g) => Foldable1 (Compose f g) where
    foldMap1 :: forall m a. Semigroup m => (a -> m) -> Compose f g a -> m
foldMap1 a -> m
f = (g a -> m) -> f (g a) -> m
forall m a. Semigroup m => (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 ((a -> m) -> g a -> m
forall m a. Semigroup m => (a -> m) -> g a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f) (f (g a) -> m) -> (Compose f g a -> f (g a)) -> Compose f g a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compose f g a -> f (g a)
forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose

    foldrMap1 :: forall a b. (a -> b) -> (a -> b -> b) -> Compose f g a -> b
foldrMap1 a -> b
f a -> b -> b
g = (g a -> b) -> (g a -> b -> b) -> f (g a) -> b
forall a b. (a -> b) -> (a -> b -> b) -> f a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1 ((a -> b) -> (a -> b -> b) -> g a -> b
forall a b. (a -> b) -> (a -> b -> b) -> g a -> b
forall (t :: * -> *) a b.
Foldable1 t =>
(a -> b) -> (a -> b -> b) -> t a -> b
foldrMap1 a -> b
f a -> b -> b
g) (\g a
xs b
x -> (a -> b -> b) -> b -> g a -> b
forall a b. (a -> b -> b) -> b -> g a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> b -> b
g b
x g a
xs) (f (g a) -> b) -> (Compose f g a -> f (g a)) -> Compose f g a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compose f g a -> f (g a)
forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose

    head :: forall a. Compose f g a -> a
head = g a -> a
forall a. g a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
head (g a -> a) -> (Compose f g a -> g a) -> Compose f g a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (g a) -> g a
forall a. f a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
head (f (g a) -> g a)
-> (Compose f g a -> f (g a)) -> Compose f g a -> g a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compose f g a -> f (g a)
forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose
    last :: forall a. Compose f g a -> a
last = g a -> a
forall a. g a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
last (g a -> a) -> (Compose f g a -> g a) -> Compose f g a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (g a) -> g a
forall a. f a -> a
forall (t :: * -> *) a. Foldable1 t => t a -> a
last (f (g a) -> g a)
-> (Compose f g a -> f (g a)) -> Compose f g a -> g a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compose f g a -> f (g a)
forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose

(#.) :: Coercible b c => (b -> c) -> (a -> b) -> a -> c
#. :: forall b c a. Coercible b c => (b -> c) -> (a -> b) -> a -> c
(#.) b -> c
_f = (a -> b) -> a -> c
forall a b. Coercible a b => a -> b
coerce