erik writes:
> [...]
> instance Read A where
>    readsPrec _ s = case s of  { "1" -> [(A,"")]; "2" -> [(AA,"")] ;  _  ->
> []}
> 
> [...]
> 
> Test> read (show X{a=A}) :: X
> 
> Program error: PreludeText.read: no parse

Try the following (one could write it even nicer, but I tried to change
the code as little as possible):

----------------------------------------------------------------------
instance Read A where
   readsPrec _ s = case s of { '1':xs -> [(A,xs)]; '2':xs -> [(AA,xs)] ; _ -> []}
----------------------------------------------------------------------

and you will get:

----------------------------------------------------------------------
Test> read (show X{a=A}) :: X
X{a=1}
----------------------------------------------------------------------

Your definition of readsPrec can parse an A only at the very end of the
input string. And in "X{a=1}" the `1' is not at the very end.

Regards, Heribert.


PS: This problem is a good argument for Phil Wadlers suggestion to
replace Read (and Show) by something less awkward.
(http://www.cs.chalmers.se/~rjmh/Haskell/Messages/Display.cgi?id=5)



Reply via email to