{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Safe #-}
#endif
#if __GLASGOW_HASKELL__ >= 710
{-# LANGUAGE AutoDeriveTypeable #-}
#endif
module Control.Monad.Trans.Accum (
Accum,
accum,
runAccum,
execAccum,
evalAccum,
mapAccum,
AccumT(AccumT),
runAccumT,
execAccumT,
evalAccumT,
mapAccumT,
look,
looks,
add,
liftCallCC,
liftCallCC',
liftCatch,
liftListen,
liftPass,
readerToAccumT,
writerToAccumT,
accumToStateT,
) where
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Control.Monad.Trans.Reader (ReaderT(..))
import Control.Monad.Trans.Writer (WriterT(..))
import Control.Monad.Trans.State (StateT(..))
import Data.Functor.Identity
import Control.Applicative
import Control.Monad
#if MIN_VERSION_base(4,9,0)
import qualified Control.Monad.Fail as Fail
#endif
import Control.Monad.Fix
import Control.Monad.Signatures
#if !MIN_VERSION_base(4,8,0)
import Data.Monoid
#endif
type Accum w = AccumT w Identity
accum :: (Monad m) => (w -> (a, w)) -> AccumT w m a
accum :: forall (m :: * -> *) w a. Monad m => (w -> (a, w)) -> AccumT w m a
accum w -> (a, w)
f = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ \ w
w -> forall (m :: * -> *) a. Monad m => a -> m a
return (w -> (a, w)
f w
w)
{-# INLINE accum #-}
runAccum :: Accum w a -> w -> (a, w)
runAccum :: forall w a. Accum w a -> w -> (a, w)
runAccum Accum w a
m = forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT Accum w a
m
{-# INLINE runAccum #-}
execAccum :: Accum w a -> w -> w
execAccum :: forall w a. Accum w a -> w -> w
execAccum Accum w a
m w
w = forall a b. (a, b) -> b
snd (forall w a. Accum w a -> w -> (a, w)
runAccum Accum w a
m w
w)
{-# INLINE execAccum #-}
evalAccum :: (Monoid w) => Accum w a -> w -> a
evalAccum :: forall w a. Monoid w => Accum w a -> w -> a
evalAccum Accum w a
m w
w = forall a b. (a, b) -> a
fst (forall w a. Accum w a -> w -> (a, w)
runAccum Accum w a
m w
w)
{-# INLINE evalAccum #-}
mapAccum :: ((a, w) -> (b, w)) -> Accum w a -> Accum w b
mapAccum :: forall a w b. ((a, w) -> (b, w)) -> Accum w a -> Accum w b
mapAccum (a, w) -> (b, w)
f = forall (m :: * -> *) a w (n :: * -> *) b.
(m (a, w) -> n (b, w)) -> AccumT w m a -> AccumT w n b
mapAccumT (forall a. a -> Identity a
Identity forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, w) -> (b, w)
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Identity a -> a
runIdentity)
{-# INLINE mapAccum #-}
newtype AccumT w m a = AccumT (w -> m (a, w))
runAccumT :: AccumT w m a -> w -> m (a, w)
runAccumT :: forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT (AccumT w -> m (a, w)
f) = w -> m (a, w)
f
{-# INLINE runAccumT #-}
execAccumT :: (Monad m) => AccumT w m a -> w -> m w
execAccumT :: forall (m :: * -> *) w a. Monad m => AccumT w m a -> w -> m w
execAccumT AccumT w m a
m w
w = do
~(a
_, w
w') <- forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT AccumT w m a
m w
w
forall (m :: * -> *) a. Monad m => a -> m a
return w
w'
{-# INLINE execAccumT #-}
evalAccumT :: (Monad m, Monoid w) => AccumT w m a -> w -> m a
evalAccumT :: forall (m :: * -> *) w a.
(Monad m, Monoid w) =>
AccumT w m a -> w -> m a
evalAccumT AccumT w m a
m w
w = do
~(a
a, w
_) <- forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT AccumT w m a
m w
w
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
{-# INLINE evalAccumT #-}
mapAccumT :: (m (a, w) -> n (b, w)) -> AccumT w m a -> AccumT w n b
mapAccumT :: forall (m :: * -> *) a w (n :: * -> *) b.
(m (a, w) -> n (b, w)) -> AccumT w m a -> AccumT w n b
mapAccumT m (a, w) -> n (b, w)
f AccumT w m a
m = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT (m (a, w) -> n (b, w)
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT AccumT w m a
m)
{-# INLINE mapAccumT #-}
instance (Functor m) => Functor (AccumT w m) where
fmap :: forall a b. (a -> b) -> AccumT w m a -> AccumT w m b
fmap a -> b
f = forall (m :: * -> *) a w (n :: * -> *) b.
(m (a, w) -> n (b, w)) -> AccumT w m a -> AccumT w n b
mapAccumT forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a -> b) -> a -> b
$ \ ~(a
a, w
w) -> (a -> b
f a
a, w
w)
{-# INLINE fmap #-}
instance (Monoid w, Functor m, Monad m) => Applicative (AccumT w m) where
pure :: forall a. a -> AccumT w m a
pure a
a = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, forall a. Monoid a => a
mempty)
{-# INLINE pure #-}
AccumT w m (a -> b)
mf <*> :: forall a b. AccumT w m (a -> b) -> AccumT w m a -> AccumT w m b
<*> AccumT w m a
mv = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ \ w
w -> do
~(a -> b
f, w
w') <- forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT AccumT w m (a -> b)
mf w
w
~(a
v, w
w'') <- forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT AccumT w m a
mv (w
w forall a. Monoid a => a -> a -> a
`mappend` w
w')
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b
f a
v, w
w' forall a. Monoid a => a -> a -> a
`mappend` w
w'')
{-# INLINE (<*>) #-}
instance (Monoid w, Functor m, MonadPlus m) => Alternative (AccumT w m) where
empty :: forall a. AccumT w m a
empty = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const forall (m :: * -> *) a. MonadPlus m => m a
mzero
{-# INLINE empty #-}
AccumT w m a
m <|> :: forall a. AccumT w m a -> AccumT w m a -> AccumT w m a
<|> AccumT w m a
n = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ \ w
w -> forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT AccumT w m a
m w
w forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT AccumT w m a
n w
w
{-# INLINE (<|>) #-}
instance (Monoid w, Functor m, Monad m) => Monad (AccumT w m) where
#if !(MIN_VERSION_base(4,8,0))
return a = AccumT $ const $ return (a, mempty)
{-# INLINE return #-}
#endif
AccumT w m a
m >>= :: forall a b. AccumT w m a -> (a -> AccumT w m b) -> AccumT w m b
>>= a -> AccumT w m b
k = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ \ w
w -> do
~(a
a, w
w') <- forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT AccumT w m a
m w
w
~(b
b, w
w'') <- forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT (a -> AccumT w m b
k a
a) (w
w forall a. Monoid a => a -> a -> a
`mappend` w
w')
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, w
w' forall a. Monoid a => a -> a -> a
`mappend` w
w'')
{-# INLINE (>>=) #-}
#if !(MIN_VERSION_base(4,13,0))
fail msg = AccumT $ const (fail msg)
{-# INLINE fail #-}
#endif
#if MIN_VERSION_base(4,9,0)
instance (Monoid w, Fail.MonadFail m) => Fail.MonadFail (AccumT w m) where
fail :: forall a. String -> AccumT w m a
fail String
msg = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const (forall (m :: * -> *) a. MonadFail m => String -> m a
Fail.fail String
msg)
{-# INLINE fail #-}
#endif
instance (Monoid w, Functor m, MonadPlus m) => MonadPlus (AccumT w m) where
mzero :: forall a. AccumT w m a
mzero = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const forall (m :: * -> *) a. MonadPlus m => m a
mzero
{-# INLINE mzero #-}
AccumT w m a
m mplus :: forall a. AccumT w m a -> AccumT w m a -> AccumT w m a
`mplus` AccumT w m a
n = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ \ w
w -> forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT AccumT w m a
m w
w forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT AccumT w m a
n w
w
{-# INLINE mplus #-}
instance (Monoid w, Functor m, MonadFix m) => MonadFix (AccumT w m) where
mfix :: forall a. (a -> AccumT w m a) -> AccumT w m a
mfix a -> AccumT w m a
m = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ \ w
w -> forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix forall a b. (a -> b) -> a -> b
$ \ ~(a
a, w
_) -> forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT (a -> AccumT w m a
m a
a) w
w
{-# INLINE mfix #-}
instance (Monoid w) => MonadTrans (AccumT w) where
lift :: forall (m :: * -> *) a. Monad m => m a -> AccumT w m a
lift m a
m = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ do
a
a <- m a
m
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, forall a. Monoid a => a
mempty)
{-# INLINE lift #-}
instance (Monoid w, Functor m, MonadIO m) => MonadIO (AccumT w m) where
liftIO :: forall a. IO a -> AccumT w m a
liftIO = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
{-# INLINE liftIO #-}
look :: (Monoid w, Monad m) => AccumT w m w
look :: forall w (m :: * -> *). (Monoid w, Monad m) => AccumT w m w
look = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ \ w
w -> forall (m :: * -> *) a. Monad m => a -> m a
return (w
w, forall a. Monoid a => a
mempty)
looks :: (Monoid w, Monad m) => (w -> a) -> AccumT w m a
looks :: forall w (m :: * -> *) a.
(Monoid w, Monad m) =>
(w -> a) -> AccumT w m a
looks w -> a
f = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ \ w
w -> forall (m :: * -> *) a. Monad m => a -> m a
return (w -> a
f w
w, forall a. Monoid a => a
mempty)
add :: (Monad m) => w -> AccumT w m ()
add :: forall (m :: * -> *) w. Monad m => w -> AccumT w m ()
add w
w = forall (m :: * -> *) w a. Monad m => (w -> (a, w)) -> AccumT w m a
accum forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const ((), w
w)
{-# INLINE add #-}
liftCallCC :: CallCC m (a, w) (b, w) -> CallCC (AccumT w m) a b
liftCallCC :: forall (m :: * -> *) a w b.
CallCC m (a, w) (b, w) -> CallCC (AccumT w m) a b
liftCallCC CallCC m (a, w) (b, w)
callCC (a -> AccumT w m b) -> AccumT w m a
f = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ \ w
w ->
CallCC m (a, w) (b, w)
callCC forall a b. (a -> b) -> a -> b
$ \ (a, w) -> m (b, w)
c ->
forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT ((a -> AccumT w m b) -> AccumT w m a
f (\ a
a -> forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ \ w
_ -> (a, w) -> m (b, w)
c (a
a, w
w))) w
w
{-# INLINE liftCallCC #-}
liftCallCC' :: CallCC m (a, w) (b, w) -> CallCC (AccumT w m) a b
liftCallCC' :: forall (m :: * -> *) a w b.
CallCC m (a, w) (b, w) -> CallCC (AccumT w m) a b
liftCallCC' CallCC m (a, w) (b, w)
callCC (a -> AccumT w m b) -> AccumT w m a
f = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ \ w
s ->
CallCC m (a, w) (b, w)
callCC forall a b. (a -> b) -> a -> b
$ \ (a, w) -> m (b, w)
c ->
forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT ((a -> AccumT w m b) -> AccumT w m a
f (\ a
a -> forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ \ w
s' -> (a, w) -> m (b, w)
c (a
a, w
s'))) w
s
{-# INLINE liftCallCC' #-}
liftCatch :: Catch e m (a, w) -> Catch e (AccumT w m) a
liftCatch :: forall e (m :: * -> *) a w.
Catch e m (a, w) -> Catch e (AccumT w m) a
liftCatch Catch e m (a, w)
catchE AccumT w m a
m e -> AccumT w m a
h =
forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ \ w
w -> forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT AccumT w m a
m w
w Catch e m (a, w)
`catchE` \ e
e -> forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT (e -> AccumT w m a
h e
e) w
w
{-# INLINE liftCatch #-}
liftListen :: (Monad m) => Listen w m (a, s) -> Listen w (AccumT s m) a
liftListen :: forall (m :: * -> *) w a s.
Monad m =>
Listen w m (a, s) -> Listen w (AccumT s m) a
liftListen Listen w m (a, s)
listen AccumT s m a
m = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ \ s
s -> do
~((a
a, s
s'), w
w) <- Listen w m (a, s)
listen (forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT AccumT s m a
m s
s)
forall (m :: * -> *) a. Monad m => a -> m a
return ((a
a, w
w), s
s')
{-# INLINE liftListen #-}
liftPass :: (Monad m) => Pass w m (a, s) -> Pass w (AccumT s m) a
liftPass :: forall (m :: * -> *) w a s.
Monad m =>
Pass w m (a, s) -> Pass w (AccumT s m) a
liftPass Pass w m (a, s)
pass AccumT s m (a, w -> w)
m = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ \ s
s -> Pass w m (a, s)
pass forall a b. (a -> b) -> a -> b
$ do
~((a
a, w -> w
f), s
s') <- forall w (m :: * -> *) a. AccumT w m a -> w -> m (a, w)
runAccumT AccumT s m (a, w -> w)
m s
s
forall (m :: * -> *) a. Monad m => a -> m a
return ((a
a, s
s'), w -> w
f)
{-# INLINE liftPass #-}
readerToAccumT :: (Functor m, Monoid w) => ReaderT w m a -> AccumT w m a
readerToAccumT :: forall (m :: * -> *) w a.
(Functor m, Monoid w) =>
ReaderT w m a -> AccumT w m a
readerToAccumT (ReaderT w -> m a
f) = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ \ w
w -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\ a
a -> (a
a, forall a. Monoid a => a
mempty)) (w -> m a
f w
w)
{-# INLINE readerToAccumT #-}
writerToAccumT :: WriterT w m a -> AccumT w m a
writerToAccumT :: forall w (m :: * -> *) a. WriterT w m a -> AccumT w m a
writerToAccumT (WriterT m (a, w)
m) = forall w (m :: * -> *) a. (w -> m (a, w)) -> AccumT w m a
AccumT forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ m (a, w)
m
{-# INLINE writerToAccumT #-}
accumToStateT :: (Functor m, Monoid s) => AccumT s m a -> StateT s m a
accumToStateT :: forall (m :: * -> *) s a.
(Functor m, Monoid s) =>
AccumT s m a -> StateT s m a
accumToStateT (AccumT s -> m (a, s)
f) =
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT forall a b. (a -> b) -> a -> b
$ \ s
w -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\ ~(a
a, s
w') -> (a
a, s
w forall a. Monoid a => a -> a -> a
`mappend` s
w')) (s -> m (a, s)
f s
w)
{-# INLINE accumToStateT #-}