显示QuickCheck属性中使用的函数

我正在尝试编写一个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
模块,但它没有文档记录,我只能通过查看类型签名来弄清楚其中的内容是什么或者打算如何使用它。     
已邀请:
你是对的
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)
    
import Text.Show.Functions
也可用于保留原始签名。     

要回复问题请先登录注册