import Mathlib import OAI.Computability.MaxCut.Machines.CompareLoop import OAI.Computability.MaxCut.Machines.MachineCloudPrefix import OAI.Computability.MaxCut.PCP.ExpanderRowControl namespace OAI namespace MaxCutGames.Foundations.Complexity.MachineFixedDivMod open Turing open Reduction.MachineSubstitution (pushWord stepAux_pushWord statementPushBound_pushWord) variable {K Λ σ : Type} [DecidableEq K] abbrev Alphabet (_ : K) := Bool abbrev State (σ : Type) (d : Nat) := (σ × Fin d) × Option Bool def residue (d : Nat) (positive : 0 < d) (n : Nat) : Fin d := ⟨n % d, Nat.mod_lt n positive⟩ def nextResidue (d : Nat) (positive : 0 < d) (r : Fin d) : Fin d := residue d positive (r.val + 1) theorem nextResidue_residue (d : Nat) (positive : 0 < d) (n : Nat) : nextResidue d positive (residue d positive n) = residue d positive (n + 1) := by apply Fin.ext change (n % d + 1) % d = (n + 1) % d simp only [Nat.add_mod, Nat.mod_mod] def scanLoop (d : Nat) (positive : 0 < d) (source quotient : K) (scanLabel : Λ) (emitterLabel : Fin d → Λ) : TM2.Stmt (Alphabet (K := K)) Λ (State σ d) := .pop source (fun state head => (state.1, head)) (.branch (fun state => state.2.getD false) (.branch (fun state => decide ((nextResidue d positive state.1.2).val = 0)) (.push quotient (fun _ => true) (.load (fun state => ((state.1.1, nextResidue d positive state.1.2), state.2)) (.goto fun _ => scanLabel))) (.load (fun state => ((state.1.1, nextResidue d positive state.1.2), state.2)) (.goto fun _ => scanLabel))) (.push source (fun _ => false) (.goto fun state => emitterLabel state.1.2))) /-- Each emitter is a fixed finite statement indexed by one of the `d` residues. -/ def emitter (d : Nat) (positive : 0 < d) (remainder : K) (exit : Option Λ) (r : Fin d) : TM2.Stmt (Alphabet (K := K)) Λ (State σ d) := pushWord remainder (List.replicate r.val true) (.load (fun state => ((state.1.1, residue d positive 0), none)) (Reduction.MachineTransfer.exitAt remainder exit)) omit [DecidableEq K] in theorem scanPushBound (d : Nat) (positive : 0 < d) (source quotient : K) (scanLabel : Λ) (emitterLabel : Fin d → Λ) : Runtime.statementPushBound (scanLoop (σ := σ) d positive source quotient scanLabel emitterLabel) = 1 := rfl omit [DecidableEq K] in theorem emitterPushBound (d : Nat) (positive : 0 < d) (remainder : K) (exit : Option Λ) (r : Fin d) : Runtime.statementPushBound (emitter (σ := σ) d positive remainder exit r) = r.val := by cases exit <;> simp [emitter, statementPushBound_pushWord, Runtime.statementPushBound, Reduction.MachineTransfer.exitAt] /-- Three designated tapes, with the complete surrounding tape family retained. -/ abbrev tapes (source quotient remainder : K) (base : K → List Bool) (input quotientWord remainderWord : List Bool) : K → List Bool := MachineCopy.forkTapes source quotient remainder base input quotientWord remainderWord def unaryTapes (source quotient remainder : K) (base : K → List Bool) (n q r : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) : K → List Bool := tapes source quotient remainder base (encodeWord n ++ sourceSuffix) (encodeWord q ++ quotientSuffix) (encodeWord r ++ remainderSuffix) @[simp] theorem unaryTapes_source (source quotient remainder : K) (sourceQuotient : source ≠ quotient) (sourceRemainder : source ≠ remainder) (base : K → List Bool) (n q r : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) : unaryTapes source quotient remainder base n q r sourceSuffix quotientSuffix remainderSuffix source = encodeWord n ++ sourceSuffix := by simp [unaryTapes, sourceQuotient, sourceRemainder] @[simp] theorem unaryTapes_quotient (source quotient remainder : K) (quotientRemainder : quotient ≠ remainder) (base : K → List Bool) (n q r : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) : unaryTapes source quotient remainder base n q r sourceSuffix quotientSuffix remainderSuffix quotient = encodeWord q ++ quotientSuffix := by simp [unaryTapes, quotientRemainder] @[simp] theorem unaryTapes_remainder (source quotient remainder : K) (base : K → List Bool) (n q r : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) : unaryTapes source quotient remainder base n q r sourceSuffix quotientSuffix remainderSuffix remainder = encodeWord r ++ remainderSuffix := by simp [unaryTapes] theorem unaryTapes_other (source quotient remainder other : K) (notSource : other ≠ source) (notQuotient : other ≠ quotient) (notRemainder : other ≠ remainder) (base : K → List Bool) (n q r : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) : unaryTapes source quotient remainder base n q r sourceSuffix quotientSuffix remainderSuffix other = base other := by simp [unaryTapes, tapes, MachineCopy.forkTapes, notSource, notQuotient, notRemainder] private theorem update_source_inline_MachineFixedDivMod (source quotient remainder : K) (sourceQuotient : source ≠ quotient) (sourceRemainder : source ≠ remainder) (quotientRemainder : quotient ≠ remainder) (base : K → List Bool) (input quotientWord remainderWord replacement : List Bool) : Function.update (tapes source quotient remainder base input quotientWord remainderWord) source replacement = tapes source quotient remainder base replacement quotientWord remainderWord := by funext k by_cases hs : k = source · subst k simp [tapes, MachineCopy.forkTapes, sourceQuotient, sourceRemainder] · by_cases hq : k = quotient · subst k simp [tapes, MachineCopy.forkTapes, Ne.symm sourceQuotient, quotientRemainder] · by_cases hr : k = remainder · subst k simp [tapes, MachineCopy.forkTapes, Ne.symm sourceRemainder] · simp [tapes, MachineCopy.forkTapes, hs, hq, hr] private theorem update_quotient_inline_MachineFixedDivMod (source quotient remainder : K) (quotientRemainder : quotient ≠ remainder) (base : K → List Bool) (input quotientWord remainderWord replacement : List Bool) : Function.update (tapes source quotient remainder base input quotientWord remainderWord) quotient replacement = tapes source quotient remainder base input replacement remainderWord := by funext k by_cases hq : k = quotient · subst k simp [tapes, MachineCopy.forkTapes, quotientRemainder] · by_cases hr : k = remainder · subst k simp [tapes, MachineCopy.forkTapes, Ne.symm quotientRemainder] · simp [tapes, MachineCopy.forkTapes, hq, hr] private theorem update_remainder_inline_MachineFixedDivMod (source quotient remainder : K) (base : K → List Bool) (input quotientWord remainderWord replacement : List Bool) : Function.update (tapes source quotient remainder base input quotientWord remainderWord) remainder replacement = tapes source quotient remainder base input quotientWord replacement := by simp [tapes, MachineCopy.forkTapes] theorem scanAux_true (d : Nat) (positive : 0 < d) (source quotient remainder : K) (sourceQuotient : source ≠ quotient) (sourceRemainder : source ≠ remainder) (quotientRemainder : quotient ≠ remainder) (scanLabel : Λ) (emitterLabel : Fin d → Λ) (base : K → List Bool) (input quotientWord remainderWord : List Bool) (ambient : σ) (r : Fin d) (register : Option Bool) : TM2.stepAux (scanLoop d positive source quotient scanLabel emitterLabel) ((ambient, r), register) (tapes source quotient remainder base (true :: input) quotientWord remainderWord) = ⟨some scanLabel, ((ambient, nextResidue d positive r), some true), tapes source quotient remainder base input (if (nextResidue d positive r).val = 0 then true :: quotientWord else quotientWord) remainderWord⟩ := by by_cases hcarry : (nextResidue d positive r).val = 0 <;> simp [scanLoop, TM2.stepAux, sourceQuotient, sourceRemainder, quotientRemainder, update_source_inline_MachineFixedDivMod, update_quotient_inline_MachineFixedDivMod, hcarry] theorem scanAux_false (d : Nat) (positive : 0 < d) (source quotient remainder : K) (sourceQuotient : source ≠ quotient) (sourceRemainder : source ≠ remainder) (quotientRemainder : quotient ≠ remainder) (scanLabel : Λ) (emitterLabel : Fin d → Λ) (base : K → List Bool) (input quotientWord remainderWord : List Bool) (ambient : σ) (r : Fin d) (register : Option Bool) : TM2.stepAux (scanLoop d positive source quotient scanLabel emitterLabel) ((ambient, r), register) (tapes source quotient remainder base (false :: input) quotientWord remainderWord) = ⟨some (emitterLabel r), ((ambient, r), some false), tapes source quotient remainder base (false :: input) quotientWord remainderWord⟩ := by simp [scanLoop, TM2.stepAux, sourceQuotient, sourceRemainder, quotientRemainder, update_source_inline_MachineFixedDivMod] /-- A scan transition updates the actual quotient and finite residue of the already-processed count. The count itself is a proof index, not machine state. -/ theorem scanStep_succ (d : Nat) (positive : 0 < d) (source quotient remainder : K) (sourceQuotient : source ≠ quotient) (sourceRemainder : source ≠ remainder) (quotientRemainder : quotient ≠ remainder) (scanLabel : Λ) (emitterLabel : Fin d → Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ d)) (atScan : program scanLabel = scanLoop d positive source quotient scanLabel emitterLabel) (base : K → List Bool) (n k : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) (ambient : σ) (register : Option Bool) : TM2.step program ⟨some scanLabel, ((ambient, residue d positive k), register), unaryTapes source quotient remainder base (n + 1) (k / d) 0 sourceSuffix quotientSuffix remainderSuffix⟩ = some ⟨some scanLabel, ((ambient, residue d positive (k + 1)), some true), unaryTapes source quotient remainder base n ((k + 1) / d) 0 sourceSuffix quotientSuffix remainderSuffix⟩ := by change some (TM2.stepAux (program scanLabel) ((ambient, residue d positive k), register) (unaryTapes source quotient remainder base (n + 1) (k / d) 0 sourceSuffix quotientSuffix remainderSuffix)) = _ rw [atScan] simp only [unaryTapes, encodeWord, List.replicate_succ, List.cons_append] rw [scanAux_true d positive source quotient remainder sourceQuotient sourceRemainder quotientRemainder, nextResidue_residue] by_cases hmod : (k + 1) % d = 0 · rw [Nat.succ_div_of_mod_eq_zero hmod] simp [residue, hmod, List.replicate_succ] · rw [Nat.succ_div_of_mod_ne_zero hmod] simp [residue, hmod] theorem scanStep_zero (d : Nat) (positive : 0 < d) (source quotient remainder : K) (sourceQuotient : source ≠ quotient) (sourceRemainder : source ≠ remainder) (quotientRemainder : quotient ≠ remainder) (scanLabel : Λ) (emitterLabel : Fin d → Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ d)) (atScan : program scanLabel = scanLoop d positive source quotient scanLabel emitterLabel) (base : K → List Bool) (q : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) (ambient : σ) (r : Fin d) (register : Option Bool) : TM2.step program ⟨some scanLabel, ((ambient, r), register), unaryTapes source quotient remainder base 0 q 0 sourceSuffix quotientSuffix remainderSuffix⟩ = some ⟨some (emitterLabel r), ((ambient, r), some false), unaryTapes source quotient remainder base 0 q 0 sourceSuffix quotientSuffix remainderSuffix⟩ := by change some (TM2.stepAux (program scanLabel) ((ambient, r), register) (unaryTapes source quotient remainder base 0 q 0 sourceSuffix quotientSuffix remainderSuffix)) = _ rw [atScan] simp only [unaryTapes, encodeWord, List.replicate_zero, List.nil_append, List.singleton_append] rw [scanAux_false d positive source quotient remainder sourceQuotient sourceRemainder quotientRemainder] theorem emitterStep (d : Nat) (positive : 0 < d) (source quotient remainder : K) (emitterLabel : Fin d → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ d)) (atEmitter : ∀ r, program (emitterLabel r) = emitter d positive remainder exit r) (base : K → List Bool) (n q : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) (ambient : σ) (r : Fin d) (register : Option Bool) : TM2.step program ⟨some (emitterLabel r), ((ambient, r), register), unaryTapes source quotient remainder base n q 0 sourceSuffix quotientSuffix remainderSuffix⟩ = some ⟨exit, ((ambient, residue d positive 0), none), unaryTapes source quotient remainder base n q r.val sourceSuffix quotientSuffix remainderSuffix⟩ := by change some (TM2.stepAux (program (emitterLabel r)) ((ambient, r), register) (unaryTapes source quotient remainder base n q 0 sourceSuffix quotientSuffix remainderSuffix)) = _ rw [atEmitter] unfold emitter rw [stepAux_pushWord] simp only [unaryTapes, MachineCopy.forkTapes_right, List.reverse_replicate] rw [update_remainder_inline_MachineFixedDivMod] cases exit <;> simp [TM2.stepAux, Reduction.MachineTransfer.exitAt, encodeWord, List.append_assoc] /-- The general invariant starts at the finite residue and unary quotient of `k`, then scans `n` additional input bits. It is an actual execution theorem. -/ theorem trace_fromCount (d : Nat) (positive : 0 < d) (source quotient remainder : K) (sourceQuotient : source ≠ quotient) (sourceRemainder : source ≠ remainder) (quotientRemainder : quotient ≠ remainder) (scanLabel : Λ) (emitterLabel : Fin d → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ d)) (atScan : program scanLabel = scanLoop d positive source quotient scanLabel emitterLabel) (atEmitter : ∀ r, program (emitterLabel r) = emitter d positive remainder exit r) (base : K → List Bool) (n k : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) (ambient : σ) (register : Option Bool) : (MachineComposition.advance (TM2.step program))^[n + 2] (some ⟨some scanLabel, ((ambient, residue d positive k), register), unaryTapes source quotient remainder base n (k / d) 0 sourceSuffix quotientSuffix remainderSuffix⟩) = some ⟨exit, ((ambient, residue d positive 0), none), unaryTapes source quotient remainder base 0 ((k + n) / d) ((k + n) % d) sourceSuffix quotientSuffix remainderSuffix⟩ := by induction n generalizing k register with | zero => change (TM2.step program ⟨some scanLabel, ((ambient, residue d positive k), register), unaryTapes source quotient remainder base 0 (k / d) 0 sourceSuffix quotientSuffix remainderSuffix⟩).bind (TM2.step program) = _ rw [scanStep_zero d positive source quotient remainder sourceQuotient sourceRemainder quotientRemainder scanLabel emitterLabel program atScan, Option.bind_some] simpa only [residue, Nat.add_zero] using emitterStep d positive source quotient remainder emitterLabel exit program atEmitter base 0 (k / d) sourceSuffix quotientSuffix remainderSuffix ambient (residue d positive k) (some false) | succ n ih => rw [Function.iterate_succ_apply] change (MachineComposition.advance (TM2.step program))^[n + 2] (TM2.step program ⟨some scanLabel, ((ambient, residue d positive k), register), unaryTapes source quotient remainder base (n + 1) (k / d) 0 sourceSuffix quotientSuffix remainderSuffix⟩) = _ rw [scanStep_succ d positive source quotient remainder sourceQuotient sourceRemainder quotientRemainder scanLabel emitterLabel program atScan] simpa only [Nat.add_assoc, Nat.add_comm, Nat.add_left_comm] using ih (k + 1) (some true) /-- Exact unary quotient and remainder, with both output counters initially zero. -/ theorem divModTrace (d : Nat) (positive : 0 < d) (source quotient remainder : K) (sourceQuotient : source ≠ quotient) (sourceRemainder : source ≠ remainder) (quotientRemainder : quotient ≠ remainder) (scanLabel : Λ) (emitterLabel : Fin d → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ d)) (atScan : program scanLabel = scanLoop d positive source quotient scanLabel emitterLabel) (atEmitter : ∀ r, program (emitterLabel r) = emitter d positive remainder exit r) (base : K → List Bool) (n : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) (ambient : σ) (register : Option Bool) : (MachineComposition.advance (TM2.step program))^[n + 2] (some ⟨some scanLabel, ((ambient, residue d positive 0), register), unaryTapes source quotient remainder base n 0 0 sourceSuffix quotientSuffix remainderSuffix⟩) = some ⟨exit, ((ambient, residue d positive 0), none), unaryTapes source quotient remainder base 0 (n / d) (n % d) sourceSuffix quotientSuffix remainderSuffix⟩ := by simpa only [Nat.zero_add, Nat.zero_div] using trace_fromCount d positive source quotient remainder sourceQuotient sourceRemainder quotientRemainder scanLabel emitterLabel exit program atScan atEmitter base n 0 sourceSuffix quotientSuffix remainderSuffix ambient register /-- Caller-frame form: the only initial tape hypotheses are the three unary prefixes. -/ theorem divModFromTapes (d : Nat) (positive : 0 < d) (source quotient remainder : K) (sourceQuotient : source ≠ quotient) (sourceRemainder : source ≠ remainder) (quotientRemainder : quotient ≠ remainder) (scanLabel : Λ) (emitterLabel : Fin d → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ d)) (atScan : program scanLabel = scanLoop d positive source quotient scanLabel emitterLabel) (atEmitter : ∀ r, program (emitterLabel r) = emitter d positive remainder exit r) (base : K → List Bool) (n : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) (sourceInput : base source = encodeWord n ++ sourceSuffix) (quotientInput : base quotient = encodeWord 0 ++ quotientSuffix) (remainderInput : base remainder = encodeWord 0 ++ remainderSuffix) (ambient : σ) (register : Option Bool) : (MachineComposition.advance (TM2.step program))^[n + 2] (some ⟨some scanLabel, ((ambient, residue d positive 0), register), base⟩) = some ⟨exit, ((ambient, residue d positive 0), none), unaryTapes source quotient remainder base 0 (n / d) (n % d) sourceSuffix quotientSuffix remainderSuffix⟩ := by have hbase : unaryTapes source quotient remainder base n 0 0 sourceSuffix quotientSuffix remainderSuffix = base := by simp only [unaryTapes, ← sourceInput, ← quotientInput, ← remainderInput, MachineCopy.forkTapes_self] have h := divModTrace d positive source quotient remainder sourceQuotient sourceRemainder quotientRemainder scanLabel emitterLabel exit program atScan atEmitter base n sourceSuffix quotientSuffix remainderSuffix ambient register rw [hbase] at h exact h def divModInTime (d : Nat) (positive : 0 < d) (source quotient remainder : K) (sourceQuotient : source ≠ quotient) (sourceRemainder : source ≠ remainder) (quotientRemainder : quotient ≠ remainder) (scanLabel : Λ) (emitterLabel : Fin d → Λ) (exit : Option Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ d)) (atScan : program scanLabel = scanLoop d positive source quotient scanLabel emitterLabel) (atEmitter : ∀ r, program (emitterLabel r) = emitter d positive remainder exit r) (base : K → List Bool) (n : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) (sourceInput : base source = encodeWord n ++ sourceSuffix) (quotientInput : base quotient = encodeWord 0 ++ quotientSuffix) (remainderInput : base remainder = encodeWord 0 ++ remainderSuffix) (ambient : σ) (register : Option Bool) : StateTransition.EvalsToInTime (TM2.step program) ⟨some scanLabel, ((ambient, residue d positive 0), register), base⟩ (some ⟨exit, ((ambient, residue d positive 0), none), unaryTapes source quotient remainder base 0 (n / d) (n % d) sourceSuffix quotientSuffix remainderSuffix⟩) (n + 2) where steps := n + 2 evals_in_steps := divModFromTapes d positive source quotient remainder sourceQuotient sourceRemainder quotientRemainder scanLabel emitterLabel exit program atScan atEmitter base n sourceSuffix quotientSuffix remainderSuffix sourceInput quotientInput remainderInput ambient register steps_le_m := Nat.le_refl _ /-- Stop just before emission, retaining the final finite residue as actual control. -/ theorem scanTrace_fromCount (d : Nat) (positive : 0 < d) (source quotient remainder : K) (sourceQuotient : source ≠ quotient) (sourceRemainder : source ≠ remainder) (quotientRemainder : quotient ≠ remainder) (scanLabel : Λ) (emitterLabel : Fin d → Λ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ d)) (atScan : program scanLabel = scanLoop d positive source quotient scanLabel emitterLabel) (base : K → List Bool) (n k : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) (ambient : σ) (register : Option Bool) : (MachineComposition.advance (TM2.step program))^[n + 1] (some ⟨some scanLabel, ((ambient, residue d positive k), register), unaryTapes source quotient remainder base n (k / d) 0 sourceSuffix quotientSuffix remainderSuffix⟩) = some ⟨some (emitterLabel (residue d positive (k + n))), ((ambient, residue d positive (k + n)), some false), unaryTapes source quotient remainder base 0 ((k + n) / d) 0 sourceSuffix quotientSuffix remainderSuffix⟩ := by induction n generalizing k register with | zero => simpa only [Nat.zero_add, Nat.add_zero, Function.iterate_one, MachineComposition.advance_some] using scanStep_zero d positive source quotient remainder sourceQuotient sourceRemainder quotientRemainder scanLabel emitterLabel program atScan base (k / d) sourceSuffix quotientSuffix remainderSuffix ambient (residue d positive k) register | succ n ih => rw [Function.iterate_succ_apply] change (MachineComposition.advance (TM2.step program))^[n + 1] (TM2.step program ⟨some scanLabel, ((ambient, residue d positive k), register), unaryTapes source quotient remainder base (n + 1) (k / d) 0 sourceSuffix quotientSuffix remainderSuffix⟩) = _ rw [scanStep_succ d positive source quotient remainder sourceQuotient sourceRemainder quotientRemainder scanLabel emitterLabel program atScan] simpa only [Nat.add_assoc, Nat.add_comm, Nat.add_left_comm] using ih (k + 1) (some true) /-- A fixed table on finite ambient control may retain the residue before local reset. -/ def emitterWithFinish [Fintype σ] (d : Nat) (positive : 0 < d) (remainder : K) (exit : Option Λ) (finish : σ → Fin d → σ) (r : Fin d) : TM2.Stmt (Alphabet (K := K)) Λ (State σ d) := pushWord remainder (List.replicate r.val true) (.load (fun state => ((finish state.1.1 r, residue d positive 0), none)) (Reduction.MachineTransfer.exitAt remainder exit)) omit [DecidableEq K] in theorem emitterWithFinishPushBound [Fintype σ] (d : Nat) (positive : 0 < d) (remainder : K) (exit : Option Λ) (finish : σ → Fin d → σ) (r : Fin d) : Runtime.statementPushBound (emitterWithFinish d positive remainder exit finish r) = r.val := by cases exit <;> simp [emitterWithFinish, statementPushBound_pushWord, Runtime.statementPushBound, Reduction.MachineTransfer.exitAt] theorem emitterWithFinishStep [Fintype σ] (d : Nat) (positive : 0 < d) (source quotient remainder : K) (emitterLabel : Fin d → Λ) (exit : Option Λ) (finish : σ → Fin d → σ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ d)) (atEmitter : ∀ r, program (emitterLabel r) = emitterWithFinish d positive remainder exit finish r) (base : K → List Bool) (n q : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) (ambient : σ) (r : Fin d) (register : Option Bool) : TM2.step program ⟨some (emitterLabel r), ((ambient, r), register), unaryTapes source quotient remainder base n q 0 sourceSuffix quotientSuffix remainderSuffix⟩ = some ⟨exit, ((finish ambient r, residue d positive 0), none), unaryTapes source quotient remainder base n q r.val sourceSuffix quotientSuffix remainderSuffix⟩ := by change some (TM2.stepAux (program (emitterLabel r)) ((ambient, r), register) (unaryTapes source quotient remainder base n q 0 sourceSuffix quotientSuffix remainderSuffix)) = _ rw [atEmitter] unfold emitterWithFinish rw [stepAux_pushWord] simp only [unaryTapes, MachineCopy.forkTapes_right, List.reverse_replicate] rw [update_remainder_inline_MachineFixedDivMod] cases exit <;> simp [TM2.stepAux, Reduction.MachineTransfer.exitAt, encodeWord, List.append_assoc] /-- The callback is a fixed finite-control table, applied in the existing emitter transition. -/ theorem divModTraceWithFinish [Fintype σ] (d : Nat) (positive : 0 < d) (source quotient remainder : K) (sourceQuotient : source ≠ quotient) (sourceRemainder : source ≠ remainder) (quotientRemainder : quotient ≠ remainder) (scanLabel : Λ) (emitterLabel : Fin d → Λ) (exit : Option Λ) (finish : σ → Fin d → σ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ d)) (atScan : program scanLabel = scanLoop d positive source quotient scanLabel emitterLabel) (atEmitter : ∀ r, program (emitterLabel r) = emitterWithFinish d positive remainder exit finish r) (base : K → List Bool) (n : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) (ambient : σ) (register : Option Bool) : (MachineComposition.advance (TM2.step program))^[n + 2] (some ⟨some scanLabel, ((ambient, residue d positive 0), register), unaryTapes source quotient remainder base n 0 0 sourceSuffix quotientSuffix remainderSuffix⟩) = some ⟨exit, ((finish ambient (residue d positive n), residue d positive 0), none), unaryTapes source quotient remainder base 0 (n / d) (n % d) sourceSuffix quotientSuffix remainderSuffix⟩ := by have hscan := scanTrace_fromCount d positive source quotient remainder sourceQuotient sourceRemainder quotientRemainder scanLabel emitterLabel program atScan base n 0 sourceSuffix quotientSuffix remainderSuffix ambient register simp only [Nat.zero_add, Nat.zero_div] at hscan rw [Function.iterate_succ_apply', hscan, MachineComposition.advance_some] exact emitterWithFinishStep d positive source quotient remainder emitterLabel exit finish program atEmitter base 0 (n / d) sourceSuffix quotientSuffix remainderSuffix ambient (residue d positive n) (some false) theorem divModFromTapesWithFinish [Fintype σ] (d : Nat) (positive : 0 < d) (source quotient remainder : K) (sourceQuotient : source ≠ quotient) (sourceRemainder : source ≠ remainder) (quotientRemainder : quotient ≠ remainder) (scanLabel : Λ) (emitterLabel : Fin d → Λ) (exit : Option Λ) (finish : σ → Fin d → σ) (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State σ d)) (atScan : program scanLabel = scanLoop d positive source quotient scanLabel emitterLabel) (atEmitter : ∀ r, program (emitterLabel r) = emitterWithFinish d positive remainder exit finish r) (base : K → List Bool) (n : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) (sourceInput : base source = encodeWord n ++ sourceSuffix) (quotientInput : base quotient = encodeWord 0 ++ quotientSuffix) (remainderInput : base remainder = encodeWord 0 ++ remainderSuffix) (ambient : σ) (register : Option Bool) : (MachineComposition.advance (TM2.step program))^[n + 2] (some ⟨some scanLabel, ((ambient, residue d positive 0), register), base⟩) = some ⟨exit, ((finish ambient (residue d positive n), residue d positive 0), none), unaryTapes source quotient remainder base 0 (n / d) (n % d) sourceSuffix quotientSuffix remainderSuffix⟩ := by have hbase : unaryTapes source quotient remainder base n 0 0 sourceSuffix quotientSuffix remainderSuffix = base := by simp only [unaryTapes, ← sourceInput, ← quotientInput, ← remainderInput, MachineCopy.forkTapes_self] have h := divModTraceWithFinish d positive source quotient remainder sourceQuotient sourceRemainder quotientRemainder scanLabel emitterLabel exit finish program atScan atEmitter base n sourceSuffix quotientSuffix remainderSuffix ambient register rw [hbase] at h exact h end MaxCutGames.Foundations.Complexity.MachineFixedDivMod namespace MaxCutGames.Foundations.Complexity.MachineExpanderRow open Turing open PCP.ExpanderTables PCP.ExpanderRowControl inductive Tape | inputVertex | table | output | emitScratch | queryReverse | queryIndex | lookupOutput | lookupWork | lookupRestore | quotientFirst | quotientSecond | remainderFirst | remainderSecond deriving DecidableEq protected abbrev Tape.enumList : List Tape := [.inputVertex, .table, .output, .emitScratch, .queryReverse, .queryIndex, .lookupOutput, .lookupWork, .lookupRestore, .quotientFirst, .quotientSecond, .remainderFirst, .remainderSecond] 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 inductive Label (d : Nat) | initialize | firstEmit (label : PCP.AlphabetTable.Emitter.Label 3 (degree d)) | firstReverse | firstLookup (label : MachinePreservingLookupClean.Label) | firstScan | firstResidue (r : Fin (degree d)) | clearQuery | secondEmit (label : PCP.AlphabetTable.Emitter.Label 3 (degree d)) | secondReverse | secondLookup (label : MachinePreservingLookupClean.Label) | secondScan | secondResidue (r : Fin (degree d)) | outputEmit (label : PCP.AlphabetTable.Emitter.Label 3 (rowFactor d)) | cleanup (i : Fin 6) | done deriving DecidableEq, Fintype abbrev Ambient (ρ : Type) (d : Nat) := ρ × Control d abbrev State (ρ : Type) (d : Nat) := PCP.AlphabetTable.Emitter.State (Ambient ρ d × Fin (degree d)) /-- The residue and the emitter's unit field are just permuted; no tape is read or written by this static change of finite state coordinates. -/ def divisionStates (ρ : Type) (d : Nat) : MachineFixedDivMod.State (Ambient ρ d × Unit) (degree d) ≃ State ρ d where toFun s := (((s.1.1.1, s.1.2), ()), s.2) invFun s := (((s.1.1.1, ()), s.1.1.2), s.2) left_inv := by rintro ⟨⟨⟨a, u⟩, r⟩, b⟩; cases u; rfl right_inv := by rintro ⟨⟨⟨a, r⟩, u⟩, b⟩; cases u; rfl def firstFinish {ρ : Type} {d : Nat} (s : Ambient ρ d × Unit) (r : Fin (degree d)) : Ambient ρ d × Unit := ((s.1.1, receiveFirst s.1.2 r), ()) def secondFinish {ρ : Type} {d : Nat} (H : Table (cloudSize d) d) (s : Ambient ρ d × Unit) (r : Fin (degree d)) : Ambient ρ d × Unit := ((s.1.1, receiveSecond H s.1.2 r), ()) def affinePlan {σ : Type} {bound : Nat} (coefficient : Nat) (offset : σ → Fin bound) : Fin 3 → PCP.AlphabetTable.Emitter.Command 1 σ bound := PCP.AlphabetTable.Emitter.listCommands (PCP.AlphabetTable.Emitter.affineCommands [(0, coefficient)] offset) def firstPlan (ρ : Type) (d : Nat) : Fin 3 → PCP.AlphabetTable.Emitter.Command 1 (Ambient ρ d × Fin (degree d)) (degree d) := affinePlan (degree d) (fun s => firstOffset s.1.2) def secondPlan (ρ : Type) (d : Nat) : Fin 3 → PCP.AlphabetTable.Emitter.Command 1 (Ambient ρ d × Fin (degree d)) (degree d) := affinePlan (degree d) (fun s => secondOffset s.1.2) def outputPlan (ρ : Type) (d : Nat) : Fin 3 → PCP.AlphabetTable.Emitter.Command 1 (Ambient ρ d × Fin (degree d)) (rowFactor d) := affinePlan (rowFactor d) (fun s => outputOffset s.1.2) def lookupTape : Fin 5 → Tape | ⟨0, _⟩ => .table | ⟨1, _⟩ => .queryIndex | ⟨2, _⟩ => .lookupWork | ⟨3, _⟩ => .lookupOutput | _ => .lookupRestore def dirtyTape : Fin 6 → Tape | ⟨0, _⟩ => .queryIndex | ⟨1, _⟩ => .lookupOutput | ⟨2, _⟩ => .quotientFirst | ⟨3, _⟩ => .quotientSecond | ⟨4, _⟩ => .remainderFirst | _ => .remainderSecond variable {K Λ ρ : Type} [DecidableEq K] [Fintype ρ] /-- Every branch is a concrete finite TM2 statement. The fixed table `H` is evaluated only on finite control values, never on an unbounded tape value. -/ def statement {d : Nat} (positive : 0 < d) (H : Table (cloudSize d) d) (ports : Tape → K) (labels : Label d → Λ) (exit : Option Λ) : Label d → TM2.Stmt (fun _ : K => Bool) Λ (State ρ d) | .initialize => .push (ports .quotientFirst) (fun _ => false) (.push (ports .quotientSecond) (fun _ => false) (.push (ports .remainderFirst) (fun _ => false) (.push (ports .remainderSecond) (fun _ => false) (.load (fun s => (s.1, none)) (.goto fun _ => labels (.firstEmit (PCP.AlphabetTable.Emitter.labelAt 3 _ 0 .entry))))))) | .firstEmit l => PCP.AlphabetTable.Emitter.statement (firstPlan ρ d) (fun _ => ports .inputVertex) (ports .emitScratch) (ports .queryReverse) (fun k => labels (.firstEmit k)) (some (labels .firstReverse)) l | .firstReverse => Reduction.MachineTransfer.loopAt (ports .queryReverse) (ports .queryIndex) id false (labels .firstReverse) (some (labels (.firstLookup (.run .copyFirst)))) | .firstLookup l => MachinePreservingLookupClean.statement (ports ∘ lookupTape) (fun k => labels (.firstLookup k)) (some (labels .firstScan)) l | .firstScan => MachineControl.statement id (divisionStates ρ d) (MachineFixedDivMod.scanLoop (degree d) (Nat.mul_pos positive positive) (ports .lookupOutput) (ports .quotientFirst) (labels .firstScan) (fun r => labels (.firstResidue r))) | .firstResidue r => MachineControl.statement id (divisionStates ρ d) (MachineFixedDivMod.emitterWithFinish (degree d) (Nat.mul_pos positive positive) (ports .remainderFirst) (some (labels .clearQuery)) firstFinish r) | .clearQuery => .pop (ports .queryIndex) (fun s _ => s) (.pop (ports .lookupOutput) (fun s _ => s) (.goto fun _ => labels (.secondEmit (PCP.AlphabetTable.Emitter.labelAt 3 _ 0 .entry)))) | .secondEmit l => PCP.AlphabetTable.Emitter.statement (secondPlan ρ d) (fun _ => ports .quotientFirst) (ports .emitScratch) (ports .queryReverse) (fun k => labels (.secondEmit k)) (some (labels .secondReverse)) l | .secondReverse => Reduction.MachineTransfer.loopAt (ports .queryReverse) (ports .queryIndex) id false (labels .secondReverse) (some (labels (.secondLookup (.run .copyFirst)))) | .secondLookup l => MachinePreservingLookupClean.statement (ports ∘ lookupTape) (fun k => labels (.secondLookup k)) (some (labels .secondScan)) l | .secondScan => MachineControl.statement id (divisionStates ρ d) (MachineFixedDivMod.scanLoop (degree d) (Nat.mul_pos positive positive) (ports .lookupOutput) (ports .quotientSecond) (labels .secondScan) (fun r => labels (.secondResidue r))) | .secondResidue r => MachineControl.statement id (divisionStates ρ d) (MachineFixedDivMod.emitterWithFinish (degree d) (Nat.mul_pos positive positive) (ports .remainderSecond) (some (labels (.outputEmit (PCP.AlphabetTable.Emitter.labelAt 3 _ 0 .entry)))) (secondFinish H) r) | .outputEmit l => PCP.AlphabetTable.Emitter.statement (outputPlan ρ d) (fun _ => ports .quotientSecond) (ports .emitScratch) (ports .output) (fun k => labels (.outputEmit k)) (some (labels (.cleanup 0))) l | .cleanup i => MachineDrain.drain (ports (dirtyTape i)) (labels (.cleanup i)) (some (if hi : i.val + 1 < 6 then labels (.cleanup ⟨i.val + 1, hi⟩) else labels .done)) | .done => Reduction.MachineTransfer.exitAt (ports .output) exit def program {d : Nat} (positive : 0 < d) (H : Table (cloudSize d) d) : Label d → TM2.Stmt (fun _ : Tape => Bool) (Label d) (State ρ d) := statement positive H id id none end MaxCutGames.Foundations.Complexity.MachineExpanderRow /-! The two actual division phases of the expander-row program. Static state conjugation connects the fixed-divisor machine to the common emitter state. Only the concrete statement placement equations are premises; no phase trace or whole-row execution is assumed. Each phase has exactly `n + 2` transitions. -/ namespace MaxCutGames.Foundations.Complexity.MachineExpanderRow open Turing open PCP.ExpanderTables PCP.ExpanderRowControl private theorem controlStatement_inverse_inline_MachineExpanderRowDivision {K Λ σ τ : Type} {Γ : K → Type} (e : σ ≃ τ) (q : TM2.Stmt Γ Λ σ) : MachineControl.statement id e.symm (MachineControl.statement id e q) = q := by induction q <;> simp_all only [MachineControl.statement, Equiv.apply_symm_apply, Equiv.symm_apply_apply, id_eq] /-- Pull the actual target program into division-state coordinates, derive its trace from its concrete statements, then transport every actual transition back. -/ private theorem transportedDivision_inline_MachineExpanderRowDivision {K Λ σ τ : Type} [DecidableEq K] [Fintype σ] (q : Nat) (positive : 0 < q) (source quotient remainder : K) (sourceQuotient : source ≠ quotient) (sourceRemainder : source ≠ remainder) (quotientRemainder : quotient ≠ remainder) (states : MachineFixedDivMod.State σ q ≃ τ) (scanLabel : Λ) (emitterLabel : Fin q → Λ) (exit : Option Λ) (finish : σ → Fin q → σ) (target : Λ → TM2.Stmt (fun _ : K => Bool) Λ τ) (atScan : target scanLabel = MachineControl.statement id states (MachineFixedDivMod.scanLoop q positive source quotient scanLabel emitterLabel)) (atEmitter : ∀ r, target (emitterLabel r) = MachineControl.statement id states (MachineFixedDivMod.emitterWithFinish q positive remainder exit finish r)) (base : K → List Bool) (n : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) (sourceInput : base source = encodeWord n ++ sourceSuffix) (quotientInput : base quotient = encodeWord 0 ++ quotientSuffix) (remainderInput : base remainder = encodeWord 0 ++ remainderSuffix) (ambient : σ) (register : Option Bool) : (MachineComposition.advance (TM2.step target))^[n + 2] (some (MachineControl.configuration id states ⟨some scanLabel, ((ambient, MachineFixedDivMod.residue q positive 0), register), base⟩)) = some (MachineControl.configuration id states ⟨exit, ((finish ambient (MachineFixedDivMod.residue q positive n), MachineFixedDivMod.residue q positive 0), none), MachineFixedDivMod.unaryTapes source quotient remainder base 0 (n / q) (n % q) sourceSuffix quotientSuffix remainderSuffix⟩) := by let sourceProgram := MachineControl.program (Equiv.refl Λ) states.symm target have sourceScan : sourceProgram scanLabel = MachineFixedDivMod.scanLoop q positive source quotient scanLabel emitterLabel := by change MachineControl.statement id states.symm (target scanLabel) = _ rw [atScan] exact controlStatement_inverse_inline_MachineExpanderRowDivision states _ have sourceEmit : ∀ r, sourceProgram (emitterLabel r) = MachineFixedDivMod.emitterWithFinish q positive remainder exit finish r := by intro r change MachineControl.statement id states.symm (target (emitterLabel r)) = _ rw [atEmitter] exact controlStatement_inverse_inline_MachineExpanderRowDivision states _ have htrace := MachineFixedDivMod.divModFromTapesWithFinish q positive source quotient remainder sourceQuotient sourceRemainder quotientRemainder scanLabel emitterLabel exit finish sourceProgram sourceScan sourceEmit base n sourceSuffix quotientSuffix remainderSuffix sourceInput quotientInput remainderInput ambient register have roundtrip : MachineControl.program (Equiv.refl Λ) states sourceProgram = target := by funext label change MachineControl.statement id states (MachineControl.statement id states.symm (target label)) = target label exact controlStatement_inverse_inline_MachineExpanderRowDivision states.symm _ have simulation : ∀ a b, TM2.step sourceProgram a = some b → TM2.step target (MachineControl.configuration id states a) = some (MachineControl.configuration id states b) := by intro a b hab have h := MachineControl.step_simulation (Equiv.refl Λ) states sourceProgram a rw [roundtrip, hab] at h exact h exact MachineComposition.liftSuccessfulTrace (TM2.step sourceProgram) (TM2.step target) (MachineControl.configuration id states) simulation (n + 2) _ _ htrace variable {K Λ ρ : Type} [DecidableEq K] [Fintype ρ] @[simp] theorem divisionStates_apply (ρ : Type) (d : Nat) (s : MachineFixedDivMod.State (Ambient ρ d × Unit) (degree d)) : divisionStates ρ d s = (((s.1.1.1, s.1.2), ()), s.2) := rfl /-- The common row state at a division boundary, with its local residue reset. -/ def divisionState {d : Nat} (positive : 0 < d) (ambient : ρ) (control : Control d) (register : Option Bool) : State ρ d := ((((ambient, control), MachineFixedDivMod.residue (degree d) (Nat.mul_pos positive positive) 0), ()), register) def divisionOutput (d : Nat) (ports : Tape → K) (quotient remainder : Tape) (base : K → List Bool) (n : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) : K → List Bool := MachineFixedDivMod.unaryTapes (ports .lookupOutput) (ports quotient) (ports remainder) base 0 (n / degree d) (n % degree d) sourceSuffix quotientSuffix remainderSuffix /-- Exact first lookup-result division, including the receiveFirst control update. -/ theorem firstDivisionTrace {d : Nat} (positive : 0 < d) (H : Table (cloudSize d) d) (ports : Tape → K) (portsInjective : Function.Injective ports) (labels : Label d → Λ) (exit : Option Λ) (target : Λ → TM2.Stmt (fun _ : K => Bool) Λ (State ρ d)) (atProgram : ∀ label, target (labels label) = statement positive H ports labels exit label) (base : K → List Bool) (n : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) (sourceInput : base (ports .lookupOutput) = encodeWord n ++ sourceSuffix) (quotientInput : base (ports .quotientFirst) = encodeWord 0 ++ quotientSuffix) (remainderInput : base (ports .remainderFirst) = encodeWord 0 ++ remainderSuffix) (ambient : ρ) (control : Control d) (register : Option Bool) : (MachineComposition.advance (TM2.step target))^[n + 2] (some ⟨some (labels .firstScan), divisionState positive ambient control register, base⟩) = some ⟨some (labels .clearQuery), divisionState positive ambient (receiveFirst control (MachineFixedDivMod.residue (degree d) (Nat.mul_pos positive positive) n)) none, divisionOutput d ports .quotientFirst .remainderFirst base n sourceSuffix quotientSuffix remainderSuffix⟩ := by have hsq : ports .lookupOutput ≠ ports .quotientFirst := fun h => Tape.noConfusion (portsInjective h) have hsr : ports .lookupOutput ≠ ports .remainderFirst := fun h => Tape.noConfusion (portsInjective h) have hqr : ports .quotientFirst ≠ ports .remainderFirst := fun h => Tape.noConfusion (portsInjective h) have hs : target (labels .firstScan) = MachineControl.statement id (divisionStates ρ d) (MachineFixedDivMod.scanLoop (degree d) (Nat.mul_pos positive positive) (ports .lookupOutput) (ports .quotientFirst) (labels .firstScan) (fun r => labels (.firstResidue r))) := atProgram .firstScan have he : ∀ r, target (labels (.firstResidue r)) = MachineControl.statement id (divisionStates ρ d) (MachineFixedDivMod.emitterWithFinish (degree d) (Nat.mul_pos positive positive) (ports .remainderFirst) (some (labels .clearQuery)) firstFinish r) := fun r => atProgram (.firstResidue r) have h := transportedDivision_inline_MachineExpanderRowDivision (degree d) (Nat.mul_pos positive positive) (ports .lookupOutput) (ports .quotientFirst) (ports .remainderFirst) hsq hsr hqr (divisionStates ρ d) (labels .firstScan) (fun r => labels (.firstResidue r)) (some (labels .clearQuery)) firstFinish target hs he base n sourceSuffix quotientSuffix remainderSuffix sourceInput quotientInput remainderInput ((ambient, control), ()) register simpa only [MachineControl.configuration, divisionStates_apply, firstFinish, divisionState, divisionOutput, Option.map_some, id_eq] using h /-- Exact second division, including receiveSecond and the actual output-emitter entry label. -/ theorem secondDivisionTrace {d : Nat} (positive : 0 < d) (H : Table (cloudSize d) d) (ports : Tape → K) (portsInjective : Function.Injective ports) (labels : Label d → Λ) (exit : Option Λ) (target : Λ → TM2.Stmt (fun _ : K => Bool) Λ (State ρ d)) (atProgram : ∀ label, target (labels label) = statement positive H ports labels exit label) (base : K → List Bool) (n : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) (sourceInput : base (ports .lookupOutput) = encodeWord n ++ sourceSuffix) (quotientInput : base (ports .quotientSecond) = encodeWord 0 ++ quotientSuffix) (remainderInput : base (ports .remainderSecond) = encodeWord 0 ++ remainderSuffix) (ambient : ρ) (control : Control d) (register : Option Bool) : (MachineComposition.advance (TM2.step target))^[n + 2] (some ⟨some (labels .secondScan), divisionState positive ambient control register, base⟩) = some ⟨some (labels (.outputEmit (PCP.AlphabetTable.Emitter.labelAt 3 _ 0 .entry))), divisionState positive ambient (receiveSecond H control (MachineFixedDivMod.residue (degree d) (Nat.mul_pos positive positive) n)) none, divisionOutput d ports .quotientSecond .remainderSecond base n sourceSuffix quotientSuffix remainderSuffix⟩ := by have hsq : ports .lookupOutput ≠ ports .quotientSecond := fun h => Tape.noConfusion (portsInjective h) have hsr : ports .lookupOutput ≠ ports .remainderSecond := fun h => Tape.noConfusion (portsInjective h) have hqr : ports .quotientSecond ≠ ports .remainderSecond := fun h => Tape.noConfusion (portsInjective h) have hs : target (labels .secondScan) = MachineControl.statement id (divisionStates ρ d) (MachineFixedDivMod.scanLoop (degree d) (Nat.mul_pos positive positive) (ports .lookupOutput) (ports .quotientSecond) (labels .secondScan) (fun r => labels (.secondResidue r))) := atProgram .secondScan have he : ∀ r, target (labels (.secondResidue r)) = MachineControl.statement id (divisionStates ρ d) (MachineFixedDivMod.emitterWithFinish (degree d) (Nat.mul_pos positive positive) (ports .remainderSecond) (some (labels (.outputEmit (PCP.AlphabetTable.Emitter.labelAt 3 _ 0 .entry)))) (secondFinish H) r) := fun r => atProgram (.secondResidue r) have h := transportedDivision_inline_MachineExpanderRowDivision (degree d) (Nat.mul_pos positive positive) (ports .lookupOutput) (ports .quotientSecond) (ports .remainderSecond) hsq hsr hqr (divisionStates ρ d) (labels .secondScan) (fun r => labels (.secondResidue r)) (some (labels (.outputEmit (PCP.AlphabetTable.Emitter.labelAt 3 _ 0 .entry)))) (secondFinish H) target hs he base n sourceSuffix quotientSuffix remainderSuffix sourceInput quotientInput remainderInput ((ambient, control), ()) register simpa only [MachineControl.configuration, divisionStates_apply, secondFinish, divisionState, divisionOutput, Option.map_some, id_eq] using h @[simp] theorem divisionOutput_source (d : Nat) (ports : Tape → K) (portsInjective : Function.Injective ports) (quotient remainder : Tape) (notSourceQuotient : Tape.lookupOutput ≠ quotient) (notSourceRemainder : Tape.lookupOutput ≠ remainder) (base : K → List Bool) (n : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) : divisionOutput d ports quotient remainder base n sourceSuffix quotientSuffix remainderSuffix (ports .lookupOutput) = encodeWord 0 ++ sourceSuffix := by exact MachineFixedDivMod.unaryTapes_source _ _ _ (fun h => notSourceQuotient (portsInjective h)) (fun h => notSourceRemainder (portsInjective h)) _ _ _ _ _ _ _ @[simp] theorem divisionOutput_quotient (d : Nat) (ports : Tape → K) (portsInjective : Function.Injective ports) (quotient remainder : Tape) (distinct : quotient ≠ remainder) (base : K → List Bool) (n : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) : divisionOutput d ports quotient remainder base n sourceSuffix quotientSuffix remainderSuffix (ports quotient) = encodeWord (n / degree d) ++ quotientSuffix := by exact MachineFixedDivMod.unaryTapes_quotient _ _ _ (fun h => distinct (portsInjective h)) _ _ _ _ _ _ _ @[simp] theorem divisionOutput_remainder (d : Nat) (ports : Tape → K) (quotient remainder : Tape) (base : K → List Bool) (n : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) : divisionOutput d ports quotient remainder base n sourceSuffix quotientSuffix remainderSuffix (ports remainder) = encodeWord (n % degree d) ++ remainderSuffix := by exact MachineFixedDivMod.unaryTapes_remainder _ _ _ _ _ _ _ _ _ _ theorem divisionOutput_other (d : Nat) (ports : Tape → K) (quotient remainder : Tape) (base : K → List Bool) (n : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) (other : K) (notSource : other ≠ ports .lookupOutput) (notQuotient : other ≠ ports quotient) (notRemainder : other ≠ ports remainder) : divisionOutput d ports quotient remainder base n sourceSuffix quotientSuffix remainderSuffix other = base other := by exact MachineFixedDivMod.unaryTapes_other _ _ _ other notSource notQuotient notRemainder _ _ _ _ _ _ _ def firstDivisionInTime {d : Nat} (positive : 0 < d) (H : Table (cloudSize d) d) (ports : Tape → K) (portsInjective : Function.Injective ports) (labels : Label d → Λ) (exit : Option Λ) (target : Λ → TM2.Stmt (fun _ : K => Bool) Λ (State ρ d)) (atProgram : ∀ label, target (labels label) = statement positive H ports labels exit label) (base : K → List Bool) (n : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) (sourceInput : base (ports .lookupOutput) = encodeWord n ++ sourceSuffix) (quotientInput : base (ports .quotientFirst) = encodeWord 0 ++ quotientSuffix) (remainderInput : base (ports .remainderFirst) = encodeWord 0 ++ remainderSuffix) (ambient : ρ) (control : Control d) (register : Option Bool) : StateTransition.EvalsToInTime (TM2.step target) ⟨some (labels .firstScan), divisionState positive ambient control register, base⟩ (some ⟨some (labels .clearQuery), divisionState positive ambient (receiveFirst control (MachineFixedDivMod.residue (degree d) (Nat.mul_pos positive positive) n)) none, divisionOutput d ports .quotientFirst .remainderFirst base n sourceSuffix quotientSuffix remainderSuffix⟩) (n + 2) where steps := n + 2 evals_in_steps := firstDivisionTrace positive H ports portsInjective labels exit target atProgram base n sourceSuffix quotientSuffix remainderSuffix sourceInput quotientInput remainderInput ambient control register steps_le_m := Nat.le_refl _ def secondDivisionInTime {d : Nat} (positive : 0 < d) (H : Table (cloudSize d) d) (ports : Tape → K) (portsInjective : Function.Injective ports) (labels : Label d → Λ) (exit : Option Λ) (target : Λ → TM2.Stmt (fun _ : K => Bool) Λ (State ρ d)) (atProgram : ∀ label, target (labels label) = statement positive H ports labels exit label) (base : K → List Bool) (n : Nat) (sourceSuffix quotientSuffix remainderSuffix : List Bool) (sourceInput : base (ports .lookupOutput) = encodeWord n ++ sourceSuffix) (quotientInput : base (ports .quotientSecond) = encodeWord 0 ++ quotientSuffix) (remainderInput : base (ports .remainderSecond) = encodeWord 0 ++ remainderSuffix) (ambient : ρ) (control : Control d) (register : Option Bool) : StateTransition.EvalsToInTime (TM2.step target) ⟨some (labels .secondScan), divisionState positive ambient control register, base⟩ (some ⟨some (labels (.outputEmit (PCP.AlphabetTable.Emitter.labelAt 3 _ 0 .entry))), divisionState positive ambient (receiveSecond H control (MachineFixedDivMod.residue (degree d) (Nat.mul_pos positive positive) n)) none, divisionOutput d ports .quotientSecond .remainderSecond base n sourceSuffix quotientSuffix remainderSuffix⟩) (n + 2) where steps := n + 2 evals_in_steps := secondDivisionTrace positive H ports portsInjective labels exit target atProgram base n sourceSuffix quotientSuffix remainderSuffix sourceInput quotientInput remainderInput ambient control register steps_le_m := Nat.le_refl _ end MaxCutGames.Foundations.Complexity.MachineExpanderRow end OAI