Haskell QuickCheck最小计数器示例

时间:2015-04-08 05:30:18

标签: haskell quickcheck

考虑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)

2 个答案:

答案 0 :(得分:14)

当您实际评估test2时,GHCi必须选择要使用的类型a。如果没有更多信息,GHCi的扩展默认规则会使其默认为(),而法律属实。

答案 1 :(得分:13)

> verboseCheck test2 

Passed:
[]
[]
Passed:
[]
[]
Passed:
[(),()]
[()]
Passed:
[(),(),()]
[()]
Passed:
[()]
[(),(),(),()]
...

多态参数默认为(),当然所有这些值都相等。