Copyright © 2006 Gradiance Corporation.

 

 

     

Types

 

 



 

Questions on types and unification.

 


1.  

If integers take four bytes and reals take eight bytes, what is the width of the type int[3][5]?

 

 

 

 a) 

12

 

 b) 

180

 

 c) 

60

 

 d) 

20

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2.  

In the following figure:

are two trees representing type expressions. Greek letters α, β, and γ represent type variables, while Roman letters a through e represent constants --- that is, basic types like int or type operations like product. The numbers of the nodes are not part of the expressions, and are used only to refer to the nodes.

Attempt to unify the two trees. Which nodes on the left (nodes 1 through 5) unify with which nodes on the right (nodes 6 through 10)? Which type expressions are unified? [Note: assume type expressions are expressed in infix notation, so, e.g., the expression represented by the tree on the left is αbdac.]

Now, identify the true statement from the list below.

 

 

 

 a) 

Variable β unifies with expression c.

 

 b) 

Variable δ unifies with expression d.

 

 c) 

Variable γ unifies with expression αbd.

 

 d) 

Variable γ unifies with expression b.

 

 

 

 

 

3.  

Suppose we use the Java hierarchy of types:

 
          double
            |
           float
            |
           long
            |
           int
         /     \
      short    char
        |
      byte
 

and we wish to translate the assignment statement x=y+z to 3-address code. Assume x is of type long, y is of type short, and z is of type int. Coercion statements are of the form

a = (type) b

and convert b to an equivalent value of the indicated type, which must be higher on the hierarchy than the type of b. The result is assigned to a, which must be of that higher type. Note that in a normal translation scheme, the value of an expression is computed independently of the type of the variable to which that value (x in this case) is ultimately assigned.

Which of the following sequences of 3-address statements implements this assignment, with all necessary coercions?

 

 

 

 a) 

T1 = (short) z

T2 = y+T1

x = (long) T2

 

 b) 

T1 = (long) y

T2 = (long) z

T3 = T1+T2

x = T3

 

 c) 

T1 = (int) y

T2 = T1+z

x = (long) T2

 

 d) 

T1 = (long) y

T2 = (long) z

x = T1+T2

 

 

 

 

 

4.  

Suppose we use the Java hierarchy of types:

 
          double
            |
           float
            |
           long
            |
            int
          /     \
       short    char
         |
       byte
 

and we wish to translate the assignment statement x=y+z to 3-address code. Assume x is of type long, y is of type short, and z is of type char. Coercion statements are of the form

a = (type) b

and convert b to an equivalent value of the indicated type, which must be higher on the hierarchy than the type of b. The result is assigned to a, which must be of that higher type. Note that in a normal translation scheme, the value of an expression is computed independently of the type of the variable to which that value (x in this case) is ultimately assigned.

Which of the following sequences of 3-address statements implements this assignment, with all necessary coercions?

 

 

 

 a) 

T1 = (int) z

T2 = (int) y

T3 = T2+T1

x = (long) T3

 

 b) 

T1 = (long) y

T2 = (long) z

T3 = T1+T2

x = T3

 

 c) 

T1 = (long) y

T2 = (long) z

x = T1+T2

 

 d) 

T1 = (short) z

T2 = (char) y

T3 = T2+T1

x = (long) T3