Contract Parameters
(require contract-parameter) | |
package: contract-parameter |
This library is experimental; compatibility may not be maintained.
This package implements contract parameters that allow for dynamically bound contracts. Contract parameters can be used to implement checks that are similar to computational contracts [Scholliers et al. 2013].
1 Example: Prohibit Contract
Suppose you want a contract that prevents
calling a certain family of functions—
First, create a contract parameter called prohibitable/c. What it checks depends on the context. By default, it checks any/c.
(define prohibitable/c (make-contract-parameter any/c))
Next, define a prohibit/c contract that will instantiate the prohibitable/c parameter with none/c. Thus, if you attach prohibit/c to a function, then during that function’s execution prohibitable/c will be bound to none/c.
(define prohibit/c (parameterize/c [prohibitable/c none/c]))
Using prohibitable/c in a function contract will allow you to prohibit it. In this case, we can check that will-prevent will never be called.
(define/contract (will-prevent x) (-> (and/c integer? prohibitable/c) any) (add1 x)) (define/contract (good-prohibit) prohibit/c 43) (define/contract (bad-prohibit) prohibit/c (will-prevent 42))
Here’s what it looks like if you try to call good-prohibit and bad-prohibit:
> (good-prohibit) 43
> (bad-prohibit) will-prevent: contract violation;
none/c allows no values
given: 42
in: the 2nd conjunct of
the 1st argument of
(-> (and/c integer? ???) any)
contract from: (function will-prevent)
blaming: top-level
(assuming the contract is correct)
at: eval:3:0
2 Reference
syntax
(parameterize/c [parameter expr] ...+)
procedure
(make-contract-parameter contract [name]) → contract-parameter?
contract : contract? name : symbol? = '???
procedure
v : any/c
procedure
(contract-parameter? v) → boolean?
v : any/c
Bibliography
Christophe Scholliers, Éric Tanter, and Wolfgang De Meuter. Computational Contracts. In Proc. Science of Computer Programming, 2013. |