Skip to content
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

Documentation: clarify role of first argument to trans function in the arbitrary struct #28

Open
97jaz opened this issue Nov 15, 2016 · 2 comments

Comments

@97jaz
Copy link

97jaz commented Nov 15, 2016

The documentation for the arbitrary struct says:

Represents a source of randomly generated values, except where the values are filtered by the function trans to produce a narrower set of new values.

And trans has the contract:

(-> any/c generator? generator?)

But nothing indicates how the first argument to that function is used to narrow the set.

@97jaz
Copy link
Author

97jaz commented Nov 15, 2016

From reading the source and the original paper on Quickcheck, I'm getting a better sense of what's going on, but it makes me think that variant should have been provided.

@97jaz
Copy link
Author

97jaz commented Nov 15, 2016

To give some background, I'm trying to produce arbitrary dates. I got this to work -- I hope correctly -- by doing this:

(require rackunit
         quickcheck
         (only-in quickcheck/generator
                  variant
                  lift->generator)
         datetime/date)


(define (choose-date lower upper)
  (define lo (date->jdn lower))
  (define hi (date->jdn upper))

  (lift->generator
   jdn->date
   (choose-integer lo hi)))

(define arbitrary-date
  (arbitrary
   (sized
    (λ (n)
      (choose-date (jdn->date (- n))
                   (jdn->date n))))
   (λ (n gen)
     (variant (if (>= n 0)
                  (* 2 n)
                  (+ (* 2 (- n)) 1))
              gen))))

This solution has a couple of deficiencies: it requires two undocumented procedures from quickcheck, and it reproduces the variant-choosing logic from the implementation of arbitrary-integer (which, I think, is to ensure that the first argument to variant is nonnegative).

Have I overlooked a better way of accomplishing this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant