From: dnsurber@lescsse.jsc.nasa.gov (Douglas N. Surber)
Subject: Re: Suggestion for multivalue boolean case for 9X
Organization: Lockheed Engineering and Sciences
Date: 14 Dec 92 14:49:05 GMT


dunbar@saifr00.cfsat.honeywell.com (Mark Dunbar) writes:
> Using Ada83, instead of the nested if statement, one could do the following:
>   if    ( not Cond_1 ) and ( not Cond_2 ) and ( not Cond_3 ) then
>   elsif ( not Cond_1 ) and ( not Cond_2 ) and (     Cond_3 ) then
>   elsif ( not Cond_1 ) and (     Cond_2 ) and ( not Cond_3 ) then
>   etc.

alex@cs.umd.edu (Alex Blakemore) writes:
>I've rarely encountered complex nested if statements where
>every combination of conditions had a completely different action.
>More often, several combinations share the same action, or parts of
>the same action.  In such cases, you can frequently improve things
>by combining and reordering tests - often removing a level of nesting.

>for a simple example,

>  if x then                               if not x then
>    if y then        can be rewritten       C;
>      A;                                  elsif y then
>    else                                    A;
>       B;                                 else
>    end if;                                 B;
>  else                                    end if;
>    C;
>  end if;

crigler@cs.ucf.edu (James Crigler) writes:
>Having done this enough times to try to find a solution, I use
>this:  Write a function that takes the conditions shown above as
>inputs and returns a value of an enumerated type, then case on the
>function return value like this:
>
>  type SELECTIONS is (E1, E2, E3);
>
>  function SELECTION (C1, C2, C3 : in BOOLEAN) is
>  begin
>     ...
>  end SELECTION;
>
>  case SELECTION(COND_1, COND_2, COND_3) is
>    when E1 =>
>      STMT1;
>    when E2 =>
>      STMT2;

I agree that code equivalent to a multivalued case can be written a number
of ways, but I believe that the multivalued case is a natural extension to
the language.  Mark Dunbar's solution clearly expresses the structure of
the logic (a truth table), but it requires that the conditions be expressed
multiple times or additional variables be declared.  It also does not allow
the compiler to verify that every combination of conditions is uniquely
represented.

Alex Blakemore's solution works quite nicely if the problem is simple enough,
but it fails in the large.  Additionaly it sometimes requires convoluted
logic.  The advantage of the multivalued case is that it clearly expresses
the logic of a decision table and works even for very large problems.
This solution also does not provide sufficient information to enable the 
compiler to verify completeness and consistency.

As Mr. Blakemore pointed out it is often true that many of the branches share
common actions.  It is my opinion that it is more important the clearly
express the conditions under which each branch is taken than it is to 
express that several branches have the same action.  This is especially
true in long lived code.  Maintenance and design changes often require that
previously identical branches be separated.  This is trivial with the
multivalued case.  It is quite difficult using Mr. Blakemore's approach.

I have on ocassion used James Crigler's solution, but only for fairly
large cases involving enumerated types other than boolean.  The implementation
of the Selection function is either identical to the original problem or
an obscure expression involving 'Pos, 'Val, 'First, 'Last, etc.  If the names
of the elements in the enumeration type are well chosen, it can be fairly easy
to read, but establishing its correctness is problematic as it depends on
the rather obscure relationship between the various enumeration types.

Let's face it, the only control structures we really need are while and semi-
colon, everything else is just an attempt to simplify the expression of 
common bits of code.  Having been remined by Mssrs. Dunbar, Blakemore,and
Crigler of the various ways of expressing this logic, I am even more certain
that a multivalued case would be a big help.  And it seems to me a simple and 
logical extension to the language.

Douglas Surber
Lockheed
Houston, TX

Newsgroups: comp.lang.ada