import Mathlib import OAI.Computability.DirectedFeedback.Encoding.Body namespace OAI namespace DFVSGames.Foundations.PCP.AlphabetTable open DFVSGames.Foundations.Complexity theorem Driver.finiteAlphabet (q : Nat) : MachineFiniteAlphabet.FiniteAlphabet (Driver.machine q) := by intro k change Finite Bool infer_instance theorem Runtime.finiteAlphabet (q : Nat) : MachineFiniteAlphabet.FiniteAlphabet (Runtime.tablePolynomialTime q).tm := Driver.finiteAlphabet q end DFVSGames.Foundations.PCP.AlphabetTable namespace DFVSGames.Foundations.Complexity.MachineStateEquiv open Turing.TM2 variable {K Λ σ τ : Type} {Γ : K → Type} abbrev statement (e : σ ≃ τ) : Stmt Γ Λ σ → Stmt Γ Λ τ := MachineControl.statement (id : Λ → Λ) e @[simp] theorem statement_symm_statement (e : σ ≃ τ) (q : Stmt Γ Λ σ) : statement e.symm (statement e q) = q := by induction q <;> simp_all only [statement, MachineControl.statement, Equiv.symm_symm, Equiv.symm_apply_apply, id_eq] def configuration (e : σ ≃ τ) (c : Cfg Γ Λ σ) : Cfg Γ Λ τ := ⟨c.l, e c.var, c.stk⟩ @[simp] theorem configuration_label (e : σ ≃ τ) (c : Cfg Γ Λ σ) : (configuration e c).l = c.l := rfl @[simp] theorem configuration_state (e : σ ≃ τ) (c : Cfg Γ Λ σ) : (configuration e c).var = e c.var := rfl @[simp] theorem configuration_tapes (e : σ ≃ τ) (c : Cfg Γ Λ σ) : (configuration e c).stk = c.stk := rfl @[simp] theorem configuration_symm_configuration (e : σ ≃ τ) (c : Cfg Γ Λ σ) : configuration e.symm (configuration e c) = c := by cases c simp only [configuration, Equiv.symm_apply_apply] @[simp] theorem configuration_configuration_symm (e : σ ≃ τ) (c : Cfg Γ Λ τ) : configuration e (configuration e.symm c) = c := by cases c simp only [configuration, Equiv.apply_symm_apply] def configurationEquiv (e : σ ≃ τ) : Cfg Γ Λ σ ≃ Cfg Γ Λ τ where toFun := configuration e invFun := configuration e.symm left_inv := configuration_symm_configuration e right_inv := configuration_configuration_symm e @[simp] theorem configurationEquiv_apply (e : σ ≃ τ) (c : Cfg Γ Λ σ) : configurationEquiv e c = configuration e c := rfl @[simp] theorem control_configuration (e : σ ≃ τ) (c : Cfg Γ Λ σ) : MachineControl.configuration (id : Λ → Λ) e c = configuration e c := by cases c simp [MachineControl.configuration, configuration] def program (e : σ ≃ τ) (source : Λ → Stmt Γ Λ σ) : Λ → Stmt Γ Λ τ := fun l => statement e (source l) @[simp] theorem program_symm_program (e : σ ≃ τ) (source : Λ → Stmt Γ Λ σ) : program e.symm (program e source) = source := by funext l exact statement_symm_statement e (source l) theorem statementPushBound (e : σ ≃ τ) (q : Stmt Γ Λ σ) : Runtime.statementPushBound (statement e q) = Runtime.statementPushBound q := by induction q <;> simp_all only [statement, MachineControl.statement, Runtime.statementPushBound] theorem supportsStmt_iff (e : σ ≃ τ) (S : Finset Λ) (q : Stmt Γ Λ σ) : SupportsStmt S (statement e q) ↔ SupportsStmt S q := by induction q with | push k f next ih => simpa only [statement, MachineControl.statement, SupportsStmt] using ih | peek k f next ih => simpa only [statement, MachineControl.statement, SupportsStmt] using ih | pop k f next ih => simpa only [statement, MachineControl.statement, SupportsStmt] using ih | load f next ih => simpa only [statement, MachineControl.statement, SupportsStmt] using ih | branch f yes no ihYes ihNo => simp only [statement, MachineControl.statement, SupportsStmt, ihYes, ihNo] | goto f => change (∀ state : τ, f (e.symm state) ∈ S) ↔ ∀ state : σ, f state ∈ S constructor · intro h state simpa only [Equiv.symm_apply_apply] using h (e state) · intro h state exact h (e.symm state) | halt => rfl theorem supports_iff [Inhabited Λ] (e : σ ≃ τ) (source : Λ → Stmt Γ Λ σ) (S : Finset Λ) : Supports (program e source) S ↔ Supports source S := by simp only [Supports, program, supportsStmt_iff] variable [DecidableEq K] theorem stepAux_transport (e : σ ≃ τ) (q : Stmt Γ Λ σ) (state : σ) (tapes : ∀ k, List (Γ k)) : stepAux (statement e q) (e state) tapes = configuration e (stepAux q state tapes) := by simpa only [statement, control_configuration] using MachineControl.stepAux_simulation (id : Λ → Λ) e q state tapes theorem stepAux_transport_symm (e : σ ≃ τ) (q : Stmt Γ Λ σ) (state : τ) (tapes : ∀ k, List (Γ k)) : stepAux (statement e q) state tapes = configuration e (stepAux q (e.symm state) tapes) := by simpa only [Equiv.apply_symm_apply] using stepAux_transport e q (e.symm state) tapes theorem step_transport (e : σ ≃ τ) (source : Λ → Stmt Γ Λ σ) (c : Cfg Γ Λ σ) : step (program e source) (configuration e c) = (step source c).map (configuration e) := by cases c with | mk l state tapes => cases l with | none => rfl | some l => change some (stepAux (statement e (source l)) (e state) tapes) = _ rw [stepAux_transport] rfl theorem step_iff (e : σ ≃ τ) (source : Λ → Stmt Γ Λ σ) (a b : Cfg Γ Λ σ) : step (program e source) (configuration e a) = some (configuration e b) ↔ step source a = some b := by rw [step_transport] change (step source a).map (configuration e) = (some b).map (configuration e) ↔ _ constructor · intro h apply Option.map_injective (configurationEquiv e).injective change (step source a).map (configuration e) = (some b).map (configuration e) exact h · exact congrArg (Option.map (configuration e)) theorem advance_transport (e : σ ≃ τ) (source : Λ → Stmt Γ Λ σ) (c : Option (Cfg Γ Λ σ)) : MachineComposition.advance (step (program e source)) (c.map (configuration e)) = (MachineComposition.advance (step source) c).map (configuration e) := by cases c with | none => rfl | some c => exact step_transport e source c theorem iterate_transport (e : σ ≃ τ) (source : Λ → Stmt Γ Λ σ) (n : Nat) (c : Option (Cfg Γ Λ σ)) : (MachineComposition.advance (step (program e source)))^[n] (c.map (configuration e)) = ((MachineComposition.advance (step source))^[n] c).map (configuration e) := by induction n with | zero => rfl | succ n ih => rw [Function.iterate_succ_apply', ih, advance_transport, Function.iterate_succ_apply'] theorem trace_iff (e : σ ≃ τ) (source : Λ → Stmt Γ Λ σ) (n : Nat) (a b : Cfg Γ Λ σ) : (MachineComposition.advance (step (program e source)))^[n] (some (configuration e a)) = some (configuration e b) ↔ (MachineComposition.advance (step source))^[n] (some a) = some b := by change (MachineComposition.advance (step (program e source)))^[n] ((some a).map (configuration e)) = (some b).map (configuration e) ↔ _ rw [iterate_transport] constructor · intro h apply Option.map_injective (configurationEquiv e).injective change ((MachineComposition.advance (step source))^[n] (some a)).map (configuration e) = (some b).map (configuration e) exact h · exact congrArg (Option.map (configuration e)) theorem trace (e : σ ≃ τ) (source : Λ → Stmt Γ Λ σ) (n : Nat) (a b : Cfg Γ Λ σ) (run : (MachineComposition.advance (step source))^[n] (some a) = some b) : (MachineComposition.advance (step (program e source)))^[n] (some (configuration e a)) = some (configuration e b) := (trace_iff e source n a b).2 run def execution (e : σ ≃ τ) (source : Λ → Stmt Γ Λ σ) {start : Cfg Γ Λ σ} {finish : Option (Cfg Γ Λ σ)} {budget : Nat} (run : StateTransition.EvalsToInTime (step source) start finish budget) : StateTransition.EvalsToInTime (step (program e source)) (configuration e start) (finish.map (configuration e)) budget where steps := run.steps evals_in_steps := by have h := run.evals_in_steps change (MachineComposition.advance (step source))^[run.steps] (some start) = finish at h change (MachineComposition.advance (step (program e source)))^[run.steps] ((some start).map (configuration e)) = finish.map (configuration e) rw [iterate_transport, h] steps_le_m := run.steps_le_m @[simp] theorem execution_steps (e : σ ≃ τ) (source : Λ → Stmt Γ Λ σ) {start : Cfg Γ Λ σ} {finish : Option (Cfg Γ Λ σ)} {budget : Nat} (run : StateTransition.EvalsToInTime (step source) start finish budget) : (execution e source run).steps = run.steps := rfl def execution_reflect (e : σ ≃ τ) (source : Λ → Stmt Γ Λ σ) {start : Cfg Γ Λ σ} {finish : Option (Cfg Γ Λ σ)} {budget : Nat} (run : StateTransition.EvalsToInTime (step (program e source)) (configuration e start) (finish.map (configuration e)) budget) : StateTransition.EvalsToInTime (step source) start finish budget where steps := run.steps evals_in_steps := by change (MachineComposition.advance (step source))^[run.steps] (some start) = finish apply Option.map_injective (configurationEquiv e).injective change ((MachineComposition.advance (step source))^[run.steps] (some start)).map (configuration e) = finish.map (configuration e) rw [← iterate_transport] exact run.evals_in_steps steps_le_m := run.steps_le_m @[simp] theorem execution_reflect_steps (e : σ ≃ τ) (source : Λ → Stmt Γ Λ σ) {start : Cfg Γ Λ σ} {finish : Option (Cfg Γ Λ σ)} {budget : Nat} (run : StateTransition.EvalsToInTime (step (program e source)) (configuration e start) (finish.map (configuration e)) budget) : (execution_reflect e source run).steps = run.steps := rfl end DFVSGames.Foundations.Complexity.MachineStateEquiv namespace DFVSGames.Foundations.Complexity.MachineUnaryEqualityBit open Turing open MachineComposition variable {K Λ A : Type} [DecidableEq K] abbrev Alphabet (_ : K) := Bool abbrev State (A : Type) := (A × Bool × Option Bool) × Option Bool def clean (ambient : A) : State A := ((ambient, false, none), none) def compareStateEquiv (A : Type) : MachineCompare.State A ≃ State A where toFun s := ((s.1, s.2.1, s.2.2.1), s.2.2.2) invFun s := (s.1.1, s.1.2.1, s.1.2.2, s.2) left_inv := by rintro ⟨a, b, c, d⟩; rfl right_inv := by rintro ⟨⟨a, b, c⟩, d⟩; rfl inductive Label | seedLeft | scanLeft | restoreLeft | seedRight | scanRight | restoreRight | compare | equal | different deriving DecidableEq instance : Fintype Label where elems := {.seedLeft, .scanLeft, .restoreLeft, .seedRight, .scanRight, .restoreRight, .compare, .equal, .different} complete label := by cases label <;> simp def exitAt (exit : Option Λ) : TM2.Stmt (Alphabet (K := K)) Λ (State A) := match exit with | none => .halt | some label => .goto fun _ => label def bitEncoding (bit : Bool) : List Bool := encodeWord (if bit then 1 else 0) def emit (destination : K) (bit : Bool) (exit : Option Λ) : TM2.Stmt (Alphabet (K := K)) Λ (State A) := .push destination (fun _ => false) (if bit then .push destination (fun _ => true) (exitAt exit) else exitAt exit) def instruction (tape : Fin 6 → K) (labels : Label → Λ) (exit : Option Λ) : Label → TM2.Stmt (Alphabet (K := K)) Λ (State A) | .seedLeft => MachineUnaryAffineAt.seed (tape 2) 0 (labels .scanLeft) | .scanLeft => MachineUnaryAffineAt.scan (tape 0) (tape 4) (tape 2) 1 (labels .scanLeft) (labels .restoreLeft) | .restoreLeft => Reduction.MachineTransfer.loopAt (tape 4) (tape 0) id false (labels .restoreLeft) (some (labels .seedRight)) | .seedRight => MachineUnaryAffineAt.seed (tape 3) 0 (labels .scanRight) | .scanRight => MachineUnaryAffineAt.scan (tape 1) (tape 4) (tape 3) 1 (labels .scanRight) (labels .restoreRight) | .restoreRight => Reduction.MachineTransfer.loopAt (tape 4) (tape 1) id false (labels .restoreRight) (some (labels .compare)) | .compare => MachineStateEquiv.statement (compareStateEquiv A) (MachineCompare.loop (tape 2) (tape 3) (labels .compare) (some (labels .equal)) (some (labels .different))) | .equal => emit (tape 5) true exit | .different => emit (tape 5) false exit def copySteps (n : Nat) : Nat := 2 * (n + 1) + 1 def compareSteps (a b : Nat) : Nat := max (a + 1) (b + 1) + 1 def steps (a b : Nat) : Nat := copySteps a + copySteps b + compareSteps a b + 1 theorem steps_le (a b : Nat) : steps a b ≤ 3 * (a + b) + 9 := by unfold steps copySteps compareSteps omega theorem encodeWord_eq_iff (a b : Nat) : encodeWord a = encodeWord b ↔ a = b := by constructor · intro h have hlength := congrArg List.length h simp only [encodeWord_length] at hlength omega · rintro rfl rfl theorem emit_step (destination : K) (bit : Bool) (exit : Option Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A)) (label : Λ) (atEmit : program label = emit destination bit exit) (state : State A) (base : K → List Bool) : TM2.step program ⟨some label, state, base⟩ = some ⟨exit, state, Function.update base destination (bitEncoding bit ++ base destination)⟩ := by change some (TM2.stepAux (program label) state base) = _ rw [atEmit] cases bit <;> cases exit <;> simp [emit, exitAt, TM2.stepAux, bitEncoding, encodeWord, Function.update_idem] private theorem joinTrace_inline_MachineUnaryEqualityBit {X : Type*} {f : X → X} {a b c : X} {n m : Nat} (first : f^[n] a = b) (second : f^[m] b = c) : f^[n + m] a = c := by rw [Nat.add_comm, Function.iterate_add_apply, first, second] theorem equalityTrace (tape : Fin 6 → K) (distinct : Function.Injective tape) (labels : Label → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A)) (atLabels : ∀ l, program (labels l) = instruction tape labels exit l) (base : K → List Bool) (a b : Nat) (leftSuffix rightSuffix : List Bool) (leftWord : base (tape 0) = encodeWord a ++ leftSuffix) (rightWord : base (tape 1) = encodeWord b ++ rightSuffix) (leftEmpty : base (tape 2) = []) (rightEmpty : base (tape 3) = []) (scratchEmpty : base (tape 4) = []) (ambient : A) : (advance (TM2.step program))^[steps a b] (some ⟨some (labels .seedLeft), clean ambient, base⟩) = some ⟨exit, clean ambient, Function.update base (tape 5) (bitEncoding (decide (a = b)) ++ base (tape 5))⟩ := by have hd (i j : Fin 6) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h) let midLeft := Function.update base (tape 2) (encodeWord a) let midBoth := Function.update midLeft (tape 3) (encodeWord b) have leftRun : (advance (TM2.step program))^[copySteps a] (some ⟨some (labels .seedLeft), clean ambient, base⟩) = some ⟨some (labels .seedRight), clean ambient, midLeft⟩ := by simpa only [copySteps, clean, midLeft, Nat.one_mul, Nat.add_zero, leftEmpty, List.append_nil] using MachineUnaryAffineAt.seededAffineTrace (tape 0) (tape 4) (tape 2) (hd 0 4 (by decide)) (hd 0 2 (by decide)) (hd 4 2 (by decide)) 1 0 (labels .seedLeft) (labels .scanLeft) (labels .restoreLeft) (some (labels .seedRight)) program (atLabels .seedLeft) (atLabels .scanLeft) (atLabels .restoreLeft) base a leftSuffix leftWord scratchEmpty (ambient, false, none) none have rightSource : midLeft (tape 1) = encodeWord b ++ rightSuffix := by simpa [midLeft, hd 1 2 (by decide)] using rightWord have scratchAfterLeft : midLeft (tape 4) = [] := by simpa [midLeft, hd 4 2 (by decide)] using scratchEmpty have rightCopyAfterLeft : midLeft (tape 3) = [] := by simpa [midLeft, hd 3 2 (by decide)] using rightEmpty have rightRun : (advance (TM2.step program))^[copySteps b] (some ⟨some (labels .seedRight), clean ambient, midLeft⟩) = some ⟨some (labels .compare), clean ambient, midBoth⟩ := by simpa only [copySteps, clean, midBoth, Nat.one_mul, Nat.add_zero, rightCopyAfterLeft, List.append_nil] using MachineUnaryAffineAt.seededAffineTrace (tape 1) (tape 4) (tape 3) (hd 1 4 (by decide)) (hd 1 3 (by decide)) (hd 4 3 (by decide)) 1 0 (labels .seedRight) (labels .scanRight) (labels .restoreRight) (some (labels .compare)) program (atLabels .seedRight) (atLabels .scanRight) (atLabels .restoreRight) midLeft b rightSuffix rightSource scratchAfterLeft (ambient, false, none) none have copyLeftWord : midBoth (tape 2) = encodeWord a := by simp [midBoth, midLeft, hd 2 3 (by decide)] have copyRightWord : midBoth (tape 3) = encodeWord b := by simp [midBoth] have restored : Reduction.MachineTransfer.tapesAt (tape 2) (tape 3) midBoth [] [] = base := by funext k by_cases hl : k = tape 2 · subst k simp [Reduction.MachineTransfer.tapesAt, midBoth, midLeft, hd 2 3 (by decide), leftEmpty] · by_cases hr : k = tape 3 · subst k simp [Reduction.MachineTransfer.tapesAt, midBoth, midLeft, rightEmpty] · simp [Reduction.MachineTransfer.tapesAt, midBoth, midLeft, hl, hr] let back := MachineStateEquiv.program (compareStateEquiv A).symm program have atCompare : back (labels .compare) = MachineCompare.loop (tape 2) (tape 3) (labels .compare) (some (labels .equal)) (some (labels .different)) := by change MachineStateEquiv.statement (compareStateEquiv A).symm (program (labels .compare)) = _ rw [atLabels .compare] exact MachineStateEquiv.statement_symm_statement (compareStateEquiv A) _ have backForward : MachineStateEquiv.program (compareStateEquiv A) back = program := by funext l change MachineStateEquiv.statement (compareStateEquiv A) (MachineStateEquiv.statement (compareStateEquiv A).symm (program l)) = program l simpa only [Equiv.symm_symm] using MachineStateEquiv.statement_symm_statement (compareStateEquiv A).symm (program l) have compareRun : (advance (TM2.step program))^[compareSteps a b] (some ⟨some (labels .compare), clean ambient, midBoth⟩) = some ⟨if a = b then some (labels .equal) else some (labels .different), clean ambient, base⟩ := by have native := MachineCompare.compareTrace_fromTapes (tape 2) (tape 3) (hd 2 3 (by decide)) (labels .compare) (some (labels .equal)) (some (labels .different)) back atCompare midBoth ambient none none rw [copyLeftWord, copyRightWord, restored] at native simp only [encodeWord_eq_iff, encodeWord_length] at native have transported := MachineStateEquiv.trace (compareStateEquiv A) back _ _ _ native rw [backForward] at transported simpa [MachineStateEquiv.configuration, compareStateEquiv, clean, compareSteps] using transported have emitRun : TM2.step program ⟨if a = b then some (labels .equal) else some (labels .different), clean ambient, base⟩ = some ⟨exit, clean ambient, Function.update base (tape 5) (bitEncoding (decide (a = b)) ++ base (tape 5))⟩ := by by_cases heq : a = b · simpa only [heq, ite_true, decide_true] using emit_step (tape 5) true exit program (labels .equal) (atLabels .equal) (clean ambient) base · simpa only [heq, ite_false, decide_false] using emit_step (tape 5) false exit program (labels .different) (atLabels .different) (clean ambient) base have copied := joinTrace_inline_MachineUnaryEqualityBit leftRun rightRun have compared := joinTrace_inline_MachineUnaryEqualityBit copied compareRun rw [steps, Function.iterate_succ_apply', compared, advance_some] exact emitRun def equalityInTime (tape : Fin 6 → K) (distinct : Function.Injective tape) (labels : Label → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A)) (atLabels : ∀ l, program (labels l) = instruction tape labels exit l) (base : K → List Bool) (a b : Nat) (leftSuffix rightSuffix : List Bool) (leftWord : base (tape 0) = encodeWord a ++ leftSuffix) (rightWord : base (tape 1) = encodeWord b ++ rightSuffix) (leftEmpty : base (tape 2) = []) (rightEmpty : base (tape 3) = []) (scratchEmpty : base (tape 4) = []) (ambient : A) : StateTransition.EvalsToInTime (TM2.step program) ⟨some (labels .seedLeft), clean ambient, base⟩ (some ⟨exit, clean ambient, Function.update base (tape 5) (bitEncoding (decide (a = b)) ++ base (tape 5))⟩) (3 * (a + b) + 9) where steps := steps a b evals_in_steps := equalityTrace tape distinct labels exit program atLabels base a b leftSuffix rightSuffix leftWord rightWord leftEmpty rightEmpty scratchEmpty ambient steps_le_m := steps_le a b def machine : FinTM2 where K := Fin 6 k₀ := 0 k₁ := 5 Γ _ := Bool Λ := Label main := .seedLeft σ := State Unit initialState := clean () m := instruction id id none def machineInTime (base : Fin 6 → List Bool) (a b : Nat) (leftSuffix rightSuffix : List Bool) (leftWord : base 0 = encodeWord a ++ leftSuffix) (rightWord : base 1 = encodeWord b ++ rightSuffix) (leftEmpty : base 2 = []) (rightEmpty : base 3 = []) (scratchEmpty : base 4 = []) : StateTransition.EvalsToInTime machine.step ⟨some .seedLeft, clean (), base⟩ (some ⟨none, clean (), Function.update base (5 : Fin 6) (bitEncoding (decide (a = b)) ++ base (5 : Fin 6))⟩) (3 * (a + b) + 9) := equalityInTime id (fun _ _ h => h) id none (instruction id id none) (fun _ => rfl) base a b leftSuffix rightSuffix leftWord rightWord leftEmpty rightEmpty scratchEmpty () end DFVSGames.Foundations.Complexity.MachineUnaryEqualityBit namespace DFVSGames.Foundations.Complexity.PoweringMachineRow open Turing open MachineFixedBlockMap open DFVSGames.Foundations.PCP abbrev Field (S : Nat) := (GraphTables.Label × GraphTables.Label) ⊕ (Fin S ⊕ Fin S) def inputSize (t S : Nat) : Nat := t * (4096 + (S + S)) def fieldEquiv (S : Nat) : Field S ≃ Fin (4096 + (S + S)) := (Equiv.sumCongr GraphTables.relationIndex (finSumFinEquiv : (Fin S ⊕ Fin S) ≃ Fin (S + S))).trans finSumFinEquiv def inputEquiv (t S : Nat) : (Fin t × Field S) ≃ Fin (inputSize t S) := (Equiv.prodCongr (Equiv.refl (Fin t)) (fieldEquiv S)).trans finProdFinEquiv def packData {t S : Nat} (data : Fin t → Field S → Bool) : Buffer (inputSize t S) := fun i => data ((inputEquiv t S).symm i).1 ((inputEquiv t S).symm i).2 @[simp] theorem packData_inputEquiv {t S : Nat} (data : Fin t → Field S → Bool) (k : Fin t) (f : Field S) : packData data (inputEquiv t S (k, f)) = data k f := by simp [packData] def basePredicate {t S : Nat} (bits : Buffer (inputSize t S)) (k : Fin t) (a b : GraphTables.Label) : Bool := bits (inputEquiv t S (k, .inl (a, b))) def leftMatches {t S : Nat} (bits : Buffer (inputSize t S)) (k : Fin t) (i : Fin S) : Bool := bits (inputEquiv t S (k, .inr (.inl i))) def rightMatches {t S : Nat} (bits : Buffer (inputSize t S)) (k : Fin t) (i : Fin S) : Bool := bits (inputEquiv t S (k, .inr (.inr i))) def firstMatch {S : Nat} (mask : Fin S → Bool) : Option (Fin S) := (List.ofFn id).find? mask theorem firstMatch_spec {S : Nat} (mask : Fin S → Bool) (i : Fin S) : firstMatch mask = some i ↔ mask i = true ∧ ∃ before after, List.ofFn id = before ++ i :: after ∧ ∀ j ∈ before, mask j = false := by simpa only [firstMatch, Bool.not_eq_eq_eq_not, Bool.not_true] using (List.find?_eq_some_iff_append (xs := List.ofFn id) (p := mask) (b := i)) theorem firstMatch_none {S : Nat} (mask : Fin S → Bool) : firstMatch mask = none ↔ ∀ i, mask i = false := by simp [firstMatch, List.find?_eq_none, List.mem_ofFn] def edgeAccept {t S q : Nat} (labelAt : Fin q → Fin S → GraphTables.Label) (bits : Buffer (inputSize t S)) (k : Fin t) (a b : Fin q) : Bool := match firstMatch (leftMatches bits k), firstMatch (rightMatches bits k) with | some i, some j => basePredicate bits k (labelAt a i) (labelAt b j) | _, _ => false theorem edgeAccept_of_matches {t S q : Nat} (labelAt : Fin q → Fin S → GraphTables.Label) (bits : Buffer (inputSize t S)) (k : Fin t) (a b : Fin q) (i j : Fin S) (hl : firstMatch (leftMatches bits k) = some i) (hr : firstMatch (rightMatches bits k) = some j) : edgeAccept labelAt bits k a b = basePredicate bits k (labelAt a i) (labelAt b j) := by simp [edgeAccept, hl, hr] def rowAccept {t S q : Nat} (labelAt : Fin q → Fin S → GraphTables.Label) (bits : Buffer (inputSize t S)) (a b : Fin q) : Bool := (List.ofFn id).all (fun k : Fin t => edgeAccept labelAt bits k a b) theorem rowAccept_eq_true {t S q : Nat} (labelAt : Fin q → Fin S → GraphTables.Label) (bits : Buffer (inputSize t S)) (a b : Fin q) : rowAccept labelAt bits a b = true ↔ ∀ k, edgeAccept labelAt bits k a b = true := by simp [rowAccept, List.all_eq_true, List.mem_ofFn] def rowBlock {t S q : Nat} (labelAt : Fin q → Fin S → GraphTables.Label) (bits : Buffer (inputSize t S)) : Buffer (q * q) := fun i => rowAccept labelAt bits ((GenericGraphTables.relationIndex q).symm i).1 ((GenericGraphTables.relationIndex q).symm i).2 @[simp] theorem rowBlock_at {t S q : Nat} (labelAt : Fin q → Fin S → GraphTables.Label) (bits : Buffer (inputSize t S)) (a b : Fin q) : rowBlock labelAt bits (GenericGraphTables.relationIndex q (a, b)) = rowAccept labelAt bits a b := by simp [rowBlock] def encodeBit (b : Bool) : List Bool := if b then [true, false] else [false] def encodeBits : List Bool → List Bool | [] => [] | b :: bits => encodeBit b ++ encodeBits bits @[simp] theorem encodeBits_append (xs ys : List Bool) : encodeBits (xs ++ ys) = encodeBits xs ++ encodeBits ys := by induction xs with | nil => rfl | cons b xs ih => simp [encodeBits, ih, List.append_assoc] theorem encodeBits_graphWords (bits : List Bool) : encodeBits bits = encodeWords (bits.map GraphTables.bitWord) := by induction bits with | nil => rfl | cons b bits ih => cases b <;> simp [encodeBits, encodeBit, encodeWords, GraphTables.bitWord, encodeWord, ih] theorem encodeBits_length_le (bits : List Bool) : (encodeBits bits).length ≤ 2 * bits.length := by induction bits with | nil => simp [encodeBits] | cons b bits ih => cases b <;> simp only [encodeBits, encodeBit, Bool.false_eq_true, ite_false, ite_true, List.length_append, List.length_cons, List.length_nil] <;> omega section Chains variable {K Λ σ : Type} {N : Nat} def readEncodedSlots (src : K) : List (Fin N) → TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N) → TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N) | [], next => next | i :: slots, next => .pop src (fun state head => (state.1, Function.update state.2 i (head.getD false))) (.branch (fun state => state.2 i) (.pop src (fun state _ => state) (readEncodedSlots src slots next)) (readEncodedSlots src slots next)) variable [DecidableEq K] theorem stepAux_readEncodedSlots (src : K) (slots : List (Fin N)) (next : TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N)) (ambient : σ) (bits buffer : Buffer N) (tapes : K → List Bool) (suffix : List Bool) : TM2.stepAux (readEncodedSlots src slots next) (ambient, buffer) (Function.update tapes src (encodeBits (slots.map bits) ++ suffix)) = TM2.stepAux next (ambient, fill slots bits buffer) (Function.update tapes src suffix) := by induction slots generalizing buffer tapes with | nil => simp [readEncodedSlots, encodeBits, fill] | cons i slots ih => cases hb : bits i <;> simpa only [readEncodedSlots, List.map_cons, encodeBits, encodeBit, hb, Bool.false_eq_true, ite_false, ite_true, List.cons_append, List.nil_append, TM2.stepAux, Function.update_self, List.head?_cons, Option.getD_some, List.tail_cons, Function.update_idem, Bool.cond_false, Bool.cond_true, fill, List.foldl_cons] using ih (Function.update buffer i (bits i)) tapes theorem stepAux_readEncodedAll (src : K) (next : TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N)) (state : σ × Buffer N) (bits : Buffer N) (tapes : K → List Bool) (suffix : List Bool) (hinput : tapes src = encodeBits (List.ofFn bits) ++ suffix) : TM2.stepAux (readEncodedSlots src (List.ofFn id) next) state tapes = TM2.stepAux next (state.1, bits) (Function.update tapes src suffix) := by have h := stepAux_readEncodedSlots src (List.ofFn id) next state.1 bits state.2 tapes suffix have hin : Function.update tapes src (encodeBits ((List.ofFn id).map bits) ++ suffix) = tapes := by simpa only [List.map_ofFn, Function.comp_id, ← hinput] using Function.update_eq_self src tapes rw [hin, fill_all] at h exact h omit [DecidableEq K] in theorem statementPushBound_readEncodedSlots (src : K) (slots : List (Fin N)) (next : TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N)) : Runtime.statementPushBound (readEncodedSlots src slots next) = Runtime.statementPushBound next := by induction slots with | nil => rfl | cons i slots ih => simp [readEncodedSlots, Runtime.statementPushBound, ih] def writeEncodedSlots {M : Nat} (dst : K) (emit : σ → Buffer M) : List (Fin M) → TM2.Stmt (fun _ : K => Bool) Λ σ → TM2.Stmt (fun _ : K => Bool) Λ σ | [], next => next | i :: slots, next => .push dst (fun _ => false) (.branch (fun state => emit state i) (.push dst (fun _ => true) (writeEncodedSlots dst emit slots next)) (writeEncodedSlots dst emit slots next)) theorem stepAux_writeEncodedSlots {M : Nat} (dst : K) (emit : σ → Buffer M) (slots : List (Fin M)) (next : TM2.Stmt (fun _ : K => Bool) Λ σ) (state : σ) (tapes : K → List Bool) : TM2.stepAux (writeEncodedSlots dst emit slots next) state tapes = TM2.stepAux next state (Function.update tapes dst (encodeBits ((slots.map (emit state)).reverse) ++ tapes dst)) := by induction slots generalizing tapes with | nil => simp [writeEncodedSlots, encodeBits] | cons i slots ih => cases hb : emit state i <;> simp only [writeEncodedSlots, TM2.stepAux, hb, Bool.cond_false, Bool.cond_true] all_goals rw [ih] all_goals simp [Function.update_idem, List.map_cons, List.reverse_cons, encodeBits_append, encodeBits, encodeBit, hb, List.append_assoc] omit [DecidableEq K] in theorem statementPushBound_writeEncodedSlots {M : Nat} (dst : K) (emit : σ → Buffer M) (slots : List (Fin M)) (next : TM2.Stmt (fun _ : K => Bool) Λ σ) : Runtime.statementPushBound (writeEncodedSlots dst emit slots next) = 2 * slots.length + Runtime.statementPushBound next := by induction slots with | nil => simp [writeEncodedSlots] | cons i slots ih => simp only [writeEncodedSlots, Runtime.statementPushBound, ih, List.length_cons] omega end Chains section Block variable {K Λ σ : Type} {N M : Nat} def encodedBlockStmt (src dst : K) (F : Buffer N → Buffer M) (next : TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N)) : TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N) := readEncodedSlots src (List.ofFn id) (writeEncodedSlots dst (fun state => F state.2) (List.ofFn id).reverse (.load (fun state => (state.1, emptyBuffer N)) next)) def encodedBlockAt (src dst : K) (F : Buffer N → Buffer M) (exit : Option Λ) : TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N) := encodedBlockStmt src dst F (finishAt exit) theorem statementPushBound_encodedBlockAt (src dst : K) (F : Buffer N → Buffer M) (exit : Option Λ) : Runtime.statementPushBound (encodedBlockAt (σ := σ) src dst F exit) = 2 * M := by cases exit <;> simp [encodedBlockAt, encodedBlockStmt, statementPushBound_readEncodedSlots, statementPushBound_writeEncodedSlots, finishAt, Runtime.statementPushBound] variable [DecidableEq K] theorem stepAux_encodedBlockStmt (src dst : K) (F : Buffer N → Buffer M) (next : TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N)) (hne : src ≠ dst) (bits : Buffer N) (suffix : List Bool) (state : σ × Buffer N) (tapes : K → List Bool) (hinput : tapes src = encodeBits (List.ofFn bits) ++ suffix) : TM2.stepAux (encodedBlockStmt src dst F next) state tapes = TM2.stepAux next (state.1, emptyBuffer N) (Function.update (Function.update tapes src suffix) dst (encodeBits (List.ofFn (F bits)) ++ tapes dst)) := by unfold encodedBlockStmt rw [stepAux_readEncodedAll src _ state bits tapes suffix hinput, stepAux_writeEncodedSlots] simp only [List.map_reverse, List.map_ofFn, Function.comp_id, List.reverse_reverse, Function.update_of_ne (Ne.symm hne), TM2.stepAux] theorem stepAux_encodedBlockAt (src dst : K) (F : Buffer N → Buffer M) (exit : Option Λ) (hne : src ≠ dst) (bits : Buffer N) (suffix : List Bool) (state : σ × Buffer N) (tapes : K → List Bool) (hinput : tapes src = encodeBits (List.ofFn bits) ++ suffix) : TM2.stepAux (encodedBlockAt src dst F exit) state tapes = { l := exit, var := (state.1, emptyBuffer N), stk := Function.update (Function.update tapes src suffix) dst (encodeBits (List.ofFn (F bits)) ++ tapes dst) } := by rw [encodedBlockAt, stepAux_encodedBlockStmt src dst F _ hne bits suffix state tapes hinput] cases exit <;> rfl theorem step_encodedBlockAt (src dst : K) (F : Buffer N → Buffer M) (exit : Option Λ) (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N)) (label : Λ) (hprogram : program label = encodedBlockAt src dst F exit) (hne : src ≠ dst) (bits : Buffer N) (suffix : List Bool) (state : σ × Buffer N) (tapes : K → List Bool) (hinput : tapes src = encodeBits (List.ofFn bits) ++ suffix) : TM2.step program { l := some label, var := state, stk := tapes } = some { l := exit var := (state.1, emptyBuffer N) stk := Function.update (Function.update tapes src suffix) dst (encodeBits (List.ofFn (F bits)) ++ tapes dst) } := by simp only [TM2.step, hprogram, stepAux_encodedBlockAt src dst F exit hne bits suffix state tapes hinput] end Block def rowAt {K Λ σ : Type} {t S q : Nat} (src dst : K) (labelAt : Fin q → Fin S → GraphTables.Label) (exit : Option Λ) : TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer (inputSize t S)) := encodedBlockAt src dst (rowBlock labelAt) exit def machine {t S q : Nat} (labelAt : Fin q → Fin S → GraphTables.Label) : FinTM2 where K := Bool k₀ := false k₁ := true Γ _ := Bool Λ := Unit main := () σ := Unit × Buffer (inputSize t S) initialState := ((), emptyBuffer (inputSize t S)) m _ := rowAt false true labelAt none theorem machine_statementPushBound {t S q : Nat} (labelAt : Fin q → Fin S → GraphTables.Label) : Runtime.statementPushBound ((machine (t := t) labelAt).m ()) = 2 * (q * q) := statementPushBound_encodedBlockAt false true (rowBlock labelAt) none def machineInTime {t S q : Nat} (labelAt : Fin q → Fin S → GraphTables.Label) (bits : Buffer (inputSize t S)) (suffix : List Bool) (register : Buffer (inputSize t S)) (tapes : Bool → List Bool) (hinput : tapes false = encodeBits (List.ofFn bits) ++ suffix) : StateTransition.EvalsToInTime (machine (t := t) labelAt).step ⟨some (), ((), register), tapes⟩ (some ⟨none, ((), emptyBuffer (inputSize t S)), Function.update (Function.update tapes false suffix) true (encodeBits (List.ofFn (rowBlock labelAt bits)) ++ tapes true)⟩) 1 where steps := 1 evals_in_steps := by change TM2.step (fun _ : Unit => rowAt false true labelAt none) { l := some (), var := ((), register), stk := tapes } = some { l := none var := ((), emptyBuffer (inputSize t S)) stk := Function.update (Function.update tapes false suffix) true (encodeBits (List.ofFn (rowBlock labelAt bits)) ++ tapes true) } exact step_encodedBlockAt false true (rowBlock labelAt) (none : Option Unit) (fun _ => rowAt false true labelAt none) () rfl (by decide) bits suffix ((), register) tapes hinput steps_le_m := le_rfl end DFVSGames.Foundations.Complexity.PoweringMachineRow end OAI