Virtual Turing Machine (VTM)
$M = new VTM(); $M->tape("_1_"); $M->blank("_"); $M->start_state("start"); $M->set_instruction("start", "1", "start", "_", 1); $M->step();
The VTM.pm module emulates a Turing machine as a Perl object. You can write your own instruction input for it or use the supplied ones.
A Turing machine is theoretical computer consisting of a finite set of internal states, a finite alphabet that includes a blank symbol, and a finite set of instructions. It has a physical head and a physical infinitely long tape, which is divided into cells. The cell values consist of the alphabet. The tape has a finite number of non-blank cells. The head can read and write to the cells and move the tape one cell to the left and one cell to the right.
An instruction is defined as a five tuple: initial state, read value, final state, write value, movement. The inital state is the current internal state of the machine. The read value is the value of the cell the head is currently positioned at. The final state becomes the new state of the machine. The write value overwrites the cell the head is positioned at. Movement specifies which direction the head moves, either left or right.
When the machine does not have an instruction for a given internal state and cell value, it will halt. The Turing machine will start at the leftmost non-blank cell on the tape (if there are no non-blank cells in the tape, the VTM will start in the middle of the tape).
The VTM's state names consist of letters, underscores, plus signs, minus signs, asterixes, less than signs, greater than signs, equal signs, and forward slashes. There are no restrictions on length or what the first character is.
The VTM's alphabet is fixed. It consists of letters, numbers, underscores, plus signs, minus signs, asterixes, forward slashes, backslashes, less than signs, greater than signs, equal signs, periods, exclamation marks, at signs, dollar signs, percentage, ampersands, parentheses, square brackets, braces, pipes, single quotation marks, double quotation marks, and pipes. (Phew!)
All the interactions with the VTM is handled through methods. This means you do NOT look inside the internal structure. Got it?
new()
is_not_run()
is_halted()
is_in_infinite_loop()
is_executing()
error_msg()
blank($blank_char)
blank($string)
blank()
blank()
returns the machine's blank
character. If no blank character is set, an empty string is returned.
tape($tape_string)
tape(@tape)
tape(\@tape)
tape()
sets the machine's initial tape. The status
of the machine must be ``not_run''. It returns 1 if the tape was set
successfully, 0 otherwise (in case the tape given is not valid).
tape()
tape()
returns the machine's current tape as a
list.
start_state($start_state)
start_state()
infinite_tape($boolean)
infinite_tape()
infinite_tape()
returns 1 if it the machine
is simulating infinite tapes, 0 otherwise.
infinite_loop_check($boolean)
infinite_loop_check()
half_step($boolean)
step()
needs to be called
twice for the Turing machine to complete a full step. In the first
half-step, $M->step()
will write the new cell value and change to a
new state. In the second half-step, $M->step()
will move the position
of the head.
half_step()
half_step()
will return 1 if half-step mode
is on, 0 otherwise.
parse_instruction($line)
start()
step()
will do so if required.
Returns 1 if the machine has all the necessary information to start, 0
otherwise.
reset()
step()
half_step()
above).
$M->step()
returns 1 if an instruction was executed, 0 if there were
no instructions to execute or there were no instructions to execute.
position()
state()
offset()
infinite_tape()
above.
valid_state_name($state_name)
valid_state_name()
will return 1 if $state_name is valid, 0 otherwise.
It is a class method.
valid_cell_value($cell)
valid_tape($tape)
Paul R. C. Ming <prcm@nmt.edu>
<http://www.nmt.edu/~prcm/turing/>
Please report bugs to the author with the conditions to reproduce the bug. The author also appreciates fixes for bugs.
vtm(1)
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.