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

Roman Cheplyaka roma at ro-che.info
Sat Jul 13 12:16:47 CEST 2013


* martin <martin.drautzburg at web.de> [2013-07-13 10:10:39+0200]
> Am 07/12/2013 09:18 AM, schrieb Roman Cheplyaka:
> 
> > 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
> > 
> 
> This requires HashedList to be a new type, right? So far my code only
> used type synonyms.
> 
> Does this mean I have to convert type synonyms into types in order to
> use QuickCheck?

Probably. Technically, you could use OverlappingInstances to provide a
special case for your lists (because they have a spcific shape), but I'd
consider it a kludge in this case.

Or you can avoid defining an instance at all, as Aleksey has pointed
out.

> Does this mean I have to "plan for QuickCheck" when I design my types?

Kind of. It's not really specific to QuickCheck. Every time you want to
give some non-standard instances to your types, you should be using
newtypes or data-types.

More generally, you should be using newtypes or data types every time
when you introduce a non-trivial abstraction that isn't described by the
standard types. You case is a good example of that.

Roman



More information about the Haskell-Cafe mailing list