{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}module Data.Rope.Util.Reducer ( Reducer(..) -- * Folding with 'Reducer's , foldMapReduce -- :: (Foldable f, e `Reducer` m) => (a -> e) -> f a -> m , foldReduce -- :: (Foldable f, e `Reducer` m) => f e -> m -- * 'Applicative' reduction , pureUnit -- :: (Applicative f, c `Reducer` n) => c -> m n , returnUnit -- :: (Monad m, c `Reducer` n) c -> m n ) whereimport Control.Applicativeimport Control.Monad import qualified Data.FingerTree as FingerTreeimport Data.FingerTree (FingerTree, Measured)import Data.ByteString (ByteString)import qualified Data.ByteString.Lazy as Lazyimport Data.Monoidimport Data.Foldable{-import qualified Data.Sequence as Seqimport Data.Sequence (Seq)import qualified Data.Set as Setimport Data.Set (Set)import qualified Data.IntSet as IntSetimport Data.IntSet (IntSet)import qualified Data.IntMap as IntMapimport Data.IntMap (IntMap)import qualified Data.Map as Mapimport Data.Map (Map)-}-- | This type may be best read infix. A @c `Reducer` m@ is a 'Monoid' @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' or 'snoc'class Monoid m => Reducer c m where -- | Convert a value into a 'Monoid' unit :: c -> m -- | Append a value to a 'Monoid' for use in left-to-right reduction snoc :: m -> c -> m -- | Prepend a value onto a 'Monoid' for use during right-to-left reduction cons :: c -> m -> m unit = snoc mempty snoc m = mappend m . unit cons = mappend . unit-- | Apply a 'Reducer' to a 'Foldable' container, after mapping the contents into a suitable form for reduction.foldMapReduce :: (Foldable f, e `Reducer` m) => (a -> e) -> f a -> mfoldMapReduce f = foldMap (unit . f)-- | Apply a 'Reducer' to a 'Foldable' mapping each element through 'unit'foldReduce :: (Foldable f, e `Reducer` m) => f e -> mfoldReduce = foldMap unitreturnUnit :: (Monad m, c `Reducer` n) => c -> m n returnUnit = return . unitpureUnit :: (Applicative f, c `Reducer` n) => c -> f npureUnit = pure . unitinstance (Reducer c m, Reducer c n) => Reducer c (m,n) where unit x = (unit x,unit x) (m,n) `snoc` x = (m `snoc` x, n `snoc` x) x `cons` (m,n) = (x `cons` m, x `cons` n)instance (Reducer c m, Reducer c n, Reducer c o) => Reducer c (m,n,o) where unit x = (unit x,unit x, unit x) (m,n,o) `snoc` x = (m `snoc` x, n `snoc` x, o `snoc` x) x `cons` (m,n,o) = (x `cons` m, x `cons` n, x `cons` o)instance (Reducer c m, Reducer c n, Reducer c o, Reducer c p) => Reducer c (m,n,o,p) where unit x = (unit x,unit x, unit x, unit x) (m,n,o,p) `snoc` x = (m `snoc` x, n `snoc` x, o `snoc` x, p `snoc` x) x `cons` (m,n,o,p) = (x `cons` m, x `cons` n, x `cons` o, x `cons` p)instance Reducer ByteString ByteString where unit = id snoc = mappend cons = mappendinstance Reducer ByteString Lazy.ByteString where unit = Lazy.fromChunks . returninstance Reducer c [c] where unit = return cons = (:) xs `snoc` x = xs ++ [x]instance Reducer c () where unit _ = () _ `snoc` _ = () _ `cons` _ = ()instance Reducer Bool Any where unit = Anyinstance Reducer Bool All where unit = Allinstance Reducer (a -> a) (Endo a) where unit = Endoinstance Monoid a => Reducer a (Dual a) where unit = Dual instance Num a => Reducer a (Sum a) where unit = Suminstance Num a => Reducer a (Product a) where unit = Productinstance Reducer (Maybe a) (First a) where unit = Firstinstance Reducer a (First a) where unit = First . Justinstance Reducer (Maybe a) (Last a) where unit = Lastinstance Reducer a (Last a) where unit = Last . Just-- instance Measured v a => Monoid (FingerTree v a) where-- mempty = FingerTree.empty-- mappend = (><)instance Measured v a => Reducer a (FingerTree v a) where unit = FingerTree.singleton{-instance Reducer a (Seq a) where unit = Seq.singleton cons = (Seq.<|) snoc = (Seq.|>)instance Reducer Int IntSet where unit = IntSet.singleton cons = IntSet.insert snoc = flip IntSet.insert -- left bias irrelevantinstance Ord a => Reducer a (Set a) where unit = Set.singleton cons = Set.insert -- pedantic about order in case 'Eq' doesn't implement structural equality snoc s m | Set.member m s = s | otherwise = Set.insert m sinstance Reducer (Int,v) (IntMap v) where unit = uncurry IntMap.singleton cons = uncurry IntMap.insert snoc = flip . uncurry . IntMap.insertWith $ const idinstance Ord k => Reducer (k,v) (Map k v) where unit = uncurry Map.singleton cons = uncurry Map.insert snoc = flip . uncurry . Map.insertWith $ const id-}