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
-}
#if __GLASGOW_HASKELL__
+import Text.Read
import Data.Generics.Basics
import Data.Generics.Instances
#endif
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__
{--------------------------------------------------------------------
Show
--------------------------------------------------------------------}
instance Show a => Show (Set a) where
- showsPrec d s = showSet (toAscList s)
+ showsPrec p xs = showParen (p > 10) $
+ showString "fromList " . shows (toList xs)
showSet :: (Show a) => [a] -> ShowS
showSet []
Read
--------------------------------------------------------------------}
instance (Read a, Ord a) => Read (Set a) where
- readsPrec _ = readParen False $ \ r ->
- [(fromList xs,t) | ("{",s) <- lex r,
- (xs,t) <- readl s]
- where readl s = [([],t) | ("}",t) <- lex s] ++
- [(x:xs,u) | (x,t) <- reads s
- , (xs,u) <- readl' t]
- readl' s = [([],t) | ("}",t) <- lex s] ++
- [(x:xs,v) | (",",t) <- lex s
- , (x,u) <- reads t
- , (xs,v) <- readl' u]
-
+#ifdef __GLASGOW_HASKELL__
+ readPrec = parens $ prec 10 $ do
+ Ident "fromList" <- lexP
+ xs <- readPrec
+ return (fromList xs)
+
+ readListPrec = readListPrecDefault
+#else
+ readsPrec p = readParen (p > 10) $ \ r -> do
+ ("fromList",s) <- lex r
+ (xs,t) <- reads s
+ return (fromList xs,t)
+#endif
+
{--------------------------------------------------------------------
Typeable/Data
--------------------------------------------------------------------}