You are looking at historical revision 33776 of this page. It may differ significantly from its current revision.

Design by contract for procedures

This is my third attempt to implement Bertrand Meyer's Design by contract in Chicken. The other two, contracts and dbc are now considered obsolete.

We enhance arguments and return values of lambda-expressions by pre- and postconditions respectively, thus marking a clear distinction between the duties of suppliers and clients. If the arguments of a procedure call pass the preconditions and a post-exception is raised, then the supplier is to blame. On the other hand, if a pre-exception is raised when a procedure-call is issued, then the client has violenced its duties and the supplier is not even forced to do anything at all. In other words, the supplier can safely assume a procedure is called with correct arguments, he or she need not and should not check tehem again.

Off course, pre- and postconditions must be stored in the procedure itself and a representation of them must be exportable, so that both parties of the contract know their duties. Here is the syntax of xdefine, a macro to implement queries, i.e. routines without state changes.

(xdefine ((r r? ...) ... name (a a? ...) ... [as as? ...]) xpr ....)

where name is the name of the procedure, r ... are retrurn values with corresponding postconditions r? ..., a ... are fixed variables with preconditions a? ... and as is an optional variable argument with preconditions as? ...

For state-changing routines, so called commands, xlambda must be used. The syntax is

(xlambda k ((r r? ...) ... (a a? ...) ... [as as? ...]) xpr ....)
(xlambda ((r r? ...) (a a? ...) ... [as as? ...]) xpr ....)

where k is the number of return values, if provided, 1 otherwise. xlambda can be defined on top of a let, thus supplying state. But even those routines must return values, namely the values of state variables before a state change has taken place. So one can check, if the state change did what it should have done.

Note, that a parameter, contract-check-level, is supplied, so that one can always control what to check, nothing, only preconditions or pre- and postconditions. Only precondition check is the default.

Documentation

simple-contracts

[procedure] (simple-contracts sym ..)

documentation procedure. Shows the exported symbols and the syntax of such an exported symbol, respectively.

contract-check-level

[parameter] (contract-check-level n ..)

no contract checks if n is -1 only precondition checks if n is 0, the default pre- and postcondition checks if n is +1

xdefine

[syntax] (xdefine ((r r? ...) ... name (a a? ...) ... [as as? ...]) . body)

contract guarded version of define for procedures, where name is the name of the procedure r ... are return values with corresponding postcondition r? a ... are fixed arguments with preconditions a? ... as is an optional variable argument with preconditions as? ...

xlambda

[syntax] (xlambda k ((r r? ...) ... (a a? ...) ... [as as? ...]) . body)
[syntax] (xlambda ((r r? ...) (a a? ...) ... [as as? ...]) . body)

contract guarded version of lambda, where k is the number of returned values r ..., if provided, 1 otherwise, r? ... their corresponding postconditions, a ... are fixed arguments with preconditions a? ..., as is an optional variable argument with preconditions as? ...

procedures with state change should return old versions of state variables before the state change

%%

[procedure] (%% proc)

multi argument version of flip, which can be used in pipe

pipe

[syntax] (pipe combination ...)

sequencing curried combinations

Requirements

simple-exceptions

Usage

(use simple-contracts)

Examples


(use simple-contracts)

((%% list)) ; -> '()
((%% list) 1) ; -> '(1)
((%% list) 1 2) ; -> '(2 1)
((%% list) 1 2 3 4) ; -> '(2 3 4 1)
((%% map) '(0 1 2) add1) ; -> '(1 2 3)

((pipe) 5) ; -> 5
((pipe (+ 1)) 5) ; -> 6
((pipe (- 10) (positive?)) 5) ; -> #f
((pipe (- 10) (negative?)) 5) ; -> #t
((pipe ((%% list) 1 2 3 4)) 10) ; -> '(1 2 3 4 10)
((pipe (list 1 2 3 4)) 10) ; -> '(10 1 2 3 4))
((pipe ((%% map) add1)) '(0 1 2)) ; -> '(1 2 3))
((pipe (list 1 2 3)) 0) ; - > '(0 1 2 3)) 
((pipe ((%% list) 1 2 3)) 0) ; - > '(1 2 3 0)) 

(define-values (counter! counter)
  (let ((state 0))
    (values
      (xlambda 1 ((old (pipe (+ 1) (= state))))
        (let ((o state))
          (set! state (add1 state))
          o))
      (xlambda 1 ((result (pipe (= state))))
        state))))
(counter) ; -> 0
(counter!)
(counter)  ; -> 1
(counter!)
(counter)  ; -> 2

(define-values (add add-pre add-post)
  (xlambda ((result integer? odd? (pipe (= (apply + x y ys))))
            (x integer? odd?) (y integer? even?) ys integer? even?)
    (apply + x y ys)))
(add 1 2 4 6) ; -> 13
(condition-case (add 1 2 3) ((exn arguments) #f)) ; -> #f
add-pre
 ; -> '((x (conjoin integer? odd?))
        (y (conjoin integer? even?))
        ys (conjoin integer?  even?))
add-post
 ; -> '(result (conjoin integer? odd? (pipe (= (apply + x y ys)))))

(define wrong-add
  (xlambda ((result integer? even?)
            (x integer? odd?) xs integer?  even?)
    (apply + x xs)))
(condition-case (wrong-add 1 2 4) ((exn results) #f)) ; -> #f

(define-values (euclid euclid-pre euclid-post)
  (xlambda 2 ((q integer?)
              (r (pipe (+ (* n q))
                       (= m)))
              (m integer? (pipe (>= 0)))
              (n integer? positive?))
    (let loop ((q 0) (r m))
      (if (< r n)
        (values q r)
        (loop (+ q 1) (- r n))))))
(call-with-values
  (lambda () (euclid 385 25))
  list)
  ; -> '(15 10)
euclid-pre
 ; - > '((m (conjoin integer? (pipe (>= 0))))
         (n (conjoin integer? positive?)))
euclid-post
 ; -> '((q integer?)
        (r (pipe (+ (* n q)) (= m))))

(xdefine ((result integer?) sum (a integer?) as integer?)
  (apply + a as))
(sum 1 2 3) ; -> 6
(condition-case (sum 1 2 #f) ((exn arguments) #f)) ; -> #f

(xdefine ((result list?) wrong-sum (a integer?) as integer?)
  (apply + a as))
(condition-case (wrong-sum 1 2 3) ((exn results) #f)) ; -> #f

Last update

Dec 30, 2016

Author

Juergen Lorenz

License

Copyright (c) 2011-2016, Juergen Lorenz
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the author nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission. 
  
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Version History

1.2
<<, >>, pre- and post-exception outsourced to simple-exceptions
1.1
number of return values in xlambda is only needed if not 1
1.0
initial import