Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

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

Discussion 12 The MCE (solutions)

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

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

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

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

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

Functional Programming. Pure Functional Programming

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

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.

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

From Syntactic Sugar to the Syntactic Meth Lab:

Turtles All The Way Down

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

An Explicit-Continuation Metacircular Evaluator

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

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

CS61A Midterm 2 Review (v1.1)

Lexical vs. Dynamic Scope

Project 2: Scheme Interpreter

CS 314 Principles of Programming Languages. Lecture 16

4.2 Variations on a Scheme -- Lazy Evaluation

Documentation for LISP in BASIC

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

CSc 520 Principles of Programming Languages

Building a system for symbolic differentiation

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

An Explicit Continuation Evaluator for Scheme

User-defined Functions. Conditional Expressions in Scheme

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

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

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

CS450 - Structure of Higher Level Languages

Building a system for symbolic differentiation

CS 314 Principles of Programming Languages

Evaluating Scheme Expressions

Sample Final Exam Questions

FP Foundations, Scheme

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

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

Scheme: Strings Scheme: I/O

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

C311 Lab #3 Representation Independence: Representation Independent Interpreters

Comp 311: Sample Midterm Examination

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

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

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

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,

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

SCHEME AND CALCULATOR 5b

A Brief Introduction to Scheme (II)

Principles of Programming Languages

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

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

An Introduction to Scheme

Administrivia. Simple data types

CS 314 Principles of Programming Languages

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

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

COP4020 Programming Assignment 1 - Spring 2011

Introduction to Scheme

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

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

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

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

Duplication and Partial Evaluation For a Better Understanding of Reflective Languages

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

(scheme-1) has lambda but NOT define

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

Mid-Term 2 Grades

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

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))

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

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

6.001: Structure and Interpretation of Computer Programs

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

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

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

Chapter 15 Functional Programming Languages

Programming Languages

Functional Programming. Big Picture. Design of Programming Languages

MetacircularScheme! (a variant of lisp)

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

Principles of Programming Languages 2017W, Functional Programming

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

CS 275 Name Final Exam Solutions December 16, 2016

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?

Racket: Modules, Contracts, Languages

Scheme Quick Reference

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

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Functional Programming

Programming Project #4: A Logo Interpreter Summer 2005

Transcription:

Scheme in Scheme: The Metacircular Evaluator Eval and Apply CS21b: Structure and Interpretation of Computer Programs Brandeis University Spring Term, 2015

The metacircular evaluator is A rendition of Scheme, in Scheme, where expressions and environments are just list structures that get modified according to fixed rules. A concise language description (as opposed to, say, English) -- which, of course, you need to know Scheme already to understand. A kind of fixed point of the Scheme interpreter. (factorial is not a fixed point.) A straightforward medium to discuss language modifications among language designers No more complicated than many of the programs we have discussed in class What you are about to learn is powerful, secular wizardry. Your life will never be the same again.

Evaluator -- top level (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 expression type EVAL" exp))))

Apply (coroutines with Eval) (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 type APPLY" procedure)))) (define (list-of-values exps env) (if (no-operands? exps) '() (cons (eval (first-operand exps) env) (list-of-values (rest-operands exps) env)))) (define (apply-primitive-procedure proc args) (apply-in-underlying-scheme (primitive-implementation proc) args))

Syntax-directed evaluation (examples) (define (eval-if exp env) (if (true? (eval (if-predicate exp) env)) (eval (if-consequent exp) env) (eval (if-alternative exp) env))) (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)))) (define (eval-assignment exp env) (set-variable-value! (assignment-variable exp) (eval (assignment-value exp) env) env) 'ok) (define (eval-definition exp env) (define-variable! (definition-variable exp) (eval (definition-value exp) env) env) 'ok) (define (make-procedure parameters body env) (list 'procedure parameters body env)) (define (compound-procedure? p) (tagged-list? p 'procedure))

Environments (define (enclosing-environment env) (cdr env)) (define (first-frame env) (car env)) (define the-empty-environment '()) (define (make-frame variables values) (cons variables values)) (define (frame-variables frame) (car frame)) (define (frame-values frame) (cdr frame)) (define (add-binding-to-frame! var val frame) (set-car! frame (cons var (car frame))) (set-cdr! frame (cons val (cdr frame)))) (define (extend-environment vars vals base-env) (if (= (length vars) (length vals)) (cons (make-frame vars vals) base-env) (if (< (length vars) (length vals)) (error "Too many arguments supplied" vars vals) (error "Too few arguments supplied" vars vals))))

Environments (variable lookup) (define (lookup-variable-value var env) (define (env-loop env) (define (scan vars vals) (cond ((null? vars) (env-loop (enclosing-environment env))) ((eq? var (car vars)) (car vals)) (else (scan (cdr vars) (cdr vals))))) (if (eq? env the-empty-environment) (error "Unbound variable" var) (let ((frame (first-frame env))) (scan (frame-variables frame) (frame-values frame))))) (env-loop env))

Environments (variable lookup) (define (set-variable-value! var val env) (define (env-loop env) (define (scan vars vals) (cond ((null? vars) (env-loop (enclosing-environment env))) ((eq? var (car vars)) (set-car! vals val)) (else (scan (cdr vars) (cdr vals))))) (if (eq? env the-empty-environment) (error "Unbound variable SET!" var) (let ((frame (first-frame env))) (scan (frame-variables frame) (frame-values frame))))) (env-loop env)) (define (define-variable! var val env) (let ((frame (first-frame env))) (define (scan vars vals) (cond ((null? vars) (add-binding-to-frame! var val frame)) ((eq? var (car vars)) (set-car! vals val)) (else (scan (cdr vars) (cdr vals))))) (scan (frame-variables frame) (frame-values frame))))

Environments (variable lookup) (define (setup-environment) (let ((initial-env (extend-environment (primitive-procedure-names) (primitive-procedure-objects) the-empty-environment))) (define-variable! 'true #t initial-env) (define-variable! 'false #f initial-env) initial-env))

Driver loop (define input-prompt "") (define output-prompt "") (define (driver-loop) (prompt-for-input input-prompt) (let ((input (read))) (let ((output (eval input the-global-environment))) (announce-output output-prompt) (user-print output))) (driver-loop)) (define (prompt-for-input string) (newline) (newline) (display string) (newline)) (define (announce-output string) (newline) (display string) (newline)) (define (user-print object) (if (compound-procedure? object) (display (list 'compound-procedure (procedure-parameters object) (procedure-body object) '<procedure-env>)) (display object)))

5 5 (+ 5 7) 12 ((lambda (x) (* x x)) 5) 25 (define square (lambda (x) (* x x))) ok Using the metacircular evaluator... (square 5) 25 (define fact (lambda (n) (if (= n 0) 1 (* n (fact (- n 1)))))) ok (fact 5) 120