8.4 Lazy Data-structure Contracts (original) (raw)

8.4 Lazy Data-structure Contracts🔗

(contract-struct id (field-id ...))

Like struct, but with two differences: they do not define field mutators, and they define two contract constructors:id/c and id/dc. The first is a procedure that accepts as many arguments as there are fields and returns a contract for struct values whose fields match the arguments. The second is a syntactic form that also produces contracts on the structs, but the contracts on later fields may depend on the values of earlier fields.

The generated contract combinators are lazy: they only verify the contract holds for the portion of some data structure that is actually inspected. More precisely, a lazy data structure contract is not checked until a selector extracts a field of a struct.

(id/dc field-spec ...)
field-spec = [field-id contract-expr] | [field-id (field-id ...) contract-expr]

In each field-spec case, the first field-idspecifies which field the contract applies to; the fields must be specified in the same order as the originalcontract-struct. The first case is for when the contract on the field does not depend on the value of any other field. The second case is for when the contract on the field does depend on some other fields, and the parenthesized field-ids indicate which fields it depends on; these dependencies can only be to earlier fields.

(define-contract-struct id (field-id ...))

Like contract-struct, but where the constructor’s name ismake-id, much like define-struct.