Almost every programming language has:
If the same name x is used for several different variables, a programmer can resolve which variable is really meant by a particular use of the name x.For example, in Java a loop header may introduce a new loop variable and its scope:
for(int i = 0; i < NumOfElements; i++) ... ;
The scope of the variable i
is the program text following
the =
all the way to the end of the loop body. If the
loop contains a break construct and if the loop is followed by some
statement like
System.out.println("the index where we stopped searching is " + i);then we know from the scoping rules for Java that the program is almost certainly erroneous. Since the use of
i
in the invocation
of println
does not refer to the variable
i
that was introduced as the loop variable,
the reference to variable i
in the print statement either
is illegal or refers to a variable with a scope that encloses the loop
and the subsequent print statement.
Note: We analyzed the preceding program fragment with a rough understanding of its execution semantics but with a precise understanding of the scoping rules. These rules are crucial for building a good mental execution model.
Other examples of scoping constructs in Java and C++ include
Let's call the toy language from last lecture LC (for Lambda Calculus) Using LC, we can show how to define the notion of free and bound occurrences of program variables rigorously. This specification will serve two purposes:
Recall the surface syntax of LC:
Exp = Var | Num | (lambda (Var) Exp) | (Exp Exp) Var = Sym \ {lambda}
If we interpret LC as a sub-language of Scheme, it contains only one
binding construct: lambda
-expressions. In
(lambda (a-variable) an-expression)
a-variable is introduced as a new, unique variable whose
scope is the body an-expression
of the lambda
-expression (with the
exception of possible "holes", which we describe in a moment). We have
included parentheses around the parameter of a lambda expression
(a-variable in the example above) to make LC syntax
consistent with Scheme syntax.
One way to determine the scope of variable binding constructs is to define when some variable occurs free in some expression. By specifying where a variable occurs free and how this relation is affected by the various constructs, we also define the scope of variable definitions. Here is the definition for LC.
Definition (Free occurrence of a variable in LC):
Let x,y range over the elements of
Var
.
Let M,
N range over the elements of
Exp
.
Then x occurs free in:
(lambda (y) M)
if x != y and x occurs free in
M.
(M N)
if it occurs free either in M or in N.
If x occurs free in M, then some specific occurrence of x in M can be identified as a "free occurrence". In fact, we could adapt the preceding definition to define the related notion "occurrence k of x is free in M", but the details are tedious because we must label each occurrence of x with a unique index k, or alternatively, we must uniquely identify each occurrence by a path from the root in the abstract syntax tree for M.
An occurrence of variable x is bound in
M iff it is not free in
M. The most blatant form of bound occurrence
is the second component of a lambda
-expression. This
form of variable occurrence is called the binding occurrence
for the variable. Each distinct variable obviously has one
binding occurrence; if x occurs twice as the
second component of a lambda
-expression, it is the name
of two distinct variables.
Note that the definition of the "occurs free" and "free occurence"
relations are inductive over expressions
precisely because the definition of Exp
is itself
inductive.
The definitions of free and bound implicitly determine the notion of a
scope in LC expressions. Clearly, a lambda
expression
opens a new scope; or, to put it differently, the scope of the binding
occurrence x in
(lambda (x) M)is that textual region of M where x might occur free.
Given a variable occurrence it is natural for a programmer to ask where the binding occurrence is. This may tell him whether or not some nearby variable occurrence is related, or it may help him to determine something about the value that it stands for. Consider the expression
(lambda (z) (lambda (x) ((lambda (x) (z (z (z x)))) x)))
The two occurrences of x in the fragment
... x)))) x)
are unrelated, but only the binding occurrences for the two can tell them apart.
For a human being, a representation that includes arrows from bound occurrences to binding occurrences is clearly preferable. We can approximate such graphical representations of programs by replacing variable occurrences with numbers that indicate how far away in the surrounding context the binding construct is. The above expression would translate into
(lambda (z) (lambda (x) ((lambda (x) (3 (3 (3 1)))) 1)))
Indeed, since the parameters in lambda
's are now
superfluous, we can omit them completely:
(lambda (lambda ((lambda (3 (3 (3 1)))) 1)))
This representation is often called either the static distance representation of the term or the "deBruijn (de BROIN) notation" for the term. deBruijn is a Dutch mathematician who recognized the theoretical advantages of the notation in the context of idealized programming language called the lambda-calculus, which was invented by Alonzo Church in the 1930's. LC is a slight extension of the lambda-calculus. Although the static distance representation is not particularly helpful for people, it is valuable for compilers and interpreters.
We could specify the process that replaces variable occurrences with static distances in English along the lines of the above definitions. Instead, we write a program that performs the translation.
First, recall the abstract representation of the set of LC expressions is given by the equation:
AR = (make-var Var) | (make-const Num) | (make-proc Var AR) | (make-app AR AR)based on the data definitions
(define-struct var (symbol)) (define-struct const (number)) (define-struct proc (var body)) (define-struct app (rator rand))
Second, the program template for abstract representations is clearly
(define fAR (lambda (an-ar) (cond ((var? an-ar) ...) ((const? an-ar) ...) ((proc? an-ar) ... (fAR ... (proc-body an-ar)) ... (proc-var an-ar) ...) ((app? an-ar) ... (fAR (app-rator an-ar)) ... ... (fAR (app-rand an-ar)) ...))))
Since the replacement process substitutes a variable by a number that depends on the context of the variable, that is, the syntactic constructions surrounding the variable occurrence, we also need an accumulator. In our case, it suffices to accumulate the variables in binding constructs as we traverse the expression. This means the template can be refined to:
(define fAR (lambda (an-ar binding-vars) (cond ((var? an-ar) ...) ((const? an-ar) ...) ((proc? an-ar) ... (fAR (proc-body an-ar) (cons (proc-var an-ar) binding-vars)) ...) ((app? an-ar) ... (fAR (app-rator an-ar) binding-vars) ... ... (fAR (app-rand an-ar) binding-vars) ...))))
Finally, to distinguish the initial abstract representation of "deBruijn" programs from the new one, we introducce two new data definitions:
(define-struct sdvar (sdc)) (define-struct sdproc (body))
and define the set of SD programs by the equation:
SD = (make-sdvar sdc) | (make-num Num) | (make-sdproc SD) | (make-app AR AR)We do not introduce new records for constants or applications, because their structure is unchanged.
We can now complete the translation:
(define sd (lambda (an-ar binding-vars) (cond ((var? an-ar) (make-sdvar (sdlookup (var-name an-ar) binding-vars))) ((const? an-ar) an-ar) ((proc? an-ar) (make-sdproc (sd (proc-body an-ar) (cons (proc-var an-ar) binding-vars)))) (else (make-app (sd (app-rator an-ar) binding-vars) (sd (app-rand an-ar) binding-vars))))))where
(define sdlookup (lambda (a-var vars) (cond ((null? vars) (error 'sdlookup "free occurrence of ~s" a-var)) (else (if (eq? (car vars) a-var) 1 (add1 (sdlookup a-var (cdr vars))))))))
Variables are replaced by their static distance coordinate, which is determined by looking up how deep in the list of binding vars it occurs. If the list does not contain the variable, we signal an error. Constants do not need to be translated. Applications are traversed and re-constructed.
We can test sd by applying it to the result of parsing some surface syntax and the empty list.
(sd (parse '(lambda (z) (lambda (x) ((lambda (x) (z (z (z x)))) x)))) null)
(The empty list of variables indicates that we consider this to be the complete program with no further context.)
cork@cs.rice.edu/ (adapted from shriram@cs.rice.edu)