[plt-scheme] Contracts on Libraries

From: Joe Marshall (jmarshall at alum.mit.edu)
Date: Sat May 26 15:13:10 EDT 2007

On a Common Lisp project I was working on, there was some library code
that could deliver high performance if you used it correctly, but
would likely crash the machine if you didn't.  CL doesn't have
contracts, but you can manually insert checks (poor-man's contracts?).
 I'd write code like this:

(defun foo (x y)
  (if (and (ok? x) (ok? y))
      (%%fast-foo x y)
      (error)))

or

(defun foo (x y)
   (if (and (ok? x) (ok? y))
       (%%fast-foo x y)
       (slow-generic-foo x y)))

Where %%fast-foo was the high-performance, low-safety implementation.
I'd generally not export the %%fast-foo and require client code to
take the slower route, but CL allows you to bypass the export
restrictions, so when I needed the fast one, I could call it directly.

It seems to me that a similar technique could be used with contracts.



On 5/26/07, Carl Eastlund <cce at ccs.neu.edu> wrote:
> I concur: contracts are a good thing.  They're helpful for bug-finding
> at all stages of development. As long as you don't put a linear-time
> contract on a log-time operation (or something similar), efficiency
> shouldn't be too bad.  Scheme already has safety checks on many
> operations; as far as I'm concerned adding new checks to new
> operations is just standard overhead.
>
> If you have a really speed-sensitive library, you could always expose
> both the contracted and uncontracted modules and let users choose.
> Personally, I wouldn't worry about it for 99.99% of planet libraries.
>
> --Carl
>
> On 5/26/07, Chongkai Zhu <czhu at cs.utah.edu> wrote:
> > I agree with you and quite many people do the same thing. I can't see any significant efficiency difference between using contracts and user code.
> >
> > Chongkai
> >
> > ----- Original Message -----
> > From: "Paulo J. Matos" <pocm at soton.ac.uk>
> > To: <plt-scheme at list.cs.brown.edu>
> > Sent: Saturday, May 26, 2007 10:39 AM
> > Subject: [plt-scheme] Contracts on Libraries
> >
> >
> > > Hello all,
> > >
> > > I'd like some comments on addings contracts to libraries. I'm
> > > developing a graph library, soon on planet, which on the interface
> > > module contains contracts on the functions the user might access.
> > > In my opinion this is good because it allows me to separate the code
> > > doing real stuff and the checks (does the function receive a struct
> > > and blablabla). However, I'd like to know if people agree with the use
> > > of contracts on libraries or if for efficiency reasons (or any other),
> > > think that no contracts should be used and that they should only
> > > appear on user code.
> > >
> > > Cheers,
> > >
> > > --
> > > Paulo Jorge Matos - pocm at soton.ac.uk
> > > http://www.personal.soton.ac.uk/pocm
> > > PhD Student @ ECS
> > > University of Southampton, UK
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>


-- 
~jrm


Posted on the users mailing list.