CS 3520 Homework 12

Due: Wednesday, December 4th, 2019 11:59pm

Difficulty:  ★★☆☆

A Generic Interpreter

The interpereters integer_lambda.rhm and string_lambda.rhm are extremely similar. The former is our usual interpreter with first-class functions. The latter has string expressions and values instead of number expressions and values, and it was created by copying all of the code in integer_lambda.rhm and modifying it to work with strings.

Instead of copying the parse, interp, and lookup functions, we’d like to have a single implementation that works with both. We don’t want numbers to work in string programs or strings to work in number programs, though; we want to keep the languages separate. When we start with an S-expression, we will decide whether to parse and interpret it as a number program or a string program.

The interpereters integer_lit_lambda.rhm and string_lit_lambda.rhm are even more similar than integer_lambda.rhm and string_lambda.rhm, because the literal-expression constructor is renamed from intE or strE to litE in both interpreters. Similarly, intV and strV are renamed to litV. New entry points parse_int and interp_int or parse_str and interp_str will let us pick which kind of program we want to parse and interpret.

Your task is to merge integer_lit_lambda.rhm and string_lit_lambda.rhm into a single module with a single implementation of parse, interp, and lookup. All of the tests in integer_lit_lambda.rhm and string_lit_lambda.rhm should work unmodified with the merged implementation.

To merge the implementation, you will need to change Exp to be parameterized over a type for literals, so that parse_int returns a Expof(Int), and parse_str returns a Expof(String). You’ll have to parameterize other types, and you’ll have to parameterize functions over other functions.


Last update: Friday, August 8th, 2025
mflatt@cs.utah.edu