[Haskell-cafe] a bug in Read instance of Int?
javran.c at gmail.com
Wed Mar 27 07:16:33 UTC 2019
Not sure if there are existing discussions, but I recently run into a
problem with Read instance of Int:
Prelude> :set -XTypeApplications
Prelude> reads @Int "123."
Prelude> reads @Int "123.aaa"
Prelude> reads @Int "123.456aaa"
 -- I expected this to be [(123,".456aaa")]
Prelude> reads @Double "123.234aaa"
Further investigation shows that [realNumber](
is used for Read instance of Int.
I think what happened is that when the leading parser input can be parsed
as a floating number, it will do so and commit to that decision, making
I do understand that Read just need to be able to parse whatever Show can
produce and is not designed to deal with raw inputs, but this is still a
surprising behavior to me.
When I'm using Text.ParserCombinators.ReadP, I really appreciates it that I
can use `readP_to_S read` to parse simple values (integers and floating
points in particular),
but it bothers me that parsing Int from "123.aaa" is fine but "123.1aa"
will fail simply because the not-yet-consumed part of input is different.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe