简体   繁体   English

为什么 Haskell 在读取 Num 时似乎默认读取 Int?

[英]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 type of (read "7") based on the code.因为无法根据代码完全推断(阅读“7”)的类型。 But GHC (6.12.3) thinks otherwise and prints 14.但是 GHC (6.12.3) 不这样认为并打印 14。

If "7" is changed to "7.2", the code fails with "no parse".如果将“7”更改为“7.2”,则代码将失败并显示“no parse”。 What's going on here?这里发生了什么? how is Haskell deciding which instance of Read to use? Haskell 如何决定使用哪个读取实例?

This is caused by Haskell's defaulting rules for the Num class .这是由 HaskellNum 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. Int 是此实例中的默认类型。 See sec.见秒。 6.3, Ambiguity and Type Defaulting, in A History of Haskell: Being Lazy with Class, 6.3,歧义和类型默认,在 Haskell 的历史中:懒惰与 Class,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM