Generating random arguments for a function

I am trying to use quickcheck to generate random arguments of a given function (assuming all its types have Arbitrary instance and Show instance) along with the evaluation of the function at those arguments. Suppose I have a function add :: Int -> Int -> Int add a b = a+b Then I assume a behavior like > randomEvaluate add (["1","3"],"4") where 1 and 3 are random values generated for `Int` and 4 is `f 1 3`. I have asked this on SOhttp://stackoverflow.com/questions/14294802/evaluating-function-at-random-ar... but am not fully satisfied with the answers. -Satvik

In general you can't do this whether you use pats of QuickCheck or not - `randomEvalute` would need to inspect the supplied function to see how many input parameters it has so it can list them, but there is no such introspection in Haskell.

* Stephen Tetley
In general you can't do this whether you use pats of QuickCheck or not - `randomEvalute` would need to inspect the supplied function to see how many input parameters it has so it can list them, but there is no such introspection in Haskell.
This can be done with relatively simple type class hackery. In fact, QuickCheck already does that in order to generate arguments and print them in case of failure. Roman

Yes - I was just checking the first QuickCheck paper to see how the
authors did this.
You would need a new type class that works like `Testable` and the
versions of associated machinery `forAll` and `evaluate` to unroll
function application.
On 13 January 2013 09:28, Roman Cheplyaka
This can be done with relatively simple type class hackery. In fact, QuickCheck already does that in order to generate arguments and print them in case of failure.
Roman

I think it's more complicated because he doesn't know what the return type or arity of the function is. In QuickCheck they know the return type of a property is Bool. In this case, we only know that the return type is an instance of Show. I don't think that's enough to simply implement this. On Sunday, January 13, 2013, Stephen Tetley wrote:
Yes - I was just checking the first QuickCheck paper to see how the authors did this.
You would need a new type class that works like `Testable` and the versions of associated machinery `forAll` and `evaluate` to unroll function application.
On 13 January 2013 09:28, Roman Cheplyaka
javascript:;> wrote: This can be done with relatively simple type class hackery. In fact, QuickCheck already does that in order to generate arguments and print them in case of failure.
Roman
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org javascript:; http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi, Am Sonntag, den 13.01.2013, 07:34 -0800 schrieb Bob Ippolito:
I think it's more complicated because he doesn't know what the return type or arity of the function is. In QuickCheck they know the return type of a property is Bool. In this case, we only know that the return type is an instance of Show. I don't think that's enough to simply implement this.
I posted on SE an answer that tries to do it with OverlappingInstances: http://stackoverflow.com/questions/14294802/evaluating-function-at-random-ar... Greetings, Joachim -- Joachim "nomeata" Breitner mail@joachim-breitner.de | nomeata@debian.org | GPG: 0x4743206C xmpp: nomeata@joachim-breitner.de | http://www.joachim-breitner.de/

I have a working code of this but for that I have to reimplement Arbitrary
and Testable typeclasses which I don't want to do. I thought it might be
possible to use parts of quickcheck without actually changing its code but
still I am unable to find a suitable solution.
-Satvik
On Sun, Jan 13, 2013 at 2:58 PM, Roman Cheplyaka
* Stephen Tetley
[2013-01-13 08:49:08+0000] In general you can't do this whether you use pats of QuickCheck or not - `randomEvalute` would need to inspect the supplied function to see how many input parameters it has so it can list them, but there is no such introspection in Haskell.
This can be done with relatively simple type class hackery. In fact, QuickCheck already does that in order to generate arguments and print them in case of failure.
Roman
participants (5)
-
Bob Ippolito
-
Joachim Breitner
-
Roman Cheplyaka
-
satvik chauhan
-
Stephen Tetley