import Mathlib.Tactic.FinCases import OAI.Computability.BinPacking.PCP.RoundTableGap namespace OAI namespace BinPackingGames.Foundations.PCP.AlphabetTable.RuntimeModel open Turing open BinPackingGames.Foundations.Complexity inductive Tape | original | cursor | scan | vertices | darts | counter | edge | tail | reverseIndex | head | scratch | indexScan | compareLeft | compareRight | reversed | output deriving DecidableEq protected abbrev Tape.enumList : List Tape := [.original, .cursor, .scan, .vertices, .darts, .counter, .edge, .tail, .reverseIndex, .head, .scratch, .indexScan, .compareLeft, .compareRight, .reversed, .output] protected theorem Tape.enumList_getElem?_ctorIdx_eq (x : Tape) : Tape.enumList[x.ctorIdx]? = some x := by cases x <;> rfl protected theorem Tape.enumList_nodup : Tape.enumList.Nodup := by decide instance : Fintype Tape where elems := ⟨Tape.enumList, Tape.enumList_nodup⟩ complete x := by cases x <;> decide abbrev Alphabet (_ : Tape) := Bool abbrev Flags := Bool × Bool × Option Bool abbrev Ambient (q : Nat) := Flags × GenericGraphTables.RelationTable q abbrev State (q : Nat) := Ambient q × Option Bool def normal {q : Nat} (relation : GenericGraphTables.RelationTable q) (selfLoop : Bool) : State q := (((selfLoop, false, none), relation), none) def initial (q : Nat) : State q := normal (Vector.replicate (q * q) false) false def context {q : Nat} (ambient : Ambient q) : GenericGraphTables.RelationTable q × Bool := (ambient.2, ambient.1.1) def compareEquiv (q : Nat) : State q ≃ MachineCompare.State (GenericGraphTables.RelationTable q × Bool) where toFun s := ((s.1.2, s.1.1.1), s.1.1.2.1, s.1.1.2.2, s.2) invFun s := (((s.1.2, s.2.1, s.2.2.1), s.1.1), s.2.2.2) left_inv _ := rfl right_inv _ := rfl @[simp] theorem compareEquiv_normal {q : Nat} (relation : GenericGraphTables.RelationTable q) (selfLoop : Bool) : compareEquiv q (normal relation selfLoop) = ((relation, selfLoop), false, none, none) := rfl def emitterEquiv (q : Nat) : ((Ambient q × Unit) × Option Bool) ≃ State q where toFun s := (s.1.1, s.2) invFun s := ((s.1, ()), s.2) left_inv s := by rcases s with ⟨⟨s, u⟩, r⟩; cases u; rfl right_inv _ := rfl def setupPorts : Fin 7 → Tape := ![.original, .cursor, .vertices, .darts, .counter, .scratch, .edge] theorem setupPorts_injective : Function.Injective setupPorts := by intro i j h fin_cases i <;> fin_cases j <;> simp_all [setupPorts] def headPorts : Fin 6 → Tape := ![.original, .reverseIndex, .scan, .indexScan, .head, .scratch] theorem headPorts_injective : Function.Injective headPorts := by intro i j h fin_cases i <;> fin_cases j <;> simp_all [headPorts] def rowSources : Fin 5 → Tape := ![.vertices, .darts, .edge, .tail, .head] def headerSources : Fin 2 → Tape := ![.vertices, .darts] def fieldPorts : Fin 3 → Tape := ![.tail, .reverseIndex, .head] def cleanupPorts : Fin 15 → Tape := ![.original, .cursor, .scan, .vertices, .darts, .counter, .edge, .tail, .reverseIndex, .head, .scratch, .indexScan, .compareLeft, .compareRight, .reversed] theorem cleanupPorts_ne_output (i : Fin 15) : cleanupPorts i ≠ .output := by fin_cases i <;> simp [cleanupPorts] theorem cleanupPorts_cover (tape : Tape) (h : tape ≠ .output) : ∃ i, cleanupPorts i = tape := by cases tape · exact ⟨0, rfl⟩ · exact ⟨1, rfl⟩ · exact ⟨2, rfl⟩ · exact ⟨3, rfl⟩ · exact ⟨4, rfl⟩ · exact ⟨5, rfl⟩ · exact ⟨6, rfl⟩ · exact ⟨7, rfl⟩ · exact ⟨8, rfl⟩ · exact ⟨9, rfl⟩ · exact ⟨10, rfl⟩ · exact ⟨11, rfl⟩ · exact ⟨12, rfl⟩ · exact ⟨13, rfl⟩ · exact ⟨14, rfl⟩ · exact (h rfl).elim variable {Λ : Type} {q : Nat} def compareStatement (stmt : TM2.Stmt Alphabet Λ (MachineCompare.State (GenericGraphTables.RelationTable q × Bool))) : TM2.Stmt Alphabet Λ (State q) := MachineControl.statement id (compareEquiv q).symm stmt def emitterStatement (stmt : TM2.Stmt Alphabet Λ ((Ambient q × Unit) × Option Bool)) : TM2.Stmt Alphabet Λ (State q) := MachineControl.statement id (emitterEquiv q) stmt def storeLoop (value : Bool) (next : Λ) : TM2.Stmt Alphabet Λ (State q) := .load (fun state => (((value, false, none), state.1.2), none)) (.goto fun _ => next) theorem stepAux_storeLoop (value : Bool) (next : Λ) (state : State q) (base : Tape → List Bool) : TM2.stepAux (storeLoop value next) state base = ⟨some next, normal state.1.2 value, base⟩ := rfl def nextRow (next : Λ) : TM2.Stmt Alphabet Λ (State q) := .push .edge (fun _ => true) (.load (fun state => (state.1, none)) (.goto fun _ => next)) theorem stepAux_nextRow (next : Λ) (state : State q) (base : Tape → List Bool) (edge : Nat) (suffix : List Bool) (he : base .edge = encodeWord edge ++ suffix) : TM2.stepAux (nextRow next) state base = ⟨some next, (state.1, none), Function.update base .edge (encodeWord (edge + 1) ++ suffix)⟩ := by simp [nextRow, TM2.stepAux, he, encodeWord, List.replicate_succ] def transportInTime {K τ υ : Type} [DecidableEq K] {Γ : K → Type} (e : τ ≃ υ) (p : Λ → TM2.Stmt Γ Λ τ) {start finish : TM2.Cfg Γ Λ τ} {budget : Nat} (run : StateTransition.EvalsToInTime (TM2.step p) start (some finish) budget) : StateTransition.EvalsToInTime (TM2.step (MachineControl.program (Equiv.refl Λ) e p)) (MachineControl.configuration id e start) (some (MachineControl.configuration id e finish)) budget := by apply MachineComposition.liftExecutionInTime _ _ (MachineControl.configuration id e) _ run intro a b hab have h := MachineControl.step_simulation (Equiv.refl Λ) e p a rw [hab] at h exact h end BinPackingGames.Foundations.PCP.AlphabetTable.RuntimeModel namespace BinPackingGames.Foundations.PCP.AlphabetTable.CompareLoop open Turing open BinPackingGames.Foundations.Complexity open RuntimeModel inductive Label | leftFirst | leftSecond | rightFirst | rightSecond | compare | equalStore | differentStore deriving DecidableEq protected abbrev Label.enumList : List Label := [.leftFirst, .leftSecond, .rightFirst, .rightSecond, .compare, .equalStore, .differentStore] 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 {q : Nat} {Λ : Type} def statement (labels : Label → Λ) (next : Λ) : Label → TM2.Stmt Alphabet Λ (State q) | .leftFirst => compareStatement (MachineFieldProfile.copyStatement (Reduction.MachineTransfer.loopAt .tail .scratch id false (labels .leftFirst) (some (labels .leftSecond)))) | .leftSecond => compareStatement (MachineFieldProfile.copyStatement (MachineCopy.forkLoop .scratch .tail .compareLeft false (labels .leftSecond) (some (labels .rightFirst)))) | .rightFirst => compareStatement (MachineFieldProfile.copyStatement (Reduction.MachineTransfer.loopAt .head .scratch id false (labels .rightFirst) (some (labels .rightSecond)))) | .rightSecond => compareStatement (MachineFieldProfile.copyStatement (MachineCopy.forkLoop .scratch .head .compareRight false (labels .rightSecond) (some (labels .compare)))) | .compare => compareStatement (MachineCompare.loop .compareLeft .compareRight (labels .compare) (some (labels .equalStore)) (some (labels .differentStore))) | .equalStore => storeLoop true next | .differentStore => storeLoop false next private theorem compareProgram_at (program : Λ → TM2.Stmt Alphabet Λ (State q)) (label : Λ) (stmt : TM2.Stmt Alphabet Λ (MachineCompare.State (GenericGraphTables.RelationTable q × Bool))) (atLabel : program label = compareStatement stmt) : (MachineControl.program (Equiv.refl Λ) (compareEquiv q) program) label = stmt := by change MachineControl.statement id (compareEquiv q) (program label) = stmt rw [atLabel] exact MachineFieldProfile.statement_roundtrip (compareEquiv q) stmt def compareWordsInTime (labels : Label → Λ) (next : Λ) (program : Λ → TM2.Stmt Alphabet Λ (State q)) (atLabel : ∀ label, program (labels label) = statement labels next label) (base : Tape → List Bool) (hLeft : base .compareLeft = []) (hRight : base .compareRight = []) (hScratch : base .scratch = []) (relation : GenericGraphTables.RelationTable q) (oldLoop : Bool) : StateTransition.EvalsToInTime (TM2.step program) ⟨some (labels .leftFirst), normal relation oldLoop, base⟩ (some ⟨some next, normal relation (decide (base .tail = base .head)), base⟩) (2 * ((base .tail).length + 1) + 2 * ((base .head).length + 1) + max (base .tail).length (base .head).length + 2) := by let view := MachineControl.program (Equiv.refl Λ) (compareEquiv q) program have compared := MachineFieldProfile.nonDestructiveCompareInTime Tape.tail Tape.head Tape.compareLeft Tape.compareRight Tape.scratch (by decide) (by decide) (by decide) (labels .leftFirst) (labels .leftSecond) (labels .rightFirst) (labels .rightSecond) (labels .compare) (some (labels .equalStore)) (some (labels .differentStore)) view (compareProgram_at program _ _ (atLabel .leftFirst)) (compareProgram_at program _ _ (atLabel .leftSecond)) (compareProgram_at program _ _ (atLabel .rightFirst)) (compareProgram_at program _ _ (atLabel .rightSecond)) (compareProgram_at program _ _ (atLabel .compare)) base hLeft hRight hScratch (relation, oldLoop) have restoredProgram : MachineControl.program (Equiv.refl Λ) (compareEquiv q).symm view = program := by funext label exact MachineFieldProfile.statement_roundtrip (compareEquiv q).symm (program label) have transported := transportInTime (compareEquiv q).symm view compared rw [restoredProgram] at transported have copied : StateTransition.EvalsToInTime (TM2.step program) ⟨some (labels .leftFirst), normal relation oldLoop, base⟩ (some ⟨if base .tail = base .head then some (labels .equalStore) else some (labels .differentStore), normal relation oldLoop, base⟩) (2 * ((base .tail).length + 1) + 2 * ((base .head).length + 1) + max (base .tail).length (base .head).length + 1) := by simpa [MachineControl.configuration, compareEquiv, MachineFieldProfile.normalState, normal] using transported have stored : StateTransition.EvalsToInTime (TM2.step program) ⟨if base .tail = base .head then some (labels .equalStore) else some (labels .differentStore), normal relation oldLoop, base⟩ (some ⟨some next, normal relation (decide (base .tail = base .head)), base⟩) 1 := { steps := 1 evals_in_steps := by change TM2.step program ⟨if base .tail = base .head then some (labels .equalStore) else some (labels .differentStore), normal relation oldLoop, base⟩ = _ by_cases heq : base .tail = base .head · simp [heq, TM2.step, atLabel, statement, stepAux_storeLoop, normal] · simp [heq, TM2.step, atLabel, statement, stepAux_storeLoop, normal] steps_le_m := Nat.le_refl _ } let run := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ copied stored exact { toEvalsTo := run.toEvalsTo, steps_le_m := by have h := run.steps_le_m; omega } def comparisonTime (tailName headName : Nat) : Nat := 2 * (tailName + 2) + 2 * (headName + 2) + max (tailName + 1) (headName + 1) + 2 theorem encodeWord_eq_iff (left right : Nat) : encodeWord left = encodeWord right ↔ left = right := by constructor · intro h have hh := congrArg List.length h simpa only [encodeWord_length, Nat.add_right_cancel_iff] using hh · rintro rfl rfl def compareInTime (labels : Label → Λ) (next : Λ) (program : Λ → TM2.Stmt Alphabet Λ (State q)) (atLabel : ∀ label, program (labels label) = statement labels next label) (base : Tape → List Bool) (tailName headName : Nat) (hTail : base .tail = encodeWord tailName) (hHead : base .head = encodeWord headName) (hLeft : base .compareLeft = []) (hRight : base .compareRight = []) (hScratch : base .scratch = []) (relation : GenericGraphTables.RelationTable q) (oldLoop : Bool) : StateTransition.EvalsToInTime (TM2.step program) ⟨some (labels .leftFirst), normal relation oldLoop, base⟩ (some ⟨some next, normal relation (decide (tailName = headName)), base⟩) (comparisonTime tailName headName) := by have run := compareWordsInTime labels next program atLabel base hLeft hRight hScratch relation oldLoop simpa only [hTail, hHead, encodeWord_eq_iff, encodeWord_length, comparisonTime, Nat.add_assoc] using run theorem comparisonTime_le (tailName headName N : Nat) (hTail : tailName ≤ N) (hHead : headName ≤ N) : comparisonTime tailName headName ≤ 5 * N + 11 := by have hm : max (tailName + 1) (headName + 1) ≤ N + 1 := max_le (by omega) (by omega) unfold comparisonTime omega theorem comparisonTime_le_of_lt (tailName headName N : Nat) (hTail : tailName < N) (hHead : headName < N) : comparisonTime tailName headName ≤ 5 * N + 6 := by have hm : max (tailName + 1) (headName + 1) ≤ N := max_le (by omega) (by omega) unfold comparisonTime omega def compareWordsInTime_bounded (labels : Label → Λ) (next : Λ) (program : Λ → TM2.Stmt Alphabet Λ (State q)) (atLabel : ∀ label, program (labels label) = statement labels next label) (base : Tape → List Bool) (hLeft : base .compareLeft = []) (hRight : base .compareRight = []) (hScratch : base .scratch = []) (relation : GenericGraphTables.RelationTable q) (oldLoop : Bool) (N : Nat) (hTail : (base .tail).length ≤ N) (hHead : (base .head).length ≤ N) : StateTransition.EvalsToInTime (TM2.step program) ⟨some (labels .leftFirst), normal relation oldLoop, base⟩ (some ⟨some next, normal relation (decide (base .tail = base .head)), base⟩) (5 * N + 6) := by let run := compareWordsInTime labels next program atLabel base hLeft hRight hScratch relation oldLoop refine { toEvalsTo := run.toEvalsTo, steps_le_m := ?_ } have hm := max_le hTail hHead have h := run.steps_le_m omega def rowLoop (table : GenericGraphTables.Table q) (e : Fin table.darts) : Bool := decide (table.rows[e].tail.val = table.rows[table.rows[e].reverseIndex].tail.val) def rowState (table : GenericGraphTables.Table q) (e : Fin table.darts) : State q := normal table.rows[e].relation (rowLoop table e) theorem rowLoop_eq_graph (table : GenericGraphTables.Table q) (e : Fin table.darts) : rowLoop table e = decide ((GenericGraphTables.semantics table).tail e = (GenericGraphTables.semantics table).head e) := by simp only [rowLoop, ConstraintGraph.head, GenericGraphTables.semantics_tail, GenericGraphTables.semantics_reverse, Fin.val_inj] rfl theorem rowContext_eq (table : GenericGraphTables.Table q) (e : Fin table.darts) : context (rowState table e).1 = (table.rows[e].relation, decide ((GenericGraphTables.semantics table).tail e = (GenericGraphTables.semantics table).head e)) := by change (table.rows[e].relation, rowLoop table e) = _ rw [rowLoop_eq_graph] def compareRowInTime (labels : Label → Λ) (next : Λ) (program : Λ → TM2.Stmt Alphabet Λ (State q)) (atLabel : ∀ label, program (labels label) = statement labels next label) (table : GenericGraphTables.Table q) (e : Fin table.darts) (base : Tape → List Bool) (hTail : base .tail = encodeWord table.rows[e].tail.val) (hHead : base .head = encodeWord table.rows[table.rows[e].reverseIndex].tail.val) (hLeft : base .compareLeft = []) (hRight : base .compareRight = []) (hScratch : base .scratch = []) (oldLoop : Bool) : StateTransition.EvalsToInTime (TM2.step program) ⟨some (labels .leftFirst), normal table.rows[e].relation oldLoop, base⟩ (some ⟨some next, rowState table e, base⟩) (5 * table.vertices + 6) := by let run := compareInTime labels next program atLabel base _ _ hTail hHead hLeft hRight hScratch table.rows[e].relation oldLoop refine { toEvalsTo := run.toEvalsTo, steps_le_m := ?_ } exact run.steps_le_m.trans (comparisonTime_le_of_lt _ _ _ table.rows[e].tail.isLt table.rows[table.rows[e].reverseIndex].tail.isLt) theorem relationFromRowState (table : GenericGraphTables.Table q) (e : Fin table.darts) (event : AlphabetGraph.LocalEvent (Fin q)) (slot : Fin 6) (orientation : Bool) (a b : GraphTables.Label) : GraphTables.relationAt (Relations.relationFromTable (context (rowState table e).1).1 (context (rowState table e).1).2 event slot orientation) a b = (AlphabetGraph.graph (GenericGraphTables.semantics table)).accepts (((e, event), slot), orientation) (Enumeration.labelEquiv.symm a) (Enumeration.labelEquiv.symm b) := by rw [rowContext_eq] exact Relations.relationAt_relation_graph (GenericGraphTables.semantics table) e event slot orientation a b end BinPackingGames.Foundations.PCP.AlphabetTable.CompareLoop end OAI