How to use modifiers with Quickcheck (positive in my case)

I have a rev function that returns some value for a type that is in three types:

 rev :: (Integral a, Show a, Read a) => a -> a rev = read . reverse . show 

I would like to check some properties about this with quickcheck. Although, I am not interested in testing negative values โ€‹โ€‹of Integral types because I use Integer due to the lack of the Natural type in the base library. So I thought: let's say the opposite of the value generated when the generated value is negative, and I'll be fine:

 prop_id :: (Integral a, Show a, Read a) => Positive a -> Bool prop_id n | n >= 0 = (rev.rev) n == n | otherwise = let n' = -n in (rev.rev) n' == n' 

(the tested property is not important here - in particular, it does not work for very simple values, and I know about it, this is not the topic of this question)

Then I came across the Positive modifier and thought that although my test is now working, it would be nice to implement it better. So I tried:

 prop_id :: (Integral a, Show a, Read a) => Positive a -> Bool prop_id n = (rev.rev) n == n 

I must admit that I was surprised when it compiled. But then when the test starts, a message is displayed:

 *** Failed! Exception: 'Prelude.read: no parse' (after 1 test): Positive {getPositive = 1} 

So, I thought: "mmk, I must declare this Positive thing an instance of Read ." So I did just that, but the instance has already been declared in the quickCheck library because ghci yelled at me.

And at that moment I was lost because I did not find good documentation (if any).

Any pointer that helps me understand modifiers and other nice things in the quickcheck library will be appreciated.

+7
source share
1 answer

A common way to use these modifiers is to match patterns with them, for example.

 prop_id :: (Integral a, Show a, Read a) => Positive a -> Bool prop_id (Positive n) = (rev.rev) n == n 

So n will have a base type.

+17
source

All Articles