d3a88e8d6cc900efe3cfb75491a6cf6e232ea070
[ghc-base.git] / Control / Exception.hs
1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  Control.Exception
5 -- Copyright   :  (c) The University of Glasgow 2001
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  experimental
10 -- Portability :  non-portable (extended exceptions)
11 --
12 -- This module provides support for raising and catching both built-in
13 -- and user-defined exceptions.
14 --
15 -- In addition to exceptions thrown by 'IO' operations, exceptions may
16 -- be thrown by pure code (imprecise exceptions) or by external events
17 -- (asynchronous exceptions), but may only be caught in the 'IO' monad.
18 -- For more details, see:
19 --
20 --  * /A semantics for imprecise exceptions/, by Simon Peyton Jones,
21 --    Alastair Reid, Tony Hoare, Simon Marlow, Fergus Henderson,
22 --    in /PLDI'99/.
23 --
24 --  * /Asynchronous exceptions in Haskell/, by Simon Marlow, Simon Peyton
25 --    Jones, Andy Moran and John Reppy, in /PLDI'01/.
26 --
27 -----------------------------------------------------------------------------
28
29 module Control.Exception (
30
31         -- * The Exception type
32         SomeException(..),
33         Exception(..),          -- instance Eq, Ord, Show, Typeable
34         IOException,            -- instance Eq, Ord, Show, Typeable
35         ArithException(..),     -- instance Eq, Ord, Show, Typeable
36         ArrayException(..),     -- instance Eq, Ord, Show, Typeable
37         AssertionFailed(..),
38         AsyncException(..),     -- instance Eq, Ord, Show, Typeable
39         NonTermination(..), nonTermination,
40         BlockedOnDeadMVar(..),
41         BlockedIndefinitely(..),
42         NestedAtomically(..), nestedAtomically,
43         Deadlock(..),
44         NoMethodError(..),
45         PatternMatchFail(..),
46         RecConError(..),
47         RecSelError(..),
48         RecUpdError(..),
49
50         -- * Throwing exceptions
51         throwIO,        -- :: Exception -> IO a
52         throw,          -- :: Exception -> a
53         ioError,        -- :: IOError -> IO a
54 #ifdef __GLASGOW_HASKELL__
55         throwTo,        -- :: ThreadId -> Exception -> a
56 #endif
57
58         -- * Catching Exceptions
59
60         -- |There are several functions for catching and examining
61         -- exceptions; all of them may only be used from within the
62         -- 'IO' monad.
63
64         -- ** The @catch@ functions
65         catch,     -- :: IO a -> (Exception -> IO a) -> IO a
66         catches, Handler(..),
67         catchAny,
68         catchJust, -- :: (Exception -> Maybe b) -> IO a -> (b -> IO a) -> IO a
69
70         -- ** The @handle@ functions
71         handle,    -- :: (Exception -> IO a) -> IO a -> IO a
72         handleAny,
73         handleJust,-- :: (Exception -> Maybe b) -> (b -> IO a) -> IO a -> IO a
74
75         -- ** The @try@ functions
76         try,       -- :: IO a -> IO (Either Exception a)
77         tryJust,   -- :: (Exception -> Maybe b) -> a    -> IO (Either b a)
78         ignoreExceptions,
79         onException,
80
81         -- ** The @evaluate@ function
82         evaluate,  -- :: a -> IO a
83
84         -- ** The @mapException@ function
85         mapException,           -- :: (Exception -> Exception) -> a -> a
86
87         -- * Asynchronous Exceptions
88
89         -- $async
90
91         -- ** Asynchronous exception control
92
93         -- |The following two functions allow a thread to control delivery of
94         -- asynchronous exceptions during a critical region.
95
96         block,          -- :: IO a -> IO a
97         unblock,        -- :: IO a -> IO a
98         blocked,        -- :: IO Bool
99
100         -- *** Applying @block@ to an exception handler
101
102         -- $block_handler
103
104         -- *** Interruptible operations
105
106         -- $interruptible
107
108         -- * Assertions
109
110         assert,         -- :: Bool -> a -> a
111
112         -- * Utilities
113
114         bracket,        -- :: IO a -> (a -> IO b) -> (a -> IO c) -> IO ()
115         bracket_,       -- :: IO a -> IO b -> IO c -> IO ()
116         bracketOnError,
117
118         finally,        -- :: IO a -> IO b -> IO a
119
120         recSelError, recConError, irrefutPatError, runtimeError,
121         nonExhaustiveGuardsError, patError, noMethodBindingError,
122
123 #ifdef __GLASGOW_HASKELL__
124         setUncaughtExceptionHandler,      -- :: (Exception -> IO ()) -> IO ()
125         getUncaughtExceptionHandler       -- :: IO (Exception -> IO ())
126 #endif
127   ) where
128
129 #ifdef __GLASGOW_HASKELL__
130 import GHC.Base
131 import GHC.IOBase
132 import {-# SOURCE #-} GHC.Handle
133 import GHC.List
134 import GHC.Num
135 import GHC.Show
136 import GHC.IOBase as ExceptionBase
137 import GHC.Exception hiding ( Exception )
138 import {-# SOURCE #-} GHC.Conc         ( ThreadId(ThreadId) )
139 import Foreign.C.String ( CString, withCString )
140 #endif
141
142 #ifdef __HUGS__
143 import Hugs.Exception   as ExceptionBase
144 #endif
145
146 import Data.Dynamic
147 import Data.Either
148 import Data.Maybe
149
150 #ifdef __NHC__
151 import qualified System.IO.Error as H'98 (catch)
152 import System.IO.Error (ioError)
153 import IO              (bracket)
154 import DIOError         -- defn of IOError type
155 import System          (ExitCode())
156
157 -- minimum needed for nhc98 to pretend it has Exceptions
158 data Exception   = IOException    IOException
159                  | ArithException ArithException
160                  | ArrayException ArrayException
161                  | AsyncException AsyncException
162                  | ExitException  ExitCode
163                  deriving Show
164 type IOException = IOError
165 data ArithException
166 data ArrayException
167 data AsyncException
168 instance Show ArithException
169 instance Show ArrayException
170 instance Show AsyncException
171
172 catch    :: IO a -> (Exception -> IO a) -> IO a
173 a `catch` b = a `H'98.catch` (b . IOException)
174
175 throwIO  :: Exception -> IO a
176 throwIO (IOException e) = ioError e
177 throwIO _               = ioError (UserError "Control.Exception.throwIO"
178                                              "unknown exception")
179 throw    :: Exception -> a
180 throw     = unsafePerformIO . throwIO
181
182 evaluate :: a -> IO a
183 evaluate x = x `seq` return x
184
185 assert :: Bool -> a -> a
186 assert True  x = x
187 assert False _ = throw (IOException (UserError "" "Assertion failed"))
188 #endif
189
190 #ifndef __GLASGOW_HASKELL__
191 -- Dummy definitions for implementations lacking asynchonous exceptions
192
193 block   :: IO a -> IO a
194 block    = id
195 unblock :: IO a -> IO a
196 unblock  = id
197 blocked :: IO Bool
198 blocked  = return False
199 #endif
200
201 -----------------------------------------------------------------------------
202 -- Catching exceptions
203
204 -- |This is the simplest of the exception-catching functions.  It
205 -- takes a single argument, runs it, and if an exception is raised
206 -- the \"handler\" is executed, with the value of the exception passed as an
207 -- argument.  Otherwise, the result is returned as normal.  For example:
208 --
209 -- >   catch (openFile f ReadMode) 
210 -- >       (\e -> hPutStr stderr ("Couldn't open "++f++": " ++ show e))
211 --
212 -- For catching exceptions in pure (non-'IO') expressions, see the
213 -- function 'evaluate'.
214 --
215 -- Note that due to Haskell\'s unspecified evaluation order, an
216 -- expression may return one of several possible exceptions: consider
217 -- the expression @error \"urk\" + 1 \`div\` 0@.  Does
218 -- 'catch' execute the handler passing
219 -- @ErrorCall \"urk\"@, or @ArithError DivideByZero@?
220 --
221 -- The answer is \"either\": 'catch' makes a
222 -- non-deterministic choice about which exception to catch.  If you
223 -- call it again, you might get a different exception back.  This is
224 -- ok, because 'catch' is an 'IO' computation.
225 --
226 -- Note that 'catch' catches all types of exceptions, and is generally
227 -- used for \"cleaning up\" before passing on the exception using
228 -- 'throwIO'.  It is not good practice to discard the exception and
229 -- continue, without first checking the type of the exception (it
230 -- might be a 'ThreadKilled', for example).  In this case it is usually better
231 -- to use 'catchJust' and select the kinds of exceptions to catch.
232 --
233 -- Also note that the "Prelude" also exports a function called
234 -- 'Prelude.catch' with a similar type to 'Control.Exception.catch',
235 -- except that the "Prelude" version only catches the IO and user
236 -- families of exceptions (as required by Haskell 98).  
237 --
238 -- We recommend either hiding the "Prelude" version of 'Prelude.catch'
239 -- when importing "Control.Exception": 
240 --
241 -- > import Prelude hiding (catch)
242 --
243 -- or importing "Control.Exception" qualified, to avoid name-clashes:
244 --
245 -- > import qualified Control.Exception as C
246 --
247 -- and then using @C.catch@
248 --
249 #ifndef __NHC__
250 catch   :: Exception e
251         => IO a         -- ^ The computation to run
252         -> (e -> IO a)  -- ^ Handler to invoke if an exception is raised
253         -> IO a
254 catch = ExceptionBase.catchException
255
256 catches :: IO a -> [Handler a] -> IO a
257 catches io handlers = io `catch` catchesHandler handlers
258
259 catchesHandler :: [Handler a] -> SomeException -> IO a
260 catchesHandler handlers e = foldr tryHandler (throw e) handlers
261     where tryHandler (Handler handler) res
262               = case fromException e of
263                 Just e' -> handler e'
264                 Nothing -> res
265
266 data Handler a = forall e . Exception e => Handler (e -> IO a)
267 #endif
268 -- | The function 'catchJust' is like 'catch', but it takes an extra
269 -- argument which is an /exception predicate/, a function which
270 -- selects which type of exceptions we\'re interested in.
271 --
272 -- >   result <- catchJust errorCalls thing_to_try handler
273 --
274 -- Any other exceptions which are not matched by the predicate
275 -- are re-raised, and may be caught by an enclosing
276 -- 'catch' or 'catchJust'.
277 catchJust
278         :: Exception e
279         => (e -> Maybe b)         -- ^ Predicate to select exceptions
280         -> IO a                   -- ^ Computation to run
281         -> (b -> IO a)            -- ^ Handler
282         -> IO a
283 catchJust p a handler = catch a handler'
284   where handler' e = case p e of 
285                         Nothing -> throw e
286                         Just b  -> handler b
287
288 -- | A version of 'catch' with the arguments swapped around; useful in
289 -- situations where the code for the handler is shorter.  For example:
290 --
291 -- >   do handle (\e -> exitWith (ExitFailure 1)) $
292 -- >      ...
293 handle     :: Exception e => (e -> IO a) -> IO a -> IO a
294 handle     =  flip catch
295
296 handleAny  :: (forall e . Exception e => e -> IO a) -> IO a -> IO a
297 handleAny  =  flip catchAny
298
299 -- | A version of 'catchJust' with the arguments swapped around (see
300 -- 'handle').
301 handleJust :: Exception e => (e -> Maybe b) -> (b -> IO a) -> IO a -> IO a
302 handleJust p =  flip (catchJust p)
303
304 -----------------------------------------------------------------------------
305 -- 'mapException'
306
307 -- | This function maps one exception into another as proposed in the
308 -- paper \"A semantics for imprecise exceptions\".
309
310 -- Notice that the usage of 'unsafePerformIO' is safe here.
311
312 mapException :: Exception e => (e -> e) -> a -> a
313 mapException f v = unsafePerformIO (catch (evaluate v)
314                                           (\x -> throw (f x)))
315
316 -----------------------------------------------------------------------------
317 -- 'try' and variations.
318
319 -- | Similar to 'catch', but returns an 'Either' result which is
320 -- @('Right' a)@ if no exception was raised, or @('Left' e)@ if an
321 -- exception was raised and its value is @e@.
322 --
323 -- >  try a = catch (Right `liftM` a) (return . Left)
324 --
325 -- Note: as with 'catch', it is only polite to use this variant if you intend
326 -- to re-throw the exception after performing whatever cleanup is needed.
327 -- Otherwise, 'tryJust' is generally considered to be better.
328 --
329 -- Also note that "System.IO.Error" also exports a function called
330 -- 'System.IO.Error.try' with a similar type to 'Control.Exception.try',
331 -- except that it catches only the IO and user families of exceptions
332 -- (as required by the Haskell 98 @IO@ module).
333
334 try :: Exception e => IO a -> IO (Either e a)
335 try a = catch (a >>= \ v -> return (Right v)) (\e -> return (Left e))
336
337 -- | A variant of 'try' that takes an exception predicate to select
338 -- which exceptions are caught (c.f. 'catchJust').  If the exception
339 -- does not match the predicate, it is re-thrown.
340 tryJust :: Exception e => (e -> Maybe b) -> IO a -> IO (Either b a)
341 tryJust p a = do
342   r <- try a
343   case r of
344         Right v -> return (Right v)
345         Left  e -> case p e of
346                         Nothing -> throw e
347                         Just b  -> return (Left b)
348
349 ignoreExceptions :: IO () -> IO ()
350 ignoreExceptions io = io `catchAny` \_ -> return ()
351
352 onException :: IO a -> IO () -> IO a
353 onException io what = io `catch` \e -> do what
354                                           throw (e :: SomeException)
355
356 -----------------------------------------------------------------------------
357 -- Some Useful Functions
358
359 -- | When you want to acquire a resource, do some work with it, and
360 -- then release the resource, it is a good idea to use 'bracket',
361 -- because 'bracket' will install the necessary exception handler to
362 -- release the resource in the event that an exception is raised
363 -- during the computation.  If an exception is raised, then 'bracket' will 
364 -- re-raise the exception (after performing the release).
365 --
366 -- A common example is opening a file:
367 --
368 -- > bracket
369 -- >   (openFile "filename" ReadMode)
370 -- >   (hClose)
371 -- >   (\handle -> do { ... })
372 --
373 -- The arguments to 'bracket' are in this order so that we can partially apply 
374 -- it, e.g.:
375 --
376 -- > withFile name mode = bracket (openFile name mode) hClose
377 --
378 #ifndef __NHC__
379 bracket 
380         :: IO a         -- ^ computation to run first (\"acquire resource\")
381         -> (a -> IO b)  -- ^ computation to run last (\"release resource\")
382         -> (a -> IO c)  -- ^ computation to run in-between
383         -> IO c         -- returns the value from the in-between computation
384 bracket before after thing =
385   block (do
386     a <- before 
387     r <- catchAny
388            (unblock (thing a))
389            (\e -> do { after a; throw e })
390     after a
391     return r
392  )
393 #endif
394
395 -- | A specialised variant of 'bracket' with just a computation to run
396 -- afterward.
397 -- 
398 finally :: IO a         -- ^ computation to run first
399         -> IO b         -- ^ computation to run afterward (even if an exception 
400                         -- was raised)
401         -> IO a         -- returns the value from the first computation
402 a `finally` sequel =
403   block (do
404     r <- catchAny
405              (unblock a)
406              (\e -> do { sequel; throw e })
407     sequel
408     return r
409   )
410
411 -- | A variant of 'bracket' where the return value from the first computation
412 -- is not required.
413 bracket_ :: IO a -> IO b -> IO c -> IO c
414 bracket_ before after thing = bracket before (const after) (const thing)
415
416 -- | Like bracket, but only performs the final action if there was an 
417 -- exception raised by the in-between computation.
418 bracketOnError
419         :: IO a         -- ^ computation to run first (\"acquire resource\")
420         -> (a -> IO b)  -- ^ computation to run last (\"release resource\")
421         -> (a -> IO c)  -- ^ computation to run in-between
422         -> IO c         -- returns the value from the in-between computation
423 bracketOnError before after thing =
424   block (do
425     a <- before 
426     catchAny
427         (unblock (thing a))
428         (\e -> do { after a; throw e })
429  )
430
431 -- -----------------------------------------------------------------------------
432 -- Asynchronous exceptions
433
434 {- $async
435
436  #AsynchronousExceptions# Asynchronous exceptions are so-called because they arise due to
437 external influences, and can be raised at any point during execution.
438 'StackOverflow' and 'HeapOverflow' are two examples of
439 system-generated asynchronous exceptions.
440
441 The primary source of asynchronous exceptions, however, is
442 'throwTo':
443
444 >  throwTo :: ThreadId -> Exception -> IO ()
445
446 'throwTo' (also 'throwDynTo' and 'Control.Concurrent.killThread') allows one
447 running thread to raise an arbitrary exception in another thread.  The
448 exception is therefore asynchronous with respect to the target thread,
449 which could be doing anything at the time it receives the exception.
450 Great care should be taken with asynchronous exceptions; it is all too
451 easy to introduce race conditions by the over zealous use of
452 'throwTo'.
453 -}
454
455 {- $block_handler
456 There\'s an implied 'block' around every exception handler in a call
457 to one of the 'catch' family of functions.  This is because that is
458 what you want most of the time - it eliminates a common race condition
459 in starting an exception handler, because there may be no exception
460 handler on the stack to handle another exception if one arrives
461 immediately.  If asynchronous exceptions are blocked on entering the
462 handler, though, we have time to install a new exception handler
463 before being interrupted.  If this weren\'t the default, one would have
464 to write something like
465
466 >      block (
467 >           catch (unblock (...))
468 >                      (\e -> handler)
469 >      )
470
471 If you need to unblock asynchronous exceptions again in the exception
472 handler, just use 'unblock' as normal.
473
474 Note that 'try' and friends /do not/ have a similar default, because
475 there is no exception handler in this case.  If you want to use 'try'
476 in an asynchronous-exception-safe way, you will need to use
477 'block'.
478 -}
479
480 {- $interruptible
481
482 Some operations are /interruptible/, which means that they can receive
483 asynchronous exceptions even in the scope of a 'block'.  Any function
484 which may itself block is defined as interruptible; this includes
485 'Control.Concurrent.MVar.takeMVar'
486 (but not 'Control.Concurrent.MVar.tryTakeMVar'),
487 and most operations which perform
488 some I\/O with the outside world.  The reason for having
489 interruptible operations is so that we can write things like
490
491 >      block (
492 >         a <- takeMVar m
493 >         catch (unblock (...))
494 >               (\e -> ...)
495 >      )
496
497 if the 'Control.Concurrent.MVar.takeMVar' was not interruptible,
498 then this particular
499 combination could lead to deadlock, because the thread itself would be
500 blocked in a state where it can\'t receive any asynchronous exceptions.
501 With 'Control.Concurrent.MVar.takeMVar' interruptible, however, we can be
502 safe in the knowledge that the thread can receive exceptions right up
503 until the point when the 'Control.Concurrent.MVar.takeMVar' succeeds.
504 Similar arguments apply for other interruptible operations like
505 'System.IO.openFile'.
506 -}
507
508 #if !(__GLASGOW_HASKELL__ || __NHC__)
509 assert :: Bool -> a -> a
510 assert True x = x
511 assert False _ = throw (AssertionFailed "")
512 #endif
513
514
515 #ifdef __GLASGOW_HASKELL__
516 {-# NOINLINE uncaughtExceptionHandler #-}
517 uncaughtExceptionHandler :: IORef (SomeException -> IO ())
518 uncaughtExceptionHandler = unsafePerformIO (newIORef defaultHandler)
519    where
520       defaultHandler :: SomeException -> IO ()
521       defaultHandler se@(SomeException ex) = do
522          (hFlush stdout) `catchAny` (\ _ -> return ())
523          let msg = case cast ex of
524                Just Deadlock -> "no threads to run:  infinite loop or deadlock?"
525                _ -> case cast ex of
526                     Just (ErrorCall s) -> s
527                     _                  -> showsPrec 0 se ""
528          withCString "%s" $ \cfmt ->
529           withCString msg $ \cmsg ->
530             errorBelch cfmt cmsg
531
532 -- don't use errorBelch() directly, because we cannot call varargs functions
533 -- using the FFI.
534 foreign import ccall unsafe "HsBase.h errorBelch2"
535    errorBelch :: CString -> CString -> IO ()
536
537 setUncaughtExceptionHandler :: (SomeException -> IO ()) -> IO ()
538 setUncaughtExceptionHandler = writeIORef uncaughtExceptionHandler
539
540 getUncaughtExceptionHandler :: IO (SomeException -> IO ())
541 getUncaughtExceptionHandler = readIORef uncaughtExceptionHandler
542 #endif
543
544 recSelError, recConError, irrefutPatError, runtimeError,
545              nonExhaustiveGuardsError, patError, noMethodBindingError
546         :: Addr# -> a   -- All take a UTF8-encoded C string
547
548 recSelError              s = throw (RecSelError (unpackCStringUtf8# s)) -- No location info unfortunately
549 runtimeError             s = error (unpackCStringUtf8# s)               -- No location info unfortunately
550
551 nonExhaustiveGuardsError s = throw (PatternMatchFail (untangle s "Non-exhaustive guards in"))
552 irrefutPatError          s = throw (PatternMatchFail (untangle s "Irrefutable pattern failed for pattern"))
553 recConError              s = throw (RecConError      (untangle s "Missing field in record construction"))
554 noMethodBindingError     s = throw (NoMethodError    (untangle s "No instance nor default method for class operation"))
555 patError                 s = throw (PatternMatchFail (untangle s "Non-exhaustive patterns in"))
556
557 -----
558
559 data PatternMatchFail = PatternMatchFail String
560     deriving Typeable
561
562 instance Exception PatternMatchFail
563
564 instance Show PatternMatchFail where
565     showsPrec _ (PatternMatchFail err) = showString err
566
567 -----
568
569 data RecSelError = RecSelError String
570     deriving Typeable
571
572 instance Exception RecSelError
573
574 instance Show RecSelError where
575     showsPrec _ (RecSelError err) = showString err
576
577 -----
578
579 data RecConError = RecConError String
580     deriving Typeable
581
582 instance Exception RecConError
583
584 instance Show RecConError where
585     showsPrec _ (RecConError err) = showString err
586
587 -----
588
589 data RecUpdError = RecUpdError String
590     deriving Typeable
591
592 instance Exception RecUpdError
593
594 instance Show RecUpdError where
595     showsPrec _ (RecUpdError err) = showString err
596
597 -----
598
599 data NoMethodError = NoMethodError String
600     deriving Typeable
601
602 instance Exception NoMethodError
603
604 instance Show NoMethodError where
605     showsPrec _ (NoMethodError err) = showString err
606
607 -----
608
609 data AssertionFailed = AssertionFailed String
610     deriving Typeable
611
612 instance Exception AssertionFailed
613
614 instance Show AssertionFailed where
615     showsPrec _ (AssertionFailed err) = showString err
616
617 -----
618
619 data NonTermination = NonTermination
620     deriving Typeable
621
622 instance Exception NonTermination
623
624 instance Show NonTermination where
625     showsPrec _ NonTermination = showString "<<loop>>"
626
627 -- GHC's RTS calls this
628 nonTermination :: SomeException
629 nonTermination = toException NonTermination
630
631 -----
632
633 data Deadlock = Deadlock
634     deriving Typeable
635
636 instance Exception Deadlock
637
638 instance Show Deadlock where
639     showsPrec _ Deadlock = showString "<<deadlock>>"
640
641 -----
642
643 data NestedAtomically = NestedAtomically
644     deriving Typeable
645
646 instance Exception NestedAtomically
647
648 instance Show NestedAtomically where
649     showsPrec _ NestedAtomically = showString "Control.Concurrent.STM.atomically was nested"
650
651 -- GHC's RTS calls this
652 nestedAtomically :: SomeException
653 nestedAtomically = toException NestedAtomically
654
655 -----
656
657 instance Exception Dynamic
658
659 -----
660
661 assertError :: Addr# -> Bool -> a -> a
662 assertError str pred v
663   | pred      = v
664   | otherwise = throw (AssertionFailed (untangle str "Assertion failed"))
665
666 {-
667 (untangle coded message) expects "coded" to be of the form
668         "location|details"
669 It prints
670         location message details
671 -}
672 untangle :: Addr# -> String -> String
673 untangle coded message
674   =  location
675   ++ ": " 
676   ++ message
677   ++ details
678   ++ "\n"
679   where
680     coded_str = unpackCStringUtf8# coded
681
682     (location, details)
683       = case (span not_bar coded_str) of { (loc, rest) ->
684         case rest of
685           ('|':det) -> (loc, ' ' : det)
686           _         -> (loc, "")
687         }
688     not_bar c = c /= '|'
689
690 -- XXX From GHC.Conc
691 throwTo :: Exception e => ThreadId -> e -> IO ()
692 throwTo (ThreadId id) ex = IO $ \ s ->
693    case (killThread# id (toException ex) s) of s1 -> (# s1, () #)
694