Keywords: pame

Subsections

- Copying values
- Assigning operators
- Exercises

# Assignment

The process of causing a name to refer to a value is called assignment. Using the identifier declared above, we can write

       a:=3

We say “a assign 3”. Note that the mode of the name identified by `a` is `REF INT`, and the mode of the denotation `3` is `INT`. After the assignment, the name identified by `a` refers to the value denoted by `3`.

Suppose now we want the name identified by `a` to refer to the value denoted by `4` (this may seem pedantic, but as you will see below, it is necessary to distinguish between the denotation of a value and that value itself). We write

       a:=4

Let us juxtapose these two assignments:

       a:=3
         ;
       a:=4

If you look carefully at the two assignments, a number of things spring to mind. Firstly, an assignment consists of three parts: on the left-hand side is an identifier of a name, in the middle is the assignment token, and on the right-hand side is a denotation. Secondly, the left-hand side of the two assignments is the same identifier: `a`. Since the identifier is the same, the value must be the same.6.2 That is, in the two assignments, `a` is synonymous with a value which does not change. The value is a name and has the mode `REF INT` (in this case). Thus the value of the left-hand side of an assignment is a name.

Thirdly, the values on the right-hand side of the two assignments differ. Firstly, `a` is assigned the value denoted by `3`, then (after the go-on symbol), `a` is assigned the value denoted by `4`.

After the second assignment, `a` refers to `4`. Of course, when we say “`a` refers to”, we mean “the name identified by `a` refers to”. What has happened to the value `3`? To understand this, we need to look a little more closely at what we mean by the value `3`. The denotation `3` represents the number three. Now, of course, the number three exists independently of a computer program. When the digit `3` is elaborated in an Algol 68 program, an instance of the number three is created. Likewise, elaborating the digit `4` creates an instance of the number four. When `a` is assigned an instance of the value four, the instance of the value three disappears. This property of assignment is very important. Because an assignment causes data to disappear, it is dangerous to use. You have to be careful that the data which disappears is not data you wanted to keep. So the instance of a value can disappear, but the value still exists (like the number three).

It is worth reiterating that however many times a name is assigned a value, the value of the name remains unchanged. It is the value referred to which is superseded. Outwith the realm of computers, if an individual is assigned to a department of an organisation, clearly the department hasn't changed. Only its members have changed.

When an identifier for a name has been declared, the name can be made to refer to a value immediately after the declaration. For example

       REF REAL x = LOC REAL := pi

where `pi` is the value declared in the standard prelude. `LOC REAL` generates a name of mode `REF REAL`.

The right-hand side of an assignment is a strong context so widening is allowed. Thus we can write

       x:=3

where the `3` is widened to `3.0` before being assigned to `x`. In reality, the value denoted by `3` is not changed to the value denoted by `3.0`: it is replaced by the new value. There is an important principle here. It is called the “principle of value integrity”: once an instance of a value has been created, it does not change until such time as it disappears. Thus, in Algol 68, every value is a constant. Every coercion defined in Algol 68 replaces a value of one mode with a related value of another mode.

## Copying values

Here is another identity declaration with an initial assignment:

       REF INT c = LOC INT := 5

Using the identifier `a` declared earlier, we can write

       a:=c

and say “a assign c”. The name on the left-hand side of the assignment has mode `REF INT`, so a value which has mode `INT` is required on the right-hand side, but what has been provided is a name with mode `REF INT`. Fortunately, there is a coercion which replaces a name with the value to which it refers. It is called dereferencing and is allowed in a strong context. In the above assignment, the name identified by `c` is dereferenced yielding an instance of the value five which is a copy of the instance referred to by `c`. That new instance is assigned to `a`. It is important to remember that the process of dereferencing yields a new instance of a value.

Try the following program:

       PROGRAM assign CONTEXT VOID
       USE standard
       BEGIN
          REF INT a = LOC INT,
                  b = LOC INT:=7;
          print(("b=",b,newline));
          print("Please key 123G:"); read(b);
          a:=b;
          print(("a now refers to",a,newline,
                 "b now refers to",b,newline))
       END
       FINISH

This should convince you that dereferencing involves copying.

Every construct in Algol 68 has a value except an identity declaration. We said above that the value of the left-hand side of an assignment is a name. In fact, the value of the whole of the assignment is the value of the left-hand side. Because this is a name, it can be used on the right-hand side of another assignment. For example:

       a:=b:=c

You should note that an assignment is not an operator. The assignments are performed from right to left: firstly, `c` is dereferenced and the resulting value assigned to `b`. Then `b` is dereferenced and the resulting value is assigned to `a`.

## Assigning operators

The following assignment

       a:=a

does not do anything useful, but serves to remind us that the name identified by `a` on the right-hand side of the assignment is dereferenced, and the resulting value is assigned to `a`. However, `a` now refers to a new instance of the value it previously referred to and the previous instance has now disappeared.

Now consider the phrases

       c:=5; a:=c+1

The right-hand side of the second assignment is now a formula. The name identified by `c` is now in a firm context (it is the left-operand of the `+` operator). Fortunately, dereferencing is also allowed in a firm context. Thus the value of `c` (a name with mode `REF INT`) is replaced in the formula by a copy of the value to which it refers (`5`), which is added to `1`, and `a` is assigned the new value (`6`). We say “a is assigned c plus one”.

What about the phrase

       a:=a+1

In exactly the same way as the previous phrase, the name on the right-hand side is dereferenced, the new value created is added to `1`, and then the same name is assigned the new value.

One of the features of assignment is that the elaboration of the two sides is performed collaterally. This means that the order of elaboration is undefined. This does not matter in the last example because the value of the name identified by `a` is the same on the two sides of the assignment. Remember that the value of `a` is a name with mode `REF INT`. It is the value to which `a` referred which was superceded.

Assignments of this kind are so common that a special operator has been devised to perform them. The above assignment can be written

       a+:=1

and is read “a plus-and-assign one”. The operator has the alternative representation `PLUSAB`.6.3 Note that the left-hand operand must be a name. The right-hand operand must be any unit which yields a value of the appropriate mode in a firm context.

The operator `+:=` is defined for a left-operand of mode `REF` `INT` or `REF` `REAL`, and a right-operand of mode `INT` or `REAL` respectively. The yield of the operator is the value of the left-operand (the name). If the left-operand has mode `REF REAL`, the right-operand can also have mode `INT`. No widening occurs in this case, the operator having been declared for operands having these modes. Because the operator yields a name, that name can be used as the operand for another assigning operator. For example

       x +:= 3.0 *:= 4.0

which results in `x` referring to `4.0*(x+3.0)`. The formula is elaborated in left-to-right order because the operators have the same priority. The operators are more efficient than writing out the assignments in full.

There are four other operators like `+:=`. They are `-:=`, `*:=`, `/:=`, `%:=` and `%*:=`. Their alternative representations are respectively `MINUSAB`, `TIMESAB`, `DIVAB`, `OVERAB` and `MODAB`. The operators `OVERAB` and `MODAB` are only declared for operands with modes `REF` `INT` and `INT`. The priority of all the operators is 1.

The assignment operators are operators, not assignments (although they perform an assignment), so that the previous example is not an assignment, but a formula.

The right-hand side of an assignment can be any unit which yields a value whose mode has one less `REF` than the mode of the name on the left-hand side. Names whose mode contains more than one `REF` will be considered in chapter 11.

## Exercises

5.1
  The following identity declarations

         REF CHAR s = LOC CHAR,
         REF INT  i = LOC INT,
         REF REAL r = LOC REAL

  hold in this and the following exercises.6.4 What is the mode of `i`? Ans
5.2
  After the assignment `r:=-2.7` has been elaborated, what is the mode of the value referred to by `r`? Ans
5.3
  What is wrong with the assignment `i:=r` and how would you correct it? Ans