
Hi fellow Haskellers! If I bring up ghci and evaluate `read 2` I get the error "Prelude.read: no parse" GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help Prelude> read "2" *** Exception: Prelude.read: no parse Exactly how is this message coming about? I understand that `read` needs a return type in order to determine which type class instance to run. So, in this case, which type is `read` being asked to return? Thanks, Erik

GHCi enables -XExtendedDefaultRules, which cause read "2" to default to (). On 5/27/2016 8:28 PM, Erik Rantapaa wrote:
Hi fellow Haskellers!
If I bring up ghci and evaluate `read 2` I get the error "Prelude.read: no parse"
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help Prelude> read "2" *** Exception: Prelude.read: no parse
Exactly how is this message coming about? I understand that `read` needs a return type in order to determine which type class instance to run. So, in this case, which type is `read` being asked to return?
Thanks, Erik
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Without a type signature, all GHCI knows:
Prelude> :t read "2"
read "2" :: Read a => a
is that it should return some kind of Read -- that is, something that can
be from a string. GHCI is not being asked to return any (concrete) type.
If you specify a type -- and if that type can be expressed as a string
containing a single digit -- then it works:
Prelude> read "2" :: Float
2.0
And otherwise it won't:
Prelude> read "2" :: [Float]
*** Exception: Prelude.read: no parse
You don't necessarily have to provide a type signature, though, if it can
be inferred from context:
Prelude> floor $ read "2"
2
Prelude> :t floor
floor :: (Integral b, RealFrac a) => a -> b
Prelude> :t floor $ read "2"
floor $ read "2" :: Integral b => b
Note that GHCI in this case still does not know exactly the type of (floor
$ read "2"), but thanks to the type of floor, it knows enough to proceed.
On Fri, May 27, 2016 at 11:38 AM, David Kraeutmann
GHCi enables -XExtendedDefaultRules, which cause read "2" to default to (). On 5/27/2016 8:28 PM, Erik Rantapaa wrote:
Hi fellow Haskellers!
If I bring up ghci and evaluate `read 2` I get the error "Prelude.read: no parse"
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help Prelude> read "2" *** Exception: Prelude.read: no parse
Exactly how is this message coming about? I understand that `read` needs a return type in order to determine which type class instance to run. So, in this case, which type is `read` being asked to return?
Thanks, Erik
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Jeffrey Benjamin Brown

On 27.05.2016 20:42, Jeffrey Brown wrote:
Without a type signature, all GHCI knows:
Prelude> :t read "2" read "2" :: Read a => a
is that it should return some kind of Read -- that is, something that can be from a string. GHCI is not being asked to return any (concrete) type.
If that were true (which it would be without extensions), the code would fail to compile rather than throwing a runtime exception. But as your parent post stated, GHCi enables ExtendedDefaultRules by default, defaulting to (). Note that this means that `read "()"` works without any type annotations and produces ().
You don't necessarily have to provide a type signature, though, if it can be inferred from context:
Prelude> floor $ read "2" 2 Prelude> :t floor floor :: (Integral b, RealFrac a) => a -> b Prelude> :t floor $ read "2" floor $ read "2" :: Integral b => b
Note that GHCI in this case still does not know exactly the type of (floor $ read "2"), but thanks to the type of floor, it knows enough to proceed.
That's not accurate. If it did not know which numeric type to use, it would not be able to pick which overload of read to use (it's not like all RealFracs share the same implementation of read), so it wouldn't be able to produce a result. So again defaulting rules apply (this time even without extensions) and the `read "2"` in `floor $ read "2"` produces a Double (unless you declare a different default type).

On Friday, May 27, 2016 at 1:38:36 PM UTC-5, David Kraeutmann wrote:
GHCi enables -XExtendedDefaultRules, which cause read "2" to default to ().
Ah - thank you very much! Indeed it is a consequence of the ExtendedDefaultRules option: GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help Prelude> :set -XNoExtendedDefaultRules Prelude> read "2" <interactive>:3:1: No instance for (Read a0) arising from a use of ‘it’ On 5/27/2016 8:28 PM, Erik Rantapaa wrote:
Hi fellow Haskellers!
If I bring up ghci and evaluate `read 2` I get the error "Prelude.read: no parse"
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help Prelude> read "2" *** Exception: Prelude.read: no parse
Exactly how is this message coming about? I understand that `read` needs a return type in order to determine which type class instance to run. So, in this case, which type is `read` being asked to return?
Thanks, Erik
_______________________________________________ Haskell-Cafe mailing list Haskel...@haskell.org javascript: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
participants (4)
-
David Kraeutmann
-
Erik Rantapaa
-
Jeffrey Brown
-
Sebastian Hungerecker