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

Similar documents
Lexical vs. Dynamic Scope

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

Discussion 12 The MCE (solutions)

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

(scheme-1) has lambda but NOT define

4.2 Variations on a Scheme -- Lazy Evaluation

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

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

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

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

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

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

CS61A Midterm 2 Review (v1.1)

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

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

Fall Semester, Lecture Notes { November 12, Variations on a Scheme Nondeterministic evaluation

From Syntactic Sugar to the Syntactic Meth Lab:

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

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

SCHEME AND CALCULATOR 5b

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

Functional Programming. Pure Functional Programming

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

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions.

The Eval/Apply Cycle Eval. Evaluation and universal machines. Examining the role of Eval. Eval from perspective of language designer

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

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

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Project 2: Scheme Interpreter

Code example: sfact SICP Explicit-control evaluator. Example register machine: instructions. Goal: a tail-recursive evaluator.

An Explicit Continuation Evaluator for Scheme

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington

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

TAIL RECURSION, SCOPE, AND PROJECT 4 11

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

Func%onal Programming in Scheme and Lisp

6.001: Structure and Interpretation of Computer Programs

CS 275 Name Final Exam Solutions December 16, 2016

Func%onal Programming in Scheme and Lisp

An Explicit-Continuation Metacircular Evaluator

Building a system for symbolic differentiation

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

Berkeley Scheme s OOP

CS3L Summer 2011 Final Exam, Part 2 You may leave when finished; or, you must stop promptly at 12:20

INTERPRETATION AND SCHEME LISTS 12

Introduction to Scheme

CS 314 Principles of Programming Languages. Lecture 16

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


MetacircularScheme! (a variant of lisp)

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

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

CSc 520 Principles of Programming Languages

A Small Interpreted Language

Building a system for symbolic differentiation

STRUCTURE AND INTERPRETATION COMPUTER PROGRAMS >>> COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 23 Py TODAY REVIEW: INTERPRETATION 7/26/2012 READ PRINT

STRUCTURE AND INTERPRETATION COMPUTER PROGRAMS COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 23 Py TODAY 7/26/2012

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

INTERPRETATION AND SCHEME LISTS 12

Discussion 4. Data Abstraction and Sequences

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

(first (hello)) (hello) CS61A Lecture 2. Computer Science. Hierarchy of Abstraction. Functions. REVIEW: Two Types of( s so far

Programming Project #4: A Logo Interpreter Summer 2005

How many ways to make 50 cents? first-denomination Solution. CS61A Lecture 5. count-change. cc base cases. How many have you figured out?

CS61A Lecture 23 Py. Jom Magrotker UC Berkeley EECS July 26, 2012

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

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

6.945 Adventures in Advanced Symbolic Programming

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

Scheme Quick Reference

Sample midterm 1 #1. Problem 1 (What will Scheme print?).

bindings (review) Topic 18 Environment Model of Evaluation bindings (review) frames (review) frames (review) frames (review) x: 10 y: #f x: 10

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

Administrivia. Simple data types

CS 360 Programming Languages Interpreters

CS3: Introduction to Symbolic Programming. Lecture 8: Introduction to Higher Order Functions. Spring 2008 Nate Titterton

An Introduction to Scheme

Functional Programming. Pure Functional Languages

O(1) How long does a function take to run? CS61A Lecture 6

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

An introduction to Scheme

Lecture Notes on Lisp A Brief Introduction

DRAWING ENVIRONMENT DIAGRAMS

C311 Lab #3 Representation Independence: Representation Independent Interpreters

Scheme Quick Reference

Object Oriented Programming (OOP) Overview CS61A Lecture 14

Lecture 3: Expressions

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

61A Lecture 2. Wednesday, September 4, 2013

CS61A Notes Week 13: Interpreters

A Brief Introduction to Scheme (II)

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013

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

Turtles All The Way Down

CSE 505: Concepts of Programming Languages

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

Transcription:

2 1 Review Analyzing Evaluator CS61A Lecture 2011-08-02 Colleen Lewis What s look like What the output of analyze was Fact: The body of a lambda gets analyzed! We can give names to analyzed lambdas What gets returned by mc-eval? (not analyzing eval) REVIEW What is a? 4 STk> (mc-eval '(lambda (* x x)) '(((a) ))) A.( ((* x x)) (((a) ))) B.( (* x x) (((a) ))) C.(lambda ((* x x)) (((a) ))) D.(lambda (* x x) (((a) ))) E. Other (or no clue!) STk> (mc-eval '(lambda (* x x)) '(((a) ))) ( ((* x x)) (((a) ))) Global a: ((* x x)) Params: x Body: (+ x x) ((a).()) ((self-evaluating? exp) exp)... (define (analyze exp) ((self-evaluating? exp) (analyze-self-evaluating exp))... 5 The body of a lambda gets analyzed! STk> (mc-eval '(lambda ) '(((a) ))) ( ((λ(env) )) (((a) ))) 6 (define (analyze-self-evaluating exp) (lambda (env) exp)) List representing expression analyze STK Scheme expression (λ(env) Global a: ((λ(env) )) ((a).()) 1

(analyze '(if #t 4)) (define (analyze exp) ((self-evaluating? exp) (analyze-self-evaluating exp)) ((if? exp) (analyze-if exp))... (define (analyze-if exp) (let ((pproc (analyze (if-predicate exp))) (cproc (analyze (if-consequent exp))) (aproc (analyze (if-alternative exp)))) (lambda (env) (if (true? ( pproc (λ(e) ) (λ(e) #t) env)) ( cproc env) (λ(e) 4) ( approc env))))) 7 The body of a lambda gets analyzed! STk>(mc-eval '(lambda '(if #t 4))'(((a) ))) ( ( ) (((a) ))) (if ((λ (e) ) env) ((λ (e) 4) env)) ((if ((λ (e) ) env) ((λ (e) 4) env))) ((a).()) 8 The body of a lambda gets analyzed! STk>(mc-eval '(define f (lambda '(if #t 4)) '(((a) ))) 9 The body of a lambda gets analyzed! STk>(mce) ;;; M-Eval input: (f 1) ((analyze exp) env)) If we call f many times we save time! 10 f f ((if ((λ (e) ) env) ((λ (e) 4) env))) ((f((a).()). ( )) ((if ((λ (e) ) env) ((λ (e) 4) env))) ((f((a).()). ( )) 12 11 I ll get the actual value of arguments once they re really needed Lazy Evaluator (played by Lazy Smurf) Understanding Lazy [eval/smurf] Thunk ADT A Thunk List not a real scheme thunk Storing the environment If we re going to delay the evaluation of arguments we need to keep track of the environment where they should be evaluated. Delay arguments to user-defined s! Not to primitive s 2

Thunk ADT (not a STk REAL thunk!) (define (delay-it exp env) (list 'thunk exp env)) Thunk list thunk (define (thunk? obj) exp env (tagged-list? obj 'thunk)) (define (thunk-exp thunk) (cadr thunk)) (define (thunk-env thunk) (caddr thunk)) exp is(a) list representing an expression or (B) REAL Scheme? 1 What would lazy [eval/smurf] do? STk> (define (square x) (* x x)) STk> (square (+ 2 )) (square (+ 2 )) (square (+ 2 )) (square 5) (* (+ 2 ) (+ 2 )) (* 5 5) (* 5 5) A. Applicative Order B. Normal Order STk> (define x ) STk> (define (square x) (* x x)) STk> (square (+ 2 x)) (square (+ 2 x)) BEFORE we call square: figure out arguments! (square (+ 2 x)) If we re going to delay-it we need to keep track of the environment! (define (delay-it exp env) (list 'thunk exp env)) 16 (square 5) (* (+ 2 x) (+ 2 x)) (* 5 5) Applicative Order???? This x should come from global! Normal Order thunk (+ 2 x) When I get forced: evaluate the exp in this environment env How (regular) mc-eval evaluated args?!? 17 How lazy [smurf] mc-eval evaluates args?!? 18... ((application? exp) (mc-apply (mc-eval (operator exp) env) (list-of-values (operands exp) env))) (define (list-of-values exps env) (if (no-operands? exps) '() (cons (mc-eval (first-operand exps) env) (list-of-values (rest-operands exps)env))))... ((application? exp) (mc-apply (mc-eval (operator exp) env) (list-of-values (operands exp) env))) We re going to change the range of mc-eval so we ll have to change this too.

Regular mc-apply 19 This is the lazy version how many changes? A. 1 B. 2 C. D. 4 20 (define (mc-apply arguments) ((primitive-? ) (apply-primitive- arguments)) ((compound-? ) (eval-sequence (-body ) (extend-environment (-parameters ) arguments (-environment )))) (else (error "Unknown" )))) ((show 2)(show )) env (define (mc-apply arguments env) ((primitive-? ) (apply-primitive- (list-of-arg-values arguments env))) ((compound-? ) (eval-sequence (-body ) (extend-environment (-parameters ) (list-of-delayed-args arguments env) (-environment )))) (else (error "Unknown" )))) DO evaluate the arguments The lazy mc-apply Primitive mc-apply Don t evaluate the arguments Compound (User defined) Procedure 21 In the lazy version Where do arguments get delayed? A. In mc-eval B. In mc-apply C. In both D. In neither E.??? 22 In the REGULAR version Where do arguments get evaluated? A. In mc-eval B. In mc-apply C. In both D. In neither E.??? 2 In the lazy version Where do arguments get evaluated? A. In mc-eval B. In mc-apply C. In both D. In neither E.??? 24 4

STk> How many of these are different in Normal vs. Applicative order? (invent one example that isn t and one that is) STk> (define x ) x STk> x STk> 'x x STk> (set! x 4) STk> (if #t 4) STk> (lambda x) #[closure arglist= 7ff27c98] STk> (begin 2 ) A. 0 B. 1-2 C. -5 D. 6-8 E.?? ((self-evaluating? exp)... ((variable? exp)... ((quoted? exp)... ((assignment? exp)... ((definition? exp)... ((if? exp)... ((lambda? exp)... ((begin? exp)... (? exp)... ((application? exp)... (else (error what?")))) How many times might we be lazy (and delay stuff)? A. 0 B. 1-2 C. -5 D. 6-8 E.?? 26 A problem with delaying stuff 27 Remember we delayed args to compound s (user-defined) 28 STk> (load "lazy.scm") STk> (define g-env (setup-environment)) g-env STk> (mc-eval '((lambda x) (+ 2 )) g-env) (thunk (+ 2 ) env ) User-defined : Don t evaluate the arguments (define (mc-apply arguments env) ((primitive-? ) (apply-primitive- (list-of-arg-values arguments env))) ((compound-? ) (eval-sequence (-body ) (extend-environment (-parameters ) (list-of-delayed-args arguments env) (-environment )))) (else (error "Unknown" )))) What can be returned by the lazy mc-eval function What can be ret A. Values B. Lists C. Thunk ADTs D. All of the above E. None of the above 29 What happens here? STk> (load "lazy.scm") STk> (define g-env (setup-environment)) g-env STk> (mc-eval '((lambda(+ 1 x))(+ 2 )) g-env) What is returned? a. (thunk (+ 1 5) env ) b. (thunk (+ 1 (+ 2 )) env ) c. 6 d. Something else e.??? 0 5

SOLUTION (define (driver-loop) (prompt-for-input input-prompt) (let ((input (read))) (let ((output (actual-value input the-global-environment))) (announce-output output-prompt) (user-print output))) (driver-loop)) This was: mc-eval If the driver-loop needs to print it make sure you haven t been TOO lazy. mc-eval might return a delayed argument from a compound 1 actual-value (define (actual-value exp env) (force-it (mc-eval exp env))) (define (force-it obj) (if (thunk? obj)?? (actual-value obj)) thunk A. (mc-eval B. (actual-value (thunk-exp obj) (thunk-env obj)) exp env 2 Example of why we call actual-value STk> (load "lazy.scm") STk> (define g-env (setup-environment)) g-env STk> (mc-eval '((lambda x) ((lambda (y) y) (+ 2 ))) g-env) (thunk ((lambda (y) y) (+ 2 )) env ) ((self-evaluating? exp)... ((variable? exp)... ((quoted? exp)... ((assignment? exp)... ((definition? exp)... ((if? exp)... ((lambda? exp)... ((begin? exp)... (? exp)... ((application? exp)... (else (error what?")))) What happens if we pass a Thunk ADT as exp? A. errors B. application C. lambda D. self-eval. E.?? 4 if s need actual values! (define (eval-if exp env) (if (true? (actual-value (if-predicate exp) env)) mc-eval sometimes returns Thunk ADTs (mc-eval (if-consequent exp) env) (mc-eval (if-alternative exp) env))) 5 Summary & Additional Notes Thunk ADTs could also be memoized We delayed arguments to compound s Compound s are defined by the user We didn t delay arguments to primitive s We made sure we had the actual value to print it Ifs needed REAL values 6 6