Practical Session 7

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

description

Practical Session 7. Substitution-Model Evaluator The Core: applicative- eval Abstract Syntax Parser Derived Expressions Special Form Data Structures Environments-Model. The Evaluator. applicative- eval (substitution-core.rkt). substitute. applicative- eval. apply-procedure. rename. - PowerPoint PPT Presentation

Transcript of Practical Session 7

Page 1: Practical Session 7

1

Practical Session 7

• Substitution-Model Evaluator– The Core: applicative-eval– Abstract Syntax Parser– Derived Expressions– Special Form– Data Structures

• Environments-Model

Page 2: Practical Session 7

2

The Evaluator

Page 3: Practical Session 7

3

applicative-eval (substitution-core.rkt)

(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)))))

applicative-eval apply-procedure

substitute

rename

Page 4: Practical Session 7

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

Page 5: Practical Session 7

5

ASP- Abstract Syntax Parser

ASPASP

Derived expressions

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)•No evaluation at this stage!

Page 6: Practical Session 7

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

Page 7: Practical Session 7

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

Page 8: Practical Session 7

8

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

Page 9: Practical Session 7

9

(define case? (lambda (exp) (tagged-list? exp 'case)))

(define make-case-clause (lambda (compared actions) (cons compared actions)))

Adding the required ADT procedures to the ASP

Example: supporting case expressions

(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-rest-clauses (lambda (clauses) (cdr clauses)))

(define case-last-clause? (lambda (clauses) (and (null? (cdr clauses)) (eq? (case-compared (case-first-clause clauses)) 'else))))

Page 10: Practical Session 7

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?... We can identify a case expression, extract its components and build such an expression. But how to we evaluate a case expression?

Page 11: Practical Session 7

11

Derived Expressions

Motivation:– Smaller, simpler core– Change in evaluator implementation requires less

work

shallow-derive

derive

Page 12: Practical Session 7

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

Page 13: Practical Session 7

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

Page 14: Practical Session 7

14

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

(define special-form? (lambda (exp) (or (quoted? exp) (lambda? exp) (definition? exp) (if? exp) (begin? exp) (case? exp))))

Page 15: Practical Session 7

15

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 …

Page 16: Practical Session 7

16

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

Page 17: Practical Session 7

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

Page 18: Practical Session 7

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.

x:3y:5

I

z:6x:7

II n:1y:2

III

Env A: (II,I)

Env B: (III,I)

Env C: (I)

Page 19: Practical Session 7

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.

x:3y:5

I

z:6x:7

II n:1y: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))

Page 20: Practical Session 7

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.

E1

(* x x)

GE

square:

P:(x)

B:(* x x)

x:5

(define (square x)(* x x))(square 5)

Page 21: Practical Session 7

21

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

Page 22: Practical Session 7

22

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:5B3

E2

(+ (sq x) (sq y))

x:6y:10

B2

E3

(* x x)

x:6B1

E4

(* x x)

x:10

B1

Page 23: Practical Session 7

23

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: 8y: 8

E1

(+ x y)

Page 24: Practical Session 7

24

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: 8b: 5c: 3

E1

(let)…Parameters: (d e)Body: (f e d)

d: 13e: 40

E2

(f e d)

E3x: 13y: 40(+ x y)

Page 25: Practical Session 7

25

Example (3)

> (define fact (lambda (n)(if (= n 0) 1 (* n (fact (- n 1))))))

B1

B1

B1

B1

E1

GEfact:

P:(n)B:(if…)

(if …)

n:3E2

(if …)

n:2E3

(if …)

n:1E4

(if …)

n:0

Page 26: Practical Session 7

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

P:(x)B: x

n:2c:

P:(fact-n-1)B:(c (* ..)

n:1c:

P:(fact-n-1)B:(c (* ..)

n:0c:

fact-n-1 :1

fact-n-1 :1

x:2