While trying to read a list of dot-separated integers, I've noticed a strange thing.
Prelude> (reads "123") :: [(Integer,String)]
[(123,"")]
Prelude> (reads "123.") :: [(Integer,String)]
[(123,".")]
Prelude> (reads "123.456") :: [(Integer,String)]
[]
I understand why it happens in terms of the implementation (readNumber
succeeds and then convert
fails), and I understand how to use readDec
to overcome it.
My question is, is this behaviour documented somewhere in the report? If so, why?
Not only is this not documented in the report, it appears to be at variance from the report.
There are many places where for efficiency reasons or otherwise the GHC standard libraries diverge from the reference implementations in the Report. But, unless carefully noted, the implementations should match behavior.
However, if we look at the relevant part of the Haskell Report, we see
instance Read Int where
readsPrec p r = [(fromInteger i, t) | (i,t) <- readsPrec p r]
-- Reading at the Integer type avoids
-- possible difficulty with minInt
instance Read Integer where
readsPrec p = readSigned readDec
We see that it uses a different implementation that I don't think has this behavior.
So this is something that certainly should be raised to the libraries list at haskell.org.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With