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

Views: 418346
1
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
2
<!-- %% -->
3
<!-- %A boolean.msk GAP documentation Martin Schönert -->
4
<!-- %A Alexander Hulpke -->
5
<!-- %% -->
6
<!-- %A @(#)<M>Id: boolean.msk,v 1.14 2002/04/15 10:02:27 sal Exp </M> -->
7
<!-- %% -->
8
<!-- %Y (C) 1998 School Math and Comp. Sci., University of St Andrews, Scotland -->
9
<!-- %Y Copyright (C) 2002 The GAP Group -->
10
<!-- %% -->
11
<Chapter Label="Booleans">
12
<Heading>Booleans</Heading>
13
14
<Index Subkey="boolean">type</Index>
15
<Index>logical</Index>
16
17
The two main <E>boolean</E> values are <K>true</K> and <K>false</K>.
18
They stand for the <E>logical</E> values of the same name.
19
They appear as values of the conditions in <K>if</K>-statements
20
and <K>while</K>-loops.
21
Booleans are also important as return values of <E>filters</E>
22
(see&nbsp;<Ref Sect="Filters"/>)
23
such as <Ref Prop="IsFinite"/> and <Ref Func="IsBool"/>.
24
Note that it is a convention that the name of a function that
25
returns <K>true</K> or <K>false</K> according to the outcome,
26
starts with <C>Is</C>.
27
<P/>
28
For technical reasons, also the value <K>fail</K>
29
(see&nbsp;<Ref Sect="Fail"/>) is regarded as a boolean.
30
31
32
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
33
<Section Label="sect:IsBool">
34
<Heading>IsBool (Filter)</Heading>
35
36
<#Include Label="IsBool">
37
38
</Section>
39
40
41
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
42
<Section Label="Fail">
43
<Heading>Fail (Variable)</Heading>
44
45
<ManSection>
46
<Var Name="fail"/>
47
48
<Description>
49
The value <K>fail</K> is used to indicate situations when an operation could
50
not be performed for the given arguments, either because of shortcomings of
51
the arguments or because of restrictions in the implementation or
52
computability.
53
So for example <Ref Func="Position"/> will return <K>fail</K>
54
if the point searched for is not in the list.
55
<P/>
56
<K>fail</K> is simply an object that is different from every other object
57
than itself.
58
<P/>
59
For technical reasons, <K>fail</K> is a boolean value.
60
But note that <K>fail</K> cannot be used to form boolean expressions with
61
<K>and</K>, <K>or</K>, and <K>not</K>
62
(see&nbsp;<Ref Sect="Operations for Booleans"/> below),
63
and <K>fail</K> cannot appear in boolean lists
64
(see Chapter&nbsp;<Ref Chap="Boolean Lists"/>).
65
</Description>
66
</ManSection>
67
68
</Section>
69
70
71
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
72
<Section Label="Comparisons of Booleans">
73
<Heading>Comparisons of Booleans</Heading>
74
<Index Subkey="of booleans">comparisons</Index>
75
<Subsection Label="Equality and inequality of Booleans">
76
<Heading>Equality and inequality of Booleans</Heading>
77
<Index Subkey="of booleans">equality</Index>
78
<Index Subkey="of booleans">inequality</Index>
79
<C><A>bool1</A> = <A>bool2</A></C>
80
<P/>
81
<Alt Only="LaTeX">\noindent</Alt>
82
<C><A>bool1</A> &lt;> <A>bool2</A></C>
83
<P/>
84
The equality operator <C>=</C> evaluates to <K>true</K>
85
if the two boolean values <A>bool1</A> and <A>bool2</A> are equal,
86
i.e., both are <K>true</K> or both are <K>false</K> or both <K>fail</K>,
87
and <K>false</K> otherwise.
88
The inequality operator <C>&lt;></C> evaluates to <K>true</K>
89
if the two boolean values <A>bool1</A>, <A>bool2</A> are different,
90
and <K>false</K> otherwise.
91
This operation is also called the <E>exclusive or</E>,
92
because its value is <K>true</K> if exactly one of <A>bool1</A> or
93
<A>bool2</A> is <K>true</K>.
94
<P/>
95
You can compare boolean values with objects of other types.
96
Of course they are never equal.
97
<P/>
98
<Example><![CDATA[
99
gap> true = false;
100
false
101
gap> false = (true = fail);
102
true
103
gap> true <> 17;
104
true
105
]]></Example>
106
</Subsection>
107
108
<Subsection Label="Ordering of Booleans">
109
<Heading>Ordering of Booleans</Heading>
110
<Index Subkey="booleans">ordering</Index>
111
<A>bool1</A> <C>&lt;</C> <A>bool2</A>
112
<P/>
113
The ordering of boolean values is defined by
114
<K>true</K> <C>&lt;</C> <K>false</K> <C>&lt;</C> <K>fail</K>.
115
For the comparison of booleans with other &GAP; objects,
116
see Section&nbsp;<Ref Sect="Comparisons"/>.
117
<P/>
118
<Example><![CDATA[
119
gap> true < false; fail >= false;
120
true
121
true
122
]]></Example>
123
</Subsection>
124
125
</Section>
126
127
128
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
129
<Section Label="Operations for Booleans">
130
<Heading>Operations for Booleans</Heading>
131
132
<Index Subkey="for booleans">operations</Index>
133
<Index>logical operations</Index>
134
135
The following boolean operations are only applicable to <K>true</K> and
136
<K>false</K>.
137
<P/>
138
139
<Subsection Label="Logical disjunction">
140
<Heading>Logical disjunction</Heading>
141
<Index>Logical disjunction</Index>
142
<Index Key="or"><K>or</K></Index>
143
<A>bool1</A> <K>or</K> <A>bool2</A>
144
<P/>
145
The logical operator <K>or</K> evaluates to <K>true</K>
146
if at least one of the two boolean operands <A>bool1</A> and <A>bool2</A>
147
is <K>true</K>, and to <K>false</K> otherwise.
148
<P/>
149
<K>or</K> first evaluates <A>bool1</A>.
150
If the value is neither <K>true</K> nor <K>false</K> an error is signalled.
151
If the value is <K>true</K>, then <K>or</K> returns <K>true</K>
152
<E>without</E> evaluating <A>bool2</A>.
153
If the value is <K>false</K>, then <K>or</K> evaluates <A>bool2</A>.
154
Again, if the value is neither <K>true</K> nor <K>false</K>
155
an error is signalled.
156
Otherwise <K>or</K> returns the value of <A>bool2</A>.
157
This <E>short-circuited</E> evaluation is important if the value of
158
<A>bool1</A> is <K>true</K>
159
and evaluation of <A>bool2</A> would take much time or cause an error.
160
<P/>
161
<K>or</K> is associative, i.e., it is allowed to write
162
<A>b1</A> <K>or</K> <A>b2</A> <K>or</K> <A>b3</A>,
163
which is interpreted as (<A>b1</A> <K>or</K> <A>b2</A>) <K>or</K> <A>b3</A>.
164
<K>or</K> has the lowest precedence of the logical operators.
165
All logical operators have lower precedence than the comparison operators
166
<C>=</C>, <C>&lt;</C>, <K>in</K>, etc.
167
<P/>
168
<Example><![CDATA[
169
gap> true or false;
170
true
171
gap> false or false;
172
false
173
gap> i := -1;; l := [1,2,3];;
174
gap> if i <= 0 or l[i] = false then # this does not cause an error,
175
> Print("aha\n"); fi; # because `l[i]' is not evaluated
176
aha
177
]]></Example>
178
</Subsection>
179
180
<Subsection Label="Logical conjunction">
181
<Heading>Logical conjunction</Heading>
182
<Index>Logical conjunction</Index>
183
<Index Key="and"><K>and</K></Index>
184
<A>bool1</A> <K>and</K> <A>bool2</A>
185
<P/>
186
<Index Key="and" Subkey="for filters"><K>and</K></Index>
187
<Alt Only="LaTeX">\noindent</Alt>
188
<A>fil1</A> <K>and</K> <A>fil2</A>
189
<P/>
190
The logical operator <K>and</K> evaluates to <K>true</K>
191
if both boolean operands <A>bool1</A>, <A>bool2</A> are <K>true</K>,
192
and to <K>false</K> otherwise.
193
<P/>
194
<K>and</K> first evaluates <A>bool1</A>.
195
If the value is neither <K>true</K> nor <K>false</K> an error is signalled.
196
If the value is <K>false</K>, then <K>and</K> returns <K>false</K>
197
<E>without</E> evaluating <A>bool2</A>.
198
If the value is <K>true</K>, then <K>and</K> evaluates <A>bool2</A>.
199
Again, if the value is neither <K>true</K> nor <K>false</K>
200
an error is signalled.
201
Otherwise <K>and</K> returns the value of <A>bool2</A>.
202
This <E>short-circuited</E> evaluation is important if the value of
203
<A>bool1</A> is <K>false</K> and evaluation of <A>bool2</A> would take much
204
time or cause an error.
205
<P/>
206
<K>and</K> is associative, i.e., it is allowed to write
207
<A>b1</A> <K>and</K> <A>b2</A> <K>and</K> <A>b3</A>,
208
which is interpreted as (<A>b1</A> <K>and</K> <A>b2</A>) <K>and</K> <A>b3</A>.
209
<K>and</K> has higher precedence than the logical <K>or</K> operator,
210
but lower than the unary logical <K>not</K> operator.
211
All logical operators have lower precedence than the comparison operators
212
<C>=</C>, <C>&lt;</C>, <K>in</K>, etc.
213
<P/>
214
<Example><![CDATA[
215
gap> true and false;
216
false
217
gap> true and true;
218
true
219
gap> false and 17; # does not cause error, because 17 is never looked at
220
false
221
]]></Example>
222
<P/>
223
<K>and</K> can also be applied to filters.
224
It returns a filter that when applied to some argument <A>x</A>,
225
tests <A>fil1</A><M>(x)</M> <K>and</K> <A>fil2</A><M>(x)</M>.
226
<P/>
227
<Example><![CDATA[
228
gap> andfilt:= IsPosRat and IsInt;;
229
gap> andfilt( 17 ); andfilt( 1/2 );
230
true
231
false
232
]]></Example>
233
</Subsection>
234
235
<Subsection Label="Logical negation">
236
<Heading>Logical negation</Heading>
237
<Index>Logical negation</Index>
238
<Index Key="not"><K>not</K></Index>
239
<K>not</K> <A>bool</A>
240
<P/>
241
The logical operator <K>not</K> returns <K>true</K>
242
if the boolean value <A>bool</A> is <K>false</K>, and <K>true</K> otherwise.
243
An error is signalled if <A>bool</A> does not evaluate to <K>true</K> or
244
<K>false</K>.
245
<P/>
246
<K>not</K> has higher precedence than the other logical operators,
247
<K>or</K> and <K>and</K>.
248
All logical operators have lower precedence than the comparison operators
249
<C>=</C>, <C>&lt;</C>, <K>in</K>, etc.
250
<P/>
251
<Example><![CDATA[
252
gap> true and false;
253
false
254
gap> not true;
255
false
256
gap> not false;
257
true
258
]]></Example>
259
</Subsection>
260
261
</Section>
262
</Chapter>
263
264
265
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
266
<!-- %% -->
267
<!-- %E -->
268
269
270