Subsections

Identity relations

Consider the declarations of the last section:

   INT x,y; REF INT xx,yy

We had assigned a name to xx with the assignment

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

and we wished to ascertain whether xx referred to x or to y. Unfortunately, we cannot use the equals operator = for this purpose because its operands would be completely dereferenced and the underlying integers would be compared. Instead, we use an identity relation which is used exclusively for comparing names. The identity relation

   xx :=: x

yields TRUE if xx refers to x. The alternative representation of :=: is IS. The identity relation

   xx :/=: x

yields TRUE if xx does not refer to x. The alternative representation of :/=: is ISNT. Here is a short program which illustrates the difference between = and IS:

   PROGRAM test CONTEXT VOID
   USE standard
   BEGIN
      REF INT xx, INT x:=2,y:=3;
      TO 3
      DO
         xx:=(random>0.5|x|y);
         print(("xx :=: x =",
                (xx :=: x|"TRUE"|"FALSE"),
                newline,"xx = ",xx,newline))
      OD
   END
   FINISH

If you want to compare the names that both xx and yy refer to, it is no good writing

   xx IS yy

This always yields FALSE because the names that xx and yy identify always differ (they were created using two local generators so the names are bound to be different). The point is that no automatic dereferencing takes place in an identity relation. To compare the names that both xx and yy refer to, you should place one side or both sides in a cast:

   REF INT(xx) IS yy

This will ensure that the right-hand side (in this case) is dereferenced to yield a name of the same mode as the left-hand side. This is because an identity relation is subject to balancing: one side of the relation is in a soft context and the other side is in a strong context. Given the cast on the left-hand side, the two sides of the identity relation would yield REF INT and REF REF INT. Since no dereferencing is allowed in a soft context, it can be seen that the left-hand side is in the soft context and the right-hand side is in the strong context.

The IS and ISNT in the identity relation are not operators. Since the identity relation is a quaternary (see section 10.8), remember to enclose it in parentheses if you want to use it in a formula:

   IF (field OF struct ISNT xx) & x>=-5
   THEN field OF struct = 0
   ELSE FALSE
   FI

Exercises

11.10
The program fragment
   REF STRING ff, ss; STRING f, s;
   f:="Joan of Arc";
   s:="Robert Burns";
   ff:=(random<0.1|f|s);
   ss:=(ff IS f|s|f)
applies to this and the following exercises. What are the modes of f and ss? Ans[*]
11.11
What does f refer to? Ans[*]
11.12
Write a formula which compares the 3rd and 4th characters of the multiple f refers to with the 7th and 8th characters of the multiple s refers to. What are the modes of the operands of the operator? Ans[*]
11.13
Write an expression which compares the name referred to by ff with the name referred to by ss. Ans[*]


Sian Mountbatten 2012-01-19