Binary library

Jan de Wit jwit@students.cs.uu.nl
Wed, 21 Nov 2001 13:43:48 +0100 (MET)


> Can anyone spot the fault in the derived instance of Read produced
> by DrIFT?
> 
> Regards,
>     Malcolm

The precedence of things printed or read after a label should IMO be
lower than 10, probably even 0. In other words: calls to 
showsPrec/readsPrec 10 should be replaced by showsPrec/readsPrec 0 in the
code below. 

I haven't checked this though, however I've written a generic
version of read and show using Generic Haskell which omits the superfluous
parentheses by using precedence 0.

Cheers, Jan

> 
> > 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