Se connecter

Se connecter avec OpenID

Abstract Types Defined as Classes of Variables

Abstract Types
Defined as Classes of
Jeffrey Smith, Vincent Fumo,
Richard Bruno
What is a type
Current approaches
Why a new definition
The new approach
Common Types
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 Basic
Dim 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
Example from C:
Boolean flag;
void setTrue() {
flag = 1;
void setFalse() {
flag = 0;
Here the type is shown in terms of its
primitive types. The primitives are
usually hardware or compiler
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 {
void setTime(int, int, int);
void PrintStandard();
int hour;
int minutes;
int second;
Why Don’t These Work?
The previously outlined approaches
don’t define extensible language types
Can’t create clear and simple semantic
 Can’t achieve practical goals (strong
compiler type checking)
Why Type Extension?
Type extension needs to support the
following four goals:
 Redundancy and compile time checking
 Abbreviation
 Data Portability
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;
User-defined data types make for
shorter programs that are easier to
understand, modify, write, etc.
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 userdefined 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
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.
Type 1
Type 2
Int x;
Int a;
Char y;
Char b;
Void Function1(int x);
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 Types
2 Modes
3 Variables
These are types that are defined by the
characteristics observed using the
Their internal representation does not
matter, so long as they meet the
Spec-Type Example
Spec-Type A
Spec-Type B
Double x
Int x
Double y
Int y
Int Op1(); //Add values
Int Op1(); //Add values
Void Op2(); //Multiply 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.
These types are defined by their internal
Those types with identical internal
representations are of the same reptype 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 realpart;
double y
double complexpart;
Compute () {
Compute () {
(x*y)*x); }
(x*y)*x; }
Type A and Type B are rep-types because both types have
the same internal representation.
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 {
My_type* array;
int Max, Begin, End, OverWriteFlag, LastItemFlag
void IntArray();
void CleanupArray();
QUEUE(int num);
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
A weaker form of Spec-Types
Variant-Types Example
Type A
Type B
Double x
Int x
Double y
Int y
Void Op1(); //Add values
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
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
He even calls them Classes!
Code Sharing?
In order to share code the compiled
code must be able to anticipate all spectypes that can occur.
Example – You must consider things
like big-endian and little-endian (ie.
Whether least or most significant bits
are stored first).
The bottom line…..
Go Invent C++
Без категории
Taille du fichier
177 Кб