Practical Session 7 SubstitutionModel Evaluator The Core applicativeeval
Practical Session 7 • Substitution-Model Evaluator – The Core: applicative-eval – Abstract Syntax Parser – Derived Expressions – Special Form – Data Structures • Environments-Model 1
The Evaluator 2
applicative-eval (substitution-core. rkt) sub stit ute applicative-eval apply-procedure e m ena r (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) ((application? exp) (apply-procedure (applicative-eval (operator exp)) (list-of-values (operands exp)))) (else (error 'eval "unknown expression type: ~s" exp))))) 3
apply-procedure (substitution-core. rkt) (define apply-procedure (lambda (procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-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))))) 4
ASP- Abstract Syntax Parser • 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) • No evaluation at this stage! 5 ASP Derived expressions Core Special forms Data Structures (+GE)
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))) 6
Handling ; ; Type: [LIST(Symbol)*LIST -> LIST] (define make-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)))) 7 ; ; Type: [LIST -> LIST] (define lambda-body (lambda (exp) (cdr (get-content exp))))
Example: supporting case expressions (define fib (lambda (n) (case n (0 0) (1 1) (else … Abstract syntax: <CASE>: Components: Control expression: <EXP> Clause: <CASE_CLAUSE>. Amount>=0. ordered Else-clause: <ELSE_CLAUSE> Concrete syntax: <CASE> (case <EXP> <CASE_CLAUSE>* <ELSE_CLAUSE>) <CASE_CLAUSE> (<EXP> <EXP-SEQUENCE>) 8
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-clauses)) 'else)))) (define make-case (lambda (control case-clauses) (attach-tag (cons control case-clauses) 'case))) (define case-control cadr) (define case-clauses cddr) 9 (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 case-rest-clauses (lambda (clauses) (cdr clauses))) (define case? (lambda (exp) (tagged-list? exp 'case)))
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? 10
Derived Expressions Motivation: – Smaller, simpler core – Change in evaluator implementation requires less work shallow-derive 11 derive
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 (derived-exp)))))) 12
Derived Expression (2) (define derived? (lambda (exp) (or (cond? exp) (function-definition? 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)))) 13
Example: supporting case expression Supporting the evaluation of a case expression Is it a compound expression (e 0 … 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)))) exp) 14
Example: supporting case expression Evaluation of a 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. 15 (define fib (lambda (n) (case n (0 0) (1 1) (else …
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-clauses))) control)) (eval-sequence (case-actions (case-first-clauses)))) (else (eval-clauses control (case-rest-clauses))))))) (eval-clauses (applicative-eval (case-control exp)) (case-clauses exp)))) 16
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)))) 17
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 (f 1, f 2, …, fn). The last frame is the Global Environment, the only frame to statically exist. x: 3 y: 5 I Env A: (II, I) Env B: (III, I) Env C: (I) z: 6 II x: 7 18 n: 1 III y: 2
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. (define (square x) (* x x)) x: 3 y: 5 GE Env A: (II, I) Env B: (III, I) Env C: (I) square: P: (x) B: (* x x) 19 I z: 6 II x: 7 n: 1 III y: 2
The environment model Procedure application in the GE: Let f be a procedure with formal parameters (x 1 … xn). When applying f to v 1 … vn: • Create a new frame binding the variables x 1 … xn to the values v 1 … 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 (square x)(* x x)) (square 5) GE square: P: (x) B: (* x x) E 1 x: 5 (* x x) 20
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)) 21
Example (1) > (f 5) GE sq: P: (x) B 1: (* x x) sum-of-squares: P: (x y) B 2: (+ (sq x) (sq y)) f: E 1 P: (a) B 3: (sum-of-squares (+ a 1) (* a 2)) B 3 a: 5 (sum-of-…) E 2 B 2 x: 6 y: 10 (+ (sq x) (sq y)) 22 E 3 B 1 x: 6 (* x x) E 4 B 1 x: 10 (* x x)
Example (2) > > > (define a (define b (define c (define f (f a c) 8) 5) a) (lambda (x y) (+ x y))) a: 8 b: 5 E 1 x: 8 y: 8 c: 8 f: Parameters: (x y) Body: (+ x y) 23 (+ x y)
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 Parameters: (x y) Body: (+ x y) f: p: Parameters: (a b c) Body: (let…) Parameters: (d e) Body: (f e d) 24 E 1 a: 8 b: 5 c: 3 (let…) E 2 d: 13 e: 40 (f e d) E 3 x: 13 y: 40 (+ x y)
Example (3) > (define fact (lambda (n) (if (= n 0) 1 (* n (fact (- n 1)))))) GE fact: P: (n) B: (if…) E 1 B n: 3 1 (if …) 25 E 2 B n: 2 1 (if …) E 3 B n: 1 1 (if …) E 4 B n: 0 1 (if …)
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$: n: 2 c: n: 1 c: n: 0 c: 26 P: (n c) B: (if…) P: (x) B: x P: (fact-n-1) B: (c (*. . ) x: 2 fact-n-1 : 1 P: (fact-n-1) B: (c (*. . )
- Slides: 26