{-# LANGUAGE CPP
, NoImplicitPrelude
, ExistentialQuantification
, FlexibleContexts #-}
#if MIN_VERSION_base(4,3,0)
{-# LANGUAGE RankNTypes #-}
#endif
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Safe #-}
#endif
module Control.Exception.Lifted
( module Control.Exception
, throwIO, ioError, throwTo
, catch, catches, Handler(..), catchJust
, handle, handleJust
, try, tryJust
, evaluate
#if MIN_VERSION_base(4,3,0)
, mask, mask_
, uninterruptibleMask, uninterruptibleMask_
, getMaskingState
#if MIN_VERSION_base(4,4,0)
, allowInterrupt
#endif
#else
, block, unblock
#endif
#if !MIN_VERSION_base(4,4,0)
, blocked
#endif
, bracket, bracket_, bracketOnError
, finally, onException
) where
import Prelude ( (.) )
import Data.Function ( ($) )
import Data.Either ( Either(Left, Right), either )
import Data.Maybe ( Maybe )
import Control.Monad ( (>>=), return, liftM )
import System.IO.Error ( IOError )
import System.IO ( IO )
#if __GLASGOW_HASKELL__ < 700
import Control.Monad ( fail )
#endif
import Control.Exception hiding
( throwIO, ioError, throwTo
, catch, catches, Handler(..), catchJust
, handle, handleJust
, try, tryJust
, evaluate
#if MIN_VERSION_base(4,3,0)
, mask, mask_
, uninterruptibleMask, uninterruptibleMask_
, getMaskingState
#if MIN_VERSION_base(4,4,0)
, allowInterrupt
#endif
#else
, block, unblock
#endif
#if !MIN_VERSION_base(4,4,0)
, blocked
#endif
, bracket, bracket_, bracketOnError
, finally, onException
)
import qualified Control.Exception as E
import qualified Control.Concurrent as C
import Control.Concurrent ( ThreadId )
#if !MIN_VERSION_base(4,4,0)
import Data.Bool ( Bool )
#endif
import Control.Monad.Base ( MonadBase, liftBase )
import Control.Monad.Trans.Control ( MonadBaseControl, StM
, liftBaseWith, restoreM
, control, liftBaseOp_
)
#if defined (__HADDOCK__)
import Control.Monad.Trans.Control ( liftBaseOp )
#endif
#include "inlinable.h"
throwIO :: (MonadBase IO m, Exception e) => e -> m a
throwIO :: forall (m :: * -> *) e a. (MonadBase IO m, Exception e) => e -> m a
throwIO = IO a -> m a
forall α. IO α -> m α
forall (b :: * -> *) (m :: * -> *) α. MonadBase b m => b α -> m α
liftBase (IO a -> m a) -> (e -> IO a) -> e -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> IO a
forall e a. Exception e => e -> IO a
E.throwIO
{-# INLINABLE throwIO #-}
ioError :: MonadBase IO m => IOError -> m a
ioError :: forall (m :: * -> *) a. MonadBase IO m => IOError -> m a
ioError = IO a -> m a
forall α. IO α -> m α
forall (b :: * -> *) (m :: * -> *) α. MonadBase b m => b α -> m α
liftBase (IO a -> m a) -> (IOError -> IO a) -> IOError -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IOError -> IO a
forall a. IOError -> IO a
E.ioError
{-# INLINABLE ioError #-}
throwTo :: (MonadBase IO m, Exception e) => ThreadId -> e -> m ()
throwTo :: forall (m :: * -> *) e.
(MonadBase IO m, Exception e) =>
ThreadId -> e -> m ()
throwTo ThreadId
tid e
e = IO () -> m ()
forall α. IO α -> m α
forall (b :: * -> *) (m :: * -> *) α. MonadBase b m => b α -> m α
liftBase (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ ThreadId -> e -> IO ()
forall e. Exception e => ThreadId -> e -> IO ()
C.throwTo ThreadId
tid e
e
{-# INLINABLE throwTo #-}
catch :: (MonadBaseControl IO m, Exception e)
=> m a
-> (e -> m a)
-> m a
catch :: forall (m :: * -> *) e a.
(MonadBaseControl IO m, Exception e) =>
m a -> (e -> m a) -> m a
catch m a
a e -> m a
handler = (RunInBase m IO -> IO (StM m a)) -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b (StM m a)) -> m a
control ((RunInBase m IO -> IO (StM m a)) -> m a)
-> (RunInBase m IO -> IO (StM m a)) -> m a
forall a b. (a -> b) -> a -> b
$ \RunInBase m IO
runInIO ->
IO (StM m a) -> (e -> IO (StM m a)) -> IO (StM m a)
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
E.catch (m a -> IO (StM m a)
RunInBase m IO
runInIO m a
a)
(\e
e -> m a -> IO (StM m a)
RunInBase m IO
runInIO (m a -> IO (StM m a)) -> m a -> IO (StM m a)
forall a b. (a -> b) -> a -> b
$ e -> m a
handler e
e)
{-# INLINABLE catch #-}
catches :: MonadBaseControl IO m => m a -> [Handler m a] -> m a
catches :: forall (m :: * -> *) a.
MonadBaseControl IO m =>
m a -> [Handler m a] -> m a
catches m a
a [Handler m a]
handlers = (RunInBase m IO -> IO (StM m a)) -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b (StM m a)) -> m a
control ((RunInBase m IO -> IO (StM m a)) -> m a)
-> (RunInBase m IO -> IO (StM m a)) -> m a
forall a b. (a -> b) -> a -> b
$ \RunInBase m IO
runInIO ->
IO (StM m a) -> [Handler (StM m a)] -> IO (StM m a)
forall a. IO a -> [Handler a] -> IO a
E.catches (m a -> IO (StM m a)
RunInBase m IO
runInIO m a
a)
[ (e -> IO (StM m a)) -> Handler (StM m a)
forall a e. Exception e => (e -> IO a) -> Handler a
E.Handler ((e -> IO (StM m a)) -> Handler (StM m a))
-> (e -> IO (StM m a)) -> Handler (StM m a)
forall a b. (a -> b) -> a -> b
$ \e
e -> m a -> IO (StM m a)
RunInBase m IO
runInIO (m a -> IO (StM m a)) -> m a -> IO (StM m a)
forall a b. (a -> b) -> a -> b
$ e -> m a
handler e
e
| Handler e -> m a
handler <- [Handler m a]
handlers
]
{-# INLINABLE catches #-}
data Handler m a = forall e. Exception e => Handler (e -> m a)
catchJust :: (MonadBaseControl IO m, Exception e)
=> (e -> Maybe b)
-> m a
-> (b -> m a)
-> m a
catchJust :: forall (m :: * -> *) e b a.
(MonadBaseControl IO m, Exception e) =>
(e -> Maybe b) -> m a -> (b -> m a) -> m a
catchJust e -> Maybe b
p m a
a b -> m a
handler = (RunInBase m IO -> IO (StM m a)) -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b (StM m a)) -> m a
control ((RunInBase m IO -> IO (StM m a)) -> m a)
-> (RunInBase m IO -> IO (StM m a)) -> m a
forall a b. (a -> b) -> a -> b
$ \RunInBase m IO
runInIO ->
(e -> Maybe b)
-> IO (StM m a) -> (b -> IO (StM m a)) -> IO (StM m a)
forall e b a.
Exception e =>
(e -> Maybe b) -> IO a -> (b -> IO a) -> IO a
E.catchJust e -> Maybe b
p
(m a -> IO (StM m a)
RunInBase m IO
runInIO m a
a)
(\b
e -> m a -> IO (StM m a)
RunInBase m IO
runInIO (b -> m a
handler b
e))
{-# INLINABLE catchJust #-}
handle :: (MonadBaseControl IO m, Exception e) => (e -> m a) -> m a -> m a
handle :: forall (m :: * -> *) e a.
(MonadBaseControl IO m, Exception e) =>
(e -> m a) -> m a -> m a
handle e -> m a
handler m a
a = (RunInBase m IO -> IO (StM m a)) -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b (StM m a)) -> m a
control ((RunInBase m IO -> IO (StM m a)) -> m a)
-> (RunInBase m IO -> IO (StM m a)) -> m a
forall a b. (a -> b) -> a -> b
$ \RunInBase m IO
runInIO ->
(e -> IO (StM m a)) -> IO (StM m a) -> IO (StM m a)
forall e a. Exception e => (e -> IO a) -> IO a -> IO a
E.handle (\e
e -> m a -> IO (StM m a)
RunInBase m IO
runInIO (e -> m a
handler e
e))
(m a -> IO (StM m a)
RunInBase m IO
runInIO m a
a)
{-# INLINABLE handle #-}
handleJust :: (MonadBaseControl IO m, Exception e)
=> (e -> Maybe b) -> (b -> m a) -> m a -> m a
handleJust :: forall (m :: * -> *) e b a.
(MonadBaseControl IO m, Exception e) =>
(e -> Maybe b) -> (b -> m a) -> m a -> m a
handleJust e -> Maybe b
p b -> m a
handler m a
a = (RunInBase m IO -> IO (StM m a)) -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b (StM m a)) -> m a
control ((RunInBase m IO -> IO (StM m a)) -> m a)
-> (RunInBase m IO -> IO (StM m a)) -> m a
forall a b. (a -> b) -> a -> b
$ \RunInBase m IO
runInIO ->
(e -> Maybe b)
-> (b -> IO (StM m a)) -> IO (StM m a) -> IO (StM m a)
forall e b a.
Exception e =>
(e -> Maybe b) -> (b -> IO a) -> IO a -> IO a
E.handleJust e -> Maybe b
p (\b
e -> m a -> IO (StM m a)
RunInBase m IO
runInIO (b -> m a
handler b
e))
(m a -> IO (StM m a)
RunInBase m IO
runInIO m a
a)
{-# INLINABLE handleJust #-}
sequenceEither :: MonadBaseControl IO m => Either e (StM m a) -> m (Either e a)
sequenceEither :: forall (m :: * -> *) e a.
MonadBaseControl IO m =>
Either e (StM m a) -> m (Either e a)
sequenceEither = (e -> m (Either e a))
-> (StM m a -> m (Either e a))
-> Either e (StM m a)
-> m (Either e a)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either e a -> m (Either e a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either e a -> m (Either e a))
-> (e -> Either e a) -> e -> m (Either e a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> Either e a
forall a b. a -> Either a b
Left) ((a -> Either e a) -> m a -> m (Either e a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM a -> Either e a
forall a b. b -> Either a b
Right (m a -> m (Either e a))
-> (StM m a -> m a) -> StM m a -> m (Either e a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StM m a -> m a
forall a. StM m a -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
StM m a -> m a
restoreM)
{-# INLINE sequenceEither #-}
try :: (MonadBaseControl IO m, Exception e) => m a -> m (Either e a)
try :: forall (m :: * -> *) e a.
(MonadBaseControl IO m, Exception e) =>
m a -> m (Either e a)
try m a
m = (RunInBase m IO -> IO (Either e (StM m a)))
-> m (Either e (StM m a))
forall a. (RunInBase m IO -> IO a) -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b a) -> m a
liftBaseWith (\RunInBase m IO
runInIO -> IO (StM m a) -> IO (Either e (StM m a))
forall e a. Exception e => IO a -> IO (Either e a)
E.try (m a -> IO (StM m a)
RunInBase m IO
runInIO m a
m)) m (Either e (StM m a))
-> (Either e (StM m a) -> m (Either e a)) -> m (Either e a)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either e (StM m a) -> m (Either e a)
forall (m :: * -> *) e a.
MonadBaseControl IO m =>
Either e (StM m a) -> m (Either e a)
sequenceEither
{-# INLINABLE try #-}
tryJust :: (MonadBaseControl IO m, Exception e) => (e -> Maybe b) -> m a -> m (Either b a)
tryJust :: forall (m :: * -> *) e b a.
(MonadBaseControl IO m, Exception e) =>
(e -> Maybe b) -> m a -> m (Either b a)
tryJust e -> Maybe b
p m a
m = (RunInBase m IO -> IO (Either b (StM m a)))
-> m (Either b (StM m a))
forall a. (RunInBase m IO -> IO a) -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b a) -> m a
liftBaseWith (\RunInBase m IO
runInIO -> (e -> Maybe b) -> IO (StM m a) -> IO (Either b (StM m a))
forall e b a.
Exception e =>
(e -> Maybe b) -> IO a -> IO (Either b a)
E.tryJust e -> Maybe b
p (m a -> IO (StM m a)
RunInBase m IO
runInIO m a
m)) m (Either b (StM m a))
-> (Either b (StM m a) -> m (Either b a)) -> m (Either b a)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either b (StM m a) -> m (Either b a)
forall (m :: * -> *) e a.
MonadBaseControl IO m =>
Either e (StM m a) -> m (Either e a)
sequenceEither
{-# INLINABLE tryJust #-}
evaluate :: MonadBase IO m => a -> m a
evaluate :: forall (m :: * -> *) a. MonadBase IO m => a -> m a
evaluate = IO a -> m a
forall α. IO α -> m α
forall (b :: * -> *) (m :: * -> *) α. MonadBase b m => b α -> m α
liftBase (IO a -> m a) -> (a -> IO a) -> a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> IO a
forall a. a -> IO a
E.evaluate
{-# INLINABLE evaluate #-}
#if MIN_VERSION_base(4,3,0)
mask :: MonadBaseControl IO m => ((forall a. m a -> m a) -> m b) -> m b
mask :: forall (m :: * -> *) b.
MonadBaseControl IO m =>
((forall a. m a -> m a) -> m b) -> m b
mask (forall a. m a -> m a) -> m b
f = (RunInBase m IO -> IO (StM m b)) -> m b
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b (StM m a)) -> m a
control ((RunInBase m IO -> IO (StM m b)) -> m b)
-> (RunInBase m IO -> IO (StM m b)) -> m b
forall a b. (a -> b) -> a -> b
$ \RunInBase m IO
runInBase ->
((forall a. IO a -> IO a) -> IO (StM m b)) -> IO (StM m b)
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
E.mask (((forall a. IO a -> IO a) -> IO (StM m b)) -> IO (StM m b))
-> ((forall a. IO a -> IO a) -> IO (StM m b)) -> IO (StM m b)
forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
g -> m b -> IO (StM m b)
RunInBase m IO
runInBase (m b -> IO (StM m b)) -> m b -> IO (StM m b)
forall a b. (a -> b) -> a -> b
$ (forall a. m a -> m a) -> m b
f ((forall a. m a -> m a) -> m b) -> (forall a. m a -> m a) -> m b
forall a b. (a -> b) -> a -> b
$ (IO (StM m a) -> IO (StM m a)) -> m a -> m a
forall (b :: * -> *) (m :: * -> *) a c.
MonadBaseControl b m =>
(b (StM m a) -> b (StM m c)) -> m a -> m c
liftBaseOp_ IO (StM m a) -> IO (StM m a)
forall a. IO a -> IO a
g
{-# INLINABLE mask #-}
mask_ :: MonadBaseControl IO m => m a -> m a
mask_ :: forall (m :: * -> *) a. MonadBaseControl IO m => m a -> m a
mask_ = (IO (StM m a) -> IO (StM m a)) -> m a -> m a
forall (b :: * -> *) (m :: * -> *) a c.
MonadBaseControl b m =>
(b (StM m a) -> b (StM m c)) -> m a -> m c
liftBaseOp_ IO (StM m a) -> IO (StM m a)
forall a. IO a -> IO a
E.mask_
{-# INLINABLE mask_ #-}
uninterruptibleMask
:: MonadBaseControl IO m => ((forall a. m a -> m a) -> m b) -> m b
uninterruptibleMask :: forall (m :: * -> *) b.
MonadBaseControl IO m =>
((forall a. m a -> m a) -> m b) -> m b
uninterruptibleMask (forall a. m a -> m a) -> m b
f =
(RunInBase m IO -> IO (StM m b)) -> m b
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b (StM m a)) -> m a
control ((RunInBase m IO -> IO (StM m b)) -> m b)
-> (RunInBase m IO -> IO (StM m b)) -> m b
forall a b. (a -> b) -> a -> b
$ \RunInBase m IO
runInBase ->
((forall a. IO a -> IO a) -> IO (StM m b)) -> IO (StM m b)
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
E.uninterruptibleMask (((forall a. IO a -> IO a) -> IO (StM m b)) -> IO (StM m b))
-> ((forall a. IO a -> IO a) -> IO (StM m b)) -> IO (StM m b)
forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
g -> m b -> IO (StM m b)
RunInBase m IO
runInBase (m b -> IO (StM m b)) -> m b -> IO (StM m b)
forall a b. (a -> b) -> a -> b
$ (forall a. m a -> m a) -> m b
f ((forall a. m a -> m a) -> m b) -> (forall a. m a -> m a) -> m b
forall a b. (a -> b) -> a -> b
$ (IO (StM m a) -> IO (StM m a)) -> m a -> m a
forall (b :: * -> *) (m :: * -> *) a c.
MonadBaseControl b m =>
(b (StM m a) -> b (StM m c)) -> m a -> m c
liftBaseOp_ IO (StM m a) -> IO (StM m a)
forall a. IO a -> IO a
g
{-# INLINABLE uninterruptibleMask #-}
uninterruptibleMask_ :: MonadBaseControl IO m => m a -> m a
uninterruptibleMask_ :: forall (m :: * -> *) a. MonadBaseControl IO m => m a -> m a
uninterruptibleMask_ = (IO (StM m a) -> IO (StM m a)) -> m a -> m a
forall (b :: * -> *) (m :: * -> *) a c.
MonadBaseControl b m =>
(b (StM m a) -> b (StM m c)) -> m a -> m c
liftBaseOp_ IO (StM m a) -> IO (StM m a)
forall a. IO a -> IO a
E.uninterruptibleMask_
{-# INLINABLE uninterruptibleMask_ #-}
getMaskingState :: MonadBase IO m => m MaskingState
getMaskingState :: forall (m :: * -> *). MonadBase IO m => m MaskingState
getMaskingState = IO MaskingState -> m MaskingState
forall α. IO α -> m α
forall (b :: * -> *) (m :: * -> *) α. MonadBase b m => b α -> m α
liftBase IO MaskingState
E.getMaskingState
{-# INLINABLE getMaskingState #-}
#if MIN_VERSION_base(4,4,0)
allowInterrupt :: MonadBase IO m => m ()
allowInterrupt :: forall (m :: * -> *). MonadBase IO m => m ()
allowInterrupt = IO () -> m ()
forall α. IO α -> m α
forall (b :: * -> *) (m :: * -> *) α. MonadBase b m => b α -> m α
liftBase IO ()
E.allowInterrupt
{-# INLINABLE allowInterrupt #-}
#endif
#else
block :: MonadBaseControl IO m => m a -> m a
block = liftBaseOp_ E.block
{-# INLINABLE block #-}
unblock :: MonadBaseControl IO m => m a -> m a
unblock = liftBaseOp_ E.unblock
{-# INLINABLE unblock #-}
#endif
#if !MIN_VERSION_base(4,4,0)
blocked :: MonadBase IO m => m Bool
blocked = liftBase E.blocked
{-# INLINABLE blocked #-}
#endif
bracket :: MonadBaseControl IO m
=> m a
-> (a -> m b)
-> (a -> m c)
-> m c
bracket :: forall (m :: * -> *) a b c.
MonadBaseControl IO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket m a
before a -> m b
after a -> m c
thing = (RunInBase m IO -> IO (StM m c)) -> m c
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b (StM m a)) -> m a
control ((RunInBase m IO -> IO (StM m c)) -> m c)
-> (RunInBase m IO -> IO (StM m c)) -> m c
forall a b. (a -> b) -> a -> b
$ \RunInBase m IO
runInIO ->
IO (StM m a)
-> (StM m a -> IO (StM m b))
-> (StM m a -> IO (StM m c))
-> IO (StM m c)
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
E.bracket (m a -> IO (StM m a)
RunInBase m IO
runInIO m a
before)
(\StM m a
st -> m b -> IO (StM m b)
RunInBase m IO
runInIO (m b -> IO (StM m b)) -> m b -> IO (StM m b)
forall a b. (a -> b) -> a -> b
$ StM m a -> m a
forall a. StM m a -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
StM m a -> m a
restoreM StM m a
st m a -> (a -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m b
after)
(\StM m a
st -> m c -> IO (StM m c)
RunInBase m IO
runInIO (m c -> IO (StM m c)) -> m c -> IO (StM m c)
forall a b. (a -> b) -> a -> b
$ StM m a -> m a
forall a. StM m a -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
StM m a -> m a
restoreM StM m a
st m a -> (a -> m c) -> m c
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m c
thing)
{-# INLINABLE bracket #-}
bracket_ :: MonadBaseControl IO m
=> m a
-> m b
-> m c
-> m c
bracket_ :: forall (m :: * -> *) a b c.
MonadBaseControl IO m =>
m a -> m b -> m c -> m c
bracket_ m a
before m b
after m c
thing = (RunInBase m IO -> IO (StM m c)) -> m c
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b (StM m a)) -> m a
control ((RunInBase m IO -> IO (StM m c)) -> m c)
-> (RunInBase m IO -> IO (StM m c)) -> m c
forall a b. (a -> b) -> a -> b
$ \RunInBase m IO
runInIO ->
IO (StM m a) -> IO (StM m b) -> IO (StM m c) -> IO (StM m c)
forall a b c. IO a -> IO b -> IO c -> IO c
E.bracket_ (m a -> IO (StM m a)
RunInBase m IO
runInIO m a
before)
(m b -> IO (StM m b)
RunInBase m IO
runInIO m b
after)
(m c -> IO (StM m c)
RunInBase m IO
runInIO m c
thing)
{-# INLINABLE bracket_ #-}
bracketOnError :: MonadBaseControl IO m
=> m a
-> (a -> m b)
-> (a -> m c)
-> m c
bracketOnError :: forall (m :: * -> *) a b c.
MonadBaseControl IO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracketOnError m a
before a -> m b
after a -> m c
thing =
(RunInBase m IO -> IO (StM m c)) -> m c
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b (StM m a)) -> m a
control ((RunInBase m IO -> IO (StM m c)) -> m c)
-> (RunInBase m IO -> IO (StM m c)) -> m c
forall a b. (a -> b) -> a -> b
$ \RunInBase m IO
runInIO ->
IO (StM m a)
-> (StM m a -> IO (StM m b))
-> (StM m a -> IO (StM m c))
-> IO (StM m c)
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
E.bracketOnError (m a -> IO (StM m a)
RunInBase m IO
runInIO m a
before)
(\StM m a
st -> m b -> IO (StM m b)
RunInBase m IO
runInIO (m b -> IO (StM m b)) -> m b -> IO (StM m b)
forall a b. (a -> b) -> a -> b
$ StM m a -> m a
forall a. StM m a -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
StM m a -> m a
restoreM StM m a
st m a -> (a -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m b
after)
(\StM m a
st -> m c -> IO (StM m c)
RunInBase m IO
runInIO (m c -> IO (StM m c)) -> m c -> IO (StM m c)
forall a b. (a -> b) -> a -> b
$ StM m a -> m a
forall a. StM m a -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
StM m a -> m a
restoreM StM m a
st m a -> (a -> m c) -> m c
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m c
thing)
{-# INLINABLE bracketOnError #-}
finally :: MonadBaseControl IO m
=> m a
-> m b
-> m a
finally :: forall (m :: * -> *) a b.
MonadBaseControl IO m =>
m a -> m b -> m a
finally m a
a m b
sequel = (RunInBase m IO -> IO (StM m a)) -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b (StM m a)) -> m a
control ((RunInBase m IO -> IO (StM m a)) -> m a)
-> (RunInBase m IO -> IO (StM m a)) -> m a
forall a b. (a -> b) -> a -> b
$ \RunInBase m IO
runInIO ->
IO (StM m a) -> IO (StM m b) -> IO (StM m a)
forall a b. IO a -> IO b -> IO a
E.finally (m a -> IO (StM m a)
RunInBase m IO
runInIO m a
a)
(m b -> IO (StM m b)
RunInBase m IO
runInIO m b
sequel)
{-# INLINABLE finally #-}
onException :: MonadBaseControl IO m => m a -> m b -> m a
onException :: forall (m :: * -> *) a b.
MonadBaseControl IO m =>
m a -> m b -> m a
onException m a
m m b
what = (RunInBase m IO -> IO (StM m a)) -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
(RunInBase m b -> b (StM m a)) -> m a
control ((RunInBase m IO -> IO (StM m a)) -> m a)
-> (RunInBase m IO -> IO (StM m a)) -> m a
forall a b. (a -> b) -> a -> b
$ \RunInBase m IO
runInIO ->
IO (StM m a) -> IO (StM m b) -> IO (StM m a)
forall a b. IO a -> IO b -> IO a
E.onException (m a -> IO (StM m a)
RunInBase m IO
runInIO m a
m)
(m b -> IO (StM m b)
RunInBase m IO
runInIO m b
what)
{-# INLINABLE onException #-}