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

Similar documents
From Syntactic Sugar to the Syntactic Meth Lab:

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

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

An Explicit-Continuation Metacircular Evaluator

Functional Programming. Pure Functional Programming

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

CS61A Midterm 2 Review (v1.1)

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

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

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.

A Brief Introduction to Scheme (II)

6.001: Structure and Interpretation of Computer Programs

CSc 520 Principles of Programming Languages

(scheme-1) has lambda but NOT define

Introduction to Scheme

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

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

Building a system for symbolic differentiation

Lexical vs. Dynamic Scope

Functional Programming. Pure Functional Languages

Building a system for symbolic differentiation

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

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

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

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

Functional Programming. Pure Functional Languages

Project 2: Scheme Interpreter

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

Essentials of Programming Languages Language

Documentation for LISP in BASIC

Essentials of Programming Languages Language

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

4.2 Variations on a Scheme -- Lazy Evaluation

User-defined Functions. Conditional Expressions in Scheme

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

CS 314 Principles of Programming Languages

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

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

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and 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 360 Programming Languages Interpreters

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

CS 275 Name Final Exam Solutions December 16, 2016

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

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

Comp 311: Sample Midterm Examination

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

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


An Explicit Continuation Evaluator for Scheme

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

C311 Lab #3 Representation Independence: Representation Independent Interpreters

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

ALISP interpreter in Awk

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

6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

Sample Final Exam Questions

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

Turtles All The Way Down

Principles of Programming Languages

6.945 Adventures in Advanced Symbolic Programming

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

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

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

University of Massachusetts Lowell

1.3. Conditional expressions To express case distinctions like

An Introduction to Scheme

CS 314 Principles of Programming Languages. Lecture 16

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

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

Lecture Notes on Lisp A Brief Introduction

Evaluating Scheme Expressions

Lecture #24: Programming Languages and Programs

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

Data Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong.

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

What is tail position?

INTERPRETATION AND SCHEME LISTS 12

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

SOFTWARE ARCHITECTURE 6. LISP

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

6.001 Notes: Section 15.1

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

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

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

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

UNIVERSITY OF TORONTO AT MISSISSAUGA April 2006 Examination CSC 324H5 S Instructor: Richard Krueger Duration 3 hours No Aids Allowed

Lecture08: Scope and Lexical Address

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

Scheme Quick Reference

Functional Programming

FP Foundations, Scheme

Administrivia. Simple data types

COP4020 Programming Assignment 1 - Spring 2011

Transcription:

Computer Science 21b (Spring Term, 2017) Structure and Interpretation of Computer Programs Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want Here is one of the big ideas of this course: That in many, many programming languages and systems, there is at their core something that looks like Scheme. Why? What really is in Scheme? A way of talking about Constants and built-in operations Variables (and naming i.e., a namespace) Data structures (and something to make them out of... ) Conditional/branching Procedures (making them, and calling them) an abstraction mechanism An inductive syntactic structure to programs What isn t as important is the specific way these are realized (applicative order? normal order? dynamic binding? static binding?) or the niceties of the syntax. Consider the following kind of conditional: (C (only if) P (and otherwise) A) (while P do E) (for i from B to E do C) The syntax of a language like a the shape of a nose doesn t really matter: it s what s in it that counts... 1

Eval (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)) ((application? exp) (apply (eval (operator exp) env) (list-of-values (operands exp) env))) ; Extra language features, via syntactic sugar: ((let? exp) (eval (unsugar-let exp) env)) ((cond? exp) (eval (cond->if (clauses exp)) env)) ((while? exp) (eval (unsugar-while exp) env)) ((for-loop? exp) (eval (unsugar-for exp) env)) (else (error "Unknown expression type -- EVAL" exp)))) 2

(let ((x 1 e 1 ) (x 2 e 2 ) (x n e n )) b) ((lambda (x 1 x 2 x n ) b) e 1 e 2 e n ) (define (unsugar-let exp) (let ((vars (map car (cadr exp))) (vals (map cadr (cadr exp))) (body (caddr exp))) (cons (list lambda vars body) vals))) ;Value: unsugar-let (unsugar-let (let ((x1 e1) (x2 e2) (x3 e3)) b)) ;Value: ((lambda (x1 x2 x3) b) e1 e2 e3) 3

(cond (p 1 e 1 ) (p 2 e 2 ) (p n e n )) (if p 1 e 1 (if p 2 e 2 (if p n e n false) )) (define (cond->if clauses) (if (null? clauses) false (list if (caar clauses) (cadar clauses) (cond->if (cdr clauses))))) ;Value: cond->if (cond->if ((p1 e1) (p2 e2) (pn en))) ;Value: (if p1 e1 (if p2 e2 (if pn en false))) 4

(while predicate do command) (if predicate (begin command (while predicate do command)) done) (define (unsugar-while exp) (let ((pred (cadr exp)) (command (cadddr exp))) (list if pred (list begin command exp) done))) ;Value: unsugar-while (unsugar-while (while p1 do c)) ;Value: (if p1 (begin c (while p1 do c)) done) 5

(while predicate do command) [Another solution] (define (eval-while exp env) (let ((pred (cadr exp)) (command (cadddr exp))) (if (true? (eval pred env)) (begin (eval command env) (eval-while exp env)) done))) ;Value: unsugar-while 6

(for i e1 e2 command) (let ((i e 1 )) (while (<= i e 2 ) do (begin command (set! i (1+ i)))) (define (unsugar-for exp) (let ((index-var (cadr exp)) (from (caddr exp)) (to (cadddr exp)) (command (cadr (cdddr exp)))) (list let (list (list index-var from)) (list while (list <= index-var to) do (list begin command (list set! index-var (list 1+ index-var))))))) ;Value: unsugar-for (pretty-print (unsugar-for (for count 1 10 (eat-candy)))) (let ((count 1)) (while (<= count 10) do (begin (eat-candy) (set! count (1+ count))))) ;No value 7

(for i e1 e2 command) (let ((i e 1 )) (while (<= i e 2 ) do (begin command (set! i (1+ i)))) (define (unsugar-for exp) (let ((index-var (cadr exp)) (from (caddr exp)) (to (cadddr exp)) (command (cadr (cdddr exp)))) (let ((,index-var,from)) (while (<=,index-var,to) do (begin,command (set!,index-var (1+,index-var))))))) ;Value: unsugar-for (pretty-print (unsugar-for (for count 1 10 (eat-candy)))) (let ((count 1)) (while (<= count 10) do (begin (eat-candy) (set! count (1+ count))))) ;No value 8

(for i e1 e2 command) [Another semantics] (define (unsugar-for exp env) (let ((index-var (cadr exp)) (from (caddr exp)) (to (eval (cadddr exp) env)) (command (cadr (cdddr exp)))) (let ((,index-var,from)) (while (<=,index-var,to) do (begin,command (set!,index-var (1+,index-var))))))) ;Value: unsugar-for Suppose (upper-bound) evaluates to 320: (unsugar-for (for count 1 (upper-bound) (eat-candy))) (let ((count 1)) (while (<= count 320) do (begin (eat-candy) (set! count (1+ count))))) ;No value So what is the difference between this and the code below? (let ((count 1)) (while (<= count (upper-bound)) do (begin (eat-candy) (set! count (1+ count))))) Answer: In the above code, the body of the while loop cannot change the value of the upper bound. This suggests the delicacy of the decisions that language designers must make in defining semantics. 9