diff options
Diffstat (limited to 'compiler/utils')
-rw-r--r-- | compiler/utils/ListSetOps.hs | 2 | ||||
-rw-r--r-- | compiler/utils/Util.hs | 38 |
2 files changed, 37 insertions, 3 deletions
diff --git a/compiler/utils/ListSetOps.hs b/compiler/utils/ListSetOps.hs index 88af48e41a..f1aa2c3755 100644 --- a/compiler/utils/ListSetOps.hs +++ b/compiler/utils/ListSetOps.hs @@ -46,7 +46,7 @@ getNth xs n = ASSERT2( xs `lengthExceeds` n, ppr n $$ ppr xs ) unionLists :: (Outputable a, Eq a) => [a] -> [a] -> [a] -- Assumes that the arguments contain no duplicates unionLists xs ys - = WARN(length xs > 100 || length ys > 100, ppr xs $$ ppr ys) + = WARN(lengthExceeds xs 100 || lengthExceeds ys 100, ppr xs $$ ppr ys) [x | x <- xs, isn'tIn "unionLists" x ys] ++ ys -- | Calculate the set difference of two lists. This is diff --git a/compiler/utils/Util.hs b/compiler/utils/Util.hs index a4bc8d4653..35a6340fd4 100644 --- a/compiler/utils/Util.hs +++ b/compiler/utils/Util.hs @@ -36,9 +36,10 @@ module Util ( foldl1', foldl2, count, all2, - lengthExceeds, lengthIs, lengthAtLeast, + lengthExceeds, lengthIs, lengthIsNot, + lengthAtLeast, lengthAtMost, lengthLessThan, listLengthCmp, atLength, - equalLength, compareLength, leLength, + equalLength, neLength, compareLength, leLength, ltLength, isSingleton, only, singleton, notNull, snocView, @@ -494,6 +495,7 @@ lengthExceeds lst n | otherwise = atLength notNull False lst n +-- | @(lengthAtLeast xs n) = (length xs >= n)@ lengthAtLeast :: [a] -> Int -> Bool lengthAtLeast = atLength (const True) False @@ -505,6 +507,24 @@ lengthIs lst n | otherwise = atLength null False lst n +-- | @(lengthIsNot xs n) = (length xs /= n)@ +lengthIsNot :: [a] -> Int -> Bool +lengthIsNot lst n + | n < 0 = True + | otherwise = atLength notNull True lst n + +-- | @(lengthAtMost xs n) = (length xs <= n)@ +lengthAtMost :: [a] -> Int -> Bool +lengthAtMost lst n + | n < 0 + = False + | otherwise + = atLength null True lst n + +-- | @(lengthLessThan xs n) == (length xs < n)@ +lengthLessThan :: [a] -> Int -> Bool +lengthLessThan = atLength (const False) True + listLengthCmp :: [a] -> Int -> Ordering listLengthCmp = atLength atLen atEnd where @@ -514,10 +534,17 @@ listLengthCmp = atLength atLen atEnd atLen _ = GT equalLength :: [a] -> [b] -> Bool +-- ^ True if length xs == length ys equalLength [] [] = True equalLength (_:xs) (_:ys) = equalLength xs ys equalLength _ _ = False +neLength :: [a] -> [b] -> Bool +-- ^ True if length xs /= length ys +neLength [] [] = False +neLength (_:xs) (_:ys) = neLength xs ys +neLength _ _ = True + compareLength :: [a] -> [b] -> Ordering compareLength [] [] = EQ compareLength (_:xs) (_:ys) = compareLength xs ys @@ -531,6 +558,13 @@ leLength xs ys = case compareLength xs ys of EQ -> True GT -> False +ltLength :: [a] -> [b] -> Bool +-- ^ True if length xs < length ys +ltLength xs ys = case compareLength xs ys of + LT -> True + EQ -> False + GT -> False + ---------------------------- singleton :: a -> [a] singleton x = [x] |