Version: 5.0.99.5

## Contract-Utils: Various contract functions and utilities

Version 4.0, December 2010

Richard Cobbe <cobbe at ccs dot neu dot edu>

 (require (planet cobbe/contract-utils:4/contract-utils))

This module provides several general-purpose contracts and contract utilities.

### 1Flat Contracts

 sexp/c : flat-contract?
A flat contract that accepts arbitrary S-expressions. Here, an S-expression is any of
• the empty list

• a number

• a symbol

• a string

• a Boolean

• a character

• a cons cell containing two S-expressions

 positive-int/c : flat-contract?
A contract that accepts strictly positive integers.

 (nelistof/c c) → contract? c : flat-contract/c
Returns a contract that accepts non-empty lists whose every element matches the contract c. As with listof, when this contract is applied to a value, the result is not necessarily eq? to the input.

 (listof-unique/c elt=?) → flat-contract? elt=? : (any/c any/c . -> . boolean?)
Returns a contract that accepts lists whose elements are unique according to the equality predicate elt=?. As with listof and nelistof/c, the resulting contracts do not necessarily preserve object identity. Additionally, the resulting contract may apply elt=? to individual elements and pairs of elements arbitrarily many times.

 (listof-unique-compare/c cmp) → contract? cmp : comparison/c
Like listof-unique/c, but uses the order function cmp to determine equality for members of the list.

 immutable-string/c : flat-contract?
A contract that accepts immutable strings. (Historically useful for writing contracts for substructures of exn, although this is no longer as important.)

### 2Higher-Order Contracts

 predicate/c : contract?
A contract that accepts predicate functions. A predicate function is a function which accepts any single value and always produces a Boolean result.

 (binary-predicate/c arg/c) → contract? arg/c : contract?
Returns a contract for a function that accepts two arguments (each of which must match the contract arg/c) and returns a Boolean.

 (equality/c arg/c) → contract? arg/c : contract?
Returns a contract for a function that accepts two arguments (each of which must match the contract arg/c) and returns a Boolean. For now, this this function is equivalent to binary-predicate/c, but in later releases, we may modify equality/c to check for additional properties of equivalence relations, like reflexivity.

 (comparison/c arg/c) → contract? arg/c : contract?
Returns a contract for a SRFI-67-style comparison functions: functions that accept two arguments (each of which must match the contract arg/c) and return one of 1, 0, or -1. We may eventually extend this to check for other desirable properties, like order reflexivity and anti-symmetry.

 (optional/c c) → contract? c : contract?
Produces a contract that accepts an optional value—that is, all values that c accepts, as well as #f. The result is a flat contract if and only if c is a flat contract or a predicate.

 contract/c : contract?
A contract that accepts contracts and (unary) predicates.

 flat-contract/c : contract?
A contract that accepts flat contracts and (unary) predicates.

### 3Contract Utilities

 (contract-of contract-or-predicate) → contract? contract-or-predicate : contract/c
Wraps a predicate in a flat contract and leaves contracts unchanged.

 (predicate-of flat-contract-or-predicate) → predicate/c flat-contract-or-predicate : flat-contract/c
Extracts a flat contract’s predicate and leaves predicates unchanged.

 (eta function)
Eta-expands function. That is, it expands to
(lambda args (apply function args))
This is useful primarily for writing contract specifications that appear before their predicate definitions.

### 4Acknowledgments

Thanks to Carl Eastlund for contract/c, flat-contract/c, contract-of, predicate-of, listof-unique/c, listof-unique-compare/c, equality/c, comparison/c.