-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use splittable RNG in QCheck2 #318
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…eck2 variant test wrt. {ap,bind} splitting
…ens from the list front
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #317
This PR fixes the weird shrinking behaviour in QCheck2 as seen in #317. With it generators built with
Gen.bind
Gen.bytes_size
(and by extension allGen.bytes*
andGen.string*
generators)Gen.list_size
(and by extensionGen.list*
,Gen.array*
, and function generators)start exhibiting deterministic shrinking. The problem is that in generators such as
Gen.(x >>= f)
the underlying
Random.State
has state, but is computed lazily. As such,split
the RNG, so that any delayed generation inf
is unaffected by, e.g., fewer RNG invocations in theRandom.State
passed tox
andcopy
the RNG, so thatf
when restarted (e.g., with a smaller size input) restarts from the sameRandom.State
.This is not new, e.g., in #156 (comment) I commented:
The integrated shrinking requires a splittable RNG - and OCaml 4's
Random.State
module does not offer one.As such, on OCaml 4, the PR builds on a horrible
split
hack I added some years back. I cannot see us switching to, e.g., https://github.com/ocaml/stdlib-random, asRandom.State
is exposed in QCheck's interfaces, so a switch would be backward incompatible. In addition, theRandom.State.make
used to hack around the absence ofsplit
is costly, meaningQCheck2
regrettably performs worse on OCaml4 than on OCaml5+.WTF
)As such, should anyone be reading this PR, it is recommended to do so, commit-by-commit.
Finally: The PR does not solve all QCheck2's shrinking problems such as #157 and #163.
Getting QCheck2's shrinker trees to act more deterministically and less like bogosort is however a prerequisite and a step towards improving its shrinker algorithms (behaviour + complexity).