Skip to content

Commit

Permalink
Improve contract-stronger for object/c-opaque
Browse files Browse the repository at this point in the history
This changes `contract-stronger?` to recognize that if two contracts
both restrict the same field or method to not be callable, then they
are equally strong.
  • Loading branch information
dfeltey committed Apr 25, 2018
1 parent badec99 commit ff2956d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
16 changes: 14 additions & 2 deletions typed-racket-lib/typed-racket/utils/opaque-object.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@

(provide object/c-opaque)

(module+ for-testing
(provide restrict-typed->/c
restrict-typed-field/c))

;; projection for base-object/c-opaque
(define ((object/c-opaque-late-neg-proj ctc) blame)
(λ (obj neg-party)
Expand Down Expand Up @@ -172,7 +176,11 @@
#:property prop:chaperone-contract
(build-chaperone-contract-property
#:name restrict-typed->-name
#:stronger equal?
#:stronger
(λ (this that)
(define this-name (restrict-typed->/c-name this))
(define that-name (restrict-typed->/c-name that))
(eq? this-name that-name))
#:late-neg-projection restrict-typed->-late-neg-projection))

(define (restrict-typed-field-late-neg-proj ctc)
Expand All @@ -198,5 +206,9 @@
#:property prop:flat-contract
(build-flat-contract-property
#:name restrict-typed-field-name
#:stronger equal?
#:stronger
(λ (this that)
(define this-name (restrict-typed-field/c-name this))
(define that-name (restrict-typed-field/c-name that))
(eq? this-name that-name))
#:late-neg-projection restrict-typed-field-late-neg-proj))
40 changes: 39 additions & 1 deletion typed-racket-test/succeed/opaque-object-stronger.rkt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#lang racket
(require typed-racket/utils/opaque-object)
(require typed-racket/utils/opaque-object
(submod typed-racket/utils/opaque-object for-testing))

;; --------------------------------------------------------------------------------------------------
;; test helpers
Expand Down Expand Up @@ -184,3 +185,40 @@
(m1 (->m any/c any/c any/c))))

)

(let () ;; restrict-typed->/c and restrict-typed-field/c
(test-stronger?
(restrict-typed->/c 'foo)
(restrict-typed->/c 'foo))

(test-not-stronger?
(restrict-typed->/c 'foo)
(restrict-typed->/c 'bar))

(test-stronger?
(restrict-typed-field/c 'foo)
(restrict-typed-field/c 'foo))

(test-not-stronger?
(restrict-typed-field/c 'foo)
(restrict-typed-field/c 'bar))
)

(let ()
(define ctc
(object/c-opaque
(mtd (->m any/c any/c))))
(define (make-obj)
(new
(class object%
(super-new)
(define/public (mtd x) x)
(define/public (guts) #f))))
(define c1
(value-contract
(contract ctc (make-obj) 'p 'n)))
(define c2
(value-contract
(contract ctc (make-obj) 'p 'n)))
(test-stronger? c1 c2)
(test-stronger? c2 c1))

0 comments on commit ff2956d

Please sign in to comment.