2021-12-10_building-a-turing-machine.html
001|
002|
003|
004|
005|
006| pgm1 = (
007| (0, 0, 1, +1, 1),
008| (1, 0, 0, +1, 2),
009| (2, 0, 2, +1, 3),
010| (3, 0, 0, +1, 0),
011| )
012|
013|
014| def turing_machine (program, max_steps=32):
015| '''TM. program=(state,key,write,move,next)'''
016| outfmt = '%2d: S%d [%d] {%d,%s} (%s)'
017| def find_pgm_entry (_state, _curr):
018| for pe in program:
019| if (pe[0]==_state) and (pe[1]==_curr):
020| return pe
021| raise KeyError('No Program Entry!')
022|
023| tape = [[0,0]]
024| tt = turing_tape(tape)
025| curr = tt.send(None)
026|
027| state = 0
028| watchdog = 1
029| while 0 <= state:
030|
031| pe = find_pgm_entry(state,curr)
032|
033| d = 'L' if pe[3]<0 else 'R'
034| x = 'HALT' if pe[4] < 0 else ('S%d' % pe[4])
035| out = (watchdog, state, curr, pe[2],d, x)
036|
037| print(outfmt % out)
038|
039| curr = tt.send(pe[2:4])
040|
041| state = pe[4]
042|
043| watchdog += 1
044| if max_steps < watchdog:
045| break
046|
047| out = [(t0,t1) for t0,t1 in tape if t1]
048|
049| ps0 = 'HALT' if state < 0 else ('S%d' % state)
050| ps2 = ' '.join([str(t[0]) for t in out])
051| print('- ')
052| print('%s (%d) [%s]' % (ps0, len(out), ps2))
053| print()
054|
055| return (state, out)
056|
057|
058| def turing_tape (tape, Blank=0):
059| '''Turing Machine Tape (Python generator)'''
060| head = 0
061| while True:
062| _tval,_move = yield tape[head][0]
063| tval = int(_tval)
064| move = int(_move)
065|
066| tape[head][0] = tval
067| tape[head][1] += 1
068|
069| if 0 < move:
070| head += 1
071|
072| if len(tape) <= head:
073| tape.append([Blank,0])
074| continue
075|
076| if move < 0:
077| head -= 1
078|
079| if head < 0:
080| head = 0
081| tape.insert(0, [Blank,0])
082| continue
083|
084|
085|
086| final,tape = turing_machine(pgm1, max_steps=15)
087|
088|
089| class TuringTape (object):
090| def __init__ (self, blank=0):
091| self.blank = blank
092| self.reset()
093| def reset (self):
094| self.head = 0
095| self.tape = [[self.blank, 0]]
096| def curr (self):
097| return self.tape[self.head][0]
098| def __call__ (self, cell, move):
099| _cell = int(cell)
100| _move = int(move)
101|
102| self.tape[self.head][0] = _cell
103| self.tape[self.head][1] += 1
104|
105| if 0 < _move:
106| self.head += 1
107|
108| if len(self.tape) <= self.head:
109| self.tape.append([self.blank,0])
110| return
111|
112| if _move < 0:
113| self.head -= 1
114|
115| if self.head < 0:
116| self.head = 0
117| self.tape.insert(0, [self.blank,0])
118| return
119|
120| def __len__ (self):
121| return len(self.tape)
122| def __str__ (self):
123| t = ' '.join([str(t0) for t0,t1 in self.tape if t1])
124| return '[%s]' % t
125| def _trim (self):
126| while (1 < len(self)) and (self.tape[-1][1] == 0):
127| self.tape.pop(-1)
128| while (1 < len(self)) and (self.tape[0][1] == 0):
129| self.tape.pop(0)
130|
131|
132| class TuringMachine (object):
133| def __init__ (self, program, max_steps=32, blank=0):
134| self.program = program
135| self.max_steps = max_steps
136| self.state = 0
137| self.watchdog = 1
138| self.tape = TuringTape(blank=blank)
139| def reset (self):
140| self.state = 0
141| self.watchdog = 1
142| self.tape.reset()
143| def run (self):
144| while 0 <= self.state:
145| pe = self._find_pgm_entry()
146| d = 'L' if pe[3]<0 else 'R'
147| x = 'HALT' if pe[4] < 0 else ('S%d' % pe[4])
148| s = '%2d: S%d [%d] {%d,%s} (%s)'
149| t = (self.watchdog, self.state, self.tape.curr(), pe[2],d, x)
150| print(s % t)
151| self.tape(pe[2], pe[3])
152| self.state = pe[4]
153| self.watchdog += 1
154| if self.max_steps < self.watchdog:
155| break
156|
157| self.tape._trim()
158| out = [(t0,t1) for t0,t1 in self.tape.tape if t1]
159|
160| ps0 = 'HALT' if self.state < 0 else ('S%d' % self.state)
161| ps2 = ' '.join([str(t[0]) for t in out])
162| print('- ')
163| print('%s (%d) [%s]' % (ps0, len(out), ps2))
164| print()
165|
166| return (self.state, out)
167| def __str__ (self):
168| return ''
169| def _find_pgm_entry (self):
170| for pe in self.program:
171| if (pe[0]==self.state) and (pe[1]==self.tape.curr()):
172| return pe
173| raise KeyError('No Program Entry!')
174|
175|
176| tm1 = TuringMachine(pgm1, max_steps=15)
177| final,tape = tm1.run()
178|
179|
180|
181| pgm2 = (
182| (0, 0, 1, +1, 1),
183| (0, 1, 1, -1, 2),
184| (1, 0, 1, -1, 0),
185| (1, 1, 1, +1, 1),
186| (2, 0, 1, -1, 1),
187| (2, 1, 1, 0, -1),
188| )
189|
190|
191|