turing machine

the engine is chatgpt

but the machine is ME

Signed-off-by: Luong Duc Anh <[email protected]>
This commit is contained in:
lda committed 2026-01-15 11:43:06 +07:00
1 parent 405f518d77
commit bce8cf7ad9
1 file changed
+115
+115
View File
@@ -0,0 +1,115 @@
# Improved TuringMachine class with debug output and updated transition table per your fix.
from dataclasses import dataclass
from typing import Dict, Tuple, List, TypeAlias, Literal
# === Type Aliases ===
State: TypeAlias = str
Symbol: TypeAlias = str
Direction: TypeAlias = Literal["L", "R"] # precise directions only
TransitionKey: TypeAlias = Tuple[State, Symbol]
TransitionValue: TypeAlias = Tuple[State, Symbol, Direction]
TransitionFunction: TypeAlias = Dict[TransitionKey, TransitionValue]
Tape: TypeAlias = List[Symbol]
@dataclass
class TuringMachine:
transition_function: TransitionFunction
start_state: State
accept_state: State
reject_state: State
blank_symbol: Symbol = " "
def run(
self, tape_input: str, max_steps: int = 10_000, verbose: bool = True
) -> Tuple[State, Tape]:
tape: Tape = list(tape_input) + [self.blank_symbol] * 20
head = 0
state = self.start_state
steps = 0
def debug_print():
tape_view = "".join(tape).replace(self.blank_symbol, "_")
head_marker = " " * head + "^"
print(
f"[{steps:04}] State: {state} | Head: {head} | Tape:\n{tape_view}\n{head_marker}"
)
debug_print()
while (
state != self.accept_state
and state != self.reject_state
and steps < max_steps
):
if verbose:
debug_print()
read = tape[head]
key = (state, read)
if key not in self.transition_function:
print(f"No transition for ({state}, '{read}') — rejecting.")
state = self.reject_state
break
next_state, write, move = self.transition_function[key]
tape[head] = write
head += 1 if move == "R" else -1
if head < 0:
tape.insert(0, self.blank_symbol)
head = 0
state = next_state
steps += 1
debug_print()
return state, tape
# Updated transitions
transitions_cleaned: TransitionFunction = {}
# Scan phase: 1 → X, 0 → Y
transitions_cleaned[("scan", "1")] = ("scan", "X", "R")
transitions_cleaned[("scan", "0")] = ("scan", "Y", "R")
transitions_cleaned[("scan", " ")] = ("back", " ", "L")
# Back phase: move left over 0/1/X/Y and turn forward on ' ', 'M', 'N'
for sym in ["0", "1", "X", "Y"]:
transitions_cleaned[("back", sym)] = ("back", sym, "L")
transitions_cleaned[("back", " ")] = ("forward", " ", "R")
transitions_cleaned[("back", "M")] = ("forward", "M", "R")
transitions_cleaned[("back", "N")] = ("forward", "N", "R")
# Forward: if blank, done; if digit → sweep; if mark → prepare copy
transitions_cleaned[("forward", " ")] = ("accept", " ", "R")
transitions_cleaned[("forward", "0")] = ("sweep", "0", "L")
transitions_cleaned[("forward", "1")] = ("sweep", "1", "L")
transitions_cleaned[("forward", "X")] = ("fw1", "M", "R") # 1 → M
transitions_cleaned[("forward", "Y")] = ("fw0", "N", "R") # 0 → N
# Copy phases
for sym in ["0", "1", "X", "Y"]: # ts wont ever meet M N
transitions_cleaned[("fw1", sym)] = ("fw1", sym, "R")
transitions_cleaned[("fw0", sym)] = ("fw0", sym, "R")
transitions_cleaned[("fw1", " ")] = ("back", "1", "L")
transitions_cleaned[("fw0", " ")] = ("back", "0", "L")
# Sweep phase: M → 1, N → 0
transitions_cleaned[("sweep", "M")] = ("sweep", "1", "L")
transitions_cleaned[("sweep", "N")] = ("sweep", "0", "L")
transitions_cleaned[("sweep", "0")] = ("sweep", "0", "L")
transitions_cleaned[("sweep", "1")] = ("sweep", "1", "L")
transitions_cleaned[("sweep", " ")] = ("accept", " ", "R")
# Build and run the machine with debug output
tm = TuringMachine(
transition_function=transitions_cleaned,
start_state="scan",
accept_state="accept",
reject_state="reject",
)
final_state, final_tape = tm.run("00110")
print("Final State:", final_state)
print("Final Tape:", "".join(final_tape).strip())