Crates.io | tm-interpreter |
lib.rs | tm-interpreter |
version | 0.1.0 |
source | src |
created_at | 2022-04-28 00:07:59.498097 |
updated_at | 2022-04-28 00:07:59.498097 |
description | A program to simulate a turing machine |
homepage | |
repository | |
max_upload_size | |
id | 576498 |
size | 52,306 |
A simple way to simulate a turing machine.
tm-interpreter <path-to-tm> [OPTIONS]
[OPTIONS]: -v
-> verbose mode (prints the TM step by step)
In a file. For example:
A Turing machine that accepts if the input is divisible by 3
alphabet: 0123456789
tape: 14526
tape_offset: 0
start_state: S0
accepted_states: S0
rule: S0 0369 none r S0
rule: S0 147 none r S1
rule: S0 258 none r S2
rule: S1 0369 none r S1
rule: S1 147 none r S2
rule: S1 258 none r S0
rule: S2 0369 none r S2
rule: S2 147 none r S0
rule: S2 258 none r S1
Let's break that down:
The basic structure is: keyword: <values>
. Any line that does not match this will be ignored.
Sets the alphabet to be used, here all possible characters are digits.
alphabet: 0123456789
Sets the input word.
tape: 14526
Defines where the tape starts. An offset of 3 would shift the tape 3 characters to the LEFT(for example if you want to write an oracle to the left of the head, the offset yould be the length of that oracle)
tape_offset: 0
To control a turing machine a state pattern is used.
Here we do not need a complete definition of all possible states, as a starting state and all state transitions (I will refer to them as rules) are sufficient.
A state is unambiguously defined by its name.
In which state the TM starts.
starting_state: S0
Defines which states are accepting end states. If there are multiple ones they are separated by a space like so S0 S1
.
accepted_states: S0
A rule consists of 5 things.
The first 2 are conditions to be met, the last 3 are actions that are taken if those conditions are met.
Generally speaking it is: rule: <in-state> <read-condition> <write-action> <head_movement> <next-state>
, where:
states
are just a String
<read-condition>
is one of the following:
any
-> if any character is written on the tape at the heads positionempty
-> if there is character at the head12345
-> if this contains the character at the head (this would rule would apply for [1-5])<write-action>
is one of:
none
-> leave it as isdelete
-> erase the current chara
(a single char) -> write a
<movement>
is any singular char, l
moves to left, r
to right, the rest stays in place.
rule: S1 258 none r S0
^^^^^^ if in state S1 and read 2, 5 or 8
^^^^^^^^^^ then leave the tape as is, move to the right and switch to state S0