{-
    Copyright 2023 Mario Blazevic

    License: BSD3 (see BSD3-LICENSE.txt file)
-}

-- | This module defines the monoid transformer data type 'Shadowed'.
{-# LANGUAGE Haskell2010, DeriveDataTypeable #-}

module Data.Monoid.Instances.PrefixMemory (
   Shadowed, shadowed, content, prefix
   )
where

import Control.Applicative -- (Applicative(..))
import qualified Data.List as List
import Data.String (IsString(fromString))

import Data.Data (Data, Typeable)
import Data.Semigroup (Semigroup(..))
import Data.Monoid (Monoid(..), Endo(..))
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, filter, foldl, foldl1, foldr, foldr1, lines, map, concatMap,
                       length, null, reverse, scanl, scanr, scanl1, scanr1, span, splitAt)

-- | Monoid transformer that keeps track of the former 'prefix' of its 'content'. All functions that return a suffix
-- of their argument, such as 'stripPrefix' or 'commonSuffix', preserve the discarded 'prefix'.
data Shadowed m = Shadowed{forall m. Shadowed m -> m
prefix :: !m,
                           -- ^ used to precede the 'content' but has been consumed
                           forall m. Shadowed m -> m
content :: !m
                           -- ^ the present value
                          } deriving (Typeable (Shadowed m)
Typeable (Shadowed m) =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> Shadowed m -> c (Shadowed m))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Shadowed m))
-> (Shadowed m -> Constr)
-> (Shadowed m -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (Shadowed m)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (Shadowed m)))
-> ((forall b. Data b => b -> b) -> Shadowed m -> Shadowed m)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Shadowed m -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Shadowed m -> r)
-> (forall u. (forall d. Data d => d -> u) -> Shadowed m -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> Shadowed m -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Shadowed m -> m (Shadowed m))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Shadowed m -> m (Shadowed m))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Shadowed m -> m (Shadowed m))
-> Data (Shadowed m)
Shadowed m -> Constr
Shadowed m -> DataType
(forall b. Data b => b -> b) -> Shadowed m -> Shadowed m
forall m. Data m => Typeable (Shadowed m)
forall m. Data m => Shadowed m -> Constr
forall m. Data m => Shadowed m -> DataType
forall m.
Data m =>
(forall b. Data b => b -> b) -> Shadowed m -> Shadowed m
forall m u.
Data m =>
Int -> (forall d. Data d => d -> u) -> Shadowed m -> u
forall m u.
Data m =>
(forall d. Data d => d -> u) -> Shadowed m -> [u]
forall m r r'.
Data m =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Shadowed m -> r
forall m r r'.
Data m =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Shadowed m -> r
forall m (m :: * -> *).
(Data m, Monad m) =>
(forall d. Data d => d -> m d) -> Shadowed m -> m (Shadowed m)
forall m (m :: * -> *).
(Data m, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Shadowed m -> m (Shadowed m)
forall m (c :: * -> *).
Data m =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Shadowed m)
forall m (c :: * -> *).
Data m =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Shadowed m -> c (Shadowed m)
forall m (t :: * -> *) (c :: * -> *).
(Data m, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Shadowed m))
forall m (t :: * -> * -> *) (c :: * -> *).
(Data m, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Shadowed m))
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) -> Shadowed m -> u
forall u. (forall d. Data d => d -> u) -> Shadowed m -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Shadowed m -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Shadowed m -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Shadowed m -> m (Shadowed m)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Shadowed m -> m (Shadowed m)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Shadowed m)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Shadowed m -> c (Shadowed m)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Shadowed m))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Shadowed m))
$cgfoldl :: forall m (c :: * -> *).
Data m =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Shadowed m -> c (Shadowed m)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Shadowed m -> c (Shadowed m)
$cgunfold :: forall m (c :: * -> *).
Data m =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Shadowed m)
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Shadowed m)
$ctoConstr :: forall m. Data m => Shadowed m -> Constr
toConstr :: Shadowed m -> Constr
$cdataTypeOf :: forall m. Data m => Shadowed m -> DataType
dataTypeOf :: Shadowed m -> DataType
$cdataCast1 :: forall m (t :: * -> *) (c :: * -> *).
(Data m, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Shadowed m))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Shadowed m))
$cdataCast2 :: forall m (t :: * -> * -> *) (c :: * -> *).
(Data m, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Shadowed m))
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Shadowed m))
$cgmapT :: forall m.
Data m =>
(forall b. Data b => b -> b) -> Shadowed m -> Shadowed m
gmapT :: (forall b. Data b => b -> b) -> Shadowed m -> Shadowed m
$cgmapQl :: forall m r r'.
Data m =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Shadowed m -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Shadowed m -> r
$cgmapQr :: forall m r r'.
Data m =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Shadowed m -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Shadowed m -> r
$cgmapQ :: forall m u.
Data m =>
(forall d. Data d => d -> u) -> Shadowed m -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> Shadowed m -> [u]
$cgmapQi :: forall m u.
Data m =>
Int -> (forall d. Data d => d -> u) -> Shadowed m -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Shadowed m -> u
$cgmapM :: forall m (m :: * -> *).
(Data m, Monad m) =>
(forall d. Data d => d -> m d) -> Shadowed m -> m (Shadowed m)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Shadowed m -> m (Shadowed m)
$cgmapMp :: forall m (m :: * -> *).
(Data m, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Shadowed m -> m (Shadowed m)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Shadowed m -> m (Shadowed m)
$cgmapMo :: forall m (m :: * -> *).
(Data m, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Shadowed m -> m (Shadowed m)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Shadowed m -> m (Shadowed m)
Data, Typeable)

-- | The constructor of a 'Shadowed' monoid, with the initial @prefix = null@
shadowed :: Monoid m => m -> Shadowed m
shadowed :: forall m. Monoid m => m -> Shadowed m
shadowed = m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
forall a. Monoid a => a
mempty

instance Eq m => Eq (Shadowed m) where
   Shadowed{content :: forall m. Shadowed m -> m
content = m
a} == :: Shadowed m -> Shadowed m -> Bool
== Shadowed{content :: forall m. Shadowed m -> m
content = m
b} = m
a m -> m -> Bool
forall a. Eq a => a -> a -> Bool
== m
b

instance Ord m => Ord (Shadowed m) where
   compare :: Shadowed m -> Shadowed m -> Ordering
compare Shadowed{content :: forall m. Shadowed m -> m
content= m
a} Shadowed{content :: forall m. Shadowed m -> m
content= m
b} = m -> m -> Ordering
forall a. Ord a => a -> a -> Ordering
compare m
a m
b

instance (MonoidNull m, Show m) => Show (Shadowed m) where
   showsPrec :: Int -> Shadowed m -> ShowS
showsPrec Int
prec (Shadowed m
p m
c) String
rest
      | m -> Bool
forall m. MonoidNull m => m -> Bool
null m
p = Int -> m -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
prec m
c String
rest
      | Bool
otherwise = String
"Shadowed{prefix=" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> m -> ShowS
forall a. Show a => a -> ShowS
shows m
p (String
", content=" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> m -> ShowS
forall a. Show a => a -> ShowS
shows m
c (String
"}" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
rest))

instance (MonoidNull m, StableFactorial m) => Semigroup (Shadowed m) where
   Shadowed m
p1 m
c1 <> :: Shadowed m -> Shadowed m -> Shadowed m
<> m2 :: Shadowed m
m2@Shadowed{content :: forall m. Shadowed m -> m
content = m
c2}
      | m -> Bool
forall m. MonoidNull m => m -> Bool
null m
c1 Bool -> Bool -> Bool
&& m -> Bool
forall m. MonoidNull m => m -> Bool
null m
p1 = Shadowed m
m2
      | Bool
otherwise = m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p1 (m
c1 m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
c2)
   {-# INLINE (<>) #-}

instance (MonoidNull m, StableFactorial m) => Monoid (Shadowed m) where
   mempty :: Shadowed m
mempty = m -> Shadowed m
forall m. Monoid m => m -> Shadowed m
shadowed m
forall a. Monoid a => a
mempty
   mappend :: Shadowed m -> Shadowed m -> Shadowed m
mappend = Shadowed m -> Shadowed m -> Shadowed m
forall a. Semigroup a => a -> a -> a
(<>)
   {-# INLINE mempty #-}
   {-# INLINE mappend #-}

instance (MonoidNull m, StableFactorial m) => MonoidNull (Shadowed m) where
   null :: Shadowed m -> Bool
null = m -> Bool
forall m. MonoidNull m => m -> Bool
null (m -> Bool) -> (Shadowed m -> m) -> Shadowed m -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Shadowed m -> m
forall m. Shadowed m -> m
content
   {-# INLINE null #-}

instance (PositiveMonoid m, StableFactorial m) => PositiveMonoid (Shadowed m)

instance (MonoidNull m, StableFactorial m, LeftReductive m) => LeftReductive (Shadowed m) where
   Shadowed m
t1 isPrefixOf :: Shadowed m -> Shadowed m -> Bool
`isPrefixOf` Shadowed m
t2 = Shadowed m -> m
forall m. Shadowed m -> m
content Shadowed m
t1 m -> m -> Bool
forall m. LeftReductive m => m -> m -> Bool
`isPrefixOf` Shadowed m -> m
forall m. Shadowed m -> m
content Shadowed m
t2
   stripPrefix :: Shadowed m -> Shadowed m -> Maybe (Shadowed m)
stripPrefix (Shadowed m
_ m
c1) (Shadowed m
p m
c2) = (m -> Shadowed m) -> Maybe m -> Maybe (Shadowed m)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed (m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
c1)) (m -> m -> Maybe m
forall m. LeftReductive m => m -> m -> Maybe m
stripPrefix m
c1 m
c2)
   {-# INLINE isPrefixOf #-}
   {-# INLINE stripPrefix #-}

instance (Eq m, StableFactorial m, FactorialMonoid m, LeftGCDMonoid m) => LeftGCDMonoid (Shadowed m) where
   stripCommonPrefix :: Shadowed m -> Shadowed m -> (Shadowed m, Shadowed m, Shadowed m)
stripCommonPrefix (Shadowed m
p1 m
c1) (Shadowed m
p2 m
c2) =
      (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
prefix' m
common, m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed (m
p1 m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
common) m
c1', m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed (m
p2 m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
common) m
c2')
      where (m
common, m
c1', m
c2') = m -> m -> (m, m, m)
forall m. LeftGCDMonoid m => m -> m -> (m, m, m)
stripCommonPrefix m
c1 m
c2
            prefix' :: m
prefix' = if m
p1 m -> m -> Bool
forall a. Eq a => a -> a -> Bool
== m
p2 then m
p1 m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
common else m
common
   {-# INLINE stripCommonPrefix #-}

instance (StableFactorial m, FactorialMonoid m, RightReductive m) => RightReductive (Shadowed m) where
   isSuffixOf :: Shadowed m -> Shadowed m -> Bool
isSuffixOf (Shadowed m
_ m
c1) (Shadowed m
_ m
c2) = m -> m -> Bool
forall m. RightReductive m => m -> m -> Bool
isSuffixOf m
c1 m
c2
   stripSuffix :: Shadowed m -> Shadowed m -> Maybe (Shadowed m)
stripSuffix (Shadowed m
_ m
c1) (Shadowed m
p m
c2) = (m -> Shadowed m) -> Maybe m -> Maybe (Shadowed m)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p) (m -> m -> Maybe m
forall m. RightReductive m => m -> m -> Maybe m
stripSuffix m
c1 m
c2)
   {-# INLINE isSuffixOf #-}
   {-# INLINE stripSuffix #-}

instance (StableFactorial m, FactorialMonoid m, RightGCDMonoid m) => RightGCDMonoid (Shadowed m) where
   commonSuffix :: Shadowed m -> Shadowed m -> Shadowed m
commonSuffix (Shadowed m
_ m
c1) (Shadowed m
_ m
c2) = m -> Shadowed m
forall m. Monoid m => m -> Shadowed m
shadowed m
suffix
      where suffix :: m
suffix = m -> m -> m
forall m. RightGCDMonoid m => m -> m -> m
commonSuffix m
c1 m
c2
   stripCommonSuffix :: Shadowed m -> Shadowed m -> (Shadowed m, Shadowed m, Shadowed m)
stripCommonSuffix (Shadowed m
p1 m
c1) (Shadowed m
p2 m
c2) =
      (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p1 m
c1', m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p2 m
c2',
       m -> Shadowed m
forall m. Monoid m => m -> Shadowed m
shadowed m
suffix)
      where (m
c1', m
c2', m
suffix) = m -> m -> (m, m, m)
forall m. RightGCDMonoid m => m -> m -> (m, m, m)
stripCommonSuffix m
c1 m
c2
   {-# INLINE commonSuffix #-}
   {-# INLINE stripCommonSuffix #-}

instance (FactorialMonoid m, StableFactorial m) => Factorial (Shadowed m) where
   factors :: Shadowed m -> [Shadowed m]
factors (Shadowed m
p m
c) = m -> Shadowed m
rewrap (m -> Shadowed m) -> [m] -> [Shadowed m]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [m] -> [m]
forall a. HasCallStack => [a] -> [a]
List.tail (m -> [m]
forall m. FactorialMonoid m => m -> [m]
inits m
c)
      where rewrap :: m -> Shadowed m
rewrap m
t
               | Just (m
p', m
prime) <- m -> Maybe (m, m)
forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimeSuffix m
t = m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed (m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
p') m
prime
               | Bool
otherwise = String -> Shadowed m
forall a. HasCallStack => String -> a
error String
"all (not . null) . tail . inits"
   primePrefix :: Shadowed m -> Shadowed m
primePrefix (Shadowed m
p m
c) = m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p (m -> m
forall m. Factorial m => m -> m
primePrefix m
c)
   foldl :: forall a. (a -> Shadowed m -> a) -> a -> Shadowed m -> a
foldl a -> Shadowed m -> a
f a
a0 (Shadowed m
p0 m
c0) = (a, m) -> a
forall a b. (a, b) -> a
fst ((a, m) -> a) -> (a, m) -> a
forall a b. (a -> b) -> a -> b
$ ((a, m) -> m -> (a, m)) -> (a, m) -> m -> (a, m)
forall m a. Factorial m => (a -> m -> a) -> a -> m -> a
forall a. (a -> m -> a) -> a -> m -> a
Factorial.foldl (a, m) -> m -> (a, m)
f' (a
a0, m
p0) m
c0
      where f' :: (a, m) -> m -> (a, m)
f' (a
a, m
p) m
c = (a -> Shadowed m -> a
f a
a (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
c), m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
c)
   foldl' :: forall a. (a -> Shadowed m -> a) -> a -> Shadowed m -> a
foldl' a -> Shadowed m -> a
f a
a0 (Shadowed m
p0 m
c0) = (a, m) -> a
forall a b. (a, b) -> a
fst ((a, m) -> a) -> (a, m) -> a
forall a b. (a -> b) -> a -> b
$ ((a, m) -> m -> (a, m)) -> (a, m) -> m -> (a, m)
forall m a. Factorial m => (a -> m -> a) -> a -> m -> a
forall a. (a -> m -> a) -> a -> m -> a
Factorial.foldl' (a, m) -> m -> (a, m)
f' (a
a0, m
p0) m
c0
      where f' :: (a, m) -> m -> (a, m)
f' (a
a, m
p) m
c = ((,) (a -> m -> (a, m)) -> a -> m -> (a, m)
forall a b. (a -> b) -> a -> b
$! a -> Shadowed m -> a
f a
a (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
c)) (m -> (a, m)) -> m -> (a, m)
forall a b. (a -> b) -> a -> b
$! m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
c
   foldr :: forall a. (Shadowed m -> a -> a) -> a -> Shadowed m -> a
foldr Shadowed m -> a -> a
f a
a0 (Shadowed m
p0 m
c0) = (m -> (m -> a) -> m -> a) -> (m -> a) -> m -> m -> a
forall m a. Factorial m => (m -> a -> a) -> a -> m -> a
forall a. (m -> a -> a) -> a -> m -> a
Factorial.foldr m -> (m -> a) -> m -> a
f' (a -> m -> a
forall a b. a -> b -> a
const a
a0) m
c0 m
p0
      where f' :: m -> (m -> a) -> m -> a
f' m
c m -> a
cont m
p = Shadowed m -> a -> a
f (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
c) (m -> a
cont (m -> a) -> m -> a
forall a b. (a -> b) -> a -> b
$! m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
c)
   foldMap :: forall n. Monoid n => (Shadowed m -> n) -> Shadowed m -> n
foldMap Shadowed m -> n
f (Shadowed m
p0 m
c) = Endo (m -> n) -> (m -> n) -> m -> n
forall a. Endo a -> a -> a
appEndo ((m -> Endo (m -> n)) -> m -> Endo (m -> n)
forall n. Monoid n => (m -> n) -> m -> n
forall m n. (Factorial m, Monoid n) => (m -> n) -> m -> n
Factorial.foldMap m -> Endo (m -> n)
f' m
c) (n -> m -> n
forall a b. a -> b -> a
const n
forall a. Monoid a => a
mempty) m
p0
      where -- f' :: m -> Endo (Int -> m)
            f' :: m -> Endo (m -> n)
f' m
prime = ((m -> n) -> m -> n) -> Endo (m -> n)
forall a. (a -> a) -> Endo a
Endo (\m -> n
cont m
p-> Shadowed m -> n
f (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
prime) n -> n -> n
forall a. Monoid a => a -> a -> a
`mappend` (m -> n
cont (m -> n) -> m -> n
forall a b. (a -> b) -> a -> b
$! m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
prime))
   length :: Shadowed m -> Int
length (Shadowed m
_ m
c) = m -> Int
forall m. Factorial m => m -> Int
length m
c
   reverse :: Shadowed m -> Shadowed m
reverse (Shadowed m
p m
c) = m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p (m -> m
forall m. Factorial m => m -> m
Factorial.reverse m
c)
   {-# INLINE primePrefix #-}
   {-# INLINE foldl #-}
   {-# INLINE foldl' #-}
   {-# INLINE foldr #-}
   {-# INLINE foldMap #-}

instance (StableFactorial m, FactorialMonoid m) => FactorialMonoid (Shadowed m) where
   splitPrimePrefix :: Shadowed m -> Maybe (Shadowed m, Shadowed m)
splitPrimePrefix (Shadowed m
p m
c) = ((m, m) -> (Shadowed m, Shadowed m))
-> Maybe (m, m) -> Maybe (Shadowed m, Shadowed m)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (m, m) -> (Shadowed m, Shadowed m)
rewrap (m -> Maybe (m, m)
forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimePrefix m
c)
      where rewrap :: (m, m) -> (Shadowed m, Shadowed m)
rewrap (m
cp, m
cs) = (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
cp, m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed (m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
cp) m
cs)
   splitPrimeSuffix :: Shadowed m -> Maybe (Shadowed m, Shadowed m)
splitPrimeSuffix (Shadowed m
p m
c) = ((m, m) -> (Shadowed m, Shadowed m))
-> Maybe (m, m) -> Maybe (Shadowed m, Shadowed m)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (m, m) -> (Shadowed m, Shadowed m)
rewrap (m -> Maybe (m, m)
forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimeSuffix m
c)
      where rewrap :: (m, m) -> (Shadowed m, Shadowed m)
rewrap (m
cp, m
cs) = (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
cp, m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed (m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
cp) m
cs)
   spanMaybe :: forall s.
s
-> (s -> Shadowed m -> Maybe s)
-> Shadowed m
-> (Shadowed m, Shadowed m, s)
spanMaybe s
s0 s -> Shadowed m -> Maybe s
f (Shadowed m
p0 m
c) = (m, m, (s, m)) -> (Shadowed m, Shadowed m, s)
forall {m} {c}. (m, m, (c, m)) -> (Shadowed m, Shadowed m, c)
rewrap ((m, m, (s, m)) -> (Shadowed m, Shadowed m, s))
-> (m, m, (s, m)) -> (Shadowed m, Shadowed m, s)
forall a b. (a -> b) -> a -> b
$ (s, m) -> ((s, m) -> m -> Maybe (s, m)) -> m -> (m, m, (s, m))
forall s. s -> (s -> m -> Maybe s) -> m -> (m, m, s)
forall m s.
FactorialMonoid m =>
s -> (s -> m -> Maybe s) -> m -> (m, m, s)
Factorial.spanMaybe (s
s0, m
p0) (s, m) -> m -> Maybe (s, m)
f' m
c
      where f' :: (s, m) -> m -> Maybe (s, m)
f' (s
s, m
p) m
prime = do s
s' <- s -> Shadowed m -> Maybe s
f s
s (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
prime)
                                 let p' :: m
p' = m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
prime
                                 (s, m) -> Maybe (s, m)
forall a. a -> Maybe a
Just ((s, m) -> Maybe (s, m)) -> (s, m) -> Maybe (s, m)
forall a b. (a -> b) -> a -> b
$! m -> (s, m) -> (s, m)
forall a b. a -> b -> b
seq m
p' (s
s', m
p')
            rewrap :: (m, m, (c, m)) -> (Shadowed m, Shadowed m, c)
rewrap (m
cp, m
cs, (c
s, m
p)) = (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p0 m
cp, m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
cs, c
s)
   spanMaybe' :: forall s.
s
-> (s -> Shadowed m -> Maybe s)
-> Shadowed m
-> (Shadowed m, Shadowed m, s)
spanMaybe' s
s0 s -> Shadowed m -> Maybe s
f (Shadowed m
p0 m
c) = (m, m, (s, m)) -> (Shadowed m, Shadowed m, s)
forall {m} {c}. (m, m, (c, m)) -> (Shadowed m, Shadowed m, c)
rewrap ((m, m, (s, m)) -> (Shadowed m, Shadowed m, s))
-> (m, m, (s, m)) -> (Shadowed m, Shadowed m, s)
forall a b. (a -> b) -> a -> b
$! (s, m) -> ((s, m) -> m -> Maybe (s, m)) -> m -> (m, m, (s, m))
forall s. s -> (s -> m -> Maybe s) -> m -> (m, m, s)
forall m s.
FactorialMonoid m =>
s -> (s -> m -> Maybe s) -> m -> (m, m, s)
Factorial.spanMaybe' (s
s0, m
p0) (s, m) -> m -> Maybe (s, m)
f' m
c
      where f' :: (s, m) -> m -> Maybe (s, m)
f' (s
s, m
p) m
prime = do s
s' <- s -> Shadowed m -> Maybe s
f s
s (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
prime)
                                 let p' :: m
p' = m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
prime
                                 (s, m) -> Maybe (s, m)
forall a. a -> Maybe a
Just ((s, m) -> Maybe (s, m)) -> (s, m) -> Maybe (s, m)
forall a b. (a -> b) -> a -> b
$! s
s' s -> (s, m) -> (s, m)
forall a b. a -> b -> b
`seq` m
p' m -> (s, m) -> (s, m)
forall a b. a -> b -> b
`seq` (s
s', m
p')
            rewrap :: (m, m, (c, m)) -> (Shadowed m, Shadowed m, c)
rewrap (m
cp, m
cs, (c
s, m
p)) = (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p0 m
cp, m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
cs, c
s)
   span :: (Shadowed m -> Bool) -> Shadowed m -> (Shadowed m, Shadowed m)
span Shadowed m -> Bool
f (Shadowed m
p0 m
c) = (m, m, m) -> (Shadowed m, Shadowed m)
forall {m}. (m, m, m) -> (Shadowed m, Shadowed m)
rewrap ((m, m, m) -> (Shadowed m, Shadowed m))
-> (m, m, m) -> (Shadowed m, Shadowed m)
forall a b. (a -> b) -> a -> b
$ m -> (m -> m -> Maybe m) -> m -> (m, m, m)
forall s. s -> (s -> m -> Maybe s) -> m -> (m, m, s)
forall m s.
FactorialMonoid m =>
s -> (s -> m -> Maybe s) -> m -> (m, m, s)
Factorial.spanMaybe' m
p0 m -> m -> Maybe m
f' m
c
      where f' :: m -> m -> Maybe m
f' m
p m
prime = if Shadowed m -> Bool
f (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
prime)
                         then m -> Maybe m
forall a. a -> Maybe a
Just (m -> Maybe m) -> m -> Maybe m
forall a b. (a -> b) -> a -> b
$! m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
prime
                         else Maybe m
forall a. Maybe a
Nothing
            rewrap :: (m, m, m) -> (Shadowed m, Shadowed m)
rewrap (m
cp, m
cs, m
p) = (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p0 m
cp, m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
cs)
   splitAt :: Int -> Shadowed m -> (Shadowed m, Shadowed m)
splitAt Int
n (Shadowed m
p m
c) = (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
cp, m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed (m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
cp) m
cs)
      where (m
cp, m
cs) = Int -> m -> (m, m)
forall m. FactorialMonoid m => Int -> m -> (m, m)
splitAt Int
n m
c
   take :: Int -> Shadowed m -> Shadowed m
take Int
n (Shadowed m
p m
c) = m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p (Int -> m -> m
forall m. FactorialMonoid m => Int -> m -> m
Factorial.take Int
n m
c)
   {-# INLINE splitPrimePrefix #-}
   {-# INLINE splitPrimeSuffix #-}
   {-# INLINE span #-}
   {-# INLINE splitAt #-}
   {-# INLINE take #-}

instance (StableFactorial m, FactorialMonoid m) => StableFactorial (Shadowed m)

instance (Monoid m, IsString m) => IsString (Shadowed m) where
   fromString :: String -> Shadowed m
fromString = m -> Shadowed m
forall m. Monoid m => m -> Shadowed m
shadowed (m -> Shadowed m) -> (String -> m) -> String -> Shadowed m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> m
forall a. IsString a => String -> a
fromString

instance (Eq m, StableFactorial m, TextualMonoid m) => TextualMonoid (Shadowed m) where
   splitCharacterPrefix :: Shadowed m -> Maybe (Char, Shadowed m)
splitCharacterPrefix (Shadowed m
p m
t) = (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p (m -> Shadowed m) -> (Char, m) -> (Char, Shadowed m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) ((Char, m) -> (Char, Shadowed m))
-> Maybe (Char, m) -> Maybe (Char, Shadowed m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m -> Maybe (Char, m)
forall t. TextualMonoid t => t -> Maybe (Char, t)
Textual.splitCharacterPrefix m
t

   fromText :: Text -> Shadowed m
fromText = m -> Shadowed m
forall m. Monoid m => m -> Shadowed m
shadowed (m -> Shadowed m) -> (Text -> m) -> Text -> Shadowed m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> m
forall t. TextualMonoid t => Text -> t
fromText
   singleton :: Char -> Shadowed m
singleton = m -> Shadowed m
forall m. Monoid m => m -> Shadowed m
shadowed (m -> Shadowed m) -> (Char -> m) -> Char -> Shadowed m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> m
forall t. TextualMonoid t => Char -> t
singleton

   characterPrefix :: Shadowed m -> Maybe Char
characterPrefix = m -> Maybe Char
forall t. TextualMonoid t => t -> Maybe Char
characterPrefix (m -> Maybe Char) -> (Shadowed m -> m) -> Shadowed m -> Maybe Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Shadowed m -> m
forall m. Shadowed m -> m
content

   map :: (Char -> Char) -> Shadowed m -> Shadowed m
map Char -> Char
f (Shadowed m
p m
c) = m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p ((Char -> Char) -> m -> m
forall t. TextualMonoid t => (Char -> Char) -> t -> t
map Char -> Char
f m
c)
   concatMap :: (Char -> Shadowed m) -> Shadowed m -> Shadowed m
concatMap Char -> Shadowed m
f (Shadowed m
p m
c) = m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p ((Char -> m) -> m -> m
forall t. TextualMonoid t => (Char -> t) -> t -> t
concatMap (Shadowed m -> m
forall m. Shadowed m -> m
content (Shadowed m -> m) -> (Char -> Shadowed m) -> Char -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Shadowed m
f) m
c)
   all :: (Char -> Bool) -> Shadowed m -> Bool
all Char -> Bool
p = (Char -> Bool) -> m -> Bool
forall t. TextualMonoid t => (Char -> Bool) -> t -> Bool
all Char -> Bool
p (m -> Bool) -> (Shadowed m -> m) -> Shadowed m -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Shadowed m -> m
forall m. Shadowed m -> m
content
   any :: (Char -> Bool) -> Shadowed m -> Bool
any Char -> Bool
p = (Char -> Bool) -> m -> Bool
forall t. TextualMonoid t => (Char -> Bool) -> t -> Bool
any Char -> Bool
p (m -> Bool) -> (Shadowed m -> m) -> Shadowed m -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Shadowed m -> m
forall m. Shadowed m -> m
content

   foldl :: forall a.
(a -> Shadowed m -> a) -> (a -> Char -> a) -> a -> Shadowed m -> a
foldl a -> Shadowed m -> a
ft a -> Char -> a
fc a
a0 (Shadowed m
p0 m
c0) = (a, m) -> a
forall a b. (a, b) -> a
fst ((a, m) -> a) -> (a, m) -> a
forall a b. (a -> b) -> a -> b
$ ((a, m) -> m -> (a, m))
-> ((a, m) -> Char -> (a, m)) -> (a, m) -> m -> (a, m)
forall t a.
TextualMonoid t =>
(a -> t -> a) -> (a -> Char -> a) -> a -> t -> a
forall a. (a -> m -> a) -> (a -> Char -> a) -> a -> m -> a
Textual.foldl (a, m) -> m -> (a, m)
ft' (a, m) -> Char -> (a, m)
forall {b}. TextualMonoid b => (a, b) -> Char -> (a, b)
fc' (a
a0, m
p0) m
c0
      where ft' :: (a, m) -> m -> (a, m)
ft' (a
a, m
p) m
c = (a -> Shadowed m -> a
ft a
a (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
c), m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
c)
            fc' :: (a, b) -> Char -> (a, b)
fc' (a
a, b
p) Char
c = (a -> Char -> a
fc a
a Char
c, b
p b -> b -> b
forall a. Semigroup a => a -> a -> a
<> Char -> b
forall t. TextualMonoid t => Char -> t
Textual.singleton Char
c)
   foldl' :: forall a.
(a -> Shadowed m -> a) -> (a -> Char -> a) -> a -> Shadowed m -> a
foldl' a -> Shadowed m -> a
ft a -> Char -> a
fc a
a0 (Shadowed m
p0 m
c0) = (a, m) -> a
forall a b. (a, b) -> a
fst ((a, m) -> a) -> (a, m) -> a
forall a b. (a -> b) -> a -> b
$ ((a, m) -> m -> (a, m))
-> ((a, m) -> Char -> (a, m)) -> (a, m) -> m -> (a, m)
forall t a.
TextualMonoid t =>
(a -> t -> a) -> (a -> Char -> a) -> a -> t -> a
forall a. (a -> m -> a) -> (a -> Char -> a) -> a -> m -> a
Textual.foldl' (a, m) -> m -> (a, m)
ft' (a, m) -> Char -> (a, m)
forall {a}. TextualMonoid a => (a, a) -> Char -> (a, a)
fc' (a
a0, m
p0) m
c0
      where ft' :: (a, m) -> m -> (a, m)
ft' (a
a, m
p) m
c = ((,) (a -> m -> (a, m)) -> a -> m -> (a, m)
forall a b. (a -> b) -> a -> b
$! a -> Shadowed m -> a
ft a
a (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
c)) (m -> (a, m)) -> m -> (a, m)
forall a b. (a -> b) -> a -> b
$! m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
c
            fc' :: (a, a) -> Char -> (a, a)
fc' (a
a, a
p) Char
c = ((,) (a -> a -> (a, a)) -> a -> a -> (a, a)
forall a b. (a -> b) -> a -> b
$! a -> Char -> a
fc a
a Char
c) (a -> (a, a)) -> a -> (a, a)
forall a b. (a -> b) -> a -> b
$! a
p a -> a -> a
forall a. Semigroup a => a -> a -> a
<> Char -> a
forall t. TextualMonoid t => Char -> t
Textual.singleton Char
c
   foldr :: forall a.
(Shadowed m -> a -> a) -> (Char -> a -> a) -> a -> Shadowed m -> a
foldr Shadowed m -> a -> a
ft Char -> a -> a
fc a
a0 (Shadowed m
p0 m
c0) = (m, a) -> a
forall a b. (a, b) -> b
snd ((m, a) -> a) -> (m, a) -> a
forall a b. (a -> b) -> a -> b
$ (m -> (m, a) -> (m, a))
-> (Char -> (m, a) -> (m, a)) -> (m, a) -> m -> (m, a)
forall t a.
TextualMonoid t =>
(t -> a -> a) -> (Char -> a -> a) -> a -> t -> a
forall a. (m -> a -> a) -> (Char -> a -> a) -> a -> m -> a
Textual.foldr m -> (m, a) -> (m, a)
ft' Char -> (m, a) -> (m, a)
forall {a}. TextualMonoid a => Char -> (a, a) -> (a, a)
fc' (m
p0, a
a0) m
c0
      where ft' :: m -> (m, a) -> (m, a)
ft' m
c (m
p, a
a) = ((,) (m -> a -> (m, a)) -> m -> a -> (m, a)
forall a b. (a -> b) -> a -> b
$! m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
c) (a -> (m, a)) -> a -> (m, a)
forall a b. (a -> b) -> a -> b
$! Shadowed m -> a -> a
ft (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
c) a
a
            fc' :: Char -> (a, a) -> (a, a)
fc' Char
c (a
p, a
a) = ((,) (a -> a -> (a, a)) -> a -> a -> (a, a)
forall a b. (a -> b) -> a -> b
$! a
p a -> a -> a
forall a. Semigroup a => a -> a -> a
<> Char -> a
forall t. TextualMonoid t => Char -> t
Textual.singleton Char
c) (a -> (a, a)) -> a -> (a, a)
forall a b. (a -> b) -> a -> b
$! Char -> a -> a
fc Char
c a
a

   scanl :: (Char -> Char -> Char) -> Char -> Shadowed m -> Shadowed m
scanl Char -> Char -> Char
f Char
ch (Shadowed m
p m
c) = m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p ((Char -> Char -> Char) -> Char -> m -> m
forall t.
TextualMonoid t =>
(Char -> Char -> Char) -> Char -> t -> t
Textual.scanl Char -> Char -> Char
f Char
ch m
c)
   scanl1 :: (Char -> Char -> Char) -> Shadowed m -> Shadowed m
scanl1 Char -> Char -> Char
f (Shadowed m
p m
c) = m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p ((Char -> Char -> Char) -> m -> m
forall t. TextualMonoid t => (Char -> Char -> Char) -> t -> t
Textual.scanl1 Char -> Char -> Char
f m
c)
   scanr :: (Char -> Char -> Char) -> Char -> Shadowed m -> Shadowed m
scanr Char -> Char -> Char
f Char
ch (Shadowed m
p m
c) = m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p ((Char -> Char -> Char) -> Char -> m -> m
forall t.
TextualMonoid t =>
(Char -> Char -> Char) -> Char -> t -> t
Textual.scanr Char -> Char -> Char
f Char
ch m
c)
   scanr1 :: (Char -> Char -> Char) -> Shadowed m -> Shadowed m
scanr1 Char -> Char -> Char
f (Shadowed m
p m
c) = m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p ((Char -> Char -> Char) -> m -> m
forall t. TextualMonoid t => (Char -> Char -> Char) -> t -> t
Textual.scanr1 Char -> Char -> Char
f m
c)
   mapAccumL :: forall a.
(a -> Char -> (a, Char)) -> a -> Shadowed m -> (a, Shadowed m)
mapAccumL a -> Char -> (a, Char)
f a
a0 (Shadowed m
p m
c) = (m -> Shadowed m) -> (a, m) -> (a, Shadowed m)
forall a b. (a -> b) -> (a, a) -> (a, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p) ((a -> Char -> (a, Char)) -> a -> m -> (a, m)
forall t a.
TextualMonoid t =>
(a -> Char -> (a, Char)) -> a -> t -> (a, t)
forall a. (a -> Char -> (a, Char)) -> a -> m -> (a, m)
Textual.mapAccumL a -> Char -> (a, Char)
f a
a0 m
c)
   mapAccumR :: forall a.
(a -> Char -> (a, Char)) -> a -> Shadowed m -> (a, Shadowed m)
mapAccumR a -> Char -> (a, Char)
f a
a0 (Shadowed m
p m
c) = (m -> Shadowed m) -> (a, m) -> (a, Shadowed m)
forall a b. (a -> b) -> (a, a) -> (a, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p) ((a -> Char -> (a, Char)) -> a -> m -> (a, m)
forall t a.
TextualMonoid t =>
(a -> Char -> (a, Char)) -> a -> t -> (a, t)
forall a. (a -> Char -> (a, Char)) -> a -> m -> (a, m)
Textual.mapAccumR a -> Char -> (a, Char)
f a
a0 m
c)

   spanMaybe :: forall s.
s
-> (s -> Shadowed m -> Maybe s)
-> (s -> Char -> Maybe s)
-> Shadowed m
-> (Shadowed m, Shadowed m, s)
spanMaybe s
s0 s -> Shadowed m -> Maybe s
ft s -> Char -> Maybe s
fc (Shadowed m
p0 m
t) = (m, m, (s, m)) -> (Shadowed m, Shadowed m, s)
forall {m} {c}. (m, m, (c, m)) -> (Shadowed m, Shadowed m, c)
rewrap ((m, m, (s, m)) -> (Shadowed m, Shadowed m, s))
-> (m, m, (s, m)) -> (Shadowed m, Shadowed m, s)
forall a b. (a -> b) -> a -> b
$ (s, m)
-> ((s, m) -> m -> Maybe (s, m))
-> ((s, m) -> Char -> Maybe (s, m))
-> m
-> (m, m, (s, m))
forall s.
s
-> (s -> m -> Maybe s) -> (s -> Char -> Maybe s) -> m -> (m, m, s)
forall t s.
TextualMonoid t =>
s
-> (s -> t -> Maybe s) -> (s -> Char -> Maybe s) -> t -> (t, t, s)
Textual.spanMaybe (s
s0, m
p0) (s, m) -> m -> Maybe (s, m)
ft' (s, m) -> Char -> Maybe (s, m)
forall {b}. TextualMonoid b => (s, b) -> Char -> Maybe (s, b)
fc' m
t
      where ft' :: (s, m) -> m -> Maybe (s, m)
ft' (s
s, m
p) m
prime = do s
s' <- s -> Shadowed m -> Maybe s
ft s
s (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
prime)
                                  let p' :: m
p' = m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
prime
                                  (s, m) -> Maybe (s, m)
forall a. a -> Maybe a
Just ((s, m) -> Maybe (s, m)) -> (s, m) -> Maybe (s, m)
forall a b. (a -> b) -> a -> b
$! m -> (s, m) -> (s, m)
forall a b. a -> b -> b
seq m
p' (s
s', m
p')
            fc' :: (s, b) -> Char -> Maybe (s, b)
fc' (s
s, b
p) Char
c = do s
s' <- s -> Char -> Maybe s
fc s
s Char
c
                              let p' :: b
p' = b
p b -> b -> b
forall a. Semigroup a => a -> a -> a
<> Char -> b
forall t. TextualMonoid t => Char -> t
Textual.singleton Char
c
                              (s, b) -> Maybe (s, b)
forall a. a -> Maybe a
Just ((s, b) -> Maybe (s, b)) -> (s, b) -> Maybe (s, b)
forall a b. (a -> b) -> a -> b
$! b -> (s, b) -> (s, b)
forall a b. a -> b -> b
seq b
p' (s
s', b
p')
            rewrap :: (m, m, (c, m)) -> (Shadowed m, Shadowed m, c)
rewrap (m
tp, m
ts, (c
s, m
p)) = (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p0 m
tp, m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
ts, c
s)
   spanMaybe' :: forall s.
s
-> (s -> Shadowed m -> Maybe s)
-> (s -> Char -> Maybe s)
-> Shadowed m
-> (Shadowed m, Shadowed m, s)
spanMaybe' s
s0 s -> Shadowed m -> Maybe s
ft s -> Char -> Maybe s
fc (Shadowed m
p0 m
t) = (m, m, (s, m)) -> (Shadowed m, Shadowed m, s)
forall {m} {c}. (m, m, (c, m)) -> (Shadowed m, Shadowed m, c)
rewrap ((m, m, (s, m)) -> (Shadowed m, Shadowed m, s))
-> (m, m, (s, m)) -> (Shadowed m, Shadowed m, s)
forall a b. (a -> b) -> a -> b
$! (s, m)
-> ((s, m) -> m -> Maybe (s, m))
-> ((s, m) -> Char -> Maybe (s, m))
-> m
-> (m, m, (s, m))
forall s.
s
-> (s -> m -> Maybe s) -> (s -> Char -> Maybe s) -> m -> (m, m, s)
forall t s.
TextualMonoid t =>
s
-> (s -> t -> Maybe s) -> (s -> Char -> Maybe s) -> t -> (t, t, s)
Textual.spanMaybe' (s
s0, m
p0) (s, m) -> m -> Maybe (s, m)
ft' (s, m) -> Char -> Maybe (s, m)
forall {b}. TextualMonoid b => (s, b) -> Char -> Maybe (s, b)
fc' m
t
      where ft' :: (s, m) -> m -> Maybe (s, m)
ft' (s
s, m
p) m
prime = do s
s' <- s -> Shadowed m -> Maybe s
ft s
s (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
prime)
                                  let p' :: m
p' = m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
prime
                                  (s, m) -> Maybe (s, m)
forall a. a -> Maybe a
Just ((s, m) -> Maybe (s, m)) -> (s, m) -> Maybe (s, m)
forall a b. (a -> b) -> a -> b
$! s
s' s -> (s, m) -> (s, m)
forall a b. a -> b -> b
`seq` m
p' m -> (s, m) -> (s, m)
forall a b. a -> b -> b
`seq` (s
s', m
p')
            fc' :: (s, b) -> Char -> Maybe (s, b)
fc' (s
s, b
p) Char
c = do s
s' <- s -> Char -> Maybe s
fc s
s Char
c
                              let p' :: b
p' = b
p b -> b -> b
forall a. Semigroup a => a -> a -> a
<> Char -> b
forall t. TextualMonoid t => Char -> t
Textual.singleton Char
c
                              (s, b) -> Maybe (s, b)
forall a. a -> Maybe a
Just ((s, b) -> Maybe (s, b)) -> (s, b) -> Maybe (s, b)
forall a b. (a -> b) -> a -> b
$! s
s' s -> (s, b) -> (s, b)
forall a b. a -> b -> b
`seq` b
p' b -> (s, b) -> (s, b)
forall a b. a -> b -> b
`seq` (s
s', b
p')
            rewrap :: (m, m, (c, m)) -> (Shadowed m, Shadowed m, c)
rewrap (m
tp, m
ts, (c
s, m
p)) = (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p0 m
tp, m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
ts, c
s)
   span :: (Shadowed m -> Bool)
-> (Char -> Bool) -> Shadowed m -> (Shadowed m, Shadowed m)
span Shadowed m -> Bool
ft Char -> Bool
fc (Shadowed m
p0 m
t) = (m, m, m) -> (Shadowed m, Shadowed m)
forall {m}. (m, m, m) -> (Shadowed m, Shadowed m)
rewrap ((m, m, m) -> (Shadowed m, Shadowed m))
-> (m, m, m) -> (Shadowed m, Shadowed m)
forall a b. (a -> b) -> a -> b
$ m
-> (m -> m -> Maybe m) -> (m -> Char -> Maybe m) -> m -> (m, m, m)
forall s.
s
-> (s -> m -> Maybe s) -> (s -> Char -> Maybe s) -> m -> (m, m, s)
forall t s.
TextualMonoid t =>
s
-> (s -> t -> Maybe s) -> (s -> Char -> Maybe s) -> t -> (t, t, s)
Textual.spanMaybe' m
p0 m -> m -> Maybe m
ft' m -> Char -> Maybe m
forall {a}. TextualMonoid a => a -> Char -> Maybe a
fc' m
t
      where ft' :: m -> m -> Maybe m
ft' m
p m
prime = if Shadowed m -> Bool
ft (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
prime)
                          then m -> Maybe m
forall a. a -> Maybe a
Just (m -> Maybe m) -> m -> Maybe m
forall a b. (a -> b) -> a -> b
$! m
p m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
prime
                          else Maybe m
forall a. Maybe a
Nothing
            fc' :: a -> Char -> Maybe a
fc' a
p Char
c = if Char -> Bool
fc Char
c
                      then a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$! a
p a -> a -> a
forall a. Semigroup a => a -> a -> a
<> Char -> a
forall t. TextualMonoid t => Char -> t
Textual.singleton Char
c
                      else Maybe a
forall a. Maybe a
Nothing
            rewrap :: (m, m, m) -> (Shadowed m, Shadowed m)
rewrap (m
tp, m
ts, m
p) = (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p0 m
tp, m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
ts)

   split :: (Char -> Bool) -> Shadowed m -> [Shadowed m]
split Char -> Bool
f (Shadowed m
p0 m
c0) = m -> [m] -> [Shadowed m]
forall {t}. Semigroup t => t -> [t] -> [Shadowed t]
rewrap m
p0 ((Char -> Bool) -> m -> [m]
forall t. TextualMonoid t => (Char -> Bool) -> t -> [t]
Textual.split Char -> Bool
f m
c0)
      where rewrap :: t -> [t] -> [Shadowed t]
rewrap t
_ [] = []
            rewrap t
p (t
c:[t]
rest) = t -> t -> Shadowed t
forall m. m -> m -> Shadowed m
Shadowed t
p t
c Shadowed t -> [Shadowed t] -> [Shadowed t]
forall a. a -> [a] -> [a]
: t -> [t] -> [Shadowed t]
rewrap (t
p t -> t -> t
forall a. Semigroup a => a -> a -> a
<> t
c) [t]
rest
   find :: (Char -> Bool) -> Shadowed m -> Maybe Char
find Char -> Bool
p = (Char -> Bool) -> m -> Maybe Char
forall t. TextualMonoid t => (Char -> Bool) -> t -> Maybe Char
find Char -> Bool
p (m -> Maybe Char) -> (Shadowed m -> m) -> Shadowed m -> Maybe Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Shadowed m -> m
forall m. Shadowed m -> m
content

   foldl_ :: forall a. (a -> Char -> a) -> a -> Shadowed m -> a
foldl_ a -> Char -> a
fc a
a0 (Shadowed m
_ m
c) = (a -> Char -> a) -> a -> m -> a
forall t a. TextualMonoid t => (a -> Char -> a) -> a -> t -> a
forall a. (a -> Char -> a) -> a -> m -> a
Textual.foldl_ a -> Char -> a
fc a
a0 m
c
   foldl_' :: forall a. (a -> Char -> a) -> a -> Shadowed m -> a
foldl_' a -> Char -> a
fc a
a0 (Shadowed m
_ m
c) = (a -> Char -> a) -> a -> m -> a
forall t a. TextualMonoid t => (a -> Char -> a) -> a -> t -> a
forall a. (a -> Char -> a) -> a -> m -> a
Textual.foldl_' a -> Char -> a
fc a
a0 m
c
   foldr_ :: forall a. (Char -> a -> a) -> a -> Shadowed m -> a
foldr_ Char -> a -> a
fc a
a0 (Shadowed m
_ m
c) = (Char -> a -> a) -> a -> m -> a
forall t a. TextualMonoid t => (Char -> a -> a) -> a -> t -> a
forall a. (Char -> a -> a) -> a -> m -> a
Textual.foldr_ Char -> a -> a
fc a
a0 m
c

   spanMaybe_ :: forall s.
s
-> (s -> Char -> Maybe s)
-> Shadowed m
-> (Shadowed m, Shadowed m, s)
spanMaybe_ s
s0 s -> Char -> Maybe s
fc (Shadowed m
p0 m
t) = (m, m, (s, m)) -> (Shadowed m, Shadowed m, s)
forall {m} {c}. (m, m, (c, m)) -> (Shadowed m, Shadowed m, c)
rewrap ((m, m, (s, m)) -> (Shadowed m, Shadowed m, s))
-> (m, m, (s, m)) -> (Shadowed m, Shadowed m, s)
forall a b. (a -> b) -> a -> b
$ (s, m) -> ((s, m) -> Char -> Maybe (s, m)) -> m -> (m, m, (s, m))
forall s. s -> (s -> Char -> Maybe s) -> m -> (m, m, s)
forall t s.
TextualMonoid t =>
s -> (s -> Char -> Maybe s) -> t -> (t, t, s)
Textual.spanMaybe_' (s
s0, m
p0) (s, m) -> Char -> Maybe (s, m)
forall {b}. TextualMonoid b => (s, b) -> Char -> Maybe (s, b)
fc' m
t
      where fc' :: (s, b) -> Char -> Maybe (s, b)
fc' (s
s, b
p) Char
c = do s
s' <- s -> Char -> Maybe s
fc s
s Char
c
                              let p' :: b
p' = b
p b -> b -> b
forall a. Semigroup a => a -> a -> a
<> Char -> b
forall t. TextualMonoid t => Char -> t
Textual.singleton Char
c
                              (s, b) -> Maybe (s, b)
forall a. a -> Maybe a
Just ((s, b) -> Maybe (s, b)) -> (s, b) -> Maybe (s, b)
forall a b. (a -> b) -> a -> b
$! b -> (s, b) -> (s, b)
forall a b. a -> b -> b
seq b
p' (s
s', b
p')
            rewrap :: (m, m, (c, m)) -> (Shadowed m, Shadowed m, c)
rewrap (m
tp, m
ts, (c
s, m
p)) = (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p0 m
tp, m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
ts, c
s)
   spanMaybe_' :: forall s.
s
-> (s -> Char -> Maybe s)
-> Shadowed m
-> (Shadowed m, Shadowed m, s)
spanMaybe_' s
s0 s -> Char -> Maybe s
fc (Shadowed m
p0 m
t) = (m, m, (s, m)) -> (Shadowed m, Shadowed m, s)
forall {m} {c}. (m, m, (c, m)) -> (Shadowed m, Shadowed m, c)
rewrap ((m, m, (s, m)) -> (Shadowed m, Shadowed m, s))
-> (m, m, (s, m)) -> (Shadowed m, Shadowed m, s)
forall a b. (a -> b) -> a -> b
$! (s, m) -> ((s, m) -> Char -> Maybe (s, m)) -> m -> (m, m, (s, m))
forall s. s -> (s -> Char -> Maybe s) -> m -> (m, m, s)
forall t s.
TextualMonoid t =>
s -> (s -> Char -> Maybe s) -> t -> (t, t, s)
Textual.spanMaybe_' (s
s0, m
p0) (s, m) -> Char -> Maybe (s, m)
forall {b}. TextualMonoid b => (s, b) -> Char -> Maybe (s, b)
fc' m
t
      where fc' :: (s, b) -> Char -> Maybe (s, b)
fc' (s
s, b
p) Char
c = do s
s' <- s -> Char -> Maybe s
fc s
s Char
c
                              let p' :: b
p' = b
p b -> b -> b
forall a. Semigroup a => a -> a -> a
<> Char -> b
forall t. TextualMonoid t => Char -> t
Textual.singleton Char
c
                              (s, b) -> Maybe (s, b)
forall a. a -> Maybe a
Just ((s, b) -> Maybe (s, b)) -> (s, b) -> Maybe (s, b)
forall a b. (a -> b) -> a -> b
$! s
s' s -> (s, b) -> (s, b)
forall a b. a -> b -> b
`seq` b
p' b -> (s, b) -> (s, b)
forall a b. a -> b -> b
`seq` (s
s', b
p')
            rewrap :: (m, m, (c, m)) -> (Shadowed m, Shadowed m, c)
rewrap (m
tp, m
ts, (c
s, m
p)) = (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p0 m
tp, m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p m
ts, c
s)
   span_ :: Bool -> (Char -> Bool) -> Shadowed m -> (Shadowed m, Shadowed m)
span_ Bool
bt Char -> Bool
fc (Shadowed m
p0 m
t) = (m, m) -> (Shadowed m, Shadowed m)
rewrap ((m, m) -> (Shadowed m, Shadowed m))
-> (m, m) -> (Shadowed m, Shadowed m)
forall a b. (a -> b) -> a -> b
$ Bool -> (Char -> Bool) -> m -> (m, m)
forall t. TextualMonoid t => Bool -> (Char -> Bool) -> t -> (t, t)
Textual.span_ Bool
bt Char -> Bool
fc m
t
      where rewrap :: (m, m) -> (Shadowed m, Shadowed m)
rewrap (m
tp, m
ts) = (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p0 m
tp, m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed (m
p0 m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
tp) m
ts)
   break_ :: Bool -> (Char -> Bool) -> Shadowed m -> (Shadowed m, Shadowed m)
break_ Bool
bt Char -> Bool
fc (Shadowed m
p0 m
t) = (m, m) -> (Shadowed m, Shadowed m)
rewrap ((m, m) -> (Shadowed m, Shadowed m))
-> (m, m) -> (Shadowed m, Shadowed m)
forall a b. (a -> b) -> a -> b
$ Bool -> (Char -> Bool) -> m -> (m, m)
forall t. TextualMonoid t => Bool -> (Char -> Bool) -> t -> (t, t)
Textual.break_ Bool
bt Char -> Bool
fc m
t
      where rewrap :: (m, m) -> (Shadowed m, Shadowed m)
rewrap (m
tp, m
ts) = (m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p0 m
tp, m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed (m
p0 m -> m -> m
forall a. Semigroup a => a -> a -> a
<> m
tp) m
ts)
   dropWhile_ :: Bool -> (Char -> Bool) -> Shadowed m -> Shadowed m
dropWhile_ Bool
bt Char -> Bool
fc Shadowed m
t = (Shadowed m, Shadowed m) -> Shadowed m
forall a b. (a, b) -> b
snd (Bool -> (Char -> Bool) -> Shadowed m -> (Shadowed m, Shadowed m)
forall t. TextualMonoid t => Bool -> (Char -> Bool) -> t -> (t, t)
span_ Bool
bt Char -> Bool
fc Shadowed m
t)
   takeWhile_ :: Bool -> (Char -> Bool) -> Shadowed m -> Shadowed m
takeWhile_ Bool
bt Char -> Bool
fc (Shadowed m
p m
t) = m -> m -> Shadowed m
forall m. m -> m -> Shadowed m
Shadowed m
p (Bool -> (Char -> Bool) -> m -> m
forall t. TextualMonoid t => Bool -> (Char -> Bool) -> t -> t
takeWhile_ Bool
bt Char -> Bool
fc m
t)
   toString :: (Shadowed m -> String) -> Shadowed m -> String
toString Shadowed m -> String
ft (Shadowed m
_ m
t) = (m -> String) -> m -> String
forall t. TextualMonoid t => (t -> String) -> t -> String
toString (Shadowed m -> String
ft (Shadowed m -> String) -> (m -> Shadowed m) -> m -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m -> Shadowed m
forall m. Monoid m => m -> Shadowed m
shadowed) m
t
   toText :: (Shadowed m -> Text) -> Shadowed m -> Text
toText Shadowed m -> Text
ft (Shadowed m
_ m
t) = (m -> Text) -> m -> Text
forall t. TextualMonoid t => (t -> Text) -> t -> Text
toText (Shadowed m -> Text
ft (Shadowed m -> Text) -> (m -> Shadowed m) -> m -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m -> Shadowed m
forall m. Monoid m => m -> Shadowed m
shadowed) m
t

   {-# INLINE characterPrefix #-}
   {-# INLINE splitCharacterPrefix #-}
   {-# INLINE map #-}
   {-# INLINE concatMap #-}
   {-# INLINE foldl' #-}
   {-# INLINE foldr #-}
   {-# INLINABLE spanMaybe #-}
   {-# INLINABLE spanMaybe' #-}
   {-# INLINABLE span #-}
   {-# INLINE foldl_' #-}
   {-# INLINE foldr_ #-}
   {-# INLINE any #-}
   {-# INLINE all #-}
   {-# INLINABLE spanMaybe_ #-}
   {-# INLINABLE spanMaybe_' #-}
   {-# INLINE span_ #-}
   {-# INLINE break_ #-}
   {-# INLINE dropWhile_ #-}
   {-# INLINE takeWhile_ #-}
   {-# INLINE split #-}
   {-# INLINE find #-}