7.6.1 Contracts and eq?
As a general rule, adding a contract to a program should either leave the behavior of the program unchanged, or should signal a contract violation. And this is almost true for PLT Scheme contracts, with one exception: eq?.
The eq? procedure is designed to be fast and does not provide much in the way of guarantees, except that if it returns true, it means that the two values behave identically in all respects. Internally, this is implemented as pointer equality at a low-level so it exposes information about how PLT Scheme is implemented (and how contracts are implemented).
|(define (make-adder x)|
|(if (= 1 x)|
|(lambda (y) (+ x 1))))|
|(provide/contract [make-adder (-> number? (-> number? number?))])|
It exports the make-adder function that is the usual curried addition function, except that it returns Scheme’s add1 when its input is 1.
|(eq? (make-adder 1)|
Moral: do not use eq? on values that have contracts.
When defining a self-referential contract, it is natural to use define. For example, one might try to write a contract on streams like this:
reference to undefined identifier: stream/c
Unfortunately, this does not work because the value of stream/c is needed before it is defined. Put another way, all of the combinators evaluate their arguments eagerly, even thought the values that they accept do not.
The use of recursive-contract delays the evaluation of the identifier stream/c until after the contract is first checked, long enough to ensure that stream/c is defined.
See also Checking Properties of Data Structures.
7.6.3 Using set! to Assign to Variables Provided via provide/contract
The contract library assumes that variables exported via provide/contract are not assigned to, but does not enforce it. Accordingly, if you try to set! those variables, you may be surprised. Consider the following example:
|> (require 'client)|
Both calls to print-latest print 0, even though the value of x has been incremented (and the change is visible inside the module x).
To work around this, export accessor functions, rather than exporting the variable directly, like this:
|(define (get-x) x)|
|(define (inc-x!) (set! x (+ x 1)))|
|(define x 0)|
|(provide/contract [inc-x! (-> void?)]|
|[get-x (-> integer?)])|
Moral: This is a bug we hope to address in a future release.