X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Futils%2FBag.lhs;h=b2be2c30db697b411231bb63a28b39dfe1741c65;hb=ef7d9db56c08b8da3d77258eb73ed69d0a24bed7;hp=481dedfd8e48497a2a82d08bcb64d0a34151b958;hpb=241fdb3509084d0f00bc62f91cf65ade9df5f0e7;p=ghc-hetmet.git diff --git a/compiler/utils/Bag.lhs b/compiler/utils/Bag.lhs index 481dedf..b2be2c3 100644 --- a/compiler/utils/Bag.lhs +++ b/compiler/utils/Bag.lhs @@ -11,16 +11,19 @@ module Bag ( emptyBag, unitBag, unionBags, unionManyBags, mapBag, - elemBag, + elemBag, lengthBag, filterBag, partitionBag, concatBag, foldBag, foldrBag, foldlBag, isEmptyBag, isSingletonBag, consBag, snocBag, anyBag, listToBag, bagToList, mapBagM, mapAndUnzipBagM ) where +#include "Typeable.h" + import Outputable -import Util ( isSingleton ) +import Util +import Data.Data import Data.List ( partition ) infixr 3 `consBag` @@ -41,6 +44,12 @@ emptyBag = EmptyBag unitBag :: a -> Bag a unitBag = UnitBag +lengthBag :: Bag a -> Int +lengthBag EmptyBag = 0 +lengthBag (UnitBag {}) = 1 +lengthBag (TwoBags b1 b2) = lengthBag b1 + lengthBag b2 +lengthBag (ListBag xs) = length xs + elemBag :: Eq a => a -> Bag a -> Bool elemBag _ EmptyBag = False elemBag x (UnitBag y) = x == y @@ -182,4 +191,12 @@ bagToList b = foldrBag (:) [] b \begin{code} instance (Outputable a) => Outputable (Bag a) where ppr bag = braces (pprWithCommas ppr (bagToList bag)) + +INSTANCE_TYPEABLE1(Bag,bagTc,"Bag") + +instance Data a => Data (Bag a) where + gfoldl k z b = z listToBag `k` bagToList b -- traverse abstract type abstractly + toConstr _ = abstractConstr $ "Bag("++show (typeOf (undefined::a))++")" + gunfold _ _ = error "gunfold" + dataTypeOf _ = mkNoRepType "Bag" \end{code}