[project @ 2002-12-05 23:49:43 by mthomas]
[ghc-hetmet.git] / ghc / compiler / utils / Bag.lhs
index 430af93..ed9a540 100644 (file)
@@ -1,36 +1,28 @@
 %
-% (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
+% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
 %
 \section[Bags]{@Bag@: an unordered collection with duplicates}
 
 \begin{code}
-#ifdef COMPILING_GHC
-#include "HsVersions.h"
-#endif
-
 module Bag (
        Bag,    -- abstract type
 
        emptyBag, unitBag, unionBags, unionManyBags,
        mapBag,
-#ifndef COMPILING_GHC
        elemBag,
-#endif
-       filterBag, partitionBag, concatBag, foldBag, foldrBag,
+       filterBag, partitionBag, concatBag, foldBag, foldrBag, foldlBag,
        isEmptyBag, consBag, snocBag,
        listToBag, bagToList
     ) where
 
-#ifdef COMPILING_GHC
-IMP_Ubiq(){-uitous-}
-IMPORT_1_3(List(partition))
+#include "HsVersions.h"
+
+import Outputable
+import List            ( partition )
+\end{code}
 
-import Outputable      --( interpp'SP )
-import Pretty
-#else
-import List(partition)
-#endif
 
+\begin{code}
 data Bag a
   = EmptyBag
   | UnitBag    a
@@ -42,7 +34,6 @@ data Bag a
 emptyBag = EmptyBag
 unitBag  = UnitBag
 
-#ifndef COMPILING_GHC
 elemBag :: Eq a => a -> Bag a -> Bool
 
 elemBag x EmptyBag        = False
@@ -50,7 +41,6 @@ elemBag x (UnitBag y)     = x==y
 elemBag x (TwoBags b1 b2) = x `elemBag` b1 || x `elemBag` b2
 elemBag x (ListBag ys)    = any (x ==) ys
 elemBag x (ListOfBags bs) = any (x `elemBag`) bs
-#endif
 
 unionManyBags [] = EmptyBag
 unionManyBags xs = ListOfBags xs
@@ -140,6 +130,16 @@ foldrBag k z (TwoBags b1 b2) = foldrBag k (foldrBag k z b2) b1
 foldrBag k z (ListBag xs)    = foldr k z xs
 foldrBag k z (ListOfBags bs) = foldr (\b r -> foldrBag k r b) z bs
 
+foldlBag :: (r -> a -> r) -> r
+        -> Bag a
+        -> r
+
+foldlBag k z EmptyBag        = z
+foldlBag k z (UnitBag x)     = k z x
+foldlBag k z (TwoBags b1 b2) = foldlBag k (foldlBag k z b1) b2
+foldlBag k z (ListBag xs)    = foldl k z xs
+foldlBag k z (ListOfBags bs) = foldl (\r b -> foldlBag k r b) z bs
+
 
 mapBag :: (a -> b) -> Bag a -> Bag b
 mapBag f EmptyBag       = EmptyBag
@@ -158,14 +158,11 @@ bagToList b = foldrBag (:) [] b
 \end{code}
 
 \begin{code}
-#ifdef COMPILING_GHC
-
 instance (Outputable a) => Outputable (Bag a) where
-    ppr sty EmptyBag       = ptext SLIT("emptyBag")
-    ppr sty (UnitBag a)     = ppr sty a
-    ppr sty (TwoBags b1 b2) = hsep [ppr sty b1 <> comma, ppr sty b2]
-    ppr sty (ListBag as)    = interpp'SP sty as
-    ppr sty (ListOfBags bs) = brackets (interpp'SP sty bs)
+    ppr EmptyBag       = ptext SLIT("emptyBag")
+    ppr (UnitBag a)     = ppr a
+    ppr (TwoBags b1 b2) = hsep [ppr b1 <> comma, ppr b2]
+    ppr (ListBag as)    = interpp'SP as
+    ppr (ListOfBags bs) = brackets (interpp'SP bs)
 
-#endif {- COMPILING_GHC -}
 \end{code}