comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen_leake@stephe-leake.org>
Subject: Re: Very confused by Ada tasking, can not explain the execution  outcome.
Date: Sun, 31 Aug 2008 09:37:20 -0400
Date: 2008-08-31T09:37:20-04:00	[thread overview]
Message-ID: <uljyde25r.fsf@stephe-leake.org> (raw)
In-Reply-To: 48b927f8$0$17330$426a74cc@news.free.fr

Damien Carbonne <damien.carbonne@free.fr> writes:

> climber.cui@gmail.com a �crit :
>> Thank you Jim for modifying the program and making it work. I saw how
>> you modify the guarding conditions for the procedures. In fact, the
>> conditions are fine by themselves, because i wrote the same program in
>> Java. However, the modified program is working now. It indeed reminds
>> me that Ada probably follows the 'strict evaluation' rule when it
>> comes to the boolean expressions, so in
>>   if Next=1 and State=0 and D<N and Needs(D)  then ...
>>   Ada probably would evaluate all the predicates connect by logical
>> AND, but as soon as D=N is reached, array index for Needs(_) would go
>> out of bound (because Needs is defined to be  array(0..N-1) of
>> boolean). I am just guessing here.
>
> Use "and then" instead of "and" to obtain what Java does with "and" :
> --> if Next=1 and then State=0 and then D<N and then Needs(D)  then ...

A better style is to use 'and then' only when it is actually necessary
to protect against an exception. In this case, that would be:

    if Next=1 and State=0 and (D<N and then Needs(D))  then ...

since Needs(D) will raise an exception if D<N is false.

That lets the compiler more completely optimize the full expression;
'Next=1' and 'State=0' can be done in parallel register operations. 

It also tells the reader that they should consider exceptions for the
D part of the expression, but they _don't_ have to consider them for
the other part.

Ada is a very expressive language; it helps both the reader and the
compiler if the code is precise.

-- 
-- Stephe



  reply	other threads:[~2008-08-31 13:37 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-30  1:54 Very confused by Ada tasking, can not explain the execution outcome climber.cui
2008-08-30  4:17 ` jimmaureenrogers
2008-08-30  9:34   ` climber.cui
2008-08-30 10:59     ` Damien Carbonne
2008-08-31 13:37       ` Stephen Leake [this message]
2008-08-30  5:12 ` Jeffrey R. Carter
2008-08-30  9:42   ` climber.cui
2008-08-30 13:40     ` Georg Bauhaus
replies disabled

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