CSCI 421 HW 5
(Refer to Exercise 2 on page 130 of the textbook) Ada must sometimes use the context of a function call to determine which overloaded definition to use. Consider the statemente := f(f(a,b), f(c, d)) in an Ada-like language. Give an example of a set of types for the overloaded function f that makes this statement ambiguous, even when the types of a, b, c, d, and e are known.
Selected Answer: Consider the variables a, b, c, d, and e to be either type x or y. Specifically: a : x b : x c : x d : x e : y Consider the function f to be one of the following types: f1: x * x -> y f2: y * y -> y f3: x * x -> x If we then consider the statement e = f( f(a,b), f(c, d) ), we see that the statement is ambiguous because the overloading can be resolved two different ways: e = f2( f1(a,b), f1(c, d) )e = f2( f1(x,x), f1(x, x) ) -> e = f2(y, y) -> y e = f1( f3(a,b), f3(c, d) )e = f1( f3(x,x), f3(x, x) ) -> e = f1(x, x) -> y Therefore, the statement can be ambiguous, even when the types of a, b, c, d, e are known. Correct Answer: For any two types x and y, if f has these types: f1: x*x->yf2: y*y->yf3: x*x->x and if the variables are typed like this: a:xb:xc:xd:xe:y then the statement is ambiguous, since the overloading can be resolved in two ways: e:=f2(f1(a,b),f1(c,d))e:=f1(f3(a,b),f3(c,d))
(Refer to Exercise 4 on page 131 of the textbook) Consider an unknown language with integer and string types in which 1+2*3 evaluates to 7, "1"+"2"+"3" evaluates to "123", "1"+2+3 evaluates to "123", and 1+"2"+"3" has a type error. Describe a system of precedence, associativity, overloading, and coercion that could account for this. In your system, what is the result of evaluating the expression "1"+ 2*3?
Selected Answer: Precedence: The * operator has higher precedence than the + operator. Associativity: Both operators, + and *, are left-associative. Overloading: The + operator is overloaded with separate definitions for integers and strings. int * int -> int (adds the integers together to return the sum as an integer) string * string -> string (concatenates the strings together to return the combination as a string) Coercion: When the left operand of the + operator is a string and the right operand is an integer, the integer is first converted to a string. Then the + operator is applied, concatenating the two operands together. Based upon this system, the expression "1" + 2 * 3 is evaluated as such: First, the * operator is applied to 2 * 3 (highest precedence) Then, since the left operand is a string and the right operand is an integer, the right operand is coerced into a string ("6") Last, the + operator is applied, using the definition for strings, concatenating the two operands together: "1" + "6" = "16" Correct Answer: The * operator has higher precedence than the + operator, and both are left-associative. The + operator is overloaded, with type int*int->int for addition, and string*string->string for string concatenation. When the left operand of a + operator is a string and the right operand is an int, the int is coerced to string before the + operator is applied. Following these rules, we have: 1+2*3 = 1+(2*3) = 1+6 = 7"1"+"2"+"3" = ("1"+"2")+"3" = "12"+"3" = "123""1"+2+3 = ("1"+2)+3 = ("1"+"2")+"3" = "12"+"3 = "123" The expression 1+"2*3" causes a type error because no coercion is specified when the left operand of a + operator is an int and the right operand is a string. (This similar to the way Java works, but in Java the final expression would not cause a type error. In Java, when either operand of a + operator is of type String, the other is coerced to String.)
(Refer to Exercise 3 on page 131 of the textbook) Consider an unknown language with itneger and real types in which 1+2, 0+2, 1+2.0, and 0+2.0 are all legal expressions. a. Explain how this could be the result of coercion, using no overloading b. Explain how this could be the result of overloading, using no coercion. c. Explain how this could result from a combination of overloading and coercion.
Selected Answer: a. Explain how this could be the result of coercion, using no overloading With coercion, the language system could implicitly convert the integer type values to real type values before applying the + operator. b. Explain how this could be the result of overloading, using no coercion. With overloading, an overloaded + operator could have a separate definition for integer operands, real operands, and a combination of the two: int * int -> int real * real -> real int * real -> real real * int -> real The language system uses the operands' types to determine which definition of the + operator to apply in a given situation. c. Explain how this could result from a combination of overloading and coercion. With both coercion and overloading, an overloaded + operator could have less definitions for operands than the previous example I gave in b. Instead, it could have just two type definitions: one for integer operands and another for real operands. If an expression had mixed operands, both integer and real, then the language system could first use coercion to convert one of the operands so that it matched the other. For instance, if the expression was integer + real, then the integer operand could be implicitly converted to a real operand. Based upon the operands, the the language system could determine which definition of the + operator to apply (real * real -> real). Correct Answer: The * operator applies only to pairs of real numbers. All integers are coerced to real before the * operator is applied.The * operator has four types: int*int->int, int*real->real,real*int->real, and real*real->real. No coercion is performed.The * operator has two types: int*int->int and real*real->real. When the operands are of mixed types, the int is coerced to real before applying the real*real->real operator.
(Refer to Exercise 5 on page 131 of the textbook) Write an ML function definition for each of the following functions. Try to predict what polytype ML will infer for each funtion. Then check your prediction using the ML language system. What is the polytype determined by ML for each case? a. f(x) = 1 b. f(g) = g(1) c. f(g, x, y) = g(x, y) d. f(g, x) = g(g(x))
Selected Answer: a. f(x) = 1 'a -> int b. f(g) = g(1) (int -> 'a) -> 'a c. f(g, x, y) = g(x, y) ('a * 'b -> 'c) * 'a * 'b -> 'c d. f(g, x) = g(g(x)) ('a -> 'a) * 'a -> 'a Correct Answer: ' a. a -> int b. (int -> 'a) -> 'a c. ('a * 'b -> 'c) * 'a * 'b -> 'c d. ('a -> 'a) * 'a -> 'a
(Refer to Exercise 1 on page 130 of the textbook) Consider an unknown language with a left-associative + operator that is overloaded to have the following types: int*real -> real. int*int -> int, real*int -> real, and real*real -> real. Suppose that the variable i has type int and the variable r has type real. For each + in each of the following expressions, say which type of + is used: a. i + r b. i + r + i c. i + (r + i)
Selected Answer: a. i + r int * real -> real b. i + r + i start with the left-most operands, i + r: int * real -> real, then, r + i: real * int -> real c. i + (r + i) start with the operands in parentheses, r + i: real * int -> real, then, i + r: int * real -> real Correct Answer: a. i + r. int*real -> real b. i + r + i. int*real -> real, then real*int -> real c. i + (r + i). real*int -> real, then int*real -> real,