[Haskell-cafe] Re: Positive integers

Nils Anders Danielsson nad at cs.chalmers.se
Fri Mar 24 12:36:55 EST 2006

On Fri, 24 Mar 2006, Henning Thielemann <lemming at henning-thielemann.de> wrote:

>  Further on think of QuickCheck: A Cardinal type with an Arbitrary
> instance would save us the (>=0) condition and it would reduce the
> number of tests that must be skipped because of non-fulfilled
> conditions. Because I was confronted with adding positivity checks to
> QuickCheck properties quite frequently, I finally wrote wrappers
>   newtype Cardinal = Cardinal Integer deriving (Show, Read, Eq, Ord, Ix)
>   newtype Card     = Card     Int     deriving (Show, Read, Eq, Ord, Ix)
>    in order to simplify such checks.

I wouldn't mind having a natural number type, but the technique above
is useful anyway. When using QuickCheck you often need custom
generators, and the technique removes the need for non-dependent

  prop_foo (Cardinal n) (Balanced t) (Prime p) (Small s) =
    ... n ... t ... p ... s ...

The property above is considerably easier to read than the following

  prop_foo =
    forAll cardinal $ \c ->
    forAll balanced $ \t ->
    forAll prime $ \p ->
    forAll small $ \s ->
      ... n ... t ... p ... s ...

It would be nice to have a bunch of newtypes like these in a library.


More information about the Haskell-Cafe mailing list