Search code examples
haskellquickcheck

Rewriting Arbitrary Double in QuickCheck2


Sorry for a newbie question, but how can I redefine Arbitrary Double to produce +/- infinity and NaNs as well as usual doubles? And how to use my version of this Arbitrary?


Solution

  • You don't need to make a new Arbitrary instance just to make a custom Generator for a type. You can just create it as a free-standing definition:

    evilDouble :: Gen Double
    evilDouble = oneOf [ weirdDouble, arbitrary ]
      where
        weirdDouble = error "This is where you generate inf and NaN values"
    

    and then use it explicitly with QuickCheck's forAll:

    prop_foo = forAll evilDouble $ \x -> abs (foo x - 123) < 0.1