如何编写一个函数来触发 QuickCheck prop_xxx?

发布于 2024-11-02 11:55:37 字数 750 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一百个冬季 2024-11-09 11:55:37

你必须给它一个单态类型签名,例如

prop_foo :: [Int] -> Bool

毕竟,问题是:在您的原始版本中,quickCheck 应该选择哪种类型 a 来测试函数? a = Inta = 双?还有别的事吗?错误消息抱怨 a 不明确,即没有唯一的选择。

You have to give it a monomorphic type signature, like

prop_foo :: [Int] -> Bool

After all, the question is: in your original version, which type a should quickCheck choose to test the function with? a = Int? a = Double? Something else? The error message complains that a is ambiguous, i.e. there is no unique choice for it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文