Thursday, September 2, 2010

Structured Data Types

Most imperative languages provide some support for structured types. Users may be able to define their own types, and this can create more meaningful programs. Various types can be combined to create aggregate types, composed of elements of other types, such as arrays and records.
Most programming Languages have at least one built-in type, although there are typeless languages, such as APL and MUMPS, where data objects can be coerced automatically from one type to another. Even here, the programmer is thinking and the program operating on some sort of structured type.

Use-Defined Types

When a type consists of discrete values that have a unique and successor, it is referred to as an ordinal type or (in Ada) discrete type. This includes character, Boolean, and integer types. The real types si generally excluded-though there is an order, it is not composed of discrete values. Many languages permit the programmer to define new ordinal types, either by defening subranges of those previously defined or by enumeration.

Subrange Types

A subrange type is used to constrain the values of some parent type to be within a specified range. The parent types is limited to ordinal type in Pascal, while Ada permits subranges of fixed and floating point types. Since the operations are those defined on the base and subrange types.

   type
    monthRange = integer 1 .. 12;
    dayRange  = integer 1 .. 31;
  var                                                  (1..1)
    month: monthRange;
    today, day: dayRange;

Subrange types are commonly used to make the code more readble. In the pseudocade in listing (1..1), the listed type name implies the use of variables of that type. While month and day could simply be of integer type, the subrange clarifies the intended use. If hour is another type, which happens to specify the range 1..12, should assignments be permitted between the two tyes? This is an issue of type equivalence, which is discussed later in this section.
Ad added benefit of subrange types is the assistance available in error chechking. If a variable is assigned a value outside the spesified range during run time, a constraint error can help the programmer find the problem. Since this constant checking can mean longer run times, some compilers may offer a switch that turns range checking on or off (and may even have range checking off by default). It might be switched of after some premilinary debugging is competed, provided one is willing to risk errors in order to improve execution times.

Enumeration Types

In enumeration types one lists all the values that can be taken by that type. Consider the pseudocode exam in listing (1..2).

  types                                          (1..2)                                                                          
     months = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
  var
     month: months;

The values are called  enumeration literals, shown here as identifiers. They cannot also be used for variable names. In many languages, the Boolean type is essentially a predefined enumeration type: boolean = (false, true);. Ada also permits characters to be used as character literals, hence the character type in Ada is also considered a prefined enumeration type.
  The listing of the enumeration literals provides an ordering of the discrete values, hence they are also ordinal types. The code can include comparisons, such as if month <= Jun then, or loop constructs like for Month := Jan to Dec do. In order to step through the values, pred and succ functions return the predecessor or successor in the list, though an attempt to find succ (Dec) should cause an error condition.

The language design question that arises is that of repeated use of the same enumeration literals. While not permitted in Pascal or C, this is important in Ada, since the character types fall into this category. A pseudocode declaration such as:
      type vowels = ('a','e','i','o','u');
includes the same character literals as those in the predefined character type. Hence Ada makes provisions for this overloading10 of literals. The user-defined enumeration types may well not be supported by the input/output routines. An attempt to print (month) could cause an error unless the language makes special provision for output of this type.
When programming in languages without enumeration types, it is common practice to simply use integers. If we define the identifiers Jan=1, Feb=2, etc., and month is of integer type, the month:= Jan makes sense, as does for month := Jan to Dec do.

Aggregate Types


FORTRAN II had five simple data types: integer, real, double-precison real, complex, and logical. The single aggregate type was the array. Character strings were facilitated through a crippled Hollerith11  type, which was really relegated to the integers. There were no other types, so users kept the "real meaning" of the data in their heads or described it through numerous comment lines.
Most newer languages (including FORTRAN 90) permit a number of aggregate type, formed from components of other types. These commonly include strings, arrays records, and possibly others. These enable the user to combine various components in ways that make the structures more meaningful.

No comments:

Post a Comment