next up previous
Next: Complications Up: Scoping affects how we Previous: Scoping affects how we

Making sense of scope

If the language design and implementation does not correspond to what you had in mind (or worse, forbids you from expressing what you had in mind) there is a problem! How can a language specify these settings?

In the examples below we use Scheme syntax. Scheme is a dialect of Lisp, and so you are all probably unfamiliar with it. Since this is unknown, I hope it will be neutral and you won't impose prior knowledge of C or Fortran on it. (If you are familiar with Scheme, I hope you will forgive the minor abuses I commit).

ALTERNATIVE 1

Total lexical model

define fn in lexical scope 1

    (let ((roundingmode nearest))
       (define (fn x)  <body of f>) 
        )

define gz in lexical scope 2

    (let ((roundingmode zero))
       (define (gz x) ... (fn x) )
         ;; gz calls fn
         ;; where rounding is to nearest
      )

Disadvantage of this:

Say you want to re-execute the exact same body of code twice, the first time through jiggling the rounding up, and the second time through, jiggling the rounding down.

[Why? You might try to relate this to sensitivity of the computation to rounding errors; see WK notes/Borneo spec for polynomial evaluation with upper and lower bounds.]

Then you have to plan ahead, and you could definitely not use fn above. Here's a way. You would invoke the function something like this. Define a new function frm meaning ``f with rounding mode'' and then use: (frm x ToNEGV) to compute f with rounding down, (frm x ToPOSV) to round up. Here's how:

(define (frm  x rm)
  (let ((roundingmode rm))
     <body of f>))

ALTERNATIVE 2

Total dynamic model; clumsier, but certainly a possibility

(define (f x) ...) ; f will use whatever rounding is globally set

   (define (h x) ... 
     (set old_rounding_mode rounding_mode)  ;save current mode
     (set rounding_mode nearest)
     (f x) ...
     ;;this is computed in new rounding mode
     (set rounding_mode old_rounding_mode)  ;restore mode
    ...
     )
If you have all names in a single space (A typical assembler view of the world) and a single process model, you could presumably write all the tedious code necessary to save and restore modes. The point of scoping rules is to make the normal and useful behavior easier and less error-prone. We don't like Alternative 2, but it is better than nothing.


next up previous
Next: Complications Up: Scoping affects how we Previous: Scoping affects how we

Richard J. Fateman
Wed Aug 12 22:53:48 PDT 1998