How to use the Turing Machine simulator
Getting Python
If you have already used Python, you may skip this section.
The simulator is written in Python. There is a version of the
simulator written for Python 2, one one for Python 3. You do
not need to know any Python to use the simulator, but you do need to
have Python installed. If you have a Mac, you probably already have
Python. You should check this by going to the Terminal utility
and typing
python
at the prompt. You should see that the command is recognized,
and that you're running version 2.something or version
3.something. Alternatively, you can install the latest version
from python.org, using the link in the next paragraph.
On Windows, go to the website https://www.python.org/downloads/
and download the latest version, which is probably 3.7. You will
then need to edit the Environment variable Path to include the
directory containing python.exe. (I believe you can skip this
step if you use IDLE--see below.)
Preparing Input for the Simulator
You specify the Turing machine by creating an ordinary text file and
giving it the extension .tm. The best way to see how this
works is to compare the specification file reverse.tm with the
description of the same TM in the lecture notes. You should
note the following:
- States are always integers, and 0 is the initial state.
- -1 denotes a halt-and-accept state, -2 a halt-and-reject
state, and -3 a general halt state. This is to accommodate
both Turing machines for decision problems, where you use the
reject and accept states, and those for search problems, where
you use the simple halt state. All other state numbers must be
nonnegative.
- Each line has the form state character new-state
new-character direction, where direction is the
character 'R' or 'L'. Thus the line 1
a 2 b R corresponds to a
transition that says: in state 1 reading symbol a, write b, move
right, and change to state 2.
- Upper-case `B' represents the blank symbol. Case
matters!
- Lines beginning with # as the first character are
comments. It is perfectly all right, as in this example,
to use # as a tape symbol as well. There is no ambiguity
because # can not be the first character of a machine
instruction.
- If you do not specify what to do on a given state and
character combination, the machine will halt in state -2.
(I forget what happens if you over-specify---give two
different transitions for the same state and character
combination. I think the machine only uses the last one. )
Running the Simulator
Using the Command Line
Download tm.py or tmpy2.py
, depending on whether you are running Python 3 or Python 2. Also
download the specification reverse.tm.
On a Mac, open the Terminal utility, and navigate to the folder
where you have stored these files. On Windows, open the
Command Prompt accessory and navigate to the folder where you have
stored these files. Type (Python 3)
python3
import tm
or (Python 2)
python
import tmpy2
Then, if you want to run the specification reverse.tm on the
input 1011, type
tm.runtm('reverse','1011')
You can run any specification on any input in exactly the same
way.
Using IDLE
Alternatively, you can use Python's built-in interactive
development environment IDLE. You aunch IDLE from the
command line in a Mac by typing
idle
In Windows, launch it by selecting it from the Python folder
under `All Programs'. Once in IDLE, you can open tm.py
or tmpy2.py directly from the
File menu. Select Run Module from the Run menu and the
Python Shell window will appear. Type
runtm('reverse','1011')
at the prompt. Observe that you don't need to type tm
in front of runtm.
Options
The simulator allows runtm to be executed with two
optional arguments. If you type
runtm('reverse','1011',verbose=False)
then the simulator will only print the final halted
configuration. If you type
runtm('reverse','1011',bidirectional=False)
the specification will be interpreted as for a machine with a
one-way infinite tape (see Assignment 2). The two
options can be combined.