170 likes | 269 Vues
Inductive Sets of Data. Programming Language Essentials 2nd edition Chapter 1.3 Scoping and Binding of Variables. Scoping and Binding of Variables. (f x y) ; references to variables (lambda (x) …) ; declarations (let ((x …)) …) (define x …) denotation: value named by variable
E N D
Inductive Sets of Data • Programming Language Essentials • 2nd edition • Chapter 1.3 Scoping and Binding of Variables
Scoping and Binding of Variables • (f x y) ; references to variables • (lambda (x) …) ; declarations • (let ((x …)) …) • (define x …) • denotation: value named by variable • reference is bound by a declaration • binding rules: scope of declaration • Scheme is statically scoped: determined from program text alone • check bc: http://www.cs.rit.edu/~ats/plc-2002-2/html/skript-17.html
Binding Rule for Lambda Calculus • expr: 'Symbol' • | '(' 'lambda' '(' 'Symbol' ')' expr ')' • | '(' expr expr ')' • In '(' 'lambda' '(' 'Symbol' ')' expr ')' the occurrence of 'Symbol' is a declaration that binds all occurrences of that variable in expr unless some intervening declaration of the same variable occurs.
Free and Bound Variables • x occurs free in E iff there is some use of x in E that is not bound by any declaration of x in E. • x occurs bound in E iff there is some use of x in E that is bound by a declaration of x in E. • (lambda (x) x) • ((lambda (x) x) y) • ((lambda (x) x) x); one free x, one bound x • (lambda (y) • ((lambda (x) x) y))
Free and Bound Variables (2) • expression value only depends on values for free variables; context must provide. • (lambda (y) ; context • ((lambda (x) x) y) ; independent of x • ) • expression value does not depend on bindings of variables that are not free in expression. • ((lambda (x) x) y) • the red x is free, but it is not in an expr.
Combinators • lambda expression without free variables • fixed meaning • identity, returns argument: • (lambda (x) x) • application, applies function to argument: • (lambda (f) • (lambda (x) • (f x) • ) )
Free and Bound in Lambda Expression • x occurs free in lambda calculus expression E iff one of • (1) E is a variable reference and E is the same as x • (2) E is (lambda (y) E') with y not x and x free in E' • (3) E is (E1 E2) and x is free in either • x occurs bound in lambda calculus expression E iff one of • (1) E is (lambda (y) E') with x bound in E' or x same variable as y and y free in E' • (2) E is (E1 E2) and x is bound in either
occurs-free? • (define occurs-free? • (lambda (var exp) • (cond • ((symbol? exp) (eqv? var exp)) • ((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-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)) • ) ) ) ) )
Scope and Lexical Address • given a declaration, which references refer to it? • (lambda (x) body) ; region for x is body • (define x value); region is entire program • (define x • (lambda (x) • (map (lambda (x) (+ x 1)) x ) • ) ) • (x ‘(1 2 3)) ; produces ‘(2 3 4) • block-structured, lexical binding: use innermost declaration
Scope and Lexical Address (2) • scope of variable declaration: text region where references to variable refer to declaration • inner declaration shadows outer, creates hole • scope of variable contains all free references to variable in region associated with declaration • to find declaration: start with innermost region and search outward for first associated declaration • if nothing is found, variable is free
Contour and Lexical Depth • contour: border of region associated with declaration • lexical depth: number of contours crossed while travelling from reference to declaration (count from zero) • (lambda (x y) • ((lambda (a) • (x (a y)) • ) • x • ) • )
Contour and Lexical Depth • contour: border of region associated with declaration • lexical depth: number of contours crossed while travelling from reference to declaration (count from zero) • (lambda (x y) ; [ • ((lambda (a) ; [ • (x (a y)) ; ] • ) • x • ) ; ] • )
Contour and Lexical Depth • contour: border of region associated with declaration • lexical depth: number of contours crossed while travelling from reference to declaration (count from zero) • (lambda (x y) ; [ • ((lambda (a) ; [ • (x (a y)) ; ] a at 0, x y at 1 • ) • x ; x at depth 0 • ) ; ] • )
Lexical Address • declaration position: counted from 0 in contour • lexical address: depth and position • (lambda (x y) ; [ x at 0, y at 1 • ((lambda (a) ; [ a at 0 • (x (a y)) ; ] a at 0 0, x at 1 0 • ; y at 1 1 • ) • x ; x at 0 0 • ) ; ] • )
Lexical Address (2) • lexical address: depth and position • completely specifies a variable • (lambda (x y) • ((lambda (a) • ((x : 1 0) ((a : 0 0) (y : 1 1))) • ) • (x : 0 0) • ) • )
Lexical Address (2) • lexical address: depth and position • completely specifies a variable • (lambda 2 • ((lambda 1 • (( : 1 0) (( : 0 0) ( : 1 1))) • ) • ( : 0 0) • ) • ) • names are not really needed