考虑distributivity law between reverse and ++,
的以下测试import Test.QuickCheck
test :: [Int] -> [Int] -> Bool
test xs ys = reverse (xs ++ ys) == reverse xs ++ reverse ys
test2 :: (Eq a) => [a] -> [a] -> Bool
test2 xs ys = reverse (xs ++ ys) == reverse xs ++ reverse ys
请注意Int
*Main> quickCheck test
*** Failed! Falsifiable (after 5 tests and 3 shrinks):
[1]
[0]
然而,测试等份项目列表,
*Main> quickCheck test2
+++ OK, passed 100 tests.
第二次测试的原因是什么?
更新在使用main = quickCheck test2
进行编译时,模糊类型变量的后续错误会提示问题(如答案中所示),
No instance for (Eq a0) arising from a use of `test2'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
答案 0 :(得分:14)
当您实际评估test2
时,GHCi必须选择要使用的类型a
。如果没有更多信息,GHCi的扩展默认规则会使其默认为()
,而法律属实。
答案 1 :(得分:13)
> verboseCheck test2
Passed:
[]
[]
Passed:
[]
[]
Passed:
[(),()]
[()]
Passed:
[(),(),()]
[()]
Passed:
[()]
[(),(),(),()]
...
多态参数默认为()
,当然所有这些值都相等。