CS 3520 Homework 7   - Due October 24

Exercise 7.1, Playing Type Inferencer

In the first lecture 14 type-checked language, the types for function arguments and letrec-function results must be declared explicitly by the programmer.

Convert the following to well-typed expressions by replacing ? with a suitable type expression. In all cases, use the shortest type expression possible. For example, given the near-expression proc(? x)x, convert it to proc(int x)x, as opposed to proc(bool x)x or proc((int -> int) x)x.

  1. proc(? x)x
  2. let f = proc(? x)x in (f 10)
  3. let f = proc(? x)x g = proc(? y)y in (g f)
  4. let f = proc(? x)x g = proc(? y)(y false) in (g f)
  5. let f = proc(? x)proc(? z)x g = proc(? y)((y false) 10) in (g f)
  6. letrec ? f(? x) = x in (f false)
  7. letrec ? f(? x) = (f x) in (f false)
  8. letrec ? f(? x) = proc(? w)((f x) w) in ((f false) 12)
For each part i, supply your argument as a function answer-for-7.1.i that takes zero arguments and returns an string containing the expression text.

Exercise 7.2, Implementing a Type Checker

Your task is to implement a type checker for the tree language of homework 5, extended with type expressions and an extra loop-forever form:
  <expr> = !                            ;; loops forever, type is num
         | <number>
         | <id>
         | <primitive>(<expr>*,)
         | proc(<type> <id>) <expr>
         | (<expr> <expr>)
         | let <id> = <expr> in <expr>
         | if <id> then <expr> else <expr>
The type system includes the number type, function types, and pair types:
  <type> = num
         | (<type> -> <type>)
         | [<type> : <type>]

The main novelty of this type system is the handling of primitives. Type-checking for addition must ensure, for example, that only num-trees of the same shape are added. In general for this language, a typed program has no run-time errors.

The file hw7.scm provides a starting point, including a complete interpreter, datatype definitions for types, and most of a type checker. You must complete the checking of if and primitive applications.

You can test your checker with the type-check function:


Last update: Monday, October 21st, 2002
mflatt@cs.utah.edu