测试Eq类型的空列表[] [英] Testing empty list [] with Eq type
问题描述
当前,我正在Haskell中编写一个函数来检查列表是否对称.
isReflexive ::等式=>[(a,a)]->布尔isReflexive [] = TrueisReflexive xs =和[elem(x,x)xs |x<-[fst u |u<-xs] ++ [snd u |u<-xs]]测试=做打印(isReflexive [])主=测试
该函数在不为空的列表上可以正常工作.但是,当我使用该功能测试空列表时,它引发了错误
由于使用"isReflexive"而产生的模棱两可的类型变量"a2"阻止解决约束(Eq a2)".
可能的解决方法:使用类型注释来指定"a2"应该是什么.
这些潜在的实例存在:
实例方程式排序-在"GHC.Classes"中定义
instance Eq Integer-在"integer-gmp-1.0.2.0:GHC.Integer.Type"中定义
instance Eq a => Eq(也许a)-在"GHC.Maybe"中定义...再加上22个人
...加上涉及范围外类型的7个实例
(使用-fprint-potential-instances查看全部内容)
•在打印"的第一个参数中,即(isReflexive [])"如何解决此错误?
问题很简单,为了应用 isReflexive
,GHC需要知道您在哪种类型上使用它.
isReflexive
的类型签名- Eq a =>[(a,a)]->Bool
没有告诉GHC该函数可以使用的具体类型.没关系,这很正常,但是通常,调用该函数的代码使该特定应用程序中的 a
确切是什么.在这里不是这样,因为 []
本身具有多态(因此模棱两可)的类型, [a]
(对于任何 a
)./p>
要修复此问题,只需在此处为 []
提供一个具体类型,该类型与 isReflexive
的签名一致.真的什么都没关系,但是许多可行的例子是:
test =做print(isReflexive([] :: [(Int,Int)]))
(请注意,这正是GHC在提示可能的解决方法时告诉您的信息:使用类型注释指定"a2"应为
.该消息中的"a2"对应于"a"'在这里,GHC倾向于使用'a1','a2'等来引用所有类型变量.)
Currently, I am writing a function in Haskell to check a list is symmetric or not.
isReflexive :: Eq a => [(a, a)] -> Bool
isReflexive [] = True
isReflexive xs = and [elem (x, x) xs | x <- [fst u | u <- xs] ++ [snd u | u <- xs]]
test = do
print(isReflexive [])
main = test
The function works fine on the list that is not empty. However, when I test the empty list with the function, it raised an error
Ambiguous type variable ‘a2’ arising from a use of ‘isReflexive’ prevents the constraint ‘(Eq a2)’ from being solved.
Probable fix: use a type annotation to specify what ‘a2’ should be.
These potential instances exist:
instance Eq Ordering -- Defined in ‘GHC.Classes’
instance Eq Integer -- Defined in ‘integer-gmp-1.0.2.0:GHC.Integer.Type’
instance Eq a => Eq (Maybe a) -- Defined in ‘GHC.Maybe’ ...plus 22 others
...plus 7 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the first argument of ‘print’, namely ‘(isReflexive [])’ How to fix this error?
The problem is simply that, in order to apply isReflexive
, GHC needs to know which type you are using it on.
The type signature of isReflexive
- Eq a => [(a, a)] -> Bool
doesn't tell GHC a concrete type that the function works on. That's perfectly fine, and usual, but most often the code that calls the function makes it clear what exactly a
is in that particular application. That's not so here, because []
has itself a polymorphic (and therefore ambiguous) type, [a]
(for any a
).
To fix it you simply have to provide a concrete type for your []
here, which is consistent with the signature of isReflexive
. It really doesn't matter what, but an example from many that will work is:
test = do
print(isReflexive ([] :: [(Int, Int)]))
(Note that this is exactly what GHC is telling you when it says Probable fix: use a type annotation to specify what 'a2' should be
. 'a2' in that message corresponds to 'a' here, GHC tends to use 'a1', 'a2' etc to refer to all type variables.)
这篇关于测试Eq类型的空列表[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!