Buckets:
import Mathlib
import OAI.Computability.MaxCut.Machines.Runtime2
import OAI.Computability.MaxCut.Machines.MachineCloudPrefix
namespace OAI
namespace MaxCutGames.Foundations.Complexity.MachineRegularTable
open Turing MachineComposition
/-! The outer construction uses independent finite registers for its existing
subprograms. This transport combines tape placement, an untouched register
frame, and a static register equivalence, without adding any machine steps. -/
namespace Lift
open MachineCloudPadding
variable {K K' Λ Λ' σ τ υ : Type}
def statement (tape : K → K') (labels : Λ → Λ') (exit : Option Λ')
(registers : (σ × τ) ≃ υ) (q : TM2.Stmt (fun _ : K => Bool) Λ σ) :
TM2.Stmt (fun _ : K' => Bool) Λ' υ :=
MachineStateEquiv.statement registers
(MachineStateFrame.frameStatement (Placement.statement tape labels exit q))
def configuration (view : K' → Option K) (labels : Λ → Λ') (exit : Option Λ')
(registers : (σ × τ) ≃ υ) (ambient : τ) (extra : K' → List Bool)
(c : TM2.Cfg (fun _ : K => Bool) Λ σ) : TM2.Cfg (fun _ : K' => Bool) Λ' υ :=
⟨Placement.label labels exit c.l, registers (c.var, ambient),
Placement.tapes view c.stk extra⟩
variable [DecidableEq K] [DecidableEq K']
theorem stepAux (tape : K → K') (view : K' → Option K)
(left : ∀ k, view (tape k) = some k)
(right : ∀ j k, view j = some k → tape k = j)
(labels : Λ → Λ') (exit : Option Λ') (registers : (σ × τ) ≃ υ)
(ambient : τ) (extra : K' → List Bool)
(q : TM2.Stmt (fun _ : K => Bool) Λ σ) (state : σ) (source : K → List Bool) :
TM2.stepAux (statement tape labels exit registers q) (registers (state, ambient))
(Placement.tapes view source extra) =
configuration view labels exit registers ambient extra (TM2.stepAux q state source) := by
rw [statement, MachineStateEquiv.stepAux_transport, MachineStateFrame.frame_stepAux,
Placement.stepAux_simulation tape view left right]
rfl
theorem step (tape : K → K') (view : K' → Option K)
(left : ∀ k, view (tape k) = some k)
(right : ∀ j k, view j = some k → tape k = j)
(labels : Λ → Λ') (exit : Option Λ') (registers : (σ × τ) ≃ υ)
(ambient : τ) (extra : K' → List Bool)
(source : Λ → TM2.Stmt (fun _ : K => Bool) Λ σ)
(target : Λ' → TM2.Stmt (fun _ : K' => Bool) Λ' υ)
(code : ∀ l, target (labels l) = statement tape labels exit registers (source l))
(a b : TM2.Cfg (fun _ : K => Bool) Λ σ) (h : TM2.step source a = some b) :
TM2.step target (configuration view labels exit registers ambient extra a) =
some (configuration view labels exit registers ambient extra b) := by
cases a with
| mk label state sourceTapes =>
cases label with
| none => simp [TM2.step] at h
| some label =>
have hb : TM2.stepAux (source label) state sourceTapes = b := Option.some.inj h
rw [← hb]
change some (TM2.stepAux (target (labels label)) (registers (state, ambient))
(Placement.tapes view sourceTapes extra)) = _
rw [code, stepAux tape view left right]
theorem trace (tape : K → K') (view : K' → Option K)
(left : ∀ k, view (tape k) = some k)
(right : ∀ j k, view j = some k → tape k = j)
(labels : Λ → Λ') (exit : Option Λ') (registers : (σ × τ) ≃ υ)
(ambient : τ) (extra : K' → List Bool)
(source : Λ → TM2.Stmt (fun _ : K => Bool) Λ σ)
(target : Λ' → TM2.Stmt (fun _ : K' => Bool) Λ' υ)
(code : ∀ l, target (labels l) = statement tape labels exit registers (source l))
(steps : Nat) (a b : TM2.Cfg (fun _ : K => Bool) Λ σ)
(run : (advance (TM2.step source))^[steps] (some a) = some b) :
(advance (TM2.step target))^[steps]
(some (configuration view labels exit registers ambient extra a)) =
some (configuration view labels exit registers ambient extra b) :=
liftSuccessfulTrace _ _ _
(step tape view left right labels exit registers ambient extra source target code) steps a b run
end Lift
end MaxCutGames.Foundations.Complexity.MachineRegularTable
namespace MaxCutGames.Foundations.Complexity.MachineCloudRank
open Turing MachineComposition MachineCloudCount
variable {σ : Type}
@[simp] theorem update_memory_spare (a b c d e f x : List Bool) :
Function.update (memory a b c d e f) .spare x = memory a b c d e x := by
funext k
cases k <;> rfl
inductive RankLabel
| queryFirst | querySecond | querySkip | tableFirst | tableSecond
| headerFirst | headerSecond | row | field | restore | done
| skip (i : Fin 4097)
deriving DecidableEq, Fintype
def rankSkip (i : Nat) : RankLabel :=
if h : i < 4097 then .skip ⟨i, h⟩ else .row
def rankProgram : RankLabel → TM2.Stmt Alphabet RankLabel (State σ)
| .queryFirst => Reduction.MachineTransfer.loopAt
.target .scratch id false .queryFirst (some .querySecond)
| .querySecond => MachineCopy.forkLoop
.scratch .target .spare false .querySecond (some .querySkip)
| .querySkip => MachineLookup.discard .spare .querySkip .tableFirst
| .tableFirst => Reduction.MachineTransfer.loopAt
.original .scratch id false .tableFirst (some .tableSecond)
| .tableSecond => MachineCopy.forkLoop
.scratch .original .work false .tableSecond (some .headerFirst)
| .headerFirst => MachineLookup.discard .work .headerFirst .headerSecond
| .headerSecond => MachineLookup.discard .work .headerSecond .row
| .row => MachineUnaryCounter.guard .spare .field .done
| .field => fieldLoop .field .restore
| .restore => Reduction.MachineTransfer.loopAt
.scratch .target id false .restore (some (rankSkip 0))
| .skip i => MachineLookup.discard .work (.skip i) (rankSkip (i.val + 1))
| .done => .halt
theorem rankProgram_skip {i : Nat} (hi : i < 4097) :
rankProgram (σ := σ) (rankSkip i) =
MachineLookup.discard .work (rankSkip i) (rankSkip (i + 1)) := by
simp only [rankSkip, dite_eq_left hi, rankProgram]
theorem rowGuard_succ (original work target scratch count fuelSuffix : List Bool)
(k : Nat) (ambient : σ) (flag : Bool) (register : Option Bool) :
TM2.step (rankProgram (σ := σ))
⟨some .row, ((ambient, flag), register),
memory original work target scratch count (encodeWord (k + 1) ++ fuelSuffix)⟩ =
some ⟨some .field, ((ambient, flag), none),
memory original work target scratch count (encodeWord k ++ fuelSuffix)⟩ := by
change some (TM2.stepAux (rankProgram .row) _ _) = _
simp [rankProgram, MachineUnaryCounter.guard, TM2.stepAux, encodeWord,
List.replicate_succ]
theorem rowGuard_zero (original work target scratch count fuelSuffix : List Bool)
(ambient : σ) (flag : Bool) (register : Option Bool) :
TM2.step (rankProgram (σ := σ))
⟨some .row, ((ambient, flag), register),
memory original work target scratch count (encodeWord 0 ++ fuelSuffix)⟩ =
some ⟨some .done, ((ambient, flag), none),
memory original work target scratch count (encodeWord 0 ++ fuelSuffix)⟩ := rfl
theorem zeroRowsTrace (original work target scratch count fuelSuffix : List Bool)
(ambient : σ) (flag : Bool) (register : Option Bool) :
(advance (TM2.step (rankProgram (σ := σ))))^[2]
(some ⟨some .row, ((ambient, flag), register),
memory original work target scratch count (encodeWord 0 ++ fuelSuffix)⟩) =
some ⟨none, ((ambient, flag), none),
memory original work target scratch count (encodeWord 0 ++ fuelSuffix)⟩ := by
rw [Function.iterate_succ_apply]
change advance (TM2.step (rankProgram (σ := σ)))
(TM2.step rankProgram ⟨some .row, ((ambient, flag), register),
memory original work target scratch count (encodeWord 0 ++ fuelSuffix)⟩) = _
rw [rowGuard_zero]
rfl
/-- One actual row comparison consumes one unit of fuel and restores the
entire vertex query before discarding the remaining fixed row fields. -/
theorem rankRowTrace (r : Row) (hwidth : r.2.length = 4097) (v k : Nat)
(original suffix targetSuffix fuelSuffix count : List Bool)
(ambient : σ) (register : Option Bool) :
(advance (TM2.step (rankProgram (σ := σ))))^[rowTime v r]
(some ⟨some .row, ((ambient, false), register),
memory original (encodeWords (rowWords r) ++ suffix) (encodeWord v ++ targetSuffix)
[] count (encodeWord (k + 1) ++ fuelSuffix)⟩) =
some ⟨some .row, ((ambient, false), none),
memory original suffix (encodeWord v ++ targetSuffix) []
(List.replicate (if r.1 = v then 1 else 0) true ++ count)
(encodeWord k ++ fuelSuffix)⟩ := by
have hfield := preservingFieldTrace RankLabel.field RankLabel.restore (rankSkip 0)
rankProgram rfl rfl original (encodeWords r.2 ++ suffix) targetSuffix count
(encodeWord k ++ fuelSuffix) r.1 v ambient none
have hskip := discardFieldsTrace rankSkip (rankProgram (σ := σ)) r.2 0
(by intro i hi; simpa only [Nat.zero_add] using rankProgram_skip (hwidth ▸ hi))
original suffix (encodeWord v ++ targetSuffix) []
(List.replicate (if r.1 = v then 1 else 0) true ++ count)
(encodeWord k ++ fuelSuffix) ambient false none
have hnonempty : r.2 ≠ [] := by intro h; simp [h] at hwidth
simp only [Nat.zero_add, hwidth, rankSkip, lt_self_iff_false, ↓reduceDIte,
hnonempty, ite_false] at hskip
have htime : rowTime v r = (encodeWords r.2).length + (r.1 + min r.1 v + 2) + 1 := by
simp only [rowTime, rowWords, encodeWords, List.length_append, encodeWord_length]
omega
rw [htime, Function.iterate_succ_apply]
change (advance (TM2.step (rankProgram (σ := σ))))^[
(encodeWords r.2).length + (r.1 + min r.1 v + 2)]
(TM2.step rankProgram ⟨some .row, ((ambient, false), register),
memory original (encodeWords (rowWords r) ++ suffix) (encodeWord v ++ targetSuffix)
[] count (encodeWord (k + 1) ++ fuelSuffix)⟩) = _
rw [rowGuard_succ, Function.iterate_add_apply]
simp only [rowWords, encodeWords, List.append_assoc]
rw [hfield]
exact hskip
/-- Specification of the trace length; this function is not part of control. -/
def prefixTime (v : Nat) : List Row → Nat
| [] => 2
| r :: rs => prefixTime v rs + rowTime v r
/-- The row loop runs for the unary fuel on spare, not until the input ends. -/
theorem prefixTrace (rs : List Row) (hwidth : ∀ r ∈ rs, r.2.length = 4097) (v : Nat)
(original suffix targetSuffix fuelSuffix count : List Bool)
(ambient : σ) (register : Option Bool) :
(advance (TM2.step (rankProgram (σ := σ))))^[prefixTime v rs]
(some ⟨some .row, ((ambient, false), register),
memory original (encodeWords (rs.flatMap rowWords) ++ suffix)
(encodeWord v ++ targetSuffix) [] count (encodeWord rs.length ++ fuelSuffix)⟩) =
some ⟨none, ((ambient, false), none),
memory original suffix (encodeWord v ++ targetSuffix) []
(List.replicate (rowsHits v rs) true ++ count) (encodeWord 0 ++ fuelSuffix)⟩ := by
induction rs generalizing count register with
| nil =>
simpa only [prefixTime, List.flatMap_nil, encodeWords, List.nil_append,
List.length_nil, rowsHits, List.replicate_zero] using
zeroRowsTrace original suffix (encodeWord v ++ targetSuffix) [] count fuelSuffix
ambient false register
| cons r rs ih =>
have hrow := rankRowTrace r (hwidth r (by simp)) v rs.length original
(encodeWords (rs.flatMap rowWords) ++ suffix) targetSuffix fuelSuffix count ambient register
have hrest := ih (by intro x hx; exact hwidth x (by simp [hx]))
(List.replicate (if r.1 = v then 1 else 0) true ++ count) none
simp only [prefixTime, List.flatMap_cons, encodeWords_append, List.append_assoc,
List.length_cons]
rw [Function.iterate_add_apply, hrow]
simpa only [rowsHits, ← List.append_assoc, List.replicate_append_replicate,
Nat.add_comm] using hrest
theorem prefixTime_le (v : Nat) (rs : List Row) :
prefixTime v rs ≤ 3 * (encodeWords (rs.flatMap rowWords)).length + 2 := by
induction rs with
| nil => simp only [prefixTime, List.flatMap_nil, encodeWords, List.length_nil,
Nat.mul_zero, Nat.zero_add, le_refl]
| cons r rs ih =>
have hr := rowTime_le v r
simp only [prefixTime, List.flatMap_cons, encodeWords_append, List.length_append]
omega
def queryWord (v k : Nat) (suffix : List Bool) : List Bool :=
encodeWord v ++ (encodeWord k ++ suffix)
theorem encoded_rows_split (rs : List Row) (k : Nat) :
encodeWords (rs.flatMap rowWords) =
encodeWords ((rs.take k).flatMap rowWords) ++
encodeWords ((rs.drop k).flatMap rowWords) := by
have h := congrArg (fun xs : List Row => encodeWords (xs.flatMap rowWords))
(List.take_append_drop k rs)
simpa only [List.flatMap_append, encodeWords_append] using h.symm
def rankSteps (n m v k : Nat) (rs : List Row) (querySuffix : List Bool) : Nat :=
2 * ((queryWord v k querySuffix).length + 1) + (v + 1) +
2 * ((inputWord n m rs).length + 1) + (n + 1) + (m + 1) +
prefixTime v (rs.take k)
/-- The full finite trace copies the query, extracts its unary row fuel,
preserves the graph while copying it, skips both headers, and counts a prefix. -/
theorem rankTrace (n m v k : Nat) (rs : List Row) (hk : k ≤ rs.length)
(hwidth : ∀ r ∈ rs, r.2.length = 4097) (querySuffix count : List Bool)
(ambient : σ) (register : Option Bool) :
(advance (TM2.step (rankProgram (σ := σ))))^[rankSteps n m v k rs querySuffix]
(some ⟨some .queryFirst, ((ambient, false), register),
memory (inputWord n m rs) [] (queryWord v k querySuffix) [] count []⟩) =
some ⟨none, ((ambient, false), none),
memory (inputWord n m rs) (encodeWords ((rs.drop k).flatMap rowWords))
(queryWord v k querySuffix) []
(List.replicate (rowsHits v (rs.take k)) true ++ count)
(encodeWord 0 ++ querySuffix)⟩ := by
have hquery := MachineCopy.copyTrace Tape.target Tape.spare Tape.scratch
(by decide) (by decide) (by decide) false RankLabel.queryFirst RankLabel.querySecond
(some RankLabel.querySkip) rankProgram rfl rfl
(memory (inputWord n m rs) [] (queryWord v k querySuffix) [] count [])
rfl (ambient, false) register
simp only [memory_target, memory_spare, List.append_nil, update_memory_spare] at hquery
have hquerySkip := MachineLookup.discardTrace Tape.spare RankLabel.querySkip RankLabel.tableFirst
rankProgram rfl
(memory (inputWord n m rs) [] (queryWord v k querySuffix) [] count (queryWord v k querySuffix))
v (encodeWord k ++ querySuffix) rfl (ambient, false) none
simp only [update_memory_spare] at hquerySkip
have hcopy := MachineCopy.copyTrace Tape.original Tape.work Tape.scratch
(by decide) (by decide) (by decide) false RankLabel.tableFirst RankLabel.tableSecond
(some RankLabel.headerFirst) rankProgram rfl rfl
(memory (inputWord n m rs) [] (queryWord v k querySuffix) [] count
(encodeWord k ++ querySuffix)) rfl (ambient, false) none
simp only [memory_original, memory_work, List.append_nil, update_memory_work] at hcopy
have hfirst := MachineLookup.discardTrace Tape.work RankLabel.headerFirst RankLabel.headerSecond
rankProgram rfl
(memory (inputWord n m rs) (inputWord n m rs) (queryWord v k querySuffix) [] count
(encodeWord k ++ querySuffix))
n (encodeWord m ++ encodeWords (rs.flatMap rowWords))
(inputWord_eq n m rs) (ambient, false) none
simp only [update_memory_work] at hfirst
have hsecond := MachineLookup.discardTrace Tape.work RankLabel.headerSecond RankLabel.row
rankProgram rfl
(memory (inputWord n m rs) (encodeWord m ++ encodeWords (rs.flatMap rowWords))
(queryWord v k querySuffix) [] count (encodeWord k ++ querySuffix))
m (encodeWords (rs.flatMap rowWords)) rfl (ambient, false) none
simp only [update_memory_work] at hsecond
have hprefix := prefixTrace (rs.take k)
(by intro r hr; exact hwidth r (List.mem_of_mem_take hr)) v
(inputWord n m rs) (encodeWords ((rs.drop k).flatMap rowWords))
(encodeWord k ++ querySuffix) querySuffix count ambient none
have htake : (rs.take k).length = k := by
simp only [List.length_take, Nat.min_eq_left hk]
rw [htake, ← encoded_rows_split rs k] at hprefix
rw [show rankSteps n m v k rs querySuffix = prefixTime v (rs.take k) +
((m + 1) + ((n + 1) + (2 * ((inputWord n m rs).length + 1) +
((v + 1) + 2 * ((queryWord v k querySuffix).length + 1))))) by
unfold rankSteps; omega,
Function.iterate_add_apply _ (prefixTime v (rs.take k)),
Function.iterate_add_apply _ (m + 1), Function.iterate_add_apply _ (n + 1),
Function.iterate_add_apply _ (2 * ((inputWord n m rs).length + 1)),
Function.iterate_add_apply _ (v + 1), hquery, hquerySkip, hcopy, hfirst, hsecond]
exact hprefix
/-- The concrete trace has a linear bound in the two preserved input words. -/
theorem rankSteps_le (n m v k : Nat) (rs : List Row) (querySuffix : List Bool) :
rankSteps n m v k rs querySuffix ≤
5 * (inputWord n m rs).length + 3 * (queryWord v k querySuffix).length + 6 := by
have hprefix := prefixTime_le v (rs.take k)
have hsplit := congrArg List.length (encoded_rows_split rs k)
simp only [List.length_append] at hsplit
have hinput := inputWord_length n m rs
have hquery : v + 1 ≤ (queryWord v k querySuffix).length := by
simp only [queryWord, List.length_append, encodeWord_length]
omega
unfold rankSteps
omega
/-- In a filtered list, the rank of a retained entry is the number of retained
entries preceding its first original occurrence. This is a specification lemma. -/
theorem idxOf_filter_prefix {α : Type*} [BEq α] [LawfulBEq α]
(p : α → Bool) (a : α) (hp : p a = true) :
∀ xs : List α, a ∈ xs →
(xs.filter p).idxOf a = ((xs.take (xs.idxOf a)).filter p).length := by
classical
intro xs
induction xs with
| nil => intro ha; simp at ha
| cons b xs ih =>
intro ha
by_cases hba : b = a
· subst b
simp [hp]
· have ha' : a ∈ xs := by simpa [hba, Ne.symm hba] using ha
cases hb : p b <;>
simp [hba, hb, ih ha']
open MaxCutGames.Foundations.PCP
theorem tableRows_length (t : GraphTables.Table) : (tableRows t).length = t.darts := by
simp only [tableRows, List.length_map, GraphTables.rowList_length]
/-- The executable cloud rank is exactly the count of matching earlier rows. -/
theorem cloudRank_eq_prefix_count (t : GraphTables.Table) (v : Fin t.vertices)
(e : DegreeReplacement.Cloud (GraphTables.semantics t) v) :
(PreprocessingCloudIndex.cloudRank t v e).val =
rowsHits v.val ((tableRows t).take e.val.val) := by
have hindex := idxOf_filter_prefix (fun x : Fin t.darts => decide (t.rows[x].tail = v))
e.val (by simpa using e.property) (List.finRange t.darts) (List.mem_finRange e.val)
simp only [List.idxOf_finRange] at hindex
change (PreprocessingCloudIndex.cloudDarts t v).idxOf e.val = _
unfold PreprocessingCloudIndex.cloudDarts
rw [hindex, rowsHits_eq_filter]
simp only [tableRows, rowList_eq_finRange_map, ← List.map_take, List.filter_map,
List.length_map, Function.comp_def, Fin.val_inj]
/-- Full trace on a genuine stored graph and a genuine member of its cloud.
The complete input table and the entire vertex/dart query remain unchanged. -/
theorem cloudRankTrace (t : GraphTables.Table) (v : Fin t.vertices)
(e : DegreeReplacement.Cloud (GraphTables.semantics t) v)
(querySuffix countSuffix : List Bool) (ambient : σ) (register : Option Bool) :
(advance (TM2.step (rankProgram (σ := σ))))^[
rankSteps t.vertices t.darts v.val e.val.val (tableRows t) querySuffix]
(some ⟨some .queryFirst, ((ambient, false), register),
memory (GraphTables.tableBits t) [] (queryWord v.val e.val.val querySuffix) []
(encodeWord 0 ++ countSuffix) []⟩) =
some ⟨none, ((ambient, false), none),
memory (GraphTables.tableBits t)
(encodeWords (((tableRows t).drop e.val.val).flatMap rowWords))
(queryWord v.val e.val.val querySuffix) []
(encodeWord (PreprocessingCloudIndex.cloudRank t v e).val ++ countSuffix)
(encodeWord 0 ++ querySuffix)⟩ := by
have hk : e.val.val ≤ (tableRows t).length := by
rw [tableRows_length]
exact e.val.isLt.le
have h := rankTrace t.vertices t.darts v.val e.val.val (tableRows t) hk
(tableRows_width t) querySuffix (encodeWord 0 ++ countSuffix) ambient register
rw [tableRows_input, ← cloudRank_eq_prefix_count t v e, ← List.append_assoc,
replicate_encodeWord, Nat.add_zero] at h
exact h
/-- A genuine timed execution witness with a polynomial (indeed linear)
transition budget. Neither the result nor a body trace is a premise. -/
def cloudRankInTime (t : GraphTables.Table) (v : Fin t.vertices)
(e : DegreeReplacement.Cloud (GraphTables.semantics t) v)
(querySuffix countSuffix : List Bool) (ambient : σ) (register : Option Bool) :
StateTransition.EvalsToInTime (TM2.step (rankProgram (σ := σ)))
⟨some .queryFirst, ((ambient, false), register),
memory (GraphTables.tableBits t) [] (queryWord v.val e.val.val querySuffix) []
(encodeWord 0 ++ countSuffix) []⟩
(some ⟨none, ((ambient, false), none),
memory (GraphTables.tableBits t)
(encodeWords (((tableRows t).drop e.val.val).flatMap rowWords))
(queryWord v.val e.val.val querySuffix) []
(encodeWord (PreprocessingCloudIndex.cloudRank t v e).val ++ countSuffix)
(encodeWord 0 ++ querySuffix)⟩)
(5 * (GraphTables.tableBits t).length +
3 * (queryWord v.val e.val.val querySuffix).length + 6) where
steps := rankSteps t.vertices t.darts v.val e.val.val (tableRows t) querySuffix
evals_in_steps := cloudRankTrace t v e querySuffix countSuffix ambient register
steps_le_m := by
simpa only [tableRows_input] using
rankSteps_le t.vertices t.darts v.val e.val.val (tableRows t) querySuffix
end MaxCutGames.Foundations.Complexity.MachineCloudRank
namespace MaxCutGames.Foundations.Complexity.MachineTableRows
open Turing
open PCP.GraphTables
inductive Label
| relationRead | relationRestore | reverseRead | reverseRestore
| tailRead | tailRestore | appendOld | appendRow | appendBack
deriving DecidableEq
protected abbrev Label.enumList : List Label := [.relationRead, .relationRestore,
.reverseRead, .reverseRestore, .tailRead, .tailRestore, .appendOld, .appendRow, .appendBack]
protected theorem Label.enumList_getElem?_ctorIdx_eq (x : Label) :
Label.enumList[x.ctorIdx]? = some x := by
cases x <;> rfl
protected theorem Label.enumList_nodup : Label.enumList.Nodup := by decide
instance : Fintype Label where
elems := ⟨Label.enumList, Label.enumList_nodup⟩
complete x := by cases x <;> decide
variable {K Λ σ : Type} [DecidableEq K]
abbrev Alphabet (_ : K) := Bool
/-- `fields 0`, `fields 1`, and `fields 2` are the tail, reverse index, and
encoded relation tapes. Every instruction reads or writes a single tape head. -/
def routine (fields : Fin 3 → K) (row output scratch : K)
(labels : Label → Λ) (exit : Option Λ) :
Label → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)
| .relationRead => Reduction.MachineTransfer.loopAt (fields 2) scratch id false
(labels .relationRead) (some (labels .relationRestore))
| .relationRestore => MachineCopy.forkLoop scratch (fields 2) row false
(labels .relationRestore) (some (labels .reverseRead))
| .reverseRead => Reduction.MachineTransfer.loopAt (fields 1) scratch id false
(labels .reverseRead) (some (labels .reverseRestore))
| .reverseRestore => MachineCopy.forkLoop scratch (fields 1) row false
(labels .reverseRestore) (some (labels .tailRead))
| .tailRead => Reduction.MachineTransfer.loopAt (fields 0) scratch id false
(labels .tailRead) (some (labels .tailRestore))
| .tailRestore => MachineCopy.forkLoop scratch (fields 0) row false
(labels .tailRestore) (some (labels .appendOld))
| .appendOld => Reduction.MachineTransfer.loopAt output scratch id false
(labels .appendOld) (some (labels .appendRow))
| .appendRow => Reduction.MachineTransfer.loopAt row scratch id false
(labels .appendRow) (some (labels .appendBack))
| .appendBack => Reduction.MachineTransfer.loopAt scratch output id false
(labels .appendBack) exit
/-- Exact row field concatenation at the level of stored bits. -/
def fieldBits (fields : Fin 3 → K) (base : K → List Bool) : List Bool :=
base (fields 0) ++ base (fields 1) ++ base (fields 2)
def fieldSize (fields : Fin 3 → K) (base : K → List Bool) : Nat :=
(base (fields 0)).length + (base (fields 1)).length + (base (fields 2)).length
omit [DecidableEq K] in
@[simp] theorem fieldBits_length (fields : Fin 3 → K) (base : K → List Bool) :
(fieldBits fields base).length = fieldSize fields base := by
simp [fieldBits, fieldSize, Nat.add_assoc]
def prefixTapes (fields : Fin 3 → K) (row : K) (base : K → List Bool) : K → List Bool :=
Function.update base row (fieldBits fields base ++ base row)
/-- The three actual preserving-copy phases, with exact tape handoffs. -/
theorem prefixTrace (fields : Fin 3 → K) (row output scratch : K)
(hfields : ∀ i, fields i ≠ row ∧ fields i ≠ scratch)
(hrowScratch : row ≠ scratch) (labels : Label → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(hprogram : ∀ label, program (labels label) = routine fields row output scratch labels exit label)
(base : K → List Bool) (hscratch : base scratch = [])
(ambient : σ) (register : Option Bool) :
(MachineComposition.advance (TM2.step program))^[2 * (fieldSize fields base + 3)]
(some ⟨some (labels .relationRead), (ambient, register), base⟩) =
some ⟨some (labels .appendOld), (ambient, none), prefixTapes fields row base⟩ := by
let afterRelation := Function.update base row (base (fields 2) ++ base row)
let afterReverse := Function.update afterRelation row
(base (fields 1) ++ afterRelation row)
have hfirst := MachineCopy.copyTrace (fields 2) row scratch
(hfields 2).1 (hfields 2).2 hrowScratch false
(labels .relationRead) (labels .relationRestore) (some (labels .reverseRead))
program (hprogram .relationRead) (hprogram .relationRestore) base hscratch ambient register
change (MachineComposition.advance (TM2.step program))^[2 * ((base (fields 2)).length + 1)]
(some ⟨some (labels .relationRead), (ambient, register), base⟩) =
some ⟨some (labels .reverseRead), (ambient, none), afterRelation⟩ at hfirst
have hsecond := MachineCopy.copyTrace (fields 1) row scratch
(hfields 1).1 (hfields 1).2 hrowScratch false
(labels .reverseRead) (labels .reverseRestore) (some (labels .tailRead))
program (hprogram .reverseRead) (hprogram .reverseRestore) afterRelation
(by simp [afterRelation, Ne.symm hrowScratch, hscratch]) ambient none
have hrev : afterRelation (fields 1) = base (fields 1) := by
simp [afterRelation, (hfields 1).1]
rw [hrev] at hsecond
change (MachineComposition.advance (TM2.step program))^[2 * ((base (fields 1)).length + 1)]
(some ⟨some (labels .reverseRead), (ambient, none), afterRelation⟩) =
some ⟨some (labels .tailRead), (ambient, none), afterReverse⟩ at hsecond
have hthird := MachineCopy.copyTrace (fields 0) row scratch
(hfields 0).1 (hfields 0).2 hrowScratch false
(labels .tailRead) (labels .tailRestore) (some (labels .appendOld))
program (hprogram .tailRead) (hprogram .tailRestore) afterReverse
(by simp [afterReverse, afterRelation, Ne.symm hrowScratch, hscratch]) ambient none
have htail : afterReverse (fields 0) = base (fields 0) := by
simp [afterReverse, afterRelation, (hfields 0).1]
rw [htail] at hthird
have hfinal : Function.update afterReverse row
(base (fields 0) ++ afterReverse row) = prefixTapes fields row base := by
simp [afterReverse, afterRelation, prefixTapes, fieldBits, List.append_assoc]
rw [hfinal] at hthird
rw [show 2 * (fieldSize fields base + 3) =
2 * ((base (fields 0)).length + 1) +
(2 * ((base (fields 1)).length + 1) + 2 * ((base (fields 2)).length + 1)) by
simp only [fieldSize]; omega,
Function.iterate_add_apply,
Function.iterate_add_apply (m := 2 * ((base (fields 1)).length + 1))
(n := 2 * ((base (fields 2)).length + 1)), hfirst, hsecond]
exact hthird
theorem appendTrace (fields : Fin 3 → K) (row output scratch : K)
(hfields : ∀ i, fields i ≠ row ∧ fields i ≠ scratch)
(hrowOutput : row ≠ output) (hrowScratch : row ≠ scratch)
(houtputScratch : output ≠ scratch) (labels : Label → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(hprogram : ∀ label, program (labels label) = routine fields row output scratch labels exit label)
(base : K → List Bool) (hrow : base row = []) (hscratch : base scratch = [])
(ambient : σ) (register : Option Bool) :
(MachineComposition.advance (TM2.step program))^[
4 * fieldSize fields base + 2 * (base output).length + 9]
(some ⟨some (labels .relationRead), (ambient, register), base⟩) =
some ⟨exit, (ambient, none),
Function.update base output (base output ++ fieldBits fields base)⟩ := by
have hprefix := prefixTrace fields row output scratch hfields hrowScratch
labels exit program hprogram base hscratch ambient register
have happend := MachineAppendAt.appendTrace row output scratch hrowOutput hrowScratch
houtputScratch false (labels .appendOld) (labels .appendRow) (labels .appendBack)
exit program (hprogram .appendOld) (hprogram .appendRow) (hprogram .appendBack)
(prefixTapes fields row base)
(by simp [prefixTapes, Ne.symm hrowScratch, hscratch]) ambient none
have hrowbits : prefixTapes fields row base row = fieldBits fields base := by
simp [prefixTapes, hrow]
have hout : prefixTapes fields row base output = base output := by
simp [prefixTapes, Ne.symm hrowOutput]
rw [hrowbits, hout, fieldBits_length] at happend
have hfinal : MachineAppendAt.appendTapes row output (prefixTapes fields row base) =
Function.update base output (base output ++ fieldBits fields base) := by
funext k
by_cases ho : k = output
· subst k
simp [MachineAppendAt.appendTapes, hrowbits, hout]
· by_cases hr : k = row
· subst k
simp [MachineAppendAt.appendTapes, Reduction.MachineTransfer.tapesAt,
prefixTapes, hrowOutput, hrow]
· simp [MachineAppendAt.appendTapes, Reduction.MachineTransfer.tapesAt,
prefixTapes, ho, hr]
rw [hfinal] at happend
rw [show 4 * fieldSize fields base + 2 * (base output).length + 9 =
(2 * (fieldSize fields base + (base output).length) + 3) +
2 * (fieldSize fields base + 3) by omega,
Function.iterate_add_apply, hprefix]
exact happend
/-- The full existing graph codec, including all 4096 unary predicate fields. -/
theorem rowBits_eq {n m : Nat} (r : DartRow n m) :
encodeWords (rowWords r) = encodeWord r.tail.val ++
encodeWord r.reverseIndex.val ++ encodeWords (relationWords r.relation) := by
simp [rowWords, encodeWords]
omit [DecidableEq K] in
theorem fieldBits_eq_rowBits {n m : Nat} (r : DartRow n m)
(fields : Fin 3 → K) (base : K → List Bool)
(htail : base (fields 0) = encodeWord r.tail.val)
(hreverse : base (fields 1) = encodeWord r.reverseIndex.val)
(hrelation : base (fields 2) = encodeWords (relationWords r.relation)) :
fieldBits fields base = encodeWords (rowWords r) := by
rw [fieldBits, htail, hreverse, hrelation, rowBits_eq]
noncomputable def timePolynomial : Polynomial Nat := Polynomial.C 4 * Polynomial.X + Polynomial.C 9
omit [DecidableEq K] in
theorem timePolynomial_bounds (fields : Fin 3 → K) (base : K → List Bool) (output : K) :
4 * fieldSize fields base + 2 * (base output).length + 9 ≤
timePolynomial.eval (fieldSize fields base + (base output).length) := by
simp only [timePolynomial, Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_C,
Polynomial.eval_X]
omega
/-- A composable actual execution certificate for a correctly encoded row.
The program is fixed; row data occur only in the input-tape equalities. -/
def rowAppendInTime {n m : Nat} (r : DartRow n m)
(fields : Fin 3 → K) (row output scratch : K)
(hfields : ∀ i, fields i ≠ row ∧ fields i ≠ scratch)
(hrowOutput : row ≠ output) (hrowScratch : row ≠ scratch)
(houtputScratch : output ≠ scratch) (labels : Label → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool))
(hprogram : ∀ label, program (labels label) = routine fields row output scratch labels exit label)
(base : K → List Bool) (hrow : base row = []) (hscratch : base scratch = [])
(htail : base (fields 0) = encodeWord r.tail.val)
(hreverse : base (fields 1) = encodeWord r.reverseIndex.val)
(hrelation : base (fields 2) = encodeWords (relationWords r.relation))
(ambient : σ) (register : Option Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some (labels .relationRead), (ambient, register), base⟩
(some ⟨exit, (ambient, none),
Function.update base output (base output ++ encodeWords (rowWords r))⟩)
(timePolynomial.eval (fieldSize fields base + (base output).length)) where
steps := 4 * fieldSize fields base + 2 * (base output).length + 9
evals_in_steps := by
change (MachineComposition.advance (TM2.step program))^[_] _ = _
rw [← fieldBits_eq_rowBits r fields base htail hreverse hrelation]
exact appendTrace fields row output scratch hfields hrowOutput hrowScratch
houtputScratch labels exit program hprogram base hrow hscratch ambient register
steps_le_m := timePolynomial_bounds fields base output
/-- A fixed nine-label six-stack finite machine implementing the routine.
Its inputs may be supplied in a caller's distributed tape configuration. -/
def machine : FinTM2 where
K := Fin 6
k₀ := 0
k₁ := 4
Γ _ := Bool
Λ := Label
main := .relationRead
σ := Unit × Option Bool
initialState := ((), none)
m := routine (fun i : Fin 3 => i.castLE (by decide)) 3 4 5 id none
/-- Direct execution of the concrete finite machine on caller-supplied unary
fields. This instantiates all program equations; no execution premise remains. -/
def machineRowInTime {n m : Nat} (r : DartRow n m) (base : Fin 6 → List Bool)
(hrow : base 3 = []) (hscratch : base 5 = [])
(htail : base 0 = encodeWord r.tail.val)
(hreverse : base 1 = encodeWord r.reverseIndex.val)
(hrelation : base 2 = encodeWords (relationWords r.relation)) (register : Option Bool) :
StateTransition.EvalsToInTime machine.step
⟨some .relationRead, ((), register), base⟩
(some ⟨none, ((), none),
Function.update base (4 : Fin 6) (base 4 ++ encodeWords (rowWords r))⟩)
(timePolynomial.eval ((encodeWords (rowWords r)).length + (base 4).length)) := by
let fields : Fin 3 → Fin 6 := fun i => i.castLE (by decide)
have hfields (i : Fin 3) : fields i ≠ 3 ∧ fields i ≠ 5 := by
constructor
· intro h
have he := congrArg Fin.val h
change i.val = 3 at he
omega
· intro h
have he := congrArg Fin.val h
change i.val = 5 at he
omega
have hsize : fieldSize fields base = (encodeWords (rowWords r)).length := by
rw [← fieldBits_length, fieldBits_eq_rowBits r fields base htail hreverse hrelation]
have run := rowAppendInTime r fields (3 : Fin 6) 4 5 hfields (by decide) (by decide)
(by decide) id none machine.m (fun _ => rfl) base hrow hscratch
htail hreverse hrelation () register
rw [hsize] at run
convert run using 1 ; rfl
theorem outputFrame (base : K → List Bool) (output k : K) (hk : k ≠ output)
(bits : List Bool) :
Function.update base output (base output ++ bits) k = base k := by
simp [hk]
end MaxCutGames.Foundations.Complexity.MachineTableRows
/-!
# Machine execution for an inherited regularization row
The machine reads the original reverse index from the physically stored input
table. The unread working copy then starts at the 4096 original predicate
fields; a fixed finite Boolean block copy retains exactly those fields. Unary
arithmetic computes the new reverse index, and the checked row emitter appends
the three physical fields. No input table or unbounded arithmetic function is
stored in the machine's finite state or instructions.
-/
namespace MaxCutGames.Foundations.Complexity.MachineRegularOriginalRow
open Turing MachineComposition
open PCP PCP.GraphTables PCP.PreprocessingRegularTables
def inheritedIndex (_H : BaseTable) (t : Table) (e : Fin t.darts) :
Fin (vertexCount t (padding t) * (internalDegree + 1)) :=
PortTables.rowIndex _ _
(vertexOrder t (padding t) (.inl e), portOrder internalDegree (.inr ()))
def inheritedRow (H : BaseTable) (t : Table) (e : Fin t.darts) :
DartRow (vertexCount t (padding t))
(vertexCount t (padding t) * (internalDegree + 1)) :=
(PortTables.flatRows (regularize H t))[inheritedIndex H t e]
theorem inheritedRow_eq (H : BaseTable) (t : Table) (e : Fin t.darts) :
inheritedRow H t e =
⟨((PortTables.rowIndex _ _).symm (inheritedIndex H t e)).1,
(regularize H t).reverseIndex[inheritedIndex H t e],
(regularize H t).relations[inheritedIndex H t e]⟩ := by
simp only [inheritedRow, PortTables.flatRows, Fin.getElem_fin,
Vector.getElem_ofFn, Fin.eta]
@[simp] theorem inheritedRow_tail (H : BaseTable) (t : Table) (e : Fin t.darts) :
(inheritedRow H t e).tail.val = e.val := by
simp only [inheritedRow_eq, inheritedIndex, Equiv.symm_apply_apply,
vertexOrder_original]
@[simp] theorem inheritedRow_reverse (H : BaseTable) (t : Table) (e : Fin t.darts) :
(inheritedRow H t e).reverseIndex.val =
(internalDegree + 1) * t.rows[e].reverseIndex.val + internalDegree := by
rw [inheritedRow_eq]
change ((regularize H t).reverseIndex[PortTables.rowIndex _ _
(vertexOrder t (padding t) (.inl e), portOrder internalDegree (.inr ()))]).val = _
rw [← PortTables.rowIndex_rotation, regularize, rotation_ofCloudTables]
change (PortTables.rowIndex _ _
(vertexOrder t (padding t) (.inl (reverseAt t.rows e)),
portOrder internalDegree (.inr ()))).val = _
rw [PortTables.rowIndex_val, vertexOrder_original, portOrder_inherited]
exact Nat.add_comm _ _
@[simp] theorem inheritedRow_relation (H : BaseTable) (t : Table) (e : Fin t.darts) :
(inheritedRow H t e).relation = t.rows[e].relation := by
rw [inheritedRow_eq]
apply Vector.ext
intro i hi
let j : Fin 4096 := ⟨i, hi⟩
let ab := relationIndex.symm j
have h := accepts_original t (padding t) (familyCloudTable H t) e ab.1 ab.2
change relationAt ((regularize H t).relations[inheritedIndex H t e]) ab.1 ab.2 =
relationAt t.rows[e].relation ab.1 ab.2 at h
have hj : relationIndex (ab.1, ab.2) = j := relationIndex.apply_symm_apply j
simpa only [relationAt, hj, Fin.getElem_fin, j] using h
theorem inheritedRow_words (H : BaseTable) (t : Table) (e : Fin t.darts) :
rowWords (inheritedRow H t e) = e.val ::
((internalDegree + 1) * t.rows[e].reverseIndex.val + internalDegree) ::
relationWords t.rows[e].relation := by
simp only [rowWords, inheritedRow_tail, inheritedRow_reverse, inheritedRow_relation,
List.cons_append, List.nil_append]
def prefixWords (t : Table) (e : Fin t.darts) : List Nat :=
[t.vertices, t.darts] ++ ((rowList t).take e.val).flatMap rowWords
def suffixWords (t : Table) (e : Fin t.darts) : List Nat :=
((rowList t).drop (e.val + 1)).flatMap rowWords
theorem prefixWords_length (t : Table) (e : Fin t.darts) :
(prefixWords t e).length = 2 + 4098 * e.val := by
simp only [prefixWords, List.length_append, List.length_cons, List.length_nil,
rowsWords_length, List.length_take, rowList_length, Nat.min_eq_left e.isLt.le]
theorem tableWords_at_row (t : Table) (e : Fin t.darts) :
GraphTables.tableWords t = prefixWords t e ++ (rowWords t.rows[e] ++ suffixWords t e) := by
have he : e.val < (rowList t).length := by simpa only [rowList_length] using e.isLt
have hs : (rowList t).take e.val ++ (rowList t)[e.val] ::
(rowList t).drop (e.val + 1) = rowList t := by
rw [List.getElem_cons_drop he, List.take_append_drop]
have hw := congrArg (fun rs : List (DartRow t.vertices t.darts) => rs.flatMap rowWords) hs
simp only [List.flatMap_append, List.flatMap_cons, rowList, Vector.getElem_toList] at hw
unfold GraphTables.tableWords prefixWords suffixWords rowList
rw [← hw]
simp only [List.append_assoc, Fin.getElem_fin]
theorem tableWords_drop_row (t : Table) (e : Fin t.darts) :
(GraphTables.tableWords t).drop (2 + 4098 * e.val) = rowWords t.rows[e] ++ suffixWords t e := by
rw [tableWords_at_row]
exact List.drop_left' (l₂ := rowWords t.rows[e] ++ suffixWords t e)
(prefixWords_length t e)
theorem selected_reverse (t : Table) (e : Fin t.darts) :
(GraphTables.tableWords t)[4098 * e.val + 3]? = some t.rows[e].reverseIndex.val := by
have h := congrArg (fun words : List Nat => words[1]?) (tableWords_drop_row t e)
simp only [List.getElem?_drop] at h
rw [show 2 + 4098 * e.val + 1 = 4098 * e.val + 3 by omega] at h
simpa [rowWords] using h
theorem remaining_relation (t : Table) (e : Fin t.darts) :
(GraphTables.tableWords t).drop (4098 * e.val + 3 + 1) =
relationWords t.rows[e].relation ++ suffixWords t e := by
rw [show 4098 * e.val + 3 + 1 = (2 + 4098 * e.val) + 2 by omega,
← List.drop_drop, tableWords_drop_row]
simp only [rowWords, List.cons_append, List.drop_succ_cons, List.drop_zero, List.nil_append]
abbrev Tape := Fin 10
abbrev Buffer := MachineFixedBlockMap.Buffer 4096
abbrev State (σ : Type) := (σ × Buffer) × Option Bool
abbrev Alphabet (_ : Tape) := Bool
def zeroBuffer : Buffer := MachineFixedBlockMap.emptyBuffer 4096
def lookupTape (i : Fin 5) : Tape := ⟨i.val + 1, by omega⟩
theorem lookupTape_injective : Function.Injective lookupTape := by
intro i j h
apply Fin.ext
have hv := congrArg Fin.val h
simp only [lookupTape] at hv
omega
theorem source_outside (i : Fin 5) : (0 : Tape) ≠ lookupTape i := by
intro h
have hv := congrArg Fin.val h
simp only [lookupTape] at hv
omega
def fields : Fin 3 → Tape := Fin.cases 0 (Fin.cases 6 (fun _ => 7))
@[simp] theorem fields_zero : fields 0 = 0 := rfl
@[simp] theorem fields_one : fields 1 = 6 := rfl
@[simp] theorem fields_two : fields 2 = 7 := rfl
theorem fields_separate (i : Fin 3) : fields i ≠ 8 ∧ fields i ≠ 5 := by
fin_cases i <;> decide
def copyStateEquiv (σ : Type) : ((σ × Option Bool) × Buffer) ≃ State σ where
toFun s := ((s.1.1, s.2), s.1.2)
invFun s := ((s.1.1, s.2), s.1.2)
left_inv _ := rfl
right_inv _ := rfl
inductive Label
| lookup (label : MachineAffineLookup.Label)
| copyRelation | reverseSeed | reverseScan | reverseRestore
| emit (label : MachineTableRows.Label)
deriving DecidableEq, Fintype
def relationCopyAt {K σ Λ : Type} (src dst : K) (exit : Option Λ) :
TM2.Stmt (fun _ : K => Bool) Λ (State σ) :=
MachineStateEquiv.statement (copyStateEquiv σ)
(PoweringMachineRow.encodedBlockAt src dst (id : Buffer → Buffer) exit)
def instruction {σ Λ : Type} (q : Nat) (labels : Label → Λ) (exit : Option Λ) :
Label → TM2.Stmt Alphabet Λ (State σ)
| .lookup l => MachineAffineLookup.instruction 0 lookupTape 4098 3
(fun l => labels (.lookup l)) (some (labels .copyRelation)) l
| .copyRelation => relationCopyAt 3 7 (some (labels .reverseSeed))
| .reverseSeed => MachineUnaryAffineAt.seed 6 q (labels .reverseScan)
| .reverseScan => MachineUnaryAffineAt.scan 4 5 6 (q + 1)
(labels .reverseScan) (labels .reverseRestore)
| .reverseRestore => Reduction.MachineTransfer.loopAt 5 4 id false
(labels .reverseRestore) (some (labels (.emit .relationRead)))
| .emit l => MachineTableRows.routine fields 8 9 5 (fun l => labels (.emit l)) exit l
def machine (q : Nat) : FinTM2 where
K := Tape
k₀ := 1
k₁ := 9
Γ := Alphabet
Λ := Label
main := .lookup .seed
σ := State Unit
initialState := (((), zeroBuffer), none)
m := instruction q id none
def looked (t : Table) (e : Fin t.darts) (base : Tape → List Bool) : Tape → List Bool :=
MachineAffineLookup.finalTapes lookupTape base (GraphTables.tableWords t) (4098 * e.val + 3)
t.rows[e].reverseIndex.val
def copied (t : Table) (e : Fin t.darts) (base : Tape → List Bool) : Tape → List Bool :=
Function.update (Function.update (looked t e base) 3
(encodeWords (suffixWords t e) ++ base 3)) 7
(encodeWords (relationWords t.rows[e].relation) ++ base 7)
def affined (q : Nat) (t : Table) (e : Fin t.darts) (base : Tape → List Bool) :
Tape → List Bool :=
Function.update (copied t e base) 6
(encodeWord ((q + 1) * t.rows[e].reverseIndex.val + q) ++ base 6)
theorem looked_other (t : Table) (e : Fin t.darts) (base : Tape → List Bool)
(k : Tape) (h₂ : k ≠ 2) (h₃ : k ≠ 3) (h₄ : k ≠ 4) :
looked t e base k = base k :=
MachineAffineLookup.finalTapes_other lookupTape base _ _ _ k h₂ h₃ h₄
theorem looked_reverse (t : Table) (e : Fin t.darts) (base : Tape → List Bool) :
looked t e base 4 = encodeWord t.rows[e].reverseIndex.val ++ base 4 :=
MachineAffineLookup.finalTapes_output lookupTape base _ _ _
theorem looked_relation (t : Table) (e : Fin t.darts) (base : Tape → List Bool) :
looked t e base 3 = encodeWords (relationWords t.rows[e].relation) ++
(encodeWords (suffixWords t e) ++ base 3) := by
change MachineLookup.tapes (2 : Tape) 3 4 base
(encodeWord 0 ++ base 2)
(encodeWords ((GraphTables.tableWords t).drop (4098 * e.val + 3 + 1)) ++ base 3)
(encodeWord t.rows[e].reverseIndex.val ++ base 4) 3 = _
rw [MachineLookup.tapes_source _ _ _ (by decide), remaining_relation, encodeWords_append]
exact List.append_assoc _ _ _
theorem vectorBits_eq_encodedBlock {n : Nat} (relation : Vector Bool n) :
PoweringMachineRow.encodeBits (List.ofFn (fun i : Fin n => relation[i])) =
encodeWords (relation.toList.map GraphTables.bitWord) := by
rw [PoweringMachineRow.encodeBits_graphWords]
congr 1
congr 1
have hvec : Vector.ofFn (fun i : Fin n => relation[i]) = relation := by
apply Vector.ext
intro i hi
simp only [Vector.getElem_ofFn, Fin.getElem_fin]
exact Vector.toList_ofFn.symm.trans (congrArg Vector.toList hvec)
theorem relationBits_eq_encodedBlock (relation : RelationTable) :
PoweringMachineRow.encodeBits (List.ofFn (fun i : Fin 4096 => relation[i])) =
encodeWords (relationWords relation) := vectorBits_eq_encodedBlock relation
/-- Reusable identity copy of exactly the physically read 4096 unary Boolean
fields. The fixed register is reset; every other state component is retained. -/
theorem relationCopy_step {K Λ σ : Type} [DecidableEq K]
(src dst : K) (hne : src ≠ dst) (label : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (State σ))
(code : program label = relationCopyAt src dst exit)
(relation : RelationTable) (suffix : List Bool) (base : K → List Bool)
(input : base src = encodeWords (relationWords relation) ++ suffix)
(ambient : σ) (buffer : Buffer) (register : Option Bool) :
TM2.step program ⟨some label, ((ambient, buffer), register), base⟩ =
some ⟨exit, ((ambient, zeroBuffer), register),
Function.update (Function.update base src suffix) dst
(encodeWords (relationWords relation) ++ base dst)⟩ := by
change some (TM2.stepAux (program label) _ base) = _
rw [code]
unfold relationCopyAt
rw [MachineStateEquiv.stepAux_transport_symm]
change some (MachineStateEquiv.configuration (copyStateEquiv σ)
(TM2.stepAux (PoweringMachineRow.encodedBlockAt src dst (id : Buffer → Buffer) exit)
((ambient, register), buffer) base)) = _
have hinput : base src = PoweringMachineRow.encodeBits
(List.ofFn (fun i : Fin 4096 => relation[i])) ++ suffix := by
rw [relationBits_eq_encodedBlock]
exact input
rw [PoweringMachineRow.stepAux_encodedBlockAt src dst (id : Buffer → Buffer)
exit hne (fun i : Fin 4096 => relation[i]) suffix
((ambient, register), buffer) base hinput]
simp only [id_eq, relationBits_eq_encodedBlock, MachineStateEquiv.configuration,
copyStateEquiv, zeroBuffer]
rfl
theorem relationCopy_pushBound {K Λ σ : Type} (src dst : K) (exit : Option Λ) :
Runtime.statementPushBound (relationCopyAt (σ := σ) src dst exit) = 8192 := by
rw [relationCopyAt, MachineStateEquiv.statementPushBound,
PoweringMachineRow.statementPushBound_encodedBlockAt]
theorem copied_other (t : Table) (e : Fin t.darts) (base : Tape → List Bool)
(k : Tape) (h₂ : k ≠ 2) (h₃ : k ≠ 3) (h₄ : k ≠ 4) (h₇ : k ≠ 7) :
copied t e base k = base k := by
simp only [copied, Function.update_of_ne h₇, Function.update_of_ne h₃,
looked_other t e base k h₂ h₃ h₄]
theorem copied_reverse (t : Table) (e : Fin t.darts) (base : Tape → List Bool) :
copied t e base 4 = encodeWord t.rows[e].reverseIndex.val ++ base 4 := by
simp only [copied, Function.update_of_ne (by decide : (4 : Tape) ≠ 7),
Function.update_of_ne (by decide : (4 : Tape) ≠ 3), looked_reverse]
theorem copied_relation (t : Table) (e : Fin t.darts) (base : Tape → List Bool) :
copied t e base 7 = encodeWords (relationWords t.rows[e].relation) ++ base 7 := by
simp only [copied, Function.update_self]
theorem affined_other (q : Nat) (t : Table) (e : Fin t.darts) (base : Tape → List Bool)
(k : Tape) (h₂ : k ≠ 2) (h₃ : k ≠ 3) (h₄ : k ≠ 4) (h₆ : k ≠ 6) (h₇ : k ≠ 7) :
affined q t e base k = base k := by
simp only [affined, Function.update_of_ne h₆, copied_other t e base k h₂ h₃ h₄ h₇]
theorem affined_reverse (q : Nat) (t : Table) (e : Fin t.darts) (base : Tape → List Bool) :
affined q t e base 6 = encodeWord ((q + 1) * t.rows[e].reverseIndex.val + q) ++ base 6 := by
simp only [affined, Function.update_self]
theorem affined_relation (q : Nat) (t : Table) (e : Fin t.darts) (base : Tape → List Bool) :
affined q t e base 7 = encodeWords (relationWords t.rows[e].relation) ++ base 7 := by
simp only [affined, Function.update_of_ne (by decide : (7 : Tape) ≠ 6), copied_relation]
/-- The input graph and dart index occur only as physically stored tape data. -/
structure Input (t : Table) (e : Fin t.darts) (base : Tape → List Bool) : Prop where
indexWord : base 0 = encodeWord e.val
tableWord : base 1 = tableBits t
reverseEmpty : base 4 = []
scratchEmpty : base 5 = []
computedEmpty : base 6 = []
relationEmpty : base 7 = []
rowEmpty : base 8 = []
def emittedWords (q : Nat) (t : Table) (e : Fin t.darts) : List Nat :=
e.val :: ((q + 1) * t.rows[e].reverseIndex.val + q) :: relationWords t.rows[e].relation
theorem affined_fields (q : Nat) (t : Table) (e : Fin t.darts)
(base : Tape → List Bool) (input : Input t e base) :
MachineTableRows.fieldBits fields (affined q t e base) = encodeWords (emittedWords q t e) := by
have htail := affined_other q t e base 0 (by decide) (by decide) (by decide)
(by decide) (by decide)
simp only [MachineTableRows.fieldBits, fields_zero, fields_one, fields_two,
htail, input.indexWord, affined_reverse, input.computedEmpty,
affined_relation, input.relationEmpty, List.append_nil,
emittedWords, encodeWords, List.append_assoc]
def rowSteps (q : Nat) (t : Table) (e : Fin t.darts) (base : Tape → List Bool) : Nat :=
4 * (encodeWords (emittedWords q t e)).length + 2 * (base 9).length + 9
def steps (q : Nat) (t : Table) (e : Fin t.darts) (base : Tape → List Bool) : Nat :=
MachineAffineLookup.steps (GraphTables.tableWords t) e.val 4098 3 + 1 +
(2 * (t.rows[e].reverseIndex.val + 1) + 1) + rowSteps q t e base
def finalTapes (q : Nat) (t : Table) (e : Fin t.darts) (base : Tape → List Bool) :
Tape → List Bool :=
Function.update (affined q t e base) 9 (base 9 ++ encodeWords (emittedWords q t e))
section Execution
variable {Λ σ : Type}
private theorem join_trace_inline_MachineRegularOriginalRow {A : Type*} {f : A → A} {m n : Nat} {a b c : A}
(first : f^[m] a = b) (second : f^[n] b = c) : f^[m + n] a = c := by
rw [Nat.add_comm m n, Function.iterate_add_apply, first, second]
theorem lookupTrace (q : Nat) (labels : Label → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt Alphabet Λ (State σ))
(code : ∀ l, program (labels l) = instruction q labels exit l)
(t : Table) (e : Fin t.darts) (base : Tape → List Bool) (input : Input t e base)
(ambient : σ) (register : Option Bool) :
(advance (TM2.step program))^[MachineAffineLookup.steps (GraphTables.tableWords t) e.val 4098 3]
(some ⟨some (labels (.lookup .seed)), ((ambient, zeroBuffer), register), base⟩) =
some ⟨some (labels .copyRelation), ((ambient, zeroBuffer), none), looked t e base⟩ := by
exact MachineAffineLookup.affineLookupTrace 0 lookupTape lookupTape_injective source_outside
4098 3 (fun l => labels (.lookup l)) (some (labels .copyRelation)) program
(fun l => code (.lookup l)) base (GraphTables.tableWords t) input.tableWord input.scratchEmpty
e.val [] (by simpa only [List.append_nil] using input.indexWord)
t.rows[e].reverseIndex.val (selected_reverse t e) (ambient, zeroBuffer) register
theorem copyTrace (q : Nat) (labels : Label → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt Alphabet Λ (State σ))
(code : ∀ l, program (labels l) = instruction q labels exit l)
(t : Table) (e : Fin t.darts) (base : Tape → List Bool) (ambient : σ) :
(advance (TM2.step program))^[1]
(some ⟨some (labels .copyRelation), ((ambient, zeroBuffer), none), looked t e base⟩) =
some ⟨some (labels .reverseSeed), ((ambient, zeroBuffer), none), copied t e base⟩ := by
simp only [Function.iterate_one, advance_some]
have run := relationCopy_step (3 : Tape) 7 (by decide) (labels .copyRelation)
(some (labels .reverseSeed)) program (code .copyRelation) t.rows[e].relation
(encodeWords (suffixWords t e) ++ base 3) (looked t e base) (looked_relation t e base)
ambient zeroBuffer none
rw [looked_other t e base 7 (by decide) (by decide) (by decide)] at run
exact run
theorem affineTrace (q : Nat) (labels : Label → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt Alphabet Λ (State σ))
(code : ∀ l, program (labels l) = instruction q labels exit l)
(t : Table) (e : Fin t.darts) (base : Tape → List Bool) (input : Input t e base)
(ambient : σ) :
(advance (TM2.step program))^[2 * (t.rows[e].reverseIndex.val + 1) + 1]
(some ⟨some (labels .reverseSeed), ((ambient, zeroBuffer), none), copied t e base⟩) =
some ⟨some (labels (.emit .relationRead)), ((ambient, zeroBuffer), none),
affined q t e base⟩ := by
have hsource : copied t e base 4 = encodeWord t.rows[e].reverseIndex.val ++ [] := by
rw [copied_reverse, input.reverseEmpty]
have hscratch : copied t e base 5 = [] := by
rw [copied_other t e base 5 (by decide) (by decide) (by decide) (by decide), input.scratchEmpty]
have run := MachineUnaryAffineAt.seededAffineTrace (4 : Tape) 5 6 (by decide) (by decide)
(by decide) (q + 1) q (labels .reverseSeed) (labels .reverseScan) (labels .reverseRestore)
(some (labels (.emit .relationRead))) program (code .reverseSeed) (code .reverseScan)
(code .reverseRestore) (copied t e base) t.rows[e].reverseIndex.val [] hsource hscratch
(ambient, zeroBuffer) none
rw [copied_other t e base 6 (by decide) (by decide) (by decide) (by decide)] at run
exact run
theorem emitTrace (q : Nat) (labels : Label → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt Alphabet Λ (State σ))
(code : ∀ l, program (labels l) = instruction q labels exit l)
(t : Table) (e : Fin t.darts) (base : Tape → List Bool) (input : Input t e base)
(ambient : σ) :
(advance (TM2.step program))^[rowSteps q t e base]
(some ⟨some (labels (.emit .relationRead)), ((ambient, zeroBuffer), none), affined q t e base⟩) =
some ⟨exit, ((ambient, zeroBuffer), none), finalTapes q t e base⟩ := by
have hrow : affined q t e base 8 = [] := by
rw [affined_other q t e base 8 (by decide) (by decide) (by decide) (by decide)
(by decide), input.rowEmpty]
have hscratch : affined q t e base 5 = [] := by
rw [affined_other q t e base 5 (by decide) (by decide) (by decide) (by decide)
(by decide), input.scratchEmpty]
have hout := affined_other q t e base 9 (by decide) (by decide) (by decide)
(by decide) (by decide)
have hfields := affined_fields q t e base input
have hsize : MachineTableRows.fieldSize fields (affined q t e base) =
(encodeWords (emittedWords q t e)).length := by
rw [← MachineTableRows.fieldBits_length, hfields]
have run := MachineTableRows.appendTrace fields (8 : Tape) 9 5 fields_separate
(by decide) (by decide) (by decide) (fun l => labels (.emit l)) exit program
(fun l => code (.emit l)) (affined q t e base) hrow hscratch (ambient, zeroBuffer) none
rw [hsize, hout, hfields] at run
exact run
/-- All four phases are derived from their concrete instructions. -/
theorem traceAt (q : Nat) (labels : Label → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt Alphabet Λ (State σ))
(code : ∀ l, program (labels l) = instruction q labels exit l)
(t : Table) (e : Fin t.darts) (base : Tape → List Bool) (input : Input t e base)
(ambient : σ) (register : Option Bool) :
(advance (TM2.step program))^[steps q t e base]
(some ⟨some (labels (.lookup .seed)), ((ambient, zeroBuffer), register), base⟩) =
some ⟨exit, ((ambient, zeroBuffer), none), finalTapes q t e base⟩ := by
have hlookup := lookupTrace q labels exit program code t e base input ambient register
have hcopy := copyTrace q labels exit program code t e base ambient
have haffine := affineTrace q labels exit program code t e base input ambient
have hemit := emitTrace q labels exit program code t e base input ambient
exact join_trace_inline_MachineRegularOriginalRow (join_trace_inline_MachineRegularOriginalRow (join_trace_inline_MachineRegularOriginalRow hlookup hcopy) haffine) hemit
end Execution
def timeBound (q inputLength outputLength : Nat) : Nat :=
(4 * q + 17) * inputLength + 2 * outputLength + 4 * q + 32795
theorem steps_le (q : Nat) (t : Table) (e : Fin t.darts) (base : Tape → List Bool) :
steps q t e base ≤ timeBound q (tableBits t).length (base 9).length := by
have hl := MachineAffineLookup.steps_le (GraphTables.tableWords t) e.val 4098 3
t.rows[e].reverseIndex.val (selected_reverse t e)
have htable := tableWords_length_le_bits t
have he : e.val ≤ (tableBits t).length := by have he := e.isLt; omega
have hr : t.rows[e].reverseIndex.val ≤ (tableBits t).length := by
have hrlt := t.rows[e].reverseIndex.isLt
omega
have hrel := relationBits_length_le t.rows[e].relation
have hmul := Nat.mul_le_mul_left (4 * (q + 1) + 2) hr
change MachineAffineLookup.steps (GraphTables.tableWords t) e.val 4098 3 ≤
2 * e.val + 5 * (tableBits t).length + 6 at hl
unfold steps rowSteps timeBound emittedWords
simp only [encodeWords, List.length_append, encodeWord_length]
nlinarith
/-- Concrete finite-machine execution for the actual regularized input row. -/
def machineInTime (H : BaseTable) (t : Table) (e : Fin t.darts)
(base : Tape → List Bool) (input : Input t e base) (register : Option Bool) :
StateTransition.EvalsToInTime (machine internalDegree).step
⟨some (.lookup .seed), (((), zeroBuffer), register), base⟩
(some ⟨none, (((), zeroBuffer), none),
Function.update (affined internalDegree t e base) (9 : Tape)
(base (9 : Tape) ++ encodeWords (rowWords (inheritedRow H t e)))⟩)
(timeBound internalDegree (tableBits t).length (base (9 : Tape)).length) where
steps := steps internalDegree t e base
evals_in_steps := by
change (advance (TM2.step (instruction internalDegree id none)))^[_] _ = _
rw [inheritedRow_words]
exact traceAt internalDegree id none (instruction internalDegree id none)
(fun _ => rfl) t e base input () register
steps_le_m := steps_le internalDegree t e base
end MaxCutGames.Foundations.Complexity.MachineRegularOriginalRow
end OAI
import Mathlib
import OAI.Computability.MaxCut.Machines.Runtime2
import OAI.Computability.MaxCut.Machines.MachineCloudPrefix
namespace OAI
namespace MaxCutGames.Foundations.Complexity.MachineRegularTable
open Turing MachineComposition
/-! The outer construction uses independent finite registers for its existing
subprograms. This transport combines tape placement, an untouched register
frame, and a static register equivalence, without adding any machine steps. -/
namespace Lift
open MachineCloudPadding
variable {K K' Λ Λ' σ τ υ : Type}
def statement (tape : K → K') (labels : Λ → Λ') (exit : Option Λ')
(registers : (σ × τ) ≃ υ) (q : TM2.Stmt (fun _ : K => Bool) Λ σ) :
TM2.Stmt (fun _ : K' => Bool) Λ' υ :=
MachineStateEquiv.statement registers
(MachineStateFrame.frameStatement (Placement.statement tape labels exit q))
def configuration (view : K' → Option K) (labels : Λ → Λ') (exit : Option Λ')
(registers : (σ × τ) ≃ υ) (ambient : τ) (extra : K' → List Bool)
(c : TM2.Cfg (fun _ : K => Bool) Λ σ) : TM2.Cfg (fun _ : K' => Bool) Λ' υ :=
⟨Placement.label labels exit c.l, registers (c.var, ambient),
Placement.tapes view c.stk extra⟩
variable [DecidableEq K] [DecidableEq K']
theorem stepAux (tape : K → K') (view : K' → Option K)
(left : ∀ k, view (tape k) = some k)
(right : ∀ j k, view j = some k → tape k = j)
(labels : Λ → Λ') (exit : Option Λ') (registers : (σ × τ) ≃ υ)
(ambient : τ) (extra : K' → List Bool)
(q : TM2.Stmt (fun _ : K => Bool) Λ σ) (state : σ) (source : K → List Bool) :
TM2.stepAux (statement tape labels exit registers q) (registers (state, ambient))
(Placement.tapes view source extra) =
configuration view labels exit registers ambient extra (TM2.stepAux q state source) := by
rw [statement, MachineStateEquiv.stepAux_transport, MachineStateFrame.frame_stepAux,
Placement.stepAux_simulation tape view left right]
rfl
theorem step (tape : K → K') (view : K' → Option K)
(left : ∀ k, view (tape k) = some k)
(right : ∀ j k, view j = some k → tape k = j)
(labels : Λ → Λ') (exit : Option Λ') (registers : (σ × τ) ≃ υ)
(ambient : τ) (extra : K' → List Bool)
(source : Λ → TM2.Stmt (fun _ : K => Bool) Λ σ)
(target : Λ' → TM2.Stmt (fun _ : K' => Bool) Λ' υ)
(code : ∀ l, target (labels l) = statement tape labels exit registers (source l))
(a b : TM2.Cfg (fun _ : K => Bool) Λ σ) (h : TM2.step source a = some b) :
TM2.step target (configuration view labels exit registers ambient extra a) =
some (configuration view labels exit registers ambient extra b) := by
cases a with
| mk label state sourceTapes =>
cases label with
| none => simp [TM2.step] at h
| some label =>
have hb : TM2.stepAux (source label) state sourceTapes = b := Option.some.inj h
rw [← hb]
change some (TM2.stepAux (target (labels label)) (registers (state, ambient))
(Placement.tapes view sourceTapes extra)) = _
rw [code, stepAux tape view left right]
theorem trace (tape : K → K') (view : K' → Option K)
(left : ∀ k, view (tape k) = some k)
(right : ∀ j k, view j = some k → tape k = j)
(labels : Λ → Λ') (exit : Option Λ') (registers : (σ × τ) ≃ υ)
(ambient : τ) (extra : K' → List Bool)
(source : Λ → TM2.Stmt (fun _ : K => Bool) Λ σ)
(target : Λ' → TM2.Stmt (fun _ : K' => Bool) Λ' υ)
(code : ∀ l, target (labels l) = statement tape labels exit registers (source l))
(steps : Nat) (a b : TM2.Cfg (fun _ : K => Bool) Λ σ)
(run : (advance (TM2.step source))^[steps] (some a) = some b) :
(advance (TM2.step target))^[steps]
(some (configuration view labels exit registers ambient extra a)) =
some (configuration view labels exit registers ambient extra b) :=
liftSuccessfulTrace _ _ _
(step tape view left right labels exit registers ambient extra source target code) steps a b run
end Lift
end MaxCutGames.Foundations.Complexity.MachineRegularTable
namespace MaxCutGames.Foundations.Complexity.MachineCloudRank
open Turing MachineComposition MachineCloudCount
variable {σ : Type}
@[simp] theorem update_memory_spare (a b c d e f x : List Bool) :
Function.update (memory a b c d e f) .spare x = memory a b c d e x := by
funext k
cases k <;> rfl
inductive RankLabel
Xet Storage Details
- Size:
- 60.8 kB
- Xet hash:
- a30cdf0affe3ab786cf10bbd426a9a213d9af22626c6b33b56f7363148eb4251
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.