[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Rough proposal for contracts in Chicken
From: |
felix winkelmann |
Subject: |
Re: [Chicken-users] Rough proposal for contracts in Chicken |
Date: |
Thu, 21 Dec 2006 20:20:21 +0100 |
On 12/19/06, John Cowan <address@hidden> wrote:
I've read over the PLT Scheme approach, and I think I see how it can be
adapted to Chicken. Basically, the syntax of the export declaration is
extended to provide per-procedure argument and result checking, and a
new declaration is added to provide module invariants.
Here another proposal, slightly more thought out than my last one:
(contracted
(export ((plus number? number?) -> number?)
((plus/all number? #!rest (list-of number?)) -> number?)
((exact->div0+mod0 exact? exact?) -> exact? exact?) )
(invariant (= 0 (length local-stack)))
(define (plus ...) ...)
...)
This is effectively a module that expands into a set of "defines",
and a local scope with the real code, sth along these lines:
(begin
(define plus (void))
(define plus/all (void))
(define exact->div0+mod0 (void))
(let ((vals
(let ()
(define (invariant1 . vals)
(assert (= 0 (length local-stack)))
(apply values vals) )
(define (plus x y) ...)
(define (plus/all x . more) ...)
(define (exact->div0+mod0 a b) ...)
(vector
(lambda (x y)
(call-with-values
(lambda ()
(plus (ensure exact? x) (ensure exact? y)) )
invariant1) )
...))))
(set! plus (vector-ref vals 0))
...) )
The vector-stuff is just to pass the internal definitions outwards
to the toplevel or outer-level scope. DSSSL lambda-lists should
be supported and also nicely handle the problem of the ambiguous
rest-argument case. The code is naturally pretty slow, but adding
cond-expand forms to the expansion that lets one switch off the
checks shouldn't be too hard. The same goes for unsafe mode.
For syntax-case, this could actually expand into an anonymous
module.
Putting everything into an enclosing form may not meet anyones
taste, but simplifies the implementation.
cheers,
felix