Com S 342 meeting -*- Outline -*-
* static properties of variables (2.3)
Motivation: now we move back from how to program
to the study of languages in general
Still using (subsets of) Scheme for examples, however.
** static vs. dynamic properties
------------------------------------------
STATIC VS. DYNAMIC PROPERTIES (2.3)
def: a property of a language is:
- *static* if
- *dynamic* if
Static properties are important for
------------------------------------------
... it can be checked (in general) before running the program
... it cannot be checked (in general) before running the program
... code improvement (so-called optimization),
for reasoning about correctness,
and for consistency checking (such as type checking).
Q: Why aren't the dynamic ones important?
** variable bindings, static (2.3.1)
------------------------------------------
THE LAMBDA CALCULUS (2.3.1)
lambda-1
Why?
- core of most programming languages
- only naming
Grammar:
::=
::= `
`` ::=
------------------------------------------
we'll call this lambda-1 to distinguish it from other versions,
the 1 refers to the 1-argument nature of the procedures
...
| (lambda (``) )
| ( )
Note change: a is a ``, but not vice-versa.
Q: Can you give some examples in this grammar?
Q: Can you write a derivation of (lambda (x) x) using this grammar?
The traditional syntax for the 2nd rule is with greek lambda and dot:
(\ `` . )
The meaning is like in Scheme,
think of (lambda (x) ...) as a declaration of x,
with value to be supplied by a call.
*** free and bound variable references
------------------------------------------
FREE AND BOUND VARREFS
((lambda (n) n) (car x))
def: a is *bound* in E if it
refers to
def: a is *free* in E if it
does not refer to a surrounding
formal parameter.
def: a variable x *occurs free* in E if
def: a variable x *occurs bound* in E if
E contains a bound named x.
Example:
((lambda (n) n) n)
^ ^
bound-/ \-free
------------------------------------------
Q: in the expression, what does n refer to? car? x?
Draw arrow from n to its declaration.
Be sure they understand "refers to" before going on to the formal
definitions
... a surrounding formal parameter
... E contains a free named x.
Q: So if n occurs free in a expression,
does that mean it doesn't occur bound?
------------------------------------------
FOR YOU TO DO
What are the (a) free, and
(b) bound variables in ...
(lambda (x) (lambda (y) x))
(f (cdr x))
(lambda (x) (f (cdr x)))
(lambda (f) (lambda (x) (f (cdr x))))
------------------------------------------
------------------------------------------
FORMAL DEFINITIONS
notation:
FV(E) = set of E's free variables
BV(E) = set of E's bound variables
def: x \in FV(E) if and only if either:
1. E is a varref, x
2. E is an application, (E1 E2), and
3. E is a lambda, (lambda (y) E), and
def: x in BV(E) if and only if either:
1. E is a varref and false
2. E is an application, (E1 E2), and
3. E is a lambda, (lambda (y) E), and
------------------------------------------
these are really the variables that *occur* free or bound
... x \in FV(E1) or x \in FV(E2)
... x \in FV(E) and x is different from y,
i.e., x \in FV(E) - {y}
... x \in BV(E1) or x \in BV(E2)
... x \in BV(E) or ( x is the same as y and y \in FV(E) )
Thus, y occurs bound in (lambda (y) E) if y \in FV(E)
------------------------------------------
MORE TERMINOLOGY
lexically bound =
globally bound =
dependency:
(lambda (ls) (car (car ls)))
closed expression, combinator =
SOME COMBINATORS
(lambda (n) n) ; I
(lambda (x) ; K
(lambda (y) x))
(lambda (x) ; S
(lambda (y)
(lambda (z)
((x z) (y z)))))
------------------------------------------
a varref is:
... bound to a formal parameter in a surrounding lambda
... bound at the top level
it's an error in Scheme if a variable isn't lexically or
globally bound
the lambda given is dependent on the value of car,
if that is changed, it does something different.
... a term with no free varrefs
interesting that these 3 combinators suffice to program
any computable function!
** scope and lexical address (2.3.2)
Now we're going to switch grammars a bit,
and allow multiple arguments to applications,
and multiple formals
------------------------------------------
VARIABLE DECLARATION REGIONS
!-----------------------------!
! (lambda (x) !
! !----------------------! !
! ! (lambda (y) ! !
! ! ! !
! ! (car (cons x y)) )!) !
! !----------------------! !
!-----------------------------!
(lambda (x)
(lambda (x)
(+ x 3) ) )
def: a variable declaration's *region* is
an area of the program's text that
includes the declaration, and within which
def: a variable declaration's *scope* is
that part of its region within which
------------------------------------------
- point out the regions, contours
- show how to draw arrows from varrefs to formal declarations
... the declaration *may* have effect (varrefs may refer to it)
... the declaration *does* have effect (varrefs will refer to it)
Note: region and scope refer to declarations, not variables.
however, sometimes people confuse the two,
(ok when the same variable isn't used in 2 declarations,
but can lead to confusion otherwise.)
motto: declarations have scope, not variables.
The following shows this
------------------------------------------
FOR YOU TO DO
(lambda (x)
(lambda (y)
((lambda (x) (x y)) x) ) )
1. draw arrows from each
to the `` declaration for it.
2. draw the contours.
3. What is the scope of each declaration?
TERMINOLOGY
hole in the scope =
------------------------------------------
... subarea of a var declaration's region
in which the var declaration has no effect.
point out the hole in the scope above.
Q: Is there a hole in the declaration of y's scope?
*** lexical depth
This is used in compilers to tell how many static links
to traverse to find the variable.
------------------------------------------
LEXICAL DEPTH
def: the lexical depth of a is
!-----------------------------!
! (lambda (x) !
! !----------------------! !
! ! (lambda (y) ! !
! ! ! !
! ! (car (cons x y)) )!) !
! !----------------------! !
!-----------------------------!
------------------------------------------
... the number of contours crossed in
going from the varref to its declaration.
Q: What's the lexical depth of y? x? cons? car?
*** lexical address
compilers don't want to deal with names for variables,
work better with numbers
what they use is a pair of numbers to access each variable:
lexical depth (number of static links),
and the lexical position (an offset within activation frame)
------------------------------------------
LEXICAL ADDRESS
identify each varref by 2 numbers:
d = lexical depth
p = position, counted from 0
format:
(v : d p)
^
\-------- var name
(lexical-address
(parse-lcm+if-exp 'cdr)) ==> (cdr : 0 0)
(lexical-address
(parse-lcm+if-exp
'(lambda (ls) (cdr ls))))
==> (lambda (ls)
((cdr : 1 0) (ls: 0 0)))
(lexical-address
(parse-lcm+if-exp
'(lambda (x)
(lambda (y)
(car (cons x y))))))
==> (lambda (x)
(lambda (y)
((car : ) ((cons : )
(x : )
(y : )))))
------------------------------------------
...
can erase the names,
unless needed for debugging (as in -g option of CC)
------------------------------------------
FOR YOU TO DO
What is?...
(lexical-address
(parse-lcm+if-exp
'(lambda (x) (lambda (y) x))))
(lexical-address
(parse-lcm+if-exp
'(lambda (x) (lambda (x) x))))
(lexical-address
(parse-lcm+if-exp
'(lambda (x) (lambda (z) x))))
(lexical-address
(parse-lcm+if-exp
'(lambda (a b c)
(lambda (f g)
(f (g a b) c a)))))
------------------------------------------
allowing more than one var in a lambda above,
and more than one expression in an application
this is as in homework
*** renaming variables (2.3.3)
The first program transformation rule this course,
we'll see several,
all useful in rewriting programs.
Q: do variable names really matter to a compiler?
What matters is the shape of an expression,
that is, what variables refer to what var declarations,
as in lexical-address computation
------------------------------------------
RENAMING FORMALS
Which are equivalent?
(lambda (x) (lambda (y) x)) ; a
(lambda (x) (lambda (x) x)) ; b
(lambda (x) (lambda (z) x)) ; c
(lambda (m) (lambda (y) m)) ; d
(lambda (m) (lambda (y) x)) ; e
Problems in renaming variables:
- capture, as in
- respecting inner bindings, as in
------------------------------------------
Q: What are their lexical address forms?
... changing y in a to x, producing b, instead of c
... changing x in a to m, producing e, instead of d
------------------------------------------
FOR YOU TO DO
Are these equivalent?
(lambda (x)
((lambda (x) (cons x '()))
(cons x '())))
and
(lambda (y)
((lambda (x) (cons y '()))
(cons y '())))
------------------------------------------
... no
`