问题 显示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

我已经尝试过编写自己的do-nothing实例 Show 功能......

instance Show (a -> b) where
    show _ = "[func]"

...编译,但触发警告 -Wall...

Test.hs:3:9: Warning: orphan instance: instance Show (a -> b)

......这让我觉得有更正确的方法可以做到这一点。

我的直觉告诉我答案在于 Test.QuickCheck.Function 模块,但它没有文档,我只能通过查看类型签名来弄清楚它的内容是什么或者它是如何使用的。


4742
2018-03-06 04:05


起源



答案:


你是对的 Test.QuickCheck.Function 是正确的答案。 您只需更改类型:

prop_composition       :: Fun Int Int -> Fun Int Int -> Int -> Bool
prop_composition f g x = ((apply f) . (apply g)) x == (apply f) ((apply g) x)

10
2018-03-06 09:46





import Text.Show.Functions 也可用于保留原始签名。


3
2018-05-23 01:19