[Next] [Prev] [Right] [Left] [Up] [Index] [Root]
Logical values

Logical values

This section deals with logical values (`Booleans').

Booleans are primarily of importance as (return) values for (intrinsic) predicates. It is important to know that the truth-value of predicates is always evaluated left to right, that is, the left-most clause is evaluated first, and if that determines the value of the predicate evaluation is aborted; if not, the next clause is evaluated, etc. So, for example, if x is a Boolean, it is safe (albeit silly) to type:

> if x eq true or x eq false or x/0 eq 1 then
>    print "fine";
> else
>    print "error";
> end if;
even though x/0 would cause an error ("Bad arguments", not "Division by zero"!) upon evaluation, because the truth value will have been determined before the evaluation of x/0 takes place.
Subsections

Creation of Booleans

Booleans() : Nil -> Bool
The Boolean structure.
true
false
The Boolean elements.

Boolean Operators

x and y : BoolElt, BoolElt -> BoolElt
True if both x and y are true, false otherwise.
x or y: BoolElt, BoolElt -> BoolElt
True if x or y is true (or both are true), false otherwise.
x xor y: BoolElt, BoolElt -> BoolElt
True if either x or y is true (but not both), false otherwise.
not x : BoolElt -> BoolElt
Negate the truth value of x.

Functions on Booleans

x eq y : BoolElt, BoolElt -> BoolElt
True if both x and y are true or both x and y are false, false otherwise.
x ne y : BoolElt, BoolElt -> BoolElt
True if either x is true and y is false, or x is false and y is true, otherwise false.
x in B : BoolElt, Bool -> BoolElt
True if x is a Boolean value, false otherwise.
x notin B : BoolElt, Bool -> BoolElt
True if x is not a Boolean value, false otherwise.
Random(B) : Bool -> BoolElt
Return a random Boolean.

Iteration

A Boolean structure B may be used for enumeration: for x in B do, and x in B in set and sequence constructors.


Example Lang_Booleans (H1E27)

The following program checks that the functions ne and xor coincide.

> P := Booleans();
>  for x, y in P do
>     print (x ne y) eq (x xor y);
> end for;
true
true
true
true
Similarly, we can test whether for any pair of Booleans x, y it is true that x = y iff (x ^ y) v (not x ^ not y).

> equal := true;
> for x, y in P do
>     if (x eq y) and not ((x and y) or (not x and not y)) then
>         equal := false;
>     end if;
> end for;
> print equal;
true

[Next] [Prev] [Right] [Left] [Up] [Index] [Root]