6.001 SICP 1 6.001 SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
(define applicative-eval (lambda (exp) (cond ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list.
1 Lecture 18 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
SICP Symbolic data Symbol: a primitive type Generalization Symbolic differentiation.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
The environment model evaluator and compiler 1 The env model evaluator Motivation In one word: Efficiency Saves repeated renaming and substitution: Using.
1 Saves repeated renaming and substitution: explicit substitution is replaced by variable bindings using new data structures (frame, environment). Can.
1 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure + symbol.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
1 The metacircular evaluator (Cont.) Defining new procedures (define (lambda? e) (tag-check e 'lambda)) (define (eval exp env) (cond ((number? exp)
Scheme in Scheme 1. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 15: Meta-Circular Evaluator 한 태숙.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
1 Subt. model interpreter: Structure of the interpreter ASP Derived expressions Core Test Data structures Utils A racket lib Proc / Primitive-Proc. Global.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
The environment-based operational semantics Chapter
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
1 Lecture 20 Lazy Evaluation Continued (4.2.1, 4.2.2) MC-eval examples from exams (Time permitting)
1 Subt. model interpreter: Introduction Abstract Syntax Parser Abstract Syntax Parser Derived Expressions Core Test Data Structures Utils (a racket lib)
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Env. Model Implementation & Analyzer Practice Session #10 Env. Model Implementation & Analyzer Practice Session #10.
1/ SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Making the environment model concrete Defining eval defines the language –Provides.
Scheme in Scheme 2. What’s next  Adding set!  Dynamic vs. lexical variable scope  Extending mcscheme v1 with libraries  Can mcscheme execute mcscheme?
1 Lecture 19 Review last lecture on evaluator, Dynamic Scoping Lazy Evaluation (4.2.1, 4.2.2)
CS61A Lecture Colleen Lewis. Clicker poll Do you feel comfortable posting questions to piazza? A)Yes with my classmates and instructors.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
(Thunking about Thunks)
Scheme in Scheme 1.
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Abstract Syntax cs7100 (Prasad) L7AST.
Edited by Original material by Eric Grimson
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
The interpreter.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Env. Model Implementation
Original material by Eric Grimson
Nondeterministic Evaluation
The Metacircular Evaluator
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture #9 מבוא מורחב.
Abstract Syntax cs7100 (Prasad) L7AST.
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
6.001 SICP Variations on a Scheme
6.001 SICP Interpretation Parts of an interpreter
Lecture 13: Assignment and the Environment Model (EM)
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
*Lecture based on notes from SICP
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements

6.001 SICP 2 Stages of an interpreter "(average 4 (+ 5 5))" (average4( +55 ) ) 4 symbol average 5 symbol Lexical analyzer Parser Evaluator Environment Printer "7" input to each stage

6.001 SICP 3 Arithmetic calculator (define (tag-check e sym) (and (pair? e) (eq? (car e) sym))) (define (sum? e) (tag-check e 'plus*)) (define (eval exp) (cond ((number? exp) exp) ((sum? exp) (eval-sum exp)) (else (error "unknown expression " exp)))) (define (eval-sum exp) (+ (eval (cadr exp)) (eval (caddr exp)))) (eval '(plus* 24 (plus* 5 6)))

6.001 SICP 4 Arithmetic calculator Want to evaluate arithmetic expressions of two arguments, like: (plus* 24 (plus* 5 6))

6.001 SICP 5 We are just walking through a tree … 24plus* 65 (eval ) 24plus* 65 sum? checks the tag

6.001 SICP 6 We are just walking through a tree … (eval-sum ) 24plus* (+ (eval 24) (eval )) (+ (eval 5) (eval 6))

6.001 SICP 7 The Evaluators 1.arithmetic calculator 2.add names: quote, define, environment is a table 3.add conditionals: eval-greater, eval-if, first apply 4.operators in environment: make-primitive, scheme-apply 5.environment as parameter to eval eval-special and lookup 6.compound procedures lambda evaluates to double bubble (list of params, body, encl.env.) apply on double bubble drops frame from encl.env., binds names to values, evals body in new env. environment is now a list of tables

6.001 SICP 8 Eval 6 review - eval (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)))) (else (error "unknown expression " exp)))) (define (define? exp) (tag-check exp ’define*)) (define (if? exp) (tag-check exp ’if*)) (define (application? e) (pair? e)) (define (lambda? e) (tag-check e 'lambda*))

6.001 SICP 9 Eval 6 review - env (define (extend-env-with-new-frame names values env) (let ((new-frame (make-table))) (make-bindings! names values new-frame) (cons new-frame env))) (define (make-bindings! names values table) (for-each (lambda (name value) (table-put! table name value)) names values)) ; the initial global environment (define GE (extend-env-with-new-frame (list 'plus* 'greater*) (list (make-primitive +) (make-primitive >)) nil))

6.001 SICP 10 Eval 6 review - lookup / define ; lookup searches the list of frames for the first match (define (lookup name env) (if (null? env) (error "unbound variable: " name) (let ((binding (table-get (car env) name))) (if (null? binding) (lookup name (cdr env)) (binding-value binding))))) ; define changes the first frame in the environment (define (eval-define exp env) (let ((name (cadr exp)) (defined-to-be (caddr exp))) (table-put! (car env) name (eval defined-to-be env)) 'undefined))

6.001 SICP 11 Eval 6 review - if (define (eval-if exp env) (let ((predicate (cadr exp)) (consequent (caddr exp)) (alternative (cadddr exp))) (let ((test (eval predicate env))) (cond ((eq? test #t) (eval consequent env)) ((eq? test #f) (eval alternative env)) (else (error "val not boolean: " predicate))))))

6.001 SICP 12 Eval 6 review - lambda (define (eval-lambda exp env) (let ((args (cadr exp)) (body (caddr exp))) (make-compound args body env))) ;; ADT that implements the “double bubble” (define compound-tag 'compound) (define (make-compound parameters body env) (list compound-tag parameters body env)) (define (compound? exp) (tag-check exp compound-tag)) (define (parameters compound) (cadr compound)) (define (body compound) (caddr compound)) (define (env compound) (cadddr compound))

6.001 SICP 13 Eval 6 review - apply (define scheme-apply apply) (define (apply operator operands) (cond ((primitive? operator) (scheme-apply (get-scheme-procedure operator) operands)) ((compound? operator) (eval (body operator) (extend-env-with-new-frame (parameters operator) operands (env operator)))) (else (error "operator not a procedure: " operator))))

6.001 SICP 14 Eval examples ; assume (table-put! (car GE) ‘times* (make- primitive *)) (eval '(define* z* (plus* 1 3)) GE) (eval '(define* mpy* (lambda* (x* y*) (times* x* y*))) GE) (eval '(mpy* 3 z*) GE)

6.001 SICP 15 evaluation example (eval '(define* z* (plus* 1 3)) GE) (define* z* (plus* 1 3)) | GE

6.001 SICP 16 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)))) (else (error "unknown expression " exp)))) (define (eval-define exp env) (let ((name (cadr exp)) (defined-to-be (caddr exp))) (table-put! (car env) name (eval defined-to-be env)) 'undefined))

6.001 SICP 17 evaluation example (eval '(define* z* (plus* 1 3)) GE) (define* z* (plus* 1 3)) | GE (plus* 1 3) | GE

6.001 SICP 18 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)) )) (else (error "unknown expression " exp))))

6.001 SICP 19 evaluation example (eval '(define* z* (plus* 1 3)) GE) (define* z* (plus* 1 3)) | GE (plus* 1 3) | GE 1 | GE 3 | GE

6.001 SICP 20 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)))) (else (error "unknown expression " exp))))

6.001 SICP 21 evaluation example (eval '(define* z* (plus* 1 3)) GE) (define* z* (plus* 1 3)) | GE (plus* 1 3) | GE 1 | GE ==> 1 3 | GE ==> 3

6.001 SICP 22 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)) )) (else (error "unknown expression " exp))))

6.001 SICP 23 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)) )) (else (error "unknown expression " exp))))

6.001 SICP 24 evaluation example (eval '(define* z* (plus* 1 3)) GE) (define* z* (plus* 1 3)) | GE (plus* 1 3) | GE 1 | GE ==> 1 3 | GE ==> 3 plus* | GE

6.001 SICP 25 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)))) (else (error "unknown expression " exp))))

6.001 SICP 26 evaluation example (eval '(define* z* (plus* 1 3)) GE) (define* z* (plus* 1 3)) | GE (plus* 1 3) | GE 1 | GE ==> 1 3 | GE ==> 3 plus* | GE ==> (primitive [prim +])

6.001 SICP 27 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)) )) (else (error "unknown expression " exp))))

6.001 SICP 28 (define scheme-apply apply) (define (apply operator operands) (cond ((primitive? operator) (scheme-apply (get-scheme-procedure operator) operands)) ((compound? operator) (eval (body operator) (extend-env-with-new-frame (parameters operator) operands (env operator)))) (else (error "operator not a procedure: " operator))))

6.001 SICP 29 evaluation example (eval '(define* z* (plus* 1 3)) GE) (define* z* (plus* 1 3)) | GE (plus* 1 3) | GE 1 | GE ==> 1 3 | GE ==> 3 plus* | GE ==> (primitive [prim +]) (scheme-apply [prim +] 1 3) ==> 4

6.001 SICP 30 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)))) (else (error "unknown expression " exp)))) (define (eval-define exp env) (let ((name (cadr exp)) (defined-to-be (caddr exp))) (table-put! (car env) name (eval defined-to-be env)) 'undefined))

6.001 SICP 31 evaluation example (eval '(define* z* (plus* 1 3)) GE) (define z* (plus* 1 3)) | GE (plus* 1 3) | GE 1 | GE ==> 1 3 | GE ==> 3 plus* | GE ==> (primitive [prim +]) (scheme-apply [prim +] 1 3) ==> 4 (table-put! (car GE) ‘z* 4) ;modified environment!

6.001 SICP 32 evaluation example ;assume (table-put! (car GE) ‘times* (make- primitive *)) (eval '(define* z* (plus* 1 3)) GE) (eval '(define* mpy* (lambda* (x* y*) (times* x* y*))) GE) (eval '(mpy* 3 z*) GE)

6.001 SICP 33 evaluation example (eval '(define* mpy* (lambda* (x* y*) (times* x* y*))) GE) (define* mpy* (lambda* (x* y*) (times* x* y*))) | GE

6.001 SICP 34 evaluation example (eval '(define* mpy* (lambda* (x* y*) (times* x* y*))) GE) (define* mpy* (lambda* (x* y*) (times* x* y*))) | GE (lambda* (x* y*) (times* x* y*)) | GE

6.001 SICP 35 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)) )) (else (error "unknown expression " exp)))) (define (eval-lambda exp env) (let ((args (cadr exp)) (body (caddr exp))) (make-compound args body env))) (define compound-tag 'compound) (define (make-compound parameters body env) (list compound-tag parameters body env))

6.001 SICP 36 evaluation example (eval* '(define* mpy* (lambda* (x* y*) (times* x* y*))) GE) (define* mpy* (lambda* (x* y*) (times* x* y*))) | GE (lambda* (x* y*) (times* x* y*)) | GE (make-compound ‘(x* y*) ‘(times* x* y*) GE)

6.001 SICP 37 evaluation example (eval '(define* mpy* (lambda* (x* y*) (times* x* y*))) GE) (define* mpy* (lambda* (x* y*) (times* x* y*))) | GE (lambda* (x* y*) (times* x* y*)) | GE (make-compound ‘(x* y*) ‘(times* x* y*) GE) ==> (list ‘compound ‘(x* y*) ‘(times* x* y*) GE)

6.001 SICP 38 evaluation example (eval '(define* mpy* (lambda* (x* y*) (times* x* y*))) GE) (define* mpy* (lambda* (x* y*) (times* x* y*))) | GE (lambda* (x* y*) (times* x* y*)) | GE (make-compound ‘(x* y*) ‘(times* x* y*) GE) ==> (list ‘compound ‘(x* y*) ‘(times* x* y*) GE) (table-put! (car GE) ‘mpy* (list ‘compound ‘(x* y*) ‘(times* x* y*) GE)) ;modified environment!

6.001 SICP 39 evaluation example ;assume (table-put! (car GE) ‘times* (make- primitive *)) (eval '(define* z* (plus* 1 3)) GE) (eval '(define* mpy* (lambda* (x* y*) (times* x* y*))) GE) (eval '(mpy* 3 z*) GE)

6.001 SICP 40 evaluation example (eval '(mpy* 3 z*) GE) (mpy* 3 z*) | GE

6.001 SICP 41 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)) )) (else (error "unknown expression " exp))))

6.001 SICP 42 evaluation example (eval '(mpy* 3 z*) GE) (mpy* 3 z*)| GE mpy* | GE ==> ‘(compound …) 3 | GE ==> 3 z* | GE ==> 4 (apply (list ‘compound ‘(x* y*) ‘(times* x* y*) GE) ‘( 3 4))

6.001 SICP 43 (define scheme-apply apply) (define (apply operator operands) (cond ((primitive? operator) (scheme-apply (get-scheme-procedure operator) operands)) ((compound? operator) (eval (body operator) (extend-env-with-new-frame (parameters operator) operands (env operator)))) (else (error "operator not a procedure: " operator))))

6.001 SICP 44 evaluation example (eval '(mpy* 3 z*) GE) (mpy* 3 z*) | GE mpy* | GE ==> ‘(compound …) 3 | GE ==> 3 z* | GE ==> 4 (apply (list ‘compound ‘(x* y*) ‘(times* x* y*) GE) ‘(3 4)) (times* x* y*) | (extend-env-with-new-frame ‘(x* y*) ‘(3 4) GE)) [E1--> (x*:3, y*:4) --> GE ] (times* x* y*) | E1 (scheme-apply [prim *] 3 4) ==> 12!

6.001 SICP 45 evaluation example (eval '(define* class (quote* is-over))) (define* class (quote* is-over)) | GE (quote* is-over) | GE

6.001 SICP 46 evaluation example (eval '(define* class (quote* is-over))) (define* class (quote* is-over)) | GE (quote* is-over) | GE unbound variable: quote*

6.001 SICP 47 (define (eval exp env) (cond ((number? exp) exp) ((quote? exp) (eval-quote exp)) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)) )) (else (error "unknown expression " exp)))) (define quote? (tag-check exp ‘quote*)) (define (eval-quote exp) (cadr exp))

6.001 SICP 48 evaluation example (eval '(define class (quote* is-over))) (define class (quote* is-over)) | GE (quote* is-over) | GE ==> is-over (table-put! (car GE) ‘class ‘is-over)

6.001 SICP 49 Summary Eval / Apply cycle is core of eval. eval calls apply with operator and args apply calls eval with expression and env no pending operations on either call – if expression is iterative so is evaluation

6.001 SICP 50 Exercise Extend the define* evaluator so define* also returns a value. e.g. (define* a 16) ==> 16

6.001 SICP 51 Eval 6 review - lookup / define ; lookup searches the list of frames for the first match (define (lookup name env) (if (null? env) (error "unbound variable: " name) (let ((binding (table-get (car env) name))) (if (null? binding) (lookup name (cdr env)) (binding-value binding))))) ; define changes the first frame in the environment (define (eval-define exp env) (let ((name (cadr exp)) (defined-to-be (caddr exp))) (table-put! (car env) name (eval defined-to-be env)) 'undefined))

6.001 SICP 52 Exercise Extend our language with the and operator. (and.. )

6.001 SICP 53 Exercise -- add AND (define (tag-check e sym) (and (pair? e) (eq? (car e) sym))) (define (and? e) (tag-check e 'and*)) (define (eval exp) (cond ((number? exp) exp) ((boolean? exp) exp) ((and? exp) (eval-and exp)) (else (error "unknown expression " exp)))) (define (eval-and exp) (define (try-next terms) (if (null? terms) #t (let ((first-term (eval (car terms)))) (if first-term (try-next (cdr terms)) #f)))) (try-next (cdr exp))) (eval '(and* #t #t))

6.001 SICP 54 Exercise - 4 Modify the interpreter so we can write infix assignments instead. (define MyVar 10)  (myVar := 10)

6.001 SICP 55 Eval 6 review - lookup / define ; lookup searches the list of frames for the first match (define (lookup name env) (if (null? env) (error "unbound variable: " name) (let ((binding (table-get (car env) name))) (if (null? binding) (lookup name (cdr env)) (binding-value binding))))) ; define changes the first frame in the environment (define (eval-define exp env) (let ((name (cadr exp)) (defined-to-be (caddr exp))) (table-put! (car env) name (eval defined-to-be env)) 'undefined))