1 nv model evaluator Motivation • Saves repeated renaming and substitution: Using new data structures (frame, environment), explicit substitution is replaced by variable bindings. • Can be utilized to gain further improvements that cannot be achieved using the substitution model (coming up next..)
The env model evaluator Motivation. The environment model evaluator and compiler. Saves repeated renaming and substitution: Using new data structures (frame, environment), explicit substitution is replaced by variable bindings. - PowerPoint PPT Presentation
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
The environment model evaluator and compilerThe env model evaluator Motivation
• Saves repeated renaming and substitution: Using new data
structures (frame, environment), explicit substitution is replaced
by variable bindings.
• Can be utilized to gain further improvements that cannot be
achieved using the substitution model (coming up next..)
2
The environment model evaluator and compilerThe env model evaluator Structure & implementation
Data structures
Abstract syntax parser
Core
Derived expressions
Special forms
No changes here!
• New data structures:• A closure• A binding• A frame• An environment
• Additional procedures to handle with these ADTs
• Procedure application and evaluation of expressions is now done with respect to a certain environment.
• No need for renaming & substitution procedures.
3
The environment model evaluator and compilerThe env model evaluator Structure & implementation
A closure: The data structure representing a procedure value includes, in
addition to its params and body, also the env in which the procedure has been
declared. This env is to be extended when applying the procedure.
Data structures package:
A binding: A pair consisting of a variable and its value.
#<primitive:+>
+
P:B:
env
4
The environment model evaluator and compilerThe env model evaluator Structure & implementation
A frame:
• A variable-value mapping. Every variable in a frame is bound to one value only.
• Implemented as a pair of lists: a list of variables and a list of values.
Data structures package:
#<primitive:+> #<procedure>
+ x
3
f
5
The environment model evaluator and compilerThe env model evaluator Structure & implementation
An environment:
• A finite sequence of frames in which the last frame is the global environment.
• Implemented as a list of frames.
Data structures package:E2
+ Implementation
…
…
… …
+
*
cons
x
y
3
4
* Implementation
cons Implementation
E3
E1
6
The environment model evaluator and compilerThe env model evaluator Structure & implementation
“Our” global environment is referenced by a variable in Dr.Scheme!
• A finite sequence of frames in which the last frame is the global environment.
The environment model evaluator and compilerSupporting a let* expression: As a special formThe environment model evaluator and compilerThe env model evaluator Adding let* as a special form
20
3. Add the required changes to evaluate a let* expression
Create a new, smaller let* expression and recursively evaluate it with respect to an extended env.
The environment model evaluator and compilerSupporting a let* expression: As a special formThe environment model evaluator and compilerThe env model evaluator Adding let* as a special form
Keep extending the given env with new frames. When vars-lst becomes empty, No more extensions are needed. It only remain to evaluate the body.
(let* ((v1 e1) (v2 e2) … (vn en)) b1 b2 … bm)
3. Add the required changes to evaluate a let* expression
Q: How is option B different from option A?A: In option A we create code in run-time (when calling make-let*).
The environment model evaluator and compilerSupporting a let* expression: As a special formThe environment model evaluator and compilerThe env model evaluator Adding let* as a special form
22
1. Add the required ADT procedures to the ASP: Same as earlier!
ASPASP
Derived expressions
Derived expressions
Core
Special forms
Data Structures(+GE)
2. Add the required changes to identify a let* expression as a derived
3. Support a let* expression in the shallow-derive procedure
How will let*->nested-let work?
The environment model evaluator and compilerSupporting a let* expression: As a special formThe environment model evaluator and compilerThe env model evaluator Adding let* as a derived exp
23
4. Provide with a procedure to derive a let* expression(define (let*->nested-let exp) (let ( (bindings (let*-bindings exp)) (body (let*-body exp)) ) (if (null? (cdr bindings)) (derive (make-let bindings body)) (derive (make-let (make-let-bindings (list (let*-first-variable exp))
Q1: Why do we use derive in the body of let*->nested-let?A1: We derive let* into nested let expressions, but let is a derived expression itself. We must derive it so the evaluator can handle it.
Q2: In the alternative clause, we enclose the body parameter for make-let in a list. Why?A2: Since the body may include a sequence of expressions, make-let expects a list. let*->nested-let returns a single expression, so we must enclose it within a list to be given as an argument to make-let.
The environment model evaluator and compilerSupporting a let* expression: As a special formThe environment model evaluator and compilerThe env model evaluator Adding let* as a derived exp
24
• Env-eval combines expression analysis and evaluation in the same process.
• This leads to inefficiency due to repeated analysis (repeated calls for ASP procedures).
• We avoid this by separating analysis from evaluation.
• This could not be achieved in the substitution model, where every application involves
substitution and analysis.
The environment model evaluator and compilerSupporting a let* expression: As a special formThe environment model evaluator and compilerThe env model compiler What is it good for?
We analyze our code, producing a procedure that is ready for execution, given an
environment argument. The procedure returns the evaluation of the analyzed code with
respect to the given environment. No analysis is performed during the evaluation.
The analysis of an if expression is performed only once. It saves us the calls for ASP procedure for the evaluation during run-time. The returned procedure performsevaluation only.
The environment model evaluator and compilerSupporting a let* expression: As a special formThe environment model evaluator and compilerThe env model compiler Adding a let* expression
What should be done to add let* as a derived expression?Same thing we did with the evaluator!
How would we support a let* expression as a special form?1. Add the required ADT procedures to the ASP
2. identify a let* expression as a special form
3. Support a let* expression in the analyze-special-form procedure:
28
The environment model evaluator and compilerSupporting a let* expression: As a special formThe environment model evaluator and compilerThe env model compiler Adding a let* expression
4. Provide with a procedure to analyze a let* expression:• Take an evaluator procedure that evaluates a let* expression.• Curry the env parameter.• inductively apply analyze to all sub expressions.
It produces code on run-time, which we cannot analyze. Analysis is
performed ONLY on static time, before evaluation!
29
The environment model evaluator and compilerSupporting a let* expression: As a special formThe environment model evaluator and compilerThe env model compiler Adding a let* expression
The second version for eval-let* (option B) translated into analyze-let*:(define (analyze-let* exp) (let ( (vars (let*-variables exp)) (vals (map analyze (let*-initial-values exp))) (body (analyze-sequence (let*-body exp))) ) (letrec ((helper (lambda (vars-lst vals-lst env) (if (null? vars-lst) (body env) (helper (cdr vars-lst) (cdr vals-lst) (extend-env (make-frame (list (car vars-lst)) (list ((car vals-lst) env))) env)))))) (lambda (env) ;letrc body – carrying (helper vars vals env)))))
Note: If we were to support let* as a derived expression, we would have used derive-analyze-eval