{-# LANGUAGE Haskell2010, DeriveDataTypeable #-}
module Data.Monoid.Instances.Stateful (
Stateful(Stateful), extract, state, setState
)
where
import Control.Applicative
import Data.Data (Data, Typeable)
import Data.Functor
import qualified Data.List as List
import Data.String (IsString(..))
import Data.Semigroup (Semigroup(..))
import Data.Monoid (Monoid(..))
import Data.Semigroup.Cancellative (LeftReductive(..), RightReductive(..))
import Data.Semigroup.Factorial (Factorial(..), StableFactorial)
import Data.Monoid.GCD (LeftGCDMonoid(..), RightGCDMonoid(..))
import Data.Monoid.Null (MonoidNull(null), PositiveMonoid)
import Data.Monoid.Factorial (FactorialMonoid(..))
import Data.Monoid.Textual (TextualMonoid(..))
import qualified Data.Semigroup.Factorial as Factorial
import qualified Data.Monoid.Factorial as Factorial
import qualified Data.Monoid.Textual as Textual
import Prelude hiding (all, any, break, elem, drop, filter, foldl, foldl1, foldr, foldr1, gcd, map, concatMap,
length, null, reverse, scanl, scanr, scanl1, scanr1, span, splitAt, take)
newtype Stateful a b = Stateful (b, a) deriving (Stateful a b -> DataType
Stateful a b -> Constr
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 {a} {b}. (Data a, Data b) => Typeable (Stateful a b)
forall a b. (Data a, Data b) => Stateful a b -> DataType
forall a b. (Data a, Data b) => Stateful a b -> Constr
forall a b.
(Data a, Data b) =>
(forall b. Data b => b -> b) -> Stateful a b -> Stateful a b
forall a b u.
(Data a, Data b) =>
Int -> (forall d. Data d => d -> u) -> Stateful a b -> u
forall a b u.
(Data a, Data b) =>
(forall d. Data d => d -> u) -> Stateful a b -> [u]
forall a b r r'.
(Data a, Data b) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Stateful a b -> r
forall a b r r'.
(Data a, Data b) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Stateful a b -> r
forall a b (m :: * -> *).
(Data a, Data b, Monad m) =>
(forall d. Data d => d -> m d) -> Stateful a b -> m (Stateful a b)
forall a b (m :: * -> *).
(Data a, Data b, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Stateful a b -> m (Stateful a b)
forall a b (c :: * -> *).
(Data a, Data b) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Stateful a b)
forall a b (c :: * -> *).
(Data a, Data b) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Stateful a b -> c (Stateful a b)
forall a b (t :: * -> *) (c :: * -> *).
(Data a, Data b, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Stateful a b))
forall a b (t :: * -> * -> *) (c :: * -> *).
(Data a, Data b, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Stateful a b))
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Stateful a b)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Stateful a b -> c (Stateful a b)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Stateful a b))
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Stateful a b -> m (Stateful a b)
$cgmapMo :: forall a b (m :: * -> *).
(Data a, Data b, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Stateful a b -> m (Stateful a b)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Stateful a b -> m (Stateful a b)
$cgmapMp :: forall a b (m :: * -> *).
(Data a, Data b, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Stateful a b -> m (Stateful a b)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Stateful a b -> m (Stateful a b)
$cgmapM :: forall a b (m :: * -> *).
(Data a, Data b, Monad m) =>
(forall d. Data d => d -> m d) -> Stateful a b -> m (Stateful a b)
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Stateful a b -> u
$cgmapQi :: forall a b u.
(Data a, Data b) =>
Int -> (forall d. Data d => d -> u) -> Stateful a b -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Stateful a b -> [u]
$cgmapQ :: forall a b u.
(Data a, Data b) =>
(forall d. Data d => d -> u) -> Stateful a b -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Stateful a b -> r
$cgmapQr :: forall a b r r'.
(Data a, Data b) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Stateful a b -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Stateful a b -> r
$cgmapQl :: forall a b r r'.
(Data a, Data b) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Stateful a b -> r
gmapT :: (forall b. Data b => b -> b) -> Stateful a b -> Stateful a b
$cgmapT :: forall a b.
(Data a, Data b) =>
(forall b. Data b => b -> b) -> Stateful a b -> Stateful a b
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Stateful a b))
$cdataCast2 :: forall a b (t :: * -> * -> *) (c :: * -> *).
(Data a, Data b, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Stateful a b))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Stateful a b))
$cdataCast1 :: forall a b (t :: * -> *) (c :: * -> *).
(Data a, Data b, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Stateful a b))
dataTypeOf :: Stateful a b -> DataType
$cdataTypeOf :: forall a b. (Data a, Data b) => Stateful a b -> DataType
toConstr :: Stateful a b -> Constr
$ctoConstr :: forall a b. (Data a, Data b) => Stateful a b -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Stateful a b)
$cgunfold :: forall a b (c :: * -> *).
(Data a, Data b) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Stateful a b)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Stateful a b -> c (Stateful a b)
$cgfoldl :: forall a b (c :: * -> *).
(Data a, Data b) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Stateful a b -> c (Stateful a b)
Data, Stateful a b -> Stateful a b -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall a b. (Eq b, Eq a) => Stateful a b -> Stateful a b -> Bool
/= :: Stateful a b -> Stateful a b -> Bool
$c/= :: forall a b. (Eq b, Eq a) => Stateful a b -> Stateful a b -> Bool
== :: Stateful a b -> Stateful a b -> Bool
$c== :: forall a b. (Eq b, Eq a) => Stateful a b -> Stateful a b -> Bool
Eq, Stateful a b -> Stateful a b -> Bool
Stateful a b -> Stateful a b -> Ordering
Stateful a b -> Stateful a b -> Stateful a b
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
forall {a} {b}. (Ord b, Ord a) => Eq (Stateful a b)
forall a b. (Ord b, Ord a) => Stateful a b -> Stateful a b -> Bool
forall a b.
(Ord b, Ord a) =>
Stateful a b -> Stateful a b -> Ordering
forall a b.
(Ord b, Ord a) =>
Stateful a b -> Stateful a b -> Stateful a b
min :: Stateful a b -> Stateful a b -> Stateful a b
$cmin :: forall a b.
(Ord b, Ord a) =>
Stateful a b -> Stateful a b -> Stateful a b
max :: Stateful a b -> Stateful a b -> Stateful a b
$cmax :: forall a b.
(Ord b, Ord a) =>
Stateful a b -> Stateful a b -> Stateful a b
>= :: Stateful a b -> Stateful a b -> Bool
$c>= :: forall a b. (Ord b, Ord a) => Stateful a b -> Stateful a b -> Bool
> :: Stateful a b -> Stateful a b -> Bool
$c> :: forall a b. (Ord b, Ord a) => Stateful a b -> Stateful a b -> Bool
<= :: Stateful a b -> Stateful a b -> Bool
$c<= :: forall a b. (Ord b, Ord a) => Stateful a b -> Stateful a b -> Bool
< :: Stateful a b -> Stateful a b -> Bool
$c< :: forall a b. (Ord b, Ord a) => Stateful a b -> Stateful a b -> Bool
compare :: Stateful a b -> Stateful a b -> Ordering
$ccompare :: forall a b.
(Ord b, Ord a) =>
Stateful a b -> Stateful a b -> Ordering
Ord, Int -> Stateful a b -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a b. (Show b, Show a) => Int -> Stateful a b -> ShowS
forall a b. (Show b, Show a) => [Stateful a b] -> ShowS
forall a b. (Show b, Show a) => Stateful a b -> String
showList :: [Stateful a b] -> ShowS
$cshowList :: forall a b. (Show b, Show a) => [Stateful a b] -> ShowS
show :: Stateful a b -> String
$cshow :: forall a b. (Show b, Show a) => Stateful a b -> String
showsPrec :: Int -> Stateful a b -> ShowS
$cshowsPrec :: forall a b. (Show b, Show a) => Int -> Stateful a b -> ShowS
Show, Typeable)
extract :: Stateful a b -> b
(Stateful (b
t, a
_)) = b
t
state :: Stateful a b -> a
state :: forall a b. Stateful a b -> a
state (Stateful (b
_, a
x)) = a
x
setState :: a -> Stateful a b -> Stateful a b
setState :: forall a b. a -> Stateful a b -> Stateful a b
setState a
s (Stateful (b
t, a
_)) = forall a b. (b, a) -> Stateful a b
Stateful (b
t, a
s)
instance Functor (Stateful a) where
fmap :: forall a b. (a -> b) -> Stateful a a -> Stateful a b
fmap a -> b
f (Stateful (a
x, a
s)) = forall a b. (b, a) -> Stateful a b
Stateful (a -> b
f a
x, a
s)
instance Monoid a => Applicative (Stateful a) where
pure :: forall a. a -> Stateful a a
pure a
m = forall a b. (b, a) -> Stateful a b
Stateful (a
m, forall a. Monoid a => a
mempty)
Stateful (a -> b
f, a
s1) <*> :: forall a b. Stateful a (a -> b) -> Stateful a a -> Stateful a b
<*> Stateful (a
x, a
s2) = forall a b. (b, a) -> Stateful a b
Stateful (a -> b
f a
x, forall a. Monoid a => a -> a -> a
mappend a
s1 a
s2)
instance (Semigroup a, Semigroup b) => Semigroup (Stateful a b) where
Stateful (b, a)
x <> :: Stateful a b -> Stateful a b -> Stateful a b
<> Stateful (b, a)
y = forall a b. (b, a) -> Stateful a b
Stateful ((b, a)
x forall a. Semigroup a => a -> a -> a
<> (b, a)
y)
{-# INLINE (<>) #-}
instance (Semigroup a, Semigroup b, Monoid a, Monoid b) => Monoid (Stateful a b) where
mempty :: Stateful a b
mempty = forall a b. (b, a) -> Stateful a b
Stateful forall a. Monoid a => a
mempty
mappend :: Stateful a b -> Stateful a b -> Stateful a b
mappend = forall a. Semigroup a => a -> a -> a
(<>)
{-# INLINE mempty #-}
{-# INLINE mappend #-}
instance (MonoidNull a, MonoidNull b) => MonoidNull (Stateful a b) where
null :: Stateful a b -> Bool
null (Stateful (b, a)
x) = forall m. MonoidNull m => m -> Bool
null (b, a)
x
{-# INLINE null #-}
instance (PositiveMonoid a, PositiveMonoid b) => PositiveMonoid (Stateful a b)
instance (LeftReductive a, LeftReductive b) => LeftReductive (Stateful a b) where
isPrefixOf :: Stateful a b -> Stateful a b -> Bool
isPrefixOf (Stateful (b, a)
x) (Stateful (b, a)
x') = forall m. LeftReductive m => m -> m -> Bool
isPrefixOf (b, a)
x (b, a)
x'
stripPrefix :: Stateful a b -> Stateful a b -> Maybe (Stateful a b)
stripPrefix (Stateful (b, a)
x) (Stateful (b, a)
x') = forall a b. (b, a) -> Stateful a b
Stateful forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall m. LeftReductive m => m -> m -> Maybe m
stripPrefix (b, a)
x (b, a)
x'
{-# INLINE isPrefixOf #-}
{-# INLINE stripPrefix #-}
instance (RightReductive a, RightReductive b) => RightReductive (Stateful a b) where
isSuffixOf :: Stateful a b -> Stateful a b -> Bool
isSuffixOf (Stateful (b, a)
x) (Stateful (b, a)
x') = forall m. RightReductive m => m -> m -> Bool
isSuffixOf (b, a)
x (b, a)
x'
stripSuffix :: Stateful a b -> Stateful a b -> Maybe (Stateful a b)
stripSuffix (Stateful (b, a)
x) (Stateful (b, a)
x') = forall a b. (b, a) -> Stateful a b
Stateful forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall m. RightReductive m => m -> m -> Maybe m
stripSuffix (b, a)
x (b, a)
x'
{-# INLINE stripSuffix #-}
{-# INLINE isSuffixOf #-}
instance (LeftGCDMonoid a, LeftGCDMonoid b) => LeftGCDMonoid (Stateful a b) where
commonPrefix :: Stateful a b -> Stateful a b -> Stateful a b
commonPrefix (Stateful (b, a)
x) (Stateful (b, a)
x') = forall a b. (b, a) -> Stateful a b
Stateful (forall m. LeftGCDMonoid m => m -> m -> m
commonPrefix (b, a)
x (b, a)
x')
stripCommonPrefix :: Stateful a b
-> Stateful a b -> (Stateful a b, Stateful a b, Stateful a b)
stripCommonPrefix (Stateful (b, a)
x) (Stateful (b, a)
x') = (forall a b. (b, a) -> Stateful a b
Stateful (b, a)
prefix, forall a b. (b, a) -> Stateful a b
Stateful (b, a)
suffix1, forall a b. (b, a) -> Stateful a b
Stateful (b, a)
suffix2)
where ((b, a)
prefix, (b, a)
suffix1, (b, a)
suffix2) = forall m. LeftGCDMonoid m => m -> m -> (m, m, m)
stripCommonPrefix (b, a)
x (b, a)
x'
{-# INLINE commonPrefix #-}
{-# INLINE stripCommonPrefix #-}
instance (RightGCDMonoid a, RightGCDMonoid b) => RightGCDMonoid (Stateful a b) where
commonSuffix :: Stateful a b -> Stateful a b -> Stateful a b
commonSuffix (Stateful (b, a)
x) (Stateful (b, a)
x') = forall a b. (b, a) -> Stateful a b
Stateful (forall m. RightGCDMonoid m => m -> m -> m
commonSuffix (b, a)
x (b, a)
x')
{-# INLINE commonSuffix #-}
instance (FactorialMonoid a, FactorialMonoid b) => Factorial (Stateful a b) where
factors :: Stateful a b -> [Stateful a b]
factors (Stateful (b, a)
x) = forall a b. (a -> b) -> [a] -> [b]
List.map forall a b. (b, a) -> Stateful a b
Stateful (forall m. Factorial m => m -> [m]
factors (b, a)
x)
length :: Stateful a b -> Int
length (Stateful (b, a)
x) = forall m. Factorial m => m -> Int
length (b, a)
x
reverse :: Stateful a b -> Stateful a b
reverse (Stateful (b, a)
x) = forall a b. (b, a) -> Stateful a b
Stateful (forall m. Factorial m => m -> m
reverse (b, a)
x)
primePrefix :: Stateful a b -> Stateful a b
primePrefix (Stateful (b, a)
x) = forall a b. (b, a) -> Stateful a b
Stateful (forall m. Factorial m => m -> m
primePrefix (b, a)
x)
primeSuffix :: Stateful a b -> Stateful a b
primeSuffix (Stateful (b, a)
x) = forall a b. (b, a) -> Stateful a b
Stateful (forall m. Factorial m => m -> m
primeSuffix (b, a)
x)
foldl :: forall a. (a -> Stateful a b -> a) -> a -> Stateful a b -> a
foldl a -> Stateful a b -> a
f a
a0 (Stateful (b, a)
x) = forall m a. Factorial m => (a -> m -> a) -> a -> m -> a
Factorial.foldl a -> (b, a) -> a
f' a
a0 (b, a)
x
where f' :: a -> (b, a) -> a
f' a
a (b, a)
x1 = a -> Stateful a b -> a
f a
a (forall a b. (b, a) -> Stateful a b
Stateful (b, a)
x1)
foldl' :: forall a. (a -> Stateful a b -> a) -> a -> Stateful a b -> a
foldl' a -> Stateful a b -> a
f a
a0 (Stateful (b, a)
x) = forall m a. Factorial m => (a -> m -> a) -> a -> m -> a
Factorial.foldl' a -> (b, a) -> a
f' a
a0 (b, a)
x
where f' :: a -> (b, a) -> a
f' a
a (b, a)
x1 = a -> Stateful a b -> a
f a
a (forall a b. (b, a) -> Stateful a b
Stateful (b, a)
x1)
foldr :: forall a. (Stateful a b -> a -> a) -> a -> Stateful a b -> a
foldr Stateful a b -> a -> a
f a
a (Stateful (b, a)
x) = forall m a. Factorial m => (m -> a -> a) -> a -> m -> a
Factorial.foldr (Stateful a b -> a -> a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (b, a) -> Stateful a b
Stateful) a
a (b, a)
x
foldMap :: forall n. Monoid n => (Stateful a b -> n) -> Stateful a b -> n
foldMap Stateful a b -> n
f (Stateful (b, a)
x) = forall m n. (Factorial m, Monoid n) => (m -> n) -> m -> n
Factorial.foldMap (Stateful a b -> n
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (b, a) -> Stateful a b
Stateful) (b, a)
x
{-# INLINE primePrefix #-}
{-# INLINE primeSuffix #-}
{-# INLINE foldl' #-}
{-# INLINE foldr #-}
{-# INLINE foldMap #-}
{-# INLINE length #-}
instance (FactorialMonoid a, FactorialMonoid b) => FactorialMonoid (Stateful a b) where
splitPrimePrefix :: Stateful a b -> Maybe (Stateful a b, Stateful a b)
splitPrimePrefix (Stateful (b, a)
x) = do ((b, a)
xp, (b, a)
xs) <- forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimePrefix (b, a)
x
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xp, forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xs)
splitPrimeSuffix :: Stateful a b -> Maybe (Stateful a b, Stateful a b)
splitPrimeSuffix (Stateful (b, a)
x) = do ((b, a)
xp, (b, a)
xs) <- forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimeSuffix (b, a)
x
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xp, forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xs)
span :: (Stateful a b -> Bool)
-> Stateful a b -> (Stateful a b, Stateful a b)
span Stateful a b -> Bool
p (Stateful (b, a)
x) = (forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xp, forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xs)
where ((b, a)
xp, (b, a)
xs) = forall m. FactorialMonoid m => (m -> Bool) -> m -> (m, m)
Factorial.span (Stateful a b -> Bool
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (b, a) -> Stateful a b
Stateful) (b, a)
x
spanMaybe :: forall s.
s
-> (s -> Stateful a b -> Maybe s)
-> Stateful a b
-> (Stateful a b, Stateful a b, s)
spanMaybe s
s0 s -> Stateful a b -> Maybe s
f (Stateful (b, a)
x) = (forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xp, forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xs, s
s')
where ((b, a)
xp, (b, a)
xs, s
s') = forall m s.
FactorialMonoid m =>
s -> (s -> m -> Maybe s) -> m -> (m, m, s)
Factorial.spanMaybe s
s0 s -> (b, a) -> Maybe s
f' (b, a)
x
f' :: s -> (b, a) -> Maybe s
f' s
s (b, a)
x1 = s -> Stateful a b -> Maybe s
f s
s (forall a b. (b, a) -> Stateful a b
Stateful (b, a)
x1)
spanMaybe' :: forall s.
s
-> (s -> Stateful a b -> Maybe s)
-> Stateful a b
-> (Stateful a b, Stateful a b, s)
spanMaybe' s
s0 s -> Stateful a b -> Maybe s
f (Stateful (b, a)
x) = (forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xp, forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xs, s
s')
where ((b, a)
xp, (b, a)
xs, s
s') = forall m s.
FactorialMonoid m =>
s -> (s -> m -> Maybe s) -> m -> (m, m, s)
Factorial.spanMaybe' s
s0 s -> (b, a) -> Maybe s
f' (b, a)
x
f' :: s -> (b, a) -> Maybe s
f' s
s (b, a)
x1 = s -> Stateful a b -> Maybe s
f s
s (forall a b. (b, a) -> Stateful a b
Stateful (b, a)
x1)
split :: (Stateful a b -> Bool) -> Stateful a b -> [Stateful a b]
split Stateful a b -> Bool
p (Stateful (b, a)
x) = forall a b. (a -> b) -> [a] -> [b]
List.map forall a b. (b, a) -> Stateful a b
Stateful (forall m. FactorialMonoid m => (m -> Bool) -> m -> [m]
Factorial.split (Stateful a b -> Bool
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (b, a) -> Stateful a b
Stateful) (b, a)
x)
splitAt :: Int -> Stateful a b -> (Stateful a b, Stateful a b)
splitAt Int
n (Stateful (b, a)
x) = (forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xp, forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xs)
where ((b, a)
xp, (b, a)
xs) = forall m. FactorialMonoid m => Int -> m -> (m, m)
splitAt Int
n (b, a)
x
take :: Int -> Stateful a b -> Stateful a b
take Int
n (Stateful (b, a)
x) = forall a b. (b, a) -> Stateful a b
Stateful (forall m. FactorialMonoid m => Int -> m -> m
take Int
n (b, a)
x)
drop :: Int -> Stateful a b -> Stateful a b
drop Int
n (Stateful (b, a)
x) = forall a b. (b, a) -> Stateful a b
Stateful (forall m. FactorialMonoid m => Int -> m -> m
drop Int
n (b, a)
x)
{-# INLINE splitPrimePrefix #-}
{-# INLINE splitPrimeSuffix #-}
{-# INLINE span #-}
{-# INLINE spanMaybe #-}
{-# INLINE spanMaybe' #-}
{-# INLINE splitAt #-}
{-# INLINE take #-}
{-# INLINE drop #-}
instance (FactorialMonoid a, FactorialMonoid b, StableFactorial a, StableFactorial b) => StableFactorial (Stateful a b)
instance (Monoid a, IsString b) => IsString (Stateful a b) where
fromString :: String -> Stateful a b
fromString = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IsString a => String -> a
fromString
instance (LeftGCDMonoid a, FactorialMonoid a, TextualMonoid b) => TextualMonoid (Stateful a b) where
fromText :: Text -> Stateful a b
fromText Text
t = forall a b. (b, a) -> Stateful a b
Stateful (forall t. TextualMonoid t => Text -> t
fromText Text
t, forall a. Monoid a => a
mempty)
singleton :: Char -> Stateful a b
singleton Char
c = forall a b. (b, a) -> Stateful a b
Stateful (forall t. TextualMonoid t => Char -> t
singleton Char
c, forall a. Monoid a => a
mempty)
characterPrefix :: Stateful a b -> Maybe Char
characterPrefix = forall t. TextualMonoid t => t -> Maybe Char
characterPrefix forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Stateful a b -> b
extract
splitCharacterPrefix :: Stateful a b -> Maybe (Char, Stateful a b)
splitCharacterPrefix (Stateful (b
t, a
x)) = do (Char
c, b
t') <- forall t. TextualMonoid t => t -> Maybe (Char, t)
splitCharacterPrefix b
t
forall (m :: * -> *) a. Monad m => a -> m a
return (Char
c, forall a b. (b, a) -> Stateful a b
Stateful (b
t', a
x))
map :: (Char -> Char) -> Stateful a b -> Stateful a b
map Char -> Char
f (Stateful (b
t, a
x)) = forall a b. (b, a) -> Stateful a b
Stateful (forall t. TextualMonoid t => (Char -> Char) -> t -> t
Textual.map Char -> Char
f b
t, a
x)
all :: (Char -> Bool) -> Stateful a b -> Bool
all Char -> Bool
p = forall t. TextualMonoid t => (Char -> Bool) -> t -> Bool
all Char -> Bool
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Stateful a b -> b
extract
any :: (Char -> Bool) -> Stateful a b -> Bool
any Char -> Bool
p = forall t. TextualMonoid t => (Char -> Bool) -> t -> Bool
any Char -> Bool
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Stateful a b -> b
extract
foldl :: forall a.
(a -> Stateful a b -> a)
-> (a -> Char -> a) -> a -> Stateful a b -> a
foldl a -> Stateful a b -> a
fx a -> Char -> a
fc a
a0 (Stateful (b
t, a
x)) = forall m a. Factorial m => (a -> m -> a) -> a -> m -> a
Factorial.foldl a -> a -> a
f2 (forall t a.
TextualMonoid t =>
(a -> t -> a) -> (a -> Char -> a) -> a -> t -> a
Textual.foldl a -> b -> a
f1 a -> Char -> a
fc a
a0 b
t) a
x
where f1 :: a -> b -> a
f1 a
a = a -> Stateful a b -> a
fx a
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. Monoid b => a -> Stateful b a
fromFst
f2 :: a -> a -> a
f2 a
a = a -> Stateful a b -> a
fx a
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Monoid a => b -> Stateful b a
fromSnd
foldr :: forall a.
(Stateful a b -> a -> a)
-> (Char -> a -> a) -> a -> Stateful a b -> a
foldr Stateful a b -> a -> a
fx Char -> a -> a
fc a
a (Stateful (b
t, a
x)) = forall t a.
TextualMonoid t =>
(t -> a -> a) -> (Char -> a -> a) -> a -> t -> a
Textual.foldr (Stateful a b -> a -> a
fx forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. Monoid b => a -> Stateful b a
fromFst) Char -> a -> a
fc (forall m a. Factorial m => (m -> a -> a) -> a -> m -> a
Factorial.foldr (Stateful a b -> a -> a
fx forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Monoid a => b -> Stateful b a
fromSnd) a
a a
x) b
t
foldl' :: forall a.
(a -> Stateful a b -> a)
-> (a -> Char -> a) -> a -> Stateful a b -> a
foldl' a -> Stateful a b -> a
fx a -> Char -> a
fc a
a0 (Stateful (b
t, a
x)) = a
a' seq :: forall a b. a -> b -> b
`seq` forall m a. Factorial m => (a -> m -> a) -> a -> m -> a
Factorial.foldl' a -> a -> a
f2 a
a' a
x
where a' :: a
a' = forall t a.
TextualMonoid t =>
(a -> t -> a) -> (a -> Char -> a) -> a -> t -> a
Textual.foldl' a -> b -> a
f1 a -> Char -> a
fc a
a0 b
t
f1 :: a -> b -> a
f1 a
a = a -> Stateful a b -> a
fx a
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. Monoid b => a -> Stateful b a
fromFst
f2 :: a -> a -> a
f2 a
a = a -> Stateful a b -> a
fx a
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Monoid a => b -> Stateful b a
fromSnd
foldl_' :: forall a. (a -> Char -> a) -> a -> Stateful a b -> a
foldl_' a -> Char -> a
fc a
a (Stateful (b
t, a
_)) = forall t a. TextualMonoid t => (a -> Char -> a) -> a -> t -> a
foldl_' a -> Char -> a
fc a
a b
t
foldr_ :: forall a. (Char -> a -> a) -> a -> Stateful a b -> a
foldr_ Char -> a -> a
fc a
a (Stateful (b
t, a
_)) = forall t a. TextualMonoid t => (Char -> a -> a) -> a -> t -> a
Textual.foldr_ Char -> a -> a
fc a
a b
t
toString :: (Stateful a b -> String) -> Stateful a b -> String
toString Stateful a b -> String
fx (Stateful (b
t, a
x)) = forall t. TextualMonoid t => (t -> String) -> t -> String
toString (Stateful a b -> String
fx forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. Monoid b => a -> Stateful b a
fromFst) b
t forall a. [a] -> [a] -> [a]
++ forall m n. (Factorial m, Monoid n) => (m -> n) -> m -> n
Factorial.foldMap (Stateful a b -> String
fx forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Monoid a => b -> Stateful b a
fromSnd) a
x
scanl :: (Char -> Char -> Char) -> Char -> Stateful a b -> Stateful a b
scanl Char -> Char -> Char
f Char
c (Stateful (b
t, a
x)) = forall a b. (b, a) -> Stateful a b
Stateful (forall t.
TextualMonoid t =>
(Char -> Char -> Char) -> Char -> t -> t
Textual.scanl Char -> Char -> Char
f Char
c b
t, a
x)
scanl1 :: (Char -> Char -> Char) -> Stateful a b -> Stateful a b
scanl1 Char -> Char -> Char
f (Stateful (b
t, a
x)) = forall a b. (b, a) -> Stateful a b
Stateful (forall t. TextualMonoid t => (Char -> Char -> Char) -> t -> t
Textual.scanl1 Char -> Char -> Char
f b
t, a
x)
scanr :: (Char -> Char -> Char) -> Char -> Stateful a b -> Stateful a b
scanr Char -> Char -> Char
f Char
c (Stateful (b
t, a
x)) = forall a b. (b, a) -> Stateful a b
Stateful (forall t.
TextualMonoid t =>
(Char -> Char -> Char) -> Char -> t -> t
Textual.scanr Char -> Char -> Char
f Char
c b
t, a
x)
scanr1 :: (Char -> Char -> Char) -> Stateful a b -> Stateful a b
scanr1 Char -> Char -> Char
f (Stateful (b
t, a
x)) = forall a b. (b, a) -> Stateful a b
Stateful (forall t. TextualMonoid t => (Char -> Char -> Char) -> t -> t
Textual.scanr1 Char -> Char -> Char
f b
t, a
x)
mapAccumL :: forall a.
(a -> Char -> (a, Char)) -> a -> Stateful a b -> (a, Stateful a b)
mapAccumL a -> Char -> (a, Char)
f a
a (Stateful (b
t, a
x)) = (a
a', forall a b. (b, a) -> Stateful a b
Stateful (b
t', a
x))
where (a
a', b
t') = forall t a.
TextualMonoid t =>
(a -> Char -> (a, Char)) -> a -> t -> (a, t)
Textual.mapAccumL a -> Char -> (a, Char)
f a
a b
t
mapAccumR :: forall a.
(a -> Char -> (a, Char)) -> a -> Stateful a b -> (a, Stateful a b)
mapAccumR a -> Char -> (a, Char)
f a
a (Stateful (b
t, a
x)) = (a
a', forall a b. (b, a) -> Stateful a b
Stateful (b
t', a
x))
where (a
a', b
t') = forall t a.
TextualMonoid t =>
(a -> Char -> (a, Char)) -> a -> t -> (a, t)
Textual.mapAccumR a -> Char -> (a, Char)
f a
a b
t
span :: (Stateful a b -> Bool)
-> (Char -> Bool) -> Stateful a b -> (Stateful a b, Stateful a b)
span Stateful a b -> Bool
pt Char -> Bool
pc (Stateful (b
t, a
x)) = (forall a b. (b, a) -> Stateful a b
Stateful (b
tp, a
xp), forall a b. (b, a) -> Stateful a b
Stateful (b
ts, a
xs))
where (b
tp, b
ts) = forall t.
TextualMonoid t =>
(t -> Bool) -> (Char -> Bool) -> t -> (t, t)
Textual.span (Stateful a b -> Bool
pt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. Monoid b => a -> Stateful b a
fromFst) Char -> Bool
pc b
t
(a
xp, a
xs) | forall m. MonoidNull m => m -> Bool
null b
ts = forall m. FactorialMonoid m => (m -> Bool) -> m -> (m, m)
Factorial.span (Stateful a b -> Bool
pt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Monoid a => b -> Stateful b a
fromSnd) a
x
| Bool
otherwise = (forall a. Monoid a => a
mempty, a
x)
span_ :: Bool
-> (Char -> Bool) -> Stateful a b -> (Stateful a b, Stateful a b)
span_ Bool
bt Char -> Bool
pc (Stateful (b
t, a
x)) = (forall a b. (b, a) -> Stateful a b
Stateful (b
tp, a
xp), forall a b. (b, a) -> Stateful a b
Stateful (b
ts, a
xs))
where (b
tp, b
ts) = forall t. TextualMonoid t => Bool -> (Char -> Bool) -> t -> (t, t)
Textual.span_ Bool
bt Char -> Bool
pc b
t
(a
xp, a
xs) | forall m. MonoidNull m => m -> Bool
null b
ts Bool -> Bool -> Bool
&& Bool
bt = (a
x, forall a. Monoid a => a
mempty)
| Bool
otherwise = (forall a. Monoid a => a
mempty, a
x)
break :: (Stateful a b -> Bool)
-> (Char -> Bool) -> Stateful a b -> (Stateful a b, Stateful a b)
break Stateful a b -> Bool
pt Char -> Bool
pc (Stateful (b
t, a
x)) = (forall a b. (b, a) -> Stateful a b
Stateful (b
tp, a
xp), forall a b. (b, a) -> Stateful a b
Stateful (b
ts, a
xs))
where (b
tp, b
ts) = forall t.
TextualMonoid t =>
(t -> Bool) -> (Char -> Bool) -> t -> (t, t)
Textual.break (Stateful a b -> Bool
pt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. Monoid b => a -> Stateful b a
fromFst) Char -> Bool
pc b
t
(a
xp, a
xs) | forall m. MonoidNull m => m -> Bool
null b
ts = forall m. FactorialMonoid m => (m -> Bool) -> m -> (m, m)
Factorial.break (Stateful a b -> Bool
pt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Monoid a => b -> Stateful b a
fromSnd) a
x
| Bool
otherwise = (forall a. Monoid a => a
mempty, a
x)
spanMaybe :: forall s.
s
-> (s -> Stateful a b -> Maybe s)
-> (s -> Char -> Maybe s)
-> Stateful a b
-> (Stateful a b, Stateful a b, s)
spanMaybe s
s0 s -> Stateful a b -> Maybe s
ft s -> Char -> Maybe s
fc (Stateful (b
t, a
x)) = (forall a b. (b, a) -> Stateful a b
Stateful (b
tp, a
xp), forall a b. (b, a) -> Stateful a b
Stateful (b
ts, a
xs), s
s'')
where (b
tp, b
ts, s
s') = forall t s.
TextualMonoid t =>
s
-> (s -> t -> Maybe s) -> (s -> Char -> Maybe s) -> t -> (t, t, s)
Textual.spanMaybe s
s0 s -> b -> Maybe s
ft' s -> Char -> Maybe s
fc b
t
(a
xp, a
xs, s
s'') | forall m. MonoidNull m => m -> Bool
null b
ts = forall m s.
FactorialMonoid m =>
s -> (s -> m -> Maybe s) -> m -> (m, m, s)
Factorial.spanMaybe s
s' s -> a -> Maybe s
ft'' a
x
| Bool
otherwise = (forall a. Monoid a => a
mempty, a
x, s
s')
ft' :: s -> b -> Maybe s
ft' s
s b
t1 = s -> Stateful a b -> Maybe s
ft s
s (forall a b. (b, a) -> Stateful a b
Stateful (b
t1, forall a. Monoid a => a
mempty))
ft'' :: s -> a -> Maybe s
ft'' s
s a
x1 = s -> Stateful a b -> Maybe s
ft s
s (forall a b. (b, a) -> Stateful a b
Stateful (forall a. Monoid a => a
mempty, a
x1))
spanMaybe' :: forall s.
s
-> (s -> Stateful a b -> Maybe s)
-> (s -> Char -> Maybe s)
-> Stateful a b
-> (Stateful a b, Stateful a b, s)
spanMaybe' s
s0 s -> Stateful a b -> Maybe s
ft s -> Char -> Maybe s
fc (Stateful (b
t, a
x)) = (forall a b. (b, a) -> Stateful a b
Stateful (b
tp, a
xp), forall a b. (b, a) -> Stateful a b
Stateful (b
ts, a
xs), s
s'')
where (b
tp, b
ts, s
s') = forall t s.
TextualMonoid t =>
s
-> (s -> t -> Maybe s) -> (s -> Char -> Maybe s) -> t -> (t, t, s)
Textual.spanMaybe' s
s0 s -> b -> Maybe s
ft' s -> Char -> Maybe s
fc b
t
(a
xp, a
xs, s
s'') | forall m. MonoidNull m => m -> Bool
null b
ts = forall m s.
FactorialMonoid m =>
s -> (s -> m -> Maybe s) -> m -> (m, m, s)
Factorial.spanMaybe' s
s' s -> a -> Maybe s
ft'' a
x
| Bool
otherwise = (forall a. Monoid a => a
mempty, a
x, s
s')
ft' :: s -> b -> Maybe s
ft' s
s b
t1 = s -> Stateful a b -> Maybe s
ft s
s (forall a b. (b, a) -> Stateful a b
Stateful (b
t1, forall a. Monoid a => a
mempty))
ft'' :: s -> a -> Maybe s
ft'' s
s a
x1 = s -> Stateful a b -> Maybe s
ft s
s (forall a b. (b, a) -> Stateful a b
Stateful (forall a. Monoid a => a
mempty, a
x1))
spanMaybe_' :: forall s.
s
-> (s -> Char -> Maybe s)
-> Stateful a b
-> (Stateful a b, Stateful a b, s)
spanMaybe_' s
s0 s -> Char -> Maybe s
fc (Stateful (b
t, a
x)) = (forall a b. (b, a) -> Stateful a b
Stateful (b
tp, a
xp), forall a b. (b, a) -> Stateful a b
Stateful (b
ts, a
xs), s
s')
where (b
tp, b
ts, s
s') = forall t s.
TextualMonoid t =>
s -> (s -> Char -> Maybe s) -> t -> (t, t, s)
Textual.spanMaybe_' s
s0 s -> Char -> Maybe s
fc b
t
(a
xp, a
xs) | forall m. MonoidNull m => m -> Bool
null b
ts = (a
x, forall a. Monoid a => a
mempty)
| Bool
otherwise = (forall a. Monoid a => a
mempty, a
x)
split :: (Char -> Bool) -> Stateful a b -> [Stateful a b]
split Char -> Bool
p (Stateful (b
t, a
x)) = forall {b} {c}. ([Stateful a b] -> c) -> [b] -> c
restore forall a. a -> a
id [b]
ts
where ts :: [b]
ts = forall t. TextualMonoid t => (Char -> Bool) -> t -> [t]
Textual.split Char -> Bool
p b
t
restore :: ([Stateful a b] -> c) -> [b] -> c
restore [Stateful a b] -> c
f [b
t1] = [Stateful a b] -> c
f [forall a b. (b, a) -> Stateful a b
Stateful (b
t1, a
x)]
restore [Stateful a b] -> c
f ~(b
hd:[b]
tl) = ([Stateful a b] -> c) -> [b] -> c
restore ([Stateful a b] -> c
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a b. (b, a) -> Stateful a b
Stateful (b
hd, forall a. Monoid a => a
mempty)forall a. a -> [a] -> [a]
:)) [b]
tl
find :: (Char -> Bool) -> Stateful a b -> Maybe Char
find Char -> Bool
p = forall t. TextualMonoid t => (Char -> Bool) -> t -> Maybe Char
find Char -> Bool
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Stateful a b -> b
extract
elem :: Char -> Stateful a b -> Bool
elem Char
c = forall t. TextualMonoid t => Char -> t -> Bool
elem Char
c forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Stateful a b -> b
extract
{-# INLINE characterPrefix #-}
{-# INLINE splitCharacterPrefix #-}
{-# INLINE map #-}
{-# INLINE foldl' #-}
{-# INLINE foldr #-}
{-# INLINE spanMaybe' #-}
{-# INLINE span #-}
{-# INLINE spanMaybe_' #-}
{-# INLINE span_ #-}
{-# INLINE any #-}
{-# INLINE all #-}
{-# INLINE split #-}
{-# INLINE find #-}
{-# INLINE elem #-}
{-# INLINE fromFst #-}
fromFst :: Monoid b => a -> Stateful b a
fromFst :: forall b a. Monoid b => a -> Stateful b a
fromFst a
a = forall a b. (b, a) -> Stateful a b
Stateful (a
a, forall a. Monoid a => a
mempty)
{-# INLINE fromSnd #-}
fromSnd :: Monoid a => b -> Stateful b a
fromSnd :: forall a b. Monoid a => b -> Stateful b a
fromSnd b
b = forall a b. (b, a) -> Stateful a b
Stateful (forall a. Monoid a => a
mempty, b
b)