EECS 321 Programming Languages: Homework 8

Due: Friday, March 15th, 2013, noon

Install plai-typed.plt via DrScheme's File | Install .plt File ... menu to use the PLAI Typed language. Your homework must be written in the #lang plai-typed language, which will be available after installation.

See the plai-typed-demo.txt file for a quick introduction to plai-typed's type system.

See also the plai-typed documentation.

Part 1 – True, False, equals, and if

Add support for true, false, {= ... ...}, and {if ... ... ...} expressions, where = accepts only numbers (and produces a boolean), and if requires a boolean expression for the test and that the then and the else branches have the same type.

Don't write a parser; just test directly on abstract syntax.

Part 2 – Typed Lists

Add support for lists to the language by extending it with the following expressions:

  <TFAE> = ...
          | {cons <TFAE> <TFAE>}
          | {first <TFAE>}
          | {rest <TFAE>}
          | {nil <type>}
          | {empty? <TFAE>}
  <type> = number
          | bool
          | {<type> -> <type>}
          | {listof <type>}

All of which work as they do in Racket, with the exception of nil, which is just the empty list, and must be annotated with a type. For example, the type of {nil num} is {listof num}.

Require that all elements of a list have the same type, which you can ensure when type-checking cons.

It might seem unusual that nil must come with a type. Since the empty list can have many types, the programmer has to explicitly pick the one that agrees with the rest of the list.

Change the type checker to support lists. Again, don't bother with a parser and just use the abstract syntax directly.

Part 3 – Typing Rules

Here are the precise typing rules for the new constructs

Part N – Handin instructions

You must use the following definitions of types and expressions (you are free to make other types that you use internally).

(define-type TFAE
  [num (n : number)]
  [bool (b : boolean)]
  [add (l : TFAE) (r : TFAE)]
  [sub (l : TFAE) (r : TFAE)]
  [eql (l : TFAE) (r : TFAE)]
  [id (name : symbol)]
  [ifthenelse (tst : TFAE) (thn : TFAE) (els : TFAE)]
  [fun (arg : symbol) (typ : Type) (body : TFAE)]
  [app (rator : TFAE) (rand : TFAE)]
  [consl (fst : TFAE) (rst : TFAE)]
  [firstl (lst : TFAE)]
  [restl (lst : TFAE)]
  [nil (typ : Type)]
  [mtl? (lst : TFAE)])

(define-type Type 
  [arrowT (dom : Type) (codom : Type)]
  [listT (typ : Type)])

Your program must define this function:

type-check-expr : TFAE -> Type

If type-check-expr is passed an expression that isn't well-typed, then it should fail with an exception containing the string: type-error.

Last update: Wednesday, March 6th, 2013