[project @ 2005-09-28 13:18:28 by malcolm]
authormalcolm <unknown>
Wed, 28 Sep 2005 13:18:28 +0000 (13:18 +0000)
committermalcolm <unknown>
Wed, 28 Sep 2005 13:18:28 +0000 (13:18 +0000)
Add an instance of Read to correspond to the existing Show instance.
(Implementation based on H'98 Report definition of 'readList'.)

Data/Set.hs

index eb41f94..99ba4a1 100644 (file)
@@ -34,7 +34,7 @@
 
 module Data.Set  ( 
             -- * Set type
-              Set          -- instance Eq,Show
+              Set          -- instance Eq,Ord,Show,Read,Data,Typeable
 
             -- * Operators
             , (\\)
@@ -522,9 +522,21 @@ showSet (x:xs)
   where
     showTail []     = showChar '}'
     showTail (x:xs) = showChar ',' . shows x . showTail xs
-    
 
 {--------------------------------------------------------------------
+  Read
+--------------------------------------------------------------------}
+instance (Read a, Ord a) => Read (Set a) where
+  readsPrec i r = [ (fromList xs, t) | ("{",s) <- lex r,  (xs,t) <- readl s ]
+      where readl s  = [([],t)   | ("}",t) <- lex s] ++
+                       [(x:xs,u) | (x,t)   <- readsPrec i s
+                                 , (xs,u)  <- readl' t]
+            readl' s = [([],t)   | ("}",t) <- lex s] ++
+                       [(x:xs,v) | (",",t) <- lex s
+                                 , (x,u)   <- readsPrec i t
+                                 , (xs,v)  <- readl' u]
+    
+{--------------------------------------------------------------------
   Typeable/Data
 --------------------------------------------------------------------}