[prev in list] [next in list] [prev in thread] [next in thread] 

List:       haskell-cafe
Subject:    Re: [Haskell-cafe] Using MonadRandom with QuickCheck
From:       Magnus Grindal Bakken <magnusbakken () gmail ! com>
Date:       2016-08-27 10:59:39
Message-ID: 2e57eb62-678f-4027-95b5-3a9699efa408 () googlegroups ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Thanks, Li-yao! For anyone else reading this, this is the MonadRandom 
instance
I ended up with:

mkGen :: (QCGen -> a) -> Gen a
mkGen f = MkGen $ \g _ -> f g

instance MonadRandom Gen where
    getRandom = mkGen (fst . random)
    getRandoms = mkGen randoms
    getRandomR range = mkGen (fst . randomR range)
    getRandomRs range = mkGen (randomRs range)

With that said, I found that in many cases to get the game states I needed 
for
my property tests I had to use more specific generators that took input 
such as
a range of number of moves, whether the game was finished or not, the 
current
action, etc., so it didn't end up being as useful for me as I hoped.

>
On Sunday, August 21, 2016 at 10:37:35 PM UTC+2, Li-yao Xia wrote:
>
> Hi Magnus,
>
> Import Test.QuickCheck.Gen to get the internals of Gen, which AFAIK is 
> currently the only way to get to the underlying random generator (QCGen).
>
> getRandom = MkGen $ \g _ -> fst (random g)
> -- repeat with randomRs, randoms.
>
> I don't know why Gen is not an instance of MonadRandom either. It seems 
> reasonable to add one, though it might go against some design principle 
> of QuickCheck by ignoring the size parameter.
>
> Regards,
> Li-yao
>
> On 08/21/2016 09:38 PM, Magnus Grindal Bakken wrote:
> > Is there some easy way to use the Gen type from QuickCheck with the
> > MonadRandom class?
> >
> > Links to the packages in question:
> > https://hackage.haskell.org/package/MonadRandom
> > https://hackage.haskell.org/package/QuickCheck
> >
> > Some background in case there's an easier way to solve the problem:
> > I'm toying around with an API for a simple turn-based board game. The
> > main type in my API is called GameState, and I'm trying to define an
> > Arbitrary instance for it so I can set up invariants with QuickCheck.
> > The Arbitrary instance should produce game states that are the result
> > of playing a random number of random moves.
> >
> > At certain points in the gameplay the game itself needs to invoke some
> > randomness. I've designed the public game API to have basically just
> > two functions:
> >
> > playAction :: ActionData -> GameState -> GameState
> > evaluateGameState :: MonadRandom m => GameState -> m GameState
> >
> > The usage is supposed to be that the client first calls playAction
> > with an ActionData value that depends on the current move, and
> > afterwards calls evaluateGameState to evaluate whatever random events
> > need to happen at that point (e.g. reshuffling a deck of cards). That
> > way my main "action" logic doesn't need to concern itself with any
> > form of randomness.
> >
> > I'm using MonadRandom from Control.Monad.Random because it seems like
> > a much nicer way to generate random numbers than using RandomGen
> > directly. However I can't figure out how to make it play nice with
> > QuickCheck. I can easily set up my Arbitrary instance to call
> > playAction an arbitrary number of times, but I can't call
> > evaluateGameState since it lives in the other monad. It would work if
> > Gen were an instance of MonadRandom, but I can't quite work out how to
> > write that instance since most functions on Gen only work if the
> > output type is an instance of Arbitrary, while MonadRandom needs to
> > work with any types that are part of the Random class. This part
> > works:
> >
> > instance MonadRandom Gen where
> >     getRandomR = choose
> >
> > But I don't know how to define the other functions on MonadRandom for 
> Gen.
> >
> > I think I might also be able to use the PropertyM monad transformer:
> > 
> https://hackage.haskell.org/package/QuickCheck-2.9.1/docs/Test-QuickCheck-Monadic.html
> ,
> > but monad transformers always confuse me so I haven't been able to
> > figure out how to do that yet either.
> >
> > I've thought about rewriting the API to use the Gen type instead of
> > MonadRandom, but it feels kind of iffy to have the API depend on a
> > testing library.
> >
> > Has anyone tried to use these libraries together before?
> > _______________________________________________
> > Haskell-Cafe mailing list
> > To (un)subscribe, modify options or view archives go to:
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> > Only members subscribed via the mailman list are allowed to post.
> >
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
>
>
[Attachment #5 (text/html)]

<div dir="ltr"><div>Thanks, Li-yao! For anyone else reading this, this is the \
MonadRandom instance</div><div>I ended up with:</div><div><br></div><div><div>mkGen \
:: (QCGen -&gt; a) -&gt; Gen a</div><div>mkGen f = MkGen $ \g _ -&gt; f \
g</div><div><br></div><div>instance MonadRandom Gen where</div><div>      getRandom = \
mkGen (fst . random)</div><div>      getRandoms = mkGen randoms</div><div>      \
getRandomR range = mkGen (fst . randomR range)</div><div>      getRandomRs range = \
mkGen (randomRs range)</div></div><div><br></div><div>With that said, I found that in \
many cases to get the game states I needed for</div><div>my property tests I had to \
use more specific generators that took input such as</div><div>a range of number of \
moves, whether the game was finished or not, the current</div><div>action, etc., so \
it didn&#39;t end up being as useful for me as I hoped.</div><blockquote \
class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc \
solid;padding-left: 1ex;"><p></p><p></p><p></p><p></p></blockquote><br>On Sunday, \
August 21, 2016 at 10:37:35 PM UTC+2, Li-yao Xia wrote:<blockquote \
class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc \
solid;padding-left: 1ex;">Hi Magnus,<p>Import Test.QuickCheck.Gen to get the \
internals of Gen, which AFAIK is <br>currently the only way to get to the underlying \
random generator (QCGen).</p><p>getRandom = MkGen $ \g _ -&gt; fst (random g)<br>-- \
repeat with randomRs, randoms.</p><p>I don&#39;t know why Gen is not an instance of \
MonadRandom either. It seems <br>reasonable to add one, though it might go against \
some design principle <br>of QuickCheck by ignoring the size \
parameter.</p><p>Regards,<br>Li-yao</p><p>On 08/21/2016 09:38 PM, Magnus Grindal \
Bakken wrote:<br>&gt; Is there some easy way to use the Gen type from QuickCheck with \
the<br>&gt; MonadRandom class?<br>&gt;<br>&gt; Links to the packages in \
question:<br>&gt; <a href="https://hackage.haskell.org/package/MonadRandom" \
target="_blank" rel="nofollow" \
onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fhackage.hask \
ell.org%2Fpackage%2FMonadRandom\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHyvIViQfrpGYuZCFSXIVDwuZn5Zw&#39;;return \
true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fhackage.h \
askell.org%2Fpackage%2FMonadRandom\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHyvIViQfrpGYuZCFSXIVDwuZn5Zw&#39;;return \
true;">https://hackage.haskell.org/<wbr>package/MonadRandom</a><br>&gt; <a \
href="https://hackage.haskell.org/package/QuickCheck" target="_blank" rel="nofollow" \
onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fhackage.hask \
ell.org%2Fpackage%2FQuickCheck\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEAEpDozS2EcQqVxYMWHkPRT3sJoQ&#39;;return \
true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fhackage.h \
askell.org%2Fpackage%2FQuickCheck\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEAEpDozS2EcQqVxYMWHkPRT3sJoQ&#39;;return \
true;">https://hackage.haskell.org/<wbr>package/QuickCheck</a><br>&gt;<br>&gt; Some \
background in case there&#39;s an easier way to solve the problem:<br>&gt; I&#39;m \
toying around with an API for a simple turn-based board game. The<br>&gt; main type \
in my API is called GameState, and I&#39;m trying to define an<br>&gt; Arbitrary \
instance for it so I can set up invariants with QuickCheck.<br>&gt; The Arbitrary \
instance should produce game states that are the result<br>&gt; of playing a random \
number of random moves.<br>&gt;<br>&gt; At certain points in the gameplay the game \
itself needs to invoke some<br>&gt; randomness. I&#39;ve designed the public game API \
to have basically just<br>&gt; two functions:<br>&gt;<br>&gt; playAction :: \
ActionData -&gt; GameState -&gt; GameState<br>&gt; evaluateGameState :: MonadRandom m \
=&gt; GameState -&gt; m GameState<br>&gt;<br>&gt; The usage is supposed to be that \
the client first calls playAction<br>&gt; with an ActionData value that depends on \
the current move, and<br>&gt; afterwards calls evaluateGameState to evaluate whatever \
random events<br>&gt; need to happen at that point (e.g. reshuffling a deck of \
cards). That<br>&gt; way my main &quot;action&quot; logic doesn&#39;t need to concern \
itself with any<br>&gt; form of randomness.<br>&gt;<br>&gt; I&#39;m using MonadRandom \
from Control.Monad.Random because it seems like<br>&gt; a much nicer way to generate \
random numbers than using RandomGen<br>&gt; directly. However I can&#39;t figure out \
how to make it play nice with<br>&gt; QuickCheck. I can easily set up my Arbitrary \
instance to call<br>&gt; playAction an arbitrary number of times, but I can&#39;t \
call<br>&gt; evaluateGameState since it lives in the other monad. It would work \
if<br>&gt; Gen were an instance of MonadRandom, but I can&#39;t quite work out how \
to<br>&gt; write that instance since most functions on Gen only work if the<br>&gt; \
output type is an instance of Arbitrary, while MonadRandom needs to<br>&gt; work with \
any types that are part of the Random class. This part<br>&gt; works:<br>&gt;<br>&gt; \
instance MonadRandom Gen where<br>&gt;       getRandomR = choose<br>&gt;<br>&gt; But \
I don&#39;t know how to define the other functions on MonadRandom for \
Gen.<br>&gt;<br>&gt; I think I might also be able to use the PropertyM monad \
transformer:<br>&gt; <a \
href="https://hackage.haskell.org/package/QuickCheck-2.9.1/docs/Test-QuickCheck-Monadic.html" \
target="_blank" rel="nofollow" \
onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fhackage.hask \
ell.org%2Fpackage%2FQuickCheck-2.9.1%2Fdocs%2FTest-QuickCheck-Monadic.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHRpLKpW3eYtDscb59kQimPuJ74Jw&#39;;return \
true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fhackage.h \
askell.org%2Fpackage%2FQuickCheck-2.9.1%2Fdocs%2FTest-QuickCheck-Monadic.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHRpLKpW3eYtDscb59kQimPuJ74Jw&#39;;return \
true;">https://hackage.haskell.org/<wbr>package/QuickCheck-2.9.1/docs/<wbr>Test-QuickCheck-Monadic.html</a>,<br>&gt; \
but monad transformers always confuse me so I haven&#39;t been able to<br>&gt; figure \
out how to do that yet either.<br>&gt;<br>&gt; I&#39;ve thought about rewriting the \
API to use the Gen type instead of<br>&gt; MonadRandom, but it feels kind of iffy to \
have the API depend on a<br>&gt; testing library.<br>&gt;<br>&gt; Has anyone tried to \
use these libraries together before?<br>&gt; \
______________________________<wbr>_________________<br>&gt; Haskell-Cafe mailing \
list<br>&gt; To (un)subscribe, modify options or view archives go to:<br>&gt; <a \
href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" target="_blank" \
rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2 \
Fmail.haskell.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fhaskell-cafe\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH7sFgl7KfuDcDlaGGG3ip3kRaoIA&#39;;return \
true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fmail.haskel \
l.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fhaskell-cafe\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH7sFgl7KfuDcDlaGGG3ip3kRaoIA&#39;;return \
true;">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>&gt; \
Only members subscribed via the mailman list are allowed to \
post.<br>&gt;<br>______________________________<wbr>_________________<br>Haskell-Cafe \
mailing list<br>To (un)subscribe, modify options or view archives go to:<br><a \
href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" target="_blank" \
rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2 \
Fmail.haskell.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fhaskell-cafe\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH7sFgl7KfuDcDlaGGG3ip3kRaoIA&#39;;return \
true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fmail.haskel \
l.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fhaskell-cafe\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH7sFgl7KfuDcDlaGGG3ip3kRaoIA&#39;;return \
true;">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>Only \
members subscribed via the mailman list are allowed to \
post.</p><p></p><p></p><p></p><p></p></blockquote></div>


[Attachment #6 (text/plain)]

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic