Skip to content
This repository was archived by the owner on Sep 13, 2019. It is now read-only.

Commit 8393f0b

Browse files
committed
fix opt/c for flat-contract?
Thanks to Philip McGrath for spotting the problem Also, along the way, discover the setup for chaperoneness for opt contracts was bogus, so fix that up too
1 parent 95dab07 commit 8393f0b

File tree

5 files changed

+53
-33
lines changed

5 files changed

+53
-33
lines changed

pkgs/racket-test/tests/racket/contract/flat-contracts.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
(define (test-flat-contract/proc contract pass fail line
1818
#:skip-predicate-checks? [skip-predicate-checks? #f])
1919
(contract-eval `(,test #t flat-contract? ,contract))
20+
(contract-eval `(,test #t flat-contract? (opt/c ,contract)))
2021
(define (run-two-tests maybe-rewrite)
2122
(define name (if (pair? contract) (car contract) contract))
2223
(let/ec k

racket/collects/racket/contract/combinator.rkt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727

2828
make-contract
2929

30-
prop:opt-chaperone-contract
31-
prop:opt-chaperone-contract?
32-
prop:opt-chaperone-contract-get-test
33-
3430
prop:orc-contract
3531
prop:orc-contract?
3632
prop:orc-contract-get-subcontracts

racket/collects/racket/contract/private/guts.rkt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@
123123
(or (simple-flat-contract? x)
124124
(let ([c (coerce-contract/f x)])
125125
(and c
126-
(or (chaperone-contract-struct? c)
127-
(and (prop:opt-chaperone-contract? c)
128-
((prop:opt-chaperone-contract-get-test c) c)))))))
126+
(chaperone-contract-struct? c)))))
129127

130128
(define (simple-flat-contract? x)
131129
(or (and (procedure? x) (procedure-arity-includes? x 1))

racket/collects/racket/contract/private/opt.rkt

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@
236236
(optres-superlifts an-optres)
237237
(bind-lifts
238238
(optres-lifts an-optres)
239-
#`(make-opt-contract
239+
#`(make-an-opt-contract
240240
(λ (ctc)
241241
(λ (blame)
242242
#,(bind-superlifts
@@ -246,7 +246,11 @@
246246
(λ (this that) #f)
247247
(vector)
248248
(begin-lifted (box #f))
249-
#,(optres-chaperone an-optres))))
249+
#,(optres-chaperone an-optres)
250+
#,(let ([f (optres-flat an-optres)])
251+
(if f
252+
#`(λ (val) #,f)
253+
#'#f)))))
250254
#`(coerce-contract '#,error-name-sym #,exp))))
251255

252256
;; this macro optimizes 'e' as a contract,
@@ -324,7 +328,7 @@
324328
(optres-superlifts an-optres)
325329
(bind-lifts
326330
(optres-lifts an-optres)
327-
#`(make-opt-contract
331+
#`(make-an-opt-contract
328332
(λ (ctc)
329333
(λ (blame)
330334
(λ (val)
@@ -333,7 +337,11 @@
333337
(λ (this that) #f)
334338
(vector)
335339
(begin-lifted (box #f))
336-
#,(optres-chaperone an-optres)))))
340+
#,(optres-chaperone an-optres)
341+
#,(let ([f (optres-flat an-optres)])
342+
(if f
343+
#`(λ (val) #,f)
344+
#'#f))))))
337345
(values f1 f2)))]))
338346

339347
;; optimized contracts
@@ -346,18 +354,48 @@
346354
(define-values (orig-ctc-prop orig-ctc-pred? orig-ctc-get)
347355
(make-struct-type-property 'original-contract))
348356

357+
(define (make-an-opt-contract proj name stronger stronger-vars stamp
358+
chaperone? flat)
359+
(cond
360+
[flat
361+
(make-flat-opt-contract proj name stronger stronger-vars stamp flat)]
362+
[chaperone?
363+
(make-chaperone-opt-contract proj name stronger stronger-vars stamp)]
364+
[else
365+
(make-impersonator-opt-contract proj name stronger stronger-vars stamp)]))
366+
349367
;; the stronger-vars don't seem to be used anymore for stronger; probably
350368
;; they should be folded into the lifts and then there should be a separate
351369
;; setup for consolidating stronger checks
352-
(define-struct opt-contract (proj name stronger stronger-vars stamp chaperone?)
353-
#:property prop:opt-chaperone-contract (λ (ctc) (opt-contract-chaperone? ctc))
354-
#:property prop:custom-write (λ (val port mode) (fprintf port "#<opt-contract: ~.s>" (opt-contract-name val)))
370+
(define-struct opt-contract (proj name stronger stronger-vars stamp))
371+
372+
(define (opt-contract-stronger-proc this that)
373+
(and (opt-contract? that)
374+
(eq? (opt-contract-stamp this) (opt-contract-stamp that))
375+
((opt-contract-stronger this) this that)))
376+
377+
(define-struct (flat-opt-contract opt-contract) (predicate)
378+
#:property prop:custom-write
379+
(λ (val port mode) (fprintf port "#<opt-flat-contract: ~.s>" (opt-contract-name val)))
380+
#:property prop:flat-contract
381+
(build-flat-contract-property
382+
#:projection (λ (ctc) ((opt-contract-proj ctc) ctc))
383+
#:first-order (λ (ctc) (flat-opt-contract-predicate ctc))
384+
#:name (λ (ctc) (opt-contract-name ctc))
385+
#:stronger opt-contract-stronger-proc))
386+
(define-struct (chaperone-opt-contract opt-contract) ()
387+
#:property prop:custom-write
388+
(λ (val port mode) (fprintf port "#<opt-chaperone-contract: ~.s>" (opt-contract-name val)))
389+
#:property prop:chaperone-contract
390+
(build-chaperone-contract-property
391+
#:projection (λ (ctc) ((opt-contract-proj ctc) ctc))
392+
#:name (λ (ctc) (opt-contract-name ctc))
393+
#:stronger opt-contract-stronger-proc))
394+
(define-struct (impersonator-opt-contract opt-contract) ()
395+
#:property prop:custom-write
396+
(λ (val port mode) (fprintf port "#<opt-contract: ~.s>" (opt-contract-name val)))
355397
#:property prop:contract
356398
(build-contract-property
357399
#:projection (λ (ctc) ((opt-contract-proj ctc) ctc))
358400
#:name (λ (ctc) (opt-contract-name ctc))
359-
#:stronger
360-
(λ (this that)
361-
(and (opt-contract? that)
362-
(eq? (opt-contract-stamp this) (opt-contract-stamp that))
363-
((opt-contract-stronger this) this that)))))
401+
#:stronger opt-contract-stronger-proc))

racket/collects/racket/contract/private/prop.rkt

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@
3434
make-contract
3535
make-chaperone-contract
3636
make-flat-contract
37-
38-
prop:opt-chaperone-contract
39-
prop:opt-chaperone-contract?
40-
prop:opt-chaperone-contract-get-test
41-
37+
4238
prop:orc-contract
4339
prop:orc-contract?
4440
prop:orc-contract-get-subcontracts
@@ -221,15 +217,6 @@
221217
chaperone-contract-property-guard
222218
(list (cons prop:contract chaperone-contract-property->contract-property))))
223219

224-
;; this property is so the opt'd contracts can
225-
;; declare that they are chaperone'd; the property
226-
;; is a function that extracts a boolean from the
227-
;; original struct
228-
(define-values (prop:opt-chaperone-contract
229-
prop:opt-chaperone-contract?
230-
prop:opt-chaperone-contract-get-test)
231-
(make-struct-type-property 'prop:opt-chaperone-contract))
232-
233220
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
234221
;;
235222
;; Flat Contract Property

0 commit comments

Comments
 (0)