[project @ 2005-11-29 14:31:59 by ross]
[ghc-base.git] / Data / Set.hs
index fd09eb0..fe3b0b4 100644 (file)
@@ -113,7 +113,9 @@ module Data.Set  (
 
 import Prelude hiding (filter,foldr,null,map)
 import qualified Data.List as List
+import Data.Monoid (Monoid(..))
 import Data.Typeable
+import Data.Foldable (Foldable(foldMap))
 
 {-
 -- just for testing
@@ -123,7 +125,7 @@ import qualified List
 -}
 
 #if __GLASGOW_HASKELL__
-import Text.Read (Lexeme(Ident), lexP, parens, prec, readPrec)
+import Text.Read
 import Data.Generics.Basics
 import Data.Generics.Instances
 #endif
@@ -146,6 +148,15 @@ data Set a    = Tip
 
 type Size     = Int
 
+instance Ord a => Monoid (Set a) where
+    mempty  = empty
+    mappend = union
+    mconcat = unions
+
+instance Foldable Set where
+    foldMap f Tip = mempty
+    foldMap f (Bin _s k l r) = foldMap f l `mappend` f k `mappend` foldMap f r
+
 #if __GLASGOW_HASKELL__
 
 {--------------------------------------------------------------------
@@ -534,10 +545,12 @@ instance (Read a, Ord a) => Read (Set a) where
     Ident "fromList" <- lexP
     xs <- readPrec
     return (fromList xs)
+
+  readListPrec = readListPrecDefault
 #else
   readsPrec p = readParen (p > 10) $ \ r -> do
-    ("fromList",s) <- lex
-    (xs,t) <- reads
+    ("fromList",s) <- lex r
+    (xs,t) <- reads s
     return (fromList xs,t)
 #endif