This homework is due December 2, 8:00 AM.
Submit your work using DrScheme's Handin button, even though it's OCaml code. The handin server will not run any tests.
Start with the the TICAE interpreter and typechecker from lecture.
Implement some of the follow additions, which are of varying difficulty. Each addition is annotated by a star rating; implement enough additions so that the sum of the star ratings is at least 6. Extra credit will be awarded for rating sums beyond 6.
★ 1. | Add an {instanceof <ICAE> <cid>} form to the language. The result of {instanceof <ICAE> <cid>} is 0 if <ICAE> produces an object that is an instance of the class <cid> or one of its sub-classes, otherwise it produces 1. The typechecker should ensure that <ICAE> does not produce a number, since the instanceof test could never succeed in that case. |
★★ 2. | Add a {cast <cid> <ICAE>} form to the language. At run-time, the cast expression reports an error if <ICAE> does not produce an instance of <cid> or one of its sub-classes. Otherwise, the result is just the resuilt of <ICAE>. Of course, <cid> should generally be a sub-type of the type for <ICAE>, and the type checker can assume that the {cast <cid> <ICAE>} has type <cid>. The type-checker must reject the expression if the type of <ICAE> is neither a sub-type nor super-type of <cid>, since the cast cannot succeed in that case. |
★★★ 3. | Add field assignment to the language. That is, in addition to the {get <ICAE> <fid>} form, add a {set <ICAE> <fid> <ICAE>} form that sets the value of the indicated field. (Hint: the value for each field in an object must be placed in a reference cell.) Update the typechecker to ensure that the field is always available for the assignment, and to ensure that the type of the second <ICAE> in a set form is a sub-type of the field's type. |
★ 4. | Add a Java-style null expression and value to the language. A null value can be used like an object, except that attempting to call a method or get (or set) a field of null produces a run-time error. The typechecker should ensure that null is never used like a number. |
★ 5. | [Pre-reqsuites: 3 and 4] Change new so that it does not accept values for the object's fields. Instead, new creates an object with null for each field's value, and then the fields can later be changed through set. |
★★★ 6. | Add Java-style object arrays to the language with the forms {newarray <tyexpr> <ICAE> <ICAE>}, {arrayref <ICAE> <ICAE>}, and {arrayset <ICAE> <ICAE>}.
|
★★ 7. | [Pre-requisite: 6] As in Java, allow a array-of-<tyexpr>1 to be a subtype of array-of-<tyexpr>2 when <tyexpr>1 is a sub-type of <tyexpr>2. A run-time check must be added to arrayset to ensure that an instance of a class is not installed into an array that requires sub-class instances. |
★★ 8. | Add multi-argument methods, so that method arguments must be explicitly named in a method definition (along with the type for each argument), and identifiers are expressions. Allow fields to referenced directly, like method arguments (i.e., using the field name as an expression instead of using a get form). |
★★★ 9. | Add Java-style overloading, where multiple methods can have the same name as long as they have different argument types. Usually, this is implemented by having the type-checker convert the input program to replace the original method names with names that describe the argument types. |
★★★★ 10. | Add Java-style interfaces to the language. An interface is a set of method names, and it is a sub-interface of an arbitrary number of other interfaces (in which case the interface includes all of the methods of its super-interfaces). A class implements any number of interfaces, and it is obliged to implement every method that is named in its implemented interfaces; the class is then a sub-type of the interface. The type language is extended to include interface names in addition to class names and num. |
★★★★ 11. | Add Java-style generics, which are a form of parametric polymorphism. A generic class is parameterized with respect to a type variable, and whenever the generic class name is used, a specific type for the parameterization must be supplied. Consult a Java reference for more information; ignore Java's support for generic methods and for using a generic type directly as a type (which is for backward compatibility only). |
Last update: Monday, November 28th, 2005mflatt@cs.utah.edu |