Skip to main content

Verifying preconditions

Summary: We consider the constraints we might place upon procedures and mechanisms for expressing those constraints.

Introduction: Implicit constraints

Several of the Scheme procedures that we have written or studied in preceding labs presuppose that their arguments will meet specific preconditions – constraints on the types or values of its arguments. For example, the following procedure assumes that its input is a list of cities in the standard zip-code format and that all of the cities have a longitude and latitude.

;;; Procedure:
;;;   northernmost-latitude
;;; Parameters:
;;;   cities, a nonempty list of cities in standard zip-first format
;;; Purpose:
;;;   Find the latitude of the northernmost city
;;; Produces:
;;;   lat, a real number
;;; Preconditions:
;;;   Each entry in cities is the form
;;;     (zip:string latitude:real longitude:real 
;;;      name:string state:string county:string)
;;; Postconditions:
;;;   * lat is the latitude of at least one city in cities.
;;;   * lat is greater than or equal to the latitudes of all the
;;;     cities in cities.
(define northernmost-latitude
  (lambda (cities)
    (reduce max (map1 cadr cities))))

If some careless programmer invokes northernmost-latitude and gives it, as an argument, the empty list, or a list in which one of the elements is not a standard city entry, or perhaps even some Scheme value that is not a list at all, the computation that the definition of northernmost-latitude describes cannot be completed.

> (northernmost-latitude (list))
. . cdr: contract violation
  expected: pair?
  given: '()
> (northernmost-latitude (list 1))
. . cadr: contract violation
  expected: (cons/c any/c pair?)
  given: 1
> (northernmost-latitude 'boston)
. . map1: contract violation
  expected: list?
  given: 'boston
  in: the 2nd argument of
      (->
       (-> any/c any)
       (listof any/c)
       (listof any/c))
  contract from: <pkgs>/csc151/lists.rkt
  at: <pkgs>/csc151/lists.rkt:14.5

As you can see, none of these error messages are particularly helpful. Whose responsibility is it to handle these types of errors? As we will see, it is possible to share responsibility between the person who writes a procedure and the person who calls a procedure.

Procedures as contracts

A procedure definition is like a contract between the author of the definition and someone who invokes the procedure. The postconditions of the procedure are what the author guarantees: When the computation directed by the procedure is finished, the postconditions shall be met. Usually the postconditions are constraints on the value of the result returned by the procedure. For instance, the postcondition of the square procedure,

(define square
  (lambda (val)
    (* val val)))

is that the result is the square of the argument val. (Alternately, we might say that the square root of the result is val.)

The preconditions are the guarantees that the invoker of a procedure makes to the author, the constraints that the arguments shall meet. For instance, it is a precondition of the square procedure that val is a number.

If the invoker of a procedure violates its preconditions, then the contract is broken and the author’s guarantee of the postconditions is void. (If val is, say, a list or a drawing, then the author can’t very well guarantee to return its square. What would that even mean?) To make it less likely that an invoker violates a precondition by mistake, it is usual to document preconditions carefully and to include occasional checks in one’s programs, ensuring that the preconditions are met before starting a complicated computation.

Many of Scheme’s primitive procedures have such preconditions, which they enforce by aborting the computation and displaying a diagnostic message when the preconditions are not met:

> (/ 1 0)
. . /: division by zero
> (log 0)
. . /: log: undefined for 0 
> (+ 1 'two)
. . +: contract violation
  expected: number?
  given: 'two
  argument position: 2nd
  other arguments...:
> (length 116)
. . length: contract violation
  expected: list?
  given: 116

Generating explicit errors

To enable us to enforce preconditions in the same way, most implementations of Scheme provides a procedure named error, which takes a string as its first argument. Calling the error procedure aborts the entire computation of which the call is a part and causes the string to be displayed as a diagnostic message.

For instance, we could enforce northernmost-latitude’s precondition that its parameter be a non-empty list of cities in the standard zip code format by rewriting its definition thus:

(define northernmost-latitude
  (lambda (cities)
    (when (or (not (list? cities))
              (null? cities)
              (not (all-cities? cities)))
      (error "northernmost-latitude: expects a non-empty list of cities"))
    (reduce max (map1 cadr cities))))

In this definition, we predicate that takes any list as its argument and determines whether or not all of the elements of that list are cities. We will have to write that procedure soon.

Now the northernmost-latitude procedure enforces its preconditions

> (northernmost-latitude (list))
. . northernmost-latitude: expects a non-empty list of cities
> (northernmost-latitude (list 1))
. . northernmost-latitude: expects a non-empty list of cities
> (northernmost-latitude 'boston)
. . northernmost-latitude: expects a non-empty list of cities

Of course, while these error messages are better than the original error messages, they don’t tell us the complete story. In particular, they don’t tell us what the value of the incorrect parameter is. Fortunately, error can take additional parameters, which it presents verbatim.

(define northernmost-latitude
  (lambda (cities)
    (when (or (not (list? cities))
              (null? cities)
              (not (all-cities? cities)))
      (error "northernmost-latitude: expects a non-empty list of cities, given "
             cities))
    (reduce max (map1 cadr cities))))
> (northernmost-latitude (list))
. . northernmost-latitude: expects a non-empty list of cities, given  ()
> (northernmost-latitude (list 1))
. . northernmost-latitude: expects a non-empty list of cities, given  (1)
> (northernmost-latitude 'boston)
. . northernmost-latitude: expects a non-empty list of cities, given  boston

Husks and kernels

Including precondition testing in your procedures often makes them markedly easier to analyze and check, so we recommend the practice, especially during program development. There is a trade-off, however: It takes time to test the preconditions, and that time will be consumed on every invocation of the procedure. Since time is often a scarce resource, it makes sense to save time by skipping the test when you can prove that the precondition will be met. This often happens when you, as programmer, control the context in which the procedure is called as well as the body of the procedure itself.

For example, in the preceding definition of northernmost-latitude, although it is useful to test the precondition when the procedure is invoked “from outside” by a potentially irresponsible caller, if we are only using it as a helper to another procedure that verifies that it has a list of cities, it is a waste of time to repeat the potentially expensive tests.

One solution to this problem is to replace the definition of northernmost-latitude with two separate procedures, a “husk” and a “kernel”. The husk interacts with the outside world, performs the precondition test, and launches the recursion. The kernel is supposed to be invoked only when the precondition can be proven true; its job is to perform the main work of the original procedure, as efficiently as possible:

(define northernmost-latitude
  (lambda (cities)
    (when (or (not (list? cities))
              (null? cities)
              (not (all-cities? cities)))
      (error "northernmost-latitude: expects a non-empty list of cities, given "
             cities))
    (northernmost-latitude-kernel cities)))

(define northernmost-latitude-kernel
  (lambda (cities)
    (reduce max (map1 cadr cities))))

The kernel has the same preconditions as the husk procedure, but does not need to enforce them, because we invoke it only in situations where we already know that the preconditions are satisfied.

The one weakness in this idea is that some potentially irresponsible caller might still call the kernel procedure directly, bypassing the husk procedure that he’s supposed to invoke. In a subsequent reading and lab, we’ll see that there are a few ways to put the kernel back inside the husk without losing the efficiency gained by dividing the labor in this way.

While the benefits of this approach may not immediately be obvious, when you start to write procedures the step through the elements of a list, you will find it helpful to avoid revisiting all of the elements of the list at every step.

Improving error messages

Are we done? Mostly. However, instead of giving the same error message for every type of error, we might customize error messages for the particular kind of error, giving a different error message in each case. The drawings-leftmost procedure is perhaps not the best example, because all three kinds of errors are essentially a failure to provide a non-empty list of drawings, but we’ll use it as a demonstration anyway.

(define northernmost-latitude
  (lambda (cities)
    (cond
      [(not (list? cities))
       (error "northernmost-latitude: requires a non-empty list of cities, received a non-list: " cities)]
      [(null? cities)
       (error "northernmost-latitude: requires a non-empty list of cities, received the empty list")]
      [(not (all-cities? cities))
       (error "northernmost-latitude: requires a non-empty list of cities, received a list that includes at least one value in the wrong format: " cities)]
      [else
       (northernmost-latitude-kernel cities)])))

(define northernmost-latitude-kernel
  (lambda (cities)
    (reduce max (map1 cadr cities))))

Now, our messages are a bit more informative.

> (northernmost-latitude (list))
. . northernmost-latitude: requires a non-empty list of cities, received the empty list
> (northernmost-latitude (list 1))
. . northernmost-latitude: requires a non-empty list of cities, received a list that includes at least one value in the wrong format:  (1)
> (northernmost-latitude 'boston)
. . northernmost-latitude: requires a non-empty list of cities, received a non-list:  boston

Here’s a pattern we often use when we include precondition checking in our procedures.

(define safe-procedure
  (lambda (parameters)
    (cond
      [(precondition-guard-1)
       (error "failed first precondition" parameters)]
      [(precondition-guard-2)
       (error "failed second precondition" parameters)]
      ...
      [(precondition-guard-n)
       (error "failed last precondition" parameters)]
      [else
       (procedure-kernel parameters)])))

Self Checks

Check 1: Precondition roots

In an earlier class session, you may have written 6P documentation for a quadratic root finding procedure similar to the following:

;;; Procedure
;;;  quadratic-root
;;; Parameters:
;;;   a, a real number
;;;   b, a real number
;;;   c, a real number
;;; Purpose:
;;;   Find the first root of the polynomial ax^2+bx+c
;;; Produces:
;;;   root, a real number
;;; Preconditions:
;;;   FORTHCOMING
;;; Postconditions:
;;;   root = ( -b + sqrt(b^2-4*ac) ) / (2*a)
(define quadratic-root
  (lambda (a b c)
    (/ (+ (- b) 
          (sqrt (- (* b b) (* 4 a c))))
       (* 2 a))))

a. What are the preconditions for quadratic-root? Document them in the 6Ps.

b. Following a pattern similar to safe-procedure, add a cond statement to quadratic-root and verify its preconditions.