Assignment 7, CSC430, Winter 2018
1 Goal
2 Guidelines
2.1 Handling Errors
2.2 Progress Toward Goal comment
3 The Assignment
4 Syntax of YUKV7
4.1 Type Checking
4.1.1 Binops
5 Suggested Implementation Strategy
6 Interface
6.90.0.20

Assignment 7, CSC430, Winter 2018

1 Goal

Extend the evaluator (no classes) to include a type system.

2 Guidelines

For this and all remaining assignments, every function you develop must come with the following things:

For this assignment, you must develop your solutions using the typed/racket language. If you still haven’t seen them, you might be interested in these Hints on Using Typed Racket in CPE 430.

Your test cases must use the check-equal?, check-=, or check-exn forms.

Your solution should take the form of a single file. Solve each problem separately, ands make sure that each solution appears in a separate part of the file, with comments separating each problem’s solution.

Hand in your solution using the handin server. For help with the handin server, please see the course web page.

2.1 Handling Errors

All of your error messages must start with the string "YUKV: ". Essentially, this allows my test cases to distinguish errors correctly signaled by your implementation from errors in your implementation. To be more specific: any error message that doesn’t contain the string "YUKV" will be considered to be an error in your implementation.

2.2 Progress Toward Goal comment

Graders are happier when they know what to expect. Your final submission should start with a short one- or two-line comment indicating how far you got through the project. Ideally, this would just be: “Full project implemented.” But if you only implemented, say, squazz and blotz, and didn’t get to frob or dringo, please indicate this in the comment, so that we don’t spend all our time searching for bits that aren’t there.

3 The Assignment

This assignment will build on the same core as the previous assignment: the Assignment 3 evaluator, with the addition of strings and recursive bindings. If you have a completed submission for Assignment 5, you can simply discard the class desugaring. If not, you can start from the Assignment 4 checkpoint or even from Assignment 3.

4 Syntax of YUKV7

A YUKV7 program consists of a single expression.

The concrete syntax of the YUKV7 expressions with these additional features can be captured with the following EBNFs.

  expr = num
  | string
  | id
  | {if expr expr expr}
  | {var {id : ty = expr} ... expr}
  | {lam {[id : ty] ...} expr}
  | {rec {id : ty = {lam {[id : ty] ...} expr}} expr}
  | {expr expr ...}

  ty = num
  | bool
  | str
  | {ty ... -> ty}

  operator = +
  | -
  | *
  | /
  | num-eq?
  | str-eq?
  | <=
  | substring

... where an id is not var, if, lam, =, <-, ->, :, or rec.

4.1 Type Checking

Implement a type checker for your language. Note that since functions must now come annotated with types for arguments, you will need to have a type parser that parses types. For Heaven’s sake, make a separate function for this. Note that the types of functions are extended to handle multiple arguments. So, for instance, the type {num str -> bool} refers to a function that accepts two arguments, a number and a string, and returns a boolean.

All type rules are standard.

4.1.1 Binops

Type-checking binops is more or less as you might expect. For instance, a + should receive two numbers, and will produce a number. The <= operator will take two numbers, and return a boolean.

The equal? operator is a bit different. Specifically, we don’t have subtyping, and we treat the equality operator as a function in the environment, so it must have a single type. In order to simplify our lives, we split it into two equality operators; one that only works for numbers, called num-eq?, with type {num num -> bool}, and one that only works for strings, called str-eq?, with type {str str -> bool}.

5 Suggested Implementation Strategy

Here are some of the steps that I followed. I wrote test cases for every step before implementing it.

6 Interface

Make sure that you include the following functions, and that they match this interface:

procedure

(parse s)  ExprC

  s : Sexp
Parses an expression.

procedure

(type-check e env)  Ty

  e : ExprC
  env : TEnv
Type-check an expression.

value

base-tenv : TEnv

The base type environment.

procedure

(interp e env)  Value

  e : ExprC
  env : Environment
Interprets an expression, with a given environment.

procedure

(top-interp s)  string

  s : s-expression
Combines parsing, type-checking, evaluation, and serialization.