| 1 |
\documentclass{article} |
| 2 |
\usepackage{amsmath} |
| 3 |
\usepackage{amssymb} |
| 4 |
\usepackage{newlfont} |
| 5 |
\usepackage{program} |
| 6 |
\setlength{\parskip}{1ex} |
| 7 |
|
| 8 |
% Number algorithms separately: |
| 9 |
%\newtheorem{algorithm}{Algorithm} |
| 10 |
|
| 11 |
% Number algorithms within sections: |
| 12 |
\newtheorem{algorithm}{Algorithm}[section] |
| 13 |
|
| 14 |
% For "floating" algorithms, use: |
| 15 |
% \begin{figure} |
| 16 |
% \begin{program} |
| 17 |
% ... |
| 18 |
% \end{program} |
| 19 |
% \end{figure} |
| 20 |
|
| 21 |
% To use |foo_bar| in headings, set _ to be active. |
| 22 |
% Not done by default as this clashes with \includegraphics |
| 23 |
% if filenames have _ characters in them: |
| 24 |
|
| 25 |
\catcode`\_\active |
| 26 |
|
| 27 |
\begin{document} |
| 28 |
\title{A demonstration of the {\tt program} environment} |
| 29 |
\author{Martin Ward\\ |
| 30 |
\tt{[email protected]}} |
| 31 |
\maketitle |
| 32 |
|
| 33 |
\tableofcontents |
| 34 |
|
| 35 |
\section{Example with |first_set| and $|first_set|$.} |
| 36 |
|
| 37 |
|
| 38 |
The {\tt program} style defines two environments, {\tt program} and {\tt |
| 39 |
programbox} for typesetting programs and algorithms. Within the program |
| 40 |
environment: |
| 41 |
\begin{enumerate} |
| 42 |
\item Newlines are significant; |
| 43 |
\item Each line is in math mode, so for example spaces in the input |
| 44 |
file are not significant; |
| 45 |
\item The command \verb:\\: within a line causes an extra |
| 46 |
linebreak in the output; |
| 47 |
\item The indentation of each line is calculated automatically; |
| 48 |
\item To cause extra indentation, use the commands \verb:\tab: to |
| 49 |
set a new tab, and \verb:\untab: to remove it (see the examples below); |
| 50 |
\item Vertical bars are used to delimit long variable names with |
| 51 |
underscores (and other unusual characters). |
| 52 |
\end{enumerate} |
| 53 |
|
| 54 |
\verb:testing | in verbatim: |
| 55 |
|
| 56 |
\begin{verbatim} |
| 57 |
testing | and @ in verbatim |
| 58 |
\end{verbatim} |
| 59 |
|
| 60 |
|
| 61 |
Here is a small program: |
| 62 |
\( |first_set|:= \set{x | x^2 + y_1 > 0 } \) |
| 63 |
It shows how to typeset mathematics as part of a program. Since each line is |
| 64 |
typeset in maths mode, all spacing is done automatically. The set brackets |
| 65 |
expand automatically, for example in this program (which also demonstrates the |
| 66 |
\verb:\tab: and \verb:\untab: commands): |
| 67 |
\begin{program} |
| 68 |
t := \set{x | \displaystyle\frac{x}{y} = z}; |
| 69 |
t := t \setminus u; |
| 70 |
z := a \tab {} + b + c + d |
| 71 |
{} + e + f + g |
| 72 |
{} + h + i + j; \untab |
| 73 |
\IF x = 0 \THEN y :=0 \FI |
| 74 |
\end{program} |
| 75 |
|
| 76 |
You can use |variable_names| in text or math mode: $|variable_name|^2 = 2$. |
| 77 |
Names can have |odd_characters:!@#$%^&*:;_like_this!|. |
| 78 |
|
| 79 |
Note that \verb:\(: and \verb:\): are redefined to typeset a program in |
| 80 |
a minipage. (This is useful in running text, or to keep a short program all on |
| 81 |
one page). There is some notation for sequences: \( \seq{x_1,x_2,\dots,x_n} \) |
| 82 |
and for universal and existential quantifiers: $ \Forall x. \Exists y. y>x $ |
| 83 |
(yes, I use these in my programs!) |
| 84 |
|
| 85 |
I often use bold letters to represent program fragments, formulas etc. so |
| 86 |
I have set up commands \S{}, \R{} etc. for the most common ones. The |
| 87 |
commands have one argument (a subscript, eg \S1, \S2, \S{23}) or a |
| 88 |
sequence of ``prime'' characters: \S', \S''''' etc. If you want both a |
| 89 |
subscript and one or more primes, then you must use maths mode, eg $\S2'$ |
| 90 |
Consider the difference between typing \verb:``\S2'': which gives ``\S2'' and |
| 91 |
\verb:``$\S2''$'': which gives ``$\S2''$''. Outside maths mode, \verb:\S: |
| 92 |
assumes any primes after a subscript are either closing quotes or apostrophes. |
| 93 |
|
| 94 |
Here are two program examples with different indentation styles. Note that |
| 95 |
all indentation is calculated automatically in either style: |
| 96 |
|
| 97 |
\noindent |
| 98 |
% programbox keeps the program on one page - like parbox |
| 99 |
\begin{programbox} |
| 100 |
\IF \T1 |
| 101 |
\THEN \IF \T2 |
| 102 |
\THEN \IF \T3 |
| 103 |
\THEN \S4 |
| 104 |
\ELSE \S3 |
| 105 |
\FI |
| 106 |
\ELSE \S2 |
| 107 |
\FI |
| 108 |
\ELSE \S1 |
| 109 |
\FI; |
| 110 |
% Blank lines are ignored, so you need a ~ on the line |
| 111 |
% to get a blank line in the output: |
| 112 |
~ |
| 113 |
\IF \T1 \THEN \IF \T2 \THEN \IF \T3 \THEN \S4 |
| 114 |
\ELSE \S3 \FI |
| 115 |
\ELSE \S2 \FI |
| 116 |
\ELSE \S1 \FI; |
| 117 |
\end{programbox} |
| 118 |
|
| 119 |
Note that \keyword{then} and \keyword{else} should be at the {\em start} of |
| 120 |
a line (as in the examples above), not at the end. This is so that you can |
| 121 |
line them up in short \keyword{if} statements, for example: |
| 122 |
\begin{program} |
| 123 |
\IF x=1 \THEN |a_long_procedure_name|(|arg1|, |arg2|, \dots) |
| 124 |
\ELSE |another_long_procedure_name|(|arg1|, |arg2|, \dots) \FI |
| 125 |
\end{program} |
| 126 |
If the test is long, then you probably want an extra linebreak: |
| 127 |
\begin{program} |
| 128 |
\IF |a_long_boolean_function_name?|(|arg1|, |arg2|, \dots) |
| 129 |
\THEN |a_long_procedure_name|(|arg1|, |arg2|, \dots) |
| 130 |
\ELSE |another_long_procedure_name|(|arg1|, |arg2|, \dots) \FI |
| 131 |
\end{program} |
| 132 |
Compare this with the following (which has linebreaks in the ``wrong'' places): |
| 133 |
\begin{program} |
| 134 |
\IF |a_long_boolean_function_name?|(|arg1|, |arg2|, \dots) \THEN |
| 135 |
|a_long_procedure_name|(|arg1|, |arg2|, \dots) |
| 136 |
\ELSE |
| 137 |
|another_long_procedure_name|(|arg1|, |arg2|, \dots) \FI |
| 138 |
\end{program} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
Just to show that {\tt\origbar} still works normally to indicate the |
| 143 |
placing of vertical lines) in the preamble of a tabular |
| 144 |
(or array) environment: |
| 145 |
|
| 146 |
\begin{tabular}{|r|l|} \hline |
| 147 |
\bf Statement & \bf Conditions \\ |
| 148 |
\hline |
| 149 |
\S1 & \B1 \\ |
| 150 |
\S2 & \B2 \\ |
| 151 |
\hline |
| 152 |
\end{tabular} |
| 153 |
|
| 154 |
\section{Procedures and Functions} |
| 155 |
|
| 156 |
Turning on line numbering here. Also using the algoritm enviroment to number |
| 157 |
the algorithms within the sections. |
| 158 |
\NumberProgramstrue |
| 159 |
\begin{algorithm} |
| 160 |
\begin{program} |
| 161 |
\label{one}% |
| 162 |
\mbox{A fast exponentiation function:} \\ % You can use \mbox for comments |
| 163 |
\BEGIN % |
| 164 |
\FOR i:=1 \TO 10 \STEP 1 \DO |
| 165 |
|print|(|expt|(2,i)); \\ |newline|() \OD |
| 166 |
\WHERE |
| 167 |
\FUNCT |expt|(x,n) \BODY |
| 168 |
\EXP z:=1; |
| 169 |
\WHILE n \neq 0 \DO |
| 170 |
\WHILE |even|(n) \DO |
| 171 |
n:=n/2; x:=x*x \OD; \label{foo} |
| 172 |
n:=n-1; z:=z*x \OD; |
| 173 |
z \ENDEXP \ENDFUNCT |
| 174 |
\END\label{last} |
| 175 |
\end{program} |
| 176 |
\end{algorithm} |
| 177 |
First line is line~\ref{one}, last is line~\ref{last}. |
| 178 |
Line~\ref{foo} is what makes this function fast! |
| 179 |
|
| 180 |
\begin{algorithm} |
| 181 |
\begin{program} |
| 182 |
\mbox{A fast exponentiation procedure:} |
| 183 |
\BEGIN % |
| 184 |
\FOR i:=1 \TO 10 \STEP 1 \DO |
| 185 |
|expt|(2,i); \\ |newline|() \OD % |
| 186 |
\rcomment{This text will be set flush to the right margin} |
| 187 |
\WHERE |
| 188 |
\PROC |expt|(x,n) \BODY |
| 189 |
z:=1; |
| 190 |
\DO \IF n=0 \THEN \EXIT \FI; |
| 191 |
\DO \IF |odd|(n) \THEN \EXIT \FI; |
| 192 |
\COMMENT{This is a comment statement}; |
| 193 |
n:=n/2; x:=x*x \OD; |
| 194 |
\{ n>0 \}; |
| 195 |
n:=n-1; z:=z*x \OD; |
| 196 |
|print|(z) \ENDPROC |
| 197 |
\END |
| 198 |
\end{program} |
| 199 |
\end{algorithm} |
| 200 |
|
| 201 |
\noindent An action system equivalent to a \keyword{while} loop: |
| 202 |
\[ |
| 203 |
\( \ACTIONS A: |
| 204 |
A \EQ \IF \B{} \THEN \S{}; \CALL A |
| 205 |
\ELSE \CALL Z \FI \QE |
| 206 |
\ENDACTIONS \) |
| 207 |
\EQT |
| 208 |
\( \WHILE \B{} \DO \S{} \OD \) |
| 209 |
\] |
| 210 |
Note the use of \verb:\(: and \verb:\): to enclose the two program boxes. |
| 211 |
Turning off line numbers here. |
| 212 |
\NumberProgramsfalse |
| 213 |
|
| 214 |
\noindent Dijkstra conditionals and loops: |
| 215 |
\begin{program} |
| 216 |
\IF x = 1 \AR y:=y+1 |
| 217 |
\BAR x = 2 \AR y:=y^2 |
| 218 |
\utdots |
| 219 |
\BAR x = n \AR y:=\displaystyle\sum_{i=1}^n y_i \FI |
| 220 |
|
| 221 |
\DO 2 \origbar x \AND x>0 \AR x:= x/2 |
| 222 |
\BAR \NOT 2 \origbar x \AR x:= \modbar{x+3} \OD |
| 223 |
\end{program} |
| 224 |
|
| 225 |
\noindent Loops with multiple \keyword{exit}s: |
| 226 |
\begin{program} |
| 227 |
\DO \DO \IF \B1 \THEN \EXIT \FI; |
| 228 |
\S1; |
| 229 |
\IF \B2 \THEN \EXIT(2) \FI \OD; |
| 230 |
\IF \B1 \THEN \EXIT \FI \OD |
| 231 |
\end{program} |
| 232 |
|
| 233 |
\noindent I hope you get the idea! |
| 234 |
|
| 235 |
\section{A Reverse Engineering Example} |
| 236 |
|
| 237 |
Here's the original program: |
| 238 |
\begin{algorithm} |
| 239 |
\begin{program} |
| 240 |
\VAR \seq{m := 0, p := 0, |last| := `` ''}; |
| 241 |
\ACTIONS |prog|: |
| 242 |
|prog| \ACTIONEQ % |
| 243 |
\seq{|line| := `` '', m := 0, i := 1}; |
| 244 |
\CALL |inhere| \ENDACTION |
| 245 |
l \ACTIONEQ % |
| 246 |
i := i+1; |
| 247 |
\IF (i=(n+1)) \THEN \CALL |alldone| \FI ; |
| 248 |
m := 1; |
| 249 |
\IF |item|[i] \neq |last| |
| 250 |
\THEN |write|(|line|); |line| := `` ''; m := 0; |
| 251 |
\CALL |inhere| \FI ; |
| 252 |
\CALL |more| \ENDACTION |
| 253 |
|inhere| \ACTIONEQ % |
| 254 |
p := |number|[i]; |line| := |item|[i]; |
| 255 |
|line| := |line| \concat `` '' \concat p; |
| 256 |
\CALL |more| \ENDACTION |
| 257 |
|more| \ACTIONEQ % |
| 258 |
\IF (m=1) \THEN p := |number|[i]; |
| 259 |
|line| := |line| \concat ``, '' \concat p \FI ; |
| 260 |
|last| := |item|[i]; |
| 261 |
\CALL l \ENDACTION |
| 262 |
|alldone| \ACTIONEQ |write|(|line|); \CALL Z \ENDACTION \ENDACTIONS \END |
| 263 |
\end{program} |
| 264 |
\end{algorithm} |
| 265 |
|
| 266 |
And here's the transformed and corrected version: |
| 267 |
\begin{algorithm} |
| 268 |
\begin{program} |
| 269 |
\seq{|line| := `` '', i := 1}; |
| 270 |
\WHILE i \neq n+1 \DO |
| 271 |
|line| := |item|[i] \concat `` '' \concat |number|[i]; |
| 272 |
i := i+1; |
| 273 |
\WHILE i \neq n+1 \AND |item|[i] = |item|[i-1] \DO |
| 274 |
|line| := |line| \concat ``, '' \concat |number|[i]); |
| 275 |
i := i+1 \OD ; |
| 276 |
|write|(|line|) \OD |
| 277 |
\end{program} |
| 278 |
\end{algorithm} |
| 279 |
|
| 280 |
Below are the same programs in a bold serif style with underlined keywords, using the command |
| 281 |
\verb:\bfvariables:: |
| 282 |
\bfvariables |
| 283 |
\begin{program} |
| 284 |
\VAR \seq{m := 0, p := 0, |last| := `` ''}; |
| 285 |
\ACTIONS |prog|: |
| 286 |
|prog| \ACTIONEQ % |
| 287 |
\seq{|line| := `` '', m := 0, i := 1}; |
| 288 |
\CALL |inhere| \ENDACTION |
| 289 |
l \ACTIONEQ % |
| 290 |
i := i+1; |
| 291 |
\IF (i=(n+1)) \THEN \CALL |alldone| \FI ; |
| 292 |
m := 1; |
| 293 |
\IF |item|[i] \neq |last| |
| 294 |
\THEN |write|(|line|); |line| := `` ''; m := 0; |
| 295 |
\CALL |inhere| \FI ; |
| 296 |
\CALL |more| \ENDACTION |
| 297 |
|inhere| \ACTIONEQ % |
| 298 |
p := |number|[i]; |line| := |item|[i]; |
| 299 |
|line| := |line| \concat `` '' \concat p; |
| 300 |
\CALL |more| \ENDACTION |
| 301 |
|more| \ACTIONEQ % |
| 302 |
\IF (m=1) \THEN p := |number|[i]; |
| 303 |
|line| := |line| \concat ``, '' \concat p \FI ; |
| 304 |
|last| := |item|[i]; |
| 305 |
\CALL l \ENDACTION |
| 306 |
|alldone| \ACTIONEQ |write|(|line|); \CALL Z \ENDACTION \ENDACTIONS \END |
| 307 |
\end{program} |
| 308 |
|
| 309 |
\begin{program} |
| 310 |
\seq{|line|:=`` '', i:=1}; |
| 311 |
\WHILE i \neq n+1 \DO |
| 312 |
|line| := |item|[i] \concat `` '' \concat |number|[i]; |
| 313 |
i := i+1; |
| 314 |
\WHILE i \neq n+1 \AND |item|[i] = |item|[i-1] \DO |
| 315 |
|line| := |line| \concat ``, '' \concat |number|[i]); |
| 316 |
i := i+1 \OD ; |
| 317 |
|write|(|line|) \OD |
| 318 |
\end{program} |
| 319 |
|
| 320 |
In my opinion, the \verb:\sfvariables: style looks much better. |
| 321 |
The \verb:\bfvariables: style was the default, but this was changed with version 3.3.11. |
| 322 |
|
| 323 |
|
| 324 |
\end{document} |
| 325 |
|