3 #include <utils/compare.h>
4 #include <utils/printer.h>
5 #include <symbol/turingMachineSymbolPattern.h>
6 #include <transition/Turing/localTuringMachineTransition.h>
7 #include <transition/Turing/mConfigurationTuringMachineTransition.h>
16 template<
typename T,
bool hasHalfTape>
71 typedef std::variant<ConstantInstruction, DynamicWrite>
Instruction;
73 static_assert(std::is_same_v<T, SymbolType>);
74 static_assert(std::is_same_v<T, NodeStateType>);
86 template <
typename CharT =
char,
typename Traits = std::
char_traits<CharT>>
87 friend std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os,
const DynamicWrite& i) {
88 os <<
"dyn_write(" << i.symbol_key <<
")";
99 template <
typename CharT =
char,
typename Traits = std::
char_traits<CharT>>
100 friend std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os,
const Instruction& instruction) {
105 [&os](
const DynamicWrite& i) { os << i; },
116 template <
typename CharT =
char,
typename Traits = std::
char_traits<CharT>>
117 std::function<std::basic_ostream<CharT, Traits>&(std::basic_ostream<CharT, Traits>&)>
debug()
const {
118 return [
this](std::basic_ostream<CharT, Traits>& os) -> std::basic_ostream<CharT, Traits>& {
119 using Utils::Debug::operator<<;
120 os <<
"(" << preStatePattern.debug() <<
", " << preSymbolPattern <<
", " << symbolPlaceHolder <<
") -> "
126 template <
typename U,
bool hT,
typename CharT,
typename Traits>
145 const std::vector<Instruction>& instructions,
201 return preStatePattern;
227 static constexpr std::vector<ConstantInstruction> generateConstantInstructions(
const std::vector<Instruction>&,
const SubstitutionType&);
229 std::shared_ptr<const State::MConfiguration::MConfiguration<NodeStateType>> preStatePattern;
231 std::vector<Instruction> instructions;
232 std::shared_ptr<const State::MConfiguration::MConfiguration<NodeStateType>> postStatePattern;
245 template <
typename U,
bool hT,
typename CharT =
char,
typename Traits = std::
char_traits<CharT>>
246 std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os,
const TuringStyleMConfigurationTuringMachineTransition<U, hT>& transition) {
247 using Utils::Debug::operator<<;
249 <<
"(" << transition.preStatePattern <<
", " << transition.preSymbolPattern <<
", " << transition.symbolPlaceHolder <<
") -> "
250 <<
"(" <<
Utils::Debug::debug(transition.instructions) <<
", " << transition.postStatePattern <<
")";
254 template<
typename T,
bool hT>
255 constexpr
bool TuringStyleMConfigurationTuringMachineTransition<T, hT>::matchSymbolPattern(
256 const SymbolPattern& pattern,
257 const SubstitutionType& substitution,
258 const SymbolType& symbol) {
259 bool matched = pattern.match(symbol, substitution);
263 template<
typename T,
bool hT>
264 constexpr std::vector<typename TuringStyleMConfigurationTuringMachineTransition<T, hT>::ConstantInstruction>
265 TuringStyleMConfigurationTuringMachineTransition<T, hT>::generateConstantInstructions(
266 const std::vector<Instruction>& instructions,
267 const SubstitutionType& substitution) {
268 std::vector<ConstantInstruction> is;
270 instructions.begin(),
272 std::back_inserter(is),
273 [&substitution](
const Instruction& i) { return generateConstantInstruction(i, substitution); }
278 template<
typename T,
bool hT>
280 TuringStyleMConfigurationTuringMachineTransition<T, hT>::generateConstantInstruction(
281 const Instruction& instruction,
282 const SubstitutionType& substitution) {
284 ConstantInstruction i =
287 [](
const ConstantInstruction& i) {
return i; },
288 [&substitution](
const DynamicWrite& i) -> ConstantInstruction {
289 typename SubstitutionType::const_iterator it = substitution.find(i.symbol_key);
290 if(it == substitution.end()) {
291 using Utils::Debug::operator<<;
292 std::ostringstream ss;
293 ss <<
"Unable to transform dynamic write into constant write" << std::endl;
296 throw std::runtime_error(ss.str());
298 std::shared_ptr<const State::MConfiguration::MConfiguration<T>> symbol = it->second;
299 typename MConfigurationTuringMachineTransition_::WriteInstruction wi {symbol->getNode()};
308 template<
typename T,
bool hT>
309 std::optional<typename TuringStyleMConfigurationTuringMachineTransition<T, hT>::ApplyHelperType>
311 const SymbolType& symbol)
const {
312 SubstitutionType substitution;
314 std::shared_ptr<const State::MConfiguration::MConfiguration<T>> symbolAsState = std::make_shared<const State::MConfiguration::MConfiguration<T>>(symbol);
315 std::shared_ptr<const State::MConfiguration::MConfiguration<T>> preStatePatternWithoutPlaceholder =
317 substitution = preStatePatternWithoutPlaceholder->unifyWithMConf(m_conf);
320 return std::optional<ApplyHelperType>(std::nullopt);
323 if(matchSymbolPattern(preSymbolPattern, substitution, symbol)) {
324 return std::optional<ApplyHelperType>(substitution);
326 return std::optional<ApplyHelperType>(std::nullopt);
329 template<
typename T,
bool hT>
334 ApplyHelperType&& substitution,
335 bool& running)
const {
336 SymbolType preSymbol = tape.read();
337 typename ApplyHelperType::const_iterator it = substitution.find(symbolPlaceHolder);
338 StateType preSymbolAsMConf = std::make_shared<const State::MConfiguration::MConfiguration<T>>(preSymbol);
339 if (it == substitution.end()) {
340 substitution.insert({symbolPlaceHolder, preSymbolAsMConf});
344 std::vector<ConstantInstruction> constant_instructions = generateConstantInstructions(instructions, substitution);
345 MConfigurationTuringMachineTransition_::run_instructions(tape, running, constant_instructions);
349 template<
typename T,
bool hT>
353 const std::vector<Instruction>& instructions,
354 const NodeStateType& symbolPlaceHolder,
357 preStatePattern(preStatePattern), preSymbolPattern(preSymbolPattern), instructions(instructions),
358 postStatePattern(postStatePattern), symbolPlaceHolder(symbolPlaceHolder) {
361 template<
typename T,
bool hT>
367 std::optional<AlphaRelation> preStateRelation_o = preStatePattern->isAlphaEquivalent(other.preStatePattern);
368 std::optional<AlphaRelation> postStateRelation_o = postStatePattern->isAlphaEquivalent(other.postStatePattern);
370 if(!preStateRelation_o || !postStateRelation_o) {
374 AlphaRelation preStateRelation = *preStateRelation_o;
375 AlphaRelation postStateRelation = *postStateRelation_o;
377 for(
const std::pair<T, T>& binding : preStateRelation.first) {
378 typename AlphaRewriting::iterator it = postStateRelation.first.find(binding.first);
379 if(it != postStateRelation.first.end()) {
380 if(it->second != binding.second) {
386 if(
typename AlphaRewriting::iterator it = postStateRelation.first.find(symbolPlaceHolder);
387 it != postStateRelation.first.end()) {
388 if (it->second != other.symbolPlaceHolder) {
393 if(!preSymbolPattern.isAlphaEquivalent(preSymbolPattern, preStateRelation.first)) {
397 if(instructions.size() != other.instructions.size()) {
401 for(
typename std::vector<Instruction>::const_iterator it = instructions.begin(),
402 it2 = other.instructions.begin(); it != instructions.end(); ++it, ++it2) {
406 if(instruction.index() != otherInstruction.index()) {
409 else if(std::holds_alternative<DynamicWrite>(instruction)) {
410 DynamicWrite dynamicWrite = std::get<DynamicWrite>(instruction);
411 DynamicWrite otherDynamicWrite = std::get<DynamicWrite>(otherInstruction);
416 else if(instruction != otherInstruction) {
424 template<
typename T,
bool hT>
427 cmp = preStatePattern->compare(*other.preStatePattern);
439 cmp = postStatePattern->compare(*other.postStatePattern);