-
Notifications
You must be signed in to change notification settings - Fork 37
/
herrmann1.hint
363 lines (260 loc) · 12.9 KB
/
herrmann1.hint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
Best abuse of the C preprocessor:
Immanuel Herrmann
University of Freiburg, Germany
Rehlingstr. 17
79100 Freiburg
Germany
http://pcpool.mathematik.uni-freiburg.de/~immi/
Judges' Comments:
To build:
Running and building are combined for this entry.
Try:
./herrmann1.sh 'prg=herrmann1.times2'
-or-
./herrmann1.sh 'prg=herrmann1.gcd'
There are some clear hints in the source code as to the
purpose of this program. Perhaps the only way to figure out what is
going on is to look at the differences between the runs of the C
preprocessor! Computer Science students should recognize what is going
on.
Selected Author's Comments:
Many cool things have been done with the preprocessor,
e. g. calculation of prime numbers or the tower of Hanoi. However,
up to now, no one proved that the preprocessor is Turing complete,
and I wondered why. Well, the answer is simply that the
preprocessor is _not_ Turing complete, at least not if the program
is preprocessed only once. This is true even if the program is
allowed to include itself. (The reason being that for a given
program, the preprocessor has only a finite number of states, plus
a stack consisting of the places which the file has been included
from. This is only a push-down automaton.)
Well, this entry _is_ a Turing machine implemented in the
preprocessor; so of course, it has to be preprocessed several
times. Here are the main features:
- Easy to program: Write your own Turing programs in a nice
syntax.
- Easy to debug: (some) errors in the Turing machine program will
be reported nicely. (That's something which is not often seen in
IOCCC programs.)
- Ultra-fast: The resulting C program will print the output in
time O(n), where n is the length of the output. This means for
example that you can solve NP hard problems in polynomial time!
;-)
- And best of all: During the preprocessing, watch the animated
Turing machine running over the tape and doing its work in your
terminal!
How to use it
=============
For the impatient
-----------------
The info files "herrmann1.turing" and "herrmann1.gcd" are sample
programs for the Turing machine. For example, type
./herrmann1.sh 'prg=herrmann1.times2'
and watch the animated Turing machine multiply 8 by two (in
unary representation). Here's a screen shot during compilation:
/*
*
* State: st2.
*
* _ _ _ _ _ * _ _ _ _ _ _ _ _ _ _ _ _ _
* ... O O I I I I I O I I I I I O O ...
* ~ ~ ~ ~ ~ * ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
*
*
*/
(As on real videos, you'll see a bit of "white noise" at the
beginning and at the end.;-) After compilation has terminated,
type ./prog to see the final result. The output will be
Final tape:
*
..OOIIIIIIIIIIIIIIIIOOOO..
*
(Note that the last frame of the animation is _not_ the final
result.)
In the previous example, the Turing machine started on the default
tape of the program "times2.turing". To provide your own tape, type
(for example)
./build prg=times2.turing tape="O O O I I I O O O"
(The O's and I's are letters, not digits.) You might prefer to
provide only a finite portion of the tape, like I did above. In
this case, the remainder of the tape is filled with O's. ;-)
If you just type
./build
and run the program, you'll get a usage message (and the return
code will be 2).
Note: To get a really nice animation, you'll need a terminal which
is able to scroll fast enough. (xterm is not very good. The
kde terminal "konsole" or the text console under linux are much
better. Simply try some out.)
Writing your own programs
-------------------------
Now, let's assume you want to write a program which adds 1 to a
number in unary representation. (That is, a row of I's is
expected, and one more I is appended to the row.) Let's call it
"plus1.turing". Here's what it could look like:
/* Search beginning of number. */
#define start_O O, right, start
/* Number found. Move one step to the left. */
#define start_I I, left, st2
/* Write a I and stop */
#define st2_O I, left, stop
Some explanations:
- The tape alphabet is always O and I.
- The states can be anything starting with "st". "start" and
"stop" are the start and stop states, respectively.
- to define a state, use two lines: one for each letter on the
tape. You may omit one of the lines if the Turing machine will
never enter the state reading that letter. (In the example, the
line defining st2_I has been left out.)
- The order of the lines is irrelevant.
- You may use c-style comments. (Would you have guessed it?)
Now, the program can be used with
./herrmann1.sh 'prg=plus1.turing' 'tape="I I I"'
But what happens if no tape is provided? Up to now, the Turing
machine would start on an empty tape (that is, a tape filled with
O's). For many programs, this doesn't make much sense (as is
the case for "plus1.turing"). So you might want to provide a
default tape by adding a line like
#define tape O O O O O I I I I
to your program.
Debugging
---------
When writing long programs, the most common errors are to forget
to define a state or to misspell one. Let's assume you forgot the
line
#define start_I I, left, st2
Remove it and type
./herrmann1 'prg=plus1.turing'
The output will be
Undefined state start.
Tape:
*
..OOOOOOOOIIIIOO..
*
and the return code will be 1. As you can see, the Turing machine
moved over the O's, but when it arrived at the I (still in state
start), the error occurred.
Note that other kinds of errors can cause compilation to fail.
If this happens, the build script will write the error message of
gcc into a file called "error" and then exit.
What the build script does
==========================
In principle, the build script simply runs the preprocessor over
and over again, until nothing changes anymore; then, the program
is compiled. After each preprocessing step, the script outputs the
beginning of the program; this is where the animation takes place
(except in the first and last few steps).
However, there's a technical problem to overcome: Some compilers
(including gcc) insert a lot of white space during the
preprocessing. This is bad for two reasons. First, the animated
picture won't look right. And second, after some time, the file
will grow too big for your hard disk. (The amount of white space
grows exponentially.) So after each preprocessing, the first thing
the build script does is remove all the unnecessary white space
using sed.
(gcc would even insert #line directives if I didn't suppress
them with the -P option.)
Some more details
-----------------
- The arguments passed to the build script are passed on to gcc
with a "-D" before. I had a slight technical problem doing this
(especially with the spaces that can appear within the tape
argument). That made the build script a bit longer.
- It would be enough to pass the defines in the first
preprocessing. The program then "remembers" them. However, it
doesn't matter if they are passed in all preprocessings. (This
doesn't sound very impressive, but it has required some extra
lines of code.)
- During the preprocessing, intermediate files "herrmann1-1.c" and
"herrmann1-2.c" are generated.
- Also, a file "error" is generated. This file is removed after
the compilation, except if gcc produced an error; then it contains
the error message. (I can't let gcc output all its messages
directly, because the warnings would clobber the animation. I
could have made the compile script output the error file in case
of an error, but I didn't want to make it yet longer.)
Some technical details / obfuscations
====================================
- The program is compressed. In the first three preprocessing
passes, it uncompresses itself and initializes the
"variables". So if you want to understand how it works, the best
thing is to preprocess it three times and then look at it
again. (However, I wouldn't have wasted three passes if they
weren't needed anyway to initialize the tape in certain
cases. Find out why.)
- The first problem I had to solve when writing this program was
to write a preprocessor program which outputs itself. Of course,
each program which doesn't contain any preprocessor directives
is such a program, but that's not what I needed. In fact, to do
this, the program has to include itself, but even then, it's not
easy.
- The next problem to solve was how to pass results of a
calculation of one preprocessing to the next one. This is done
by outputting #defines with the result. But there's a problem
with this: it is impossible to output a "#define result xxx",
where xxx depends on the previous value of result. Try it if you
don't believe it. And find out what I did instead.
- What's a useful way to store information, especially the content
of the tape? Of course, a "#define tape OOIOOIOIIIIO" contains
the information, but then, how do you access the information?
(For example, you won't be able to write preprocessor code which
replaces the first O by I.) My answer looks very much like the
way a stack would have been implemented in lambda calculus. (The
tape is implemented as two stacks, to be exact.) And still,
there were some technical problems to solve. Look at the program
for more details.
- There are many other interesting things to look at, e.g. the way
to access the information in the user-provided program, the way
a non-defined state is detected, the way the picture is
produced, etc. If you want a real challenge, try to understand
what the line "w j k(P ## x)" does.
- Due to the fact that our alphabet has only 53 ;-) letters, I had
to use many variables more than once.
- I couldn't refrain from doing a little bit of nasty stuff like
#define a 0&0
#if !a
Of course, it is a bit more subtle, and even useful.
- The layout of the source code..., well, let's call it the
"modern art of programming" - a mixture of program-like
indenting and modern art. There are even some comments...
Compatibility
=============
- It works with many versions of gcc. I tested it with versions
2.7.2.1, 2.8.1, 2.95.2, and ecgs-2.91.66. However, an old version
of gcc produced "internal compiler errors" (obviously not my
fault).
- The only warnings I get tell me that I'm undefining __FILE__ and
that preprocessing directives are not recognized within macro
args. My answer to this is "yes, I know this and it's all
right". With "-Wall", I also get some warnings about "/*"
inside comments (Well, _I_ like some well-placed "/*/*/".) as
well as some suggestions where to place some more parentheses
(as if I didn't know the operator precedence).
- Different versions of cc all produced only rubbish. Apparently,
the preprocessors didn't really work. (I don't feel guilty
there, either. Even with very simple programs, they produced
mysterious output.)
- For Mac-Users: It works with the C compiler of Codewarrior (an
old version). This compiler even leaves out all that unnecessary
white space, so that no clean-up is needed. (It would be useful
to write a kind of script which does the equivalent to the build
script, so that one does not have to choose "preprocess" from
the menu over and over again. I leave this as an exercise to the
experienced Mac programmer.)
- In the build script, I passed the -P option to gcc to suppress
the insertion of #line directives. If your compiler doesn't
support this option (and does insert #line directives), you
should change the call to sed to remove them, too: Add
-e "/^#line/ d"
to the sed parameters. Or, if your #line directives look like
# 15 "filename"
add
-e "/^# [0-9]/ d"
instead.
- First, I had "-ansi -pedantic" turned on, but on some systems,
the stdio.h is not compatible to that, so I turned it back off.
- The build script probably needs a bash to run in. So perhaps you
have to add "#!/bin/bash" or something like this at the
beginning.
- On SunOS, I had to remove the "-q" option from diff in the build
script.