comp.lang.ada
 help / color / mirror / Atom feed
* Re: Order of logical operations
       [not found]     ` <40C0208A.80DBE53A@wldelft.nl>
@ 2004-06-04 18:40       ` James Van Buskirk
  2004-06-04 20:06         ` Ed Falis
                           ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: James Van Buskirk @ 2004-06-04 18:40 UTC (permalink / raw)


"Arjen Markus" <arjen.markus@wldelft.nl> wrote in message
news:40C0208A.80DBE53A@wldelft.nl...

> Short-circuiting does not necessarily lead to quicker programs.
> And if you really need it, you might invent a new operator,
> like ".cand." (the implementation ought to be very simple).

As others have pointed out such operators have to be defined
at the language level.  Short-circuiting operators are trickier
in Fortran than in Ada because to be consistent with the rest
of the language they would have to be elemental.  Most of the
tricky stuff has already been dealt with, however, because
Fortran already has masking constructs (WHERE) and there are
only a few new cases left like:

a(x) .and_then. b(x)

when a(x) is array-valued and b(x) is scalar.  I am curious
as to how useful short-circuiting logical operators are
considered to be in languages such as Ada that have both
short-circuiting and non-short-circuiting operators.

-- 
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end





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

* Re: Order of logical operations
  2004-06-04 18:40       ` Order of logical operations James Van Buskirk
@ 2004-06-04 20:06         ` Ed Falis
  2004-06-04 21:09           ` Nick Roberts
  2004-06-04 21:37         ` Björn Persson
  2004-06-05 10:44         ` Dmitry A. Kazakov
  2 siblings, 1 reply; 7+ messages in thread
From: Ed Falis @ 2004-06-04 20:06 UTC (permalink / raw)


On Fri, 04 Jun 2004 18:40:53 GMT, James Van Buskirk  
<not_valid@comcast.net> wrote:
<snip>

> I am curious
> as to how useful short-circuiting logical operators are
> considered to be in languages such as Ada that have both
> short-circuiting and non-short-circuiting operators.
>

The short-circuit forms in Ada are considered to be very useful for  
structural analysis of code during safety certification (multiple  
condition, multiple decision analysis).  This is because sequentialization  
of the conditions reduces the number of tests that would be necessary  
compared to the simultaneous forms, where all combinations of conditions  
would need to be exercised.

Aside from that, I personally don't have a strong feeling about the  
relative merits, excepting some potential performance gains.

- Ed



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

* Re: Order of logical operations
  2004-06-04 20:06         ` Ed Falis
@ 2004-06-04 21:09           ` Nick Roberts
  2004-06-07 17:00             ` Warren W. Gay VE3WWG
  0 siblings, 1 reply; 7+ messages in thread
From: Nick Roberts @ 2004-06-04 21:09 UTC (permalink / raw)


"Ed Falis" <falis@verizon.net> wrote in message
news:opr8255wqr5afhvo@garuda.mshome.net...

> On Fri, 04 Jun 2004 18:40:53 GMT, James Van Buskirk
> > I am curious
> > as to how useful short-circuiting logical operators are
> > considered to be in languages such as Ada that have both
> > short-circuiting and non-short-circuiting operators.
>
> The short-circuit forms in Ada are considered to be very
> useful for structural analysis of code during safety
> certification (multiple condition, multiple decision analysis).
> This is because sequentialization of the conditions reduces
> the number of tests that would be necessary compared to
> the simultaneous forms, where all combinations of
> conditions would need to be exercised.
>
> Aside from that, I personally don't have a strong feeling
> about the relative merits, excepting some potential
> performance gains.

Surely it's quite handy to be able to write things like the following?

   A: array (1..10) of Boolean;
   ...
   while i > 0 and then A(i) loop
      ...

The alternatives, such as:

   loop
      exit when i <= 0;
      exit when not A(i);
      ...

are quite a bit clumsier.

As for speed, the situation tends to be complicated if the target
architecture/machine is a typical modern superscalar one. For these, an
optimising compiler is likely to perform some fairly strong transformations,
and the results can be very unexpected. Best speed is likely to depend on
whether the compiler can make the most of special (conditional)
instructions, and often the choice between the short-circuit or ordinary
form will not affect the speed of the emitted code in a predictable manner.

My advice: (a) try to write Ada code to make sense at the level of the
application; (b) if you can (and have time), look at the machine code
emitted; (c) if you can (and have time), perform profiling on the code in
typical execution.

-- 
Nick Roberts





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

* Re: Order of logical operations
  2004-06-04 18:40       ` Order of logical operations James Van Buskirk
  2004-06-04 20:06         ` Ed Falis
@ 2004-06-04 21:37         ` Björn Persson
  2004-06-05 10:44         ` Dmitry A. Kazakov
  2 siblings, 0 replies; 7+ messages in thread
From: Björn Persson @ 2004-06-04 21:37 UTC (permalink / raw)


James Van Buskirk wrote:

> I am curious
> as to how useful short-circuiting logical operators are
> considered to be in languages such as Ada that have both
> short-circuiting and non-short-circuiting operators.

Well I use them frequently in "if" statements where the first test 
determines whether the second test would be valid. An example:

    if Str'Length > 0 and then Str(1) = Apostrophe then
       Do_A;
    else
       Do_B;
    end if;

If Str'Length is zero then there is no Str(1), so without 
short-circuiting operators I'd have to write:

    if Str'Length > 0 then
       if Str(1) = Apostrophe then
          Do_A;
       else
          Do_B;
       end if;
    else
       Do_B;
    end if;

-- 
Björn Persson

jor ers @sv ge.
b n_p son eri nu




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

* Re: Order of logical operations
  2004-06-04 18:40       ` Order of logical operations James Van Buskirk
  2004-06-04 20:06         ` Ed Falis
  2004-06-04 21:37         ` Björn Persson
@ 2004-06-05 10:44         ` Dmitry A. Kazakov
  2 siblings, 0 replies; 7+ messages in thread
From: Dmitry A. Kazakov @ 2004-06-05 10:44 UTC (permalink / raw)


James Van Buskirk wrote:

> "Arjen Markus" <arjen.markus@wldelft.nl> wrote in message
> news:40C0208A.80DBE53A@wldelft.nl...
> 
>> Short-circuiting does not necessarily lead to quicker programs.
>> And if you really need it, you might invent a new operator,
>> like ".cand." (the implementation ought to be very simple).
> 
> As others have pointed out such operators have to be defined
> at the language level.

Not necessarily.

The language might support a more general concept: lazy arguments. Consider:

-- This is not Ada!
function "and then" (Left : Boolean; Right : lazy Boolean)
   return Boolean is
begin
   if Left then
      return True;
   else
      return Right;
         -- Right is evaluated only at this point on the
         -- caller's context.
   end if;
end "and then";

It is a very powerful mechanism that allows to enforce the evaluation order
of subroutine arguments:

-- This is not Ada!
function "&" (A : String, B : lazy String) return String is
   Value_A : String renames A; -- This evaluates A
begin
   return Value_A & B; -- B is evaluated after A
end "&";

Now it is safe to write:

... := Read_String & Read_String & Read_String;

Though, that could be tricky to implement, especially if we consider lazy
arguments of recursive calls, protected operations, side effects of lazy
evaluations on alien contexts and all that stuff.

-- 
Regards,
Dmitry A. Kazakov
www.dmitry-kazakov.de



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

* Re: Order of logical operations
  2004-06-04 21:09           ` Nick Roberts
@ 2004-06-07 17:00             ` Warren W. Gay VE3WWG
  2004-06-07 17:36               ` Ed Falis
  0 siblings, 1 reply; 7+ messages in thread
From: Warren W. Gay VE3WWG @ 2004-06-07 17:00 UTC (permalink / raw)


Nick Roberts wrote:

> "Ed Falis" <falis@verizon.net> wrote in message
> news:opr8255wqr5afhvo@garuda.mshome.net...
>>On Fri, 04 Jun 2004 18:40:53 GMT, James Van Buskirk
>>
>>>I am curious
>>>as to how useful short-circuiting logical operators are
>>>considered to be in languages such as Ada that have both
>>>short-circuiting and non-short-circuiting operators.
>>
>>The short-circuit forms in Ada are considered to be very
>>useful for structural analysis of code during safety
>>certification (multiple condition, multiple decision analysis).
>>This is because sequentialization of the conditions reduces
>>the number of tests that would be necessary compared to
>>the simultaneous forms, where all combinations of
>>conditions would need to be exercised.
>>
>>Aside from that, I personally don't have a strong feeling
>>about the relative merits, excepting some potential
>>performance gains.
> 
> 
> Surely it's quite handy to be able to write things like the following?
> 
>    A: array (1..10) of Boolean;
>    ...
>    while i > 0 and then A(i) loop
>       ...

I find it handy for access types (for the FORTRAN folks,
the use of pointers):

if P /= null and then P.Member <= 23 then
    ...

If this were not short-circuited the P.Member access
would be problematic when P was null.
-- 
Warren W. Gay VE3WWG
http://home.cogeco.ca/~ve3wwg




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

* Re: Order of logical operations
  2004-06-07 17:00             ` Warren W. Gay VE3WWG
@ 2004-06-07 17:36               ` Ed Falis
  0 siblings, 0 replies; 7+ messages in thread
From: Ed Falis @ 2004-06-07 17:36 UTC (permalink / raw)


On Mon, 07 Jun 2004 13:00:20 -0400, Warren W. Gay VE3WWG  
<ve3wwg@cogeco.ca> wrote:

> Nick Roberts wrote:
>
>>   Surely it's quite handy to be able to write things like the following?
>>     A: array (1..10) of Boolean;
>>    ...
>>    while i > 0 and then A(i) loop
>>       ...
>
> I find it handy for access types (for the FORTRAN folks,
> the use of pointers):
>
> if P /= null and then P.Member <= 23 then
>     ...
>
> If this were not short-circuited the P.Member access
> would be problematic when P was null.


Sorry I didn't mention this kind of use in my first message.  It's so  
obvious and widespread that I forgot to list it ;-)

- Ed



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

end of thread, other threads:[~2004-06-07 17:36 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <slrncbtfkn.247.tconnors@tellurium.ssi.swin.edu.au>
     [not found] ` <m11xkwd6ct.fsf@macfortran.local>
     [not found]   ` <slrn-0.9.7.4-31741-29330-200406040800-tc@hexane.ssi.swin.edu.au>
     [not found]     ` <40C0208A.80DBE53A@wldelft.nl>
2004-06-04 18:40       ` Order of logical operations James Van Buskirk
2004-06-04 20:06         ` Ed Falis
2004-06-04 21:09           ` Nick Roberts
2004-06-07 17:00             ` Warren W. Gay VE3WWG
2004-06-07 17:36               ` Ed Falis
2004-06-04 21:37         ` Björn Persson
2004-06-05 10:44         ` Dmitry A. Kazakov

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