CSC302 2007S, Class 33: Types (3) Admin: * Presentation topics? * YA Stupid Meeting in Office Hours Overview: * The CLU Reading. * Cardelli and Wegner. * Type Equivalence. * Structural Equivalence, Revisited. * Assignment Compatibility. * Type Coercion. * Design Issues. /CLU - My Questions/ * What does the year in which the article was published (1977) tell you about the context in which the article was written? * DETOUR: INCREDIBLY IMPORTANT PARADIGM SHIFT - VISICALC * Home computers can be useful for more than hobbyists * END OF DETOUR * Non-Dominant languages: Pascal (mostly academic); LISP (mostly AI); Simula (mostly Norwegians); Smalltalk (mostly people who had access to languages under development) * Dominant languages: Fortran, COBOL, Assembly * Techniques: Imperative, ad hoc, goto-ridden * What is the thesis of the CLU reading? * Note Paradigm of program design: Abstraction and decomposition * Thesis: To successfully program good systems, one should use a "decomposition model" that is supported by abstraction. * Decomposition: Top down * Abstraction: Separate implementation from interface * Note: Imperative model, no OO model * What seem to be the more interesting type (or non-type) features of CLU? * Information hiding as a type structuring technique ; abstraction * Exception handling (not described much in this paper) * Not really OO - no inheritance etc. * Something about "parametric polymorphism" * What is parametric polymorphism?- A single operator works uniformly across a range of types * Write the code once, use it on many different kinds of values * What Sam just calls "polymorphism" in 152 * Prototypical parametric polymorphic procedure example: length * Prototypical parametric polymorphic type example: list-of[A] is either nil or cons(A,list-of[A]) * Sam thinks CLU was either the first or one of the first languages to suppot parameterized types * A union type - oneof * Control abstractions - the iterator - Wraps up a collection so that you can traverse the collection, whether it is an array, a list, a tree, a graph, a ... - Strategy is similar to continuations * Are there type features that you are surprised have not been adopted (or adopted as quickly) in more modern languages? * parameterized types /CLU - Your Questions/ * On pg565, fourth paragraph of second column, Liskov et al say that "the behavior of the data objects must be completely characterized by the set of operations." On pg570, first paragraph of first column, they say that "CLU objects exist independently of procedure activations." My question: The existence of (data) objects seems to be useless without procedure activations since the objects can be manipulated only through the operations provided with them. Hence, should the existence of the objects be dependent on procedure activation? Could you also clarify what they mean by "procedure activation"? * Procedure activiation: What is stored on the stack while a procedure is executing - local variables, etc. * Traditionally contains: Local variables, temporaries, return address (where to jump when done)a * CLU idea: When the procedure exits, values it builds can still exist * In C, you do this with pointers * A more general strategy, and makes memory managment the system's responsibility rather than the programmer's. * One thing in the paper is not clear to me. That is put_n(r,x) method, which the paper says "makes x the n component of the record r (568)". r.n = x; I understand this as that this method can update a component of an object during running time. We have seen the similar thing in Ruby (we can update an object's method). However, CLU does type checking in compiling time, which sounds contradictory to what I understand. For example, if a pr ogram declares an integer as the type of a component and pass the type checking in com piling time, then during running time, it invokes the put_n(r,x) to change the type to float. What will happen then? * Rephrased We've declared n as a field of type integer. In Ruby, we can still say r.n = 3.4 What happens when we say r.n := 3.4 in CLU? * Not permitted, caught by the compiler. * The authors said "Thus variables are completely private to the procedure in which they are declared and cannot be accessed or modified by any other procedure". Is that a design choice to limit mistakes of the programmer or was that done for a different reason? /Cardelli and Wegner - Basic Concepts/ * What is the difference between untyped, staticly typed, dynamically, and strongly typed? * What are the relationships between parameteric polymorphism, inclusion polymorphism, ad-hoc polymorphism, and coercion? * What are the relationships between typing and polymorphism? /Cardelli and Wegner - Your Questions/ * Cardelli and Wegner's seem to pretty clearly favor strongly typed languages, and they present monomorphism vs. polymorphism as the only two options for a language. In Automata, the way we look at recursion theory and Turing machines states that anything can be encoded as a string or a number (whichever one we want to work with), including the machines themselves. This sort of encourages polymorphism not by having subtypes, but by making every thing the same type and making it not matter how we treat the data. This approach doesn't seem to jibe with some of the ideas in this article. I realize this is sort of apples and oranges, but I'm curious how it can tie back into what we've read here. * After discussing "Sets" in the previous class, we know that Sets in set theory is a type. The author tells us that "Untyped" means that there is only one type (pg. 472) and that monomorphic also means that there is only one type(pg 475). I am not sure I understand the difference between these terms. * Cardelli and Wegner claim that Static typing is a useful property, but the requirement that all variables and expressions are bound to a type at compile time is sometimes too restrictive. Can you give an example of static typing being too restrictive? * Java seems to use mostly ad-hoc polymorphism (overloading, coercion) and some inclusive polymorphism through inheritance. Is that really the case? * What's the difference between class parametrization and polymorphism? Is there a formal definition for parametrization? * What method of OO code reuse (Polymorphism or inheritance) is preferred? /Type Equivalence/ type xy = record x: integer; y: real; end; xy_alias = xy; ab = record a: integer; b: real; end; yx = record y: real; x: integer; end; abc = record a: integer; b: real; c: real; end; intrec = record x: integer; end; iarr = array[1..10] of integer; iare = array[2..11] of integer; var rec1: xy; rec2: xy_alias; rec3: ab; rec4: abc; rec5,rec6: record a: integer; b: real; end; rec7: record a: integer; b: real; end; rec8: yx; rec9: intrec; rec10: xy; arr1: iarr; arr2: iare; arr3,arr4: array[1..10] of integer; arr5: array[1..10] of integer;