Running QuickCheck versus a simple test with a feature

Given the following:

test :: (Int -> Int) -> Int -> Bool test _ _ = True 

After compiling the source, I try to run quickCheck test :

 > quickCheck test <interactive>:27:1: No instance for (Show (Int -> Int)) arising from a use of 'quickCheck' In the expression: quickCheck test In an equation for 'it': it = quickCheck test 

If you look at the Show instance for functions , it seems to me that such an instance does not exist.

How can I run quickCheck test , i.e. to bypass or refer to an absent instance of Show for Int -> Int ?

+7
haskell quickcheck
source share
1 answer

QuickCheck has a special Test.QuickCheck.Function module for generating "functions" that can be shown (as well as "compressed", as QuickCheck simplifies its counterexamples). You can convert them to regular functions with apply . For example, if you have a file:

 import Test.QuickCheck import Test.QuickCheck.Function test :: Fun Int Int -> Int -> Bool test _ _ = True test2 :: Fun Int Int -> Int -> Bool test2 fx = apply fx == x 

Then in GHCi:

 *Main> quickCheck test +++ OK, passed 100 tests. *Main> quickCheck test2 *** Failed! Falsifiable (after 2 tests and 3 shrinks): {_->0} 1 

In fact, you can define a Show instance for the functions themselves and use it. But if your input type is not the final type of the Bool type, you cannot print all the information about the function in this way. You can import a dummy instance that does not show useful information from Text.Show.Functions .

However, the type of Test.QuickCheck.Function.Fun used above looks like it was designed to give the basic information a lot more succinctly, so I would certainly use it myself if possible.

+8
source share

All Articles