11

我正在尝试编写一个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

我尝试编写自己的Showfor 函数的无操作实例...

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

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

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

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

我的直觉告诉我答案就在Test.QuickCheck.Function模块中,但它没有记录在案,而且我无法仅通过查看类型签名来弄清楚其中的任何内容或打算如何使用它。

4

2 回答 2

10

你是对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)
于 2011-03-06T09:46:26.387 回答
3

import Text.Show.Functions也可以与保留原始签名一起使用。

于 2012-05-23T01:19:26.387 回答