► |  | The Racket Reference

1 | Language Model
2 | Syntactic Forms
3 | Datatypes
4 | Structures
5 | Classes and Objects
6 | Units
7 | Contracts
8 | Pattern Matching
9 | Control Flow
10 | Concurrency and Parallelism
11 | Macros
12 | Input and Output
13 | Reflection and Security
14 | Operating System
15 | Memory Management
16 | Unsafe Operations
17 | Running Racket
| Bibliography
| Index

▼ | 7 | Contracts

7.1 | Data-structure Contracts
7.2 | Function Contracts
7.3 | Parametric Contracts
7.4 | Lazy Data-structure Contracts
7.5 | Structure Type Property Contracts
7.6 | Attaching Contracts to Values
7.7 | Building New Contract Combinators
7.8 | Contract Utilities
7.9 | racket/ contract/ base
7.10 | Legacy Contracts
7.11 | Random generation

top ← prev up next →

### 7`` Contracts

> > > Contracts in The Racket Guide introduces contracts.

The contract system guards one part of a program from another. Programmers specify the behavior of a module’s exports via (provide (contract-out ....)), and the contract system enforces those constraints.

(require racket/contract)

The bindings documented in this section are provided by the racket/contract and racket libraries, but not racket/base.

Contracts come in two forms: those constructed by the various operations listed in this section of the manual, and various ordinary Racket values that double as contracts, including

- symbols, booleans, characters, keywords, and null, which are treated as contracts that recognize themselves, using eq?,

- strings and byte strings, which are treated as contracts that recognize themselves using equal?,

- numbers, which are treated as contracts that recognize themselves using =,

- regular expressions, which are treated as contracts that recognize byte strings and strings that match the regular expression, and

- predicates: any procedure of arity 1 is treated as a predicate. During contract checking, it is applied to the values that appear and should return #f to indicate that the contract failed, and anything else to indicate it passed.

Contracts in Racket are subdivided into three different categories:

- Flat contracts can be fully checked immediately for a given value. These kinds of contracts are essentially predicate functions. Using flat-contract-predicate, you can extract the predicate from a flat contract, and flat-contract? recognizes a flat contract.

- Chaperone contracts are not always immediately checkable, but are guaranteed to not change any properties of any values that they check. That is, they may wrap a value in such a way that it signals contract violations later, as the value is used (e.g., a function contract checks the inputs and outputs to the function only when the function is called and returned), but any properties that the value had before being wrapped by the contract are preserved by the contract wrapper.

  All flat contracts are also chaperone contracts (but not vice-versa).

- Impersonator contracts do not provide any guarantees about values they check. Impersonator contracts may hide properties of values, or even make them completely opaque (e.g, new-∀/c).

  All contracts are impersonator contracts.

For more about this hierarchy, see chaperones and a research paper on chaperones, impersonators, and how they can be used to implement contracts [Strickland12].

7.1 Data-structure Contracts
7.2 Function Contracts
7.3 Parametric Contracts
7.4 Lazy Data-structure Contracts
7.5 Structure Type Property Contracts
7.6 Attaching Contracts to Values
7.6.1 Nested Contract Boundaries
7.6.2 Low-level Contract Boundaries
7.7 Building New Contract Combinators
7.7.1 Blame Objects
7.7.2 Contracts as structs
7.7.3 Obligation Information in Check Syntax
7.7.4 Utilities for Building New Combinators
7.8 Contract Utilities
7.9 racket/contract/base
7.10 Legacy Contracts
7.11 Random generation

top ← prev up next →