2 % (c) The AQUA Project, Glasgow University, 1994-1995
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}).
21 #if defined(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, singletonFM, listToFM,
42 IF_NOT_GHC(addToFM_C COMMA)
44 IF_NOT_GHC(delFromFM COMMA)
48 IF_NOT_GHC(intersectFM COMMA intersectFM_C COMMA)
49 minusFM, -- exported for GHCI only
51 IF_NOT_GHC(mapFM COMMA foldFM COMMA filterFM COMMA)
53 IF_NOT_GHC(sizeFM COMMA)
54 isEmptyFM, elemFM, lookupFM, lookupWithDefaultFM,
56 fmToList, keysFM, eltsFM{-used in GHCI-}
58 #if defined(COMPILING_GHC)
59 , FiniteSet(..), emptySet, mkSet, isEmptySet
60 , elementOf, setToList, union, minusSet{-exported for GHCI-}
63 -- To make it self-sufficient
71 #if defined(COMPILING_GHC)
76 import CLabelInfo ( CLabel ) -- for specialising
77 #if ! OMIT_NATIVE_CODEGEN
78 import AsmRegAlloc ( Reg ) -- ditto
81 #define IF_NCG(a) {--}
85 -- SIGH: but we use unboxed "sizes"...
86 #if __GLASGOW_HASKELL__
94 %************************************************************************
96 \subsection{The signature of the module}
98 %************************************************************************
102 emptyFM :: FiniteMap key elt
103 singletonFM :: key -> elt -> FiniteMap key elt
104 listToFM :: (Ord key OUTPUTABLE_key) => [(key,elt)] -> FiniteMap key elt
105 -- In the case of duplicates, the last is taken
107 -- ADDING AND DELETING
108 -- Throws away any previous binding
109 -- In the list case, the items are added starting with the
110 -- first one in the list
111 addToFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> elt -> FiniteMap key elt
112 addListToFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> [(key,elt)] -> FiniteMap key elt
114 -- Combines with previous binding
115 addToFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
116 -> FiniteMap key elt -> key -> elt
118 addListToFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
119 -> FiniteMap key elt -> [(key,elt)]
122 -- Deletion doesn't complain if you try to delete something
124 delFromFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> FiniteMap key elt
125 delListFromFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> [key] -> FiniteMap key elt
128 -- Bindings in right argument shadow those in the left
129 plusFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
132 -- Combines bindings for the same thing with the given function
133 plusFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
134 -> FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
136 minusFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
137 -- (minusFM a1 a2) deletes from a1 any bindings which are bound in a2
139 intersectFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
140 intersectFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
141 -> FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
143 -- MAPPING, FOLDING, FILTERING
144 foldFM :: (key -> elt -> a -> a) -> a -> FiniteMap key elt -> a
145 mapFM :: (key -> elt1 -> elt2) -> FiniteMap key elt1 -> FiniteMap key elt2
146 filterFM :: (Ord key OUTPUTABLE_key) => (key -> elt -> Bool)
147 -> FiniteMap key elt -> FiniteMap key elt
150 sizeFM :: FiniteMap key elt -> Int
151 isEmptyFM :: FiniteMap key elt -> Bool
153 elemFM :: (Ord key OUTPUTABLE_key) => key -> FiniteMap key elt -> Bool
154 lookupFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> Maybe elt
156 :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> elt -> key -> elt
157 -- lookupWithDefaultFM supplies a "default" elt
158 -- to return for an unmapped key
161 fmToList :: FiniteMap key elt -> [(key,elt)]
162 keysFM :: FiniteMap key elt -> [key]
163 eltsFM :: FiniteMap key elt -> [elt]
166 %************************************************************************
168 \subsection{The @FiniteMap@ data type, and building of same}
170 %************************************************************************
172 Invariants about @FiniteMap@:
175 all keys in a FiniteMap are distinct
177 all keys in left subtree are $<$ key in Branch and
178 all keys in right subtree are $>$ key in Branch
180 size field of a Branch gives number of Branch nodes in the tree
182 size of left subtree is differs from size of right subtree by a
183 factor of at most \tr{sIZE_RATIO}
187 data FiniteMap key elt
189 | Branch key elt -- Key and elt stored here
190 IF_GHC(Int#,Int{-STRICT-}) -- Size >= 1
191 (FiniteMap key elt) -- Children
199 = Branch bottom bottom IF_GHC(0#,0) bottom bottom
201 bottom = panic "emptyFM"
204 -- #define EmptyFM (Branch _ _ IF_GHC(0#,0) _ _)
206 singletonFM key elt = Branch key elt IF_GHC(1#,1) emptyFM emptyFM
208 listToFM key_elt_pairs = addListToFM emptyFM key_elt_pairs
211 %************************************************************************
213 \subsection{Adding to and deleting from @FiniteMaps@}
215 %************************************************************************
218 addToFM fm key elt = addToFM_C (\ old new -> new) fm key elt
220 addToFM_C combiner EmptyFM key elt = singletonFM key elt
221 addToFM_C combiner (Branch key elt size fm_l fm_r) new_key new_elt
222 #ifdef __GLASGOW_HASKELL__
223 = case _tagCmp new_key key of
224 _LT -> mkBalBranch key elt (addToFM_C combiner fm_l new_key new_elt) fm_r
225 _GT -> mkBalBranch key elt fm_l (addToFM_C combiner fm_r new_key new_elt)
226 _EQ -> Branch new_key (combiner elt new_elt) size fm_l fm_r
228 | new_key < key = mkBalBranch key elt (addToFM_C combiner fm_l new_key new_elt) fm_r
229 | new_key > key = mkBalBranch key elt fm_l (addToFM_C combiner fm_r new_key new_elt)
230 | otherwise = Branch new_key (combiner elt new_elt) size fm_l fm_r
233 addListToFM fm key_elt_pairs = addListToFM_C (\ old new -> new) fm key_elt_pairs
235 addListToFM_C combiner fm key_elt_pairs
236 = foldl add fm key_elt_pairs -- foldl adds from the left
238 add fmap (key,elt) = addToFM_C combiner fmap key elt
242 delFromFM EmptyFM del_key = emptyFM
243 delFromFM (Branch key elt size fm_l fm_r) del_key
244 #ifdef __GLASGOW_HASKELL__
245 = case _tagCmp del_key key of
246 _GT -> mkBalBranch key elt fm_l (delFromFM fm_r del_key)
247 _LT -> mkBalBranch key elt (delFromFM fm_l del_key) fm_r
248 _EQ -> glueBal fm_l fm_r
251 = mkBalBranch key elt fm_l (delFromFM fm_r del_key)
254 = mkBalBranch key elt (delFromFM fm_l del_key) fm_r
260 delListFromFM fm keys = foldl delFromFM fm keys
263 %************************************************************************
265 \subsection{Combining @FiniteMaps@}
267 %************************************************************************
270 plusFM_C combiner EmptyFM fm2 = fm2
271 plusFM_C combiner fm1 EmptyFM = fm1
272 plusFM_C combiner fm1 (Branch split_key elt2 _ left right)
273 = mkVBalBranch split_key new_elt
274 (plusFM_C combiner lts left)
275 (plusFM_C combiner gts right)
277 lts = splitLT fm1 split_key
278 gts = splitGT fm1 split_key
279 new_elt = case lookupFM fm1 split_key of
281 Just elt1 -> combiner elt1 elt2
283 -- It's worth doing plusFM specially, because we don't need
284 -- to do the lookup in fm1.
286 plusFM EmptyFM fm2 = fm2
287 plusFM fm1 EmptyFM = fm1
288 plusFM fm1 (Branch split_key elt1 _ left right)
289 = mkVBalBranch split_key elt1 (plusFM lts left) (plusFM gts right)
291 lts = splitLT fm1 split_key
292 gts = splitGT fm1 split_key
294 minusFM EmptyFM fm2 = emptyFM
295 minusFM fm1 EmptyFM = fm1
296 minusFM fm1 (Branch split_key elt _ left right)
297 = glueVBal (minusFM lts left) (minusFM gts right)
298 -- The two can be way different, so we need glueVBal
300 lts = splitLT fm1 split_key -- NB gt and lt, so the equal ones
301 gts = splitGT fm1 split_key -- are not in either.
303 intersectFM fm1 fm2 = intersectFM_C (\ left right -> right) fm1 fm2
305 intersectFM_C combiner fm1 EmptyFM = emptyFM
306 intersectFM_C combiner EmptyFM fm2 = emptyFM
307 intersectFM_C combiner fm1 (Branch split_key elt2 _ left right)
309 | maybeToBool maybe_elt1 -- split_elt *is* in intersection
310 = mkVBalBranch split_key (combiner elt1 elt2) (intersectFM_C combiner lts left)
311 (intersectFM_C combiner gts right)
313 | otherwise -- split_elt is *not* in intersection
314 = glueVBal (intersectFM_C combiner lts left) (intersectFM_C combiner gts right)
317 lts = splitLT fm1 split_key -- NB gt and lt, so the equal ones
318 gts = splitGT fm1 split_key -- are not in either.
320 maybe_elt1 = lookupFM fm1 split_key
321 Just elt1 = maybe_elt1
324 %************************************************************************
326 \subsection{Mapping, folding, and filtering with @FiniteMaps@}
328 %************************************************************************
331 foldFM k z EmptyFM = z
332 foldFM k z (Branch key elt _ fm_l fm_r)
333 = foldFM k (k key elt (foldFM k z fm_r)) fm_l
335 mapFM f EmptyFM = emptyFM
336 mapFM f (Branch key elt size fm_l fm_r)
337 = Branch key (f key elt) size (mapFM f fm_l) (mapFM f fm_r)
339 filterFM p EmptyFM = emptyFM
340 filterFM p (Branch key elt _ fm_l fm_r)
341 | p key elt -- Keep the item
342 = mkVBalBranch key elt (filterFM p fm_l) (filterFM p fm_r)
344 | otherwise -- Drop the item
345 = glueVBal (filterFM p fm_l) (filterFM p fm_r)
348 %************************************************************************
350 \subsection{Interrogating @FiniteMaps@}
352 %************************************************************************
355 --{-# INLINE sizeFM #-}
357 sizeFM (Branch _ _ size _ _) = IF_GHC(I# size, size)
359 isEmptyFM fm = sizeFM fm == 0
361 lookupFM EmptyFM key = Nothing
362 lookupFM (Branch key elt _ fm_l fm_r) key_to_find
363 #ifdef __GLASGOW_HASKELL__
364 = case _tagCmp key_to_find key of
365 _LT -> lookupFM fm_l key_to_find
366 _GT -> lookupFM fm_r key_to_find
369 | key_to_find < key = lookupFM fm_l key_to_find
370 | key_to_find > key = lookupFM fm_r key_to_find
371 | otherwise = Just elt
375 = case (lookupFM fm key) of { Nothing -> False; Just elt -> True }
377 lookupWithDefaultFM fm deflt key
378 = case (lookupFM fm key) of { Nothing -> deflt; Just elt -> elt }
381 %************************************************************************
383 \subsection{Listifying @FiniteMaps@}
385 %************************************************************************
388 fmToList fm = foldFM (\ key elt rest -> (key,elt) : rest) [] fm
389 keysFM fm = foldFM (\ key elt rest -> key : rest) [] fm
390 eltsFM fm = foldFM (\ key elt rest -> elt : rest) [] fm
394 %************************************************************************
396 \subsection{The implementation of balancing}
398 %************************************************************************
400 %************************************************************************
402 \subsubsection{Basic construction of a @FiniteMap@}
404 %************************************************************************
406 @mkBranch@ simply gets the size component right. This is the ONLY
407 (non-trivial) place the Branch object is built, so the ASSERTion
408 recursively checks consistency. (The trivial use of Branch is in
415 mkBranch :: (Ord key OUTPUTABLE_key) -- Used for the assertion checking only
418 -> FiniteMap key elt -> FiniteMap key elt
421 mkBranch which key elt fm_l fm_r
422 = --ASSERT( left_ok && right_ok && balance_ok )
423 #if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)
424 if not ( left_ok && right_ok && balance_ok ) then
425 pprPanic ("mkBranch:"++show which) (ppAboves [ppr PprDebug [left_ok, right_ok, balance_ok],
432 result = Branch key elt (unbox (1 + left_size + right_size)) fm_l fm_r
434 -- if sizeFM result <= 8 then
437 -- pprTrace ("mkBranch:"++(show which)) (ppr PprDebug result) (
441 left_ok = case fm_l of
443 Branch left_key _ _ _ _ -> let
444 biggest_left_key = fst (findMax fm_l)
446 biggest_left_key < key
447 right_ok = case fm_r of
449 Branch right_key _ _ _ _ -> let
450 smallest_right_key = fst (findMin fm_r)
452 key < smallest_right_key
453 balance_ok = True -- sigh
456 = -- Both subtrees have one or no elements...
457 (left_size + right_size <= 1)
458 -- NO || left_size == 0 -- ???
459 -- NO || right_size == 0 -- ???
460 -- ... or the number of elements in a subtree does not exceed
461 -- sIZE_RATIO times the number of elements in the other subtree
462 || (left_size * sIZE_RATIO >= right_size &&
463 right_size * sIZE_RATIO >= left_size)
466 left_size = sizeFM fm_l
467 right_size = sizeFM fm_r
469 #ifdef __GLASGOW_HASKELL__
471 unbox (I# size) = size
478 %************************************************************************
480 \subsubsection{{\em Balanced} construction of a @FiniteMap@}
482 %************************************************************************
484 @mkBalBranch@ rebalances, assuming that the subtrees aren't too far
488 mkBalBranch :: (Ord key OUTPUTABLE_key)
490 -> FiniteMap key elt -> FiniteMap key elt
493 mkBalBranch key elt fm_L fm_R
495 | size_l + size_r < 2
496 = mkBranch 1{-which-} key elt fm_L fm_R
498 | size_r > sIZE_RATIO * size_l -- Right tree too big
500 Branch _ _ _ fm_rl fm_rr
501 | sizeFM fm_rl < 2 * sizeFM fm_rr -> single_L fm_L fm_R
502 | otherwise -> double_L fm_L fm_R
503 -- Other case impossible
505 | size_l > sIZE_RATIO * size_r -- Left tree too big
507 Branch _ _ _ fm_ll fm_lr
508 | sizeFM fm_lr < 2 * sizeFM fm_ll -> single_R fm_L fm_R
509 | otherwise -> double_R fm_L fm_R
510 -- Other case impossible
512 | otherwise -- No imbalance
513 = mkBranch 2{-which-} key elt fm_L fm_R
519 single_L fm_l (Branch key_r elt_r _ fm_rl fm_rr)
520 = mkBranch 3{-which-} key_r elt_r (mkBranch 4{-which-} key elt fm_l fm_rl) fm_rr
522 double_L fm_l (Branch key_r elt_r _ (Branch key_rl elt_rl _ fm_rll fm_rlr) fm_rr)
523 = mkBranch 5{-which-} key_rl elt_rl (mkBranch 6{-which-} key elt fm_l fm_rll)
524 (mkBranch 7{-which-} key_r elt_r fm_rlr fm_rr)
526 single_R (Branch key_l elt_l _ fm_ll fm_lr) fm_r
527 = mkBranch 8{-which-} key_l elt_l fm_ll (mkBranch 9{-which-} key elt fm_lr fm_r)
529 double_R (Branch key_l elt_l _ fm_ll (Branch key_lr elt_lr _ fm_lrl fm_lrr)) fm_r
530 = mkBranch 10{-which-} key_lr elt_lr (mkBranch 11{-which-} key_l elt_l fm_ll fm_lrl)
531 (mkBranch 12{-which-} key elt fm_lrr fm_r)
536 mkVBalBranch :: (Ord key OUTPUTABLE_key)
538 -> FiniteMap key elt -> FiniteMap key elt
541 -- Assert: in any call to (mkVBalBranch_C comb key elt l r),
542 -- (a) all keys in l are < all keys in r
543 -- (b) all keys in l are < key
544 -- (c) all keys in r are > key
546 mkVBalBranch key elt EmptyFM fm_r = addToFM fm_r key elt
547 mkVBalBranch key elt fm_l EmptyFM = addToFM fm_l key elt
549 mkVBalBranch key elt fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
550 fm_r@(Branch key_r elt_r _ fm_rl fm_rr)
551 | sIZE_RATIO * size_l < size_r
552 = mkBalBranch key_r elt_r (mkVBalBranch key elt fm_l fm_rl) fm_rr
554 | sIZE_RATIO * size_r < size_l
555 = mkBalBranch key_l elt_l fm_ll (mkVBalBranch key elt fm_lr fm_r)
558 = mkBranch 13{-which-} key elt fm_l fm_r
565 %************************************************************************
567 \subsubsection{Gluing two trees together}
569 %************************************************************************
571 @glueBal@ assumes its two arguments aren't too far out of whack, just
572 like @mkBalBranch@. But: all keys in first arg are $<$ all keys in
576 glueBal :: (Ord key OUTPUTABLE_key)
577 => FiniteMap key elt -> FiniteMap key elt
580 glueBal EmptyFM fm2 = fm2
581 glueBal fm1 EmptyFM = fm1
583 -- The case analysis here (absent in Adams' program) is really to deal
584 -- with the case where fm2 is a singleton. Then deleting the minimum means
585 -- we pass an empty tree to mkBalBranch, which breaks its invariant.
586 | sizeFM fm2 > sizeFM fm1
587 = mkBalBranch mid_key2 mid_elt2 fm1 (deleteMin fm2)
590 = mkBalBranch mid_key1 mid_elt1 (deleteMax fm1) fm2
592 (mid_key1, mid_elt1) = findMax fm1
593 (mid_key2, mid_elt2) = findMin fm2
596 @glueVBal@ copes with arguments which can be of any size.
597 But: all keys in first arg are $<$ all keys in second.
600 glueVBal :: (Ord key OUTPUTABLE_key)
601 => FiniteMap key elt -> FiniteMap key elt
604 glueVBal EmptyFM fm2 = fm2
605 glueVBal fm1 EmptyFM = fm1
606 glueVBal fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
607 fm_r@(Branch key_r elt_r _ fm_rl fm_rr)
608 | sIZE_RATIO * size_l < size_r
609 = mkBalBranch key_r elt_r (glueVBal fm_l fm_rl) fm_rr
611 | sIZE_RATIO * size_r < size_l
612 = mkBalBranch key_l elt_l fm_ll (glueVBal fm_lr fm_r)
614 | otherwise -- We now need the same two cases as in glueBal above.
617 (mid_key_l,mid_elt_l) = findMax fm_l
618 (mid_key_r,mid_elt_r) = findMin fm_r
623 %************************************************************************
625 \subsection{Local utilities}
627 %************************************************************************
630 splitLT, splitGT :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> FiniteMap key elt
632 -- splitLT fm split_key = fm restricted to keys < split_key
633 -- splitGE fm split_key = fm restricted to keys >= split_key (UNUSED)
634 -- splitGT fm split_key = fm restricted to keys > split_key
636 splitLT EmptyFM split_key = emptyFM
637 splitLT (Branch key elt _ fm_l fm_r) split_key
638 #ifdef __GLASGOW_HASKELL__
639 = case _tagCmp split_key key of
640 _LT -> splitLT fm_l split_key
641 _GT -> mkVBalBranch key elt fm_l (splitLT fm_r split_key)
644 | split_key < key = splitLT fm_l split_key
645 | split_key > key = mkVBalBranch key elt fm_l (splitLT fm_r split_key)
650 splitGE EmptyFM split_key = emptyFM
651 splitGE (Branch key elt _ fm_l fm_r) split_key
652 #ifdef __GLASGOW_HASKELL__
653 = case _tagCmp split_key key of
654 _GT -> splitGE fm_r split_key
655 _LT -> mkVBalBranch key elt (splitGE fm_l split_key) fm_r
656 _EQ -> mkVBalBranch key elt emptyFM fm_r
658 | split_key > key = splitGE fm_r split_key
659 | split_key < key = mkVBalBranch key elt (splitGE fm_l split_key) fm_r
660 | otherwise = mkVBalBranch key elt emptyFM fm_r
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)
703 {- this is the real one actually...
704 instance (Outputable key, Outputable elt) => Outputable (FiniteMap key elt) where
705 ppr sty fm = ppr sty (fmToList fm)
708 -- temp debugging (ToDo: rm)
709 instance (Outputable key) => Outputable (FiniteMap key elt) where
710 ppr sty fm = pprX sty fm
712 pprX sty EmptyFM = ppChar '!'
713 pprX sty (Branch key elt sz fm_l fm_r)
714 = ppBesides [ppLparen, pprX sty fm_l, ppSP,
715 ppr sty key, ppSP, ppInt (IF_GHC(I# sz, sz)), ppSP,
716 pprX sty fm_r, ppRparen]
719 #if !defined(COMPILING_GHC)
720 instance (Eq key, Eq elt) => Eq (FiniteMap key elt) where
721 fm_1 == fm_2 = (sizeFM fm_1 == sizeFM fm_2) && -- quick test
722 (fmToList fm_1 == fmToList fm_2)
724 {- NO: not clear what The Right Thing to do is:
725 instance (Ord key, Ord elt) => Ord (FiniteMap key elt) where
726 fm_1 <= fm_2 = (sizeFM fm_1 <= sizeFM fm_2) && -- quick test
727 (fmToList fm_1 <= fmToList fm_2)
732 %************************************************************************
734 \subsection{FiniteSets---a thin veneer}
736 %************************************************************************
739 #if defined(COMPILING_GHC)
741 type FiniteSet key = FiniteMap key ()
742 emptySet :: FiniteSet key
743 mkSet :: (Ord key OUTPUTABLE_key) => [key] -> FiniteSet key
744 isEmptySet :: FiniteSet key -> Bool
745 elementOf :: (Ord key OUTPUTABLE_key) => key -> FiniteSet key -> Bool
746 minusSet :: (Ord key OUTPUTABLE_key) => FiniteSet key -> FiniteSet key -> FiniteSet key
747 setToList :: FiniteSet key -> [key]
748 union :: (Ord key OUTPUTABLE_key) => FiniteSet key -> FiniteSet key -> FiniteSet key
751 mkSet xs = listToFM [ (x, ()) | x <- xs]
752 isEmptySet = isEmptyFM
761 %************************************************************************
763 \subsection{Efficiency pragmas for GHC}
765 %************************************************************************
767 When the FiniteMap module is used in GHC, we specialise it for
768 \tr{Uniques}, for dastardly efficiency reasons.
771 #if defined(COMPILING_GHC) && __GLASGOW_HASKELL__
772 -- the __GLASGOW_HASKELL__ chk avoids an hbc 0.999.7 bug
774 {-# SPECIALIZE listToFM
775 :: [(Int,elt)] -> FiniteMap Int elt,
776 [(CLabel,elt)] -> FiniteMap CLabel elt,
777 [(FAST_STRING,elt)] -> FiniteMap FAST_STRING elt,
778 [((FAST_STRING,FAST_STRING),elt)] -> FiniteMap (FAST_STRING, FAST_STRING) elt
779 IF_NCG(COMMA [(Reg COMMA elt)] -> FiniteMap Reg elt)
781 {-# SPECIALIZE addToFM
782 :: FiniteMap Int elt -> Int -> elt -> FiniteMap Int elt,
783 FiniteMap FAST_STRING elt -> FAST_STRING -> elt -> FiniteMap FAST_STRING elt,
784 FiniteMap CLabel elt -> CLabel -> elt -> FiniteMap CLabel elt
785 IF_NCG(COMMA FiniteMap Reg elt -> Reg -> elt -> FiniteMap Reg elt)
787 {-# SPECIALIZE addListToFM
788 :: FiniteMap Int elt -> [(Int,elt)] -> FiniteMap Int elt,
789 FiniteMap CLabel elt -> [(CLabel,elt)] -> FiniteMap CLabel elt
790 IF_NCG(COMMA FiniteMap Reg elt -> [(Reg COMMA elt)] -> FiniteMap Reg elt)
792 {-NOT EXPORTED!! # SPECIALIZE addToFM_C
793 :: (elt -> elt -> elt) -> FiniteMap Int elt -> Int -> elt -> FiniteMap Int elt,
794 (elt -> elt -> elt) -> FiniteMap CLabel elt -> CLabel -> elt -> FiniteMap CLabel elt
795 IF_NCG(COMMA (elt -> elt -> elt) -> FiniteMap Reg elt -> Reg -> elt -> FiniteMap Reg elt)
797 {-# SPECIALIZE addListToFM_C
798 :: (elt -> elt -> elt) -> FiniteMap Int elt -> [(Int,elt)] -> FiniteMap Int elt,
799 (elt -> elt -> elt) -> FiniteMap TyCon elt -> [(TyCon,elt)] -> FiniteMap TyCon elt,
800 (elt -> elt -> elt) -> FiniteMap CLabel elt -> [(CLabel,elt)] -> FiniteMap CLabel elt
801 IF_NCG(COMMA (elt -> elt -> elt) -> FiniteMap Reg elt -> [(Reg COMMA elt)] -> FiniteMap Reg elt)
803 {-NOT EXPORTED!!! # SPECIALIZE delFromFM
804 :: FiniteMap Int elt -> Int -> FiniteMap Int elt,
805 FiniteMap CLabel elt -> CLabel -> FiniteMap CLabel elt
806 IF_NCG(COMMA FiniteMap Reg elt -> Reg -> FiniteMap Reg elt)
808 {-# SPECIALIZE delListFromFM
809 :: FiniteMap Int elt -> [Int] -> FiniteMap Int elt,
810 FiniteMap CLabel elt -> [CLabel] -> FiniteMap CLabel elt
811 IF_NCG(COMMA FiniteMap Reg elt -> [Reg] -> FiniteMap Reg elt)
813 {-# SPECIALIZE elemFM
814 :: FAST_STRING -> FiniteMap FAST_STRING elt -> Bool
816 {-not EXPORTED!!! # SPECIALIZE filterFM
817 :: (Int -> elt -> Bool) -> FiniteMap Int elt -> FiniteMap Int elt,
818 (CLabel -> elt -> Bool) -> FiniteMap CLabel elt -> FiniteMap CLabel elt
819 IF_NCG(COMMA (Reg -> elt -> Bool) -> FiniteMap Reg elt -> FiniteMap Reg elt)
821 {-NOT EXPORTED!!! # SPECIALIZE intersectFM
822 :: FiniteMap Int elt -> FiniteMap Int elt -> FiniteMap Int elt,
823 FiniteMap CLabel elt -> FiniteMap CLabel elt -> FiniteMap CLabel elt
824 IF_NCG(COMMA FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
826 {-not EXPORTED !!!# SPECIALIZE intersectFM_C
827 :: (elt -> elt -> elt) -> FiniteMap Int elt -> FiniteMap Int elt -> FiniteMap Int elt,
828 (elt -> elt -> elt) -> FiniteMap CLabel elt -> FiniteMap CLabel elt -> FiniteMap CLabel elt
829 IF_NCG(COMMA (elt -> elt -> elt) -> FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
831 {-# SPECIALIZE lookupFM
832 :: FiniteMap Int elt -> Int -> Maybe elt,
833 FiniteMap CLabel elt -> CLabel -> Maybe elt,
834 FiniteMap FAST_STRING elt -> FAST_STRING -> Maybe elt,
835 FiniteMap (FAST_STRING,FAST_STRING) elt -> (FAST_STRING,FAST_STRING) -> Maybe elt
836 IF_NCG(COMMA FiniteMap Reg elt -> Reg -> Maybe elt)
838 {-# SPECIALIZE lookupWithDefaultFM
839 :: FiniteMap Int elt -> elt -> Int -> elt,
840 FiniteMap CLabel elt -> elt -> CLabel -> elt
841 IF_NCG(COMMA FiniteMap Reg elt -> elt -> Reg -> elt)
843 {-# SPECIALIZE minusFM
844 :: FiniteMap Int elt -> FiniteMap Int elt -> FiniteMap Int elt,
845 FiniteMap TyCon elt -> FiniteMap TyCon elt -> FiniteMap TyCon elt,
846 FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt,
847 FiniteMap CLabel elt -> FiniteMap CLabel elt -> FiniteMap CLabel elt
848 IF_NCG(COMMA FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
850 {-# SPECIALIZE plusFM
851 :: FiniteMap Int elt -> FiniteMap Int elt -> FiniteMap Int elt,
852 FiniteMap TyCon elt -> FiniteMap TyCon elt -> FiniteMap TyCon elt,
853 FiniteMap CLabel elt -> FiniteMap CLabel elt -> FiniteMap CLabel elt
854 IF_NCG(COMMA FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
856 {-# SPECIALIZE plusFM_C
857 :: (elt -> elt -> elt) -> FiniteMap Int elt -> FiniteMap Int elt -> FiniteMap Int elt,
858 (elt -> elt -> elt) -> FiniteMap CLabel elt -> FiniteMap CLabel elt -> FiniteMap CLabel elt
859 IF_NCG(COMMA (elt -> elt -> elt) -> FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
862 #endif {- compiling for GHC -}