[Haskell-cafe] a bug in Read instance of Int?

Javran Cheng javran.c at gmail.com
Wed Mar 27 07:16:33 UTC 2019


Hi Cafe,

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."
[(123,".")]
Prelude> reads @Int "123.aaa"
[(123,".aaa")]
Prelude> reads @Int "123.456aaa"
[] -- I expected this to be [(123,".456aaa")]
Prelude> reads @Double "123.234aaa"
[(123.234,"aaa")]

Further investigation shows that [realNumber](
http://hackage.haskell.org/package/base/docs/src/GHC.Read.html#readNumber)
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
backtracking impossible.

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.

Javran
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190327/ed484d3b/attachment.html>


More information about the Haskell-Cafe mailing list