supplement more on polymorphism oct 9
play

Supplement: More on Polymorphism (Oct. 9) In the slides from 9 - PowerPoint PPT Presentation

Supplement: More on Polymorphism (Oct. 9) In the slides from 9 October we find the following: Polymorphism results when the compiler finds that it doesn't need to know certain things What does this mean? Consider the following Haskell


  1. Supplement: More on Polymorphism (Oct. 9) In the slides from 9 October we find the following: • Polymorphism results when the compiler finds that it doesn't need to know certain things What does this mean? Consider the following Haskell function: f x y = x < y Function f returns a Boolean value of True if x is less than y, False otherwise. QUESTION: what are the types of x and y?

  2. Supplement: More on Polymorphism Answer : they could be Integer, Char, Float, … any data type that permits a “less than” comparison. The Haskell compiler (yes, it has one named ghc ) will successfully compile this code (as long as you include a “main” program along with it), even though it can’t determine the types of variables x and y . This type of polymorphism is called “parametric polymorphism.” According to the Haskell Wiki, “Polymorphism is widespread in Haskell and is a key feature of its type system.” https://wiki.haskell.org/Polymorphism

  3. Supplement: More on Polymorphism We know that in Java, objects of a subclass can be considered as objects of their superclass. We did an example of this on Oct. 9; unfortunately, I think the stuff about “not overriding instance variables” obscured the main point of the example. The only thing relevant to defining “polymorphism” is the fact that a variable declared to be of class “B” is also of class “A” (same variable can be interpreted as different types). This is an example of “subtype polymorphism.”

  4. Supplement: More on “orthogonality” (Oct. 9) It’s not worth making a big deal of this. It’s not so much an absolute property of a language as a relative one--in some languages, a large number of language features “work together” well in many combinations (orthogonality), in others not so much. Example: imagine every kind of statement has a value: “if” statements have a value; “for”-loops have a value, assignment statement have a value; etc. Then you can use any of these statements as an expression wherever a value is required. This is an example of orthogonality. (see next slide)

  5. Supplement: More on “orthogonality” In such a language, something like the following would be valid and would have some meaning (not necessarily an obvious or logical one, but a well-defined meaning nevertheless): a = (while (if 10-k then True) k=k+1) Many languages have some degree of orthogonality; for instance, in C we can use numeric values as booleans; we can use assignment statements in expressions; etc.

  6. Supplement: Type Systems (Oct. 9) The following is not a satisfactory definition in many ways, but it will have to do for now: A type system is a way of assigning properties to certain expressions so that we can reason about those expressions, that is, draw conclusions about how they can be used, about when they are compatible with each other, i.e., share common properties. These properties are called “types.” (See next slide.)

  7. Supplement: Type Systems For instance, when the Java compiler sees an expression like this – “ a = b + c ” – it can use the type information about a , b , and c to determine what to do according to a clear set of rules. For instance, “if a , b , and c are strings, generate code to concatenate strings.” “If a and b are strings and c is some other type, call the toString method on c , then concatenate.” “If a is a double and b and c are int , convert the expression b+c to double and store it in a .” The type system enables the compiler to determine these actions.

  8. Supplement: Type Systems A language is statically typed if the above-mentioned determinations can all be made at compile time; it is dynamically typed if these determinations are made at runtime. “Strongly typed” is a much less precise term. Basically, it’s a measure of “how much” type checking is done or how likely it is that a runtime error could occur as a result of faulty assumptions about the type of an expression. See next slide.

  9. Supplement: Type Systems Haskell is an example of a language with a very strong type system. Interestingly, it is often unnecessary to declare the type of a variable or expression in Haskell (compare to C and Java, where every variable has to be declared). In Haskell, the compiler or interpreter can look at any expression or function and determine as much type information as it needs: let f x y = x ++ y implies that x and y are lists containing objects of the same type. Thus, f [1,2,3] [‘a’,‘b’,‘c’] is rejected.

  10. A Type System Provides the Ability To Do... Type inference: the ability of compiler or interpreter to determine the type of an expression Type compatibility: the ability of the compiler or interpreter to see if two types can work together in a given situation. E. g., functions that expect a double will usually work if you provide an int . On the other hand, you can’t assign a double to an int , so here they are not compatible. Type equivalence: the ability to determine if two expressions are of the same type

  11. Supplement: Composite Types Page 300 in Scott lists a number of so-called “composite types”–types composed of simpler types. You know: arrays – sequences of values of one particular type, accessed by means of an index value (“arrays of integers”, “arrays of boolean”, “arrays of strings”, etc.). Usually fixed size. lists – more flexible, no fixed size, often defined recursively in terms of a “head” (first element) and a “tail” (list minus the head). Some languages allow mixed types of list values.

  12. Supplement: Composite Types sets – an unordered collection of data in which all elements must be distinct (arrays and lists can have duplicate values). For example, Python has a “set” type. pointers – (I’m not sure I would have called this a composite type!). A reference (often a memory address) to a value. We saw pointers in C. records – structures with a fixed number of fields that may all be of different types. We looked at “ struct ” types in C. (Similar to instance variables in an object-oriented language.)

  13. Supplement: Composite Types unions (also called “variant records”) – sort of like a record, but the fields share common memory. We looked at the “ union ” data type in C. (See next slide.) files – we didn’t look at these at all!

  14. One More “union” Example in C #include <stdio.h> union splat {/* defining a type named “union splat” */ char s[6]; /* character array s shares memory with i */ int i; }; int main() { union splat x; /* declare x and fill s with “hello” */ x.s[0] = 'h'; x.s[1] = 'e'; x.s[2] = 'l'; x.s[3] = 'l'; x.s[4] = 'o'; x.s[5] = '\0'; printf("BEFORE: x.s = %s\n",x.s); Output x.i = 79 + 256*(79 + 256*(80 + 256*83)); printf("AFTER: x.s = %s\n",x.s); BEFORE: x.s = hello } AFTER: x.s = OOPSo

  15. What Good Are unions? If memory is tight (e.g., small embedded devices), we can “recycle” memory by using union structures (e.g., once we are done using one variable, re-use that memory for something else). Also sometimes convenient for doing bitwise operations (e.g., easier to manipulate int than float ). But that flexibility opens up the possibility of mis-use, e.g., accidentally (or intentionally) performing an invalid operation on some data by manipulating a variable sharing the same memory. For this reason many say C is not strongly typed, by which they really mean, “not as strongly typed as other languages where this is prohibited.”

Recommend


More recommend