From Syntactic Sugar to the Syntactic Meth Lab:

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

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

Functional Programming. Pure Functional Programming

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

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

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

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

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

Project 2: Scheme Interpreter

C311 Lab #3 Representation Independence: Representation Independent Interpreters

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

CS61A Midterm 2 Review (v1.1)

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

Documentation for LISP in BASIC

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

A Brief Introduction to Scheme (II)

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

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

Building a system for symbolic differentiation

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

Lexical vs. Dynamic Scope

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

6.001: Structure and Interpretation of Computer Programs

CSc 520 Principles of Programming Languages

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

Building a system for symbolic differentiation

4.2 Variations on a Scheme -- Lazy Evaluation

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

An Explicit-Continuation Metacircular Evaluator

User-defined Functions. Conditional Expressions in Scheme

CS 275 Name Final Exam Solutions December 16, 2016

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

Typed Racket: Racket with Static Types

Comp 311: Sample Midterm Examination

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

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

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

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

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

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

(scheme-1) has lambda but NOT define

CSE341 Autumn 2017, Final Examination December 12, 2017

The Typed Racket Guide

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

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

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

Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

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

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

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

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

6.945 Adventures in Advanced Symbolic Programming

CSE341, Spring 2013, Final Examination June 13, 2013

An Introduction to Scheme

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

Lecture 3: Expressions

An Explicit Continuation Evaluator for Scheme

Sample Final Exam Questions

CSE341 Spring 2017, Final Examination June 8, 2017

Scheme Quick Reference

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

CS 314 Principles of Programming Languages. Lecture 16

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

Introduction to Scheme

INTRODUCTION TO SCHEME

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

9 Objects and Classes

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

Scheme Quick Reference

Programming Languages

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

Essentials of Programming Languages Language

Typed Scheme: Scheme with Static Types

Essentials of Programming Languages Language

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

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

CS 360 Programming Languages Interpreters

CSE341 Spring 2017, Final Examination June 8, 2017

An introduction to Scheme

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

CSE341 Spring 2016, Final Examination June 6, 2016

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

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

CS 314 Principles of Programming Languages


Functional Programming

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

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

Evaluating Scheme Expressions

COP4020 Programming Assignment 1 - Spring 2011

Chapter 1. Fundamentals of Higher Order Programming

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Turtles All The Way Down

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

Transcription:

From Syntactic Sugar to the Syntactic Meth Lab: Using Macros to Cook the Language You Want a V E N E TRUT H Brandeis S PA R T U N T O I T S I N N E R M OS T COMPUTER SCIENCE 21B: Structure and Interpretation of Computer Programs a Drugs are bad, m kay? From Syntactic Sugar to the Syntactic Meth Lab: p.1/35

Wake up! From Syntactic Sugar to the Syntactic Meth Lab: p.2/35

Sugar, Sugar (define (mc-eval exp env) ;; the meta-circular evaluator with syntactic sugar. (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) (mc-apply (mc-eval (operator exp) env) (list-of-values (operands exp) env))) ;; Extra language features, via syntactic sugar. ((let? exp) (mc-eval (unsugar-let exp) env)) ((cond? exp) (mc-eval (unsugar-cond exp) env)) ((while? exp) (mc-eval (unsugar-while exp) env)) ((for? exp) (mc-eval (unsugar-for exp) env)) (else (error "Unknown expression type EVAL" exp)))) From Syntactic Sugar to the Syntactic Meth Lab: p.3/35

Boring We re just doing the same thing over and over again. To add sugar, we: From Syntactic Sugar to the Syntactic Meth Lab: p.4/35

Boring We re just doing the same thing over and over again. To add sugar, we: 1. Create a new kind of expression (a list starting with unique symbol). thing? From Syntactic Sugar to the Syntactic Meth Lab: p.4/35

Boring We re just doing the same thing over and over again. To add sugar, we: 1. Create a new kind of expression (a list starting with unique symbol). thing? 2. Write a transformation (a Scheme procedure) from the new form into a simpler form. unsugar-thing From Syntactic Sugar to the Syntactic Meth Lab: p.4/35

Boring We re just doing the same thing over and over again. To add sugar, we: 1. Create a new kind of expression (a list starting with unique symbol). thing? 2. Write a transformation (a Scheme procedure) from the new form into a simpler form. unsugar-thing 3. Eval the new form by applying the transformation to obtain a simpler form and eval that form in the current environment. ((thing? exp) (mc-eval (unsugar-thing exp) env)) From Syntactic Sugar to the Syntactic Meth Lab: p.4/35

Transformations: let Transformations (unsugarings) are just Scheme procedures that input a list and output a list (representing Scream expressions). (let ((x e) ) b) = ((lambda (x ) b) e ) From Syntactic Sugar to the Syntactic Meth Lab: p.5/35

Transformations: let Transformations (unsugarings) are just Scheme procedures that input a list and output a list (representing Scream expressions). (let ((x e) ) b) = ((lambda (x ) b) e ) (define (unsugar-let exp)... ) From Syntactic Sugar to the Syntactic Meth Lab: p.5/35

Transformations: let Transformations (unsugarings) are just Scheme procedures that input a list and output a list (representing Scream expressions). (let ((x e) ) b) = ((lambda (x ) b) e ) (define (unsugar-let exp) (let ((xs (let-vars exp)) (es (let-exps exp)) (b (let-body exp))) (cons (list lambda xs b) es))) From Syntactic Sugar to the Syntactic Meth Lab: p.5/35

Transformations: let Transformations (unsugarings) are just Scheme procedures that input a list and output a list (representing Scream expressions). (let ((x e) ) b) = ((lambda (x ) b) e ) (define (unsugar-let exp) (let ((xs (let-vars exp)) (es (let-exps exp)) (b (let-body exp))) (cons (list lambda xs b) es))) (define (let-vars exp) (map car (cadr exp))) (define (let-exps exp) (map cadr (cadr exp))) (define (let-body exp) (caddr exp)) From Syntactic Sugar to the Syntactic Meth Lab: p.5/35

Transformations: cond (cond (p1 e1) (p2 e2) (pn en)) = (if p1 e1 (if p2 e2 (if pn en false) )) From Syntactic Sugar to the Syntactic Meth Lab: p.6/35

Transformations: cond (cond (p1 e1) (p2 e2) (pn en)) = (if p1 e1 (if p2 e2 (if pn en false) )) (define (unsugar-cond exp)... ) From Syntactic Sugar to the Syntactic Meth Lab: p.6/35

Transformations: cond (cond (p1 e1) (p2 e2) (pn en)) = (if p1 e1 (if p2 e2 (if pn en false) )) (define (unsugar-cond exp) (if (null? (cond-clauses exp)) false... )) From Syntactic Sugar to the Syntactic Meth Lab: p.6/35

Transformations: cond (cond (p1 e1) (p2 e2) (pn en)) = (if p1 e1 (if p2 e2 (if pn en false) )) (define (unsugar-cond exp) (if (null? (cond-clauses exp)) false (list if (clause-predicate (car (cond-clauses exp))) (clause-expression (car (cond-clauses exp))) (cons cond (cdr (cond-clauses exp)))))) From Syntactic Sugar to the Syntactic Meth Lab: p.6/35

Transformations: cond (cond (p1 e1) (p2 e2) (pn en)) = (if p1 e1 (if p2 e2 (if pn en false) )) (define (unsugar-cond exp) (if (null? (cond-clauses exp)) false (list if (clause-predicate (car (cond-clauses exp))) (clause-expression (car (cond-clauses exp))) (cons cond (cdr (cond-clauses exp)))))) (define cond-clauses... ) (define clause-predicate... ) (define clause-expression... ) From Syntactic Sugar to the Syntactic Meth Lab: p.6/35

Sugar (Refactored) Let s refactor the code so mc-eval doesn t have to change with each new form of syntactic sugar: (define (mc-eval exp env) (cond ((self-evaluating? exp) exp) ((variable? exp) (lookup-variable-value exp env)) ((let? exp) (mc-eval (unsugar-let exp) env)) ((cond? exp) (mc-eval (unsugar-cond exp) env)) ((while? exp) (mc-eval (unsugar-while exp) env)) ((for? exp) (mc-eval (unsugar-for exp) env)) (else (error "Unknown expression type EVAL" exp)))) From Syntactic Sugar to the Syntactic Meth Lab: p.7/35

Sugar (Refactored) Let s refactor the code so mc-eval doesn t have to change with each new form of syntactic sugar: (define (mc-eval exp env) (cond ((self-evaluating? exp) exp) ((variable? exp) (lookup-variable-value exp env)) ((sugar? exp) (mc-eval (unsugar exp) env)) (else (error "Unknown expression type EVAL" exp)))) From Syntactic Sugar to the Syntactic Meth Lab: p.7/35

Sugar (Refactored) We need a table of sugar (name, transformation) pairs. From Syntactic Sugar to the Syntactic Meth Lab: p.8/35

Sugar (Refactored) We need a table of sugar (name, transformation) pairs. (define sugar (list (cons let unsugar-let) (cons cond unsugar-cond)... )) From Syntactic Sugar to the Syntactic Meth Lab: p.8/35

Sugar (Refactored) We need a table of sugar (name, transformation) pairs. (define sugar (list (cons let unsugar-let) (cons cond unsugar-cond)... )) We need a sugar? predicate that returns true if the given expression starts with a name in the sugar table. From Syntactic Sugar to the Syntactic Meth Lab: p.8/35

Sugar (Refactored) We need a table of sugar (name, transformation) pairs. (define sugar (list (cons let unsugar-let) (cons cond unsugar-cond)... )) We need a sugar? predicate that returns true if the given expression starts with a name in the sugar table. (define (sugar? exp) (assq (car exp) sugar )) From Syntactic Sugar to the Syntactic Meth Lab: p.8/35

Sugar (Refactored) We need a table of sugar (name, transformation) pairs. (define sugar (list (cons let unsugar-let) (cons cond unsugar-cond)... )) We need a sugar? predicate that returns true if the given expression starts with a name in the sugar table. (define (sugar? exp) (assq (car exp) sugar )) We need unsugar which unsugars an expression according to the transformation in the table. From Syntactic Sugar to the Syntactic Meth Lab: p.8/35

Sugar (Refactored) We need a table of sugar (name, transformation) pairs. (define sugar (list (cons let unsugar-let) (cons cond unsugar-cond)... )) We need a sugar? predicate that returns true if the given expression starts with a name in the sugar table. (define (sugar? exp) (assq (car exp) sugar )) We need unsugar which unsugars an expression according to the transformation in the table. (define (unsugar exp) (let ((transform (cdr (assq (car exp) sugar )))) (transform exp))) From Syntactic Sugar to the Syntactic Meth Lab: p.8/35

Sugar (Refactored) To add syntactic sugar, write an unsugar Scheme procedure and add an entry to the sugar table. (define sugar (list (cons thing unsguar-thing)... )) From Syntactic Sugar to the Syntactic Meth Lab: p.9/35

Sugar (Refactored) To add syntactic sugar, write an unsugar Scheme procedure and add an entry to the sugar table. (define sugar (list (cons thing unsguar-thing)... )) Let s write a procedure to make it easy: From Syntactic Sugar to the Syntactic Meth Lab: p.9/35

Sugar (Refactored) To add syntactic sugar, write an unsugar Scheme procedure and add an entry to the sugar table. (define sugar (list (cons thing unsguar-thing)... )) Let s write a procedure to make it easy: (define (define-sugar! name transformation) (set! sugar (cons (cons name transformation) sugar ))) From Syntactic Sugar to the Syntactic Meth Lab: p.9/35

Sugar (Refactored) To add syntactic sugar, write an unsugar Scheme procedure and add an entry to the sugar table. (define sugar (list (cons thing unsguar-thing)... )) Let s write a procedure to make it easy: (define (define-sugar! name transformation) (set! sugar (cons (cons name transformation) sugar ))) For example: (define-sugar! delay (λ (exp) (list lambda () (cadr exp)))) From Syntactic Sugar to the Syntactic Meth Lab: p.9/35

Sugar (Refactored) To add syntactic sugar, write an unsugar Scheme procedure and add an entry to the sugar table. (define sugar (list (cons thing unsguar-thing)... )) Let s write a procedure to make it easy: (define (define-sugar! name transformation) (set! sugar (cons (cons name transformation) sugar ))) For example: (define-sugar! delay (λ (exp) (list lambda () (cadr exp)))) We still haven t done anything interesting. From Syntactic Sugar to the Syntactic Meth Lab: p.9/35

Macros The idea: Why not give define-sugar! to the user? In other words: Make a define-sugar Scream form. As a language implementor, we only need to worry about the core forms. As a user, we can cook our own syntactic abstractions. We d like to write (in Scream): (define-sugar delay (lambda (exp) (list lambda () (cadr exp)))) (define (force thunk) (thunk))... code using delay and force... From Syntactic Sugar to the Syntactic Meth Lab: p.10/35

How would we do this? How can we make define-sugar a Scream form? (define (mc-eval exp env) (cond ((define-sugar? exp) (define-sugar! (cadr exp) (??? exp)) ok) )) From Syntactic Sugar to the Syntactic Meth Lab: p.11/35

How would we do this? How can we make define-sugar a Scream form? (define (mc-eval exp env) (cond ((define-sugar? exp) (define-sugar! (cadr exp) (??? exp)) ok) )) But what should??? be? caddr? (caddr (define-sugar delay (lambda (exp) (list lambda () (cadr exp))))) =? From Syntactic Sugar to the Syntactic Meth Lab: p.11/35

How would we do this? How can we make define-sugar a Scream form? (define (mc-eval exp env) (cond ((define-sugar? exp) (define-sugar! (cadr exp) (??? exp)) ok) )) But what should??? be? caddr? (caddr (define-sugar delay (lambda (exp) (list lambda () (cadr exp))))) = (lambda (exp) (list lambda () (cadr exp))) From Syntactic Sugar to the Syntactic Meth Lab: p.11/35

How would we do this? How can we make define-sugar a Scream form? (define (mc-eval exp env) (cond ((define-sugar? exp) (define-sugar! (cadr exp) (??? exp)) ok) )) But what should??? be? caddr? (caddr (define-sugar delay (lambda (exp) (list lambda () (cadr exp))))) = (lambda (exp) (list lambda () (cadr exp))) Will this work? From Syntactic Sugar to the Syntactic Meth Lab: p.11/35

How would we do this? How can we make define-sugar a Scream form? (define (mc-eval exp env) (cond ((define-sugar? exp) (define-sugar! (cadr exp) (??? exp)) ok) )) But what should??? be? caddr? (caddr (define-sugar delay (lambda (exp) (list lambda () (cadr exp))))) = (lambda (exp) (list lambda () (cadr exp))) Will this work? (define-sugar! delay (lambda (exp) (list lambda () (cadr exp)))) From Syntactic Sugar to the Syntactic Meth Lab: p.11/35

The problem (lambda (exp) (list lambda () (cadr exp))) (lambda (exp) (list lambda () (cadr exp))) The define-sugar! procedure is expecting a name (a symbol) and a Scheme procedure. We re giving it a name and a list representing a (Scheme?, Scream?) procedure. From Syntactic Sugar to the Syntactic Meth Lab: p.12/35

The problem (lambda (exp) (list lambda () (cadr exp))) (lambda (exp) (list lambda () (cadr exp))) The define-sugar! procedure is expecting a name (a symbol) and a Scheme procedure. We re giving it a name and a list representing a (Scheme?, Scream?) procedure. Are we toadily screwed? From Syntactic Sugar to the Syntactic Meth Lab: p.12/35

The problem (lambda (exp) (list lambda () (cadr exp))) (lambda (exp) (list lambda () (cadr exp))) The define-sugar! procedure is expecting a name (a symbol) and a Scheme procedure. We re giving it a name and a list representing a (Scheme?, Scream?) procedure. Are we toadily screwed? Which is it? Scheme? Scream? From Syntactic Sugar to the Syntactic Meth Lab: p.12/35

The problem (lambda (exp) (list lambda () (cadr exp))) (lambda (exp) (list lambda () (cadr exp))) The define-sugar! procedure is expecting a name (a symbol) and a Scheme procedure. We re giving it a name and a list representing a (Scheme?, Scream?) procedure. Are we toadily screwed? Which is it? Scheme? Scream? When you come to a fork in the road, take it. Yogi Berra From Syntactic Sugar to the Syntactic Meth Lab: p.12/35

Transformations as Scheme procedures If we want to implement transformations as Scheme procedures, we have to solve the problem of going from a list representation of a lambda expression, to the lambda expression itself. (lambda (exp) (list lambda () (cadr exp))) (lambda (exp) (list lambda () (cadr exp))) Requires linguistic transubstantiation. As with any act of God it should not be taken lightly. We have not seen how to do this in this course. Scheme has its own meta-circular evaluator: eval. From Syntactic Sugar to the Syntactic Meth Lab: p.13/35

eval (eval (lambda (exp) (list lambda () (cadr exp)))) (lambda (exp) (list lambda () (cadr exp))) From Syntactic Sugar to the Syntactic Meth Lab: p.14/35

Sugar using eval Let s look at mc-eval now: (define (mc-eval exp env) ((define-sugar? exp) (define-sugar! (sugar-name exp) (eval (sugar-transform exp))) ok) ) (define sugar-name... ) ;; usual syntax stuff (define sugar-transform... ) We ve successfully given the user the power of define-sugar!. We ve broken a serious abstraction barrier. From Syntactic Sugar to the Syntactic Meth Lab: p.15/35

Exposed! We started off trying to give the user the ability to specify expression transformations for syntactic sugar. We ended up embedding the meta-language in the object-language. Exposing the implementing language is bad. What if we wanted to change the implementing language? (To say, Haskell or C?) Another consequence of this approach: transformations cannot refer to Scream values. (define (unsugar-thing exp)... ) (define-sugar thing unsugar-thing) Scheme knows nothing about unsugar-thing. From Syntactic Sugar to the Syntactic Meth Lab: p.16/35

Transformations as Scream procedures Let s rethink the define-sugar approach. If we can program list transformations in Scream, can t we use Scream procedures to implement sugar? We can program things like the following in Scream after all, right? (define (unsugar-let exp) (cons (list lambda (let-vars exp) (let-body exp)) (let-exps exp))) (define (let-vars exp) (map car (cadr exp))) (define (let-exps exp) (map cadr (cadr exp))) (define (let-body exp) (caddr exp)) From Syntactic Sugar to the Syntactic Meth Lab: p.17/35

Transformations as Scream procedures Let s rethink the define-sugar approach. The sugar table should still associated names with transformations, but transformations should now be Scream procedures. (let s assume it s initially empty now.) A transformation is a Scream procedure that inputs a list and outputs a list (representing a Scream expression). What s shakin? sugar? unchanged. define-sugar! unchanged. (define sugar ()). unsugar has to change. Why? From Syntactic Sugar to the Syntactic Meth Lab: p.18/35

Transformations as Scream procedures Let s rethink the define-sugar approach. The old unsugar procedure looked up a transformation (a Scheme procedure) in sugar, then applied the procedure to the given expression. (define (unsugar exp) (let ((transform (cdr (assq (car exp) sugar )))) (transform exp))) Now, lookup stays the same, but transform is now a Scream procedure. How can we apply it? (define (unsugar exp) (let ((transform (cdr (assq (car exp) sugar ))))... )) From Syntactic Sugar to the Syntactic Meth Lab: p.19/35

Transformations as Scream procedures Let s rethink the define-sugar approach. The old unsugar procedure looked up a transformation (a Scheme procedure) in sugar, then applied the procedure to the given expression. (define (unsugar exp) (let ((transform (cdr (assq (car exp) sugar )))) (transform exp))) Now, lookup stays the same, but transform is now a Scream procedure. How can we apply it? (define (unsugar exp) (let ((transform (cdr (assq (car exp) sugar )))) (mc-apply transform (list exp)))) From Syntactic Sugar to the Syntactic Meth Lab: p.19/35

Transformations as Scream procedures What do we know so far? (define (mc-eval exp env) (cond ((self-evaluating? exp) exp) ((variable? exp) (lookup-variable-value exp env)) ((sugar? exp) (mc-eval (unsugar exp) env)) ((define-sugar? exp) (define-sugar! (sugar-name exp)... (sugar-transform exp)... ) ok) (else (error "Unknown expression type EVAL" exp)))) From Syntactic Sugar to the Syntactic Meth Lab: p.20/35

Transformations as Scream procedures How should we fill this out? (define-sugar! (sugar-name exp)... (sugar-transform exp)... ) In English, how do we want to evaluate (define-sugar name exp)? From Syntactic Sugar to the Syntactic Meth Lab: p.21/35

Transformations as Scream procedures How should we fill this out? (define-sugar! (sugar-name exp)... (sugar-transform exp)... ) In English, how do we want to evaluate (define-sugar name exp)? Evaluate exp. From Syntactic Sugar to the Syntactic Meth Lab: p.21/35

Transformations as Scream procedures How should we fill this out? (define-sugar! (sugar-name exp)... (sugar-transform exp)... ) In English, how do we want to evaluate (define-sugar name exp)? Evaluate exp. In what environment? From Syntactic Sugar to the Syntactic Meth Lab: p.21/35

Transformations as Scream procedures How should we fill this out? (define-sugar! (sugar-name exp)... (sugar-transform exp)... ) In English, how do we want to evaluate (define-sugar name exp)? Evaluate exp. In what environment? The current environment. From Syntactic Sugar to the Syntactic Meth Lab: p.21/35

Transformations as Scream procedures How should we fill this out? (define-sugar! (sugar-name exp)... (sugar-transform exp)... ) In English, how do we want to evaluate (define-sugar name exp)? Evaluate exp. In what environment? The current environment. It should be a procedure. Associate name with this procedure in the table of syntactic sugar forms. From Syntactic Sugar to the Syntactic Meth Lab: p.21/35

Transformations as Scream procedures How should we fill this out? (define-sugar! (sugar-name exp)... (sugar-transform exp)... ) In English, how do we want to evaluate (define-sugar name exp)? Evaluate exp. In what environment? The current environment. It should be a procedure. Associate name with this procedure in the table of syntactic sugar forms. (define-sugar! (sugar-name exp) (mc-eval (sugar-transform exp) env)) From Syntactic Sugar to the Syntactic Meth Lab: p.21/35

Done(?) (define (mc-eval exp env) (cond ((self-evaluating? exp) exp) ((sugar? exp) (mc-eval (unsugar exp) env)) ((define-sugar? exp) (define-sugar! (sugar-name exp) (mc-eval (sugar-transform exp) env)) ok) (else (error "Unknown expression type EVAL" exp)))) (define (unsugar exp) (let ((transform (cdr (assq (car exp) sugar )))) (mc-apply transform (list exp)))) (define sugar ()) (define (sugar? exp) (assq (car exp) sugar )) (define (define-sugar! name transformation) (set! sugar (cons (cons name transformation) sugar ))) From Syntactic Sugar to the Syntactic Meth Lab: p.22/35

Some worrisome examples Application or Sugar? (define f... ) (define-sugar f... ) (f X Y Z) What about? (define-sugar f... ) (define f... ) (f X Y Z) What about? (define-sugar f... ) (let ((f... )) (f X Y Z)) What should happen here? (define-sugar f... ) (define g... ) (g f) Analogous to (g cond) in Scheme. From Syntactic Sugar to the Syntactic Meth Lab: p.23/35

The problem We have essentially two environment structures. One for (variable,value) pairs. One for (sugar name,transformer) pairs. By making them distinct sugar names aren t properly shadowed. Why not merge the two environment structures into one? In other words, let s put sugar into env. From Syntactic Sugar to the Syntactic Meth Lab: p.24/35

The solution (sketch) Change environment structure to include (( sugar var) transform) associations. lookup-variable-value has to change. sugar? should check for a ( sugar var) binding in env. unsugar should get the transform from env. define-sugar! should take an env and update it (much like define-variable!). If we lookup a variable s value and find it is bound to a sugar transformation, raise an error, eg. (f cond). You can do all of these in the privacy of your own home. From Syntactic Sugar to the Syntactic Meth Lab: p.25/35

let-sugar We can now add new constructs: (let-sugar ((f... ) (g... ))... code using f and g... ) (define (mc-eval exp env) ((let-sugar? exp) (mc-eval (let-sugar-body exp) (extend-environment (map (λ (name) (list sugar name)) (let-sugar-names exp)) (let-sugar-transforms exp) env))) ) From Syntactic Sugar to the Syntactic Meth Lab: p.26/35

Some examples Delays and Streams: (define-sugar delay (lambda (exp) (list lambda () (cadr exp)))) (define (force thunk) (thunk)) (define-sugar stream-cons (lambda (exp) (list cons (cadr exp) (list delay (caddr exp))))) (define the-empty-stream ()) (define empty-stream? null?) (define stream-car car) (define (stream-cdr s) (force (cdr s))) From Syntactic Sugar to the Syntactic Meth Lab: p.27/35

Some examples All the sugar you can eat: (define-sugar let (lambda (exp) (cons (list lambda (let-vars exp) (let-body exp)) (let-exps exp)))) (define-sugar cond (lambda (exp) (if (null? (cond-clauses exp)) false (list if (clause-predicate (car (cond-clauses exp))) (clause-expression (car (cond-clauses exp))) (cons cond (cdr (cond-clauses exp))))))) From Syntactic Sugar to the Syntactic Meth Lab: p.28/35

Some ideas Little languages: (define-sugar regexp (lambda (exp)... )) (define starts-with-xys-or-pqs? (regexp ( (+ ( "x" "y")) (+ ( "p" "q"))))) (define-sugar automaton (lambda (exp)... )) (define m (automaton init (init : (c more)) (more : (a more) (d more) (r end)) (end : accept))) (m (c a d d a r)) true (m (c a d d a r r)) false From Syntactic Sugar to the Syntactic Meth Lab: p.29/35

The hygiene problem Consider the following swap! macro: (define-sugar swap! (lambda (exp) (list let (list (list tmp (cadr exp))) (list set! (cadr exp) (caddr exp)) (list set! (caddr exp) tmp)))) (let ((x 1) (y 2)) (swap! x y)) (let ((x 1) (y 2)) (let ((tmp x)) (set! x y) (set! y tmp))) (let ((tmp 1) (y 2)) (swap! tmp y)) (let ((tmp 1) (y 2)) (let ((tmp tmp)) (set! tmp y) (set! y tmp))) From Syntactic Sugar to the Syntactic Meth Lab: p.30/35

The hygiene problem Consider the following stream-cons! macro: (define-sugar stream-cons (lambda (exp) (list cons (cadr exp) (list delay (caddr exp))))) (stream-cons 1 null-stream) (cons 1 (delay null-stream)) (let ((cons 1)) (stream-cons 1 null-stream)) (let ((cons 1)) (cons 1 (delay null-stream))) From Syntactic Sugar to the Syntactic Meth Lab: p.31/35

Scheme Macros There are two flavors of Scheme macros. A procedural system much like what we ve just seen, except it solves the hygiene problem and uses a syntax datatype in place of lists and symbols. A rewriting system that uses pattern matching and templates to specify transformations (also hygienic). Let s look at the rewriting system (aka syntax-rules). (define-syntax let (syntax-rules () ((let ((name val)... ) body) ((lambda (name... ) body) val... )))) From Syntactic Sugar to the Syntactic Meth Lab: p.32/35

Scheme Macros (define-syntax delay (syntax-rules () ((delay e) (lambda () e)))) (define-syntax stream-cons (syntax-rules () ((stream-cons x y) (cons x (delay y))))) (define-syntax and (syntax-rules () ((and) #t) ((and test) test) ((and test1 test2... ) (if test1 (and test2... ) #f)))) (define-syntax or (syntax-rules () ((or) #f) ((or test) test) ((or test1 test2... ) (let ((x test1)) (if x x (or test2... )))))) From Syntactic Sugar to the Syntactic Meth Lab: p.33/35

Scheme Macros (define-syntax automaton (syntax-rules (:) ((automaton init-state (state : response... )... ) (let-syntax ((process-state (syntax-rules (accept ) (( accept) (lambda (input) (empty? input))) (( (label target) (...... )) (lambda (stream) (case (first input) ((label) (target (rest input))) (...... ) (else #f))))))) (letrec ((state (process-state response... ))... ) init-state))))) From Syntactic Sugar to the Syntactic Meth Lab: p.34/35

Head From Syntactic Sugar to the Syntactic Meth Lab: p.35/35