7.6 Contract Utilities
(flat-contract? v) → boolean? |
v : any/c |
For example, flat-contract constructs flat contracts from predicates, and symbols, booleans, numbers, and other ordinary Racket values (that are defined as contracts) are also flat contracts.
(flat-contract-predicate v) → (any/c . -> . any/c) |
v : flat-contract? |
(value-contract v) → contract? |
v : has-contract? |
(has-contract? v) → boolean? |
v : any/c |
(contract-stronger? x y) → boolean? |
x : contract? |
y : contract? |
This function is conservative, so it may return #f when x does, in fact, accept fewer values.
Examples: | ||
> (contract-stronger? integer? integer?) | ||
#t | ||
> (contract-stronger? (between/c 25 75) (between/c 0 100)) | ||
#t | ||
> (contract-stronger? (between/c 0 100) (between/c 25 75)) | ||
#f | ||
> (contract-stronger? (between/c -10 0) (between/c 0 10)) | ||
#f | ||
| ||
#f |
(contract-first-order-passes? contract v) → boolean? |
contract : contract? |
v : any/c |
If it returns #f, the contract is guaranteed not to hold for that value; if it returns #t, the contract may or may not hold. If the contract is a first-order contract, a result of #t guarantees that the contract holds.
(contract-name c) → any/c |
c : contract? |
(contract-first-order c) → (-> any/c boolean?) |
c : contract? |
(make-none/c sexp-name) → contract? |
sexp-name : any/c |
(current-blame-format) → (-> blame? any/c string? string?) |
(current-blame-format proc) → void? |
proc : (-> blame? any/c string? string?) |
the blame object for the violation,
the value that the contract applies to, and
a message indicating the kind of violation.
Examples: | |||||||||||
| |||||||||||
> [current-blame-format show-blame-error] | |||||||||||
| |||||||||||
> (f 2) | |||||||||||
1 | |||||||||||
> (f 1) | |||||||||||
Contract Violation! | |||||||||||
Guilty Party: (function f) | |||||||||||
Innocent Party: top-level | |||||||||||
Contracted Value Name: f | |||||||||||
Contract Location: #(struct:srcloc eval 4 0 4 1) | |||||||||||
Contract Name: (-> integer? integer?) | |||||||||||
Offending Value: 1/2 | |||||||||||
Offense: expected <integer?>, given: 1/2 | |||||||||||
> (f 1/2) | |||||||||||
Contract Violation! | |||||||||||
Guilty Party: top-level | |||||||||||
Innocent Party: (function f) | |||||||||||
Contracted Value Name: f | |||||||||||
Contract Location: #(struct:srcloc eval 4 0 4 1) | |||||||||||
Contract Name: (-> integer? integer?) | |||||||||||
Offending Value: 1/2 | |||||||||||
Offense: expected <integer?>, given: 1/2 |
(recursive-contract contract-expr) |
(opt/c contract-expr) |
(define-opt/c (id id ...) expr) |
For example,
(define-contract-struct bt (val left right)) |
(define-opt/c (bst-between/c lo hi) |
(or/c null? |
(bt/c [val (real-in lo hi)] |
[left (val) (bst-between/c lo val)] |
[right (val) (bst-between/c val hi)]))) |
(define bst/c (bst-between/c -inf.0 +inf.0)) |
defines the bst/c contract that checks the binary search tree invariant. Removing the -opt/c also makes a binary search tree contract, but one that is (approximately) 20 times slower.