command.tex 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. \chapter{Commands and Declarations}
  2. A command\index{Command} is an order to the system to do something. Some
  3. commands cause visible results (such as calling for input or output);
  4. others, usually called declarations\index{Declaration}, set options,
  5. define properties of variables, or define procedures. Commands are
  6. formally defined as a statement followed by a terminator
  7. \begin{verbatim}
  8. <command> ::= <statement> <terminator>
  9. <terminator> ::= ;|$
  10. \end{verbatim}
  11. Some {\REDUCE} commands and declarations are described in the following
  12. sub-sections.
  13. \section{Array Declarations}
  14. Array\ttindex{ARRAY} declarations in {\REDUCE} are similar to FORTRAN
  15. dimension statements. For example:
  16. \begin{verbatim}
  17. array a(10),b(2,3,4);
  18. \end{verbatim}
  19. Array indices each range from 0 to the value declared. An element of an
  20. array is referred to in standard FORTRAN notation, e.g. {\tt A(2)}.
  21. We can also use an expression for defining an array bound, provided the
  22. value of the expression is a positive integer. For example, if {\tt X} has the
  23. value 10 and {\tt Y} the value 7 then
  24. {\tt array c(5*x+y)} is the same as {\tt array c(57)}.
  25. If an array is referenced by an index outside its range, an error occurs.
  26. If the array is to be one-dimensional, and the bound a number or a variable
  27. (not a more general expression) the parentheses may be omitted:
  28. \begin{verbatim}
  29. array a 10, c 57;
  30. \end{verbatim}
  31. The operator {\tt LENGTH}\ttindex{LENGTH} applied to an array name
  32. returns a list of its dimensions.
  33. All array elements are initialized to 0 at declaration time. In other words,
  34. an array element has an {\em instant evaluation\/}\index{Instant evaluation}
  35. property and cannot stand for itself. If this is required, then an
  36. operator should be used instead.
  37. Array declarations can appear anywhere in a program. Once a symbol is
  38. declared to name an array, it can not also be used as a variable, or to
  39. name an operator or a procedure. It can however be re-declared to be an
  40. array, and its size may be changed at that time. An array name can also
  41. continue to be used as a parameter in a procedure, or a local variable in
  42. a compound statement, although this use is not recommended, since it can
  43. lead to user confusion over the type of the variable.
  44. Arrays once declared are global in scope, and so can then be referenced
  45. anywhere in the program. In other words, unlike arrays in most other
  46. languages, a declaration within a block (or a procedure) does not limit
  47. the scope of the array to that block, nor does the array go away on
  48. exiting the block (use {\tt CLEAR} instead for this purpose).
  49. \section{Mode Handling Declarations}\index{Mode}
  50. The {\tt ON}\ttindex{ON} and {\tt OFF}\ttindex{OFF} declarations are
  51. available to the user for controlling various system options. Each option
  52. is represented by a {\em switch\/}\index{Switch} name. {\tt ON} and {\tt OFF}
  53. take a list of switch names as argument and turn them on and off
  54. respectively, e.g.,
  55. \begin{verbatim}
  56. on time;
  57. \end{verbatim}
  58. causes the system to print a message after each command giving the elapsed
  59. CPU time since the last command, or since {\tt TIME}\ttindex{TIME} was
  60. last turned off, or the session began. Another useful switch with
  61. interactive use is {\tt DEMO},\ttindex{DEMO} which causes the system to
  62. pause after each command in a file (with the exception of comments)
  63. until a \key{Return} is typed on the terminal. This
  64. enables a user to set up a demonstration file and step through it command
  65. by command.
  66. As with most declarations, arguments to {\tt ON} and {\tt OFF} may be
  67. strung together separated by commas. For example,
  68. \begin{verbatim}
  69. off time,demo;
  70. \end{verbatim}
  71. will turn off both the time messages and the demonstration switch.
  72. We note here that while most {\tt ON} and {\tt OFF} commands are obeyed
  73. almost instantaneously, some trigger time-consuming actions such as
  74. reading in necessary modules from secondary storage.
  75. A diagnostic message is printed if {\tt ON}\ttindex{ON} or {\tt OFF}
  76. \ttindex{OFF} are used with a switch that is not known to the system. For
  77. example, if you misspell {\tt DEMO} and type
  78. \begin{verbatim}
  79. on demq;
  80. \end{verbatim}
  81. you will get the message\index{Switch}
  82. \begin{verbatim}
  83. ***** DEMQ not defined as switch.
  84. \end{verbatim}
  85. \section{END}
  86. The identifier {\tt END}\ttindex{END} has two separate uses.
  87. 1) Its use in a {\tt BEGIN \ldots END} bracket has been discussed in
  88. connection with compound statements.
  89. 2) Files to be read using {\tt IN} should end with an extra {\tt END};
  90. command. The reason for this is explained in the section on the {\tt IN}
  91. command. This use of {\tt END} does not allow an immediately
  92. preceding {\tt END} (such as the {\tt END} of a procedure definition), so
  93. we advise using {\tt ;END;} there.
  94. %3) A command {\tt END}; entered at the top level transfers control to the
  95. %Lisp system\index{Lisp} which is the host of the {\REDUCE} system. All
  96. %files opened by {\tt IN} or {\tt OUT} statements are closed in the
  97. %process. {\tt END;} does not stop {\REDUCE}. Those familiar with Lisp can
  98. %experiment with typing identifiers and ({\tt <function name> <argument
  99. %list>}) lists to see the value returned by Lisp. (No terminators, other
  100. %than the RETURN key, should be used.) The data structures created during
  101. %the {\REDUCE} run are accessible.
  102. %You remain in this Lisp mode until you explicitly re-enter {\REDUCE} by
  103. %saying {\tt (BEGIN)} at the Lisp top level. In most systems, a Lisp error
  104. %also returns you to {\REDUCE} (exceptions are noted in the operating
  105. %instructions for your particular {\REDUCE} implementation). In either
  106. %case, you will return to {\REDUCE} in the same mode, algebraic or
  107. %symbolic, that you were in before the {\tt END};. If you are in
  108. %Lisp mode\index{Lisp mode} by mistake -- which is usually the case,
  109. %the result of typing more {\tt END}s\ttindex{END} than {\tt BEGIN}s --
  110. %type {\tt (BEGIN)} in parentheses and hit the RETURN key.
  111. \section{BYE Command}\ttindex{BYE}
  112. The command {\tt BYE}; (or alternatively {\tt QUIT};)\ttindex{QUIT}
  113. stops the execution
  114. of {\REDUCE}, closes all open output files, and returns you to the calling
  115. program (usually the operating system). Your {\REDUCE} session is
  116. normally destroyed.
  117. \section{SHOWTIME Command}\ttindex{SHOWTIME}
  118. {\tt SHOWTIME}; prints the elapsed time since the last call of this
  119. command or, on its first call, since the current {\REDUCE} session began.
  120. The time is normally given in milliseconds and gives the time as measured
  121. by a system clock. The operations covered by this measure are system
  122. dependent.
  123. \section{DEFINE Command}
  124. The command {\tt DEFINE}\ttindex{DEFINE} allows a user to supply a new name for
  125. any identifier or replace it by any well-formed expression. Its argument
  126. is a list of expressions of the form
  127. \begin{verbatim}
  128. <identifier> = <number>|<identifier>|<operator>|
  129. <reserved word>|<expression>
  130. \end{verbatim}
  131. {\it Example:}
  132. \begin{verbatim}
  133. define be==,x=y+z;
  134. \end{verbatim}
  135. means that {\tt BE} will be interpreted as an equal sign, and {\tt X}
  136. as the expression {\tt y+z} from then on. This renaming is done at parse
  137. time, and therefore takes precedence over any other replacement declared
  138. for the same identifier. It stays in effect until the end of the
  139. {\REDUCE} run.
  140. The identifiers {\tt ALGEBRAIC} and {\tt SYMBOLIC} have properties which
  141. prevent {\tt DEFINE}\ttindex{DEFINE} from being used on them. To define
  142. {\tt ALG} to be a synonym for {\tt ALGEBRAIC}, use the more complicated
  143. construction
  144. \begin{verbatim}
  145. put('alg,'newnam,'algebraic);
  146. \end{verbatim}