Go back to passing the value to the type-contains function.

This'll make the modification to look at constructors cleaner.
This commit is contained in:
Adam Sampson 2008-04-02 16:11:13 +00:00
parent f515b5ce55
commit bd9c4dae98
2 changed files with 18 additions and 15 deletions

View File

@ -146,8 +146,7 @@ testTypeContains = TestList
Int -> Bool -> a -> b -> Test Int -> Bool -> a -> b -> Test
test n exp start find test n exp start find
= TestCase $ assertEqual ("testTypeContains " ++ show n) = TestCase $ assertEqual ("testTypeContains " ++ show n)
exp (typeContains (typeKey start) exp (containsTypes start [typeKey find])
(typeKey find))
--Returns the list of tests: --Returns the list of tests:
tests :: Test tests :: Test

View File

@ -24,7 +24,7 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
-- added to in the definition of 'contains'). -- added to in the definition of 'contains').
module GenericUtils ( module GenericUtils (
TypeKey, typeKey TypeKey, typeKey
, typeContains , containsTypes
, gmapMFor , gmapMFor
) where ) where
@ -85,10 +85,17 @@ contains = IntMap.fromList [(typeKey t,
where where
allTypes = containedTypes (undefined :: A.AST) allTypes = containedTypes (undefined :: A.AST)
-- | Does one type contain another? -- | Does a value contain any of the listed types?
-- (A type always contains itself.) -- (A value always contains its own type.)
typeContains :: TypeKey -> TypeKey -> Bool containsTypes :: Data t => t -> [TypeKey] -> Bool
typeContains start target containsTypes x targets
= or $ map containsType targets
where
start :: TypeKey
start = typeKey x
containsType :: TypeKey -> Bool
containsType target
| start == target = True | start == target = True
| otherwise = case IntMap.lookup start contains of | otherwise = case IntMap.lookup start contains of
Just set -> target `IntSet.member` set Just set -> target `IntSet.member` set
@ -106,7 +113,4 @@ gmapMFor targets f = gmapM (each f)
each :: (Monad m, Data t) => each :: (Monad m, Data t) =>
(forall s. Data s => s -> m s) -> (t -> m t) (forall s. Data s => s -> m s) -> (t -> m t)
each f x each f x
= if cont then f x else return x = if containsTypes x targets then f x else return x
where
cont = or $ map (typeContains xKey) targets
xKey = typeKey x