For a given predicate pred : 'a list -> bool
and generator gen : Gen<'a>
, consider the following generator of well-formed lists that satisfy the predicate:
let wellFormedList pred gen =
Gen.ofList gen
|> Gen.filter pred
As mentioned in the FsCheck manual, there should be a high chance that predicate holds for a random list. Unfortunately, this assumption does not hold in my case. Thus, I need te define a custom generator for lists that satisfy the predicate.
How can I define a custom generator that starts from the empty list and extends it with new random elements, until the list satisfies the predicate?
I probably need to use the computation expression gen { }
for generators, but I do not see how.
PS: I am aware that, unlike the original implementation of wellFormedList
, the distribution of such a custom generator is not uniform over all lists that satisfy the predicate.