From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,99e73f65ea2533b9 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!postnews.google.com!s50g2000hsb.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: and then... (a curiosity) Date: Tue, 2 Sep 2008 03:33:52 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <18b41828-bda4-4484-8884-ad62ce1c831d@f36g2000hsa.googlegroups.com> <57qdnfULQ9tzKCHVnZ2dnUVZ_tHinZ2d@comcast.com> <48bd0003$1@news.post.ch> NNTP-Posting-Host: 153.98.68.197 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1220351632 14651 127.0.0.1 (2 Sep 2008 10:33:52 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 2 Sep 2008 10:33:52 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: s50g2000hsb.googlegroups.com; posting-host=153.98.68.197; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4.3) Gecko/20040924,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:1862 Date: 2008-09-02T03:33:52-07:00 List-Id: stefan-lu...@see-the.signature wrote: > True. But, at the logic level, the Ada-statement > > if (X >= A'First) and (X <= A'Last) and (A[X]=Y) then > ... > else > ... > end if; > > should *not* raise a Constraint_Error if X < A'First or X > A'Last, but > instead handle the "else" case. Except that X may be a function returning a different value for each call! > True enough, at the logic level the same should hold for > > if (A(x)=Y) and (X >= A'First) and (X <= A'Last) then ... end if; > > > It's all about those side effect which make order of evaluation so > > important. Otherwise it should not matter. > > The bad thing is that Constraint_Error counts as a valid side effect here. And what would be an "invalid" side effect? > Ideally, the program should check the other branches of the "and" > expression, and only propagate the exception if none of them evaluates to > false without raising an exception of its own. No, because the Constraint_Error (or other exception, or other side effect such as logging, assignment to a variable, etc.) may be intentional! The compiler does not and should not try to read your mind. At least I would find it very disturbing to program in a language that would try to "do what I mean, not what I say". > Consider a programm fragment like > > if X and Y then > ... > end if > > Assume Y raises a Constraint_Error if X is false. This appears to be a > common bug pattern in Ada. I have seen this several times in Ada > sourcecode, and *never* it was the programmer's intention to raise an > exception if X is false ... except when an exception was raised > explicitely in the else branch. Even then, the programmers typically > expected the exception they raised there, not Constraint_Error. I have seen this bug a couple of times and always concluded it was *my* fault and that I should have used "and then" to specify that the order of evaluation was important. In such situation the mathematical "and" (where "X and Y" is strictly equivalent to "Y and X") is not what I want. I definitely like the fact that Ada gives me the choice (short-circuit or full evaluation) and the means ("and" vs. "and then") to express my intent exactly. -- Ludovic Brenta.