[QuickCheck] Bug with Small and Positive types

Nick Smallbone nicsma at chalmers.se
Mon May 5 14:17:34 BST 2014


So, I've just added explicit Num and Enum instances for the affected
types, which fixes this problem. I need to think about how the Num
instance should behave, though - for now, fromInteger accepts
out-of-bounds arguments (but silently changes them into acceptable
values) but the other operations crash on a bound error.

An alternative is maybe to remove the Num instances for Positive and
family, since they're not very natural - that way, Small (Positive
Int) will give a type error and only Positive (Small Int) will work.

Nick

On Monday 05 May, 2014 at 11:57 am, Nick Smallbone wrote:
> 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