4.5.1 Logical Operators and Short-circuit Control Forms
Name Resolution Rules
1
{short-circuit control
form} {and
then (short-circuit control form)} {or
else (short-circuit control form)} An
expression consisting of two
relations
connected by
and then or
or else (a
short-circuit control
form) shall resolve to be of some boolean type;
{expected
type (short-circuit control form relation) [partial]} the
expected type for both
relations is that same
boolean type.
1.a
Reason: This rule is written this way
so that overload resolution treats the two operands symmetrically; the
resolution of overloading present in either one can benefit from the
resolution of the other. Furthermore, the type expected by context can
help.
Static Semantics
2
{logical
operator} {operator
(logical)} {and
operator} {operator
(and)} {or
operator} {operator
(or)} {xor
operator} {operator
(xor)} The following logical operators
are predefined for every boolean type
T, for every modular type
T, and for every one-dimensional array type
T whose component
type is a boolean type:
{bit string:
See logical operators on boolean arrays}
3
function "and"(Left, Right : T) return T
function "or" (Left, Right : T) return T
function "xor"(Left, Right : T) return T
3.a/2
This paragraph
was deleted.To be honest: {
AI95-00145-01}
For predefined operators, the parameter and result
subtypes shown as T are actually the unconstrained subtype of
the type.
3.b/2
Ramification: {
AI95-00145-01}
For these operators, we are talking about the type
without any (interesting) subtype, and not some subtype with a constraint
or exclusion. Since it's possible that there is no name for the “uninteresting”
subtype, we denote the type with an italicized T. This applies
to the italicized T in many other predefined operators and attributes
as well.{T (italicized)}
3.c/2
{
AI95-00145-01}
In many cases, there is a subtype with the correct
properties available. The italicized T means:
3.d/2
3.e/2
- the first
subtype of T, for tagged types;
3.f/2
- a subtype
of the type T without any constraint or null exclusion, in other
cases.
3.g/2
Note that “without
a constraint” is not the same as unconstrained. For instance, a
record type with no discriminant part is considered constrained; no subtype
of it has a constraint, but the subtype is still constrained.
3.h/2
Thus, the last case often
is the same as the first subtype of T, but that isn't the case
for constrained array types (where the correct subtype is unconstrained)
and for access types with a null_exclusion
(where the correct subtype does not exclude null).
3.i/2
This italicized T
is used for defining operators and attributes of the language. The meaning
is intended to be as described here.
4
For boolean types, the predefined logical operators
and, or, and xor perform the conventional operations
of conjunction, inclusive disjunction, and exclusive disjunction, respectively.
5
For modular types, the predefined logical operators
are defined on a bit-by-bit basis, using the binary representation of
the value of the operands to yield a binary representation for the result,
where zero represents False and one represents True. If this result is
outside the base range of the type, a final subtraction by the modulus
is performed to bring the result into the base range of the type.
6
The logical operators on arrays are performed on
a component-by-component basis on matching components (as for equality
— see
4.5.2), using the predefined
logical operator for the component type. The bounds of the resulting
array are those of the left operand.
Dynamic Semantics
7
{evaluation (short-circuit
control form) [partial]} The short-circuit
control forms
and then and
or else deliver the same result
as the corresponding predefined
and and
or operators for
boolean types, except that the left operand is always evaluated first,
and the right operand is not evaluated if the value of the left operand
determines the result.
8
{Length_Check
[partial]} {check,
language-defined (Length_Check)} For the
logical operators on arrays, a check is made that for each component
of the left operand there is a matching component of the right operand,
and vice versa.
{Range_Check [partial]}
{check, language-defined
(Range_Check)} Also, a check is made that
each component of the result belongs to the component subtype.
{Constraint_Error
(raised by failure of run-time check)} The
exception Constraint_Error is raised if either of the above checks fails.
8.a
Discussion: The check against the component
subtype is per AI83-00535.
9
12 The conventional
meaning of the logical operators is given by the following truth table:
10
A B (A and B) (A or B) (A xor B)
True True True True False
True False False True True
False True False True True
False False False False False
Examples
11
Examples of logical
operators:
12
Sunny
or Warm
Filter(1 .. 10)
and Filter(15 .. 24) --
see 3.6.1
13
Examples of short-circuit
control forms:
14
Next_Car.Owner /=
null and then Next_Car.Owner.Age > 25 --
see 3.10.1
N = 0
or else A(N) = Hit_Value