signature
8.18
top
← prev up next →

signatureπŸ”— i

Scott Moore

Syntax classes and utilities for defining function signatures with contracts, including an alternative syntax for indy-dependent contracts.

syntax

( proc/c argument...optional-rest->result...)

argument = id+ctc
| keywordid+ctc
id+ctc = id-or-optional
| (id-or-optional:contract-expr)
id-or-optional = id
| [id]
optional-rest =
| ..id+ctc
result = (id+ctc)
The proc/c defines an indy-dependent contract like ->i. Unlike ->i, proc/c does not require contract expressions to declare dependencies on other arguments. Instead, it infers them automatically using the names of arguments and result values declared in the signature. Warning: proc/c is not currently as efficient as ->i and has a higher performance overhead.

For example, the contract:
(->i([xnumber? ]
[y(x)(>=/cx)])
[result(xy)(and/cnumber? (>=/c(+ xy)))])
can be written as the following.

(proc/c (x:number? )(y:(>=/cx))->(result:(and/cnumber? (>=/c(+ xy)))))

Unlike ->i, mandatory and optional arguments are not specified separately. The contract
(->i(#:x[xnumber? ])
(#:y[y(x)(>=/cx)])
[result(xy)(and/cnumber? (>=/c(+ xy)))])
can be written as the following.

(proc/c #:x(x:number? )#:y([y]:(>=/cx))->(result:(and/cnumber? (>=/c(+ xy)))))

Like ->i, the contract expressions are not always evaluated in order. If there are optional arguments that are not supplied, then the corresponding variables will be bound to a special value called the-unsupplied-arg value.

syntax

( define/sig (idargument...optional-rest->result...)
body...)
argument = id+ctc
| keywordid+ctc
id+ctc = id-or-optional/default
| (id-or-optional:contract-expr)
id-or-optional/default = id
| [iddefault-expr]
optional-rest =
| ..id+ctc
result = (id+ctc)
Defines a procedure with the given signature and applies the corresponding contract.

For example,
(define/sig (add#:x(x:number? )#:y([yx]:(>=/cx))->(result:(and/cnumber? (>=/c(+ xy)))))
(+ xy))
is equivalent to the following.
(define/contract(add#:xx#:y[yx])
(proc/c (#:x(x:number? )#:y([y]:(>=/cx)))->(result:(and/cnumber? (>=/c(+ xy)))))
(+ xy))

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /