Social Contracts
(require contract/social) | package: social-contract |
Collectively-defined contracts for commonly encountered types.
procedure
(function/c) → contract?
(function/c source/c target/c) → contract? source/c : contract? target/c : contract?
> (define/contract (list-length lst) (function/c list? natural-number/c) (if (empty? lst) 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?
> (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
> (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? = any/c b/c : contract? = #f target/c : contract? = #f
procedure
(variadic-function/c [source/c target/c]) → contract?
source/c : contract? = any/c target/c : contract? = #f
procedure
(binary-variadic-function/c [ a/c b/c target/c #:tail? tail?]) → contract? a/c : contract? = any/c b/c : contract? = #f target/c : contract? = #f tail? : boolean? = #f
procedure
(predicate/c [on-type/c]) → contract?
on-type/c : contract? = any/c
procedure
as-type/c : contract?
procedure
from-type/c : contract?
procedure
procedure
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
(binary-variadic-composition/c type/c) → contract?
type/c : contract?
procedure
type/c : contract? = any/c target/c : contract? = #f
procedure
(functional/c [procedure/c]) → contract?
procedure/c : contract? = procedure?
procedure
(classifier/c [by-type/c]) → contract?
by-type/c : contract? = any/c
procedure
source/c : contract? = any/c target/c : contract? = #f
procedure
of-type/c : contract? = any/c
procedure
(binary-constructor/c primitive/c composite/c [ #:order order]) → contract? primitive/c : contract? composite/c : contract? order : (one-of/c 'abb 'bab) = 'abb
procedure
(variadic-constructor/c primitive/c composite/c [ #:order order]) → contract? primitive/c : contract? composite/c : contract? order : (one-of/c 'abb 'bab) = 'abb