[英]If compiler of Haskell always needs polymorphic parameters of polymorphic functions to be specified with “::Int”,why “show 2” is legal
If we type show 2
,then we will get "2".But the question is show satisfies show :: Show a => a -> String
,and 2
is polymorphic, if unfortunately show 2::Int
differs from show 2::Integer
we'd have to write show 2::Int
and show 2::Integer
rather than simply show 2
. 如果我们输入
show 2
,那么我们将得到“2”。但问题是show satisfies show :: Show a => a -> String
, 2
是多态的,如果不幸show 2::Int
与show 2::Integer
我们必须编写show 2::Int
并show 2::Integer
而不是简单地show 2
。
I refuse to assume that the compiler is enough intelligent to know when (A a)=>show a
,all current instances of A are of Show, gives the same result, we needn't to specify show a::X
and when (A a)=>show a
,all current instances of A are of Show, gives different results, we have to specify show a::X
. 我拒绝假设编译器足够智能知道何时
(A a)=>show a
,A的所有当前实例都是Show,给出相同的结果,我们不需要指定show a::X
和when (A a)=>show a
,A的所有当前实例都是Show,给出不同的结果,我们必须指定show a::X
This is due to defaulting rules. 这是由于违约规则造成的。 So
show 2
is actually show (2::Integer)
. 所以
show 2
实际上是show (2::Integer)
。 You can read this in haskell 2010 report here in section 4.3.4 . 您可以在4.3.4节中的haskell 2010报告中阅读此内容 。
To answer your second question, compiler is not intelligent enough. 要回答第二个问题,编译器不够智能。 It happens due to type defaulting.
它是由于类型默认而发生的。
You can check 你可以检查一下
number = 2
In ghci 在ghci
*Main> :t number
number :: Integer
Now your custom default signature 现在您的自定义默认签名
default (Int)
number = 2
In ghci 在ghci
*Main> :t number
number :: Int
You can read about when a type is defaultable in the document I referenced. 您可以在我引用的文档中了解何时类型可以违约。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.