Assignment 1 – Contracts

Assignment 1 – Contracts

Due Fri 4/7 11:59pm

1. Add one (non-dependent) contract-out specification for this function and write three calls to sum such that one blames this module, one blames the calling module and one produces a result without any blame. In the code below, change only the any/c into some other contract (plus add a (require (submod "." sum)) and some calls to sum).

  #lang racket
   
  (module sum racket
   
    (provide
     (contract-out
      [sum any/c]))
   
    (define (sum l)
      (cond
        [(empty? (rest l)) (first l)]
        [else (+ (first l)
                 (sum (rest l)))])))
   
  (require (submod "." sum))

2. Add a submodule and a dependent contract via contract-out that ensures that the results of the function below, f, are positive integers (the contract may disallow some inputs that the function does not crash on, but that is okay – just because something does not crash does not make it work) and that there can never be blame to the module containing f.
  #lang racket
   
  (define (f x y)
    (- x y))

3. The following contract does not capture g’s behavior. Come up with an example call to g that results in a contract violation blaming the following module (which contains g).

  #lang racket
   
  (module g racket
    (provide
     (contract-out
      [g (-> (-> natural? natural?)
             natural?
             natural?)]))
   
    (define (g f i)
      (f (- (f i)))))

4.

Not all calls to g trigger blame, however. Come up with a call to g that does not violate the contract.

5.

Come up with a new contract for g that is as general as you can, such that there are no arguments you can pass to g that will assign blame to g.

Submit a racket file with your solution via Canvas.