Subsections

Assignments in formulæ

Since an assignment yields a name, it can be used in a formula. However, the assignment must be converted into an enclosed clause (using parentheses or BEGIN and END) ensuring that the assignment is elaborated first. For example, in

   3*(a:=c+4)+2

if c refers to 3, the value of the formula will be 23 with mode INT, a will refer to 7, the value of the assignment is a name of mode REF INT and c will still refer to 3. Remember that assignment is not an operator.

Here is an example of two assignments in a conditional clause:

   IF a<2 THEN x:=3.2 ELSE x:=-5.0 FI

This can be written with greater efficiency as

   x:=IF a < 2 THEN 3.2 ELSE -5.0 FI

The left-hand side of an assignment has a soft context. In a soft context, dereferencing is not allowed (it is the only context in which dereferencing is not allowed). In the following phrase, the conditional clause on the left yields a name which is then assigned the value of the right-hand side:

   IF a < 2 THEN x ELSE y FI := 3.5

In the next assignment, a conditional clause appears on both sides of the assignment:

   (a<2|x|y):=(b<2|x|y)

The result depends on the values referred to by both a and b as much as on the values referred to by both x and y.


Exercises

5.4
What is wrong with the following program fragment?
   REF REAL x = LOC REAL,
            y = LOC REAL:=3.5;
   y:=4.2+x
Ans[*]
5.5
If x refers to 3.5 and y refers to -2.5, what is the mode and value yielded by the following phrases: Ans[*]
(a)
x:=-y

(b)
ABS y

5.6
What does x refer to after
   x:=1.5;  x PLUSAB 2.0 DIVAB 3.0
(try it in a small program). Ans[*]


Sian Mountbatten 2012-01-19