Contract Miscellanea
dynamic->d
self/  c
apply/  c
return/  c
:
8.1

Contract Miscellanea

Cameron Moy

 (require contract-etc) package: contract-etc

This library is experimental; compatibility may not be maintained.

procedure

(dynamic->d make-contract)  contract?

  make-contract : 
(-> (unconstrained-domain-> contract?)
    contract?)
This contract protected a procedure. It applies make-contract when the procedure is called with its arguments. The return value is expected to be a function contract that is then applied to the procedure, and the arguments are then reapplied to that protected procedure.

Examples:
> (define increasing/c
    (dynamic->d
      (λ (x)
        (-> integer? (>/c x)))))
> (define/contract add1* increasing/c add1)
> (add1* 42)

43

> (define/contract values* increasing/c values)
> (values* 42)

values*: broke its own contract

  promised: a number strictly greater than 42

  produced: 42

  in: the range of

      dynamic->d

  contract from: (definition values*)

  blaming: (definition values*)

   (assuming the contract is correct)

  at: eval:4.0

procedure

(self/c make-contract    
  [#:chaperone? chaperone?])  contract?
  make-contract : (-> any/c contract?)
  chaperone? : boolean? = #f
Constructs a contract where the contract itself depends on the value it’s protecting. When the contract is attached to a value, make-contract is applied to it and the resulting contract is then attached to that value.

Examples:
> (define cdr-returns-car/c
    (self/c
      (λ (p)
        (match-define (cons x f) p)
        (cons/c any/c (-> x)))))
> (define/contract good-self cdr-returns-car/c (cons 1 (const 1)))
> ((cdr good-self))

1

> 1

1

> (define/contract bad-self cdr-returns-car/c (cons 1 (const 2)))
> ((cdr bad-self))

bad-self: broke its own contract

  promised: 1

  produced: 2

  in: the range of

      the cdr of

      self/c

  contract from: (definition bad-self)

  blaming: (definition bad-self)

   (assuming the contract is correct)

  at: eval:10.0

syntax

(apply/c [contract-expr to-protect-expr maybe-positive] ...+)

 
maybe-positive = 
  | #:positive

syntax

(return/c [contract-expr to-protect-expr maybe-positive] ...+)

 
maybe-positive = 
  | #:positive
These contracts expect a procedure and sends a constant value through another contract when the procedure is applied or returns respectively. The #:positive option swaps the blame for violation of the contract.

Examples:
> (define (apply-at-most-once/c)
    (define count 0)
    (define (incr n)
      (set! count (+ count n))
      (<= count 1))
    (apply/c [incr 1]))
> (define/contract f (apply-at-most-once/c) void)
> (f)
> (f)

...ontract-etc/main.rkt:106:4: broke its own contract

  promised: incr

  produced: 1

  in: incr

  contract from: top-level

  blaming: top-level

   (assuming the contract is correct)

syntax

(: id contract-expr)

If the RKT_PRIVATE_CONTRACTS environment variable is set at compile time, then this annotation provides id with the given contract. Additionally, the references to id within the test submodule are contracted. If the environment variable isn’t set, then id is provided without any contracts.