From 3814a039d5541d204e4cb52aef45e64cf9f65259 Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Fri, 14 Dec 2007 15:17:19 +0000 Subject: [PATCH] Refactored the use of mygcd with foldl into one function --- transformations/ArrayUsageCheck.hs | 6 +++++- transformations/RainUsageCheckTest.hs | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/transformations/ArrayUsageCheck.hs b/transformations/ArrayUsageCheck.hs index 3ea1df7..2e69c01 100644 --- a/transformations/ArrayUsageCheck.hs +++ b/transformations/ArrayUsageCheck.hs @@ -86,7 +86,7 @@ solveConstraints p ineq normaliseEq' e | g == 0 = Just e | ((e ! 0) `mod` g) /= 0 = Nothing | otherwise = Just $ amap (\x -> x `div` g) e - where g = foldl1 mygcd (map abs $ tail $ elems e) -- g is the GCD of a_1 .. a_n (not a_0) + where g = mygcdList (tail $ elems e) -- g is the GCD of a_1 .. a_n (not a_0) -- | Solves all equality problems in the given list. -- Will either succeed (Just () in the Error/Maybe monad) or fail (Nothing) @@ -206,6 +206,10 @@ mygcd :: Integer -> Integer -> Integer mygcd 0 0 = 0 mygcd x y = gcd x y +mygcdList :: [Integer] -> Integer +mygcdList [] = 0 +mygcdList [x] = abs x +mygcdList (x:xs) = foldl mygcd x xs -- | Prunes the inequalities. It does what is described in section 2.3 of Pugh's ACM paper; -- it removes redundant inequalities, fails (evaluates to Nothing) if it finds a contradiction diff --git a/transformations/RainUsageCheckTest.hs b/transformations/RainUsageCheckTest.hs index b77779a..8619199 100644 --- a/transformations/RainUsageCheckTest.hs +++ b/transformations/RainUsageCheckTest.hs @@ -502,7 +502,7 @@ testIndexes = TestList -- This is so they can be used as normalised coefficients in a linear equation coprimeList :: Int -> Gen [Integer] coprimeList size = do non_normal <- replicateM size $ choose (1,100) - return $ map (\x -> x `div` (foldl mygcd 0 non_normal)) non_normal + return $ map (\x -> x `div` (mygcdList non_normal)) non_normal -- | Generates a list of lists of co-prime numbers, where each list is distinct. -- The returned list of lists will be square; N equations, each with N items