Fix warnings
[ghc-hetmet.git] / compiler / utils / Exception.hs
1 {-# OPTIONS_GHC -fno-warn-deprecations #-}
2 module Exception
3     (
4     module Control.Exception,
5     module Exception
6     )
7     where
8
9 import Prelude hiding (catch)
10
11 import Control.Exception
12
13 catchIO :: IO a -> (IOException -> IO a) -> IO a
14 catchIO = catch
15
16 handleIO :: (IOException -> IO a) -> IO a -> IO a
17 handleIO = flip catchIO
18
19 tryIO :: IO a -> IO (Either IOException a)
20 tryIO = try
21
22 -- | A monad that can catch exceptions.  A minimal definition
23 -- requires a definition of 'gcatch'.
24 --
25 -- Implementations on top of 'IO' should implement 'gblock' and 'gunblock' to
26 -- eventually call the primitives 'Control.Exception.block' and
27 -- 'Control.Exception.unblock' respectively.  These are used for
28 -- implementations that support asynchronous exceptions.  The default
29 -- implementations of 'gbracket' and 'gfinally' use 'gblock' and 'gunblock'
30 -- thus rarely require overriding.
31 --
32 class Monad m => ExceptionMonad m where
33
34   -- | Generalised version of 'Control.Exception.catch', allowing an arbitrary
35   -- exception handling monad instead of just 'IO'.
36   gcatch :: Exception e => m a -> (e -> m a) -> m a
37
38   -- | Generalised version of 'Control.Exception.mask_', allowing an arbitrary
39   -- exception handling monad instead of just 'IO'.
40   gmask :: ((m a -> m a) -> m b) -> m b
41
42   -- | Generalised version of 'Control.Exception.bracket', allowing an arbitrary
43   -- exception handling monad instead of just 'IO'.
44   gbracket :: m a -> (a -> m b) -> (a -> m c) -> m c
45
46   -- | Generalised version of 'Control.Exception.finally', allowing an arbitrary
47   -- exception handling monad instead of just 'IO'.
48   gfinally :: m a -> m b -> m a
49
50   -- | DEPRECATED, here for backwards compatibilty.  Instances can
51   -- define either 'gmask', or both 'block' and 'unblock'.
52   gblock   :: m a -> m a
53   -- | DEPRECATED, here for backwards compatibilty  Instances can
54   -- define either 'gmask', or both 'block' and 'unblock'.
55   gunblock :: m a -> m a
56   -- XXX we're keeping these two methods for the time being because we
57   -- have to interact with Haskeline's MonadException class which
58   -- still has block/unblock; see GhciMonad.hs.
59
60   gmask    f = gblock (f gunblock)
61   gblock   f = gmask (\_ -> f)
62   gunblock f = f -- XXX wrong; better override this if you need it
63
64   gbracket before after thing =
65     gmask $ \restore -> do
66       a <- before
67       r <- restore (thing a) `gonException` after a
68       _ <- after a
69       return r
70
71   a `gfinally` sequel =
72     gmask $ \restore -> do
73       r <- restore a `gonException` sequel
74       _ <- sequel
75       return r
76
77 #if __GLASGOW_HASKELL__ < 613
78 instance ExceptionMonad IO where
79   gcatch    = catch
80   gmask f   = block $ f unblock
81   gblock    = block
82   gunblock  = unblock
83 #else
84 instance ExceptionMonad IO where
85   gcatch    = catch
86   gmask f   = mask (\x -> f x)
87   gblock    = block
88   gunblock  = unblock
89 #endif
90
91 gtry :: (ExceptionMonad m, Exception e) => m a -> m (Either e a)
92 gtry act = gcatch (act >>= \a -> return (Right a))
93                   (\e -> return (Left e))
94
95 -- | Generalised version of 'Control.Exception.handle', allowing an arbitrary
96 -- exception handling monad instead of just 'IO'.
97 ghandle :: (ExceptionMonad m, Exception e) => (e -> m a) -> m a -> m a
98 ghandle = flip gcatch
99
100 -- | Always executes the first argument.  If this throws an exception the
101 -- second argument is executed and the exception is raised again.
102 gonException :: (ExceptionMonad m) => m a -> m b -> m a
103 gonException ioA cleanup = ioA `gcatch` \e ->
104                              do _ <- cleanup
105                                 throw (e :: SomeException)
106