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