Fix scoped type variables for expression type signatures
[ghc-hetmet.git] / compiler / utils / UniqSet.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1998
3 %
4 \section[UniqSet]{Specialised sets, for things with @Uniques@}
5
6 Based on @UniqFMs@ (as you would expect).
7
8 Basically, the things need to be in class @Uniquable@.
9
10 \begin{code}
11 module UniqSet (
12         UniqSet,    -- abstract type: NOT
13
14         mkUniqSet, uniqSetToList, emptyUniqSet, unitUniqSet,
15         addOneToUniqSet, addListToUniqSet, delOneFromUniqSet, delListFromUniqSet,
16         unionUniqSets, unionManyUniqSets, minusUniqSet,
17         elementOfUniqSet, mapUniqSet, intersectUniqSets,
18         isEmptyUniqSet, filterUniqSet, sizeUniqSet, foldUniqSet,
19         elemUniqSet_Directly, lookupUniqSet, hashUniqSet
20     ) where
21
22 #include "HsVersions.h"
23
24 import {-# SOURCE #-} Name ( Name )
25
26 import Maybes           ( maybeToBool )
27 import UniqFM
28 import Unique           ( Unique, Uniquable(..) )
29
30 #if ! OMIT_NATIVE_CODEGEN
31 #define IF_NCG(a) a
32 #else
33 #define IF_NCG(a) {--}
34 #endif
35 \end{code}
36
37 %************************************************************************
38 %*                                                                      *
39 \subsection{The @UniqSet@ type}
40 %*                                                                      *
41 %************************************************************************
42
43 We use @UniqFM@, with a (@getUnique@-able) @Unique@ as ``key''
44 and the thing itself as the ``value'' (for later retrieval).
45
46 \begin{code}
47 --data UniqSet a = MkUniqSet (FiniteMap Unique a) : NOT
48
49 type UniqSet a = UniqFM a
50 #define MkUniqSet {--}
51
52 emptyUniqSet :: UniqSet a
53 emptyUniqSet = MkUniqSet emptyUFM
54
55 unitUniqSet :: Uniquable a => a -> UniqSet a
56 unitUniqSet x = MkUniqSet (unitUFM x x)
57
58 uniqSetToList :: UniqSet a -> [a]
59 uniqSetToList (MkUniqSet set) = eltsUFM set
60
61 foldUniqSet :: (a -> b -> b) -> b -> UniqSet a -> b
62 foldUniqSet k z (MkUniqSet set) = foldUFM k z set 
63
64 mkUniqSet :: Uniquable a => [a]  -> UniqSet a
65 mkUniqSet xs = MkUniqSet (listToUFM [ (x, x) | x <- xs])
66
67 addOneToUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
68 addOneToUniqSet (MkUniqSet set) x = MkUniqSet (addToUFM set x x)
69
70 delOneFromUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
71 delOneFromUniqSet (MkUniqSet set) x = MkUniqSet (delFromUFM set x)
72
73 delListFromUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
74 delListFromUniqSet (MkUniqSet set) xs = MkUniqSet (delListFromUFM set xs)
75
76 addListToUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
77 addListToUniqSet (MkUniqSet set) xs = MkUniqSet (addListToUFM set [(x,x) | x<-xs])
78
79 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
80 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
81
82 unionManyUniqSets :: [UniqSet a] -> UniqSet a
83         -- = foldr unionUniqSets emptyUniqSet ss
84 unionManyUniqSets []     = emptyUniqSet
85 unionManyUniqSets [s]    = s
86 unionManyUniqSets (s:ss) = s `unionUniqSets` unionManyUniqSets ss
87
88 minusUniqSet  :: UniqSet a -> UniqSet a -> UniqSet a
89 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
90
91 filterUniqSet :: (a -> Bool) -> UniqSet a -> UniqSet a
92 filterUniqSet pred (MkUniqSet set) = MkUniqSet (filterUFM pred set)
93
94 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
95 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
96
97 elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
98 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
99
100 lookupUniqSet :: Uniquable a => UniqSet a -> a -> Maybe a
101 lookupUniqSet (MkUniqSet set) x = lookupUFM set x
102
103 elemUniqSet_Directly :: Unique -> UniqSet a -> Bool
104 elemUniqSet_Directly x (MkUniqSet set) = maybeToBool (lookupUFM_Directly set x)
105
106 sizeUniqSet :: UniqSet a -> Int
107 sizeUniqSet (MkUniqSet set) = sizeUFM set
108
109 hashUniqSet :: UniqSet a -> Int
110 hashUniqSet (MkUniqSet set) = hashUFM set
111
112 isEmptyUniqSet :: UniqSet a -> Bool
113 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
114
115 mapUniqSet :: (a -> a) -> UniqSet a -> UniqSet a
116   -- VERY IMPORTANT: *assumes* that the function doesn't change the unique
117 mapUniqSet f (MkUniqSet set) = MkUniqSet (mapUFM f set)
118 \end{code}
119
120 \begin{code}
121 #if __GLASGOW_HASKELL__
122 {-# SPECIALIZE
123     addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique
124     #-}
125 {- SPECIALIZE
126     elementOfUniqSet :: Name -> UniqSet Name -> Bool
127                       , Unique -> UniqSet Unique -> Bool
128     -}
129 {- SPECIALIZE
130     mkUniqSet :: [Name] -> UniqSet Name
131     -}
132
133 {- SPECIALIZE
134     unitUniqSet :: Name -> UniqSet Name
135                  , Unique -> UniqSet Unique
136     -}
137 #endif
138 \end{code}