Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't reads dot-separated integers

Tags:

haskell

ghc

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?

like image 962
n. 1.8e9-where's-my-share m. Avatar asked Dec 14 '15 17:12

n. 1.8e9-where's-my-share m.


1 Answers

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.

like image 198
sclv Avatar answered Nov 01 '22 07:11

sclv