Programming in Maple
Roger Kraft Department of Mathematics, Computer Science, and Statistics Purdue University Calumet
roger@calumet.purdue.edu
5.5. Boolean expressions
Boolean expression are used mostly with while-loops and conditional statements. In this section we look at more examples that use boolean expressions and we define the syntax for boolean expressions.
Recall our first definition of bigger3 .
Here is another way to write it.
Try this new version out.
The new version uses a single conditional statement (with an elif-part) and it uses a more sophisticated kind of boolean expression. Let us define several terms used with boolean expression.
Boolean expressions are expressions that evaluate to either true or false . We say that true and false are the two possible boolean values . Boolean expressions can be contrasted with arithmetic (or algebraic) expressions which are expressions that evaluate to a number. Just as arithmetic expressions are made up of basic arithmetic operators (e.g., + , - , * , / , ^ ) and functions that return a number (i.e., real valued functions), boolean expressions are made up of the three logical operators ( and , or , not ), the relational operators ( < , <= , > , >= , = , <> ), and functions that return true or false (i.e., boolean functions ). We have already mentioned the relational operators and we have seen examples of using boolean functions like isprime . But we have not mentioned the basic logical operators and , or ,and not , so we go over them now.
We call and and or binary boolean operators and we call not a unary boolean operator . A binary operator is an operator that acts on two operands (like + , - , * , / , and ^ ) with one operand placed on either side of the operator. But + , - , * , / , and ^ are arithmetic operators that operate on numbers and the result that they return is a number. The operators and and or operate on boolean values and return a boolean value, so they are boolean operators. A unary operator acts on only one operand which is sometimes placed before the operator and sometimes placed after the operator (placing the operand after the unary operator symbol is more common). A common example of a unary operator is the arithmetic operator - for negation (not to be confused with the binary operator - for subtraction) which is place before the operand. Another unary operator is the factorial operator ! which is placed after its operand. The not operator is placed before its boolean operand.
Since boolean operators operate on boolean values, and there are only two boolean values, it is possible to make a table of a boolean operator's value for every possible input. We call these truth tables . Here is the truth table for or .
We can verify some of these entries with simple Maple commands.
Exercise : Here are several examples of boolean expressions. Explain their values.
Note: The parentheses in the last example are not part of a function call since not is not a function. The parentheses are for grouping, and not(x=y) should be thought of like the arithmetic expression -(x+y) (recall that both not and negation are unary operators).
Exercise : The expression -(x+y) is the same as (-x)+(-y) . Is not(x=y) the same as (not x)=(not y) ? Is not(x and y) the same as (not x) and (not y) ?
Exercise : Give examples of some other binary operators in Maple. Give examples of some other unary operators. For your examples, specify the data type of the operands and the return value of the operator.
Notice that the following is not a boolean expression in Maple, though it would be a true statement in a mathematics book.
Error, unexpected relational operator
Here is how the last example should be expressed in Maple.
Similarly we cannot say the following in Maple.
Error, `=` unexpected
Instead we must put it this way.
But now consider this next example. We shouldn't be surprised that Maple will not let us enter the following true mathematical statement.
But the following boolean expression does not return what we are expecting.
Let us analyze how Maple evaluates the boolean expression 1=1^0 and 1^0=0^0 since it brings up an important fact about equations and boolean expressions. Since 1^0 simplifies to 1 , Maple automatically simplifies the equation 1=1^0 to be the equation 1=1 . Similarly for 1^0=0^0 .
So the expression 1=1^0 and 1^0=0^0 simplifies (automatically) to 1=1 and 1=1 . Then Maple automatically simplifies the expression 1=1 and 1=1 to be 1=1 since any expression of the form x and x automatically simplifies to x for any expression x (why?).
But why does the equation 1=1 not evaluate to true , since it certainly is true? The reason has to do with the dual role that equations (and inequalities) play in Maple. Equations are used both as boolean expressions and as algebraic equations.
But it would be very inconvenient if Maple would take an equation like the following and automatically evaluate it as a boolean expression.
Of course, the above equation is a boolean expression, but that is not usually what we have in mind when we type it in. Here is how Maple would evaluate it as a boolean expression.
Since equations in their role as algebraic equations are so common, Maple has a rule that it will not evaluate an isolated equation (or inequality) as a boolean expression unless explicitly told to do so by using the evalb command. But if an equation (or inequality) is part of a larger boolean expression, or contained in the boolean part of a while-loop or conditional statement, then Maple will automatically evaluate the equation (or inequality) as a boolean expression.
So now we know why Maple evaluates the expression 1=1^0 and 1^0=0^0 as 1=1 . Even though the expression starts out as a boolean expression, it automatically simplifies to an equation, and Maple will not evaluate this isolated equation as a boolean expression unless we explicitly tell it to do so by using the evalb command.
But now suppose we go back to the expression 1=0! and 0!=1! . Since both 1=0! and 0!=1! simplify to 1=1 , why does Maple return true for this expression?
The key here is that 0! and 1! do not automatically simplify to 1, both expressions need to be evaluated to 1. It seems that for logical operators like and , Maple does not fully evaluate the operands of the operator before calling the operator.
Parentheses play an important role in boolean expressions just as they do in arithmetic expressions. For example, the following two expressions are equivalent.
But they are not equivalent to the next expression.
(Why did Maple not evaluate this last expression as true or false?)
There is a whole algebra to boolean expressions that specifies the order of precedence for all of the boolean operations, associativity rules for each boolean operation, and algebraic identities for boolean expressions. For example, here are some automatic simplifications that Maple knows for the algebra of boolean expressions.
The last two simplifications are known as DeMorgan's Rules.
Most of the boolean expressions that we need for conditional statements and while-loops are formed using simple combinations of and , or , not , the relational operators, and a few boolean functions like type , isprime , has , and member . So we will not got into any more detail about the algebra of boolean expressions.