Contract Parameters
1 Example:   Prohibit Contract
2 Reference
parameterize/  c
make-contract-parameter
parameterize-contract?
contract-parameter?
Bibliography
8.3

Contract Parameters

Cameron Moy

 (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—perhaps forbidding a certain function from printing to the screen. Enforcing such a restriction requires cooperation between the function to be prohibited and its context. Contract parameters allow this cooperation by allowing the context to dynamically bind a contract.

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] ...+)

Returns a parameterize contract that protects a procedure. Within the dynamic extent of this procedure: contract parameters will be bound to the given contract, and parameters will be bound to the given value.

procedure

(make-contract-parameter contract [name])  contract-parameter?

  contract : contract?
  name : symbol? = '???
Returns a contract parameter with a default contract. Such a contract can be dynamically bound with parameterize/c.

procedure

(parameterize-contract? v)  boolean?

  v : any/c
Returns whether the argument is a parameterize contract.

procedure

(contract-parameter? v)  boolean?

  v : any/c
Returns whether the argument is a contract parameter.

Bibliography

Christophe Scholliers, Éric Tanter, and Wolfgang De Meuter. Computational Contracts. In Proc. Science of Computer Programming, 2013.