progstr.tex 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. \chapter{Structure of Programs}
  2. A {\REDUCE} program\index{Program structure} consists of a set of
  3. functional commands which are evaluated sequentially by the computer.
  4. These commands are built up from declarations, statements and expressions.
  5. Such entities are composed of sequences of numbers, variables, operators,
  6. strings, reserved words and delimiters (such as commas and parentheses),
  7. which in turn are sequences of basic characters.
  8. \section{The {\REDUCE} Standard Character Set}
  9. \index{Character set}The basic characters which are used to build
  10. {\REDUCE} symbols are the following:
  11. \begin{enumerate}
  12. \item The 26 letters {\tt a} through {\tt z}
  13. \item The 10 decimal digits {\tt 0} through {\tt 9}
  14. \item The special characters \_\_ ! " \$ \% ' ( ) * + , - . / : ; $<$ $>$
  15. = \{ \} $<$blank$>$
  16. \end{enumerate}
  17. With the exception of strings and characters preceded by an
  18. exclamation mark\index{Exclamation mark}, the case
  19. of characters is ignored: depending of the underlying LISP
  20. they will all be converted internally into lower case or
  21. upper case: {\tt ALPHA}, {\tt Alpha} and {\tt alpha}
  22. represent the same symbol. Most implementations allow you to switch
  23. this conversion off. The operating instructions for a particular
  24. implementation should be consulted on this point. For portability, we
  25. shall limit ourselves to the standard character set in this exposition.
  26. \section{Numbers}
  27. \index{Number}There are several different types of numbers available in
  28. \REDUCE. Integers consist of a signed or unsigned sequence of decimal
  29. digits written without a decimal point, for example:
  30. \begin{verbatim}
  31. -2, 5396, +32
  32. \end{verbatim}
  33. In principle, there is no practical limit on the number of digits
  34. permitted as exact arithmetic is used in most implementations. (You should
  35. however check the specific instructions for your particular system
  36. implementation to make sure that this is true.) For example, if you ask
  37. for the value of $2^{2000}$ you get it
  38. displayed as a number of 603 decimal digits, taking up nine lines of
  39. output on an interactive display. It should be borne in mind of course
  40. that computations with such long numbers can be quite slow.
  41. Numbers that aren't integers are usually represented as the quotient of
  42. two integers, in lowest terms: that is, as rational numbers.
  43. In essentially all versions of {\REDUCE} it is also possible (but not always
  44. desirable!) to ask {\REDUCE} to work with floating point approximations to
  45. numbers again, to any precision. Such numbers are called {\em real}.
  46. \index{Real} They can be input in two ways:
  47. \begin{enumerate}
  48. \item as a signed or unsigned sequence of any number of decimal digits
  49. with an embedded or trailing decimal point.
  50. \item as in 1. followed by a decimal exponent which is written as the
  51. letter {\tt E} followed by a signed or unsigned integer.
  52. \end{enumerate}
  53. e.g. {\tt 32. +32.0 0.32E2} and {\tt 320.E-1} are all representations of
  54. 32.
  55. The declaration {\tt SCIENTIFIC\_NOTATION}\ttindex{SCIENTIFIC\_NOTATION}
  56. controls the output format of floating point numbers. At
  57. the default settings, any number with five or less digits before the
  58. decimal point is printed in a fixed-point notation, e.g., {\tt 12345.6}.
  59. Numbers with more than five digits are printed in scientific notation,
  60. e.g., {\tt 1.234567E+5}. Similarly, by default, any number with eleven or
  61. more zeros after the decimal point is printed in scientific notation. To
  62. change these defaults, {\tt SCIENTIFIC\_NOTATION} can be used in one of two
  63. ways. {\tt SCIENTIFIC\_NOTATION} {\em m};, where {\em m\/} is a positive
  64. integer, sets the printing format so that a number with more than {\em m\/}
  65. digits before the decimal point, or {\em m\/} or more zeros after the
  66. decimal point, is printed in scientific notation. {\tt SCIENTIFIC\_NOTATION}
  67. \{{\em m,n}\}, with {\em m\/} and {\em n\/} both positive integers, sets the
  68. format so that a number with more than {\em m\/} digits before the decimal
  69. point, or {\em n\/} or more zeros after the decimal point is printed in
  70. scientific notation.
  71. {\it CAUTION:} The unsigned part of any number\index{Number} may {\em not\/}
  72. begin with a decimal point, as this causes confusion with the {\tt CONS} (.)
  73. operator, i.e., NOT ALLOWED: {\tt .5 -.23 +.12};
  74. use {\tt 0.5 -0.23 +0.12} instead.
  75. \section{Identifiers}
  76. Identifiers\index{Identifier} in {\REDUCE} consist of one or more
  77. alphanumeric characters (i.e. alphabetic letters or decimal
  78. digits) the first of which must be alphabetic. The maximum number of
  79. characters allowed is implementation dependent, although twenty-four is
  80. permitted in most implementations. In addition, the underscore character
  81. (\_) is considered a letter if it is {\it within} an identifier. For example,
  82. \begin{verbatim}
  83. a az p1 q23p a_very_long_variable
  84. \end{verbatim}
  85. are all identifiers, whereas
  86. \begin{verbatim}
  87. _a
  88. \end{verbatim}
  89. is not.
  90. A sequence of alphanumeric characters in which the first is a digit is
  91. interpreted as a product. For example, {\tt 2ab3c} is interpreted as
  92. {\tt 2*ab3c}. There is one exception to this: If the first letter after a
  93. digit is {\tt E}, the system will try to interpret that part of the
  94. sequence as a real number\index{Real}, which may fail in some cases. For
  95. example, {\tt 2E12} is the real number $2.0*10^{12}$, {\tt 2e3c} is
  96. 2000.0*C, and {\tt 2ebc} gives an error.
  97. Special characters, such as $-$, *, and blank, may be used in identifiers
  98. too, even as the first character, but each must be preceded by an
  99. exclamation mark in input. For example:
  100. \begin{verbatim}
  101. light!-years d!*!*n good! morning
  102. !$sign !5goldrings
  103. \end{verbatim}
  104. {\it CAUTION:} Many system identifiers have such special characters in their
  105. names (especially * and =). If the user accidentally picks the name of one
  106. of them for his own purposes it may have catastrophic consequences for his
  107. {\REDUCE} run. Users are therefore advised to avoid such names.
  108. Identifiers are used as variables, labels and to name arrays, operators
  109. and procedures.
  110. \subsection*{Restrictions}
  111. The reserved words listed in another section may not be used as
  112. identifiers. No spaces may appear within an identifier, and an identifier
  113. may not extend over a line of text. (Hyphenation of an identifier, by
  114. using a reserved character as a hyphen before an end-of-line character is
  115. possible in some versions of {\REDUCE}).
  116. \section{Variables}
  117. Every variable\index{Variable} is named by an identifier, and is given a
  118. specific type. The type is of no concern to the ordinary user. Most
  119. variables are allowed to have the default type, called {\em scalar}.
  120. These can receive, as values, the representation of any ordinary algebraic
  121. expression. In the absence of such a value, they stand for themselves.
  122. \subsection*{Reserved Variables}
  123. Several variables\index{Reserved variable} in {\REDUCE} have particular
  124. properties which should not be changed by the user. These variables
  125. include:
  126. \begin{list}{}{\renewcommand{\makelabel}[1]{{\tt#1}\hspace{\fill}}%
  127. \settowidth{\labelwidth}{\tt INFINITY}%
  128. \setlength{\labelsep}{1em}%
  129. \settowidth{\leftmargin}{\tt INFINITY\hspace*{\labelsep}}}
  130. \item[E] Intended to represent the base of
  131. \ttindex{E}
  132. the natural logarithms. {\tt log(e)}, if it occurs in an expression, is
  133. automatically replaced by 1. If {\tt ROUNDED}\ttindex{ROUNDED} is
  134. on, {\tt E} is replaced by the value of E to the current degree of
  135. floating point precision\index{Numerical precision}.
  136. \item[I] Intended to represent the square
  137. \ttindex{I}
  138. root of $-1$. {\tt i\verb|^|2} is replaced by $-1$, and appropriately for higher
  139. powers of {\tt I}. This applies only to the symbol {\tt I} used on the top
  140. level, not as a formal parameter in a procedure, a local variable, nor in
  141. the context {\tt for i:= ...}
  142. \item[INFINITY] Intended to represent $\infty$
  143. \ttindex{INFINITY}
  144. in limit and power series calculations for example. Note however that the
  145. current system does {\em not\/} do proper arithmetic on $\infty$. For example,
  146. {\tt infinity + infinity} is {\tt 2*infinity}.
  147. \item[NIL] In {\REDUCE} (algebraic mode only)
  148. taken as a synonym for zero. Therefore {\tt NIL} cannot be used as a
  149. variable.
  150. \item[PI] Intended to represent the circular
  151. \ttindex{PI}
  152. constant. With {\tt ROUNDED} on, it is replaced by the value of $\pi$ to
  153. the current degree of floating point precision.
  154. \item[T] Should not be used as a formal
  155. \ttindex{T}
  156. parameter or local variable in procedures, since conflict arises with the
  157. symbolic mode meaning of T as {\em true}.
  158. \end{list}
  159. Other reserved variables, such as {\tt LOW\_POW}, described in other sections,
  160. are listed in Appendix A.
  161. Using these reserved variables\index{Reserved variable} inappropriately
  162. will lead to errors.
  163. There are also internal variables used by {\REDUCE} that have similar
  164. restrictions. These usually have an asterisk in their names, so it is
  165. unlikely a casual user would use one. An example of such a variable is
  166. {\tt K!*} used in the asymptotic command package.
  167. Certain words are reserved in {\REDUCE}. They may only be used in the manner
  168. intended. A list of these is given in the section ``Reserved Identifiers''.
  169. There are, of course, an impossibly large number of such names to keep in
  170. mind. The reader may therefore want to make himself a copy of the list,
  171. deleting the names he doesn't think he is likely to use by mistake.
  172. \section{Strings}
  173. Strings\index{String} are used in {\tt WRITE} statements, in other
  174. output statements (such as error messages), and to name files. A string
  175. consists of any number of characters enclosed in double quotes. For example:
  176. \begin{verbatim}
  177. "A String".
  178. \end{verbatim}
  179. Lower case characters within a string are not converted to upper case.
  180. The string {\tt ""} represents the empty string. A double quote may be
  181. included in a string by preceding it by another double quote. Thus
  182. {\tt "a""b"} is the string {\tt a"b}, and {\tt """"} is the string {\tt "}.
  183. \section{Comments}
  184. Text can be included in program\index{Program} listings for the
  185. convenience of human readers, in such a way that {\REDUCE} pays no
  186. attention to it. There are two ways to do this:
  187. \begin{enumerate}
  188. \item Everything from the word {\tt COMMENT}\ttindex{COMMENT} to the next
  189. statement terminator, normally ; or \$, is ignored. Such comments
  190. can be placed anywhere a blank could properly appear. (Note that {\tt END}
  191. and $>>$ are {\em not\/} treated as {\tt COMMENT} delimiters!)
  192. \item Everything from the symbol {\tt \%}\index{Percent sign} to the end
  193. of the line on which it appears is ignored. Such comments can be placed
  194. as the last part of any line. Statement terminators have no special
  195. meaning in such comments. Remember to put a semicolon before the {\tt \%}
  196. if the earlier part of the line is intended to be so terminated. Remember
  197. also to begin each line of a multi-line {\tt \%} comment with a {\tt \%}
  198. sign.
  199. \end{enumerate}
  200. \section{Operators}
  201. \label{sec-operators}
  202. Operators\index{Operator} in {\REDUCE} are specified by name and type.
  203. There are two types, infix\index{Infix operator} and prefix.
  204. \index{Prefix operator} Operators can be purely abstract, just symbols
  205. with no properties; they can have values assigned (using {\tt :=} or
  206. simple {\tt LET} declarations) for specific arguments; they can have
  207. properties declared for some collection of arguments (using more general
  208. {\tt LET} declarations); or they can be fully defined (usually by a
  209. procedure declaration).
  210. Infix operators\index{Infix operator} have a definite precedence with
  211. respect to one another, and normally occur between their arguments.
  212. For example:
  213. \begin{quote}
  214. \begin{tabbing}
  215. {\tt a + b - c} \hspace{1.5in} \= (spaces optional) \\
  216. {\tt x<y and y=z} \> (spaces required where shown)
  217. \end{tabbing}
  218. \end{quote}
  219. Spaces can be freely inserted between operators and variables or operators
  220. and operators. They are required only where operator names are spelled out
  221. with letters (such as the {\tt AND} in the example) and must be unambiguously
  222. separated from another such or from a variable (like {\tt Y}). Wherever one
  223. space can be used, so can any larger number.
  224. Prefix operators occur to the left of their arguments, which are written as
  225. a list enclosed in parentheses and separated by commas, as with normal
  226. mathematical functions, e.g.,
  227. \begin{verbatim}
  228. cos(u)
  229. df(x^2,x)
  230. q(v+w)
  231. \end{verbatim}
  232. Unmatched parentheses, incorrect groupings of infix operators
  233. \index{Infix operator} and the like, naturally lead to syntax errors. The
  234. parentheses can be omitted (replaced by a space following the
  235. operator\index{Operator} name) if the operator is unary and the argument
  236. is a single symbol or begins with a prefix operator name:
  237. \begin{quote}
  238. \begin{tabbing}
  239. {\tt cos y} \hspace{1.75in} \= means cos(y) \\
  240. {\tt cos (-y)} \> -- parentheses necessary \\
  241. {\tt log cos y} \> means log(cos(y)) \\
  242. {\tt log cos (a+b)} \> means log(cos(a+b))
  243. \end{tabbing}
  244. \end{quote}
  245. but
  246. \begin{quote}
  247. \begin{tabbing}
  248. {\tt cos a*b} \hspace{1.6in} \= means (cos a)*b \\
  249. {\tt cos -y} \> is erroneous (treated as a variable \\
  250. \> ``cos'' minus the variable y)
  251. \end{tabbing}
  252. \end{quote}
  253. A unary prefix operator\index{Prefix operator} has a precedence
  254. \index{Operator precedence} higher than any infix operator, including
  255. unary infix operators. \index{Infix operator}
  256. In other words, {\REDUCE} will always interpret {\tt cos~y + 3} as
  257. {\tt (cos~y) + 3} rather than as {\tt cos(y + 3)}.
  258. Infix operators may also be used in a prefix format on input, e.g.,
  259. {\tt +(a,b,c)}. On output, however, such expressions will always be
  260. printed in infix form (i.e., {\tt a + b + c} for this example).
  261. A number of prefix operators are built into the system with predefined
  262. properties. Users may also add new operators and define their rules for
  263. simplification. The built in operators are described in another section.
  264. \subsection*{Built-In Infix Operators}
  265. The following infix operators\index{Infix operator} are built into the
  266. system. They are all defined internally as procedures.
  267. \begin{verbatim}
  268. <infix operator>::= where|:=|or|and|member|memq|=|neq|eq|
  269. >=|>|<=|<|+|-|*|/|^|**|.
  270. \end{verbatim}
  271. These operators may be further divided into the following subclasses:
  272. \begin{verbatim}
  273. <assignment operator> ::= :=
  274. <logical operator> ::= or|and|member|memq
  275. <relational operator> ::= =|neq|eq|>=|>|<=|<
  276. <substitution operator> ::= where
  277. <arithmetic operator> ::= +|-|*|/|^|**
  278. <construction operator> ::= .
  279. \end{verbatim}
  280. {\tt MEMQ} and {\tt EQ} are not used in the algebraic mode of
  281. {\REDUCE}. They are explained in the section on symbolic mode.
  282. {\tt WHERE} is described in the section on substitutions.
  283. In previous versions of {\REDUCE}, {\em not} was also defined as an infix
  284. operator. In the present version it is a regular prefix operator, and
  285. interchangeable with {\em null}.
  286. For compatibility with the intermediate language used by {\REDUCE}, each
  287. special character infix operator\index{Infix operator} has an alternative
  288. alphanumeric identifier associated with it. These identifiers may be used
  289. interchangeably with the corresponding special character names on input.
  290. This correspondence is as follows:
  291. \begin{quote}
  292. \begin{tabbing}
  293. {\tt := setq} \hspace{0.5in} \= (the assignment operator) \\
  294. {\tt = equal} \\
  295. {\tt >= geq} \\
  296. {\tt > greaterp} \\
  297. {\tt <= leq} \\
  298. {\tt < lessp} \\
  299. {\tt + plus} \\
  300. {\tt - difference} \> (if unary, {\tt minus}) \\
  301. {\tt * times} \\
  302. {\tt / quotient} \> (if unary, {\tt recip}) \\
  303. {\tt \verb|^| or ** expt} \> (raising to a power) \\
  304. {\tt . cons}
  305. \end{tabbing}
  306. \end{quote}
  307. Note: {\tt NEQ} is used to mean {\em not equal}. There is no special
  308. symbol provided for it.
  309. The above operators\index{Operator} are binary, except {\tt NOT} which is
  310. unary and {\tt +} and {\tt *} which are nary (i.e., taking an arbitrary
  311. number of arguments). In addition, {\tt -} and {\tt /} may be used as
  312. unary operators, e.g., /2 means the same as 1/2. Any other operator is
  313. parsed as a binary operator using a left association rule. Thus {\tt
  314. a/b/c} is interpreted as {\tt (a/b)/c}. There are two exceptions to this
  315. rule: {\tt :=} and {\tt .} are right associative. Example: {\tt a:=b:=c}
  316. is interpreted as {\tt a:=(b:=c)}. Unlike ALGOL and PASCAL, {\tt \verb|^|} is
  317. left associative. In other words, {\tt a\verb|^|b\verb|^|c} is interpreted as
  318. {\tt (a\verb|^|b)\verb|^|c}.
  319. The operators\index{Operator} {\tt $<$}, {\tt $<$=}, {\tt $>$}, {\tt $>$=}
  320. can only be used for making comparisons between numbers. No meaning is
  321. currently assigned to this kind of comparison between general expressions.
  322. Parentheses may be used to specify the order of combination. If
  323. parentheses are omitted then this order is by the ordering of the
  324. precedence list\index{Operator precedence} defined by the right-hand side
  325. of the {\tt <infix operator>}\index{Infix operator} table
  326. at the beginning of this section,
  327. from lowest to highest. In other words, {\tt WHERE} has the lowest
  328. precedence, and {\tt .} (the dot operator) the highest.