[plt-scheme] Dynamic Contracts

From: Chris Warrington (gcw5217 at rice.edu)
Date: Fri Jul 21 12:27:16 EDT 2006

Does contract.ss support what I want to call "dynamic contracts?"

Consider this function:
; make-list : natural-number X -> [X]
; Creates a list of the given length in which each element
; is the provided element.
(define (make-list count val) ...)

Using provide/contract (yes, I'm doing this in a module), I can attach a simple
contract to this:
(provide/contract [make-list (natural-number/c any/c . -> . (listof any/c))])

However, this contract can be made stronger. For example, the length of the list
should be equal to count. However, I can't find a good way to do this.
Thus far, the best I have is this:

; dc:make-list
; Dynamic contract for make-list
(define (dc:make-list count val)
  ((contract (natural-number/c any/c . -> . (and/c (listof any/c)
                                                   (λ (lst) (= (length lst)
   count val))

Is there an easier way to do these "dynamic contracts?" How would one go about
enforcing the type contraint in this case (i.e., the list should be of the same
type as val)?

Chris Warrington <chrisw at rice.edu>
(away from his normal computer)

Posted on the users mailing list.