module Control.Monad.IO.Peel (
MonadPeelIO(..),
liftIOOp,
liftIOOp_,
) where
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Trans.Peel
import Control.Monad.Trans.Identity
import Control.Monad.Trans.List
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Error
import Control.Monad.Trans.Reader
import Control.Monad.Trans.State
import qualified Control.Monad.Trans.State.Strict as Strict
import Control.Monad.Trans.Writer
import qualified Control.Monad.Trans.Writer.Strict as Strict
import qualified Control.Monad.Trans.RWS as RWS
import qualified Control.Monad.Trans.RWS.Strict as RWS.Strict
import Data.Monoid
class MonadIO m => MonadPeelIO m where
peelIO :: m (m a -> IO (m a))
instance MonadPeelIO IO where
peelIO = idPeel
instance MonadPeelIO m => MonadPeelIO (IdentityT m) where
peelIO = liftPeel peelIO
instance MonadPeelIO m => MonadPeelIO (ListT m) where
peelIO = liftPeel peelIO
instance MonadPeelIO m => MonadPeelIO (MaybeT m) where
peelIO = liftPeel peelIO
instance (Error e, MonadPeelIO m) => MonadPeelIO (ErrorT e m) where
peelIO = liftPeel peelIO
instance MonadPeelIO m => MonadPeelIO (ReaderT r m) where
peelIO = liftPeel peelIO
instance MonadPeelIO m => MonadPeelIO (StateT s m) where
peelIO = liftPeel peelIO
instance MonadPeelIO m => MonadPeelIO (Strict.StateT s m) where
peelIO = liftPeel peelIO
instance (Monoid w, MonadPeelIO m) => MonadPeelIO (WriterT w m) where
peelIO = liftPeel peelIO
instance (Monoid w, MonadPeelIO m) => MonadPeelIO (Strict.WriterT w m) where
peelIO = liftPeel peelIO
instance (Monoid w, MonadPeelIO m) => MonadPeelIO (RWS.RWST r w s m) where
peelIO = liftPeel peelIO
instance (Monoid w, MonadPeelIO m) =>
MonadPeelIO (RWS.Strict.RWST r w s m) where
peelIO = liftPeel peelIO
liftIOOp :: MonadPeelIO m => ((a -> IO (m b)) -> IO (m c)) -> (a -> m b) -> m c
liftIOOp f g = do
k <- peelIO
join $ liftIO $ f (k . g)
liftIOOp_ :: MonadPeelIO m => (IO (m a) -> IO (m b)) -> m a -> m b
liftIOOp_ f m = do
k <- peelIO
join $ liftIO $ f (k m)