REDUCE

3.3 Boolean Expressions

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

\(\langle \)expression\(\rangle \) \(\langle \)relational operator\(\rangle \) \(\langle \)expression\(\rangle \)

or

\(\langle \)boolean operator\(\rangle \)(\(\langle \)arguments\(\rangle \))

or

\(\langle \)boolean expression\(\rangle \) \(\langle \)logical operator\(\rangle \) \(\langle \)boolean expression\(\rangle \).

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

In addition to the logical and relational operators defined earlier as infix operators, the following boolean operators are also defined:

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 identifiers);

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 different 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 verified 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 defined return value (for example, a block without a return statement in it) has the boolean value false.


Hosted by Download REDUCE Powered by MathJax