I'm trying to convert a project over to typed racket from racket and I'm encountering errors with working code because of the testing engine.
I've reduced it to the smallest piece of code I can create that reproduces the problem:
#lang typed/racket
; provides check-expect and others for testing
(require typed/test-engine/racket-tests)
(: bar (-> Positive-Integer Integer))
(check-random (bar 6) (random 6))
(define (bar x)
(random x))
(test)
And the errors are:
. Type Checker: type mismatch
expected: Pseudo-Random-Generator
given: Any in: (check-random (bar 6) (random 6))
. Type Checker: type mismatch
expected: Positive-Integer
given: Any in: (check-random (bar 6) (random 6))
. Type Checker: Summary: 3 errors encountered in:
(check-random (bar 6) (random 6))
(check-random (bar 6) (random 6))
(check-random (bar 6) (random 6))
Does anyone have any advice on how to fix this? I really want to be able to use the type checking features if possible.
Thanks
Sure, I can help, but it depends a lot on what exactly you want, and where you're going with this.
Short overview: there are multiple testing frameworks for Racket. You're using the one that's built for the teaching languages. It has several nice features, but generally speaking rackunit is the testing framework used by others.
It appears to me that the typed version of the teaching-language-test-framework doesn't include support for check-random. I'll check on this on the mailing list. That would steer you toward rackunit.
Unfortunately, rackunit doesn't include the "check-random" form. Fortunately, it's not hard to implement. Here's my implementation, attached to your code.
#lang typed/racket
; provides check-expect and others for testing
(require typed/rackunit)
;; create a new prng, set the seed to the given number, run the thunk.
(: run-with-seed (All (T) ((-> T) Positive-Integer -> T)))
(define (run-with-seed thunk seed)
(parameterize ([current-pseudo-random-generator
(make-pseudo-random-generator)])
(random-seed seed)
(thunk)))
;; run a check-equal where both sides get the same PRNG seed
(define-syntax check-random-equal?
(syntax-rules ()
[(_ a b) (let ([seed (add1 (random (sub1 (expt 2 31))))])
(check-equal? (run-with-seed (λ () a) seed)
(run-with-seed (λ () b) seed)))]))
(: bar (-> Positive-Integer Integer))
(define (bar x)
(random x))
(check-random-equal? (bar 6)
(random 6))
I should probably tell you about several important differences between the two testing frameworks.