{-# LANGUAGE UndecidableInstances , FlexibleContexts , MultiParamTypeClasses , FlexibleInstances , GeneralizedNewtypeDeriving, TypeOperators, ScopedTypeVariables, CPP #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Trustworthy #-}
#endif

#ifndef MIN_VERSION_semigroups
#define MIN_VERSION_semigroups(x,y,z) 1
#endif

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Semigroup.Reducer
-- Copyright   :  (c) Edward Kmett 2009
-- License     :  BSD3
-- Maintainer  :  ekmett@gmail.com
-- Stability   :  experimental
-- Portability :  non-portable (MPTCs)
--
-- A @c@-'Reducer' is a 'Semigroup' with a canonical mapping from @c@ to the Semigroup.
--
-----------------------------------------------------------------------------

module Data.Semigroup.Reducer
  ( Reducer(..)
  , foldMapReduce, foldMapReduce1
  , foldReduce, foldReduce1
  , pureUnit
  , returnUnit
  , Count(..)
  ) where

#if __GLASGOW_HASKELL__ < 710
import Control.Applicative
#endif

import qualified Data.Monoid as Monoid
import Data.Semigroup as Semigroup
import Data.Semigroup.Foldable
import Data.Semigroup.Instances ()
import Data.Hashable

#if __GLASGOW_HASKELL__ < 710
import Data.Foldable
#endif

import Data.FingerTree

import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import qualified Data.Set as Set
import Data.Set (Set)
import qualified Data.IntSet as IntSet
import Data.IntSet (IntSet)
import qualified Data.IntMap as IntMap
import Data.IntMap (IntMap)
import qualified Data.Map as Map
import Data.Map (Map)
import qualified Data.HashMap.Lazy as HashMap
import Data.HashMap.Lazy (HashMap)

#ifdef LANGUAGE_DeriveDataTypeable
import Data.Data
#endif

--import Text.Parsec.Prim

-- | This type may be best read infix. A @c `Reducer` m@ is a 'Semigroup' @m@ that maps
-- values of type @c@ through @unit@ to values of type @m@. A @c@-'Reducer' may also
-- supply operations which tack-on another @c@ to an existing 'Monoid' @m@ on the left
-- or right. These specialized reductions may be more efficient in some scenarios
-- and are used when appropriate by a 'Generator'. The names 'cons' and 'snoc' work
-- by analogy to the synonymous operations in the list monoid.
--
-- This class deliberately avoids functional-dependencies, so that () can be a @c@-Reducer
-- for all @c@, and so many common reducers can work over multiple types, for instance,
-- First and Last may reduce both @a@ and 'Maybe' @a@. Since a 'Generator' has a fixed element
-- type, the input to the reducer is generally known and extracting from the monoid usually
-- is sufficient to fix the result type. Combinators are available for most scenarios where
-- this is not the case, and the few remaining cases can be handled by using an explicit
-- type annotation.
--
-- Minimal definition: 'unit'
class Semigroup m => Reducer c m where
  -- | Convert a value into a 'Semigroup'
  unit :: c -> m
  -- | Append a value to a 'Semigroup' for use in left-to-right reduction
  snoc :: m -> c -> m
  -- | Prepend a value onto a 'Semigroup' for use during right-to-left reduction
  cons :: c -> m -> m

  snoc 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
. c -> m
forall c m. Reducer c m => c -> m
unit
  cons = 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
. c -> m
forall c m. Reducer c m => c -> m
unit

-- | Apply a 'Reducer' to a 'Foldable' container, after mapping the contents into a suitable form for reduction.
foldMapReduce :: (Foldable f, Monoid m, Reducer e m) => (a -> e) -> f a -> m
foldMapReduce :: (a -> e) -> f a -> m
foldMapReduce a -> e
f = (a -> m) -> f a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (e -> m
forall c m. Reducer c m => c -> m
unit (e -> m) -> (a -> e) -> a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> e
f)

foldMapReduce1 :: (Foldable1 f, Reducer e m) => (a -> e) -> f a -> m
foldMapReduce1 :: (a -> e) -> f a -> m
foldMapReduce1 a -> e
f = (a -> m) -> f a -> 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) -> (a -> e) -> a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> e
f)

-- | Apply a 'Reducer' to a 'Foldable' mapping each element through 'unit'
foldReduce :: (Foldable f, Monoid m, Reducer e m) => f e -> m
foldReduce :: f e -> m
foldReduce = (e -> m) -> f e -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap e -> m
forall c m. Reducer c m => c -> m
unit

-- | Apply a 'Reducer' to a 'Foldable1' mapping each element through 'unit'
foldReduce1 :: (Foldable1 f, Reducer e m) => f e -> m
foldReduce1 :: f e -> m
foldReduce1 = (e -> m) -> f 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

returnUnit :: (Monad m, Reducer c n) => c -> m n
returnUnit :: c -> m n
returnUnit = n -> m n
forall (m :: * -> *) a. Monad m => a -> m a
return (n -> m n) -> (c -> n) -> c -> m n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> n
forall c m. Reducer c m => c -> m
unit

pureUnit :: (Applicative f, Reducer c n) => c -> f n
pureUnit :: c -> f n
pureUnit = n -> f n
forall (f :: * -> *) a. Applicative f => a -> f a
pure (n -> f n) -> (c -> n) -> c -> f n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> n
forall c m. Reducer c m => c -> m
unit

newtype Count = Count { Count -> Int
getCount :: Int } deriving
  ( Count -> Count -> Bool
(Count -> Count -> Bool) -> (Count -> Count -> Bool) -> Eq Count
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Count -> Count -> Bool
$c/= :: Count -> Count -> Bool
== :: Count -> Count -> Bool
$c== :: Count -> Count -> Bool
Eq, Eq Count
Eq Count
-> (Count -> Count -> Ordering)
-> (Count -> Count -> Bool)
-> (Count -> Count -> Bool)
-> (Count -> Count -> Bool)
-> (Count -> Count -> Bool)
-> (Count -> Count -> Count)
-> (Count -> Count -> Count)
-> Ord Count
Count -> Count -> Bool
Count -> Count -> Ordering
Count -> Count -> Count
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Count -> Count -> Count
$cmin :: Count -> Count -> Count
max :: Count -> Count -> Count
$cmax :: Count -> Count -> Count
>= :: Count -> Count -> Bool
$c>= :: Count -> Count -> Bool
> :: Count -> Count -> Bool
$c> :: Count -> Count -> Bool
<= :: Count -> Count -> Bool
$c<= :: Count -> Count -> Bool
< :: Count -> Count -> Bool
$c< :: Count -> Count -> Bool
compare :: Count -> Count -> Ordering
$ccompare :: Count -> Count -> Ordering
$cp1Ord :: Eq Count
Ord, Int -> Count -> ShowS
[Count] -> ShowS
Count -> String
(Int -> Count -> ShowS)
-> (Count -> String) -> ([Count] -> ShowS) -> Show Count
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Count] -> ShowS
$cshowList :: [Count] -> ShowS
show :: Count -> String
$cshow :: Count -> String
showsPrec :: Int -> Count -> ShowS
$cshowsPrec :: Int -> Count -> ShowS
Show, ReadPrec [Count]
ReadPrec Count
Int -> ReadS Count
ReadS [Count]
(Int -> ReadS Count)
-> ReadS [Count]
-> ReadPrec Count
-> ReadPrec [Count]
-> Read Count
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Count]
$creadListPrec :: ReadPrec [Count]
readPrec :: ReadPrec Count
$creadPrec :: ReadPrec Count
readList :: ReadS [Count]
$creadList :: ReadS [Count]
readsPrec :: Int -> ReadS Count
$creadsPrec :: Int -> ReadS Count
Read
#ifdef LANGUAGE_DeriveDataTypeable
  , Typeable Count
DataType
Constr
Typeable Count
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Count -> c Count)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c Count)
-> (Count -> Constr)
-> (Count -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c Count))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Count))
-> ((forall b. Data b => b -> b) -> Count -> Count)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Count -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Count -> r)
-> (forall u. (forall d. Data d => d -> u) -> Count -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Count -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Count -> m Count)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Count -> m Count)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Count -> m Count)
-> Data Count
Count -> DataType
Count -> Constr
(forall b. Data b => b -> b) -> Count -> Count
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Count -> c Count
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Count
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Count -> u
forall u. (forall d. Data d => d -> u) -> Count -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Count -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Count -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Count -> m Count
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Count -> m Count
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Count
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Count -> c Count
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Count)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Count)
$cCount :: Constr
$tCount :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> Count -> m Count
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Count -> m Count
gmapMp :: (forall d. Data d => d -> m d) -> Count -> m Count
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Count -> m Count
gmapM :: (forall d. Data d => d -> m d) -> Count -> m Count
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Count -> m Count
gmapQi :: Int -> (forall d. Data d => d -> u) -> Count -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Count -> u
gmapQ :: (forall d. Data d => d -> u) -> Count -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Count -> [u]
gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Count -> r
$cgmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Count -> r
gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Count -> r
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Count -> r
gmapT :: (forall b. Data b => b -> b) -> Count -> Count
$cgmapT :: (forall b. Data b => b -> b) -> Count -> Count
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Count)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Count)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c Count)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Count)
dataTypeOf :: Count -> DataType
$cdataTypeOf :: Count -> DataType
toConstr :: Count -> Constr
$ctoConstr :: Count -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Count
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Count
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Count -> c Count
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Count -> c Count
$cp1Data :: Typeable Count
Data, Typeable
#endif
  )

instance Hashable Count where
  hashWithSalt :: Int -> Count -> Int
hashWithSalt Int
n = Int -> Int -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
n (Int -> Int) -> (Count -> Int) -> Count -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Count -> Int
getCount

instance Semigroup Count where
  Count Int
a <> :: Count -> Count -> Count
<> Count Int
b = Int -> Count
Count (Int
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
b)
#if MIN_VERSION_semigroups(0,17,0)
  stimes :: b -> Count -> Count
stimes b
n (Count Int
a) = Int -> Count
Count (Int -> Count) -> Int -> Count
forall a b. (a -> b) -> a -> b
$ b -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral b
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
a
#else
  times1p n (Count a) = Count $ (fromIntegral n + 1) * a
#endif

instance Monoid Count where
  mempty :: Count
mempty = Int -> Count
Count Int
0
#if !(MIN_VERSION_base(4,11,0))
  Count a `mappend` Count b = Count (a + b)
#endif

instance Reducer a Count where
  unit :: a -> Count
unit a
_ = Int -> Count
Count Int
1
  Count Int
n snoc :: Count -> a -> Count
`snoc` a
_ = Int -> Count
Count (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
  a
_ cons :: a -> Count -> Count
`cons` Count Int
n = Int -> Count
Count (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)

instance (Reducer c m, Reducer c n) => Reducer c (m,n) where
  unit :: c -> (m, n)
unit c
x = (c -> m
forall c m. Reducer c m => c -> m
unit c
x,c -> n
forall c m. Reducer c m => c -> m
unit c
x)
  (m
m,n
n) snoc :: (m, n) -> c -> (m, n)
`snoc` c
x = (m
m m -> c -> m
forall c m. Reducer c m => m -> c -> m
`snoc` c
x, n
n n -> c -> n
forall c m. Reducer c m => m -> c -> m
`snoc` c
x)
  c
x cons :: c -> (m, n) -> (m, n)
`cons` (m
m,n
n) = (c
x c -> m -> m
forall c m. Reducer c m => c -> m -> m
`cons` m
m, c
x c -> n -> n
forall c m. Reducer c m => c -> m -> m
`cons` n
n)

instance (Reducer c m, Reducer c n, Reducer c o) => Reducer c (m,n,o) where
  unit :: c -> (m, n, o)
unit c
x = (c -> m
forall c m. Reducer c m => c -> m
unit c
x,c -> n
forall c m. Reducer c m => c -> m
unit c
x, c -> o
forall c m. Reducer c m => c -> m
unit c
x)
  (m
m,n
n,o
o) snoc :: (m, n, o) -> c -> (m, n, o)
`snoc` c
x = (m
m m -> c -> m
forall c m. Reducer c m => m -> c -> m
`snoc` c
x, n
n n -> c -> n
forall c m. Reducer c m => m -> c -> m
`snoc` c
x, o
o o -> c -> o
forall c m. Reducer c m => m -> c -> m
`snoc` c
x)
  c
x cons :: c -> (m, n, o) -> (m, n, o)
`cons` (m
m,n
n,o
o) = (c
x c -> m -> m
forall c m. Reducer c m => c -> m -> m
`cons` m
m, c
x c -> n -> n
forall c m. Reducer c m => c -> m -> m
`cons` n
n, c
x c -> o -> o
forall c m. Reducer c m => c -> m -> m
`cons` o
o)

instance (Reducer c m, Reducer c n, Reducer c o, Reducer c p) => Reducer c (m,n,o,p) where
  unit :: c -> (m, n, o, p)
unit c
x = (c -> m
forall c m. Reducer c m => c -> m
unit c
x,c -> n
forall c m. Reducer c m => c -> m
unit c
x, c -> o
forall c m. Reducer c m => c -> m
unit c
x, c -> p
forall c m. Reducer c m => c -> m
unit c
x)
  (m
m,n
n,o
o,p
p) snoc :: (m, n, o, p) -> c -> (m, n, o, p)
`snoc` c
x = (m
m m -> c -> m
forall c m. Reducer c m => m -> c -> m
`snoc` c
x, n
n n -> c -> n
forall c m. Reducer c m => m -> c -> m
`snoc` c
x, o
o o -> c -> o
forall c m. Reducer c m => m -> c -> m
`snoc` c
x, p
p p -> c -> p
forall c m. Reducer c m => m -> c -> m
`snoc` c
x)
  c
x cons :: c -> (m, n, o, p) -> (m, n, o, p)
`cons` (m
m,n
n,o
o,p
p) = (c
x c -> m -> m
forall c m. Reducer c m => c -> m -> m
`cons` m
m, c
x c -> n -> n
forall c m. Reducer c m => c -> m -> m
`cons` n
n, c
x c -> o -> o
forall c m. Reducer c m => c -> m -> m
`cons` o
o, c
x c -> p -> p
forall c m. Reducer c m => c -> m -> m
`cons` p
p)

instance Reducer c [c] where
  unit :: c -> [c]
unit = c -> [c]
forall (m :: * -> *) a. Monad m => a -> m a
return
  cons :: c -> [c] -> [c]
cons = (:)
  [c]
xs snoc :: [c] -> c -> [c]
`snoc` c
x = [c]
xs [c] -> [c] -> [c]
forall a. [a] -> [a] -> [a]
++ [c
x]

instance Reducer c () where
  unit :: c -> ()
unit c
_ = ()
  ()
_ snoc :: () -> c -> ()
`snoc` c
_ = ()
  c
_ cons :: c -> () -> ()
`cons` ()
_ = ()

instance Reducer Bool Any where
  unit :: Bool -> Any
unit = Bool -> Any
Any

instance Reducer Bool All where
  unit :: Bool -> All
unit = Bool -> All
All

instance Reducer (a -> a) (Endo a) where
  unit :: (a -> a) -> Endo a
unit = (a -> a) -> Endo a
forall a. (a -> a) -> Endo a
Endo

instance Semigroup a => Reducer a (Dual a) where
  unit :: a -> Dual a
unit = a -> Dual a
forall a. a -> Dual a
Dual

instance Num a => Reducer a (Sum a) where
  unit :: a -> Sum a
unit = a -> Sum a
forall a. a -> Sum a
Sum

instance Num a => Reducer a (Product a) where
  unit :: a -> Product a
unit = a -> Product a
forall a. a -> Product a
Product

instance Ord a => Reducer a (Min a) where
  unit :: a -> Min a
unit = a -> Min a
forall a. a -> Min a
Min

instance Ord a => Reducer a (Max a) where
  unit :: a -> Max a
unit = a -> Max a
forall a. a -> Max a
Max

instance Reducer (Maybe a) (Monoid.First a) where
  unit :: Maybe a -> First a
unit = Maybe a -> First a
forall a. Maybe a -> First a
Monoid.First

instance Reducer a (Semigroup.First a) where
  unit :: a -> First a
unit = a -> First a
forall a. a -> First a
Semigroup.First

instance Reducer (Maybe a) (Monoid.Last a) where
  unit :: Maybe a -> Last a
unit = Maybe a -> Last a
forall a. Maybe a -> Last a
Monoid.Last

instance Reducer a (Semigroup.Last a) where
  unit :: a -> Last a
unit = a -> Last a
forall a. a -> Last a
Semigroup.Last

instance Measured v a => Reducer a (FingerTree v a) where
  unit :: a -> FingerTree v a
unit = a -> FingerTree v a
forall v a. Measured v a => a -> FingerTree v a
singleton
  cons :: a -> FingerTree v a -> FingerTree v a
cons = a -> FingerTree v a -> FingerTree v a
forall v a. Measured v a => a -> FingerTree v a -> FingerTree v a
(<|)
  snoc :: FingerTree v a -> a -> FingerTree v a
snoc = FingerTree v a -> a -> FingerTree v a
forall v a. Measured v a => FingerTree v a -> a -> FingerTree v a
(|>)

--instance (Stream s m t, Reducer c a) => Reducer c (ParsecT s u m a) where
--    unit = return . unit

instance Reducer a (Seq a) where
  unit :: a -> Seq a
unit = a -> Seq a
forall a. a -> Seq a
Seq.singleton
  cons :: a -> Seq a -> Seq a
cons = a -> Seq a -> Seq a
forall a. a -> Seq a -> Seq a
(Seq.<|)
  snoc :: Seq a -> a -> Seq a
snoc = Seq a -> a -> Seq a
forall a. Seq a -> a -> Seq a
(Seq.|>)

instance Reducer Int IntSet where
  unit :: Int -> IntSet
unit = Int -> IntSet
IntSet.singleton
  cons :: Int -> IntSet -> IntSet
cons = Int -> IntSet -> IntSet
IntSet.insert
  snoc :: IntSet -> Int -> IntSet
snoc = (Int -> IntSet -> IntSet) -> IntSet -> Int -> IntSet
forall a b c. (a -> b -> c) -> b -> a -> c
flip Int -> IntSet -> IntSet
IntSet.insert -- left bias irrelevant

instance Ord a => Reducer a (Set a) where
  unit :: a -> Set a
unit = a -> Set a
forall a. a -> Set a
Set.singleton
  cons :: a -> Set a -> Set a
cons = a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
Set.insert
  -- pedantic about order in case 'Eq' doesn't implement structural equality
  snoc :: Set a -> a -> Set a
snoc Set a
s a
m | a -> Set a -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member a
m Set a
s = Set a
s
           | Bool
otherwise = a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
Set.insert a
m Set a
s

instance Reducer (Int, v) (IntMap v) where
  unit :: (Int, v) -> IntMap v
unit = (Int -> v -> IntMap v) -> (Int, v) -> IntMap v
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> v -> IntMap v
forall a. Int -> a -> IntMap a
IntMap.singleton
  cons :: (Int, v) -> IntMap v -> IntMap v
cons = (Int -> v -> IntMap v -> IntMap v)
-> (Int, v) -> IntMap v -> IntMap v
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> v -> IntMap v -> IntMap v
forall a. Int -> a -> IntMap a -> IntMap a
IntMap.insert
  snoc :: IntMap v -> (Int, v) -> IntMap v
snoc = ((Int, v) -> IntMap v -> IntMap v)
-> IntMap v -> (Int, v) -> IntMap v
forall a b c. (a -> b -> c) -> b -> a -> c
flip (((Int, v) -> IntMap v -> IntMap v)
 -> IntMap v -> (Int, v) -> IntMap v)
-> ((v -> v -> v) -> (Int, v) -> IntMap v -> IntMap v)
-> (v -> v -> v)
-> IntMap v
-> (Int, v)
-> IntMap v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> v -> IntMap v -> IntMap v)
-> (Int, v) -> IntMap v -> IntMap v
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((Int -> v -> IntMap v -> IntMap v)
 -> (Int, v) -> IntMap v -> IntMap v)
-> ((v -> v -> v) -> Int -> v -> IntMap v -> IntMap v)
-> (v -> v -> v)
-> (Int, v)
-> IntMap v
-> IntMap v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (v -> v -> v) -> Int -> v -> IntMap v -> IntMap v
forall a. (a -> a -> a) -> Int -> a -> IntMap a -> IntMap a
IntMap.insertWith ((v -> v -> v) -> IntMap v -> (Int, v) -> IntMap v)
-> (v -> v -> v) -> IntMap v -> (Int, v) -> IntMap v
forall a b. (a -> b) -> a -> b
$ (v -> v) -> v -> v -> v
forall a b. a -> b -> a
const v -> v
forall a. a -> a
id

instance Ord k => Reducer (k, v) (Map k v) where
  unit :: (k, v) -> Map k v
unit = (k -> v -> Map k v) -> (k, v) -> Map k v
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry k -> v -> Map k v
forall k a. k -> a -> Map k a
Map.singleton
  cons :: (k, v) -> Map k v -> Map k v
cons = (k -> v -> Map k v -> Map k v) -> (k, v) -> Map k v -> Map k v
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry k -> v -> Map k v -> Map k v
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert
  snoc :: Map k v -> (k, v) -> Map k v
snoc = ((k, v) -> Map k v -> Map k v) -> Map k v -> (k, v) -> Map k v
forall a b c. (a -> b -> c) -> b -> a -> c
flip (((k, v) -> Map k v -> Map k v) -> Map k v -> (k, v) -> Map k v)
-> ((v -> v -> v) -> (k, v) -> Map k v -> Map k v)
-> (v -> v -> v)
-> Map k v
-> (k, v)
-> Map k v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k -> v -> Map k v -> Map k v) -> (k, v) -> Map k v -> Map k v
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((k -> v -> Map k v -> Map k v) -> (k, v) -> Map k v -> Map k v)
-> ((v -> v -> v) -> k -> v -> Map k v -> Map k v)
-> (v -> v -> v)
-> (k, v)
-> Map k v
-> Map k v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (v -> v -> v) -> k -> v -> Map k v -> Map k v
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
Map.insertWith ((v -> v -> v) -> Map k v -> (k, v) -> Map k v)
-> (v -> v -> v) -> Map k v -> (k, v) -> Map k v
forall a b. (a -> b) -> a -> b
$ (v -> v) -> v -> v -> v
forall a b. a -> b -> a
const v -> v
forall a. a -> a
id


instance (Eq k, Hashable k) => Reducer (k, v) (HashMap k v) where
  unit :: (k, v) -> HashMap k v
unit = (k -> v -> HashMap k v) -> (k, v) -> HashMap k v
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry k -> v -> HashMap k v
forall k v. Hashable k => k -> v -> HashMap k v
HashMap.singleton
  cons :: (k, v) -> HashMap k v -> HashMap k v
cons = (k -> v -> HashMap k v -> HashMap k v)
-> (k, v) -> HashMap k v -> HashMap k v
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry k -> v -> HashMap k v -> HashMap k v
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HashMap.insert
  snoc :: HashMap k v -> (k, v) -> HashMap k v
snoc = ((k, v) -> HashMap k v -> HashMap k v)
-> HashMap k v -> (k, v) -> HashMap k v
forall a b c. (a -> b -> c) -> b -> a -> c
flip (((k, v) -> HashMap k v -> HashMap k v)
 -> HashMap k v -> (k, v) -> HashMap k v)
-> ((v -> v -> v) -> (k, v) -> HashMap k v -> HashMap k v)
-> (v -> v -> v)
-> HashMap k v
-> (k, v)
-> HashMap k v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k -> v -> HashMap k v -> HashMap k v)
-> (k, v) -> HashMap k v -> HashMap k v
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((k -> v -> HashMap k v -> HashMap k v)
 -> (k, v) -> HashMap k v -> HashMap k v)
-> ((v -> v -> v) -> k -> v -> HashMap k v -> HashMap k v)
-> (v -> v -> v)
-> (k, v)
-> HashMap k v
-> HashMap k v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (v -> v -> v) -> k -> v -> HashMap k v -> HashMap k v
forall k v.
(Eq k, Hashable k) =>
(v -> v -> v) -> k -> v -> HashMap k v -> HashMap k v
HashMap.insertWith ((v -> v -> v) -> HashMap k v -> (k, v) -> HashMap k v)
-> (v -> v -> v) -> HashMap k v -> (k, v) -> HashMap k v
forall a b. (a -> b) -> a -> b
$ (v -> v) -> v -> v -> v
forall a b. a -> b -> a
const v -> v
forall a. a -> a
id

instance Monoid m => Reducer m (WrappedMonoid m) where
  unit :: m -> WrappedMonoid m
unit = m -> WrappedMonoid m
forall m. m -> WrappedMonoid m
WrapMonoid