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.
- proc(? x)x
- let f = proc(? x)x in (f 10)
- let f = proc(? x)x g = proc(? y)y in (g f)
- let f = proc(? x)x g = proc(? y)(y false) in (g f)
- let f = proc(? x)proc(? z)x g = proc(? y)((y false) 10) in (g f)
- letrec ? f(? x) = x in (f false)
- letrec ? f(? x) = (f x) in (f false)
- 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:
- (type-check "proc(num x)+(x,1)") => (num -> num)
- (type-check "proc(num x)!") => (num -> num)
- (type-check "proc((num -> num) x)x") => ((num -> num) -> (num -> num))
- (type-check "proc([num : num] x)x") => ([num : num] -> [num : num])
- (type-check "cons(1,1)") => [num : num]
- (type-check "cons(1,cons(proc(num x)x,2))") => [num : [(num -> num) : num]]
- (type-check "car(cons(1,1))") => num
- (type-check "car(cons(proc(num x)1,1))") => (num -> num)
- (type-check "cdr(cons(proc(num x)1,1))") => num
- (type-check "+(1,!)") => num
- (type-check "+(cons(1,1),cons(!,!))") => [num : num]
- (type-check "+(1, (proc(num x)x 7))") => num
- (type-check "if 1 then 1 else 2") => num
- (type-check "if cons(1,+(1,!)) then 1 else 2") => num
- (type-check "y") => error
- (type-check "(1 1)") => error
- (type-check "+(1, cons(1,1))") => error
- (type-check "+(1, proc(num x)x)") => error
- (type-check "+(cons(1,2), cons(1,proc(num x)x))") => error
- (type-check "car(1)") => error
- (type-check "if 1 then proc(num x)x else 1") => error
- (type-check "if proc(num x)x then 1 else 2") => error
- (type-check "if cons(1,proc(num x)x) then 1 else 2") => error
Last update: Monday, October 21st, 2002mflatt@cs.utah.edu |