logo

Turing machines in Python

How to make your computer emulate a computer.

Image by Hans Johnson — source and license.

While you may not have access to a physical Turing machine, that should’nt stop you from simulating a Turing machine with… your computer! I will cover how Turing machines function, and Python code to build a simple one to test for alleged palindromes. I know that programs that check for palindromes are common exercise for programming beginners, and that you could simply check online or even copy some code in your favorite language. However, let’s pretend that deep down you would like to use a Turing machine for the task.

This article is inspired by Chapter 5 of by Robert Geroch — Amazon.

You can find the complete code on GitHub here.

What is a Turing machine?

A Turing machines consists of three parts:

  1. A tape.
  2. A write head
  3. A machine state.

The tape is divided into a sequence of squares, each of which may store a single character belonging to a given character set.

That didn’t answer much

Why are Turing machines interesting? You’ve definitely heard of them already, so let’s just copy it quickly from Turing’s own words and leave further details to Geroch’s chapter referenced above.

It is possible to invent a which can be used to compute computable sequence.

A Turing machine can simulate anything, given unlimited storage space!

How do Turing machines function?

The machine works based on a table of rules. At any given step, the write head is over some square on the tape. At this time point, the machine: (1) reads that square of the tape and (2) reads the state that the machine is in. The machine then looks in the table of rules to determine: (1) which new character to write, (2) which new state to go to, and (3) which direction, one square left or right, the head should move. Of course, the machine is allowed to print the same character and maintain the same state.

When has the machine finished? A special state is reserved for this purpose. Once the machine enters this state, the program has finished running.

Technically, we will make several simplifcations to the Turing machine. First, we will assume we have access at all times to the machine’s state, and can simply measure whether or not it has finished. That is, there will be no need for the machine to write on the tape to indicate it has finished. As for the code, if you will, ignore the inefficiencies and focus on the function.

Implementing the Turing machine

The turing machine has three elements (which we will implement as follows):

  1. the state (a string named state)
  2. the write head (an integer named write_head)
  3. the tape (a list named tape_list)

As a simple implementation, we will write a class with these elements as follows:

We’ll work on the table of rules next, defined in updateMachine.

Table of Rules

What about the table of rules? The table that needs to be implemented is given below — an explanation follows below it.

*There’s a good reason for this that has to do with the length of the string (even number vs odd), which hopefully will be illuminated in the examples below.

This warrants some explanation! Let’s start with a definition of the states:

  • q1 — the initial state. Here, we read in a character n that corresponds to the nth character in the character list (provided it is not zero). This information is “stored” in the state by going in the corresponding state pn.
  • pn — state after reading the nth character initially — now go right and search for the end of the string, marked by a zero.
  • rn — state after finding zero on the end of the string — now compare the last character of the string. If it’s a palindrome, it should be the same as the nth character that we read at the beginning, and we go into state q2. else, go to state qn, meaning “no, its not a palindrome.”
  • q2 — state after sucessful comparison of last character in string — now go left and search for the beginning of the string, marked by a zero. Restart the loop by going to q1.

If you are confused, some examples may be illuminating. In the tables below, we list the tape at each step (where the position of the write head is underlined), and the state and char.

First, the word “of”, which is not a palindrome:

Tape for the word “of”.

In the last line, since “f” was not the 15th character of the alphabet (namely, “o”, which we read in at the first step), the machine goes to state qn, indicating correctly that “of” is not a palindrome.

Second, the characters “bbb”, which is a palindrome:

Tape for the word “bbb”.

That was horribly long, but it worked! Notice that to switch from p2 to r2 the second time while reading a zero, our extra * condition in the rules table was used.

Or, whatever, it works.

For the code, again, keep it simple and use if and elif statements:

Setting up input/output

We still need a way to input a string into the program, and also to set the initial state of the machine, the starting write-head position, and write the string onto the tape. Let’s do all of that now:

Running the code

Finally, let’s run the code! Again, we cheated and are accessing the state at each iteration to check whether or not the program has finished (state qn or qy). However, you can easily imagine printing on the tape a special character to indicate the result if desired (we’ll leave it as an “exercise”’). Create a file called test.py with contents:

Running this gives:

> python test.py hello
Checking: hello
- - -
q1 0 ['h', 'e', 'l', 'l', 'o', 0]
p7 1 [0, 'e', 'l', 'l', 'o', 0]
p7 2 [0, 'e', 'l', 'l', 'o', 0]
p7 3 [0, 'e', 'l', 'l', 'o', 0]
p7 4 [0, 'e', 'l', 'l', 'o', 0]
p7 5 [0, 'e', 'l', 'l', 'o', 0]
r7 4 [0, 'e', 'l', 'l', 'o', 0]
qn 3 [0, 'e', 'l', 'l', 'o', 0]
- - -
hello is NOT a palindrome! Steps: 7

and:

Checking: ooo
- - -
q1 0 ['o', 'o', 'o', 0]
p14 1 [0, 'o', 'o', 0]
p14 2 [0, 'o', 'o', 0]
p14 3 [0, 'o', 'o', 0]
r14 2 [0, 'o', 'o', 0]
q2 1 [0, 'o', 0, 0]
q2 0 [0, 'o', 0, 0]
q1 1 [0, 'o', 0, 0]
p14 2 [0, 0, 0, 0]
r14 1 [0, 0, 0, 0]
q2 0 [0, 0, 0, 0]
q1 1 [0, 0, 0, 0]
qy 2 [0, 0, 0, 0]
- - -
ooo is a palindrome! Steps: 12

Final thoughts ==============

That was definitely the most tedious way ever conceived to check for palindromes! But the magic is: there is no limit to what you can implement with a Turing machine — until your computer runs out of storage.

Contents

Oliver K. Ernst
July 20, 2020

Read this on Medium