bool -- Boolean evaluation
Introductionbool(b) evaluates the Boolean expression
b.
Call(s)bool(b)
Parametersb |
- | a Boolean expression |
ReturnsTRUE, FALSE, or UNKNOWN.
b
Related
Functions_lazy_and,
_lazy_or, FALSE, if, is, repeat, TRUE, UNKNOWN, while
Detailsbool serves for reducing Boolean
expressions to one of the Boolean constants TRUE,
FALSE, or UNKNOWN.
Boolean expressions are expressions that are composed of equalities, inequalities, and
these constants, combined via the logical operators and, or, not.
The function bool evaluates all equalities and
inequalities inside a Boolean expression to either TRUE or FALSE. The resulting logical
combination of the Boolean constants is reduced according to the rules
of MuPAD's three state logic (see and, or, not).
Equations x = y and inequalities
x <> y are evaluated syntactically by
bool. It does not test equality in any mathematical
sense.
Inequalities x < y, x <=
y etc. can be evaluated by bool if and only if
x and y are real numbers of type Type::Real. Otherwise, an error
occurs.
bool evaluates all subexpressions of a
Boolean expression before simplifying the result. The functions
_lazy_and, _lazy_or provide an
alternative: ``lazy Boolean evaluation''.bool in the conditional part
of if, repeat, and while statements. Internally, these
statements enforce Boolean evaluation by _lazy_and and _lazy_or. Cf. example 5.simplify with
the option logic to simplify expressions involving
symbolic Boolean subexpressions. Cf. example 6.bool is a function of the system kernel.
Example
1MuPAD realizes that 1 is less than 2:
>> 1 < 2 = bool(1 < 2)
(1 < 2) = TRUE
Note that bool can only compare real numbers of
syntactical type Type::Real:
>> bool(PI < 2 + sqrt(2))
Error: Can't evaluate to boolean [_less]
One can compare floating point approximations.
Alternatively, one can use is:
>> bool(float(PI) < float(2 + sqrt(2))), is(PI < 2 + sqrt(2))
TRUE, TRUE
Example
2The Boolean operators and, or, not do not evaluate equations and
inequalities logically, and return a symbolic Boolean expression.
Boolean evaluation and simplification is enforced by
bool:
>> a = a and 3 < 4
a = a and 3 < 4
>> bool(a = a and 3 < 4)
TRUE
Example
3bool handles the special Boolean constant
UNKNOWN:
>> bool(UNKNOWN and 1 < 2), bool(UNKNOWN or 1 < 2), bool(UNKNOWN and 1 > 2), bool(UNKNOWN or 1 > 2)
UNKNOWN, TRUE, FALSE, UNKNOWN
Example
4bool must be able to reduce all parts of a
composite Boolean expression to one of the Boolean constants. No
symbolic Boolean subexpressions may be involved:
>> b := b1 and b2 or b3: bool(b)
Error: Can't evaluate to boolean [bool]
>> b1 := 1 < 2: b2 := x = x: b3 := FALSE: bool(b)
TRUE
>> delete b, b1, b2, b3:
Example
5There is no need to use bool explicitly in
the conditional parts of if, repeat, and while statements. Note, however, that
these structures internally use ``lazy evaluation'' via _lazy_and and _lazy_or rather than ``complete
Boolean evaluation'' via bool:
>> x := 0: if x <> 0 and sin(1/x) = 0 then 1 else 2 end
2
In contrast to ``lazy evaluation'', bool
evaluates all conditions. Consequently, a division by zero
occurs in the evaluation of sin(1/x) = 0:
>> bool(x <> 0 and sin(1/x) = 0)
Error: Division by zero
>> delete x:
Example
6Expressions involving symbolic Boolean subexpressions
cannot be processed by bool. However, simplify with the option
logic can be used for simplification:
>> (b1 and b2) or (b1 and (not b2)) and (1 < 2)
b1 and b2 or b1 and not b2 and 1 < 2
>> simplify(%, logic)
b1