MiniML Typechecker

Due Monday, October 21 at 2pm

In this assignment, you will build a typechecker for MiniML. Your typechecker should handle the entire syntax of MiniML. Your typechecker must also properly handle recursive functions.

There are a few issues discussed in class that you will not be responsible for handling properly.

You can start the assignment from the version of MiniML in /cse/courses/cse505/02au/hw2. This is the same interpreter as used in the first assignment, with the following augmentations:

Unlike with the evaluator, we have not provided a template for the overall structure of the typechecker. Instead, we provide descriptions of some of the interesting aspects of the assignment:

Type annotations: Some declarations, expressions, and patterns have optional type annotations as part of their syntax (see ast.sml). In order to perform typechecking, you should require such annotations to always be provided, raising a type error otherwise.

Type equality: To test whether two Types are equal, you may use "=". This is not as precise as it could be, because we should be able to sometimes treat a function of type 'a->'a as also having the type 'b->'b. However, allowing type variables to be arbitrarily interchangeable would make this unsound interpreter even more unsound. The problem is that only generalizable type variables should be interchangeable in this way.

Top-level: Top-level declarations are typechecked by typechecking the associated declaration and returning any new bindings for the type environment. Top-level expressions are typechecked by typechecking the associated expression and returning a type environment with a binding that maps "it" to the expression's type.

Declarations: A declaration is typechecked in the context of a type environment, returning type bindings for any variables introduced by the declaration.

Expressions: An expression is typechecked in the context of a type environment and produces the expression's type. Some representative cases follow.

Patterns: Typechecking a pattern returns the type of values that match the pattern and a set of type bindings for variables introduced by the pattern. It is a type error if a pattern introduces the same variable name twice. The function combine_envs_no_dups in env.sml will be useful for checking this.

How and what to turn in

Send Todd email with your typecheck.sml file as an attachment. For extra credit, also provide an example of a function that typechecks in the unsound typechecker but not in the sound typechecker, and illustrate how that function can cause a run-time type error when invoked.