我正在使用QuickCheck v1。这是一个简单的prop_xxx定义如下:
prop_foo :: (Num a) =>[a] -> Bool
prop_foo xs = (reverse.reverse) xs == id xs
这可以在GHCi中正确测试: quickCheck prop_foo
但是,当我尝试用以下函数包装调用时:
f :: IO ()
f = quickCheck prop_foo
它报告了错误:
Ambiguous type variable `a' in the constraints:
`Num a' arising from a use of `prop_foo' at Foo.hs:147:15-22
`Arbitrary a'
arising from a use of `quickCheck' at Foo.hs:147:4-22
Probable fix: add a type signature that fixes these type variable(s)
我应该提供类似
的内容instance Arbitrary Xxx where
arbitrary = ...
coarbitrary c = ...
非常感谢。
- 拉里
答案 0 :(得分:6)
你必须给它一个单态类型签名,比如
prop_foo :: [Int] -> Bool
毕竟,问题是:在您的原始版本中,a
应该quickCheck
选择使用哪种类型来测试该功能? a = Int
? a = Double
?别的什么?错误消息抱怨a
不明确,即没有唯一的选择。