|
| 1 | +from TMsim.parsing import parse_text, get_delta_dict |
| 2 | +from TMsim.tape import Tape |
| 3 | + |
| 4 | + |
| 5 | +class TuringMachine: |
| 6 | + |
| 7 | + init_state = 'Qinit' |
| 8 | + halt_states = {'Qhalt', 'Qaccept', 'Qreject'} |
| 9 | + |
| 10 | + def __init__(self, num_tapes, alphabet, states, transition_func): |
| 11 | + self.num_tapes = num_tapes |
| 12 | + self.alphabet = alphabet |
| 13 | + self.states = states |
| 14 | + self.transition_func = transition_func |
| 15 | + self._initialize() |
| 16 | + |
| 17 | + def __repr__(self): |
| 18 | + class_name = self.__class__.__qualname__ |
| 19 | + params = list(self.__init__.__code__.co_varnames) |
| 20 | + params.remove('self') |
| 21 | + params.remove('transition_func') |
| 22 | + args = '\n'.join([f'\t{key} = {getattr(self, key)}' for key in params]) |
| 23 | + return f'{class_name}(\n{args}\n)' |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def from_txt(cls, filepath): |
| 27 | + with open(filepath) as file: |
| 28 | + lines = file.readlines() |
| 29 | + num_tapes, alphabet, states, transitions = parse_text(lines) |
| 30 | + transition_func = get_delta_dict(transitions, alphabet) |
| 31 | + return cls(num_tapes=num_tapes, alphabet=alphabet, states=states, |
| 32 | + transition_func=transition_func) |
| 33 | + |
| 34 | + def _initialize(self): |
| 35 | + self._check_symbols() |
| 36 | + self._check_init_state() |
| 37 | + self.state = self.init_state |
| 38 | + self.steps = 0 |
| 39 | + self.tapes = self._init_tapes() |
| 40 | + |
| 41 | + def _check_symbols(self): |
| 42 | + for symbol in {'>', '_'}: |
| 43 | + if symbol not in self.alphabet: |
| 44 | + raise ValueError( |
| 45 | + f'The required symbol "{symbol}" is not in the alphabet!') |
| 46 | + |
| 47 | + def _check_init_state(self): |
| 48 | + if self.init_state not in self.states: |
| 49 | + raise ValueError( |
| 50 | + f'The required state "{self.init_state}" is not in the set of states!') |
| 51 | + |
| 52 | + def _init_tapes(self): |
| 53 | + empty_tapes = [Tape() for i in range(self.num_tapes)] |
| 54 | + return empty_tapes |
| 55 | + |
| 56 | + def load_input(self, string): |
| 57 | + for s in string: |
| 58 | + self.tapes[0].move(key='R') |
| 59 | + self.tapes[0].write(s) |
| 60 | + self.tapes[0].head = self.tapes[0].min_head+1 |
| 61 | + |
| 62 | + def get_config(self): |
| 63 | + config = (self.state,) |
| 64 | + for tape in self.tapes: |
| 65 | + config += (tape.read(),) |
| 66 | + return config |
| 67 | + |
| 68 | + def execute(self, instructions): |
| 69 | + self.state = instructions[0] |
| 70 | + k = 1+self.num_tapes |
| 71 | + symbols = instructions[1:k] |
| 72 | + moves = instructions[k:] |
| 73 | + for i, s, m in zip(range(self.num_tapes), symbols, moves): |
| 74 | + self.tapes[i].write(symbol=s) |
| 75 | + self.tapes[i].move(key=m) |
| 76 | + |
| 77 | + def make_step(self): |
| 78 | + config = self.get_config() |
| 79 | + instructions = self.transition_func[config] |
| 80 | + self.execute(instructions) |
| 81 | + |
| 82 | + def run(self): |
| 83 | + while self.state not in self.halt_states: |
| 84 | + self.make_step() |
| 85 | + self.steps += 1 |
| 86 | + |
| 87 | + def show_state(self, prefix=''): |
| 88 | + print(f'{prefix}state = {self.state}') |
| 89 | + |
| 90 | + def show_input(self): |
| 91 | + self.show_state(prefix='init_') |
| 92 | + print(f'input = {self.tapes[0]}') |
| 93 | + |
| 94 | + def show_output(self): |
| 95 | + self.show_state(prefix='final_') |
| 96 | + print(f'output = {self.tapes[-1]}') |
| 97 | + print(f'num_steps = {self.steps}') |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + |
| 102 | + filepath = './../codes/1tape_inverse.txt' |
| 103 | + TM = TuringMachine.from_txt(filepath) |
| 104 | + |
| 105 | + input_string = '00011101' |
| 106 | + TM.load_input(input_string) |
| 107 | + |
| 108 | + TM.show_input() |
| 109 | + TM.run() |
| 110 | + TM.show_output() |
0 commit comments