1
2\chapter{Modules}
3\label{modules}
4
5Modules\index{module} are the basic execution\index{execution} blocks.
6Statements\index{statements} are always part of a module, and they will be
7executed only when the module is executed. This is directly opposite to
8preprocessor instructions which are executed when they are encountered in
9the input stream.
10
11Modules are terminated by a line that starts with a period\index{period}.
12Such a line is called the module\index{module instruction} instruction.
13Once the module instruction has been recognized, the compilation of the
14module is terminated and the module will be executed. All active
15expressions will be processed one by one, term by term. When each term of
16an expression has been through all statements of the module, the combined
17results of all operations on all the terms of the expression will be sorted
18and the resulting expression will be sent to the output. This can be an
19intermediate file\index{file!intermediate}, or it can be some
20memory\index{memory}, depending on the size of the output. If the combined
21output of all active expressions is less than the parameter
22``ScratchSize''\index{ScratchSize}, the results stay in memory. ScratchSize
23is one of the setup parameters (see chapter \ref{setup}).
24
25A module consists in general of several types of statements:
26\begin{description}
27\item [Declarations\index{declarations}] These are the declarations of
28variables.
29\item [Specifications\index{specifications}] These tell what to do with
30existing expressions as a whole.
31\item [Definitions\index{definitions}] These define new expressions.
32\item [Executable\index{executable statements} statements] The operations
33on all active expressions.
34\item [OutputSpecifications\index{output specifications}] These specify the
35output representation.
36\item [End-of-module specifications\index{end of module specifications}]
37Extra settings that are for this module only.
38\item [Mixed statements\index{mixed statements}] They can occur in various
39classes. Most notably the print statement.
40\end{description}
41Statements must occur in such an order that no statement follows a
42statement of a later category. The only exception is formed by the mixed
43statements, which can occur anywhere. This is different from earlier
44versions of \FORM\ in which the order of the statements was not fixed. This
45did cause a certain amount of confusion about the workings of \FORM.
46
47There are several types of modules.
48\begin{description}
49\item[.sort\index{.sort}] \label{instrsort} The general end-of-module.
50Causes execution of all active expressions, and prepares them for the next
51module.
52\item[.end\index{.end}] \label{instrend} Executes all active expressions
53and terminates the program.
54\item[.store\index{.store}] \label{instrstore} Executes all active
55expressions. Then it writes all active global expressions to an
56intermediate storage file\index{file!storage} and removes all other
57non-global expressions. Removes all memory of declarations except for those
58that were made before a .global instruction.
59\item[.global\index{.global}] \label{instrglobal} No execution of
60expressions. It just saves declarations made thus far from being erased by
61a .store instruction.
62\item[.clear\index{.clear}] \label{instrclear} Executes all active
63expressions. Then it clears all buffers with the exception of the main
64input stream. Continues execution in the main input stream as if the
65program had started at this point. The only parameters that cannot be
66changed at this point are the setup parameters. They remain. By default
67also the clock\index{clock} is reset. If this is not desired this can be
68changed by means of the ResetTimeOnClear\index{resettimeonclear} setup
69variable (see chapter \ref{setup}).
70\end{description}
71Each program must be terminated by a .end instruction. If such an
72instruction is absent and \FORM\ encounters an end-of-input it will issue a
73warning and generate a .end instruction.
74
75Module instructions can contain a special commentary that will be printed
76in all statistics that are generated during the execution of the module.
77This special commentary is restricted to 24 characters (the statistics have
78a fixed format and hence there is only a limited amount of space
79available). This commentary is initiated by a colon and terminated by a
80semicolon. The characters between this colon and the semicolon are the
81special message, also called advertisement. Example
82\begin{verbatim}
83	.sort:Eliminate x;
84\end{verbatim}
85would give in the statistics something like
86\begin{verbatim}
87Time =       0.46 sec    Generated terms =        360
88                F        Terms in output =        360
89            Eliminate x  Bytes used      =       4506
90\end{verbatim}
91If the statistics are switched off, there will be no printing of this
92advertisement either.
93
94For backwards compatibility there is still an obsolete\index{obsolete}
95mechanism to pass module options via the module instructions. This is a
96feature which will probably disappear in future versions of \FORM. We do
97give the syntax to allow the user to identify the option properly and
98enable proper translation into the moduleoption\index{moduleoption}
99statement (see \ref{substamoduleoption}).
100\begin{verbatim}
101    .sort(PolyFun=functionname);
102    .sort(PolyFun=functionname):advertisement;
103\end{verbatim}
104causes the given function to be treated as a polynomial\index{polyfun}
105function. This means that its (single) argument would be treated as the
106coefficient of the terms. The action of \FORM\ on individual terms is
107\begin{enumerate}
108\item Ignore polynomial functions with more than one argument.
109\item If there is no polynomial function with a single argument, generate
110one with the argument 1.\item If there is more than one polynomial function
111with a single argument, multiply the arguments and replace these functions
112with a single polynomial function with the product of the arguments for a
113single argument.
114\item Multiply the argument of the polynomial function with the coefficient
115of the term. Replace the coefficient itself by one.
116\end{enumerate}
117If, after this, two terms differ only in the argument of their polynomial
118function \FORM\ will add the arguments and replace the two terms by a single
119term which is identical to the two previous terms except for that the
120argument of its polynomial function is the sum of their two arguments.
121
122It should be noted that the proper placement of .sort\index{.sort}
123instructions in a \FORM\ program is an art by itself. Too many .sort
124instructions cause too much sorting, which can slow execution down
125considerably. It can also cause the writing of intermediate expressions
126which are much larger than necessary, if the next statements would cause
127great simplifications. Not enough .sort instructions can make that
128cancellations are postponed unnecessarily and hence much work will be done
129double. This can slow down execution by a big factor. First an example of a
130superfluous .sort:
131\begin{verbatim}
132    S	a1,...,a7;
133    L	F = (a1+...+a7)^16;
134    .sort
135
136Time =      31.98 sec    Generated terms =      74613
137                F        Terms in output =      74613
138                         Bytes used      =    1904316
139    id	a7 = a1+a2+a3;
140    .end
141
142Time =     290.34 sec
143                F        Terms active    =      87027
144                         Bytes used      =    2253572
145
146Time =     295.20 sec    Generated terms =     735471
147                F        Terms in output =      20349
148                         Bytes used      =     538884
149\end{verbatim}
150Without the sort the same program gives:
151\begin{verbatim}
152    S	a1,...,a7;
153    L	F = (a1+...+a7)^16;
154    id	a7 = a1+a2+a3;
155    .end
156
157Time =     262.79 sec
158                F        Terms active    =      94372
159                         Bytes used      =    2643640
160
161Time =     267.81 sec    Generated terms =     735471
162                F        Terms in output =      20349
163                         Bytes used      =     538884
164\end{verbatim}
165and we see that the sorting in the beginning is nearly completely wasted.
166Now a clear example of not enough .sort instructions. A common problem is
167the substitution of one power\index{power series} series into another. If
168one does this in one step one could have:
169\begin{verbatim}
170    #define MAX "36"
171    S  j,x(:`MAX'),y(:`MAX');
172    *
173    *	Power series expansion of ln_(1+x)
174    *
175    L	F = -sum_(j,1,`MAX',sign_(j)*x^j/j);
176    *
177    *	Substitute the expansion of x = exp_(y)-1
178    *
179    id	x = x*y;
180    #do j = 2,`MAX'+1
181    id	x = 1+x*y/`j';
182    #enddo
183    Print;
184    .end
185
186Time =      76.84 sec    Generated terms =      99132
187                F        Terms in output =          1
188                         Bytes used      =         18
189
190   F =
191      y;
192\end{verbatim}
193With an extra .sort inside the loop one obtains for the same program (after
194suppressing some of the statistics:
195\begin{verbatim}
196    #define MAX "36"
197    S  j,x(:`MAX'),y(:`MAX');
198    *
199    *	Power series expansion of ln_(1+x)
200    *
201    L	F = -sum_(j,1,`MAX',sign_(j)*x^j/j);
202    *
203    *	Substitute the expansion of x = exp_(y)-1
204    *
205    id	x = x*y;
206    #do j = 2,`MAX'+1
207    id	x = 1+x*y/`j';
208    .sort: step `j';
209
210Time =       0.46 sec    Generated terms =        360
211                F        Terms in output =        360
212                 step 2  Bytes used      =       4506
213    #enddo
214           .
215           .
216           .
217Time =       3.07 sec    Generated terms =          3
218                F        Terms in output =          1
219                step 37  Bytes used      =         18
220    Print;
221    .end
222
223Time =       3.07 sec    Generated terms =          1
224                F        Terms in output =          1
225                         Bytes used      =         18
226
227   F =
228      y;
229\end{verbatim}
230It is very hard to give general rules that are more specific than what has
231been said above. The user should experiment with the placements of the .sort
232before making a very large run.
233
234\section{Checkpoints}
235\label{checkpoints}
236
237If\index{checkpoints} \FORM\ programs have to run for a long time, the
238reliability of the hardware(computer system or network) or of the software
239infrastructure becomes a critical issue. Program
240termination\index{termination} due to unforeseen failures may waste days or
241weeks of invested execution time. The checkpoint mechanism was introduced
242to protect long running \FORM\ programs as good as possible from such
243accidental interruptions. With activated checkpoints \FORM\ will save its
244internal state and data from time to time on the hard disk. This data then
245allows a recovery from a crash\index{crash}.
246
247The checkpoint mechanism can be activated or deactivated by {\tt
248On}\index{on} and {\tt Off}\index{off} statements. If the user has
249activated checkpoints, recovery\index{recovery} data will be written to disk
250at the end of a module execution. Options allow to influence the details of
251the saving mechanism. If a program is terminated during execution, \FORM\ can
252be restarted with the {\tt -R} option and it will continue its execution at
253the last saved recovery point.
254
255The syntax of the checkpoint activation and deactivation is
256\begin{verbatim}
257    On checkpoint [<OPTIONS>];
258    Off checkpoint;
259\end{verbatim}
260
261If no options are given, the recovery data will be saved at the end of every
262module\index{module}. If one gives a time\index{time}
263\begin{verbatim}
264    On checkpoint <NUMBER>[<UNIT>];
265\end{verbatim}
266the saving will only be done if the given time has passed after the last
267saving. Possible unit specifiers are {\tt s, m, h, d} and the number will
268then be interpreted as seconds, minutes, hours, or days, respectively. The
269default unit is seconds.
270
271If one needs to run a script\index{run a script} before or after the saving,
272one can specify a script filename.
273\begin{verbatim}
274    On checkpoint runbefore="<SCRIPTFILENAME>";
275    On checkpoint runafter="<SCRIPTFILENAME>";
276    On checkpoint run="<SCRIPTFILENAME>";
277\end{verbatim}
278The option {\tt run}\index{run} sets both the scripts to be run before and
279after saving.The scripts must have the executable flag set and they must
280reside in the execution path of the shell\index{shell} (unless the filename
281already contains the proper path).
282
283The scripts receive the module number\index{module number} as an argument
284(accessible as \$1 inside the script). The return value of the script
285running before the saving will be interpreted. If the script returns an
286error (non-zero return value), a message will be issued and the saving will
287be skipped.
288
289The recovery data will be written to files named {\tt FORMrecv.*} with
290various name extensions. If a file {\tt FORMrecv.tmp} exists, \FORM\ will not
291run unless one gives it the recovery option\index{recovery option}
292{\tt -R}. This is to prevent the unintentional loss of recovery data. If
293\FORM\ terminates successfully, all the additional data files will be removed.
294
295The additional recovery files will be created in the directory containing
296the scratch files.  The extra files will occupy roughly as much space as
297the scratch files\index{scratch files} and the save\index{save files} and
298hide files\index{hide files} combined. This extra space must be made
299available, of course.
300
301If recovery data exists and \FORM\ is started with the {\tt -R} option, \FORM\
302will continue execution after the last module that successfully wrote the
303recovery data. All the command line parameters that have been given to the
304crashed \FORM\ program\index{crashed \FORM\ program} must also be given to the
305recovering \FORM\ program. The input files are not part of the recovery data
306and will be read in anew when recovering. Therefore it is strongly
307discouraged to change any of these files between saving and recovery.
308