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