Derived Read instance for types with infix constructors (ghc 6.4.1)

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. Cheers, Misha

Hi
*Main> show $ A `And` A "A And A"
For me, using GHCi 6.4.2 + Windows, I get: "A `And` A" Which all works perfectly. For reference, Hugs writes out "And A A", and then parses it back again. Hugs cannot cope with the "A `And` A" form with read, and GHC can't cope with "And A A". Thanks Neil

Neil Mitchell wrote:
*Main> show $ A `And` A "A And A"
For me, using GHCi 6.4.2 + Windows, I get: "A `And` A"
I installed GHC 6.4.2 now (on Linux). It really does print "A `And` A", but still doesn't read it. Would you agree that GHC doesn't conform to the Haskell Report here? In fact it seems to produce a Read instance with no valid input for it! Daniel Fischer wrote:
Put the constructor in the prefix position in the data definition and the derived Read instance of ghc 6.4.1 will also be able to read the prefix form.
This might be a solution, but I was hoping that I can have a Read instance that would read the infix form - this would be more natural for the data I have (like logical expressions). Cheers, Misha

On 8/26/06, Misha Aizatulin
Neil Mitchell wrote:
*Main> show $ A `And` A "A And A"
For me, using GHCi 6.4.2 + Windows, I get: "A `And` A"
I installed GHC 6.4.2 now (on Linux). It really does print "A `And` A", but still doesn't read it. Would you agree that GHC doesn't conform to the Haskell Report here? In fact it seems to produce a Read instance with no valid input for it!
I would agree that this is a flaw.
Daniel Fischer wrote:
Put the constructor in the prefix position in the data definition and the derived Read instance of ghc 6.4.1 will also be able to read the prefix form.
This might be a solution, but I was hoping that I can have a Read instance that would read the infix form - this would be more natural for the data I have (like logical expressions).
Before you get too caught up in deriving Read, remember that in Haskell it's very easy to create your own custom parser. Assuming you have previous experience with happy or parsec you could probably have already created a custom parser with time you've spent debugging this automatic Read instance flaw. And if you had your own parser couldn't you define read to use that parser? Just a thought... Jason

Jason Dagit wrote:
Before you get too caught up in deriving Read, remember that in Haskell it's very easy to create your own custom parser. Assuming you have previous experience with happy or parsec you could probably have already created a custom parser with time you've spent debugging this automatic Read instance flaw. And if you had your own parser couldn't you define read to use that parser? Just a thought...
You are right, I will probably end up rolling my own instances with Template Haskell or DrIFT (there are a lot of types where I need these instances). I just wanted to make sure that I'm not missing something stupid and that there is no easier way to do that. By the way, if someone already has TH code for deriving Read instances, sharing would be greatly appreciated :) Cheers, Misha

On Sat, Aug 26, 2006 at 10:15:17PM +0300, Misha Aizatulin wrote:
Neil Mitchell wrote:
*Main> show $ A `And` A "A And A"
For me, using GHCi 6.4.2 + Windows, I get: "A `And` A"
I installed GHC 6.4.2 now (on Linux). It really does print "A `And` A", but still doesn't read it. Would you agree that GHC doesn't conform to the Haskell Report here? In fact it seems to produce a Read instance with no valid input for it!
ghci on 6.4.2 Linux works for me: *Main> show (A `And` A) "A `And` A" *Main> read (show (A `And` A)) :: T A `And` A (recent 6.5 also seems fine). Thanks Ian

Extremely odd, today ghc 6.4.2 (also on linux) works for me, too: Prelude InfixR> show (A `And` A) "A `And` A" Prelude InfixR> show (And A A) "A `And` A" Prelude InfixR> read (show (And A A)) :: T A `And` A Prelude InfixR> read "And A A" :: T *** Exception: Prelude.read: no parse Prelude InfixR> read "A `And` A" :: T A `And` A (and it can't read "And A A", which is what ghc 6.2.2 could read), yesterday it couldn't read either form. What sort of hiccough could produce such behaviour????????? Bewildered, Daniel Am Samstag, 26. August 2006 21:47 schrieb Ian Lynagh:
On Sat, Aug 26, 2006 at 10:15:17PM +0300, Misha Aizatulin wrote:
Neil Mitchell wrote:
*Main> show $ A `And` A "A And A"
For me, using GHCi 6.4.2 + Windows, I get: "A `And` A"
I installed GHC 6.4.2 now (on Linux). It really does print "A `And` A", but still doesn't read it. Would you agree that GHC doesn't conform to the Haskell Report here? In fact it seems to produce a Read instance with no valid input for it!
ghci on 6.4.2 Linux works for me:
*Main> show (A `And` A) "A `And` A" *Main> read (show (A `And` A)) :: T A `And` A
(recent 6.5 also seems fine).
Thanks Ian
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- "In My Egotistical Opinion, most people's C programs should be indented six feet downward and covered with dirt." -- Blair P. Houghton

Am Sonntag, 27. August 2006 01:02 schrieb Daniel Fischer:
Extremely odd,
today ghc 6.4.2 (also on linux) works for me, too: Prelude InfixR> show (A `And` A) "A `And` A" Prelude InfixR> show (And A A) "A `And` A" Prelude InfixR> read (show (And A A)) :: T A `And` A Prelude InfixR> read "And A A" :: T *** Exception: Prelude.read: no parse Prelude InfixR> read "A `And` A" :: T A `And` A
(and it can't read "And A A", which is what ghc 6.2.2 could read), yesterday it couldn't read either form.
What sort of hiccough could produce such behaviour?????????
Bewildered, Daniel
Probably my sleep deprived memory erred and what couldn't read either form was ghc 6.4.1. Bash history confirms that this is definitely a possibility. And since it's far easier to believe that I erred in this respect than that the derived Read instance of one day could read what that from another day couldn't, I'm pretty sure that's what happened. Sorry. Another thing: Would it be a good idea to create derived Read instances that could parse both, "A `And` A" and "And A A" ? Since 6.4.2 parses the former and 6.2.2 parses the latter that should be possible, I believe (and both forms are accepted at the ghci prompt). Cheers, Daniel -- "In My Egotistical Opinion, most people's C programs should be indented six feet downward and covered with dirt." -- Blair P. Houghton

| Another thing: | Would it be a good idea to create derived Read instances that could parse | both, "A `And` A" and "And A A" ? | Since 6.4.2 parses the former and 6.2.2 parses the latter that should be | possible, I believe (and both forms are accepted at the ghci prompt). Well, the Haskell 98 Report says that the derived Read will only read the infix form if you declare the constructor infix, and vice versa. (Similarly records.) Technically it'd be very easy to make the change, although code size would increase a bit. We won't do it in 6.6 though. Simon

Daniel Fischer wrote:
Another thing: Would it be a good idea to create derived Read instances that could parse both, "A `And` A" and "And A A" ? Since 6.4.2 parses the former and 6.2.2 parses the latter that should be possible, I believe (and both forms are accepted at the ghci prompt).
I made a Template Haskell function that does exactly that. I wrote about it to the template-haskell list: http://www.haskell.org/pipermail/template-haskell/2006-September/000587.html Cheers, Misha

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

Am Samstag, 26. August 2006 00:50 schrieb Misha Aizatulin:
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.
Appearance notwithstanding, your datatype introduces the _prefix_ constructor And (infix constructors are symbols beginning with a colon, like :+ etc). And hugs and ghc 6.2.2 can read the prefix form: ghc 6.2.2: Ok, modules loaded: InfixR. *InfixR> read "A `And` A" :: T *** Exception: Prelude.read: no parse *InfixR> read "And A A" :: T And A A hugs: InfixR> And A A And A A InfixR> A `And` A And A A InfixR> read "And A A" :: T And A A InfixR> read "A `And` A" :: T Program error: Prelude.read: no parse
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.
Put the constructor in the prefix position in the data definition and the derived Read instance of ghc 6.4.1 will also be able to read the prefix form.
Cheers, Misha
Cheers, Daniel -- "In My Egotistical Opinion, most people's C programs should be indented six feet downward and covered with dirt." -- Blair P. Houghton
participants (7)
-
Daniel Fischer
-
Ian Lynagh
-
Jason Dagit
-
Misha Aizatulin
-
Neil Mitchell
-
Robert Dockins
-
Simon Peyton-Jones