/
Practical Session 7 Practical Session 7

Practical Session 7 - PowerPoint Presentation

calandra-battersby
calandra-battersby . @calandra-battersby
Follow
380 views
Uploaded On 2015-12-02

Practical Session 7 - PPT Presentation

SubstitutionModel Evaluator The Core applicative eval Abstract Syntax Parser Derived Expressions Special Form Data Structures EnvironmentsModel 1 The Evaluator 2 applicative eval substitutioncorerkt ID: 212063

case exp define lambda exp case lambda define procedure list clauses clause expression eval tag parameters fact symbol body

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Practical Session 7" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Practical Session 7

Substitution-Model EvaluatorThe Core: applicative-evalAbstract Syntax ParserDerived ExpressionsSpecial FormData StructuresEnvironments-Model

1Slide2

The Evaluator

2Slide3

applicative-

eval (substitution-core.rkt)3(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

renameSlide4

apply-procedure (substitution-core.rkt)

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

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

5Slide6

Handling Tags

6;; 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)))Slide7

Handling

7

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

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

8Slide9

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

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?Slide11

Derived Expressions

Motivation:Smaller, simpler coreChange in evaluator implementation requires less work11

shallow-derive

deriveSlide12

Derived Expression (1)

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

Derived Expression (2)

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

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)

)))

14Slide15

Evaluation of a case expression

Example: supporting

case

expression

Determining the evaluation rule for a

case

expression:

Evaluate the

control

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

15Slide16

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

Data Structues

17; 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))))Slide18

18

The environment modelA 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:5I

z:6

x:7

II

n:1

y:2

III

Env

A: (II,I)

Env

B: (III,I)

Env

C: (I)Slide19

19

The environment modelDefinitions: 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:5I

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

))Slide20

20

The environment modelProcedure 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)Slide21

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

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

B3

E2

(+ (sq x)

(sq y))

x:6

y:10

B2

E3

(* x

x

)

x:6

B1

E4

(* x

x

)

x:10

B1Slide23

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

y: 8

E1

(+ x y)Slide24

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

b: 5

c: 3

E1

(let…)

Parameters: (d e)

Body: (f e d)

d: 13

e: 40

E2

(f e d)

E3

x

:

13

y:

40

(+ x y)Slide25

25

Example (3)

> (define

 

fact (lambda (n

)

(if (= n 0)

1

(* n (fact (- n 1))))))

B1

B1

B1

B1

E1

GE

fact:

P:(n)

B:(if…)

(if …)

n:3

E2

(if …)

n:2

E3

(if …)

n:1

E4

(if …)

n:0Slide26

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:2

c:

P

:(fact-n-1)

B:

(c (* ..)

n:1

c:

P

:(fact-n-1)

B:

(c (* ..)

n:0

c:

fact-n-1 :

1

fact-n-1 :

1

x:2