[Haskell-cafe] Derived Read instance for types with infix
constructors (ghc 6.4.1)
robdockins at fastmail.fm
Fri Aug 25 19:45:06 EDT 2006
On Aug 25, 2006, at 6:50 PM, Misha Aizatulin wrote:
> the Haskell Report 10.4 says that
> "The result of show is readable by read if all component types are
> however if I define a type like
> data T = A | T `And` T deriving (Read, Show)
> *Main> show $ A `And` A
> "A And A"
> *Main> (read "A And A") :: T
> *** Exception: Prelude.read: no parse
> In fact, I wasn't able to guess, what I should type so that the
> (A `And` A) gets parsed.
> I have ghc 6.4.1. Looking into the code of the derived instance I
> that it expects Text.Read.Lex.lex to return (Symbol "And") for the
> constructor. If I understand the code for lex correctly, then it
> 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
And (And A A) A
That is, I think the Show and Read instances are going to ignore the
backticks in the definition.
Speak softly and drive a Sherman tank.
Laugh hard; it's a long way to the bank.
More information about the Haskell-Cafe