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