cfahlgren1/math / lean /OAI /Combinatorics /IndependentSets /Machines /MachineExpanderRowDivision.lean
cfahlgren1's picture
download
raw
16.3 kB
import Mathlib
import OAI.Combinatorics.IndependentSets.Machines.MachineExpanderRowProgram
namespace OAI
namespace IndependentSetsGames.Foundations.Complexity.MachineExpanderRow
open Turing
open PCP.ExpanderTables PCP.ExpanderRowControl
private theorem controlStatement_inverse {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]
private theorem transportedDivision {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 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 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 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
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
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 (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
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 (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 IndependentSetsGames.Foundations.Complexity.MachineExpanderRow
end OAI

Xet Storage Details

Size:
16.3 kB
·
Xet hash:
dc05019b029466e665fe93312e69e2f42597cc321095e671b217b4f63563df09

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.