模糊类型变量 [英] Ambiguous type variable

查看:64
本文介绍了模糊类型变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前关于遍历数据结构的问题 ,将代码与uniplate软件包一起使用时,我在使代码通用时遇到问题.我正在处理

Related to my earlier question on traversing data structures, I'm having a problem making my code generic when I use it along with the uniplate package. I'm dealing with the data structures in the Language.Exts.Annotated.Syntax module, which are all generic with a type parameter l. This l is the same throughout the tree.

我正在编写的代码是这样的:

The kind of code I'm writing is like this:

doInt :: Child1 l -> Child1 l
doInt (Child1 l n) = Child1 l (n + 1)

doString :: Child2 l -> Child2 l
doString (Child2 l (_:s)) = Child2 l ('j' : s)

replace :: Data l => Parent l -> Parent l
replace = transformBi doInt
        . transformBi doString

此代码在后两行均产生以下错误:

This code produces the following error on both of the last two lines:

Ambiguous type variable `l' in the constraint:
  `Data l' arising from a use of `transformBi' at Test.hs:31:10-52
Probable fix: add a type signature that fixes these type variable(s)

我可以看到为什么这段代码是模棱两可的:transformBi接受(to -> to)from并将其转换为from;就我而言,在Child1 l中的lParent l中的l之间没有链接.我看不到如何解决它.我曾尝试添加类似transformBi (doInt :: Child1 l -> Child1 l)的类型约束,但是却遇到了同样的错误.就像我在执行此操作时要引入一个新的l.

I can see why this code is ambiguous: transformBi accepts a (to -> to) and from and turns it into a from; in my case there's no link between the l in Child1 l and the l in Parent l. What I don't see is how to fix it. I've tried adding a type constraint like transformBi (doInt :: Child1 l -> Child1 l), but I get the same error; it's as if I'm introducing a new l when I do this.

如何告诉编译器我对replacetransformBi doInttransformBi doString使用相同的l?

How can I tell the compiler that I'm using the same l for replace, transformBi doInt and transformBi doString?

这是演示我在做什么的完整程序.在GHC 6.10.4下,该程序无法编译,并出现上述错误.

Here is the full program that demonstrates what I'm doing. Under GHC 6.10.4, this program fails to compile, with the above error.

推荐答案

您似乎需要请注意,量化 必须明确,以便将l纳入范围.

Note that quantification must be explicit to bring l into scope.

这篇关于模糊类型变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆