Pau*_*icz 11 haskell 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
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不能编译,因为属性的输入需要实现,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
Run Code Online (Sandbox Code Playgroud)
我已经尝试编写自己的do-nothing实例Show函数了...
instance Show (a -> b) where
show _ = "[func]"
Run Code Online (Sandbox Code Playgroud)
...编译,但触发警告-Wall......
Test.hs:3:9: Warning: orphan instance: instance Show (a -> b)
Run Code Online (Sandbox Code Playgroud)
......这让我觉得有更正确的方法可以做到这一点.
我的直觉告诉我答案在于Test.QuickCheck.Function模块,但它没有文档记录,我只能通过查看类型签名来弄清楚其中的内容是什么或者如何使用它.
edo*_*don 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)
Run Code Online (Sandbox Code Playgroud)