<commit_before>-- This is a very cut-down version of GHC's Exception module
--
-- There are two big differences:
-- 1) The IOException type has a smaller set of different constructors.
--    This is because Hugs uses a different internal representation for
--    errors.
-- 2) It is not possible to catch certain kinds of exception in the current
--    Hugs implementation.  In particular, heap and stack overflow and
--    ctrl-C.  Indeed, it is not entirely clear what to do in response to
--    ctrl-C.
-- 3) We don't support an arbitrary throw.
--    It just isn't possible to throw an IOError from outside the
--    IO monad.  You can throw a HugsException or you can use throwIO.
-- 
-- The code is directly based on the code in GHC.

module Exception( 
        Exception(..),

	tryAll,    -- :: a    -> IO (Either Exception a)
	tryAllIO,  -- :: IO a -> IO (Either Exception a)
	try,	   -- :: (Exception -> Maybe b) -> a    -> IO (Either b a)
	tryIO,	   -- :: (Exception -> Maybe b) -> IO a -> IO (Either b a)

	catchAll,  -- :: a    -> (Exception -> IO a) -> IO a
	catchAllIO,-- :: IO a -> (Exception -> IO a) -> IO a
	catch,     -- :: (Exception -> Maybe b) -> a    -> (b -> IO a) -> IO a
	catchIO,   -- :: (Exception -> Maybe b) -> IO a -> (b -> IO a) -> IO a

	-- Exception predicates

	justIoErrors,		-- :: Exception -> Maybe IOError
        justHugsExceptions,     -- :: Exception -> Maybe HugsException

	-- Throwing exceptions

	throwIO,		-- :: Exception -> IO a

	-- Utilities
		
	bracket,  	-- :: IO a -> (a -> IO b) -> (a -> IO c) -> IO ()
	bracket_, 	-- :: IO a -> IO b -> IO c -> IO ()
        finally,        -- :: IO a -> IO b
  ) where

import Prelude hiding (catch)

----------------------------------------------------------------
-- Exception datatype and operations
----------------------------------------------------------------

data Exception
  = IOException 	IOError		-- IO exceptions (from 'ioError')
  | HugsException  	HugsException	-- An error detected by Hugs

instance Show Exception where
  showsPrec _ (IOException err)	  = shows err 
  showsPrec _ (HugsException exn) = shows exn 

----------------------------------------------------------------
-- Primitive catch
----------------------------------------------------------------

throwIO :: Exception -> IO a
throwIO (IOException err)   = ioError err
throwIO (HugsException exn) = primThrowException exn

catchException :: IO a -> (Exception -> IO a) -> IO a
catchException m k = do
  (m `catchHugsException` (k . HugsException)) `Prelude.catch` (k . IOException)

----------------------------------------------------------------
-- Catching Exceptions
----------------------------------------------------------------

catchAll  :: a    -> (Exception -> IO a) -> IO a
catchAll a handler = catchException (a `seq` return a) handler

catchAllIO :: IO a -> (Exception -> IO a) -> IO a
catchAllIO =  catchException

catch :: (Exception -> Maybe b) -> a -> (b -> IO a) -> IO a
catch p a handler = catchAll a handler'
  where handler' e = case p e of 
			Nothing -> throwIO e
			Just b  -> handler b

catchIO :: (Exception -> Maybe b) -> IO a -> (b -> IO a) -> IO a
catchIO p a handler = catchAllIO a handler'
  where handler' e = case p e of 
			Nothing -> throwIO e
			Just b  -> handler b

----------------------------------------------------------------
-- 'try' and variations.
----------------------------------------------------------------

tryAll :: a    -> IO (Either Exception a)
tryAll a = catchException (a `seq` return (Right a)) (\e -> return (Left e))

tryAllIO :: IO a -> IO (Either Exception a)
tryAllIO a = catchAllIO (a >>= \ v -> return (Right v))
			(\e -> return (Left e))

try :: (Exception -> Maybe b) -> a -> IO (Either b a)
try p a = do
  r <- tryAll a
  case r of
	Right v -> return (Right v)
	Left  e -> case p e of
			Nothing -> throwIO e
			Just b  -> return (Left b)

tryIO :: (Exception -> Maybe b) -> IO a -> IO (Either b a)
tryIO p a = do
  r <- tryAllIO a
  case r of
	Right v -> return (Right v)
	Left  e -> case p e of
			Nothing -> throwIO e
			Just b  -> return (Left b)

bracket        :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket before after m = do
        x  <- before
        rs <- tryAllIO (m x)
        after x
        case rs of<commit_msg>This is one of two commits aimed at reducing the gap between Hugs-supplied libraries and their GHC counterparts.  The ultimate goal is to make the fptools/hslibs libraries work with Hugs.<commit_after>-- This is a very cut-down version of GHC's Exception module
--
-- There are two big differences:
-- 1) The IOException type has a smaller set of different constructors.
--    This is because Hugs uses a different internal representation for
--    errors.
-- 2) It is not possible to catch certain kinds of exception in the current
--    Hugs implementation.  In particular, heap and stack overflow and
--    ctrl-C.  Indeed, it is not entirely clear what to do in response to
--    ctrl-C.
-- 3) We don't support an arbitrary throw.
--    It just isn't possible to throw an IOError from outside the
--    IO monad.  You can throw a HugsException or you can use throwIO.
-- 
-- The code is directly based on the code in GHC.

module Exception( 
        Exception(..),

	try,       -- :: IO a -> IO (Either Exception a)
	tryJust,   -- :: (Exception -> Maybe b) -> a    -> IO (Either b a)

	catch,     -- :: IO a -> (Exception -> IO a) -> IO a
	catchJust, -- :: (Exception -> Maybe b) -> IO a -> (b -> IO a) -> IO a

	tryAll,    -- :: a    -> IO (Either Exception a)
	tryAllIO,  -- :: IO a -> IO (Either Exception a)

	catchAll,  -- :: a    -> (Exception -> IO a) -> IO a
	catchAllIO,-- :: IO a -> (Exception -> IO a) -> IO a

	evaluate,  -- :: a -> IO a

	-- Exception predicates

	justIoErrors,		-- :: Exception -> Maybe IOError
        justHugsExceptions,     -- :: Exception -> Maybe HugsException

	-- Throwing exceptions

	throwIO,		-- :: Exception -> IO a

	-- Async exception control

        block,          -- :: IO a -> IO a
        unblock,        -- :: IO a -> IO a

	-- Utilities
		
	bracket,  	-- :: IO a -> (a -> IO b) -> (a -> IO c) -> IO ()
	bracket_, 	-- :: IO a -> IO b -> IO c -> IO ()
        finally,        -- :: IO a -> IO b
  ) where

import Prelude hiding (catch)

----------------------------------------------------------------
-- Exception datatype and operations
----------------------------------------------------------------

data Exception
  = IOException 	IOError		-- IO exceptions (from 'ioError')
  | HugsException  	HugsException	-- An error detected by Hugs

instance Show Exception where
  showsPrec _ (IOException err)	  = shows err 
  showsPrec _ (HugsException exn) = shows exn 

----------------------------------------------------------------
-- Primitive catch
----------------------------------------------------------------

throwIO :: Exception -> IO a
throwIO (IOException err)   = ioError err
throwIO (HugsException exn) = primThrowException exn

catchException :: IO a -> (Exception -> IO a) -> IO a
catchException m k = do
  (m `catchHugsException` (k . HugsException)) `Prelude.catch` (k . IOException)

----------------------------------------------------------------
-- Catching Exceptions
----------------------------------------------------------------

catch  :: IO a  -> (Exception -> IO a) -> IO a
catch a handler = catchException a handler

catchAll  :: a  -> (Exception -> IO a) -> IO a
catchAll a handler = catchException (a `seq` return a) handler

catchAllIO :: IO a -> (Exception -> IO a) -> IO a
catchAllIO =  catchException

catchJust :: (Exception -> Maybe b) -> IO a -> (b -> IO a) -> IO a
catchJust p a handler = catchAllIO a handler'
  where handler' e = case p e of 
			Nothing -> throwIO e
			Just b  -> handler b

----------------------------------------------------------------
-- evaluate
----------------------------------------------------------------

evaluate :: a -> IO a
evaluate a = a `seq` return a

----------------------------------------------------------------
-- dummy implementations of block and unblock
----------------------------------------------------------------

block, unblock :: IO a -> IO a
block   m = m
unblock m = m

----------------------------------------------------------------
-- 'try' and variations.
----------------------------------------------------------------

tryAll :: a    -> IO (Either Exception a)
tryAll a = catchException (a `seq` return (Right a)) (\e -> return (Left e))

tryAllIO :: IO a -> IO (Either Exception a)
tryAllIO = try

try :: IO a -> IO (Either Exception a)
try a = catchAllIO (a >>= \ v -> return (Right v))
			(\e -> return (Left e))

tryJust :: (Exception -> Maybe b) -> a -> IO (Either b a)
tryJust p a = do
  r <- tryAll a
  case r of
	Right v -> return (Right v)
	Left  e -> case p e of
			Nothing -> throwIO e
			Just b  -> return (Left b)

bracket        :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket before after m = do
        x  <- before
        rs <- tryAllIO (m x)
        after x
        case rs of
{'fraction_non_alphanumeric': 0.2518796992481203, 'fraction_numerical': 0.0006634232640424591, 'mean_word_length': 1.4706364381316581, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 166, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4853728', 'n_tokens_mistral': 2581, 'n_tokens_neox': 2472, 'n_words': 1090}