2 % (c) The AQUA Project, Glasgow University, 1994-1998
4 \section[UniqFM]{Specialised finite maps, for things with @Uniques@}
6 Based on @FiniteMaps@ (as you would expect).
8 Basically, the things need to be in class @Uniquable@, and we use the
9 @getUnique@ method to grab their @Uniques@.
11 (A similar thing to @UniqSet@, as opposed to @Set@.)
15 UniqFM, -- abstract type
23 addListToUFM,addListToUFM_C,
25 addListToUFM_Directly,
41 lookupUFM, lookupUFM_Directly,
42 lookupWithDefaultUFM, lookupWithDefaultUFM_Directly,
47 #include "HsVersions.h"
49 import {-# SOURCE #-} Name ( Name )
51 import Unique ( Uniquable(..), Unique, getKey#, mkUniqueGrimily )
56 import GLAEXTS -- Lots of Int# operations
59 %************************************************************************
61 \subsection{The @UniqFM@ type, and signatures for the functions}
63 %************************************************************************
65 We use @FiniteMaps@, with a (@getUnique@-able) @Unique@ as ``key''.
68 emptyUFM :: UniqFM elt
69 isNullUFM :: UniqFM elt -> Bool
70 unitUFM :: Uniquable key => key -> elt -> UniqFM elt
71 unitDirectlyUFM -- got the Unique already
72 :: Unique -> elt -> UniqFM elt
73 listToUFM :: Uniquable key => [(key,elt)] -> UniqFM elt
75 :: [(Unique, elt)] -> UniqFM elt
77 addToUFM :: Uniquable key => UniqFM elt -> key -> elt -> UniqFM elt
78 addListToUFM :: Uniquable key => UniqFM elt -> [(key,elt)] -> UniqFM elt
80 :: UniqFM elt -> Unique -> elt -> UniqFM elt
82 addToUFM_C :: Uniquable key => (elt -> elt -> elt) -- old -> new -> result
85 -> UniqFM elt -- result
87 addListToUFM_C :: Uniquable key => (elt -> elt -> elt)
88 -> UniqFM elt -> [(key,elt)]
91 delFromUFM :: Uniquable key => UniqFM elt -> key -> UniqFM elt
92 delListFromUFM :: Uniquable key => UniqFM elt -> [key] -> UniqFM elt
93 delFromUFM_Directly :: UniqFM elt -> Unique -> UniqFM elt
95 plusUFM :: UniqFM elt -> UniqFM elt -> UniqFM elt
97 plusUFM_C :: (elt -> elt -> elt)
98 -> UniqFM elt -> UniqFM elt -> UniqFM elt
100 minusUFM :: UniqFM elt1 -> UniqFM elt2 -> UniqFM elt1
102 intersectUFM :: UniqFM elt -> UniqFM elt -> UniqFM elt
103 intersectUFM_C :: (elt1 -> elt2 -> elt3)
104 -> UniqFM elt1 -> UniqFM elt2 -> UniqFM elt3
105 foldUFM :: (elt -> a -> a) -> a -> UniqFM elt -> a
106 mapUFM :: (elt1 -> elt2) -> UniqFM elt1 -> UniqFM elt2
107 filterUFM :: (elt -> Bool) -> UniqFM elt -> UniqFM elt
109 sizeUFM :: UniqFM elt -> Int
110 hashUFM :: UniqFM elt -> Int
111 elemUFM :: Uniquable key => key -> UniqFM elt -> Bool
113 lookupUFM :: Uniquable key => UniqFM elt -> key -> Maybe elt
114 lookupUFM_Directly -- when you've got the Unique already
115 :: UniqFM elt -> Unique -> Maybe elt
117 :: Uniquable key => UniqFM elt -> elt -> key -> elt
118 lookupWithDefaultUFM_Directly
119 :: UniqFM elt -> elt -> Unique -> elt
121 keysUFM :: UniqFM elt -> [Unique] -- Get the keys
122 eltsUFM :: UniqFM elt -> [elt]
123 ufmToList :: UniqFM elt -> [(Unique, elt)]
126 %************************************************************************
128 \subsection{The @IdFinMap@ and @TyVarFinMap@ specialisations for Ids/TyVars}
130 %************************************************************************
133 -- Turn off for now, these need to be updated (SDM 4/98)
136 #ifdef __GLASGOW_HASKELL__
137 -- I don't think HBC was too happy about this (WDP 94/10)
140 addListToUFM :: UniqFM elt -> [(Name, elt)] -> UniqFM elt
143 addListToUFM_C :: (elt -> elt -> elt) -> UniqFM elt -> [(Name, elt)] -> UniqFM elt
146 addToUFM :: UniqFM elt -> Unique -> elt -> UniqFM elt
149 listToUFM :: [(Unique, elt)] -> UniqFM elt
152 lookupUFM :: UniqFM elt -> Name -> Maybe elt
153 , UniqFM elt -> Unique -> Maybe elt
156 #endif /* __GLASGOW_HASKELL__ */
160 %************************************************************************
162 \subsection{Andy Gill's underlying @UniqFM@ machinery}
164 %************************************************************************
166 ``Uniq Finite maps'' are the heart and soul of the compiler's
167 lookup-tables/environments. Important stuff! It works well with
168 Dense and Sparse ranges.
169 Both @Uq@ Finite maps and @Hash@ Finite Maps
170 are built ontop of Int Finite Maps.
172 This code is explained in the paper:
174 A Gill, S Peyton Jones, B O'Sullivan, W Partain and Aqua Friends
175 "A Cheap balancing act that grows on a tree"
176 Glasgow FP Workshop, Sep 1994, pp??-??
179 %************************************************************************
181 \subsubsection{The @UniqFM@ type, and signatures for the functions}
183 %************************************************************************
185 @UniqFM a@ is a mapping from Unique to a.
187 First, the DataType itself; which is either a Node, a Leaf, or an Empty.
192 | LeafUFM FastInt ele
193 | NodeUFM FastInt -- the switching
199 -- for debugging only :-)
200 instance Outputable (UniqFM a) where
201 ppr(NodeUFM a b t1 t2) =
202 sep [text "NodeUFM " <+> int IBOX(a) <+> int IBOX(b),
203 nest 1 (parens (ppr t1)),
204 nest 1 (parens (ppr t2))]
205 ppr (LeafUFM x a) = text "LeafUFM " <+> int IBOX(x)
206 ppr (EmptyUFM) = empty
208 -- and when not debugging the package itself...
209 instance Outputable a => Outputable (UniqFM a) where
210 ppr ufm = ppr (ufmToList ufm)
213 %************************************************************************
215 \subsubsection{The @UniqFM@ functions}
217 %************************************************************************
219 First the ways of building a UniqFM.
223 unitUFM key elt = mkLeafUFM (getKey# (getUnique key)) elt
224 unitDirectlyUFM key elt = mkLeafUFM (getKey# key) elt
226 listToUFM key_elt_pairs
227 = addListToUFM_C use_snd EmptyUFM key_elt_pairs
229 listToUFM_Directly uniq_elt_pairs
230 = addListToUFM_directly_C use_snd EmptyUFM uniq_elt_pairs
233 Now ways of adding things to UniqFMs.
235 There is an alternative version of @addListToUFM_C@, that uses @plusUFM@,
236 but the semantics of this operation demands a linear insertion;
237 perhaps the version without the combinator function
238 could be optimised using it.
241 addToUFM fm key elt = addToUFM_C use_snd fm key elt
243 addToUFM_Directly fm u elt = insert_ele use_snd fm (getKey# u) elt
245 addToUFM_C combiner fm key elt
246 = insert_ele combiner fm (getKey# (getUnique key)) elt
248 addListToUFM fm key_elt_pairs = addListToUFM_C use_snd fm key_elt_pairs
249 addListToUFM_Directly fm uniq_elt_pairs = addListToUFM_directly_C use_snd fm uniq_elt_pairs
251 addListToUFM_C combiner fm key_elt_pairs
252 = foldl (\ fm (k, e) -> insert_ele combiner fm (getKey# (getUnique k)) e)
255 addListToUFM_directly_C combiner fm uniq_elt_pairs
256 = foldl (\ fm (k, e) -> insert_ele combiner fm (getKey# k) e)
260 Now ways of removing things from UniqFM.
263 delListFromUFM fm lst = foldl delFromUFM fm lst
265 delFromUFM fm key = delete fm (getKey# (getUnique key))
266 delFromUFM_Directly fm u = delete fm (getKey# u)
268 delete EmptyUFM _ = EmptyUFM
269 delete fm key = del_ele fm
271 del_ele :: UniqFM a -> UniqFM a
273 del_ele lf@(LeafUFM j _)
274 | j ==# key = EmptyUFM
275 | otherwise = lf -- no delete!
277 del_ele nd@(NodeUFM j p t1 t2)
279 = mkSLNodeUFM (NodeUFMData j p) (del_ele t1) t2
281 = mkLSNodeUFM (NodeUFMData j p) t1 (del_ele t2)
283 del_ele _ = panic "Found EmptyUFM FM when rec-deleting"
286 Now ways of adding two UniqFM's together.
289 plusUFM tr1 tr2 = plusUFM_C use_snd tr1 tr2
291 plusUFM_C f EmptyUFM tr = tr
292 plusUFM_C f tr EmptyUFM = tr
293 plusUFM_C f fm1 fm2 = mix_trees fm1 fm2
295 mix_trees (LeafUFM i a) t2 = insert_ele (flip f) t2 i a
296 mix_trees t1 (LeafUFM i a) = insert_ele f t1 i a
298 mix_trees left_t@(NodeUFM j p t1 t2) right_t@(NodeUFM j' p' t1' t2')
300 (ask_about_common_ancestor
304 -- Given a disjoint j,j' (p >^ p' && p' >^ p):
308 -- t1 t2 t1' t2' j j'
313 mix_branches (NewRoot nd False)
314 = mkLLNodeUFM nd left_t right_t
315 mix_branches (NewRoot nd True)
316 = mkLLNodeUFM nd right_t left_t
322 -- t1 t2 t1' t2' t1 + t1' t2 + t2'
324 mix_branches (SameRoot)
325 = mkSSNodeUFM (NodeUFMData j p)
328 -- Now the 4 different other ways; all like this:
330 -- Given j >^ j' (and, say, j > j')
334 -- t1 t2 t1' t2' t1 t2 + j'
337 mix_branches (LeftRoot Leftt) -- | trace "LL" True
340 (mix_trees t1 right_t)
343 mix_branches (LeftRoot Rightt) -- | trace "LR" True
347 (mix_trees t2 right_t)
349 mix_branches (RightRoot Leftt) -- | trace "RL" True
352 (mix_trees left_t t1')
355 mix_branches (RightRoot Rightt) -- | trace "RR" True
359 (mix_trees left_t t2')
361 mix_trees _ _ = panic "EmptyUFM found when inserting into plusInt"
364 And ways of subtracting them. First the base cases,
365 then the full D&C approach.
368 minusUFM EmptyUFM _ = EmptyUFM
369 minusUFM t1 EmptyUFM = t1
370 minusUFM fm1 fm2 = minus_trees fm1 fm2
373 -- Notice the asymetry of subtraction
375 minus_trees lf@(LeafUFM i a) t2 =
380 minus_trees t1 (LeafUFM i _) = delete t1 i
382 minus_trees left_t@(NodeUFM j p t1 t2) right_t@(NodeUFM j' p' t1' t2')
384 (ask_about_common_ancestor
388 -- Given a disjoint j,j' (p >^ p' && p' >^ p):
392 -- t1 t2 t1' t2' t1 t2
397 minus_branches (NewRoot nd _) = left_t
403 -- t1 t2 t1' t2' t1 + t1' t2 + t2'
405 minus_branches (SameRoot)
406 = mkSSNodeUFM (NodeUFMData j p)
409 -- Now the 4 different other ways; all like this:
410 -- again, with asymatry
413 -- The left is above the right
415 minus_branches (LeftRoot Leftt)
418 (minus_trees t1 right_t)
420 minus_branches (LeftRoot Rightt)
424 (minus_trees t2 right_t)
427 -- The right is above the left
429 minus_branches (RightRoot Leftt)
430 = minus_trees left_t t1'
431 minus_branches (RightRoot Rightt)
432 = minus_trees left_t t2'
434 minus_trees _ _ = panic "EmptyUFM found when insering into plusInt"
437 And taking the intersection of two UniqFM's.
440 intersectUFM t1 t2 = intersectUFM_C use_snd t1 t2
442 intersectUFM_C f EmptyUFM _ = EmptyUFM
443 intersectUFM_C f _ EmptyUFM = EmptyUFM
444 intersectUFM_C f fm1 fm2 = intersect_trees fm1 fm2
446 intersect_trees (LeafUFM i a) t2 =
449 Just b -> mkLeafUFM i (f a b)
451 intersect_trees t1 (LeafUFM i a) =
454 Just b -> mkLeafUFM i (f b a)
456 intersect_trees left_t@(NodeUFM j p t1 t2) right_t@(NodeUFM j' p' t1' t2')
458 (ask_about_common_ancestor
462 -- Given a disjoint j,j' (p >^ p' && p' >^ p):
465 -- / \ + / \ ==> EmptyUFM
470 intersect_branches (NewRoot nd _) = EmptyUFM
476 -- t1 t2 t1' t2' t1 x t1' t2 x t2'
478 intersect_branches (SameRoot)
479 = mkSSNodeUFM (NodeUFMData j p)
480 (intersect_trees t1 t1')
481 (intersect_trees t2 t2')
482 -- Now the 4 different other ways; all like this:
484 -- Given j >^ j' (and, say, j > j')
488 -- t1 t2 t1' t2' t1' t2'
490 -- This does cut down the search space quite a bit.
492 intersect_branches (LeftRoot Leftt)
493 = intersect_trees t1 right_t
494 intersect_branches (LeftRoot Rightt)
495 = intersect_trees t2 right_t
496 intersect_branches (RightRoot Leftt)
497 = intersect_trees left_t t1'
498 intersect_branches (RightRoot Rightt)
499 = intersect_trees left_t t2'
501 intersect_trees x y = panic ("EmptyUFM found when intersecting trees")
504 Now the usual set of `collection' operators, like map, fold, etc.
507 foldUFM f a (NodeUFM _ _ t1 t2) = foldUFM f (foldUFM f a t2) t1
508 foldUFM f a (LeafUFM _ obj) = f obj a
509 foldUFM f a EmptyUFM = a
513 mapUFM fn EmptyUFM = EmptyUFM
514 mapUFM fn fm = map_tree fn fm
516 filterUFM fn EmptyUFM = EmptyUFM
517 filterUFM fn fm = filter_tree fn fm
520 Note, this takes a long time, O(n), but
521 because we dont want to do this very often, we put up with this.
522 O'rable, but how often do we look at the size of
527 sizeUFM (NodeUFM _ _ t1 t2) = sizeUFM t1 + sizeUFM t2
528 sizeUFM (LeafUFM _ _) = 1
530 isNullUFM EmptyUFM = True
533 -- hashing is used in VarSet.uniqAway, and should be fast
534 -- We use a cheap and cheerful method for now
536 hashUFM (NodeUFM n _ _ _) = iBox n
537 hashUFM (LeafUFM n _) = iBox n
540 looking up in a hurry is the {\em whole point} of this binary tree lark.
541 Lookup up a binary tree is easy (and fast).
544 elemUFM key fm = case lookUp fm (getKey# (getUnique key)) of
548 lookupUFM fm key = lookUp fm (getKey# (getUnique key))
549 lookupUFM_Directly fm key = lookUp fm (getKey# key)
551 lookupWithDefaultUFM fm deflt key
552 = case lookUp fm (getKey# (getUnique key)) of
556 lookupWithDefaultUFM_Directly fm deflt key
557 = case lookUp fm (getKey# key) of
561 lookUp EmptyUFM _ = Nothing
562 lookUp fm i = lookup_tree fm
564 lookup_tree :: UniqFM a -> Maybe a
566 lookup_tree (LeafUFM j b)
568 | otherwise = Nothing
569 lookup_tree (NodeUFM j p t1 t2)
570 | j ># i = lookup_tree t1
571 | otherwise = lookup_tree t2
573 lookup_tree EmptyUFM = panic "lookup Failed"
576 folds are *wonderful* things.
579 eltsUFM fm = foldUFM (:) [] fm
581 ufmToList fm = fold_tree (\ iu elt rest -> (mkUniqueGrimily (iBox iu), elt) : rest) [] fm
583 keysUFM fm = fold_tree (\ iu elt rest -> mkUniqueGrimily (iBox iu) : rest) [] fm
585 fold_tree f a (NodeUFM _ _ t1 t2) = fold_tree f (fold_tree f a t2) t1
586 fold_tree f a (LeafUFM iu obj) = f iu obj a
587 fold_tree f a EmptyUFM = a
590 %************************************************************************
592 \subsubsection{The @UniqFM@ type, and its functions}
594 %************************************************************************
596 You should always use these to build the tree.
597 There are 4 versions of mkNodeUFM, depending on
598 the strictness of the two sub-tree arguments.
599 The strictness is used *both* to prune out
600 empty trees, *and* to improve performance,
601 stoping needless thunks lying around.
602 The rule of thumb (from experence with these trees)
603 is make thunks strict, but data structures lazy.
604 If in doubt, use mkSSNodeUFM, which has the `strongest'
605 functionality, but may do a few needless evaluations.
608 mkLeafUFM :: FastInt -> a -> UniqFM a
609 mkLeafUFM i a = LeafUFM i a
611 -- The *ONLY* ways of building a NodeUFM.
613 mkSSNodeUFM (NodeUFMData j p) EmptyUFM t2 = t2
614 mkSSNodeUFM (NodeUFMData j p) t1 EmptyUFM = t1
615 mkSSNodeUFM (NodeUFMData j p) t1 t2
616 = ASSERT(correctNodeUFM (iBox j) (iBox p) t1 t2)
619 mkSLNodeUFM (NodeUFMData j p) EmptyUFM t2 = t2
620 mkSLNodeUFM (NodeUFMData j p) t1 t2
621 = ASSERT(correctNodeUFM (iBox j) (iBox p) t1 t2)
624 mkLSNodeUFM (NodeUFMData j p) t1 EmptyUFM = t1
625 mkLSNodeUFM (NodeUFMData j p) t1 t2
626 = ASSERT(correctNodeUFM (iBox j) (iBox p) t1 t2)
629 mkLLNodeUFM (NodeUFMData j p) t1 t2
630 = ASSERT(correctNodeUFM (iBox j) (iBox p) t1 t2)
640 correctNodeUFM j p t1 t2
641 = correct (j-p) (j-1) p t1 && correct j ((j-1)+p) p t2
643 correct low high _ (LeafUFM i _)
644 = low <= iBox i && iBox i <= high
645 correct low high above_p (NodeUFM j p _ _)
646 = low <= iBox j && iBox j <= high && above_p > iBox p
647 correct _ _ _ EmptyUFM = panic "EmptyUFM stored inside a tree"
650 Note: doing SAT on this by hand seems to make it worse. Todo: Investigate,
651 and if necessary do $\lambda$ lifting on our functions that are bound.
661 insert_ele f EmptyUFM i new = mkLeafUFM i new
663 insert_ele f (LeafUFM j old) i new
665 mkLLNodeUFM (getCommonNodeUFMData
670 | j ==# i = mkLeafUFM j (f old new)
672 mkLLNodeUFM (getCommonNodeUFMData
678 insert_ele f n@(NodeUFM j p t1 t2) i a
680 = if (i >=# (j -# p))
681 then mkSLNodeUFM (NodeUFMData j p) (insert_ele f t1 i a) t2
682 else mkLLNodeUFM (getCommonNodeUFMData
688 = if (i <=# ((j -# _ILIT(1)) +# p))
689 then mkLSNodeUFM (NodeUFMData j p) t1 (insert_ele f t2 i a)
690 else mkLLNodeUFM (getCommonNodeUFMData
700 map_tree f (NodeUFM j p t1 t2)
701 = mkSSNodeUFM (NodeUFMData j p) (map_tree f t1) (map_tree f t2)
702 map_tree f (LeafUFM i obj)
703 = mkLeafUFM i (f obj)
705 map_tree f _ = panic "map_tree failed"
709 filter_tree f nd@(NodeUFM j p t1 t2)
710 = mkSSNodeUFM (NodeUFMData j p) (filter_tree f t1) (filter_tree f t2)
712 filter_tree f lf@(LeafUFM i obj)
714 | otherwise = EmptyUFM
715 filter_tree f _ = panic "filter_tree failed"
718 %************************************************************************
720 \subsubsection{The @UniqFM@ type, and signatures for the functions}
722 %************************************************************************
726 This is the information that is held inside a NodeUFM, packaged up for
731 = NodeUFMData FastInt
735 This is the information used when computing new NodeUFMs.
738 data Side = Leftt | Rightt -- NB: avoid 1.3 names "Left" and "Right"
740 = LeftRoot Side -- which side is the right down ?
741 | RightRoot Side -- which side is the left down ?
742 | SameRoot -- they are the same !
743 | NewRoot NodeUFMData -- here's the new, common, root
744 Bool -- do you need to swap left and right ?
747 This specifies the relationship between NodeUFMData and CalcNodeUFMData.
750 indexToRoot :: FastInt -> NodeUFMData
754 l = (_ILIT(1) :: FastInt)
756 NodeUFMData (((i `shiftR_` l) `shiftL_` l) +# _ILIT(1)) l
758 getCommonNodeUFMData :: NodeUFMData -> NodeUFMData -> NodeUFMData
760 getCommonNodeUFMData (NodeUFMData i p) (NodeUFMData i2 p2)
761 | p ==# p2 = getCommonNodeUFMData_ p j j2
762 | p <# p2 = getCommonNodeUFMData_ p2 (j `quotFastInt` (p2 `quotFastInt` p)) j2
763 | otherwise = getCommonNodeUFMData_ p j (j2 `quotFastInt` (p `quotFastInt` p2))
765 l = (_ILIT(1) :: FastInt)
766 j = i `quotFastInt` (p `shiftL_` l)
767 j2 = i2 `quotFastInt` (p2 `shiftL_` l)
769 getCommonNodeUFMData_ :: FastInt -> FastInt -> FastInt -> NodeUFMData
771 getCommonNodeUFMData_ p j j_
773 = NodeUFMData (((j `shiftL_` l) +# l) *# p) p
775 = getCommonNodeUFMData_ (p `shiftL_` l) (j `shiftR_` l) (j_ `shiftR_` l)
777 ask_about_common_ancestor :: NodeUFMData -> NodeUFMData -> CommonRoot
779 ask_about_common_ancestor x@(NodeUFMData j p) y@(NodeUFMData j2 p2)
780 | j ==# j2 = SameRoot
782 = case getCommonNodeUFMData x y of
783 nd@(NodeUFMData j3 p3)
784 | j3 ==# j -> LeftRoot (decideSide (j ># j2))
785 | j3 ==# j2 -> RightRoot (decideSide (j <# j2))
786 | otherwise -> NewRoot nd (j ># j2)
788 decideSide :: Bool -> Side
789 decideSide True = Leftt
790 decideSide False = Rightt
793 This might be better in Util.lhs ?
796 Now the bit twiddling functions.
798 shiftL_ :: FastInt -> FastInt -> FastInt
799 shiftR_ :: FastInt -> FastInt -> FastInt
801 #if __GLASGOW_HASKELL__
802 {-# INLINE shiftL_ #-}
803 {-# INLINE shiftR_ #-}
804 #if __GLASGOW_HASKELL__ >= 503
805 shiftL_ n p = word2Int#((int2Word# n) `uncheckedShiftL#` p)
807 shiftL_ n p = word2Int#((int2Word# n) `shiftL#` p)
809 shiftR_ n p = word2Int#((int2Word# n) `shiftr` p)
811 #if __GLASGOW_HASKELL__ >= 503
812 shiftr x y = uncheckedShiftRL# x y
814 shiftr x y = shiftRL# x y
818 shiftL_ n p = n * (2 ^ p)
819 shiftR_ n p = n `quot` (2 ^ p)
825 use_snd :: a -> b -> b