Commit 793cc56f authored by ross's avatar ross

[project @ 2005-10-21 10:47:25 by ross]

conformant Show and Read instances.

(sorry to jump in, JP, but I had these to hand)
parent a5d8b458
...@@ -147,6 +147,7 @@ import qualified List ...@@ -147,6 +147,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
...@@ -978,8 +979,8 @@ instance Functor IntMap where ...@@ -978,8 +979,8 @@ instance Functor IntMap where
--------------------------------------------------------------------} --------------------------------------------------------------------}
instance Show a => Show (IntMap a) where instance Show a => Show (IntMap a) where
showsPrec d t = showMap (toList t) showsPrec d m = showParen (d > 10) $
showString "fromList " . shows (toList m)
showMap :: (Show a) => [(Key,a)] -> ShowS showMap :: (Show a) => [(Key,a)] -> ShowS
showMap [] showMap []
...@@ -991,7 +992,23 @@ showMap (x:xs) ...@@ -991,7 +992,23 @@ showMap (x:xs)
showTail (x:xs) = showChar ',' . showElem x . showTail xs showTail (x:xs) = showChar ',' . showElem x . showTail xs
showElem (k,x) = shows k . showString ":=" . shows x showElem (k,x) = shows k . showString ":=" . shows x
{--------------------------------------------------------------------
Read
--------------------------------------------------------------------}
instance (Read e) => Read (IntMap e) 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
--------------------------------------------------------------------} --------------------------------------------------------------------}
......
...@@ -161,6 +161,7 @@ import List(nub,sort) ...@@ -161,6 +161,7 @@ import List(nub,sort)
-} -}
#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
...@@ -1308,16 +1309,17 @@ instance Functor (Map k) where ...@@ -1308,16 +1309,17 @@ instance Functor (Map k) where
Read Read
--------------------------------------------------------------------} --------------------------------------------------------------------}
instance (Ord k, Read k, Read e) => Read (Map k e) where instance (Ord k, Read k, Read e) => Read (Map k e) 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) <- readPair 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) <- readPair t (xs,t) <- reads
, (xs,v) <- readl' u] return (fromList xs,t)
#endif
-- parses a pair of things with the syntax a:=b -- parses a pair of things with the syntax a:=b
readPair :: (Read a, Read b) => ReadS (a,b) readPair :: (Read a, Read b) => ReadS (a,b)
...@@ -1330,7 +1332,8 @@ readPair s = do (a, ct1) <- reads s ...@@ -1330,7 +1332,8 @@ readPair s = do (a, ct1) <- reads s
Show Show
--------------------------------------------------------------------} --------------------------------------------------------------------}
instance (Show k, Show a) => Show (Map k a) where instance (Show k, Show a) => Show (Map k a) where
showsPrec d m = showMap (toAscList m) showsPrec d m = showParen (d > 10) $
showString "fromList " . shows (toList m)
showMap :: (Show k,Show a) => [(k,a)] -> ShowS showMap :: (Show k,Show a) => [(k,a)] -> ShowS
showMap [] showMap []
......
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