FastTypes - delete ugly _signatures, comment on Bool not FastBool where the typecheck...
[ghc-hetmet.git] / compiler / utils / Util.lhs
index 522f795..39fd64b 100644 (file)
@@ -1,4 +1,5 @@
 %
+% (c) The University of Glasgow 2006
 % (c) The University of Glasgow 1992-2002
 %
 \section[Util]{Highly random utility functions}
@@ -11,7 +12,7 @@ module Util (
         zipLazy, stretchZipWith,
        mapFst, mapSnd,
        mapAndUnzip, mapAndUnzip3,
-       nOfThem, filterOut,
+       nOfThem, filterOut, partitionWith, splitEithers,
 
        lengthExceeds, lengthIs, lengthAtLeast, 
        listLengthCmp, atLength, equalLength, compareLength,
@@ -82,26 +83,26 @@ module Util (
 import Panic           ( panic, trace )
 import FastTypes
 
-import EXCEPTION       ( Exception(..), finally, throwDyn, catchDyn, throw )
-import qualified EXCEPTION as Exception
-import DYNAMIC         ( Typeable )
-import DATA_IOREF      ( IORef, newIORef )
-import UNSAFE_IO       ( unsafePerformIO )
-import DATA_IOREF      ( readIORef, writeIORef )
+import Control.Exception ( Exception(..), finally, catchDyn, throw )
+import qualified Control.Exception as Exception
+import Data.Dynamic    ( Typeable )
+import Data.IORef      ( IORef, newIORef )
+import System.IO.Unsafe        ( unsafePerformIO )
+import Data.IORef      ( readIORef, writeIORef )
 
-import qualified List  ( elem, notElem )
+import qualified Data.List as List ( elem, notElem )
 
 #ifndef DEBUG
-import List            ( zipWith4 )
+import Data.List               ( zipWith4 )
 #endif
 
-import Monad           ( when )
-import IO              ( catch, isDoesNotExistError )
-import Directory       ( doesDirectoryExist, createDirectory )
-import Char            ( isUpper, isAlphaNum, isSpace, ord, isDigit )
-import Ratio           ( (%) )
-import Time            ( ClockTime )
-import Directory       ( getModificationTime )
+import Control.Monad   ( when )
+import SYSTEM_IO_ERROR as IO ( catch, isDoesNotExistError )
+import System.Directory        ( doesDirectoryExist, createDirectory,
+                          getModificationTime )
+import Data.Char       ( isUpper, isAlphaNum, isSpace, ord, isDigit )
+import Data.Ratio      ( (%) )
+import System.Time     ( ClockTime )
 
 infixr 9 `thenCmp`
 \end{code}
@@ -167,6 +168,22 @@ filterOut :: (a->Bool) -> [a] -> [a]
 filterOut p [] = []
 filterOut p (x:xs) | p x       = filterOut p xs
                   | otherwise = x : filterOut p xs
+
+partitionWith :: (a -> Either b c) -> [a] -> ([b], [c])
+partitionWith f [] = ([],[])
+partitionWith f (x:xs) = case f x of
+                          Left  b -> (b:bs, cs)
+                          Right c -> (bs, c:cs)
+                      where
+                        (bs,cs) = partitionWith f xs
+
+splitEithers :: [Either a b] -> ([a], [b])
+splitEithers [] = ([],[])
+splitEithers (e : es) = case e of
+                         Left x -> (x:xs, ys)
+                         Right y -> (xs, y:ys)
+                     where
+                       (xs,ys) = splitEithers es
 \end{code}
 
 A paranoid @zip@ (and some @zipWith@ friends) that checks the lists
@@ -873,13 +890,9 @@ handleDyn :: Typeable ex => (ex -> IO a) -> IO a -> IO a
 handleDyn = flip catchDyn
 
 handle :: (Exception -> IO a) -> IO a -> IO a
-#if __GLASGOW_HASKELL__ < 501
-handle = flip Exception.catchAllIO
-#else
 handle h f = f `Exception.catch` \e -> case e of
     ExitException _ -> throw e
     _               -> h e
-#endif
 
 -- --------------------------------------------------------------
 -- check existence & modification time at the same time