1 / 23

(define applicative- eval (lambda (exp)

applicative- eval (substitution-core.scm). (define applicative- eval (lambda (exp) ( cond ((atomic? exp) ( eval -atomic exp)) ((special-form? exp) ( eval -special-form exp)) ((list-form? exp) ( eval -list exp))

oprah
Download Presentation

(define applicative- eval (lambda (exp)

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. applicative-eval (substitution-core.scm) (define applicative-eval (lambda (exp) (cond ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list exp)) ((value? exp) exp) ((application? exp) (let ((renamed-exp (rename exp))) ; <- arguments are renamed (apply-procedure ; <- last, the operator is applied (applicative-eval (operator renamed-exp)) ; <- first, the operator is evaluated (list-of-values (operands renamed-exp))))) ;<- then, the arguments are evaluated (else (error "Unknown expression type -- APPLICATIVE-EVAL" exp)))))

  2. Some examples ASP.scm: > (applicative-eval 4) ((atomic? exp) (eval-atomic exp)) ((atomic? exp) (eval-atomic exp)) Substitution-core.scm: (define eval-atomic (lambda (exp) (if (not (variable? exp)) exp (look-up-variable-value exp)) Substitution-core.scm: > (applicative-eval '(list 1 2)) (define list-form? (lambda (exp) (or (tagged-list? exp ‘cons) (tagged-list? exp ‘list) (tagged-list? exp ‘append)))) (define eval-list (lambda (lst) (make-list (apply-primitive-procedure (applicative-eval (operator lst)) (list-of-values (operands lst))))) ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list exp))

  3. ASP.scm: > (applicative-eval '(lambda (x y) (+ x y 10))); (define special-form? (lambda (exp) (or (quoted? exp) (lambda? exp) (definition? exp) (if? exp) (begin? exp)))) ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) Substitution-core.scm: (define eval-special-form (lambda (exp) (cond ((quoted? exp) (make-symbol exp)) ((lambda? exp) (eval-lambda exp)) … ASP.scm: (define lambda? (lambda (exp) (tagged-list? exp 'lambda) )) Substitution-core.scm: (define eval-lambda (lambda (exp) (make-procedure (lambda-parameters exp) (lambda-body exp))))

  4. Examine the following cond clause of applicative-eval: ((application? exp) (let ((renamed-exp (rename exp))) ; <- arguments are renamed (apply-procedure ; <- last, the operator is applied (applicative-eval (operator renamed-exp)) ; <- first, the operator is evaluated (list-of-values (operands renamed-exp))))) ;<- then, the arguments are evaluated 1.What changes are required for normal-order evaluation? Operands must not be evaluated. 2.What does ‘apply-procedure’ do?

  5. Examine the following cond clause of applicative-eval: (define apply-procedure (lambda (procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure arguments)) ((compound-procedure? procedure) (let ((body (rename (procedure-body procedure))) (parameters (procedure-parameters procedure))) (eval-sequence (substitute body parameters arguments)))) (else (error "Unknown procedure type -- APPLY" procedure))))) (list-of-values arguments) What changes are required above for normal-order evaluation? We must remember that the arguments to apply primitive procedure might not be evaluated. We must evaluate them since“There is no choice”.

  6. ASP- Abstract Syntax Parser • A “tool“ for handling expressions in the supported language. • The ASP includes ADTs for every expression: Each includes aconstructor, selectors(to extract the components of an expression) and a predicate(to identify the kind of an expression). • Provides an abstraction barrier between concrete syntax andoperational semantics(we can change the concrete syntax and this will not affect the API of the parser, only the implementation. On the other hand, we can change the implementation without affecting the syntax) ASP Derived expressions Core Special forms Data Structures(+GE)

  7. Some ASP procedures < (define exp '(lambda (x y) (+ x y 10))) < (tagged-list? exp ‘define) # F < (tagged-list? exp ‘lambda) # T (define tagged-list? (lambda (x tag) (and (list? x) (eq? (get-tag x) tag)))) (define lambda? (lambda (exp) (tagged-list? exp 'lambda) )) (define lambda-parameters (lambda (exp) (car (get-content exp)))) The ADT implemented in the ASPfor lambda expressions (define lambda-body (lambda (exp) (cdr (get-content exp)))) (define make-lambda (lambda (parameters body) (attach-tag (cons parameters body) 'lambda)))

  8. Example: supporting case expressions Abstract syntax: <CASE>: Components: Control expression: <EXP> Clause: <CASE_CLAUSE>. Amount>=0. ordered Else-clause: <ELSE_CLAUSE> <CASE>: Components: Compared expression: <EXP> Action expression: <EXP>. Amount>=1. ordered (define fib (lambda (n) (case n (0 0) (1 1) (else … Concrete syntax: <CASE> (case <EXP> <CASE_CLAUSE>* <ELSE_CLAUSE>) <CASE_CLAUSE> (<EXP> <EXP-SEQUENCE>)

  9. Example: supporting case expressions Adding the required ADT procedures to the ASP ; Case (define case? (lambda (exp) (tagged-list? exp 'case))) ; A constructor for case-clauses: (define make-case-clause (lambda (compared actions) (cons compared actions))) (define case-compared car) ; A constructor for case (define make-case (lambda (control case-clauses) (attach-tag (cons control case-clauses) 'case))) (define case-actions cdr) (define case-first-clause (lambda (clauses) (car clauses))) (define case-rest-clauses (lambda (clauses) (cdr clauses))) (define case-control cadr) (define case-last-clause? (lambda (clauses) (and (null? (cdr clauses)) (eq? (case-compared (case-first-clause clauses)) 'else)))) (define case-clauses cddr)

  10. Example: supporting case expression Using the ADT we can build a case expression! > (make-case 'n (list (make-case-clause '0 '(0)) (make-case-clause '1 '(1)) (make-case-clause 'else '( (display 'processing…) (newline) (+ (fib (- n 1)) (fib (– n 2))))))) Are there any other changes required?... We can identify a case expression, extract its components and build such an expression. But how to we evaluate a case expression?

  11. Example: supporting case expression Supporting the evaluation of a case expression Is it a compound expression (e0 … en)? Is it a special form? Is it an airplane? (define eval-special-form (lambda (exp) (cond ((quoted? exp) (make-symbol exp)) ((lambda? exp) (eval-lambda exp)) … ((case? exp) (eval-case exp)) … ) (define special-form? (lambda (exp) (or (quoted? exp) (lambda? exp) (definition? exp) (if? exp) (begin? exp) (case? exp))))

  12. Example: supporting case expression Evaluation of a case expression Determining the evaluation rule for a case expression: Evaluate the control component Compare its value to each of the compared components by order (we assume numerical values). Find the first clause (if exists) of which the compared value equals the control value. Evaluate each of the actions of this clause. If no such clause exists, evaluate the actions included in the else-clause by order. (define fib (lambda (n) (case n (0 0) (1 1) (else …

  13. Example: supporting case expression Evaluation of a case expression (define (eval-case exp) (letrec ((eval-clauses (lambda (control clauses) (cond((null? clauses) 'unspecified) ((or (case-last-clause? clauses) (= (applicative-eval (case-compared (case-first-clause clauses))) control)) (eval-sequence (case-actions (case-first-clause clauses)))) (else (eval-clauses control (case-rest-clauses clauses))))))) (eval-clauses (applicative-eval (case-control exp)) (case-clauses exp))))

  14. ASP Derived expressions Core Special forms Data Structures (+GE) Data Structures • This package of the interpreter implements all the data structures needed by the supported language. For example, the global environment. • (lookup-variable-value-var) returns the value of a given variable • bound in the global environment. • (make-binding varval) creates a binding (ADT) of var to val. • (add-binding! binding) adds a given binding to the GE. Symbol table of the supporting language The-Global-Environment(A variable in the supporting language, used for “our” evaluator implementation)

  15. Testing the evaluator (define apply-test-list (lambda (eval-proclst) (letrec ((apply-test-list$ (lambda (lst fail) (if (null? lst) 'ok (let ((tuple (car lst))) (let ((res (eval-proc (car tuple))) (expected-res (cadrtuple))) (if (not (equal? res expected-res)) (fail (append tuple (list res))) (apply-test-list$ (cdrlst) fail)))))))) (apply-test-list$ lst error)))) lst:A list of pairs: (<expression>, <expected value>) eval-proc: The evaluation procedure to be tested If the evaluation of an <expression> is different than the <expected value>, The computation is stopped. This is possible thanks to CPS.Example lst input: ( ((if (> 3 2) 5 1) 5) ((if (< 3 2) 5 1) 1) ((if (> (+ 1 2) 2) (+ 1 2 3) 1) 6) ((define (f n) (if (= n 0) 1 (* n (f (- n 1))))) ok) ((f 5) 120) )

  16. The environment model • A computational model different than the substitution model. • Expressions are evaluated with respect to a certain environment. • Saves substitution (of formal parameters by argument) and renaming. • Definitions: • Frame: A mapping between variables and values. Every bound variable in a frame is • given one (and only one) value. • Environment: A finite sequence of Frames (f1,f2,…,fn). The last frame is the Global • Environment, the only frame to statically exist. II III EnvA: (II,I) Env B: (III,I) Env C: (I) I x:3 y:5 z:6 x:7 n:1 y:2

  17. The environment model • Definitions: • The value of x (a variable) in e (an environment): is the value of x in the first frame of • e where it is bound to a value. • For example: The value of y in B is 2, the value of y in A is 5. z is unbound in B. • A procedure in the environment model: is a pair (a closure) of which the first element • stores the procedure parameters and body and the second element stores a “pointer” • to the environment where the procedure was declared. • For example: the variable square is bound to the procedure define in the GE. II III EnvA: (II,I) Env B: (III,I) Env C: (I) I (define(squarex) (*xx)) x:3 y:5 GE square: z:6 x:7 n:1 y:2 P:(x) B:(* x x)

  18. The environment model • Procedure application in the GE: • Let f be a procedure with formal parameters (x1 … xn). When applying f to v1 … vn: • Create a new frame binding the variables x1 … xn to the values v1 … vn respectively. • This new frame extends the environment E in which f was declared. • This is noted in a diagram by a pointer from the new frame to E. • E is stored within the closure data structure that was created by evaluating f. • Evaluate the body of f with respect to the extended environment. (define(squarex)(*xx)) (square 5) GE square: P:(x) B:(* x x) E1 x:5 (* x x)

  19. Example (1) (definesq (lambda (x) (*xx))) (definesum-of-squares (lambda (xy) (+(sqx)(sqy)))) (definef (lambda (a)(sum-of-squares(+a1)(*a2)))) GE sq: P:(x)B:(* x x) • sum-of-squares: P:(x y)B:(+ (sq x) (sq y)) • f: P:(a)B:(sum-of-squares (+ a 1) (* a 2))

  20. Example (1) > (f 5) GE sq: • sum-of-squares: • f: P:(x)B1:(* x x) B3 E1 a:5 E2 B2 P:(x y)B2:(+ (sq x) (sq y)) x:6y:10 B1 E3 (sum-of-…) E4 B1 x:6 (+ (sq x) (sq y)) x:10 (* x x) (* x x) P:(a)B3:(sum-of-squares (+ a 1) (* a 2))

  21. Example (2) > (definea 8) • > (defineb 5) • > (definec a) • > (definef (lambda (x y) (+ x y))) • > (fa c) a: 8 b: 5 c: 8 f: E1 x: 8y: 8 (+ x y) Parameters: (x y)Body: (+ x y)

  22. Example (2) > (definep (lambda (a b c) (let ((d (+ a b)) (e (* a b)) (f e d)))) > (p a b 3) a: 8 b: 5 c: 8 f: p: Parameters: (x y)Body: (+ x y) Parameters: (a b c)Body: (let…) E1 E2 E3 a: 8b: 5c: 3 d: 13e: 40 d: 13e: 40 (f e d) (+ x y) Parameters: (d e)Body: (f e d) (let…)

  23. Example (3) > (definefact (lambda (n) (if (= n 0) 1 (* n (fact (- n 1)))))) GE fact: P:(n)B:(if…) E4 E1 E2 E3 B1 B1 B1 B1 n:3 n:2 n:1 n:0 (if …) (if …) (if …) (if …)

More Related