UniqFM.hs 12.3 KB
Newer Older
Austin Seipp's avatar
Austin Seipp committed
1 2 3 4
{-
(c) The University of Glasgow 2006
(c) The AQUA Project, Glasgow University, 1994-1998

Simon Marlow's avatar
Simon Marlow committed
5

6
UniqFM: Specialised finite maps, for things with @Uniques@.
7

8
Basically, the things need to be in class @Uniquable@, and we use the
9
@getUnique@ method to grab their @Uniques@.
10 11 12

(A similar thing to @UniqSet@, as opposed to @Set@.)

13
The interface is based on @FiniteMap@s, but the implementation uses
ian@well-typed.com's avatar
ian@well-typed.com committed
14
@Data.IntMap@, which is both maintained and faster than the past
15 16 17 18 19 20
implementation (see commit log).

The @UniqFM@ interface maps directly to Data.IntMap, only
``Data.IntMap.union'' is left-biased and ``plusUFM'' right-biased
and ``addToUFM\_C'' and ``Data.IntMap.insertWith'' differ in the order
of arguments of combining function.
Austin Seipp's avatar
Austin Seipp committed
21
-}
22

23 24 25
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveTraversable #-}
26
{-# LANGUAGE FlexibleContexts #-}
27
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
28
{-# LANGUAGE StandaloneDeriving #-}
29
{-# OPTIONS_GHC -Wall #-}
30

31
module UniqFM (
ian@well-typed.com's avatar
ian@well-typed.com committed
32 33
        -- * Unique-keyed mappings
        UniqFM,       -- abstract type
34

35
        -- ** Manipulating those mappings
ian@well-typed.com's avatar
ian@well-typed.com committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        emptyUFM,
        unitUFM,
        unitDirectlyUFM,
        listToUFM,
        listToUFM_Directly,
        listToUFM_C,
        addToUFM,addToUFM_C,addToUFM_Acc,
        addListToUFM,addListToUFM_C,
        addToUFM_Directly,
        addListToUFM_Directly,
        adjustUFM, alterUFM,
        adjustUFM_Directly,
        delFromUFM,
        delFromUFM_Directly,
        delListFromUFM,
        plusUFM,
        plusUFM_C,
53
        plusUFM_CD,
ian@well-typed.com's avatar
ian@well-typed.com committed
54 55 56
        minusUFM,
        intersectUFM,
        intersectUFM_C,
Jan Stolarek's avatar
Jan Stolarek committed
57
        disjointUFM,
ian@well-typed.com's avatar
ian@well-typed.com committed
58 59 60
        foldUFM, foldUFM_Directly,
        mapUFM, mapUFM_Directly,
        elemUFM, elemUFM_Directly,
61
        filterUFM, filterUFM_Directly, partitionUFM,
ian@well-typed.com's avatar
ian@well-typed.com committed
62 63 64 65 66
        sizeUFM,
        isNullUFM,
        lookupUFM, lookupUFM_Directly,
        lookupWithDefaultUFM, lookupWithDefaultUFM_Directly,
        eltsUFM, keysUFM, splitUFM,
Joachim Breitner's avatar
Joachim Breitner committed
67
        ufmToSet_Directly,
ian@well-typed.com's avatar
ian@well-typed.com committed
68
        ufmToList,
Simon Peyton Jones's avatar
Simon Peyton Jones committed
69
        joinUFM, pprUniqFM
70 71
    ) where

Simon Peyton Jones's avatar
Simon Peyton Jones committed
72
import FastString
73
import Unique           ( Uniquable(..), Unique, getKey )
74
import Outputable
75

76 77
import Compiler.Hoopl   hiding (Unique)

78
import qualified Data.IntMap as M
Joachim Breitner's avatar
Joachim Breitner committed
79
import qualified Data.IntSet as S
80
import qualified Data.Foldable as Foldable
dimitris's avatar
dimitris committed
81
import qualified Data.Traversable as Traversable
82 83
import Data.Typeable
import Data.Data
84
#if __GLASGOW_HASKELL__ < 709
Gergő Érdi's avatar
Gergő Érdi committed
85
import Data.Monoid
86
#endif
87

Austin Seipp's avatar
Austin Seipp committed
88 89 90
{-
************************************************************************
*                                                                      *
91
\subsection{The signature of the module}
Austin Seipp's avatar
Austin Seipp committed
92 93 94
*                                                                      *
************************************************************************
-}
95

ian@well-typed.com's avatar
ian@well-typed.com committed
96 97 98
emptyUFM        :: UniqFM elt
isNullUFM       :: UniqFM elt -> Bool
unitUFM         :: Uniquable key => key -> elt -> UniqFM elt
99
unitDirectlyUFM -- got the Unique already
ian@well-typed.com's avatar
ian@well-typed.com committed
100 101
                :: Unique -> elt -> UniqFM elt
listToUFM       :: Uniquable key => [(key,elt)] -> UniqFM elt
102
listToUFM_Directly
ian@well-typed.com's avatar
ian@well-typed.com committed
103 104 105
                :: [(Unique, elt)] -> UniqFM elt
listToUFM_C     :: Uniquable key => (elt -> elt -> elt)
                           -> [(key, elt)]
106
                           -> UniqFM elt
107

ian@well-typed.com's avatar
ian@well-typed.com committed
108 109
addToUFM        :: Uniquable key => UniqFM elt -> key -> elt  -> UniqFM elt
addListToUFM    :: Uniquable key => UniqFM elt -> [(key,elt)] -> UniqFM elt
110
addListToUFM_Directly :: UniqFM elt -> [(Unique,elt)] -> UniqFM elt
111
addToUFM_Directly
ian@well-typed.com's avatar
ian@well-typed.com committed
112
                :: UniqFM elt -> Unique -> elt -> UniqFM elt
113

ian@well-typed.com's avatar
ian@well-typed.com committed
114 115 116 117
addToUFM_C      :: Uniquable key => (elt -> elt -> elt) -- old -> new -> result
                           -> UniqFM elt                -- old
                           -> key -> elt                -- new
                           -> UniqFM elt                -- result
118

ian@well-typed.com's avatar
ian@well-typed.com committed
119 120 121 122 123 124
addToUFM_Acc    :: Uniquable key =>
                              (elt -> elts -> elts)     -- Add to existing
                           -> (elt -> elts)             -- New element
                           -> UniqFM elts               -- old
                           -> key -> elt                -- new
                           -> UniqFM elts               -- result
125

ian@well-typed.com's avatar
ian@well-typed.com committed
126
alterUFM        :: Uniquable key =>
127
                              (Maybe elt -> Maybe elt)  -- How to adjust
ian@well-typed.com's avatar
ian@well-typed.com committed
128 129 130
                           -> UniqFM elt                -- old
                           -> key                       -- new
                           -> UniqFM elt                -- result
131

ian@well-typed.com's avatar
ian@well-typed.com committed
132 133 134
addListToUFM_C  :: Uniquable key => (elt -> elt -> elt)
                           -> UniqFM elt -> [(key,elt)]
                           -> UniqFM elt
135

ian@well-typed.com's avatar
ian@well-typed.com committed
136
adjustUFM       :: Uniquable key => (elt -> elt) -> UniqFM elt -> key -> UniqFM elt
137 138
adjustUFM_Directly :: (elt -> elt) -> UniqFM elt -> Unique -> UniqFM elt

ian@well-typed.com's avatar
ian@well-typed.com committed
139 140
delFromUFM      :: Uniquable key => UniqFM elt -> key    -> UniqFM elt
delListFromUFM  :: Uniquable key => UniqFM elt -> [key] -> UniqFM elt
141
delFromUFM_Directly :: UniqFM elt -> Unique -> UniqFM elt
142

143
-- Bindings in right argument shadow those in the left
ian@well-typed.com's avatar
ian@well-typed.com committed
144
plusUFM         :: UniqFM elt -> UniqFM elt -> UniqFM elt
145

ian@well-typed.com's avatar
ian@well-typed.com committed
146 147
plusUFM_C       :: (elt -> elt -> elt)
                -> UniqFM elt -> UniqFM elt -> UniqFM elt
148

149 150 151 152 153 154 155 156 157 158 159
-- | `plusUFM_CD f m1 d1 m2 d2` merges the maps using `f` as the
-- combinding function and `d1` resp. `d2` as the default value if
-- there is no entry in `m1` reps. `m2`. The domain is the union of
-- the domains of `m1` and `m2`.
--
-- Representative example:
--
-- @
-- plusUFM_CD f {A: 1, B: 2} 23 {B: 3, C: 4} 42
--    == {A: f 1 42, B: f 2 3, C: f 23 4 }
-- @
160 161 162
plusUFM_CD      :: (elt -> elt -> elt)
                -> UniqFM elt -> elt -> UniqFM elt -> elt -> UniqFM elt

ian@well-typed.com's avatar
ian@well-typed.com committed
163
minusUFM        :: UniqFM elt1 -> UniqFM elt2 -> UniqFM elt1
164

ian@well-typed.com's avatar
ian@well-typed.com committed
165 166 167
intersectUFM    :: UniqFM elt -> UniqFM elt -> UniqFM elt
intersectUFM_C  :: (elt1 -> elt2 -> elt3)
                -> UniqFM elt1 -> UniqFM elt2 -> UniqFM elt3
Jan Stolarek's avatar
Jan Stolarek committed
168
disjointUFM     :: UniqFM elt1 -> UniqFM elt2 -> Bool
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
169

ian@well-typed.com's avatar
ian@well-typed.com committed
170
foldUFM         :: (elt -> a -> a) -> a -> UniqFM elt -> a
171
foldUFM_Directly:: (Unique -> elt -> a -> a) -> a -> UniqFM elt -> a
ian@well-typed.com's avatar
ian@well-typed.com committed
172
mapUFM          :: (elt1 -> elt2) -> UniqFM elt1 -> UniqFM elt2
Edward Z. Yang's avatar
Edward Z. Yang committed
173
mapUFM_Directly :: (Unique -> elt1 -> elt2) -> UniqFM elt1 -> UniqFM elt2
ian@well-typed.com's avatar
ian@well-typed.com committed
174
filterUFM       :: (elt -> Bool) -> UniqFM elt -> UniqFM elt
175
filterUFM_Directly :: (Unique -> elt -> Bool) -> UniqFM elt -> UniqFM elt
176
partitionUFM    :: (elt -> Bool) -> UniqFM elt -> (UniqFM elt, UniqFM elt)
177

ian@well-typed.com's avatar
ian@well-typed.com committed
178 179 180
sizeUFM         :: UniqFM elt -> Int
--hashUFM               :: UniqFM elt -> Int
elemUFM         :: Uniquable key => key -> UniqFM elt -> Bool
181
elemUFM_Directly:: Unique -> UniqFM elt -> Bool
182

183
splitUFM        :: Uniquable key => UniqFM elt -> key -> (UniqFM elt, Maybe elt, UniqFM elt)
ian@well-typed.com's avatar
ian@well-typed.com committed
184 185
                   -- Splits a UFM into things less than, equal to, and greater than the key
lookupUFM       :: Uniquable key => UniqFM elt -> key -> Maybe elt
186
lookupUFM_Directly  -- when you've got the Unique already
ian@well-typed.com's avatar
ian@well-typed.com committed
187
                :: UniqFM elt -> Unique -> Maybe elt
188
lookupWithDefaultUFM
ian@well-typed.com's avatar
ian@well-typed.com committed
189
                :: Uniquable key => UniqFM elt -> elt -> key -> elt
190
lookupWithDefaultUFM_Directly
ian@well-typed.com's avatar
ian@well-typed.com committed
191 192 193
                :: UniqFM elt -> elt -> Unique -> elt
keysUFM         :: UniqFM elt -> [Unique]       -- Get the keys
eltsUFM         :: UniqFM elt -> [elt]
Joachim Breitner's avatar
Joachim Breitner committed
194
ufmToSet_Directly :: UniqFM elt -> S.IntSet
ian@well-typed.com's avatar
ian@well-typed.com committed
195
ufmToList       :: UniqFM elt -> [(Unique, elt)]
196

Austin Seipp's avatar
Austin Seipp committed
197 198 199
{-
************************************************************************
*                                                                      *
Gergő Érdi's avatar
Gergő Érdi committed
200
\subsection{Monoid interface}
Austin Seipp's avatar
Austin Seipp committed
201 202 203
*                                                                      *
************************************************************************
-}
Gergő Érdi's avatar
Gergő Érdi committed
204 205 206 207 208

instance Monoid (UniqFM a) where
    mempty = emptyUFM
    mappend = plusUFM

Austin Seipp's avatar
Austin Seipp committed
209 210 211
{-
************************************************************************
*                                                                      *
212
\subsection{Implementation using ``Data.IntMap''}
Austin Seipp's avatar
Austin Seipp committed
213 214 215
*                                                                      *
************************************************************************
-}
216

217
newtype UniqFM ele = UFM (M.IntMap ele)
218
  deriving (Data, Eq, Functor, Traversable.Traversable,
219
            Typeable)
220

221 222
deriving instance Foldable.Foldable UniqFM

223 224 225 226 227 228 229 230
emptyUFM = UFM M.empty
isNullUFM (UFM m) = M.null m
unitUFM k v = UFM (M.singleton (getKey $ getUnique k) v)
unitDirectlyUFM u v = UFM (M.singleton (getKey u) v)
listToUFM = foldl (\m (k, v) -> addToUFM m k v) emptyUFM
listToUFM_Directly = foldl (\m (u, v) -> addToUFM_Directly m u v) emptyUFM
listToUFM_C f = foldl (\m (k, v) -> addToUFM_C f m k v) emptyUFM

231 232
alterUFM f (UFM m) k = UFM (M.alter f (getKey $ getUnique k) m)
addToUFM (UFM m) k v   = UFM (M.insert (getKey $ getUnique k) v m)
233 234 235 236 237 238 239 240 241 242 243
addListToUFM = foldl (\m (k, v) -> addToUFM m k v)
addListToUFM_Directly = foldl (\m (k, v) -> addToUFM_Directly m k v)
addToUFM_Directly (UFM m) u v = UFM (M.insert (getKey u) v m)

-- Arguments of combining function of M.insertWith and addToUFM_C are flipped.
addToUFM_C f (UFM m) k v =
  UFM (M.insertWith (flip f) (getKey $ getUnique k) v m)
addToUFM_Acc exi new (UFM m) k v =
  UFM (M.insertWith (\_new old -> exi v old) (getKey $ getUnique k) (new v) m)
addListToUFM_C f = foldl (\m (k, v) -> addToUFM_C f m k v)

244 245 246
adjustUFM f (UFM m) k = UFM (M.adjust f (getKey $ getUnique k) m)
adjustUFM_Directly f (UFM m) u = UFM (M.adjust f (getKey u) m)

247 248 249 250 251 252
delFromUFM (UFM m) k = UFM (M.delete (getKey $ getUnique k) m)
delListFromUFM = foldl delFromUFM
delFromUFM_Directly (UFM m) u = UFM (M.delete (getKey u) m)

-- M.union is left-biased, plusUFM should be right-biased.
plusUFM (UFM x) (UFM y) = UFM (M.union y x)
253 254 255
     -- Note (M.union y x), with arguments flipped
     -- M.union is left-biased, plusUFM should be right-biased.

256
plusUFM_C f (UFM x) (UFM y) = UFM (M.unionWith f x y)
257 258 259 260 261 262 263

plusUFM_CD f (UFM xm) dx (UFM ym) dy
    = UFM $ M.mergeWithKey
        (\_ x y -> Just (x `f` y))
        (M.map (\x -> x `f` dy))
        (M.map (\y -> dx `f` y))
        xm ym
264 265 266
minusUFM (UFM x) (UFM y) = UFM (M.difference x y)
intersectUFM (UFM x) (UFM y) = UFM (M.intersection x y)
intersectUFM_C f (UFM x) (UFM y) = UFM (M.intersectionWith f x y)
Jan Stolarek's avatar
Jan Stolarek committed
267
disjointUFM (UFM x) (UFM y) = M.null (M.intersection x y)
268 269 270 271

foldUFM k z (UFM m) = M.fold k z m
foldUFM_Directly k z (UFM m) = M.foldWithKey (k . getUnique) z m
mapUFM f (UFM m) = UFM (M.map f m)
Edward Z. Yang's avatar
Edward Z. Yang committed
272
mapUFM_Directly f (UFM m) = UFM (M.mapWithKey (f . getUnique) m)
273 274
filterUFM p (UFM m) = UFM (M.filter p m)
filterUFM_Directly p (UFM m) = UFM (M.filterWithKey (p . getUnique) m)
275 276
partitionUFM p (UFM m) = case M.partition p m of
                           (left, right) -> (UFM left, UFM right)
277 278 279 280 281 282 283 284 285 286 287 288 289

sizeUFM (UFM m) = M.size m
elemUFM k (UFM m) = M.member (getKey $ getUnique k) m
elemUFM_Directly u (UFM m) = M.member (getKey u) m

splitUFM (UFM m) k = case M.splitLookup (getKey $ getUnique k) m of
                       (less, equal, greater) -> (UFM less, equal, UFM greater)
lookupUFM (UFM m) k = M.lookup (getKey $ getUnique k) m
lookupUFM_Directly (UFM m) u = M.lookup (getKey u) m
lookupWithDefaultUFM (UFM m) v k = M.findWithDefault v (getKey $ getUnique k) m
lookupWithDefaultUFM_Directly (UFM m) v u = M.findWithDefault v (getKey u) m
keysUFM (UFM m) = map getUnique $ M.keys m
eltsUFM (UFM m) = M.elems m
Joachim Breitner's avatar
Joachim Breitner committed
290
ufmToSet_Directly (UFM m) = M.keysSet m
291
ufmToList (UFM m) = map (\(k, v) -> (getUnique k, v)) $ M.toList m
292

293 294 295 296 297 298 299 300 301 302
-- Hoopl
joinUFM :: JoinFun v -> JoinFun (UniqFM v)
joinUFM eltJoin l (OldFact old) (NewFact new) = foldUFM_Directly add (NoChange, old) new
    where add k new_v (ch, joinmap) =
            case lookupUFM_Directly joinmap k of
                Nothing -> (SomeChange, addToUFM_Directly joinmap k new_v)
                Just old_v -> case eltJoin l (OldFact old_v) (NewFact new_v) of
                                (SomeChange, v') -> (SomeChange, addToUFM_Directly joinmap k v')
                                (NoChange, _) -> (ch, joinmap)

Austin Seipp's avatar
Austin Seipp committed
303 304 305
{-
************************************************************************
*                                                                      *
306
\subsection{Output-ery}
Austin Seipp's avatar
Austin Seipp committed
307 308 309
*                                                                      *
************************************************************************
-}
310

311
instance Outputable a => Outputable (UniqFM a) where
Simon Peyton Jones's avatar
Simon Peyton Jones committed
312 313 314 315 316 317 318
    ppr ufm = pprUniqFM ppr ufm

pprUniqFM :: (a -> SDoc) -> UniqFM a -> SDoc
pprUniqFM ppr_elt ufm
  = brackets $ fsep $ punctuate comma $
    [ ppr uq <+> ptext (sLit ":->") <+> ppr_elt elt
    | (uq, elt) <- ufmToList ufm ]