-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_utm.py
More file actions
294 lines (258 loc) · 18.4 KB
/
create_utm.py
File metadata and controls
294 lines (258 loc) · 18.4 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
# EXPECTED FORMAT OF THE UTM INPUT:
# <initial_state> | <sub_machine_input> | <transitions> | <final_state>
#
# EXPECTED FORMAT OF TRANSITIONS:
# <from_state> <from_letter> <to_state> <to_letter> <movement>
#
# each of the above items is represented by a letter of the input_alphabet,
# it is up to the user to decide how to distribute those input characters.
#
# EXAMPLE:
# translation of user input:
# '1' = '1'
# '+' = '+'
# '=' = '='
# '.' = '.'
# 'a' = 'scanright'
# 'b' = 'replaceplus'
# 'c' = 'eraseone'
# 'd' = 'halt'
#
# ft_turing input for a unary_add: "a|111+11=|a.a.Ra1a1Ra+b1Rb1b1Rb=c.Lc1d.R|d"
# ft_turing expected output for the above: "a|11111..|a.a.Ra1a1Ra+b1Rb1b1Rb=c.Lc1d.R|d"
#
# EXAMPLE 2:
# translation of user input:
# '1' = '1'
# '.' = '.'
# '-' = '-'
# '=' = '='
# 'a' = 'scanright'
# 'b' = 'eraseone'
# 'c' = 'subone'
# 'd' = 'skip'
# 'e' = 'HALT'
#
# ft_turing input for a unary_sub: "a|111-11=|a.a.Ra1a1Ra-a-Ra=b.Lb1c=Lb-e.Lc1c1Lc-d-Ld.d.Ld1a.R|e"
# ft_turing expected output for the above: "a|1......|a.a.Ra1a1Ra-a-Ra=b.Lb1c=Lb-e.Lc1c1Lc-d-Ld.d.Ld1a.R|e"
import json
# user alphabet
input_alphabet = ["a", "b", "c", "d", "e", "1", "+", "-", ".", "="]
#reserved alphabet
input_alphabet_marker = "!"
section_separator = "|"
movement_chars = ["L", "R"]
transitions_alphabet = input_alphabet + movement_chars
blank = "~"
#total machine alphabet
machine_alphabet = [blank] + input_alphabet + [input_alphabet_marker] + [section_separator] + movement_chars
initial_state = "get_init_state"
final_state = "HALT"
# fill in json info
machine_description = {}
machine_description["name"] = "universal_turing_machine"
machine_description["alphabet"] = machine_alphabet
machine_description["blank"] = blank
machine_description["states"] = []
machine_description["initial"] = initial_state
machine_description["finals"] = [final_state]
machine_description["transitions"] = {}
def create_new_state(name):
machine_description["states"].append(name)
machine_description["transitions"][name] = []
def create_new_transition(from_state, read, to_state, write, action):
machine_description["transitions"][from_state].append({
"read" : read,
"to_state" : to_state,
"write" : write,
"action" : action
})
create_new_state(initial_state)
create_new_state(final_state)
# buscar el estado inicial
for s in input_alphabet:
find_input_from_state_s = f"find_input_from_state_{s}"
create_new_state(find_input_from_state_s)
create_new_transition(initial_state, s, find_input_from_state_s, s, "RIGHT")
# saltarse el separador
create_new_transition(find_input_from_state_s, section_separator, find_input_from_state_s, section_separator, "RIGHT")
# encontrar el primer carácter del input de la submáquina (desde el estado inicial) y marcarlo
for l in input_alphabet:
find_trans_from_state_s_letter_l = f"find_trans_from_state_{s}_letter_{l}"
create_new_state(find_trans_from_state_s_letter_l)
create_new_transition(find_input_from_state_s, l, find_trans_from_state_s_letter_l, input_alphabet_marker, "RIGHT")
# buscar transición para el estado y letra actual, saltándose todo el input hasta llegar al 2º separador
for l2 in input_alphabet:
create_new_transition(find_trans_from_state_s_letter_l, l2, find_trans_from_state_s_letter_l, l2, "RIGHT")
# cuando encuentra el segundo separador de sección, se lo salta pero entra en un nuevo estado
# para comprobar si la siguiente letra que encuentre es el state que busca:
for s in input_alphabet:
for l in input_alphabet:
find_trans_from_state_s_letter_l = f"find_trans_from_state_{s}_letter_{l}"
check_state_from_state_s_letter_l = f"check_state_from_state_{s}_letter_{l}"
create_new_state(check_state_from_state_s_letter_l)
create_new_transition(find_trans_from_state_s_letter_l, section_separator, check_state_from_state_s_letter_l, section_separator, "RIGHT")
# si encuentra una "R" o "L", que separan transiciones, tiene el mismo comportamiento que con el separador 2 (se usa para saltar de una transición a otra):
create_new_transition(find_trans_from_state_s_letter_l, "R", check_state_from_state_s_letter_l, "R", "RIGHT")
create_new_transition(find_trans_from_state_s_letter_l, "L", check_state_from_state_s_letter_l, "L", "RIGHT")
# comprobar si la siguiente letra que encuentre es el state que busca:
for l2 in input_alphabet:
if l2 != s:
# si el state NO ha coincidido, buscar la siguiente transition (se reusan las transiciones que hemos creado anteriormente):
create_new_transition(check_state_from_state_s_letter_l, l2, find_trans_from_state_s_letter_l, l2, "RIGHT")
else:
# si sí coincide el state, saltárselo para comprobar si la siguiente letra es la letter que buscamos:
check_letter_from_state_s_letter_l = f"check_letter_from_state_{s}_letter_{l}"
create_new_state(check_letter_from_state_s_letter_l)
create_new_transition(check_state_from_state_s_letter_l, l2, check_letter_from_state_s_letter_l, l2, "RIGHT")
# si el state ha coincidido, comprobar si la siguiente letra que encuentre es la letter que busca:
find_new_state = "find_new_state"
create_new_state(find_new_state)
for s in input_alphabet:
for l in input_alphabet:
find_trans_from_state_s_letter_l = f"find_trans_from_state_{s}_letter_{l}"
check_letter_from_state_s_letter_l = f"check_letter_from_state_{s}_letter_{l}"
for l2 in input_alphabet:
if l2 != l:
# si la letra NO ha coincidido, buscar la siguiente transition (se reusan las transiciones que hemos creado anteriormente):
# FIXME: se tiene que saltar tb el R y L
create_new_transition(check_letter_from_state_s_letter_l, l2, find_trans_from_state_s_letter_l, l2, "RIGHT")
else:
# saltarse la letra actual para encontrar el to_state al que hay que cambiar:
create_new_transition(check_letter_from_state_s_letter_l, l2, find_new_state, l2, "RIGHT")
# si tanto el state como la letra han coincidido con los que buscábamos, guardarse el state y letra al que hay que cambiar, y el L o R de la transición
for s in input_alphabet:
find_new_letter_for_new_state_s = f"find_new_letter_for_new_state_{s}"
create_new_state(find_new_letter_for_new_state_s)
create_new_transition(find_new_state, s, find_new_letter_for_new_state_s, s, "RIGHT")
for l in input_alphabet:
find_movement_for_new_state_s_letter_l = f"find_movement_for_new_state_{s}_letter_{l}"
create_new_state(find_movement_for_new_state_s_letter_l)
create_new_transition(find_new_letter_for_new_state_s, l, find_movement_for_new_state_s_letter_l, l, "RIGHT")
for s in input_alphabet:
for l in input_alphabet:
find_movement_for_new_state_s_letter_l = f"find_movement_for_new_state_{s}_letter_{l}"
find_finals_section_from_trans_s_l_R = f"find_finals_section_from_trans_{s}_{l}_R"
find_finals_section_from_trans_s_l_L = f"find_finals_section_from_trans_{s}_{l}_L"
create_new_state(find_finals_section_from_trans_s_l_R)
create_new_state(find_finals_section_from_trans_s_l_L)
create_new_transition(find_movement_for_new_state_s_letter_l, "R", find_finals_section_from_trans_s_l_R, "R", "RIGHT")
create_new_transition(find_movement_for_new_state_s_letter_l, "L", find_finals_section_from_trans_s_l_L, "L", "RIGHT")
# hay que llegar hasta el separador 3 para poder comprobar si nuestro nuevo estado al que tenemos que cambiar es un final, así que nos saltamos todo lo que haya entre medias:
for l2 in transitions_alphabet:
create_new_transition(find_finals_section_from_trans_s_l_R, l2, find_finals_section_from_trans_s_l_R, l2, "RIGHT")
create_new_transition(find_finals_section_from_trans_s_l_L, l2, find_finals_section_from_trans_s_l_L, l2, "RIGHT")
# Cuando encuentra el 3er separador, se mueve al siguiente carácter para poder checkear si coincide con el state al que estamos cambiando:
for s in input_alphabet:
for l in input_alphabet:
find_finals_section_from_trans_s_l_R = f"find_finals_section_from_trans_{s}_{l}_R"
find_finals_section_from_trans_s_l_L = f"find_finals_section_from_trans_{s}_{l}_L"
find_final_state_from_trans_s_l_R = f"find_final_state_from_trans_{s}_{l}_R"
find_final_state_from_trans_s_l_L = f"find_final_state_from_trans_{s}_{l}_L"
create_new_state(find_final_state_from_trans_s_l_R)
create_new_state(find_final_state_from_trans_s_l_L)
create_new_transition(find_finals_section_from_trans_s_l_R, section_separator, find_final_state_from_trans_s_l_R, section_separator, "RIGHT")
create_new_transition(find_finals_section_from_trans_s_l_L, section_separator, find_final_state_from_trans_s_l_L, section_separator, "RIGHT")
# comprobar si el estado al que tiene que cambiar es un final state (de momento, solo vamos a trabajar como si solo pudiese haber un final state)
for l in input_alphabet:
create_new_state(f"jump_back_2_separators_to_process_final_trans_{l}_R")
create_new_state(f"jump_back_2_separators_to_process_final_trans_{l}_L")
create_new_state(f"jump_back_1_separator_to_process_final_trans_{l}_R")
create_new_state(f"jump_back_1_separator_to_process_final_trans_{l}_L")
for s in input_alphabet:
for l in input_alphabet:
find_final_state_from_trans_s_l_R = f"find_final_state_from_trans_{s}_{l}_R"
find_final_state_from_trans_s_l_L = f"find_final_state_from_trans_{s}_{l}_L"
jump_back_2_separators_to_process_final_trans_l_R = f"jump_back_2_separators_to_process_final_trans_{l}_R"
jump_back_2_separators_to_process_final_trans_l_L = f"jump_back_2_separators_to_process_final_trans_{l}_L"
jump_back_1_separator_to_process_final_trans_l_R = f"jump_back_1_separator_to_process_final_trans_{l}_R"
jump_back_1_separator_to_process_final_trans_l_L = f"jump_back_1_separator_to_process_final_trans_{l}_L"
jump_back_2_separators_to_process_trans_s_l_R = f"jump_back_2_separators_to_process_trans_{s}_{l}_R"
jump_back_2_separators_to_process_trans_s_l_L = f"jump_back_2_separators_to_process_trans_{s}_{l}_L"
create_new_state(jump_back_2_separators_to_process_trans_s_l_R)
create_new_state(jump_back_2_separators_to_process_trans_s_l_L)
jump_back_1_separator_to_process_trans_s_l_R = f"jump_back_1_separator_to_process_trans_{s}_{l}_R"
jump_back_1_separator_to_process_trans_s_l_L = f"jump_back_1_separator_to_process_trans_{s}_{l}_L"
create_new_state(jump_back_1_separator_to_process_trans_s_l_R)
create_new_state(jump_back_1_separator_to_process_trans_s_l_L)
for l2 in input_alphabet:
if l2 != s:
create_new_transition(find_final_state_from_trans_s_l_R, l2, jump_back_2_separators_to_process_trans_s_l_R, l2, "LEFT")
create_new_transition(find_final_state_from_trans_s_l_L, l2, jump_back_2_separators_to_process_trans_s_l_L, l2, "LEFT")
else:
create_new_transition(find_final_state_from_trans_s_l_R, l2, jump_back_2_separators_to_process_final_trans_l_R, l2, "LEFT")
create_new_transition(find_final_state_from_trans_s_l_L, l2, jump_back_2_separators_to_process_final_trans_l_L, l2, "LEFT")
# movemos la head hacia atrás saltándonos todo hasta llegar al input de nuevo (al separator 2) para poder aplicar la transición:
for l2 in transitions_alphabet:
create_new_transition(jump_back_2_separators_to_process_trans_s_l_R, l2, jump_back_2_separators_to_process_trans_s_l_R, l2, "LEFT")
create_new_transition(jump_back_2_separators_to_process_trans_s_l_L, l2, jump_back_2_separators_to_process_trans_s_l_L, l2, "LEFT")
create_new_transition(jump_back_1_separator_to_process_trans_s_l_R, l2, jump_back_1_separator_to_process_trans_s_l_R, l2, "LEFT")
create_new_transition(jump_back_1_separator_to_process_trans_s_l_L, l2, jump_back_1_separator_to_process_trans_s_l_L, l2, "LEFT")
create_new_transition(jump_back_2_separators_to_process_trans_s_l_R, section_separator, jump_back_1_separator_to_process_trans_s_l_R, section_separator, "LEFT")
create_new_transition(jump_back_2_separators_to_process_trans_s_l_L, section_separator, jump_back_1_separator_to_process_trans_s_l_L, section_separator, "LEFT")
# mover la head hacia atrás saltándonos todo hasta llegar al input de nuevo (al separator 2), pero para el caso final:
for l in input_alphabet:
jump_back_2_separators_to_process_final_trans_l_R = f"jump_back_2_separators_to_process_final_trans_{l}_R"
jump_back_2_separators_to_process_final_trans_l_L = f"jump_back_2_separators_to_process_final_trans_{l}_L"
jump_back_1_separator_to_process_final_trans_l_R = f"jump_back_1_separator_to_process_final_trans_{l}_R"
jump_back_1_separator_to_process_final_trans_l_L = f"jump_back_1_separator_to_process_final_trans_{l}_L"
create_new_transition(jump_back_2_separators_to_process_final_trans_l_R, section_separator, jump_back_1_separator_to_process_final_trans_l_R, section_separator, "LEFT")
create_new_transition(jump_back_2_separators_to_process_final_trans_l_L, section_separator, jump_back_1_separator_to_process_final_trans_l_L, section_separator, "LEFT")
for l2 in transitions_alphabet:
create_new_transition(jump_back_2_separators_to_process_final_trans_l_R, l2, jump_back_2_separators_to_process_final_trans_l_R, l2, "LEFT")
create_new_transition(jump_back_2_separators_to_process_final_trans_l_L, l2, jump_back_2_separators_to_process_final_trans_l_L, l2, "LEFT")
create_new_transition(jump_back_1_separator_to_process_final_trans_l_R, l2, jump_back_1_separator_to_process_final_trans_l_R, l2, "LEFT")
create_new_transition(jump_back_1_separator_to_process_final_trans_l_L, l2, jump_back_1_separator_to_process_final_trans_l_L, l2, "LEFT")
# Cuando encuentra el 2º separador, empieza a buscar el input marcado para sustituirlo y aplicar la transición:
for s in input_alphabet:
for l in input_alphabet:
jump_back_1_separator_to_process_trans_s_l_R = f"jump_back_1_separator_to_process_trans_{s}_{l}_R"
jump_back_1_separator_to_process_trans_s_l_L = f"jump_back_1_separator_to_process_trans_{s}_{l}_L"
find_marked_input_to_apply_s_l_R = f"find_marked_input_to_apply_{s}_{l}_R"
find_marked_input_to_apply_s_l_L = f"find_marked_input_to_apply_{s}_{l}_L"
create_new_state(find_marked_input_to_apply_s_l_R)
create_new_state(find_marked_input_to_apply_s_l_L)
create_new_transition(jump_back_1_separator_to_process_trans_s_l_R, section_separator, find_marked_input_to_apply_s_l_R, section_separator, "LEFT")
create_new_transition(jump_back_1_separator_to_process_trans_s_l_L, section_separator, find_marked_input_to_apply_s_l_L, section_separator, "LEFT")
# saltarse caracteres no marcados:
for l2 in input_alphabet:
create_new_transition(find_marked_input_to_apply_s_l_R, l2, find_marked_input_to_apply_s_l_R, l2, "LEFT")
create_new_transition(find_marked_input_to_apply_s_l_L, l2, find_marked_input_to_apply_s_l_L, l2, "LEFT")
# lo mismo pero para el final state (es distinto xq solo guarda la letra, el state no le hace falta)
for l in input_alphabet:
jump_back_1_separator_to_process_final_trans_l_R = f"jump_back_1_separator_to_process_final_trans_{l}_R"
jump_back_1_separator_to_process_final_trans_l_L = f"jump_back_1_separator_to_process_final_trans_{l}_L"
find_marked_input_to_apply_final_l_R = f"find_marked_input_to_apply_final_{l}_R"
find_marked_input_to_apply_final_l_L = f"find_marked_input_to_apply_final_{l}_L"
create_new_state(find_marked_input_to_apply_final_l_R)
create_new_state(find_marked_input_to_apply_final_l_L)
create_new_transition(jump_back_1_separator_to_process_final_trans_l_R, section_separator, find_marked_input_to_apply_final_l_R, section_separator, "LEFT")
create_new_transition(jump_back_1_separator_to_process_final_trans_l_L, section_separator, find_marked_input_to_apply_final_l_L, section_separator, "LEFT")
# saltarse caracteres no marcados:
for l2 in input_alphabet:
create_new_transition(find_marked_input_to_apply_final_l_R, l2, find_marked_input_to_apply_final_l_R, l2, "LEFT")
create_new_transition(find_marked_input_to_apply_final_l_L, l2, find_marked_input_to_apply_final_l_L, l2, "LEFT")
# si el carácter del input está marcado, sustituir el carácter por el que toque y mover la head según indica la transición:
for s in input_alphabet:
move_head_with_state_s = f"move_head_with_state_{s}"
create_new_state(move_head_with_state_s)
for l in input_alphabet:
find_marked_input_to_apply_s_l_R = f"find_marked_input_to_apply_{s}_{l}_R"
find_marked_input_to_apply_s_l_L = f"find_marked_input_to_apply_{s}_{l}_L"
create_new_transition(find_marked_input_to_apply_s_l_R, input_alphabet_marker, move_head_with_state_s, l, "RIGHT") # se mueve right
create_new_transition(find_marked_input_to_apply_s_l_L, input_alphabet_marker, move_head_with_state_s, l, "LEFT") # se mueve left
# una vez movido el head, buscar una nueva transición para el state y letter actuales (vuelta al principio!)
find_trans_from_state_s_letter_l = f"find_trans_from_state_{s}_letter_{l}"
create_new_transition(move_head_with_state_s, l, find_trans_from_state_s_letter_l, input_alphabet_marker, "RIGHT")
# lo mismo para los final states:
move_head_with_final_state = "move_head_with_final_state"
create_new_state(move_head_with_final_state)
for l in input_alphabet:
find_marked_input_to_apply_final_l_R = f"find_marked_input_to_apply_final_{l}_R"
find_marked_input_to_apply_final_l_L = f"find_marked_input_to_apply_final_{l}_L"
create_new_transition(find_marked_input_to_apply_final_l_R, input_alphabet_marker, move_head_with_final_state, l, "RIGHT") # se mueve right
create_new_transition(find_marked_input_to_apply_final_l_L, input_alphabet_marker, move_head_with_final_state, l, "LEFT") # se mueve left
# una vez movido el head, TERMINAR!!!!
create_new_transition(move_head_with_final_state, l, final_state, l, "RIGHT")
with open("machine_descriptions/universal_turing_machine.json", "w") as f:
f.write(json.dumps(machine_description, indent=4))