Skip to content

Commit 39195bd

Browse files
committed
add generator for (and/c real? (not/c negative?)) and (and/c rational? (not/c negative?))
and fix -> generator for mandatory keyword arguments
1 parent 67f215e commit 39195bd

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

pkgs/racket-pkgs/racket-test/tests/racket/contract-rand-test.rkt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
(check-not-exn (λ () (test-contract-generation (</c 0.0))))
3737
(check-not-exn (λ () (test-contract-generation (=/c 0))))
3838
(check-not-exn (λ () (test-contract-generation (=/c 0.0))))
39+
(check-not-exn (λ () (test-contract-generation (and/c real? (not/c negative?)))))
40+
(check-not-exn (λ () (test-contract-generation (and/c rational? (not/c negative?)))))
3941
(check-not-exn (λ () (test-contract-generation (or/c boolean? boolean?))))
4042
(check-not-exn (λ () (test-contract-generation (cons/c integer? boolean?))))
4143
(check-not-exn (λ () (test-contract-generation any/c)))
@@ -167,6 +169,14 @@
167169
(contract-exercise #:fuel N . exps)
168170
(void))))]))
169171

172+
(check-exercise
173+
10
174+
pos-exn-or-silence?
175+
(contract (-> #:b boolean? any/c)
176+
(λ (#:b b) b)
177+
'pos
178+
'neg))
179+
170180
(check-exercise
171181
1
172182
pos-exn?

racket/collects/racket/contract/private/arrow-val-first.rkt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -754,24 +754,36 @@
754754
[else (λ (fuel) #f)]))
755755

756756
(define (->-exercise ctc)
757-
(define dom-ctcs (base->-doms ctc))
758757
(define rng-ctcs (base->-rngs ctc))
758+
(define dom-ctcs (for/list ([doms (in-list (base->-doms ctc))]
759+
[i (in-range (base->-min-arity ctc))])
760+
doms))
761+
(define dom-kwd-infos (for/list ([dom-kwd (in-list (base->-kwd-infos ctc))]
762+
#:when (kwd-info-mandatory? dom-kwd))
763+
dom-kwd))
764+
(define dom-kwds (map kwd-info-kwd dom-kwd-infos))
759765
(cond
760-
[(and (equal? (length dom-ctcs) (base->-min-arity ctc))
761-
(not (base->-rest ctc)))
766+
[(not (base->-rest ctc))
762767
(λ (fuel)
763768
(define gens
764769
(for/list ([dom-ctc (in-list dom-ctcs)])
765770
(generate/choose dom-ctc fuel)))
771+
(define kwd-gens
772+
(for/list ([kwd-info (in-list dom-kwd-infos)])
773+
(generate/choose (kwd-info-ctc kwd-info) fuel)))
766774
(define env (generate-env))
767775
(cond
768-
[(andmap values gens)
776+
[(and (andmap values gens)
777+
(andmap values kwd-gens))
769778
(values
770779
(λ (f)
771780
(call-with-values
772781
(λ ()
773-
(apply
782+
(keyword-apply
774783
f
784+
dom-kwds
785+
(for/list ([kwd-gen (in-list kwd-gens)])
786+
(kwd-gen))
775787
(for/list ([gen (in-list gens)])
776788
(gen))))
777789
(λ results

racket/collects/racket/contract/private/misc.rkt

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,33 @@
175175
this-ctcs
176176
that-ctcs)))))
177177

178+
(define (and/c-generate? ctc)
179+
(cond
180+
[(and/c-check-nonneg ctc real?) => values]
181+
[(and/c-check-nonneg ctc rational?) => values]
182+
[else (λ (fuel) #f)]))
183+
184+
(define (and/c-check-nonneg ctc pred)
185+
(define sub-contracts (base-and/c-ctcs ctc))
186+
(cond
187+
[(are-stronger-contracts? (list pred (not/c negative?))
188+
sub-contracts)
189+
(define go (hash-ref predicate-generator-table pred))
190+
(λ (fuel)
191+
(λ ()
192+
(abs (go fuel))))]
193+
[else #f]))
194+
195+
(define (are-stronger-contracts? c1s c2s)
196+
(let loop ([c1s c1s]
197+
[c2s c2s])
198+
(cond
199+
[(and (null? c1s) (null? c2s)) #t]
200+
[(and (pair? c1s) (pair? c2s))
201+
(and (contract-stronger? (car c1s) (car c2s))
202+
(loop (cdr c1s) (cdr c2s)))]
203+
[else #f])))
204+
178205
(define-struct base-and/c (ctcs))
179206
(define-struct (first-order-and/c base-and/c) (predicates)
180207
#:property prop:custom-write custom-write-property-proc
@@ -184,7 +211,8 @@
184211
#:val-first-projection first-order-val-first-and-proj
185212
#:name and-name
186213
#:first-order and-first-order
187-
#:stronger and-stronger?))
214+
#:stronger and-stronger?
215+
#:generate and/c-generate?))
188216
(define-struct (chaperone-and/c base-and/c) ()
189217
#:property prop:custom-write custom-write-property-proc
190218
#:property prop:chaperone-contract
@@ -194,7 +222,8 @@
194222
#:val-first-projection val-first-and-proj
195223
#:name and-name
196224
#:first-order and-first-order
197-
#:stronger and-stronger?)))
225+
#:stronger and-stronger?
226+
#:generate and/c-generate?)))
198227
(define-struct (impersonator-and/c base-and/c) ()
199228
#:property prop:custom-write custom-write-property-proc
200229
#:property prop:contract
@@ -203,7 +232,8 @@
203232
#:val-first-projection val-first-and-proj
204233
#:name and-name
205234
#:first-order and-first-order
206-
#:stronger and-stronger?))
235+
#:stronger and-stronger?
236+
#:generate and/c-generate?))
207237

208238

209239
(define/subexpression-pos-prop (and/c . raw-fs)

0 commit comments

Comments
 (0)