Wednesday, May 11, 2016

A Turing Machine For Calculating The Fibonacci Sequence

Table 1: Representation of the Fibonacci Sequence
Input/Output TapeTerms in Series
0b1;1;1, 1
0b1;1;11;1, 1, 2
0b1;1;11;1111, 1, 2, 3
0b1;1;11;111;11111;1, 1, 2, 3, 5
0b1;1;11;111;11111;11111111;1, 1, 2, 3, 5, 8

1.0 Introduction

I thought I would describe the program for a specific Turing machine. This Turing machine computes the Fibonacci sequence in tally arithmetic, as illustrated in Table 1 above. The left-hand column shows the tape for the Turing machine for successive transitions into the Start state. (The location of the head is indicated by the bolded character.) The right-hand column shows a more familiar representation of a Fibonacci sequence. This Turing machine never halts for valid inputs. It can calculate other infinite sequences, such as specific Lucas sequences, for other valid inputs.

A Turing machine is specified by the alphabet of characters that can appear on the tape, possible valid sequences of characters for the start of the tape, the location of the head at the beginning of a computation, the states and the state transition rules, and the location of the state pointer at beginning of a computation.

2.0 Alphabet

Table 2: The Alphabet For The Input Tape
SymbolNumber Of
Occurrences
Comments
01Start of tape marker
bPotentially InfiniteBlank
;Potentially InfiniteSymbol for number termination
1Potentially InfiniteA tally
x1For internal use
y1For internal use
z1For internal use

3.0 Specification of Valid Input Tapes

At start, the (input) tape should contain, in this order:

  • 0, the start of tape marker.
  • b, a blank.
  • Zero or more 1s.
  • ;, a semicolon.
  • One or more of the following:
    • Zero or more 1s.
    • ;, a semicolon.

The head shall be at a blank or semicolon such that exactly two semicolons exist in the tape to the right of the head. Table 3 provides examples (with the head being at the bolded character).

Table 3: Examples of Valid Initial Input
0b;;
0b1;;
0b1;1;
0b11;1;
0b1;1;11;111;11111;11111111;

4.0 Definition of State

The states are grouped into two subroutines, CopyPair and Add. Error is the only halting state, to be entered when an invalid input tape is detected. The Turing machine begins the computation with the state pointer pointing to the Start state, in the CopyPair subroutine. Eventually, the Turing machine enters the PauseCopy state. The machine then transitions to the StartAdd state, in the Add subroutine. Another number in the sequence has been successfully appended to the tape when the Turing machine enters the PauseAdd state.

The Turing machine then transitions into the Start state. The CopyPair and Add subroutines are repeated in pairs forever.

4.1 CopyPair

The input tape for the CopyPair subroutine is any valid input tape, as described above. The state pointer starts in the Start tape. Error is the only halting state. The subroutine exits with a transition from the PauseCopy state to the StartAdd state. When the PauseCopy state is entered, the tape shall be in the following configuration:

  • The terminal semicolon in the tape, when the Start state was entered, shall be replaced with a z.
  • The head shall be at that z.
  • The tape to the right of the z shall contain a copy of the character string to the right of the head when the Start state was entered.

This subroutine can be implemented by the states described in Table 4. The detailed implementation of each state is provided in the appendix. Throughout these states, there are transitions to the Error state triggered by encountering on the tape a character that cannot be there in a valid computation.

Table 4: States in the CopyPair Subroutine
StateDescription
StartMoves the head forward one character.
ReadFirstCharReplaces first ; or 1 (after position of head when the subroutine was called) with x or y, respectively.
WriteFirstSemiWrites a ; at the end of the tape. Transitions to GoToTapeEnd.
WriteFirstOneWrites a 1 at the end of the tape. Transitions to GoToTapeEnd.
GoToTapeEndMoves the head backward one character to locate the head at the character that was at the end of the tape when the subroutine was called.
MarkTapeEndReplaces original terminating ; with z.
NexCharReplaces the x or y on the tape with ; or 1, respectively.
StepForwardMoves the head forward one character.
ReadCharReplaces the next ; or 1 with x or y, respectively.
WriteSemiWrites a ; at the end of the tape. Transitions to NextChar.
WriteOneWrites a 1 at the end of the tape. Transitions to NextChar.
WriteLastSemiWrites a ; at the end of the tape. Transitions to SetHead.
SetHeadMoves head to the z on the tape.
PauseCopyFor noting that last two numbers on the tape, when the subroutine was called, have been copied to the end of the tape.

4.2 Add

When the PauseAdd state is entered, the tape shall be in the following configuration:

  • The semicolon between the z and the last semicolon, when the StartAdd state is entered, shall be replaced by a 1, if there is at least one 1 between this character and the terminating semicolon.
  • The semicolon at the end of the tape, when the StartAdd state is entered, shall be erased (replaced by a blank).
  • The character before the erased semicolon shall be replaced by a semicolon.
  • The z shall be replaced by a semicolon.
  • The head shall be at a semicolon such that two semicolons exist to the right of the head.

Table 5: States in the Add Subroutine
StateDescription
StartAddMoves the head forward one character.
FindSemiForDeleReplaces the ; mid-number with 1.
FindSumEndErases terminating ;.
EndSumWrites terminating ; at the tape position one character backwards.
FindSumStartReplaces z with ;.
StepBackwardMoves the head backwards one character.
ResetHeadSet head to previous ;, before the ; just written.
PauseAddFor noting next number in Fibonacci series.

5.0 Length of Tape and the Number of States

After three run-throughs of this Turing machine, five numbers in the Fibonacci sequence will be calculated. And the tape will contain 19 characters. As shown in Table 6, the number of states is 22. For the group activity I have defined for simulating a Turing machine, 42 people are needed. (One more person is needed, in computing the next number in the sequence, to be erased from the tape than ends up as characters on the tape.) I suppose one could get by with 36 people, if one is willing to some represent two states, one in each subroutine.

Table 6: State Count
SubroutineNumber Of
States
State Names
CopyPair15Error, Start, ReadFirstChar,
WriteFirstSemi, WriteFirstOne,
GoToTapeEnd, MarkTapeEnd,
NextChar, StepForward,
ReadChar, WriteSemi,
WriteLastSemi, SetHead,
WriteOne, PauseCopy
Add7StartAdd, FindSemiForDele,
FindSumEnd, EndSum,
FindSumStart, StepBackward,
PauseAdd
Total22

Appendix A: State Transition Tables

A.1: The CopyPair Subroutine
Table A-1: Start and ReadFirstChar
StartReadFirstChar
00Error00Error
bForwardsReadFirstCharbbError
;ForwardsReadFirstChar;xWriteFirstSemi
11Error1yWriteFirstOne
xxErrorxxError
yyErroryyError
zzErrorzzError
Table A-2: WriteFirstSemi and WriteFirstOne
WriteFirstSemiWriteFirstOne
00Error00Error
b;GoToTapeEndb1GoToTapeEnd
;ForwardsWriteFirstSemi;ForwardsWriteFirstOne
1ForwardsWriteFirstSemi1ForwardsWriteFirstOne
xForwardsWriteFirstSemixForwardsWriteFirstOne
yForwardsWriteFirstSemiyForwardsWriteFirstOne
zzErrorzzError
Table A-3: GoToTapeEnd and MarkTapeEnd
GoToTapeEndMarkTapeEnd
000Error
bbbError
;BackwardsMarkTapeEnd;zNextChar
1BackwardsMarkTapeEnd11Error
xxxError
yyyError
zzzError
Table A-4: NextChar and StepForward
NextCharStepForward
00Error0
bbErrorb
;BackwardsNextChar;ForwardsReadChar
1BackwardsNextChar1ForwardsReadChar
x;StepForwardx
y1StepForwardy
zBackwardsNextCharz
Table A-5: ReadChar and WriteSemi
ReadCharWriteSemi
00Error00Error
b1Errorb;NextChar
;xWriteSemi;FowardsWriteSemi
1yWriteOne1ForwardsWriteSemi
xxErrorxForwardsWriteSemi
yyErroryForwardsWriteSemi
zzWriteLastSemizForwardsWriteSemi
Table A-6: WriteLastSemi and SetHead
WriteLastSemiSetHead
00Error00Error
b;SetHeadbbError
;ForwardsWriteLastSemi;BackwardsSetHead
1ForwardsWriteLastSemi1BackwardsSetHead
xForwardsWriteLastSemixxError
yForwardsWriteLastSemiyyError
zForwardsWriteLastSemizzPauseCopy
Table A-7: WriteOne and PauseCopy
WriteOnePauseCopy
00Error0
b1NextCharb
;ForwardsWriteOne;
1ForwardsWriteOne1
xForwardsWriteOnex
yForwardsWriteOney
zForwardsWriteOnezzStartAdd
A.2: The Add Subroutine
Table A-8: StartAdd and FindSemiForDele
StartAddFindSemiForDele
00
bb
;;1FindSumEnd
11ForwardsFindSemiForDele
xx
yy
zForwardsFindSemiForDelez
Table A-9: FindSumEnd and EndSum
FindSumEndEndSum
00
bBackwardsEndSumbBackwardsEndSum
;bEndSum;bEndSum
1ForwardsFindSumEnd1;FindSumStart
xx
yy
zz
Table A-10: FindSumStart and StepBackward
FindSumStartStepBackward
00
bb
;BackwardsFindSumStart;BackwardsResetHead
1BackwardsFindSumStart1
xx
yy
z;StepBackwardz
Table A-11: ResetHead and PauseAdd
ResetHeadPauseAdd
00
bb
;;PauseAdd;;Start
1BackwardsResetHead1
xx
yy
zz
A.3: Modifications?

The above is my first working version. I have not proven that cases can never arise where I have not specified rules in the tables for the states for the Add subroutine. Nor do I know that all rules can be triggered by some, possibly invalid, input tape. I know that I have not defined the minimum number of states for the system. For example, the ReadChar state could be defined as in Table A-12, along with the elimination of the WriteLastSemi and SetHead states. This would result in the CopyPair subroutine specification not being met and a tighter coupling between the two subroutines. On the other hand, the subroutines are already coupled through the appearance of z on the tape during the transition from one subroutine to the other.

Table A-12: Modified ReadChar
ReadChar
00Error
b1Error
;xWriteSemi
1yWriteOne
xxError
yyError
zzPauseCopy

No comments:

Post a Comment