import Mathlib.Tactic.FinCases import OAI.Computability.BinPacking.PCP.RawInitialMachineLoopData namespace OAI namespace BinPackingGames.Foundations.PCP.RawInitialMachineReadClause open Turing Complexity Hastad RawInitialMachineModel def fieldValue {n : ℕ} (c : Target.Clause n) (j : Fin 6) : ℕ := (Complexity.clauseWords c)[j.val]'(by rw [Complexity.clauseWords_length] exact j.isLt) def recordTapes {n : ℕ} (base : Tape → List Bool) (c : Target.Clause n) (suffix : List Bool) : Tape → List Bool | .input => suffix | .field j => encodeWord (fieldValue c j) | k => base k @[simp] theorem recordTapes_input {n : ℕ} (base : Tape → List Bool) (c : Target.Clause n) (suffix : List Bool) : recordTapes base c suffix .input = suffix := rfl @[simp] theorem recordTapes_field {n : ℕ} (base : Tape → List Bool) (c : Target.Clause n) (suffix : List Bool) (j : Fin 6) : recordTapes base c suffix (.field j) = encodeWord (fieldValue c j) := rfl theorem recordTapes_other {n : ℕ} (base : Tape → List Bool) (c : Target.Clause n) (suffix : List Bool) (k : Tape) (hi : k ≠ .input) (hf : ∀ j, k ≠ .field j) : recordTapes base c suffix k = base k := by cases k <;> try rfl · exact (hi rfl).elim · exact (hf _ rfl).elim @[simp] theorem recordTapes_name {n : ℕ} (base : Tape → List Bool) (c : Target.Clause n) (suffix : List Bool) (j : Fin 3) : recordTapes base c suffix (.field (nameField j)) = encodeWord ((c)[j].variableIndex.val) := by fin_cases j <;> rfl private theorem trace_trans {α : Type*} (f : α → α) {a b : ℕ} {x y z : α} (first : f^[a] x = y) (second : f^[b] y = z) : f^[a + b] x = z := by rw [Nat.add_comm, Function.iterate_add_apply, first, second] theorem fieldsTrace {n : ℕ} (base : Tape → List Bool) (c : Target.Clause n) (suffix : List Bool) (hinput : base .input = encodeWords (Complexity.clauseWords c) ++ suffix) (hempty : ∀ j, base (.field j) = []) (state : State) : (MachineComposition.advance (TM2.step program))^[ (encodeWords (Complexity.clauseWords c)).length + 6] (some ⟨some (.fieldStart 0), state, base⟩) = some ⟨some .loadSigns, (state.1, none), recordTapes base c suffix⟩ := by let a := fieldValue c 0 let b := fieldValue c 1 let d := fieldValue c 2 let e := fieldValue c 3 let f := fieldValue c 4 let g := fieldValue c 5 have hwords : Complexity.clauseWords c = [a, b, d, e, f, g] := rfl have hin : base .input = encodeWord a ++ (encodeWords [b, d, e, f, g] ++ suffix) := by rw [hinput, hwords] simp only [encodeWords, List.append_assoc] let t1 := SourceMachine.afterField .input (.field 0) base a (encodeWords [b, d, e, f, g] ++ suffix) let t2 := SourceMachine.afterField .input (.field 1) t1 b (encodeWords [d, e, f, g] ++ suffix) let t3 := SourceMachine.afterField .input (.field 2) t2 d (encodeWords [e, f, g] ++ suffix) let t4 := SourceMachine.afterField .input (.field 3) t3 e (encodeWords [f, g] ++ suffix) let t5 := SourceMachine.afterField .input (.field 4) t4 f (encodeWords [g] ++ suffix) let t6 := SourceMachine.afterField .input (.field 5) t5 g suffix have h1 := (SourceMachine.fieldInTime .input (.field 0) (by decide) (.fieldStart 0) (.fieldLoop 0) (some (.fieldStart 1)) program rfl rfl base a (encodeWords [b, d, e, f, g] ++ suffix) hin state.1 state.2).evals_in_steps change (MachineComposition.advance (TM2.step program))^[a + 2] (some ⟨some (.fieldStart 0), state, base⟩) = some ⟨some (.fieldStart 1), (state.1, none), t1⟩ at h1 have h2 := (SourceMachine.fieldInTime .input (.field 1) (by decide) (.fieldStart 1) (.fieldLoop 1) (some (.fieldStart 2)) program rfl rfl t1 b (encodeWords [d, e, f, g] ++ suffix) (by simp [t1, SourceMachine.afterField, encodeWords, List.append_assoc]) state.1 none).evals_in_steps change (MachineComposition.advance (TM2.step program))^[b + 2] (some ⟨some (.fieldStart 1), (state.1, none), t1⟩) = some ⟨some (.fieldStart 2), (state.1, none), t2⟩ at h2 have h3 := (SourceMachine.fieldInTime .input (.field 2) (by decide) (.fieldStart 2) (.fieldLoop 2) (some (.fieldStart 3)) program rfl rfl t2 d (encodeWords [e, f, g] ++ suffix) (by simp [t2, SourceMachine.afterField, encodeWords, List.append_assoc]) state.1 none).evals_in_steps change (MachineComposition.advance (TM2.step program))^[d + 2] (some ⟨some (.fieldStart 2), (state.1, none), t2⟩) = some ⟨some (.fieldStart 3), (state.1, none), t3⟩ at h3 have h4 := (SourceMachine.fieldInTime .input (.field 3) (by decide) (.fieldStart 3) (.fieldLoop 3) (some (.fieldStart 4)) program rfl rfl t3 e (encodeWords [f, g] ++ suffix) (by simp [t3, SourceMachine.afterField, encodeWords, List.append_assoc]) state.1 none).evals_in_steps change (MachineComposition.advance (TM2.step program))^[e + 2] (some ⟨some (.fieldStart 3), (state.1, none), t3⟩) = some ⟨some (.fieldStart 4), (state.1, none), t4⟩ at h4 have h5 := (SourceMachine.fieldInTime .input (.field 4) (by decide) (.fieldStart 4) (.fieldLoop 4) (some (.fieldStart 5)) program rfl rfl t4 f (encodeWords [g] ++ suffix) (by simp [t4, SourceMachine.afterField, encodeWords, List.append_assoc]) state.1 none).evals_in_steps change (MachineComposition.advance (TM2.step program))^[f + 2] (some ⟨some (.fieldStart 4), (state.1, none), t4⟩) = some ⟨some (.fieldStart 5), (state.1, none), t5⟩ at h5 have h6 := (SourceMachine.fieldInTime .input (.field 5) (by decide) (.fieldStart 5) (.fieldLoop 5) (some .loadSigns) program rfl rfl t5 g suffix (by simp [t5, SourceMachine.afterField, encodeWords]) state.1 none).evals_in_steps change (MachineComposition.advance (TM2.step program))^[g + 2] (some ⟨some (.fieldStart 5), (state.1, none), t5⟩) = some ⟨some .loadSigns, (state.1, none), t6⟩ at h6 have total := trace_trans _ (trace_trans _ (trace_trans _ (trace_trans _ (trace_trans _ h1 h2) h3) h4) h5) h6 have htime : (((((a + 2) + (b + 2)) + (d + 2)) + (e + 2)) + (f + 2)) + (g + 2) = (encodeWords (Complexity.clauseWords c)).length + 6 := by rw [hwords, encodeWords_length] simp omega rw [htime] at total have frame : t6 = recordTapes base c suffix := by funext k cases k with | field j => fin_cases j <;> simp [t6, t5, t4, t3, t2, t1, SourceMachine.afterField, SourceMachine.fieldTapes, recordTapes, hempty, a, b, d, e, f, g] | _ => simp [t6, t5, t4, t3, t2, t1, SourceMachine.afterField, SourceMachine.fieldTapes, recordTapes] rw [frame] at total exact total theorem readClauseTrace {n : ℕ} (base : Tape → List Bool) (c : Target.Clause n) (suffix : List Bool) (hinput : base .input = encodeWords (Complexity.clauseWords c) ++ suffix) (hempty : ∀ j, base (.field j) = []) (state : State) : (MachineComposition.advance (TM2.step program))^[ (encodeWords (Complexity.clauseWords c)).length + 7] (some ⟨some (.fieldStart 0), state, base⟩) = some ⟨some (.scan (.tailVariables 0)), (RawInitialRows.clauseSigns c, none), recordTapes base c suffix⟩ := by have first := fieldsTrace base c suffix hinput hempty state have last := RawInitialMachinePhases.loadSigns_step (recordTapes base c suffix) (c)[0].positive (c)[1].positive (c)[2].positive rfl rfl rfl (state.1, none) have lastTrace : (MachineComposition.advance (TM2.step program))^[1] (some ⟨some .loadSigns, (state.1, none), recordTapes base c suffix⟩) = some ⟨some (.scan (.tailVariables 0)), (RawInitialRows.clauseSigns c, none), recordTapes base c suffix⟩ := last have total := trace_trans _ first lastTrace simpa only [show (encodeWords (Complexity.clauseWords c)).length + 6 + 1 = (encodeWords (Complexity.clauseWords c)).length + 7 by omega] using total def readClauseInTime {n : ℕ} (base : Tape → List Bool) (c : Target.Clause n) (suffix : List Bool) (hinput : base .input = encodeWords (Complexity.clauseWords c) ++ suffix) (hempty : ∀ j, base (.field j) = []) (state : State) : StateTransition.EvalsToInTime (TM2.step program) ⟨some (.fieldStart 0), state, base⟩ (some ⟨some (.scan (.tailVariables 0)), (RawInitialRows.clauseSigns c, none), recordTapes base c suffix⟩) ((encodeWords (Complexity.clauseWords c)).length + 7) where steps := (encodeWords (Complexity.clauseWords c)).length + 7 evals_in_steps := readClauseTrace base c suffix hinput hempty state steps_le_m := Nat.le_refl _ end BinPackingGames.Foundations.PCP.RawInitialMachineReadClause end OAI