Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
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
Project: cocalc-sagemath-dev-slelievre
Views: 418346<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->1<!-- %% -->2<!-- %A boolean.msk GAP documentation Martin Schönert -->3<!-- %A Alexander Hulpke -->4<!-- %% -->5<!-- %A @(#)<M>Id: boolean.msk,v 1.14 2002/04/15 10:02:27 sal Exp </M> -->6<!-- %% -->7<!-- %Y (C) 1998 School Math and Comp. Sci., University of St Andrews, Scotland -->8<!-- %Y Copyright (C) 2002 The GAP Group -->9<!-- %% -->10<Chapter Label="Booleans">11<Heading>Booleans</Heading>1213<Index Subkey="boolean">type</Index>14<Index>logical</Index>1516The two main <E>boolean</E> values are <K>true</K> and <K>false</K>.17They stand for the <E>logical</E> values of the same name.18They appear as values of the conditions in <K>if</K>-statements19and <K>while</K>-loops.20Booleans are also important as return values of <E>filters</E>21(see <Ref Sect="Filters"/>)22such as <Ref Prop="IsFinite"/> and <Ref Func="IsBool"/>.23Note that it is a convention that the name of a function that24returns <K>true</K> or <K>false</K> according to the outcome,25starts with <C>Is</C>.26<P/>27For technical reasons, also the value <K>fail</K>28(see <Ref Sect="Fail"/>) is regarded as a boolean.293031<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->32<Section Label="sect:IsBool">33<Heading>IsBool (Filter)</Heading>3435<#Include Label="IsBool">3637</Section>383940<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->41<Section Label="Fail">42<Heading>Fail (Variable)</Heading>4344<ManSection>45<Var Name="fail"/>4647<Description>48The value <K>fail</K> is used to indicate situations when an operation could49not be performed for the given arguments, either because of shortcomings of50the arguments or because of restrictions in the implementation or51computability.52So for example <Ref Func="Position"/> will return <K>fail</K>53if the point searched for is not in the list.54<P/>55<K>fail</K> is simply an object that is different from every other object56than itself.57<P/>58For technical reasons, <K>fail</K> is a boolean value.59But note that <K>fail</K> cannot be used to form boolean expressions with60<K>and</K>, <K>or</K>, and <K>not</K>61(see <Ref Sect="Operations for Booleans"/> below),62and <K>fail</K> cannot appear in boolean lists63(see Chapter <Ref Chap="Boolean Lists"/>).64</Description>65</ManSection>6667</Section>686970<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->71<Section Label="Comparisons of Booleans">72<Heading>Comparisons of Booleans</Heading>73<Index Subkey="of booleans">comparisons</Index>74<Subsection Label="Equality and inequality of Booleans">75<Heading>Equality and inequality of Booleans</Heading>76<Index Subkey="of booleans">equality</Index>77<Index Subkey="of booleans">inequality</Index>78<C><A>bool1</A> = <A>bool2</A></C>79<P/>80<Alt Only="LaTeX">\noindent</Alt>81<C><A>bool1</A> <> <A>bool2</A></C>82<P/>83The equality operator <C>=</C> evaluates to <K>true</K>84if the two boolean values <A>bool1</A> and <A>bool2</A> are equal,85i.e., both are <K>true</K> or both are <K>false</K> or both <K>fail</K>,86and <K>false</K> otherwise.87The inequality operator <C><></C> evaluates to <K>true</K>88if the two boolean values <A>bool1</A>, <A>bool2</A> are different,89and <K>false</K> otherwise.90This operation is also called the <E>exclusive or</E>,91because its value is <K>true</K> if exactly one of <A>bool1</A> or92<A>bool2</A> is <K>true</K>.93<P/>94You can compare boolean values with objects of other types.95Of course they are never equal.96<P/>97<Example><![CDATA[98gap> true = false;99false100gap> false = (true = fail);101true102gap> true <> 17;103true104]]></Example>105</Subsection>106107<Subsection Label="Ordering of Booleans">108<Heading>Ordering of Booleans</Heading>109<Index Subkey="booleans">ordering</Index>110<A>bool1</A> <C><</C> <A>bool2</A>111<P/>112The ordering of boolean values is defined by113<K>true</K> <C><</C> <K>false</K> <C><</C> <K>fail</K>.114For the comparison of booleans with other &GAP; objects,115see Section <Ref Sect="Comparisons"/>.116<P/>117<Example><![CDATA[118gap> true < false; fail >= false;119true120true121]]></Example>122</Subsection>123124</Section>125126127<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->128<Section Label="Operations for Booleans">129<Heading>Operations for Booleans</Heading>130131<Index Subkey="for booleans">operations</Index>132<Index>logical operations</Index>133134The following boolean operations are only applicable to <K>true</K> and135<K>false</K>.136<P/>137138<Subsection Label="Logical disjunction">139<Heading>Logical disjunction</Heading>140<Index>Logical disjunction</Index>141<Index Key="or"><K>or</K></Index>142<A>bool1</A> <K>or</K> <A>bool2</A>143<P/>144The logical operator <K>or</K> evaluates to <K>true</K>145if at least one of the two boolean operands <A>bool1</A> and <A>bool2</A>146is <K>true</K>, and to <K>false</K> otherwise.147<P/>148<K>or</K> first evaluates <A>bool1</A>.149If the value is neither <K>true</K> nor <K>false</K> an error is signalled.150If the value is <K>true</K>, then <K>or</K> returns <K>true</K>151<E>without</E> evaluating <A>bool2</A>.152If the value is <K>false</K>, then <K>or</K> evaluates <A>bool2</A>.153Again, if the value is neither <K>true</K> nor <K>false</K>154an error is signalled.155Otherwise <K>or</K> returns the value of <A>bool2</A>.156This <E>short-circuited</E> evaluation is important if the value of157<A>bool1</A> is <K>true</K>158and evaluation of <A>bool2</A> would take much time or cause an error.159<P/>160<K>or</K> is associative, i.e., it is allowed to write161<A>b1</A> <K>or</K> <A>b2</A> <K>or</K> <A>b3</A>,162which is interpreted as (<A>b1</A> <K>or</K> <A>b2</A>) <K>or</K> <A>b3</A>.163<K>or</K> has the lowest precedence of the logical operators.164All logical operators have lower precedence than the comparison operators165<C>=</C>, <C><</C>, <K>in</K>, etc.166<P/>167<Example><![CDATA[168gap> true or false;169true170gap> false or false;171false172gap> i := -1;; l := [1,2,3];;173gap> if i <= 0 or l[i] = false then # this does not cause an error,174> Print("aha\n"); fi; # because `l[i]' is not evaluated175aha176]]></Example>177</Subsection>178179<Subsection Label="Logical conjunction">180<Heading>Logical conjunction</Heading>181<Index>Logical conjunction</Index>182<Index Key="and"><K>and</K></Index>183<A>bool1</A> <K>and</K> <A>bool2</A>184<P/>185<Index Key="and" Subkey="for filters"><K>and</K></Index>186<Alt Only="LaTeX">\noindent</Alt>187<A>fil1</A> <K>and</K> <A>fil2</A>188<P/>189The logical operator <K>and</K> evaluates to <K>true</K>190if both boolean operands <A>bool1</A>, <A>bool2</A> are <K>true</K>,191and to <K>false</K> otherwise.192<P/>193<K>and</K> first evaluates <A>bool1</A>.194If the value is neither <K>true</K> nor <K>false</K> an error is signalled.195If the value is <K>false</K>, then <K>and</K> returns <K>false</K>196<E>without</E> evaluating <A>bool2</A>.197If the value is <K>true</K>, then <K>and</K> evaluates <A>bool2</A>.198Again, if the value is neither <K>true</K> nor <K>false</K>199an error is signalled.200Otherwise <K>and</K> returns the value of <A>bool2</A>.201This <E>short-circuited</E> evaluation is important if the value of202<A>bool1</A> is <K>false</K> and evaluation of <A>bool2</A> would take much203time or cause an error.204<P/>205<K>and</K> is associative, i.e., it is allowed to write206<A>b1</A> <K>and</K> <A>b2</A> <K>and</K> <A>b3</A>,207which is interpreted as (<A>b1</A> <K>and</K> <A>b2</A>) <K>and</K> <A>b3</A>.208<K>and</K> has higher precedence than the logical <K>or</K> operator,209but lower than the unary logical <K>not</K> operator.210All logical operators have lower precedence than the comparison operators211<C>=</C>, <C><</C>, <K>in</K>, etc.212<P/>213<Example><![CDATA[214gap> true and false;215false216gap> true and true;217true218gap> false and 17; # does not cause error, because 17 is never looked at219false220]]></Example>221<P/>222<K>and</K> can also be applied to filters.223It returns a filter that when applied to some argument <A>x</A>,224tests <A>fil1</A><M>(x)</M> <K>and</K> <A>fil2</A><M>(x)</M>.225<P/>226<Example><![CDATA[227gap> andfilt:= IsPosRat and IsInt;;228gap> andfilt( 17 ); andfilt( 1/2 );229true230false231]]></Example>232</Subsection>233234<Subsection Label="Logical negation">235<Heading>Logical negation</Heading>236<Index>Logical negation</Index>237<Index Key="not"><K>not</K></Index>238<K>not</K> <A>bool</A>239<P/>240The logical operator <K>not</K> returns <K>true</K>241if the boolean value <A>bool</A> is <K>false</K>, and <K>true</K> otherwise.242An error is signalled if <A>bool</A> does not evaluate to <K>true</K> or243<K>false</K>.244<P/>245<K>not</K> has higher precedence than the other logical operators,246<K>or</K> and <K>and</K>.247All logical operators have lower precedence than the comparison operators248<C>=</C>, <C><</C>, <K>in</K>, etc.249<P/>250<Example><![CDATA[251gap> true and false;252false253gap> not true;254false255gap> not false;256true257]]></Example>258</Subsection>259260</Section>261</Chapter>262263264<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->265<!-- %% -->266<!-- %E -->267268269270