turing.icn: Program to simulate a Turing machine

November 14, 1994; Gregg M. Townsend
This file is in the public domain.
   This program simulates the operation of an n-state Turing machine,
tracing all actions.  The machine starts in state 1 with an empty tape.

   A description of the Turing machine is read from the file given as a
command-line argument, or from standard input if none is specified.
Comment lines beginning with '#' are allowed, as are empty lines.

   The program states must be numbered from 1 and must appear in order.
Each appears on a single line in this form:

    sss.  wdnnn  wdnnn

sss is the state number in decimal.  The wdnnn fields specify the
action to be taken on reading a 0 or 1 respectively:

    w   is the digit to write (0 or 1)
    d   is the direction to move (L/l/R/r, or H/h to halt)
    nnn is the next state number (0 if halting)

Sample input file:

    1. 1r2 1l3
    2. 1l1 1r2
    3. 1l2 1h0

   One line is written for each cycle giving the cycle number, current
state, and an image of that portion of the tape that has been visited
so far.  The current position is indicated by reverse video (using
ANSI terminal escape sequences).

   Input errors are reported to standard error output and inhibit
execution.

   Bugs:

   Transitions to nonexistent states are not detected.
Reverse video should be parameterizable or at least optional.
There is no way to limit the number of cycles.
Infinite loops are not detected.  (Left as an exercise... :-)

Reference:

   Scientific American, August 1984, pp. 19-23.  A. K. Dewdney's
discussion of "busy beaver" turing machines in his "Computer
Recreations" column motivated this program.  The sample above
is the three-state busy beaver.

Source code | Program Library Page | Icon Home Page