Numerals in input vs numerals in source

Consider e1 = read "1" :: Integer e2 = read "1" :: Rational e3 = read "1" :: Double e1 and e3 evaluate to 1 and 1.0 respectively e2 produces an error: "Prelude.read: no parse" Yet (1::Integer, 1::Rational, 1::Double) evaluates to (1, 1%1, 1.0) Is the inconsistent parsing defensible, or just an (irrational!) fact? I believe a reconciliation would break nothing. Doug McIlroy

On Tue, Sep 23, 2014 at 11:19 PM, Doug McIlroy
Yet (1::Integer, 1::Rational, 1::Double) evaluates to (1, 1%1, 1.0)
Is the inconsistent parsing defensible, or just an (irrational!) fact? I believe a reconciliation would break nothing.
The parsing is the same; read is not expected to throw a fromIntegral into the mix, whereas the compiler is required to. (The 1 is parsed as Integer by the compiler in all three cases, but the resulting AST is (fromIntegral 1) in all three cases.) I suspect a Read instance that included a fromIntegral would be even more irrational. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On Tue, Sep 23, 2014 at 11:49 PM, Brandon Allbery
(The 1 is parsed as Integer by the compiler in all three cases, but the resulting AST is (fromIntegral 1) in all three cases.)
Actually, to be pedantic, it's fromInteger and the resulting AST node is something like ((fromInteger :: Num a => Integer -> a) (1 :: Integer)) -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

I am not sure this is an answer but languages like Python and R are much less fussy and if you want to use data that they parse quite happily then you have to do something like newtype LaxDouble = LaxDouble { laxDouble :: Double } and write your own Read instance.

On 09/24/2014 04:14 AM, Dominic Steinitz wrote:
I am not sure this is an answer but languages like Python and R are much less fussy and if you want to use data that they parse quite happily then you have to do something like
newtype LaxDouble = LaxDouble { laxDouble :: Double }
and write your own Read instance.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
The following works just fine. I think this is correct behavior. read "1%1" :: Rational -- Jonathan Paugh jpaugh@gmx.us
participants (4)
-
Brandon Allbery
-
Dominic Steinitz
-
Doug McIlroy
-
Jonathan Paugh