Essentials of Programming Languages - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Essentials of Programming Languages

Description:

A variable x occurs free in a lambda calculus expression E if and only if. 1) E is a variable reference and E is the same as x; or ... – PowerPoint PPT presentation

Number of Views:104
Avg rating:3.0/5.0
Slides: 18
Provided by: davidwoo4
Category:

less

Transcript and Presenter's Notes

Title: Essentials of Programming Languages


1
Essentials of Programming Languages
  • Section 1.3

2
Scoping and Binding of Variables
  • Variables in Scheme appear in two different
    ways
  • 1) References x in the expression (f x y)
  • 2) Declarations x in the expressions
  • (lambda (x) )
  • (let (x ) )
  • The value named by a variable is its denotation
  • The denotation comes from a declaration
  • The variable is bound by the declaration

3
Lexical Scoping in Scheme
  • Declarations in Scheme have a limited scope (the
    area in which the variable is bound)
  • For lambda expressions the scope of a variable is
    the body of the lambda expression
  • For let expressions, the scope of a variable is
    the let body
  • (let (( )
  • ( )
  • ...
  • ( ))
  • )

4
Static Binding
  • (let ((x 3))
  • (let ((y x)
  • (x 5))
  • (display x)))
  • Scope can be determined by analyzing the text
  • Lisp uses dynamic scoping binding is determined
    during execution

5
Lambda Calculus Expressions
  • (lambda () )
  • ( )
  • Binding rule for Lambda Calculus expressions In
    (lambda () ), the
    occurrence of is a declaration that
    binds all occurrences of that variable in
    unless some intervening declaration
    of the same variable occurs

6
Free Variables
  • A variable x occurs free in E if and only if
    there is some use of x in E that is not bound by
    any declaration of x in E
  • In ((lambda (x) x) y), y occurs free
  • The variable y occurs free in
  • (define foo
  • (lambda (x)
  • ( x y)))

7
Bound Variables
  • A variable x occurs bound in an expression E if
    and only if there is some use of x in E that is
    bound by a declaration of x in E
  • The variable x is bound in
  • ((lambda (x) x) y)
  • The variable x is bound in
  • (let ((x 3) (y 4)) ( x y z))

8
Free Variables in Lambda Calculus Expressions
  • A variable x occurs free in a lambda calculus
    expression E if and only if
  • 1) E is a variable reference and E is the
    same as x or
  • 2) E is of the form (lambda (y) E), where y
    is different from x and x occurs free in E or
  • 3) E is of the form (E1 E2) and x occurs
    bound in E1 or E2

9
Occurs-free?
(lambda (y) y) (cdr (lambda (y) x)) ? ((y) x) (
cadr (lambda (y) x))?(y) (caadr (lambda (y) x)
? y
(caddr (lambda (y) x) ? x
  • (define occurs-free?
  • (lambda (var exp)
  • (cond
  • ((symbol? exp) (eqv? exp var))
  • ((eqv? (car exp) 'lambda)
  • (and (not (eqv? (caadr exp) var))
  • (occurs-free? var (caddr exp))))
  • (else (or (occurs-free? var (car exp))
  • (occurs-free? var (cadr
    exp)))))))
  • (occurs-free? 'x 'x)
  • (occurs-free? 'x '(lambda (x) x))
  • (occurs-free? 'x '(lambda (y) x))

Rule 1 Rule 2 Rule 3
10
Bound Variables in Lambda Calculus Expressions
  • A variable occurs bound in a lambda calculus
    expression E if and only if
  • 1) E is of the form (lambda (y) E), where x
    occurs bound in E or x and y are the same
    variable and y occurs free in E or
  • 2) E is of the form (E1,E2) and x occurs
    bound in E1 or E2

11
Occurs-bound?
  • (define occurs-bound?
  • (lambda (var exp)
  • (cond
  • ((symbol? exp) f)
  • ((eqv? (car exp) 'lambda)
  • (or (occurs-bound? var (caddr exp))
  • (and (eqv? (caadr exp) var)
  • (occurs-free? var (caddr
    exp)))))
  • (else (or (occurs-bound? var (car exp))
  • (occurs-bound? var (cadr
    exp)))))))
  • (occurs-bound? 'x 'x)
  • (occurs-bound? 'x '(lambda (x) x))
  • (occurs-bound? 'x '(lambda (y) x))

12
Scope and Lexical Address
  • Given a declaration, which variable references
    refer to it?
  • The scope of x in (lambda (x) ) is the body of
    the lambda expression
  • The scope of x in (define x ) is the whole
    program
  • Nesting of blocks complicates the scope rules

13
Nesting of Blocks
  • (define x call this x1
  • (lambda (x) call this x2
  • (map
  • (lambda (x) call this x3
  • ( x 1)) refers to x3
  • x))) refers to x2
  • (x '(1 2 3)) refers to x1

14
Scope and Lexical Address
  • Scope of a variable declaration is the text
    within which references to the variable refer to
    the declaration
  • Shadowing occurs when the inner declaration of a
    variable takes precedence over an outer
    declaration of the same variable
  • The inner declaration of a variable creates a
    hole in the scope of an outer variable with the
    same name

15
Contour Diagrams
  • Borders of regions that denote scope are called
    contours
  • (lambda (z)
  • ((lambda (a b c)
  • (a (lambda (a) ( a c) ) b ) )
  • (lambda (f x) (f (z x)) )) )

16
Lexical Addresses
  • The declarations associated with a region may be
    numbered in the order of their appearance in the
    text. Each variable reference may then be
    associated with two numbers its lexical depth
    and the position of its declaration in the
    declaring contour (its declaration position)
  • Taken together, these two numbers are the lexical
    address of the variable reference. Thus a
    variable reference v in an expression, may be
    replaced by (v d p), where d is its lexical
    depth and p is its declaration position, (using
    zero-base indexing for both d and p)
  • Note that lexical depth is measured from the
    declaration which is depth 0

17
Lexical Addresses (vd p)
  • (lambda (x y)
  • (lambda (a)
  • ( x y a)))
  • (lambda (x y)
  • (lambda (a)
  • ( x y a) ) )

Position 0 Position 1
Position 0
Level 0
Level 1
Region
Write a Comment
User Comments (0)
About PowerShow.com