开发者

Why does Haskell appear to default to reading Int when reading Num?

I didn't expect the following code to work:

foo :: (Num a) => a -> a
foo x = x + x

main = do
    print (foo (read "7"))

because it is not possible to fully infer the typ开发者_运维百科e of (read "7") based on the code. But GHC (6.12.3) thinks otherwise and prints 14.

If "7" is changed to "7.2", the code fails with "no parse". What's going on here? how is Haskell deciding which instance of Read to use?


This is caused by Haskell's defaulting rules for the Num class. If you added

default (Double, Integer)

to the top of your file, then you'd get the following results:

main = do
  print (foo (read "7")) -- prints "14.0"
  print (foo (read "7.2")) -- prints "14.2"

In a nutshell, defaulting rules are an attempt to "try to do the right thing" and save you from a compile error when you have an ambiguous type in your program. Unfortunately in this case it trades a compile-time error for a runtime error.

You can disable defaulting like so:

default ()

which will force you to explicitly disambiguate the types of such terms via type annotations:

print (foo (read "7" :: Int))


Int is the default type in this instance. See sec. 6.3, Ambiguity and Type Defaulting, in A History of Haskell: Being Lazy with Class,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜