2 % (c) The AQUA Project, Glasgow University, 1994-1996
4 \section[FiniteMap]{An implementation of finite maps}
6 ``Finite maps'' are the heart of the compiler's
7 lookup-tables/environments and its implementation of sets. Important
10 This code is derived from that in the paper:
13 "Efficient sets: a balancing act"
14 Journal of functional programming 3(4) Oct 1993, pp553-562
17 The code is SPECIALIZEd to various highly-desirable types (e.g., Id)
18 near the end (only \tr{#ifdef COMPILING_GHC}).
22 #include "HsVersions.h"
23 #define IF_NOT_GHC(a) {--}
25 #define ASSERT(e) {--}
26 #define IF_NOT_GHC(a) a
28 #define _tagCmp compare
34 #if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)/* NB NB NB */
35 #define OUTPUTABLE_key , Outputable key
37 #define OUTPUTABLE_key {--}
41 FiniteMap, -- abstract type
43 emptyFM, unitFM, listToFM,
61 sizeFM, isEmptyFM, elemFM, lookupFM, lookupWithDefaultFM,
63 fmToList, keysFM, eltsFM
67 , SYN_IE(FiniteSet), emptySet, mkSet, isEmptySet
68 , elementOf, setToList, union, minusSet
72 IMPORT_DELOOPER(SpecLoop)
73 #if __GLASGOW_HASKELL__ >= 202
76 #if defined(USE_FAST_STRINGS)
80 import Bag ( Bag, foldrBag )
81 import Outputable ( PprStyle, Outputable(..) )
86 # if ! OMIT_NATIVE_CODEGEN
89 # define IF_NCG(a) {--}
93 -- SIGH: but we use unboxed "sizes"...
94 #if __GLASGOW_HASKELL__
102 %************************************************************************
104 \subsection{The signature of the module}
106 %************************************************************************
110 emptyFM :: FiniteMap key elt
111 unitFM :: key -> elt -> FiniteMap key elt
112 listToFM :: (Ord key OUTPUTABLE_key) => [(key,elt)] -> FiniteMap key elt
113 -- In the case of duplicates, the last is taken
115 bagToFM :: (Ord key OUTPUTABLE_key) => Bag (key,elt) -> FiniteMap key elt
116 -- In the case of duplicates, who knows which is taken
119 -- ADDING AND DELETING
120 -- Throws away any previous binding
121 -- In the list case, the items are added starting with the
122 -- first one in the list
123 addToFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> elt -> FiniteMap key elt
124 addListToFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> [(key,elt)] -> FiniteMap key elt
126 -- Combines with previous binding
127 addToFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
128 -> FiniteMap key elt -> key -> elt
130 addListToFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
131 -> FiniteMap key elt -> [(key,elt)]
134 -- Deletion doesn't complain if you try to delete something
136 delFromFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> FiniteMap key elt
137 delListFromFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> [key] -> FiniteMap key elt
140 -- Bindings in right argument shadow those in the left
141 plusFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
144 -- Combines bindings for the same thing with the given function
145 plusFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
146 -> FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
148 minusFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
149 -- (minusFM a1 a2) deletes from a1 any bindings which are bound in a2
151 intersectFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
152 intersectFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt2)
153 -> FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt2
155 -- MAPPING, FOLDING, FILTERING
156 foldFM :: (key -> elt -> a -> a) -> a -> FiniteMap key elt -> a
157 mapFM :: (key -> elt1 -> elt2) -> FiniteMap key elt1 -> FiniteMap key elt2
158 filterFM :: (Ord key OUTPUTABLE_key) => (key -> elt -> Bool)
159 -> FiniteMap key elt -> FiniteMap key elt
162 sizeFM :: FiniteMap key elt -> Int
163 isEmptyFM :: FiniteMap key elt -> Bool
165 elemFM :: (Ord key OUTPUTABLE_key) => key -> FiniteMap key elt -> Bool
166 lookupFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> Maybe elt
168 :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> elt -> key -> elt
169 -- lookupWithDefaultFM supplies a "default" elt
170 -- to return for an unmapped key
173 fmToList :: FiniteMap key elt -> [(key,elt)]
174 keysFM :: FiniteMap key elt -> [key]
175 eltsFM :: FiniteMap key elt -> [elt]
178 %************************************************************************
180 \subsection{The @FiniteMap@ data type, and building of same}
182 %************************************************************************
184 Invariants about @FiniteMap@:
187 all keys in a FiniteMap are distinct
189 all keys in left subtree are $<$ key in Branch and
190 all keys in right subtree are $>$ key in Branch
192 size field of a Branch gives number of Branch nodes in the tree
194 size of left subtree is differs from size of right subtree by a
195 factor of at most \tr{sIZE_RATIO}
199 data FiniteMap key elt
201 | Branch key elt -- Key and elt stored here
202 IF_GHC(Int#,Int{-STRICT-}) -- Size >= 1
203 (FiniteMap key elt) -- Children
211 = Branch bottom bottom IF_GHC(0#,0) bottom bottom
213 bottom = panic "emptyFM"
216 -- #define EmptyFM (Branch _ _ IF_GHC(0#,0) _ _)
218 unitFM key elt = Branch key elt IF_GHC(1#,1) emptyFM emptyFM
220 listToFM = addListToFM emptyFM
223 bagToFM = foldrBag (\(k,v) fm -> addToFM fm k v) emptyFM
227 %************************************************************************
229 \subsection{Adding to and deleting from @FiniteMaps@}
231 %************************************************************************
234 addToFM fm key elt = addToFM_C (\ old new -> new) fm key elt
236 addToFM_C combiner EmptyFM key elt = unitFM key elt
237 addToFM_C combiner (Branch key elt size fm_l fm_r) new_key new_elt
238 #ifdef __GLASGOW_HASKELL__
239 = case _tagCmp new_key key of
240 _LT -> mkBalBranch key elt (addToFM_C combiner fm_l new_key new_elt) fm_r
241 _GT -> mkBalBranch key elt fm_l (addToFM_C combiner fm_r new_key new_elt)
242 _EQ -> Branch new_key (combiner elt new_elt) size fm_l fm_r
244 | new_key < key = mkBalBranch key elt (addToFM_C combiner fm_l new_key new_elt) fm_r
245 | new_key > key = mkBalBranch key elt fm_l (addToFM_C combiner fm_r new_key new_elt)
246 | otherwise = Branch new_key (combiner elt new_elt) size fm_l fm_r
249 addListToFM fm key_elt_pairs = addListToFM_C (\ old new -> new) fm key_elt_pairs
251 addListToFM_C combiner fm key_elt_pairs
252 = foldl add fm key_elt_pairs -- foldl adds from the left
254 add fmap (key,elt) = addToFM_C combiner fmap key elt
258 delFromFM EmptyFM del_key = emptyFM
259 delFromFM (Branch key elt size fm_l fm_r) del_key
260 #ifdef __GLASGOW_HASKELL__
261 = case _tagCmp del_key key of
262 _GT -> mkBalBranch key elt fm_l (delFromFM fm_r del_key)
263 _LT -> mkBalBranch key elt (delFromFM fm_l del_key) fm_r
264 _EQ -> glueBal fm_l fm_r
267 = mkBalBranch key elt fm_l (delFromFM fm_r del_key)
270 = mkBalBranch key elt (delFromFM fm_l del_key) fm_r
276 delListFromFM fm keys = foldl delFromFM fm keys
279 %************************************************************************
281 \subsection{Combining @FiniteMaps@}
283 %************************************************************************
286 plusFM_C combiner EmptyFM fm2 = fm2
287 plusFM_C combiner fm1 EmptyFM = fm1
288 plusFM_C combiner fm1 (Branch split_key elt2 _ left right)
289 = mkVBalBranch split_key new_elt
290 (plusFM_C combiner lts left)
291 (plusFM_C combiner gts right)
293 lts = splitLT fm1 split_key
294 gts = splitGT fm1 split_key
295 new_elt = case lookupFM fm1 split_key of
297 Just elt1 -> combiner elt1 elt2
299 -- It's worth doing plusFM specially, because we don't need
300 -- to do the lookup in fm1.
302 plusFM EmptyFM fm2 = fm2
303 plusFM fm1 EmptyFM = fm1
304 plusFM fm1 (Branch split_key elt1 _ left right)
305 = mkVBalBranch split_key elt1 (plusFM lts left) (plusFM gts right)
307 lts = splitLT fm1 split_key
308 gts = splitGT fm1 split_key
310 minusFM EmptyFM fm2 = emptyFM
311 minusFM fm1 EmptyFM = fm1
312 minusFM fm1 (Branch split_key elt _ left right)
313 = glueVBal (minusFM lts left) (minusFM gts right)
314 -- The two can be way different, so we need glueVBal
316 lts = splitLT fm1 split_key -- NB gt and lt, so the equal ones
317 gts = splitGT fm1 split_key -- are not in either.
319 intersectFM fm1 fm2 = intersectFM_C (\ left right -> right) fm1 fm2
321 intersectFM_C combiner fm1 EmptyFM = emptyFM
322 intersectFM_C combiner EmptyFM fm2 = emptyFM
323 intersectFM_C combiner fm1 (Branch split_key elt2 _ left right)
325 | maybeToBool maybe_elt1 -- split_elt *is* in intersection
326 = mkVBalBranch split_key (combiner elt1 elt2) (intersectFM_C combiner lts left)
327 (intersectFM_C combiner gts right)
329 | otherwise -- split_elt is *not* in intersection
330 = glueVBal (intersectFM_C combiner lts left) (intersectFM_C combiner gts right)
333 lts = splitLT fm1 split_key -- NB gt and lt, so the equal ones
334 gts = splitGT fm1 split_key -- are not in either.
336 maybe_elt1 = lookupFM fm1 split_key
337 Just elt1 = maybe_elt1
340 %************************************************************************
342 \subsection{Mapping, folding, and filtering with @FiniteMaps@}
344 %************************************************************************
347 foldFM k z EmptyFM = z
348 foldFM k z (Branch key elt _ fm_l fm_r)
349 = foldFM k (k key elt (foldFM k z fm_r)) fm_l
351 mapFM f EmptyFM = emptyFM
352 mapFM f (Branch key elt size fm_l fm_r)
353 = Branch key (f key elt) size (mapFM f fm_l) (mapFM f fm_r)
355 filterFM p EmptyFM = emptyFM
356 filterFM p (Branch key elt _ fm_l fm_r)
357 | p key elt -- Keep the item
358 = mkVBalBranch key elt (filterFM p fm_l) (filterFM p fm_r)
360 | otherwise -- Drop the item
361 = glueVBal (filterFM p fm_l) (filterFM p fm_r)
364 %************************************************************************
366 \subsection{Interrogating @FiniteMaps@}
368 %************************************************************************
371 --{-# INLINE sizeFM #-}
373 sizeFM (Branch _ _ size _ _) = IF_GHC(I# size, size)
375 isEmptyFM fm = sizeFM fm == 0
377 lookupFM EmptyFM key = Nothing
378 lookupFM (Branch key elt _ fm_l fm_r) key_to_find
379 #ifdef __GLASGOW_HASKELL__
380 = case _tagCmp key_to_find key of
381 _LT -> lookupFM fm_l key_to_find
382 _GT -> lookupFM fm_r key_to_find
385 | key_to_find < key = lookupFM fm_l key_to_find
386 | key_to_find > key = lookupFM fm_r key_to_find
387 | otherwise = Just elt
391 = case (lookupFM fm key) of { Nothing -> False; Just elt -> True }
393 lookupWithDefaultFM fm deflt key
394 = case (lookupFM fm key) of { Nothing -> deflt; Just elt -> elt }
397 %************************************************************************
399 \subsection{Listifying @FiniteMaps@}
401 %************************************************************************
404 fmToList fm = foldFM (\ key elt rest -> (key,elt) : rest) [] fm
405 keysFM fm = foldFM (\ key elt rest -> key : rest) [] fm
406 eltsFM fm = foldFM (\ key elt rest -> elt : rest) [] fm
410 %************************************************************************
412 \subsection{The implementation of balancing}
414 %************************************************************************
416 %************************************************************************
418 \subsubsection{Basic construction of a @FiniteMap@}
420 %************************************************************************
422 @mkBranch@ simply gets the size component right. This is the ONLY
423 (non-trivial) place the Branch object is built, so the ASSERTion
424 recursively checks consistency. (The trivial use of Branch is in
431 mkBranch :: (Ord key OUTPUTABLE_key) -- Used for the assertion checking only
434 -> FiniteMap key elt -> FiniteMap key elt
437 mkBranch which key elt fm_l fm_r
438 = --ASSERT( left_ok && right_ok && balance_ok )
439 #if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)
440 if not ( left_ok && right_ok && balance_ok ) then
441 pprPanic ("mkBranch:"++show which) (vcat [ppr PprDebug [left_ok, right_ok, balance_ok],
448 result = Branch key elt (unbox (1 + left_size + right_size)) fm_l fm_r
450 -- if sizeFM result <= 8 then
453 -- pprTrace ("mkBranch:"++(show which)) (ppr PprDebug result) (
457 left_ok = case fm_l of
459 Branch left_key _ _ _ _ -> let
460 biggest_left_key = fst (findMax fm_l)
462 biggest_left_key < key
463 right_ok = case fm_r of
465 Branch right_key _ _ _ _ -> let
466 smallest_right_key = fst (findMin fm_r)
468 key < smallest_right_key
469 balance_ok = True -- sigh
472 = -- Both subtrees have one or no elements...
473 (left_size + right_size <= 1)
474 -- NO || left_size == 0 -- ???
475 -- NO || right_size == 0 -- ???
476 -- ... or the number of elements in a subtree does not exceed
477 -- sIZE_RATIO times the number of elements in the other subtree
478 || (left_size * sIZE_RATIO >= right_size &&
479 right_size * sIZE_RATIO >= left_size)
482 left_size = sizeFM fm_l
483 right_size = sizeFM fm_r
485 #ifdef __GLASGOW_HASKELL__
487 unbox (I# size) = size
494 %************************************************************************
496 \subsubsection{{\em Balanced} construction of a @FiniteMap@}
498 %************************************************************************
500 @mkBalBranch@ rebalances, assuming that the subtrees aren't too far
504 mkBalBranch :: (Ord key OUTPUTABLE_key)
506 -> FiniteMap key elt -> FiniteMap key elt
509 mkBalBranch key elt fm_L fm_R
511 | size_l + size_r < 2
512 = mkBranch 1{-which-} key elt fm_L fm_R
514 | size_r > sIZE_RATIO * size_l -- Right tree too big
516 Branch _ _ _ fm_rl fm_rr
517 | sizeFM fm_rl < 2 * sizeFM fm_rr -> single_L fm_L fm_R
518 | otherwise -> double_L fm_L fm_R
519 -- Other case impossible
521 | size_l > sIZE_RATIO * size_r -- Left tree too big
523 Branch _ _ _ fm_ll fm_lr
524 | sizeFM fm_lr < 2 * sizeFM fm_ll -> single_R fm_L fm_R
525 | otherwise -> double_R fm_L fm_R
526 -- Other case impossible
528 | otherwise -- No imbalance
529 = mkBranch 2{-which-} key elt fm_L fm_R
535 single_L fm_l (Branch key_r elt_r _ fm_rl fm_rr)
536 = mkBranch 3{-which-} key_r elt_r (mkBranch 4{-which-} key elt fm_l fm_rl) fm_rr
538 double_L fm_l (Branch key_r elt_r _ (Branch key_rl elt_rl _ fm_rll fm_rlr) fm_rr)
539 = mkBranch 5{-which-} key_rl elt_rl (mkBranch 6{-which-} key elt fm_l fm_rll)
540 (mkBranch 7{-which-} key_r elt_r fm_rlr fm_rr)
542 single_R (Branch key_l elt_l _ fm_ll fm_lr) fm_r
543 = mkBranch 8{-which-} key_l elt_l fm_ll (mkBranch 9{-which-} key elt fm_lr fm_r)
545 double_R (Branch key_l elt_l _ fm_ll (Branch key_lr elt_lr _ fm_lrl fm_lrr)) fm_r
546 = mkBranch 10{-which-} key_lr elt_lr (mkBranch 11{-which-} key_l elt_l fm_ll fm_lrl)
547 (mkBranch 12{-which-} key elt fm_lrr fm_r)
552 mkVBalBranch :: (Ord key OUTPUTABLE_key)
554 -> FiniteMap key elt -> FiniteMap key elt
557 -- Assert: in any call to (mkVBalBranch_C comb key elt l r),
558 -- (a) all keys in l are < all keys in r
559 -- (b) all keys in l are < key
560 -- (c) all keys in r are > key
562 mkVBalBranch key elt EmptyFM fm_r = addToFM fm_r key elt
563 mkVBalBranch key elt fm_l EmptyFM = addToFM fm_l key elt
565 mkVBalBranch key elt fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
566 fm_r@(Branch key_r elt_r _ fm_rl fm_rr)
567 | sIZE_RATIO * size_l < size_r
568 = mkBalBranch key_r elt_r (mkVBalBranch key elt fm_l fm_rl) fm_rr
570 | sIZE_RATIO * size_r < size_l
571 = mkBalBranch key_l elt_l fm_ll (mkVBalBranch key elt fm_lr fm_r)
574 = mkBranch 13{-which-} key elt fm_l fm_r
581 %************************************************************************
583 \subsubsection{Gluing two trees together}
585 %************************************************************************
587 @glueBal@ assumes its two arguments aren't too far out of whack, just
588 like @mkBalBranch@. But: all keys in first arg are $<$ all keys in
592 glueBal :: (Ord key OUTPUTABLE_key)
593 => FiniteMap key elt -> FiniteMap key elt
596 glueBal EmptyFM fm2 = fm2
597 glueBal fm1 EmptyFM = fm1
599 -- The case analysis here (absent in Adams' program) is really to deal
600 -- with the case where fm2 is a singleton. Then deleting the minimum means
601 -- we pass an empty tree to mkBalBranch, which breaks its invariant.
602 | sizeFM fm2 > sizeFM fm1
603 = mkBalBranch mid_key2 mid_elt2 fm1 (deleteMin fm2)
606 = mkBalBranch mid_key1 mid_elt1 (deleteMax fm1) fm2
608 (mid_key1, mid_elt1) = findMax fm1
609 (mid_key2, mid_elt2) = findMin fm2
612 @glueVBal@ copes with arguments which can be of any size.
613 But: all keys in first arg are $<$ all keys in second.
616 glueVBal :: (Ord key OUTPUTABLE_key)
617 => FiniteMap key elt -> FiniteMap key elt
620 glueVBal EmptyFM fm2 = fm2
621 glueVBal fm1 EmptyFM = fm1
622 glueVBal fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
623 fm_r@(Branch key_r elt_r _ fm_rl fm_rr)
624 | sIZE_RATIO * size_l < size_r
625 = mkBalBranch key_r elt_r (glueVBal fm_l fm_rl) fm_rr
627 | sIZE_RATIO * size_r < size_l
628 = mkBalBranch key_l elt_l fm_ll (glueVBal fm_lr fm_r)
630 | otherwise -- We now need the same two cases as in glueBal above.
633 (mid_key_l,mid_elt_l) = findMax fm_l
634 (mid_key_r,mid_elt_r) = findMin fm_r
639 %************************************************************************
641 \subsection{Local utilities}
643 %************************************************************************
646 splitLT, splitGT :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> FiniteMap key elt
648 -- splitLT fm split_key = fm restricted to keys < split_key
649 -- splitGT fm split_key = fm restricted to keys > split_key
651 splitLT EmptyFM split_key = emptyFM
652 splitLT (Branch key elt _ fm_l fm_r) split_key
653 #ifdef __GLASGOW_HASKELL__
654 = case _tagCmp split_key key of
655 _LT -> splitLT fm_l split_key
656 _GT -> mkVBalBranch key elt fm_l (splitLT fm_r split_key)
659 | split_key < key = splitLT fm_l split_key
660 | split_key > key = mkVBalBranch key elt fm_l (splitLT fm_r split_key)
664 splitGT EmptyFM split_key = emptyFM
665 splitGT (Branch key elt _ fm_l fm_r) split_key
666 #ifdef __GLASGOW_HASKELL__
667 = case _tagCmp split_key key of
668 _GT -> splitGT fm_r split_key
669 _LT -> mkVBalBranch key elt (splitGT fm_l split_key) fm_r
672 | split_key > key = splitGT fm_r split_key
673 | split_key < key = mkVBalBranch key elt (splitGT fm_l split_key) fm_r
677 findMin :: FiniteMap key elt -> (key,elt)
678 findMin (Branch key elt _ EmptyFM _) = (key,elt)
679 findMin (Branch key elt _ fm_l _) = findMin fm_l
681 deleteMin :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
682 deleteMin (Branch key elt _ EmptyFM fm_r) = fm_r
683 deleteMin (Branch key elt _ fm_l fm_r) = mkBalBranch key elt (deleteMin fm_l) fm_r
685 findMax :: FiniteMap key elt -> (key,elt)
686 findMax (Branch key elt _ _ EmptyFM) = (key,elt)
687 findMax (Branch key elt _ _ fm_r) = findMax fm_r
689 deleteMax :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
690 deleteMax (Branch key elt _ fm_l EmptyFM) = fm_l
691 deleteMax (Branch key elt _ fm_l fm_r) = mkBalBranch key elt fm_l (deleteMax fm_r)
694 %************************************************************************
696 \subsection{Output-ery}
698 %************************************************************************
701 #if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)
703 instance (Outputable key) => Outputable (FiniteMap key elt) where
704 ppr sty fm = pprX sty fm
706 pprX sty EmptyFM = char '!'
707 pprX sty (Branch key elt sz fm_l fm_r)
708 = parens (hcat [pprX sty fm_l, space,
709 ppr sty key, space, int (IF_GHC(I# sz, sz)), space,
713 #ifndef COMPILING_GHC
714 instance (Eq key, Eq elt) => Eq (FiniteMap key elt) where
715 fm_1 == fm_2 = (sizeFM fm_1 == sizeFM fm_2) && -- quick test
716 (fmToList fm_1 == fmToList fm_2)
718 {- NO: not clear what The Right Thing to do is:
719 instance (Ord key, Ord elt) => Ord (FiniteMap key elt) where
720 fm_1 <= fm_2 = (sizeFM fm_1 <= sizeFM fm_2) && -- quick test
721 (fmToList fm_1 <= fmToList fm_2)
726 %************************************************************************
728 \subsection{FiniteSets---a thin veneer}
730 %************************************************************************
735 type FiniteSet key = FiniteMap key ()
736 emptySet :: FiniteSet key
737 mkSet :: (Ord key OUTPUTABLE_key) => [key] -> FiniteSet key
738 isEmptySet :: FiniteSet key -> Bool
739 elementOf :: (Ord key OUTPUTABLE_key) => key -> FiniteSet key -> Bool
740 minusSet :: (Ord key OUTPUTABLE_key) => FiniteSet key -> FiniteSet key -> FiniteSet key
741 setToList :: FiniteSet key -> [key]
742 union :: (Ord key OUTPUTABLE_key) => FiniteSet key -> FiniteSet key -> FiniteSet key
745 mkSet xs = listToFM [ (x, ()) | x <- xs]
746 isEmptySet = isEmptyFM
755 %************************************************************************
757 \subsection{Efficiency pragmas for GHC}
759 %************************************************************************
761 When the FiniteMap module is used in GHC, we specialise it for
762 \tr{Uniques}, for dastardly efficiency reasons.
765 #if defined(COMPILING_GHC) && __GLASGOW_HASKELL__ && !defined(REALLY_HASKELL_1_3)
767 {-# SPECIALIZE addListToFM
768 :: FiniteMap (FAST_STRING, FAST_STRING) elt -> [((FAST_STRING, FAST_STRING),elt)] -> FiniteMap (FAST_STRING, FAST_STRING) elt
769 , FiniteMap RdrName elt -> [(RdrName,elt)] -> FiniteMap RdrName elt
770 IF_NCG(COMMA FiniteMap Reg elt -> [(Reg COMMA elt)] -> FiniteMap Reg elt)
772 {-# SPECIALIZE addListToFM_C
773 :: (elt -> elt -> elt) -> FiniteMap TyCon elt -> [(TyCon,elt)] -> FiniteMap TyCon elt
774 , (elt -> elt -> elt) -> FiniteMap FAST_STRING elt -> [(FAST_STRING,elt)] -> FiniteMap FAST_STRING elt
775 IF_NCG(COMMA (elt -> elt -> elt) -> FiniteMap Reg elt -> [(Reg COMMA elt)] -> FiniteMap Reg elt)
777 {-# SPECIALIZE addToFM
778 :: FiniteMap CLabel elt -> CLabel -> elt -> FiniteMap CLabel elt
779 , FiniteMap FAST_STRING elt -> FAST_STRING -> elt -> FiniteMap FAST_STRING elt
780 , FiniteMap (FAST_STRING, FAST_STRING) elt -> (FAST_STRING, FAST_STRING) -> elt -> FiniteMap (FAST_STRING, FAST_STRING) elt
781 , FiniteMap RdrName elt -> RdrName -> elt -> FiniteMap RdrName elt
782 IF_NCG(COMMA FiniteMap Reg elt -> Reg -> elt -> FiniteMap Reg elt)
784 {-# SPECIALIZE addToFM_C
785 :: (elt -> elt -> elt) -> FiniteMap (RdrName, RdrName) elt -> (RdrName, RdrName) -> elt -> FiniteMap (RdrName, RdrName) elt
786 , (elt -> elt -> elt) -> FiniteMap FAST_STRING elt -> FAST_STRING -> elt -> FiniteMap FAST_STRING elt
787 IF_NCG(COMMA (elt -> elt -> elt) -> FiniteMap Reg elt -> Reg -> elt -> FiniteMap Reg elt)
789 {-# SPECIALIZE bagToFM
790 :: Bag (FAST_STRING,elt) -> FiniteMap FAST_STRING elt
792 {-# SPECIALIZE delListFromFM
793 :: FiniteMap RdrName elt -> [RdrName] -> FiniteMap RdrName elt
794 , FiniteMap FAST_STRING elt -> [FAST_STRING] -> FiniteMap FAST_STRING elt
795 IF_NCG(COMMA FiniteMap Reg elt -> [Reg] -> FiniteMap Reg elt)
797 {-# SPECIALIZE listToFM
798 :: [([Char],elt)] -> FiniteMap [Char] elt
799 , [(FAST_STRING,elt)] -> FiniteMap FAST_STRING elt
800 , [((FAST_STRING,FAST_STRING),elt)] -> FiniteMap (FAST_STRING, FAST_STRING) elt
801 IF_NCG(COMMA [(Reg COMMA elt)] -> FiniteMap Reg elt)
803 {-# SPECIALIZE lookupFM
804 :: FiniteMap CLabel elt -> CLabel -> Maybe elt
805 , FiniteMap [Char] elt -> [Char] -> Maybe elt
806 , FiniteMap FAST_STRING elt -> FAST_STRING -> Maybe elt
807 , FiniteMap (FAST_STRING,FAST_STRING) elt -> (FAST_STRING,FAST_STRING) -> Maybe elt
808 , FiniteMap RdrName elt -> RdrName -> Maybe elt
809 , FiniteMap (RdrName,RdrName) elt -> (RdrName,RdrName) -> Maybe elt
810 IF_NCG(COMMA FiniteMap Reg elt -> Reg -> Maybe elt)
812 {-# SPECIALIZE lookupWithDefaultFM
813 :: FiniteMap FAST_STRING elt -> elt -> FAST_STRING -> elt
814 IF_NCG(COMMA FiniteMap Reg elt -> elt -> Reg -> elt)
816 {-# SPECIALIZE plusFM
817 :: FiniteMap RdrName elt -> FiniteMap RdrName elt -> FiniteMap RdrName elt
818 , FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt
819 IF_NCG(COMMA FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
821 {-# SPECIALIZE plusFM_C
822 :: (elt -> elt -> elt) -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt
823 IF_NCG(COMMA (elt -> elt -> elt) -> FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
826 #endif {- compiling for GHC -}