运行 针对带功能的简单测试的 QuickCheck

Running QuickCheck against Simple Test w/ Function

鉴于以下情况:

test :: (Int -> Int) -> Int -> Bool
test _ _ = True

源码编译后,我尝试运行 quickCheck test:

> quickCheck test
<interactive>:27:1:
    No instance for (Show (Int -> Int))
      arising from a use of ‘quickCheck’
    In the expression: quickCheck test
    In an equation for ‘it’: it = quickCheck test

查看此 Show instance for functions,在我看来,不存在这样的实例。

我如何 运行 quickCheck test,即绕过或解决 Int -> Int 缺少的 Show 实例?

QuickCheck 有一个特殊模块 Test.QuickCheck.Function 用于生成可以显示的 "functions"(以及 "shrinked",这是 QuickCheck 简化其反例的方式)。您可以使用 apply 将它们转换为普通函数。例如,如果您有文件:

import Test.QuickCheck
import Test.QuickCheck.Function

test :: Fun Int Int -> Int -> Bool
test _ _ = True

test2 :: Fun Int Int -> Int -> Bool
test2 f x = apply f x == x

然后在 GHCi 中:

*Main> quickCheck test
+++ OK, passed 100 tests.
*Main> quickCheck test2
*** Failed! Falsifiable (after 2 tests and 3 shrinks): 
{_->0}
1

实际上可以为函数本身定义一个 Show 实例,并使用它。但是除非你的输入类型是像 Bool 这样的有限类型,否则你将无法以这种方式打印关于函数的所有信息。您可以从 Text.Show.Functions.

导入显示 no 有用信息的虚拟实例

但是,上面使用的 Test.QuickCheck.Function.Fun 类型看起来是为了更简洁地提供基本信息而设计的,所以如果可能的话,我当然会自己使用它。