使 Test.QuickCheck.Batch 使用默认类型来测试列表函数

发布于 2024-07-04 20:57:54 字数 794 浏览 8 评论 0原文

我正在测试一个名为“提取”的函数,该函数可以在任何列表上运行。

extractions :: [a] -> [(a,[a])]
extractions [] = []
extractions l = extract l []
    where extract [] _ = []
          extract (x:xs) prev = (x, prev++xs) : extract xs (x : prev)

例如,我想测试它,

import Test.QuickCheck.Batch    
prop_len l = length l == length (extractions l)
main = runTests "extractions" defOpt [run prop_len]

但是这不会编译; 我必须为 runprop_len 提供类型,因为 QuickCheck 无法生成 [a],它必须生成具体的内容。 所以我选择了Int

main = runTests "extractions" defOpt [r prop_len]
    where r = run :: ([Int] -> Bool) -> TestOptions -> IO TestResult

有没有办法让QuickCheck为我选择a,而不是在run类型中指定它?

I am testing a function called extractions that operates over any list.

extractions :: [a] -> [(a,[a])]
extractions [] = []
extractions l = extract l []
    where extract [] _ = []
          extract (x:xs) prev = (x, prev++xs) : extract xs (x : prev)

I want to test it, for example, with

import Test.QuickCheck.Batch    
prop_len l = length l == length (extractions l)
main = runTests "extractions" defOpt [run prop_len]

But this won't compile; I have to supply a type either for run or prop_len, because QuickCheck can't generate [a], it has to generate something concrete. So I chose Int:

main = runTests "extractions" defOpt [r prop_len]
    where r = run :: ([Int] -> Bool) -> TestOptions -> IO TestResult

Is there any way to get QuickCheck to choose a for me instead of having it specified in the type of run?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

柏拉图鍀咏恒 2024-07-11 20:57:55

quickcheck 手册说“不”:

属性必须具有单态类型。 “多态”属性,例如上面的属性,必须限制为用于测试的特定类型。 通过在 a 中声明一个或多个参数的类型可以很方便地做到这一点

其中类型 = (x1 :: t1, x2 :: t2, ...)

条款...

The quickcheck manual says "no":

Properties must have monomorphic types. `Polymorphic' properties, such as the one above, must be restricted to a particular type to be used for testing. It is convenient to do so by stating the types of one or more arguments in a

where types = (x1 :: t1, x2 :: t2, ...)

clause...

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