home *** CD-ROM | disk | FTP | other *** search
/ Math Solutions 1995 October / Math_Solutions_CD-ROM_Walnut_Creek_October_1995.iso / pc / mac / discrete / doc / boolean.tex < prev    next >
Encoding:
Text File  |  1993-05-05  |  6.7 KB  |  193 lines

  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %%
  3. %A  boolean.tex                 GAP documentation            Martin Schoenert
  4. %%
  5. %A  @(#)$Id: boolean.tex,v 3.4 1993/02/19 10:48:42 gap Exp $
  6. %%
  7. %Y  Copyright 1990-1992,  Lehrstuhl D fuer Mathematik,  RWTH Aachen,  Germany
  8. %%
  9. %%  This file describes the boolean datatype and its operations.
  10. %%
  11. %H  $Log: boolean.tex,v $
  12. %H  Revision 3.4  1993/02/19  10:48:42  gap
  13. %H  adjustments in line length and spelling
  14. %H
  15. %H  Revision 3.3  1991/12/27  16:07:04  martin
  16. %H  revised everything for GAP 3.1 manual
  17. %H
  18. %H  Revision 3.2  1991/07/26  12:34:01  martin
  19. %H  improved the index
  20. %H
  21. %H  Revision 3.1  1991/07/25  16:16:59  martin
  22. %H  fixed some minor typos
  23. %H
  24. %H  Revision 3.0  1991/04/11  11:10:48  martin
  25. %H  Initial revision under RCS.
  26. %H
  27. %%
  28. \Chapter{Booleans}%
  29. \index{type!boolean}\index{logical}
  30.  
  31. The two *boolean*   values are 'true' and  'false'.   They stand for  the
  32. *logical* values  of the same name.  They  appear mainly as values of the
  33. conditions in 'if'-statements and 'while'-loops.
  34.  
  35. This  chapter contains  sections   describing  the operations  that   are
  36. available   for  the  boolean  values   (see   "Comparisons of Booleans",
  37. "Operations for Booleans").
  38.  
  39. Further this chapter contains a section  about the function 'IsBool' (see
  40. "IsBool").  Note that it is a convention that the name of a function that
  41. tests a   property, and  returns  'true' and   'false'  according to  the
  42. outcome, starts with 'Is', as in 'IsBool'.
  43.  
  44. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  45. \Section{Comparisons of Booleans}%
  46. \index{comparisons!of booleans}
  47.  
  48. '<bool1> = <bool2>', '<bool1> \<> <bool2>'
  49.  
  50. The equality  operator '=' evaluates to  'true' if the two boolean values
  51. <bool1> and <bool2> are equal, i.e., both are 'true' or both are 'false',
  52. and 'false' otherwise.  The inequality operator '\<>' evaluates to 'true'
  53. if the two boolean  values <bool1> and  <bool2> are different and 'false'
  54. otherwise.  This operation is also called the *exclusive or*, because its
  55. value is 'true' if exactly one of <bool1> or <bool2> is 'true'.
  56.  
  57. You  can compare boolean  values with objects  of other types.  Of course
  58. they are never equal.
  59.  
  60. |    gap> true = false;
  61.     false
  62.     gap> false = (true = false);
  63.     true
  64.     gap> true <> 17;
  65.     true |
  66.  
  67. '<bool1> \<\ <bool2>', '<bool1> \<= <bool2>',\\
  68. '<bool1>  > <bool2>', '<bool1>  >= <bool2>'
  69.  
  70. The operators  '\<', '\<=',  '>', and   '=>'  evaluate to 'true'  if  the
  71. boolean value <bool1> is less than, less than  or equal to, greater than,
  72. and greater than or equal to the  boolean value <bool2>.  The ordering of
  73. boolean values is defined by 'true \<\ false'.
  74.  
  75. You can  compare boolean values with objects  of other  types.  Integers,
  76. rationals, cyclotomics, permutations,  and words are smaller than boolean
  77. values.  Objects of the other types, i.e., strings, functions, lists, and
  78. records are larger.
  79.  
  80. |    gap> true < false;
  81.     true
  82.     gap> false >= true;
  83.     true
  84.     gap> 17 < true;
  85.     true
  86.     gap> true < [17];
  87.     true |
  88.  
  89. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  90. \Section{Operations for Booleans}%
  91. \index{operations!for booleans}\index{logical operations}%
  92. \index{or}\index{and}\index{not}
  93.  
  94. '<bool1> or <bool2>'
  95.  
  96. The logical operator 'or' evaluates to 'true' if at  least one of the two
  97. boolean operands <bool1> and <bool2> is  'true' and to 'false' otherwise.
  98.  
  99. 'or' first evaluates <bool1>.  If the value is neither 'true' nor 'false'
  100. an error is signalled.  If the value is 'true', then  'or' returns 'true'
  101. *without*  evaluating  <bool2>.   If  the value  is  'false',  then  'or'
  102. evaluates <bool2>.  Again, if the value is neither 'true'  nor 'false' an
  103. error is signalled.  Otherwise 'or' returns  the value  of <bool2>.  This
  104. *short-circuited* evaluation  is important if   the value of  <bool1>  is
  105. 'true' and evaluation of <bool2> would take much time or cause an error.
  106.  
  107. 'or' is associative, i.e., it is allowed to write '<b1> or <b2> or <b3>',
  108. which is interpreted as  '(<b1> or <b2>) or <b3>'.   'or' has the  lowest
  109. precedence  of the logical  operators.   All logical operators have lower
  110. precedence than the comparison operators '=', '\<', 'in', etc.
  111.  
  112. |    gap> true or false;
  113.     true
  114.     gap> false or false;
  115.     false
  116.     gap> i := -1;;  l := [1,2,3];;
  117.     gap> if i <= 0 or l[i] = false  then Print("aha\n");  fi;
  118.     aha    # no error, because 'l[i]' is not evaluated |
  119.  
  120. '<bool1> and <bool2>'
  121.  
  122. The  logical operator 'and' evaluates  to 'true' if both boolean operands
  123. <bool1> and <bool2> are 'true' and to 'false' otherwise.
  124.  
  125. 'and'  first evaluates  <bool1>.    If the value   is neither 'true'  nor
  126. 'false' an  error is signalled.   If the  value  is 'false',  then  'and'
  127. returns 'false'  *without* evaluating <bool2>.  If  the value is  'true',
  128. then 'and' evaluates <bool2>.  Again, if the value  is neither 'true' nor
  129. 'false' an  error is signalled.   Otherwise   'and' returns the  value of
  130. <bool2>.  This *short-circuited* evaluation is important if the  value of
  131. <bool1> is 'false'  and evaluation  of   <bool2> would take much time  or
  132. cause an error.
  133.  
  134. 'and' is  associative, i.e., it is  allowed  to write '<b1>  and <b2> and
  135. <b3>',  which is interpreted  as '(<b1> and <b2>)  and  <b3>'.  'and' has
  136. higher precedence than  the  logical 'or' operator,  but lower  than  the
  137. unary  logical 'not'  operator.    All   logical operators  have    lower
  138. precedence than the comparison operators '=', '\<', 'in', etc.
  139.  
  140. |    gap> true and false;
  141.     false
  142.     gap> true and true;
  143.     true
  144.     gap> false and 17;
  145.     false    # is no error, because '17' is never looked at |
  146.  
  147. 'not <bool>'
  148.  
  149. The logical operator 'not' returns 'true'  if the boolean value <bool> is
  150. 'false' and 'true' otherwise.  An error is  signalled if <bool>  does not
  151. evaluate to 'true' or 'false'.
  152.  
  153. 'not' has higher  precedence than the  other logical operators,  'or' and
  154. 'and'.  All logical operators  have lower precedence than  the comparison
  155. operators '=', '\<', 'in', etc.
  156.  
  157. |    gap> not true;
  158.     false
  159.     gap> not false;
  160.     true |
  161.  
  162. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  163. \Section{IsBool}%
  164. \index{test!for a boolean}
  165.  
  166. 'IsBool( <obj> )'
  167.  
  168. 'IsBool' returns 'true' if <obj>, which may  be an object of an arbitrary
  169. type, is a boolean value and 'false' otherwise.  'IsBool' will  signal an
  170. error if <obj> is an unbound variable.
  171.  
  172. |    gap> IsBool( true );
  173.     true
  174.     gap> IsBool( false );
  175.     true
  176.     gap> IsBool( 17 );
  177.     false |
  178.  
  179. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  180. %%
  181. %E  Emacs . . . . . . . . . . . . . . . . . . . . . local Emacs variables
  182. %%
  183. %%  Local Variables:
  184. %%  mode:               outline
  185. %%  outline-regexp:     "\\\\Chapter\\|\\\\Section"
  186. %%  fill-column:        73
  187. %%  eval:               (hide-body)
  188. %%  End:
  189. %%
  190.  
  191.  
  192.  
  193.