Boolean Expressions
Date: 2020-10-07 18:10:00

Up | Next | Prev | PrevTail | Tail

### 3.3 Boolean Expressions

A boolean expression returns a truth value. In the algebraic mode of REDUCE, boolean expressions have the syntactical form:

⟨expression⟩⟨relational operator⟩⟨expression⟩

or

⟨boolean operator⟩(⟨arguments⟩)

or

⟨boolean expression⟩⟨logical operator⟩⟨boolean expression⟩.

Parentheses can also be used to control the precedence of expressions.

In addition to the logical and relational operators deﬁned earlier as inﬁx operators, the following boolean operators are also deﬁned:

EVENP(U) | determines if the number U is even or not;
FIXP(U) | determines if the expression U is integer or not;
FREEOF(U,V) | determines if the expression U does not contain the kernel V anywhere in its structure;
NUMBERP(U) | determines if U is a number or not;
ORDP(U,V) | determines if U is ordered ahead of V by some canonical ordering (based on the expression structure and an internal ordering of identiﬁers);
PRIMEP(U) | true if U is a prime object, i.e., any object other than 0 and plus or minus 1 which is only exactly divisible by itself or a unit.

Examples:

j<1
x>0 or x=-2
numberp x
fixp x and evenp x
numberp x and x neq 0

Boolean expressions can only appear directly within IF, FOR, WHILE, and UNTIL statements, as described in other sections. Such expressions cannot be used in place of ordinary algebraic expressions, or assigned to a variable.

NB: For those familiar with symbolic mode, the meaning of some of these operators is diﬀerent in that mode. For example, NUMBERP is true only for integers and reals in symbolic mode.

When two or more boolean expressions are combined with AND, they are evaluated one by one until a false expression is found. The rest are not evaluated. Thus

numberp x and numberp y and x>y

does not attempt to make the x>y comparison unless X and Y are both veriﬁed to be numbers.

Similarly, evaluation of a sequence of boolean expressions connected by OR stops as soon as a true expression is found.

NB: In a boolean expression, and in a place where a boolean expression is expected, the algebraic value 0 is interpreted as false, while all other algebraic values are converted to true. So in algebraic mode a procedure can be written for direct usage in boolean expressions, returning say 1 or 0 as its value as in

procedure polynomialp(u,x);
if den(u)=1 and deg(u,x)>=1 then 1 else 0;

One can then use this in a boolean construct, such as

if polynomialp(q,z) and not polynomialp(q,y) then ...

In addition, any procedure that does not have a deﬁned return value (for example, a block without a RETURN statement in it) has the boolean value false.

Up | Next | Prev | PrevTail | Front