cfahlgren1's picture
download
raw
28.2 kB
import Mathlib
import OAI.Combinatorics.IndependentSets.Machines.MachineCopy
import OAI.Combinatorics.IndependentSets.Machines.PushWord
namespace OAI
namespace IndependentSetsGames.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)))
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]
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]
theorem update_source (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 (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 (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, update_quotient, 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]
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]
cases exit <;>
simp [TM2.stepAux, Reduction.MachineTransfer.exitAt, encodeWord, List.append_assoc]
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)
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
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 _
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)
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]
cases exit <;>
simp [TM2.stepAux, Reduction.MachineTransfer.exitAt, encodeWord, List.append_assoc]
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 IndependentSetsGames.Foundations.Complexity.MachineFixedDivMod
end OAI

Xet Storage Details

Size:
28.2 kB
·
Xet hash:
b2cd5e433b367f54e8de3290314adb974c81c99a1603d3d38324cecea9567032

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