Administrivia Project 3 Part A due 3/29 (Monday after SB) Part B due 4/5 (a week after)  Everyone with a partner that wants a partner?  Extra Office.

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.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
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.
Chapter 5 Part 1 COSI 11a Prepared by Ross Shaull.
Six compound procedures and higher-order procedures.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
CSC 160 Computer Programming for Non-Majors Chapter 2: Numbers, Expressions, and Simple Programs Prof. Adam M. Wittenstein
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.
Eight compound procedures and higher-order procedures.
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.
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.
Thirteen conditional expressions: letting programs make “decisions”
Six compound procedures and higher-order procedures.
Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
Functional Programming in Scheme and Lisp.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Lecture 14: Assignment and the Environment Model (EM)
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
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.
The Environment Model an extension of the substitution model more "operational" fully explains static scoping and the process by which variable names are.
Lecture 3: Rules of Evaluation CS150: Computer Science
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.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Coding – Week 2 Functions, Arrays, and Objects. Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Scheme in Scheme 1.
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Environments and the Contour Model
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Edited by Original material by Eric Grimson
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
6.001 SICP Variations on a Scheme
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
The Metacircular Evaluator
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
6.001 SICP Environment model
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 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
6.001 SICP Environment model
6.001 SICP Variations on a Scheme
6.001 SICP Interpretation Parts of an interpreter
6.001 SICP Environment model
Lecture 3: Rules of Evaluation CS200: Computer Science
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
Introduction to the Lab
Common Lisp II.
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Administrivia Project 3 Part A due 3/29 (Monday after SB) Part B due 4/5 (a week after)  Everyone with a partner that wants a partner?  Extra Office Hours on Sunday 3/28 in C50 from 1pm Midterm 3 on 4/14 (appr. 1 week after Part B is due)  Covers OOP to Concurrency (week 8-11) What to expect on week we come back…  Scheme-2 (the last of the interpreters before MCE)  Vectors/Arrays  Mutation…the list kind

Environment Diagrams… Practice…it’s on!

Agenda Step by Step Environment Diagram Stuff Practice, Practice, Practice

The Rules What are “The Rules”?  They’re a set of guidelines to follow when doing environment diagrams  Follow The Rules and you’ll be an environment diagram MASTER!  Once you’ve mastered it, you never need to look at them again (But keep them for reference)  Remember… DON’T THINK, JUST DO!

The Rules EVERY expression typed into Scheme is either an atom or a list So believe it or not… STk > (define (a) 3) ;;  THIS is a LIST!

The Rules There is ALWAYS a current frame. Initially it’s the global environment G Primitives live here

The Rules: Atoms Self-Evaluating: Numbers, Strings, #t, #f Example: STk > 1 ;; no need to do anything 1 Symbols: (aka variables) look for first binding Example: STk > x ;; say if (define x 3) 3

The Rules: Lists (aka Compound Expressions) Take the car of it, if it’s a special form go to SPECIAL FORMs of The Rules i.e. (define (foo x) 3) Otherwise you’re calling a procedure! i.e. (square 3)  So evaluate ALL subexpressions by The Rules then...  If car is primitive  apply by magic i.e. (+ 2 3)  *poof* it returns 5  If car is a λ then… Create frame, f Point f to where λ points Bind formal parameters of λ in f & make f the current frame Use The Rules to evaluate the body of λ in f

The Rules: Special Forms DESUGAR!  Define: (define (foo x) …)  (define foo (λ (x) …)) 1. Write variable name in current frame 2. Evaluate body by The Rules in CF (current frame) 3. Point 1 (variable name)  2 (evaled body)  Let ( (λ (args) body) vals)  just evaluate again by The Rules

The Rules: Special Forms  λ  procedure (λ (params) body) Draw Bubbles! Left Bubble points to parameters and body Right Bubble points to CF (current frame) where it’s being evaluated CF Params: … Body: …

Super Simple Example What happens when we type: STk > (define x 3) First off everything from the STk prompt will be evaluated starting from the global environment. So this expression is saying… “evaluate the expression (define x 3) in the global environment”

Super Simple Example So what’s next? STk > (define x 3) Let’s look at The Rules Is it an Atom? Is it a List? No! YES!

Super Simple Example So let’s take the car of the expression define  lookie it’s a special form! Go to the Special Form section of The Rules! No need for any desugaring because the first argument to define is a variable not another list like (define (x) 3), so let’s continue on…

Super Simple Example (define x 3) So it says in The Rules write the variable name in the current frame so… then evaluate the body… Then point 1  2 G x3

Tada! So that’s an easy variable binding example. Let’s do one more easy procedure and then we’ll do more problems!

Another Example (define (f x) (+ x 2)) So what do we do first with this expression? First off, it’s a list, second off the car is define so… DESUGAR! (define f (λ (x) (+ x 2)))

Another Example 1.Next Write the variable name in the current frame 2.Evaluate the body by The Rules  (λ (x) (+ x 2))  It’s a list, the car’s a λ so….  Draw Bubbles! 3.Now Point 1  2 G f P: x B: (+ x 2)

Another Example…Call So let’s call the procedure STk > (f 17) So what happens now?  It’s a list, the car is NOT a special form, so evaluate all the subexpressions  f is a procedure (let’s call this )  17 is self-evaluating  So now you have ( 17 )

Another Example…Call So take the car, it’s just a λ, so  Create a frame, F1  Point F1 to where the right bubble of points  Bind formal parameters of in F1, make F1 the current frame  Use The Rules to eval the body of in F1  Returns 19 magically by ‘+’ G f P: x B: (+ x 2) F1 x  17 CF (+ x 2) (#[closure..] x 2) (#[closure...] 17 2)

So LET’s do this… Remember that you couldn’t do (let ((y 10) (f (λ (x) (+ x y)))) (f 3)) just through intuition…but now see the REAL reason why…let’s draw the environment diagram for this expression.

So LET’s do this… (let ((y 10) (f (λ (x) (+ x y)))) (f 3)) It’s a list! Take the car, it’s the special form LET so… DESUGAR! ( (λ (y f) (f 3)) 10 (λ (x) (+ x y))) Evaluate all subexpressions  (λ (y f) (f 3))   10  10  (λ (x) (+ x y)))  ♫ Now call the procedure  Create a frame, F1  Point F1 to where ’s right bubble points  Bind formal parameters y & f in F1, make F1 the current frame  Use The Rules to eval the body of in F1 G P: y, f B: (f 3) P: x B: (+ x y) ♫ CF F1 y  10 f  ♫

So LET’s do this… ( (λ (y f) (f 3)) 10 (λ (x) (+ x y))) So the body of is: (f 3) It’s a list, the car’s not a special form, eval all subexp  (♫ 3) Now call ♫ on 3  Create a frame, F2  Point F2 to where ♫’s right bubble points  Bind formal parameters in F2, make F2 the current frame  Use The Rules to eval the body of ♫ G P: x B: (+ x y) P: y, f B: (f 3) F1 y  10 f  ♫ ♫ F2 x  3 (+ x y) (#[closure] x y) (#[closure] 3 y) error! CF

Okay that’s enough of that… So hopefully you’re comfy with easy problems. Now let’s do some more…don’t you love me