1 / 17

Inductive Sets of Data

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

Télécharger la présentation

Inductive Sets of Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

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

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

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

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

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

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

  12. 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 of contours crossed while travelling from reference to declaration (count from zero) • (lambda (x y) ; [ • ((lambda (a) ; [ • (x (a y)) ; ] • ) • x • ) ; ] • )

  14. 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 • ) ; ] • )

  15. 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 • ) ; ] • )

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

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

More Related