CSE 130: Principles of Programming Languages
Solution and background for problem 8 of midterm

According to our text (page 137) and lectures, a type system is a set of rules that associates types to expressions, and a type system rejects an expression if the rules do not associate a type to that expression. Usually expressions are defined by a grammar in which (some of) the non-terminals are types, and an expression is accepted if and only if it can be parsed by the grammar, and the rules associate a type to the expression (more precisely, the rules are typically given as an "attribute grammar" in the sense of Section 2 of our class notes). The advantage of a type system defined this way is that type checking is static, i.e., it can be done at compile time. All the type systems we have seen so far in this course are of this kind.

Again according to our text (page 142) and lectures, a type error occurs when a function receives an argument of a type that it does not expect; this differs from expression rejection, since it refers to a (very limited) aspect of the semantics of functions, namely what types they expect as arguments; it does not refer to the syntax of expressions. An expression is type safe if it does not generate a type error when it is evaluated, and a type system is strong if no expression that it accepts causes a type error. A type system that is not strong is called weak. In practice, executing an expression that is not type safe may result in a core dump, an undocumented coercion, a random value, ...

Pages 122-3 of the text show that the Pascal type system is weak, because of the way it defines variant records: when the tag field is omitted, it is not possible to check for type errors at compile time. This is perhaps an ideal answer to the question.

However, there are much simpler (but somewhat artificial) examples of non-safe expressions in weak type systems, such as that given in class (on 17 Feb):

5 + '&' where the type system allows + to take two arguments having any types at all, but when + gets the argument 5 it expects to get another number, not a character. Many other examples like this are perfectly good answers, provided they are adequately explained, though of course the detailed explanations given above are much more than is necessary for this purpose.
To CSE 130 midterm
To CSE 130 homepage
© 2005 Joseph Goguen