[Haskell-cafe] Generating arbitrary functions with QuickCheck?
Jonas Almström Duregård
jonas.duregard at chalmers.se
Thu Sep 16 09:27:57 EDT 2010
The type (String -> Maybe (a, String)) should already be an instance of
arbitrary if a is.
One way of generating functions is to have some sort of hashing function (a
-> Int). Functions from a to b can be generating by using the hash value to
transform the random seed before the b is generated. Here is a rough outline
for such a system:
genB :: StdGen -> B
hashA :: A -> Int
transformGen :: Int -> StdGen -> StdGen
genFun :: StdGen -> (A -> B)
genFun g a = genB $ transformGen (hashA a) g
In QC you have this instance:
(CoArbitrary<http://hackage.haskell.org/packages/archive/QuickCheck/2.1.0.1/doc/html/Test-QuickCheck-Arbitrary.html#t:CoArbitrary>
a, Arbitrary<http://hackage.haskell.org/packages/archive/QuickCheck/2.1.0.1/doc/html/Test-QuickCheck-Arbitrary.html#t:Arbitrary>
b)
=> Arbitrary<http://hackage.haskell.org/packages/archive/QuickCheck/2.1.0.1/doc/html/Test-QuickCheck-Arbitrary.html#t:Arbitrary>
(a
-> b)
And CoArbitrary is the class where you implement the trick described above.
Look at the function called variant as well.
/J
On 16 September 2010 02:59, Ivan Lazar Miljenovic <ivan.miljenovic at gmail.com
> wrote:
> On 16 September 2010 01:58, Thomas Davie <tom.davie at gmail.com> wrote:
> >> 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.
>
> I've also written and used dedicated generation functions (especially
> for Strings, since I don't want _really_ arbitrary strings since some
> of them might accidentally bring up formatting that my parser can't
> cope with (or will parse as something else). Note that this is more
> for generating sub-parts of other data types, rather than for types I
> want to be able to run QC tests on.
>
> --
> Ivan Lazar Miljenovic
> Ivan.Miljenovic at gmail.com
> IvanMiljenovic.wordpress.com
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100916/e1fbe986/attachment.html
More information about the Haskell-Cafe
mailing list