X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2Futils%2FBag.lhs;h=430af9307558294dbddf5f8e0076a44c24943e1c;hb=351426092b5b38cc72ca4c87ee65ea0412b865b5;hp=36fe3148ad872b65b95fce21196d4d15585140e9;hpb=ae45ff0e9831a0dc862a5d68d03e355d7e323c62;p=ghc-hetmet.git diff --git a/ghc/compiler/utils/Bag.lhs b/ghc/compiler/utils/Bag.lhs index 36fe314..430af93 100644 --- a/ghc/compiler/utils/Bag.lhs +++ b/ghc/compiler/utils/Bag.lhs @@ -4,14 +4,19 @@ \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, -- UNUSED: elemBag, - filterBag, partitionBag, concatBag, foldBag, + mapBag, +#ifndef COMPILING_GHC + elemBag, +#endif + filterBag, partitionBag, concatBag, foldBag, foldrBag, isEmptyBag, consBag, snocBag, listToBag, bagToList ) where @@ -20,8 +25,10 @@ module Bag ( IMP_Ubiq(){-uitous-} IMPORT_1_3(List(partition)) -import Outputable ( interpp'SP ) +import Outputable --( interpp'SP ) import Pretty +#else +import List(partition) #endif data Bag a @@ -35,7 +42,7 @@ data Bag a emptyBag = EmptyBag unitBag = UnitBag -{- UNUSED: +#ifndef COMPILING_GHC elemBag :: Eq a => a -> Bag a -> Bool elemBag x EmptyBag = False @@ -43,7 +50,7 @@ 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 @@ -55,8 +62,9 @@ unionBags b EmptyBag = b unionBags b1 b2 = TwoBags b1 b2 consBag :: a -> Bag a -> Bag a -consBag elt bag = (unitBag elt) `unionBags` bag snocBag :: Bag a -> a -> Bag a + +consBag elt bag = (unitBag elt) `unionBags` bag snocBag bag elt = bag `unionBags` (unitBag elt) isEmptyBag EmptyBag = True @@ -122,6 +130,16 @@ foldBag t u e (TwoBags b1 b2) = foldBag t u (foldBag t u e b2) b1 foldBag t u e (ListBag xs) = foldr (t.u) e xs foldBag t u e (ListOfBags bs) = foldr (\b r -> foldBag t u r b) e bs +foldrBag :: (a -> r -> r) -> r + -> Bag a + -> r + +foldrBag k z EmptyBag = z +foldrBag k z (UnitBag x) = k x z +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 + mapBag :: (a -> b) -> Bag a -> Bag b mapBag f EmptyBag = EmptyBag @@ -136,28 +154,18 @@ listToBag [] = EmptyBag listToBag vs = ListBag vs bagToList :: Bag a -> [a] -bagToList EmptyBag = [] -bagToList (ListBag vs) = vs -bagToList b = bagToList_append b [] - - -- (bagToList_append b xs) flattens b and puts xs on the end. - -- (not exported) -bagToList_append EmptyBag xs = xs -bagToList_append (UnitBag x) xs = x:xs -bagToList_append (TwoBags b1 b2) xs = bagToList_append b1 (bagToList_append b2 xs) -bagToList_append (ListBag xx) xs = xx++xs -bagToList_append (ListOfBags bs) xs = foldr bagToList_append xs bs +bagToList b = foldrBag (:) [] b \end{code} \begin{code} #ifdef COMPILING_GHC instance (Outputable a) => Outputable (Bag a) where - ppr sty EmptyBag = ppStr "emptyBag" + ppr sty EmptyBag = ptext SLIT("emptyBag") ppr sty (UnitBag a) = ppr sty a - ppr sty (TwoBags b1 b2) = ppCat [ppr sty b1, pp'SP, ppr sty b2] + 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) = ppCat [ppLbrack, interpp'SP sty bs, ppRbrack] + ppr sty (ListOfBags bs) = brackets (interpp'SP sty bs) #endif {- COMPILING_GHC -} \end{code}