[QuickCheck] Obtaining value of QC counter example

Koen Claessen koen at chalmers.se
Mon Oct 15 09:51:40 BST 2012


Hi Jurien,

Since it is impossible to know on the outside of the property what the
type is of the values you are interested in, this is not very easy.

I usually use the following trick (a bit ugly, but it works).

Suppose you have a property:

  prop_Monkey =
    forAll ....
      ...
        x == y

And you want to get the value of x and y. x and y can be quantified
variables, or other values that live inside the property.

If you want to print them, you can say:

  prop_Monkey =
    forAll ....
      ...
        whenFail (print (x,y)) $
          x == y

If you want to get them and use them as Haskell values, you can say:

  prop_Monkey ref =
    forAll ....
      ...
        whenFail (writeIORef ref (Just (x,y))) $
          x == y

Now, before you QuickCheck the property, you create an IORef like so:

  ref <- newIORef Nothing
  quickCheck (prop_Monkey ref)

Afterwards you can look in the ref to see if there is a counter example.

A bit ugly, but since the type of the internal values is not known,
the only way I know how to do it.

Let me know if this works for you.

/Koen

On Sat, Oct 13, 2012 at 11:36 AM, Jurriën Stutterheim
<j.stutterheim at me.com> wrote:
> Dear QuickCheck developers,
>
>
> Is it possible to obtain the value of the shrunk counter example that QuickCheck produces, so that I may use it in the rest of my program? If so, how? :)
> I could imagine that it's possible (at least for non-function values) to return a single counter example value for functions with a single argument, and a tuple of values for functions with more than one argument.
>
> Cheers,
>
>
> Jurriën
> _______________________________________________
> QuickCheck mailing list
> QuickCheck at projects.haskell.org
> http://projects.haskell.org/cgi-bin/mailman/listinfo/quickcheck



More information about the QuickCheck mailing list