Subsections

- Exercises

# Structure denotations

In chapter 3, we saw how a number of individual values can be collected together to form a multiple whose mode was expressed as “row of” the base mode. The principal characteristic of multiples is that all the elements have the same mode. A structure is another way of grouping data elements, but in this case, the individual parts can be, but need not be, of different modes. In general, accessing the elements of a multiple is determined at run-time by the elaboration of a slice. In a structure, access to the individual parts, called fields, are determined at compile time. Structures are, therefore, an efficient means of grouping data elements.

The mode constructor `STRUCT` is used to create structure modes. Here is a simple identity declaration of a structure:

       STRUCT(INT i,CHAR c) s = (2,"e")

The mode of the structure is

       STRUCT(INT i,CHAR c)

and its identifier is `s`. The `i` and the `c` are called field selectors and are part of the mode. They are not identifiers, even though the rule for identifier construction applies to them, because they are not values in themselves. You cannot say that `i` has mode `INT` because `i` cannot stand by itself. We shall see in the next section how they are used.

The expression to the right of the equals symbol is called a structure-display. Like row-displays, structure-displays can only appear in a strong context. In a strong context, the compiler can determine which mode is required and so it can tell whether a row-display or a structure-display has been provided. We could now declare another such structure:

       STRUCT(INT i,CHAR c) t = s

and `t` would have the same value as `s`.

Here is a structure declaration

       STRUCT(INT j,CHAR c) ss = (2,"e")

which looks almost exactly like the first structure declaration above, except that the field selector `i` has been replaced with `j`. The structure `ss` has a different mode from `s` because not only must the constituent modes be the same, but the field selectors must also be identical.

Structure names can be declared:

       REF STRUCT(INT i,CHAR c) sn =
                 LOC STRUCT(INT i,CHAR c)

Because the field selectors are part of the mode, they appear on both sides of the declaration. The abbreviated form is

       STRUCT(INT i,CHAR c) sn

We could then write

       sn:=s

in the usual way.

The modes of the fields can be any mode. For example, we can declare

       STRUCT(REAL x,REAL y,REAL z) vector

which can be abbreviated to

       STRUCT(REAL x,y,z)vector

and here is a possible assignment:

       vector:=(1.3,-4,5.6e10)

where the value `-4` would be widened to `-4.0`.

Here is a structure with a procedure field:

       STRUCT(INT int,
              PROC(REAL)REAL p,
              CHAR char) method = (1,sin,"s")

Here is a name referring to such a structure:

       STRUCT(INT int,
              PROC(REAL)REAL p,
              CHAR char) m := method

A structure can even contain another structure:

       STRUCT(CHAR c,
              STRUCT(INT i,j)s) double=("c",(1,2))

In this case, the inner structure has the field selector `s` and its field selectors are `i` and `j`.

## Exercises

7.1
  Declare a structure containing three integer values with field selectors `i`, `j` and `k`. Ans
7.2
  Declare a name which can refer to a structure containing an integer, a real and a boolean using field selectors `i`, `r` and `b` respectively. Ans