Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1 Review: BNF Grammar for lists:

Similar presentations


Presentation on theme: "Chapter 1 Review: BNF Grammar for lists:"— Presentation transcript:

1 Chapter 1 Review: BNF Grammar for lists:
<list> ::= ({<datum>}*) <dotted-datum> ::= ({<datum>}+ . <datum>) <vector> ::= #({<datum>}*) <datum> ::= <number> | <symbol> | <boolean> | <string> ::= <list> | <dotted-datum> | <vector> Let's derive (a “mixed” # (bag (of . data))) from this grammar....

2 Chapter 1 Review: Free and Bound Variables
A variable x occurs free in a l-calculus expression E iff: 1. E is a variable reference and E = x; or 2. E is of the form (l (y) E') where y ≠ x and x occurs free in E'; or 3. E is of the form (E1 E2) and x occurs free in E1 or E2 Let's prove that b occurs free in (l (a) (a b))

3 Chapter 1 Review: Free and Bound Variables
A variable x occurs bound in a l-calculus expression E iff: 1. E is of the form (l (y) E') where x occurs bound in E' or x = y and y occurs free in E'; or 2. E is of the form (E1 E2) and x occurs bound in E1 or E2 Let's prove that a occurs bound in (l (a) (a b))

4 Coding It Up: Follow the Definition
(define occurs-free? (lambda (var exp) (cond 1. E is a variable reference and E = x; or ((symbol? exp) (eqv? var exp)) 2. E is of the form (l (y) E') where y ≠ x and x occurs free in E'; or ((eqv? (car exp) 'lambda) (and (not (eqv? (caadr exp) var)) (occurs-free? var (caddr exp)))) 3. E is of the form (E1 E2) and x occurs free in E1 or E2 ((else (or (occurs-free? var (car exp)) (occurs-free? var (cadr exp)))))))

5 (define occurs-bound? (lambda (var exp) (cond 0. symbol by itself isn't bound ((symbol? exp) #f) 1. E is of the form (l (y) E') where x occurs bound in E' ((eqv? (car exp) 'lambda) (or (occurs-bound? var (caddr exp)) or x = y and y occurs free in E'; or (and (eqv? (caadr exp) var) (occurs-free? var (caddr exp))))) 2. E is of the form (E1 E2) and x occurs bound in E1 or E2 (else (or (occurs-bound? var (car exp)) (occurs-bound? var (cadr exp)))))))

6 Use helper functions! (define (lambda-exp? exp)
(eqv? (car exp?) 'lambda)) (define (lambda-arg exp) (caadr exp)) (define (lambda-body exp) (caddr exp)) (define (app-fun exp) (car exp)) (define (app-arg exp) (cadr exp))

7 (define occurs-free? (lambda (var exp) (cond 1. E is a variable reference and E = x; or ((symbol? exp) (eqv? var exp)) 2. E is of the form (l (y) E') where y ≠ x and x occurs free in E'; or ((lambda-exp? exp) (and (not (eqv? (lambda-arg exp) var) (occurs-free? var (lambda-body exp)))) 3. E is of the form (E1 E2) and x occurs free in E1 or E2 ((else (or (occurs-free? var (app-fun exp)) (occurs-free? var (app-arg exp)))))))


Download ppt "Chapter 1 Review: BNF Grammar for lists:"

Similar presentations


Ads by Google