A Turing machine (TM) is a mathematical model of computation that defines an abstract machine, which manipulates symbols on an infinitely long strip of tape according to a table of rules. Despite the model’s simplicity, given any computer algorithm, a Turing machine capable of simulating that algorithm’s logic can be constructed.
Actions of the turing machine in a certain state \(q_0\) always go according the to the following schedule:
We can also use a TM to compute functions on values. To do this we initialise the tape with the input value to our function. Then we execute the machine and look at what remains on the tape.
We can tranform the table of rules into a prolog predicate transition/5
.
transition(CState, CTape, NState, NTape, >)
if and only if \(\delta(\texttt{CState},\texttt{CTape}) = (\texttt{NState},\texttt{NTape}, \rightarrow)\) entransition(CState, CTape, NState, NTape, <)
if and only if \(\delta(\texttt{CState},\texttt{CTape}) = (\texttt{NState},\texttt{NTape}, \leftarrow)\)If a TM arrives at an accepting state (indicated by accepting(State)
), it stops executing and “returns true”. On the other hand, if a halting state (halting(State)
) is reached the input is rejected (return false).
accepting(qh). % qh is een accepting toestand
% current value new write on
% state tape state tape movement
transition( qs , blank , qs , blank , > ).
transition( qs , C , q0 , C , > ) :-C\==blank.
transition( q0 , C , q0 , C , > ) :-C\==blank.
transition( q0 , blank , q1 , blank , < ).
transition( q1 , 1 , q1 , 0 , < ).
transition( q1 , 0 , q2 , 1 , < ).
transition( q2 , C , q2 , C , < ) :-C\==blank.
transition( q2 , blank , qh , blank , > ).
Write a predicate turing(+State, +Input, -Output)
that unifies Output
with
ok(Tape)
if the TM started in ([], blank, Input)
stops in a accepting/1
state with Tape
being the content of the tape starting from the head.fail(Tape)
if the TM started in ([], blank, Input)
stops in a halting/1
state with Tape
being the content of the tape starting from the head.If the TM cannot make a further move it is considered stuck, in this case the predicate should fail.
Use a helper predicate turing(State, Pre, CTape, Post, Output)
that is the if the Turing machine started in state State
on the tape described by Pre, CTape, Post
gives the output Output
(as described above). The tape is represented by two stacks. Pre
is a stack of elements before the head, with the one on top being the nearest to the head. Cur
is the tape symbol currently under the TM’s head. Post
is a stack of elements after the head, again with the closes elements on top. It the TM reaches a place that is not yet defined (the stack is empty) the value of blank
should be inserted.
This is an example
qs
V
| | | 0 | 9 | 8 | 7 | 2 | 1 | 9 | | | |
State = qs
Pre = [9,0,blank,blank]
Cur = 8
Post = [7,2,1,9,blank,blank,blank]
After applying transition(qs,8,q0,1,>)
the tape looks like this:
q0
V
| | | 0 | 9 | 1 | 7 | 2 | 1 | 9 | | | |
State = q0
Pre = [1,9,0,blank,blank]
Cur = 7
Post = [2,1,9,blank,blank,blank]
For this exercise, you may assume that the the given Turing machine is deterministic (at most one transition applies to a state).
in the literature, there is a Turing machine that stops if the Rieman Hypothesis is false. Using the interpreter you write here you may become a dollar millionaire because proving the Rieman hypothesis wrong (or true) is a Millennium problem1.