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
30 #if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)/* NB NB NB */
31 #define OUTPUTABLE_key , Outputable key
33 #define OUTPUTABLE_key {--}
37 FiniteMap, -- abstract type
39 emptyFM, unitFM, listToFM,
45 IF_NOT_GHC(delFromFM COMMA)
52 IF_NOT_GHC(intersectFM COMMA)
53 IF_NOT_GHC(intersectFM_C COMMA)
54 IF_NOT_GHC(mapFM COMMA foldFM COMMA filterFM COMMA)
56 sizeFM, isEmptyFM, elemFM, lookupFM, lookupWithDefaultFM,
58 fmToList, keysFM, eltsFM
62 , FiniteSet(..), emptySet, mkSet, isEmptySet
63 , elementOf, setToList, union, minusSet
66 -- To make it self-sufficient
79 import Bag ( foldBag )
80 #if ! OMIT_NATIVE_CODEGEN
83 #define IF_NCG(a) {--}
87 -- SIGH: but we use unboxed "sizes"...
88 #if __GLASGOW_HASKELL__
96 %************************************************************************
98 \subsection{The signature of the module}
100 %************************************************************************
104 emptyFM :: FiniteMap key elt
105 unitFM :: key -> elt -> FiniteMap key elt
106 listToFM :: (Ord key OUTPUTABLE_key) => [(key,elt)] -> FiniteMap key elt
107 -- In the case of duplicates, the last is taken
109 bagToFM :: (Ord key OUTPUTABLE_key) => Bag (key,elt) -> FiniteMap key elt
110 -- In the case of duplicates, who knows which is taken
113 -- ADDING AND DELETING
114 -- Throws away any previous binding
115 -- In the list case, the items are added starting with the
116 -- first one in the list
117 addToFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> elt -> FiniteMap key elt
118 addListToFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> [(key,elt)] -> FiniteMap key elt
120 -- Combines with previous binding
121 addToFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
122 -> FiniteMap key elt -> key -> elt
124 addListToFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
125 -> FiniteMap key elt -> [(key,elt)]
128 -- Deletion doesn't complain if you try to delete something
130 delFromFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> FiniteMap key elt
131 delListFromFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> [key] -> FiniteMap key elt
134 -- Bindings in right argument shadow those in the left
135 plusFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
138 -- Combines bindings for the same thing with the given function
139 plusFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
140 -> FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
142 minusFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
143 -- (minusFM a1 a2) deletes from a1 any bindings which are bound in a2
145 intersectFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
146 intersectFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
147 -> FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
149 -- MAPPING, FOLDING, FILTERING
150 foldFM :: (key -> elt -> a -> a) -> a -> FiniteMap key elt -> a
151 mapFM :: (key -> elt1 -> elt2) -> FiniteMap key elt1 -> FiniteMap key elt2
152 filterFM :: (Ord key OUTPUTABLE_key) => (key -> elt -> Bool)
153 -> FiniteMap key elt -> FiniteMap key elt
156 sizeFM :: FiniteMap key elt -> Int
157 isEmptyFM :: FiniteMap key elt -> Bool
159 elemFM :: (Ord key OUTPUTABLE_key) => key -> FiniteMap key elt -> Bool
160 lookupFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> Maybe elt
162 :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> elt -> key -> elt
163 -- lookupWithDefaultFM supplies a "default" elt
164 -- to return for an unmapped key
167 fmToList :: FiniteMap key elt -> [(key,elt)]
168 keysFM :: FiniteMap key elt -> [key]
169 eltsFM :: FiniteMap key elt -> [elt]
172 %************************************************************************
174 \subsection{The @FiniteMap@ data type, and building of same}
176 %************************************************************************
178 Invariants about @FiniteMap@:
181 all keys in a FiniteMap are distinct
183 all keys in left subtree are $<$ key in Branch and
184 all keys in right subtree are $>$ key in Branch
186 size field of a Branch gives number of Branch nodes in the tree
188 size of left subtree is differs from size of right subtree by a
189 factor of at most \tr{sIZE_RATIO}
193 data FiniteMap key elt
195 | Branch key elt -- Key and elt stored here
196 IF_GHC(Int#,Int{-STRICT-}) -- Size >= 1
197 (FiniteMap key elt) -- Children
205 = Branch bottom bottom IF_GHC(0#,0) bottom bottom
207 bottom = panic "emptyFM"
210 -- #define EmptyFM (Branch _ _ IF_GHC(0#,0) _ _)
212 unitFM key elt = Branch key elt IF_GHC(1#,1) emptyFM emptyFM
214 listToFM = addListToFM emptyFM
217 bagToFM = foldBag plusFM (\ (k,v) -> unitFM k v) emptyFM
221 %************************************************************************
223 \subsection{Adding to and deleting from @FiniteMaps@}
225 %************************************************************************
228 addToFM fm key elt = addToFM_C (\ old new -> new) fm key elt
230 addToFM_C combiner EmptyFM key elt = unitFM key elt
231 addToFM_C combiner (Branch key elt size fm_l fm_r) new_key new_elt
232 #ifdef __GLASGOW_HASKELL__
233 = case _tagCmp new_key key of
234 _LT -> mkBalBranch key elt (addToFM_C combiner fm_l new_key new_elt) fm_r
235 _GT -> mkBalBranch key elt fm_l (addToFM_C combiner fm_r new_key new_elt)
236 _EQ -> Branch new_key (combiner elt new_elt) size fm_l fm_r
238 | new_key < key = mkBalBranch key elt (addToFM_C combiner fm_l new_key new_elt) fm_r
239 | new_key > key = mkBalBranch key elt fm_l (addToFM_C combiner fm_r new_key new_elt)
240 | otherwise = Branch new_key (combiner elt new_elt) size fm_l fm_r
243 addListToFM fm key_elt_pairs = addListToFM_C (\ old new -> new) fm key_elt_pairs
245 addListToFM_C combiner fm key_elt_pairs
246 = foldl add fm key_elt_pairs -- foldl adds from the left
248 add fmap (key,elt) = addToFM_C combiner fmap key elt
252 delFromFM EmptyFM del_key = emptyFM
253 delFromFM (Branch key elt size fm_l fm_r) del_key
254 #ifdef __GLASGOW_HASKELL__
255 = case _tagCmp del_key key of
256 _GT -> mkBalBranch key elt fm_l (delFromFM fm_r del_key)
257 _LT -> mkBalBranch key elt (delFromFM fm_l del_key) fm_r
258 _EQ -> glueBal fm_l fm_r
261 = mkBalBranch key elt fm_l (delFromFM fm_r del_key)
264 = mkBalBranch key elt (delFromFM fm_l del_key) fm_r
270 delListFromFM fm keys = foldl delFromFM fm keys
273 %************************************************************************
275 \subsection{Combining @FiniteMaps@}
277 %************************************************************************
280 plusFM_C combiner EmptyFM fm2 = fm2
281 plusFM_C combiner fm1 EmptyFM = fm1
282 plusFM_C combiner fm1 (Branch split_key elt2 _ left right)
283 = mkVBalBranch split_key new_elt
284 (plusFM_C combiner lts left)
285 (plusFM_C combiner gts right)
287 lts = splitLT fm1 split_key
288 gts = splitGT fm1 split_key
289 new_elt = case lookupFM fm1 split_key of
291 Just elt1 -> combiner elt1 elt2
293 -- It's worth doing plusFM specially, because we don't need
294 -- to do the lookup in fm1.
296 plusFM EmptyFM fm2 = fm2
297 plusFM fm1 EmptyFM = fm1
298 plusFM fm1 (Branch split_key elt1 _ left right)
299 = mkVBalBranch split_key elt1 (plusFM lts left) (plusFM gts right)
301 lts = splitLT fm1 split_key
302 gts = splitGT fm1 split_key
304 minusFM EmptyFM fm2 = emptyFM
305 minusFM fm1 EmptyFM = fm1
306 minusFM fm1 (Branch split_key elt _ left right)
307 = glueVBal (minusFM lts left) (minusFM gts right)
308 -- The two can be way different, so we need glueVBal
310 lts = splitLT fm1 split_key -- NB gt and lt, so the equal ones
311 gts = splitGT fm1 split_key -- are not in either.
313 intersectFM fm1 fm2 = intersectFM_C (\ left right -> right) fm1 fm2
315 intersectFM_C combiner fm1 EmptyFM = emptyFM
316 intersectFM_C combiner EmptyFM fm2 = emptyFM
317 intersectFM_C combiner fm1 (Branch split_key elt2 _ left right)
319 | maybeToBool maybe_elt1 -- split_elt *is* in intersection
320 = mkVBalBranch split_key (combiner elt1 elt2) (intersectFM_C combiner lts left)
321 (intersectFM_C combiner gts right)
323 | otherwise -- split_elt is *not* in intersection
324 = glueVBal (intersectFM_C combiner lts left) (intersectFM_C combiner gts right)
327 lts = splitLT fm1 split_key -- NB gt and lt, so the equal ones
328 gts = splitGT fm1 split_key -- are not in either.
330 maybe_elt1 = lookupFM fm1 split_key
331 Just elt1 = maybe_elt1
334 %************************************************************************
336 \subsection{Mapping, folding, and filtering with @FiniteMaps@}
338 %************************************************************************
341 foldFM k z EmptyFM = z
342 foldFM k z (Branch key elt _ fm_l fm_r)
343 = foldFM k (k key elt (foldFM k z fm_r)) fm_l
345 mapFM f EmptyFM = emptyFM
346 mapFM f (Branch key elt size fm_l fm_r)
347 = Branch key (f key elt) size (mapFM f fm_l) (mapFM f fm_r)
349 filterFM p EmptyFM = emptyFM
350 filterFM p (Branch key elt _ fm_l fm_r)
351 | p key elt -- Keep the item
352 = mkVBalBranch key elt (filterFM p fm_l) (filterFM p fm_r)
354 | otherwise -- Drop the item
355 = glueVBal (filterFM p fm_l) (filterFM p fm_r)
358 %************************************************************************
360 \subsection{Interrogating @FiniteMaps@}
362 %************************************************************************
365 --{-# INLINE sizeFM #-}
367 sizeFM (Branch _ _ size _ _) = IF_GHC(I# size, size)
369 isEmptyFM fm = sizeFM fm == 0
371 lookupFM EmptyFM key = Nothing
372 lookupFM (Branch key elt _ fm_l fm_r) key_to_find
373 #ifdef __GLASGOW_HASKELL__
374 = case _tagCmp key_to_find key of
375 _LT -> lookupFM fm_l key_to_find
376 _GT -> lookupFM fm_r key_to_find
379 | key_to_find < key = lookupFM fm_l key_to_find
380 | key_to_find > key = lookupFM fm_r key_to_find
381 | otherwise = Just elt
385 = case (lookupFM fm key) of { Nothing -> False; Just elt -> True }
387 lookupWithDefaultFM fm deflt key
388 = case (lookupFM fm key) of { Nothing -> deflt; Just elt -> elt }
391 %************************************************************************
393 \subsection{Listifying @FiniteMaps@}
395 %************************************************************************
398 fmToList fm = foldFM (\ key elt rest -> (key,elt) : rest) [] fm
399 keysFM fm = foldFM (\ key elt rest -> key : rest) [] fm
400 eltsFM fm = foldFM (\ key elt rest -> elt : rest) [] fm
404 %************************************************************************
406 \subsection{The implementation of balancing}
408 %************************************************************************
410 %************************************************************************
412 \subsubsection{Basic construction of a @FiniteMap@}
414 %************************************************************************
416 @mkBranch@ simply gets the size component right. This is the ONLY
417 (non-trivial) place the Branch object is built, so the ASSERTion
418 recursively checks consistency. (The trivial use of Branch is in
425 mkBranch :: (Ord key OUTPUTABLE_key) -- Used for the assertion checking only
428 -> FiniteMap key elt -> FiniteMap key elt
431 mkBranch which key elt fm_l fm_r
432 = --ASSERT( left_ok && right_ok && balance_ok )
433 #if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)
434 if not ( left_ok && right_ok && balance_ok ) then
435 pprPanic ("mkBranch:"++show which) (ppAboves [ppr PprDebug [left_ok, right_ok, balance_ok],
442 result = Branch key elt (unbox (1 + left_size + right_size)) fm_l fm_r
444 -- if sizeFM result <= 8 then
447 -- pprTrace ("mkBranch:"++(show which)) (ppr PprDebug result) (
451 left_ok = case fm_l of
453 Branch left_key _ _ _ _ -> let
454 biggest_left_key = fst (findMax fm_l)
456 biggest_left_key < key
457 right_ok = case fm_r of
459 Branch right_key _ _ _ _ -> let
460 smallest_right_key = fst (findMin fm_r)
462 key < smallest_right_key
463 balance_ok = True -- sigh
466 = -- Both subtrees have one or no elements...
467 (left_size + right_size <= 1)
468 -- NO || left_size == 0 -- ???
469 -- NO || right_size == 0 -- ???
470 -- ... or the number of elements in a subtree does not exceed
471 -- sIZE_RATIO times the number of elements in the other subtree
472 || (left_size * sIZE_RATIO >= right_size &&
473 right_size * sIZE_RATIO >= left_size)
476 left_size = sizeFM fm_l
477 right_size = sizeFM fm_r
479 #ifdef __GLASGOW_HASKELL__
481 unbox (I# size) = size
488 %************************************************************************
490 \subsubsection{{\em Balanced} construction of a @FiniteMap@}
492 %************************************************************************
494 @mkBalBranch@ rebalances, assuming that the subtrees aren't too far
498 mkBalBranch :: (Ord key OUTPUTABLE_key)
500 -> FiniteMap key elt -> FiniteMap key elt
503 mkBalBranch key elt fm_L fm_R
505 | size_l + size_r < 2
506 = mkBranch 1{-which-} key elt fm_L fm_R
508 | size_r > sIZE_RATIO * size_l -- Right tree too big
510 Branch _ _ _ fm_rl fm_rr
511 | sizeFM fm_rl < 2 * sizeFM fm_rr -> single_L fm_L fm_R
512 | otherwise -> double_L fm_L fm_R
513 -- Other case impossible
515 | size_l > sIZE_RATIO * size_r -- Left tree too big
517 Branch _ _ _ fm_ll fm_lr
518 | sizeFM fm_lr < 2 * sizeFM fm_ll -> single_R fm_L fm_R
519 | otherwise -> double_R fm_L fm_R
520 -- Other case impossible
522 | otherwise -- No imbalance
523 = mkBranch 2{-which-} key elt fm_L fm_R
529 single_L fm_l (Branch key_r elt_r _ fm_rl fm_rr)
530 = mkBranch 3{-which-} key_r elt_r (mkBranch 4{-which-} key elt fm_l fm_rl) fm_rr
532 double_L fm_l (Branch key_r elt_r _ (Branch key_rl elt_rl _ fm_rll fm_rlr) fm_rr)
533 = mkBranch 5{-which-} key_rl elt_rl (mkBranch 6{-which-} key elt fm_l fm_rll)
534 (mkBranch 7{-which-} key_r elt_r fm_rlr fm_rr)
536 single_R (Branch key_l elt_l _ fm_ll fm_lr) fm_r
537 = mkBranch 8{-which-} key_l elt_l fm_ll (mkBranch 9{-which-} key elt fm_lr fm_r)
539 double_R (Branch key_l elt_l _ fm_ll (Branch key_lr elt_lr _ fm_lrl fm_lrr)) fm_r
540 = mkBranch 10{-which-} key_lr elt_lr (mkBranch 11{-which-} key_l elt_l fm_ll fm_lrl)
541 (mkBranch 12{-which-} key elt fm_lrr fm_r)
546 mkVBalBranch :: (Ord key OUTPUTABLE_key)
548 -> FiniteMap key elt -> FiniteMap key elt
551 -- Assert: in any call to (mkVBalBranch_C comb key elt l r),
552 -- (a) all keys in l are < all keys in r
553 -- (b) all keys in l are < key
554 -- (c) all keys in r are > key
556 mkVBalBranch key elt EmptyFM fm_r = addToFM fm_r key elt
557 mkVBalBranch key elt fm_l EmptyFM = addToFM fm_l key elt
559 mkVBalBranch key elt fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
560 fm_r@(Branch key_r elt_r _ fm_rl fm_rr)
561 | sIZE_RATIO * size_l < size_r
562 = mkBalBranch key_r elt_r (mkVBalBranch key elt fm_l fm_rl) fm_rr
564 | sIZE_RATIO * size_r < size_l
565 = mkBalBranch key_l elt_l fm_ll (mkVBalBranch key elt fm_lr fm_r)
568 = mkBranch 13{-which-} key elt fm_l fm_r
575 %************************************************************************
577 \subsubsection{Gluing two trees together}
579 %************************************************************************
581 @glueBal@ assumes its two arguments aren't too far out of whack, just
582 like @mkBalBranch@. But: all keys in first arg are $<$ all keys in
586 glueBal :: (Ord key OUTPUTABLE_key)
587 => FiniteMap key elt -> FiniteMap key elt
590 glueBal EmptyFM fm2 = fm2
591 glueBal fm1 EmptyFM = fm1
593 -- The case analysis here (absent in Adams' program) is really to deal
594 -- with the case where fm2 is a singleton. Then deleting the minimum means
595 -- we pass an empty tree to mkBalBranch, which breaks its invariant.
596 | sizeFM fm2 > sizeFM fm1
597 = mkBalBranch mid_key2 mid_elt2 fm1 (deleteMin fm2)
600 = mkBalBranch mid_key1 mid_elt1 (deleteMax fm1) fm2
602 (mid_key1, mid_elt1) = findMax fm1
603 (mid_key2, mid_elt2) = findMin fm2
606 @glueVBal@ copes with arguments which can be of any size.
607 But: all keys in first arg are $<$ all keys in second.
610 glueVBal :: (Ord key OUTPUTABLE_key)
611 => FiniteMap key elt -> FiniteMap key elt
614 glueVBal EmptyFM fm2 = fm2
615 glueVBal fm1 EmptyFM = fm1
616 glueVBal fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
617 fm_r@(Branch key_r elt_r _ fm_rl fm_rr)
618 | sIZE_RATIO * size_l < size_r
619 = mkBalBranch key_r elt_r (glueVBal fm_l fm_rl) fm_rr
621 | sIZE_RATIO * size_r < size_l
622 = mkBalBranch key_l elt_l fm_ll (glueVBal fm_lr fm_r)
624 | otherwise -- We now need the same two cases as in glueBal above.
627 (mid_key_l,mid_elt_l) = findMax fm_l
628 (mid_key_r,mid_elt_r) = findMin fm_r
633 %************************************************************************
635 \subsection{Local utilities}
637 %************************************************************************
640 splitLT, splitGT :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> FiniteMap key elt
642 -- splitLT fm split_key = fm restricted to keys < split_key
643 -- splitGT fm split_key = fm restricted to keys > split_key
645 splitLT EmptyFM split_key = emptyFM
646 splitLT (Branch key elt _ fm_l fm_r) split_key
647 #ifdef __GLASGOW_HASKELL__
648 = case _tagCmp split_key key of
649 _LT -> splitLT fm_l split_key
650 _GT -> mkVBalBranch key elt fm_l (splitLT fm_r split_key)
653 | split_key < key = splitLT fm_l split_key
654 | split_key > key = mkVBalBranch key elt fm_l (splitLT fm_r split_key)
658 splitGT EmptyFM split_key = emptyFM
659 splitGT (Branch key elt _ fm_l fm_r) split_key
660 #ifdef __GLASGOW_HASKELL__
661 = case _tagCmp split_key key of
662 _GT -> splitGT fm_r split_key
663 _LT -> mkVBalBranch key elt (splitGT fm_l split_key) fm_r
666 | split_key > key = splitGT fm_r split_key
667 | split_key < key = mkVBalBranch key elt (splitGT fm_l split_key) fm_r
671 findMin :: FiniteMap key elt -> (key,elt)
672 findMin (Branch key elt _ EmptyFM _) = (key,elt)
673 findMin (Branch key elt _ fm_l _) = findMin fm_l
675 deleteMin :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
676 deleteMin (Branch key elt _ EmptyFM fm_r) = fm_r
677 deleteMin (Branch key elt _ fm_l fm_r) = mkBalBranch key elt (deleteMin fm_l) fm_r
679 findMax :: FiniteMap key elt -> (key,elt)
680 findMax (Branch key elt _ _ EmptyFM) = (key,elt)
681 findMax (Branch key elt _ _ fm_r) = findMax fm_r
683 deleteMax :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
684 deleteMax (Branch key elt _ fm_l EmptyFM) = fm_l
685 deleteMax (Branch key elt _ fm_l fm_r) = mkBalBranch key elt fm_l (deleteMax fm_r)
688 %************************************************************************
690 \subsection{Output-ery}
692 %************************************************************************
695 #if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)
697 instance (Outputable key) => Outputable (FiniteMap key elt) where
698 ppr sty fm = pprX sty fm
700 pprX sty EmptyFM = ppChar '!'
701 pprX sty (Branch key elt sz fm_l fm_r)
702 = ppBesides [ppLparen, pprX sty fm_l, ppSP,
703 ppr sty key, ppSP, ppInt (IF_GHC(I# sz, sz)), ppSP,
704 pprX sty fm_r, ppRparen]
707 #ifndef COMPILING_GHC
708 instance (Eq key, Eq elt) => Eq (FiniteMap key elt) where
709 fm_1 == fm_2 = (sizeFM fm_1 == sizeFM fm_2) && -- quick test
710 (fmToList fm_1 == fmToList fm_2)
712 {- NO: not clear what The Right Thing to do is:
713 instance (Ord key, Ord elt) => Ord (FiniteMap key elt) where
714 fm_1 <= fm_2 = (sizeFM fm_1 <= sizeFM fm_2) && -- quick test
715 (fmToList fm_1 <= fmToList fm_2)
720 %************************************************************************
722 \subsection{FiniteSets---a thin veneer}
724 %************************************************************************
729 type FiniteSet key = FiniteMap key ()
730 emptySet :: FiniteSet key
731 mkSet :: (Ord key OUTPUTABLE_key) => [key] -> FiniteSet key
732 isEmptySet :: FiniteSet key -> Bool
733 elementOf :: (Ord key OUTPUTABLE_key) => key -> FiniteSet key -> Bool
734 minusSet :: (Ord key OUTPUTABLE_key) => FiniteSet key -> FiniteSet key -> FiniteSet key
735 setToList :: FiniteSet key -> [key]
736 union :: (Ord key OUTPUTABLE_key) => FiniteSet key -> FiniteSet key -> FiniteSet key
739 mkSet xs = listToFM [ (x, ()) | x <- xs]
740 isEmptySet = isEmptyFM
749 %************************************************************************
751 \subsection{Efficiency pragmas for GHC}
753 %************************************************************************
755 When the FiniteMap module is used in GHC, we specialise it for
756 \tr{Uniques}, for dastardly efficiency reasons.
760 #if defined(COMPILING_GHC) && __GLASGOW_HASKELL__
762 {-# SPECIALIZE listToFM
763 :: [(Int,elt)] -> FiniteMap Int elt,
764 [(CLabel,elt)] -> FiniteMap CLabel elt,
765 [(FAST_STRING,elt)] -> FiniteMap FAST_STRING elt,
766 [((FAST_STRING,FAST_STRING),elt)] -> FiniteMap (FAST_STRING, FAST_STRING) elt
767 IF_NCG(COMMA [(Reg COMMA elt)] -> FiniteMap Reg elt)
769 {-# SPECIALIZE addToFM
770 :: FiniteMap Int elt -> Int -> elt -> FiniteMap Int elt,
771 FiniteMap FAST_STRING elt -> FAST_STRING -> elt -> FiniteMap FAST_STRING elt,
772 FiniteMap CLabel elt -> CLabel -> elt -> FiniteMap CLabel elt
773 IF_NCG(COMMA FiniteMap Reg elt -> Reg -> elt -> FiniteMap Reg elt)
775 {-# SPECIALIZE addListToFM
776 :: FiniteMap Int elt -> [(Int,elt)] -> FiniteMap Int elt,
777 FiniteMap CLabel elt -> [(CLabel,elt)] -> FiniteMap CLabel elt
778 IF_NCG(COMMA FiniteMap Reg elt -> [(Reg COMMA elt)] -> FiniteMap Reg elt)
780 {-NOT EXPORTED!! # SPECIALIZE addToFM_C
781 :: (elt -> elt -> elt) -> FiniteMap Int elt -> Int -> elt -> FiniteMap Int elt,
782 (elt -> elt -> elt) -> FiniteMap CLabel elt -> CLabel -> elt -> FiniteMap CLabel elt
783 IF_NCG(COMMA (elt -> elt -> elt) -> FiniteMap Reg elt -> Reg -> elt -> FiniteMap Reg elt)
785 {-# SPECIALIZE addListToFM_C
786 :: (elt -> elt -> elt) -> FiniteMap Int elt -> [(Int,elt)] -> FiniteMap Int elt,
787 (elt -> elt -> elt) -> FiniteMap TyCon elt -> [(TyCon,elt)] -> FiniteMap TyCon elt,
788 (elt -> elt -> elt) -> FiniteMap CLabel elt -> [(CLabel,elt)] -> FiniteMap CLabel elt
789 IF_NCG(COMMA (elt -> elt -> elt) -> FiniteMap Reg elt -> [(Reg COMMA elt)] -> FiniteMap Reg elt)
791 {-NOT EXPORTED!!! # SPECIALIZE delFromFM
792 :: FiniteMap Int elt -> Int -> FiniteMap Int elt,
793 FiniteMap CLabel elt -> CLabel -> FiniteMap CLabel elt
794 IF_NCG(COMMA FiniteMap Reg elt -> Reg -> FiniteMap Reg elt)
796 {-# SPECIALIZE delListFromFM
797 :: FiniteMap Int elt -> [Int] -> FiniteMap Int elt,
798 FiniteMap CLabel elt -> [CLabel] -> FiniteMap CLabel elt
799 IF_NCG(COMMA FiniteMap Reg elt -> [Reg] -> FiniteMap Reg elt)
801 {-# SPECIALIZE elemFM
802 :: FAST_STRING -> FiniteMap FAST_STRING elt -> Bool
804 {-not EXPORTED!!! # SPECIALIZE filterFM
805 :: (Int -> elt -> Bool) -> FiniteMap Int elt -> FiniteMap Int elt,
806 (CLabel -> elt -> Bool) -> FiniteMap CLabel elt -> FiniteMap CLabel elt
807 IF_NCG(COMMA (Reg -> elt -> Bool) -> FiniteMap Reg elt -> FiniteMap Reg elt)
809 {-NOT EXPORTED!!! # SPECIALIZE intersectFM
810 :: FiniteMap Int elt -> FiniteMap Int elt -> FiniteMap Int elt,
811 FiniteMap CLabel elt -> FiniteMap CLabel elt -> FiniteMap CLabel elt
812 IF_NCG(COMMA FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
814 {-not EXPORTED !!!# SPECIALIZE intersectFM_C
815 :: (elt -> elt -> elt) -> FiniteMap Int elt -> FiniteMap Int elt -> FiniteMap Int elt,
816 (elt -> elt -> elt) -> FiniteMap CLabel elt -> FiniteMap CLabel elt -> FiniteMap CLabel elt
817 IF_NCG(COMMA (elt -> elt -> elt) -> FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
819 {-# SPECIALIZE lookupFM
820 :: FiniteMap Int elt -> Int -> Maybe elt,
821 FiniteMap CLabel elt -> CLabel -> Maybe elt,
822 FiniteMap FAST_STRING elt -> FAST_STRING -> Maybe elt,
823 FiniteMap (FAST_STRING,FAST_STRING) elt -> (FAST_STRING,FAST_STRING) -> Maybe elt
824 IF_NCG(COMMA FiniteMap Reg elt -> Reg -> Maybe elt)
826 {-# SPECIALIZE lookupWithDefaultFM
827 :: FiniteMap Int elt -> elt -> Int -> elt,
828 FiniteMap CLabel elt -> elt -> CLabel -> elt
829 IF_NCG(COMMA FiniteMap Reg elt -> elt -> Reg -> elt)
831 {-# SPECIALIZE minusFM
832 :: FiniteMap Int elt -> FiniteMap Int elt -> FiniteMap Int elt,
833 FiniteMap TyCon elt -> FiniteMap TyCon elt -> FiniteMap TyCon elt,
834 FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt,
835 FiniteMap CLabel elt -> FiniteMap CLabel elt -> FiniteMap CLabel elt
836 IF_NCG(COMMA FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
838 {-# SPECIALIZE plusFM
839 :: FiniteMap Int elt -> FiniteMap Int elt -> FiniteMap Int elt,
840 FiniteMap TyCon elt -> FiniteMap TyCon elt -> FiniteMap TyCon elt,
841 FiniteMap CLabel elt -> FiniteMap CLabel elt -> FiniteMap CLabel elt
842 IF_NCG(COMMA FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
844 {-# SPECIALIZE plusFM_C
845 :: (elt -> elt -> elt) -> FiniteMap Int elt -> FiniteMap Int elt -> FiniteMap Int elt,
846 (elt -> elt -> elt) -> FiniteMap CLabel elt -> FiniteMap CLabel elt -> FiniteMap CLabel elt
847 IF_NCG(COMMA (elt -> elt -> elt) -> FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
850 #endif {- compiling for GHC -}