Documentation

CslibFrontier.Computability.Machines.AuxPDA.PrefixQuery

Deterministic queries on stored registers and read-only input #

A query runs a deterministic machine on an input split between a stored prefix and the physical input. The counting auxiliary pushdown automaton preserves its stack throughout the query. exists_prefixQuery_of_inSpace gives a polynomial clock and a logarithmic visited-cell bound starting with the prefix prepared. The answer is buffered on a rewound work tape. Framing the register has its own linear clock and visited-cell bound. The composed framing and query theorem certifies the clock and unique occurrence trace. Two raw work words can be packed and framed as a live pair before the same query runs.

noncomputable def Turing.CountingAuxPDA.prefixQuery {k : ℕ} {Symbol StackSym Q : Type} {m : ℕ} (tm : MultiTapeTM k Symbol Q) (layout : Fin (k + 1) ↪ Fin m) :

Run a deterministic tape query on selected work tapes of a counting auxiliary pushdown automaton. The last selected tape supplies a prepared prefix followed by the physical input.

theorem Turing.CountingAuxPDA.prefixQuery_q₀ {k : ℕ} {Symbol StackSym Q : Type} {m : ℕ} (tm : MultiTapeTM k Symbol Q) (layout : Fin (k + 1) ↪ Fin m) :

The initial state of a prefix query reads from the prepared prefix.

theorem Turing.CountingAuxPDA.prefixQuery_actions_length_le_one {k : ℕ} {Symbol StackSym : Type} {input : List Symbol} {Q : Type} {m : ℕ} (tm : MultiTapeTM k Symbol Q) (layout : Fin (k + 1) ↪ Fin m) (cfg : AuxPDA.Cfg m Symbol StackSym (MultiTapeTM.InputPrefix.Control Q) input) :
((prefixQuery tm layout).actions cfg).length ≤ 1

A prefix query offers at most one transition occurrence at every configuration.

theorem Turing.CountingAuxPDA.prefixQuery_runBranch {k : ℕ} {Symbol StackSym : Type} {input : List Symbol} {Q : Type} {m : ℕ} (tm : MultiTapeTM k Symbol Q) (pre : List Symbol) (cfg : Cfg k Symbol Q (pre ++ input)) (layout : Fin (k + 1) ↪ Fin m) (tapes : Fin m → ℤ → Option Symbol) (heads : Fin m → ℤ) (stack : List StackSym) {t : ℕ} (ht : (MultiTapeTM.runFrom cfg t).Halted) :
have start := MultiTapeTM.embed layout (MultiTapeTM.InputPrefix.config pre cfg) tapes heads; have final := MultiTapeTM.embed layout (MultiTapeTM.InputPrefix.config pre (MultiTapeTM.runFrom cfg t)) tapes heads; (prefixQuery tm layout).HaltsWithin (2 * t) { tapes := start, stack := stack } ∧ ∃! indices : List ℕ, indices.length ≤ 2 * t ∧ (prefixQuery tm layout).runBranch { tapes := start, stack := stack } indices = some { tapes := final, stack := stack }

Every branch of the query halts within the doubled clock. Its unique complete trace retains the auxiliary stack and the unselected work tapes.

theorem Turing.CountingAuxPDA.exists_prefixQuery_of_inSpace {ρ α β : Type} {encR : ρ ↪ List Bool} {encA : α ↪ List Bool} {encB : β ↪ List Bool} {f : ρ × α → β} {S : ρ × α → ℕ} (live : ρ × α → Prop) (s : ℕ) (h : MultiTapeTM.ComputableInSpace f (MultiTapeTM.encPair encR encA) encB S) (hctx : MultiTapeTM.ComputableInSpace.CoversContext encR encA S) (hbudget : ∀ (p : ρ × α), live p → S p ≤ s * ((encA p.2).length.log2 + 1)) :
∃ (k : ℕ) (Q : Type) (_ : Finite Q) (tm : MultiTapeTM (k + 1) Bool Q) (C : ℕ) (d : ℕ) (W : ℕ), ∀ (p : ρ × α), live p → have input := encA p.2; have pre := MultiTapeTM.frame (encR p.1); have cfg := MultiTapeTM.InputPrefix.config pre (tm.initCfg (pre ++ input)); have done := MultiTapeTM.InputPrefix.config pre (wordsCfg (pre ++ input) none (Function.update (fun (x : Fin (k + 1)) => []) (Fin.last k) (encB (f p))) []); ∃ (t : ℕ), 2 * t ≤ C * (input.length + 1) ^ d ∧ ∀ {m : ℕ} (layout : Fin (k + 1 + 1) ↪ Fin m) (tapes : Fin m → ℤ → Option Bool) (heads : Fin m → ℤ), (MultiTapeNTM.RunPath.ofDeterministic ((MultiTapeTM.InputPrefix.machine tm).extendTapes layout) (MultiTapeTM.embed layout cfg tapes heads) (2 * t)).space ≤ W * (input.length.log2 + 1) + (m - (k + 1 + 1)) ∧ ∀ {StackSym : Type} (stack : List StackSym), (prefixQuery tm layout).HaltsWithin (2 * t) { tapes := MultiTapeTM.embed layout cfg tapes heads, stack := stack } ∧ ∃! indices : List ℕ, indices.length ≤ 2 * t ∧ (prefixQuery tm layout).runBranch { tapes := MultiTapeTM.embed layout cfg tapes heads, stack := stack } indices = some { tapes := MultiTapeTM.embed layout done tapes heads, stack := stack }

A workspace certificate for a query on registers and read-only input gives a reusable native query routine on the live register values. The selected machine clears its scratch tapes and returns both input heads to their entry positions, leaving only the buffered answer. The clock is polynomial in the physical input length and the visited work cells are logarithmic.

Read a word on work tape 1, writing its frame on blank work tape 0. Tape 2 is blank scratch. The physical input head is stationary.

theorem Turing.MultiTapeTM.runsBetween_frameWorkWord {input : List Bool} (pos : Fin (input.length + 2)) (word : List Bool) (origins : Fin 3 → ℤ) (out : List Bool) :
frameWorkWord.RunsBetween (Cfg.shift origins (wordsCfgAt input pos (some ()) ![[], word, []] out)) (Cfg.shift origins (wordsCfgAt input pos (some ()) ![frame word, word, []] out)) (6 * word.length + 15) (6 * word.length + 20)

Framing uses linear time and work cells and preserves the source word and physical input cursor. Both word heads finish at their entry coordinates, which can be chosen independently.

Form the framed encoding of two raw words on work tapes 1 and 3. Tapes 0 and 2 are blank scratch. The joined word replaces the second word and its left endpoint moves left by the framed first word's length.

theorem Turing.MultiTapeTM.runsBetween_packWorkPair {input : List Bool} (pos : Fin (input.length + 2)) (first second : List Bool) (origins : Fin 4 → ℤ) (out : List Bool) :
have start := FreezeInput.atPos pos (tapesCfg [] ![[], first, [], second] origins origins out); have finalOrigins := Function.update origins 3 (origins 3 - ↑(frame first).length); have finish := FreezeInput.atPos pos (tapesCfg [] ![[], first, [], (encPair encBits encBits) (first, second)] finalOrigins finalOrigins out); have steps := 6 * first.length + 15 + ((frame first).length + 1 + ((frame first).length + 2)); packWorkPair.RunsBetween start finish steps (4 * (steps + 1))

With the heads on the first cells of the two words and their blank scratch tapes, the packing machine preserves the first word and physical input cursor. Its time and visited work cells are linear in the length of the first word.

theorem Turing.CountingAuxPDA.frameWorkWord_prefixQuery_runBranch {k m : ℕ} {StackSym : Type} {input : List Bool} {Q : Type} (tm : MultiTapeTM k Bool Q) (layout : Fin (k + 1) ↪ Fin m) (e : Fin 3 ↪ Fin m) (hshared : e 0 = layout (Fin.last k)) (hsep : ∀ (i : Fin k), layout i.castSucc ∉ Set.range ⇑e) (word : List Bool) (origin : ℤ) (tapes : Fin m → ℤ → Option Bool) (heads : Fin m → ℤ) (hblank : ∀ (i : Fin k), tapes (layout i.castSucc) = fun (x : ℤ) => none) (hhead : ∀ (i : Fin k), heads (layout i.castSucc) = 0) (stack : List StackSym) :
have before := Cfg.shift ![1, origin, 0] (MultiTapeTM.wordsCfgAt input 0 (some ()) ![[], word, []] []); have after := Cfg.shift ![1, origin, 0] (MultiTapeTM.wordsCfgAt input 0 (some ()) ![MultiTapeTM.frame word, word, []] []); have ready := MultiTapeTM.embed e after tapes heads; have prep := ofMultiTapeCTM (MultiTapeCTM.ofDeterministic (MultiTapeTM.frameWorkWord.extendTapes e)); have query := prefixQuery tm layout; have init := MultiTapeTM.InputPrefix.config (MultiTapeTM.frame word) (tm.initCfg (MultiTapeTM.frame word ++ input)); have entry := Sequential.left query { tapes := (MultiTapeTM.embed e before tapes heads).withState (some prep.q₀), stack := stack }; ∀ {indices : List ℕ} {out : Cfg m Bool (MultiTapeTM.InputPrefix.Control Q) input}, query.runBranch { tapes := MultiTapeTM.embed layout init ready.workTapes ready.workTapePos, stack := stack } indices = some { tapes := out, stack := stack } → out.Halted → (prep.seq query).HaltsWithin (6 * word.length + 15 + indices.length) entry ∧ ∃! trace : List ℕ, (prep.seq query).runBranch entry trace = some (Sequential.right { tapes := out, stack := stack })

Frame a live register on the selected prefix tape and then run the query on that prefix. The framing and query share the prefix tape, and the query's other selected tapes start blank. The live register begins at origin with its head on the first cell. The two phases have a single complete occurrence trace and leave the auxiliary stack unchanged.

def Turing.CountingAuxPDA.packedPrefixQueryReady {m : ℕ} {input : List Bool} (e : Fin 4 ↪ Fin m) (first second : List Bool) (tapes : Fin m → ℤ → Option Bool) (heads : Fin m → ℤ) :
Cfg m Bool Unit input

The tapes at the handoff to a packed query, before the query starts. The prefix begins at cell one, the joined word is retained, and the untouched tapes keep their contents.

theorem Turing.CountingAuxPDA.packedPrefixQuery_runBranch {k m : ℕ} {StackSym : Type} {input : List Bool} {Q : Type} (tm : MultiTapeTM k Bool Q) (layout : Fin (k + 1) ↪ Fin m) (e : Fin 4 ↪ Fin m) (hshared : e 0 = layout (Fin.last k)) (hsep : ∀ (i : Fin k), layout i.castSucc ∉ Set.range ⇑e) (first second : List Bool) (tapes : Fin m → ℤ → Option Bool) (heads : Fin m → ℤ) (hblank : ∀ (i : Fin k), tapes (layout i.castSucc) = fun (x : ℤ) => none) (hhead : ∀ (i : Fin k), heads (layout i.castSucc) = 0) (stack : List StackSym) :
have word := (MultiTapeTM.encPair MultiTapeTM.encBits MultiTapeTM.encBits) (first, second); have startData := MultiTapeTM.FreezeInput.atPos 0 (tapesCfg [] ![[], first, [], second] ![1, 0, 0, 0] ![1, 0, 0, 0] []); have ready := packedPrefixQueryReady e first second tapes heads; have pre := MultiTapeTM.frame word; have init := MultiTapeTM.InputPrefix.config pre (tm.initCfg (pre ++ input)); have query := prefixQuery tm layout; have machine := packedPrefixQuery tm layout e StackSym; have cfg := { tapes := (MultiTapeTM.embed e startData tapes heads).withState (some machine.q₀), stack := stack }; have packSteps := 6 * first.length + 15 + ((MultiTapeTM.frame first).length + 1 + ((MultiTapeTM.frame first).length + 2)); ∀ {indices : List ℕ} {out : Cfg m Bool (MultiTapeTM.InputPrefix.Control Q) input}, query.runBranch { tapes := MultiTapeTM.embed layout init ready.workTapes ready.workTapePos, stack := stack } indices = some { tapes := out, stack := stack } → out.Halted → machine.HaltsWithin (packSteps + (6 * word.length + 15 + indices.length)) cfg ∧ ∃! trace : List ℕ, ∃ (final : AuxPDA.Cfg m Bool StackSym (((MultiTapeTM.MarkWorkState ⊕ ((Unit × Option Bool × Bool ⊕ MultiTapeTM.RewindState) ⊕ MultiTapeTM.RewindWorkState) ⊕ MultiTapeTM.MarkWorkState) ⊕ Unit ⊕ MultiTapeTM.CopyBackState) ⊕ (MultiTapeTM.MarkWorkState ⊕ ((Unit × Option Bool × Bool ⊕ MultiTapeTM.RewindState) ⊕ MultiTapeTM.RewindWorkState) ⊕ MultiTapeTM.MarkWorkState) ⊕ MultiTapeTM.InputPrefix.Control Q) input), machine.runBranch cfg trace = some final ∧ final.Halted ∧ final.tapes.workTapes = out.workTapes ∧ final.tapes.workTapePos = out.workTapePos ∧ final.stack = stack

The packed query starts on two raw words on tapes e 1 and e 3 at cell zero. It uses e 0 as the prefix tape, e 2 as blank scratch, and leaves the existing stack intact. The linear preparation cost adds to the query's occurrence clock.