{-# 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 (Typeable (Stateful a b)
Typeable (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 (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Stateful a b))
-> (Stateful a b -> Constr)
-> (Stateful a b -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (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)))
-> ((forall b. Data b => b -> b) -> Stateful a b -> Stateful a b)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Stateful a b -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Stateful a b -> r)
-> (forall u. (forall d. Data d => d -> u) -> Stateful a b -> [u])
-> (forall u.
Int -> (forall d. Data d => d -> u) -> Stateful a b -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Stateful a b -> m (Stateful a b))
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Stateful a b -> m (Stateful a b))
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Stateful a b -> m (Stateful a b))
-> Data (Stateful a b)
Stateful a b -> Constr
Stateful a b -> DataType
(forall b. Data b => b -> b) -> Stateful a b -> Stateful a b
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) -> Stateful a b -> u
forall u. (forall d. Data d => d -> u) -> Stateful a b -> [u]
forall a b. (Data a, Data b) => Typeable (Stateful a b)
forall a b. (Data a, Data b) => Stateful a b -> Constr
forall a b. (Data a, Data b) => Stateful a b -> DataType
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 r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Stateful a b -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Stateful a b -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Stateful a b -> m (Stateful a b)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Stateful a b -> m (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. Data d => c (t d)) -> Maybe (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))
$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)
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)
$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)
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Stateful a b)
$ctoConstr :: forall a b. (Data a, Data b) => Stateful a b -> Constr
toConstr :: Stateful a b -> Constr
$cdataTypeOf :: forall a b. (Data a, Data b) => Stateful a b -> DataType
dataTypeOf :: Stateful a b -> DataType
$cdataCast1 :: forall a b (t :: * -> *) (c :: * -> *).
(Data a, Data b, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Stateful a b))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> 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))
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Stateful a b))
$cgmapT :: forall a b.
(Data a, Data b) =>
(forall b. Data b => b -> b) -> Stateful a b -> Stateful a b
gmapT :: (forall b. Data b => b -> b) -> Stateful a b -> Stateful a b
$cgmapQl :: 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
$cgmapQr :: forall a b r r'.
(Data a, Data b) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Stateful a b -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Stateful a b -> r
$cgmapQ :: forall a b u.
(Data a, Data b) =>
(forall d. Data d => d -> u) -> Stateful a b -> [u]
gmapQ :: forall u. (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
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Stateful a b -> u
$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)
gmapM :: forall (m :: * -> *).
Monad 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)
gmapMp :: 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)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Stateful a b -> m (Stateful a b)
Data, Stateful a b -> Stateful a b -> Bool
(Stateful a b -> Stateful a b -> Bool)
-> (Stateful a b -> Stateful a b -> Bool) -> Eq (Stateful a b)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall a b. (Eq b, Eq a) => 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
/= :: Stateful a b -> Stateful a b -> Bool
Eq, Eq (Stateful a b)
Eq (Stateful a b) =>
(Stateful a b -> Stateful a b -> Ordering)
-> (Stateful a b -> Stateful a b -> Bool)
-> (Stateful a b -> Stateful a b -> Bool)
-> (Stateful a b -> Stateful a b -> Bool)
-> (Stateful a b -> Stateful a b -> Bool)
-> (Stateful a b -> Stateful a b -> Stateful a b)
-> (Stateful a b -> Stateful a b -> Stateful a b)
-> Ord (Stateful a b)
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
$ccompare :: forall a b.
(Ord b, Ord a) =>
Stateful a b -> Stateful a b -> Ordering
compare :: Stateful a b -> Stateful a b -> Ordering
$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
>= :: Stateful a b -> Stateful a b -> Bool
$cmax :: 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
$cmin :: 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
Ord, Int -> Stateful a b -> ShowS
[Stateful a b] -> ShowS
Stateful a b -> String
(Int -> Stateful a b -> ShowS)
-> (Stateful a b -> String)
-> ([Stateful a b] -> ShowS)
-> Show (Stateful a b)
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
$cshowsPrec :: forall a b. (Show b, Show a) => Int -> Stateful a b -> ShowS
showsPrec :: Int -> Stateful a b -> ShowS
$cshow :: forall a b. (Show b, Show a) => Stateful a b -> String
show :: Stateful a b -> String
$cshowList :: forall a b. (Show b, Show a) => [Stateful a b] -> ShowS
showList :: [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
_)) = (b, a) -> Stateful a b
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)) = (b, a) -> Stateful a b
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 = (a, a) -> Stateful a a
forall a b. (b, a) -> Stateful a b
Stateful (a
m, a
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) = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (a -> b
f a
x, a -> a -> a
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 = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful ((b, a)
x (b, a) -> (b, a) -> (b, a)
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 = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b, a)
forall a. Monoid a => a
mempty
mappend :: Stateful a b -> Stateful a b -> Stateful a b
mappend = Stateful a b -> Stateful a b -> Stateful a b
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) = (b, a) -> Bool
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') = (b, a) -> (b, a) -> Bool
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') = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful ((b, a) -> Stateful a b) -> Maybe (b, a) -> Maybe (Stateful a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (b, a) -> (b, a) -> Maybe (b, a)
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') = (b, a) -> (b, a) -> Bool
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') = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful ((b, a) -> Stateful a b) -> Maybe (b, a) -> Maybe (Stateful a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (b, a) -> (b, a) -> Maybe (b, a)
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') = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful ((b, a) -> (b, a) -> (b, a)
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') = ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b, a)
prefix, (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b, a)
suffix1, (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b, a)
suffix2)
where ((b, a)
prefix, (b, a)
suffix1, (b, a)
suffix2) = (b, a) -> (b, a) -> ((b, a), (b, a), (b, a))
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') = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful ((b, a) -> (b, a) -> (b, a)
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) = ((b, a) -> Stateful a b) -> [(b, a)] -> [Stateful a b]
forall a b. (a -> b) -> [a] -> [b]
List.map (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful ((b, a) -> [(b, a)]
forall m. Factorial m => m -> [m]
factors (b, a)
x)
length :: Stateful a b -> Int
length (Stateful (b, a)
x) = (b, a) -> Int
forall m. Factorial m => m -> Int
length (b, a)
x
reverse :: Stateful a b -> Stateful a b
reverse (Stateful (b, a)
x) = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful ((b, a) -> (b, a)
forall m. Factorial m => m -> m
reverse (b, a)
x)
primePrefix :: Stateful a b -> Stateful a b
primePrefix (Stateful (b, a)
x) = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful ((b, a) -> (b, a)
forall m. Factorial m => m -> m
primePrefix (b, a)
x)
primeSuffix :: Stateful a b -> Stateful a b
primeSuffix (Stateful (b, a)
x) = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful ((b, a) -> (b, a)
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) = (a -> (b, a) -> a) -> a -> (b, a) -> a
forall m a. Factorial m => (a -> m -> a) -> a -> m -> a
forall a. (a -> (b, a) -> a) -> a -> (b, a) -> 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 ((b, a) -> Stateful a b
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) = (a -> (b, a) -> a) -> a -> (b, a) -> a
forall m a. Factorial m => (a -> m -> a) -> a -> m -> a
forall a. (a -> (b, a) -> a) -> a -> (b, a) -> 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 ((b, a) -> Stateful a b
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) = ((b, a) -> a -> a) -> a -> (b, a) -> a
forall m a. Factorial m => (m -> a -> a) -> a -> m -> a
forall a. ((b, a) -> a -> a) -> a -> (b, a) -> a
Factorial.foldr (Stateful a b -> a -> a
f (Stateful a b -> a -> a)
-> ((b, a) -> Stateful a b) -> (b, a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b, a) -> Stateful a b
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) = ((b, a) -> n) -> (b, a) -> n
forall n. Monoid n => ((b, a) -> n) -> (b, a) -> n
forall m n. (Factorial m, Monoid n) => (m -> n) -> m -> n
Factorial.foldMap (Stateful a b -> n
f (Stateful a b -> n) -> ((b, a) -> Stateful a b) -> (b, a) -> n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b, a) -> Stateful a b
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) <- (b, a) -> Maybe ((b, a), (b, a))
forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimePrefix (b, a)
x
(Stateful a b, Stateful a b) -> Maybe (Stateful a b, Stateful a b)
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xp, (b, a) -> Stateful a b
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) <- (b, a) -> Maybe ((b, a), (b, a))
forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimeSuffix (b, a)
x
(Stateful a b, Stateful a b) -> Maybe (Stateful a b, Stateful a b)
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xp, (b, a) -> Stateful a b
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) = ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xp, (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xs)
where ((b, a)
xp, (b, a)
xs) = ((b, a) -> Bool) -> (b, a) -> ((b, a), (b, a))
forall m. FactorialMonoid m => (m -> Bool) -> m -> (m, m)
Factorial.span (Stateful a b -> Bool
p (Stateful a b -> Bool)
-> ((b, a) -> Stateful a b) -> (b, a) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b, a) -> Stateful a b
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) = ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xp, (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xs, s
s')
where ((b, a)
xp, (b, a)
xs, s
s') = s -> (s -> (b, a) -> Maybe s) -> (b, a) -> ((b, a), (b, a), s)
forall s.
s -> (s -> (b, a) -> Maybe s) -> (b, a) -> ((b, a), (b, a), 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 ((b, a) -> Stateful a b
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) = ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xp, (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xs, s
s')
where ((b, a)
xp, (b, a)
xs, s
s') = s -> (s -> (b, a) -> Maybe s) -> (b, a) -> ((b, a), (b, a), s)
forall s.
s -> (s -> (b, a) -> Maybe s) -> (b, a) -> ((b, a), (b, a), 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 ((b, a) -> Stateful a b
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) = ((b, a) -> Stateful a b) -> [(b, a)] -> [Stateful a b]
forall a b. (a -> b) -> [a] -> [b]
List.map (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (((b, a) -> Bool) -> (b, a) -> [(b, a)]
forall m. FactorialMonoid m => (m -> Bool) -> m -> [m]
Factorial.split (Stateful a b -> Bool
p (Stateful a b -> Bool)
-> ((b, a) -> Stateful a b) -> (b, a) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b, a) -> Stateful a b
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) = ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xp, (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b, a)
xs)
where ((b, a)
xp, (b, a)
xs) = Int -> (b, a) -> ((b, a), (b, a))
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) = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (Int -> (b, a) -> (b, a)
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) = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (Int -> (b, a) -> (b, a)
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 = b -> Stateful a b
forall a. a -> Stateful a a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (b -> Stateful a b) -> (String -> b) -> String -> Stateful a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> b
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 = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (Text -> b
forall t. TextualMonoid t => Text -> t
fromText Text
t, a
forall a. Monoid a => a
mempty)
singleton :: Char -> Stateful a b
singleton Char
c = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (Char -> b
forall t. TextualMonoid t => Char -> t
singleton Char
c, a
forall a. Monoid a => a
mempty)
characterPrefix :: Stateful a b -> Maybe Char
characterPrefix = b -> Maybe Char
forall t. TextualMonoid t => t -> Maybe Char
characterPrefix (b -> Maybe Char)
-> (Stateful a b -> b) -> Stateful a b -> Maybe Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stateful a b -> b
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') <- b -> Maybe (Char, b)
forall t. TextualMonoid t => t -> Maybe (Char, t)
splitCharacterPrefix b
t
(Char, Stateful a b) -> Maybe (Char, Stateful a b)
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return (Char
c, (b, a) -> Stateful a b
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)) = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful ((Char -> Char) -> b -> b
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 = (Char -> Bool) -> b -> Bool
forall t. TextualMonoid t => (Char -> Bool) -> t -> Bool
all Char -> Bool
p (b -> Bool) -> (Stateful a b -> b) -> Stateful a b -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stateful a b -> b
forall a b. Stateful a b -> b
extract
any :: (Char -> Bool) -> Stateful a b -> Bool
any Char -> Bool
p = (Char -> Bool) -> b -> Bool
forall t. TextualMonoid t => (Char -> Bool) -> t -> Bool
any Char -> Bool
p (b -> Bool) -> (Stateful a b -> b) -> Stateful a b -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stateful a b -> b
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)) = (a -> a -> a) -> a -> a -> a
forall m a. Factorial m => (a -> m -> a) -> a -> m -> a
forall a. (a -> a -> a) -> a -> a -> a
Factorial.foldl a -> a -> a
f2 ((a -> b -> a) -> (a -> Char -> a) -> a -> b -> a
forall t a.
TextualMonoid t =>
(a -> t -> a) -> (a -> Char -> a) -> a -> t -> a
forall a. (a -> b -> a) -> (a -> Char -> a) -> a -> b -> 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 (Stateful a b -> a) -> (b -> Stateful a b) -> b -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Stateful a b
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 (Stateful a b -> a) -> (a -> Stateful a b) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Stateful a b
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)) = (b -> a -> a) -> (Char -> a -> a) -> a -> b -> a
forall t a.
TextualMonoid t =>
(t -> a -> a) -> (Char -> a -> a) -> a -> t -> a
forall a. (b -> a -> a) -> (Char -> a -> a) -> a -> b -> a
Textual.foldr (Stateful a b -> a -> a
fx (Stateful a b -> a -> a) -> (b -> Stateful a b) -> b -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Stateful a b
forall b a. Monoid b => a -> Stateful b a
fromFst) Char -> a -> a
fc ((a -> a -> a) -> a -> a -> a
forall m a. Factorial m => (m -> a -> a) -> a -> m -> a
forall a. (a -> a -> a) -> a -> a -> a
Factorial.foldr (Stateful a b -> a -> a
fx (Stateful a b -> a -> a) -> (a -> Stateful a b) -> a -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Stateful a b
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' a -> a -> a
forall a b. a -> b -> b
`seq` (a -> a -> a) -> a -> a -> a
forall m a. Factorial m => (a -> m -> a) -> a -> m -> a
forall a. (a -> a -> a) -> a -> a -> a
Factorial.foldl' a -> a -> a
f2 a
a' a
x
where a' :: a
a' = (a -> b -> a) -> (a -> Char -> a) -> a -> b -> a
forall t a.
TextualMonoid t =>
(a -> t -> a) -> (a -> Char -> a) -> a -> t -> a
forall a. (a -> b -> a) -> (a -> Char -> a) -> a -> b -> 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 (Stateful a b -> a) -> (b -> Stateful a b) -> b -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Stateful a b
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 (Stateful a b -> a) -> (a -> Stateful a b) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Stateful a b
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
_)) = (a -> Char -> a) -> a -> b -> a
forall t a. TextualMonoid t => (a -> Char -> a) -> a -> t -> a
forall a. (a -> Char -> a) -> a -> b -> 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
_)) = (Char -> a -> a) -> a -> b -> a
forall t a. TextualMonoid t => (Char -> a -> a) -> a -> t -> a
forall a. (Char -> a -> a) -> a -> b -> 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)) = (b -> String) -> b -> String
forall t. TextualMonoid t => (t -> String) -> t -> String
toString (Stateful a b -> String
fx (Stateful a b -> String) -> (b -> Stateful a b) -> b -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Stateful a b
forall b a. Monoid b => a -> Stateful b a
fromFst) b
t String -> ShowS
forall a. [a] -> [a] -> [a]
++ (a -> String) -> a -> String
forall n. Monoid n => (a -> n) -> a -> n
forall m n. (Factorial m, Monoid n) => (m -> n) -> m -> n
Factorial.foldMap (Stateful a b -> String
fx (Stateful a b -> String) -> (a -> Stateful a b) -> a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Stateful a b
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)) = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful ((Char -> Char -> Char) -> Char -> b -> b
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)) = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful ((Char -> Char -> Char) -> b -> b
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)) = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful ((Char -> Char -> Char) -> Char -> b -> b
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)) = (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful ((Char -> Char -> Char) -> b -> b
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', (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
t', a
x))
where (a
a', b
t') = (a -> Char -> (a, Char)) -> a -> b -> (a, b)
forall t a.
TextualMonoid t =>
(a -> Char -> (a, Char)) -> a -> t -> (a, t)
forall a. (a -> Char -> (a, Char)) -> a -> b -> (a, b)
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', (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
t', a
x))
where (a
a', b
t') = (a -> Char -> (a, Char)) -> a -> b -> (a, b)
forall t a.
TextualMonoid t =>
(a -> Char -> (a, Char)) -> a -> t -> (a, t)
forall a. (a -> Char -> (a, Char)) -> a -> b -> (a, b)
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)) = ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
tp, a
xp), (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
ts, a
xs))
where (b
tp, b
ts) = (b -> Bool) -> (Char -> Bool) -> b -> (b, b)
forall t.
TextualMonoid t =>
(t -> Bool) -> (Char -> Bool) -> t -> (t, t)
Textual.span (Stateful a b -> Bool
pt (Stateful a b -> Bool) -> (b -> Stateful a b) -> b -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Stateful a b
forall b a. Monoid b => a -> Stateful b a
fromFst) Char -> Bool
pc b
t
(a
xp, a
xs) | b -> Bool
forall m. MonoidNull m => m -> Bool
null b
ts = (a -> Bool) -> a -> (a, a)
forall m. FactorialMonoid m => (m -> Bool) -> m -> (m, m)
Factorial.span (Stateful a b -> Bool
pt (Stateful a b -> Bool) -> (a -> Stateful a b) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Stateful a b
forall a b. Monoid a => b -> Stateful b a
fromSnd) a
x
| Bool
otherwise = (a
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)) = ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
tp, a
xp), (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
ts, a
xs))
where (b
tp, b
ts) = Bool -> (Char -> Bool) -> b -> (b, b)
forall t. TextualMonoid t => Bool -> (Char -> Bool) -> t -> (t, t)
Textual.span_ Bool
bt Char -> Bool
pc b
t
(a
xp, a
xs) | b -> Bool
forall m. MonoidNull m => m -> Bool
null b
ts Bool -> Bool -> Bool
&& Bool
bt = (a
x, a
forall a. Monoid a => a
mempty)
| Bool
otherwise = (a
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)) = ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
tp, a
xp), (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
ts, a
xs))
where (b
tp, b
ts) = (b -> Bool) -> (Char -> Bool) -> b -> (b, b)
forall t.
TextualMonoid t =>
(t -> Bool) -> (Char -> Bool) -> t -> (t, t)
Textual.break (Stateful a b -> Bool
pt (Stateful a b -> Bool) -> (b -> Stateful a b) -> b -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Stateful a b
forall b a. Monoid b => a -> Stateful b a
fromFst) Char -> Bool
pc b
t
(a
xp, a
xs) | b -> Bool
forall m. MonoidNull m => m -> Bool
null b
ts = (a -> Bool) -> a -> (a, a)
forall m. FactorialMonoid m => (m -> Bool) -> m -> (m, m)
Factorial.break (Stateful a b -> Bool
pt (Stateful a b -> Bool) -> (a -> Stateful a b) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Stateful a b
forall a b. Monoid a => b -> Stateful b a
fromSnd) a
x
| Bool
otherwise = (a
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)) = ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
tp, a
xp), (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
ts, a
xs), s
s'')
where (b
tp, b
ts, s
s') = s
-> (s -> b -> Maybe s) -> (s -> Char -> Maybe s) -> b -> (b, b, s)
forall s.
s
-> (s -> b -> Maybe s) -> (s -> Char -> Maybe s) -> b -> (b, b, 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'') | b -> Bool
forall m. MonoidNull m => m -> Bool
null b
ts = s -> (s -> a -> Maybe s) -> a -> (a, a, s)
forall s. s -> (s -> a -> Maybe s) -> a -> (a, a, s)
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 = (a
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 ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
t1, a
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 ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
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)) = ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
tp, a
xp), (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
ts, a
xs), s
s'')
where (b
tp, b
ts, s
s') = s
-> (s -> b -> Maybe s) -> (s -> Char -> Maybe s) -> b -> (b, b, s)
forall s.
s
-> (s -> b -> Maybe s) -> (s -> Char -> Maybe s) -> b -> (b, b, 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'') | b -> Bool
forall m. MonoidNull m => m -> Bool
null b
ts = s -> (s -> a -> Maybe s) -> a -> (a, a, s)
forall s. s -> (s -> a -> Maybe s) -> a -> (a, a, s)
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 = (a
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 ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
t1, a
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 ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
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)) = ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
tp, a
xp), (b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
ts, a
xs), s
s')
where (b
tp, b
ts, s
s') = s -> (s -> Char -> Maybe s) -> b -> (b, b, s)
forall s. s -> (s -> Char -> Maybe s) -> b -> (b, b, 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) | b -> Bool
forall m. MonoidNull m => m -> Bool
null b
ts = (a
x, a
forall a. Monoid a => a
mempty)
| Bool
otherwise = (a
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)) = ([Stateful a b] -> [Stateful a b]) -> [b] -> [Stateful a b]
forall {b} {c}. ([Stateful a b] -> c) -> [b] -> c
restore [Stateful a b] -> [Stateful a b]
forall a. a -> a
id [b]
ts
where ts :: [b]
ts = (Char -> Bool) -> b -> [b]
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 [(b, a) -> Stateful a b
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 ([Stateful a b] -> c)
-> ([Stateful a b] -> [Stateful a b]) -> [Stateful a b] -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((b, a) -> Stateful a b
forall a b. (b, a) -> Stateful a b
Stateful (b
hd, a
forall a. Monoid a => a
mempty)Stateful a b -> [Stateful a b] -> [Stateful a b]
forall a. a -> [a] -> [a]
:)) [b]
tl
find :: (Char -> Bool) -> Stateful a b -> Maybe Char
find Char -> Bool
p = (Char -> Bool) -> b -> Maybe Char
forall t. TextualMonoid t => (Char -> Bool) -> t -> Maybe Char
find Char -> Bool
p (b -> Maybe Char)
-> (Stateful a b -> b) -> Stateful a b -> Maybe Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stateful a b -> b
forall a b. Stateful a b -> b
extract
elem :: Char -> Stateful a b -> Bool
elem Char
c = Char -> b -> Bool
forall t. TextualMonoid t => Char -> t -> Bool
elem Char
c (b -> Bool) -> (Stateful a b -> b) -> Stateful a b -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stateful a b -> b
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 = (a, b) -> Stateful b a
forall a b. (b, a) -> Stateful a b
Stateful (a
a, b
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 = (a, b) -> Stateful b a
forall a b. (b, a) -> Stateful a b
Stateful (a
forall a. Monoid a => a
mempty, b
b)