我正在尝试编写一个QuickCheck属性,该属性将一个或多个函数作为输入。为了简单起见,考虑一个属性来检查函数组合是否等同于连续的函数应用,以及一个快速而简单的测试驱动程序:
import Test.QuickCheck
prop_composition :: (Int -> Int) -> (Int -> Int) -> Int -> Bool
prop_composition f g x = (f . g) x == f (g x)
main :: IO ()
main = quickCheck prop_composition
不幸的是,这不会编译,因为需要实现属性的输入,Show
以便 QuickCheck 可以报告导致失败的输入,但是没有Show
函数实现:
Test.hs:10:7:
No instance for (Show (Int -> Int))
arising from a use of `quickCheck' at Test.hs:10:7-33
Possible fix: add an instance declaration for (Show (Int -> Int))
In the expression: quickCheck prop_composition
In the definition of `main': main = quickCheck prop_composition
我尝试编写自己的Show
for 函数的无操作实例...
instance Show (a -> b) where
show _ = "[func]"
...编译,但触发警告-Wall
...
Test.hs:3:9: Warning: orphan instance: instance Show (a -> b)
...这让我认为有更正确的方法可以做到这一点。
我的直觉告诉我答案就在Test.QuickCheck.Function
模块中,但它没有记录在案,而且我无法仅通过查看类型签名来弄清楚其中的任何内容或打算如何使用它。