[plt-scheme] Questions about contracts

From: Robby Findler (robby at cs.uchicago.edu)
Date: Tue Apr 17 15:04:17 EDT 2007

On 4/16/07, Chongkai Zhu <czhu at cs.utah.edu> wrote:
> Than my question is: how to write a correct contract
> for cases like this?

I think that it wasn't possible to express that contract in
yesterday's contract system. So, I've added a new contract combinator.
It accepts functions and puts no constraints on their arities, but
does but a constraint on their ranges. You can combine this with other
contracts to get the effect you (seem to) want.

Below is an example use (the docs should be out tomorrow when the
nightly build completes). In general, unconstrained-domain-> accepts
one argument for each (multiple value) result the function produces
and it puts no constraint on the arity of the function.

I hope that helps.


(module m mzscheme
  (require (lib "contract.ss")
           (lib "etc.ss"))

   [f (->r ([size natural-number/c]
            [proc (and/c (unconstrained-domain-> number?)
                         (λ (p) (procedure-arity-includes? p size)))])

  (define (f i g) (apply g (build-list i add1))))

(module n mzscheme
  (require m)

  (printf "pass: ~s\n" (f 10 +))

  ;; this one fails.
  (f 3 (λ (a b) (+ a b))))

(require n)

Posted on the users mailing list.