[Haskell-cafe] Ensuring Type Class instances follow the 'rules'

wren ng thornton wren at freegeek.org
Fri May 29 03:14:32 EDT 2009

Eugene Kirpichov wrote:
> Use QuickCheck.

Also use SmallCheck or lazy SmallCheck.

All of these are automatic tools that allow you to specify laws[1] and 
automatically generate values for testing the law. QuickCheck generates 
values randomly, which can be useful but is very often insufficient. 
Both versions of SmallCheck do exhaustive search of all values down to a 
bounded "depth" (lazy SmallCheck can often search deeper and faster).

If you want to have decent assurances of correctness then use (lazy) 
SmallCheck. If you're dealing with a really branchy type which makes it 
prohibitive to search to any reasonable depth, then use QuickCheck in 
addition. Occasionally QuickCheck can be good for ferreting out really 
obscure corner cases, but SmallCheck is much better about guaranteeing 
you get all the basic cases.

[1] e.g.

prop_compare a b =
     compare a b == negateOrdering (compare b a)
     negateOrdering LT = GT
     negateOrdering EQ = EQ
     negateOrdering GT = LT

Live well,

More information about the Haskell-Cafe mailing list