satvik chauhan | 12 Jan 20:33 2013
Picon

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 SO but am not fully satisfied with the answers.

-Satvik

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Stephen Tetley | 13 Jan 09:49 2013
Picon

Re: Generating random arguments for a function

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.
Roman Cheplyaka | 13 Jan 10:28 2013

Re: Generating random arguments for a function

* Stephen Tetley <stephen.tetley <at> gmail.com> [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
Stephen Tetley | 13 Jan 10:45 2013
Picon

Re: Generating random arguments for a function

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 <roma <at> ro-che.info> 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
Bob Ippolito | 13 Jan 16:34 2013

Re: Generating random arguments for a function

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 <roma <at> ro-che.info> 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 <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Joachim Breitner | 13 Jan 17:14 2013
Picon

Re: Generating random arguments for a function

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-arguments-using-quickcheck/14295179#14295179

Greetings,
Joachim

--

-- 
Joachim "nomeata" Breitner
  mail <at> joachim-breitner.de  |  nomeata <at> debian.org  |  GPG: 0x4743206C
  xmpp: nomeata <at> joachim-breitner.de | http://www.joachim-breitner.de/

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
satvik chauhan | 13 Jan 10:47 2013
Picon

Re: Generating random arguments for a function

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 <roma <at> ro-che.info> wrote:
* Stephen Tetley <stephen.tetley <at> gmail.com> [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




_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane