【发布时间】:2011-06-19 20:38:04
【问题描述】:
我正在 Haskell 中编写一个函数,从任何带有 Ord 元素的 ListLike 制作直方图:
import qualified Data.ListLike as LL
...
frequencies :: (Ord x, LL.ListLike xs x) => xs -> [(x, Int)]
frequencies xs = LL.map (\x->(LL.head x, LL.length x)) $ LL.group $ LL.sort xs
在尝试编译上述代码时,我收到一条关于不明确类型的错误消息:
Ambiguous type variable `full0' in the constraint:
(LL.ListLike full0 xs) arising from a use of `LL.group'
Probable fix: add a type signature that fixes these type variable(s)
In the expression: LL.group
In the second argument of `($)', namely `LL.group $ LL.sort xs'
In the expression:
LL.map (\ x -> (LL.head x, LL.length x)) $ LL.group $ LL.sort xs
LL.group 的类型为(ListLike full0 full, ListLike full item, Eq item) => full -> full0,对应于普通列表中的(Eq a) => [a]->[[a]]。
我不明白为什么模棱两可的类型会出现问题。 Haskell 是否无法以某种方式推断存在“ListLike with full as elements”之类的类型,即full0?
【问题讨论】:
标签: haskell types type-inference strong-typing