Word RAMs simulate multi-tape Turing machines #
A multi-tape Turing machine with a binary alphabet and finitely many states that halts within t
steps is simulated by one fixed word-RAM program over Basis.additive in O(t) instructions,
at every word size whose words hold addresses linear in t and the input length. Together with
Cslib.WordRAM.Program.ComputesInTime.turingComputableInPolyTime_of_width, this links polynomial
time on the two models. The construction is the usual one, tape cells as memory words and the
finite control as branches, written for the word RAM of Cslib.WordRAM; it is not transcribed
from a source.
Conventions #
- Input. The input bit string
xis given as the word listx.map Bool.toNat, soState.initplaces its length at address0and its bits at addresses1, …, |x|. The simulator reads the input there; it is not copied. The input head of a configuration is shifted by one (Turing.Cfg.inputPos), so it is the address of the symbol it reads, and the two blank cells0and|x| + 1are recognized by comparisons. - Work tapes. Cell
zof work tapeiis the memory celltapeAddr k |x| i z, past the input. The two half-tapesz ≥ 0andz < 0of allktapes are interleaved, so the address of cellzis linear in|z|. A cell holds0for a blank andb.toNat + 1for a bitb, so the zeroed memory beyond the input is the blank tape. Each tape keeps the sign of its head and the address of its head cell in registers. - Control. The state is a register, with
mfor the halting state. A step reads the input and work symbols and dispatches through a finite decision tree on the state and the symbols read; its leaves apply fixed actions. The tree is built from the transition function, so the program depends only on the machine. - Output. The program returns the output tape read as a little-endian binary number,
Cslib.BitString.lsbValue. A decisionb, written as[b], givesb.toNat; a binary numberv, written asNat.bits v, givesv. The output must be shorter than the word size. - Cost. Unit cost per instruction, the final
haltincluded. A step costs at moststepCost k m = 3m + 13k + 14instructions and the whole run at mostsimTime k m t = 2k + 13 + t (3m + 13k + 16).
Main definitions #
Cslib.WordRAM.OfTuring.tapeAddr: the memory address of a work-tape cell.Cslib.WordRAM.OfTuring.simCode,Cslib.WordRAM.OfTuring.simProgram: the simulator of the machine with a given transition function, initial state and numbering of its states.Cslib.WordRAM.OfTuring.Sim: the data of the simulator represents a configuration.
Main results #
Cslib.WordRAM.OfTuring.setupCode_spec,Cslib.WordRAM.OfTuring.stepCode_spec,Cslib.WordRAM.OfTuring.loop_spec: the setup represents the initial configuration, one step of the simulator follows one step of the machine, and the loop follows the run to its halt.Cslib.WordRAM.OfTuring.executes_simProgram: a halting run oftsteps is simulated withinsimTime k m tinstructions, returning the valueCslib.BitString.lsbValueof the output tape.Turing.MultiTapeTM.ComputableInPolyTime.exists_wordRAM,Turing.MultiTapeTM.ComputableInPolyTime.exists_wordRAM_nat: a function computable in polynomial time by a Turing machine is computed by a uniform word-RAM program in polynomially many instructions, at every word size above a polynomial threshold.
Encodings #
The half-tape of a cell: 1 for a negative position and 0 otherwise.
Moving a head #
Registers and instructions #
The number of registers of the simulator of a machine with k work tapes.
The register holding 0.
The register holding 1.
The register holding n + 1, the address of the right blank of an input of length n.
The register holding the code of the control state.
The register holding the number m of states.
The register holding the input head.
A scratch register for constants.
A scratch register for the symbol read.
The register holding the output tape read as a binary number.
The register holding 2 ^ ℓ for the length ℓ of the output tape.
The register holding 2k.
The register holding n + 1 + 2k: head addresses below it are the cells 0 and -1.
Do nothing, in one instruction.
d ← i + j.
d ← i - j, truncated.
d ← #v.
The simulator #
Dispatch on the contents of register reg: run body j when it holds j < N, comparing it
with the constants N - 1, …, 0 loaded into rTmp.
Read the input symbol and continue with next of it.
Read the symbols under the heads of the work tapes rem - 1, …, 0, record them in acc, and
continue with next of the recorded symbols.
Move the head of tape i one cell to the right.
Move the head of tape i one cell to the left.
Move the head of tape i.
Write on tape i, if the action writes.
The actions on the work tapes in l.
Move the input head, which stays on the blanks 0 and n + 1 at the ends.
Append a symbol to the output tape.
Apply an action.
Place the heads of the work tapes in l on their cells 0.
Initialize the constants and the initial configuration with state q₀.
The simulator of the machine with transition function δ and initial state q₀, whose states
are numbered by e: set up, then step while the state is not halting.
An upper bound on the instructions of one simulated step.
An upper bound on the instructions of a simulated run of t steps, the final halt
included.
Words of the simulator of a run of t steps on an input of length n stay below this bound,
except for the output.
The invariant #
The registers and memory of the work tapes represent tape contents T and head positions P,
on an input of length n.
The half-tape registers hold the half-tapes of the heads.
The address registers hold the addresses of the head cells.
Every cell of every tape holds the code of its symbol.
The constant registers hold their values, on an input of length n.
The data x of the simulator represents the configuration c.
The constant registers hold their values.
rStateholds the code of the state.rHeadholds the input head, which is the address of the input symbol it reads.rValholds the output tape read as a binary number.rPowholds2 ^ ℓfor the lengthℓof the output tape.The work tapes are represented.
The input bits stay at addresses
1, …, n.
Correctness of the pieces #
One step. The simulator's step code follows one step of a running configuration.
Setup. The simulator's setup reaches a representation of the initial configuration.
Runs #
The output tape only grows.
The loop. From a representation of the configuration after j steps, the loop of the
simulator reaches a representation of the halting configuration after t₀ = j + d steps, within
d (stepCost k m + 2) + 2 instructions.
Turing machines on the word RAM. Let tm be a machine with k work tapes and transition
function δ, whose states are numbered by e. If tm halts on inp within t steps, then at
every word size w whose words hold simBound k m |inp| t and the length of the output tape, the
simulator halts on inp.map Bool.toNat within simTime k m t instructions, returning the output
tape read as a little-endian binary number.
Polynomial time #
Polynomial-time Turing machines on the word RAM. A function computable in polynomial time
by a multi-tape Turing machine is computed by one fixed word-RAM program over Basis.additive, on
the input bits as words, within a polynomial number of instructions, at every word size above a
polynomial threshold. The program returns the encoded output read as a little-endian binary
number. The threshold is polynomial because one word holds the whole output.
Polynomial-time Turing machines on the word RAM, for binary outputs: the program returns the value itself.