[QuickCheck] Bug with Small and Positive types

Nick Smallbone nicsma at chalmers.se
Mon May 5 10:57:40 BST 2014


Eek, yes, this is obviously a bug. As Anders says, the problem is that
Small ignores the inner type's Arbitrary instance and just uses the
QuickCheck primitive 'choose' to generate a value - so the Positive
modifier ends up having no effect.

Small is supposed to detect when you use it on an unsigned type and
avoid generating negative values. It works by first generating a
random Integer, converting it to the target type and converting it
back - if it gets a different Integer back, something must have
overflowed or underflowed. The trouble is that Positive's Num instance
just uses generalised newtype deriving, so (-1) :: Positive Int gives
you Positive (-1), and there is no way for Small to know that that's
not allowed.

So I suppose the fix is to have handwritten Num instances for Positive
and its friends where fromInteger refuses to construct numbers that
are not allowed. I will fix this when I have a bit of spare time.

Nick

On Saturday 03 May, 2014 at 08:24 pm, Bryan O'Sullivan wrote:
> Not exactly expected behaviour: I get values that are not positive.
> 
> >>> sample (arbitrary :: Gen (Small (Positive Int)))
> Small {getSmall = Positive {getPositive = 0}}
> Small {getSmall = Positive {getPositive = -1}}
> Small {getSmall = Positive {getPositive = 2}}

> _______________________________________________
> QuickCheck mailing list
> QuickCheck at projects.haskell.org
> http://projects.haskell.org/cgi-bin/mailman/listinfo/quickcheck




More information about the QuickCheck mailing list