[plt-scheme] contracts and structure subtypes?

From: Richard C. Cobbe (cobbe at ccs.neu.edu)
Date: Sun May 23 15:00:08 EDT 2004

Greetings, all.

I'm trying to get the contract system to work in conjunction with
structure subtypes, and I'm running into difficulties.  Consider the
following module:

(module foo mzscheme
  
  (require (lib "contract.ss"))
  
  (define-struct base (x y))
  (define-struct (derived base) (z))
  
  (provide/contract
   (struct base ([x integer?] [y boolean?]))
   (struct derived ([z string?]))))

I don't seem to be able to apply derived's constructor:

Welcome to DrScheme, version 207.1-cvs23may2004.
Language: Pretty Big (includes MrEd and Advanced).
> (require foo)
> (make-base 3 #f)
#<struct:base>
> (make-derived 3 #f "foo")
6.3: top-level: foo broke make-derived's contract: (-> string? derived?): expected a procedure that accepts 1 arguments, given: #<struct-procedure:make-derived>

Adding the inherited fields to derived's contract doesn't work any
better (although this isn't surprising):

(module foo mzscheme
  
  (require (lib "contract.ss"))
  
  (define-struct base (x y))
  (define-struct (derived base) (z))
  
  (provide/contract
   (struct base ([x integer?] [y boolean?]))
   (struct derived ([x integer?] [y boolean?] [z string?]))))

Welcome to DrScheme, version 207.1-cvs23may2004.
Language: Pretty Big (includes MrEd and Advanced).
expand: unbound variable in module in: derived-x
>

Is there some way to make the contracts do the right thing?  Or can I
simply not use the (struct ...) form for structures that inherit fields?
Having to write the contracts for all of the relevant functions is
possible but awfully annoying.

So far as I can tell, the MzLib manual doesn't address this, and
Google's let me down as well.  Any ideas?

Thanks,

Richard


Posted on the users mailing list.