| 1 |
% Filename: lazylist.tex |
| 2 |
% Author: Alan Jeffrey |
| 3 |
% Last modified: 11 May 1990 |
| 4 |
% Modified: 24 July 2003 by Robin Fairbairns, for file name changes |
| 5 |
% and use with LaTeX 2e |
| 6 |
% |
| 7 |
% Copyright (c) 1990 Alan Jeffrey. Permission is granted to Tugboat |
| 8 |
% to publish any or all of this. |
| 9 |
% |
| 10 |
% A keyboard check: |
| 11 |
% |
| 12 |
% @ # $ % ^ & * ~ at hash dollar percent caret ampersand asterisk tilde |
| 13 |
% : ; , . colon semicolon comma period |
| 14 |
% ? ! question-mark exclamation-mark |
| 15 |
% " ' ` double-quote apostrophe back-quote |
| 16 |
% ( ) { } [ ] parentheses braces square-brackets |
| 17 |
% - + = / \ minus plus equals forward-slash backslash |
| 18 |
% _ | < > underscore vertical-bar less-than greater-than |
| 19 |
% |
| 20 |
|
| 21 |
\documentclass{ltugboat} |
| 22 |
\usepackage{lazylist} |
| 23 |
|
| 24 |
% This document defines a whole load of extra commands, some of which |
| 25 |
% over-ride how LaTeX normally lays things out. For example, ~ is |
| 26 |
% redefined to give a hairspace in math mode. This whole document |
| 27 |
% should probably be put in a group to stop it getting in the way |
| 28 |
% of other articles' macros. |
| 29 |
|
| 30 |
\title{Lists in \TeX's Mouth} |
| 31 |
|
| 32 |
\author{Alan Jeffrey} |
| 33 |
|
| 34 |
\address{Programming Research Group\\ |
| 35 |
Oxford University\\ |
| 36 |
11 Keble Road\\ |
| 37 |
Oxford OX1 3QD} |
| 38 |
|
| 39 |
\netaddress{[email protected]} |
| 40 |
|
| 41 |
\makeatletter |
| 42 |
|
| 43 |
% The mathcodes for the letters A, ..., Z, a, ..., z are changed to |
| 44 |
% generate text italic rather than math italic by default. This makes |
| 45 |
% multi-letter names look neater. The mathcode for character 'c' |
| 46 |
% is set to "7000 (variable family) + "400 (text italic) + c. |
| 47 |
% |
| 48 |
% This neat bit of code is due to Mike Spivey. |
| 49 |
% |
| 50 |
% \def\@setmcodes#1#2#3{{\count0=#1 \count1=#3 |
| 51 |
% \loop \global\mathcode\count0=\count1 \ifnum \count0<#2 |
| 52 |
% \advance\count0 by1 \advance\count1 by1 \repeat}} |
| 53 |
% |
| 54 |
% \@setmcodes{`A}{`Z}{"7441} |
| 55 |
% \@setmcodes{`a}{`z}{"7461} |
| 56 |
|
| 57 |
% however, for all its neatness, the code doesn't actually work in 2e; |
| 58 |
% the following looks even neater (if you don't consider what goes on |
| 59 |
% behind the scenes), and does the specified job |
| 60 |
|
| 61 |
\DeclareSymbolFont{letters} {OT1}{cmr} {m}{it} |
| 62 |
|
| 63 |
|
| 64 |
\def\Number#1{\csname Number-#1\endcsname} |
| 65 |
\def\Label#1{\csname Label-#1\endcsname} |
| 66 |
|
| 67 |
\newcount\Lastnum |
| 68 |
|
| 69 |
\def\Forward#1% |
| 70 |
{\global\advance\Lastnum by 1 |
| 71 |
\csnameafter\xdef{Number-#1}% |
| 72 |
{\the\Lastnum}% |
| 73 |
\csnameafter\xdef{Label-\the\Lastnum}% |
| 74 |
{\@currentlabel}} |
| 75 |
|
| 76 |
\def\csnameafter#1#2% |
| 77 |
{\expandafter#1\csname#2\endcsname} |
| 78 |
|
| 79 |
\def\Bylist#1% |
| 80 |
{\Map\Label |
| 81 |
{\Insertsort\Lessthan |
| 82 |
{\Map\Number{#1}}}} |
| 83 |
|
| 84 |
\def\By{\Show\Bylist} |
| 85 |
|
| 86 |
\let\bindspace=~ |
| 87 |
\def~{\ifmmode \, \else \bindspace \fi} |
| 88 |
|
| 89 |
\def\start#1{\lefteqn{#1}\quad\\} |
| 90 |
|
| 91 |
\def\nil{[\,\,]} |
| 92 |
|
| 93 |
\newtheorem{fact}{Fact} |
| 94 |
\def\thefact{\@roman\c@fact} |
| 95 |
|
| 96 |
\def\cstok#1{\leavevmode\thinspace\hbox{\vrule\vtop{\vbox{\hrule\kern1pt |
| 97 |
\hbox{\vphantom{\tt/}\thinspace{\tt#1}\thinspace}}% |
| 98 |
\kern1pt\hrule}\vrule}\thinspace} |
| 99 |
|
| 100 |
\begingroup \catcode `|=0 \catcode `[= 1 |
| 101 |
\catcode`]=2 \catcode `\{=12 \catcode `\}=12 |
| 102 |
\catcode`\\=12 |gdef|@xTeXcode#1\end{TeXcode}[#1|end[TeXcode]] |
| 103 |
|endgroup |
| 104 |
|
| 105 |
\def\TeXcode |
| 106 |
{\@verbatim \smallskip\hrule\medskip \frenchspacing\@vobeyspaces \@xTeXcode} |
| 107 |
\def\endTeXcode |
| 108 |
{\medskip\hrule\smallskip\endtrivlist} |
| 109 |
|
| 110 |
\makeatother |
| 111 |
|
| 112 |
\begin{document} |
| 113 |
|
| 114 |
\maketitle |
| 115 |
|
| 116 |
\section{Why lists?} |
| 117 |
|
| 118 |
Originally, I wanted lists in \TeX\ for |
| 119 |
a paper I was writing which contained a lot of facts. |
| 120 |
\begin{fact} |
| 121 |
\Forward{Fac-cows} |
| 122 |
Cows have four legs. |
| 123 |
\end{fact} |
| 124 |
\begin{fact} |
| 125 |
\Forward{Fac-people} |
| 126 |
People have two legs. |
| 127 |
\end{fact} |
| 128 |
\begin{fact} |
| 129 |
\Forward{Fac-yawn} |
| 130 |
Lots of facts in a row can be dull. |
| 131 |
\end{fact} |
| 132 |
These are generated with commands like |
| 133 |
\begin{verbatim} |
| 134 |
\begin{fact} |
| 135 |
\Forward{Fac-yawn} |
| 136 |
Lots of facts in a row can be dull. |
| 137 |
\end{fact} |
| 138 |
\end{verbatim} |
| 139 |
I can then refer to these facts by saying |
| 140 |
\begin{verbatim} |
| 141 |
\By[Fac-yawn,Fac-cows,Fac-people] |
| 142 |
\end{verbatim} |
| 143 |
to get |
| 144 |
\By[Fac-yawn,Fac-cows,Fac-people]. |
| 145 |
And as if by magic, the facts come out sorted, rather than in |
| 146 |
the jumbled order I typed them. This is very useful, as I can |
| 147 |
reorganize my document to my heart's content, and not have to worry |
| 148 |
about getting my facts straight. |
| 149 |
|
| 150 |
Originally I tried programming this sorting routine in \TeX's |
| 151 |
list macros, from Appendix~D of \TB, but I soon ran into trouble. |
| 152 |
The problem is that all the Appendix~D macros work by assigning |
| 153 |
values to macros. For example: |
| 154 |
\begin{verbatim} |
| 155 |
\concatenate\foo=\bar\baz |
| 156 |
\end{verbatim} |
| 157 |
expands out to |
| 158 |
\begin{verbatim} |
| 159 |
\ta=\expandafter{\bar} |
| 160 |
\tb=\expandafter{\baz} |
| 161 |
\edef\foo{\the\ta\the\tb} |
| 162 |
\end{verbatim} |
| 163 |
which assigns the macro \verb|\foo| the contents of \verb|\bar| |
| 164 |
followed by the contents of \verb|\baz|. Programming sorting routines |
| 165 |
(which are usually recursive) in terms of these lists became rather |
| 166 |
painful, as I was constantly having to watch out for local variables, |
| 167 |
worrying about what happened if a local variable had the same name |
| 168 |
as a global one, and generally having a hard time. |
| 169 |
|
| 170 |
Then I had one of those ``flash of light'' experiences --- |
| 171 |
``You can do lambda-calculus in \TeX,'' I thought, |
| 172 |
and since you can do lists directly in lambda calculus, |
| 173 |
you should be able to do lists straightforwardly in \TeX. And so you |
| 174 |
can. Well, fairly straightforwardly anyway. |
| 175 |
|
| 176 |
So I went and did a bit of mathematics, and derived the \TeX\ macros |
| 177 |
you see here. They were formally verified, and worked first time |
| 178 |
(modulo typing errors, of which there were two). |
| 179 |
|
| 180 |
\section{\TeX's mouth and \TeX's stomach} |
| 181 |
|
| 182 |
\TeX's programming facilities come in two forms --- there are \TeX's |
| 183 |
{\em macros\/} which are expanded in its mouth, and some additional |
| 184 |
{\em assignment\/} operations like \verb|\def| which take place in the |
| 185 |
stomach. \TeX\ can often spring surprises on you as exactly what |
| 186 |
gets evaluated where. |
| 187 |
For example, in \LaTeX\ I can put down a |
| 188 |
label by saying \verb|\label{Here}|. |
| 189 |
\label{Here} |
| 190 |
Then I can refer back to that label by saying |
| 191 |
\verb|Section~\ref{Here}|, which |
| 192 |
produces Section~\ref{Here}. Unfortunately, \verb|\ref{Here}| does |
| 193 |
{\em not\/} expand out to {\tt\ref{Here}}! Instead, it expands out to: |
| 194 |
\begin{verbatim} |
| 195 |
\edef\@tempa{\@nameuse{r@Here}} |
| 196 |
\expandafter\@car\@tempa\@nil\null |
| 197 |
\end{verbatim} |
| 198 |
This means that I can't say |
| 199 |
\begin{verbatim} |
| 200 |
\ifnum\ref{Here}<4 Hello\fi |
| 201 |
\end{verbatim} |
| 202 |
and hope that this will expand out to Hello. Instead I |
| 203 |
get an error message. Which is rather a pity, as \TeX's mouth is |
| 204 |
quite a powerful programming language (as powerful as a Turing Machine in |
| 205 |
fact). |
| 206 |
|
| 207 |
\section{Functions} |
| 208 |
|
| 209 |
A {\em function\/} is a mathematical object that takes in an argument |
| 210 |
(which could well be another function) and returns some other mathematical |
| 211 |
object. For example the function $Not$ takes in a boolean and returns |
| 212 |
its complement. I'll write function application without brackets, |
| 213 |
so $Not~b$ is the boolean complement of $b$. |
| 214 |
|
| 215 |
Function application |
| 216 |
binds to the left, so $f~a~b$ is $(f~a)~b$ rather than $f~(a~b)$. |
| 217 |
For example, $Or~a~b$ is the boolean or of $a$ and $b$, and |
| 218 |
$Or~True$ is a perfectly good function that takes in a boolean |
| 219 |
and returns $True$. |
| 220 |
|
| 221 |
The obvious equivalents of functions in \TeX\ are macros --- |
| 222 |
if I define a function $Foo$ to be: |
| 223 |
\begin{eqnarray*} |
| 224 |
Foo~x & = & True |
| 225 |
\end{eqnarray*} |
| 226 |
then it can be translated into \TeX\ as: |
| 227 |
\begin{verbatim} |
| 228 |
\def\Foo#1{\True} |
| 229 |
\end{verbatim} |
| 230 |
So where $Foo$ is a function that takes in one argument, \verb|\Foo| |
| 231 |
is a macro that takes in one parameter. Nothing has changed except |
| 232 |
the jargon and the font. \TeX\ macros can even be partially applied, |
| 233 |
for example if we defined: |
| 234 |
\begin{eqnarray*} |
| 235 |
Baz & = & Or~True |
| 236 |
\end{eqnarray*} |
| 237 |
then the \TeX\ equivalent would be |
| 238 |
\begin{verbatim} |
| 239 |
\def\Baz{\Or\True} |
| 240 |
\end{verbatim} |
| 241 |
Once \verb|\Baz| is expanded, it will expect to be given a parameter, |
| 242 |
but when we are defining things, we can go around partially applying |
| 243 |
them all we like. |
| 244 |
|
| 245 |
Here, I'm using $=$ without formally defining it, which is rather |
| 246 |
naughty. If I say $x = y$, this means |
| 247 |
``given enough parameters, $x$ and $y$ will eventually |
| 248 |
expand out to the same thing.'' For example $Foo = Baz$, because |
| 249 |
for any $x$, |
| 250 |
\begin{eqnarray*} |
| 251 |
\start{Foo~x} |
| 252 |
& = & True \\ |
| 253 |
& = & Or~True~x \\ |
| 254 |
& = & Baz~x |
| 255 |
\end{eqnarray*} |
| 256 |
Normally, functions have to {respect equality\/} which means that: |
| 257 |
\begin{itemize} |
| 258 |
\item if $x = y$ then $f~x = f~y$, and |
| 259 |
\item if $x$ respects equality, then $f~x$ respects equality. |
| 260 |
\end{itemize} |
| 261 |
However, some \TeX\ control sequences don't obey this. For example, |
| 262 |
\verb|\string\Foo| and \verb|\string\Baz| are different, even though |
| 263 |
$Foo = Baz$. Hence $string$ doesn't respect equality. |
| 264 |
Unless otherwise stated, we won't assume functions respect equality, |
| 265 |
although all the functions defined here do. |
| 266 |
|
| 267 |
All of our functions have capital letters, so that their \TeX\ equivalents |
| 268 |
(\verb|\Not|, \verb|\Or| and so on) don't clash with standard \TeX\ or |
| 269 |
\LaTeX\ macros. |
| 270 |
|
| 271 |
\subsection{Identity} |
| 272 |
|
| 273 |
The simplest function is the {\em identity\/} function, called |
| 274 |
$Identity$ funnily enough, which is defined: |
| 275 |
\begin{eqnarray*} |
| 276 |
Identity~x & = & \Identity{x} |
| 277 |
\end{eqnarray*} |
| 278 |
This, it must be admitted, is a pretty dull function, but |
| 279 |
it's a useful basic combinator. It can be implemented |
| 280 |
in \TeX\ quite simply. |
| 281 |
\begin{TeXcode} |
| 282 |
\def\Identity#1{#1} |
| 283 |
\end{TeXcode} |
| 284 |
The rules around this definition mean that it is actually part of |
| 285 |
\verb|Lambda.sty| and not just another example. |
| 286 |
|
| 287 |
\subsection{Error} |
| 288 |
|
| 289 |
Whereas $Identity$ does nothing in a fairly pleasant sort of way, |
| 290 |
$Error$ does nothing in a particularly brutal and harsh fashion. |
| 291 |
Mathematically, $Error$ is the function that destroys everything |
| 292 |
else in front of it. It is often written as $\perp$. |
| 293 |
\begin{eqnarray*} |
| 294 |
Error~x & = & Error |
| 295 |
\end{eqnarray*} |
| 296 |
In practice, destroying the entire document when we hit one error |
| 297 |
is a bit much, so we'll just print out an error message. |
| 298 |
The user can carry on past an error at their own risk, as the code |
| 299 |
will no longer be formally verified. |
| 300 |
\begin{TeXcode} |
| 301 |
\def\Error |
| 302 |
{\errmessage{Abandon verification all |
| 303 |
ye who enter here}} |
| 304 |
\end{TeXcode} |
| 305 |
Maybe this function ought to return a more useful error message \ldots |
| 306 |
|
| 307 |
\subsection{First and Second} |
| 308 |
|
| 309 |
Two other basic functions are $First$ and $Second$, both of which |
| 310 |
take in two arguments, and do the obvious thing. They are defined: |
| 311 |
\begin{eqnarray*} |
| 312 |
First~x~y & = & x \\ |
| 313 |
Second~x~y & = & y |
| 314 |
\end{eqnarray*} |
| 315 |
We could, in |
| 316 |
fact, define $Second$ in terms of $Identity$ and $First$. |
| 317 |
For any $x$ and $y$, |
| 318 |
\begin{eqnarray*} |
| 319 |
\start{First~Identity~x~y} |
| 320 |
& = & Identity~y \\ |
| 321 |
& = & y \\ |
| 322 |
& = & Second~x~y |
| 323 |
\end{eqnarray*} |
| 324 |
So $First~Identity = Second$. This means that anywhere in our \TeX\ code |
| 325 |
we have \verb|\First\Identity| we could replace it by \verb|\Second|. |
| 326 |
This is perhaps not the most astonishing \TeX\ fact known to humanity, |
| 327 |
but this sort of proof did enable more complex bits of \TeX\ to be |
| 328 |
verified before they were run. |
| 329 |
|
| 330 |
The \TeX\ definitions of \verb|\First| and \verb|\Second| are pretty |
| 331 |
obvious. |
| 332 |
\begin{TeXcode} |
| 333 |
\def\First#1#2{#1} |
| 334 |
\def\Second#1#2{#2} |
| 335 |
\end{TeXcode} |
| 336 |
Note that in \TeX\, \verb|\First\foo\bar| expands out to |
| 337 |
\verb|\foo| {\em without\/} expanding out \verb|\bar|. |
| 338 |
This is very useful, as we can write macros that would take |
| 339 |
forever and a day to run if they expanded all their arguments, |
| 340 |
but which actually terminate quite quickly. This is called |
| 341 |
{\em lazy evaluation\/} by the functional programming community. |
| 342 |
|
| 343 |
\subsection{Compose} |
| 344 |
|
| 345 |
Given two functions $f$ and $g$ we would like to be able to {\em compose\/} |
| 346 |
them to produce a function that first applies $g$ then applies $f$. |
| 347 |
Normally, this is written as $f \circ g$, but unfortunately \TeX\ doesn't |
| 348 |
have infix functions, so we'll have to write it $Compose~f~g$. |
| 349 |
\begin{eqnarray*} |
| 350 |
Compose~f~g~x & = & f~(g~x) |
| 351 |
\end{eqnarray*} |
| 352 |
>From this definition, we can deduce that $Compose$ is associative: |
| 353 |
\begin{eqnarray*} |
| 354 |
\start{Compose~(Compose~f~g)~h} |
| 355 |
& = & Compose~f~(Compose~g~h) |
| 356 |
\end{eqnarray*} |
| 357 |
and $Identity$ is the left unit of $Compose$: |
| 358 |
\begin{eqnarray*} |
| 359 |
Compose~Identity~f & = & f |
| 360 |
\end{eqnarray*} |
| 361 |
The reader may wonder why $Identity$ is called a {\em left\/} unit |
| 362 |
even though it occurs on the right of the $Compose$ --- this is a side-effect |
| 363 |
of using prefix notations where infix is more normal. The infix version |
| 364 |
of this equation is: |
| 365 |
\begin{eqnarray*} |
| 366 |
Identity \circ f & = & f |
| 367 |
\end{eqnarray*} |
| 368 |
so $Identity$ is indeed on the left of the composition. |
| 369 |
|
| 370 |
$Compose$ can be implemented in \TeX\ as |
| 371 |
\begin{TeXcode} |
| 372 |
\def\Compose#1#2#3{#1{#2{#3}}} |
| 373 |
\end{TeXcode} |
| 374 |
|
| 375 |
\subsection{Twiddle} |
| 376 |
|
| 377 |
Yet another useful little function is $Twiddle$, which takes in |
| 378 |
a function and reverses the order that function takes its (first two) |
| 379 |
arguments. |
| 380 |
\begin{eqnarray*} |
| 381 |
Twiddle~f~x~y & = & f~y~x |
| 382 |
\end{eqnarray*} |
| 383 |
Again, there aren't many immediate uses for such a function, but it'll |
| 384 |
come in handy later on. It satisfies the properties |
| 385 |
\begin{eqnarray*} |
| 386 |
Twiddle~First & = & Second \\ |
| 387 |
Twiddle~Second & = & First \\ |
| 388 |
Compose~Twiddle~Twiddle & = & Identity |
| 389 |
\end{eqnarray*} |
| 390 |
Its \TeX\ equivalent is |
| 391 |
\begin{TeXcode} |
| 392 |
\def\Twiddle#1#2#3{#1{#3}{#2}} |
| 393 |
\end{TeXcode} |
| 394 |
This function is called ``twiddle'' because it is sometimes written |
| 395 |
$\widetilde f$ (and $\sim$ is pronounced ``twiddle''). |
| 396 |
It also twiddles its arguments around, |
| 397 |
which is quite nice if your sense of humour runs to appalling puns. |
| 398 |
|
| 399 |
\section{Booleans} |
| 400 |
|
| 401 |
As we're trying to program a sorting routine, it would be nice to |
| 402 |
be able to define orderings on things, and to do this we need some |
| 403 |
representation of boolean variables. Unfortunately \TeX\ doesn't have a type |
| 404 |
for booleans, so we'll have to invent our own. We'll |
| 405 |
implement a boolean as a function $b$ of the form |
| 406 |
\begin{eqnarray*} |
| 407 |
b~x~y & |
| 408 |
= & |
| 409 |
\left\{ |
| 410 |
\begin{array}{ll} |
| 411 |
x & \mbox{if $b$ is true} \\ |
| 412 |
y & \mbox{otherwise} |
| 413 |
\end{array} |
| 414 |
\right. |
| 415 |
\end{eqnarray*} |
| 416 |
More formally, a |
| 417 |
boolean $b$ is a function which respects equality, |
| 418 |
such that for all $f$, $g$ and $z$: |
| 419 |
\begin{eqnarray*} |
| 420 |
b~f~g~z & = & b~(f~z)~(g~z) |
| 421 |
\end{eqnarray*} |
| 422 |
and for all $f$ and $g$ which respect equality, |
| 423 |
\begin{eqnarray*} |
| 424 |
b~(f~b)~(g~b) & = & b~(f~First)~(g~Second) |
| 425 |
\end{eqnarray*} |
| 426 |
All the functions in this section satisfy these properties. Surprisingly |
| 427 |
enough, so does $Error$, which is quite useful, as it allows us to |
| 428 |
reason about booleans which ``go wrong''. |
| 429 |
|
| 430 |
\subsection{True, False and Not} |
| 431 |
|
| 432 |
Since we are implementing booleans as functions, we already have the |
| 433 |
definitions of $True$, $False$ and $Not$. |
| 434 |
\begin{eqnarray*} |
| 435 |
True & = & First \\ |
| 436 |
False & = & Second \\ |
| 437 |
Not & = & Twiddle |
| 438 |
\end{eqnarray*} |
| 439 |
So for free we get the following results: |
| 440 |
\begin{eqnarray*} |
| 441 |
Not~True & = & False \\ |
| 442 |
Not~False & = & True \\ |
| 443 |
Compose~Not~Not & = & Identity |
| 444 |
\end{eqnarray*} |
| 445 |
The \TeX\ implementation is not exactly difficult: |
| 446 |
\begin{TeXcode} |
| 447 |
\let\True=\First |
| 448 |
\let\False=\Second |
| 449 |
\let\Not=\Twiddle |
| 450 |
\end{TeXcode} |
| 451 |
|
| 452 |
\subsection{And and Or} |
| 453 |
|
| 454 |
The definitions of $And$ and $Or$ are: |
| 455 |
\begin{eqnarray*} |
| 456 |
And~a~b & |
| 457 |
= & |
| 458 |
\left\{ |
| 459 |
\begin{array}{ll} |
| 460 |
b & \mbox{if $a$ is true} \\ |
| 461 |
False & \mbox{otherwise} |
| 462 |
\end{array} |
| 463 |
\right. |
| 464 |
\\ |
| 465 |
Or~a~b & |
| 466 |
= & |
| 467 |
\left\{ |
| 468 |
\begin{array}{ll} |
| 469 |
True & \mbox{if $a$ is true} \\ |
| 470 |
b & \mbox{otherwise} |
| 471 |
\end{array} |
| 472 |
\right. |
| 473 |
\end{eqnarray*} |
| 474 |
With our definition of what a boolean is, this is just the same as |
| 475 |
\begin{eqnarray*} |
| 476 |
And~a~b & = & a~b~False \\ |
| 477 |
Or~a~b & = & a~True~b |
| 478 |
\end{eqnarray*} |
| 479 |
>From these conditions, we can show that $And$ is associative, and |
| 480 |
has left unit $True$ and left zeros $False$ and $Error$: |
| 481 |
\begin{eqnarray*} |
| 482 |
And~(And~a~b)~c & = & And~a~(And~b~c) \\ |
| 483 |
And~True~b & = & b \\ |
| 484 |
And~False~b & = & False \\ |
| 485 |
And~Error~b & = & Error |
| 486 |
\end{eqnarray*} |
| 487 |
$Or$ is associative, has left unit $False$ and left zeros $True$ and $Error$: |
| 488 |
\begin{eqnarray*} |
| 489 |
Or~(Or~a~b)~c & = & Or~a~(Or~b~c) \\ |
| 490 |
Or~False~b & = & b \\ |
| 491 |
Or~True~b & = & True \\ |
| 492 |
Or~Error~b & = & Error |
| 493 |
\end{eqnarray*} |
| 494 |
De~Morgan's laws hold: |
| 495 |
\begin{eqnarray*} |
| 496 |
Not~(And~a~b) & = & Or~(Not~a)~(Not~b) \\ |
| 497 |
Not~(Or~a~b) & = & And~(Not~a)~(Not~b) |
| 498 |
\end{eqnarray*} |
| 499 |
and $And$ and $Or$ left-distribute through one another: |
| 500 |
\begin{eqnarray*} |
| 501 |
Or~a~(And~b~c) & = & And~(Or~a~b)~(Or~a~c) \\ |
| 502 |
And~a~(Or~b~c) & = & Or~(And~a~b)~(And~a~c) |
| 503 |
\end{eqnarray*} |
| 504 |
$And$ and $Or$ are {\em not\/} commutative, though. For example, |
| 505 |
\begin{eqnarray*} |
| 506 |
\start{Or~True~Error} |
| 507 |
& = & True~True~Error \\ |
| 508 |
& = & True |
| 509 |
\end{eqnarray*} |
| 510 |
but |
| 511 |
\begin{eqnarray*} |
| 512 |
\start{Or~Error~True} |
| 513 |
& = & Error~True~True \\ |
| 514 |
& = & Error |
| 515 |
\end{eqnarray*} |
| 516 |
This is actually quite useful since there are some booleans that |
| 517 |
need to return an error occasionally. If $a$ is $True$ when $b$ |
| 518 |
is safe (i.e.\ doesn't become $Error$) and is $False$ otherwise, we can |
| 519 |
say $Or~a~b$ and know we're not going to get an error. This is handy |
| 520 |
for things like checking for division by zero, or trying to get the |
| 521 |
first element of an empty list. |
| 522 |
|
| 523 |
Similarly, because of the possibility of $Error$, |
| 524 |
$And$ and $Or$ don't right-distribute through each other, |
| 525 |
as |
| 526 |
\begin{eqnarray*} |
| 527 |
\start{Or~(And~False~Error)~True} |
| 528 |
& \ne & And~(Or~False~True)~(Or~Error~True) |
| 529 |
\end{eqnarray*} |
| 530 |
As errors shouldn't crop up, this needn't worry us too much. |
| 531 |
\begin{TeXcode} |
| 532 |
\def\And#1#2{#1{#2}\False} |
| 533 |
\def\Or#1#2{#1\True{#2}} |
| 534 |
\end{TeXcode} |
| 535 |
|
| 536 |
\subsection{Lift} |
| 537 |
|
| 538 |
Quite a lot of the time we won't be dealing with booleans, but with |
| 539 |
{\em predicates}, which are just functions that return a boolean. |
| 540 |
For example, the predicate $Lessthan$ is defined below so that |
| 541 |
$Lessthan~i~j$ is true whenever $i<j$. |
| 542 |
Given a predicate $p$ we would like to be able to |
| 543 |
{\em lift\/} it to $Lift~p$, defined: |
| 544 |
\begin{eqnarray*} |
| 545 |
Lift~p~f~g~x & = & p~x~f~g~x |
| 546 |
\end{eqnarray*} |
| 547 |
For example, $Lift~(Lessthan~0)~f~g$ takes in a number and applies |
| 548 |
$f$ to it if it is positive and $g$ to it otherwise. This is quite |
| 549 |
useful for defining functions. |
| 550 |
\begin{TeXcode} |
| 551 |
\def\Lift#1#2#3#4{#1{#4}{#2}{#3}{#4}} |
| 552 |
\end{TeXcode} |
| 553 |
|
| 554 |
\subsection{Lessthan and \TeX if} |
| 555 |
|
| 556 |
Finally, we would like to be able to use \TeX's built-in booleans |
| 557 |
as well as our own. For example, we would like a predicate |
| 558 |
$Lessthan$ such that: |
| 559 |
\begin{eqnarray*} |
| 560 |
Lessthan~i~j & |
| 561 |
= & |
| 562 |
\left\{ |
| 563 |
\begin{array}{ll} |
| 564 |
True & \mbox{if } i < j \\ |
| 565 |
False & \mbox{if } i \ge j \\ |
| 566 |
Error & \mbox{otherwise} |
| 567 |
\end{array} |
| 568 |
\right. |
| 569 |
\end{eqnarray*} |
| 570 |
The $Error$ condition happens if we try applying $Lessthan$ to something |
| 571 |
that isn't a number --- $Lessthan~True~False$ is $Error$% |
| 572 |
\footnote |
| 573 |
{Actually, that's a little white lie --- trying to persuade \TeX\ to |
| 574 |
do run-time type checking isn't much fun. So the \TeX\ implementation |
| 575 |
of this is actually a {\em refinement\/} where the $Error$ condition |
| 576 |
has been replaced by whatever it is \TeX\ does if you try doing |
| 577 |
{\tt\string\ifnum $x$ < $y$} when $x$ and $y$ aren't numbers}. |
| 578 |
This is fine as a mathematical definition, but how will |
| 579 |
we implement it? If we assume we have a macro \verb|\TeXif|, |
| 580 |
which converts \TeX\ if-statements into booleans, we could just |
| 581 |
define: |
| 582 |
\begin{TeXcode} |
| 583 |
\def\Lessthan#1#2{\TeXif{\ifnum#1<#2 }} |
| 584 |
\end{TeXcode} |
| 585 |
So the question is just how to define \verb|\TeXif|. |
| 586 |
Unfortunately, the ``obvious'' code does not work: |
| 587 |
\begin{verbatim} |
| 588 |
\def\TeXif#1#2#3{#1#2\else#3\fi} |
| 589 |
\end{verbatim} |
| 590 |
For example, \verb|\TeXif\iftrue\True\True| doesn't expand out to |
| 591 |
\verb|\True|. Instead, it expands as: |
| 592 |
\begin{verbatim} |
| 593 |
\TeXif\iftrue\True\True |
| 594 |
= \iftrue\True\else\True\fi |
| 595 |
= \True\else\True\fi |
| 596 |
= \else\fi |
| 597 |
= |
| 598 |
\end{verbatim} |
| 599 |
Another common \TeX nique is to use a macro \verb|\next| to |
| 600 |
be the expansion text: |
| 601 |
\begin{verbatim} |
| 602 |
\def\TeXif#1#2#3% |
| 603 |
{#1\def\next{#2}\else\def\next{#3}\fi |
| 604 |
\next} |
| 605 |
\end{verbatim} |
| 606 |
However, this uses \TeX's stomach to do the \verb|\def|, and we are |
| 607 |
trying to do this using only the mouth. One (slightly tricky) solution |
| 608 |
is to use pattern-matching to gobble up the offending \verb|\else| and/or |
| 609 |
\verb|\fi|. |
| 610 |
\begin{TeXcode} |
| 611 |
\def\gobblefalse\else\gobbletrue\fi#1#2% |
| 612 |
{\fi#1} |
| 613 |
\def\gobbletrue\fi#1#2% |
| 614 |
{\fi#2} |
| 615 |
\def\TeXif#1% |
| 616 |
{#1\gobblefalse\else\gobbletrue\fi} |
| 617 |
\end{TeXcode} |
| 618 |
So if the \TeX\ if-statement is true, \verb|\gobblefalse| gobbles |
| 619 |
up the false-text, otherwise \verb|\gobbletrue| gobbles up the |
| 620 |
true-text. For example, |
| 621 |
\begin{verbatim} |
| 622 |
\TeXif\iftrue\True\True |
| 623 |
= \iftrue\gobblefalse\else |
| 624 |
\gobbletrue\fi\True\True |
| 625 |
= \gobblefalse\else |
| 626 |
\gobbletrue\fi\True\True |
| 627 |
= \fi\True |
| 628 |
= \True |
| 629 |
\end{verbatim} |
| 630 |
Phew. And so we have booleans. |
| 631 |
|
| 632 |
\section{Lists} |
| 633 |
|
| 634 |
A list is a (possibly infinite) sequence of values. For example, |
| 635 |
the list $[1,2,3]$ contains three numbers, the list $\nil$ contains |
| 636 |
none, and the list $[1,2,3,\ldots]$ contains infinitely many. |
| 637 |
A list is either {\em empty\/} (written $\nil$) or is comprised |
| 638 |
of a {\em head\/} |
| 639 |
$x$ and a {\em tail\/} $xs$ (in which case it's written $x:xs$). |
| 640 |
For example, $1:2:3:\nil$ is $[1,2,3]$. |
| 641 |
|
| 642 |
In a similar fashion to the implementation of booleans, |
| 643 |
a list $xs$ is implemented as a function of the form |
| 644 |
\begin{eqnarray*} |
| 645 |
xs~f~e & |
| 646 |
= & |
| 647 |
\left\{ |
| 648 |
\begin{array}{ll} |
| 649 |
e & \mbox{if $xs$ is empty} \\ |
| 650 |
f~y~ys & \mbox{if $xs$ has head $y$ and tail $ys$} |
| 651 |
\end{array} |
| 652 |
\right. |
| 653 |
\end{eqnarray*} |
| 654 |
Again, we are implementing a datatype as a function, a quite powerful |
| 655 |
trick, just not one usually seen in \TeX. We will assume that |
| 656 |
whenever a list $x:xs$ is applied to $f$ and $e$, $f~x$ respects equality. |
| 657 |
This allows us to assume that if $xs = ys$ then $x:xs = x:ys$, |
| 658 |
which is handy. |
| 659 |
|
| 660 |
\subsection{Nil, Cons, Stream and Singleton} |
| 661 |
|
| 662 |
The simplest list is $Nil$, the empty list which we have been writing |
| 663 |
$\nil$. |
| 664 |
\begin{eqnarray*} |
| 665 |
Nil & = & Second |
| 666 |
\end{eqnarray*} |
| 667 |
The other possible list is $Cons~x~xs$, which has head $x$ and tail $xs$. |
| 668 |
\begin{eqnarray*} |
| 669 |
Cons~x~xs~f~e & = & f~x~xs |
| 670 |
\end{eqnarray*} |
| 671 |
Every list can be constructed using these functions. |
| 672 |
The list $[1,2,3]$ is $Cons~1~(Cons~2~(Cons~3~Nil))$, and the |
| 673 |
list $[a,a,a,\ldots]$ is $Stream~a$ where $Stream$ is defined: |
| 674 |
\begin{eqnarray*} |
| 675 |
Stream~a & = & Cons~a~(Stream~a) |
| 676 |
\end{eqnarray*} |
| 677 |
There's even at least one application for infinite lists, |
| 678 |
as we'll see in Section~\ref{outputroutines}. |
| 679 |
|
| 680 |
The singleton list $[a]$ is $Singleton~a$, defined as: |
| 681 |
\begin{eqnarray*} |
| 682 |
Singleton~a & = & Cons~a~Nil |
| 683 |
\end{eqnarray*} |
| 684 |
These all have straightforward \TeX\ definitions. |
| 685 |
\begin{TeXcode} |
| 686 |
\let\Nil=\Second |
| 687 |
\def\Cons#1#2#3#4{#3{#1}{#2}} |
| 688 |
\def\Stream#1{\Cons{#1}{\Stream{#1}}} |
| 689 |
\def\Singleton#1{\Cons{#1}\Nil} |
| 690 |
\end{TeXcode} |
| 691 |
|
| 692 |
\subsection{Head and Tail} |
| 693 |
|
| 694 |
So, we can construct any list we like, but we still can't get any information |
| 695 |
out of it. To begin with, we'd like to be able to get the head |
| 696 |
and tail of a list. |
| 697 |
\begin{eqnarray*} |
| 698 |
Head~xs & = & xs~First~Error \\ |
| 699 |
Tail~xs & = & xs~Second~Error |
| 700 |
\end{eqnarray*} |
| 701 |
For example, the tail of $x:xs$ is |
| 702 |
\begin{eqnarray*} |
| 703 |
\start{Tail~(Cons~x~xs)} |
| 704 |
& = & Cons~x~xs~Second~Error \\ |
| 705 |
& = & Second~x~xs \\ |
| 706 |
& = & \Tail{\Cons{x}{xs}} |
| 707 |
\end{eqnarray*} |
| 708 |
The tail of $\nil$ is, as one would expect, |
| 709 |
\begin{eqnarray*} |
| 710 |
\start{Tail~Nil} |
| 711 |
& = & Nil~Second~Error \\ |
| 712 |
& = & Error |
| 713 |
\end{eqnarray*} |
| 714 |
And the head of $Stream~a$ is |
| 715 |
\begin{eqnarray*} |
| 716 |
\start{Head~(Stream~a)} |
| 717 |
& = & Stream~a~First~Error \\ |
| 718 |
& = & Cons~a~(Stream~a)~First~Error \\ |
| 719 |
& = & First~a~(Stream~a) \\ |
| 720 |
& = & \Head{\Stream{a}} |
| 721 |
\end{eqnarray*} |
| 722 |
So we can get the head of an infinite list in finite time. This is |
| 723 |
fortunate, as otherwise there wouldn't be much point in allowing |
| 724 |
infinite objects. |
| 725 |
\begin{TeXcode} |
| 726 |
\def\Head#1{#1\First\Error} |
| 727 |
\def\Tail#1{#1\Second\Error} |
| 728 |
\end{TeXcode} |
| 729 |
|
| 730 |
\subsection{Foldl and Foldr} |
| 731 |
|
| 732 |
Using $Head$ and $Tail$ we can get at the beginning of any non-empty list, |
| 733 |
but in general we need more information than that. Rather than write |
| 734 |
a whole bunch of recursive functions on lists, I'll implement two |
| 735 |
fairly general functions, with which we can implement (almost) everything |
| 736 |
else. |
| 737 |
|
| 738 |
$Foldl$ and $Foldr$ both take in functions and apply them recursively |
| 739 |
to a list. $Foldl$ starts at the left of the list, and $Foldr$ |
| 740 |
starts at the right. For example, |
| 741 |
\begin{eqnarray*} |
| 742 |
Foldl~f~e~[1,2,3] & = & f~(f~(f~e~1)~2)~3 \\ |
| 743 |
Foldr~f~e~[1,2,3] & = & f~1~(f~2~(f~3~e)) |
| 744 |
\end{eqnarray*} |
| 745 |
These functions will be used a lot later on. $Foldl$ can be defined: |
| 746 |
\begin{eqnarray*} |
| 747 |
Foldl~f~e~xs & = & xs~(Foldl'~f~e)~e \\ |
| 748 |
Foldl'~f~e~x~xs & = & Foldl~f~(f~e~x)~xs |
| 749 |
\end{eqnarray*} |
| 750 |
So $Foldl~f~e~\nil$ is |
| 751 |
\begin{eqnarray*} |
| 752 |
\start{Foldl~f~e~Nil} |
| 753 |
& = & Nil~(Foldl'~f~e)~e \\ |
| 754 |
& = & \Foldl{f}{e}\Nil |
| 755 |
\end{eqnarray*} |
| 756 |
And $Foldl~f~e~(x:xs)$ is |
| 757 |
\begin{eqnarray*} |
| 758 |
\start{Foldl~f~e~(Cons~x~xs)} |
| 759 |
& = & Cons~x~xs~(Foldl'~f~e)~e \\ |
| 760 |
& = & Foldl'~f~e~x~xs \\ |
| 761 |
& = & Foldl~f~(f~e~x)~xs |
| 762 |
\end{eqnarray*} |
| 763 |
For example, $Foldl~f~e~[1,2,3]$ is |
| 764 |
\begin{eqnarray*} |
| 765 |
\start{Foldl~f~e~[1,2,3]} |
| 766 |
& = & Foldl~f~(f~e~1)~[2,3] \\ |
| 767 |
& = & Foldl~f~(f~(f~e~1)~2)~[3] \\ |
| 768 |
& = & Foldl~f~(f~(f~(f~e~1)~2)~3)~\nil \\ |
| 769 |
& = & f~(f~(f~e~1)~2)~3 |
| 770 |
\end{eqnarray*} |
| 771 |
as promised. Similarly, we can define $Foldr$ as |
| 772 |
\begin{eqnarray*} |
| 773 |
Foldr~f~e~xs & = & xs~(Foldr'~f~e)~e \\ |
| 774 |
Foldr'~f~e~x~xs & = & f~x~(Foldr~f~e~xs) |
| 775 |
\end{eqnarray*} |
| 776 |
For $Foldr~f$ to respect equality, $f~x$ should respect equality. |
| 777 |
|
| 778 |
When we do the unfolding, we discover that |
| 779 |
\begin{eqnarray*} |
| 780 |
Foldr~f~e~\nil & = & e \\ |
| 781 |
Foldr~f~e~(x:xs) & = & f~e~(Foldr~f~e~xs) |
| 782 |
\end{eqnarray*} |
| 783 |
$Foldr$ tends to be more efficient than $Foldl$, because $Foldl$ |
| 784 |
has to run along the entire list before it can start applying $f$, |
| 785 |
whereas $Foldr$ can apply $f$ straight away. If $f$ is a lazy function, |
| 786 |
this can make quite a difference. $Foldl$ on infinite lists, anyone? |
| 787 |
\begin{TeXcode} |
| 788 |
\def\Foldl#1#2#3% |
| 789 |
{#3{\Foldl@{#1}{#2}}{#2}} |
| 790 |
\def\Foldl@#1#2#3#4% |
| 791 |
{\Foldl{#1}{#1{#2}{#3}}{#4}} |
| 792 |
\def\Foldr#1#2#3% |
| 793 |
{#3{\Foldr@{#1}{#2}}{#2}} |
| 794 |
\def\Foldr@#1#2#3#4% |
| 795 |
{#1{#3}{\Foldr{#1}{#2}{#4}}} |
| 796 |
\end{TeXcode} |
| 797 |
|
| 798 |
\subsection{Cat} |
| 799 |
|
| 800 |
Given two lists, we would like to be able to stick them together, |
| 801 |
which is what $Cat$ (short for ``concatenate'') |
| 802 |
does. For example, $Cat~[1,2]~[3,4]$ is |
| 803 |
$[1,2,3,4]$. It can be defined using $Foldr$: |
| 804 |
\begin{eqnarray*} |
| 805 |
Cat~xs~ys & = & Foldr~Cons~ys~xs |
| 806 |
\end{eqnarray*} |
| 807 |
So |
| 808 |
\begin{eqnarray*} |
| 809 |
\start{Cat~[1,2]~[3,4]} |
| 810 |
& = & Foldr~Cons~[3,4]~[1,2] \\ |
| 811 |
& = & Cons~1~(Foldr~Cons~[3,4]~[2]) \\ |
| 812 |
& = & Cons~1~(Cons~2~(Foldr~Cons~[3,4]~\nil)) \\ |
| 813 |
& = & Cons~1~(Cons~2~[3,4]) \\ |
| 814 |
& = & \Unlistize{\Cat{\Listize[1,2]}{\Listize[3,4]}} |
| 815 |
\end{eqnarray*} |
| 816 |
The \TeX\ code for \verb|\Cat| is suspiciously similar to its mathematical |
| 817 |
definition. |
| 818 |
\begin{TeXcode} |
| 819 |
\def\Cat#1#2{\Foldr\Cons{#2}{#1}} |
| 820 |
\end{TeXcode} |
| 821 |
|
| 822 |
\subsection{Reverse} |
| 823 |
|
| 824 |
We can reverse any list with the function $Reverse$, defined using |
| 825 |
$Foldl$: |
| 826 |
\begin{eqnarray*} |
| 827 |
Reverse & = & Foldl~(Twiddle~Cons)~Nil |
| 828 |
\end{eqnarray*} |
| 829 |
For example, $Reverse~[1,2,3]$ can be calculated: |
| 830 |
\begin{eqnarray*} |
| 831 |
\start{Reverse~[1,2,3]} |
| 832 |
& = & Foldl~(Twiddle~Cons)~Nil~[1,2,3] \\ |
| 833 |
& = & Twiddle~Cons \\ |
| 834 |
& & \quad (Twiddle~Cons~(Twiddle~Cons~Nil~1)~2)~3 \\ |
| 835 |
& = & Cons~3~(Cons~2~(Cons~1~Nil)) \\ |
| 836 |
& = & \Show\Reverse[1,2,3] |
| 837 |
\end{eqnarray*} |
| 838 |
The \TeX\ code for \verb|\Reverse| doesn't even take in any parameters. |
| 839 |
\begin{TeXcode} |
| 840 |
\def\Reverse{\Foldl{\Twiddle\Cons}\Nil} |
| 841 |
\end{TeXcode} |
| 842 |
|
| 843 |
\subsection{All, Some and Isempty} |
| 844 |
|
| 845 |
Given a predicate $p$, we can find out if all the elements of |
| 846 |
a list satisfy $p$ with $All~p$. Similarly we can find if something |
| 847 |
in the list satisfies $p$ with $Some~p$. For example, |
| 848 |
\begin{eqnarray*} |
| 849 |
All~(Lessthan~1)~[1,2,3] |
| 850 |
& = & \All{\Lessthan 1}{\Listize[1,2,3]}{True}{False} \\ |
| 851 |
Some~(Lessthan~1)~[1,2,3] |
| 852 |
& = & \Some{\Lessthan 1}{\Listize[1,2,3]}{True}{False} |
| 853 |
\end{eqnarray*} |
| 854 |
These can be defined |
| 855 |
\begin{eqnarray*} |
| 856 |
All~p & = & Foldr~(Compose~And~p)~True \\ |
| 857 |
Some~p & = & Foldr~(Compose~Or~p)~False |
| 858 |
\end{eqnarray*} |
| 859 |
For example, $Isempty$ can be defined |
| 860 |
\begin{eqnarray*} |
| 861 |
Isempty & = & All~(First~False) |
| 862 |
\end{eqnarray*} |
| 863 |
This is probably not the most efficient check in the world, but we |
| 864 |
hardly ever need it --- $Foldl$ or $Foldr$ will normally do the job. |
| 865 |
\begin{TeXcode} |
| 866 |
\def\All#1{\Foldr{\Compose\And{#1}}\True} |
| 867 |
\def\Some#1{\Foldr{\Compose\Or{#1}}\False} |
| 868 |
\def\Isempty{\All{\First\False}} |
| 869 |
\end{TeXcode} |
| 870 |
|
| 871 |
\subsection{Filter} |
| 872 |
|
| 873 |
$Filter$ takes a predicate $p$ and a list $xs$, and returns a list |
| 874 |
containing only those elements of $xs$ that satisfy $p$. For example, |
| 875 |
\begin{eqnarray*} |
| 876 |
Filter~(Lessthan~1)~[1,2,3] & = & \Show\Filter{\Lessthan 1}[1,2,3] |
| 877 |
\end{eqnarray*} |
| 878 |
$Filter$ can be defined as a $Foldr$: |
| 879 |
\begin{eqnarray*} |
| 880 |
Filter~p & = & Foldr~(Lift~p~Cons~Second)~Nil |
| 881 |
\end{eqnarray*} |
| 882 |
Another easy bit of \TeX: |
| 883 |
\begin{TeXcode} |
| 884 |
\def\Filter#1% |
| 885 |
{\Foldr{\Lift{#1}\Cons\Second}\Nil} |
| 886 |
\end{TeXcode} |
| 887 |
|
| 888 |
\subsection{Map} |
| 889 |
|
| 890 |
$Map$ takes a function $f$ and a list $xs$ and applies $f$ to every |
| 891 |
element of $xs$. For example, |
| 892 |
\begin{eqnarray*} |
| 893 |
Map~f~[1,2,3] & = & \Show\Map{f~}[1,2,3] |
| 894 |
\end{eqnarray*} |
| 895 |
This is another job for $Foldr$. |
| 896 |
\begin{eqnarray*} |
| 897 |
Map~f & = & Foldr~(Compose~Cons~f)~Nil |
| 898 |
\end{eqnarray*} |
| 899 |
We shall see $Map$ used later on, to convert from a list of |
| 900 |
names such as \Show\Map{\Compose\mbox\tt}[Fac-yawn,Fac-cows], to a list of |
| 901 |
labels such as \By[Fac-yawn,Fac-cows]. |
| 902 |
\begin{TeXcode} |
| 903 |
\def\Map#1{\Foldr{\Compose\Cons{#1}}\Nil} |
| 904 |
\end{TeXcode} |
| 905 |
|
| 906 |
\subsection{Insert} |
| 907 |
|
| 908 |
The only function we need which isn't easily defined as a reduction |
| 909 |
is $Insert$, which inserts an element into a sorted list. For example, |
| 910 |
\begin{eqnarray*} |
| 911 |
Insert~Lessthan~3~[1,2,4,5] & = & \Show\Insert\Lessthan3[1,2,4,5] |
| 912 |
\end{eqnarray*} |
| 913 |
$Insert$ takes in an ordering as its first parameter, so we're not stuck |
| 914 |
with one particular order. It is defined directly in terms of the |
| 915 |
definition of lists. |
| 916 |
\begin{eqnarray*} |
| 917 |
Insert~o~x~xs & = & xs~(Insert'~o~x)~(Singleton~x) \\ |
| 918 |
Insert'~o~x~y~ys & = & o~x~y \\ |
| 919 |
& & \quad (Cons~x~(Cons~y~ys)) \\ |
| 920 |
& & \quad (Cons~y~(Insert~o~x~ys)) |
| 921 |
\end{eqnarray*} |
| 922 |
We can then define the function all this has been leading up to, |
| 923 |
$Insertsort$ which takes an ordering and a list, and insert-sorts the |
| 924 |
list according to the ordering. For example, |
| 925 |
\begin{eqnarray*} |
| 926 |
Insertsort~Lessthan~[2,3,1,2] & = & \Show\Insertsort\Lessthan[2,3,1,2] |
| 927 |
\end{eqnarray*} |
| 928 |
We can implement this as a fold: |
| 929 |
\begin{eqnarray*} |
| 930 |
Insertsort~o & = & Foldr~(Insert~o)~Nil |
| 931 |
\end{eqnarray*} |
| 932 |
And so we've got sorted lists. |
| 933 |
\begin{TeXcode} |
| 934 |
\def\Insert#1#2#3% |
| 935 |
{#3{\Insert@{#1}{#2}}{\Singleton{#2}}} |
| 936 |
\def\Insert@#1#2#3#4% |
| 937 |
{#1{#2}{#3}% |
| 938 |
{\Cons{#2}{\Cons{#3}{#4}}}% |
| 939 |
{\Cons{#3}{\Insert{#1}{#2}{#4}}}} |
| 940 |
\def\Insertsort#1{\Foldr{\Insert{#1}}\Nil} |
| 941 |
\end{TeXcode} |
| 942 |
Interestingly, as we have implemented unbounded lists in \TeX's mouth, |
| 943 |
this means we can implement a Turing Machine. So, if you believe |
| 944 |
the Church-Turing thesis, \TeX's mouth is as powerful as any |
| 945 |
computer anywhere. Isn't that good to know? |
| 946 |
|
| 947 |
\section{Sorting reference lists} |
| 948 |
\label{thissection} |
| 949 |
|
| 950 |
So, these are the macros I've got to play with --- how do we apply them to |
| 951 |
sorting lists of references? Well, I'm using \LaTeX, which keeps the |
| 952 |
current reference in a macro called \verb|\@currentlabel|, which |
| 953 |
is~\ref{thissection} at the moment, as this is Section~\ref{thissection}. |
| 954 |
So I just need to store the value of \verb|\@currentlabel| somehow. |
| 955 |
|
| 956 |
Fortunately, I'm only ever going to be making references to facts |
| 957 |
earlier on in the document, in order to make sure I'm not proving |
| 958 |
any results in terms of themselves. So I don't need to play around |
| 959 |
with auxiliary files, and can just do everything in terms of |
| 960 |
macros. |
| 961 |
|
| 962 |
\subsection{Number and Label} |
| 963 |
|
| 964 |
Each label in the document is given a unique number, in the order |
| 965 |
the labels were put down. So the number of \verb|Fac-cows| |
| 966 |
is \verb|\Number{Fac-cows}|, which expands out to~\Number{Fac-cows}, |
| 967 |
the number of \verb|Fac-people| is~\Number{Fac-people}, and so on. |
| 968 |
|
| 969 |
Each number has an associated label with it. For example, |
| 970 |
the first label is \verb|\Label{1}|, which is~\Label{1}, |
| 971 |
the second label is~\Label{2} and so on. So to find the label for |
| 972 |
\verb|Fac-cows|, we say \verb|\Label{\Number{Fac-cows}}| which expands |
| 973 |
out to~\Label{\Number{Fac-cows}}. |
| 974 |
|
| 975 |
These numbers and labels are kept track of in macros. For example, |
| 976 |
the number of \verb|Fac-cows| is kept in \cstok{Number-Fac-cows}. |
| 977 |
Similarly, the first label is kept in \cstok{Label-1}. |
| 978 |
As these macros have dashes in their names, they aren't likely to |
| 979 |
be used already. |
| 980 |
|
| 981 |
So the \TeX\ code for \verb|\Number| and \verb|\Label| is pretty |
| 982 |
simple. |
| 983 |
\begin{verbatim} |
| 984 |
\def\Number#1{\csname Number-#1\endcsname} |
| 985 |
\def\Label#1{\csname Label-#1\endcsname} |
| 986 |
\end{verbatim} |
| 987 |
|
| 988 |
\subsection{Lastnum and Forward} |
| 989 |
|
| 990 |
The number of the most recent label is kept in \verb|\Lastnum|. |
| 991 |
\begin{verbatim} |
| 992 |
\newcount\Lastnum |
| 993 |
\end{verbatim} |
| 994 |
To put down a label \verb|Foo|, I type \verb|\Forward{Foo}|. |
| 995 |
\Forward{Foo} |
| 996 |
This increments the counter |
| 997 |
\verb|\Lastnum|, and \verb|\xdef|s \cstok{Number-Foo} |
| 998 |
to be the value of |
| 999 |
\verb|\Lastnum|, which is now~\the\Lastnum. So |
| 1000 |
\verb|\Number{Foo}| now expands to~\Number{Foo}. |
| 1001 |
Similarly, it \verb|\xdef|s \cstok{Label-\Number{Foo}} to be |
| 1002 |
\verb|\@currentlabel|, which is currently~\Label{\Number{Foo}}. |
| 1003 |
So \verb|\Label{\Number{Foo}}| now expands to~\Label{\Number{Foo}}. |
| 1004 |
\begin{verbatim} |
| 1005 |
\def\Forward#1% |
| 1006 |
{\global\advance\Lastnum by 1 |
| 1007 |
\csnameafter\xdef{Number-#1}% |
| 1008 |
{\the\Lastnum}% |
| 1009 |
\csnameafter\xdef{Label-\the\Lastnum}% |
| 1010 |
{\@currentlabel}} |
| 1011 |
\end{verbatim} |
| 1012 |
This uses \verb|\csnameafter\foo{bar}|, which expands out to |
| 1013 |
\verb|\foo\bar|. |
| 1014 |
\begin{verbatim} |
| 1015 |
\def\csnameafter#1#2% |
| 1016 |
{\expandafter#1\csname#2\endcsname} |
| 1017 |
\end{verbatim} |
| 1018 |
|
| 1019 |
\subsection{Listize, Unlistize and Show} |
| 1020 |
|
| 1021 |
At the moment, lists have to be built up using \verb|\Cons| and |
| 1022 |
\verb|\Nil|, which is rather annoying. Similarly, we can't actually |
| 1023 |
do anything with a list once we've built it. We'd like some way |
| 1024 |
of converting lists in the form \verb|[a,b,c]| to and from the |
| 1025 |
form $[a,b,c]$. This is done with \verb|\Listize| and \verb|\Unlistize|. |
| 1026 |
So \verb|\Listize[a,b,c]| expands to |
| 1027 |
\begin{verbatim} |
| 1028 |
\Cons{a}{\Cons{b}{\Cons{c}{\Nil}}} |
| 1029 |
\end{verbatim} |
| 1030 |
Similarly, \verb|\Unlistize| takes the list $[a,b,c]$ and expands out |
| 1031 |
to \verb|[a, b, c]|. \verb|\Unlistize| is done with a $Foldr$. |
| 1032 |
\begin{TeXcode} |
| 1033 |
\def\Unlistize#1{[#1\Unlistize@{}]} |
| 1034 |
\def\Unlistize@#1{#1\Foldr\Commaize{}} |
| 1035 |
\def\Commaize#1#2{, #1#2} |
| 1036 |
\end{TeXcode} |
| 1037 |
The macro \verb|\Listize| is just a \TeX\ hack with pattern matching. |
| 1038 |
It would have been nice to use \verb|\@ifnextchar| for this, but |
| 1039 |
that uses \verb|\futurelet|, which doesn't expand in the mouth. Oh well. |
| 1040 |
\begin{TeXcode} |
| 1041 |
\def\Listize[#1]% |
| 1042 |
{\Listize@[#1,\relax]} |
| 1043 |
\def\Listize@#1,#2]% |
| 1044 |
{\TeXif{\ifx\relax#2}% |
| 1045 |
{\Singleton{#1}}% |
| 1046 |
{\Cons{#1}{\Listize@#2]}} |
| 1047 |
\end{TeXcode} |
| 1048 |
This only works for nonempty lists --- \verb|\Listize[]| produces the |
| 1049 |
singleton list \verb|\Singleton{}|. It also uses \verb|\relax| as its |
| 1050 |
end-of-list character, so lists with \verb|\relax| in them have to |
| 1051 |
be done by hand. You can't win them all. So |
| 1052 |
\begin{verbatim} |
| 1053 |
$\Unlistize{\Listize[a,b,c]}$ |
| 1054 |
\end{verbatim} |
| 1055 |
produces |
| 1056 |
$\Unlistize{\Listize[a,b,c]}$. This is such a common construction |
| 1057 |
that I've defined a macro \verb|\Show| such that |
| 1058 |
\verb|\Show\foo[a,b,c]| expands out to |
| 1059 |
\begin{verbatim} |
| 1060 |
\Unlistize{\foo{\Listize[a,b,c]}} |
| 1061 |
\end{verbatim} |
| 1062 |
For example, the equation |
| 1063 |
\begin{eqnarray*} |
| 1064 |
Filter\,(Lessthan\,1)\,[1,2,3] |
| 1065 |
&=& \Show\Filter{\Lessthan 1}[1,2,3] |
| 1066 |
\end{eqnarray*} |
| 1067 |
was generated with |
| 1068 |
\begin{verbatim} |
| 1069 |
\begin{eqnarray*} |
| 1070 |
Filter\,(Lessthan\,1)\,[1,2,3] |
| 1071 |
&=& \Show\Filter{\Lessthan 1}[1,2,3] |
| 1072 |
\end{eqnarray*} |
| 1073 |
\end{verbatim} |
| 1074 |
Many of the examples in this article were typeset this way. |
| 1075 |
\begin{TeXcode} |
| 1076 |
\def\Show#1[#2]% |
| 1077 |
{\Unlistize{#1{\Listize[#2]}}} |
| 1078 |
\end{TeXcode} |
| 1079 |
|
| 1080 |
\subsection{By} |
| 1081 |
|
| 1082 |
Given these macros, we can now sort any list of references with $Bylist$, |
| 1083 |
defined |
| 1084 |
\begin{eqnarray*} |
| 1085 |
Bylist~xs & = & Map~Label \\ |
| 1086 |
& & \quad (Insertsort~Lessthan \\ |
| 1087 |
& & \quad\quad (Map~Number~xs)) |
| 1088 |
\end{eqnarray*} |
| 1089 |
This takes in a list of label names like \verb|Fac-yawn|, |
| 1090 |
converts it into a list of numbers with $Map~Number$, |
| 1091 |
sorts the resulting list with $Insertsort~Lessthan$, |
| 1092 |
and finally converts all the numbers into labels like |
| 1093 |
\Label{\Number{Fac-yawn}} with $Map~Label$. For example, |
| 1094 |
\begin{eqnarray*} |
| 1095 |
\start{Bylist~\Show\Map{\Compose\mbox\tt}[Fac-yawn,Fac-cows]} |
| 1096 |
& = & Map~Label~(Insertsort~Lessthan \\ |
| 1097 |
& & \quad (Map~Number~ |
| 1098 |
\Show\Map{\Compose\mbox\tt}[Fac-yawn,Fac-cows])) \\ |
| 1099 |
& = & Map~Label~(Insertsort~Lessthan~ |
| 1100 |
\Show\Map\Number[Fac-yawn,Fac-cows]) \\ |
| 1101 |
& = & Map~Label~\Show\Compose{\Insertsort\Lessthan}{\Map\Number} |
| 1102 |
[Fac-yawn,Fac-cows] \\ |
| 1103 |
& = & \Show\Bylist[Fac-yawn,Fac-cows] |
| 1104 |
\end{eqnarray*} |
| 1105 |
The \TeX\ code for this is |
| 1106 |
\begin{verbatim} |
| 1107 |
\def\Bylist#1% |
| 1108 |
{\Map\Label |
| 1109 |
{\Insertsort\Lessthan |
| 1110 |
{\Map\Number{#1}}}} |
| 1111 |
\end{verbatim} |
| 1112 |
So we can now stick all this together, and define the macro \verb|\By| |
| 1113 |
that prints out lists of references. It is |
| 1114 |
\begin{verbatim} |
| 1115 |
\def\By{\Show\Bylist} |
| 1116 |
\end{verbatim} |
| 1117 |
So \verb|\By[Fac-yawn,Fac-cows]| is~\By[Fac-yawn,Fac-cows]. |
| 1118 |
Which is quite nice. |
| 1119 |
|
| 1120 |
\section{Other applications} |
| 1121 |
\label{outputroutines} |
| 1122 |
|
| 1123 |
Is all this worth it? Well, I've managed to get my lists of facts in |
| 1124 |
order, but that's not the world's most astonishing application. |
| 1125 |
There are other things that these lists are useful for, though. |
| 1126 |
|
| 1127 |
For example, Damian Cugley has a macro package under development for |
| 1128 |
laying out magazines. {\sc Mag}\TeX's output routine needs to be quite |
| 1129 |
smart, as magazines often have gaps where illustrations or photographs |
| 1130 |
are going to live. In general, each block of text needs to be output |
| 1131 |
in a different fashion from every other block of text. |
| 1132 |
This will be handled by keeping an infinite list of output |
| 1133 |
routines. Each time a box is cut off the scroll to be output, |
| 1134 |
the head of the list is chopped off and is used as the output routine |
| 1135 |
for that box. That way, quite complex page shapes can be built up. |
| 1136 |
|
| 1137 |
Mainly, though, these macros were written just as a challenge. |
| 1138 |
I learned quite a lot about \TeX\ and needed some \TeX niques I'd |
| 1139 |
never seen before. It was also quite pleasing to see that \TeX\ code |
| 1140 |
can be formally verified, albeit in a rather noddy way. Without |
| 1141 |
some sort of abstract view of lists, these \TeX\ macros could |
| 1142 |
not have been written. |
| 1143 |
|
| 1144 |
\section{Acknowledgements} |
| 1145 |
|
| 1146 |
Thanks to Jeremy Gibbons for letting me bounce ideas off him |
| 1147 |
and spotting the duff ones, |
| 1148 |
to Damian Cugley for saying ``Do you really think \TeX\ is meant |
| 1149 |
to do this?'', and to the Problem Solving Club for hearing me out. |
| 1150 |
This work was sponsored by the Science and Engineering |
| 1151 |
Research Council and Hewlett Packard. |
| 1152 |
|
| 1153 |
\makesignature |
| 1154 |
|
| 1155 |
\end{document} |
| 1156 |
|