[commit: ghc] master: Utils: Fix `lengthIs` and `lengthExceeds` for negative args (6e280c2)

git at git.haskell.org git at git.haskell.org
Thu Jul 7 20:34:42 UTC 2016


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/6e280c2c5b2903ae38f4da15a41ea94793907407/ghc

>---------------------------------------------------------------

commit 6e280c2c5b2903ae38f4da15a41ea94793907407
Author: Ömer Sinan Ağacan <omeragacan at gmail.com>
Date:   Thu Jul 7 20:01:47 2016 +0000

    Utils: Fix `lengthIs` and `lengthExceeds` for negative args
    
    Credits goes to SPJ for finding this.


>---------------------------------------------------------------

6e280c2c5b2903ae38f4da15a41ea94793907407
 compiler/utils/Util.hs | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/compiler/utils/Util.hs b/compiler/utils/Util.hs
index ff0f45f..d20a604 100644
--- a/compiler/utils/Util.hs
+++ b/compiler/utils/Util.hs
@@ -443,9 +443,9 @@ atLength :: ([a] -> b)   -- Called when length ls >= n, passed (drop n ls)
          -> [a]
          -> Int
          -> b
-atLength atLenPred atEnd ls n
-  | n < 0     = atLenPred ls
-  | otherwise = go n ls
+atLength atLenPred atEnd ls0 n0
+  | n0 < 0    = atLenPred ls0
+  | otherwise = go n0 ls0
   where
     -- go's first arg n >= 0
     go 0 ls     = atLenPred ls
@@ -454,15 +454,24 @@ atLength atLenPred atEnd ls n
 
 -- Some special cases of atLength:
 
+-- | @(lengthExceeds xs n) = (length xs > n)@
 lengthExceeds :: [a] -> Int -> Bool
--- ^ > (lengthExceeds xs n) = (length xs > n)
-lengthExceeds = atLength notNull False
+lengthExceeds lst n
+  | n < 0
+  = True
+  | otherwise
+  = atLength notNull False lst n
 
 lengthAtLeast :: [a] -> Int -> Bool
 lengthAtLeast = atLength (const True) False
 
+-- | @(lengthIs xs n) = (length xs == n)@
 lengthIs :: [a] -> Int -> Bool
-lengthIs = atLength null False
+lengthIs lst n
+  | n < 0
+  = False
+  | otherwise
+  = atLength null False lst n
 
 listLengthCmp :: [a] -> Int -> Ordering
 listLengthCmp = atLength atLen atEnd



More information about the ghc-commits mailing list