Subsections

United mode declarations

UNION is used to create a united mode. Here is a declaration for a simple united mode:

   UNION(INT,STRING) u = (random < .5|4|"abc")

The first thing to notice is that, unlike structures, there are no field selectors. This is because a united mode does not consist of constituent parts. The second thing to notice is that the above mode could well have been written

   UNION(STRING,INT) u = (random < .5|4|"abc")

The order of the modes in the union is irrelevant.9.1 What is important is the actual modes present in the union. Moreover, a constituent mode can be repeated, as in

   UNION(STRING,INT,STRING,INT) u =
      (random < .5|4|"abc")

This is equivalent to the previous declarations.9.2

Like structured modes, united modes are often declared with the mode declaration. Here is a suitable declaration of a united mode containing the constituent modes STRING and INT:

   MODE STRINT = UNION(STRING,INT)

We could create another mode STRINTR in two ways:

   MODE STRINTR = UNION(STRINT,REAL)

or

   MODE STRINTR = UNION(STRING,INT,REAL)

Using the above declaration for STRINT, we could declare u by

   STRINT u = (random < .5|4|"abc")

In this identity declaration, the mode yielded by the right-hand side is either INT or STRING, but the mode required is UNION(STRING,INT). The value on the right-hand side is coerced to the required mode by uniting.

The united mode STRINT is a mode whose values either have mode INT or mode STRING. It was stated in chapter 1 that the number of values in the set of values defined by a mode can be zero. Any value of a united mode actually has a mode which is one of the constituent modes of the union. So there are no new values for a united mode. u identifies a value which is either an INT or a STRING. Because random yields a pseudo-random number, it is not possible to determine when the program is compiled (that is, at compile-time) which mode the conditional clause yields. As a result, all we can say is that the underlying mode of u is either INT or STRING. We shall see later how to determine that underlying mode.9.3

Because a united mode does not introduce new values, there are no denotations for united modes, although denotations may well exist for the constituent modes.

Almost any mode can be a constituent of a united mode. For example, here is a united mode containing a procedure mode and VOID:

   MODE PROID = UNION(PROC(REAL)REAL,VOID)

and here is a declaration using it:

   PROID pd = EMPTY

The only limitation on united modes is that none of the constituent modes may be firmly related (see the section 6.2.1) and a united mode cannot appear in its own declaration. The following declaration is wrong because a value of one of the constituent modes can be deprocedured in a firm context to yield a value of the united mode:

   MODE WRONG = UNION(PROC WRONG,INT)

Names for values with united modes are declared in exactly the same way as before. Here is a declaration for such a name using a local generator:

   REF UNION(BOOL,INT) un = LOC UNION(BOOL,INT);

The abbreviated declaration gives

   UNION(BOOL,INT) un;

Likewise, we could declare a name for the mode STRINT:

   STRINT sn;

In other words, objects of united modes can be declared in the same way as other objects.


Exercises

8.1
Write a mode declaration for the united mode BINT whose constituent modes are BOOL and INT. Ans[*]
8.2
Write an identity declaration for a value of mode BINT. Ans[*]
8.3
What is wrong with the mode declaration
   MODE UB = UNION(REF UB,INT,BOOL)
Ans[*]
8.4
Declare a name for a mode united from INT, []INT and [,]INT. Ans[*]


Sian Mountbatten 2012-01-19