Homework 7. ML and Typechecking.

ML

Do these problems in Scheme and/or ML. These should serve as a good preparation for the Midterm.

Sethi: problems 8.4e, 8.7, 8.8, 8.9, 8.10, 8.11, 8.12, 8.13.

Functional programming

The following are good problems to look at in Scheme. You might not know how to do them in ML just yet.

9.8, 9.10 (nice exercise on higher-order programming)

9.1-9.3 - we have done most of these. Just make sure you can do these problems in Scheme.

Typechecking

We have identified 4 types of type equivalence. Read pages 139-143 of Chapter 4 of Sethi. These are: structural equivalence, pure name equivalence, transitive name equivalence and type expression equivalence. ML uses structural equivalence and so does C.

Assume the following (Pascal-like) definition:

 
      x, y : array [0..9] of integer
      z : array [0..9] of integer

In Pascal, x and y have the same type, since they are declared together, but z does not. In the corresponding C and ML fragments, x, y, z all have the same type.

Consider the following fragment, writen in a Pascal-like imperative language. Identify all type errors if the type equivalence is

type
   SmallInt = 0 .. 99;
   SmallerInt = 0 .. 9;
var
   W: 0 .. 9;
   X: SmallInt;
   Y: SmallerInt;
   Z: 0 .. 9;
   Letter: 'A' .. 'Z';
   Digit: '0' .. '9';
procedure P(var Parm1: SmallInt; Parm2: SmallerInt);
   begin Parm1 := Parm2 end;
begin
   Digit := '0';
   Letter := 'A';
   Z := 0; X := 0; W := Z;
   if (Digit <= '0') or (Digit = Letter) then writeln('hello');
   Y := X;
   P(X,Z);
   P(X,X-1);
   P(W,Y)

Happy Haloween!