metacircular evaluation sicp chapter 4 mark boady

Download Metacircular Evaluation SICP Chapter 4 Mark Boady

If you can't read please download the document

Upload: chad-russell

Post on 16-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

  • Slide 1
  • Metacircular Evaluation SICP Chapter 4 Mark Boady
  • Slide 2
  • Metacircular Evaluator An interpreter built for a language using the same language as it is interpreting. We will be looking at a Scheme interpreter written in scheme Read Chapter 4 of SICP for a more detailed look at this interpreter Why? Extending an interpreted language Debuggers
  • Slide 3
  • Jikes RVM A Java Research Virtual Machine written in Java The RVM runs on the JVM Used For Research in: Garbage Collection Dynamic Parallelization Machine Learning for dynamic compilation Dynamic Typing Distributed VM
  • Slide 4
  • Scheme Metacircular Evaluator Eval Takes an expression and an Environment Environment = Variable Names and Values Returns the result of evaluating the expression Apply Takes a Procedure and a list of arguments Produces an expression for eval (+ (* 4 5) 6)
  • Slide 5
  • Eval Definition (define (eval exp env) (cond ((self- evaluating? exp) exp) ((variable? exp) (lookup-variable- value exp env)) ((quoted? exp) (text-of- quotation exp)) ((assignment? exp) (eval- assignment exp env)) ((definition? exp) (eval- definition exp env)) ((if? exp) (eval- if exp env)) ((lambda? exp) (make-procedure (lambda- parameters exp) (lambda- body exp) env)) ((begin? exp) (eval- sequence (begin-actions exp) env)) ((cond? exp) (eval (cond- >if exp) env)) ((application? exp) (apply (eval (operator exp) en v) (list-of- values (operands exp) env))) (else (error "Unknown expression t ype -- EVAL" exp))))
  • Slide 6
  • (define (beval exp env) (cond ((boolean? exp) exp) ((conjunct? exp) (beval-and exp env)) ((disjunct? exp) (beval-or exp env)) ((negate? exp) (beval-not exp env)) ((variable? exp)... ; You'll need to supply your own function that takes a frame, returns the assoc. value (else (error "beval: illegal syntax")))))
  • Slide 7
  • Apply Definition (define (apply procedure arguments) (cond ((primitive- procedure? procedure) (apply-primitive- procedure procedure arguments)) ((compound- procedure? procedure) (eval- sequence (procedure- body procedure) (extend- environment (procedure- parameters procedure) arguments (proc edure- environment procedure)))) (else (error "Unknown procedure type -- APPLY" procedure))))
  • Slide 8
  • What do eval and apply do? Eval Takes the expression and determines what the expression is Passes the work to a helper function that evaluates the specific type For procedures, pass control to apply Apply Determine the type of procedure Add to the environment Pass control to Eval
  • Slide 9
  • Concept We want to eval (+ 4 5) ((application? exp) (apply (eval (operator exp) en v) (list-of-values (operands exp) env))) List-of-values evaluates the inputs to the procedure Apply performs the actual procedure With a primitive (like +) the underlying language handles it With a custom procedure, we need to pass back to eval
  • Slide 10
  • Metacircular
  • Slide 11
  • Basic Example (eval (+ a 5) ((a 10) (b 7)) Evaluate the expression a+5 with the variables a=10, b=7 Eval starts with a procedure and sees it is an apply Eval evaluates the inputs to the procedure Looks up variable values in this case Apply is asked to handle with (+ 10 5) Apply can determine 10+5=15 using the underlying system
  • Slide 12
  • Details Getting the arguments of a Procedure (apply (eval (operator exp) env) (list-of- values (operands exp) env))) (define (list-of-values exps env) (if (no- operands? exps) '() (cons (eval (first- operand exps) env) (list-of-values (rest- operands exps) env))))
  • Slide 13
  • If Statements (define (eval-if exp env) (if (true? (eval (if- predicate exp) env)) (eval (if- consequent exp) env) (eval (if- alternative exp) env))) Only Evaluate based on the true case (define (true? X) (not (eq? x false))) (define (if-alternative exp) (if (not (null? (cdddr exp))) (cadddr exp) 'false))
  • Slide 14
  • Evaluating Sequences (define (eval-sequence exps env) (cond ((last- exp? exps) (eval (first- exp exps) env)) (else (eval (first- exp exps) env) (eval-sequence (rest- exps exps) env)))) Evaluate the sequence of expressions in a procedure body
  • Slide 15
  • Assigning Variables (define (eval-assignment exp env) (set-variable- value! (assignment- variable exp) (eval (assignment- value exp) env) env) 'ok) set-variable-value! Bounds a value to a name
  • Slide 16
  • Expressions Basic functionality is implemented directly Assignment (define (assignment? exp) (tagged- list? exp 'set!)) (define (assignment- variable exp) (cadr exp)) (define (assignment- value exp) (caddr exp)) Derived Expressions Some functionality is implemented using existing functionality Cond can be transformed into if statements Let statements can be transformed in lambda expressions
  • Slide 17
  • Derived Expressions Let expressions are derived expressions, because (let (( )... ( )) ) is equivalent to ((lambda (... ) )... )
  • Slide 18
  • Derived Expressions (let ((a 4) (b 6)) (+ a b)) Value 10 ( (lambda (a b) (+ a b)) 4 6) Value 10 We can write a command that will translate a let to a lambda. Let eval handle the lambda command using existing tools.
  • Slide 19
  • Environment (lookup-variable-value ) returns the value that is bound to the symbol in the environment, or signals an error if the variable is unbound. (extend-environment ) returns a new environment, consisting of a new frame in which the symbols in the list are bound to the corresponding elements in the list, where the enclosing environment is the environment.
  • Slide 20
  • Environment (define-variable! ) adds to the first frame in the environment a new binding that associates the variable with the value. (set-variable-value! ) changes the binding of the variable in the environment so that the variable is now bound to the value, or signals an error if the variable is unbound.
  • Slide 21
  • Environments There are multiple scopes in the environment We make changes to the most recent Example: Initial memory: ( ( (a b) (5 7) ) ) A new function is called with inputs b=6, c=7 We push a new set of variables into the environment New Memory: ( ( (b c) (6 7) ) ( (a b) (5 7) ) ) When the function call is over we remove it from the environment New Memory: ( ( (a b) (5 7) ) )
  • Slide 22
  • Environments (define (lookup-variable-value var env) (define (env- loop env) (define (scan vars vals) (cond ((null? vars) (env-loop (enclosing- environment env))) ((eq? var (car vars)) (car va ls)) (else (scan (cdr vars) (cdr vals))))) (if (eq? env th e-empty- environment) (error "Unbound variable" var) (let ((fr ame (first-frame env))) (scan (frame- variables frame) (frame-values frame))))) (env- loop env))
  • Slide 23
  • Environments (define (lookup-variable-value var env) (env-loop env) ) (define (env-loop env) (if (eq? env the-empty-environment) (error "Unbound variable" var) (let ((frame (first-frame env))) (scan (frame-variables frame) (frame-values frame)) )) ) (define (scan vars vals) (cond ((null? vars) (env-loop (enclosing-environment env))) ((eq? var (car vars)) (car vals)) (else (scan (cdr vars) (cdr vals))) )
  • Slide 24
  • Primitives (define primitive-procedures (list (list 'car car) (list 'cdr cdr) (list 'cons cons) (list 'null? null?) (list '- -) (list '+ +) )) (define (apply-primitive-procedure proc args) (apply-in- underlying-scheme (primitive- implementation proc) args))
  • Slide 25
  • Running the Evaluator Load the File (load "ch4-mcevalM.scm") Initialize the Global Environment (define the-global-environment (setup-environment)) Initialize the Evaluator (driver-loop) We can now execute code!
  • Slide 26
  • Running (load "ch4-mcevalM.scm") (define the-global-environment (setup-environment)) (driver-loop) ;;; M-Eval input: (define (append x y) (if (null? x) y (cons (car x) (append (cdr x) y)))) ;;; M-Eval value: ok ;;; M-Eval input: (append '(a b c) '(d e f)) ;;; M-Eval value: (a b c d e f)
  • Slide 27
  • Driver Loop (define input-prompt ";;; M-Eval input:") (define output- prompt ";;; M-Eval value:") (define (driver- loop) (prompt-for-input input- prompt) (let ((input (read))) (let ((output (eval input the-global-environment))) (announce-output output- prompt) (user-print output))) (driver- loop)) (define (prompt-for- input string) (newline) (newline) (display string) (newlin e))
  • Slide 28
  • Environments (define (setup-environment) (let ((initial-env (extend-environment (primitive-procedure-names) (primitive-procedure-objects) the-empty-environment))) (define-variable! 'true true initial-env) (define-variable! 'false false initial-env) initial-env))