Previous

3.4. Choice clauses

{Choice-clauses enable a dynamic choice to be made among different paths in a computation. The choice among the alternatives (the in-CHOICE- and the out-CHOICE-clause) is determined by the success or failure of a test on a truth value, on an integer or on a mode. The value under test is computed by an enquiry-clause before the choice is made.

A choice-using-boolean-clause (or conditional-clause) is of the form

(x > 0 | x | 0) in the "brief" style, or IF x > 0 THEN x ELSE 0 FI in the "bold" style; x > 0 is the enquiry-clause, THEN x is the in-CHOICE-clause and ELSE 0 is the out-CHOICE-clause; all three may have the syntactical structure of a series, because all choice-clauses are well closed. A choice-using-boolean-clause may also be reduced to

(x < 0 | x := - x) or IF x < 0 THEN x := - x FI; the omitted out-CHOICE-clause is then understood to be an ELSE SKIP. On the other hand, the choice can be reiterated by writing

(x > 0 | 1 + x |: x < 0 | 1 - x | 1) or IF x > 0 THEN 1 + x ELIF x < 0 THEN 1 - x ELSE 1 FI, and so on; this is to be understood as

(x > 0 | 1 + x | (x < 0 | 1 - x | 1)). CASE-clauses, which define choices depending on an integer or on a mode, are different in that the in-CASE-clause is further decomposed into units. The general pattern is

(- | -,... ,- | -) or CASE -IN-, ... ,-OUT- ESAC. The choice may also be reiterated by use of OUSE.

In a choice-using-integral-clause (or case-clause), the parts are simply units and there must be at least two of them; the choice among the units follows their textual ordering.

Example:

PROC VOID work, relax, enjoy;
CASE INT day; read(day); day
IN work, work, work, work, work, relax, enjoy
OUT print((day, "is not in the week"))
ESAC 
.
In a choice-using-UNITED-clause (or conformity-clause), which tests modes, each case-part-of-CHOICE is of the form (DECLARER identifier): unit or (DECLARER): unit. The mode specified by the declarer is compared with the mode of the value under test; the identifier, if present, is available inside the unit to access that value, with the full security of syntactical mode checking. The 'UNITED' mode provides the required freedom for the mode of the value under test; moreover, that 'UNITED' mode must contain the mode of each specification for, otherwise, the corresponding case-part-of-CHOICE could never be chosen.

Example:

MODE BOY = STRUCT(INT age, REAL weight),
MODE GIRL = STRUCT(INT age, REAL beauty);
PROC UNION(BOY, GIRL) newborn;
CASE newborn IN
    ( BOY john): print(weight OF john),
    ( GIRL mary): print(beauty OF mary)
ESAC
.
}
{The flowers that bloom in the spring, Tra la, Have nothing to do with the case. Mikado, W.S. Gilbert.}


{The hierarchy of ranges in conditional-clauses is illustrated by

and similarly for the other kinds of choice. Thus the nest and the environ of the enquiry-clause remain valid over the in-CHOICE-clause and the out-CHOICE-clause. However, no transfer back from the in- or out-CHOICE-clause into the enquiry-clause is possible, since the latter can contain no label-definitions (except within a closed-clause contained within it).}

3.4.1. Syntax

A) CHOICE :: choice using boolean ; CASE.

B) CASE :: choice using integral ; choice using UNITED.

a) SOID NEST1 CHOICE clause{5D ,551a,A341h,A349a} : CHOICE STYLE start{91a ,-}, SOID NEST1 chooser CHOICE STYLE clause{b}, CHOICE STYLE finish{91e ,-}.

b) SOID NEST1 chooser choice using MODE STYLE clause{a,l} : MODE NEST1 enquiry clause defining LAYER2{c,-}, SOID NEST1 LAYER2 alternate choice using MODE STYLE clause{d}.

c) MODE NEST1 enquiry clause defining new DECSETY2{b,35g } : meek MODE NEST1 new DECSETY2 series with DECSETY2{32b } .

d) SOID NEST2 alternate CHOICE STYLE clause{b} : SOID NEST2 in CHOICE STYLE clause{e}; where SOID balances SOID1 and SOID2{32e } , SOID1 NEST2 in CHOICE STYLE clause{e}, SOID2 NEST2 out CHOICE STYLE clause{l}.

e) SOID NEST2 in CHOICE STYLE clause{d} : CHOICE STYLE in{91b ,-}, SOID NEST2 in part of CHOICE{f,g,h}.

f) SOID NEST2 in part of choice using boolean{e} : SOID NEST2 serial clause defining LAYER3{32a } .

g) SOID NEST2 in part of choice using integral{e} : SOID NEST2 joined portrait{33b } .

h) SOID NEST2 in part of choice using UNITED{e,h} : SOID NEST2 case part of choice using UNITED{i}; where SOID balances SOID1 and SOID2{32e } , SOID1 NEST2 case part of choice using UNITED{i}, and also{94f } token, SOID2 NEST2 in part of choice using UNITED{h}.

i) SOID NEST2 case part of choice using UNITED{h} : MOID NEST2 LAYER3 specification defining LAYER3{j,k,-}, where MOID unites to UNITED{64b } , SOID NEST2 LAYER3 unit{32d} .

{HereLAYER :: new MODE TAG ; new EMPTY.}

j) MODE NEST3 specification defining new MODE TAG3{i} : NEST3 declarative defining new MODE TAG3{541e } brief pack, colon{94f } token.

k) MOID NEST3 specification defining new EMPTY{i} : formal MOID NEST3 declarer{46b } brief pack, colon{94f} token.

l) SOID NEST2 out CHOICE STYLE clause{d} : CHOICE STYLE out{91d ,-}, SOID NEST2 serial clause defining LAYER3{32a } ; CHOICE STYLE again{91c ,-}, SOID NEST2 chooser CHOICE2 STYLE clause{b}, where CHOICE2 may follow CHOICE{m}.

m) WHETHER choice using MODE2 may follow choice using MODE1 {l} : where (MODE1) is (MOOD), WHETHER (MODE2) is (MODE1) ; where (MODE1) begins with (union of), WHETHER (MODE2) begins with (union of).

n) *SOME choice clause : SOME CHOICE clause{a}.

o) *SOME conditional clause : SOME choice using boolean clause{a}.

p) *SOME case clause : SOME choice using integral clause{a}.

q) *SOME conformity clause : SOME choice using UNITED clause{a}. {Examples:

}

a)
(x > 0 | x | 0 ) ·CASE i IN princeton, grenoble OUT finish ESAC · CASE uir IN (INT i): print(i), (REAL): print("no") ESAC
b)
x > 0 | x | 0
c)
x > 0 ·i ·uir
d)
| x ·| x | 0
e)
| x ·IN princeton, grenoble ·IN (INT i): print(i), (REAL): print("no")
f)
x
g)
princeton, grenoble
h)
(INT i): print(i), (REAL); print("no")
i)
(INT i): print(i)
j)
(INT i):
k)
(REAL):
l)
OUT finish ·|: x < 0 | - x | 0
{I would to God they would either conform, or be more wise, and not be catched! Diary. 7 Aug. 1664, Samuel Pepys.}


{Rule d illustrates why 'SORT MOID's should be "balanced". If an alternate-CHOICE-clause is, say, firm, then at least its in-CHOICE-clause or its out-CHOICE-clause must be firm, while the other may be strong. For example, in (p | x | SKIP) + (p | SKIP | y), the conditional-clause (p | x | SKIP) is balanced by making | x firm and | SKIP strong whereas (p | SKIP | y) is balanced by making | SKIP strong and | y firm. The counterexample (p | SKIP | SKIP) + y illustrates that not both may be strong, for otherwise the operator + could not be identified.}

3.4.2. Semantics

a) The yield W of a chooser-CHOICE-clause C, in an environ E1, is determined as follows:

· let E2 be the environ established {3.2.2.b } around E1 according to the enquiry-clause of C;

· let V be the yield, in E2, of that enquiry-clause;

· W is the yield of the scene "chosen" {b}by V from C in E2; it is required that W be not newer in scope than E1.

b) The scene S "chosen" by a value V from a MOID-chooser-CHOICE-clause C, in an environ E2, is determined as follows:

Case A: 'CHOICE' is 'choice using boolean' and V is true:


· S is the constituent in-CHOICE-clause of C, in E2;

Case B: 'CHOICE' is 'choice using integral' and 1 <= V <= n, where n is the number of constituent units of the constituent in-part-of-CHOICE of C:


· S is the V-th such unit, in E2;

Case C: 'CHOICE' is some 'choice using UNITED' and V is acceptable to {2.1.3.6.d } the 'MOID2' of some constituent MOID2-specification D of C {; if there exists more than one such constituent specification, it is not defined which one is chosen as D} ;


· S is the unit following that D, in an environ established {nonlocally {3.2.2.b } } around E2, according to D, with V;

Other Cases {when the enquiry-clause has been unsuccessful} :
If C contains a constituent out-CHOICE-clause O,
then S is O in E2;
otherwise, S is a MOID-skip in E2.

 
Next