{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Safe #-}
#endif
#if __GLASGOW_HASKELL__ >= 710
{-# LANGUAGE AutoDeriveTypeable #-}
#endif
module Control.Monad.Trans.Writer.CPS (
Writer,
writer,
runWriter,
execWriter,
mapWriter,
WriterT,
writerT,
runWriterT,
execWriterT,
mapWriterT,
tell,
listen,
listens,
pass,
censor,
liftCallCC,
liftCatch,
) where
import Control.Applicative
import Control.Monad
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Control.Monad.Signatures
import Data.Functor.Identity
#if !(MIN_VERSION_base(4,8,0))
import Data.Monoid
#endif
#if MIN_VERSION_base(4,9,0)
import qualified Control.Monad.Fail as Fail
#endif
type Writer w = WriterT w Identity
writer :: (Monoid w, Monad m) => (a, w) -> WriterT w m a
writer :: (a, w) -> WriterT w m a
writer (a
a, w
w') = (w -> m (a, w)) -> WriterT w m a
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w -> m (a, w)) -> WriterT w m a)
-> (w -> m (a, w)) -> WriterT w m a
forall a b. (a -> b) -> a -> b
$ \ w
w ->
let wt :: w
wt = w
w w -> w -> w
forall a. Monoid a => a -> a -> a
`mappend` w
w' in w
wt w -> m (a, w) -> m (a, w)
`seq` (a, w) -> m (a, w)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, w
wt)
{-# INLINE writer #-}
runWriter :: (Monoid w) => Writer w a -> (a, w)
runWriter :: Writer w a -> (a, w)
runWriter = Identity (a, w) -> (a, w)
forall a. Identity a -> a
runIdentity (Identity (a, w) -> (a, w))
-> (Writer w a -> Identity (a, w)) -> Writer w a -> (a, w)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Writer w a -> Identity (a, w)
forall w (m :: * -> *) a. Monoid w => WriterT w m a -> m (a, w)
runWriterT
{-# INLINE runWriter #-}
execWriter :: (Monoid w) => Writer w a -> w
execWriter :: Writer w a -> w
execWriter = Identity w -> w
forall a. Identity a -> a
runIdentity (Identity w -> w) -> (Writer w a -> Identity w) -> Writer w a -> w
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Writer w a -> Identity w
forall (m :: * -> *) w a.
(Monad m, Monoid w) =>
WriterT w m a -> m w
execWriterT
{-# INLINE execWriter #-}
mapWriter :: (Monoid w, Monoid w') =>
((a, w) -> (b, w')) -> Writer w a -> Writer w' b
mapWriter :: ((a, w) -> (b, w')) -> Writer w a -> Writer w' b
mapWriter (a, w) -> (b, w')
f = (Identity (a, w) -> Identity (b, w')) -> Writer w a -> Writer w' b
forall (n :: * -> *) w w' (m :: * -> *) a b.
(Monad n, Monoid w, Monoid w') =>
(m (a, w) -> n (b, w')) -> WriterT w m a -> WriterT w' n b
mapWriterT ((b, w') -> Identity (b, w')
forall a. a -> Identity a
Identity ((b, w') -> Identity (b, w'))
-> (Identity (a, w) -> (b, w'))
-> Identity (a, w)
-> Identity (b, w')
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, w) -> (b, w')
f ((a, w) -> (b, w'))
-> (Identity (a, w) -> (a, w)) -> Identity (a, w) -> (b, w')
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity (a, w) -> (a, w)
forall a. Identity a -> a
runIdentity)
{-# INLINE mapWriter #-}
newtype WriterT w m a = WriterT { WriterT w m a -> w -> m (a, w)
unWriterT :: w -> m (a, w) }
writerT :: (Functor m, Monoid w) => m (a, w) -> WriterT w m a
writerT :: m (a, w) -> WriterT w m a
writerT m (a, w)
f = (w -> m (a, w)) -> WriterT w m a
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w -> m (a, w)) -> WriterT w m a)
-> (w -> m (a, w)) -> WriterT w m a
forall a b. (a -> b) -> a -> b
$ \ w
w ->
(\ (a
a, w
w') -> let wt :: w
wt = w
w w -> w -> w
forall a. Monoid a => a -> a -> a
`mappend` w
w' in w
wt w -> (a, w) -> (a, w)
`seq` (a
a, w
wt)) ((a, w) -> (a, w)) -> m (a, w) -> m (a, w)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (a, w)
f
{-# INLINE writerT #-}
runWriterT :: (Monoid w) => WriterT w m a -> m (a, w)
runWriterT :: WriterT w m a -> m (a, w)
runWriterT WriterT w m a
m = WriterT w m a -> w -> m (a, w)
forall w (m :: * -> *) a. WriterT w m a -> w -> m (a, w)
unWriterT WriterT w m a
m w
forall a. Monoid a => a
mempty
{-# INLINE runWriterT #-}
execWriterT :: (Monad m, Monoid w) => WriterT w m a -> m w
execWriterT :: WriterT w m a -> m w
execWriterT WriterT w m a
m = do
(a
_, w
w) <- WriterT w m a -> m (a, w)
forall w (m :: * -> *) a. Monoid w => WriterT w m a -> m (a, w)
runWriterT WriterT w m a
m
w -> m w
forall (m :: * -> *) a. Monad m => a -> m a
return w
w
{-# INLINE execWriterT #-}
mapWriterT :: (Monad n, Monoid w, Monoid w') =>
(m (a, w) -> n (b, w')) -> WriterT w m a -> WriterT w' n b
mapWriterT :: (m (a, w) -> n (b, w')) -> WriterT w m a -> WriterT w' n b
mapWriterT m (a, w) -> n (b, w')
f WriterT w m a
m = (w' -> n (b, w')) -> WriterT w' n b
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w' -> n (b, w')) -> WriterT w' n b)
-> (w' -> n (b, w')) -> WriterT w' n b
forall a b. (a -> b) -> a -> b
$ \ w'
w -> do
(b
a, w'
w') <- m (a, w) -> n (b, w')
f (WriterT w m a -> m (a, w)
forall w (m :: * -> *) a. Monoid w => WriterT w m a -> m (a, w)
runWriterT WriterT w m a
m)
let wt :: w'
wt = w'
w w' -> w' -> w'
forall a. Monoid a => a -> a -> a
`mappend` w'
w'
w'
wt w' -> n (b, w') -> n (b, w')
`seq` (b, w') -> n (b, w')
forall (m :: * -> *) a. Monad m => a -> m a
return (b
a, w'
wt)
{-# INLINE mapWriterT #-}
instance (Functor m) => Functor (WriterT w m) where
fmap :: (a -> b) -> WriterT w m a -> WriterT w m b
fmap a -> b
f WriterT w m a
m = (w -> m (b, w)) -> WriterT w m b
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w -> m (b, w)) -> WriterT w m b)
-> (w -> m (b, w)) -> WriterT w m b
forall a b. (a -> b) -> a -> b
$ \ w
w -> (\ (a
a, w
w') -> (a -> b
f a
a, w
w')) ((a, w) -> (b, w)) -> m (a, w) -> m (b, w)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> WriterT w m a -> w -> m (a, w)
forall w (m :: * -> *) a. WriterT w m a -> w -> m (a, w)
unWriterT WriterT w m a
m w
w
{-# INLINE fmap #-}
instance (Functor m, Monad m) => Applicative (WriterT w m) where
pure :: a -> WriterT w m a
pure a
a = (w -> m (a, w)) -> WriterT w m a
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w -> m (a, w)) -> WriterT w m a)
-> (w -> m (a, w)) -> WriterT w m a
forall a b. (a -> b) -> a -> b
$ \ w
w -> (a, w) -> m (a, w)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, w
w)
{-# INLINE pure #-}
WriterT w -> m (a -> b, w)
mf <*> :: WriterT w m (a -> b) -> WriterT w m a -> WriterT w m b
<*> WriterT w -> m (a, w)
mx = (w -> m (b, w)) -> WriterT w m b
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w -> m (b, w)) -> WriterT w m b)
-> (w -> m (b, w)) -> WriterT w m b
forall a b. (a -> b) -> a -> b
$ \ w
w -> do
(a -> b
f, w
w') <- w -> m (a -> b, w)
mf w
w
(a
x, w
w'') <- w -> m (a, w)
mx w
w'
(b, w) -> m (b, w)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b
f a
x, w
w'')
{-# INLINE (<*>) #-}
instance (Functor m, MonadPlus m) => Alternative (WriterT w m) where
empty :: WriterT w m a
empty = (w -> m (a, w)) -> WriterT w m a
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w -> m (a, w)) -> WriterT w m a)
-> (w -> m (a, w)) -> WriterT w m a
forall a b. (a -> b) -> a -> b
$ m (a, w) -> w -> m (a, w)
forall a b. a -> b -> a
const m (a, w)
forall (m :: * -> *) a. MonadPlus m => m a
mzero
{-# INLINE empty #-}
WriterT w -> m (a, w)
m <|> :: WriterT w m a -> WriterT w m a -> WriterT w m a
<|> WriterT w -> m (a, w)
n = (w -> m (a, w)) -> WriterT w m a
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w -> m (a, w)) -> WriterT w m a)
-> (w -> m (a, w)) -> WriterT w m a
forall a b. (a -> b) -> a -> b
$ \ w
w -> w -> m (a, w)
m w
w m (a, w) -> m (a, w) -> m (a, w)
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` w -> m (a, w)
n w
w
{-# INLINE (<|>) #-}
instance (Monad m) => Monad (WriterT w m) where
#if !(MIN_VERSION_base(4,8,0))
return a = WriterT $ \ w -> return (a, w)
{-# INLINE return #-}
#endif
WriterT w m a
m >>= :: WriterT w m a -> (a -> WriterT w m b) -> WriterT w m b
>>= a -> WriterT w m b
k = (w -> m (b, w)) -> WriterT w m b
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w -> m (b, w)) -> WriterT w m b)
-> (w -> m (b, w)) -> WriterT w m b
forall a b. (a -> b) -> a -> b
$ \ w
w -> do
(a
a, w
w') <- WriterT w m a -> w -> m (a, w)
forall w (m :: * -> *) a. WriterT w m a -> w -> m (a, w)
unWriterT WriterT w m a
m w
w
WriterT w m b -> w -> m (b, w)
forall w (m :: * -> *) a. WriterT w m a -> w -> m (a, w)
unWriterT (a -> WriterT w m b
k a
a) w
w'
{-# INLINE (>>=) #-}
#if !(MIN_VERSION_base(4,13,0))
fail msg = WriterT $ \ _ -> fail msg
{-# INLINE fail #-}
#endif
#if MIN_VERSION_base(4,9,0)
instance (Fail.MonadFail m) => Fail.MonadFail (WriterT w m) where
fail :: String -> WriterT w m a
fail String
msg = (w -> m (a, w)) -> WriterT w m a
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w -> m (a, w)) -> WriterT w m a)
-> (w -> m (a, w)) -> WriterT w m a
forall a b. (a -> b) -> a -> b
$ \ w
_ -> String -> m (a, w)
forall (m :: * -> *) a. MonadFail m => String -> m a
Fail.fail String
msg
{-# INLINE fail #-}
#endif
instance (Functor m, MonadPlus m) => MonadPlus (WriterT w m) where
mzero :: WriterT w m a
mzero = WriterT w m a
forall (f :: * -> *) a. Alternative f => f a
empty
{-# INLINE mzero #-}
mplus :: WriterT w m a -> WriterT w m a -> WriterT w m a
mplus = WriterT w m a -> WriterT w m a -> WriterT w m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
{-# INLINE mplus #-}
instance (MonadFix m) => MonadFix (WriterT w m) where
mfix :: (a -> WriterT w m a) -> WriterT w m a
mfix a -> WriterT w m a
f = (w -> m (a, w)) -> WriterT w m a
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w -> m (a, w)) -> WriterT w m a)
-> (w -> m (a, w)) -> WriterT w m a
forall a b. (a -> b) -> a -> b
$ \ w
w -> ((a, w) -> m (a, w)) -> m (a, w)
forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix (((a, w) -> m (a, w)) -> m (a, w))
-> ((a, w) -> m (a, w)) -> m (a, w)
forall a b. (a -> b) -> a -> b
$ \ ~(a
a, w
_) -> WriterT w m a -> w -> m (a, w)
forall w (m :: * -> *) a. WriterT w m a -> w -> m (a, w)
unWriterT (a -> WriterT w m a
f a
a) w
w
{-# INLINE mfix #-}
instance MonadTrans (WriterT w) where
lift :: m a -> WriterT w m a
lift m a
m = (w -> m (a, w)) -> WriterT w m a
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w -> m (a, w)) -> WriterT w m a)
-> (w -> m (a, w)) -> WriterT w m a
forall a b. (a -> b) -> a -> b
$ \ w
w -> do
a
a <- m a
m
(a, w) -> m (a, w)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, w
w)
{-# INLINE lift #-}
instance (MonadIO m) => MonadIO (WriterT w m) where
liftIO :: IO a -> WriterT w m a
liftIO = m a -> WriterT w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> WriterT w m a) -> (IO a -> m a) -> IO a -> WriterT w m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
{-# INLINE liftIO #-}
tell :: (Monoid w, Monad m) => w -> WriterT w m ()
tell :: w -> WriterT w m ()
tell w
w = ((), w) -> WriterT w m ()
forall w (m :: * -> *) a.
(Monoid w, Monad m) =>
(a, w) -> WriterT w m a
writer ((), w
w)
{-# INLINE tell #-}
listen :: (Monoid w, Monad m) => WriterT w m a -> WriterT w m (a, w)
listen :: WriterT w m a -> WriterT w m (a, w)
listen = (w -> w) -> WriterT w m a -> WriterT w m (a, w)
forall w (m :: * -> *) b a.
(Monoid w, Monad m) =>
(w -> b) -> WriterT w m a -> WriterT w m (a, b)
listens w -> w
forall a. a -> a
id
{-# INLINE listen #-}
listens :: (Monoid w, Monad m) =>
(w -> b) -> WriterT w m a -> WriterT w m (a, b)
listens :: (w -> b) -> WriterT w m a -> WriterT w m (a, b)
listens w -> b
f WriterT w m a
m = (w -> m ((a, b), w)) -> WriterT w m (a, b)
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w -> m ((a, b), w)) -> WriterT w m (a, b))
-> (w -> m ((a, b), w)) -> WriterT w m (a, b)
forall a b. (a -> b) -> a -> b
$ \ w
w -> do
(a
a, w
w') <- WriterT w m a -> m (a, w)
forall w (m :: * -> *) a. Monoid w => WriterT w m a -> m (a, w)
runWriterT WriterT w m a
m
let wt :: w
wt = w
w w -> w -> w
forall a. Monoid a => a -> a -> a
`mappend` w
w'
w
wt w -> m ((a, b), w) -> m ((a, b), w)
`seq` ((a, b), w) -> m ((a, b), w)
forall (m :: * -> *) a. Monad m => a -> m a
return ((a
a, w -> b
f w
w'), w
wt)
{-# INLINE listens #-}
pass :: (Monoid w, Monoid w', Monad m) =>
WriterT w m (a, w -> w') -> WriterT w' m a
pass :: WriterT w m (a, w -> w') -> WriterT w' m a
pass WriterT w m (a, w -> w')
m = (w' -> m (a, w')) -> WriterT w' m a
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w' -> m (a, w')) -> WriterT w' m a)
-> (w' -> m (a, w')) -> WriterT w' m a
forall a b. (a -> b) -> a -> b
$ \ w'
w -> do
((a
a, w -> w'
f), w
w') <- WriterT w m (a, w -> w') -> m ((a, w -> w'), w)
forall w (m :: * -> *) a. Monoid w => WriterT w m a -> m (a, w)
runWriterT WriterT w m (a, w -> w')
m
let wt :: w'
wt = w'
w w' -> w' -> w'
forall a. Monoid a => a -> a -> a
`mappend` w -> w'
f w
w'
w'
wt w' -> m (a, w') -> m (a, w')
`seq` (a, w') -> m (a, w')
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, w'
wt)
{-# INLINE pass #-}
censor :: (Monoid w, Monad m) => (w -> w) -> WriterT w m a -> WriterT w m a
censor :: (w -> w) -> WriterT w m a -> WriterT w m a
censor w -> w
f WriterT w m a
m = (w -> m (a, w)) -> WriterT w m a
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w -> m (a, w)) -> WriterT w m a)
-> (w -> m (a, w)) -> WriterT w m a
forall a b. (a -> b) -> a -> b
$ \ w
w -> do
(a
a, w
w') <- WriterT w m a -> m (a, w)
forall w (m :: * -> *) a. Monoid w => WriterT w m a -> m (a, w)
runWriterT WriterT w m a
m
let wt :: w
wt = w
w w -> w -> w
forall a. Monoid a => a -> a -> a
`mappend` w -> w
f w
w'
w
wt w -> m (a, w) -> m (a, w)
`seq` (a, w) -> m (a, w)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, w
wt)
{-# INLINE censor #-}
liftCallCC :: CallCC m (a, w) (b, w) -> CallCC (WriterT w m) a b
liftCallCC :: CallCC m (a, w) (b, w) -> CallCC (WriterT w m) a b
liftCallCC CallCC m (a, w) (b, w)
callCC (a -> WriterT w m b) -> WriterT w m a
f = (w -> m (a, w)) -> WriterT w m a
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w -> m (a, w)) -> WriterT w m a)
-> (w -> m (a, w)) -> WriterT w m a
forall a b. (a -> b) -> a -> b
$ \ w
w ->
CallCC m (a, w) (b, w)
callCC CallCC m (a, w) (b, w) -> CallCC m (a, w) (b, w)
forall a b. (a -> b) -> a -> b
$ \ (a, w) -> m (b, w)
c -> WriterT w m a -> w -> m (a, w)
forall w (m :: * -> *) a. WriterT w m a -> w -> m (a, w)
unWriterT ((a -> WriterT w m b) -> WriterT w m a
f (\ a
a -> (w -> m (b, w)) -> WriterT w m b
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w -> m (b, w)) -> WriterT w m b)
-> (w -> m (b, w)) -> WriterT w m b
forall a b. (a -> b) -> a -> b
$ \ w
_ -> (a, w) -> m (b, w)
c (a
a, w
w))) w
w
{-# INLINE liftCallCC #-}
liftCatch :: Catch e m (a, w) -> Catch e (WriterT w m) a
liftCatch :: Catch e m (a, w) -> Catch e (WriterT w m) a
liftCatch Catch e m (a, w)
catchE WriterT w m a
m e -> WriterT w m a
h = (w -> m (a, w)) -> WriterT w m a
forall w (m :: * -> *) a. (w -> m (a, w)) -> WriterT w m a
WriterT ((w -> m (a, w)) -> WriterT w m a)
-> (w -> m (a, w)) -> WriterT w m a
forall a b. (a -> b) -> a -> b
$ \ w
w ->
WriterT w m a -> w -> m (a, w)
forall w (m :: * -> *) a. WriterT w m a -> w -> m (a, w)
unWriterT WriterT w m a
m w
w Catch e m (a, w)
`catchE` \ e
e -> WriterT w m a -> w -> m (a, w)
forall w (m :: * -> *) a. WriterT w m a -> w -> m (a, w)
unWriterT (e -> WriterT w m a
h e
e) w
w
{-# INLINE liftCatch #-}