Proofs: RAM and Turing machine equivalence
These are the human proofs of RAM and Turing machine equivalence, at full length.
A multitape Turing machine simulates a logarithmic-cost RAM in time O(t^2) full
This proof shows: ram-by-tm.
Statement. Let be a RAM program with the instructions LOAD, STORE, ADD, SUB, READ, JUMP, JZERO, JEOF, HALT, as on the random-access machine page. Let decide in logarithmic-cost time : on every input word of length , given as integers in , halts with logarithmic cost at most , and with if and if . Then there is a deterministic 5-tape Turing machine that decides in steps. The constant depends on only.
Setup. Fix an input of length and the run of on . Let the run execute instructions, numbered , and let be the logarithmic cost of instruction . Then . Every cost is at least 1, so . We write for the bit length, with . Let be the largest length of a number that occurs in the text of (a constant, a register number or a jump target). Every constant below depends on only.
Encoding. Numbers are written in binary on the tapes with the least significant bit first and with no leading zeros, and is the string 0. A number on a tape is followed by a blank or by a separator.
Tapes of .
- Tape 1 holds the input . Its head moves only to the right, and it is on the next unread symbol.
- Tape 2, the memory tape, holds a list of records
#a:v, where and are numbers. The list stands in the first cells, and the cells after it are blank. The addresses of different records are different. A register with that has no record has the value . We write for the length of the list. - Tape 3 holds the accumulator .
- Tapes 4 and 5 are work tapes. Tape 4 holds an address, and tape 5 holds a value or a copy.
The program counter is in the finite control. Because has finitely many instructions, the control has finitely many states.
Invariant . Before instruction of the run, the control is at the instruction that executes as instruction . Tape 3 holds the value of . Tape 2 holds exactly one record #a:v for each register with that a STORE has written in the instructions , with its current value . Tapes 4 and 5 are blank. Tape 1 has read exactly as many symbols as the instructions READ before . All heads except head 1 are on cell 0.
Step 1: the length of the memory tape. Let be the length of the list before instruction . We show . A record #a:v has length . It is in the list because of the last STORE that wrote the register , and at that time received the value from . By the cost table, the cost of STORE i is , and the cost of STORE *i is . In both cases , and also . So the record has length at most . Different records belong to different instructions STORE, since one STORE writes one register. So the lengths add up to at most .
Step 2: the subroutines. All subroutines start and end with the heads of tapes 2, 4 and 5 on cell 0.
- FIND. Tape 4 holds a number . The machine moves head 2 over the list. For each record it compares with the address, symbol by symbol, with head 4 and head 2 moving together, until the symbols differ or one string ends. If both strings end together, the record matches. Then it moves head 4 back to cell 0 and head 2 to the end of the record. For one record this takes steps, since the comparison makes at most as many steps as the shorter address has symbols plus one, and the rewind of head 4 takes as many. So FIND takes steps. It stops at the first match. It ends in one of two states: found, with head 2 on the
:of the record, or not found, with head 2 on the first blank after the list. - LOOKUP. FIND, then copy the value after the
:to tape 5 if found, or write0on tape 5 if not found. The result on tape 5 is the value of . LOOKUP takes steps where is the value, and if found. So it takes steps. If , the address names the accumulator (a program can reach only through an indirect operand whose register holds ): LOOKUP copies tape 3 to tape 5 and does not use the list. - UPDATE. Tape 4 holds and tape 3 holds . If , do nothing, since
STOREthen copies to . Else FIND. If found, copy the part of the list after the old value to tape 5 and blank it on tape 2, write after the:, copy the part back from tape 5 after , and blank tape 5. This takes steps. If not found, write#, ,:and at the end of the list, which takes steps. Then rewind. The total is steps. - ARITH. Tapes 3 and 5 hold and . Add: scan both from the left with a carry, and write the sum on tape 3. Subtract: scan with a borrow; if a borrow remains at the end, write
0, else erase the leading zeros of the result. Each takes steps.
Step 3: one instruction. Assume . Let be the length of before instruction . We show that the machine executes instruction in at most steps if is a LOAD or a READ, in at most steps if is a STORE, ADD, SUB or JZERO, and in steps if is a JUMP, JEOF or HALT, and that holds after it. Only LOAD and READ pay outside . In the other instructions that read , by the cost table. Operands: for the operand the machine writes on tape 5 in steps. For the operand it writes on tape 4, which takes steps, and it does LOOKUP, which leaves on tape 5. For the operand it writes on tape 4 and does LOOKUP, so tape 5 holds . It then copies this number to tape 4, which takes steps, blanks tape 5 in steps, and does LOOKUP again, so tape 5 holds . Each of these takes steps, and each of the lengths that occur is part of the cost of the instruction.
LOAD X: get the operand on tape 5 as above, and copy tape 5 to tape 3, after erasing the old content. This takes more steps, and .STORE i,STORE *i: put the address on tape 4 (for*i, by LOOKUP of , a copy to tape 4 and the blanking of tape 5, which cost steps), so that tape 5 is blank, and do UPDATE with on tape 3. The register gets . The records of the other registers do not change. The cost is , and .ADD X,SUB X: get the operand on tape 5, and do ARITH. The cost is , and . The result is or , as ARITH computes.READ: erase tape 3, and write the symbol of tape 1 under head 1 on tape 3 (the number is 0 or 1), and move head 1 right. If head 1 reads a blank, no input is left, and the machine writes0and does not move head 1. This takes steps.JUMP k: set the control to .JZERO k: read tape 3 and jump if it is0.JEOF k: jump if head 1 reads a blank.HALT: accept if tape 3 holds1, else reject. Each takes steps.
After each instruction, the machine rewinds the heads to cell 0 (not head 1). This costs at most the lengths already counted. The effects are those of the RAM instruction, so holds. For HALT the answer is the value of , so accepts if and only if halts with .
Step 4: the length of the accumulator. In the list of Step 3, only LOAD and READ pay for the old length outside . In ADD and SUB the length is a part of . Let be the last instruction before that set (LOAD, ADD, SUB or READ). Then : for LOAD X the new value is the value of , and its length is a part of ; for ADD the new length is at most , where is the old value; for SUB it is at most the length of the old , which is a part of ; for READ it is . If no instruction has set , then . A setter is the last setter before at most one later instruction , namely the next setter. So , where the sum is over the instructions LOAD and READ.
Step 5: the total. The first step of writes 0 on tape 3, and this makes true. By Step 3, Step 1 and Step 4, the total time of the simulation is at most
$$ K \sum_{s=1}^{N} (m_s + c_s + \lambda_s) + K' \le K\bigl( N \cdot 2t(n) + t(n) + t(n) + 1 \bigr) + K'. $$
Here the sum of the is over the instructions LOAD and READ, and the other terms are bounded by Step 1 (so ) and by . Since and , this is . By the invariant at the halting instruction, decides .
Remark. The bound uses that every instruction costs at least 1, so that the number of instructions is at most the time. It also uses that a STORE costs at least the length of its record, so that the list is as short as the time. These two facts are the reason for the logarithmic cost rule. With the unit rule and an instruction MULT, repeated squaring and a STORE make a record of length , and the proof fails.
A RAM simulates a multitape Turing machine in logarithmic-cost time O(T log T) full
This proof shows: tm-by-ram.
Statement. Let be a deterministic -tape Turing machine, with the tape alphabet of symbols and the input alphabet , that decides and halts within steps on every input of length , where . Then there is a RAM program (with the instructions of the random-access machine page, no multiplication) that decides in unit-cost time and in logarithmic-cost time . The constants depend on only.
Setup. Write with , and , and use the index as the code of . A blank cell has code , which is the value of a register that was never written. Let . The registers are used like this.
- for holds the address of the register that stores the cell scanned by head .
- is a pointer for the copy of the input.
- The cell of tape is stored in the register with the address . These addresses are at least and are all different, for different pairs , since is the remainder of the offset modulo and is the quotient.
- is the accumulator.
The program has two parts. Part I copies the input into the cells of tape 1.
LOAD =B
STORE k+1 ; p = B
loop JEOF done
READ ; acc = w_c, 0 or 1
ADD =1 ; code of the bit: 1 for 0, 2 for 1
STORE *(k+1) ; the cell gets the code
LOAD k+1
ADD =k
STORE k+1 ; p = p + k
JUMP loop
done LOAD =B+i-1 ; for each i = 1..k
STORE i ; head i on cell 0
JUMP S(q0)
Part II has a block for each non-halting state of and a block for each of accept and reject. The block is LOAD =1, HALT. The block is LOAD =0, HALT. The block is a decision tree of depth that finds the scanned symbols. At a node for tape , with the symbols of the tapes already fixed by the path, it runs the tests for in this order. The test for is
LOAD *i
SUB =a
JZERO (the node for tape i+1 in which the symbol of tape i is a)
and if all tests fail, the code goes on to the node in which the symbol of tape is . The tree has leaves, one for each tuple of codes, and each leaf is a separate piece of code. In the leaf , let . The leaf has these instructions, for in turn.
LOAD =b_i, thenSTORE *i: write the symbol.- If :
LOAD i,ADD =k,STORE i. - If :
LOAD i,SUB =B+i-1,JZERO skip_i,LOAD i,SUB =k,STORE i, and then the labelskip_i. - If : nothing.
The leaf ends with JUMP S(q'). The program has instructions, which is a constant.
Step 1: the test finds the symbol. Let , the code of the scanned symbol, with . The test for computes , and the jump is taken if and only if . The tests run for in this order. If , then the tests for fail, since . The test for jumps. So the first jump is at . If no test jumps, then , so . Hence the leaf that the block reaches is the leaf of the true symbols. (After each test the accumulator is overwritten by the next LOAD *i, so the tests do not interact.)
Step 2: the left move. The cell of tape has the address , and every cell has an address of at least this value. So SUB =B+i-1 gives if and only if , that is, if and only if head is on cell . In that case the move keeps the head in place, as in the machine, since JZERO skips the subtraction. Else SUB =k moves the address of head one cell to the left (the address of cell is that of cell minus ).
Invariant . After steps of (and after Part I for ), the control is at the beginning of , where is the state of after steps. For each the register holds the address of the cell scanned by head . For each and the register with the address holds the code of the symbol in cell of tape .
Proof of . The registers other than those of tape 1 are , which is the code of the blank, and tape is blank at the start. Part I stores the codes of the input symbols at the addresses for , since starts at and grows by at each pass, and JEOF done ends the loop after passes. The cells of tape 1 are blank and the registers are . The heads start on cell , whose address is .
Proof of . Assume and non-halting. By Step 1 the block reaches the leaf of the true tuple of symbols. In the leaf, for each the instruction pair LOAD =b_i, STORE *i writes the symbol into the register of the scanned cell, and then the move updates as in Step 2. Different tapes use different registers, so the order in which the tapes are handled does not matter. The leaf jumps to . This is the configuration of after step . If is accept, halts with , and if reject with . (of the invariant)
Step 3: the cost. Let halt after steps on the input. A head moves at most one cell in a step, so during the run, every scanned cell has an index of at most (the input has length ). So every address and every register value that occurs is at most , and every constant in the program is bounded by a constant. An instruction costs at most four lengths (by the cost table), each at most , since . The number of instructions executed is as follows.
- Part I executes instructions with the length of the input, since each pass of the loop executes 8 instructions and the end has .
- Each step of executes at most instructions: at most for each tape in the tests, at most for each tape in the leaf (the longest case is the move with 6 instructions plus 2 for the write), and 1 for the jump at the end of the leaf.
- The last block has 2 instructions.
So the number of instructions is at most , using . This is the unit-cost time . Multiplying by the cost per instruction, the logarithmic-cost time is . By the invariant, halts with exactly when accepts.