[project @ 2001-03-01 15:59:51 by simonmar]
[ghc-hetmet.git] / ghc / compiler / utils / FiniteMap.lhs
index 56caa58..c2c34fb 100644 (file)
@@ -1,5 +1,5 @@
-%
-% (c) The AQUA Project, Glasgow University, 1994-1995
+
+% (c) The AQUA Project, Glasgow University, 1994-1998
 %
 \section[FiniteMap]{An implementation of finite maps}
 
@@ -15,73 +15,61 @@ This code is derived from that in the paper:
 \end{display}
 
 The code is SPECIALIZEd to various highly-desirable types (e.g., Id)
-near the end (only \tr{#ifdef COMPILING_GHC}).
+near the end.
 
 \begin{code}
-#if defined(COMPILING_GHC)
-#include "HsVersions.h"
-#define IF_NOT_GHC(a) {--}
-#else
-#define ASSERT(e) {--}
-#define IF_NOT_GHC(a) a
-#define COMMA ,
-#endif
-
-#if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)/* NB NB NB */
-#define OUTPUTABLE_key , Outputable key
-#else
-#define OUTPUTABLE_key {--}
-#endif
 
 module FiniteMap (
        FiniteMap,              -- abstract type
 
-       emptyFM, singletonFM, listToFM,
+       emptyFM, unitFM, listToFM,
 
-       addToFM,   addListToFM,
-       IF_NOT_GHC(addToFM_C COMMA)
+       addToFM,
+       addToFM_C,
+       addListToFM,
        addListToFM_C,
-       IF_NOT_GHC(delFromFM COMMA)
+       delFromFM,
        delListFromFM,
 
-       plusFM,      plusFM_C,
-       IF_NOT_GHC(intersectFM COMMA intersectFM_C COMMA)
-       minusFM, -- exported for GHCI only
+       plusFM,
+       plusFM_C,
+       minusFM,
+       foldFM,
 
-       IF_NOT_GHC(mapFM COMMA foldFM COMMA filterFM COMMA)
+       intersectFM,
+       intersectFM_C,
+       mapFM, filterFM, 
 
-       IF_NOT_GHC(sizeFM COMMA)
-       isEmptyFM, elemFM, lookupFM, lookupWithDefaultFM,
-       
-       fmToList, keysFM, eltsFM{-used in GHCI-}
+       sizeFM, isEmptyFM, elemFM, lookupFM, lookupWithDefaultFM,
 
-#if defined(COMPILING_GHC)
-       , FiniteSet(..), emptySet, mkSet, isEmptySet
-       , elementOf, setToList, union, minusSet{-exported for GHCI-}
-#endif
+       fmToList, keysFM, eltsFM
+
+       , bagToFM
 
-       -- To make it self-sufficient
-#if __HASKELL1__ < 3
-       , Maybe
-#endif
     ) where
 
-import Maybes
+#include "HsVersions.h"
+#define IF_NOT_GHC(a) {--}
 
-#if defined(COMPILING_GHC)
-import AbsUniType
-import Pretty
-import Outputable
+#if defined(DEBUG_FINITEMAPS)/* NB NB NB */
+#define OUTPUTABLE_key , Outputable key
+#else
+#define OUTPUTABLE_key {--}
+#endif
+
+import GlaExts
+import Maybes
+import Bag       ( Bag, foldrBag )
 import Util
-import CLabelInfo      ( CLabel )  -- for specialising
+import Outputable
+
 #if ! OMIT_NATIVE_CODEGEN
-import AsmRegAlloc     ( Reg )     -- ditto
-#define IF_NCG(a) a
+#  define IF_NCG(a) a
 #else
-#define IF_NCG(a) {--}
-#endif
+#  define IF_NCG(a) {--}
 #endif
 
+
 -- SIGH: but we use unboxed "sizes"...
 #if __GLASGOW_HASKELL__
 #define IF_GHC(a,b) a
@@ -100,9 +88,11 @@ import AsmRegAlloc  ( Reg )     -- ditto
 \begin{code}
 --     BUILDING
 emptyFM                :: FiniteMap key elt
-singletonFM    :: key -> elt -> FiniteMap key elt
+unitFM         :: key -> elt -> FiniteMap key elt
 listToFM       :: (Ord key OUTPUTABLE_key) => [(key,elt)] -> FiniteMap key elt
                        -- In the case of duplicates, the last is taken
+bagToFM                :: (Ord key OUTPUTABLE_key) => Bag (key,elt) -> FiniteMap key elt
+                       -- In the case of duplicates, who knows which is taken
 
 --     ADDING AND DELETING
                   -- Throws away any previous binding
@@ -112,11 +102,12 @@ addToFM           :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> elt  -> Fini
 addListToFM    :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> [(key,elt)] -> FiniteMap key elt
 
                   -- Combines with previous binding
+                  -- The combining fn goes (old -> new -> new)
 addToFM_C      :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
-                          -> FiniteMap key elt -> key -> elt  
+                          -> FiniteMap key elt -> key -> elt
                           -> FiniteMap key elt
 addListToFM_C  :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
-                          -> FiniteMap key elt -> [(key,elt)] 
+                          -> FiniteMap key elt -> [(key,elt)]
                           -> FiniteMap key elt
 
                   -- Deletion doesn't complain if you try to delete something
@@ -130,22 +121,23 @@ plusFM            :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
                           -> FiniteMap key elt
 
                   -- Combines bindings for the same thing with the given function
-plusFM_C       :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt) 
+plusFM_C       :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
                           -> FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
 
 minusFM                :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
                   -- (minusFM a1 a2) deletes from a1 any bindings which are bound in a2
 
-intersectFM    :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt 
-intersectFM_C  :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
-                          -> FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt 
+intersectFM    :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
+intersectFM_C  :: (Ord key OUTPUTABLE_key) => (elt1 -> elt2 -> elt3)
+                          -> FiniteMap key elt1 -> FiniteMap key elt2 -> FiniteMap key elt3
 
 --     MAPPING, FOLDING, FILTERING
 foldFM         :: (key -> elt -> a -> a) -> a -> FiniteMap key elt -> a
 mapFM          :: (key -> elt1 -> elt2) -> FiniteMap key elt1 -> FiniteMap key elt2
-filterFM       :: (Ord key OUTPUTABLE_key) => (key -> elt -> Bool) 
+filterFM       :: (Ord key OUTPUTABLE_key) => (key -> elt -> Bool)
                           -> FiniteMap key elt -> FiniteMap key elt
 
+
 --     INTERROGATING
 sizeFM         :: FiniteMap key elt -> Int
 isEmptyFM      :: FiniteMap key elt -> Bool
@@ -185,7 +177,7 @@ factor of at most \tr{sIZE_RATIO}
 
 \begin{code}
 data FiniteMap key elt
-  = EmptyFM 
+  = EmptyFM
   | Branch key elt             -- Key and elt stored here
     IF_GHC(Int#,Int{-STRICT-}) -- Size >= 1
     (FiniteMap key elt)                -- Children
@@ -203,9 +195,11 @@ emptyFM
 
 -- #define EmptyFM (Branch _ _ IF_GHC(0#,0) _ _)
 
-singletonFM key elt = Branch key elt IF_GHC(1#,1) emptyFM emptyFM
+unitFM key elt = Branch key elt IF_GHC(1#,1) emptyFM emptyFM
 
-listToFM key_elt_pairs = addListToFM emptyFM key_elt_pairs
+listToFM = addListToFM emptyFM
+
+bagToFM = foldrBag (\(k,v) fm -> addToFM fm k v) emptyFM
 \end{code}
 
 %************************************************************************
@@ -217,23 +211,17 @@ listToFM key_elt_pairs = addListToFM emptyFM key_elt_pairs
 \begin{code}
 addToFM fm key elt = addToFM_C (\ old new -> new) fm key elt
 
-addToFM_C combiner EmptyFM key elt = singletonFM key elt
+addToFM_C combiner EmptyFM key elt = unitFM key elt
 addToFM_C combiner (Branch key elt size fm_l fm_r) new_key new_elt
-#ifdef __GLASGOW_HASKELL__
-  = case _tagCmp new_key key of
-       _LT -> mkBalBranch key elt (addToFM_C combiner fm_l new_key new_elt) fm_r
-       _GT -> mkBalBranch key elt fm_l (addToFM_C combiner fm_r new_key new_elt)
-       _EQ -> Branch new_key (combiner elt new_elt) size fm_l fm_r
-#else
-  | new_key < key = mkBalBranch key elt (addToFM_C combiner fm_l new_key new_elt) fm_r
-  | new_key > key = mkBalBranch key elt fm_l (addToFM_C combiner fm_r new_key new_elt)
-  | otherwise    = Branch new_key (combiner elt new_elt) size fm_l fm_r
-#endif
+  = case compare new_key key of
+       LT -> mkBalBranch key elt (addToFM_C combiner fm_l new_key new_elt) fm_r
+       GT -> mkBalBranch key elt fm_l (addToFM_C combiner fm_r new_key new_elt)
+       EQ -> Branch new_key (combiner elt new_elt) size fm_l fm_r
 
 addListToFM fm key_elt_pairs = addListToFM_C (\ old new -> new) fm key_elt_pairs
 
 addListToFM_C combiner fm key_elt_pairs
-  = foldl add fm key_elt_pairs -- foldl adds from the left
+  = foldl' add fm key_elt_pairs        -- foldl adds from the left
   where
     add fmap (key,elt) = addToFM_C combiner fmap key elt
 \end{code}
@@ -241,23 +229,12 @@ addListToFM_C combiner fm key_elt_pairs
 \begin{code}
 delFromFM EmptyFM del_key = emptyFM
 delFromFM (Branch key elt size fm_l fm_r) del_key
-#ifdef __GLASGOW_HASKELL__
-  = case _tagCmp del_key key of
-       _GT -> mkBalBranch key elt fm_l (delFromFM fm_r del_key)
-       _LT -> mkBalBranch key elt (delFromFM fm_l del_key) fm_r
-       _EQ -> glueBal fm_l fm_r
-#else  
-  | del_key > key
-  = mkBalBranch key elt fm_l (delFromFM fm_r del_key)
-
-  | del_key < key
-  = mkBalBranch key elt (delFromFM fm_l del_key) fm_r
-
-  | key == del_key
-  = glueBal fm_l fm_r
-#endif
+  = case compare del_key key of
+       GT -> mkBalBranch key elt fm_l (delFromFM fm_r del_key)
+       LT -> mkBalBranch key elt (delFromFM fm_l del_key) fm_r
+       EQ -> glueBal fm_l fm_r
 
-delListFromFM fm keys = foldl delFromFM fm keys
+delListFromFM fm keys = foldl' delFromFM fm keys
 \end{code}
 
 %************************************************************************
@@ -270,7 +247,7 @@ delListFromFM fm keys = foldl delFromFM fm keys
 plusFM_C combiner EmptyFM fm2 = fm2
 plusFM_C combiner fm1 EmptyFM = fm1
 plusFM_C combiner fm1 (Branch split_key elt2 _ left right)
-  = mkVBalBranch split_key new_elt 
+  = mkVBalBranch split_key new_elt
                 (plusFM_C combiner lts left)
                 (plusFM_C combiner gts right)
   where
@@ -282,6 +259,7 @@ plusFM_C combiner fm1 (Branch split_key elt2 _ left right)
 
 -- It's worth doing plusFM specially, because we don't need
 -- to do the lookup in fm1.
+-- FM2 over-rides FM1.
 
 plusFM EmptyFM fm2 = fm2
 plusFM fm1 EmptyFM = fm1
@@ -308,7 +286,7 @@ intersectFM_C combiner fm1 (Branch split_key elt2 _ left right)
 
   | maybeToBool maybe_elt1     -- split_elt *is* in intersection
   = mkVBalBranch split_key (combiner elt1 elt2) (intersectFM_C combiner lts left)
-                                               (intersectFM_C combiner gts right)
+                                               (intersectFM_C combiner gts right)
 
   | otherwise                  -- split_elt is *not* in intersection
   = glueVBal (intersectFM_C combiner lts left) (intersectFM_C combiner gts right)
@@ -333,7 +311,7 @@ foldFM k z (Branch key elt _ fm_l fm_r)
   = foldFM k (k key elt (foldFM k z fm_r)) fm_l
 
 mapFM f EmptyFM = emptyFM
-mapFM f (Branch key elt size fm_l fm_r) 
+mapFM f (Branch key elt size fm_l fm_r)
   = Branch key (f key elt) size (mapFM f fm_l) (mapFM f fm_r)
 
 filterFM p EmptyFM = emptyFM
@@ -360,16 +338,10 @@ isEmptyFM fm = sizeFM fm == 0
 
 lookupFM EmptyFM key = Nothing
 lookupFM (Branch key elt _ fm_l fm_r) key_to_find
-#ifdef __GLASGOW_HASKELL__
-  = case _tagCmp key_to_find key of
-       _LT -> lookupFM fm_l key_to_find
-       _GT -> lookupFM fm_r key_to_find
-        _EQ -> Just elt
-#else
-  | key_to_find < key = lookupFM fm_l key_to_find
-  | key_to_find > key = lookupFM fm_r key_to_find
-  | otherwise    = Just elt
-#endif
+  = case compare key_to_find key of
+       LT -> lookupFM fm_l key_to_find
+       GT -> lookupFM fm_r key_to_find
+       EQ -> Just elt
 
 key `elemFM` fm
   = case (lookupFM fm key) of { Nothing -> False; Just elt -> True }
@@ -406,7 +378,7 @@ eltsFM fm   = foldFM (\ key elt rest -> elt : rest)       [] fm
 @mkBranch@ simply gets the size component right.  This is the ONLY
 (non-trivial) place the Branch object is built, so the ASSERTion
 recursively checks consistency.  (The trivial use of Branch is in
-@singletonFM@.)
+@unitFM@.)
 
 \begin{code}
 sIZE_RATIO :: Int
@@ -414,18 +386,18 @@ sIZE_RATIO = 5
 
 mkBranch :: (Ord key OUTPUTABLE_key)           -- Used for the assertion checking only
         => Int
-        -> key -> elt 
+        -> key -> elt
         -> FiniteMap key elt -> FiniteMap key elt
         -> FiniteMap key elt
 
 mkBranch which key elt fm_l fm_r
   = --ASSERT( left_ok && right_ok && balance_ok )
-#if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)
+#if defined(DEBUG_FINITEMAPS)
     if not ( left_ok && right_ok && balance_ok ) then
-       pprPanic ("mkBranch:"++show which) (ppAboves [ppr PprDebug [left_ok, right_ok, balance_ok],
-                                      ppr PprDebug key,
-                                      ppr PprDebug fm_l,
-                                      ppr PprDebug fm_r])
+       pprPanic ("mkBranch:"++show which) (vcat [ppr [left_ok, right_ok, balance_ok],
+                                      ppr key,
+                                      ppr fm_l,
+                                      ppr fm_r])
     else
 #endif
     let
@@ -434,7 +406,7 @@ mkBranch which key elt fm_l fm_r
 --    if sizeFM result <= 8 then
        result
 --    else
---     pprTrace ("mkBranch:"++(show which)) (ppr PprDebug result) (
+--     pprTrace ("mkBranch:"++(show which)) (ppr result) (
 --     result
 --     )
   where
@@ -486,41 +458,41 @@ out of whack.
 
 \begin{code}
 mkBalBranch :: (Ord key OUTPUTABLE_key)
-           => key -> elt 
+           => key -> elt
            -> FiniteMap key elt -> FiniteMap key elt
            -> FiniteMap key elt
 
 mkBalBranch key elt fm_L fm_R
 
-  | size_l + size_r < 2 
+  | size_l + size_r < 2
   = mkBranch 1{-which-} key elt fm_L fm_R
 
   | size_r > sIZE_RATIO * size_l       -- Right tree too big
   = case fm_R of
-       Branch _ _ _ fm_rl fm_rr 
+       Branch _ _ _ fm_rl fm_rr
                | sizeFM fm_rl < 2 * sizeFM fm_rr -> single_L fm_L fm_R
                | otherwise                       -> double_L fm_L fm_R
        -- Other case impossible
 
   | size_l > sIZE_RATIO * size_r       -- Left tree too big
   = case fm_L of
-       Branch _ _ _ fm_ll fm_lr 
+       Branch _ _ _ fm_ll fm_lr
                | sizeFM fm_lr < 2 * sizeFM fm_ll -> single_R fm_L fm_R
                | otherwise                       -> double_R fm_L fm_R
        -- Other case impossible
 
   | otherwise                          -- No imbalance
   = mkBranch 2{-which-} key elt fm_L fm_R
-  
+
   where
     size_l   = sizeFM fm_L
     size_r   = sizeFM fm_R
 
-    single_L fm_l (Branch key_r elt_r _ fm_rl fm_rr) 
+    single_L fm_l (Branch key_r elt_r _ fm_rl fm_rr)
        = mkBranch 3{-which-} key_r elt_r (mkBranch 4{-which-} key elt fm_l fm_rl) fm_rr
 
     double_L fm_l (Branch key_r elt_r _ (Branch key_rl elt_rl _ fm_rll fm_rlr) fm_rr)
-       = mkBranch 5{-which-} key_rl elt_rl (mkBranch 6{-which-} key   elt   fm_l   fm_rll) 
+       = mkBranch 5{-which-} key_rl elt_rl (mkBranch 6{-which-} key   elt   fm_l   fm_rll)
                                 (mkBranch 7{-which-} key_r elt_r fm_rlr fm_rr)
 
     single_R (Branch key_l elt_l _ fm_ll fm_lr) fm_r
@@ -534,7 +506,7 @@ mkBalBranch key elt fm_L fm_R
 
 \begin{code}
 mkVBalBranch :: (Ord key OUTPUTABLE_key)
-            => key -> elt 
+            => key -> elt
             -> FiniteMap key elt -> FiniteMap key elt
             -> FiniteMap key elt
 
@@ -557,7 +529,7 @@ mkVBalBranch key elt fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
   | otherwise
   = mkBranch 13{-which-} key elt fm_l fm_r
 
-  where 
+  where
     size_l = sizeFM fm_l
     size_r = sizeFM fm_r
 \end{code}
@@ -579,13 +551,13 @@ glueBal :: (Ord key OUTPUTABLE_key)
 
 glueBal EmptyFM fm2 = fm2
 glueBal fm1 EmptyFM = fm1
-glueBal fm1 fm2 
+glueBal fm1 fm2
        -- The case analysis here (absent in Adams' program) is really to deal
        -- with the case where fm2 is a singleton. Then deleting the minimum means
        -- we pass an empty tree to mkBalBranch, which breaks its invariant.
   | sizeFM fm2 > sizeFM fm1
   = mkBalBranch mid_key2 mid_elt2 fm1 (deleteMin fm2)
-       
+
   | otherwise
   = mkBalBranch mid_key1 mid_elt1 (deleteMax fm1) fm2
   where
@@ -604,7 +576,7 @@ glueVBal :: (Ord key OUTPUTABLE_key)
 glueVBal EmptyFM fm2 = fm2
 glueVBal fm1 EmptyFM = fm1
 glueVBal fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
-         fm_r@(Branch key_r elt_r _ fm_rl fm_rr)
+        fm_r@(Branch key_r elt_r _ fm_rl fm_rr)
   | sIZE_RATIO * size_l < size_r
   = mkBalBranch key_r elt_r (glueVBal fm_l fm_rl) fm_rr
 
@@ -614,8 +586,6 @@ glueVBal fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
   | otherwise          -- We now need the same two cases as in glueBal above.
   = glueBal fm_l fm_r
   where
-    (mid_key_l,mid_elt_l) = findMax fm_l
-    (mid_key_r,mid_elt_r) = findMin fm_r
     size_l = sizeFM fm_l
     size_r = sizeFM fm_r
 \end{code}
@@ -630,49 +600,21 @@ glueVBal fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
 splitLT, splitGT :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> FiniteMap key elt
 
 -- splitLT fm split_key  =  fm restricted to keys <  split_key
--- splitGE fm split_key  =  fm restricted to keys >= split_key (UNUSED)
 -- splitGT fm split_key  =  fm restricted to keys >  split_key
 
 splitLT EmptyFM split_key = emptyFM
 splitLT (Branch key elt _ fm_l fm_r) split_key
-#ifdef __GLASGOW_HASKELL__
-  = case _tagCmp split_key key of
-       _LT -> splitLT fm_l split_key
-       _GT -> mkVBalBranch key elt fm_l (splitLT fm_r split_key)
-       _EQ -> fm_l
-#else
-  | split_key < key = splitLT fm_l split_key
-  | split_key > key = mkVBalBranch key elt fm_l (splitLT fm_r split_key)
-  | otherwise      = fm_l
-#endif
-
-{- UNUSED:
-splitGE EmptyFM split_key = emptyFM
-splitGE (Branch key elt _ fm_l fm_r) split_key
-#ifdef __GLASGOW_HASKELL__
-  = case _tagCmp split_key key of
-       _GT -> splitGE fm_r split_key
-       _LT -> mkVBalBranch key elt (splitGE fm_l split_key) fm_r
-       _EQ -> mkVBalBranch key elt emptyFM fm_r
-#else
-  | split_key > key = splitGE fm_r split_key
-  | split_key < key = mkVBalBranch key elt (splitGE fm_l split_key) fm_r
-  | otherwise      = mkVBalBranch key elt emptyFM fm_r
-#endif
--}
+  = case compare split_key key of
+       LT -> splitLT fm_l split_key
+       GT -> mkVBalBranch key elt fm_l (splitLT fm_r split_key)
+       EQ -> fm_l
 
 splitGT EmptyFM split_key = emptyFM
 splitGT (Branch key elt _ fm_l fm_r) split_key
-#ifdef __GLASGOW_HASKELL__
-  = case _tagCmp split_key key of
-       _GT -> splitGT fm_r split_key
-       _LT -> mkVBalBranch key elt (splitGT fm_l split_key) fm_r
-       _EQ -> fm_r
-#else
-  | split_key > key = splitGT fm_r split_key
-  | split_key < key = mkVBalBranch key elt (splitGT fm_l split_key) fm_r
-  | otherwise      = fm_r
-#endif
+  = case compare split_key key of
+       GT -> splitGT fm_r split_key
+       LT -> mkVBalBranch key elt (splitGT fm_l split_key) fm_r
+       EQ -> fm_r
 
 findMin :: FiniteMap key elt -> (key,elt)
 findMin (Branch key elt _ EmptyFM _) = (key,elt)
@@ -698,68 +640,37 @@ deleteMax (Branch key elt _ fm_l    fm_r) = mkBalBranch key elt fm_l (deleteMax
 %************************************************************************
 
 \begin{code}
-#if defined(COMPILING_GHC)
-
-{- this is the real one actually...
-instance (Outputable key, Outputable elt) => Outputable (FiniteMap key elt) where
-    ppr sty fm = ppr sty (fmToList fm)
--}
+#if defined(DEBUG_FINITEMAPS)
 
--- temp debugging (ToDo: rm)
 instance (Outputable key) => Outputable (FiniteMap key elt) where
-    ppr sty fm = pprX sty fm
+    ppr fm = pprX fm
 
-pprX sty EmptyFM = ppChar '!'
-pprX sty (Branch key elt sz fm_l fm_r)
- = ppBesides [ppLparen, pprX sty fm_l, ppSP,
-             ppr sty key, ppSP, ppInt (IF_GHC(I# sz, sz)), ppSP,
-             pprX sty fm_r, ppRparen]
+pprX EmptyFM = char '!'
+pprX (Branch key elt sz fm_l fm_r)
+ = parens (hcat [pprX fm_l, space,
+                     ppr key, space, int (IF_GHC(I# sz, sz)), space,
+                     pprX fm_r])
+#else
+-- and when not debugging the package itself...
+instance (Outputable key, Outputable elt) => Outputable (FiniteMap key elt) where
+    ppr fm = ppr (fmToList fm)
 #endif
 
-#if !defined(COMPILING_GHC)
+#if 0
 instance (Eq key, Eq elt) => Eq (FiniteMap key elt) where
   fm_1 == fm_2 = (sizeFM   fm_1 == sizeFM   fm_2) &&   -- quick test
-                 (fmToList fm_1 == fmToList fm_2)
+                (fmToList fm_1 == fmToList fm_2)
 
 {- NO: not clear what The Right Thing to do is:
 instance (Ord key, Ord elt) => Ord (FiniteMap key elt) where
   fm_1 <= fm_2 = (sizeFM   fm_1 <= sizeFM   fm_2) &&   -- quick test
-                 (fmToList fm_1 <= fmToList fm_2)
+                (fmToList fm_1 <= fmToList fm_2)
 -}
 #endif
 \end{code}
 
 %************************************************************************
 %*                                                                     *
-\subsection{FiniteSets---a thin veneer}
-%*                                                                     *
-%************************************************************************
-
-\begin{code}
-#if defined(COMPILING_GHC)
-
-type FiniteSet key = FiniteMap key ()
-emptySet       :: FiniteSet key
-mkSet          :: (Ord key OUTPUTABLE_key) => [key] -> FiniteSet key
-isEmptySet     :: FiniteSet key -> Bool
-elementOf      :: (Ord key OUTPUTABLE_key) => key -> FiniteSet key -> Bool
-minusSet       :: (Ord key OUTPUTABLE_key) => FiniteSet key -> FiniteSet key -> FiniteSet key
-setToList      :: FiniteSet key -> [key]
-union          :: (Ord key OUTPUTABLE_key) => FiniteSet key -> FiniteSet key -> FiniteSet key
-
-emptySet = emptyFM
-mkSet xs = listToFM [ (x, ()) | x <- xs]
-isEmptySet = isEmptyFM
-elementOf = elemFM
-minusSet  = minusFM
-setToList = keysFM
-union = plusFM
-
-#endif
-\end{code}
-
-%************************************************************************
-%*                                                                     *
 \subsection{Efficiency pragmas for GHC}
 %*                                                                     *
 %************************************************************************
@@ -768,96 +679,70 @@ When the FiniteMap module is used in GHC, we specialise it for
 \tr{Uniques}, for dastardly efficiency reasons.
 
 \begin{code}
-#if defined(COMPILING_GHC) && __GLASGOW_HASKELL__
-    -- the __GLASGOW_HASKELL__ chk avoids an hbc 0.999.7 bug
+#if 0
+
+#if __GLASGOW_HASKELL__
 
-{-# SPECIALIZE listToFM
-               :: [(Int,elt)] -> FiniteMap Int elt,
-                  [(CLabel,elt)] -> FiniteMap CLabel elt,
-                  [(FAST_STRING,elt)] -> FiniteMap FAST_STRING elt,
-                  [((FAST_STRING,FAST_STRING),elt)] -> FiniteMap (FAST_STRING, FAST_STRING) elt
-    IF_NCG(COMMA   [(Reg COMMA elt)] -> FiniteMap Reg elt)
-    #-}
-{-# SPECIALIZE addToFM
-               :: FiniteMap Int elt -> Int -> elt  -> FiniteMap Int elt,
-                  FiniteMap FAST_STRING elt -> FAST_STRING -> elt  -> FiniteMap FAST_STRING elt,
-                  FiniteMap CLabel elt -> CLabel -> elt  -> FiniteMap CLabel elt
-    IF_NCG(COMMA   FiniteMap Reg elt -> Reg -> elt  -> FiniteMap Reg elt)
-    #-}
 {-# SPECIALIZE addListToFM
-               :: FiniteMap Int elt -> [(Int,elt)] -> FiniteMap Int elt,
-                  FiniteMap CLabel elt -> [(CLabel,elt)] -> FiniteMap CLabel elt
+               :: FiniteMap (FAST_STRING, FAST_STRING) elt -> [((FAST_STRING, FAST_STRING),elt)] -> FiniteMap (FAST_STRING, FAST_STRING) elt
+                , FiniteMap RdrName elt -> [(RdrName,elt)] -> FiniteMap RdrName elt
     IF_NCG(COMMA   FiniteMap Reg elt -> [(Reg COMMA elt)] -> FiniteMap Reg elt)
     #-}
-{-NOT EXPORTED!! # SPECIALIZE addToFM_C
-               :: (elt -> elt -> elt) -> FiniteMap Int elt -> Int -> elt -> FiniteMap Int elt,
-                  (elt -> elt -> elt) -> FiniteMap CLabel elt -> CLabel -> elt -> FiniteMap CLabel elt
-    IF_NCG(COMMA   (elt -> elt -> elt) -> FiniteMap Reg elt -> Reg -> elt -> FiniteMap Reg elt)
-    #-}
 {-# SPECIALIZE addListToFM_C
-               :: (elt -> elt -> elt) -> FiniteMap Int elt -> [(Int,elt)] -> FiniteMap Int elt,
-                  (elt -> elt -> elt) -> FiniteMap TyCon elt -> [(TyCon,elt)] -> FiniteMap TyCon elt,
-                  (elt -> elt -> elt) -> FiniteMap CLabel elt -> [(CLabel,elt)] -> FiniteMap CLabel elt
+               :: (elt -> elt -> elt) -> FiniteMap TyCon elt -> [(TyCon,elt)] -> FiniteMap TyCon elt
+                , (elt -> elt -> elt) -> FiniteMap FAST_STRING elt -> [(FAST_STRING,elt)] -> FiniteMap FAST_STRING elt
     IF_NCG(COMMA   (elt -> elt -> elt) -> FiniteMap Reg elt -> [(Reg COMMA elt)] -> FiniteMap Reg elt)
     #-}
-{-NOT EXPORTED!!! # SPECIALIZE delFromFM
-               :: FiniteMap Int elt -> Int   -> FiniteMap Int elt,
-                  FiniteMap CLabel elt -> CLabel   -> FiniteMap CLabel elt
-    IF_NCG(COMMA   FiniteMap Reg elt -> Reg   -> FiniteMap Reg elt)
-    #-}
-{-# SPECIALIZE delListFromFM
-               :: FiniteMap Int elt -> [Int] -> FiniteMap Int elt,
-                  FiniteMap CLabel elt -> [CLabel] -> FiniteMap CLabel elt
-    IF_NCG(COMMA   FiniteMap Reg elt -> [Reg] -> FiniteMap Reg elt)
+{-# SPECIALIZE addToFM
+               :: FiniteMap CLabel elt -> CLabel -> elt  -> FiniteMap CLabel elt
+                , FiniteMap FAST_STRING elt -> FAST_STRING -> elt  -> FiniteMap FAST_STRING elt
+                , FiniteMap (FAST_STRING, FAST_STRING) elt -> (FAST_STRING, FAST_STRING) -> elt  -> FiniteMap (FAST_STRING, FAST_STRING) elt
+                , FiniteMap RdrName elt -> RdrName -> elt  -> FiniteMap RdrName elt
+    IF_NCG(COMMA   FiniteMap Reg elt -> Reg -> elt  -> FiniteMap Reg elt)
     #-}
-{-# SPECIALIZE elemFM
-               :: FAST_STRING -> FiniteMap FAST_STRING elt -> Bool
+{-# SPECIALIZE addToFM_C
+               :: (elt -> elt -> elt) -> FiniteMap (RdrName, RdrName) elt -> (RdrName, RdrName) -> elt -> FiniteMap (RdrName, RdrName) elt
+                , (elt -> elt -> elt) -> FiniteMap FAST_STRING elt -> FAST_STRING -> elt -> FiniteMap FAST_STRING elt
+    IF_NCG(COMMA   (elt -> elt -> elt) -> FiniteMap Reg elt -> Reg -> elt -> FiniteMap Reg elt)
     #-}
-{-not EXPORTED!!! # SPECIALIZE filterFM
-               :: (Int -> elt -> Bool) -> FiniteMap Int elt -> FiniteMap Int elt,
-                  (CLabel -> elt -> Bool) -> FiniteMap CLabel elt -> FiniteMap CLabel elt
-    IF_NCG(COMMA   (Reg -> elt -> Bool) -> FiniteMap Reg elt -> FiniteMap Reg elt)
+{-# SPECIALIZE bagToFM
+               :: Bag (FAST_STRING,elt) -> FiniteMap FAST_STRING elt
     #-}
-{-NOT EXPORTED!!! # SPECIALIZE intersectFM
-               :: FiniteMap Int elt -> FiniteMap Int elt -> FiniteMap Int elt,
-                  FiniteMap CLabel elt -> FiniteMap CLabel elt -> FiniteMap CLabel elt
-    IF_NCG(COMMA   FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
+{-# SPECIALIZE delListFromFM
+               :: FiniteMap RdrName elt -> [RdrName]   -> FiniteMap RdrName elt
+                , FiniteMap FAST_STRING elt -> [FAST_STRING]   -> FiniteMap FAST_STRING elt
+    IF_NCG(COMMA   FiniteMap Reg elt -> [Reg]   -> FiniteMap Reg elt)
     #-}
-{-not EXPORTED !!!# SPECIALIZE intersectFM_C
-               :: (elt -> elt -> elt) -> FiniteMap Int elt -> FiniteMap Int elt -> FiniteMap Int elt,
-                  (elt -> elt -> elt) -> FiniteMap CLabel elt -> FiniteMap CLabel elt -> FiniteMap CLabel elt
-    IF_NCG(COMMA   (elt -> elt -> elt) -> FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
+{-# SPECIALIZE listToFM
+               :: [([Char],elt)] -> FiniteMap [Char] elt
+                , [(FAST_STRING,elt)] -> FiniteMap FAST_STRING elt
+                , [((FAST_STRING,FAST_STRING),elt)] -> FiniteMap (FAST_STRING, FAST_STRING) elt
+    IF_NCG(COMMA   [(Reg COMMA elt)] -> FiniteMap Reg elt)
     #-}
 {-# SPECIALIZE lookupFM
-               :: FiniteMap Int elt -> Int -> Maybe elt,
-                  FiniteMap CLabel elt -> CLabel -> Maybe elt,
-                  FiniteMap FAST_STRING elt -> FAST_STRING -> Maybe elt,
-                  FiniteMap (FAST_STRING,FAST_STRING) elt -> (FAST_STRING,FAST_STRING) -> Maybe elt
+               :: FiniteMap CLabel elt -> CLabel -> Maybe elt
+                , FiniteMap [Char] elt -> [Char] -> Maybe elt
+                , FiniteMap FAST_STRING elt -> FAST_STRING -> Maybe elt
+                , FiniteMap (FAST_STRING,FAST_STRING) elt -> (FAST_STRING,FAST_STRING) -> Maybe elt
+                , FiniteMap RdrName elt -> RdrName -> Maybe elt
+                , FiniteMap (RdrName,RdrName) elt -> (RdrName,RdrName) -> Maybe elt
     IF_NCG(COMMA   FiniteMap Reg elt -> Reg -> Maybe elt)
     #-}
 {-# SPECIALIZE lookupWithDefaultFM
-               :: FiniteMap Int elt -> elt -> Int -> elt,
-                  FiniteMap CLabel elt -> elt -> CLabel -> elt
+               :: FiniteMap FAST_STRING elt -> elt -> FAST_STRING -> elt
     IF_NCG(COMMA   FiniteMap Reg elt -> elt -> Reg -> elt)
     #-}
-{-# SPECIALIZE minusFM
-               :: FiniteMap Int elt -> FiniteMap Int elt -> FiniteMap Int elt,
-                  FiniteMap TyCon elt -> FiniteMap TyCon elt -> FiniteMap TyCon elt,
-                  FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt,
-                  FiniteMap CLabel elt -> FiniteMap CLabel elt -> FiniteMap CLabel elt
-    IF_NCG(COMMA   FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
-    #-}
 {-# SPECIALIZE plusFM
-               :: FiniteMap Int elt -> FiniteMap Int elt -> FiniteMap Int elt,
-                  FiniteMap TyCon elt -> FiniteMap TyCon elt -> FiniteMap TyCon elt,
-                  FiniteMap CLabel elt -> FiniteMap CLabel elt -> FiniteMap CLabel elt
+               :: FiniteMap RdrName elt -> FiniteMap RdrName elt -> FiniteMap RdrName elt
+                , FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt
     IF_NCG(COMMA   FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
     #-}
 {-# SPECIALIZE plusFM_C
-               :: (elt -> elt -> elt) -> FiniteMap Int elt -> FiniteMap Int elt -> FiniteMap Int elt,
-                  (elt -> elt -> elt) -> FiniteMap CLabel elt -> FiniteMap CLabel elt -> FiniteMap CLabel elt
+               :: (elt -> elt -> elt) -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt
     IF_NCG(COMMA   (elt -> elt -> elt) -> FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
     #-}
 
-#endif {- compiling for GHC -}
+#endif {- compiling with ghc and have specialiser -}
+
+#endif {- 0 -}
 \end{code}