Download presentation
Presentation is loading. Please wait.
1
(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))))) (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))))) applicative-eval (substitution-core.scm) 1
2
Some examples > (applicative-eval 4) ((atomic? exp) (eval-atomic exp)) > (applicative-eval '(list 1 2)) ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list exp)) ((atomic? exp) (eval-atomic exp)) (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))))) (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))))) (define eval-atomic (lambda (exp) (if (not (variable? exp)) exp (look-up-variable-value exp)) (define eval-atomic (lambda (exp) (if (not (variable? exp)) exp (look-up-variable-value exp)) ASP.scm: Substitution-core.scm: 2
3
> (applicative-eval '(lambda (x y) (+ x y 10))); ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) (define eval-lambda (lambda (exp) (make-procedure (lambda-parameters exp) (lambda-body exp)))) (define eval-lambda (lambda (exp) (make-procedure (lambda-parameters exp) (lambda-body exp)))) (define lambda? (lambda (exp) (tagged-list? exp 'lambda) )) (define lambda? (lambda (exp) (tagged-list? exp 'lambda) )) Substitution-core.scm: (define eval-special-form (lambda (exp) (cond ((quoted? exp) (make-symbol exp)) ((lambda? exp) (eval-lambda exp)) … (define eval-special-form (lambda (exp) (cond ((quoted? exp) (make-symbol exp)) ((lambda? exp) (eval-lambda exp)) … (define special-form? (lambda (exp) (or (quoted? exp) (lambda? exp) (definition? exp) (if? exp) (begin? exp)))) (define special-form? (lambda (exp) (or (quoted? exp) (lambda? exp) (definition? exp) (if? exp) (begin? exp)))) ASP.scm: Substitution-core.scm: 3
4
((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 ((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 Examine the following cond clause of applicative-eval: 1.What changes are required for normal-order evaluation? Operands must not be evaluated. 2.What does ‘apply-procedure’ do? 4
5
(define apply-procedure (lambda (procedure arguments) (primitive-procedure? procedure) (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure arguments)) (compound-procedure? procedure) ((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))))) (define apply-procedure (lambda (procedure arguments) (primitive-procedure? procedure) (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure arguments)) (compound-procedure? procedure) ((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))))) Examine the following cond clause of applicative-eval: 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”. (list-of-values arguments) 5
6
ASP- Abstract Syntax Parser ASP Derived expressions Core Special forms Data Structures (+GE) A “tool“ for handling expressions in the supported language. The ASP includes ADTs for every expression: Each includes a constructor, 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 and operational 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) 6
7
Some ASP procedures (define tagged-list? (lambda (x tag) (and (list? x) (eq? (get-tag x) tag)))) (define tagged-list? (lambda (x tag) (and (list? x) (eq? (get-tag x) tag)))) < (define exp '(lambda (x y) (+ x y 10))) < (tagged-list? exp ‘define) # F < (tagged-list? exp ‘lambda) # T (define lambda? (lambda (exp) (tagged-list? exp 'lambda) )) (define lambda? (lambda (exp) (tagged-list? exp 'lambda) )) (define lambda-parameters (lambda (exp) (car (get-content exp)))) (define lambda-parameters (lambda (exp) (car (get-content exp)))) (define lambda-body (lambda (exp) (cdr (get-content exp)))) (define lambda-body (lambda (exp) (cdr (get-content exp)))) (define make-lambda (lambda (parameters body) (attach-tag (cons parameters body) 'lambda))) (define make-lambda (lambda (parameters body) (attach-tag (cons parameters body) 'lambda))) The ADT implemented in the ASP for lambda expressions 7
8
Example: supporting case expressions (define fib (lambda (n) (case n (0 0) (1 1) (else … (define fib (lambda (n) (case n (0 0) (1 1) (else … Abstract syntax: : Components: Control expression: Clause:. Amount>=0. ordered Else-clause: : Components: Compared expression: Action expression:. Amount>=1. ordered Concrete syntax: (case * ) ( ) 8
9
; Case (define case? (lambda (exp) (tagged-list? exp 'case))) ; Case (define case? (lambda (exp) (tagged-list? exp 'case))) ; A constructor for case-clauses: (define make-case-clause (lambda (compared actions) (cons compared actions))) ; A constructor for case-clauses: (define make-case-clause (lambda (compared actions) (cons compared actions))) Adding the required ADT procedures to the ASP Example: supporting case expressions ; A constructor for case (define make-case (lambda (control case-clauses) (attach-tag (cons control case-clauses) 'case))) ; A constructor for case (define make-case (lambda (control case-clauses) (attach-tag (cons control case-clauses) 'case))) (define case-control cadr) (define case-clauses cddr) (define case-compared car) (define case-actions cdr) (define case-first-clause (lambda (clauses) (car clauses))) (define case-first-clause (lambda (clauses) (car clauses))) (define case-rest-clauses (lambda (clauses) (cdr clauses))) (define case-rest-clauses (lambda (clauses) (cdr clauses))) (define case-last-clause? (lambda (clauses) (and (null? (cdr clauses)) (eq? (case-compared (case-first-clause clauses)) 'else)))) (define case-last-clause? (lambda (clauses) (and (null? (cdr clauses)) (eq? (case-compared (case-first-clause clauses)) 'else)))) 9
10
Using the ADT we can build a case expression! Example: supporting 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?... 10 We can identify a case expression, extract its components and build such an expression. But how to we evaluate a case expression?
11
Supporting the evaluation of a case expression Example: supporting 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)) ((case? exp) (eval-case exp)) … ) (define eval-special-form (lambda (exp) (cond ((quoted? exp) (make-symbol exp)) ((lambda? exp) (eval-lambda exp)) … ((case? exp) (eval-case exp)) ((case? exp) (eval-case exp)) … ) (define special-form? (lambda (exp) (or (quoted? exp) (lambda? exp) (definition? exp) (case? exp) (if? exp) (begin? exp) (case? exp)))) (define special-form? (lambda (exp) (or (quoted? exp) (lambda? exp) (definition? exp) (case? exp) (if? exp) (begin? exp) (case? exp)))) 11
12
Evaluation of a case expression Example: supporting case expression Determining the evaluation rule for a case expression: 1.Evaluate the control component 2.Compare its value to each of the compared components by order (we assume numerical values). 3.Find the first clause (if exists) of which the compared value equals the control value. Evaluate each of the actions of this clause. 4.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 … (define fib (lambda (n) (case n (0 0) (1 1) (else … 12
13
Evaluation of a case expression Example: supporting 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)))) 13
14
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 var val) creates a binding (ADT) of var to val. (add-binding! binding) adds a given binding to the GE. ASP Derived expression s Core Special forms Data Structures (+GE) Symbol table of the supporting language The-Global-Environment (A variable in the supporting language, used for “our” evaluator implementation) 14
15
Testing the evaluator (define apply-test-list eval-proclst (lambda (eval-proc lst) (letrec ((apply-test-list$ (lambda (lst fail) (if (null? lst) 'ok (let ((tuple (car lst))) (let ((res (eval-proc (car tuple))) (expected-res (cadr tuple))) (if (not (equal? res expected-res)) (fail (append tuple (list res))) (apply-test-list$ (cdr lst) fail)))))))) (apply-test-list$ lst error)))) (define apply-test-list eval-proclst (lambda (eval-proc lst) (letrec ((apply-test-list$ (lambda (lst fail) (if (null? lst) 'ok (let ((tuple (car lst))) (let ((res (eval-proc (car tuple))) (expected-res (cadr tuple))) (if (not (equal? res expected-res)) (fail (append tuple (list res))) (apply-test-list$ (cdr lst) fail)))))))) (apply-test-list$ lst error)))) eval-proc: The evaluation procedure to be tested lst: A list of pairs: (, ) If the evaluation of an is different than the, 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) ) 15
16
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. x:3 y:5 I z:6 x:7 II n:1 y:2 III Env A: (II,I) Env B: (III,I) Env C: (I)
17
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. x:3 y:5 I z:6 x:7 II n:1 y:2 III Env A: (II,I) Env B: (III,I) Env C: (I) GE square: P:(x) B:(* x x) (define (square x) (* x x))
18
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. E1 (* x x) GE square: P:(x) B:(* x x) x:5 (define (square x)(* x x)) (square 5)
19
19 Example (1) (define sq (lambda (x) (* x x))) (define sum-of-squares (lambda (x y) (+ (sq x) (sq y)))) (define f (lambda (a) (sum-of-squares (+ a 1) (* a 2)))) GE sq: sum-of-squares: f: P:(x) B:(* x x) P:(x y) B:(+ (sq x) (sq y)) P:(a) B:(sum-of-squares (+ a 1) (* a 2))
20
20 Example (1) > (f 5) GE sq: sum-of-squares: f: P:(x) B1:(* x x) P:(x y) B2:(+ (sq x) (sq y)) P:(a) B3:(sum-of-squares (+ a 1) (* a 2)) E1 (sum-of-…) a:5 B3 E2 (+ (sq x) (sq y)) x:6 y:10 B2 E3 (* x x) x:6 B1 E4 (* x x) x:10 B1
21
21 Example (2) > (define a 8) > (define b 5) > (define c a) > (define f (lambda (x y) (+ x y))) > (f a c) a: 8 b: 5 c: 8 f: Parameters: (x y) Body: (+ x y) x: 8 y: 8 E1 (+ x y)
22
22 Example (2) > (define p (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…) a: 8 b: 5 c: 3 E1 (let…) Parameters: (d e) Body: (f e d) d: 13 e: 40 E2 (f e d) E3 d: 13 e: 40 (+ x y)
23
23 Example (3) > (define fact (lambda (n) (if (= n 0) 1 (* n (fact (- n 1)))))) B1B1 B1B1 B1B1 B1B1 E1 GE fact: P:(n) B:(if…) (if …) n:3 E2 (if …) n:2 E3 (if …) n:1 E4 (if …) n:0
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.