How to write a function to fire quickCheck prop_xxx?
I am using QuickCheck v1. Here is a simple prop_xxx defined as below:
prop_foo :: (Num a) =>[a] -> Bool
prop_foo xs = (reverse.reverse) xs == id xs
This can be tested in GHCi correctly: quickCheck prop_foo开发者_如何转开发
However, when I tried to wrap the call in a function like:
f :: IO ()
f = quickCheck prop_foo
It reported the error:
Ambiguous type variable `a' in the constraints:
`Num a' arising from a use of `prop_foo' at Foo.hs:147:15-22
`Arbitrary a'
arising from a use of `quickCheck' at Foo.hs:147:4-22
Probable fix: add a type signature that fixes these type variable(s)
Shall I provide something like
instance Arbitrary Xxx where
arbitrary = ...
coarbitrary c = ...
Thanks a lot.
-- Larry
You have to give it a monomorphic type signature, like
prop_foo :: [Int] -> Bool
After all, the question is: in your original version, which type a
should quickCheck
choose to test the function with? a = Int
? a = Double
? Something else? The error message complains that a
is ambiguous, i.e. there is no unique choice for it.
精彩评论