CS 360
Winter 2018
Programming Language Concepts
CS 360-001 Tuesday/Thursday 15:30-16:50 (Rush 209)
CS 360-002 Tuesday/Thursday 14:00-15:20 (Rush 209)
CS 360-003 Tuesday 18:30-21:20 (UCross 151)

 Instructor: Geoffrey Mainland mainland@drexel.edu Office: University Crossings 106 Office hours: Mondays 4pm–7pm; Thursdays 5pm–6pm. Teaching Assistant: Xiao Han CLC office hours: Tuesday 12pm–2pm; Thursday 6pm–8pm Allen Yang CLC office hours: Wednesday 6pm–8pm

Accept this assignment on GitHub Classroom here. The standard homework instructions apply.

In this assignment, you will modify the metacircular interpreter we saw in class. Successfully completing this assignment requires reading and understanding a medium-sized program written by someone else. If you do not have a good understanding of how the interpreter works, please review the material covered in lecture and in the book. Diving straight in to the homework without this understanding is going to make your task much more difficult.

You should complete all problems by modifying the README.md, mceval.rkt, and lazy-mceval.rkt files in your repository. Please do not create additional copies of mceval.rkt or lazy-mceval.rkt for the different parts of the assignment.

Only Problems 7 and 8 requires modifications to the lazy evaluator, lazy-mceval.rkt. For Problems 2–6, you should modify the applicative-order interpreter in mceval.rkt.

For Part II only, you may work with other students in class. You must record the members of your group in your README.md file.

This assignment is worth 100 points. There are 115 possible points.

Note: No late days may be used for Part I. We will cover the answers to Part I in class on Tuesday 1/30.

### Working with the interpreter

There are then two ways you can test your evaluator evaluator:

1. Type make and run the resulting binary, named mceval. The mceval program will repeatedly read in a Scheme expression and pass it to your interpreter for evaluation.

2. From DrRacket, call the top-mc-eval function with an expression, like this:

 (top-mc-eval '(+ 2 3))


I used the second approach. I also made judicious use of display and newline to debug my implementation.

### Hints for solving the problems

There are three ways to extend the interpreter:

1. Add a primitive.
2. Add a definition to the global environment.
3. Add a special form.

You should only add a special form when it is absolutely necessary. Most of the time, the standard Scheme evaluation rules are exactly what you want. Solving a problem by adding a definition rather than a new special form is also much easier and avoids cluttering up your mc-eval function.

Use display and newline to print out intermediate expressions! This is extremely helpful when debugging.

# Part I (20 points total)

Due Monday, January 29, 11:59:59PM EST. No late days may be used for Part I.

## Problem 1: Code Reading Questions (20 points total)

Submit the solutions to this problem in README.md.

### Problem 1.1: Environment representation (4 points)

What representation does the metacircular evaluator use for environments?

Please be specific. An English description will suffice; however, your answer will be stronger if you also provide examples.

### Problem 1.2: Defining the primitives (4 points)

What top-level define contains the list of primitives supported by the metacircular interpreter? Please name the variable.

### Problem 1.3: Understanding primitives (4 points)

This is Exercise 4.14 from SICP.

Eva Lu Ator and Louis Reasoner are each experimenting with the metacircular evaluator. Eva types in the definition of map, and runs some test programs that use it. They work fine. Louis, in contrast, has installed the system version of map as a primitive for the metacircular evaluator. When he tries it, things go terribly wrong. Explain why Louis’s map fails even though Eva’s works.

### Problem 1.4: Understanding mc-eval(4 points)

This is Exercise 4.2a from SICP.

Louis Reasoner plans to reorder the cond clauses in mc-eval so that the clause for procedure applications appears before the clause for assignments. He argues that this will make the interpreter more efficient: Since programs usually contain more applications than assignments, definitions, and so on, his modified mc-eval will usually check fewer clauses than the original mc-eval before identifying the type of an expression.

What is wrong with Louis’s plan? (Hint: What will Louis’s evaluator do with the expression (define x 3)?)

### Problem 1.5: Extending the environment (4 points)

The function setup-environment is used to create the initial global environment used by the metacircular interpreter. For later problems, it will be convenient to add your own definitions to the initial global environment. The most convenient way to do this is to call eval-definition with the appropriate arguments from within the function setup-environment. If you were to add a definition in this manner, what arguments would you pass to eval-definition to add the following top-level define to the initial global environment? You may give your answer in the form of a Scheme expression.

(define (not x) (if x false true))


# Part II (20 points total)

Due Monday, January 29, 11:59:59PM EST. You may work in a group only on this part of the homework.

## Problem 2: Adding Primitives (10 points total)

Add the following primitives: +, *, -, /, <, <=, =, >=, >, and error to mceval.rkt. 1 point each.

The error primitive should take no arguments and abort the interpreter with the message “Metacircular Interpreter Aborted” (without the quotes).

Hint: You should use Racket’s error function to raise an exception.

## Problem 3: Implementing and and or(10 points total)

Add support for and and or to your interpreter (5 points each) by modifying mceval.rkt. Be sure your implementation adheres to the Scheme language standard (see here for the relevant standard). Pay careful attention to how the arguments to and and or are evaluated and the value of the and or or expression.

You will probably want to use the last-exp?, first-exp, and rest-exps helper functions.

Remember that the metacircular interpreter cannot interpret #t and #f; use true and false instead.

# Part III (30 points total)

Due Monday, January 29, 11:59:59PM EST.

## Problem 4: Implementing let(10 points total)

Implement let expressions. Your implementation must create a new environment and evaluate the body of the let in this new environment. You will receive no credit for a solution that does not create a new environment, i.e., you may not implement let using the rewrite-as-a-lambda-application technique described in lecture.

Pay careful attention to the following “features” of let:

1. You must evaluate the expressions that determine the values of the variables bound by the let.
2. The body of a let is a sequence of expressions.

If you are unclear on the semantics of let, you can read the language standard.

Section 1.3.2 of SICP also contains an explanation of let.

## Problem 5: Implementing force and delay(20 points total)

Add support for force and delay to your interpreter, where delay‘ed expressions are only evaluated once when force‘d.

For full credit, you must support call-by-need evaluation, which only evaluates delay‘ed expressions once.

If your implementation is call-by-name but otherwise correct, you will receive 10 points. You do not need to submit both a call-by-name and a call-by-need implementation for full credit; just the call-by-need version will do.

It is highly recommended that you implement the call-by-name version first. If you get stuck on the call-by-need version without having implemented the call-by-name version, stop what you’re doing and get the call-by-name version working first.

We recommend you use memoization to implement call-by-need force and delay.

The implementations of both the call-by-name and call-by-need versions of force and delay were discussed in lecture. The easiest path to success will follow that implementation. If you have successfully implemented force and delay using the technique from lecture, then your evaluator should evaluate ((delay 5)) to 5 (why?).

Your solution must demonstrate that you understand the underlying mechanisms for implementing lazy evaluation. Therefore, you may not use Racket’s force and delay or equivalent syntax macros in your solution to Problem 5. The homework template is set up to prevent you from accidentally using Racket’s force and delay.

# Part IV (40 points total)

Due Monday, February 5, 11:59:59PM EST.

## Problem 6: Implementing streams (10 points total)

Add support for the following stream functions to your interpreter:

1. stream-cons (4 points)
2. empty-stream (1 points)
3. stream-empty? (1 points)
4. stream-first (2 points)
5. stream-rest (2 points)

You should be able to complete this problem with either the call-by-name or call-by-need implementation of force and delay. That is, you can receive full credit for this problem even if you did not receive full credit for Problem 5.

Your streams should be strict in the head of the stream and lazy in the tail. Note that Racket streams are lazy in both the head and the tail.

Your implementation must use your force and delay from Problem 5. You may not use Racket’s force and delay or equivalent syntax macros in your solution to Problem 6.

## Problem 7: Extending the lazy evaluator (10 points total)

For this problem, you will modify the lazy evaluator in lazy-mceval.rkt.

Add your primitives (Problem 1) and and and or (Problem 2) to the lazy evaluator. You should almost be able to copy the code over as-is. Be careful with and and or (why?).

## Problem 8: A Hybrid Evaluator (20 points total)

For this problem, you will modify the lazy evaluator in lazy-mceval.rkt.

The original metacircular evaluator in mceval.rkt implements applicative-order evaluation—arguments are evaluated before a function is called. The lazy evaluator in lazy-mceval.rkt implements normal-order evaluation—arguments are evaluated when they are needed. For this problem, you will modify the lazy evaluator to implement a hybrid approach: the definition of each function will specify which arguments are delayed and which are evaluated immediately. To illustrate, consider the following function we saw in lecture:

(define (try a b)
(if (= a 0) 1 b))


Evaluating (try 0 (/ 1 0)) will produce an error in the applicative-order interpreter. The problem is that the applicative-order interpreter evaluates all function arguments, even when they are not needed. Imagine we could write this instead (this is not standard Scheme!)

(define (try a (delayed b))
(if (= a 0) 1 b))


Where (delayed b) is an annotation to change the way compound procedures are applied as follows:

1. First evaluate the operator.
2. For each argument, if the corresponding parameter is annotated with delayed, delay its evaluation.
3. Otherwise, evaluate it.

In the case of try, this would mean that a is evaluated and b is delayed.

Modifying the interpreter to support this extension takes very little code, but it requires that you understand the metacircular interpreter and lazy evaluation.

## Problem 9: Homework Statistics (5 points total)

How long did spend on problems 1–8? Please tell us in your README.md. You may enter time using any format described here.