使用 ListLike 的不明确类型

发布于 2024-11-15 23:07:50 字数 1014 浏览 3 评论 0原文

我正在 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

< code>LL.group 的类型为 (ListLike full0 full, ListLike full item, Eq item) =>满-> full0 对应于 (Eq a) => [a]->[[a]] 用普通列表来说。

我不明白为什么类型不明确会出现问题。 Haskell 是否无法推断出存在这样一种类型:“ListLike with full as elements”,即 full0

I'm writing a function in Haskell to make a histogram from any ListLike with Ord elements:

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

When trying to compile the above code i get an error message about ambiguous types:

 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 has type (ListLike full0 full, ListLike full item, Eq item) => full -> full0 which corresponds to (Eq a) => [a]->[[a]] speaking in terms of ordinary lists.

I dont understand why there is a problem with ambigious types. Is Haskell somehow unable to infer that there is such a type as "ListLike with full as elements", i.e full0?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我ぃ本無心為│何有愛 2024-11-22 23:07:50

Haskell 是否无法推断出存在“ListLike with full 作为元素”这样的类型,即 full0

不,问题是有太多类型可供选择 full0,而 Haskell 编译器不知道该使用哪个。想一想:一般来说,可以有多个类型 full0 ,它是一种类似列表的类型,以 full 作为元素: [full]Data.Sequence.Seq full 或许多其他选择。并且由于LL.map定义的通用性,full0被限制为[full]来自函数频率的返回类型。

如果要将 full0 限制为 [full],一种方法是替换 定义中的 LL.map频率与普通的旧map列表。

Is Haskell somehow unable to infer that there is such a type as "ListLike with full as elements", i.e full0?

No, the problem is there are too many types to choose full0 from and the Haskell compiler does not know which to use. Think about it: in general, there can be more than one type full0 which is a list-like type with full as elements: [full] or Data.Sequence.Seq full or many other choices. And because of the generality of the definition of LL.map, full0 is not constrained to be [full] from the return type of the function frequencies.

If you want to restrict full0 to [full], one way to do this is to replace LL.map in the definition of frequencies with the plain old map for lists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文