Discussion 12 The MCE (solutions)

Similar documents
Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

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

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

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

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

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

Functional Programming. Pure Functional Programming

An Explicit-Continuation Metacircular Evaluator

CS61A Midterm 2 Review (v1.1)

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

Lexical vs. Dynamic Scope

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

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

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

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

4.2 Variations on a Scheme -- Lazy Evaluation

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

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

From Syntactic Sugar to the Syntactic Meth Lab:

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

CSc 520 Principles of Programming Languages

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

Building a system for symbolic differentiation

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

Turtles All The Way Down

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

(scheme-1) has lambda but NOT define

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

CS 275 Name Final Exam Solutions December 16, 2016

Documentation for LISP in BASIC

Project 2: Scheme Interpreter

An Explicit Continuation Evaluator for Scheme

Write a procedure powerset which takes as its only argument a set S and returns the powerset of S.

CS 314 Principles of Programming Languages. Lecture 16

Building a system for symbolic differentiation

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

6.001: Structure and Interpretation of Computer Programs

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

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

A Brief Introduction to Scheme (II)

INTERPRETATION AND SCHEME LISTS 12

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

Comp 311: Sample Midterm Examination

This exam is worth 30 points, or 18.75% of your total course grade. The exam contains

CS 314 Principles of Programming Languages

Evaluating Scheme Expressions

INTERPRETATION AND SCHEME LISTS 12

Lecture #24: Programming Languages and Programs

Programming Project #4: A Logo Interpreter Summer 2005

C311 Lab #3 Representation Independence: Representation Independent Interpreters

Functional Programming. Pure Functional Languages

SCHEME AND CALCULATOR 5b

Sample Final Exam Questions

Functional Programming. Pure Functional Languages

LECTURE 16. Functional Programming

Project 5 - The Meta-Circular Evaluator

6.001 Notes: Section 8.1

Principles of Programming Languages 2017W, Functional Programming

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

6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq

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

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

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


Project 5 - The Meta-Circular Evaluator

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

1.3. Conditional expressions To express case distinctions like

COP4020 Programming Assignment 1 - Spring 2011

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

SOFTWARE ARCHITECTURE 6. LISP

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

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Recursive Functions of Symbolic Expressions and Their Application, Part I

FP Foundations, Scheme

Principles of Programming Languages

CS61A Notes Week 13: Interpreters

Introduction to Scheme

Scheme Quick Reference

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

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

Vectors in Scheme. Data Structures in Scheme

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

6.001, Spring Semester, 1998, Final Exam Your Name: 2 Question 1 (12 points): Each of the parts below should be treated as independent. For each part,

CS 360 Programming Languages Interpreters

Chapter 1. Fundamentals of Higher Order Programming

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

Modern Programming Languages. Lecture LISP Programming Language An Introduction

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

Data abstraction, revisited

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

CS 314 Principles of Programming Languages

Scheme Quick Reference

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

User-defined Functions. Conditional Expressions in Scheme

Duplication and Partial Evaluation For a Better Understanding of Reflective Languages

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

6.945 Adventures in Advanced Symbolic Programming

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

Transcription:

Discussion 12 The MCE (solutions) ;;;;METACIRCULAR EVALUATOR FROM CHAPTER 4 (SECTIONS 4.1.1-4.1.4) of ;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS ;;;from section 4.1.4 -- must precede def of metacircular apply (define apply-in-underlying-scheme apply) (define (mc-eval exp env) (cond ((self-evaluating? exp) exp) ((variable? exp) (lookup-variable-value exp env)) ((quoted? exp) (text-of-quotation exp)) ((assignment? exp) (eval-assignment exp env)) ((definition? exp) (eval-definition exp env)) ((lambda? exp) (make-procedure (lambda-parameters exp) (lambda-body exp) env)) ((cond? exp) (mc-eval (cond->if exp) env)) ((MAKE-FEXPR? EXP) (EVAL-MAKE-FEXPR EXP ENV)) ((application? exp) (LET ((OP (MC-EVAL (OPERATOR EXP) ENV))) (IF (FEXPR? OP) (MC-APPLY (FEXPR-PROC OP) (LIST EXP ENV)) (mc-apply op (list-of-values (operands exp) env)) ))) (else (error "Unknown expression type -- EVAL" exp)))) (define (mc-apply procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure arguments)) ((compound-procedure? procedure) (eval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) arguments (procedure-environment procedure)))) (else (error "Unknown procedure type -- APPLY" procedure)))) (define (list-of-values exps env) (if (no-operands? exps) '() (cons (mc-eval (first-operand exps) env) (list-of-values (rest-operands exps) env)))) (define (eval-sequence exps env) (cond ((last-exp? exps) (mc-eval (first-exp exps) env)) (else (mc-eval (first-exp exps) env) (eval-sequence (rest-exps exps) env))))

(define (eval-definition exp env) (define-variable! (definition-variable exp) (mc-eval (definition-value exp) env) env) 'ok) (define (true? x) (not (eq? x false))) (define (false? x) (eq? x false)) (define (make-procedure parameters body env) (list 'procedure parameters body env)) (define (make-frame variables values) (cons variables values)) (define (extend-environment vars vals base-env) (cons (make-frame vars vals) base-env) (define (primitive-procedure? proc) (tagged-list? proc 'primitive)) (define (primitive-implementation proc) (cadr proc)) (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)) (define the-global-environment '()) (define (mce) (set! the-global-environment (setup-environment)) (driver-loop)) (DEFINE (MAKE-FEXPR? EXP) (TAGGED-LIST? EXP 'MAKE-FEXPR) )) (DEFINE (EVAL-MAKE-FEXPR EXP ENV) (LIST 'FEXPR EXP ENV)) (DEFINE (FEXPR? OP) (TAGGED-LIST? EXP FEXPR)) (DEFINE (FEXPR-PROC OP) (MC-EVAL (CAR (CADDR (CADR OP))) ENV) )

QUESTIONS 1. Recall that mceval.scm tests true or false using the true? and false? procedure: (define (true? x) (not (eq? x false))) (define (false? x) (eq? x false)) Suppose we type the following definition into MCE: MCE> (define true false) What would be returned by the following expression: MCE> (if (= 2 2) 3 4) A: 3 B: 4 C: ERROR 2. Suppose we type the following into mc-eval: MCE> (define x (* x x)) This expression evaluates without error. What would be returned by the following expressions? MCE> quote A: ERROR: unbound variable quote B: (compound-procedure quote ((* x x)) <procedure-env>) MCE> (quote 10) A: 10 B: 100 3. Suppose we just loaded mceval.scm into STk, and erroneously started MCE using (driverloop) instead of (mce). What is the return value of the following sequence of expressions typed into MCE? If there will be an error, just write ERROR: MCE> 2 2 MCE> (+ 2 3) ERROR MCE> (define x 10) ERROR

4. Most dialects of Lisp have some way for users to create their own new special forms. We ll add to the metacircular evaluator one such way, the one that was used in the very earliest version of Lisp. As an example, suppose we want to create a while looping mechanism. The notation should be (while <condition> <action>) ;general pattern (while (< x 10) (set! x (+ x 3))) ;example This has to be a special form because we want the subexpressions to be evaluated repeatedly, not just once. An FEXPR (pronounced eff exper ) is a special kind of procedure defined with two formal parameters. When it s invoked, the entire invoking expression (unevaluated) becomes the first argument, and the current environment becomes the second argument. (The expression that invokes the FEXPR can have any number of subexpressions, but that doesn t matter because the subexpressions aren t individual arguments to the FEXPR.) Here s what the user should say to create the while special form: (define while (make-fexpr (exp env) (real-while exp env))) (define (real-while exp env) (if (eval (cadr exp) env) ; evaluate the condition (begin (eval (caddr exp) env) ; if true: evaluate the action (real-while exp env)) ; do it again done)) ; if false: we re finished If the user then says (while (< x 10) (set! x (+ x 3))) then while will be invoked with that entire expression as its first argument, and the current environment as its second argument. In this case, (cadr exp) will be the condition (< x 10) and similarly the caddr will be the action to be repeated. (Notice that the while FEXPR invokes an ordinary procedure called real-while to do the work. This is necessary because real-while is recursive. A recursive call to an FEXPR won t work because the actual arguments seen in the recursive call would again be the entire expression and the current environment, which isn t what you want!) You must make two modifications to the evaluator: (1) invent the make-fexpr form that creates an FEXPR, which is just like a procedure but with a different type tag; and (2) make the evaluator recognize an FEXPR when it s invoked, and do the right thing about its arguments. (a) Is each of these a change primarily to eval or apply? change (1): is a change to EVAL because we are creating a new special form.

change (2): is also a change to EVAL because each FEXPR is a user-defined special form, so we have to prevent the evaluation of its arguments. Checking in APPLY is too late, although some smaller changes might be needed there too, depending on how you represent FEXPRs. (b) More specifically, what procedure(s) will you invent or change for each of them? change (1): is a change to EVAL itself, adding a clause to the COND, plus inventing a new EVAL-MAKE-FEXPR procedure and other details. change (2): is a change to EVAL itself, making the COND recognize a FEXPR, and then there are several possibilities for other details. (c) Make the actual changes on the first page. See solution on the first page. You will also have to add the following to the list of primitive-procedures: (list < <) (list eval mc-eval) (list cadr cadr) (list caddr caddr) Note: The point about FEXPR-PROC is that it makes the FEXPR look just like any other compound procedure by the time APPLY sees it. This makes APPLY work without any changes. Another way would be to say (APPLY OP (LIST EXP ENV)) back in eval, and then in apply we'd have to change the test for a compound procedure so that it recognizes things whose manifest type is FEXPR as well as those whose manifest type is PROCEDURE. That would mean either modifying COMPOUND-PROCEDURE? or else adding another clause to the cond in apply.