1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
|
{-# LANGUAGE CPP
, NoImplicitPrelude
, ExistentialQuantification
, FlexibleContexts #-}
#if MIN_VERSION_base(4,3,0)
{-# LANGUAGE RankNTypes #-} -- for mask
#endif
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Safe #-}
#endif
{- |
Module : Control.Exception.Lifted
Copyright : Bas van Dijk, Anders Kaseorg
License : BSD-style
Maintainer : Bas van Dijk <v.dijk.bas@gmail.com>
Stability : experimental
Portability : non-portable (extended exceptions)
This is a wrapped version of "Control.Exception" with types generalized
from 'IO' to all monads in either 'MonadBase' or 'MonadBaseControl'.
-}
module Control.Exception.Lifted
( module Control.Exception
-- * Throwing exceptions
, throwIO, ioError, throwTo
-- * Catching exceptions
-- ** The @catch@ functions
, catch, catches, Handler(..), catchJust
-- ** The @handle@ functions
, handle, handleJust
-- ** The @try@ functions
, try, tryJust
-- ** The @evaluate@ function
, evaluate
-- * Asynchronous Exceptions
-- ** Asynchronous exception control
-- |The following functions allow a thread to control delivery of
-- asynchronous exceptions during a critical region.
#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
-- * Brackets
, bracket, bracket_, bracketOnError
-- * Utilities
, finally, onException
) where
--------------------------------------------------------------------------------
-- Imports
--------------------------------------------------------------------------------
-- from base:
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
-- from transformers-base:
import Control.Monad.Base ( MonadBase, liftBase )
-- from monad-control:
import Control.Monad.Trans.Control ( MonadBaseControl, StM
, liftBaseWith, restoreM
, control, liftBaseOp_
)
#if defined (__HADDOCK__)
import Control.Monad.Trans.Control ( liftBaseOp )
#endif
#include "inlinable.h"
--------------------------------------------------------------------------------
-- * Throwing exceptions
--------------------------------------------------------------------------------
-- |Generalized version of 'E.throwIO'.
throwIO :: (MonadBase IO m, Exception e) => e -> m a
throwIO = liftBase . E.throwIO
{-# INLINABLE throwIO #-}
-- |Generalized version of 'E.ioError'.
ioError :: MonadBase IO m => IOError -> m a
ioError = liftBase . E.ioError
{-# INLINABLE ioError #-}
-- | Generalized version of 'C.throwTo'.
throwTo :: (MonadBase IO m, Exception e) => ThreadId -> e -> m ()
throwTo tid e = liftBase $ C.throwTo tid e
{-# INLINABLE throwTo #-}
--------------------------------------------------------------------------------
-- * Catching exceptions
--------------------------------------------------------------------------------
-- |Generalized version of 'E.catch'.
--
-- Note, when the given computation throws an exception any monadic
-- side effects in @m@ will be discarded.
catch :: (MonadBaseControl IO m, Exception e)
=> m a -- ^ The computation to run
-> (e -> m a) -- ^ Handler to invoke if an exception is raised
-> m a
catch a handler = control $ \runInIO ->
E.catch (runInIO a)
(\e -> runInIO $ handler e)
{-# INLINABLE catch #-}
-- |Generalized version of 'E.catches'.
--
-- Note, when the given computation throws an exception any monadic
-- side effects in @m@ will be discarded.
catches :: MonadBaseControl IO m => m a -> [Handler m a] -> m a
catches a handlers = control $ \runInIO ->
E.catches (runInIO a)
[ E.Handler $ \e -> runInIO $ handler e
| Handler handler <- handlers
]
{-# INLINABLE catches #-}
-- |Generalized version of 'E.Handler'.
data Handler m a = forall e. Exception e => Handler (e -> m a)
-- |Generalized version of 'E.catchJust'.
--
-- Note, when the given computation throws an exception any monadic
-- side effects in @m@ will be discarded.
catchJust :: (MonadBaseControl IO m, Exception e)
=> (e -> Maybe b) -- ^ Predicate to select exceptions
-> m a -- ^ Computation to run
-> (b -> m a) -- ^ Handler
-> m a
catchJust p a handler = control $ \runInIO ->
E.catchJust p
(runInIO a)
(\e -> runInIO (handler e))
{-# INLINABLE catchJust #-}
--------------------------------------------------------------------------------
-- ** The @handle@ functions
--------------------------------------------------------------------------------
-- |Generalized version of 'E.handle'.
--
-- Note, when the given computation throws an exception any monadic
-- side effects in @m@ will be discarded.
handle :: (MonadBaseControl IO m, Exception e) => (e -> m a) -> m a -> m a
handle handler a = control $ \runInIO ->
E.handle (\e -> runInIO (handler e))
(runInIO a)
{-# INLINABLE handle #-}
-- |Generalized version of 'E.handleJust'.
--
-- Note, when the given computation throws an exception any monadic
-- side effects in @m@ will be discarded.
handleJust :: (MonadBaseControl IO m, Exception e)
=> (e -> Maybe b) -> (b -> m a) -> m a -> m a
handleJust p handler a = control $ \runInIO ->
E.handleJust p (\e -> runInIO (handler e))
(runInIO a)
{-# INLINABLE handleJust #-}
--------------------------------------------------------------------------------
-- ** The @try@ functions
--------------------------------------------------------------------------------
sequenceEither :: MonadBaseControl IO m => Either e (StM m a) -> m (Either e a)
sequenceEither = either (return . Left) (liftM Right . restoreM)
{-# INLINE sequenceEither #-}
-- |Generalized version of 'E.try'.
--
-- Note, when the given computation throws an exception any monadic
-- side effects in @m@ will be discarded.
try :: (MonadBaseControl IO m, Exception e) => m a -> m (Either e a)
try m = liftBaseWith (\runInIO -> E.try (runInIO m)) >>= sequenceEither
{-# INLINABLE try #-}
-- |Generalized version of 'E.tryJust'.
--
-- Note, when the given computation throws an exception any monadic
-- side effects in @m@ will be discarded.
tryJust :: (MonadBaseControl IO m, Exception e) => (e -> Maybe b) -> m a -> m (Either b a)
tryJust p m = liftBaseWith (\runInIO -> E.tryJust p (runInIO m)) >>= sequenceEither
{-# INLINABLE tryJust #-}
--------------------------------------------------------------------------------
-- ** The @evaluate@ function
--------------------------------------------------------------------------------
-- |Generalized version of 'E.evaluate'.
evaluate :: MonadBase IO m => a -> m a
evaluate = liftBase . E.evaluate
{-# INLINABLE evaluate #-}
--------------------------------------------------------------------------------
-- ** Asynchronous exception control
--------------------------------------------------------------------------------
#if MIN_VERSION_base(4,3,0)
-- |Generalized version of 'E.mask'.
mask :: MonadBaseControl IO m => ((forall a. m a -> m a) -> m b) -> m b
mask f = control $ \runInBase ->
E.mask $ \g -> runInBase $ f $ liftBaseOp_ g
{-# INLINABLE mask #-}
-- |Generalized version of 'E.mask_'.
mask_ :: MonadBaseControl IO m => m a -> m a
mask_ = liftBaseOp_ E.mask_
{-# INLINABLE mask_ #-}
-- |Generalized version of 'E.uninterruptibleMask'.
uninterruptibleMask
:: MonadBaseControl IO m => ((forall a. m a -> m a) -> m b) -> m b
uninterruptibleMask f =
control $ \runInBase ->
E.uninterruptibleMask $ \g -> runInBase $ f $ liftBaseOp_ g
{-# INLINABLE uninterruptibleMask #-}
-- |Generalized version of 'E.uninterruptibleMask_'.
uninterruptibleMask_ :: MonadBaseControl IO m => m a -> m a
uninterruptibleMask_ = liftBaseOp_ E.uninterruptibleMask_
{-# INLINABLE uninterruptibleMask_ #-}
-- |Generalized version of 'E.getMaskingState'.
getMaskingState :: MonadBase IO m => m MaskingState
getMaskingState = liftBase E.getMaskingState
{-# INLINABLE getMaskingState #-}
#if MIN_VERSION_base(4,4,0)
-- |Generalized version of 'E.allowInterrupt'.
allowInterrupt :: MonadBase IO m => m ()
allowInterrupt = liftBase E.allowInterrupt
{-# INLINABLE allowInterrupt #-}
#endif
#else
-- |Generalized version of 'E.block'.
block :: MonadBaseControl IO m => m a -> m a
block = liftBaseOp_ E.block
{-# INLINABLE block #-}
-- |Generalized version of 'E.unblock'.
unblock :: MonadBaseControl IO m => m a -> m a
unblock = liftBaseOp_ E.unblock
{-# INLINABLE unblock #-}
#endif
#if !MIN_VERSION_base(4,4,0)
-- | Generalized version of 'E.blocked'.
-- returns @True@ if asynchronous exceptions are blocked in the
-- current thread.
blocked :: MonadBase IO m => m Bool
blocked = liftBase E.blocked
{-# INLINABLE blocked #-}
#endif
--------------------------------------------------------------------------------
-- * Brackets
--------------------------------------------------------------------------------
-- |Generalized version of 'E.bracket'.
--
-- Note:
--
-- * When the \"acquire\" or \"release\" computations throw exceptions
-- any monadic side effects in @m@ will be discarded.
--
-- * When the \"in-between\" computation throws an exception any
-- monadic side effects in @m@ produced by that computation will be
-- discarded but the side effects of the \"acquire\" or \"release\"
-- computations will be retained.
--
-- * Also, any monadic side effects in @m@ of the \"release\"
-- computation will be discarded; it is run only for its side
-- effects in @IO@.
--
-- Note that when your @acquire@ and @release@ computations are of type 'IO'
-- it will be more efficient to write:
--
-- @'liftBaseOp' ('E.bracket' acquire release)@
bracket :: MonadBaseControl IO m
=> m a -- ^ computation to run first (\"acquire resource\")
-> (a -> m b) -- ^ computation to run last (\"release resource\")
-> (a -> m c) -- ^ computation to run in-between
-> m c
bracket before after thing = control $ \runInIO ->
E.bracket (runInIO before)
(\st -> runInIO $ restoreM st >>= after)
(\st -> runInIO $ restoreM st >>= thing)
{-# INLINABLE bracket #-}
-- |Generalized version of 'E.bracket_'.
--
-- Note any monadic side effects in @m@ of /both/ the \"acquire\" and
-- \"release\" computations will be discarded. To keep the monadic
-- side effects of the \"acquire\" computation, use 'bracket' with
-- constant functions instead.
--
-- Note that when your @acquire@ and @release@ computations are of type 'IO'
-- it will be more efficient to write:
--
-- @'liftBaseOp_' ('E.bracket_' acquire release)@
bracket_ :: MonadBaseControl IO m
=> m a -- ^ computation to run first (\"acquire resource\")
-> m b -- ^ computation to run last (\"release resource\")
-> m c -- ^ computation to run in-between
-> m c
bracket_ before after thing = control $ \runInIO ->
E.bracket_ (runInIO before)
(runInIO after)
(runInIO thing)
{-# INLINABLE bracket_ #-}
-- |Generalized version of 'E.bracketOnError'.
--
-- Note:
--
-- * When the \"acquire\" or \"release\" computations throw exceptions
-- any monadic side effects in @m@ will be discarded.
--
-- * When the \"in-between\" computation throws an exception any
-- monadic side effects in @m@ produced by that computation will be
-- discarded but the side effects of the \"acquire\" computation
-- will be retained.
--
-- * Also, any monadic side effects in @m@ of the \"release\"
-- computation will be discarded; it is run only for its side
-- effects in @IO@.
--
-- Note that when your @acquire@ and @release@ computations are of
-- type 'IO' it will be more efficient to write:
--
-- @'liftBaseOp' ('E.bracketOnError' acquire release)@
bracketOnError :: MonadBaseControl IO m
=> m a -- ^ computation to run first (\"acquire resource\")
-> (a -> m b) -- ^ computation to run last (\"release resource\")
-> (a -> m c) -- ^ computation to run in-between
-> m c
bracketOnError before after thing =
control $ \runInIO ->
E.bracketOnError (runInIO before)
(\st -> runInIO $ restoreM st >>= after)
(\st -> runInIO $ restoreM st >>= thing)
{-# INLINABLE bracketOnError #-}
--------------------------------------------------------------------------------
-- * Utilities
--------------------------------------------------------------------------------
-- |Generalized version of 'E.finally'.
--
-- Note, any monadic side effects in @m@ of the \"afterward\"
-- computation will be discarded.
finally :: MonadBaseControl IO m
=> m a -- ^ computation to run first
-> m b -- ^ computation to run afterward (even if an exception was raised)
-> m a
finally a sequel = control $ \runInIO ->
E.finally (runInIO a)
(runInIO sequel)
{-# INLINABLE finally #-}
-- |Generalized version of 'E.onException'.
--
-- Note, any monadic side effects in @m@ of the \"afterward\"
-- computation will be discarded.
onException :: MonadBaseControl IO m => m a -> m b -> m a
onException m what = control $ \runInIO ->
E.onException (runInIO m)
(runInIO what)
{-# INLINABLE onException #-}
|