Overloading resolution with numbers

Hello. Consider the following ghci session: Prelude> :t read "2" + 1 read "2" + 1 :: (Num a, Read a) => a Prelude> :t read "2.3" + 1 read "2.3" + 1 :: (Num a, Read a) => a Prelude> read "2" + 1 3 Prelude> read "2.3" + 1 *** Exception: Prelude.read: no parse Why does (read "2" + 1) works, but (read "2.3" + 1) fail at runtime? Romildo

Why does (read "2" + 1) works, but (read "2.3" + 1) fail at runtime?
Try this: read "2.3" + 1 :: Float Or this: read "2.3" + 1.0 The reason that your version didn't work is because GHCi is guessing that you want the read operation to parse an Integer, since you're adding it to 1.

On Thu, Apr 05, 2012 at 01:18:39PM +0000, Amy de Buitléir wrote:
writes: Why does (read "2" + 1) works, but (read "2.3" + 1) fail at runtime?
Try this:
read "2.3" + 1 :: Float
Or this:
read "2.3" + 1.0
The reason that your version didn't work is because GHCi is guessing that you want the read operation to parse an Integer, since you're adding it to 1.
This is explanation does not seem to be enough once we consider the type of the literal 1: Prelude> :t 1 1 :: Num a => a That is, the literal 1 is overloaded to any numeric type. It is not necessarily an Integer. Romildo

The 1 is not necessarily an Integer nor is it necessarily a Float, Double
or Rational. GHC applies so called type-defaulting rules to make a
best guess in this scenario. Informally, if it could be an Integer (due
to lack of static evidence to the contrary), then you can expect the
defaulting rules to assign it that type.
--
Jason Dusek
On Apr 5, 2012 7:45 AM,
writes: Why does (read "2" + 1) works, but (read "2.3" + 1) fail at runtime?
Try this:
read "2.3" + 1 :: Float
Or this:
read "2.3" + 1.0
The reason that your version didn't work is because GHCi is guessing
On Thu, Apr 05, 2012 at 01:18:39PM +0000, Amy de Buitléir wrote: that you
want the read operation to parse an Integer, since you're adding it to 1.
This is explanation does not seem to be enough once we consider the type of the literal 1:
Prelude> :t 1 1 :: Num a => a
That is, the literal 1 is overloaded to any numeric type. It is not necessarily an Integer.
Romildo
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

See this:
Prelude> :t 2.3
2.3 :: Fractional a => a
Hope this helps.
On Thu, Apr 5, 2012 at 7:24 PM,
writes: Why does (read "2" + 1) works, but (read "2.3" + 1) fail at runtime?
Try this:
read "2.3" + 1 :: Float
Or this:
read "2.3" + 1.0
The reason that your version didn't work is because GHCi is guessing
On Thu, Apr 05, 2012 at 01:18:39PM +0000, Amy de Buitléir wrote: that you
want the read operation to parse an Integer, since you're adding it to 1.
This is explanation does not seem to be enough once we consider the type of the literal 1:
Prelude> :t 1 1 :: Num a => a
That is, the literal 1 is overloaded to any numeric type. It is not necessarily an Integer.
Romildo
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Thanks and regards, -Damodar Kulkarni

On Thu, Apr 5, 2012 at 8:29 PM, damodar kulkarni
See this: Prelude> :t 2.3 2.3 :: Fractional a => a
Hope this helps.
On Thu, Apr 5, 2012 at 7:24 PM,
wrote:
writes: Why does (read "2" + 1) works, but (read "2.3" + 1) fail at runtime?
Try this:
read "2.3" + 1 :: Float
Or this:
read "2.3" + 1.0
The reason that your version didn't work is because GHCi is guessing
want the read operation to parse an Integer, since you're adding it to
On Thu, Apr 05, 2012 at 01:18:39PM +0000, Amy de Buitléir wrote: that you 1.
This is explanation does not seem to be enough once we consider the type of the literal 1:
Prelude> :t 1 1 :: Num a => a
That is, the literal 1 is overloaded to any numeric type. It is not necessarily an Integer.
Romildo
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Thanks and regards, -Damodar Kulkarni
-- Thanks and regards, -Damodar Kulkarni

Because this works
Prelude> read "2.3" + 1.0
3.3
Seriously I can only assume that via the inference it decides based on the
one that the string must be of type Int and uses the Read instance of Int
to parse it; whereas that string is obviously a float.
That is what* I think* it does.
On Thu, Apr 5, 2012 at 4:16 PM,
Hello.
Consider the following ghci session:
Prelude> :t read "2" + 1 read "2" + 1 :: (Num a, Read a) => a
Prelude> :t read "2.3" + 1 read "2.3" + 1 :: (Num a, Read a) => a
Prelude> read "2" + 1 3
Prelude> read "2.3" + 1 *** Exception: Prelude.read: no parse
Why does (read "2" + 1) works, but (read "2.3" + 1) fail at runtime?
Romildo
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Google+: https://plus.google.com/111881868112036203454
participants (5)
-
Amy de Buitléir
-
damodar kulkarni
-
j.romildo@gmail.com
-
Jason Dusek
-
Marius Ghita