[Haskell-cafe] Quick-check: how to generate arbitrary complex data?

Roman Cheplyaka roma at ro-che.info
Fri Jul 12 09:18:35 CEST 2013


* martin <martin.drautzburg at web.de> [2013-07-12 08:33:54+0200]
> Hello all,
> 
> I have a type ("Mail") which consists of hash and a list, where the hash
> keeps some redundant data of the list for faster access. I can add and
> remove elements to values of this type using custom functions, called
> push and pop.
> 
> Now I wanted to write some quick checks, but I have no clue how to
> generate arbitrary values of this type. It will certainly no suffice to
> write arbitrary instances for the underlying types (Int and Char),
> because the hash and the list need to be synchronized.
> 
> Currently "Mail" it is only a type synonym. I suppose as a prerequisite
> I need to wrap it into a type constructor. But then what?

QuickCheck's Gen is a functor. So you can generate a list, and then
use fmap to add a hash to it.

  instance Arbitrary HashedList where
    arbitrary = addHashToList <$> arbitrary

Roman



More information about the Haskell-Cafe mailing list