2 % (c) The AQUA Project, Glasgow University, 1994
4 \section[Set]{An implementation of sets}
6 This new (94/04) implementation of sets sits squarely upon our
7 implementation of @FiniteMaps@. The interface is (roughly?) as
10 See also the @UniqSet@ module (sets of things from which you can
14 #if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)
15 #define OUTPUTABLE_a , Outputable a
17 #define OUTPUTABLE_a {--}
21 #if defined(__GLASGOW_HASKELL__)
22 Set(..), -- abstract type: NOT
24 -- not a synonym so we can make it abstract
28 mkSet, setToList, emptySet, singletonSet,
29 union, unionManySets, minusSet,
33 -- to make the interface self-sufficient
34 #if defined(__GLASGOW_HASKELL__)
35 , FiniteMap -- abstract
38 , intersectFM, minusFM, keysFM, plusFM
43 import Maybes ( maybeToBool
48 #if defined(__GLASGOW_HASKELL__)
49 -- I guess this is here so that our friend USE_ATTACK_PRAGMAS can
50 -- do his job of seeking out and destroying information hiding. ADR
51 import Util --OLD: hiding ( Set(..), emptySet )
54 #if defined(COMPILING_GHC)
60 #if defined(__GLASGOW_HASKELL__)
62 type Set a = FiniteMap a ()
67 -- This can't be a type synonym if you want to use constructor classes.
68 data Set a = MkSet (FiniteMap a ()) {-# STRICT #-}
72 emptySet = MkSet emptyFM
74 singletonSet :: a -> Set a
75 singletonSet x = MkSet (singletonFM x ())
77 setToList :: Set a -> [a]
78 setToList (MkSet set) = keysFM set
80 mkSet :: (Ord a OUTPUTABLE_a) => [a] -> Set a
81 mkSet xs = MkSet (listToFM [ (x, ()) | x <- xs])
83 union :: (Ord a OUTPUTABLE_a) => Set a -> Set a -> Set a
84 union (MkSet set1) (MkSet set2) = MkSet (plusFM set1 set2)
86 unionManySets :: (Ord a OUTPUTABLE_a) => [Set a] -> Set a
87 unionManySets ss = foldr union emptySet ss
89 minusSet :: (Ord a OUTPUTABLE_a) => Set a -> Set a -> Set a
90 minusSet (MkSet set1) (MkSet set2) = MkSet (minusFM set1 set2)
92 intersect :: (Ord a OUTPUTABLE_a) => Set a -> Set a -> Set a
93 intersect (MkSet set1) (MkSet set2) = MkSet (intersectFM set1 set2)
95 elementOf :: (Ord a OUTPUTABLE_a) => a -> Set a -> Bool
96 elementOf x (MkSet set) = maybeToBool(lookupFM set x)
98 isEmptySet :: Set a -> Bool
99 isEmptySet (MkSet set) = sizeFM set == 0
101 mapSet :: (Ord a OUTPUTABLE_a) => (b -> a) -> Set b -> Set a
102 mapSet f (MkSet set) = MkSet (listToFM [ (f key, ()) | key <- keysFM set ])