Docsity

CS3723 Homework #5: Type Systems and Polymorphism, Assignments of Programming Languages

Toggle sidebar
bg1
cs3723 Homework #5, Due:2pm, 3/13/08
Note: If you collaborated with your classmates or used their notes, please note which classmates you collaborated with. If you use an external source,
besides the text book, lectures, notes provided by the instructor, and your own intellect, please cite that source. Use quote marks if you are quoting
material word-for-word from any source (including the text book).
Types (Chapter 6)
1. (a) What is a type?
(b) List three reasons types are used in programming languages.
2. (a) Define type error.
(b) Give an example of an operation and operands that would produce a hardware exception.
(c) Give an example of an operation and operands that would not produce a hardware exception but
would not behave as expected because of the type of the operands.
(d) Which of these two examples are type errors? (answer ‘neither,’ ‘first,’ ‘second,’ or ‘both’)
3. (a) Define type safety.
(b) List three language features found in C, C++, and/or Pascal that cause those languages to not be
type safe.
4. (a) List one advantage to using run-time type instead of compile-time type checking?
(b) List two advantages of compile-time type checking over run-time type checking?
(c) Why does compile-time type-checking need to be conservative if it is to be safe?
5. What is type inference? How does it differ from simple type checking?
6. (a) What is polymorphism?
(b) What is parametric polymorphism? Give an example of an ML program exhibiting parametric poly-
morphism and explain how the example demonstrates parametric polymorphism.
(c) What is subtype polymorphism? Give an example of a Java program exhibiting subtype polymor-
phism and explain how the example demonstrates subtype polymorphism.
(d) What is ad-hoc polymorphism? Give an example of a Java program exhibiting ad-hoc polymorphism
and explain how the example demonstrate ad-hoc polymorphism.
Type Inference Exercises (Chapter 6)
For each of the following ML functions, (1) draw an “Parse Graph,”1(2) label each node with that nodes type (if it is trivially known)
or a type variable, (3) list the set of a type constraints derivable from (the application and abstractions nodes in) the “parse graph,” (4)
solve constraints to determine the type of each variable, and (5) determine the type of the function.
7.fun g(x) = 10.0 - x;
(You can ignore the ad-hoc polymorphism and use ‘real * real -> real’ as the type of ‘-’.)
8.fun g(f,x,y) = f(x) div y;
9.fun g(f,x,y) = (2+f(3*x),x,y);
(You can use ‘int * int -> int’ as the type of ‘+’ and ‘*’.)
10.fun g(x,xs) = x::xs;
(The type of :: is 'a * 'a list -> 'a list where 'aparameterizes the polymorphic 'a list’ type.)
1Also called “abbreviated parse tree”; Mitchell, p. 137, p. 157ff. This terminology is non-standard; a more accurate description might
be “type-annotated abstract syntax tree.”
1

Partial preview of the text

Download CS3723 Homework #5: Type Systems and Polymorphism and more Assignments Programming Languages in PDF only on Docsity!

cs3723 Homework # 5 , Due: 2 pm, 3 / 13 / 08

Note: If you collaborated with your classmates or used their notes, please note which classmates you collaborated with. If you use an external source, besides the text book, lectures, notes provided by the instructor, and your own intellect, please cite that source. Use quote marks if you are quoting material word-for-word from any source (including the text book).

Types (Chapter 6 )

  1. (a) What is a type? (b) List three reasons types are used in programming languages.
  2. (a) Define type error. (b) Give an example of an operation and operands that would produce a hardware exception. (c) Give an example of an operation and operands that would not produce a hardware exception but would not behave as expected because of the type of the operands. (d) Which of these two examples are type errors? (answer ‘neither,’ ‘first,’ ‘second,’ or ‘both’)
  3. (a) Define type safety. (b) List three language features found in C, C++, and/or Pascal that cause those languages to not be type safe.
  4. (a) List one advantage to using run-time type instead of compile-time type checking? (b) List two advantages of compile-time type checking over run-time type checking? (c) Why does compile-time type-checking need to be conservative if it is to be safe?
  5. What is type inference? How does it differ from simple type checking?
  6. (a) What is polymorphism? (b) What is parametric polymorphism? Give an example of an ML program exhibiting parametric poly- morphism and explain how the example demonstrates parametric polymorphism. (c) What is subtype polymorphism? Give an example of a Java program exhibiting subtype polymor- phism and explain how the example demonstrates subtype polymorphism. (d) What is ad-hoc polymorphism? Give an example of a Java program exhibiting ad-hoc polymorphism and explain how the example demonstrate ad-hoc polymorphism.

Type Inference Exercises (Chapter 6 ) For each of the following ML functions, ( 1 ) draw an “Parse Graph,”^1 ( 2 ) label each node with that nodes type (if it is trivially known) or a type variable, ( 3 ) list the set of a type constraints derivable from (the application and abstractions nodes in) the “parse graph,” ( 4 ) solve constraints to determine the type of each variable, and ( 5 ) determine the type of the function.

  1. fun g(x) = 10.0 - x; (You can ignore the ad-hoc polymorphism and use ‘real * real -> real’ as the type of ‘-’.)
  2. fun g(f,x,y) = f(x) div y;
  3. fun g(f,x,y) = (2+f(3x),x,y); (You can use ‘int * int -> int’ as the type of ‘+’ and ‘’.)
  4. fun g(x,xs) = x::xs; (The type of :: is 'a * 'a list -> 'a list where 'a parameterizes the polymorphic 'a list’ type.) (^1) Also called “abbreviated parse tree”; Mitchell, p. 137 , p. 157 ff. This terminology is non-standard; a more accurate description might be “type-annotated abstract syntax tree.”

1