Haskell: QuickCheck property does not allow tests using consequences

I have the following property that I want to check with quickcheck:

prop_zip xs ys = length xs == length ys ==> unzip (zip xs ys) == (xs,ys) 

Despite the fact that according to the definition of zip and unzip it is logically more correct that this property must be valid fo lists of the same length, checkcheck ends with:

 *** Gave up! Passed only 49 tests. 

Thanks in advance for any hint or advice!

+6
source share
1 answer

Prerequisites that are hard to come by creating random test cases are often a bad idea in QuickCheck. Instead, you should use generators to create test cases that automatically satisfy the precondition.

For example, in this case, you can use forAll to generate a second list with the same length as the first list:

 prop_zip' (xs :: [a]) = forAll (vectorOf (length xs) arbitrary) $ \ (ys :: [a]) -> unzip (zip xs ys) == (xs, ys) 

(I also use ScopedTypeVariables here to determine the type of the second list. You might want to change this to suit your specific needs.)

+14
source

All Articles