Ada short-circuit control forms
Asked Answered
V

4

10

Whats the meaning of

x AND THEN y AND z

is it

x AND THEN (y AND z)

(y, z gets never evaluated if x is FALSE) or

(x AND THEN y) AND z

(if x is FALSE, y is skipped, but its possible that z is evaluated) in ada?

Verlaverlee answered 5/6, 2011 at 20:1 Comment(1)
GNAT will reject to compile the first example with error message "mixed logical operators in expression".Garnes
S
4

The short-circuit operators have the same precedence as their strict versions.

Shows answered 5/6, 2011 at 20:15 Comment(5)
AARM §4.5(17.a) notes that detailed precedence is "implicit in the syntax rules," in this case §4.4 Expressions.Drabbet
That doesn't quite answer the question, as "AND" and "AND THEN" have the same precedence and could arguably be evaluated at "the same time", or given that AND is commutative, in an arbitrary order. Given the same precedence, I think the rule is that operators to right are only evaluated after "... THEN" operations to their left have been evaluated, that have not deterined the outcome of the subterm of same precedence.Smaltite
@Ira Baxter: I don't follow. §4.5(8) goes on to say that for "operators of the same precedence level, the operators are associated with their operands in textual order from left to right." What else is needed?Drabbet
@trashgod: Poster said "equal precedence" answered OP's question. My observation was you needed some kind of left-to-right evaluation rule in addition; your 4.5(8) is apparantly chapter and verse. I characterized it somewhat more loosely; you want the compiler to be evaluate as many AND subterms in any order to produce the best code, but you have to evaluate the AND THEN terms from left to right.Smaltite
@Ira Baxter: Ah, thanks for clarifying. I'd chased this down not long ago, and I remembered needing both sections. I think we're in agreement.Drabbet
F
5

@oenone's comment mentions that GNAT rejects x AND THEN y AND z, but doesn't explain why. It's true, in a sense, that and and and then have the same precedence, but that's not the whole story.

The grammar for an expression is:

expression ::=
  relation {and relation}  | relation {and then relation}
  | relation {or relation} | relation {or else relation}
  | relation {xor relation}

where { FOO } denotes zero or more occurrences of FOO.

This grammar is specifically designed to allow any one of these operators or control forms to be chained in a single expression (X and Y and Z, A and then B and then C), but to forbid mixing them. So the expression in the question, x AND THEN y AND z, is illegal, and the question of what it means doesn't even arise. The point of this rule is precisely to avoid confusion in cases like this.

You just need to write (X and then Y) and Z or X and then (Y and Z), whichever one matches what you want to do.

The same applies to mixing and and or:

    X and Y and Z  -- legal
    X and Y or  Z  -- ILLEGAL
    (X and Y) or Z -- legal
    X and (Y or Z) -- legal
Fungi answered 2/8, 2011 at 15:47 Comment(0)
S
4

The short-circuit operators have the same precedence as their strict versions.

Shows answered 5/6, 2011 at 20:15 Comment(5)
AARM §4.5(17.a) notes that detailed precedence is "implicit in the syntax rules," in this case §4.4 Expressions.Drabbet
That doesn't quite answer the question, as "AND" and "AND THEN" have the same precedence and could arguably be evaluated at "the same time", or given that AND is commutative, in an arbitrary order. Given the same precedence, I think the rule is that operators to right are only evaluated after "... THEN" operations to their left have been evaluated, that have not deterined the outcome of the subterm of same precedence.Smaltite
@Ira Baxter: I don't follow. §4.5(8) goes on to say that for "operators of the same precedence level, the operators are associated with their operands in textual order from left to right." What else is needed?Drabbet
@trashgod: Poster said "equal precedence" answered OP's question. My observation was you needed some kind of left-to-right evaluation rule in addition; your 4.5(8) is apparantly chapter and verse. I characterized it somewhat more loosely; you want the compiler to be evaluate as many AND subterms in any order to produce the best code, but you have to evaluate the AND THEN terms from left to right.Smaltite
@Ira Baxter: Ah, thanks for clarifying. I'd chased this down not long ago, and I remembered needing both sections. I think we're in agreement.Drabbet
V
3

As Mrab & Ira & trash have said they have equal pecedence, however what has not been pointed out explicitly is that the "and then" & "or else" operators will cause the expression to return (finish evaluation) as soon as a result can be determined.

For example(in pseudocode) :

if Almost_always_true_fn() or else costly_fn() then 
  do_stuff;
end if;

Most of the time only the first function (Almost_always_true_fn) will be evaluated, and only when that returns false will costly_fn be executed.

Compare this with :

if Almost_always_true_fn() or costly_fn() then 
  do_stuff;
end if;

In this case both Almost_always_true_fn() and costly_fn() will be evaluated.

NWS.

Vaporish answered 6/6, 2011 at 8:39 Comment(0)
D
3

Yes, in (x AND THEN y) AND z, the relation z will always be evaluated.

Drabbet answered 6/6, 2011 at 15:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.