module Text.Parser.Internal where

import Control.Applicative (liftA2)
import qualified Control.Monad.Trans.Writer.Lazy as Lazy (WriterT(WriterT))
import qualified Control.Monad.Trans.Writer.Strict as Strict (WriterT(WriterT))
import qualified Control.Monad.Trans.State.Lazy as Lazy (StateT(StateT))
import qualified Control.Monad.Trans.State.Strict as Strict (StateT(StateT))
import qualified Control.Monad.Trans.RWS.Lazy as Lazy (RWST(RWST))
import qualified Control.Monad.Trans.RWS.Strict as Strict (RWST(RWST))

mapLazyWriterT :: Applicative m => (m a -> m b) -> Lazy.WriterT w m a -> Lazy.WriterT w m b
mapLazyWriterT :: forall (m :: * -> *) a b w.
Applicative m =>
(m a -> m b) -> WriterT w m a -> WriterT w m b
mapLazyWriterT m a -> m b
f (Lazy.WriterT m (a, w)
p) = forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
Lazy.WriterT (forall {b}. m (a, b) -> m (b, b)
apply m (a, w)
p)
   where apply :: m (a, b) -> m (b, b)
apply m (a, b)
m = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (,) (m a -> m b
f forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (a, b)
m) (forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (a, b)
m)

mapStrictWriterT :: Applicative m => (m a -> m b) -> Strict.WriterT w m a -> Strict.WriterT w m b
mapStrictWriterT :: forall (m :: * -> *) a b w.
Applicative m =>
(m a -> m b) -> WriterT w m a -> WriterT w m b
mapStrictWriterT m a -> m b
f (Strict.WriterT m (a, w)
p) = forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
Strict.WriterT (forall {b}. m (a, b) -> m (b, b)
apply m (a, w)
p)
   where apply :: m (a, b) -> m (b, b)
apply m (a, b)
m = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (,) (m a -> m b
f forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (a, b)
m) (forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (a, b)
m)

mapLazyStateT :: Applicative m => (m a -> m b) -> Lazy.StateT w m a -> Lazy.StateT w m b
mapLazyStateT :: forall (m :: * -> *) a b w.
Applicative m =>
(m a -> m b) -> StateT w m a -> StateT w m b
mapLazyStateT m a -> m b
f (Lazy.StateT w -> m (a, w)
p) = forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
Lazy.StateT (forall {b}. m (a, b) -> m (b, b)
apply forall b c a. (b -> c) -> (a -> b) -> a -> c
. w -> m (a, w)
p)
   where apply :: m (a, b) -> m (b, b)
apply m (a, b)
m = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (,) (m a -> m b
f forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (a, b)
m) (forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (a, b)
m)

mapStrictStateT :: Applicative m => (m a -> m b) -> Strict.StateT s m a -> Strict.StateT s m b
mapStrictStateT :: forall (m :: * -> *) a b s.
Applicative m =>
(m a -> m b) -> StateT s m a -> StateT s m b
mapStrictStateT m a -> m b
f (Strict.StateT s -> m (a, s)
p) = forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
Strict.StateT (forall {b}. m (a, b) -> m (b, b)
apply forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> m (a, s)
p)
   where apply :: m (a, b) -> m (b, b)
apply m (a, b)
m = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (,) (m a -> m b
f forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (a, b)
m) (forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (a, b)
m)

mapLazyRWST :: Applicative m => (m a -> m b) -> Lazy.RWST r w s m a -> Lazy.RWST r w s m b
mapLazyRWST :: forall (m :: * -> *) a b r w s.
Applicative m =>
(m a -> m b) -> RWST r w s m a -> RWST r w s m b
mapLazyRWST m a -> m b
f (Lazy.RWST r -> s -> m (a, s, w)
p) = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
Lazy.RWST (\r
r-> forall {b} {c}. m (a, b, c) -> m (b, b, c)
apply forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> s -> m (a, s, w)
p r
r)
   where apply :: m (a, b, c) -> m (b, b, c)
apply m (a, b, c)
m = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a x b c. a -> (x, b, c) -> (a, b, c)
replaceFstOf3 (m a -> m b
f forall a b. (a -> b) -> a -> b
$ forall a b c. (a, b, c) -> a
fstOf3 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (a, b, c)
m) m (a, b, c)
m

mapStrictRWST :: Applicative m => (m a -> m b) -> Strict.RWST r w s m a -> Strict.RWST r w s m b
mapStrictRWST :: forall (m :: * -> *) a b r w s.
Applicative m =>
(m a -> m b) -> RWST r w s m a -> RWST r w s m b
mapStrictRWST m a -> m b
f (Strict.RWST r -> s -> m (a, s, w)
p) = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
Strict.RWST (\r
r-> forall {b} {c}. m (a, b, c) -> m (b, b, c)
apply forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> s -> m (a, s, w)
p r
r)
   where apply :: m (a, b, c) -> m (b, b, c)
apply m (a, b, c)
m = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a x b c. a -> (x, b, c) -> (a, b, c)
replaceFstOf3 (m a -> m b
f forall a b. (a -> b) -> a -> b
$ forall a b c. (a, b, c) -> a
fstOf3 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (a, b, c)
m) m (a, b, c)
m

fstOf3 :: (a, b, c) -> a
fstOf3 :: forall a b c. (a, b, c) -> a
fstOf3 (a
a, b
_, c
_) = a
a

replaceFstOf3 :: a -> (x, b, c) -> (a, b, c)
replaceFstOf3 :: forall a x b c. a -> (x, b, c) -> (a, b, c)
replaceFstOf3 a
a (x
_, b
b, c
c) = (a
a, b
b, c
c)