Documentation

CslibFrontier.Computability.WordRAM.Turing

Word-RAM polynomial time gives Turing-machine polynomial time #

A fixed word-RAM program that runs within polynomially many instructions gives a multi-tape Turing machine computing the same function in polynomial time, in the sense of Turing.MultiTapeTM.ComputableInPolyTime. This is the polynomial simulation of random access machines by Turing machines Cook and Reckhow, for the unit-cost word RAM with a word size polynomial in the input length.

Conventions #

Limits #

These theorems cover one fixed program (UniformComputableInPolyTime). A width-indexed family (WordRAM.ComputableInPolyTime) may choose arbitrary, even uncomputable, code at each width, so it gives Turing-machine polynomial time only with a computable code map, which is not provided. The size measure must be polynomially bounded in the encoded input length: a polynomial instruction count in the numeric value of an input number is exponential in its bit length and does not transfer. Width-dependent word encodings are covered only by the general theorem, with a width schedule chosen by the caller. Outputs are single words.

Main definitions #

Main results #

References #

Word bounds on finite simulation states #

The jump or branch target of an instruction, and 0 otherwise.

An upper bound on every control label that a run from label 0 reaches.

A bound on the encoded outcome of a simulation step with r registers, labels at most L, w-bit words and at most E stored cells.

Registers, the memory default and every stored cell (address and value) of a finite simulation state are w-bit words.

Reading memory returns a w-bit word.

theorem Cslib.WordRAM.WordBounded.set {B : Basis} {r w : ℕ} {s : GenericRAM.FiniteState B.genericModel r} (h : WordBounded w s) (d : Fin r) {v : ℕ} (hv : v < 2 ^ w) :
WordBounded w (s.set d v)

Setting a register to a word preserves the bound.

theorem Cslib.WordRAM.WordBounded.write {B : Basis} {r w : ℕ} {s : GenericRAM.FiniteState B.genericModel r} (h : WordBounded w s) {a v : ℕ} (ha : a < 2 ^ w) (hv : v < 2 ^ w) :
WordBounded w (s.write a v)

Storing a word at a word address preserves the bound.

theorem Cslib.WordRAM.WordBounded.withPC {B : Basis} {r w : ℕ} {s : GenericRAM.FiniteState B.genericModel r} (h : WordBounded w s) (pc : ℕ) :
WordBounded w { pc := pc, regs := s.regs, default := s.default, memory := s.memory }

Changing the control label preserves the bound.

A data instruction keeps every register and stored cell a word.

A continuing step keeps the word bound and stays below the label bound.

A halting step returns a word.

Every outcome of a clocked run from a word-bounded state below the label bound is word-bounded: continuing states keep their words and labels, and halted values are words.

Encoded outcomes along a clocked run from a word-bounded state are bounded by stateBound, with at most one new stored cell per step.

Initialization #

The stored cells of State.init input: the length at address 0, word i at i + 1.

The finite memory of State.init input.

theorem Cslib.WordRAM.read_initMemory (input : List ℕ) (a : ℕ) :
GenericRAM.SparseMemory.read 0 a (initMemory input) = if a = 0 then input.length else input.getD (a - 1) 0

initMemory stores the input convention of State.init.

The finite simulation state of State.init input, at label 0 with zero registers.

theorem Cslib.WordRAM.denote_initState {B : Basis} {r : ℕ} (input : List ℕ) :

The finite initial state denotes State.init.

theorem Cslib.WordRAM.wordBounded_initState {B : Basis} {r w : ℕ} {input : List ℕ} (h : InputFits w input) :
WordBounded w (initState B r input)

From an input that fits, the finite initial state holds only words.

@[simp]
theorem Cslib.WordRAM.initState_pc {B : Basis} {r : ℕ} (input : List ℕ) :
(initState B r input).pc = 0

From a fitting input, the encoded outcomes of the first T steps of a clocked run are bounded by stateBound with |input| + 1 + T stored cells.

A word list fits every word size at least as large as its encoded length.

Word RAM ⇒ Turing machine #

theorem Cslib.WordRAM.Program.DecidesInTime.computesInTime {B : Basis} {r : ℕ} {α : Type} {p : Program B r} {encode : ℕ → α → List ℕ} {A : Set α} {time : α → ℕ → ℕ} {allowed : α → ℕ → Prop} [DecidablePred fun (x : α) => x ∈ A] (h : p.DecidesInTime encode A time allowed) :
p.ComputesInTime encode time allowed fun (a : α) => if a ∈ A then 1 else 0

A decider computes the indicator of its problem, 1 on members and 0 otherwise, with the same time bound and admitted widths.

theorem Cslib.WordRAM.exists_unary_majorant {α : Type} {encIn : α ↪ List Bool} {u : α → ℕ} (h : Turing.MultiTapeTM.PolyBounded (fun (a : α) => (encIn a).length) u) :
∃ (g : α → ℕ), Turing.MultiTapeTM.ComputableInPolyTime g encIn Turing.MultiTapeTM.encUnary ∧ ∀ (a : α), u a ≤ g a

A quantity polynomially bounded in the encoded input length has a majorant computable in unary in polynomial time: a fixed polynomial of the input length.

The simulation's initial states are computable in polynomial time when the input words are: build the memory 0 ↦ n, i + 1 ↦ xᵢ and set zero registers and default.

theorem Cslib.WordRAM.exists_polyBounded_resultEncoding_run_le {B : Basis} {r : ℕ} {α : Type} {encIn : α ↪ List Bool} (p : GenericRAM.Program B.genericModel r) {width : α → ℕ} {words : α → List ℕ} {steps : α → ℕ} (hwidth : Turing.MultiTapeTM.ComputableInPolyTime width encIn Turing.MultiTapeTM.encUnary) (hwords : Turing.MultiTapeTM.ComputableInPolyTime words encIn (Turing.MultiTapeTM.encList Turing.MultiTapeTM.encNat)) (hfits : ∀ (a : α), InputFits (width a) (words a)) (hsteps : Turing.MultiTapeTM.PolyBounded (fun (a : α) => (encIn a).length) steps) :
∃ (bound : α → ℕ), Turing.MultiTapeTM.PolyBounded (fun (a : α) => (encIn a).length) bound ∧ ∀ (a : α), ∀ j ≤ steps a, ((GenericRAM.FiniteState.resultEncoding B.encoding) (GenericRAM.FiniteState.run (width a) p j (initState B r (words a)))).length ≤ bound a

The simulation's outcomes are polynomially bounded. For a word size and fitting input words computable in polynomial time, and a step budget polynomial in the input length, the encoded outcomes of the first steps a steps are bounded by one polynomially bounded function.

theorem Cslib.WordRAM.Program.ComputesInTime.turingComputableInPolyTime_of_width {B : Basis} {r : ℕ} {α : Type} {encIn : α ↪ List Bool} (hop : ∀ (op : B.Op), Turing.MultiTapeTM.ComputableInPolyTime (B.genericModel.checkedOp op) encWidthOperands (Turing.MultiTapeTM.encOption Turing.MultiTapeTM.encNat)) {p : Program B r} {encode : ℕ → α → List ℕ} {time : α → ℕ → ℕ} {allowed : α → ℕ → Prop} {f : α → ℕ} (h : p.ComputesInTime encode time allowed f) {width : α → ℕ} (hwidth : Turing.MultiTapeTM.ComputableInPolyTime width encIn Turing.MultiTapeTM.encUnary) (hallowed : ∀ (a : α), allowed a (width a)) (hfits : ∀ (a : α), InputFits (width a) (encode (width a) a)) (hencode : Turing.MultiTapeTM.ComputableInPolyTime (fun (a : α) => encode (width a) a) encIn (Turing.MultiTapeTM.encList Turing.MultiTapeTM.encNat)) (htime : Turing.MultiTapeTM.PolyBounded (fun (a : α) => (encIn a).length) fun (a : α) => time a (width a)) :

Word-RAM time at a polynomial-time width schedule gives Turing-machine polynomial time.

Let p be a fixed program over a basis whose checked operations are polynomial-time on a unary width and binary operands. Suppose width a is computable in unary in polynomial time, is an admitted width for a, and the word list encode (width a) a fits it and is computable in polynomial time from encIn a. If p computes f (Program.ComputesInTime) and its time bound at width a is polynomial in |encIn a|, then f is computable in polynomial time with binary output. The simulating machine computes the width, the input words and a step budget, builds the initial memory and runs the finite-state simulator; all of this is charged.

theorem Cslib.WordRAM.Program.ComputesInTime.turingComputableInPolyTime {B : Basis} {r : ℕ} {α : Type} {encIn : α ↪ List Bool} (hop : ∀ (op : B.Op), Turing.MultiTapeTM.ComputableInPolyTime (B.genericModel.checkedOp op) encWidthOperands (Turing.MultiTapeTM.encOption Turing.MultiTapeTM.encNat)) {p : Program B r} {encode : α → List ℕ} {size f : α → ℕ} {T : ℕ → ℕ} {k : ℕ} (hT : PolynomiallyBounded T) (hsize : Turing.MultiTapeTM.PolyBounded (fun (a : α) => (encIn a).length) size) (hencode : Turing.MultiTapeTM.ComputableInPolyTime encode encIn (Turing.MultiTapeTM.encList Turing.MultiTapeTM.encNat)) (h : p.ComputesInTime (fun (x : ℕ) => encode) (fun (a : α) (x : ℕ) => T (size a)) (fun (a : α) (w : ℕ) => logWidth k (size a) ≤ w) f) :

Polynomial word-RAM time gives Turing-machine polynomial time for a fixed program.

Let p be a fixed program over a basis whose checked operations are polynomial-time on a unary width and binary operands. Suppose that on every input a whose words fit, at every word size of at least logWidth k (size a), p halts with f a from State.init (encode a) within T (size a) instructions, where T is polynomially bounded. If the word list encode a is computable in polynomial time from the bit string encIn a and size a is polynomially bounded in |encIn a|, then f is computable in polynomial time with binary output.

The simulating machine chooses a word size polynomial in |encIn a|, at least logWidth k (size a) and the encoded length of the input words, so the input fits.

Uniform polynomial word-RAM time gives Turing-machine polynomial time, for a width-independent word encoding computable in polynomial time and a size measure polynomially bounded in the encoded input length.