如何编写一个函数来触发 QuickCheck prop_xxx?
我正在使用 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 = ...
“非常感谢”之类的内容吗?
-- 拉里
I am using QuickCheck v1. Here is a simple prop_xxx defined as below:
prop_foo :: (Num a) =>[a] -> Bool
prop_foo xs = (reverse.reverse) xs == id xs
This can be tested in GHCi correctly:
quickCheck prop_foo
However, when I tried to wrap the call in a function like:
f :: IO ()
f = quickCheck prop_foo
It reported the error:
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)
Shall I provide something like
instance Arbitrary Xxx where
arbitrary = ...
coarbitrary c = ...
Thanks a lot.
--
Larry
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你必须给它一个单态类型签名,例如
毕竟,问题是:在您的原始版本中,
quickCheck
应该选择哪种类型a
来测试函数?a = Int
?a = 双
?还有别的事吗?错误消息抱怨a
不明确,即没有唯一的选择。You have to give it a monomorphic type signature, like
After all, the question is: in your original version, which type
a
shouldquickCheck
choose to test the function with?a = Int
?a = Double
? Something else? The error message complains thata
is ambiguous, i.e. there is no unique choice for it.