X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Futils%2FUtil.lhs;h=eb8595dc997768cc435e505cce01e43c13f8fbc8;hb=531a43d6aa7230c737575e913f3d9c831bf42fbf;hp=8473faf42eeb94d29641c35e850b2e5d7a9454d7;hpb=8100c58a929d993d2dd7022412ed6113cf97fba7;p=ghc-hetmet.git diff --git a/compiler/utils/Util.lhs b/compiler/utils/Util.lhs index 8473faf..eb8595d 100644 --- a/compiler/utils/Util.lhs +++ b/compiler/utils/Util.lhs @@ -5,14 +5,24 @@ \section[Util]{Highly random utility functions} \begin{code} +{-# OPTIONS -w #-} +-- The above warning supression flag is a temporary kludge. +-- While working on this module you are encouraged to remove it and fix +-- any warnings in the module. See +-- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings +-- for details + module Util ( + foldl1', + -- general list processing zipEqual, zipWithEqual, zipWith3Equal, zipWith4Equal, zipLazy, stretchZipWith, mapFst, mapSnd, mapAndUnzip, mapAndUnzip3, nOfThem, filterOut, partitionWith, splitEithers, + foldl1', lengthExceeds, lengthIs, lengthAtLeast, listLengthCmp, atLength, equalLength, compareLength, @@ -26,7 +36,7 @@ module Util ( nTimes, -- sorting - sortLe, sortWith, + sortLe, sortWith, on, -- transitive closures transitiveClosure, @@ -79,20 +89,23 @@ module Util ( #include "HsVersions.h" -import Panic ( panic, trace ) import FastTypes +#if defined(DEBUG) || __GLASGOW_HASKELL__ < 604 +import Panic +#endif + 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 Data.List hiding (group) -import qualified Data.List as List ( elem, notElem ) - -#ifndef DEBUG -import Data.List ( zipWith4 ) +import qualified Data.List as List ( elem ) +#ifdef DEBUG +import qualified Data.List as List ( notElem ) #endif import Control.Monad ( when ) @@ -106,6 +119,15 @@ import System.Time ( ClockTime ) infixr 9 `thenCmp` \end{code} +\begin{code} +#if __GLASGOW_HASKELL__ < 603 +-- foldl1' was introduce in GHC 6.4 +foldl1' :: (a -> a -> a) -> [a] -> a +foldl1' f (x:xs) = foldl' f x xs +foldl1' _ [] = errorEmptyList "foldl1'" +#endif +\end{code} + %************************************************************************ %* * \subsection{A for loop} @@ -352,6 +374,16 @@ isn'tIn msg x ys # endif /* DEBUG */ \end{code} +foldl1' was added in GHC 6.4 + +\begin{code} +#if __GLASGOW_HASKELL__ < 604 +foldl1' :: (a -> a -> a) -> [a] -> a +foldl1' f (x:xs) = foldl' f x xs +foldl1' _ [] = panic "foldl1'" +#endif +\end{code} + %************************************************************************ %* * \subsubsection[Utils-Carsten-mergesort]{A mergesort from Carsten} @@ -453,6 +485,10 @@ sortWith :: Ord b => (a->b) -> [a] -> [a] sortWith get_key xs = sortLe le xs where x `le` y = get_key x < get_key y + +on :: (a -> a -> Ordering) -> (b -> a) -> b -> b -> Ordering +on cmp sel = \x y -> sel x `cmp` sel y + \end{code} %************************************************************************ @@ -586,6 +622,8 @@ cmpList cmp (a:as) (b:bs) \end{code} \begin{code} +-- This (with a more general type) is Data.List.stripPrefix from GHC 6.8. +-- This definition can be removed once we require at least 6.8 to build. maybePrefixMatch :: String -> String -> Maybe String maybePrefixMatch [] rest = Just rest maybePrefixMatch (_:_) [] = Nothing