310 likes | 481 Views
Abstract Types Defined as Classes of Variables. Jeffrey Smith, Vincent Fumo, Richard Bruno. Introduction. What is a type Current approaches Why a new definition The new approach Common Types Questions. What is a Type?.
E N D
Abstract Types Defined as Classes of Variables Jeffrey Smith, Vincent Fumo, Richard Bruno
Introduction • What is a type • Current approaches • Why a new definition • The new approach • Common Types • Questions
What is a Type? • Primitive types implicitly defined by types supported by the language used. • User-defined types allow user to add new types without altering the compiler thereby removing the implicit definition of type. • Formal definition of type necessary given the proliferation of user-defined types.
Syntactic Approach • This approach provides the typing information in the variable declaration. • Example – Visual BasicDim Index as Integer • Example – C++Int index,counter;
Value Space Approach • This approach uses the idea of enumeration. Here is a list of possible values that implicitly define the type. • Example – Type will hold Boolean information thus it will contain T or F, 1 or 0, etc.
Behavior Approach • This builds on value space by adding the notion of a set of operations to the space. • Example from C:Boolean flag; void setTrue() { flag = 1; } void setFalse() { flag = 0; }
Representation Approach • Here the type is shown in terms of its primitive types. The primitives are usually hardware or compiler implemented. • Example – C style structure:struct address { char *street; char *street2; char *city; char *state; double zip; };
Representation and Behavior Approach • Type defined by a representation combined with a set of operator defining its behavior. • Example – A Class in C++Class Time {public: Time(); void setTime(int, int, int); void PrintStandard();private: int hour; int minutes; int second;};
Why Don’t These Work? • The previously outlined approaches don’t define extensible language types because: • Can’t create clear and simple semantic rules • Can’t achieve practical goals (strong compiler type checking)
Why Type Extension? • Type extension needs to support the following four goals: • Abstraction • Redundancy and compile time checking • Abbreviation • Data Portability
Abstraction • We abstract to generalize problems in hopes of solving many problems at once (in terms of the abstraction). • User-defined data types are usually abstractions of more primitive structures and data types. • Abstraction lends itself well to: • Structured programming • Stepwise refinement • Information hiding
Redundancy / Compile Time Checking • User-defined type provides more information about data stored than primitive types do. • Restricting the possible set of operations on the data. • Example from C:void main (void){ int a; char b ='a'; char c ='b'; a = b + c;}
Abbreviation • User-defined data types make for shorter programs that are easier to understand, modify, write, etc. • Example: • desired:Function do_something(recordset); • Undesired:Function do_something(int1, int2, char, string, int, char);
Data Portability • User-defined data types reduce the changes necessary when new data or data organization is introduced. • The abstraction present in the user-defined data type aids in making your design language independent. • Example – XML – Abstract the data into XML. Describe the data in XSL.
Goals Not Realized? • Current languages (1975) have not reached the stated goals. • Five “original” definitions (syntactic, behavior, etc.) of types are too restrictive to allow the new goals (abstraction, abbreviation, etc.) outlined to be achieved.
Mode of a Variable • A mode is a group of variables that are represented and accessed in the same manner. • Defines an equivalence class on variables. (ie. Any value that can be stored in a particular mode can be stored in any mode of that type).
Classes of Modes • Two modes are in the same class (of the same type) if they can be substituted without generating a compile-time error. • Classes of modes are Types. • Types which contain more than one mode are abstract data types.
Example Type 1 Type 2 Int x; Char y; Void Function1(int x); Int a; Char b; Void Function1(int a); Here Type 1 and Type 2 are in the same class because their operations and variables are the same. In other words, they are of the same mode.
Hierarchical Structure? • Types are classes of modes. • Modes are classes of variables. • A hierarchical relationship exists with the relation being “are classes of” 1 2 3 Types Modes Variables
Spec-Types • These are types that are defined by the characteristics observed using the operators. • Their internal representation does not matter, so long as they meet the specification.
Spec-Type Example Spec-Type A Spec-Type B Double x Double y Int Op1(); //Add values Void Op2(); //Multiply values Int x Int y Int Op1(); //Add values Void Op2(); //Multiply values Type A and Type B are spec-types because you can put the same inputs into either one and you will get the same result…..thus they meet the specification. Note: Identically named functions implies identical operations.
Rep-Type • These types are defined by their internal representations. • Those types with identical internal representations are of the same rep-type but may not have the same characteristics when the operators are applied to the representation.
Rep-Type Example Type Cartesian Type Complex double x double y Compute () { (x*y)*x); } double realpart; double complexpart; Compute () { (x*y)*x; } Type A and Type B are rep-types because both types have the same internal representation.
Param-Type • The operations and variables are the same but underlying representations between two types are not the same. • C++ Templates or Java Interfaces easily demonstrate this concept.
Param-Type Example Template <class my_type> Class QUEUE {Public: enum{MAXENTRIES=128};protected: My_type* array; int Max, Begin, End, OverWriteFlag, LastItemFlagvoid IntArray();void CleanupArray();public: QUEUE(); QUEUE(int num); ~QUEUE(); void Push(my_type* obj);my_type* Pop(void);void Clear (void); }; This class will work for any type fed into the function. Without the use of the template, you would have to create a separate class for each type Queue we need.
Variant Types • These are types that have common properties but are not exact specifications. • A weaker form of Spec-Types
Variant-Types Example Type A Type B Double x Double y Void Op1(); //Add values Int x Int y Void Op1(); //Add values Int Op2(); //Multiply values Type B and type A are variant-types since type B shares Op1 with type A. Type B however, adds a second operation Op2.
More About Types The types listed (spec, rep, param and variant types) are not an exhaustive set of types, just the most common ones. The authors essentially describe a C++ class – classes contain data representation and operations. He even calls them Classes!
Code Sharing? • In order to share code the compiled code must be able to anticipate all spec-types that can occur. • Example – You must consider things like big-endian and little-endian (ie. Whether least or most significant bits are stored first).