Contents Up << >>

Array types

All the types discussed thus far are called scalar types because an object of that type has one value. An array type is a composite type. Objects of a composite type have an organized collection of values. The array type is a collection of values of the same type. The organization of the collection is such that every value has a position and that value can be referenced by its position. The idea of a position conceptually comes from the idea of spatial positions. For example, suppose I had the following values
The way they are written gives them a spatial position.  The 7 is left of
the 3, which is left of the 5, and so on.  These can be easily given a position
by indicating the position of the first and the last.  Let's say the 4 is at
position five and the 2 is at position nine, then the number at position seven
must be 3.  This is how an array definition is given.  The position in
an array of values is called an {\it index} and the range the index can have
(i.e., the position of the first and last values) is called the {\it index 
range} given by an {\it index constraint}.

An example of an array definition is
\begin{verbatim}
type memory is array (range 0 to 65535) of integer;
The array type is the ideal representation of a memory circuit because memory is a collection of values that are referenced by position. The type definition gives the name of the type, indicates it is an array of integer values and specifies that there are 65536 values numbered from 0 to 65535. This array type is called a one-dimensional array, since conceptually it could be thought of as 65526 values placed in particular positions on a line.

A type definition can also use positions with greater than one dimension. For example, the truth table of the xor function of two variables can be thought of as a two-dimensional array of 0 and 1 values. The index of a two-dimensional array has two values, one for the position in one direction and the other for the position in the other direction. We might write the two dimensional array of the xor function as 0 1
1
0
If the positions are numbered left to right from 0 to 1 and top to bottom from 0 to 1, then the value at any index position is equivalent to the result of applying the xor function to the two values in the index. An index is indicated as a list of values separated by commas and enclosed by parentheses. The valid indices for the xor array and the values at those positions are (0,0) (0,1) (1,0) (1,1)
= 0
= 1
= 1
= 0
which looks much like a truth table! Arrays are a natural way to represent functions of variables that only have a small number of values (like bit functions). For a function of N variables an N-dimensional array can be used with each variable represented by one value of the index. Any function of two bits could be represented with an object of the following array type

The grammar rule for an array definition is: There are two aspects of this rule that have not been discussed yet, the unconstrained array type and using a subtype for a range. When a subtype is indicated for a range then the range of that subtype will be the range of the index.

An unconstrained array type is an array type that has an unspecified range of values in its index. An object can not be declared an unconstrained array type because there is no way to know its size. However, the unconstrained array type can be used to declare subtypes that constrain the array by specifying the ranges of its index. Bit_vector and string are built-in unconstrained array types. Their definition is Vectors of different lengths can be declared by using a subtype to constrain the bit_vector type. The section on subtypes discusses why it is useful to use a subtype for this purpose instead of creating different array types with different lengths.