Download Type Checking Assignment in CS415: Multiple Inheritance and Array Assignments and more Assignments Programming Languages in PDF only on Docsity! CS415 — Written Assignment 5 March 21, 2008 This assignment asks you to prepare written answers to questions on type checking. Each of the questions has a short answer. You may discuss this assignment with other students and work on the problems together. However, your write-up should be your own individual work. Please print your name and email address on your homework! We need this information so that we can give you credit for the assignment and so that we can return it to you. 1. C++, unlike COOL, supports multiple inheritance. For example, the following class hierarchy is legal in C++: A / \ B C \ / D Allowing multiple inheritance changes the way we define and use the least upper bound (lub) function on types. (a) Explain, using at least one example, why it is necessary to change lub. (b) Describe how you might implement lub for multiple inheritance. Be brief. 2. The Java programming language includes arrays. The Java language specifica- tion states that if s is an array of elements of class S, and t is an array of elements of class T , then the assignment s = t is allowed as long as T is a subclass of S. This typing rule for array assignments turns out to be unsound. Java works around this by inserting runtime checks to throw an exception if arrays are used unsafely. 1 Consider the following Java program, which type checks according to the pre- ceeding rule: Listing 1: totally innocuous Java code class Mammal { String name; } class Dog extends Mammal { void beginBarking() { ... } } class Main { public static void main(String argv[]) { Dog x[] = new Dog[5]; Mammal y[] = x; // Insert your code here } } Add code to the main method so that the resulting program is a valid Java program (i.e., it compiles), but running that program triggers one of the afore- mentioned runtime checks. Include a brief explanation of how your program exhibits the problem. 3. The following typing judgments have one or more flaws. For each judgment, list the flaws and explain how they affect the judgment. (a) O ` e0 : T O ` T ≤ T0 O ` e1 : T1 O[x/T0] ` let x : T0 ← e0 in e1 : T1 (let− init) (b) O(id) = T0 O ` e1 : T1 T0 ≤ T1 O ` id← e1 : T1 (assign) (c) T ≤ C ` SELF TYPEC ≤ T (self − type) 2