On this page:
8.1 Flat Contracts
nat/ c
pos/ c
truth/ c
8.2 Higher-Order Contracts
thunk/ c
unary/ c
binary/ c
predicate/ c
predicate-like/ c
comparison/ c
comparison-like/ c
sequence/ c
dict/ c

8 Contracts

 (require (planet cce/scheme:7:5/contract))

This module provides useful contracts and contract constructors.

8.1 Flat Contracts

This contract recognizes natural numbers that satisfy exact-nonnegative-integer?.

This contract recognizes positive integers that satisfy exact-positive-integer?.

This contract recognizes Scheme truth values, i.e., any value, but with a more informative name and description. Use it in negative positions for arguments that accept arbitrary truth values that may not be booleans.

8.2 Higher-Order Contracts

These contracts recognize functions that accept 0, 1, or 2 arguments, respectively, and produce a single result.

These contracts recognize predicates: functions of a single argument that produce a boolean result.

The first constrains its output to satisfy boolean?. Use predicate/c in positive position for predicates that guarantee a result of #t or #f.

The second constrains its output to satisfy truth/c. Use predicate-like/c in negative position for predicates passed as arguments that may return arbitrary values as truth values.

These contracts recognize comparisons: functions of two arguments that produce a boolean result.

The first constrains its output to satisfy boolean?. Use comparison/c in positive position for comparisons that guarantee a result of #t or #f.

The second constrains its output to satisfy truth/c. Use comparison-like/c in negative position for comparisons passed as arguments that may return arbitrary values as truth values.

(sequence/c elem/c ...)  contract?
  elem/c : contract?
Wraps a sequence, obligating it to produce as many values as there are elem/c contracts, and obligating each value to satisfy the corresponding elem/c. The result is not guaranteed to be the same kind of sequence as the original value; for instance, a wrapped list is not guaranteed to satisfy list?.

Examples:

> (define/contract predicates
    (sequence/c (-> any/c boolean?))
    (list integer? string->symbol))
> (for ([P predicates])
    (printf "~s\n" (P "cat")))

struct-copy: expected argument of type <blame>; given

#<|chaperone->|>

(dict/c key/c value/c)  contract?
  key/c : contract?
  value/c : contract?
Wraps a dictionary, obligating its keys to satisfy key/c and their corresponding values to satisfy value/c. The result is not guaranteed to be the same kind of dictionary as the original value; for instance, a wrapped hash table is not guaranteed to satisfy hash?.

Examples:

> (define/contract table
    (dict/c symbol? string?)
    (make-immutable-hash (list (cons 'A "A") (cons 'B 2) (cons 3 "C"))))
> (dict-ref table 'A)

procedure ...private/prop.rkt:211:0: expects 1 argument,

given 5: 'program '(definition table) (list (srcloc 'eval 2

0 2 1) 'table) '(dict/c symbol? string?) #f

> (dict-ref table 'B)

procedure ...private/prop.rkt:211:0: expects 1 argument,

given 5: 'program '(definition table) (list (srcloc 'eval 2

0 2 1) 'table) '(dict/c symbol? string?) #f

> (dict-ref table 3)

procedure ...private/prop.rkt:211:0: expects 1 argument,

given 5: 'program '(definition table) (list (srcloc 'eval 2

0 2 1) 'table) '(dict/c symbol? string?) #f

Warning: Bear in mind that key and value contracts are re-wrapped on every dictionary operation, and dictionaries wrapped in dict/c multiple times will perform the checks as many times for each operation. Especially for immutable dictionaries (which may be passed through a constructor that involves dict/c on each update), contract-wrapped dictionaries may be much less efficient than the original dictionaries.