Reputation: 571
I have this working code in the random testing part of a Kata:
it "handles randoms " $
property $ \x y -> updateHealth x y == if y > x then 0 else x-y
But I wanted the function signature to use Num, but when I did that I get an error because (I believe) it doesn't know how to generate Nums (as it doesn't know what kind of Nums to make?). I just hard set the type to Double as a work around.
However is there a way to use Quickcheck when your function signature uses Nums? Do you have to tell it specifically to generate random Int, or Integer, Float or Double?
Upvotes: 1
Views: 158
Reputation: 1709
However is there a way to use Quickcheck when your function signature uses Nums? Do you have to tell it specifically to generate random Int, or Integer, Float or Double?
As @AJFarmar states, Num
is a type class (family of types), not a specific type.
You CAN use QuickCheck with a function like Num a => a -> a -> a
, you just have to specialize the type through annotation. You can either annotate the updateHealth
function or else annotate the property function itself:
it "handles randoms " $
property $ \x y -> (updateHealth :: Int -> Int -> Int) x y == if y > x then 0 else x-y
(or)
it "handles randoms " $
property ((\x y -> updateHealth x y == if y > x then 0 else x-y) :: Int -> Int -> Bool)
These options are preferable to changing updateHealth
's type at its definition; you want your functions to stay general, so you can specialize them at their call sites if necessary.
Upvotes: 1