Thinking more about the functional context, a macro — say (define/pre-post
...) that cleanly defined the following functions would be pretty sweet
(continuing with the real-sqrt example):

   - real-sqrt-unsafe
   - real-sqrt-with-pre-conditions
   - real-sqrt-with-pre-and-post-conditions
   - real-sqrt (make-parameter one-of-the above)

and of course plugging into the contract system.

Example sketch of use and expansion below ...

Dan



*Example ...*

(define/pre-post (real-sqrt x)
  #:pre ([(real? x) "real argument expected"]
         [(>= x 0) "non-negative argument expected"])

  #:implementation (sqrt x)

  #:post ([(implies (= x 0) (= result 0)) "The sqrt of zero should be zero"]
          [(implies (> x 0) (> result 0)) "Positive numbers have positive
square-roots"]
          [(implies (> x 0) (<= (abs (- x (* result result))) 0.0000001))
           "result * result = x (to within error)"]))


*Expansion (not yet using the contract system): *

(define (real-sqrt-unsafe x)
  (sqrt x))


(define (real-sqrt-with-pre-conditions x)
  (pre [(real? x) "real argument expected"]
       [(>= x 0) "non-negative argument expected"])

  (real-sqrt-unsafe x))


(define (real-sqrt-with-pre-and-post-conditions x)
  (define result (real-sqrt-with-pre-conditions x))

  (post [(implies (= x 0) (= result 0)) "The sqrt of zero should be zero"]
        [(implies (> x 0) (> result 0)) "Positive numbers have positive
square-roots"]
        [(implies (> x 0) (<= (abs (- x (* result result))) 0.0000001))
         "result * result = x (to within error)"])
  result)


(define real-sqrt (make-parameter real-sqrt-with-pre-and-post-conditions))

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to