fb5b6cfb4c953744db30f2606b01e820ef7ae06c
[ghc-hetmet.git] / ghc / compiler / basicTypes / VarSet.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section{@VarSet@: Variable sets}
5
6 \begin{code}
7 module VarSet (
8         VarSet, IdSet, TyVarSet, IdOrTyVarSet,
9         emptyVarSet, unitVarSet, mkVarSet,
10         extendVarSet,
11         elemVarSet, varSetElems, subVarSet,
12         unionVarSet, unionVarSets,
13         intersectVarSet, intersectsVarSet,
14         isEmptyVarSet, delVarSet, delVarSetByKey,
15         minusVarSet, foldVarSet, filterVarSet,
16         lookupVarSet, mapVarSet,
17
18         uniqAway
19     ) where
20
21 #include "HsVersions.h"
22
23 import CmdLineOpts      ( opt_PprStyle_Debug )
24 import Var              ( Var, Id, TyVar, IdOrTyVar, setVarUnique )
25 import Unique           ( Unique, Uniquable(..), incrUnique )
26 import UniqSet
27 import UniqFM           ( delFromUFM_Directly )
28 import Outputable
29 \end{code}
30
31 %************************************************************************
32 %*                                                                      *
33 \subsection{@VarSet@s}
34 %*                                                                      *
35 %************************************************************************
36
37 \begin{code}
38 type VarSet       = UniqSet Var
39 type IdSet        = UniqSet Id
40 type TyVarSet     = UniqSet TyVar
41 type IdOrTyVarSet = UniqSet IdOrTyVar
42
43 emptyVarSet     :: VarSet
44 intersectVarSet :: VarSet -> VarSet -> VarSet
45 intersectsVarSet:: VarSet -> VarSet -> Bool     -- True if non-empty intersection
46 unionVarSet     :: VarSet -> VarSet -> VarSet
47 unionVarSets    :: [VarSet] -> VarSet
48 varSetElems     :: VarSet -> [Var]
49 unitVarSet      :: Var -> VarSet
50 extendVarSet    :: VarSet -> Var -> VarSet
51 elemVarSet      :: Var -> VarSet -> Bool
52 delVarSet       :: VarSet -> Var -> VarSet
53 minusVarSet     :: VarSet -> VarSet -> VarSet
54 isEmptyVarSet   :: VarSet -> Bool
55 mkVarSet        :: [Var] -> VarSet
56 foldVarSet      :: (Var -> a -> a) -> a -> VarSet -> a
57 lookupVarSet    :: VarSet -> Var -> Maybe Var
58                         -- Returns the set element, which may be
59                         -- (==) to the argument, but not the same as
60 mapVarSet       :: (Var -> Var) -> VarSet -> VarSet
61 filterVarSet    :: (Var -> Bool) -> VarSet -> VarSet
62 subVarSet       :: VarSet -> VarSet -> Bool
63
64 delVarSetByKey  :: VarSet -> Unique -> VarSet
65
66 emptyVarSet     = emptyUniqSet
67 unitVarSet      = unitUniqSet
68 extendVarSet    = addOneToUniqSet
69 intersectVarSet = intersectUniqSets
70 intersectsVarSet s1 s2 = not (isEmptyVarSet (s1 `intersectVarSet` s2))
71 unionVarSet     = unionUniqSets
72 unionVarSets    = unionManyUniqSets
73 varSetElems     = uniqSetToList
74 elemVarSet      = elementOfUniqSet
75 minusVarSet     = minusUniqSet
76 delVarSet       = delOneFromUniqSet
77 isEmptyVarSet   = isEmptyUniqSet
78 mkVarSet        = mkUniqSet
79 foldVarSet      = foldUniqSet
80 lookupVarSet    = lookupUniqSet
81 mapVarSet       = mapUniqSet
82 filterVarSet    = filterUniqSet
83 a `subVarSet` b = isEmptyVarSet (a `minusVarSet` b)
84 delVarSetByKey  = delFromUFM_Directly   -- Can't be bothered to add this to UniqSet
85 \end{code}
86
87 \begin{code}
88 uniqAway :: VarSet -> Var -> Var
89 -- Give the Var a new unique, different to any in the VarSet
90 uniqAway set var
91   | not (var `elemVarSet` set) = var    -- Nothing to do
92
93   | otherwise
94   = try 1 (incrUnique (getUnique var))
95   where
96     try n uniq | uniq `elemUniqSet_Directly` set = try ((n+1)::Int) (incrUnique uniq)
97 #ifdef DEBUG
98                | opt_PprStyle_Debug && n > 3
99                = pprTrace "uniqAway:" (ppr n <+> text "tries" <+> ppr var) 
100                  setVarUnique var uniq
101 #endif                      
102                | otherwise = setVarUnique var uniq
103 \end{code}