-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate
executable file
·531 lines (424 loc) · 24.8 KB
/
translate
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#!/usr/bin/python3
import sys
from enum import Enum
from dataclasses import dataclass
from typing import NewType, Iterable
import itertools
import hashlib
Letter = NewType('Letter', str)
State = NewType('State', str)
Direction = NewType('Direction', str)
LETTER_BEGIN: Letter = Letter("#")
BLANK: Letter = Letter("0")
STATE_INIT: State = State("start")
STATE_ACCEPT: State = State("accept")
STATE_REJECT: State = State("reject")
DIRECTION_LEFT: Direction = Direction("L")
DIRECTION_RIGHT: Direction = Direction("R")
DIRECTION_STAY: Direction = Direction("S")
DIRECTIONS = [DIRECTION_LEFT, DIRECTION_RIGHT, DIRECTION_STAY]
ALPHABET = [BLANK]
ALPHABET_WITHOUT_BLANK = []
class Head(Enum):
H0 = 'H0' # No head at this cell.
H1 = 'H1' # 1st tape head at this cell.
H2 = 'H2' # 2nd tape head at this cell.
H12 = 'H12' # 1st and 2nd heads at this cell.
HEADS = [Head.H0, Head.H1, Head.H2, Head.H12]
def unique(values):
unique_list = {}
results = []
for v in values:
h = v.md5()
if h in unique_list:
continue
unique_list[h] = None
results.append(v)
return results
@dataclass
class Transition:
state: State
letter_current: Letter
state_target: State
letter_to_write: Letter
direction: Direction
def __str__(self):
return f"{self.state} {self.letter_current} {self.state_target} "\
f"{self.letter_to_write} {self.direction}"
def md5(self):
result = hashlib.md5(str(self).encode("utf-8"))
return result.digest()
@dataclass
class TransitionTwoTape:
state: State
letter_current: Letter
letter2_current: Letter
state_target: State
letter_to_write: Letter
letter2_to_write: Letter
direction1: Direction
direction2: Direction
def prepare_alphabet(transitions: Iterable[TransitionTwoTape]):
alphabet = {}
for t in transitions:
alphabet[t.letter_current] = None
alphabet[t.letter2_current] = None
alphabet[t.letter_to_write] = None
alphabet[t.letter2_to_write] = None
for a in alphabet.keys():
if a == BLANK:
continue
ALPHABET.append(a)
for a in ALPHABET[1:]:
ALPHABET_WITHOUT_BLANK.append(a)
@dataclass
class MachineLetter:
letter1: Letter
letter2: Letter
head: Head
def __str__(self):
return f"{self.letter1}_{self.letter2}_{self.head.value}"
class Transitions:
def __init__(self):
self._transitions = []
def add(self, transition: Transition):
self._transitions.append(transition)
def get_all(self) -> Iterable[Transition]:
return self._transitions
class TapeInitialiser:
_name = "init"
_state_exit: str
def __init__(self, next_state: str):
self._state_exit = next_state
def _move_input_to_right(self) -> Iterable[Transition]:
step_name = self._name + "_inputmoveright"
t = Transitions()
state_step1 = step_name + "1"
state_step2 = step_name + "2"
state_exit = self._name + "_translatealphabet"
# 1. Go to right until we find a BLANK.
t.add(Transition(STATE_INIT, BLANK, state_exit, LETTER_BEGIN, DIRECTION_RIGHT))
for l in ALPHABET_WITHOUT_BLANK:
t.add(Transition(STATE_INIT, l, state_step1, LETTER_BEGIN+l, DIRECTION_STAY))
for l in ALPHABET_WITHOUT_BLANK:
t.add(Transition(state_step1, l, state_step1, l, DIRECTION_RIGHT))
t.add(Transition(state_step1, LETTER_BEGIN+l, state_step1, LETTER_BEGIN+l, DIRECTION_RIGHT))
t.add(Transition(state_step1, BLANK, state_step2, BLANK, DIRECTION_LEFT))
# 2. Move characters to right by one, going to the left (beginning) until # found.
for a, b in itertools.product(ALPHABET_WITHOUT_BLANK, ["#", ""]):
l = a + b
write_letter = state_step2+"_"+l
go_left = write_letter+"_goleft"
t.add(Transition(state_step2, l, write_letter, l, DIRECTION_RIGHT))
for ln in ALPHABET:
t.add(Transition(write_letter, ln, go_left, l, DIRECTION_LEFT))
t.add(Transition(go_left, l, state_step2, l, DIRECTION_LEFT))
for l in ALPHABET_WITHOUT_BLANK:
state_before_exit = state_exit + f"_{l}"
t.add(Transition(state_step2, LETTER_BEGIN+l, state_before_exit, LETTER_BEGIN, DIRECTION_RIGHT))
for l2 in ALPHABET_WITHOUT_BLANK:
t.add(Transition(state_before_exit, l2, state_exit, l, DIRECTION_STAY))
return t.get_all()
def _change_alphabet(self) -> Iterable[Transition]:
step_name = self._name + "_translatealphabet"
state_step1 = step_name
state_step1_goback = step_name + "_goback"
state_step2_fixheads = step_name + "_fixheads"
state_exit = self._state_exit
t = Transitions()
for l in ALPHABET_WITHOUT_BLANK:
t.add(Transition(state_step1, l, state_step1, MachineLetter(l, BLANK, Head.H0), DIRECTION_RIGHT))
t.add(Transition(state_step1, BLANK, state_step1_goback, MachineLetter(BLANK, BLANK, Head.H0), DIRECTION_LEFT))
for l in ALPHABET_WITHOUT_BLANK:
t.add(Transition(state_step1_goback, MachineLetter(l, BLANK, Head.H0), state_step1_goback, MachineLetter(l, BLANK, Head.H0), DIRECTION_LEFT))
t.add(Transition(state_step1_goback, LETTER_BEGIN, state_step2_fixheads, LETTER_BEGIN, DIRECTION_RIGHT))
for l in ALPHABET:
t.add(Transition(state_step2_fixheads, MachineLetter(l, BLANK, Head.H0), state_exit, MachineLetter(l, BLANK, Head.H12), DIRECTION_LEFT))
return t.get_all()
def translate(self) -> Iterable[Transition]:
transitions = self._move_input_to_right()
transitions += self._change_alphabet()
return transitions
@dataclass
class TransitionPrepare:
name: State
letter1: Letter
letter2: Letter
def __str__(self):
return f"{self.name}_{self.letter1}_{self.letter2}"
@dataclass
class TransitionEmulate:
name: State
letter1: Letter
direction1: Direction
letter2: Letter
direction2: Direction
def __str__(self):
letter1 = f"{self.letter1}{self.direction1}"
letter2 = f"{self.letter2}{self.direction2}"
if self.letter1 == True:
letter1 = "DONE"
if self.letter2 == True:
letter2 = "DONE"
return f"{self.name}_{letter1}_{letter2}"
class TransitionsEmulator:
start_state = "init"
_states: [State] = []
def _init_emulator(self) -> Iterable[Transition]:
t = Transitions()
t.add(Transition(
"init",
"#",
TransitionPrepare("input_letters", None, None),
"#start",
DIRECTION_RIGHT
))
return t.get_all()
def _find_states(self, transitions: Iterable[TransitionTwoTape]):
for t in transitions:
if t.state not in self._states:
self._states.append(t.state)
if t.state_target not in self._states:
self._states.append(t.state_target)
def _find_input_letters(self) -> Iterable[Transition]:
t = Transitions()
step_name = "input_letters"
for found1, found2 in itertools.product([False, True], [False, True]):
for l1, l2 in itertools.product(ALPHABET, ALPHABET):
for sl1, sl2 in itertools.product(ALPHABET, ALPHABET):
direction = DIRECTION_RIGHT
if found1 and found2:
direction = DIRECTION_LEFT
l1v = sl1
if not found1:
l1v = None
l2v = sl2
if not found2:
l2v = None
t.add(Transition(TransitionPrepare(step_name, l1v, l2v), MachineLetter(l1, l2, Head.H0), TransitionPrepare(step_name, l1v, l2v), MachineLetter(l1, l2, Head.H0), direction))
t.add(Transition(TransitionPrepare(step_name, l1v, l2v), MachineLetter(l1, l2, Head.H1), TransitionPrepare(step_name, l1, l2v), MachineLetter(l1, l2, Head.H1), direction))
t.add(Transition(TransitionPrepare(step_name, l1v, l2v), MachineLetter(l1, l2, Head.H2), TransitionPrepare(step_name, l1v, l2), MachineLetter(l1, l2, Head.H2), direction))
t.add(Transition(TransitionPrepare(step_name, l1v, l2v), MachineLetter(l1, l2, Head.H12), TransitionPrepare(step_name, l1, l2), MachineLetter(l1, l2, Head.H12), DIRECTION_LEFT))
if found1 and found2:
t.add(Transition(TransitionPrepare(step_name, l1v, l2v), BLANK, TransitionPrepare(step_name, l1v, l2v), BLANK, DIRECTION_LEFT))
return t.get_all()
def _emulate_the_transition(self, transition: TransitionTwoTape) -> Iterable[Transition]:
t = Transitions()
if transition.state_target in [STATE_ACCEPT, STATE_REJECT]:
t.add(Transition(
TransitionPrepare("input_letters", transition.letter_current, transition.letter2_current),
"#" + transition.state,
transition.state_target,
LETTER_BEGIN,
DIRECTION_STAY
))
else:
t.add(Transition(
TransitionPrepare("input_letters", transition.letter_current, transition.letter2_current),
"#" + transition.state,
TransitionEmulate("emulate_move", transition.letter_to_write, transition.direction1, transition.letter2_to_write, transition.direction2),
"#" + transition.state_target,
DIRECTION_RIGHT
))
t.add(Transition(
"emulate_move_DONE_DONE",
"#" + transition.state,
"input_letters_None_None",
"#" + transition.state,
DIRECTION_RIGHT
))
return t.get_all()
def _emulate_the_move(self) -> Iterable[Transition]:
t = Transitions()
step_name = "emulate_move"
for l1, l2 in itertools.product(ALPHABET, ALPHABET):
for d1, d2 in itertools.product(DIRECTIONS, DIRECTIONS):
for ml1, ml2 in itertools.product(ALPHABET, ALPHABET):
for done1, done2 in itertools.product([False, True], [False, True]):
if done1 and done2:
for h in [Head.H0, Head.H1, Head.H2, Head.H12]:
state_done = TransitionEmulate(step_name, True, None, True, None)
t.add(Transition(state_done, MachineLetter(l1, l2, h), state_done, MachineLetter(l1, l2, h), DIRECTION_LEFT))
continue
l1v = ml1
if done1:
l1v = True
l2v = ml2
if done2:
l2v = True
state = TransitionEmulate(step_name, l1v, d1, l2v, d2)
t.add(Transition(state, MachineLetter(l1, l2, Head.H0), state, MachineLetter(l1, l2, Head.H0), DIRECTION_RIGHT))
if not done1:
state = TransitionEmulate(step_name, l1v, d1, l2v, d2)
state_target = TransitionEmulate(step_name+"_do1", l1v, d1, l2v, d2)
t.add(Transition(state, MachineLetter(l1, l2, Head.H1), state_target, MachineLetter(l1, l2, Head.H1), DIRECTION_STAY))
t.add(Transition(state, MachineLetter(l1, l2, Head.H12), state_target, MachineLetter(l1, l2, Head.H12), DIRECTION_STAY))
t.add(Transition(state, MachineLetter(l1, l2, Head.H2), state, MachineLetter(l1, l2, Head.H2), DIRECTION_RIGHT))
if not done2:
state = TransitionEmulate(step_name, l1v, d1, l2v, d2)
state_target = TransitionEmulate(step_name+"_do2", l1v, d1, l2v, d2)
t.add(Transition(state, MachineLetter(l1, l2, Head.H2), state_target, MachineLetter(l1, l2, Head.H2), DIRECTION_STAY))
t.add(Transition(state, MachineLetter(l1, l2, Head.H12), state_target, MachineLetter(l1, l2, Head.H12), DIRECTION_STAY))
t.add(Transition(state, MachineLetter(l1, l2, Head.H1), state, MachineLetter(l1, l2, Head.H1), DIRECTION_RIGHT))
if not done1 and not done2:
state = TransitionEmulate(step_name, l1v, d1, l2v, d2)
state_target = TransitionEmulate(step_name+"_do12", l1v, d1, l2v, d2)
t.add(Transition(state, MachineLetter(l1, l2, Head.H12), state_target, MachineLetter(l1, l2, Head.H12), DIRECTION_STAY))
return t.get_all()
def _emulate_move_do2(self) -> Iterable[Transition]:
t = Transitions()
step_name = "emulate_move"
for l1, l2 in itertools.product(ALPHABET, ALPHABET):
for d1, d2 in itertools.product(DIRECTIONS, DIRECTIONS):
for sl1, sl2 in itertools.product(ALPHABET, ALPHABET):
for done1 in [False, True]:
l1v = sl1
if done1:
l1v = True
if d2 == DIRECTION_STAY:
state = TransitionEmulate(step_name+"_do2", l1v, d1, sl2, d2)
state_target = TransitionEmulate(step_name, l1v, d1, True, None)
t.add(Transition(state, MachineLetter(l1, l2, Head.H2), state_target, MachineLetter(l1, sl2, Head.H2), DIRECTION_STAY))
t.add(Transition(state, MachineLetter(l1, l2, Head.H12), state_target, MachineLetter(l1, sl2, Head.H12), DIRECTION_STAY))
if d2 == DIRECTION_LEFT:
state = TransitionEmulate(step_name+"_do2", l1v, d1, sl2, d2)
state_target = TransitionEmulate(step_name + "_do2r", l1v, d1, True, None)
t.add(Transition(state, MachineLetter(l1, l2, Head.H2), state_target, MachineLetter(l1, sl2, Head.H0), DIRECTION_LEFT))
t.add(Transition(state, MachineLetter(l1, l2, Head.H12), state_target, MachineLetter(l1, sl2, Head.H1), DIRECTION_LEFT))
state_target2 = TransitionEmulate(step_name, l1v, d1, True, None)
t.add(Transition(state_target, MachineLetter(l1, l2, Head.H0), state_target2, MachineLetter(l1, l2, Head.H2), DIRECTION_RIGHT))
t.add(Transition(state_target, MachineLetter(l1, l2, Head.H1), state_target2, MachineLetter(l1, l2, Head.H12), DIRECTION_RIGHT))
for s in self._states:
letter_meta = LETTER_BEGIN + s
state_targetp = TransitionEmulate(step_name + "_do2rr", sl1, d1, sl2, d2)
t.add(Transition(state_target, letter_meta, state_targetp, letter_meta, DIRECTION_RIGHT))
t.add(Transition(state_targetp, MachineLetter(l1, l2, Head.H0), state_target2, MachineLetter(l1, l2, Head.H2), DIRECTION_STAY))
t.add(Transition(state_targetp, MachineLetter(l1, l2, Head.H1), state_target2, MachineLetter(l1, l2, Head.H12), DIRECTION_STAY))
if d2 == DIRECTION_RIGHT:
state = TransitionEmulate(step_name+"_do2", l1v, d1, sl2, d2)
state_target = TransitionEmulate(step_name + "_do2l", l1v, d1, True, None)
t.add(Transition(state, MachineLetter(l1, l2, Head.H2), state_target, MachineLetter(l1, sl2, Head.H0), DIRECTION_RIGHT))
t.add(Transition(state, MachineLetter(l1, l2, Head.H12), state_target, MachineLetter(l1, sl2, Head.H1), DIRECTION_RIGHT))
state_target2 = TransitionEmulate(step_name, l1v, d1, True, None)
t.add(Transition(state_target, BLANK, state_target2, MachineLetter(BLANK, BLANK, Head.H2),DIRECTION_LEFT))
t.add(Transition(state_target, MachineLetter(l1, l2, Head.H0), state_target2, MachineLetter(l1, l2, Head.H2), DIRECTION_LEFT))
t.add(Transition(state_target, MachineLetter(l1, l2, Head.H1), state_target2, MachineLetter(l1, l2, Head.H12), DIRECTION_LEFT))
return t.get_all()
def _emulate_move_do1(self) -> Iterable[Transition]:
t = Transitions()
step_name = "emulate_move"
for l1, l2 in itertools.product(ALPHABET, ALPHABET):
for d1, d2 in itertools.product(DIRECTIONS, DIRECTIONS):
for sl1, sl2 in itertools.product(ALPHABET, ALPHABET):
for done2 in [False, True]:
l2v = sl2
if done2:
l2v = True
if d1 == DIRECTION_STAY:
state = TransitionEmulate(step_name+"_do1", sl1, d1, l2v, d2)
state_target = TransitionEmulate(step_name, True, None, l2v, d2)
t.add(Transition(state, MachineLetter(l1, l2, Head.H1), state_target, MachineLetter(sl1, l2, Head.H1), DIRECTION_STAY))
t.add(Transition(state, MachineLetter(l1, l2, Head.H12), state_target, MachineLetter(sl1, l2, Head.H12), DIRECTION_STAY))
if d1 == DIRECTION_LEFT:
state = TransitionEmulate(step_name+"_do1", sl1, d1, l2v, d2)
state_target = TransitionEmulate(step_name + "_do1r", True, None, l2v, d2)
t.add(Transition(state, MachineLetter(l1, l2, Head.H1), state_target, MachineLetter(sl1, l2, Head.H0), DIRECTION_LEFT))
t.add(Transition(state, MachineLetter(l1, l2, Head.H12), state_target, MachineLetter(sl1, l2, Head.H2), DIRECTION_LEFT))
state_target2 = TransitionEmulate(step_name, True, None, l2v, d2)
t.add(Transition(state_target, MachineLetter(l1, l2, Head.H0), state_target2, MachineLetter(l1, l2, Head.H1), DIRECTION_RIGHT))
t.add(Transition(state_target, MachineLetter(l1, l2, Head.H2), state_target2, MachineLetter(l1, l2, Head.H12), DIRECTION_RIGHT))
for s in self._states:
letter_meta = LETTER_BEGIN + s
state_targetp = TransitionEmulate(step_name + "_do1rr", sl1, d1, sl2, d2)
t.add(Transition(state_target, letter_meta, state_targetp, letter_meta, DIRECTION_RIGHT))
t.add(Transition(state_targetp, MachineLetter(l1, l2, Head.H0), state_target2, MachineLetter(l1, l2, Head.H1), DIRECTION_STAY))
t.add(Transition(state_targetp, MachineLetter(l1, l2, Head.H2), state_target2, MachineLetter(l1, l2, Head.H12), DIRECTION_STAY))
if d1 == DIRECTION_RIGHT:
state = TransitionEmulate(step_name+"_do1", sl1, d1, l2v, d2)
state_target = TransitionEmulate(step_name + "_do1l", True, None, l2v, d2)
t.add(Transition(state, MachineLetter(l1, l2, Head.H1), state_target, MachineLetter(sl1, l2, Head.H0), DIRECTION_RIGHT))
t.add(Transition(state, MachineLetter(l1, l2, Head.H12), state_target, MachineLetter(sl1, l2, Head.H2), DIRECTION_RIGHT))
state_target2 = TransitionEmulate(step_name, True, None, l2v, d2)
t.add(Transition(state_target, BLANK, state_target2, MachineLetter(BLANK, BLANK, Head.H1), DIRECTION_LEFT))
t.add(Transition(state_target, MachineLetter(l1, l2, Head.H0), state_target2, MachineLetter(l1, l2, Head.H1), DIRECTION_LEFT))
t.add(Transition(state_target, MachineLetter(l1, l2, Head.H2), state_target2, MachineLetter(l1, l2, Head.H12), DIRECTION_LEFT))
return t.get_all()
def _emulate_move_do12(self) -> Iterable[Transition]:
t = Transitions()
step_name = "emulate_move"
for l1, l2 in itertools.product(ALPHABET, ALPHABET):
for d1, d2 in itertools.product(DIRECTIONS, DIRECTIONS):
for sl1, sl2 in itertools.product(ALPHABET, ALPHABET):
if d1 == DIRECTION_STAY:
state = TransitionEmulate(step_name+"_do12", sl1, d1, sl2, d2)
state_target = TransitionEmulate(step_name+"_do2", True, None, sl2, d2)
t.add(Transition(state, MachineLetter(l1, l2, Head.H12), state_target, MachineLetter(sl1, l2, Head.H12), DIRECTION_STAY))
if d1 == DIRECTION_LEFT:
state1 = TransitionEmulate(step_name+"_do12", sl1, d1, sl2, d2)
state2 = TransitionEmulate(step_name+"_do12l", sl1, d1, sl2, d2)
t.add(Transition(state1, MachineLetter(l1, l2, Head.H12), state2, MachineLetter(sl1, l2, Head.H2), DIRECTION_LEFT))
state1 = TransitionEmulate(step_name+"_do12l", sl1, d1, sl2, d2)
state2 = TransitionEmulate(step_name+"_do2", True, None, sl2, d2)
t.add(Transition(state1, MachineLetter(l1, l2, Head.H0), state2, MachineLetter(l1, l2, Head.H1), DIRECTION_RIGHT))
for s in self._states:
letter_meta = LETTER_BEGIN + s
state1p = TransitionEmulate(step_name + "_do12ll", sl1, d1, sl2, d2)
t.add(Transition(state1, letter_meta, state1p, letter_meta, DIRECTION_RIGHT))
t.add(Transition(state1p, MachineLetter(l1, l2, Head.H0), state2, MachineLetter(l1, l2, Head.H1), DIRECTION_STAY))
t.add(Transition(state1p, MachineLetter(l1, l2, Head.H2), state2, MachineLetter(l1, l2, Head.H12), DIRECTION_STAY))
if d1 == DIRECTION_RIGHT:
state1 = TransitionEmulate(step_name+"_do12", sl1, d1, sl2, d2)
state2 = TransitionEmulate(step_name+"_do12r", sl1, d1, sl2, d2)
t.add(Transition(state1, MachineLetter(l1, l2, Head.H12), state2, MachineLetter(sl1, l2, Head.H2), DIRECTION_RIGHT))
state1 = TransitionEmulate(step_name+"_do12r", sl1, d1, sl2, d2)
state2 = TransitionEmulate(step_name+"_do2", True, None, sl2, d2)
t.add(Transition(state1, BLANK, state2, MachineLetter(BLANK, BLANK, Head.H1), DIRECTION_LEFT))
t.add(Transition(state1, MachineLetter(l1, l2, Head.H0), state2, MachineLetter(l1, l2, Head.H1), DIRECTION_LEFT))
return t.get_all()
def translate(self, transitions: Iterable[TransitionTwoTape]) -> Iterable[Transition]:
self._find_states(transitions)
result = []
result += self._init_emulator()
result += self._find_input_letters()
result += self._emulate_the_move()
result += self._emulate_move_do12()
result += self._emulate_move_do2()
result += self._emulate_move_do1()
for t in transitions:
result += self._emulate_the_transition(t)
return result
def translate(transitions_two_tape: Iterable[TransitionTwoTape]) -> Iterable[Transition]:
prepare_alphabet(transitions_two_tape)
emulator = TransitionsEmulator()
transitions = []
# 1.1 Move input by 1 field to right.
# 1.2 Set the first cell as # (character which defines the beginning).
# 1.3 Replace the alphabet to format {(CharTape1, CharTape2, Head)} + {#}.
# In the end, head should be at the second cell (first input letter), right after # character
# which indicates the beginning of tape.
transitions += TapeInitialiser(next_state=emulator.start_state).translate()
# 2 Emulate transitions.
transitions += emulator.translate(transitions_two_tape)
return unique(transitions)
def parse_file(filepath: str) -> Iterable[TransitionTwoTape]:
transitions: [Transition] = []
try:
with open(filepath, "r") as f:
for line in f.readlines():
line = line.replace("\n", "")
if len(line.replace(" ", "")) == 0:
continue
if len(line) >= 2 and line[:2] == "//":
continue
transitions.append(TransitionTwoTape(*line.split(" ")))
except FileNotFoundError as e:
print(e)
sys.exit(1)
return transitions
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Example: ./translate <path_to_a_two_tape_turing_machine>")
sys.exit(0)
transitions = parse_file(sys.argv[1])
transitions_one_tape = translate(transitions)
print("\n".join([str(t) for t in transitions_one_tape]))