考虑一下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)发布于 2015-04-08 05:37:16
当您实际计算test2时,GHCi必须选择要使用的a类型。在没有更多信息的情况下,GHCi的扩展默认规则使其默认为(),对此法律是正确的。
发布于 2015-04-08 05:37:45
> verboseCheck test2
Passed:
[]
[]
Passed:
[]
[]
Passed:
[(),()]
[()]
Passed:
[(),(),()]
[()]
Passed:
[()]
[(),(),(),()]
...多态参数默认为(),当然所有这些值都是相等的。
https://stackoverflow.com/questions/29506655
复制相似问题