CS 3520 Homework 8 - Due November 1
Exercise 8.1, Type Inference for a Toy Language
Your task is to implement a type checker for the toy language of homework 5, extended with an extra loop-forever form:
<expr> = * ;; loops forever
| <number> ;; an integer
| @<number> ;; looks up a value in the env
| [<expr>] ;; creates a function (1 arg)
| call(<expr>, <expr>) ;; applies a function (1 arg)
| +(<expr>, <expr>) ;; add
| ifzero(<expr>, <expr>, <expr>) ;; branch
| -(<expr>, <expr>) ;; subtract
The type system is especially simple:
<type> = num
| (<type> -> <type>)
But since the expression language does not contain type annotations, your checker will have to infer the types. Note that a * expression can be given any type.The file hw8.scm provides a starting point, including a complete interpreter, datatype definitions for types, and helper functions for types.You must implement type-of-expression, including type environments and the type-comparing function. You can test your checker with the provided typecheck function:- (typecheck "[+(@0,1)]") => (num -> num)
- (typecheck "[1]") => (tvar74 -> num)
- (typecheck "[@0]") => (tvar74 -> tvar74)
- (typecheck "@1") => error
- (typecheck "call(1,1)") => error
You can also test your checker on the example expressions from homework 5. The expressions that produce errors should not pass your type checker (i.e., it should signal an error instead of producing a type). However, the large expression in 5.2 cannot pass your checker, because it's recursive.Here are a few additional evaluations to consider:- ifzero([1], 2, 3) => error
- ifzero(1, [2], 3) => ... shouldn't pass the checker
- call(ifzero(1, [2], [3]), 1) => 3
- call(ifzero(1, *, [3]), 1) => 3
Last update: Wednesday, October 24th, 2001mflatt@cs.utah.edu |