Social Contracts
function/  c
self-map/  c
thunk/  c
binary-function/  c
variadic-function/  c
predicate/  c
encoder/  c
decoder/  c
hash-function/  c
maybe/  c
binary-composition/  c
variadic-composition/  c
reducer/  c
functional/  c
binary-constructor/  c
classifier/  c
variadic-constructor/  c
7.8

Social Contracts

Siddhartha Kasivajhula

 (require contract/social) package: social-contract

Collectively-defined contracts for commonly encountered types.

procedure

(function/c source/c target/c)  contract?

  source/c : contract?
  target/c : contract?
A contract to recognize a simple function that maps values of type source/c to values of type target/c.

Examples:
> (define/contract (list-length lst)
    (function/c list? natural-number/c)
    (if (= lst null)
      0
      (add1 (list-length (rest lst)))))
> (list-length '(h e l l o))

5

> (list-length "hello")

list-length: contract violation

  expected: list?

  given: "hello"

  in: the 1st argument of

      (-> (listof any/c) natural-number/c)

  contract from: (function list-length)

  blaming: program

   (assuming the contract is correct)

  at: eval:1.0

procedure

(self-map/c type/c)  contract?

  type/c : contract?
A contract to recognize a self-map, i.e. a function that maps a value of type domain/c to a value of the same type.

Examples:
> (define/contract (double n)
    (self-map/c natural-number/c)
    (* n 2))
> (double 5)

10

> (double "hello")

double: contract violation

  expected: natural-number/c

  given: "hello"

  in: the 1st argument of

      (-> natural-number/c natural-number/c)

  contract from: (function double)

  blaming: program

   (assuming the contract is correct)

  at: eval:4.0

procedure

(thunk/c target/c)  contract?

  target/c : contract?
A contract to recognize a "thunk", i.e. a function taking no arguments, that returns a value of type target/c.

Examples:
> (define/contract (hello-button)
    (thunk/c string?)
    "hello!")
> (hello-button)

"hello!"

> (hello-button "friend")

hello-button: arity mismatch;

 the expected number of arguments does not match the given

number

  expected: 0

  given: 1

  arguments...:

   "friend"

> (define/contract (hello-button)
    (thunk/c number?)
    "hello!")
> (hello-button)

hello-button: broke its own contract

  promised: number?

  produced: "hello!"

  in: the range of

      (-> number?)

  contract from: (function hello-button)

  blaming: (function hello-button)

   (assuming the contract is correct)

  at: eval:10.0

procedure

(binary-function/c a/c b/c target/c)  contract?

  a/c : contract?
  b/c : contract?
  target/c : contract?

procedure

(variadic-function/c source/c target/c)  contract?

  source/c : contract?
  target/c : contract?

procedure

(predicate/c [on-type/c])  contract?

  on-type/c : contract? = any/c

procedure

(encoder/c as-type/c)  contract?

  as-type/c : contract?

procedure

(decoder/c from-type/c)  contract?

  from-type/c : contract?

procedure

(hash-function/c)  contract?

procedure

(maybe/c contract [default/c])  contract?

  contract : contract?
  default/c : contract? = #f

procedure

(binary-composition/c type/c)  contract?

  type/c : contract?

procedure

(variadic-composition/c type/c)  contract?

  type/c : contract?

procedure

(reducer/c type/c)  contract?

  type/c : contract?

procedure

(functional/c [procedure/c])  contract?

  procedure/c : contract? = procedure?

procedure

(binary-constructor/c primitive/c    
  composite/c)  contract?
  primitive/c : contract?
  composite/c : contract?

procedure

(classifier/c [by-type/c])  contract?

  by-type/c : contract? = any/c

procedure

(variadic-constructor/c primitive/c    
  composite/c)  contract?
  primitive/c : contract?
  composite/c : contract?