考虑以下关于reverse和++之间分配律的测试:链接。
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)