【发布时间】:2015-07-22 18:47:44
【问题描述】:
事实证明,在 GHC 7.10 中,这编译得很好:
mysum xs = foldr (+) 0 xs
但是这个:
mysum = foldr (+) 0
导致以下错误:
No instance for (Foldable t0) arising from a use of ‘foldr’
The type variable ‘t0’ is ambiguous
Relevant bindings include
mysum :: t0 Integer -> Integer (bound at src/Main.hs:37:1)
Note: there are several potential instances:
instance Foldable (Either a) -- Defined in ‘Data.Foldable’
instance Foldable Data.Functor.Identity.Identity
-- Defined in ‘Data.Functor.Identity’
instance Foldable Data.Proxy.Proxy -- Defined in ‘Data.Foldable’
...plus five others
In the expression: foldr (+) 0
In an equation for ‘mysum’: mysum = foldr (+) 0
为什么会发生这种情况?通过了解这种差异可以获得什么洞察力?另外,我可以给这个函数一个类型(仍然是通用的)来消除这个错误吗?
【问题讨论】:
-
可怕的单态性限制再次来袭!
-
我想,最近的GHC默认关闭了单态限制?
-
@arrowdodger,你漏掉了一封信:在最近的 GHCi 中,单态限制默认是关闭的。
标签: function haskell types monomorphism-restriction