Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Test.QuickCheck.Batch use a default type for testing list functions

I am testing a function called extractions that operates over any list.

extractions :: [a] -> [(a,[a])]
extractions [] = []
extractions l = extract l []
    where extract [] _ = []
          extract (x:xs) prev = (x, prev++xs) : extract xs (x : prev)

I want to test it, for example, with

import Test.QuickCheck.Batch    
prop_len l = length l == length (extractions l)
main = runTests "extractions" defOpt [run prop_len]

But this won't compile; I have to supply a type either for run or prop_len, because QuickCheck can't generate [a], it has to generate something concrete. So I chose Int:

main = runTests "extractions" defOpt [r prop_len]
    where r = run :: ([Int] -> Bool) -> TestOptions -> IO TestResult

Is there any way to get QuickCheck to choose a for me instead of having it specified in the type of run?

like image 843
Nathan Shively-Sanders Avatar asked Sep 15 '08 15:09

Nathan Shively-Sanders


People also ask

How do you test a function in Haskell?

If you want to easily test your functions in Haskell, you can do so by writing inputs and their expected responses right above your functions as comments. All you need is doctest.

What is QuickCheck in Haskell?

QuickCheck is a software library, specifically a combinator library, originally written in the programming language Haskell, designed to assist in software testing by generating test cases for test suites – an approach known as property testing.

Is Haskell QuickCheck static?

Haskell is a statically-typed functional programming language: Haskell type declarations are identified and checked during compilation, guaranteeing that program semantics are known by the designers before execution or possibly even before the implementation.


1 Answers

The quickcheck manual says "no":

Properties must have monomorphic types. `Polymorphic' properties, such as the one above, must be restricted to a particular type to be used for testing. It is convenient to do so by stating the types of one or more arguments in a

where types = (x1 :: t1, x2 :: t2, ...)

clause...

like image 162
ordnungswidrig Avatar answered Sep 28 '22 13:09

ordnungswidrig