1 / 31

Abstract Types Defined as Classes of Variables

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?.

dash
Download Presentation

Abstract Types Defined as Classes of Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Abstract Types Defined as Classes of Variables Jeffrey Smith, Vincent Fumo, Richard Bruno

  2. Introduction • What is a type • Current approaches • Why a new definition • The new approach • Common Types • Questions

  3. 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.

  4. Syntactic Approach • This approach provides the typing information in the variable declaration. • Example – Visual BasicDim Index as Integer • Example – C++Int index,counter;

  5. 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.

  6. 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; }

  7. 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; };

  8. 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;};

  9. 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)

  10. Why Type Extension? • Type extension needs to support the following four goals: • Abstraction • Redundancy and compile time checking • Abbreviation • Data Portability

  11. 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

  12. 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;}

  13. 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);

  14. 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.

  15. 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.

  16. 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).

  17. 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.

  18. 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.

  19. 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

  20. 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.

  21. 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.

  22. 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.

  23. 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.

  24. 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.

  25. 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.

  26. Variant Types • These are types that have common properties but are not exact specifications. • A weaker form of Spec-Types

  27. 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.

  28. 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!

  29. 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).

  30. The bottom line…..

  31. Go Invent C++

More Related