X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Futils%2FUtil.lhs;h=39fd64b679dafb6d36b68fee8f5620dcfdab1fca;hb=cd883e2f4dc0c37488441d18ef7b3a9ae9719b55;hp=522f795bcb458f0b275868a6a3532e27db6050c4;hpb=3d9f1290f071b0d2e48060a160995af7608d1045;p=ghc-hetmet.git diff --git a/compiler/utils/Util.lhs b/compiler/utils/Util.lhs index 522f795..39fd64b 100644 --- a/compiler/utils/Util.lhs +++ b/compiler/utils/Util.lhs @@ -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