module Control.Monad.Managed (
Managed,
MonadManaged(..),
managed,
managed_,
with,
runManaged,
module Control.Monad.IO.Class
) where
import Control.Monad.IO.Class (MonadIO(liftIO))
import Control.Monad.Trans.Class (lift)
#if MIN_VERSION_base(4,8,0)
import Control.Applicative (liftA2)
#else
import Control.Applicative
import Data.Monoid
#endif
import qualified Control.Monad.Trans.Cont as Cont
#if MIN_VERSION_transformers(0,4,0)
import qualified Control.Monad.Trans.Except as Except
#endif
import qualified Control.Monad.Trans.Identity as Identity
import qualified Control.Monad.Trans.Maybe as Maybe
import qualified Control.Monad.Trans.Reader as Reader
import qualified Control.Monad.Trans.RWS.Lazy as RWS.Lazy
import qualified Control.Monad.Trans.RWS.Strict as RWS.Strict
import qualified Control.Monad.Trans.State.Lazy as State.Lazy
import qualified Control.Monad.Trans.State.Strict as State.Strict
import qualified Control.Monad.Trans.Writer.Lazy as Writer.Lazy
import qualified Control.Monad.Trans.Writer.Strict as Writer.Strict
newtype Managed a = Managed { (>>-) :: forall r . (a -> IO r) -> IO r }
instance Functor Managed where
fmap f mx = Managed (\return_ ->
mx >>- \x ->
return_ (f x) )
instance Applicative Managed where
pure r = Managed (\return_ ->
return_ r )
mf <*> mx = Managed (\return_ ->
mf >>- \f ->
mx >>- \x ->
return_ (f x) )
instance Monad Managed where
return r = Managed (\return_ ->
return_ r )
ma >>= f = Managed (\return_ ->
ma >>- \a ->
f a >>- \b ->
return_ b )
instance MonadIO Managed where
liftIO m = Managed (\return_ -> do
a <- m
return_ a )
instance Monoid a => Monoid (Managed a) where
mempty = pure mempty
mappend = liftA2 mappend
instance Num a => Num (Managed a) where
fromInteger = pure . fromInteger
negate = fmap negate
abs = fmap abs
signum = fmap signum
(+) = liftA2 (+)
(*) = liftA2 (*)
() = liftA2 ()
instance Fractional a => Fractional (Managed a) where
fromRational = pure . fromRational
recip = fmap recip
(/) = liftA2 (/)
instance Floating a => Floating (Managed a) where
pi = pure pi
exp = fmap exp
sqrt = fmap sqrt
log = fmap log
sin = fmap sin
tan = fmap tan
cos = fmap cos
asin = fmap sin
atan = fmap atan
acos = fmap acos
sinh = fmap sinh
tanh = fmap tanh
cosh = fmap cosh
asinh = fmap asinh
atanh = fmap atanh
acosh = fmap acosh
(**) = liftA2 (**)
logBase = liftA2 logBase
class MonadIO m => MonadManaged m where
using :: Managed a -> m a
instance MonadManaged Managed where
using = id
instance MonadManaged m => MonadManaged (Cont.ContT r m) where
using m = lift (using m)
#if MIN_VERSION_transformers(0,4,0)
instance MonadManaged m => MonadManaged (Except.ExceptT e m) where
using m = lift (using m)
#endif
instance MonadManaged m => MonadManaged (Identity.IdentityT m) where
using m = lift (using m)
instance MonadManaged m => MonadManaged (Maybe.MaybeT m) where
using m = lift (using m)
instance MonadManaged m => MonadManaged (Reader.ReaderT r m) where
using m = lift (using m)
instance (Monoid w, MonadManaged m) => MonadManaged (RWS.Lazy.RWST r w s m) where
using m = lift (using m)
instance (Monoid w, MonadManaged m) => MonadManaged (RWS.Strict.RWST r w s m) where
using m = lift (using m)
instance MonadManaged m => MonadManaged (State.Strict.StateT s m) where
using m = lift (using m)
instance MonadManaged m => MonadManaged (State.Lazy.StateT s m) where
using m = lift (using m)
instance (Monoid w, MonadManaged m) => MonadManaged (Writer.Strict.WriterT w m) where
using m = lift (using m)
instance (Monoid w, MonadManaged m) => MonadManaged (Writer.Lazy.WriterT w m) where
using m = lift (using m)
managed :: (forall r . (a -> IO r) -> IO r) -> Managed a
managed = Managed
managed_ :: (forall r. IO r -> IO r) -> Managed ()
managed_ f = managed $ \g -> f $ g ()
with :: Managed a -> (a -> IO r) -> IO r
with = (>>-)
runManaged :: Managed () -> IO ()
runManaged m = m >>- return