Documentation

CslibFrontier.Computability.WordRAM.OfTuring

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 #

Main definitions #

Main results #

Encodings #

The code of a tape symbol in a memory cell: 0 for a blank and b.toNat + 1 for a bit.

def Cslib.WordRAM.OfTuring.stateCode {m : ℕ} {Q : Type} (e : Q ≃ Fin m) :
Option Q → ℕ

The code of a control state, m for the halting state.

The half-tape of a cell: 1 for a negative position and 0 otherwise.

The index of a cell in its half-tape.

def Cslib.WordRAM.OfTuring.tapeAddr (k n : ℕ) (i : Fin k) (z : ℤ) :

The memory address of cell z of work tape i of a machine with k work tapes, on an input of length n: the half-tapes of all tapes are interleaved past the input.

theorem Cslib.WordRAM.OfTuring.lt_tapeAddr (k n : ℕ) (i : Fin k) (z : ℤ) :
n < tapeAddr k n i z
theorem Cslib.WordRAM.OfTuring.tapeAddr_injective {k n : ℕ} {i j : Fin k} {z z' : ℤ} (h : tapeAddr k n i z = tapeAddr k n j z') :
i = j ∧ z = z'

Distinct cells have distinct addresses.

theorem Cslib.WordRAM.OfTuring.tapeAddr_ne {k n : ℕ} {i j : Fin k} {z z' : ℤ} (h : (i, z) ≠ (j, z')) :
tapeAddr k n i z ≠ tapeAddr k n j z'
theorem Cslib.WordRAM.OfTuring.tapeAddr_lt {k n : ℕ} (i : Fin k) {z : ℤ} {d : ℕ} (hz : z.natAbs ≤ d) :
tapeAddr k n i z < n + 1 + 2 * k * (d + 1)

An address of a cell at distance at most d from the origin.

Moving a head #

theorem Cslib.WordRAM.OfTuring.tapeAddr_succ_of_nonneg {k n : ℕ} {i : Fin k} {z : ℤ} (hz : 0 ≤ z) :
tapeAddr k n i (z + 1) = tapeAddr k n i z + 2 * k
theorem Cslib.WordRAM.OfTuring.tapeAddr_succ_of_lt_neg_one {k n : ℕ} {i : Fin k} {z : ℤ} (hz : z < -1) :
tapeAddr k n i (z + 1) + 2 * k = tapeAddr k n i z
theorem Cslib.WordRAM.OfTuring.tapeAddr_pred_of_pos {k n : ℕ} {i : Fin k} {z : ℤ} (hz : 0 < z) :
tapeAddr k n i (z - 1) + 2 * k = tapeAddr k n i z
theorem Cslib.WordRAM.OfTuring.tapeAddr_pred_of_neg {k n : ℕ} {i : Fin k} {z : ℤ} (hz : z < 0) :
tapeAddr k n i (z - 1) = tapeAddr k n i z + 2 * k
theorem Cslib.WordRAM.OfTuring.tapeAddr_neg_one {k n : ℕ} {i : Fin k} :
tapeAddr k n i (-1) = tapeAddr k n i 0 + 1
theorem Cslib.WordRAM.OfTuring.tapeAddr_lt_iff {k n : ℕ} {i : Fin k} {z : ℤ} :
tapeAddr k n i z < n + 1 + 2 * k ↔ magOf z = 0

A head address is below n + 1 + 2k exactly at the cells 0 and -1.

Registers and instructions #

@[reducible, inline]

The number of registers of the simulator of a machine with k work tapes.

@[reducible, inline]

The register holding 0.

@[reducible, inline]

The register holding 1.

@[reducible, inline]

The register holding n + 1, the address of the right blank of an input of length n.

@[reducible, inline]

The register holding the code of the control state.

@[reducible, inline]

The register holding the number m of states.

@[reducible, inline]

The register holding the input head.

@[reducible, inline]

A scratch register for constants.

@[reducible, inline]

A scratch register for the symbol read.

@[reducible, inline]

The register holding the output tape read as a binary number.

@[reducible, inline]

The register holding 2 ^ ℓ for the length ℓ of the output tape.

@[reducible, inline]

The register holding 2k.

@[reducible, inline]

The register holding n + 1 + 2k: head addresses below it are the cells 0 and -1.

@[reducible, inline]
abbrev Cslib.WordRAM.OfTuring.rSgn {k : ℕ} (i : Fin k) :
Fin (regs k)

The register holding the half-tape of the head of tape i.

@[reducible, inline]
abbrev Cslib.WordRAM.OfTuring.rAdr {k : ℕ} (i : Fin k) :
Fin (regs k)

The register holding the address of the head cell of tape i.

@[reducible, inline]

Do nothing, in one instruction.

@[reducible, inline]

d ← i + j.

@[reducible, inline]

d ← i - j, truncated.

@[reducible, inline]

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 action on tape i: write, then move.

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.

def Cslib.WordRAM.OfTuring.stepCode {k m : ℕ} {Q : Type} (δ : Q → Option Bool → (Fin k → Option Bool) → Turing.Action k Bool Q) (e : Q ≃ Fin m) :

One step of the machine with transition function δ: dispatch on the state and the symbols read, and apply the selected action.

Place the heads of the work tapes in l on their cells 0.

def Cslib.WordRAM.OfTuring.setupCode {k m : ℕ} {Q : Type} (q₀ : Q) (e : Q ≃ Fin m) :

Initialize the constants and the initial configuration with state q₀.

def Cslib.WordRAM.OfTuring.simCode {k m : ℕ} {Q : Type} (δ : Q → Option Bool → (Fin k → Option Bool) → Turing.Action k Bool Q) (q₀ : Q) (e : Q ≃ Fin m) :

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.

def Cslib.WordRAM.OfTuring.simProgram {k m : ℕ} {Q : Type} (δ : Q → Option Bool → (Fin k → Option Bool) → Turing.Action k Bool Q) (q₀ : Q) (e : Q ≃ Fin m) :

The simulator as a flat program, returning the output tape read as a binary number.

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 contents of a tape after an optional write at position p.

structure Cslib.WordRAM.OfTuring.TapesRel {k : ℕ} (n : ℕ) (x : Data (regs k)) (T : Fin k → ℤ → Option Bool) (P : Fin k → ℤ) :

The registers and memory of the work tapes represent tape contents T and head positions P, on an input of length n.

  • sgn (i : Fin k) : x.regs (rSgn i) = sgnOf (P i)

    The half-tape registers hold the half-tapes of the heads.

  • adr (i : Fin k) : x.regs (rAdr i) = tapeAddr k n i (P i)

    The address registers hold the addresses of the head cells.

  • tape (i : Fin k) (z : ℤ) : x.mem (tapeAddr k n i z) = symCode (T i z)

    Every cell of every tape holds the code of its symbol.

structure Cslib.WordRAM.OfTuring.Consts {k : ℕ} (n m : ℕ) (x : Data (regs k)) :

The constant registers hold their values, on an input of length n.

structure Cslib.WordRAM.OfTuring.Sim {k m : ℕ} {Q : Type} (e : Q ≃ Fin m) {inp : List Bool} (x : Data (regs k)) (c : Turing.Cfg k Bool Q inp) :

The data x of the simulator represents the configuration c.

Correctness of the pieces #

theorem Cslib.WordRAM.OfTuring.stepCode_spec {k m : ℕ} {Q : Type} {w : ℕ} {e : Q ≃ Fin m} (δ : Q → Option Bool → (Fin k → Option Bool) → Turing.Action k Bool Q) {inp : List Bool} {c : Turing.Cfg k Bool Q inp} {q : Q} (hq : c.state = some q) {x : Data (regs k)} (hx : Sim e x c) (hfit : ∀ (i : Fin k), Fits w (tapeAddr k inp.length i (((δ q c.inputSymbol c.workTapeSymbols).apply c).workTapePos i))) (hbound : inp.length + 2 * k + m + 2 < 2 ^ w) (hout : ((δ q c.inputSymbol c.workTapeSymbols).apply c).output.length < w) :
∃ (y : Data (regs k)), ExecWithin w (stepCode δ e) x y (stepCost k m) ∧ Sim e y ((δ q c.inputSymbol c.workTapeSymbols).apply c)

One step. The simulator's step code follows one step of a running configuration.

theorem Cslib.WordRAM.OfTuring.setupCode_spec {k m : ℕ} {Q : Type} {w : ℕ} {e : Q ≃ Fin m} (q₀ : Q) (inp : List Bool) (hbound : inp.length + 2 * k + m + 2 < 2 ^ w) :
∃ (y : Data (regs k)), ExecWithin w (setupCode q₀ e) (Data.init (List.map Bool.toNat inp)) y (2 * k + 10) ∧ Sim e y (Turing.Cfg.init q₀ inp)

Setup. The simulator's setup reaches a representation of the initial configuration.

Runs #

The output tape only grows.

theorem Cslib.WordRAM.OfTuring.loop_spec {k m : ℕ} {Q : Type} {tm : Turing.MultiTapeTM k Bool Q} {inp : List Bool} {δ : Q → Option Bool → (Fin k → Option Bool) → Turing.Action k Bool Q} (hδ : ∀ (q : Q) (a : Option Bool) (s : Fin k → Option Bool), tm.tr q a s = δ q a s) (e : Q ≃ Fin m) {w t₀ : ℕ} (hrun : ∀ j < t₀, (Turing.MultiTapeTM.runFrom (tm.initCfg inp) j).state ≠ none) (hhalt : (Turing.MultiTapeTM.runFrom (tm.initCfg inp) t₀).state = none) (hfit : ∀ j ≤ t₀, ∀ (i : Fin k), Fits w (tapeAddr k inp.length i ((Turing.MultiTapeTM.runFrom (tm.initCfg inp) j).workTapePos i))) (hbound : inp.length + 2 * k + m + 2 < 2 ^ w) (hout : ∀ j ≤ t₀, (Turing.MultiTapeTM.runFrom (tm.initCfg inp) j).output.length < w) (d j : ℕ) (x : Data (regs k)) :
j + d = t₀ → Sim e x (Turing.MultiTapeTM.runFrom (tm.initCfg inp) j) → ∃ (y : Data (regs k)), ExecWithin w (Code.loop (Cond.lt rState rStates) (stepCode δ e)) x y (d * (stepCost k m + 2) + 2) ∧ Sim e y (Turing.MultiTapeTM.runFrom (tm.initCfg inp) t₀)

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.

theorem Cslib.WordRAM.OfTuring.executes_simProgram {k m : ℕ} {Q : Type} {tm : Turing.MultiTapeTM k Bool Q} {inp : List Bool} {δ : Q → Option Bool → (Fin k → Option Bool) → Turing.Action k Bool Q} (hδ : ∀ (q : Q) (a : Option Bool) (s : Fin k → Option Bool), tm.tr q a s = δ q a s) (e : Q ≃ Fin m) {t : ℕ} (ht : (Turing.MultiTapeTM.runFrom (tm.initCfg inp) t).state = none) {w : ℕ} (hw : simBound k m inp.length t < 2 ^ w) (hout : (Turing.MultiTapeTM.runFrom (tm.initCfg inp) t).output.length < w) :

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 #

theorem Cslib.WordRAM.OfTuring.lt_two_pow_logWidth (D d n : ℕ) :
D * (n + 1) ^ d < 2 ^ logWidth (D + d) n

At word size logWidth (D + d) n, words hold numbers below D (n + 1) ^ d.

theorem Turing.MultiTapeTM.ComputableInPolyTime.exists_wordRAM {α : Type u_1} {β : Type u_2} {f : α → β} {encIn : α ↪ List Bool} {encOut : β ↪ List Bool} (h : ComputableInPolyTime f encIn encOut) :
∃ (r : ℕ) (p : Cslib.WordRAM.Program Cslib.WordRAM.Basis.additive r) (T : ℕ → ℕ) (W : ℕ → ℕ), Cslib.PolynomiallyBounded T ∧ Cslib.PolynomiallyBounded W ∧ p.ComputesInTime (fun (x : ℕ) (a : α) => List.map Bool.toNat (encIn a)) (fun (a : α) (x : ℕ) => T (encIn a).length) (fun (a : α) (w : ℕ) => W (encIn a).length ≤ w) fun (a : α) => Cslib.BitString.lsbValue (encOut (f a))

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.

theorem Turing.MultiTapeTM.ComputableInPolyTime.exists_wordRAM_nat {α : Type u_1} {f : α → ℕ} {encIn : α ↪ List Bool} (h : ComputableInPolyTime f encIn encNat) :
∃ (r : ℕ) (p : Cslib.WordRAM.Program Cslib.WordRAM.Basis.additive r) (T : ℕ → ℕ) (W : ℕ → ℕ), Cslib.PolynomiallyBounded T ∧ Cslib.PolynomiallyBounded W ∧ p.ComputesInTime (fun (x : ℕ) (a : α) => List.map Bool.toNat (encIn a)) (fun (a : α) (x : ℕ) => T (encIn a).length) (fun (a : α) (w : ℕ) => W (encIn a).length ≤ w) f

Polynomial-time Turing machines on the word RAM, for binary outputs: the program returns the value itself.