Binary library

Andres Loeh andres@cs.uu.nl
Wed, 21 Nov 2001 13:43:11 +0100


> Can anyone spot the fault in the derived instance of Read produced
> by DrIFT?
> 
> Regards,
>     Malcolm
> 
> > data List a = Nil | Cons { hd :: a, tl :: List a }
> >
> > {-* Generated by DrIFT-v1.0 : Look, but Don't Touch. *-}
> > instance (Show a) => Show (List a) where
> >     showsPrec d (Nil) = showString "Nil"
> >     showsPrec d (Cons aa ab) = showParen (d >= 10)
> > 	      (showString "Cons" . showChar '{' .
> > 	       showString "hd" . showChar '=' . showsPrec 10 aa . showChar ',' .
> > 	       showString "tl" . showChar '=' . showsPrec 10 ab
> > 	       . showChar '}')
> > 
> > instance (Read a) => Read (List a) where
> >     readsPrec d input =
> > 	      (\ inp -> [((Nil) , rest) | ("Nil" , rest) <- lex inp]) input
> > 	      ++
> > 	      readParen (d > 9)
> > 	      (\ inp ->
> > 	       [((Cons aa ab) , rest) | ("Cons" , inp) <- lex inp ,
> > 		("{" , inp) <- lex inp , ("hd" , inp) <- lex inp ,
> > 		("=" , inp) <- lex inp , (aa , inp) <- readsPrec 10 inp ,
> > 		("," , inp) <- lex inp , ("tl" , inp) <- lex inp ,
> > 		("=" , inp) <- lex inp , (ab , inp) <- readsPrec 10 inp ,
> > 		("}" , rest) <- lex inp])
> > 	      input

Since the Report states that the derived Show instances only insert
parentheses where needed and that the derived Read instances can
read the output produced by show, I would suggest to change all
occurences of {showsPrec 10,readsPrec 10} to {showsPrec 0,readsPrec 0}
in the above code.

Best,
  Andres

-- 

Andres Loeh, Universiteit Utrecht

mailto:andres@cs.uu.nl     mailto:mail@andres-loeh.de
http://www.andres-loeh.de