{-# LANGUAGE UndecidableInstances, TypeOperators, FlexibleContexts, MultiParamTypeClasses, FlexibleInstances, TypeFamilies, CPP #-}
#if __GLASGOW_HASKELL__ >= 702 && __GLASGOW_HASKELL__ < 710
{-# LANGUAGE Trustworthy #-}
#endif
module Data.Semigroup.Generator
(
Generator1(..)
, reduce1
, mapReduceWith1
, reduceWith1
) where
import Data.List.NonEmpty (NonEmpty)
import Data.Semigroup.Foldable
import Data.Semigroup.Reducer
import Data.Generator
#if !(MIN_VERSION_base(4,11,0))
import Data.Semigroup (Semigroup(..))
#endif
class Generator c => Generator1 c where
mapReduce1 :: Reducer e m => (Elem c -> e) -> c -> m
mapTo1 :: Reducer e m => (Elem c -> e) -> m -> c -> m
mapFrom1 :: Reducer e m => (Elem c -> e) -> c -> m -> m
mapTo1 Elem c -> e
f m
m = m -> m -> m
forall a. Semigroup a => a -> a -> a
(<>) m
m (m -> m) -> (c -> m) -> c -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Elem c -> e) -> c -> m
forall c e m.
(Generator1 c, Reducer e m) =>
(Elem c -> e) -> c -> m
mapReduce1 Elem c -> e
f
mapFrom1 Elem c -> e
f = m -> m -> m
forall a. Semigroup a => a -> a -> a
(<>) (m -> m -> m) -> (c -> m) -> c -> m -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Elem c -> e) -> c -> m
forall c e m.
(Generator1 c, Reducer e m) =>
(Elem c -> e) -> c -> m
mapReduce1 Elem c -> e
f
instance Generator1 (NonEmpty e) where
mapReduce1 :: (Elem (NonEmpty e) -> e) -> NonEmpty e -> m
mapReduce1 Elem (NonEmpty e) -> e
f = (e -> m) -> NonEmpty e -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 (e -> m
forall c m. Reducer c m => c -> m
unit (e -> m) -> (e -> e) -> e -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> e
Elem (NonEmpty e) -> e
f)
reduce1 :: (Generator1 c, Reducer (Elem c) m) => c -> m
reduce1 :: c -> m
reduce1 = (Elem c -> Elem c) -> c -> m
forall c e m.
(Generator1 c, Reducer e m) =>
(Elem c -> e) -> c -> m
mapReduce1 Elem c -> Elem c
forall a. a -> a
id
{-# SPECIALIZE reduce1 :: Reducer a m => NonEmpty a -> m #-}
mapReduceWith1 :: (Generator1 c, Reducer e m) => (m -> n) -> (Elem c -> e) -> c -> n
mapReduceWith1 :: (m -> n) -> (Elem c -> e) -> c -> n
mapReduceWith1 m -> n
f Elem c -> e
g = m -> n
f (m -> n) -> (c -> m) -> c -> n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Elem c -> e) -> c -> m
forall c e m.
(Generator1 c, Reducer e m) =>
(Elem c -> e) -> c -> m
mapReduce1 Elem c -> e
g
{-# INLINE mapReduceWith1 #-}
reduceWith1 :: (Generator1 c, Reducer (Elem c) m) => (m -> n) -> c -> n
reduceWith1 :: (m -> n) -> c -> n
reduceWith1 m -> n
f = m -> n
f (m -> n) -> (c -> m) -> c -> n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> m
forall c m. (Generator1 c, Reducer (Elem c) m) => c -> m
reduce1
{-# INLINE reduceWith1 #-}