{-# LANGUAGE FunctionalDependencies, UndecidableInstances #-}
module Control.Monad.Journal.Class (
MonadJournal(..)
, sink
, absorb
) where
import Control.Monad ( Monad )
import Control.Monad.Trans ( MonadIO, MonadTrans, lift, liftIO )
import Control.Monad.Trans.Except ( ExceptT )
import Control.Monad.Trans.Identity ( IdentityT )
import Control.Monad.Trans.List ( ListT )
import Control.Monad.Trans.Maybe ( MaybeT )
import Control.Monad.Trans.RWS ( RWST )
import Control.Monad.Trans.Reader ( ReaderT )
import Control.Monad.Trans.State ( StateT )
import Control.Monad.Trans.Writer ( WriterT )
import Data.Monoid ( Monoid, mappend, mempty )
class (Monoid w, Monad m) => MonadJournal w m | m -> w where
journal :: w -> m ()
history :: m w
clear :: m ()
sink :: (MonadJournal w m, MonadIO m) => (w -> IO ()) -> m ()
sink :: (w -> IO ()) -> m ()
sink w -> IO ()
out = m w
forall w (m :: * -> *). MonadJournal w m => m w
history m w -> (w -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (w -> IO ()) -> w -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. w -> IO ()
out m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> m ()
forall w (m :: * -> *). MonadJournal w m => m ()
clear
absorb :: (MonadJournal w m) => (a,w) -> m a
absorb :: (a, w) -> m a
absorb (a
a,w
w) = w -> m ()
forall w (m :: * -> *). MonadJournal w m => w -> m ()
journal w
w m () -> m a -> m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
instance (Monad m, Monoid w, MonadJournal w m) => MonadJournal w (IdentityT m) where
journal :: w -> IdentityT m ()
journal !w
w = m () -> IdentityT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (w -> m ()
forall w (m :: * -> *). MonadJournal w m => w -> m ()
journal w
w)
history :: IdentityT m w
history = m w -> IdentityT m w
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m w
forall w (m :: * -> *). MonadJournal w m => m w
history
clear :: IdentityT m ()
clear = m () -> IdentityT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m ()
forall w (m :: * -> *). MonadJournal w m => m ()
clear
instance (Monad m, Monoid w, MonadJournal w m) => MonadJournal w (ListT m) where
journal :: w -> ListT m ()
journal !w
w = m () -> ListT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (w -> m ()
forall w (m :: * -> *). MonadJournal w m => w -> m ()
journal w
w)
history :: ListT m w
history = m w -> ListT m w
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m w
forall w (m :: * -> *). MonadJournal w m => m w
history
clear :: ListT m ()
clear = m () -> ListT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m ()
forall w (m :: * -> *). MonadJournal w m => m ()
clear
instance (Monad m, Monoid w, MonadJournal w m) => MonadJournal w (MaybeT m) where
journal :: w -> MaybeT m ()
journal !w
w = m () -> MaybeT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (w -> m ()
forall w (m :: * -> *). MonadJournal w m => w -> m ()
journal w
w)
history :: MaybeT m w
history = m w -> MaybeT m w
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m w
forall w (m :: * -> *). MonadJournal w m => m w
history
clear :: MaybeT m ()
clear = m () -> MaybeT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m ()
forall w (m :: * -> *). MonadJournal w m => m ()
clear
instance (Monad m, Monoid w, MonadJournal w m) => MonadJournal w (RWST r w s m) where
journal :: w -> RWST r w s m ()
journal !w
w = m () -> RWST r w s m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (w -> m ()
forall w (m :: * -> *). MonadJournal w m => w -> m ()
journal w
w)
history :: RWST r w s m w
history = m w -> RWST r w s m w
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m w
forall w (m :: * -> *). MonadJournal w m => m w
history
clear :: RWST r w s m ()
clear = m () -> RWST r w s m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m ()
forall w (m :: * -> *). MonadJournal w m => m ()
clear
instance (Monad m, Monoid w, MonadJournal w m) => MonadJournal w (ReaderT r m) where
journal :: w -> ReaderT r m ()
journal !w
w = m () -> ReaderT r m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (w -> m ()
forall w (m :: * -> *). MonadJournal w m => w -> m ()
journal w
w)
history :: ReaderT r m w
history = m w -> ReaderT r m w
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m w
forall w (m :: * -> *). MonadJournal w m => m w
history
clear :: ReaderT r m ()
clear = m () -> ReaderT r m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m ()
forall w (m :: * -> *). MonadJournal w m => m ()
clear
instance (Monad m, Monoid w, MonadJournal w m) => MonadJournal w (StateT s m) where
journal :: w -> StateT s m ()
journal !w
w = m () -> StateT s m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (w -> m ()
forall w (m :: * -> *). MonadJournal w m => w -> m ()
journal w
w)
history :: StateT s m w
history = m w -> StateT s m w
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m w
forall w (m :: * -> *). MonadJournal w m => m w
history
clear :: StateT s m ()
clear = m () -> StateT s m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m ()
forall w (m :: * -> *). MonadJournal w m => m ()
clear
instance (Monad m, Monoid w, Monoid q, MonadJournal w m) => MonadJournal w (WriterT q m) where
journal :: w -> WriterT q m ()
journal !w
w = m () -> WriterT q m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (w -> m ()
forall w (m :: * -> *). MonadJournal w m => w -> m ()
journal w
w)
history :: WriterT q m w
history = m w -> WriterT q m w
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m w
forall w (m :: * -> *). MonadJournal w m => m w
history
clear :: WriterT q m ()
clear = m () -> WriterT q m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m ()
forall w (m :: * -> *). MonadJournal w m => m ()
clear
instance (Monad m, Monoid w, MonadJournal w m) => MonadJournal w (ExceptT e m) where
journal :: w -> ExceptT e m ()
journal !w
w = m () -> ExceptT e m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (w -> m ()
forall w (m :: * -> *). MonadJournal w m => w -> m ()
journal w
w)
history :: ExceptT e m w
history = m w -> ExceptT e m w
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m w
forall w (m :: * -> *). MonadJournal w m => m w
history
clear :: ExceptT e m ()
clear = m () -> ExceptT e m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m ()
forall w (m :: * -> *). MonadJournal w m => m ()
clear