Project 3: A type checker

In this project, you will write a type checker for Jay in ML, essentially an ML version of the program we saw in class on Jan 29.

1. Setup

In a directory for this class and project, copy and untar the files for this project

cp ~tvandrun/Public/cs365/proj3.tar .
tar xvf proj3.tar

This includes a variety of files. The only one you will be working on is typeCheck.sml. (You will also modify typeCheck-main.sml for the purposes of testing.)

2. Writing the type checker

The file typeCheck.sml contains the code for the type-checker, which you need to finish. This will help you understand what's going on in the type checker and what needs to be done.

First, there is the datatype tyty, and this corresponds to the Type enumeration in the Java version. Then there are a handful of utility functions, like converting a typeName from the abstract syntax to a tyty, and the function getOpType which gives a 3-tuple of tytys-- the two operand types and result types of the various operators.

The type map is implemented as a list of string, tyty pairs. The function getType looks up the type of an identifier in the type map. The set of uninitialized variables is implemented as a list of strings.

The work is done by the verify functions, starting with verifyProg, which takes a program and returns the number of type errors. Along the way, the specific type errors are displayed. Since a program is a list of declarations and a list of statements, we use the declarations to generate the type map, and then pass the type map to the functions that verify the statements.

Quite a bit of this is written for you. Here are the functions which you need to complete:

1. verifyDeclIds. Finish the function verifyDeclIds; replace (0, [], []) with something that appropriately generates the number of errors and revised typemap and revised set of uninitialized variables in the case where the first id on the list of identifiers is undefined in the type map so far.

2. verifyStmt(Assign(targ, expr), tm, uninit). Finish the function to verify assignment statements. Be sure to catch type errors of an undeclared target and a type mismatch between the expression and the target. Also, this should return a new set of uninitialized variables. How should the uninitialized set differ from the set received as a parameter?

3. verifyStmt(Loop(guard, body), tm, uninit). Using the verify function for conditionals (immediately above), write the verify function for loops. Be sure to catch the type error of a non-boolean guard. Be sure to update and return the uninitialized variable set.

4. verifyExpr(BinExpr(e1, opp, e2), tm, uninit) (the hard one). The function verifyExpression must do two things-- catch and record errors, and infer a type. Thus it must return a tuple: an int and a tyty. Using the verify functions for other expressions (especially unary expressions) as examples, write the verify function for binary expressions. Be sure to catch the type error of mismatched operands. Here the function getOpType will be of use. Note that ML allows you to use equals to compare tuples, as long as the tuple components can be compared using equals.

3. Testing

In the file typeCheck-main.sml, the line

runTests(["samples/Euclid.jay", "samples/Badtypes.jay"])

executes all the test files in the list. To add (or remove) tests, just edit this line. Then to run the type-checker on the listed test files, just enter at the command line

sml < test-t.sml

ML will spit out a verbose list of processing information, but you'll also see the result-- or a list of your mistakes.

You should also write testfiles of your own and test them.

4. Turn-in

Copy your typeCheck.sml to

/cslab/class/cs365/turnin/proj3/xxxxxx

where "xxxxxx" is [lily|chet|christopher|tim|david|ben|caleb]. I will grade your project by running it against a collection of test files.

DUE: Monday, Feb 8, 5:00 pm.


Thomas VanDrunen
Last modified: Tue Feb 12 10:19:58 CST 2008