[Haskell-cafe] Derived Read instance for types with infix
constructors (ghc 6.4.1)
Robert Dockins
robdockins at fastmail.fm
Fri Aug 25 19:45:06 EDT 2006
On Aug 25, 2006, at 6:50 PM, Misha Aizatulin wrote:
> hi,
>
> the Haskell Report 10.4 says that
>
> "The result of show is readable by read if all component types are
> readable"
>
> however if I define a type like
>
> data T = A | T `And` T deriving (Read, Show)
>
> then
>
> *Main> show $ A `And` A
> "A And A"
> *Main> (read "A And A") :: T
> *** Exception: Prelude.read: no parse
> *Main>
>
> In fact, I wasn't able to guess, what I should type so that the
> value
> (A `And` A) gets parsed.
>
> I have ghc 6.4.1. Looking into the code of the derived instance I
> see
> that it expects Text.Read.Lex.lex to return (Symbol "And") for the
> constructor. If I understand the code for lex correctly, then it
> parses
> things as Symbol if they consist only of
> "!@#$%&*+./<=>?\\^|:-~"
>
> How then do I read values of type T defined above? Thanks in advance
> for any directions.
In general, derived Read instances are designed to be inverses for
Show. The easy thing to do is to print values of type T and see what
you get. I expect that it will be in prefix form, eg:
And A A
or
And (And A A) A
etc.
That is, I think the Show and Read instances are going to ignore the
backticks in the definition.
> Cheers,
> Misha
Rob Dockins
Speak softly and drive a Sherman tank.
Laugh hard; it's a long way to the bank.
-- TMBG
More information about the Haskell-Cafe
mailing list