[plt-scheme] Is there a general overview of contracts somewhere?

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sun Apr 17 13:29:31 EDT 2005

On Apr 16, 2005, at 1:48 AM, Eric Hanchrow wrote:

>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> Looking at Chapter 13 in the MzLib manual is enough to make me curious
> about contracts ... but that manual is a bit too terse for me: I'm not
> sure just what problem they solve, nor how to use them.

Contracts solve a simple problem. You often want to tell your "clients" 
that your functions work on a specific domain and produce only certain 
kinds of outputs. Sometimes you even want to promise that the output 
depends on the input in a simple way. At the moment, the best you can 
do is encode such promises into the function, which detracts from the 
actual computation that the function is to perform.

Contracts solve this problem. Instead of writing the promises as code 
into the function, you state the promises at the module provide 
interface, which means (1) your clients can read them w/o digging 
through the code and (2) your code focuses on the functional 
requirements and isn't cluttered by irrelevant stuff.

Here is a silly example:

(module foo mzscheme

   (require (lib "contract.ss"))

   (provide/contract
    [f (integer? . -> . rational?)] ;; f's type is number -> number, but 
I refined it here
    ;; g produces positive numbers in certain circumstances (d for 
depends)
    [g (integer? . ->d . (lambda (in) ;; the input to gt
                           (lambda (out) ;; g's result
                             (and (rational? out) ;; the promise
                                  (if (>= in 32) (>= out 0) (<= out 
0))))))])

   (define (f x) (* 9/5 (- x 32)))

   (define g f)
   )

Of course, since you're the master of your module, we won't hold you to 
such promises inside the module.

In addition, the contract system supervises all clients and your module 
during execution. If something goes wrong, it pinpoints who has broken 
a promise/obligation and tells you.

Contracts are an effective tool for monitoring some fundamental 
correctness properties of systems that are composed from many modules 
and components.

-- Matthias




> Googling for
> "mzscheme contracts" didn't bring up much of use (I really don't feel
> like reading a doctoral thesis just to learn how to use them).  So: is
> there something I could read that would introduce the idea of
> contracts, and show some examples of their use with MzScheme?
>
> Thanks
> -- 
> Always code as if the guy who ends up maintaining your code will
>     be a violent psychopath who knows where you live.  John F. Woods
>



Posted on the users mailing list.