[Haskell-cafe] Generating arbitrary functions with QuickCheck?
tom.davie at gmail.com
Wed Sep 15 11:58:53 EDT 2010
On 15 Sep 2010, at 16:29, Matias Eyzaguirre wrote:
> I'v been reading a small paper/lesson on writing parser combinators in Haskell, and it seems more or less straightforward. In this case a parser is defined thusly:
>> type Parser a = String -> Maybe (a, String)
> And then it goes on to list some simple parsers, and then starts going on about combinators.
> I was wondering how one would write quickcheck properties for the items presented in the paper, and the answer seemed fairly straightforward for the actual parsers. But how on earth would you write a test for a combinator? Presumable one would need to make the type an instance of Arbitrary.
> I see two problems:
> Firstly, as far as i can tell, one cannot declare a type synonym to be an instance of a type class, thus how would you make it an instance of Arbitrary?
The standard solution here is to create a newtype, and generate them instead.
> Secondly, (and more importantly, or at least more interesting) I can see how one would make a generator for simple compound data types, but how on earth do you make a generator produce functions?
With some difficulty, but it can be done. You could for example, select at random, a combinator or axiomatic parser, on selecting a combinator, generate a pair of parsers to hand to it with a slightly lower chance of generating a combinator and stick them together.
More information about the Haskell-Cafe