cfahlgren1/math / lean /OAI /Computability /UniqueGames /Machines /MachineRegularOriginalClean.lean
cfahlgren1's picture
download
raw
12.6 kB
import OAI.Computability.UniqueGames.Machines.MachineDrain
import OAI.Computability.UniqueGames.Machines.MachineRegularOriginalRow
namespace OAI
/-!
# Reusable inherited-row execution with actual cleanup
Five concrete drain loops remove the query delimiter, unread scan suffix,
raw reverse field, computed reverse field, and copied relation. With initially
empty work tapes, the only final tape change is the appended output row. The
index on tape zero and input table on tape one are both preserved.
-/
namespace UniqueGamesTheorem.Foundations.Complexity.MachineRegularOriginalClean
open Turing MachineComposition PCP PCP.GraphTables PCP.PreprocessingRegularTables
abbrev Tape := MachineRegularOriginalRow.Tape
abbrev Alphabet := MachineRegularOriginalRow.Alphabet
abbrev State := MachineRegularOriginalRow.State
inductive Label
| row (label : MachineRegularOriginalRow.Label)
| query | scan | rawReverse | computedReverse | relation
deriving DecidableEq, Fintype
def instruction {σ Λ : Type} (q : Nat) (labels : Label → Λ) (exit : Option Λ) :
Label → TM2.Stmt Alphabet Λ (State σ)
| .row l => MachineRegularOriginalRow.instruction q (fun l => labels (.row l))
(some (labels .query)) l
| .query => MachineDrain.drain 2 (labels .query) (some (labels .scan))
| .scan => MachineDrain.drain 3 (labels .scan) (some (labels .rawReverse))
| .rawReverse => MachineDrain.drain 4 (labels .rawReverse) (some (labels .computedReverse))
| .computedReverse => MachineDrain.drain 6 (labels .computedReverse) (some (labels .relation))
| .relation => MachineDrain.drain 7 (labels .relation) exit
def cleanupTapes (base : Tape → List Bool) : Tape → List Bool :=
Function.update (Function.update (Function.update (Function.update
(Function.update base 2 []) 3 []) 4 []) 6 []) 7 []
def cleanupSteps (base : Tape → List Bool) : Nat :=
((base 2).length + 1) + ((base 3).length + 1) + ((base 4).length + 1) +
((base 6).length + 1) + ((base 7).length + 1)
private theorem join_trace {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 cleanupTraceAt {σ Λ : Type} (q : Nat) (labels : Label → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt Alphabet Λ (State σ))
(code : ∀ l, program (labels l) = instruction q labels exit l)
(base : Tape → List Bool) (state : State σ) :
(advance (TM2.step program))^[cleanupSteps base]
(some ⟨some (labels .query), state, base⟩) =
some ⟨exit, (state.1, none), cleanupTapes base⟩ := by
let t1 := Function.update base (2 : Tape) []
let t2 := Function.update t1 (3 : Tape) []
let t3 := Function.update t2 (4 : Tape) []
let t4 := Function.update t3 (6 : Tape) []
have h0 := MachineDrain.drainTrace (2 : Tape) (labels .query) (some (labels .scan))
program (code .query) base (base 2) state.1 state.2
have h1 := MachineDrain.drainTrace (3 : Tape) (labels .scan) (some (labels .rawReverse))
program (code .scan) t1 (t1 3) state.1 none
have h2 := MachineDrain.drainTrace (4 : Tape) (labels .rawReverse) (some (labels .computedReverse))
program (code .rawReverse) t2 (t2 4) state.1 none
have h3 := MachineDrain.drainTrace (6 : Tape) (labels .computedReverse) (some (labels .relation))
program (code .computedReverse) t3 (t3 6) state.1 none
have h4 := MachineDrain.drainTrace (7 : Tape) (labels .relation) exit
program (code .relation) t4 (t4 7) state.1 none
simp only [Function.update_eq_self] at h0 h1 h2 h3 h4
simp only [t1, t2, t3, t4, Function.update_of_ne (by decide : (3 : Tape) ≠ 2),
Function.update_of_ne (by decide : (4 : Tape) ≠ 3),
Function.update_of_ne (by decide : (4 : Tape) ≠ 2),
Function.update_of_ne (by decide : (6 : Tape) ≠ 4),
Function.update_of_ne (by decide : (6 : Tape) ≠ 3),
Function.update_of_ne (by decide : (6 : Tape) ≠ 2),
Function.update_of_ne (by decide : (7 : Tape) ≠ 6),
Function.update_of_ne (by decide : (7 : Tape) ≠ 4),
Function.update_of_ne (by decide : (7 : Tape) ≠ 3),
Function.update_of_ne (by decide : (7 : Tape) ≠ 2)] at h1 h2 h3 h4
exact join_trace (join_trace (join_trace (join_trace h0 h1) h2) h3) h4
structure Input (t : Table) (e : Fin t.darts) (base : Tape → List Bool) : Prop
extends MachineRegularOriginalRow.Input t e base where
queryEmpty : base 2 = []
scanEmpty : base 3 = []
theorem cleanup_frame (q : Nat) (t : Table) (e : Fin t.darts)
(base : Tape → List Bool) (input : Input t e base) :
cleanupTapes (MachineRegularOriginalRow.finalTapes q t e base) =
Function.update base 9
(base 9 ++ encodeWords (MachineRegularOriginalRow.emittedWords q t e)) := by
funext k
fin_cases k <;>
simp [cleanupTapes, MachineRegularOriginalRow.finalTapes,
MachineRegularOriginalRow.affined, MachineRegularOriginalRow.copied,
MachineRegularOriginalRow.looked, MachineAffineLookup.finalTapes,
MachinePreservingLookup.finalTapes, MachineLookup.tapes,
MachineRegularOriginalRow.lookupTape, input.queryEmpty, input.scanEmpty,
input.reverseEmpty, input.computedEmpty, input.relationEmpty]
theorem suffix_length_le (t : Table) (e : Fin t.darts) :
(encodeWords (MachineRegularOriginalRow.suffixWords t e)).length ≤ (tableBits t).length := by
have hlength : (tableBits t).length =
(encodeWords (MachineRegularOriginalRow.prefixWords t e)).length +
((encodeWords (rowWords t.rows[e])).length +
(encodeWords (MachineRegularOriginalRow.suffixWords t e)).length) := by
rw [tableBits, MachineRegularOriginalRow.tableWords_at_row,
encodeWords_append, encodeWords_append, List.length_append, List.length_append]
omega
theorem cleanupSteps_eq (q : Nat) (t : Table) (e : Fin t.darts)
(base : Tape → List Bool) (input : Input t e base) :
cleanupSteps (MachineRegularOriginalRow.finalTapes q t e base) =
(encodeWords (MachineRegularOriginalRow.suffixWords t e)).length +
(q + 2) * t.rows[e].reverseIndex.val + q +
(encodeWords (relationWords t.rows[e].relation)).length + 8 := by
have hquery : MachineRegularOriginalRow.finalTapes q t e base 2 = encodeWord 0 := by
simp only [MachineRegularOriginalRow.finalTapes,
Function.update_of_ne (by decide : (2 : Tape) ≠ 9), MachineRegularOriginalRow.affined,
Function.update_of_ne (by decide : (2 : Tape) ≠ 6), MachineRegularOriginalRow.copied,
Function.update_of_ne (by decide : (2 : Tape) ≠ 7),
Function.update_of_ne (by decide : (2 : Tape) ≠ 3)]
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) 2 = _
rw [MachineLookup.tapes_index _ _ _ (by decide) (by decide), input.queryEmpty,
List.append_nil]
have hscan : MachineRegularOriginalRow.finalTapes q t e base 3 =
encodeWords (MachineRegularOriginalRow.suffixWords t e) := by
simp only [MachineRegularOriginalRow.finalTapes,
Function.update_of_ne (by decide : (3 : Tape) ≠ 9), MachineRegularOriginalRow.affined,
Function.update_of_ne (by decide : (3 : Tape) ≠ 6), MachineRegularOriginalRow.copied,
Function.update_of_ne (by decide : (3 : Tape) ≠ 7), Function.update_self,
input.scanEmpty, List.append_nil]
have hraw : MachineRegularOriginalRow.finalTapes q t e base 4 =
encodeWord t.rows[e].reverseIndex.val := by
simp only [MachineRegularOriginalRow.finalTapes,
Function.update_of_ne (by decide : (4 : Tape) ≠ 9), MachineRegularOriginalRow.affined,
Function.update_of_ne (by decide : (4 : Tape) ≠ 6), MachineRegularOriginalRow.copied_reverse,
input.reverseEmpty, List.append_nil]
have hcomputed : MachineRegularOriginalRow.finalTapes q t e base 6 =
encodeWord ((q + 1) * t.rows[e].reverseIndex.val + q) := by
simp only [MachineRegularOriginalRow.finalTapes,
Function.update_of_ne (by decide : (6 : Tape) ≠ 9), MachineRegularOriginalRow.affined_reverse,
input.computedEmpty, List.append_nil]
have hrelation : MachineRegularOriginalRow.finalTapes q t e base 7 =
encodeWords (relationWords t.rows[e].relation) := by
simp only [MachineRegularOriginalRow.finalTapes,
Function.update_of_ne (by decide : (7 : Tape) ≠ 9), MachineRegularOriginalRow.affined_relation,
input.relationEmpty, List.append_nil]
simp only [cleanupSteps, hquery, hscan, hraw, hcomputed, hrelation, encodeWord_length]
ring
theorem cleanupSteps_le (q : Nat) (t : Table) (e : Fin t.darts)
(base : Tape → List Bool) (input : Input t e base) :
cleanupSteps (MachineRegularOriginalRow.finalTapes q t e base) ≤
(q + 3) * (tableBits t).length + q + 8200 := by
rw [cleanupSteps_eq q t e base input]
have hsuffix := suffix_length_le t e
have htable := tableWords_length_le_bits t
have hr : t.rows[e].reverseIndex.val ≤ (tableBits t).length := by
have hrlt := t.rows[e].reverseIndex.isLt
omega
have hmul := Nat.mul_le_mul_left (q + 2) hr
have hrel := relationBits_length_le t.rows[e].relation
nlinarith
def steps (q : Nat) (t : Table) (e : Fin t.darts) (base : Tape → List Bool) : Nat :=
MachineRegularOriginalRow.steps q t e base +
cleanupSteps (MachineRegularOriginalRow.finalTapes q t e base)
def timeBound (q inputLength outputLength : Nat) : Nat :=
(5 * q + 20) * inputLength + 2 * outputLength + 5 * q + 40995
theorem steps_le (q : Nat) (t : Table) (e : Fin t.darts)
(base : Tape → List Bool) (input : Input t e base) :
steps q t e base ≤ timeBound q (tableBits t).length (base 9).length := by
have hrow := MachineRegularOriginalRow.steps_le q t e base
have hclean := cleanupSteps_le q t e base input
unfold steps timeBound MachineRegularOriginalRow.timeBound at *
nlinarith
theorem traceAt {σ Λ : Type} (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 (.row (.lookup .seed))),
((ambient, MachineRegularOriginalRow.zeroBuffer), register), base⟩) =
some ⟨exit, ((ambient, MachineRegularOriginalRow.zeroBuffer), none),
Function.update base 9
(base 9 ++ encodeWords (MachineRegularOriginalRow.emittedWords q t e))⟩ := by
have row := MachineRegularOriginalRow.traceAt q (fun l => labels (.row l))
(some (labels .query)) program (fun l => code (.row l)) t e base input.toInput ambient register
have clean := cleanupTraceAt q labels exit program code
(MachineRegularOriginalRow.finalTapes q t e base)
((ambient, MachineRegularOriginalRow.zeroBuffer), none)
rw [cleanup_frame q t e base input] at clean
exact join_trace row clean
def machine (q : Nat) : FinTM2 where
K := Tape
k₀ := 1
k₁ := 9
Γ := Alphabet
Λ := Label
main := .row (.lookup .seed)
σ := State Unit
initialState := (((), MachineRegularOriginalRow.zeroBuffer), none)
m := instruction q id none
/-- Exact inherited-row emission with all working tapes restored to empty. -/
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 (.row (.lookup .seed)), (((), MachineRegularOriginalRow.zeroBuffer), register), base⟩
(some ⟨none, (((), MachineRegularOriginalRow.zeroBuffer), none),
Function.update base (9 : Tape)
(base (9 : Tape) ++ encodeWords (rowWords (MachineRegularOriginalRow.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 [MachineRegularOriginalRow.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 input
end UniqueGamesTheorem.Foundations.Complexity.MachineRegularOriginalClean
end OAI

Xet Storage Details

Size:
12.6 kB
·
Xet hash:
b42d95f6b20d4b8bc2fc462223a366422e41b951b4e1539565e669e5e8a824ef

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.