exprn.tex 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. \chapter{Expressions}
  2. {\REDUCE} expressions\index{Expression} may be of several types and consist
  3. of sequences of numbers, variables, operators, left and right parentheses
  4. and commas. The most common types are as follows:
  5. \section{Scalar Expressions}
  6. \index{Scalar}Using the arithmetic operations {\tt + - * / \verb|^|}
  7. (power) and parentheses, scalar expressions are composed from numbers,
  8. ordinary ``scalar'' variables (identifiers), array names with subscripts,
  9. operator or procedure names with arguments and statement expressions.
  10. {\it Examples:}
  11. \begin{verbatim}
  12. x
  13. x^3 - 2*y/(2*z^2 - df(x,z))
  14. (p^2 + m^2)^(1/2)*log (y/m)
  15. a(5) + b(i,q)
  16. \end{verbatim}
  17. The symbol ** may be used as an alternative to the caret symbol (\verb+^+)
  18. for forming powers, particularly in those systems that do not support a
  19. caret symbol.
  20. Statement expressions, usually in parentheses, can also form part of
  21. a scalar\index{Scalar} expression, as in the example
  22. \begin{verbatim}
  23. w + (c:=x+y) + z .
  24. \end{verbatim}
  25. When the algebraic value of an expression is needed, {\REDUCE} determines it,
  26. starting with the algebraic values of the parts, roughly as follows:
  27. Variables and operator symbols with an argument list have the algebraic
  28. values they were last assigned, or if never assigned stand for themselves.
  29. However, array elements have the algebraic values they were last assigned,
  30. or, if never assigned, are taken to be 0.
  31. Procedures are evaluated with the values of their actual parameters.
  32. In evaluating expressions, the standard rules of algebra are applied.
  33. Unfortunately, this algebraic evaluation of an expression is not as
  34. unambiguous as is numerical evaluation. This process is generally referred
  35. to as ``simplification''\index{Simplification} in the sense that the
  36. evaluation usually but not always produces a simplified form for the
  37. expression.
  38. There are many options available to the user for carrying out such
  39. simplification\index{Simplification}. If the user doesn't specify any
  40. method, the default method is used. The default evaluation of an
  41. expression involves expansion of the expression and collection of like
  42. terms, ordering of the terms, evaluation of derivatives and other
  43. functions and substitution for any expressions which have values assigned
  44. or declared (see assignments and {\tt LET} statements). In many cases,
  45. this is all that the user needs.
  46. The declarations by which the user can exercise some control over the way
  47. in which the evaluation is performed are explained in other sections. For
  48. example, if a real (floating point) number is encountered during
  49. evaluation, the system will normally convert it into a ratio of two
  50. integers. If the user wants to use real arithmetic, he can effect this by
  51. the command {\tt on rounded;}.\ttindex{ROUNDED} Other modes for
  52. coefficient arithmetic are described elsewhere.
  53. If an illegal action occurs during evaluation (such as division by zero)
  54. or functions are called with the wrong number of arguments, and so on, an
  55. appropriate error message is generated.
  56. % A list of such error messages is given in an appendix.
  57. \section{Integer Expressions}
  58. \index{Integer}These are expressions which, because of the values of the
  59. constants and variables in them, evaluate to whole numbers.
  60. {\it Examples:}
  61. \begin{verbatim}
  62. 2, 37 * 999, (x + 3)^2 - x^2 - 6*x
  63. \end{verbatim}
  64. are obviously integer expressions.
  65. \begin{verbatim}
  66. j + k - 2 * j^2
  67. \end{verbatim}
  68. is an integer expression when {\tt J} and {\tt K} have values that are
  69. integers, or if not integers are such that ``the variables and fractions
  70. cancel out'', as in
  71. \begin{verbatim}
  72. k - 7/3 - j + 2/3 + 2*j^2.
  73. \end{verbatim}
  74. \section{Boolean Expressions}
  75. \label{sec-boolean}
  76. A boolean expression\index{Boolean} returns a truth value. In the
  77. algebraic mode of {\REDUCE}, boolean expressions have the syntactical form:
  78. \begin{verbatim}
  79. <expression> <relational operator> <expression>
  80. \end{verbatim}
  81. or
  82. \begin{verbatim}
  83. <boolean operator> (<arguments>)
  84. \end{verbatim}
  85. or
  86. \begin{verbatim}
  87. <boolean expression> <logical operator>
  88. <boolean expression>.
  89. \end{verbatim}
  90. Parentheses can also be used to control the precedence of expressions.
  91. In addition to the logical and relational operators defined earlier as
  92. infix operators, the following boolean operators are also defined:\\
  93. \mbox{}\\
  94. \ttindex{EVENP}\ttindex{FIXP}\ttindex{FREEOF}\ttindex{NUMBERP}
  95. \ttindex{ORDP}\ttindex{PRIMEP}
  96. {\renewcommand{\arraystretch}{2}
  97. \begin{tabular}{lp{\redboxwidth}}
  98. {\tt EVENP(U)} & determines if the number {\tt U} is even or not; \\
  99. {\tt FIXP(U)} & determines if the expression {\tt U} is integer or not; \\
  100. {\tt FREEOF(U,V)} & determines if the expression
  101. {\tt U} does not contain the kernel {\tt V} anywhere in its
  102. structure; \\
  103. {\tt NUMBERP(U)} & determines if {\tt U} is a number or not; \\
  104. {\tt ORDP(U,V)} & determines if {\tt U} is ordered
  105. ahead of {\tt V} by some canonical ordering (based on the expression structure
  106. and an internal ordering of identifiers); \\
  107. {\tt PRIMEP(U)} & true if {\tt U} is a prime object. \\
  108. \end{tabular}}
  109. {\it Examples:}
  110. \begin{verbatim}
  111. j<1
  112. x>0 or x=-2
  113. numberp x
  114. fixp x and evenp x
  115. numberp x and x neq 0
  116. \end{verbatim}
  117. Boolean expressions can only appear directly within {\tt IF}, {\tt FOR},
  118. {\tt WHILE}, and {\tt UNTIL} statements, as described in other sections.
  119. Such expressions cannot be used in place of ordinary algebraic expressions,
  120. or assigned to a variable.
  121. NB: For those familiar with symbolic mode, the meaning of some of
  122. these operators is different in that mode. For example, {\tt NUMBERP} is
  123. true only for integers and reals in symbolic mode.
  124. When two or more boolean expressions are combined with {\tt AND}, they are
  125. evaluated one by one until a {\em false\/} expression is found. The rest are
  126. not evaluated. Thus
  127. \begin{verbatim}
  128. numberp x and numberp y and x>y
  129. \end{verbatim}
  130. does not attempt to make the {\tt x>y} comparison unless {\tt X} and {\tt Y}
  131. are both verified to be numbers.
  132. Similarly, evaluation of a sequence of boolean expressions connected by
  133. {\tt OR} stops as soon as a {\em true\/} expression is found.
  134. NB: In a boolean expression, and in a place where a boolean expression is
  135. expected, the algebraic value 0 is interpreted as {\em false}, while all
  136. other algebraic values are converted to {\em true}. So in algebraic mode
  137. a procedure can be written for direct usage in boolean expressions,
  138. returning say 1 or 0 as its value as in
  139. \begin{verbatim}
  140. procedure polynomialp(u,x);
  141. if den(u)=1 and deg(u,x)>=1 then 1 else 0;
  142. \end{verbatim}
  143. One can then use this in a boolean construct, such as
  144. \begin{verbatim}
  145. if polynomialp(q,z) and not polynomialp(q,y) then ...
  146. \end{verbatim}
  147. In addition, any procedure that does not have a defined return value
  148. (for example, a block without a {\tt RETURN} statement in it)
  149. has the boolean value {\em false}.
  150. \section{Equations}
  151. Equations\index{Equation} are a particular type of expression with the syntax
  152. \begin{verbatim}
  153. <expression> = <expression>.
  154. \end{verbatim}
  155. In addition to their role as boolean expressions, they can also be used as
  156. arguments to several operators (e.g., {\tt SOLVE}), and can be
  157. returned as values.
  158. Under normal circumstances, the right-hand-side of the equation is
  159. evaluated but not the left-hand-side. This also applies to any substitutions
  160. made by the {\tt SUB}\ttindex{SUB} operator. If both sides are to be
  161. evaluated, the switch {\tt EVALLHSEQP}\ttindex{EVALLHSEQP} should be
  162. turned on.
  163. To facilitate the handling of equations, two selectors, {\tt LHS}
  164. \ttindex{LHS} and {\tt RHS},\ttindex{RHS} which return the left- and
  165. right-hand sides of a equation\index{Equation} respectively, are provided.
  166. For example,
  167. \begin{verbatim}
  168. lhs(a+b=c) -> a+b
  169. and
  170. rhs(a+b=c) -> c.
  171. \end{verbatim}
  172. \section{Proper Statements as Expressions}
  173. Several kinds of proper statements\index{Proper statement} deliver
  174. an algebraic or numerical result of some kind, which can in turn be used as
  175. an expression or part of an expression. For example, an assignment
  176. statement itself has a value, namely the value assigned. So
  177. \begin{verbatim}
  178. 2 * (x := a+b)
  179. \end{verbatim}
  180. is equal to {\tt 2*(a+b)}, as well as having the ``side-effect''\index{Side
  181. effect} of assigning the value {\tt a+b} to {\tt X}. In context,
  182. \begin{verbatim}
  183. y := 2 * (x := a+b);
  184. \end{verbatim}
  185. sets {\tt X} to {\tt a+b} and {\tt Y} to {\tt 2*(a+b)}.
  186. The sections on the various proper statement\index{Proper statement} types
  187. indicate which of these statements are also useful as expressions.