When I want to test pure code using QuickCheck
I often have to write an Arbitrary
instance. To test monadic code, I can use Test.QuickCheck.Monadic
as described in this article.
My question is: Is there some canonical way to generate data that depends on a monadic context? In particular, is there some monadic version of Arbitrary? How would you go about generating data that requires a monadic context?
Gen
is basically what you use to work with Arbitrary
instances in a monad framework, since the Arbitrary
type class is basically just a provider of Gen a
generators.
Just generate an appropriate random monadic context. If you are running tests, you can't stay in pure imaginary monad land, you actually have to pick a particular monad to test out. Did you pick a list monad? Randomly generate a list as the context. A state monad? Randomly generate a value of the state type for the context. Writer? Randomly generate a log.
So what do you want to generate, exactly? If it is a monadic action, then you need to provide a way of generating monadic actions for your particular monad, based on its unique primitives. e.g. randomly generated State actions should be constructed randomly from State primitives get
and put
.
If you use QuickCheck's capabilities to generate a function f
that produces a monadic action, and use QuickCheck to generate a context c
, then you can simply c >>= f
to put them together. Or you can simply generate the action a
directly, and do c >> a
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With