Subsections

- Exercises

# United modes in procedures

We can now partly address the problem of the parameters for `print` and `read`. If we extend the answer to the last exercise, we should be able to construct a united mode which will accept all the modes accepted by those two procedures. In fact, the united modes used are almost the same as the two following declarations:

       MODE SIMPLOUT = UNION(CHAR, []CHAR,
                             INT,  []INT,
                             REAL, []REAL,
                             COMPL,[]COMPL,
                             BOOL, []BOOL,
                            ),

            SIMPLIN  = UNION(REF CHAR, REF[]CHAR,
                             REF INT,  REF[]INT,
                             REF REAL, REF[]REAL,
                             REF COMPL,REF[]COMPL,
                             REF BOOL, REF[]BOOL,
                            );

As you can see, the mode `SIMPLIN` used for `read` is united from modes of names.

The modes `SIMPLOUT` and `SIMPLIN` are a little more complicated than this because they include modes we have not yet met (see chapters 9 and 11), but you now have the basic idea.

The uniting coercion is available in a firm context. This means that operators which accept operands with united modes will also accept operands whose modes are any of the constituent modes. We shall return to this in the next section.

Here is an example of the uniting coercion in a call of the procedure `print`. If `a` has mode `REF INT`, `b` has mode `[]CHAR` and `c` has mode `PROC REAL`, then the call

       print((a,b,c))

causes the following to happen:

1. `a` is dereferenced to mode `INT` and then united to mode `SIMPLOUT`.
2. `b` is united to mode `SIMPLOUT`.
3. `c` is deprocedured to produce a value of mode `REAL` and then united to mode `SIMPLOUT`.
4. The three elements are regarded as a row-display for a `[]SIMPLOUT`.
5. `print` is called with its single parameter.

`print` uses a conformity clause (see next section) to extract the actual value from each element in the row.

In section 6.3.2, we gave the declaration of a procedure identified as `char in string`. The header of that procedure was

       PROC char in string=
          (CHAR ch,REF INT pos,[]CHAR s)BOOL:

The procedure yielded `TRUE` if `ch` was present in `s`, in which case `pos` contained the position. Otherwise, the procedure yielded `FALSE`. The same procedure could be written to yield the position of `ch` in `s` if it is present, and `VOID` if not:

       PROC ucis = (CHAR ch,[]CHAR s)
                        UNION(INT,VOID):

The body of the procedure has been left as an exercise.

## Exercises

8.5
  A procedure has the header

         PROC pu = ([]UNION(CHAR,[]CHAR) up)VOID:

  Explain what happens to the parameters if it is called by the phrase

         pu((CHAR: REPR(ABS"a"+1),LOC[4]CHAR))

  Ans
8.6
  Write the body of the procedure `ucis` given in the text. Ans