Environment Diagrams. Administrivia. Agenda Step by Step Environment Diagram Stuff. The Rules. Practice it s on! What are The Rules?

Similar documents
Lexical vs. Dynamic Scope

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

SCHEME AND CALCULATOR 5b

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS 61A Discussion 8: Scheme. March 23, 2017

CS 314 Principles of Programming Languages. Lecture 16

Review Analyzing Evaluator. CS61A Lecture 25. What gets returned by mc-eval? (not analyzing eval) REVIEW What is a procedure?

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

ENVIRONMENT MODEL: FUNCTIONS, DATA 18

Discussion 12 The MCE (solutions)

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

CS61A Notes Week 1A: Basics, order of evaluation, special forms, recursion

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

61A Lecture 2. Wednesday, September 4, 2013

UNIVERSITY of CALIFORNIA at Berkeley Department of Electrical Engineering and Computer Sciences Computer Sciences Division

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Programming Project #4: A Logo Interpreter Summer 2005

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

CSc 520 Principles of Programming Languages

CS61A Notes Week 13: Interpreters

Intro. Scheme Basics. scm> 5 5. scm>

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 21 Scheme TODAY REVIEW: DISPATCH DICTIONARIES DISPATCH DICTIONARIES 7/27/2012

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Computer Science 21b: Structure and Interpretation of Computer Programs (Spring Term, 2004)


61A Lecture 2. Friday, August 28, 2015

CS 314 Principles of Programming Languages

SCHEME INTERPRETER GUIDE 4

Project 5 - The Meta-Circular Evaluator

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018

Administrivia. Simple data types


CS61A Midterm 2 Review (v1.1)

61A LECTURE 1 FUNCTIONS, VALUES. Steven Tang and Eric Tzeng June 24, 2013

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

Turtles All The Way Down

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Recap: Functions as first-class values

Project 5 - The Meta-Circular Evaluator

Principles of Programming Languages

! The simplest building blocks of a language. ! Compound elements are built from simpler ones

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Part II Composition of Functions

Implementing Continuations

An Explicit Continuation Evaluator for Scheme

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

CSC209. Software Tools and Systems Programming.

Key components of a lang. Deconstructing OCaml. In OCaml. Units of computation. In Java/Python. In OCaml. Units of computation.

The Interpreter + Calling Functions. Scheme was designed by people. The Interpreter + Calling Functions. Clickers. Parentheses Matter We asked scheme

Course Wrap-up. CSC207 Fall 2015

Functions & First Class Function Values

CSE341: Programming Languages Lecture 26 Course Victory Lap. Dan Grossman Spring 2016

Parsing Scheme (+ (* 2 3) 1) * 1

Overview. CS301 Session 3. Problem set 1. Thinking recursively

Open and respond to this poll: PollEv.com/comp110. Calling Methods Mr. Roboto

CS 320 Homework One Due 2/5 11:59pm

MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science. Issued: Wed. 26 April 2017 Due: Wed.

Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

CS 4349 Lecture August 21st, 2017

RLISTS AND THE ENVIRONMENT MODEL 9

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Project 2: Scheme Interpreter

CS : Data Structures

CS450: Structure of Higher Level Languages Spring 2018 Assignment 7 Due: Wednesday, April 18, 2018

Software Engineering. Functions

Programming Languages

Implementing Recursion

An Explicit-Continuation Metacircular Evaluator

CS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers

Variables and Bindings

CS1 Lecture 4 Jan. 23, 2019

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

cs173: Programming Languages Midterm Exam

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

public class Test { public static int i = 0;

Lesson 3: Accepting User Input and Using Different Methods for Output

Functional Programming. Pure Functional Languages

CMSC Computer Architecture Lecture 15: Memory Consistency and Synchronization. Prof. Yanjing Li University of Chicago

6.189 Project 1. Readings. What to hand in. Project 1: The Game of Hangman. Get caught up on all the readings from this week!

Administrivia. IBM Info Session Date: Wed,, Jan 13 Time: 5:30 7 pm Location: Wesbrook 100

Berkeley Scheme s OOP

Not really what happens in the computer. The Environment Model. (set-car! l 'z) (car l) z

MetacircularScheme! (a variant of lisp)

CSE341 Autumn 2017, Final Examination December 12, 2017

Transcription:

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 What are? They re a set of guidelines to follow when doing environment diagrams Follow 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!

EVERY expression typed into Scheme is either an atom or a list There is ALWAYS a current frame. Initially it s the global environment So believe it or not STk > (define (a) 3) ;; THIS is a LIST! Primitives live here : 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 : Lists (aka Compound Expressions) Take the car of it, if it s a special form go to SPECIAL FORMs of i.e. (define (foo x) 3) Otherwise you re calling a procedure! i.e. (square evaluate 3) So ALL subexpressions by 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 to evaluate the body of in f

: Special Forms ( DESUAR! Define: (define (foo x) ) (define foo (x) )) Write variable name in current frame Evaluate body by in (current : Special Forms procedure ( (params) body) Draw Bubbles! Left Bubble points to parameters and body Right Bubble points to (current frame) where it s being evaluated frame) Point 1 (variable name) 2 (evaled body) Let 1. ( ( (args) body) vals) just evaluate again by Params: Body: 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 So what s next? STk > (define x 3) Let s look at Is it an Atom? No! Is it a List? YES!

So let s take the car of the expression define lookie it s a special form! o to the Special Form section of! 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 (define x 3) So it says in write the variable name in the current frame so then evaluate the body Then point 1 2 x 3 Tada! So that s an easy variable binding Another Example example. Let s do one more easy procedure and then we ll do more problems! (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 DESUAR! (define f ( (x) (+ x 2)))

Another Example 1. Next Write the variable name in the current frame 2. Evaluate the body by f 1. ( (x) (+ x 2)) 2. It s a list, the car s a so. 3. Draw Bubbles! 3. Now Point 1 2 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 to eval the body of in F1 Returns 19 magically by + f F1 x 17 P: x B: (+ x 2) (+ x 2) (#[closure..] x 2) (#[closure...] 17 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)))) P: y, f (f B: (f 3) a list! 3)) It s Take the car, it s the special (+ x y))) P: x Now call the procedure B: (+ x y) Create a frame, F1 to where F1 Point s right bubble y points Bind 10 formal parameters y & f in F1, f make F1 the current frame Use to eval the body of in F1 form LET so DESUAR! ( ( (y f) (f 3)) 10 ( (x) (+ x y))) Evaluate all subexpressions ( (y f) (f 3)) 10 10 ( (x) So LET s do this ( ((y f) (f 3)) 10 ((x) (+ x y))) So the body of ïs: (f 3) It s a list, the car s not a special form, eval all subexp (3)Now call on a frame, 3Create F2 Point F2 to where s right F1 bubble formal pointsbind parameters in F2, make F2 the current frame Use to eval the body of y 10 f F2 x 3 P: y, f B: (f 3) P: x B: (+ x y) (+ x y) (#[closure] x y) (#[closure] 3 y) error! 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