Haskell 从数组转换为未装箱数组违反了重写规则
我正在尝试将我的程序从使用 Data.Array 转换为 Data.Array.Unboxed。
作为一个快速旁注: 有几个地方声明我可以在代码中将“Array”更改为“UArray” 并添加 Data.Array.Unboxed 的导入,但是我没有混合两者 数组类型所以 我刚刚导入了 Data.Array.Unboxed 而不是 Data.Array,这是否足够?
当我进行切换时,以下重写规则会中断:
{-# RULES
"applyWindow/applyWindow" forall win1 win2 image.
applyWindow win1
(applyWindow win2
image) =
applyWindow (indexMult win1 win2)
image
#-}
这里 win1 win2 和图像都应该是 UArray。但是,这无法编译并出现以下错误。
FIPlib/Core.hs:229:99:
Ambiguous type variables `e0', `a0' in the constraint:
(IArray a0 e0) arising from a use of `applyWindow'
Probable fix: add a type signature that fixes these type variable(s)
In the expression: applyWindow (indexMult win1 win2) image
When checking the transformation rule "applyWindow/applyWindow"
FIPlib/Core.hs:229:99:
Ambiguous type variables `e0', `a2' in the constraint:
(IArray a2 e0) arising from a use of `applyWindow'
Probable fix: add a type signature that fixes these type variable(s)
In the expression: applyWindow (indexMult win1 win2) image
When checking the transformation rule "applyWindow/applyWindow"
FIPlib/Core.hs:229:112:
Ambiguous type variables `e0', `a1' in the constraint:
(IArray a1 e0) arising from a use of `indexMult'
Probable fix: add a type signature that fixes these type variable(s)
In the first argument of `applyWindow', namely
`(indexMult win1 win2)'
In the expression: applyWindow (indexMult win1 win2) image
When checking the transformation rule "applyWindow/applyWindow"
是什么让这变得模棱两可?为什么当它与 Data.Array 一起使用时会中断?
I am trying to convert my program from using Data.Array to Data.Array.Unboxed.
As a quick side note:
several places state that I can change "Array" to "UArray" in my code
and ADD an import of Data.Array.Unboxed, however I am not mixing both
types of arrays so
I just imported Data.Array.Unboxed instead of Data.Array, is this sufficient?
When I make the switch the following rewrite rule breaks:
{-# RULES
"applyWindow/applyWindow" forall win1 win2 image.
applyWindow win1
(applyWindow win2
image) =
applyWindow (indexMult win1 win2)
image
#-}
Here win1 win2 and image should all be UArrays. However, this fails to compile with the follwing errors.
FIPlib/Core.hs:229:99:
Ambiguous type variables `e0', `a0' in the constraint:
(IArray a0 e0) arising from a use of `applyWindow'
Probable fix: add a type signature that fixes these type variable(s)
In the expression: applyWindow (indexMult win1 win2) image
When checking the transformation rule "applyWindow/applyWindow"
FIPlib/Core.hs:229:99:
Ambiguous type variables `e0', `a2' in the constraint:
(IArray a2 e0) arising from a use of `applyWindow'
Probable fix: add a type signature that fixes these type variable(s)
In the expression: applyWindow (indexMult win1 win2) image
When checking the transformation rule "applyWindow/applyWindow"
FIPlib/Core.hs:229:112:
Ambiguous type variables `e0', `a1' in the constraint:
(IArray a1 e0) arising from a use of `indexMult'
Probable fix: add a type signature that fixes these type variable(s)
In the first argument of `applyWindow', namely
`(indexMult win1 win2)'
In the expression: applyWindow (indexMult win1 win2) image
When checking the transformation rule "applyWindow/applyWindow"
What makes this ambiguous? Why does this break when it works with Data.Array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于
Data.Array.Unboxed
重新导出Data.Array.IArray
,从而导出不可变数组接口的IArray
类,但是Data.Array
没有(它甚至没有导入它)。因此,如果您使用诸如bounds
、accumArray
、array
等仅导入Data.Array
的函数,则它们是数组类型中的单态性,因此不会出现歧义(元素类型中的多态性显然不会在这里造成问题,它会在适当的类型类约束下产生问题)。但如果导入 Data.Array.Unboxed,所有这些函数都会获得 IArray a e 约束,因此重写可能会涉及不同的数组类型,从而导致中断。您需要在函数或 int 规则上使用类型签名来修复类型。如果没有看到代码,我无法确切地说如何解决这个问题。注意:类型检查在 7.4 中发生了变化,在 ghc-7.4.1 中,如果导入
Data.Array.Unboxed
,没有规则的代码在没有类型签名的情况下无法编译。要修复类型,以便规则不会出现歧义,您必须
applyWindow
和paggedImage和<代码>filteredPlatedImage。
可能期望且最合理的是所有涉及的数组具有相同的类型,可以是
对于 1.,您只需添加签名,
applyWindow :: T -> T-> T
和xxImage :: T
(其中T
是所需的类型)。对于2.,您必须添加两个语言扩展,FlexibleContexts
和ScopedTypeVariables
,然后applyWindow
将获得签名,本地绑定将获得签名
xxImage :: UArray (Int,Int) e
。需要FlexibleContexts
来允许 constarint 中出现UArray
,并且需要ScopedTypeVariables
来将元素类型引入作用域,以便paddedImage
和filteredPlatedImage
完全可以获取类型签名。对于 3.,只需要ScopedTypeVariables
,applyWindow
的类型签名将是另一种强制所有数组为同一类型的方法是使用
asTypeOf
或将它们全部放入列表中(未使用的本地绑定),但 IMO 类型签名更可取。一旦所有类型都被固定(它们不一定相同,但本地绑定的类型必须由参数和结果类型确定,可能仅由参数类型确定),规则应该进行类型检查。 (可能还需要修复
indexMult
中的类型。)The problem is that
Data.Array.Unboxed
reexportsData.Array.IArray
, and hence theIArray
class for the immutable-array interface, butData.Array
doesn't (it doesn't even import it). So if you use functions likebounds
,accumArray
,array
etc. with onlyData.Array
imported, they are monomorphic in the array type, thus no ambiguity arises (the polymorphism in the element type apparently doesn't pose a problem here, it would with suitable type class constraints). But if you importData.Array.Unboxed
, all these functions get anIArray a e
constraint, and thus the rewriting might involve different array types, thus breaks. You need to fix the types with type signatures, on the functions or int the rule. How exactly to solve this I cannot say without seeing the code.Note: Type checking has changed with 7.4, with ghc-7.4.1, the code without the rule doesn't compile without type signatures if importing
Data.Array.Unboxed
.To fix the types, so that the rule doesn't run into ambiguity, you have to give type signatures
applyWindow
,paddedImage
andfilteredPaddedImage
.The probably desired and most reasonable is for all involved arrays to have the same type, that can be
UArray (Int,Int) Int
(element type could also beWord
, ...)For 1., you simply have to add the signatures,
applyWindow :: T -> T -> T
andxxImage :: T
(whereT
is the desired type). For 2., you have to add two language extensions,FlexibleContexts
andScopedTypeVariables
, thenapplyWindow
would get the signatureand the local bindings would get the signature
xxImage :: UArray (Int,Int) e
.FlexibleContexts
is needed to allow the occurrence ofUArray
in the constarint, andScopedTypeVariables
is necessary to bring the element type into scope so thatpaddedImage
andfilteredPaddedImage
can get type signatures at all. For 3., onlyScopedTypeVariables
is needed, the type signature ofapplyWindow
would then beAnother method to force all arrays to the same type is using
asTypeOf
or putting them all into a list (unused local binding), but IMO type signatures are preferable.Once all types are fixed (they need not necessarily be the same, but the type of the local bindings must be determined by the argument and result types, possibly the argument types alone), the rule should type check. (It may be necessary to fix types in
indexMult
too.)当我尝试运行此代码 没有数组的显式类型签名(在这种情况下是
UArray Int Bool
),只需使用Data.Array.Unboxed
导入即可。当我添加签名时一切正常。也许这也会对你有帮助。I've received exactly same error message (about IArray etc.) when I tried running this code without the explicit type signature for the array (
UArray Int Bool
in that case), just with theData.Array.Unboxed
import. When I added the signature all was OK. Maybe this will help you too.