CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

Path: gap4r8 / doc / ref / chap20.txt
Views: 418346
1
2
20 Booleans
3
4
The two main boolean values are true and false. They stand for the logical
5
values of the same name. They appear as values of the conditions in
6
if-statements and while-loops. Booleans are also important as return values
7
of filters (see 13.2) such as IsFinite (30.4-2) and IsBool (20.1-1). Note
8
that it is a convention that the name of a function that returns true or
9
false according to the outcome, starts with Is.
10
11
For technical reasons, also the value fail (see 20.2) is regarded as a
12
boolean.
13
14
15
20.1 IsBool (Filter)
16
17
20.1-1 IsBool
18
19
IsBool( obj )  Category
20
21
tests whether obj is true, false or fail.
22
23
 Example 
24
gap> IsBool( true ); IsBool( false ); IsBool( 17 );
25
true
26
true
27
false
28

29
30
31
20.2 Fail (Variable)
32
33
20.2-1 fail
34
35
fail global variable
36
37
The value fail is used to indicate situations when an operation could not be
38
performed for the given arguments, either because of shortcomings of the
39
arguments or because of restrictions in the implementation or computability.
40
So for example Position (21.16-1) will return fail if the point searched for
41
is not in the list.
42
43
fail is simply an object that is different from every other object than
44
itself.
45
46
For technical reasons, fail is a boolean value. But note that fail cannot be
47
used to form boolean expressions with and, or, and not (see 20.4 below), and
48
fail cannot appear in boolean lists (see Chapter 22).
49
50
51
20.3 Comparisons of Booleans
52
53
54
20.3-1 Equality and inequality of Booleans
55
56
bool1 = bool2
57
58
bool1 <> bool2
59
60
The equality operator = evaluates to true if the two boolean values bool1
61
and bool2 are equal, i.e., both are true or both are false or both fail, and
62
false otherwise. The inequality operator <> evaluates to true if the two
63
boolean values bool1, bool2 are different, and false otherwise. This
64
operation is also called the exclusive or, because its value is true if
65
exactly one of bool1 or bool2 is true.
66
67
You can compare boolean values with objects of other types. Of course they
68
are never equal.
69
70
 Example 
71
gap> true = false;
72
false
73
gap> false = (true = fail);
74
true
75
gap> true <> 17;
76
true
77

78
79
80
20.3-2 Ordering of Booleans
81
82
bool1 < bool2
83
84
The ordering of boolean values is defined by true < false < fail. For the
85
comparison of booleans with other GAP objects, see Section 4.12.
86
87
 Example 
88
gap> true < false; fail >= false;
89
true
90
true
91

92
93
94
20.4 Operations for Booleans
95
96
The following boolean operations are only applicable to true and false.
97
98
99
20.4-1 Logical disjunction
100
101
bool1 or bool2
102
103
The logical operator or evaluates to true if at least one of the two boolean
104
operands bool1 and bool2 is true, and to false otherwise.
105
106
or first evaluates bool1. If the value is neither true nor false an error is
107
signalled. If the value is true, then or returns true without evaluating
108
bool2. If the value is false, then or evaluates bool2. Again, if the value
109
is neither true nor false an error is signalled. Otherwise or returns the
110
value of bool2. This short-circuited evaluation is important if the value of
111
bool1 is true and evaluation of bool2 would take much time or cause an
112
error.
113
114
or is associative, i.e., it is allowed to write b1 or b2 or b3, which is
115
interpreted as (b1 or b2) or b3. or has the lowest precedence of the logical
116
operators. All logical operators have lower precedence than the comparison
117
operators =, <, in, etc.
118
119
 Example 
120
gap> true or false;
121
true
122
gap> false or false;
123
false
124
gap> i := -1;; l := [1,2,3];;
125
gap> if i <= 0 or l[i] = false then # this does not cause an error,
126
>  Print("aha\n"); fi; # because `l[i]' is not evaluated
127
aha
128

129
130
131
20.4-2 Logical conjunction
132
133
bool1 and bool2
134
135
fil1 and fil2
136
137
The logical operator and evaluates to true if both boolean operands bool1,
138
bool2 are true, and to false otherwise.
139
140
and first evaluates bool1. If the value is neither true nor false an error
141
is signalled. If the value is false, then and returns false without
142
evaluating bool2. If the value is true, then and evaluates bool2. Again, if
143
the value is neither true nor false an error is signalled. Otherwise and
144
returns the value of bool2. This short-circuited evaluation is important if
145
the value of bool1 is false and evaluation of bool2 would take much time or
146
cause an error.
147
148
and is associative, i.e., it is allowed to write b1 and b2 and b3, which is
149
interpreted as (b1 and b2) and b3. and has higher precedence than the
150
logical or operator, but lower than the unary logical not operator. All
151
logical operators have lower precedence than the comparison operators =, <,
152
in, etc.
153
154
 Example 
155
gap> true and false;
156
false
157
gap> true and true;
158
true
159
gap> false and 17; # does not cause error, because 17 is never looked at
160
false
161

162
163
and can also be applied to filters. It returns a filter that when applied to
164
some argument x, tests fil1(x) and fil2(x).
165
166
 Example 
167
gap> andfilt:= IsPosRat and IsInt;;
168
gap> andfilt( 17 ); andfilt( 1/2 );
169
true
170
false
171

172
173
174
20.4-3 Logical negation
175
176
not bool
177
178
The logical operator not returns true if the boolean value bool is false,
179
and true otherwise. An error is signalled if bool does not evaluate to true
180
or false.
181
182
not has higher precedence than the other logical operators, or and and. All
183
logical operators have lower precedence than the comparison operators =, <,
184
in, etc.
185
186
 Example 
187
gap> true and false;
188
false
189
gap> not true;
190
false
191
gap> not false;
192
true
193

194
195
196