Implement equalKeysUFM the right way
[ghc.git] / compiler / utils / UniqFM.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The AQUA Project, Glasgow University, 1994-1998
4
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 (A similar thing to @UniqSet@, as opposed to @Set@.)
12
13 The interface is based on @FiniteMap@s, but the implementation uses
14 @Data.IntMap@, which is both maintained and faster than the past
15 implementation (see commit log).
16
17 The @UniqFM@ interface maps directly to Data.IntMap, only
18 ``Data.IntMap.union'' is left-biased and ``plusUFM'' right-biased
19 and ``addToUFM\_C'' and ``Data.IntMap.insertWith'' differ in the order
20 of arguments of combining function.
21 -}
22
23 {-# LANGUAGE DeriveDataTypeable #-}
24 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
25 {-# OPTIONS_GHC -Wall #-}
26
27 module UniqFM (
28 -- * Unique-keyed mappings
29 UniqFM, -- abstract type
30
31 -- ** Manipulating those mappings
32 emptyUFM,
33 unitUFM,
34 unitDirectlyUFM,
35 listToUFM,
36 listToUFM_Directly,
37 listToUFM_C,
38 addToUFM,addToUFM_C,addToUFM_Acc,
39 addListToUFM,addListToUFM_C,
40 addToUFM_Directly,
41 addListToUFM_Directly,
42 adjustUFM, alterUFM,
43 adjustUFM_Directly,
44 delFromUFM,
45 delFromUFM_Directly,
46 delListFromUFM,
47 delListFromUFM_Directly,
48 plusUFM,
49 plusUFM_C,
50 plusUFM_CD,
51 plusMaybeUFM_C,
52 plusUFMList,
53 minusUFM,
54 intersectUFM,
55 intersectUFM_C,
56 disjointUFM,
57 equalKeysUFM,
58 nonDetFoldUFM, foldUFM, nonDetFoldUFM_Directly,
59 anyUFM, allUFM, seqEltsUFM,
60 mapUFM, mapUFM_Directly,
61 elemUFM, elemUFM_Directly,
62 filterUFM, filterUFM_Directly, partitionUFM,
63 sizeUFM,
64 isNullUFM,
65 lookupUFM, lookupUFM_Directly,
66 lookupWithDefaultUFM, lookupWithDefaultUFM_Directly,
67 nonDetEltsUFM, eltsUFM, nonDetKeysUFM,
68 ufmToSet_Directly,
69 nonDetUFMToList, ufmToIntMap,
70 pprUniqFM, pprUFM, pprUFMWithKeys, pluralUFM
71 ) where
72
73 import GhcPrelude
74
75 import Unique ( Uniquable(..), Unique, getKey )
76 import Outputable
77
78 import Data.List (foldl')
79
80 import qualified Data.IntMap as M
81 import qualified Data.IntSet as S
82 import Data.Data
83 import qualified Data.Semigroup as Semi
84 import Data.Functor.Classes (Eq1 (..))
85
86
87 newtype UniqFM ele = UFM (M.IntMap ele)
88 deriving (Data, Eq, Functor)
89 -- We used to derive Traversable and Foldable, but they were nondeterministic
90 -- and not obvious at the call site. You can use explicit nonDetEltsUFM
91 -- and fold a list if needed.
92 -- See Note [Deterministic UniqFM] in UniqDFM to learn about determinism.
93
94 emptyUFM :: UniqFM elt
95 emptyUFM = UFM M.empty
96
97 isNullUFM :: UniqFM elt -> Bool
98 isNullUFM (UFM m) = M.null m
99
100 unitUFM :: Uniquable key => key -> elt -> UniqFM elt
101 unitUFM k v = UFM (M.singleton (getKey $ getUnique k) v)
102
103 -- when you've got the Unique already
104 unitDirectlyUFM :: Unique -> elt -> UniqFM elt
105 unitDirectlyUFM u v = UFM (M.singleton (getKey u) v)
106
107 listToUFM :: Uniquable key => [(key,elt)] -> UniqFM elt
108 listToUFM = foldl (\m (k, v) -> addToUFM m k v) emptyUFM
109
110 listToUFM_Directly :: [(Unique, elt)] -> UniqFM elt
111 listToUFM_Directly = foldl (\m (u, v) -> addToUFM_Directly m u v) emptyUFM
112
113 listToUFM_C
114 :: Uniquable key
115 => (elt -> elt -> elt)
116 -> [(key, elt)]
117 -> UniqFM elt
118 listToUFM_C f = foldl (\m (k, v) -> addToUFM_C f m k v) emptyUFM
119
120 addToUFM :: Uniquable key => UniqFM elt -> key -> elt -> UniqFM elt
121 addToUFM (UFM m) k v = UFM (M.insert (getKey $ getUnique k) v m)
122
123 addListToUFM :: Uniquable key => UniqFM elt -> [(key,elt)] -> UniqFM elt
124 addListToUFM = foldl (\m (k, v) -> addToUFM m k v)
125
126 addListToUFM_Directly :: UniqFM elt -> [(Unique,elt)] -> UniqFM elt
127 addListToUFM_Directly = foldl (\m (k, v) -> addToUFM_Directly m k v)
128
129 addToUFM_Directly :: UniqFM elt -> Unique -> elt -> UniqFM elt
130 addToUFM_Directly (UFM m) u v = UFM (M.insert (getKey u) v m)
131
132 addToUFM_C
133 :: Uniquable key
134 => (elt -> elt -> elt) -- old -> new -> result
135 -> UniqFM elt -- old
136 -> key -> elt -- new
137 -> UniqFM elt -- result
138 -- Arguments of combining function of M.insertWith and addToUFM_C are flipped.
139 addToUFM_C f (UFM m) k v =
140 UFM (M.insertWith (flip f) (getKey $ getUnique k) v m)
141
142 addToUFM_Acc
143 :: Uniquable key
144 => (elt -> elts -> elts) -- Add to existing
145 -> (elt -> elts) -- New element
146 -> UniqFM elts -- old
147 -> key -> elt -- new
148 -> UniqFM elts -- result
149 addToUFM_Acc exi new (UFM m) k v =
150 UFM (M.insertWith (\_new old -> exi v old) (getKey $ getUnique k) (new v) m)
151
152 alterUFM
153 :: Uniquable key
154 => (Maybe elt -> Maybe elt) -- How to adjust
155 -> UniqFM elt -- old
156 -> key -- new
157 -> UniqFM elt -- result
158 alterUFM f (UFM m) k = UFM (M.alter f (getKey $ getUnique k) m)
159
160 addListToUFM_C
161 :: Uniquable key
162 => (elt -> elt -> elt)
163 -> UniqFM elt -> [(key,elt)]
164 -> UniqFM elt
165 addListToUFM_C f = foldl (\m (k, v) -> addToUFM_C f m k v)
166
167 adjustUFM :: Uniquable key => (elt -> elt) -> UniqFM elt -> key -> UniqFM elt
168 adjustUFM f (UFM m) k = UFM (M.adjust f (getKey $ getUnique k) m)
169
170 adjustUFM_Directly :: (elt -> elt) -> UniqFM elt -> Unique -> UniqFM elt
171 adjustUFM_Directly f (UFM m) u = UFM (M.adjust f (getKey u) m)
172
173 delFromUFM :: Uniquable key => UniqFM elt -> key -> UniqFM elt
174 delFromUFM (UFM m) k = UFM (M.delete (getKey $ getUnique k) m)
175
176 delListFromUFM :: Uniquable key => UniqFM elt -> [key] -> UniqFM elt
177 delListFromUFM = foldl delFromUFM
178
179 delListFromUFM_Directly :: UniqFM elt -> [Unique] -> UniqFM elt
180 delListFromUFM_Directly = foldl delFromUFM_Directly
181
182 delFromUFM_Directly :: UniqFM elt -> Unique -> UniqFM elt
183 delFromUFM_Directly (UFM m) u = UFM (M.delete (getKey u) m)
184
185 -- Bindings in right argument shadow those in the left
186 plusUFM :: UniqFM elt -> UniqFM elt -> UniqFM elt
187 -- M.union is left-biased, plusUFM should be right-biased.
188 plusUFM (UFM x) (UFM y) = UFM (M.union y x)
189 -- Note (M.union y x), with arguments flipped
190 -- M.union is left-biased, plusUFM should be right-biased.
191
192 plusUFM_C :: (elt -> elt -> elt) -> UniqFM elt -> UniqFM elt -> UniqFM elt
193 plusUFM_C f (UFM x) (UFM y) = UFM (M.unionWith f x y)
194
195 -- | `plusUFM_CD f m1 d1 m2 d2` merges the maps using `f` as the
196 -- combinding function and `d1` resp. `d2` as the default value if
197 -- there is no entry in `m1` reps. `m2`. The domain is the union of
198 -- the domains of `m1` and `m2`.
199 --
200 -- Representative example:
201 --
202 -- @
203 -- plusUFM_CD f {A: 1, B: 2} 23 {B: 3, C: 4} 42
204 -- == {A: f 1 42, B: f 2 3, C: f 23 4 }
205 -- @
206 plusUFM_CD
207 :: (elt -> elt -> elt)
208 -> UniqFM elt -- map X
209 -> elt -- default for X
210 -> UniqFM elt -- map Y
211 -> elt -- default for Y
212 -> UniqFM elt
213 plusUFM_CD f (UFM xm) dx (UFM ym) dy
214 = UFM $ M.mergeWithKey
215 (\_ x y -> Just (x `f` y))
216 (M.map (\x -> x `f` dy))
217 (M.map (\y -> dx `f` y))
218 xm ym
219
220 plusMaybeUFM_C :: (elt -> elt -> Maybe elt)
221 -> UniqFM elt -> UniqFM elt -> UniqFM elt
222 plusMaybeUFM_C f (UFM xm) (UFM ym)
223 = UFM $ M.mergeWithKey
224 (\_ x y -> x `f` y)
225 id
226 id
227 xm ym
228
229 plusUFMList :: [UniqFM elt] -> UniqFM elt
230 plusUFMList = foldl' plusUFM emptyUFM
231
232 minusUFM :: UniqFM elt1 -> UniqFM elt2 -> UniqFM elt1
233 minusUFM (UFM x) (UFM y) = UFM (M.difference x y)
234
235 intersectUFM :: UniqFM elt1 -> UniqFM elt2 -> UniqFM elt1
236 intersectUFM (UFM x) (UFM y) = UFM (M.intersection x y)
237
238 intersectUFM_C
239 :: (elt1 -> elt2 -> elt3)
240 -> UniqFM elt1
241 -> UniqFM elt2
242 -> UniqFM elt3
243 intersectUFM_C f (UFM x) (UFM y) = UFM (M.intersectionWith f x y)
244
245 disjointUFM :: UniqFM elt1 -> UniqFM elt2 -> Bool
246 disjointUFM (UFM x) (UFM y) = M.null (M.intersection x y)
247
248 foldUFM :: (elt -> a -> a) -> a -> UniqFM elt -> a
249 foldUFM k z (UFM m) = M.foldr k z m
250
251 mapUFM :: (elt1 -> elt2) -> UniqFM elt1 -> UniqFM elt2
252 mapUFM f (UFM m) = UFM (M.map f m)
253
254 mapUFM_Directly :: (Unique -> elt1 -> elt2) -> UniqFM elt1 -> UniqFM elt2
255 mapUFM_Directly f (UFM m) = UFM (M.mapWithKey (f . getUnique) m)
256
257 filterUFM :: (elt -> Bool) -> UniqFM elt -> UniqFM elt
258 filterUFM p (UFM m) = UFM (M.filter p m)
259
260 filterUFM_Directly :: (Unique -> elt -> Bool) -> UniqFM elt -> UniqFM elt
261 filterUFM_Directly p (UFM m) = UFM (M.filterWithKey (p . getUnique) m)
262
263 partitionUFM :: (elt -> Bool) -> UniqFM elt -> (UniqFM elt, UniqFM elt)
264 partitionUFM p (UFM m) =
265 case M.partition p m of
266 (left, right) -> (UFM left, UFM right)
267
268 sizeUFM :: UniqFM elt -> Int
269 sizeUFM (UFM m) = M.size m
270
271 elemUFM :: Uniquable key => key -> UniqFM elt -> Bool
272 elemUFM k (UFM m) = M.member (getKey $ getUnique k) m
273
274 elemUFM_Directly :: Unique -> UniqFM elt -> Bool
275 elemUFM_Directly u (UFM m) = M.member (getKey u) m
276
277 lookupUFM :: Uniquable key => UniqFM elt -> key -> Maybe elt
278 lookupUFM (UFM m) k = M.lookup (getKey $ getUnique k) m
279
280 -- when you've got the Unique already
281 lookupUFM_Directly :: UniqFM elt -> Unique -> Maybe elt
282 lookupUFM_Directly (UFM m) u = M.lookup (getKey u) m
283
284 lookupWithDefaultUFM :: Uniquable key => UniqFM elt -> elt -> key -> elt
285 lookupWithDefaultUFM (UFM m) v k = M.findWithDefault v (getKey $ getUnique k) m
286
287 lookupWithDefaultUFM_Directly :: UniqFM elt -> elt -> Unique -> elt
288 lookupWithDefaultUFM_Directly (UFM m) v u = M.findWithDefault v (getKey u) m
289
290 eltsUFM :: UniqFM elt -> [elt]
291 eltsUFM (UFM m) = M.elems m
292
293 ufmToSet_Directly :: UniqFM elt -> S.IntSet
294 ufmToSet_Directly (UFM m) = M.keysSet m
295
296 anyUFM :: (elt -> Bool) -> UniqFM elt -> Bool
297 anyUFM p (UFM m) = M.foldr ((||) . p) False m
298
299 allUFM :: (elt -> Bool) -> UniqFM elt -> Bool
300 allUFM p (UFM m) = M.foldr ((&&) . p) True m
301
302 seqEltsUFM :: ([elt] -> ()) -> UniqFM elt -> ()
303 seqEltsUFM seqList = seqList . nonDetEltsUFM
304 -- It's OK to use nonDetEltsUFM here because the type guarantees that
305 -- the only interesting thing this function can do is to force the
306 -- elements.
307
308 -- See Note [Deterministic UniqFM] to learn about nondeterminism.
309 -- If you use this please provide a justification why it doesn't introduce
310 -- nondeterminism.
311 nonDetEltsUFM :: UniqFM elt -> [elt]
312 nonDetEltsUFM (UFM m) = M.elems m
313
314 -- See Note [Deterministic UniqFM] to learn about nondeterminism.
315 -- If you use this please provide a justification why it doesn't introduce
316 -- nondeterminism.
317 nonDetKeysUFM :: UniqFM elt -> [Unique]
318 nonDetKeysUFM (UFM m) = map getUnique $ M.keys m
319
320 -- See Note [Deterministic UniqFM] to learn about nondeterminism.
321 -- If you use this please provide a justification why it doesn't introduce
322 -- nondeterminism.
323 nonDetFoldUFM :: (elt -> a -> a) -> a -> UniqFM elt -> a
324 nonDetFoldUFM k z (UFM m) = M.foldr k z m
325
326 -- See Note [Deterministic UniqFM] to learn about nondeterminism.
327 -- If you use this please provide a justification why it doesn't introduce
328 -- nondeterminism.
329 nonDetFoldUFM_Directly:: (Unique -> elt -> a -> a) -> a -> UniqFM elt -> a
330 nonDetFoldUFM_Directly k z (UFM m) = M.foldrWithKey (k . getUnique) z m
331
332 -- See Note [Deterministic UniqFM] to learn about nondeterminism.
333 -- If you use this please provide a justification why it doesn't introduce
334 -- nondeterminism.
335 nonDetUFMToList :: UniqFM elt -> [(Unique, elt)]
336 nonDetUFMToList (UFM m) = map (\(k, v) -> (getUnique k, v)) $ M.toList m
337
338 ufmToIntMap :: UniqFM elt -> M.IntMap elt
339 ufmToIntMap (UFM m) = m
340
341 -- Determines whether two 'UniqFm's contain the same keys.
342 equalKeysUFM :: UniqFM a -> UniqFM b -> Bool
343 equalKeysUFM (UFM m1) (UFM m2) = liftEq (\_ _ -> True) m1 m2
344
345 -- Instances
346
347 instance Semi.Semigroup (UniqFM a) where
348 (<>) = plusUFM
349
350 instance Monoid (UniqFM a) where
351 mempty = emptyUFM
352 mappend = (Semi.<>)
353
354 -- Output-ery
355
356 instance Outputable a => Outputable (UniqFM a) where
357 ppr ufm = pprUniqFM ppr ufm
358
359 pprUniqFM :: (a -> SDoc) -> UniqFM a -> SDoc
360 pprUniqFM ppr_elt ufm
361 = brackets $ fsep $ punctuate comma $
362 [ ppr uq <+> text ":->" <+> ppr_elt elt
363 | (uq, elt) <- nonDetUFMToList ufm ]
364 -- It's OK to use nonDetUFMToList here because we only use it for
365 -- pretty-printing.
366
367 -- | Pretty-print a non-deterministic set.
368 -- The order of variables is non-deterministic and for pretty-printing that
369 -- shouldn't be a problem.
370 -- Having this function helps contain the non-determinism created with
371 -- nonDetEltsUFM.
372 pprUFM :: UniqFM a -- ^ The things to be pretty printed
373 -> ([a] -> SDoc) -- ^ The pretty printing function to use on the elements
374 -> SDoc -- ^ 'SDoc' where the things have been pretty
375 -- printed
376 pprUFM ufm pp = pp (nonDetEltsUFM ufm)
377
378 -- | Pretty-print a non-deterministic set.
379 -- The order of variables is non-deterministic and for pretty-printing that
380 -- shouldn't be a problem.
381 -- Having this function helps contain the non-determinism created with
382 -- nonDetUFMToList.
383 pprUFMWithKeys
384 :: UniqFM a -- ^ The things to be pretty printed
385 -> ([(Unique, a)] -> SDoc) -- ^ The pretty printing function to use on the elements
386 -> SDoc -- ^ 'SDoc' where the things have been pretty
387 -- printed
388 pprUFMWithKeys ufm pp = pp (nonDetUFMToList ufm)
389
390 -- | Determines the pluralisation suffix appropriate for the length of a set
391 -- in the same way that plural from Outputable does for lists.
392 pluralUFM :: UniqFM a -> SDoc
393 pluralUFM ufm
394 | sizeUFM ufm == 1 = empty
395 | otherwise = char 's'