Implementing Type-Checking, CSC3300, Fall 2026
1 Goal
Extend the interpreter to include a type system.
2 Guidelines
For this and all remaining assignments, every function you develop must come with the following things:
A commented header line that expresses the result of the function in terms of its inputs, written in English. Be as precise as you can within the space of a line or two.
A type declaration (possibly inline), specifying the input and output types.
Test cases. A function without test cases is incomplete. Write the test cases first, please.
For this assignment, you must develop your solutions using the typed/racket language. If you 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.
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 contain the string "DRMG". 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 "DRMG" will be considered to be an error in your implementation.
Additionally, your error messages should be actually helpful. Since you are the primary consumer of your own error messages, making these error messages good in the first place should reduce your overall development time. There are two parts to this: first, the error message should include text that actually indicates what the programmer did wrong. Second, include the text of the user’s program, so they (actually you) can figure out how to fix it. See lab 3 for an example of how to do this. (Apologies in advance if I renumber the labs and fail to update this paragraph....)
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 the Assignment 6 interpreter.
4 Syntax of DRMG7
A DRMG7 program consists of a single expression.
The concrete syntax of the DRMG7 expressions with these additional features can be captured with the following EBNFs.
| ‹expr› | ::= | ‹num› |
|
| | | ‹id› |
|
| | | ‹string› |
|
| | | { if ‹expr› ‹expr› ‹expr› } |
|
| | | { local [‹id›:‹ty›=‹expr›]* in ‹expr› } |
|
| | | { rocal [‹id›:‹ty›=‹expr›] in ‹expr› } |
|
| | | { % [‹id›:‹ty›]* -> ‹expr› } |
|
| | | { begin ‹expr› ‹expr›* } |
|
| | | { ‹expr› ‹expr›* } |
| ‹ty› | ::= | Real |
|
| | | Bool |
|
| | | Str |
|
| | | Null |
|
| | | { Arr ‹ty› } |
|
| | | { ‹ty›* -> ‹ty› } |
... where an id is not one of the keywords: if, local, rocal, =, %, :, ->, in, or begin.
4.1 Primitives
Note that there is no error primitive in this assignment. What would its type be?
procedure
(+ a b) → Real
a : Real b : Real
procedure
(- a b) → Real
a : Real b : Real
procedure
(* a b) → Real
a : Real b : Real
procedure
(/ a b) → Real
a : Real b : Real
procedure
(<= a b) → Bool
a : Real b : Real
procedure
(num-eq? a b) → Bool
a : Real b : Real
procedure
(str-eq? a b) → Bool
a : Str b : Str
procedure
(substring Str begin end) → string
Str : Str begin : Real end : Real
procedure
(strlen Str) → Real
Str : Str
procedure
(make-array len init-val) → {Arr T}
len : Real init-val : T
procedure
(aref array idx) → T
array : {Arr T} idx : Real
procedure
(aset! array idx newval) → Null
array : {Arr T} idx : Real newval : T
value
true : Bool
value
false : Bool
4.2 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. You should definitely make a separate function for this. Note that the types of functions are extended to handle multiple arguments. So, for instance, the type {Real Str -> Bool} refers to a function that accepts two arguments, a number and a string, and returns a boolean.
All type rules are standard.
As in the book, arrays will be homogeneous; you cannot put a different type of thing into an array. In a similar way, you cannot mutate a variable to contain a different type.
4.2.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 {Real Real -> Bool}, and one that only works for strings, called str-eq?, with type {Str Str -> Bool}.
Also, note that begin is not a primitive in this language. Can you see why?
4.2.2 Local bindings and Recursion
Both local bindings and recursion forms get types. Here’s an example of a very inefficient and silly way to compute perfect squares:
{rocal {{square-helper : {Real -> Real} = {% [n : Real] -> {if {<= n 0} 0 {+ n {square-helper {- n 2}}}}}}} in {local {[square : {Real -> Real} = {% [n : Real] -> {square-helper {- {* 2 n} 1}}}]} in {square 13}}}
5 Suggested Implementation Strategy
Remove the the error primitive. Hard-code a memory size of 2000 into your top-interp function, so that you can call it without specifying a memory size.
5.1 Moving on
Here are some of the steps that I followed. I wrote test cases for every step before implementing it. In order to facilitate testing of your type checker, you’ll probably want a function that calls the parser and then the type checker.
Add a data definition for Types.
Add a parser for the language of types.
Add : to the set of keywords.
Develop a type checker that only works for constants.
Add a definition for Type Environments, mapping symbols to types.
Extend your type checker to handle variables and if expressions.
Extend your type checker to handle applications. (Since you don’t have types for LamC terms yet, your test cases should just use primitive functions.)
Extend your AST nodes for LamC nodes to include types with the parameters.
Extend your type checker to handle LamC terms.
Extend your parser and type checker to handle the modified given expression.
Add your type checker to top-interp. Type checking should happen after parsing and before interpretation.
Add the rec-given form to the set of ExprCs.
Fix breakage temporarily by signaling an error in interp on instances of RecC.
Develop the parser clause for RecC. Test cases first!
Develop the type-checking clause for RecC. Test cases first!
Develop the interpreter clause for RecC. Test cases first!
6 Interface
Make sure that you include the following functions, and that they match this interface:
procedure
(parse s) → ExprC
s : Sexp
procedure
(parse-type s) → Ty
s : Sexp
procedure
(type-check e env) → Ty
e : ExprC env : TEnv
value
base-tenv : TEnv
procedure
(top-interp s) → string
s : s-expression
7 Hint
As on other assignments, uses of "cast" other than in the parser, when using a question-mark-pattern with ellipses, are likely to be bugs. The autograding framework highlights these as likely bug locations. It’s almost certainly not a good idea to use casting in your code.
8 Minimum Viable Product
Some of you may be thinking about submitting your solution to assignment 4 for this assignment. Good idea! It won’t actually do any type checking, but it will certainly still pass some of the tests.
If you’re thinking of doing this, the most important change you’ll have to make to your code is to handle the presence of types in % and given terms; update your parser to just ignore these extra pieces.