CS61A Lecture 23 2011-07-28 Colleen Lewis 1. Clicker poll Are you in a two person group for project 4? A)Yes – I have my partner B)No – I plan to work.

Slides:



Advertisements
Similar presentations
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Advertisements

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.
Scheme in Scheme?!? …the metacircular evaluator….
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
1 Programming Languages (CS 550) Operational Semantics of Scheme using Substitution and the Lambda Calculus Jeremy R. Johnson TexPoint fonts used in EMF.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
1 Programming Languages (CS 550) Lecture 7 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts used in EMF. Read.
מבוא מורחב למדעי המחשב תרגול 13 Lazy Evaluation. Lazy Evaluation Implementation Two types of values – Delayed Values – Actual Values Introduce two functions.
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.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Metacircular Evaluator 4.1, pages definitions file on web 2.
1 Lecture 18 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Picking and Choosing… …Amb Above the Line. Intro to Nondeterminism So before we programmed to compute an answer to a problem. What if we set up a system.
Lexical vs. Dynamic Scope …where do I point to?. Intro… Remember in Scheme whenever we call a procedure we pop a frame and point it to where the procedure.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
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.
Scheme More MCE examples. Q1 new special form which defines global variables (static ) search the global environment – Variable exists: does nothing,
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.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 17: Nondeterministic Computing 한 태숙.
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.
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
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 29: Typed Scheme MC Escher, Liberation.
1 Lecture 20 Lazy Evaluation Continued (4.2.1, 4.2.2) MC-eval examples from exams (Time permitting)
1 Read-Eval-Print Loop (define (driver-loop) (prompt-for-input input-prompt) (let ((input (read))) (let ((output (eval input the-global-env))) (announce-output.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Lecture 19 Dynamic Scoping Lazy Evaluation (4.2.1, 4.2.2)
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?
CS61A Lecture Colleen Lewis. Clicker poll Where do you think you’re learning the most? I assume you’ll learn the most in lab & disc (so.
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.
CS61A Lecture Colleen Lewis. Clicker poll How do you think the midterm compared to previous midterms online? A)Harder B)A little harder.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 27: Types of Types “It would appear.
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
6.001 SICP Variations on a Scheme
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
Lecture 28: Types of Types
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
Explicit Application of Procedures, and Explicit Evaluation
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
6.001 SICP Variations on a Scheme
6.001 SICP Interpretation Parts of an interpreter
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:

CS61A Lecture Colleen Lewis 1

Clicker poll Are you in a two person group for project 4? A)Yes – I have my partner B)No – I plan to work individually C)No – But I want a partner 2

scheme-1 Review (define (scheme-1) (display "Scheme-1: ") (flush) (print (eval-1 (read))) (scheme-1)) Whatever you typed in is treated as a list eval-1 evaluated these lists Infinite loop

Metacircular Evaluator (MCE) read-eval-print loop (define (driver-loop) (prompt-for-input input-prompt) (let ((input (read))) (let ((output (mc-eval input the-global-environment))) (announce-output output-prompt) (user-print output))) (driver-loop)) 4 The big idea!

Today’s Plan Is mc-eval basically the same as eval-1 ? – Yes Is mc-apply basically the same as apply-1 ? – Yes How is this different than scheme-1 ? – Everything has its own ADT! – We have environments and can define things! 5 The big idea!

(define (mc-eval exp env) (cond ((self-evaluating? exp)... ((variable? exp)... ((quoted? exp)... ((assignment? exp)... ((definition? exp)... ((if? exp)... ((lambda? exp)... ((begin? exp)... ((cond? exp)... ((application? exp)... (else (error “what?")))) 6 New (not very interesting)

(define (mc-eval exp env) (cond ((self-evaluating? exp)... ((variable? exp)... ((quoted? exp)... ((assignment? exp)... ((definition? exp)... ((if? exp)... ((lambda? exp)... ((begin? exp)... ((cond? exp)... ((application? exp)... (else (error “what?")))) 7 (mc-eval '(sq 3) '()) Is caught by: A. quoted? B. lambda? C. application? (mc-eval 'x '()) Is caught by: A. self-evaluating? B. variable? C. quoted?

More things create/use ADTs (makes not-new stuff different) STk> (eval-1 '(lambda (x) (* x x))) (lambda (x) (* x x)) STk> (mc-eval '(lambda (x) (* x x)) '()) (procedure (x) ((* x x)) ()) 8 ADT overkill? This is tagged with procedure, but we already had it tagged with lambda.

What do environments look like? 9

Frames in MCE (below the line) ((x y). (2 4)) or ((x y) 2 4 ) 10 Global x: 2 y: 4 E1 a: 5 b: 7 c: 3 ((a b c). (5 7 3)) or ((a b c) ) (define (frame-variables frame) (car frame)) (define (frame-values frame) (cdr frame))

Environments (below the line) List of frames! (define the-empty-environment '()) (extend-environment '(x y) ;; vars '(2 4) ;; vals the-empty-environment) ;; base-env (define (extend-environment vars vals base-env) (cons (make-frame vars vals) base-env)) 11 Error checking omitted

Environments (below the line) List of frames! (define the-empty-environment '()) (extend-environment '(x y) ;; vars '(2 4) ;; vals the-empty-environment) ;; base-env 12 carcdr ((x y).(1 2)) Global x: 2 y: 4 Frame Environment

Environments (Below the line) 13 Global x: 2 y: 4 E1 a: 5 b: 7 c: 3 carcdr ((x y).(1 2)) carcdr ((a b c).(5 7 3))

14 Global x: 2 y: 4 E3 a: 5 b: 7 c: 3 carcdr ((x y).(1 2)) carcdr ((a b c).(5 7 3)) E1 E2 carcdr (().()) E3 is the current frame. Draw the environment. How many elements are in the list you made? A. 1 B. 2 C. 3 D. 4 E. 5

How do we look-up values from environments? (define (scan vars vals) (cond ((null? vars)...) ;; look in enclosing env. ((eq? var (car vars)) (car vals)) (else (scan (cdr vars) (cdr vals))))) 15

How do we look-up values from environments? (continued) (define (lookup-variable-value var 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))))) (env-loop env)) 16

How do we look-up values from environments? (define (scan vars vals) (cond ((null? vars) (env-loop (enclosing-environment env))) ((eq? var (car vars)) (car vals)) (else (scan (cdr vars) (cdr vals))))) 17

How many times is scan called? A.Once for each frame B.Once for each variable in the environment C.Once for each variable you are looking up D.OTHER Write a definition for (enclosing-environment env) 18

What does this environment look like? 19 STk>(define a 3) STk>(define sq (lambda (x) (* x x))) Params: x Body: (* x x) Global a: 3 sq: carcdr ((a sq).(3 ???))

What is a procedure? STk> (mc-eval '(lambda (x) (* x x)) '(((a) 3))) (procedure (x) ((* x x)) (((a) 3))) 20 carcdr ((a).(3)) Params: x Body: (+ x x) Global a: 3 procedure carcdr (x) carcdr ((* x x)) carcdr carcdr

What does this environment look like? 21 STk>(define a 3) STk>(define sq (lambda (x) (* x x))) Params: x Body: (* x x) Global a: 3 sq: carcdr ((a sq).(3 (procedure (x) ((* x x))___))) The environment

Printing Environments is… A.going to be really helpful to see what is going on in mc-eval B.not going to be possible because they are really big C.not going to be possible because they contain infinite structures 22

What would scheme print (wwsp)? (define (my-scope x) (lambda () x)) (define (current-scope x thunk) (thunk)) STk> (define my-thunk (my-scope 3)) my-thunk STk> (current-scope 4 my-thunk) Prints: A. 3 B. 4 C. error D. ??? 23

Lexical vs. Dynamic Scope Scheme – Lexical Scope – Extend the frame that the procedure was created in Logo – Dynamic Scope – Extend the frame that the procedure was called from 24

LOGO Demo 25

Commands versus Operations In LOGO procedures are divided into – Operations – return values – Commands – don’t return values You have to start each instruction with a command print sum

Parentheses can be used print (sum ) print 3*(4+5) 27

Variables vs. Procedures We can have a function and a variable with the same name in LOGO. How to make a variable: make "x 10 print :x make "sum 15 print sum :x :sum 28

Quoting things in LOGO We use " instead of single quotes. make "name "colleen print :name make "my-sent [a b c] print :my-sent 29

There are no special forms! We can just quote things by putting them in [] and then they won’t be evaluated –WOW! ifelse 2=3 [print "hi] [print "bye] 30

Defining a function We use the word “to” - “to teach logo a new word”. ? to add-up :x :y :z > sum :x :y :z > end ? print add-up

Scope - We have frames We have frames so calling a function creates a new bind – it doesn’t change the global frame ? make "x 10 ? to add-up :x :y :z > sum :x :y :z > end ? print add-up ? print :x 32

New frames extend the CURRENT environment (not the environment in which they were created) ? make "pi 3.14 ? to area > :radius * :pi > end ? to mess-up :pi > area 5 > end ? mess-up 4 33 THE BIG IDEA! Will LOGO return: A. 20 B C. ??

Solutions 34

35 Global x: 2 y: 4 E3 a: 5 b: 7 c: 3 carcdr ((x y).(1 2)) carcdr ((a b c).(5 7 3)) E1 E2 carcdr (().()) E3 is the current frame. Draw the environment. How many elements are in the list you made? A. 1 B. 2 C. 3 D. 4 E. 5