purescript-deprecated/purescript-strongcheck

Why does applyGen return a new generator?

Closed this issue · 4 comments

I have some code that uses a StateT Test.QuickCheck.Gen.GenState m a, so that during an action, I can produce random values using lots of different types Gen; in an action, I might want to generate both Array Int and Seq Int, for example.

Currently I'm doing this:

stepGen :: forall m a. Gen a -> StateT GenState m a
stepGen gen = do
  st <- get
  let out = runGen gen st
  put out.state
  return out.value

I'm struggling to work out what the strongcheck equivalent is, other than using applyGen and ignoring the returned generator. Why does applyGen produce a new generator? Will bad things happen if I ignore the new generator and feed the new GenState into the same Gen later?

Also: after using runGen, how are you supposed to continue the process at a later time if you don't get a new GenState out?

You have to use the new generator, because it may maintain its own internal state information apart from GenState. runGen should only be used "at the end of the world", in generally you'd want to use applyGen, foldGen, transGen, depending on what you want to do.

I'm not sure why you are using StateT on GenState. Can't you just do something like:

do
  arrayInts <- arbitrary
  seqInts <- arbitrary

etc?

Doing something like that didn't occur to me (StateT GenState seems to work in QuickCheck). But yes, what you suggest will work. Thank you for explaining!

Sure, let me know if you run into any problems!