-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappendix-programs.tex
More file actions
419 lines (365 loc) · 9.52 KB
/
Copy pathappendix-programs.tex
File metadata and controls
419 lines (365 loc) · 9.52 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
This appendix collects the Pascal programs and program fragments that appear in
the text, together with direct Python 3 and Rust translations. The translations
preserve the same automata, transition tables, start states, final states, and
input conventions as the Pascal originals.
\section{State Transition Function}
\subsection*{Original Pascal}
\begin{verbatim}
type
Sigma = 'a'..'c';
State = (s0,s1,s2);
var
TransitionTable=array[State,Sigma] of State;
function Delta(S:State;A:Sigma):State;
begin
Delta := TransitionTable [S, A]
end; {Delta}
\end{verbatim}
\subsection*{Python 3}
\begin{verbatim}
from enum import Enum
from collections.abc import Mapping
class State(Enum):
S0 = 0
S1 = 1
S2 = 2
TransitionTable = Mapping[tuple[State, str], State]
def delta(transition_table: TransitionTable,
state: State,
letter: str) -> State:
return transition_table[(state, letter)]
\end{verbatim}
\subsection*{Rust}
\begin{verbatim}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum State {
S0,
S1,
S2,
}
fn state_index(state: State) -> usize {
match state {
State::S0 => 0,
State::S1 => 1,
State::S2 => 2,
}
}
fn sigma_index(letter: char) -> usize {
match letter {
'a' => 0,
'b' => 1,
'c' => 2,
_ => panic!("letter outside Sigma"),
}
}
fn delta(transition_table: &[[State; 3]; 3],
state: State,
letter: char) -> State {
transition_table[state_index(state)][sigma_index(letter)]
}
\end{verbatim}
\section{Extended State Transition Function}
\subsection*{Original Pascal}
\begin{verbatim}
const
MaxWordLength = 255; {an arbitrary constraint}
type
Word = record
Length :0..MaxWordLength;
Letters:packed array [0..MaxWordLength] of Sigma
end; {Word}
function DeltaBar(S:State; W:Word) : State;
{uses the function Delta defined previously}
var
T:State;
I:0..MaxWordLength;
begin
T := S;
if W.Length>0
then
for I := 1 to W.Length do
T := Delta(T, W.Letters[I]);
DeltaBar := T
end; {DeltaBar}
\end{verbatim}
\subsection*{Python 3}
\begin{verbatim}
MAX_WORD_LENGTH = 255
def delta_bar(transition_table: TransitionTable,
state: State,
word: str) -> State:
if len(word) > MAX_WORD_LENGTH:
raise ValueError("word exceeds MaxWordLength")
current = state
for letter in word:
current = delta(transition_table, current, letter)
return current
\end{verbatim}
\subsection*{Rust}
\begin{verbatim}
const MAX_WORD_LENGTH: usize = 255;
fn delta_bar(transition_table: &[[State; 3]; 3],
state: State,
word: &str) -> State {
if word.chars().count() > MAX_WORD_LENGTH {
panic!("word exceeds MaxWordLength");
}
let mut current = state;
for letter in word.chars() {
current = delta(transition_table, current, letter);
}
current
}
\end{verbatim}
\section{Acceptance Test}
\subsection*{Original Pascal}
\begin{verbatim}
function Accept(W:Word):Boolean;
{returns TRUE iff W is accepted by the DFA}
begin
Accept := DeltaBar(s0, W) in FinalState
end; {Accept}
\end{verbatim}
\subsection*{Python 3}
\begin{verbatim}
from collections.abc import Set
def accept(transition_table: TransitionTable,
final_state: Set[State],
word: str) -> bool:
return delta_bar(transition_table, State.S0, word) in final_state
\end{verbatim}
\subsection*{Rust}
\begin{verbatim}
fn accept(transition_table: &[[State; 3]; 3],
final_state: &[State],
word: &str) -> bool {
final_state.contains(&delta_bar(transition_table, State::S0, word))
}
\end{verbatim}
\section{DFA Emulator}
\subsection*{Original Pascal}
\begin{verbatim}
program DFA(input, output);
{This program tests whether input strings are accepted by the }
{automaton displayed in Figure 1.9. The program expects input from}
{the keyboard, delimited by a carriage return. No error checking }
{is done; letters outside ['a' .. 'c'] cause a range error. }
type
Sigma = 'a'..'c';
State = (s0, s1, s2);
var
TransitionTable : array [State, Sigma] of State;
FinalState : set of State;
function Delta(s : State; c : Sigma) : State;
begin
Delta := TransitionTable[s,c]
end; { Delta }
function DeltaBar(s : State) : State;
var
t : State;
begin
t := s;
{ Step through the keyboard input one letter at a time. }
while not eoln(input) do
begin
t := Delta(t, input^);
get(input)
end;
DeltaBar := t
end; { DeltaBar }
function Accept : boolean;
begin
Accept := DeltaBar(s0) in FinalState
end; { Accept }
procedure Initialize;
begin
FinalState := [s2];
{ Set up the state transition table. }
TransitionTable [s0,'a'] := s1; TransitionTable [s0,'b'] := s0;
TransitionTable [s0,'c'] := s2; TransitionTable [s1,'a'] := s2;
TransitionTable [s1,'b'] := s0; TransitionTable [s1,'c'] := s0;
TransitionTable [s2,'a'] := s0; TransitionTable [s2,'b'] := s0;
TransitionTable [s2,'c'] := s1;
end; { Initialize }
begin { DFA }
Initialize;
if Accept then
writeln(output, 'Accepted')
else
writeln(output, 'Rejected')
end. { DFA }
\end{verbatim}
\subsection*{Python 3}
\begin{verbatim}
from enum import Enum
import sys
class State(Enum):
S0 = 0
S1 = 1
S2 = 2
TRANSITION_TABLE = {
(State.S0, "a"): State.S1,
(State.S0, "b"): State.S0,
(State.S0, "c"): State.S2,
(State.S1, "a"): State.S2,
(State.S1, "b"): State.S0,
(State.S1, "c"): State.S0,
(State.S2, "a"): State.S0,
(State.S2, "b"): State.S0,
(State.S2, "c"): State.S1,
}
FINAL_STATE = {State.S2}
def delta(state: State, letter: str) -> State:
return TRANSITION_TABLE[(state, letter)]
def delta_bar(state: State, word: str) -> State:
current = state
for letter in word:
current = delta(current, letter)
return current
def accept(word: str) -> bool:
return delta_bar(State.S0, word) in FINAL_STATE
def main() -> None:
word = sys.stdin.readline().rstrip("\n")
print("Accepted" if accept(word) else "Rejected")
if __name__ == "__main__":
main()
\end{verbatim}
\subsection*{Rust}
\begin{verbatim}
use std::io::{self, Read};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum State {
S0,
S1,
S2,
}
fn delta(state: State, letter: char) -> State {
match (state, letter) {
(State::S0, 'a') => State::S1,
(State::S0, 'b') => State::S0,
(State::S0, 'c') => State::S2,
(State::S1, 'a') => State::S2,
(State::S1, 'b') => State::S0,
(State::S1, 'c') => State::S0,
(State::S2, 'a') => State::S0,
(State::S2, 'b') => State::S0,
(State::S2, 'c') => State::S1,
_ => panic!("letter outside ['a'..'c']"),
}
}
fn delta_bar(state: State, word: &str) -> State {
let mut current = state;
for letter in word.chars() {
current = delta(current, letter);
}
current
}
fn accept(word: &str) -> bool {
delta_bar(State::S0, word) == State::S2
}
fn main() {
let mut input = String::new();
io::stdin().read_to_string(&mut input).unwrap();
let word = input.lines().next().unwrap_or("");
if accept(word) {
println!("Accepted");
} else {
println!("Rejected");
}
}
\end{verbatim}
\section{Hypothetical Use of HALT}
\subsection*{Original Pascal}
\begin{verbatim}
program CHECK;
{ envisioned usage of HALT }
function HALT: boolean;
begin
{ marvelous code goes here }
end { HALT }
begin { CHECK }
if HALT then
writeln('The program in file data.p will halt')
else
writeln('The program in file data.p will not halt')
end { CHECK }.
\end{verbatim}
\subsection*{Python 3}
\begin{verbatim}
def halt() -> bool:
# Hypothetical oracle from the proof; no such total function exists.
raise NotImplementedError("marvelous code goes here")
def main() -> None:
if halt():
print("The program in file data.p will halt")
else:
print("The program in file data.p will not halt")
if __name__ == "__main__":
main()
\end{verbatim}
\subsection*{Rust}
\begin{verbatim}
fn halt() -> bool {
// Hypothetical oracle from the proof; no such total function exists.
unimplemented!("marvelous code goes here")
}
fn main() {
if halt() {
println!("The program in file data.p will halt");
} else {
println!("The program in file data.p will not halt");
}
}
\end{verbatim}
\section{Contradictory Test Program}
\subsection*{Original Pascal}
\begin{verbatim}
program TEST;
{ to be placed in the file data.p }
var FOREVER: boolean;
function HALT: boolean;
begin
{ marvelous code goes here }
end; { HALT }
begin { TEST }
FOREVER := false;
if HALT then
repeat
FOREVER := false;
until FOREVER
else
writeln('This program halts')
end { TEST }.
\end{verbatim}
\subsection*{Python 3}
\begin{verbatim}
def halt() -> bool:
# Hypothetical oracle from the proof; no such total function exists.
raise NotImplementedError("marvelous code goes here")
def main() -> None:
forever = False
if halt():
while not forever:
forever = False
else:
print("This program halts")
if __name__ == "__main__":
main()
\end{verbatim}
\subsection*{Rust}
\begin{verbatim}
fn halt() -> bool {
// Hypothetical oracle from the proof; no such total function exists.
unimplemented!("marvelous code goes here")
}
fn main() {
let mut forever = false;
if halt() {
while !forever {
forever = false;
}
} else {
println!("This program halts");
}
}
\end{verbatim}