monad-peel-0.1: Lift control operations like exception catching through monad transformers

Portabilityportable
Stabilityexperimental
MaintainerAnders Kaseorg <andersk@mit.edu>

Control.Monad.IO.Peel

Description

This module defines the class MonadPeelIO of IO-based monads into which control operations on IO (such as exception catching; see Control.Exception.Peel) can be lifted.

liftIOOp and liftIOOp_ enable convenient lifting of two common special cases of control operation types.

Synopsis

Documentation

class MonadIO m => MonadPeelIO m whereSource

MonadPeelIO is the class of IO-based monads supporting an extra operation peelIO, enabling control operations on IO to be lifted into the monad.

Methods

peelIO :: m (m a -> IO (m a))Source

peelIO is a version of peel that operates through an arbitrary stack of monad transformers directly to an inner IO (analagously to how liftIO is a version of lift). So it can be used with liftIO to lift control operations on IO into any monad in MonadPeelIO. For example:

    foo :: IO a -> IO a
    foo' :: MonadPeelIO m => m a -> m a
    foo' a = do
      k <- peelIO  -- k :: m a -> IO (m a)
      join $ liftIO $ foo (k a)  -- uses foo :: IO (m a) -> IO (m a)

Note that the "obvious" term of this type (peelIO = return return) does not work correctly. Instances of MonadPeelIO should be constructed via MonadTransPeel, using peelIO = liftPeel peelIO.

liftIOOp :: MonadPeelIO m => ((a -> IO (m b)) -> IO (m c)) -> (a -> m b) -> m cSource

liftIOOp is a particular application of peelIO that allows lifting control operations of type (a -> IO b) -> IO b (e.g. alloca, withMVar v) to MonadPeelIO m => (a -> m b) -> m b.

    liftIOOp f g = do
      k <- peelIO
      join $ liftIO $ f (k . g)

liftIOOp_ :: MonadPeelIO m => (IO (m a) -> IO (m b)) -> m a -> m bSource

liftIOOp_ is a particular application of peelIO that allows lifting control operations of type IO a -> IO a (e.g. block) to MonadPeelIO m => m a -> m a.

    liftIOOp_ f m = do
      k <- peelIO
      join $ liftIO $ f (k m)