Practical Session 7 - PowerPoint PPT Presentation

practical session 7 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Practical Session 7 PowerPoint Presentation
Download Presentation
Practical Session 7

play fullscreen
1 / 26
Download Presentation
Practical Session 7
Download Presentation

Practical Session 7

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Practical Session 7 • Substitution-Model Evaluator • The Core: applicative-eval • Abstract Syntax Parser • Derived Expressions • Special Form • Data Structures • Environments-Model

  2. The Evaluator

  3. applicative-eval (substitution-core.rkt) substitute applicative-eval apply-procedure rename (define applicative-eval (lambda (exp) (cond ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list exp)) ((evaluator-value? exp) exp) ((application? exp) (apply-procedure (applicative-eval (operator exp)) (list-of-values (operands exp)))) (else (error 'eval "unknown expression type: ~s" exp)))))

  4. apply-procedure (substitution-core.rkt) (define apply-procedure (lambda (procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure arguments)) ((compound-procedure? procedure) (let ((parameters (procedure-parameters procedure)) (body (rename (procedure-body procedure)))) (eval-sequence (substitute body parameters arguments)))) (else (error 'apply "Unknown procedure type: ~s" procedure)))))

  5. 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) • No evaluation at this stage! ASP Derived expressions Core Special forms Data Structures(+GE)

  6. Handling Tags ;; Signature: attach-tag(x, tag) ;; Type: [LIST*Symbol -> LIST] (define attach-tag (lambda (x tag) (cons tag x))) ;; Signature: tagged-list?(x, tag) ;; Type: [T*Symbol -> Boolean] (define tagged-list? (lambda (x tag) (and (list? x) (eq? (get-tag x) tag)))) ;; Signature: get-tag(x) ;; Type: LIST -> Symbol (define get-tag (lambda (x) (car x)))

  7. Handling ;; Type: [LIST(Symbol)*LIST -> LIST] (define make-lambda (lambda (parameters body) (attach-tag (cons parameters body) 'lambda))) ;; Type: [T -> Boolean] (define lambda? (lambda (exp) (tagged-list? exp 'lambda))) ;; Type: [LIST -> LIST(Symbol)] (define lambda-parameters (lambda (exp) (car (get-content exp)))) ;; Type: [LIST -> LIST] (define lambda-body (lambda (exp) (cdr (get-content exp))))

  8. Example: supporting case expressions Abstract syntax: <CASE>: Components: Control expression: <EXP> Clause: <CASE_CLAUSE>. Amount>=0. ordered Else-clause: <ELSE_CLAUSE> (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 (define case-last-clause? (lambda (clauses) (and (null? (cdr clauses)) (eq? (case-compared (case-first-clause clauses)) 'else)))) (define make-case-clause (lambda (compared actions) (cons compared actions))) (define case-compared car) (define case-actions cdr) (define case-first-clause (lambda (clauses) (car clauses))) (define make-case (lambda (control case-clauses) (attach-tag (cons control case-clauses) 'case))) (define case-rest-clauses (lambda (clauses) (cdr clauses))) (define case? (lambda (exp) (tagged-list? exp 'case))) (define case-control cadr) (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. Derived Expressions Motivation: • Smaller, simpler core • Change in evaluator implementation requires less work shallow-derive derive

  12. Derived Expression (1) (define derive (lambda (exp) (if (atomic? exp) exp (let ((derived-exp (let ((mapped-derive-exp (map derive exp))) (if (not (derived? exp)) mapped-derive-exp (shallow-derive mapped-derive-exp))))) (if (equal? exp derived-exp) exp (derive derived-exp))))))

  13. Derived Expression (2) (define derived? (lambda (exp) (or (cond? exp) (function-definition? exp) (let? exp) (letrec? exp)))) (define shallow-derive (lambda (exp) (cond ((cond? exp) (cond->if exp)) ((function-definition? exp) (function-define->define exp)) ((let? exp) (let->combination exp)) ((letrec? exp) (letrec->let exp)) (else "Unhandled derivision" exp))))

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

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

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

  17. Data Structues ; Type: [Symbol -> LIST] (define make-symbol (lambda (x) (attach-tag (list x) 'symbol))) ; Type: [T -> Boolean] (define evaluator-symbol? (lambda (s) (tagged-list? s 'symbol))) ; Type: [LIST -> Symbol] (define symbol-content (lambda (s) (car (get-content s))))

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

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

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

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

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

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

  24. 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 x: 13y: 40 (f e d) (+ x y) Parameters: (d e)Body: (f e d) (let…)

  25. 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 …)

  26. Example (4) • > (define fact$ • (lambda (n c) • (if (= n 0) (c 1) • (fact$ (- n 1) (lambda (fact-n-1) • (c (* n fact-n-1))))))) • > (fact$ 2 (lambda(x) x)) GE fact$: P:(n c)B:(if…) n:2 c: P:(x)B: x x:2 n:1 c: P:(fact-n-1)B:(c (* ..) • fact-n-1 :1 • fact-n-1 :1 n:0 c: P:(fact-n-1)B:(c (* ..)