Commit a5d8b458 authored by ross's avatar ross
Browse files

[project @ 2005-10-21 10:39:56 by ross]

conformant Show and Read instances
parent d7316cd3
...@@ -104,6 +104,7 @@ import qualified List ...@@ -104,6 +104,7 @@ import qualified List
-} -}
#if __GLASGOW_HASKELL__ #if __GLASGOW_HASKELL__
import Text.Read (Lexeme(Ident), lexP, parens, prec, readPrec)
import Data.Generics.Basics import Data.Generics.Basics
import Data.Generics.Instances import Data.Generics.Instances
#endif #endif
...@@ -582,7 +583,8 @@ instance Ord IntSet where ...@@ -582,7 +583,8 @@ instance Ord IntSet where
Show Show
--------------------------------------------------------------------} --------------------------------------------------------------------}
instance Show IntSet where instance Show IntSet where
showsPrec d s = showSet (toList s) showsPrec p xs = showParen (p > 10) $
showString "fromList " . shows (toList xs)
showSet :: [Int] -> ShowS showSet :: [Int] -> ShowS
showSet [] showSet []
...@@ -593,6 +595,22 @@ showSet (x:xs) ...@@ -593,6 +595,22 @@ showSet (x:xs)
showTail [] = showChar '}' showTail [] = showChar '}'
showTail (x:xs) = showChar ',' . shows x . showTail xs showTail (x:xs) = showChar ',' . shows x . showTail xs
{--------------------------------------------------------------------
Read
--------------------------------------------------------------------}
instance Read IntSet where
#ifdef __GLASGOW_HASKELL__
readPrec = parens $ prec 10 $ do
Ident "fromList" <- lexP
xs <- readPrec
return (fromList xs)
#else
readsPrec p = readParen (p > 10) $ \ r -> do
("fromList",s) <- lex
(xs,t) <- reads
return (fromList xs,t)
#endif
{-------------------------------------------------------------------- {--------------------------------------------------------------------
Typeable Typeable
--------------------------------------------------------------------} --------------------------------------------------------------------}
......
...@@ -123,6 +123,7 @@ import qualified List ...@@ -123,6 +123,7 @@ import qualified List
-} -}
#if __GLASGOW_HASKELL__ #if __GLASGOW_HASKELL__
import Text.Read (Lexeme(Ident), lexP, parens, prec, readPrec)
import Data.Generics.Basics import Data.Generics.Basics
import Data.Generics.Instances import Data.Generics.Instances
#endif #endif
...@@ -512,7 +513,8 @@ instance Ord a => Ord (Set a) where ...@@ -512,7 +513,8 @@ instance Ord a => Ord (Set a) where
Show Show
--------------------------------------------------------------------} --------------------------------------------------------------------}
instance Show a => Show (Set a) where 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 :: (Show a) => [a] -> ShowS
showSet [] showSet []
...@@ -527,17 +529,18 @@ showSet (x:xs) ...@@ -527,17 +529,18 @@ showSet (x:xs)
Read Read
--------------------------------------------------------------------} --------------------------------------------------------------------}
instance (Read a, Ord a) => Read (Set a) where instance (Read a, Ord a) => Read (Set a) where
readsPrec _ = readParen False $ \ r -> #ifdef __GLASGOW_HASKELL__
[(fromList xs,t) | ("{",s) <- lex r, readPrec = parens $ prec 10 $ do
(xs,t) <- readl s] Ident "fromList" <- lexP
where readl s = [([],t) | ("}",t) <- lex s] ++ xs <- readPrec
[(x:xs,u) | (x,t) <- reads s return (fromList xs)
, (xs,u) <- readl' t] #else
readl' s = [([],t) | ("}",t) <- lex s] ++ readsPrec p = readParen (p > 10) $ \ r -> do
[(x:xs,v) | (",",t) <- lex s ("fromList",s) <- lex
, (x,u) <- reads t (xs,t) <- reads
, (xs,v) <- readl' u] return (fromList xs,t)
#endif
{-------------------------------------------------------------------- {--------------------------------------------------------------------
Typeable/Data Typeable/Data
--------------------------------------------------------------------} --------------------------------------------------------------------}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment