Inductive Sets of Data Programming Language Essentials 2

  • Slides: 17
Download presentation
Inductive Sets of Data Programming Language Essentials 2 nd edition Chapter 1. 3 Scoping

Inductive Sets of Data Programming Language Essentials 2 nd edition Chapter 1. 3 Scoping and Binding of Variables 1

Scoping and Binding of Variables (f x y) ; references to variables (lambda (x)

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-20022/html/skript-17. html 2

Binding Rule for Lambda Calculus expr: 'Symbol' | '(' 'lambda' '(' 'Symbol' ')' expr

Binding Rule for Lambda Calculus expr: 'Symbol' | '(' 'lambda' '(' 'Symbol' ')' 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. 3

Free and Bound Variables x occurs free in E iff there is some use

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)) 4

Free and Bound Variables (2) expression value only depends on values for free variables;

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. 5

Combinators lambda expression without free variables fixed meaning identity, returns argument: (lambda (x) x)

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) ) ) 6

Free and Bound in Lambda Expression x occurs free in lambda calculus expression E

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 (E 1 E 2) 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 (E 1 E 2) and x is bound in either 7

occurs-free? (define occurs-free? (lambda (var exp) (cond ((symbol? exp) (eqv? var exp)) ((eqv? (car

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)) ) ) 8

occurs-bound? (define occurs-bound? (lambda (var exp) (cond ((symbol? exp) #f) ((eqv? (car exp) 'lambda)

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)) ) ) 9

Scope and Lexical Address given a declaration, which references refer to it? (lambda (x)

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 10

Scope and Lexical Address (2) scope of variable declaration: text region where references to

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 11

Contour and Lexical Depth contour: border of region associated with declaration lexical depth: number

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 ) ) 12

Contour and Lexical Depth contour: border of region associated with declaration lexical depth: number

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 ) ) ; [ ; ] 13

Contour and Lexical Depth contour: border of region associated with declaration lexical depth: number

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 ) ) ; [ ; ] a at 0, x y at 1 ; ; ] x at depth 0 14

Lexical Address declaration position: counted from 0 in contour lexical address: depth and position

Lexical Address declaration position: counted from 0 in contour lexical address: depth and position (lambda (x y) ((lambda (a) (x (a y)) ) x ) ; [ x at 0, y at 1 ; [ a at 0 ; ] a at 0 0, x at 1 0 ; y at 1 1 ; ; ] x at 0 0 ) 15

Lexical Address (2) lexical address: depth and position completely specifies a variable (lambda (x

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) ) ) 16

Lexical Address (2) lexical address: depth and position completely specifies a variable (lambda 2

Lexical Address (2) lexical address: depth and position completely specifies a variable (lambda 2 ((lambda 1 (( : 1 0) (( ) ( : 0 0) ) ) : 0 0) ( : 1 1))) names are not really needed 17