Nov 9, 1994
Due: November 16, 1994
Complex
to an object-oriented
language with existing types Integer
and Float
.
Explain how to make the + message work correctly using:
Dictionary
parameterized by K
, the type of the
keys in the dictionary, and V
, the type of the values in the
dictionary. Dictionary
has two operations:
put
, which takes two arguments of types K
and
V
respectively and doesn't return anything; and
at
, which takes one argument of type K
, and which
returns something of type V
.
We also have an abstract type Array
. Array
is
parameterized by a single type T
, the type of element in the
array. Arrays also have two operations: put
, which takes
two arguments of types Integer
and V
respectively
and doesn't return anything; and at
, which takes one
argument of type Integer
, and returns something of type
V
.
Another type is BlackHole
-- a dictionary that you can put
things into, but not get them back. BlackHole
is
parameterized by K
and V
. BlackHole
has one operation: put
, which takes two arguments of types
K
and V
respectively and doesn't return anything.
Finally there is a type Random
parameterized by K
and V
as usual. Random
has one operation, namely
at
, which takes one argument of type K
, and
which returns something of type V
(which it presumably
generates randomly -- we don't get to tell it at any rate).
Using the contravariant rule, what is the subtype relation between the following pairs of types? The answer to each question should be one of:
X
is a subtype of Y
Y
is a subtype of X
X
is a subtype of Y
and
Y
is a subtype of X
(in other words, the two
types are equivalent)
Assume that Number
is a subtype of Object
, and
Integer
is a subtype of Number
.
Dictionary[Integer,Object]
and Array[Object]
Dictionary[Number,Object]
and Array[Object]
Dictionary[Integer,Integer]
and Array[Number]
Dictionary[Integer,Number]
and
BlackHole[Integer,Object]
Dictionary[Integer,Object]
and
BlackHole[Integer,Number]
Dictionary[Integer,Number]
and
Random[Number,Object]
Dictionary[Integer,Object]
and
Random[Integer,Integer]