70 likes | 215 Vues
Chapter 1 Review: BNF. Grammar for lists: <list> ::= ({<datum>}*) <dotted-datum> ::= ({<datum>}+ . <datum>) <vector> ::= #({<datum>}*) <datum> ::= <number> | <symbol> | <boolean> | <string> ::= <list> | <dotted-datum> | <vector>
E N D
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....
Chapter 1 Review: Free and Bound Variables A variable xoccurs 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 (E1E2) and x occurs free in E1 or E2 Let's prove that b occurs free in (l (a) (a b))
Chapter 1 Review: Free and Bound Variables A variable xoccurs 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 (E1E2) and x occurs bound in E1 or E2 Let's prove that a occurs bound in (l (a) (a b))
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 (E1E2) and x occurs free in E1 or E2 ((else (or (occurs-free? var (car exp)) (occurs-free? var (cadr exp)))))))
(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 (E1E2) and x occurs bound in E1 or E2 (else (or (occurs-bound? var (car exp)) (occurs-bound? var (cadr exp)))))))
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))
(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 (E1E2) and x occurs free in E1 or E2 ((else (or (occurs-free? var (app-fun exp)) (occurs-free? var (app-arg exp)))))))