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

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

Discussion 12 The MCE (solutions)

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

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

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

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

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

Sample Final Exam Questions

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

CS 314 Principles of Programming Languages. Lecture 16

CS61A Midterm 2 Review (v1.1)

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

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

Lexical vs. Dynamic Scope

Functional Programming. Pure Functional Programming

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

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

From Syntactic Sugar to the Syntactic Meth Lab:

Building a system for symbolic differentiation

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

Turtles All The Way Down

Documentation for LISP in BASIC

CS 314 Principles of Programming Languages

Project 2: Scheme Interpreter

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

An Explicit-Continuation Metacircular Evaluator

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

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

Building a system for symbolic differentiation

(scheme-1) has lambda but NOT define

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

CS 314 Principles of Programming Languages

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

6.001, Fall Semester, 1998 Lecture Notes, October 27 { Object Oriented Programming 2 An environment diagram illustrating +define foo +cons 1 2,, +set-

C311 Lab #3 Representation Independence: Representation Independent Interpreters

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

CSc 520 Principles of Programming Languages

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

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

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

User-defined Functions. Conditional Expressions in Scheme

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

Duplication and Partial Evaluation For a Better Understanding of Reflective Languages

SCHEME AND CALCULATOR 5b

Comp 311: Sample Midterm Examination

Functions, Conditionals & Predicates

Functional Programming. Pure Functional Languages

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,

Introduction to Scheme

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

An Explicit Continuation Evaluator for Scheme

Principles of Programming Languages 2017W, Functional Programming

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

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

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

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

Administrivia. Simple data types

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

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Principles of Programming Languages

Functional Programming. Pure Functional Languages

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

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

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

A Brief Introduction to Scheme (II)


Project 5 - The Meta-Circular Evaluator

An introduction to Scheme

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

Project 5 - The Meta-Circular Evaluator

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

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

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

Evaluating Scheme Expressions

FP Foundations, Scheme

CSE341 Autumn 2017, Final Examination December 12, 2017

6.945 Adventures in Advanced Symbolic Programming

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

Berkeley Scheme s OOP

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

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

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

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

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

INTERPRETERS AND TAIL CALLS 9

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

CSE341 Spring 2017, Final Examination June 8, 2017

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

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

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

6.001: Structure and Interpretation of Computer Programs

CS 360 Programming Languages Interpreters

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

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

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

CS 275 Name Final Exam Solutions December 16, 2016

6.001 Notes: Section 15.1

First-class continuations. call/cc, stack-passing CEK machines

Scheme Quick Reference

Transcription:

1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 1996 Lecture Notes { October 31, 1996 The Metacircular Evaluator Today we shift perspective from that of a user of computer langugaes to that of a designer of computer languages. EVAL is an example of a UNIVERSAL machine. That is, it can simulate any other machine. Remember the rules of the environment model: 1. To EVALUATE a combination (a compound expression other than a special form), evaluate the subexpressions and then APPLY the value of the operator subexpression to the values of the operand subexpressions. 2. To APPLY a compound procedure to a set of arguments, EVALUATE the body of the procedure in a new environment. To construct this environment, extend the environment part of the procedure object by a frame in which the formal parameters of the procedure are bound to the arguments to which the procedure is applied. Draw the EVAL/APPLY cycle here: Assume we have some way of representing environments together with the following operations: (lookup-variable-value symbol env) {returns the value bound to the symbol in the environment. (extend-environment vars vals base-env) { returns a new environment, which extends base-env by a frame in which the vars are bound to the corresponding vals.

6.001, Fall Semester, 1996 Lecture Notes { October 31, 1996 2 We can sketch the basic idea behind representing environments and frames here: Sketch of overall Scheme system, and how EVAL ts in: When you type the expression: (* x (+ 2 y)), EVAL sees that as (eval '(* x (+ 2 y)) the-global-env). In general, your programs are EVAL's data. Here is the basic form for EVAL { note the primities, the special forms, and the reduction to apply.

6.001, Fall Semester, 1996 Lecture Notes { October 31, 1996 3 (define (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)) ((if? exp) (eval-if exp env)) ((lambda? exp) (make-procedure (lambda-parameters exp) (lambda-body exp) env)) ((begin? exp) (eval-sequence (begin-actions exp) env)) ((cond? exp) (eval (cond->if exp) env)) ((application? exp) (apply (eval (operator exp) env) (list-of-values (operands exp) env))) (else (error "Unknown exp -- EVAL" exp)))) Examples: quoted? lambda? if? Evaluation of if expressions: (define (eval-if exp env) (if (true? (eval (if-predicate exp) env)) (eval (if-consequent exp) env) (eval (if-alternative exp) env))) Begin expressions indicate sequences to be evaluated in order: (define (eval-sequence exps env) (cond ((last-exp? exps) (eval (first-exp exps) env)) (else (eval (first-exp exps) env) (eval-sequence (rest-exps exps) env))))

6.001, Fall Semester, 1996 Lecture Notes { October 31, 1996 4 Cond is implemented as a derived expression (cond ((> x 0) x) ((= x 0) 0) (else (- x))) can be converted into Key clause in EVAL that handles procedure applications: ((application? exp) (apply (eval (operator exp) env) (list-of-values (operands exp) env))) APPLY: the other half of the cycle. (define (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 -- APPLY" procedure))))

6.001, Fall Semester, 1996 Lecture Notes { October 31, 1996 5 Trace through the evaluation of ((lambda (x y) (* x y)) 5 7) Finally, here is stripped down version of the evaluator:

6.001, Fall Semester, 1996 Lecture Notes { October 31, 1996 6 (define mc-eval (lambda (exp env) (cond ((number? exp) exp) ;base-case ((string? exp) exp) ;base-case ((symbol? exp) (lookup exp env)) ;base case ((eq? (car exp) 'quote) (car (cdr exp)));special forms ((eq? (car exp) 'cond) (evcond (cdr exp) env)) ((eq? (car exp) 'sequence) (evseq (cdr exp) env)) ((eq? (car exp) 'lambda) (list 'proc (cdr exp) env)) ((eq? (car exp) 'define) (evdefine (cdr exp) env)) (else (mc-apply (mc-eval (car exp) env) (evlist (cdr exp) env)))))) (define mc-apply (lambda (fun args) (cond ((atom? fun) (apply fun args)) ;ground out ((eq? (car fun) 'proc) (mc-eval (car (cdr (car (cdr fun)))) ;procedure body (bind (car (car (cdr fun))) ;formal params args ;supplied args (car (cdr (cdr fun))))));saved env (else (error '"Unknown function"))))) (define evlist (lambda (lst env) ;map evaluator over list (cond ((null? lst) nil) (else (cons (mc-eval (car lst) env) (evlist (cdr lst) env)))))) (define evcond (lambda (clauses env) (cond ((null? clauses) nil) ((eq? 'else (car (car clauses))) (evseq (cdr (car clauses)) env)) ((mc-eval (car (car clauses)) env) (evseq (cdr (car clauses)) env)) (else (evcond (cdr clauses) env))))) (define evseq (lambda (clauses env) (cond ((null? (cdr clauses)) (mc-eval (car clauses) env)) (else (mc-eval (car clauses) env) (evseq (cdr clauses) env))))) (define evdefine (lambda (body env) ;mutate the first frame (sequence (set-cdr! (car env) (cons (cons (car body) (mc-eval (car (cdr body)) env)) (cdr (car env)))) (car body))))

6.001, Fall Semester, 1996 Lecture Notes { October 31, 1996 7 (define bind (lambda (params values env) ;add a new frame (cons (cons 'frame (make-frame-body params values)) env))) (define make-frame-body (lambda (params values) ;frame body is an association list (cond ((null? params) (cond ((null? values) nil) (else (error '"Too many values supplied")))) ((null? values) (error '"Too few values supplied")) (else (cons (cons (car params) (car values)) (make-frame-body (cdr params) (cdr values))))))) (define lookup (lambda (var env) (cond ((null? env) (error '"Unbound variable" var)) ;not in any frames (else ((lambda (binding) (cond ((null? binding) ;check each frame (lookup var (cdr env))) ;in turn (else (cdr binding)))) ;(<varname>. <value>) (find-binding var (cdr (car env)))))))) (define find-binding (lambda (var frame-body) ;this is just assq (cond ((null? frame-body) nil) ((eq? var (car (car frame-body))) (car frame-body)) (else (find-binding var (cdr frame-body)))))) (define global-env (list (list 'frame (cons 'nil '()) (cons '+ +) (cons '- -) (cons '= =) (cons '* *) (cons 'car car) (cons 'cdr cdr) (cons 'cons cons) (cons 'list list) (cons 'set-car! set-car!) (cons 'set-cdr! set-cdr!) (cons 'null? null?) (cons 'eq? eq?) (cons 'atom? atom?) (cons 'number? number?) (cons 'string? string?) (cons 'symbol? symbol?) (cons 'error error) (cons 'apply apply))))