Stopped makeEquations unnecessarily producing duplicate equations

This commit is contained in:
Neil Brown 2007-12-17 02:22:09 +00:00
parent 5a721fb428
commit 8f1d1368af
3 changed files with 14 additions and 10 deletions

View File

@ -205,10 +205,18 @@ product2 (l0,l1) = [(x0,x1) | x0 <- l0, x1 <- l1]
product3 :: ([a],[b],[c]) -> [(a,b,c)]
product3 (l0,l1,l2) = [(x0,x1,x2) | x0 <- l0, x1 <- l1, x2 <- l2]
-- | Given a triple of lists, produces a list of pairs that is the cartesian product of the three lists.
-- | Given a quadruple of lists, produces a list of pairs that is the cartesian product of the four lists.
product4 :: ([a],[b],[c],[d]) -> [(a,b,c,d)]
product4 (l0,l1,l2,l3) = [(x0,x1,x2,x3) | x0 <- l0, x1 <- l1, x2 <- l2, x3 <- l3]
-- | Given a list, produces all possible distinct pairings of the elements.
-- That is, for each pair returned, (A,B), B will not be the same element as A, and the pair (B,A)
-- will not be in the list. Note that this is not the same as B /= A; if the source list contains
-- two equal items, the returned pairs will feature a pair such that B /= A.
allPairs :: [a] -> [(a,a)]
allPairs [] = []
allPairs (x:xs) = map (\y -> (x,y)) xs ++ allPairs xs
-- | On the basis of a boolean check function, transforms x into Just x if the function returns True;
-- otherwise Nothing is returned.
boolToMaybe :: (a -> Bool) -> a -> Maybe a

View File

@ -134,7 +134,7 @@ makeEquations es high = makeEquations' >>* (\(s,v,lh) -> (s,squareEquations (pai
-- Pairs all possible combinations
pairEqs :: [(Integer,EqualityConstraintEquation)] -> [EqualityConstraintEquation]
pairEqs = filter (any (/= 0) . elems) . map (uncurry pairEqs') . product2 . mkPair
pairEqs = filter (any (/= 0) . elems) . map (uncurry pairEqs') . allPairs
where
pairEqs' (nx,ex) (ny,ey) = arrayZipWith' 0 (-) (amap (* ny) ex) (amap (* nx) ey)

View File

@ -232,26 +232,22 @@ testIndexes = TestList
,TestCase $ assertEquivalentProblems "testIndexes makeEq"
(Map.empty,(uncurry makeConsistent) (dupeEq [con 0 === con 1],leq [con 0,con 0,con 7] &&& leq [con 0,con 1,con 7]))
(Map.empty,(uncurry makeConsistent) ([con 0 === con 1],leq [con 0,con 0,con 7] &&& leq [con 0,con 1,con 7]))
=<< (checkRight $ makeEquations [intLiteral 0, intLiteral 1] (intLiteral 7))
,TestCase $ assertEquivalentProblems "testIndexes makeEq 3"
(Map.singleton "i" 1,(uncurry makeConsistent) (dupeEq [i === con 3],leq [con 0,con 3,con 7] &&& leq [con 0,i,con 7]))
(Map.singleton "i" 1,(uncurry makeConsistent) ([i === con 3],leq [con 0,con 3,con 7] &&& leq [con 0,i,con 7]))
=<< (checkRight $ makeEquations [exprVariable "i",intLiteral 3] (intLiteral 7))
,TestCase $ assertEquivalentProblems "testIndexes makeEq 4"
(Map.fromList [("i",1),("j",2)],(uncurry makeConsistent) (dupeEq [i === j],leq [con 0,i,con 7] &&& leq [con 0,j,con 7]))
(Map.fromList [("i",1),("j",2)],(uncurry makeConsistent) ([i === j],leq [con 0,i,con 7] &&& leq [con 0,j,con 7]))
=<< (checkRight $ makeEquations [exprVariable "i",exprVariable "j"] (intLiteral 7))
,TestCase $ assertEquivalentProblems "testIndexes makeEq 5"
(Map.fromList [("i",2),("j",1)],(uncurry makeConsistent) (dupeEq [i === j],leq [con 0,i,con 7] &&& leq [con 0,j,con 7]))
(Map.fromList [("i",2),("j",1)],(uncurry makeConsistent) ([i === j],leq [con 0,i,con 7] &&& leq [con 0,j,con 7]))
=<< (checkRight $ makeEquations [exprVariable "i",exprVariable "j"] (intLiteral 7))
]
where
-- Duplicates each equation by adding its negation to the list
dupeEq :: [HandyEq] -> [HandyEq]
dupeEq = concatMap (\(Eq e) -> [Eq e,Eq $ negateVars e])
-- Given some indexes using "i", this function checks whether these can
-- ever overlap within the bounds given, and matches this against
-- the expected value; True for safe, False for unsafe.