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