comp.lang.ada
 help / color / mirror / Atom feed
From: Martin Krischik <krischik@users.sourceforge.net>
Subject: Re: and then... (a curiosity)
Date: Tue, 02 Sep 2008 08:53:58 +0200
Date: 2008-09-02T08:53:58+02:00	[thread overview]
Message-ID: <48bce306$1@news.post.ch> (raw)
In-Reply-To: <ufxojoimh.fsf@STRIPCAPStelus.net>

Ray Blaak schrieb:
> Martin Krischik <krischik@users.sourceforge.net> writes:
>> Actually it is good example - for both:
>>
>> 1) Log to both Disk and Network and return true if at least one was
>> successful:
>>
>> Log_To_Network (Message) or Log_To_Disk (Message)
>>
>> 2) Log to Network and if that fails log to Disk and return true if at
>> least one was successful:
>>
>> Log_To_Network (Message) or else Log_To_Disk (Message)
>>
>> And it shows nicely the why there are two: The programmer can decide
>> which behaviour he / she wants.
> 
> My problem is not with the theoretical elegance as such, but rather that "or"
> vs "or else" is not immediately clear to me.



> As a multi-language programmer, I am really really used to thinking of boolean
> or as "if one is true we don't care about the other".

Funny I am not - probably because my first languages where Basic and
Pascal - and Basic and Pascal do not guarantee short cut boolean. Modern
Basic and Pascal dialects might have it but it is not guaranteed in the
language itself.

> This instinct is
> reenforced both from the current popular languages as well as from formal
> proof styles in programming methodology.

When I was at Polytechnic first language was PL/1, second Pascal and
therefore methodology was different. i.E. Don't rely on the execution
order of boolean expressing.

> So, I don't like relying the execution of both disjuncts. Sure, I could use
> "or else" consistently, but I like thinking of boolean or as good old regular
> boolean or.

They are not necessarily both executed with plain old OR and AND either.
Plain old OR and AND leaves all options open to the optimizer: Out of
order execution Common subexpression elimination or dead code elimination.

If the optimizer determines that the 2nd expression has no side effects
it might optimize it away. But note that raising CONSTRAINT_ERROR is a
side effect. So:

if X /= null and x.all = 5 then
  Do_Something;
end if;

will most likely become:

if x = null then
  raise CONSTRAINT_ERROR;
elif x.all = 5 then
  Do_Something;
end if;

Or - for a CPU with hardware null pointer detection - it might become:

if x.all = 5 then
  Do_Something;
end if;

Ada is a language which highly rely on the existence of an optimizer.
Unlike the C style languages - the very first C did not have an
optimizer and it still show in the syntax and semantic.- One should
always remember C's main design goal: compiler must fit's into 8 kb of
memory. It explains a lot.

Anyway by default Ada leaves all option options open to the optimizer.
For example:

type Byte is range -128 .. 127;

might not actually be a byte - especially when optimized for
performance. By default all options are open to the optimizer to make
the best of you wish. Only when you start restricting the optimizer with

for Byte'Size use 8;

you are guaranteed an 8 bit type.

You consider this when thinking of "or else" and "and then" - both
restrict the optimizer in it's doing and the result might not be faster.

Regards

Martin
-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com



  reply	other threads:[~2008-09-02  6:53 UTC|newest]

Thread overview: 93+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-29 21:06 and then... (a curiosity) mockturtle
2008-08-29 21:47 ` Samuel Tardieu
2008-08-30 21:28   ` Maciej Sobczak
2008-08-31  8:28     ` Georg Bauhaus
2008-08-31 23:21       ` Ray Blaak
2008-09-01  8:05     ` Martin Krischik
2008-09-01 17:56       ` Ray Blaak
2008-09-02  6:53         ` Martin Krischik [this message]
2008-09-02 14:56           ` Adam Beneschan
2008-09-02 16:28             ` Ray Blaak
2008-09-02 16:26           ` Ray Blaak
2008-09-02 20:50             ` Robert A Duff
2008-09-03 12:35               ` Brian Drummond
2008-09-03 15:56                 ` Robert A Duff
2008-09-04 22:09                   ` Brian Drummond
2008-09-03 21:01               ` Vinzent Hoefler
2008-09-02 14:50     ` Adam Beneschan
2008-08-29 22:28 ` Adam Beneschan
2008-08-30  1:06   ` Jeffrey R. Carter
2008-08-30 11:21   ` Dmitry A. Kazakov
2008-08-30 15:35     ` Peter C. Chapin
2008-09-02 15:06       ` Adam Beneschan
2008-09-02  3:41 ` Steve
2008-09-02  7:48   ` stefan-lucks
2008-09-02  8:57     ` Martin Krischik
2008-09-02 10:50       ` stefan-lucks
2008-09-02 10:33         ` Ludovic Brenta
2008-09-02 13:32           ` stefan-lucks
2008-09-02 12:53             ` Ludovic Brenta
2008-09-02 17:32               ` Georg Bauhaus
2008-09-03 13:14               ` stefan-lucks
2008-09-03 12:44                 ` Dmitry A. Kazakov
2008-09-02 13:39             ` stefan-lucks
2008-09-02 13:40             ` stefan-lucks
2008-09-02 16:48             ` Dmitry A. Kazakov
2008-09-02 17:00             ` Keith Thompson
2008-09-02 19:15               ` Simon Wright
2008-09-02 20:37               ` Robert A Duff
2008-09-02 20:58                 ` Jeffrey R. Carter
2008-09-02 21:08                   ` Robert A Duff
2008-09-03 12:24                     ` Pascal Obry
2008-09-02 22:34                   ` Santiago Urueña
2008-09-03  5:56                     ` Robert A Duff
2008-09-03  6:55                       ` Santiago Urueña
2008-09-03 14:14                       ` Adam Beneschan
2008-09-03  0:11                 ` Randy Brukardt
2008-09-02 17:20             ` Georg Bauhaus
2008-09-04  1:05         ` Stephen Leake
2008-09-04  6:45           ` stefan-lucks
2008-09-04  7:35             ` Dmitry A. Kazakov
2008-09-04 12:04               ` stefan-lucks
2008-09-04 13:00                 ` Dmitry A. Kazakov
2008-09-04 19:05                   ` stefan-lucks
2008-09-04 20:28                     ` Dmitry A. Kazakov
2008-09-05  6:57                       ` stefan-lucks
2008-09-05  6:34                         ` Ray Blaak
2008-09-05 14:14                     ` Robert A Duff
2008-09-05 15:04                       ` Dmitry A. Kazakov
2008-09-07 16:45                         ` stefan-lucks
2008-09-05 15:14                       ` Hyman Rosen
2008-09-05 15:59                         ` Adam Beneschan
2008-09-05 16:10                           ` Hyman Rosen
2008-09-07 16:36                       ` stefan-lucks
2008-09-07 16:08                         ` Gautier
2008-09-04  7:39             ` Karel Th�nissen
2008-09-04 12:12               ` stefan-lucks
2008-09-04 15:13                 ` Georg Bauhaus
2008-09-04 15:16                 ` Karel Th�nissen
2008-09-04 15:42                   ` Dmitry A. Kazakov
2008-09-04 19:27                   ` stefan-lucks
2008-09-04 19:43                     ` stefan-lucks
2008-09-04 19:40                       ` Georg Bauhaus
2008-09-05  7:00                         ` stefan-lucks
2008-09-05  6:35                           ` Ray Blaak
2008-09-04 20:06                       ` Karel Th�nissen
2008-09-05  7:44                         ` stefan-lucks
2008-09-05  6:41                           ` Vinzent Hoefler
2008-09-04 20:09                     ` Karel Th�nissen
2008-09-05  7:25                       ` stefan-lucks
2008-09-05  6:37                         ` Ray Blaak
2008-09-05  8:20                           ` stefan-lucks
2008-09-05 13:57                         ` Robert A Duff
2008-09-04 16:33                 ` Dmitry A. Kazakov
2008-09-04 19:31                   ` stefan-lucks
2008-09-04 19:59                     ` Karel Th�nissen
2008-09-05  7:27                       ` stefan-lucks
2008-09-05  8:38                         ` Ludovic Brenta
2008-09-04 20:17                     ` Dmitry A. Kazakov
2008-09-05 13:26                 ` Robert A Duff
2008-09-05 13:49                   ` Robert A Duff
2008-09-03  1:24     ` Stephen Leake
2008-09-03  3:31       ` tmoran
2008-09-03 13:22       ` stefan-lucks
replies disabled

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