Subsections

- Exercises

# References to names

The idea that a mode can contain more than one `REF`, or that a mode might be `REF[]REF[]CHAR` was broached at the start of chapter 5 and mentioned in section 10.3.2. The time has now come to address this topic fully.

Any mode which starts with `REF` is the mode of a name. The value to which a name refers has a mode with one `REF` less. Since names are values in their own right, there is no reason at all why a name should not refer to a name. For example, suppose we declare

       INT x,y

then the mode of both `x` and `y` is `REF INT`. We could also declare

       REF INT xx, yy

so that `xx` and `yy` both have the mode `REF REF INT`.

Now, according to the definition of an assignment (see section 10.8), it is perfectly legitimate to write

       xx:=x

without any dereferencing because the identifier on the left has mode `REF REF INT` and the identifier on the right has mode `REF INT`. Leaving aside for the moment of how useful such declarations and assignments might be (and they are very useful, essential even), let us give our attention to the mechanics. We could assign `y` to `xx` and a value to `y` with the double assignment

       xx:=y:=3

Again, no dereferencing is involved. Now, given that `xx` refers to `y` which refers to `3`, how could we make `y` refer to `4`, say? Simple. Assign `4` directly to `y`. However, if the assignment to `xx` was

       xx:=(random>0.8|x|y)

we should not know which name `xx` referred to. Finding out which name `xx` refers to is the subject of the next section.

You may remember that the context of the left-hand side of an assignment is soft so no dereferencing is allowed. The way to coerce a name of mode `REF REF INT` to a name of mode `REF INT` is to use a cast:

       REF INT(xx):=4

Note that the unit

       print(xx)

will yield `4` with `xx` being dereferenced twice. There is nothing to stop us writing

       REF REF INT xxx:=xx

with assignments like

       REF REF INT(xxx):=x
       REF INT(xxx):=-2

and we shall see in a later section that names with modes `REF REF REF some-mode` have a use. Although you can use as many `REF`s as you like, there does not seem to be any need for more than three.

Now consider the assignments

       xx:=yy:=x:=4

Both `xx` and `yy` refer to different instances of the name `x`, but when those instances are dereferenced, they both yield `4`. This means that if we assign `5` to `x`, when `xx` and `yy` are dereferenced twice, they will both yield `5`. We can represent this relationship by the diagram

where `RRI` and `RI` stand for `REF REF INT` and `REF INT` respectively. Thus, although strictly speaking `xx` and `yy` refer to different instances of the name identified by `x`, we shall regard them as both referring to `x`.

## Exercises

11.7
  Given the declaration

         REF REAL xx:=LOC REAL

  how would you make the anonymous name refer to `120.5`? Ans
11.8
  Write a declaration for `rrq` which has the mode `REF` `REF` `REF[]CHAR` and make it refer to an anonymous name which refers to an anonymous name which refers to a multiple of 10 characters. Ans
11.9
  Write the declaration of a name which can refer to a flexible name which can refer to a row of integers. In a separate assignment, assign the row-display
  `(3,-2,4)` to your name. Ans