我正在嘗試編寫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
以便快速檢查可以報告什麼樣的輸入導致了失敗,但沒有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
的功能我自己什麼都不做的情況下...
instance Show (a -> b) where
show _ = "[func]"
...這編譯,但觸發與-Wall
警告...
Test.hs:3:9: Warning: orphan instance: instance Show (a -> b)
...這讓我覺得有這樣做一個比較正確的做法。
我的直覺告訴我答案在Test.QuickCheck.Function
模塊中,但它沒有記錄,我不能僅僅從查看類型簽名中找出什麼內容或者如何使用它。