On this page:
guilty-party
contract?
flat-contract?
flat-contract-predicate
contract-first-order-passes?
make-none/ c
contract-violation->string
recursive-contract
opt/ c
define-opt/ c
Version: 4.1

7.6 Contract Utilities

(guilty-party exn)  any

  exn : exn?

Extracts the name of the guilty party from an exception raised by the contract system.

(contract? v)  boolean?

  v : any/c

Returns #t if its argument is a contract (ie, constructed with one of the combinators described in this section), #f otherwise.

(flat-contract? v)  boolean?

  v : any/c

Returns #t when its argument is a contract that has been constructed with flat-contract (and thus is essentially just a predicate), #f otherwise.

(flat-contract-predicate v)  (any/c . -> . any/c)

  v : flat-contract?

Extracts the predicate from a flat contract.

(contract-first-order-passes? contract v)  boolean?

  contract : contract?

  v : any/c

Returns a boolean indicating if the first-order tests of contract pass for v.

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.

(make-none/c sexp-name)  contract?

  sexp-name : any/c

Makes a contract that accepts no values, and reports the name sexp-name when signaling a contract violation.

(contract-violation->string)

  (any/c any/c symbol? symbol? any/c string? . -> . string?)

(contract-violation->string proc)  void?

  proc : (any/c any/c symbol? symbol? any/c string? . -> . string?)

This is a parameter that is used when constructing a contract violation error. Its value is procedure that accepts six arguments: the value that the contract applies to, a syntax object representing the source location where the contract was established, the names of the two parties to the contract (as symbols) where the first one is the guilty one, an sexpression representing the contract, and a message indicating the kind of violation. The procedure then returns a string that is put into the contract error message. Note that the value is often already included in the message that indicates the violation.

(recursive-contract contract-expr)

Delays the evaluation of its argument until the contract is checked, making recursive contracts possible.

(opt/c contract-expr)

This optimizes its argument contract expression by traversing its syntax and, for known contract combinators, fuses them into a single contract combinator that avoids as much allocation overhad as possible. The result is a contract that should behave identically to its argument, except faster (due to the less allocation).

(define-opt/c (id id ...) expr)

This defines a recursive contract and simultaneously optimizes it. Semantically, it behaves just as if the -opt/c were not present, defining a function on contracts (except that the body expression must return a contract). But, it also optimizes that contract definition, avoiding extra allocation, much like opt/c does.

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.