rtrace.tex 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. \documentclass[11pt,a4paper]{article}
  2. \usepackage{reduce}
  3. \newcommand{\til}{\symbol{'176}} % tilde character
  4. \newcommand{\rdebug}{\texttt{rdebug}}
  5. \newcommand{\rtrace}{\texttt{rtrace}}
  6. \title{Tracing in \REDUCE{}}
  7. \author{
  8. Herbert Melenk \\[0.05in]
  9. Konrad--Zuse--Zentrum \\
  10. f\"ur Informationstechnik Berlin \\
  11. % Heilbronner Strasse 10 \\
  12. % D--10711 Berlin Wilmersdorf \\
  13. Takustra\ss{}e 7 \\
  14. D--14195 Berlin--Dahlem \\
  15. Federal Republic of Germany \\[0.05in]
  16. E-mail: \texttt{melenk@zib.de} \\
  17. \and
  18. Francis J. Wright \\[0.05in]
  19. School of Mathematical Sciences \\
  20. Queen Mary and Westfield College, University of London \\
  21. Mile End Road, London E1 4NS, UK. \\[0.05in]
  22. E-mail: \texttt{F.J.Wright@QMW.ac.uk} \\
  23. \texttt{http://www.maths.qmw.ac.uk/\til fjw/}}
  24. \date{14 March 1999}
  25. \begin{document}
  26. \maketitle
  27. \section{Introduction}
  28. The package \rtrace{} provides portable tracing facilities for
  29. \REDUCE{} programming. These include
  30. \begin{itemize}
  31. \item entry-exit tracing of procedures,
  32. \item assignment tracing of procedures,
  33. %\item setting of break points,
  34. %\item conditional trace and break.
  35. \item tracing of rules when they fire.
  36. \end{itemize}
  37. In contrast to conventional Lisp-level tracing, values are printed in
  38. algebraic style whenever possible if the switch \rtrace{} is on, which
  39. it is by default. The output has been specially tailored for the
  40. needs of algebraic-mode programming. Most features can be applied
  41. without explicitly modifying the target program, and they can be
  42. turned on and off dynamically at run time. If the switch \rtrace{} is
  43. turned off then values are printed in conventional Lisp style, and the
  44. result should be similar to the tracing provided by the underlying
  45. Lisp system.
  46. To make the facilities available, load the package using the command
  47. \begin{verbatim}
  48. load_package rtrace;
  49. \end{verbatim}
  50. Alternatively, the package can be set up to auto load by putting
  51. appropriate code in your \REDUCE{} initialisation file. An example is
  52. provided in the file \texttt{reduce.rc} in the \rtrace{} source
  53. directory.
  54. \section{RTrace versus RDebug}
  55. The \rtrace{} package is a modification (by FJW) of the \rdebug{}
  56. package (written by HM, and included in the \rtrace{} source
  57. directory). The modifications are as follows. The procedure-tracing
  58. facilities in \rdebug{} rely upon the low-level tracing facilities in
  59. PSL; in \rtrace{} these low-level facilities have been (partly)
  60. re-implemented portably. The names of the tracing commands that have
  61. been re-implemented portably have been changed to avoid conflicting
  62. with those provided by the underlying Lisp system by preceding them
  63. with the letter ``r'', and they provide a generalized interface that
  64. supports algebraic mode better. An additional set of rule tracing
  65. facilities for inactive rules has been provided. Beware that the
  66. \rtrace{} package is still experimental!
  67. This package is intended to be portable, and has been tested with both
  68. CSL- and PSL-based \REDUCE{}. However, it is intended not as a
  69. replacement for \rdebug{} but as a partial re-implementation of
  70. \rdebug{} that works with CSL-\REDUCE{}, and it is assumed that PSL
  71. users will continue to use \rdebug{}. It should, in principle, be
  72. possible to use both. Any \rtrace{} functions with the same names as
  73. \rdebug{} functions should either be identical or compatible;
  74. \rtrace{} should be loaded after \rdebug{} in order to retain any
  75. enhancements provided by \rtrace{}. Perhaps at some future time the
  76. two packages should be merged. However, note that \rtrace{} currently
  77. provides \emph{only tracing} (hence the name) and does not support
  78. break points. (The current version also does not support conditional
  79. tracing.)
  80. \section{Procedure tracing: RTR, UNRTR}
  81. Tracing of one or more procedures is initiated by the command
  82. \texttt{rtr}:
  83. \begin{verbatim}
  84. rtr <proc1>, <proc2>, ..., <procn>;
  85. \end{verbatim}
  86. and cancelled by the command \texttt{unrtr}:
  87. \begin{verbatim}
  88. unrtr <proc1>, <proc2>, ..., <procn>;
  89. \end{verbatim}
  90. Every time a traced procedure is executed, a message is printed when
  91. the procedure is entered or exited. The entry message displays the
  92. actual procedure arguments equated to the dummy parameter names, and
  93. the exit message displays the value returned by the procedure.
  94. Recursive calls are marked by
  95. % an indentation and
  96. a level number. Here is a (simplistic) example, using first the
  97. default algebraic display and second conventional Lisp display:
  98. \begin{verbatim}
  99. algebraic procedure power(x, n);
  100. if n = 0 then 1 else x*power(x, n-1)$
  101. rtr power;
  102. (power)
  103. power(x+1, 2);
  104. Enter (1) power
  105. x: x + 1$
  106. n: 2$
  107. Enter (2) power
  108. x: x + 1$
  109. n: 1$
  110. Enter (3) power
  111. x: x + 1$
  112. n: 0$
  113. Leave (3) power = 1$
  114. Leave (2) power = x + 1$
  115. Leave (1) power = x**2 + 2*x + 1$
  116. 2
  117. x + 2*x + 1
  118. off rtrace;
  119. power(x+1, 2);
  120. Enter (1) power
  121. x: (plus x 1)
  122. n: 2
  123. Enter (2) power
  124. x: (plus x 1)
  125. n: 1
  126. Enter (3) power
  127. x: (plus x 1)
  128. n: 0
  129. Leave (3) power = 1
  130. Leave (2) power = (!*sq ((((x . 1) . 1) . 1) . 1) t)
  131. Leave (1) power = (!*sq ((((x . 2) . 1) ((x . 1) . 2) . 1) . 1) t)
  132. 2
  133. x + 2*x + 1
  134. on rtrace;
  135. unrtr power;
  136. (power)
  137. \end{verbatim}
  138. Many algebraic-mode operators are implemented as internal procedures
  139. with different names. If an internal procedure with the specified
  140. name does not exist then \rtrace{} tracing automatically applies to
  141. the appropriate internal procedure and returns a list of the names of
  142. the internal procedures, e.g.
  143. \begin{verbatim}
  144. rtr int;
  145. (simpint)
  146. \end{verbatim}
  147. This facility is an extension of the \rdebug{} package.
  148. Tracing of \emph{compiled} procedures by the \rtrace{} package is not
  149. completely reliable, in that recursive calls may not be traced. This
  150. is essentially because tracing works only when the procedure is called
  151. by name and not when it is called directly via an internal compiled
  152. pointer. It may not be possible to avoid this restriction in a
  153. portable way. Also, arguments of compiled procedures are not
  154. displayed using the names given to them in the source code, because
  155. these names are no longer available. Instead, they are displayed
  156. using the names \texttt{Arg1}, \texttt{Arg2}, etc.
  157. \section{Assignment tracing: RTRST, UNRTRST}
  158. One often needs information about the internal behaviour of a
  159. procedure, especially if it is a longer piece of code. For an
  160. interpreted procedure declared in an \texttt{rtrst} command:
  161. \begin{verbatim}
  162. rtrst <proc1>, <proc2>, ..., <procn>;
  163. \end{verbatim}
  164. all explicit assignments executed (as either the symbolic-mode
  165. \texttt{setq} or the algebraic-mode \texttt{setk})
  166. % and passed labels
  167. inside these procedures are displayed during procedure execution. All
  168. procedure tracing (assignment and entry-exit) is removed by the
  169. command \texttt{unrtrst} (or \texttt{unrtr}, for which it is just a
  170. synonym):
  171. \begin{verbatim}
  172. unrtrst <proc1>, <proc2>, ..., <procn>;
  173. \end{verbatim}
  174. Assignment tracing is not possible if a procedure is compiled, either
  175. because it was loaded from a ``fasl'' file or image, or because it was
  176. compiled as it was read in as source code. This is because assignment
  177. tracing works by modifying the interpreted code of the procedure,
  178. which must therefore be available.
  179. Applying \texttt{rtr} to a procedure that has been declared in an
  180. \texttt{rtrst} command, or vice versa, toggles the type of tracing
  181. applied (and displays an explanatory message).
  182. Note that when a program contains a \texttt{for} loop, \REDUCE{}
  183. translates this to a sequence of Lisp instructions. When using
  184. \texttt{rtrst}, the printout is driven by the ``unfolded'' code. When
  185. the code contains a \texttt{for each \ldots{} in} statement, the name
  186. of the control variable is internally used to keep the remainder of
  187. the list during the loop, and you will see the corresponding
  188. assignments in the trace rather than the individual values in the loop
  189. steps, e.g.
  190. \begin{verbatim}
  191. procedure fold u;
  192. for each x in u sum x$
  193. rtrst fold;
  194. (fold)
  195. fold {z, z*y, y};
  196. \end{verbatim}
  197. produces the following output (using CSL-\REDUCE{}):
  198. \begin{verbatim}
  199. Enter (1) fold
  200. u: {z,y*z,y}$
  201. x := [z,y*z,y]$
  202. G0 := 0$
  203. G0 := z$
  204. x := [y*z,y]$
  205. G0 := z*(y + 1)$
  206. x := [y]$
  207. G0 := y*z + y + z$
  208. x := []$
  209. Leave (1) fold = y*z + y + z$
  210. y*z + y + z
  211. unrtrst fold;
  212. (fold)
  213. \end{verbatim}
  214. In this example, the printed assignments for \texttt{x} show the
  215. various stages of the loop. The variable \texttt{G0} is an internally
  216. generated place-holder for the sum, and may have a slightly different
  217. name depending on the underlying Lisp systems.
  218. \section{Tracing active rules: TRRL, UNTRRL}
  219. The command \texttt{trrl} initiates tracing when they fire of
  220. individual rules or rule lists that have been activated using
  221. \texttt{let}.
  222. \begin{verbatim}
  223. trrl <rl1>, <rl2>, ..., <rln>;
  224. \end{verbatim}
  225. where each of the $<rl_i>$ is:
  226. \begin{itemize}
  227. \item a rule or rule list;
  228. \item the name of a rule or rule list (that is, a non-indexed variable
  229. which is bound to a rule or rule list);
  230. \item an operator name, representing the rules assigned to this
  231. operator.
  232. \end{itemize}
  233. The specified rules are (re-) activated in \REDUCE{} such that each of
  234. them prints a report every time it fires. The report is composed of
  235. the name of the rule or the name of the rule list together with the
  236. number of the rule in the list, the form matching the left side
  237. (``input'') and the resulting right side (``output''). For an
  238. explicitly given rule or rule list, \texttt{trrl} assigns a unique
  239. generated name.
  240. Note, however, that \texttt{trrl} does not trace rules with constant
  241. expressions on the left, on the assumption that they are not
  242. particularly interesting. [This behaviour may be made
  243. user-controllable in a future version.]
  244. The command \texttt{untrrl} removes the tracing from rules:
  245. \begin{verbatim}
  246. untrrl <rl1>, <rl2>, ..., <rln>;
  247. \end{verbatim}
  248. where each of the $<rl_i>$ is:
  249. \begin{itemize}
  250. \item a rule or rule list;
  251. \item the name of a rule or rule list (that is, a non-indexed variable
  252. which is bound to a rule or rule list or a unique name generated by
  253. \texttt{trrl});
  254. \item an operator name, representing the rules assigned to this
  255. operator.
  256. \end{itemize}
  257. The rules are reactivated in their original form. Alternatively you
  258. can use the command \texttt{clearrules} to remove the rules totally
  259. from the system. Please do not modify the rules between \texttt{trrl}
  260. and \texttt{untrrl} -- the result may be unpredictable.
  261. Here are two simple examples that show tracing via the rule name and
  262. via the operator name:
  263. \begin{verbatim}
  264. trigrules := {sin(~x)^2 => 1 - cos(x)^2};
  265. 2 2
  266. trigrules := {sin(~x) => 1 - cos(x) }
  267. let trigrules;
  268. trrl trigrules;
  269. 1 - sin(x)^2;
  270. Rule trigrules.1: sin(x)**2 => 1 - cos(x)**2$
  271. 2
  272. cos(x)
  273. untrrl trigrules;
  274. trrl sin;
  275. 1 - sin(x)^2;
  276. Rule sin.23: sin(x)**2 => 1 - cos(x)**2$
  277. 2
  278. cos(x)
  279. untrrl sin;
  280. clearrules trigrules;
  281. \end{verbatim}
  282. \section{Tracing inactive rules: TRRLID, UNTRRLID}
  283. The command \texttt{trrlid} initiates tracing of individual rule lists
  284. that have been assigned to variables, but have not been activated
  285. using \texttt{let}:
  286. \begin{verbatim}
  287. trrlid <rlid1>, <rlid2>, ..., <rlidn>;
  288. \end{verbatim}
  289. where each of the $<rlid_i>$ is an identifier of a rule list (that is,
  290. a non-indexed variable which is bound to a rule list). It is assumed
  291. that they will be activated later, either via a \texttt{let} command
  292. or by using the \texttt{where} operator. When they are activated and
  293. fire, tracing output will be as if they had been traced using
  294. \texttt{trrl}. The command \texttt{untrrlid} clears the tracing.
  295. This facility is an extension of the \rdebug{} package.
  296. Here is a simple example that continues the example above:
  297. \begin{verbatim}
  298. trrlid trigrules;
  299. 1 - sin(x)^2 where trigrules;
  300. Rule trigrules.1: sin(x)**2 => 1 - cos(x)**2$
  301. 2
  302. cos(x)
  303. untrrlid trigrules;
  304. \end{verbatim}
  305. \section{Output control: RTROUT}
  306. The trace output (only) can be redirected to a separate file by using
  307. the command \texttt{rtrout}, followed by a file name in string quotes.
  308. A second call of \texttt{rtrout} closes any current output file and
  309. opens a new one. The file name \texttt{NIL} (without string quotes)
  310. closes any current output file and causes the trace output to be
  311. redirected to the standard output device.
  312. The \rdebug{} variables \texttt{trlimit} and \texttt{trprinter!*} are
  313. not implemented in \rtrace{}. If you want to select Lisp-style
  314. tracing then turn off the switch \rtrace{}:
  315. \begin{verbatim}
  316. off rtrace;
  317. \end{verbatim}
  318. after loading the \rtrace{} package. Note that the \rtrace{} switch
  319. controls the display format of both procedure and rule tracing.
  320. \end{document}