Chapter 6
Keywords: pame

#
Chapter 6

Ex 6.1
  An identity declaration is

      <formal-mode-param> = <actual-mode-param>

Ex 6.2
  Because it is an identifier with a mode, but no associated value.
Ex 6.3

  (a)
    `REAL`
  (b)
    `INT`
  (c)
    Strong
  (d)
    `-5`
Ex 6.4
  Using a loop:-

         ([]INT i)[]CHAR:
         (
            [LWB i:UPB i]CHAR s;

            FOR n FROM LWB i TO UPB i
            DO
            s[n]:=REPR ii
            OD;
            s
         )

Ex 6.5
  `(REF REAL id)REAL:`
Ex 6.6
  `(REF CHAR a,b)REF CHAR:`
Ex 6.7

         (STRING s)[]STRING:
         BEGIN
            FLEX[1:0]STRING r:="";
               #rowing coercion#
            BOOL in word:=FALSE;
            INT st:=LWB s,fn:=UPB s;

            WHILE s[st]=blank & st<=UPB line
            DO
              st+:=1
            OD;

            WHILE s[fn]=blank & fn>=st
            DO
             fn-:=1
            OD;

            STRING ss:=s[st:fn]+blank;

            FOR c FROM LWB ss UNTIL c > UPB ss
            DO
               CHAR ssc=ss[c];

               IF   ssc/=blank & NOT in word
               THEN st:=c;  in word:=TRUE
               ELIF ssc=blank & NOT in word
               THEN SKIP
               ELIF ssc/=blank & in word
               THEN SKIP
               ELSE #ssc=blank & in word#
                  fn:=c-1;  in word:=FALSE;
                  [UPB r+1]STRING rr;
                  rr[:UPB r]:=r;
                  rr[UPB rr]:=ss[st:fn];
                  r:=rr
              #The word has been added to r#
               FI
            OD;

            r[2:]  #Omit the null string#
         END

Ex 6.8
  `REAL:`
Ex 6.9
  `VOID: print("Hi, there")`

Ex 6.10
  This table summaries the occurrences:-

  | Occurrences
  Line | Applied | Defining
  5 | `T p` | 2
  5 | `T c` | 3
  8 | `T 4` | 2
  8 | `T q` | 7
  10 | `T REPR 2` | 3
  12 | `T c` | 3
  12 | `T q` | 2

Ex 6.11

  (a)
    A name of mode `REF INT`.
  (b)
    The integer denoted by `16` of mode `INT`.
  (c)
    The integer nine of mode `INT`.
  (d)
    The integer four of mode `INT`.
Ex 6.12
  The two declarations are firmly related because, in a firm context, a name of mode `REF[]INT` can be dereferenced to a multiple of mode `[]INT`.

Ex 6.13

  (a)
    1.
  (b)
    1.
  (c)
    2.
  (d)
    2.

Ex 6.14
  These reasons are the most important:-
  1. Because their actions are not clear from the program code.
  2. They can cause indeterminate states to occur.
Ex 6.15

  (a)
    You cannot mix letters and symbols.
  (b)
    The symbol should start with `+` which has already been declared as a monadic operator.
  (c)
    This symbol is used for the identity relation (see section 11.6) and is not an operator.
Ex 6.16
  `OP PP = (REF INT a)REF INT: a+:=1`

Ex 6.17
  `PROC p = VOID: a:=3`
Ex 6.18

         PROC p = INT:
         BEGIN
            [(INT i; read((i,newline)); i)]INT a;
            read(a);
            INT sum:=0;

            FOR i TO UPB a DO sum+:=a[i] OD;
            sum
         END

Ex 6.19

         PROC p = REF[,]CHAR:
         (
            HEAP[3,20]CHAR n;
            read((n,newline));
            n
         )

Ex 6.20

         PROC p=(REF REAL r)REF REAL:
            r:=r/pi*180

Ex 6.21

         PROC p = (STRING s,INT i)VOID:
         (
            INT ii = IF i < 0
                     THEN print(newline); ABS i
                     ELSE i
                     FI;
            TO ii DO print(s) OD
         )

Ex 6.22

         PROC num in multiple=(INT i,
                               []INT m,
                               REF INT p)BOOL:
         (
            INT pos:=LWB m - 1;

            FOR j FROM LWB m TO UPB m
            WHILE pos < LWB m
            DO
               (i=m[j]|pos:=j)
            OD;

            IF pos < LWB m
            THEN FALSE
            ELSE p:=pos; TRUE
            FI
         )

Ex 6.23

  (a)
    `10.0`
  (b)
    `0.3`
  (c)

    `0.0`.

Ex 6.24

         PROC reverse = ([]CHAR s)[]CHAR:
         (SIZE s=1|s|s[UPB s]+reverse(s[:UPB s-1]))

Ex 6.25

         PROC(INT)INT cube;

         PROC square=(INT p)INT:
            (ODD p|cube(p)|p^2);
         cube:=(INT p)INT: (ODD p|p^3|square(p))

Ex 6.26
  They form the two sides of an identity declaration.
Ex 6.27

         OP LARGEST = ([,]REAL a)REAL:
         (
            REAL largest:=a[1 LWB a,2 LWB a];

            FOR i FROM 1 LWB a TO 1 UPB a
            DO
               FOR j FROM 2 LWB a TO 2 UPB a
               DO
                  largest:=largest MAX a[i,j]
               OD
            OD;
            largest
         )

Ex 6.28

         PROC pr = (INT n)REF[]INT: HEAP[n]INT

Ex 6.29

         PROC leng = INT:
         (STRING s;
          read((s,newline));
          UPB s)