UniqFM.hs 13.4 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
        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,
51
        delListFromUFM_Directly,
ian@well-typed.com's avatar
ian@well-typed.com committed
52 53
        plusUFM,
        plusUFM_C,
54
        plusUFM_CD,
ian@well-typed.com's avatar
ian@well-typed.com committed
55 56 57
        minusUFM,
        intersectUFM,
        intersectUFM_C,
Jan Stolarek's avatar
Jan Stolarek committed
58
        disjointUFM,
ian@well-typed.com's avatar
ian@well-typed.com committed
59 60 61
        foldUFM, foldUFM_Directly,
        mapUFM, mapUFM_Directly,
        elemUFM, elemUFM_Directly,
62
        filterUFM, filterUFM_Directly, partitionUFM,
ian@well-typed.com's avatar
ian@well-typed.com committed
63 64 65 66 67
        sizeUFM,
        isNullUFM,
        lookupUFM, lookupUFM_Directly,
        lookupWithDefaultUFM, lookupWithDefaultUFM_Directly,
        eltsUFM, keysUFM, splitUFM,
Joachim Breitner's avatar
Joachim Breitner committed
68
        ufmToSet_Directly,
69
        ufmToList, ufmToIntMap,
70
        joinUFM, pprUniqFM, pprUFM, pluralUFM
71 72
    ) where

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 85 86 87
#if __GLASGOW_HASKELL__ > 710
import Data.Semigroup   ( Semigroup )
import qualified Data.Semigroup as Semigroup
#endif
88

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

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

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

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

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

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

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

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

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

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

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

151 152 153 154 155 156 157 158 159 160 161
-- | `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 }
-- @
162 163 164
plusUFM_CD      :: (elt -> elt -> elt)
                -> UniqFM elt -> elt -> UniqFM elt -> elt -> UniqFM elt

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

ian@well-typed.com's avatar
ian@well-typed.com committed
167 168 169
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
170
disjointUFM     :: UniqFM elt1 -> UniqFM elt2 -> Bool
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
171

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

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

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

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

207 208 209 210 211
#if __GLASGOW_HASKELL__ > 710
instance Semigroup (UniqFM a) where
  (<>) = plusUFM
#endif

Gergő Érdi's avatar
Gergő Érdi committed
212 213 214 215
instance Monoid (UniqFM a) where
    mempty = emptyUFM
    mappend = plusUFM

Austin Seipp's avatar
Austin Seipp committed
216 217 218
{-
************************************************************************
*                                                                      *
219
\subsection{Implementation using ``Data.IntMap''}
Austin Seipp's avatar
Austin Seipp committed
220 221 222
*                                                                      *
************************************************************************
-}
223

224
newtype UniqFM ele = UFM (M.IntMap ele)
225
  deriving (Data, Eq, Functor, Traversable.Traversable,
226
            Typeable)
227

228 229
deriving instance Foldable.Foldable UniqFM

230 231 232 233 234 235 236 237
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

238 239
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)
240 241 242 243 244 245 246 247 248 249 250
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)

251 252 253
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)

254 255 256
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)
257
delListFromUFM_Directly = foldl delFromUFM_Directly
258 259 260

-- M.union is left-biased, plusUFM should be right-biased.
plusUFM (UFM x) (UFM y) = UFM (M.union y x)
261 262 263
     -- Note (M.union y x), with arguments flipped
     -- M.union is left-biased, plusUFM should be right-biased.

264
plusUFM_C f (UFM x) (UFM y) = UFM (M.unionWith f x y)
265 266 267 268 269 270 271

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
272 273 274
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
275
disjointUFM (UFM x) (UFM y) = M.null (M.intersection x y)
276 277 278 279

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
280
mapUFM_Directly f (UFM m) = UFM (M.mapWithKey (f . getUnique) m)
281 282
filterUFM p (UFM m) = UFM (M.filter p m)
filterUFM_Directly p (UFM m) = UFM (M.filterWithKey (p . getUnique) m)
283 284
partitionUFM p (UFM m) = case M.partition p m of
                           (left, right) -> (UFM left, UFM right)
285 286 287 288 289 290 291 292 293 294 295 296 297

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
298
ufmToSet_Directly (UFM m) = M.keysSet m
299
ufmToList (UFM m) = map (\(k, v) -> (getUnique k, v)) $ M.toList m
300

301 302 303
ufmToIntMap :: UniqFM elt -> M.IntMap elt
ufmToIntMap (UFM m) = m

304 305 306 307 308 309 310 311 312 313
-- 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
314 315 316
{-
************************************************************************
*                                                                      *
317
\subsection{Output-ery}
Austin Seipp's avatar
Austin Seipp committed
318 319 320
*                                                                      *
************************************************************************
-}
321

322
instance Outputable a => Outputable (UniqFM a) where
Simon Peyton Jones's avatar
Simon Peyton Jones committed
323 324 325 326 327
    ppr ufm = pprUniqFM ppr ufm

pprUniqFM :: (a -> SDoc) -> UniqFM a -> SDoc
pprUniqFM ppr_elt ufm
  = brackets $ fsep $ punctuate comma $
328
    [ ppr uq <+> text ":->" <+> ppr_elt elt
Simon Peyton Jones's avatar
Simon Peyton Jones committed
329
    | (uq, elt) <- ufmToList ufm ]
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

-- | Pretty-print a non-deterministic set.
-- The order of variables is non-deterministic and for pretty-printing that
-- shouldn't be a problem.
-- Having this function helps contain the non-determinism created with
-- eltsUFM.
pprUFM :: ([a] -> SDoc) -- ^ The pretty printing function to use on the elements
       -> UniqFM a      -- ^ The things to be pretty printed
       -> SDoc          -- ^ 'SDoc' where the things have been pretty
                        -- printed
pprUFM pp ufm = pp (eltsUFM ufm)

-- | Determines the pluralisation suffix appropriate for the length of a set
-- in the same way that plural from Outputable does for lists.
pluralUFM :: UniqFM a -> SDoc
pluralUFM ufm
  | sizeUFM ufm == 1 = empty
  | otherwise = char 's'