Buckets:
| import Mathlib | |
| import OAI.Computability.MaxCut.Machines.MachineLookupCore | |
| namespace OAI | |
| namespace MaxCutGames.Foundations.Complexity.MachineSubroutine | |
| open Turing | |
| variable {K Λ Λ' σ : Type} {Γ : K → Type} | |
| def label (labels : Λ → Λ') (exit : Option Λ') : Option Λ → Option Λ' | |
| | none => exit | |
| | some l => some (labels l) | |
| def configuration (labels : Λ → Λ') (exit : Option Λ') (c : TM2.Cfg Γ Λ σ) : | |
| TM2.Cfg Γ Λ' σ := ⟨label labels exit c.l, c.var, c.stk⟩ | |
| def statement (labels : Λ → Λ') (exit : Option Λ') : | |
| TM2.Stmt Γ Λ σ → TM2.Stmt Γ Λ' σ | |
| | .push k f next => .push k f (statement labels exit next) | |
| | .peek k f next => .peek k f (statement labels exit next) | |
| | .pop k f next => .pop k f (statement labels exit next) | |
| | .load f next => .load f (statement labels exit next) | |
| | .branch f yes no => .branch f (statement labels exit yes) (statement labels exit no) | |
| | .goto f => .goto (fun s => labels (f s)) | |
| | .halt => match exit with | |
| | none => .halt | |
| | some l => .goto (fun _ => l) | |
| variable [DecidableEq K] | |
| theorem stepAux_simulation (labels : Λ → Λ') (exit : Option Λ') | |
| (q : TM2.Stmt Γ Λ σ) (state : σ) (tapes : ∀ k, List (Γ k)) : | |
| TM2.stepAux (statement labels exit q) state tapes = | |
| configuration labels exit (TM2.stepAux q state tapes) := by | |
| induction q generalizing state tapes with | |
| | push k f next ih => | |
| simpa only [statement, TM2.stepAux] using | |
| ih state (Function.update tapes k (f state :: tapes k)) | |
| | peek k f next ih => | |
| simpa only [statement, TM2.stepAux] using ih (f state (tapes k).head?) tapes | |
| | pop k f next ih => | |
| simpa only [statement, TM2.stepAux] using | |
| ih (f state (tapes k).head?) (Function.update tapes k (tapes k).tail) | |
| | load f next ih => simpa only [statement, TM2.stepAux] using ih (f state) tapes | |
| | branch f yes no ihYes ihNo => | |
| cases h : f state with | |
| | false => simpa only [statement, TM2.stepAux, h, Bool.cond_false] using ihNo state tapes | |
| | true => simpa only [statement, TM2.stepAux, h, Bool.cond_true] using ihYes state tapes | |
| | goto f => rfl | |
| | halt => cases exit <;> rfl | |
| theorem step_simulation (labels : Λ → Λ') (exit : Option Λ') | |
| (source : Λ → TM2.Stmt Γ Λ σ) (target : Λ' → TM2.Stmt Γ Λ' σ) | |
| (atLabels : ∀ l, target (labels l) = statement labels exit (source l)) | |
| (a b : TM2.Cfg Γ Λ σ) (h : TM2.step source a = some b) : | |
| TM2.step target (configuration labels exit a) = | |
| some (configuration labels exit b) := by | |
| cases a with | |
| | mk l state tapes => | |
| cases l with | |
| | none => simp [TM2.step] at h | |
| | some l => | |
| have hb : TM2.stepAux (source l) state tapes = b := Option.some.inj h | |
| rw [← hb] | |
| change some (TM2.stepAux (target (labels l)) state tapes) = _ | |
| rw [atLabels, stepAux_simulation] | |
| theorem trace (labels : Λ → Λ') (exit : Option Λ') | |
| (source : Λ → TM2.Stmt Γ Λ σ) (target : Λ' → TM2.Stmt Γ Λ' σ) | |
| (atLabels : ∀ l, target (labels l) = statement labels exit (source l)) | |
| (steps : Nat) (a b : TM2.Cfg Γ Λ σ) | |
| (run : (MachineComposition.advance (TM2.step source))^[steps] (some a) = some b) : | |
| (MachineComposition.advance (TM2.step target))^[steps] | |
| (some (configuration labels exit a)) = some (configuration labels exit b) := | |
| MachineComposition.liftSuccessfulTrace (TM2.step source) (TM2.step target) | |
| (configuration labels exit) (step_simulation labels exit source target atLabels) steps a b run | |
| def execution (labels : Λ → Λ') (exit : Option Λ') | |
| (source : Λ → TM2.Stmt Γ Λ σ) (target : Λ' → TM2.Stmt Γ Λ' σ) | |
| (atLabels : ∀ l, target (labels l) = statement labels exit (source l)) | |
| {a b : TM2.Cfg Γ Λ σ} {budget : Nat} | |
| (run : StateTransition.EvalsToInTime (TM2.step source) a (some b) budget) : | |
| StateTransition.EvalsToInTime (TM2.step target) | |
| (configuration labels exit a) (some (configuration labels exit b)) budget := | |
| MachineComposition.liftExecutionInTime (TM2.step source) (TM2.step target) | |
| (configuration labels exit) (step_simulation labels exit source target atLabels) run | |
| end MaxCutGames.Foundations.Complexity.MachineSubroutine | |
| /-! Actual lookup from a preserved table. A fresh copy is prefixed to the scan | |
| stack before each access. The old scan suffix is retained and need not be | |
| cleared between accesses. No graph/table data occurs in finite control. -/ | |
| namespace MaxCutGames.Foundations.Complexity.MachinePreservingLookup | |
| open Turing | |
| open MachineComposition | |
| variable {K Λ σ : Type} [DecidableEq K] | |
| abbrev Alphabet (_ : K) := Bool | |
| /-- Tape roles: original table, unary query, scan work, output, copy scratch. -/ | |
| def initialTapes (tape : Fin 5 → K) (base : K → List Bool) | |
| (index : Nat) (indexSuffix workSuffix output : List Bool) : K → List Bool := | |
| MachineLookup.tapes (tape 1) (tape 2) (tape 3) base | |
| (encodeWord index ++ indexSuffix) workSuffix output | |
| def finalTapes (tape : Fin 5 → K) (base : K → List Bool) | |
| (values : List Nat) (index value : Nat) | |
| (indexSuffix workSuffix output : List Bool) : K → List Bool := | |
| MachineLookup.tapes (tape 1) (tape 2) (tape 3) base | |
| (encodeWord 0 ++ indexSuffix) | |
| (encodeWords (values.drop (index + 1)) ++ workSuffix) (encodeWord value ++ output) | |
| theorem lookupFramedHaltTrace_some (index source destination : K) | |
| (his : index ≠ source) (hid : index ≠ destination) (hsd : source ≠ destination) | |
| (base : K → List Bool) (values : List Nat) (i value : Nat) | |
| (selected : values[i]? = some value) (indexSuffix suffix output : List Bool) | |
| (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step (MachineLookup.program index source destination)))^[ | |
| MachineLookupSpec.steps values i + 1] | |
| (some ⟨some MachineLookup.Label.guard, (ambient,register), | |
| MachineLookup.tapes index source destination base (encodeWord i ++ indexSuffix) | |
| (encodeWords values ++ suffix) output⟩) = | |
| some ⟨none, (ambient,none), MachineLookup.tapes index source destination base | |
| (encodeWord 0 ++ indexSuffix) (encodeWords (values.drop (i + 1)) ++ suffix) | |
| (encodeWord value ++ output)⟩ := by | |
| rw [Function.iterate_succ_apply', | |
| MachineLookup.lookupTrace_some index source destination his hid hsd | |
| base values i value selected indexSuffix suffix output ambient register] | |
| simp only [advance_some, TM2.step, MachineLookup.program, TM2.stepAux] | |
| /-- Code-level interfaces specify the actual two copy loops and six lookup | |
| instructions in the caller's program. No execution premise is supplied. -/ | |
| theorem preservingLookupTrace (tape : Fin 5 → K) (distinct : Function.Injective tape) | |
| (firstLabel secondLabel : Λ) (lookupLabels : MachineLookup.Label → Λ) (exit : Option Λ) | |
| (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) | |
| (atFirst : program firstLabel = Reduction.MachineTransfer.loopAt | |
| (tape 0) (tape 4) id false firstLabel (some secondLabel)) | |
| (atSecond : program secondLabel = MachineCopy.forkLoop | |
| (tape 4) (tape 0) (tape 2) false secondLabel (some (lookupLabels .guard))) | |
| (atLookup : ∀ l, program (lookupLabels l) = | |
| MachineSubroutine.statement lookupLabels exit (MachineLookup.program (tape 1) (tape 2) (tape 3) l)) | |
| (base : K → List Bool) (values : List Nat) (tableWord : base (tape 0) = encodeWords values) | |
| (scratchEmpty : base (tape 4) = []) (i value : Nat) (selected : values[i]? = some value) | |
| (indexSuffix workSuffix output : List Bool) (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step program))^[ | |
| 2 * ((encodeWords values).length + 1) + MachineLookupSpec.steps values i + 1] | |
| (some ⟨some firstLabel, (ambient,register), | |
| initialTapes tape base i indexSuffix workSuffix output⟩) = | |
| some ⟨exit, (ambient,none), finalTapes tape base values i value indexSuffix workSuffix output⟩ := by | |
| have hd (a b : Fin 5) (hne : a ≠ b) : tape a ≠ tape b := fun h => hne (distinct h) | |
| let start := initialTapes tape base i indexSuffix workSuffix output | |
| have htable : start (tape 0) = encodeWords values := by | |
| simpa only [start, initialTapes, MachineLookup.tapes_other _ _ _ _ | |
| (hd 0 1 (by decide)) (hd 0 2 (by decide)) (hd 0 3 (by decide))] using tableWord | |
| have hs : start (tape 4) = [] := by | |
| simpa only [start, initialTapes, MachineLookup.tapes_other _ _ _ _ | |
| (hd 4 1 (by decide)) (hd 4 2 (by decide)) (hd 4 3 (by decide))] using scratchEmpty | |
| have hwork : start (tape 2) = workSuffix := by | |
| simp only [start, initialTapes, MachineLookup.tapes_source _ _ _ (hd 2 3 (by decide))] | |
| have hcopy := MachineCopy.copyTrace (tape 0) (tape 2) (tape 4) | |
| (hd 0 2 (by decide)) (hd 0 4 (by decide)) (hd 2 4 (by decide)) false | |
| firstLabel secondLabel (some (lookupLabels .guard)) program atFirst atSecond start hs ambient register | |
| rw [htable, hwork] at hcopy | |
| simp only [start, initialTapes, MachineLookup.update_source _ _ _ (hd 2 3 (by decide))] at hcopy | |
| have hlookup := MachineSubroutine.trace lookupLabels exit | |
| (MachineLookup.program (tape 1) (tape 2) (tape 3)) program atLookup | |
| (MachineLookupSpec.steps values i + 1) _ _ | |
| (lookupFramedHaltTrace_some (tape 1) (tape 2) (tape 3) | |
| (hd 1 2 (by decide)) (hd 1 3 (by decide)) (hd 2 3 (by decide)) | |
| base values i value selected indexSuffix workSuffix output ambient none) | |
| simp only [MachineSubroutine.configuration, MachineSubroutine.label] at hlookup | |
| rw [show 2 * ((encodeWords values).length + 1) + MachineLookupSpec.steps values i + 1 = | |
| (MachineLookupSpec.steps values i + 1) + 2 * ((encodeWords values).length + 1) by omega, | |
| Function.iterate_add_apply] | |
| change (advance (TM2.step program))^[MachineLookupSpec.steps values i + 1] | |
| ((advance (TM2.step program))^[2 * ((encodeWords values).length + 1)] | |
| (some ⟨some firstLabel, (ambient,register), | |
| MachineLookup.tapes (tape 1) (tape 2) (tape 3) base | |
| (encodeWord i ++ indexSuffix) workSuffix output⟩)) = _ | |
| rw [hcopy] | |
| exact hlookup | |
| theorem preservingLookup_steps_le (values : List Nat) (i value : Nat) | |
| (selected : values[i]? = some value) : | |
| 2 * ((encodeWords values).length + 1) + MachineLookupSpec.steps values i + 1 ≤ | |
| 5 * (encodeWords values).length + 3 := by | |
| have ht := MachineLookupSpec.steps_le_input_encoding_size values i | |
| have hi := MachineLookupSpec.index_length_le values i value selected | |
| omega | |
| def preservingLookupInTime (tape : Fin 5 → K) (distinct : Function.Injective tape) | |
| (firstLabel secondLabel : Λ) (lookupLabels : MachineLookup.Label → Λ) (exit : Option Λ) | |
| (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) | |
| (atFirst : program firstLabel = Reduction.MachineTransfer.loopAt | |
| (tape 0) (tape 4) id false firstLabel (some secondLabel)) | |
| (atSecond : program secondLabel = MachineCopy.forkLoop | |
| (tape 4) (tape 0) (tape 2) false secondLabel (some (lookupLabels .guard))) | |
| (atLookup : ∀ l, program (lookupLabels l) = | |
| MachineSubroutine.statement lookupLabels exit (MachineLookup.program (tape 1) (tape 2) (tape 3) l)) | |
| (base : K → List Bool) (values : List Nat) (tableWord : base (tape 0) = encodeWords values) | |
| (scratchEmpty : base (tape 4) = []) (i value : Nat) (selected : values[i]? = some value) | |
| (indexSuffix workSuffix output : List Bool) (ambient : σ) (register : Option Bool) : | |
| StateTransition.EvalsToInTime (TM2.step program) | |
| ⟨some firstLabel, (ambient,register), initialTapes tape base i indexSuffix workSuffix output⟩ | |
| (some ⟨exit, (ambient,none), finalTapes tape base values i value indexSuffix workSuffix output⟩) | |
| (5 * (encodeWords values).length + 3) where | |
| steps := 2 * ((encodeWords values).length + 1) + MachineLookupSpec.steps values i + 1 | |
| evals_in_steps := preservingLookupTrace tape distinct firstLabel secondLabel lookupLabels exit | |
| program atFirst atSecond atLookup base values tableWord scratchEmpty i value selected | |
| indexSuffix workSuffix output ambient register | |
| steps_le_m := preservingLookup_steps_le values i value selected | |
| inductive Label | |
| | copyFirst | copySecond | lookup (l : MachineLookup.Label) | |
| deriving DecidableEq, Fintype | |
| def program (tape : Fin 5 → K) : | |
| Label → TM2.Stmt (Alphabet (K := K)) Label (σ × Option Bool) | |
| | .copyFirst => Reduction.MachineTransfer.loopAt (tape 0) (tape 4) id false .copyFirst (some .copySecond) | |
| | .copySecond => MachineCopy.forkLoop (tape 4) (tape 0) (tape 2) false .copySecond (some (.lookup .guard)) | |
| | .lookup l => MachineSubroutine.statement Label.lookup none | |
| (MachineLookup.program (tape 1) (tape 2) (tape 3) l) | |
| def machine : FinTM2 where | |
| K := Fin 5 | |
| k₀ := 0 | |
| k₁ := 3 | |
| Γ _ := Bool | |
| Λ := Label | |
| main := .copyFirst | |
| σ := Unit × Option Bool | |
| initialState := ((),none) | |
| m := program id | |
| end MaxCutGames.Foundations.Complexity.MachinePreservingLookup | |
| namespace MaxCutGames.Foundations.Complexity.MachineAffineLookup | |
| open Turing | |
| open MachineComposition | |
| variable {K Λ σ : Type} [DecidableEq K] | |
| abbrev Alphabet (_ : K) := Bool | |
| inductive Label | |
| | seed | scan | restore | copyFirst | copySecond | |
| | lookup (l : MachineLookup.Label) | |
| deriving DecidableEq, Fintype | |
| def instruction (source : K) (tape : Fin 5 → K) (coefficient offset : Nat) | |
| (labels : Label → Λ) (exit : Option Λ) : | |
| Label → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool) | |
| | .seed => MachineUnaryAffineAt.seed (tape 1) offset (labels .scan) | |
| | .scan => MachineUnaryAffineAt.scan source (tape 4) (tape 1) | |
| coefficient (labels .scan) (labels .restore) | |
| | .restore => Reduction.MachineTransfer.loopAt (tape 4) source id false | |
| (labels .restore) (some (labels .copyFirst)) | |
| | .copyFirst => Reduction.MachineTransfer.loopAt (tape 0) (tape 4) id false | |
| (labels .copyFirst) (some (labels .copySecond)) | |
| | .copySecond => MachineCopy.forkLoop (tape 4) (tape 0) (tape 2) false | |
| (labels .copySecond) (some (labels (.lookup .guard))) | |
| | .lookup l => MachineSubroutine.statement (fun q => labels (.lookup q)) exit | |
| (MachineLookup.program (tape 1) (tape 2) (tape 3) l) | |
| def steps (values : List Nat) (a coefficient offset : Nat) : Nat := | |
| (2 * (a + 1) + 1) + | |
| (2 * ((encodeWords values).length + 1) + | |
| MachineLookupSpec.steps values (coefficient * a + offset) + 1) | |
| def finalTapes (tape : Fin 5 → K) (base : K → List Bool) | |
| (values : List Nat) (index value : Nat) : K → List Bool := | |
| MachinePreservingLookup.finalTapes tape base values index value | |
| (base (tape 1)) (base (tape 2)) (base (tape 3)) | |
| theorem initialTapes_eq_update (tape : Fin 5 → K) (distinct : Function.Injective tape) | |
| (base : K → List Bool) (i : Nat) : | |
| MachinePreservingLookup.initialTapes tape base i | |
| (base (tape 1)) (base (tape 2)) (base (tape 3)) = | |
| Function.update base (tape 1) (encodeWord i ++ base (tape 1)) := by | |
| have hd (a b : Fin 5) (hne : a ≠ b) : tape a ≠ tape b := fun h => hne (distinct h) | |
| funext k | |
| by_cases h₁ : k = tape 1 | |
| · subst k | |
| simp [MachinePreservingLookup.initialTapes, MachineLookup.tapes, | |
| hd 1 2 (by decide), hd 1 3 (by decide)] | |
| · by_cases h₂ : k = tape 2 | |
| · subst k | |
| simp [MachinePreservingLookup.initialTapes, MachineLookup.tapes, h₁, | |
| hd 2 3 (by decide)] | |
| · by_cases h₃ : k = tape 3 | |
| · subst k | |
| simp [MachinePreservingLookup.initialTapes, MachineLookup.tapes, h₁] | |
| · simp [MachinePreservingLookup.initialTapes, MachineLookup.tapes, h₁, h₂, h₃] | |
| /-- An exact trace for the actual composite instructions. `atLabels` specifies | |
| the caller's finite program; there is no execution or runtime premise. -/ | |
| theorem affineLookupTrace (source : K) (tape : Fin 5 → K) | |
| (distinct : Function.Injective tape) (outside : ∀ i, source ≠ tape i) | |
| (coefficient offset : Nat) (labels : Label → Λ) (exit : Option Λ) | |
| (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) | |
| (atLabels : ∀ l, program (labels l) = instruction source tape coefficient offset labels exit l) | |
| (base : K → List Bool) (values : List Nat) | |
| (tableWord : base (tape 0) = encodeWords values) (scratchEmpty : base (tape 4) = []) | |
| (a : Nat) (suffix : List Bool) (sourceWord : base source = encodeWord a ++ suffix) | |
| (value : Nat) (selected : values[coefficient * a + offset]? = some value) | |
| (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step program))^[steps values a coefficient offset] | |
| (some ⟨some (labels .seed), (ambient,register), base⟩) = | |
| some ⟨exit, (ambient,none), finalTapes tape base values (coefficient * a + offset) value⟩ := by | |
| have hd (i j : Fin 5) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h) | |
| have haffine := MachineUnaryAffineAt.seededAffineTrace source (tape 4) (tape 1) | |
| (outside 4) (outside 1) (hd 4 1 (by decide)) coefficient offset | |
| (labels .seed) (labels .scan) (labels .restore) (some (labels .copyFirst)) | |
| program (atLabels .seed) (atLabels .scan) (atLabels .restore) | |
| base a suffix sourceWord scratchEmpty ambient register | |
| have hlookup := MachinePreservingLookup.preservingLookupTrace tape distinct | |
| (labels .copyFirst) (labels .copySecond) (fun q => labels (.lookup q)) exit | |
| program (atLabels .copyFirst) (atLabels .copySecond) (fun q => atLabels (.lookup q)) | |
| base values tableWord scratchEmpty (coefficient * a + offset) value selected | |
| (base (tape 1)) (base (tape 2)) (base (tape 3)) ambient none | |
| rw [initialTapes_eq_update tape distinct] at hlookup | |
| rw [steps, Nat.add_comm, Function.iterate_add_apply, haffine] | |
| exact hlookup | |
| theorem steps_le (values : List Nat) (a coefficient offset value : Nat) | |
| (selected : values[coefficient * a + offset]? = some value) : | |
| steps values a coefficient offset ≤ 2 * a + 5 * (encodeWords values).length + 6 := by | |
| have h := MachinePreservingLookup.preservingLookup_steps_le values | |
| (coefficient * a + offset) value selected | |
| unfold steps | |
| omega | |
| theorem steps_le_table (values : List Nat) (a coefficient offset value : Nat) | |
| (selected : values[coefficient * a + offset]? = some value) | |
| (ha : a ≤ (encodeWords values).length) : | |
| steps values a coefficient offset ≤ 7 * (encodeWords values).length + 6 := by | |
| have h := steps_le values a coefficient offset value selected | |
| omega | |
| theorem finalTapes_other (tape : Fin 5 → K) (base : K → List Bool) | |
| (values : List Nat) (index value : Nat) (k : K) | |
| (h₁ : k ≠ tape 1) (h₂ : k ≠ tape 2) (h₃ : k ≠ tape 3) : | |
| finalTapes tape base values index value k = base k := | |
| MachineLookup.tapes_other _ _ _ _ h₁ h₂ h₃ _ _ _ _ | |
| theorem finalTapes_output (tape : Fin 5 → K) (base : K → List Bool) | |
| (values : List Nat) (index value : Nat) : | |
| finalTapes tape base values index value (tape 3) = encodeWord value ++ base (tape 3) := | |
| MachineLookup.tapes_destination _ _ _ _ _ _ _ | |
| def machine (coefficient offset : Nat) : FinTM2 where | |
| K := Fin 6 | |
| k₀ := 0 | |
| k₁ := 4 | |
| Γ _ := Bool | |
| Λ := Label | |
| main := .seed | |
| σ := Unit × Option Bool | |
| initialState := ((),none) | |
| m := instruction 0 Fin.succ coefficient offset id none | |
| end MaxCutGames.Foundations.Complexity.MachineAffineLookup | |
| /-! | |
| A fixed-width gather from an input-resident table. Every coordinate index lives | |
| on a unary tape. The coefficient and coordinate-dependent offsets alone live | |
| in finite control. Actual preserving affine lookup instructions read each | |
| requested field, and the proof below composes their traces. This is the gather | |
| phase of product-row construction, before the fixed-width Horner phase. | |
| -/ | |
| namespace MaxCutGames.Explicit.MachineProductGather | |
| open Turing | |
| open MaxCutGames.Foundations.Complexity | |
| open MachineComposition | |
| variable {K Λ σ : Type} [DecidableEq K] | |
| /-- Shared original table, query, scan and copy scratch, then input coordinate | |
| fields and the corresponding gathered output fields. -/ | |
| abbrev Tape (width : Nat) := Fin 4 ⊕ (Fin width ⊕ Fin width) | |
| def shift (width : Nat) : Tape width → Tape (width + 1) := | |
| Sum.map id (Sum.map Fin.succ Fin.succ) | |
| theorem shift_injective (width : Nat) : Function.Injective (shift width) := by | |
| intro a b h | |
| rcases a with a | (a | a) <;> rcases b with b | (b | b) <;> simp_all [shift] | |
| def lookupRole (width : Nat) : Fin 5 → Tape (width + 1) | |
| | 0 => .inl 0 | |
| | 1 => .inl 1 | |
| | 2 => .inl 2 | |
| | 3 => .inr (.inr 0) | |
| | 4 => .inl 3 | |
| theorem lookupRole_injective (width : Nat) : Function.Injective (lookupRole width) := by | |
| intro a b h | |
| fin_cases a <;> fin_cases b <;> simp_all [lookupRole] | |
| def Label : Nat → Type | |
| | 0 => Unit | |
| | width + 1 => MachineAffineLookup.Label ⊕ Label width | |
| instance labelFintype (width : Nat) : Fintype (Label width) := by | |
| induction width with | |
| | zero => exact inferInstanceAs (Fintype Unit) | |
| | succ width ih => | |
| letI := ih | |
| exact inferInstanceAs (Fintype (MachineAffineLookup.Label ⊕ Label width)) | |
| instance labelDecidableEq (width : Nat) : DecidableEq (Label width) := by | |
| induction width with | |
| | zero => exact inferInstanceAs (DecidableEq Unit) | |
| | succ width ih => | |
| letI := ih | |
| exact inferInstanceAs (DecidableEq (MachineAffineLookup.Label ⊕ Label width)) | |
| def entry : (width : Nat) → Label width | |
| | 0 => () | |
| | _ + 1 => .inl .seed | |
| def instruction : (width : Nat) → (Tape width → K) → Nat → (Fin width → Nat) → | |
| (Label width → Λ) → Option Λ → Label width → | |
| TM2.Stmt (fun _ : K => Bool) Λ (σ × Option Bool) | |
| | 0, placement, _, _, _, exit, _ => | |
| .load (fun state => (state.1, none)) | |
| (MaxCutGames.Reduction.MachineTransfer.exitAt (placement (.inl 0)) exit) | |
| | width + 1, placement, coefficient, offsets, labels, exit, label => | |
| match label with | |
| | .inl inner => MachineAffineLookup.instruction (placement (.inr (.inl 0))) | |
| (placement ∘ lookupRole width) coefficient (offsets 0) | |
| (fun l => labels (.inl l)) (some (labels (.inr (entry width)))) inner | |
| | .inr inner => instruction width (placement ∘ shift width) coefficient | |
| (fun i => offsets i.succ) (fun l => labels (.inr l)) exit inner | |
| def steps (values : List Nat) (coefficient : Nat) : | |
| (width : Nat) → (Fin width → Nat) → (Fin width → Nat) → Nat | |
| | 0, _, _ => 1 | |
| | width + 1, indices, offsets => | |
| MachineAffineLookup.steps values (indices 0) coefficient (offsets 0) + | |
| steps values coefficient width (fun i => indices i.succ) (fun i => offsets i.succ) | |
| def finalTapes (values : List Nat) (coefficient : Nat) : | |
| (width : Nat) → (Tape width → K) → (Fin width → Nat) → | |
| (Fin width → Nat) → (Fin width → Nat) → (K → List Bool) → K → List Bool | |
| | 0, _, _, _, _, base => base | |
| | width + 1, placement, indices, offsets, fields, base => | |
| finalTapes values coefficient width (placement ∘ shift width) | |
| (fun i => indices i.succ) (fun i => offsets i.succ) (fun i => fields i.succ) | |
| (MachineAffineLookup.finalTapes (placement ∘ lookupRole width) base values | |
| (coefficient * indices 0 + offsets 0) (fields 0)) | |
| theorem finalTapes_other (values : List Nat) (coefficient width : Nat) | |
| (placement : Tape width → K) (indices offsets fields : Fin width → Nat) | |
| (base : K → List Bool) (k : K) | |
| (query : k ≠ placement (.inl 1)) (scan : k ≠ placement (.inl 2)) | |
| (outputs : ∀ i, k ≠ placement (.inr (.inr i))) : | |
| finalTapes values coefficient width placement indices offsets fields base k = base k := by | |
| induction width generalizing base with | |
| | zero => rfl | |
| | succ width ih => | |
| rw [finalTapes] | |
| rw [ih (placement ∘ shift width) (fun i => indices i.succ) | |
| (fun i => offsets i.succ) (fun i => fields i.succ) _ query scan | |
| (fun i => outputs i.succ)] | |
| exact MachineAffineLookup.finalTapes_other _ _ _ _ _ k query scan (outputs 0) | |
| theorem gatherTrace (values : List Nat) (coefficient width : Nat) | |
| (placement : Tape width → K) (distinct : Function.Injective placement) | |
| (indices offsets fields : Fin width → Nat) | |
| (selected : ∀ i, values[coefficient * indices i + offsets i]? = some (fields i)) | |
| (labels : Label width → Λ) (exit : Option Λ) | |
| (program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (σ × Option Bool)) | |
| (atLabels : ∀ l, program (labels l) = instruction width placement coefficient offsets labels exit l) | |
| (base : K → List Bool) (suffixes : Fin width → List Bool) | |
| (tableWord : base (placement (.inl 0)) = encodeWords values) | |
| (scratchEmpty : base (placement (.inl 3)) = []) | |
| (sourceWords : ∀ i, base (placement (.inr (.inl i))) = encodeWord (indices i) ++ suffixes i) | |
| (ambient : σ) (register : Option Bool) : | |
| (advance (TM2.step program))^[steps values coefficient width indices offsets] | |
| (some ⟨some (labels (entry width)), (ambient, register), base⟩) = | |
| some ⟨exit, (ambient, none), | |
| finalTapes values coefficient width placement indices offsets fields base⟩ := by | |
| induction width generalizing base register with | |
| | zero => | |
| change some (TM2.stepAux (program (labels ())) _ _) = _ | |
| erw [atLabels] | |
| cases exit <;> simp [instruction, TM2.stepAux, finalTapes, | |
| MaxCutGames.Reduction.MachineTransfer.exitAt] | |
| | succ width ih => | |
| have hd (a b : Tape (width + 1)) (hne : a ≠ b) : placement a ≠ placement b := | |
| fun h => hne (distinct h) | |
| have outside : ∀ i : Fin 5, | |
| placement (.inr (.inl 0)) ≠ (placement ∘ lookupRole width) i := by | |
| intro i | |
| apply hd | |
| fin_cases i <;> simp [lookupRole] | |
| have first := MachineAffineLookup.affineLookupTrace (placement (.inr (.inl 0))) | |
| (placement ∘ lookupRole width) (distinct.comp (lookupRole_injective width)) outside | |
| coefficient (offsets 0) (fun l => labels (.inl l)) | |
| (some (labels (.inr (entry width)))) program (fun l => atLabels (.inl l)) | |
| base values tableWord scratchEmpty (indices 0) (suffixes 0) (sourceWords 0) | |
| (fields 0) (selected 0) ambient register | |
| let mid := MachineAffineLookup.finalTapes (placement ∘ lookupRole width) base values | |
| (coefficient * indices 0 + offsets 0) (fields 0) | |
| have frame (k : Tape (width + 1)) (h1 : k ≠ .inl 1) (h2 : k ≠ .inl 2) | |
| (h3 : k ≠ .inr (.inr 0)) : mid (placement k) = base (placement k) := | |
| MachineAffineLookup.finalTapes_other _ _ _ _ _ _ (hd _ _ h1) (hd _ _ h2) (hd _ _ h3) | |
| have middleTable : mid (placement (.inl 0)) = encodeWords values := by | |
| rw [frame (.inl 0) (by simp) (by simp) (by simp)] | |
| exact tableWord | |
| have middleScratch : mid (placement (.inl 3)) = [] := by | |
| rw [frame (.inl 3) (by simp) (by simp) (by simp)] | |
| exact scratchEmpty | |
| have middleSources (i : Fin width) : mid (placement (.inr (.inl i.succ))) = | |
| encodeWord (indices i.succ) ++ suffixes i.succ := by | |
| rw [frame (.inr (.inl i.succ)) (by simp) (by simp) (by simp)] | |
| exact sourceWords i.succ | |
| have rest := ih (placement ∘ shift width) (distinct.comp (shift_injective width)) | |
| (fun i => indices i.succ) (fun i => offsets i.succ) (fun i => fields i.succ) | |
| (fun i => selected i.succ) (fun l => labels (.inr l)) | |
| (fun l => atLabels (.inr l)) mid (fun i => suffixes i.succ) | |
| middleTable middleScratch middleSources none | |
| simp only [entry] | |
| rw [steps, Nat.add_comm, Function.iterate_add_apply, first] | |
| exact rest | |
| /-- Every output is the physically gathered unary field, with its old suffix | |
| preserved. This identifies the operands consumed by the Horner phase. -/ | |
| theorem finalTapes_field (values : List Nat) (coefficient width : Nat) | |
| (placement : Tape width → K) (distinct : Function.Injective placement) | |
| (indices offsets fields : Fin width → Nat) (base : K → List Bool) (i : Fin width) : | |
| finalTapes values coefficient width placement indices offsets fields base | |
| (placement (.inr (.inr i))) = | |
| encodeWord (fields i) ++ base (placement (.inr (.inr i))) := by | |
| induction width generalizing base with | |
| | zero => exact Fin.elim0 i | |
| | succ width ih => | |
| have hd (a b : Tape (width + 1)) (hne : a ≠ b) : placement a ≠ placement b := | |
| fun h => hne (distinct h) | |
| let mid := MachineAffineLookup.finalTapes (placement ∘ lookupRole width) base values | |
| (coefficient * indices 0 + offsets 0) (fields 0) | |
| refine Fin.cases ?_ (fun j => ?_) i | |
| · change finalTapes values coefficient width (placement ∘ shift width) | |
| (fun j => indices j.succ) (fun j => offsets j.succ) (fun j => fields j.succ) | |
| mid (placement (.inr (.inr 0))) = _ | |
| rw [finalTapes_other values coefficient width _ _ _ _ mid _ | |
| (hd _ _ (by simp [shift])) (hd _ _ (by simp [shift])) | |
| (fun j => hd _ _ (by simp [shift, Fin.ext_iff]))] | |
| exact MachineAffineLookup.finalTapes_output _ _ _ _ _ | |
| · change finalTapes values coefficient width (placement ∘ shift width) | |
| (fun j => indices j.succ) (fun j => offsets j.succ) (fun j => fields j.succ) | |
| mid ((placement ∘ shift width) (.inr (.inr j))) = _ | |
| rw [ih (placement ∘ shift width) (distinct.comp (shift_injective width)) | |
| (fun j => indices j.succ) (fun j => offsets j.succ) (fun j => fields j.succ) mid j] | |
| congr 1 | |
| exact MachineAffineLookup.finalTapes_other (placement ∘ lookupRole width) base values | |
| (coefficient * indices 0 + offsets 0) (fields 0) | |
| (placement (.inr (.inr j.succ))) | |
| (hd _ _ (by simp [lookupRole])) (hd _ _ (by simp [lookupRole])) | |
| (hd _ _ (by simp [lookupRole, Fin.ext_iff])) | |
| /-- For fixed width, gather time is linear in the encoded table size plus | |
| coordinate magnitudes. Query and scan debris is never rescanned. -/ | |
| theorem steps_le (values : List Nat) (coefficient width magnitude : Nat) | |
| (indices offsets fields : Fin width → Nat) | |
| (selected : ∀ i, values[coefficient * indices i + offsets i]? = some (fields i)) | |
| (bounded : ∀ i, indices i ≤ magnitude) : | |
| steps values coefficient width indices offsets ≤ | |
| width * (2 * magnitude + 5 * (encodeWords values).length + 6) + 1 := by | |
| induction width with | |
| | zero => simp [steps] | |
| | succ width ih => | |
| have first := MachineAffineLookup.steps_le values (indices 0) coefficient (offsets 0) | |
| (fields 0) (selected 0) | |
| have rest := ih (fun i => indices i.succ) (fun i => offsets i.succ) | |
| (fun i => fields i.succ) (fun i => selected i.succ) (fun i => bounded i.succ) | |
| have hb := bounded 0 | |
| rw [steps, Nat.add_mul, Nat.one_mul] | |
| omega | |
| end MaxCutGames.Explicit.MachineProductGather | |
| /-! | |
| Counted loops in one actual TM2 program. A unary guard is executed once before | |
| each caller body and once more at zero. The caller supplies only individual | |
| body traces, each returning to the guard with the same counter remainder. | |
| Schedules are indexed by the remaining count: body `r` takes ambient state | |
| `r + 1` to ambient state `r`. Consequently the run starts at index `n` and | |
| ends at index zero. Bodies may change the ambient state and unprotected tapes. | |
| -/ | |
| namespace MaxCutGames.Foundations.Complexity.MachineCountedLoop | |
| open Turing | |
| open scoped BigOperators | |
| open MachineUnaryCounter | |
| variable {K Λ σ : Type} [DecidableEq K] | |
| def guardConfiguration (counter : K) (guardLabel : Λ) (suffix : List Bool) | |
| (ambient : Nat → σ) (register : Nat → Option Bool) | |
| (base : Nat → K → List Bool) (n : Nat) : | |
| TM2.Cfg (Alphabet (K := K)) Λ (σ × Option Bool) := | |
| ⟨some guardLabel, (ambient n, register n), counterTapes counter (base n) n suffix⟩ | |
| def bodyConfiguration (counter : K) (bodyLabel : Λ) (suffix : List Bool) | |
| (ambient : Nat → σ) (base : Nat → K → List Bool) (n : Nat) : | |
| TM2.Cfg (Alphabet (K := K)) Λ (σ × Option Bool) := | |
| ⟨some bodyLabel, (ambient (n + 1), none), counterTapes counter (base (n + 1)) n suffix⟩ | |
| def exitConfiguration (counter : K) (exitLabel : Λ) (suffix : List Bool) | |
| (ambient : Nat → σ) (base : Nat → K → List Bool) : | |
| TM2.Cfg (Alphabet (K := K)) Λ (σ × Option Bool) := | |
| ⟨some exitLabel, (ambient 0, none), counterTapes counter (base 0) 0 suffix⟩ | |
| /-- The hypotheses are actual traces of the caller's body in the same program. | |
| Both endpoints have the same unary remainder and unread suffix. -/ | |
| def BodyTraces (counter : K) (guardLabel bodyLabel : Λ) | |
| (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) | |
| (suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool) | |
| (base : Nat → K → List Bool) (cost : Nat → Nat) (n : Nat) : Prop := | |
| ∀ r, r < n → | |
| (MachineComposition.advance (TM2.step program))^[cost r] | |
| (some (bodyConfiguration counter bodyLabel suffix ambient base r)) = | |
| some (guardConfiguration counter guardLabel suffix ambient register base r) | |
| /-- Exactly `n` supplied body blocks and `n + 1` concrete guard transitions. -/ | |
| def totalSteps (cost : Nat → Nat) (n : Nat) : Nat := | |
| (∑ r ∈ Finset.range n, cost r) + n + 1 | |
| @[simp] theorem totalSteps_zero (cost : Nat → Nat) : totalSteps cost 0 = 1 := by | |
| simp [totalSteps] | |
| theorem totalSteps_succ (cost : Nat → Nat) (n : Nat) : | |
| totalSteps cost (n + 1) = (totalSteps cost n + cost n) + 1 := by | |
| simp only [totalSteps, Finset.sum_range_succ] | |
| omega | |
| /-- The loop trace is derived by composing each actual unary guard transition | |
| with one supplied body trace. No whole-loop execution is assumed. -/ | |
| theorem loopTrace (counter : K) (guardLabel bodyLabel exitLabel : Λ) | |
| (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) | |
| (atGuard : program guardLabel = guard counter bodyLabel exitLabel) | |
| (suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool) | |
| (base : Nat → K → List Bool) (cost : Nat → Nat) (n : Nat) | |
| (bodyTraces : BodyTraces counter guardLabel bodyLabel program suffix | |
| ambient register base cost n) : | |
| (MachineComposition.advance (TM2.step program))^[totalSteps cost n] | |
| (some (guardConfiguration counter guardLabel suffix ambient register base n)) = | |
| some (exitConfiguration counter exitLabel suffix ambient base) := by | |
| revert bodyTraces | |
| induction n with | |
| | zero => | |
| intro _ | |
| simpa only [totalSteps_zero, guardConfiguration, exitConfiguration] using | |
| guardTrace_zero counter guardLabel bodyLabel exitLabel program atGuard | |
| (base 0) suffix (ambient 0) (register 0) | |
| | succ n ih => | |
| intro bodyTraces | |
| rw [totalSteps_succ, Function.iterate_succ_apply] | |
| change (MachineComposition.advance (TM2.step program))^[totalSteps cost n + cost n] | |
| (TM2.step program | |
| ⟨some guardLabel, (ambient (n + 1), register (n + 1)), | |
| counterTapes counter (base (n + 1)) (n + 1) suffix⟩) = _ | |
| rw [guardStep_succ counter guardLabel bodyLabel exitLabel program atGuard | |
| (base (n + 1)) n suffix (ambient (n + 1)) (register (n + 1)), | |
| Function.iterate_add_apply] | |
| change (MachineComposition.advance (TM2.step program))^[totalSteps cost n] | |
| ((MachineComposition.advance (TM2.step program))^[cost n] | |
| (some (bodyConfiguration counter bodyLabel suffix ambient base n))) = _ | |
| rw [bodyTraces n (Nat.lt_succ_self n)] | |
| exact ih (fun r hr => bodyTraces r (Nat.lt_trans hr (Nat.lt_succ_self n))) | |
| theorem totalSteps_le (cost : Nat → Nat) (n B : Nat) | |
| (bodyBound : ∀ r, r < n → cost r ≤ B) : | |
| totalSteps cost n ≤ n * (B + 1) + 1 := by | |
| have hsum : (∑ r ∈ Finset.range n, cost r) ≤ n * B := by | |
| calc | |
| (∑ r ∈ Finset.range n, cost r) ≤ ∑ _r ∈ Finset.range n, B := | |
| Finset.sum_le_sum (fun r hr => bodyBound r (Finset.mem_range.mp hr)) | |
| _ = n * B := by simp | |
| simpa only [totalSteps, Nat.mul_add, Nat.mul_one] using | |
| Nat.add_le_add_right (Nat.add_le_add_right hsum n) 1 | |
| /-- An actual timed execution witness with the exact composed step count. -/ | |
| def loopInTime (counter : K) (guardLabel bodyLabel exitLabel : Λ) | |
| (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) | |
| (atGuard : program guardLabel = guard counter bodyLabel exitLabel) | |
| (suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool) | |
| (base : Nat → K → List Bool) (cost : Nat → Nat) (n B : Nat) | |
| (bodyTraces : BodyTraces counter guardLabel bodyLabel program suffix | |
| ambient register base cost n) | |
| (bodyBound : ∀ r, r < n → cost r ≤ B) : | |
| StateTransition.EvalsToInTime (TM2.step program) | |
| (guardConfiguration counter guardLabel suffix ambient register base n) | |
| (some (exitConfiguration counter exitLabel suffix ambient base)) | |
| (n * (B + 1) + 1) where | |
| steps := totalSteps cost n | |
| evals_in_steps := by | |
| change (MachineComposition.advance (TM2.step program))^[totalSteps cost n] _ = _ | |
| exact loopTrace counter guardLabel bodyLabel exitLabel program atGuard suffix | |
| ambient register base cost n bodyTraces | |
| steps_le_m := totalSteps_le cost n B bodyBound | |
| omit [DecidableEq K] in | |
| /-- Chaining the caller's declared per-body frame equalities. -/ | |
| theorem baseFrame (base : Nat → K → List Bool) (frame : K → Prop) (n : Nat) | |
| (bodyFrame : ∀ r, r < n → ∀ k, frame k → base (r + 1) k = base r k) : | |
| ∀ k, frame k → base n k = base 0 k := by | |
| revert bodyFrame | |
| induction n with | |
| | zero => intro _ k _; rfl | |
| | succ n ih => | |
| intro bodyFrame k hk | |
| exact (bodyFrame n (Nat.lt_succ_self n) k hk).trans | |
| (ih (fun r hr => bodyFrame r (Nat.lt_trans hr (Nat.lt_succ_self n))) k hk) | |
| /-- Protected tapes are unchanged by the complete loop, including its guard | |
| steps. The counter itself is explicitly excluded from this frame. -/ | |
| theorem counterFrame (counter : K) (suffix : List Bool) | |
| (base : Nat → K → List Bool) (frame : K → Prop) (n : Nat) | |
| (counterOutside : ∀ k, frame k → k ≠ counter) | |
| (bodyFrame : ∀ r, r < n → ∀ k, frame k → base (r + 1) k = base r k) : | |
| ∀ k, frame k → | |
| counterTapes counter (base n) n suffix k = counterTapes counter (base 0) 0 suffix k := by | |
| intro k hk | |
| rw [counterTapes_other counter k (counterOutside k hk), | |
| counterTapes_other counter k (counterOutside k hk)] | |
| exact baseFrame base frame n bodyFrame k hk | |
| /-- Actual loop execution and preservation of an arbitrary declared tape frame. -/ | |
| theorem loopTrace_framed (counter : K) (guardLabel bodyLabel exitLabel : Λ) | |
| (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (σ × Option Bool)) | |
| (atGuard : program guardLabel = guard counter bodyLabel exitLabel) | |
| (suffix : List Bool) (ambient : Nat → σ) (register : Nat → Option Bool) | |
| (base : Nat → K → List Bool) (cost : Nat → Nat) (n : Nat) | |
| (bodyTraces : BodyTraces counter guardLabel bodyLabel program suffix | |
| ambient register base cost n) | |
| (frame : K → Prop) (counterOutside : ∀ k, frame k → k ≠ counter) | |
| (bodyFrame : ∀ r, r < n → ∀ k, frame k → base (r + 1) k = base r k) : | |
| ((MachineComposition.advance (TM2.step program))^[totalSteps cost n] | |
| (some (guardConfiguration counter guardLabel suffix ambient register base n)) = | |
| some (exitConfiguration counter exitLabel suffix ambient base)) ∧ | |
| (∀ k, frame k → | |
| (guardConfiguration counter guardLabel suffix ambient register base n).stk k = | |
| (exitConfiguration counter exitLabel suffix ambient base).stk k) := | |
| ⟨loopTrace counter guardLabel bodyLabel exitLabel program atGuard suffix | |
| ambient register base cost n bodyTraces, | |
| counterFrame counter suffix base frame n counterOutside bodyFrame⟩ | |
| end MaxCutGames.Foundations.Complexity.MachineCountedLoop | |
| end OAI | |
Xet Storage Details
- Size:
- 39.6 kB
- Xet hash:
- 760f624a1fbecac64d4c074d36c8f0f722ffa8092312879a459e425af74f898f
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.