comp.lang.ada
 help / color / mirror / Atom feed
* Suggestion for multivalue boolean case for 9X
@ 1992-12-10 22:57 Douglas N. Surber
  0 siblings, 0 replies; 6+ messages in thread
From: Douglas N. Surber @ 1992-12-10 22:57 UTC (permalink / raw)


How many times have you seen code like the following:

    if cond_1 then
        if cond_2
	    if cond_3 then
		s_t_t_t;
	    else
		s_t_t_f;
	    end if;
	else
	    if cond_3 then
		s_t_f_t;
	    else
		s_t_f_f;
	    end if;
	end if;
    else
        if cond_2
	    if cond_3 then
		s_f_t_t;
	    else
		s_f_t_f;
	    end if;
	else
	    if cond_3 then
		s_f_f_t;
	    else
		s_f_f_f;
	    end if;
	end if;
    end if;

I find this hard to read and even harder to maintain.  Cond_3 appears
four times.  That's a maintenance nightmare.  A case statement that
worked on arrays of booleans would be a big improvement.  Something like
the following:

    case (cond_1, cond_2, cond_3) is
	when (true,  true,  true ) =>
	    s_t_t_t;
	when (true,  true,  false) =>
	    s_t_t_f;
	when (true,  false, true ) =>
	    s_t_f_t;
	when (true,  false, false) =>
	    s_t_f_f;
	when (false, true,  true ) =>
	    s_f_t_t;
	when (false, true,  false) =>
	    s_f_t_f;
	when (false, false, true ) =>
	    s_f_f_t;
	when (false, false, false) =>
	    s_f_f_f;
    end case;

I don't know if it's too late to consider this for 9X, but I think it
would be neat.  The compiler could verify that you had covered all of the
cases and not listed any case twice, etc.  I suppose it could be generalized
to work on arrays and records of discrete types.  That might be even better.

Yes I know you could implement a function to turn an array of bools into
an enumeration type or an integer, but that is less clear. Anyway, just
an idea.

Douglas Surber
Lockheed
Houston, TX

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Suggestion for multivalue boolean case for 9X
@ 1992-12-11  1:15 Mark Dunbar
  0 siblings, 0 replies; 6+ messages in thread
From: Mark Dunbar @ 1992-12-11  1:15 UTC (permalink / raw)


In article <dnsurber.724028252@node_26400> dnsurber@lescsse.jsc.nasa.gov (Dougl
as N. Surber) writes:
>How many times have you seen code like the following:
>
>    if cond_1 then
>        if cond_2
>	    if cond_3 then
>		s_t_t_t;
>	    else
>		s_t_t_f;
>	    end if;
>	else
>	    if cond_3 then
>		s_t_f_t;
>	    else
>		s_t_f_f;
>	    end if;
>	end if;

      [etc]...

>I find this hard to read and even harder to maintain.  Cond_3 appears
>four times.  That's a maintenance nightmare.  A case statement that
>worked on arrays of booleans would be a big improvement.  Something like
>the following:
>
>    case (cond_1, cond_2, cond_3) is
>	when (true,  true,  true ) =>
>	    s_t_t_t;
>	when (true,  true,  false) =>
>	    s_t_t_f;
>	when (true,  false, true ) =>
>	    s_t_f_t;
>	when (true,  false, false) =>
>	    s_t_f_f;

	[etc]...
>
>Douglas Surber
>Lockheed
>Houston, TX
>
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
    ...
  elsif ( not Cond_1 ) and (     Cond_2 ) and (     Cond_3 ) then
    ...
  elsif (     Cond_1 ) and ( not Cond_2 ) and ( not Cond_3 ) then
    ...

  etc.

Although I do agree the case statement would be more elegant (sp?) and
potentially faster if there were enough branches to cancel out the
overhead of a case.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Suggestion for multivalue boolean case for 9X
@ 1992-12-11 19:38 James Crigler
  0 siblings, 0 replies; 6+ messages in thread
From: James Crigler @ 1992-12-11 19:38 UTC (permalink / raw)


dunbar@saifr00.cfsat.honeywell.com (Mark Dunbar) writes:

>In article <dnsurber.724028252@node_26400> dnsurber@lescsse.jsc.nasa.gov (Doug
las N. Surber) writes:
>>How many times have you seen code like the following:
>>
>>    if cond_1 then
>>        if cond_2
>>	    if cond_3 then
>>[...]
>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
>    ...
>  etc.

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;

    etc.

This allows the condition set to be arbitrarily complex over an
arbitrary value set in execution while retaining elegance in form.
You can apply pragma INLINE to the selection function to obtain
marginally better exection time (over the non-INLINE'd version.

  Jim Crigler

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Suggestion for multivalue boolean case for 9X
@ 1992-12-11 20:08 Alex Blakemore
  0 siblings, 0 replies; 6+ messages in thread
From: Alex Blakemore @ 1992-12-11 20:08 UTC (permalink / raw)


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.

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;

(assuming the conditions are free of side effects)
A single level chain of ordered condition/action pairs is
much easier to reason about than any nested logic.

I know it seems to picayune but I've found many errors
by simplifying logical statements in mechanical ways
like this - and almost always obtain a better understanding
of the code as a side effect.

This should be ingrained after CS 101 and not be worth mentioning
in such an esteemed forum as comp.lang.ada but many
programmers dont seem to take the time to find the the
simplest construct.



-- 
---------------------------------------------------
Alex Blakemore alex@cs.umd.edu   NeXT mail accepted

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Suggestion for multivalue boolean case for 9X
@ 1992-12-13 15:24 agate!spool.mu.edu!yale.edu!jvnc.net!gmd.de!Germany.EU.net!mcsun!sunic!se
  0 siblings, 0 replies; 6+ messages in thread
From: agate!spool.mu.edu!yale.edu!jvnc.net!gmd.de!Germany.EU.net!mcsun!sunic!se @ 1992-12-13 15:24 UTC (permalink / raw)


Douglas N. Surber (dnsurber@lescsse.jsc.nasa.gov) writes:
>    case (cond_1, cond_2, cond_3) is
>	when (true,  true,  true ) =>
>	    s_t_t_t;
>	when (true,  true,  false) =>
>	    s_t_t_f;
>....
>    end case;

Cobol's EVALUATE works something this. It was a couple a years ago
I played with it, but I recall that I had handy use of it. Particulary
as you could throw in ANY for a parameter to say that for the combination
of the other ones this one didn't care. (Or values that did care had been
covered by previous alternatives.) Unfortunate Cobol didn't allow you
to express non-contiguous values for the same alternative, which some-
what limited the usefulness.

But that was Cobol. Ada has better means as already have been shown by
other authors in this conferences.

>I don't know if it's too late to consider this for 9X,

Thankfully :-) it is far too late. But they scrapped my idea of chained
boolean expressions (e.g. a > b > c) too, so you are not alone.
-- 
Erland Sommarskog - ENEA Data, Stockholm - sommar@enea.se

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Suggestion for multivalue boolean case for 9X
@ 1992-12-14 21:07 John Bollenbacher
  0 siblings, 0 replies; 6+ messages in thread
From: John Bollenbacher @ 1992-12-14 21:07 UTC (permalink / raw)


Mark Dunbar (dunbar@saifr00.cfsat.honeywell.com) wrote:
: In article <dnsurber.724028252@node_26400> dnsurber@lescsse.jsc.nasa.gov (Dou
glas N. Surber) writes:
: >How many times have you seen code like the following:
: >
: >    if cond_1 then
: >        if cond_2
: >	    if cond_3 then
: >		s_t_t_t;
: >	    else
: >		s_t_t_f;
: >	    end if;
: >	else
: >	    if cond_3 then
: >		s_t_f_t;
: >	    else
: >		s_t_f_f;
: >	    end if;
: >	end if;
: 
:       [etc]...
: 
: >I find this hard to read and even harder to maintain.  Cond_3 appears
: >four times.  That's a maintenance nightmare.  A case statement that
: >worked on arrays of booleans would be a big improvement.  Something like
: >the following:
: >
: >    case (cond_1, cond_2, cond_3) is
: >	when (true,  true,  true ) =>
: >	    s_t_t_t;
: >	when (true,  true,  false) =>
: >	    s_t_t_f;
: >	when (true,  false, true ) =>
: >	    s_t_f_t;
: >	when (true,  false, false) =>
: >	    s_t_f_f;
: 
: 	[etc]...
: >
: >Douglas Surber
: >Lockheed
: >Houston, TX
: >
: 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
:     ...
:   elsif ( not Cond_1 ) and (     Cond_2 ) and (     Cond_3 ) then
:     ...
:   elsif (     Cond_1 ) and ( not Cond_2 ) and ( not Cond_3 ) then
:     ...
: 
:   etc.
: 
: Although I do agree the case statement would be more elegant (sp?) and
: potentially faster if there were enough branches to cancel out the
: overhead of a case.

Though I also agree that the original posting has merit, another possibility 
that I have used is something like:

  declare
    type BOOLS is array (1..3) of BOOLEAN;
    VAL : constant BOOLS := (COND_1, COND_2, COND_3);
  begin
    if VAL = (TRUE, TRUE, TRUE) then
      ...
    elsif VAL = (TRUE, TRUE, FALSE) then
      ...
    etc.
--
-----------------------------------------------------------------------------
- John Bollenbacher                                        jhb@dale.cts.com -
- Titan Linkabit Corp.                                       (619) 552-9963 -
- 3033 Science Park Rd.                                                     -
- San Diego, Ca. 92121                                                      -
-----------------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~1992-12-14 21:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1992-12-14 21:07 Suggestion for multivalue boolean case for 9X John Bollenbacher
  -- strict thread matches above, loose matches on Subject: below --
1992-12-13 15:24 agate!spool.mu.edu!yale.edu!jvnc.net!gmd.de!Germany.EU.net!mcsun!sunic!se
1992-12-11 20:08 Alex Blakemore
1992-12-11 19:38 James Crigler
1992-12-11  1:15 Mark Dunbar
1992-12-10 22:57 Douglas N. Surber

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox