Haskell QuickCheck 最小计数器示例
Haskell QuickCheck minimal counter example
考虑以下针对 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.
第二次测试通过的原因是什么?
Update 在使用 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)
当您实际评估 test2
时,GHCi 必须选择一个类型 a
来使用。在没有更多信息的情况下,GHCi 的扩展默认规则使其默认为 ()
,法律适用。
> verboseCheck test2
Passed:
[]
[]
Passed:
[]
[]
Passed:
[(),()]
[()]
Passed:
[(),(),()]
[()]
Passed:
[()]
[(),(),(),()]
...
多态参数默认为()
,当然这样的值都是相等的
考虑以下针对 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.
第二次测试通过的原因是什么?
Update 在使用 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)
当您实际评估 test2
时,GHCi 必须选择一个类型 a
来使用。在没有更多信息的情况下,GHCi 的扩展默认规则使其默认为 ()
,法律适用。
> verboseCheck test2
Passed:
[]
[]
Passed:
[]
[]
Passed:
[(),()]
[()]
Passed:
[(),(),()]
[()]
Passed:
[()]
[(),(),(),()]
...
多态参数默认为()
,当然这样的值都是相等的