MTMS main
Modern Turing Machine Simulator
Loading...
Searching...
No Matches
tm.cpp
Go to the documentation of this file.
1
11#include "tm.hpp"
12
13#include "string.hpp"
14
15#include <print>
16
17constexpr int kMaxSteps = 10000;
18
23 Alphabet input_alpha,
24 Alphabet tape_alpha,
25 std::set<State> states,
26 State initial_state,
27 std::size_t tape_count
28) noexcept(false)
29 : input_alphabet_(std::move(input_alpha)), tape_alphabet_(std::move(tape_alpha)),
30 states_(std::move(states)), start_state_(initial_state),
31 current_state_(std::move(initial_state)), tape_count_(tape_count)
32{
33 // Initialize the vector with 'k' empty tapes
34 this->tapes_.resize(this->tape_count_);
35}
36
41bool TuringMachine::load_input(std::string_view input) noexcept(false)
42{
43 String str(input);
44 if (!str.is_valid_for(this->input_alphabet_))
45 {
46 return false;
47 }
48
49 this->tapes_.assign(this->tape_count_, Tape());
50 this->tapes_[0] = Tape(str);
51 this->current_state_ = this->start_state_;
52 return true;
53}
54
61bool TuringMachine::step() noexcept(false)
62{
63 if (this->current_state_.is_accept())
64 {
65 return false;
66 }
67
68 // Gather the currently read symbols from all parallel tape heads
69 std::vector<Symbol> current_read;
70 current_read.reserve(this->tape_count_);
71 for (const auto &tape : this->tapes_)
72 {
73 current_read.push_back(tape.read());
74 }
75
76 // Search for a matching transition rule based on the current state and read symbols
77 const Transition *next_step_rule = nullptr;
78 for (const auto &[id, transition] : this->transitions_)
79 {
80 if (transition.get_current_state() == this->current_state_ &&
81 transition.get_read_symbols() == current_read)
82 {
83 next_step_rule = &transition;
84 break; // Found (DMT assumes a unique valid coincidence)
85 }
86 }
87
88 // If no multi-tape rule matches the current machine vector layout, halt (Crash/Reject)
89 if (next_step_rule == nullptr)
90 {
91 return false;
92 }
93
94 // Apply side-effects simultaneously to all affected tracks using the found rule
95 for (std::size_t i = 0; i < this->tape_count_; ++i)
96 {
97 this->tapes_[i].write(next_step_rule->get_write_symbols()[i]);
98 this->tapes_[i].move(next_step_rule->get_directions()[i]);
99 }
100
101 this->current_state_ = next_step_rule->get_next_state();
102 return true;
103}
104
111void TuringMachine::run() noexcept(false)
112{
113 int step_counter = 0;
114 while (this->step())
115 {
116 if (++step_counter >= kMaxSteps)
117 {
118 std::println(
119 stderr,
120 "[-] Execution forced-halted. Exceeded max security step limit ({} cycles).",
122 );
123 break;
124 }
125 }
126}
Represents the formal alphabet ( or ) of a Turing Machine.
Definition alphabet.hpp:27
Represents a single state in the finite set of states (Q) of the Turing Machine.
Definition state.hpp:26
bool is_accept() const noexcept
Checks if this state is an accepting (final) state.
Definition state.hpp:56
Represents a formal string (w) consisting of a finite sequence of Symbols.
Definition string.hpp:29
bool is_valid_for(const Alphabet &alphabet) const noexcept
Validates if all symbols within this string belong to a specific formal Alphabet.
Definition string.cpp:52
Simulates the bilateral infinite tape layout of a Turing Machine.
Definition tape.hpp:30
Represents a multi-tape transition rule (δ: Q × Γ^k → Q × Γ^k × {L, R}^k).
const std::vector< Direction > & get_directions() const noexcept
Retrieves the directions in which each parallel tape head must shift.
const std::vector< Symbol > & get_write_symbols() const noexcept
Retrieves the new symbols that will overwrite the current parallel tape cells.
const State & get_next_state() const noexcept
Retrieves the target state the machine will enter after executing this rule.
bool step() noexcept(false)
Executes a single computational transition step across all parallel tapes.
Definition tm.cpp:61
bool load_input(std::string_view input) noexcept(false)
Validates an input word against the input alphabet and loads it onto Tape 0.
Definition tm.cpp:41
void run() noexcept(false)
Loops execution triggers calling step() continuously until a halting condition occurs or the safety s...
Definition tm.cpp:111
std::vector< Tape > tapes_
Collection of k dynamic memory layouts.
Definition tm.hpp:226
State current_state_
Active contextual processing marker.
Definition tm.hpp:225
TuringMachine() noexcept
Default constructor.
Definition tm.hpp:38
std::size_t tape_count_
Cardinality multiplier tracking active tapes (k).
Definition tm.hpp:229
std::unordered_map< std::size_t, Transition > transitions_
Lookup table for transitions by ID.
Definition tm.hpp:228
String class representing a formal string (sequence of symbols) over an alphabet.
constexpr int kMaxSteps
Definition tm.cpp:17