[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ``Update functions'' in Scheme.

Following the discussion on assignments in Scheme, and how to have them
performed in subroutines, the following question strikes my mind:

*** Why didn't the designers of Scheme include locations (~anonymous variables)
as first-class values in the language? ***

Wouldn't that fit the Scheme philosophy of orthogonality and 'first-class
everything'? After all, locations are part of the denotational semantics of
Scheme, so this wouldn't be much of an extension to Scheme from an abstract
point of view.
A variable would then be an identifier bound to a location, and we would have
true constants as well, by binding identifiers to values that are not
locations. All identifier bindings would then be irreversible, only locations
could be updated (thus indirectly updating identifiers bound to them).

If that sounds too abstract, consider the following hypothetical program

(let ((x (var 1))               ; VAR creates a new location and
      (y 1))                    ;   initializes it to its argument
    (set! x 2)
    (set! x (+ val x) 2))       ; VAL looks up the current value of x
    (set! x (+ y 2)))           ; y can't be set! and needn't be val'ed

The cons function would have to create a pair of locations and initialize them
to its arguments. (So that we could make sense of setcar!)

We could now also write a 'swap' function as in

(define (swap x y)                 ; actual arguments must be variables
    (let ((h (val x)))
        (set! x (val y))
        (set! y h)))

This would not violate the 'call by value' principle, since locations would
_be_ values. First-class variables shouldn't give us any more headaches than
side-effects per se. Separating constants from variables should even simplify
some compiler optimizations (for the constants).
For the very least, we would get rid of having to write macros or extend the
syntax in order to abstract over assignments.

Any takers?

Gerald				"When I use a word, it means
<gerald@grn.umb.edu>		 exactly what I want it to mean"
				 (Humpty Dumpty, Through the Looking Glass)