cfahlgren1/math / lean /OAI /Computability /MaxCut /Machines /MachineSequential.lean
cfahlgren1's picture
download
raw
49.7 kB
import Mathlib
import OAI.Computability.MaxCut.Machines.MachineCopy
namespace OAI
/-!
A concrete sequential composition of two finite TM2 programs. The first output
is transferred to a temporary stack and then to the second input. Both loops
execute actual transitions and reset their shared register on exit. Static
renaming changes neither a stack word nor the number of executed transitions.
-/
namespace MaxCutGames.Foundations.Complexity.MachineSequential
open Turing
abbrev Tape (first second : FinTM2) := first.K ⊕ (second.K ⊕ Unit)
abbrev Symbols (first second : FinTM2) : Tape first second → Type :=
MachineEmbedding.Alphabet first.Γ
(MachineEmbedding.Alphabet second.Γ (fun _ : Unit => second.Γ second.k₀))
abbrev Label (first second : FinTM2) := first.Λ ⊕ (Bool ⊕ second.Λ)
abbrev Register (second : FinTM2) := Option (second.Γ second.k₀)
abbrev State (first second : FinTM2) := (first.σ × second.σ) × Register second
def firstStates (first second : FinTM2) :
first.σ × (second.σ × Register second) ≃ State first second where
toFun st := ((st.1, st.2.1), st.2.2)
invFun st := (st.1.1, (st.1.2, st.2))
left_inv _ := rfl
right_inv _ := rfl
def secondStates (first second : FinTM2) :
second.σ × (first.σ × Register second) ≃ State first second where
toFun st := ((st.2.1, st.1), st.2.2)
invFun st := (st.1.2, (st.1.1, st.2))
left_inv _ := rfl
right_inv _ := rfl
def secondLabels (first second : FinTM2) :
second.Λ ⊕ (Bool ⊕ first.Λ) → Label first second
| .inl label => .inr (.inr label)
| .inr (.inl bit) => .inr (.inl bit)
| .inr (.inr label) => .inl label
def bridgeLabel (first second : FinTM2) (bit : Bool) : Label first second :=
.inr (.inl bit)
def firstStatement (first second : FinTM2) (q : first.Stmt) :
TM2.Stmt (Symbols first second) (Label first second) (State first second) :=
MachineControl.statement id (firstStates first second)
(MachineEmbedding.statement (some (bridgeLabel first second false)) q)
def secondStatement (first second : FinTM2) (q : second.Stmt) :
TM2.Stmt (Symbols first second) (Label first second) (State first second) :=
MachineControl.statement (secondLabels first second) (secondStates first second)
(MachineStackSwap.statement
(MachineEmbedding.statement
(Δ := MachineEmbedding.Alphabet first.Γ (fun _ : Unit => second.Γ second.k₀))
(Λextra := Bool ⊕ first.Λ) none q))
def program (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀) :
Label first second → TM2.Stmt (Symbols first second) (Label first second)
(State first second)
| .inl label => firstStatement first second (first.m label)
| .inr (.inr label) => secondStatement first second (second.m label)
| .inr (.inl false) =>
Reduction.MachineTransfer.loopAt (Γ := Symbols first second)
(.inl first.k₁) (.inr (.inr ())) relabel fallback
(bridgeLabel first second false) (some (bridgeLabel first second true))
| .inr (.inl true) =>
Reduction.MachineTransfer.loopAt (Γ := Symbols first second)
(.inr (.inr ())) (.inr (.inl second.k₀)) id fallback
(bridgeLabel first second true) (some (.inr (.inr second.main)))
def machine (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀) : FinTM2 where
K := Tape first second
kFin := by
letI := first.kFin
letI := second.kFin
exact inferInstanceAs (Fintype (first.K ⊕ (second.K ⊕ Unit)))
k₀ := .inl first.k₀
k₁ := .inr (.inl second.k₁)
Γ := Symbols first second
Λ := Label first second
ΛFin := by
letI := first.ΛFin
letI := second.ΛFin
exact inferInstanceAs (Fintype (first.Λ ⊕ (Bool ⊕ second.Λ)))
main := .inl first.main
σ := State first second
σFin := by
letI := first.σFin
letI := second.σFin
letI := second.Γk₀Fin
exact inferInstanceAs (Fintype ((first.σ × second.σ) × Option (second.Γ second.k₀)))
initialState := ((first.initialState, second.initialState), none)
Γk₀Fin := first.Γk₀Fin
m := program first second relabel fallback
def firstConfiguration (first second : FinTM2) (c : first.Cfg) :
TM2.Cfg (Symbols first second) (Label first second) (State first second) :=
MachineControl.configuration id (firstStates first second)
(MachineEmbedding.configuration (some (bridgeLabel first second false))
(second.initialState, (none : Register second)) (fun _ => []) c)
def secondConfiguration (first second : FinTM2) (c : second.Cfg) :
TM2.Cfg (Symbols first second) (Label first second) (State first second) :=
MachineControl.configuration (secondLabels first second) (secondStates first second)
(MachineStackSwap.configuration
(MachineEmbedding.configuration
(Δ := MachineEmbedding.Alphabet first.Γ (fun _ : Unit => second.Γ second.k₀))
(Λextra := Bool ⊕ first.Λ) none
(first.initialState, (none : Register second)) (fun _ => []) c))
theorem firstStep (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀) (a b : first.Cfg)
(transition : first.step a = some b) :
(machine first second relabel fallback).step (firstConfiguration first second a) =
some (firstConfiguration first second b) := by
cases a with
| mk label state tapes =>
cases label with
| none => cases transition
| some label =>
have hb := Option.some.inj transition
subst b
change some (TM2.stepAux
(MachineControl.statement id (firstStates first second)
(MachineEmbedding.statement (some (bridgeLabel first second false)) (first.m label)))
((firstStates first second) (state, (second.initialState, none)))
(MachineEmbedding.tapes tapes (fun _ => []))) = _
have controlled := MachineControl.stepAux_simulation id (firstStates first second)
(MachineEmbedding.statement
(Δ := MachineEmbedding.Alphabet second.Γ (fun _ : Unit => second.Γ second.k₀))
(some (bridgeLabel first second false)) (first.m label))
(state, (second.initialState, none)) (MachineEmbedding.tapes tapes (fun _ => []))
have embedded := MachineEmbedding.stepAux_simulation
(Δ := MachineEmbedding.Alphabet second.Γ (fun _ : Unit => second.Γ second.k₀))
(some (bridgeLabel first second false)) (second.initialState, (none : Register second))
(fun _ => []) (first.m label) state tapes
exact congrArg some (controlled.trans
(congrArg (MachineControl.configuration id (firstStates first second)) embedded))
theorem secondStep (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀) (a b : second.Cfg)
(transition : second.step a = some b) :
(machine first second relabel fallback).step (secondConfiguration first second a) =
some (secondConfiguration first second b) := by
cases a with
| mk label state tapes =>
cases label with
| none => cases transition
| some label =>
have hb := Option.some.inj transition
subst b
change some (TM2.stepAux
(MachineControl.statement (secondLabels first second) (secondStates first second)
(MachineStackSwap.statement
(MachineEmbedding.statement
(Δ := MachineEmbedding.Alphabet first.Γ (fun _ : Unit => second.Γ second.k₀))
(Λextra := Bool ⊕ first.Λ) none (second.m label))))
((secondStates first second) (state, (first.initialState, none)))
(MachineStackSwap.tapes (MachineEmbedding.tapes tapes (fun _ => [])))) = _
rw [MachineControl.stepAux_simulation, MachineStackSwap.stepAux_simulation,
MachineEmbedding.stepAux_simulation]
rfl
theorem firstConfiguration_init (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀) (input : List (first.Γ first.k₀)) :
firstConfiguration first second (initList first input) =
initList (machine first second relabel fallback) input := by
unfold firstConfiguration MachineControl.configuration MachineEmbedding.configuration initList
congr 1
funext k
rcases k with k | k | k
· by_cases h : k = first.k₀
· subst k
simp [machine, MachineEmbedding.tapes]; rfl
· simp [machine, MachineEmbedding.tapes, h]
· simp [machine, MachineEmbedding.tapes]
· simp [machine, MachineEmbedding.tapes]
theorem secondConfiguration_halt (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀) (output : List (second.Γ second.k₁)) :
secondConfiguration first second (haltList second output) =
haltList (machine first second relabel fallback) output := by
unfold secondConfiguration MachineControl.configuration MachineStackSwap.configuration
MachineEmbedding.configuration haltList
congr 1
funext k
rcases k with k | k | k
· simp [machine, MachineStackSwap.tapes, MachineEmbedding.tapes]
· by_cases h : k = second.k₁
· subst k
simp [machine, MachineStackSwap.tapes, MachineEmbedding.tapes]; rfl
· simp [machine, MachineStackSwap.tapes, MachineEmbedding.tapes, h]
· simp [machine, MachineStackSwap.tapes, MachineEmbedding.tapes]
def temporaryTapes (first second : FinTM2) (word : List (second.Γ second.k₀)) :
∀ k, List (Symbols first second k)
| .inl _ => []
| .inr (.inl _) => []
| .inr (.inr _) => word
def temporaryConfiguration (first second : FinTM2) (word : List (second.Γ second.k₀)) :
TM2.Cfg (Symbols first second) (Label first second) (State first second) :=
⟨some (bridgeLabel first second true),
((first.initialState, second.initialState), none), temporaryTapes first second word⟩
theorem firstTransfer_tapes (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(word : List (first.Γ first.k₁)) :
Reduction.MachineTransfer.tapesAt (.inl first.k₁) (.inr (.inr ()))
(firstConfiguration first second (haltList first word)).stk [] (word.reverse.map relabel) =
temporaryTapes first second (word.reverse.map relabel) := by
funext k
rcases k with k | k | k
· by_cases h : k = first.k₁
· subst k
simp [Reduction.MachineTransfer.tapesAt, temporaryTapes]
· simp [Reduction.MachineTransfer.tapesAt, temporaryTapes, firstConfiguration,
MachineControl.configuration, MachineEmbedding.configuration, MachineEmbedding.tapes,
haltList, h]
· simp [Reduction.MachineTransfer.tapesAt, temporaryTapes, firstConfiguration,
MachineControl.configuration, MachineEmbedding.configuration, MachineEmbedding.tapes]
· cases k
simp [Reduction.MachineTransfer.tapesAt, temporaryTapes]
theorem secondTransfer_tapes (first second : FinTM2) (word : List (second.Γ second.k₀)) :
Reduction.MachineTransfer.tapesAt (Γ := Symbols first second)
(.inr (.inr ())) (.inr (.inl second.k₀))
(temporaryTapes first second word) [] word.reverse =
(secondConfiguration first second (initList second word.reverse)).stk := by
funext k
rcases k with k | k | k
· simp [Reduction.MachineTransfer.tapesAt, temporaryTapes, secondConfiguration,
MachineControl.configuration, MachineStackSwap.configuration, MachineStackSwap.tapes,
MachineEmbedding.configuration, MachineEmbedding.tapes]
· by_cases h : k = second.k₀
· subst k
simp [Reduction.MachineTransfer.tapesAt, secondConfiguration,
MachineControl.configuration, MachineStackSwap.configuration, MachineStackSwap.tapes,
MachineEmbedding.configuration, MachineEmbedding.tapes, initList]
· simp [Reduction.MachineTransfer.tapesAt, temporaryTapes, secondConfiguration,
MachineControl.configuration, MachineStackSwap.configuration, MachineStackSwap.tapes,
MachineEmbedding.configuration, MachineEmbedding.tapes, initList, h]
· cases k
simp [Reduction.MachineTransfer.tapesAt, secondConfiguration,
MachineControl.configuration, MachineStackSwap.configuration, MachineStackSwap.tapes,
MachineEmbedding.configuration, MachineEmbedding.tapes]
def firstTransfer (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀) (word : List (first.Γ first.k₁)) :
StateTransition.EvalsToInTime (machine first second relabel fallback).step
(firstConfiguration first second (haltList first word))
(some (temporaryConfiguration first second (word.reverse.map relabel)))
(word.length + 1) := by
let run := Reduction.MachineTransfer.transferAtInTime
(.inl first.k₁ : Tape first second) (.inr (.inr ())) (by intro h; cases h)
relabel fallback (bridgeLabel first second false)
(some (bridgeLabel first second true)) (program first second relabel fallback) rfl
(firstConfiguration first second (haltList first word)).stk
(first.initialState, second.initialState) none
have inputTape : (firstConfiguration first second (haltList first word)).stk (.inl first.k₁) =
word := by simp [firstConfiguration, MachineControl.configuration,
MachineEmbedding.configuration, MachineEmbedding.tapes, haltList]
have outputTape :
(firstConfiguration first second (haltList first word)).stk (.inr (.inr ())) = [] := rfl
rw [inputTape, outputTape, List.append_nil, firstTransfer_tapes] at run
exact run
def secondTransfer (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀) (word : List (second.Γ second.k₀)) :
StateTransition.EvalsToInTime (machine first second relabel fallback).step
(temporaryConfiguration first second word)
(some (secondConfiguration first second (initList second word.reverse)))
(word.length + 1) := by
let run := Reduction.MachineTransfer.transferAtInTime
(.inr (.inr ()) : Tape first second) (.inr (.inl second.k₀)) (by intro h; cases h)
id fallback (bridgeLabel first second true) (some (.inr (.inr second.main)))
(program first second relabel fallback) rfl (temporaryTapes first second word)
(first.initialState, second.initialState) none
simp only [temporaryTapes, List.map_id, List.append_nil] at run
rw [secondTransfer_tapes] at run
exact run
/-- An execution of each component yields an actual execution of the combined
machine. The two transfer loops add exactly twice the intermediate length plus two. -/
def execute (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀)
(input : List (first.Γ first.k₀)) (middle : List (first.Γ first.k₁))
(output : List (second.Γ second.k₁)) (firstBudget secondBudget : Nat)
(runFirst : TM2OutputsInTime first input (some middle) firstBudget)
(runSecond : TM2OutputsInTime second (middle.map relabel) (some output) secondBudget) :
TM2OutputsInTime (machine first second relabel fallback) input (some output)
(firstBudget + 2 * (middle.length + 1) + secondBudget) := by
let start := MachineComposition.liftExecutionInTime first.step
(machine first second relabel fallback).step (firstConfiguration first second)
(firstStep first second relabel fallback) runFirst
let bridge₁ := firstTransfer first second relabel fallback middle
have bridge₂ := secondTransfer first second relabel fallback (middle.reverse.map relabel)
have reversal : (middle.reverse.map relabel).reverse = middle.map relabel := by
simp only [List.map_reverse, List.reverse_reverse]
rw [reversal] at bridge₂
simp only [List.length_map, List.length_reverse] at bridge₂
let finish := MachineComposition.liftExecutionInTime second.step
(machine first second relabel fallback).step (secondConfiguration first second)
(secondStep first second relabel fallback) runSecond
let phase₁₂ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ start bridge₁
let phase₁₂₃ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ phase₁₂ bridge₂
have full := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ phase₁₂₃ finish
rw [firstConfiguration_init, secondConfiguration_halt] at full
exact {
toEvalsTo := full.toEvalsTo
steps_le_m := by have h := full.steps_le_m; omega
}
/-- General sequential composition with an explicit symbol of the intermediate
alphabet to totalize the bridge's unused empty-register expression. Its time
polynomial uses the transition-derived output-length bound of the first machine. -/
noncomputable def compose {α β γ αΓ βΓ γΓ : Type}
{ea : α → List αΓ} {eb : β → List βΓ} {ec : γ → List γΓ}
{f : α → β} {g : β → γ}
(first : TM2ComputableInPolyTime ea eb f)
(second : TM2ComputableInPolyTime eb ec g) (fallback : βΓ) :
TM2ComputableInPolyTime ea ec (fun a => g (f a)) := by
let relabel : first.tm.Γ first.tm.k₁ → second.tm.Γ second.tm.k₀ :=
fun symbol => second.inputAlphabet.symm (first.outputAlphabet symbol)
let defaultSymbol := second.inputAlphabet.symm fallback
let intermediate : Polynomial Nat := Polynomial.X +
Polynomial.C (Runtime.programPushBound first.tm) * first.time
refine {
tm := machine first.tm second.tm relabel defaultSymbol
inputAlphabet := first.inputAlphabet
outputAlphabet := second.outputAlphabet
time := MachineComposition.compositionPolynomial first.time second.time intermediate
outputsFun := ?_
}
intro a
let input := (ea a).map first.inputAlphabet.invFun
let middle := (eb (f a)).map first.outputAlphabet.invFun
let output := (ec (g (f a))).map second.outputAlphabet.invFun
have handoff : middle.map relabel = (eb (f a)).map second.inputAlphabet.invFun := by
dsimp only [middle, relabel]
simp only [List.map_map]
apply List.map_congr_left
intro symbol _
exact congrArg second.inputAlphabet.symm (first.outputAlphabet.apply_symm_apply symbol)
have runSecond : TM2OutputsInTime second.tm (middle.map relabel) (some output)
(second.time.eval (eb (f a)).length) := by
rw [handoff]
exact second.outputsFun (f a)
have full := execute first.tm second.tm relabel defaultSymbol input middle output
(first.time.eval (ea a).length) (second.time.eval (eb (f a)).length)
(first.outputsFun a) runSecond
have intermediateBound : (eb (f a)).length ≤ intermediate.eval (ea a).length :=
Runtime.encodedOutputLength first a
have totalBound := MachineComposition.compositionBudget_le first.time second.time
intermediate (ea a).length (eb (f a)).length intermediateBound
have middleLength : middle.length = (eb (f a)).length := by simp only [middle, List.length_map]
exact {
toEvalsTo := full.toEvalsTo
steps_le_m := by
apply Nat.le_trans full.steps_le_m
simpa only [middleLength] using totalBound
}
noncomputable def composeBits {α β γ : Type}
{ea : α → List Bool} {eb : β → List Bool} {ec : γ → List Bool}
{f : α → β} {g : β → γ}
(first : TM2ComputableInPolyTime ea eb f)
(second : TM2ComputableInPolyTime eb ec g) :
TM2ComputableInPolyTime ea ec (fun a => g (f a)) :=
compose first second false
end MaxCutGames.Foundations.Complexity.MachineSequential
namespace MaxCutGames.Reduction.MachineTransducer
open Turing
open MaxCutGames.Foundations.Complexity
open MachineSubstitution (pushWord stepAux_pushWord statementPushBound_pushWord)
def tapeStacks (input output : List Bool) : Bool → List Bool :=
fun side => if side then output else input
private theorem update_input_inline_MachineTransducer (input output replacement : List Bool) :
Function.update (tapeStacks input output) false replacement = tapeStacks replacement output := by
funext side
cases side <;> simp [tapeStacks]
private theorem update_output_inline_MachineTransducer (input output replacement : List Bool) :
Function.update (tapeStacks input output) true replacement = tapeStacks input replacement := by
funext side
cases side <;> simp [tapeStacks]
variable {Q : Type} [Fintype Q]
/-- The semantic output, with the control state updated after each input bit. -/
def output (transition : Q → Bool → Q) (emit : Q → Bool → List Bool) :
Q → List Bool → List Bool
| _, [] => []
| state, symbol :: input =>
emit state symbol ++ output transition emit (transition state symbol) input
def loop (initial : Q) :
TM2.Stmt (fun _ : Bool => Bool) (Option (Q × Bool)) (Q × Option Bool) :=
.pop false (fun state head => (state.1, head))
(.branch (fun state => state.2.isSome)
(.goto fun state => some (state.1, state.2.getD false))
(.load (fun _ => (initial, none)) .halt))
def program (initial : Q) (transition : Q → Bool → Q) (emit : Q → Bool → List Bool) :
Option (Q × Bool) →
TM2.Stmt (fun _ : Bool => Bool) (Option (Q × Bool)) (Q × Option Bool)
| none => loop initial
| some (state, symbol) =>
.load (fun register => (transition state symbol, register.2))
(pushWord true (emit state symbol) (.goto fun _ => none))
def machine (initial : Q) (transition : Q → Bool → Q) (emit : Q → Bool → List Bool) :
FinTM2 where
K := Bool
k₀ := false
k₁ := true
Γ _ := Bool
Λ := Option (Q × Bool)
main := none
σ := Q × Option Bool
initialState := (initial, none)
m := program initial transition emit
/-- A finite maximum depending only on the fixed emission table. -/
noncomputable def maxEmission (emit : Q → Bool → List Bool) : Nat :=
Finset.univ.sup (fun pair : Q × Bool => (emit pair.1 pair.2).length)
theorem emission_le (emit : Q → Bool → List Bool) (state : Q) (symbol : Bool) :
(emit state symbol).length ≤ maxEmission emit := by
exact Finset.le_sup (f := fun pair : Q × Bool => (emit pair.1 pair.2).length)
(Finset.mem_univ (state, symbol))
theorem statementPushBound_le (initial : Q) (transition : Q → Bool → Q)
(emit : Q → Bool → List Bool) (label : Option (Q × Bool)) :
Runtime.statementPushBound (program initial transition emit label) ≤ maxEmission emit := by
cases label with
| none => simp [program, loop, Runtime.statementPushBound]
| some pair =>
simpa only [program, Runtime.statementPushBound, statementPushBound_pushWord,
Nat.add_zero] using emission_le emit pair.1 pair.2
theorem programPushBound_le (initial : Q) (transition : Q → Bool → Q)
(emit : Q → Bool → List Bool) :
Runtime.programPushBound (machine initial transition emit) ≤ maxEmission emit := by
have allLabels (labels : List (Option (Q × Bool))) :
Runtime.maxLabelPushes (program initial transition emit) labels ≤ maxEmission emit := by
induction labels with
| nil => exact Nat.zero_le _
| cons label rest ih =>
exact max_le (statementPushBound_le initial transition emit label) ih
exact allLabels (machine initial transition emit).ΛFin.elems.toList
def running (initial : Q) (transition : Q → Bool → Q) (emit : Q → Bool → List Bool)
(state : Q) (input accumulator : List Bool) (register : Option Bool) :
(machine initial transition emit).Cfg :=
⟨some none, (state, register), tapeStacks input accumulator⟩
def emitting (initial : Q) (transition : Q → Bool → Q) (emit : Q → Bool → List Bool)
(oldState : Q) (symbol : Bool) (input accumulator : List Bool)
(state : Q × Option Bool) : (machine initial transition emit).Cfg :=
⟨some (some (oldState, symbol)), state, tapeStacks input accumulator⟩
def halted (initial : Q) (transition : Q → Bool → Q) (emit : Q → Bool → List Bool)
(accumulator : List Bool) : (machine initial transition emit).Cfg :=
⟨none, (initial, none), tapeStacks [] accumulator⟩
/-- The final empty pop also restores the initial finite control state. -/
theorem step_empty (initial : Q) (transition : Q → Bool → Q)
(emit : Q → Bool → List Bool) (state : Q) (accumulator : List Bool)
(register : Option Bool) :
(machine initial transition emit).step
(running initial transition emit state [] accumulator register) =
some (halted initial transition emit accumulator) := by
change some (TM2.stepAux (loop initial) (state, register) (tapeStacks [] accumulator)) = _
simp [loop, TM2.stepAux, tapeStacks, halted]
erw [update_input_inline_MachineTransducer]
rfl
theorem step_cons (initial : Q) (transition : Q → Bool → Q)
(emit : Q → Bool → List Bool) (state : Q) (symbol : Bool)
(input accumulator : List Bool) (register : Option Bool) :
(machine initial transition emit).step
(running initial transition emit state (symbol :: input) accumulator register) =
some (emitting initial transition emit state symbol input accumulator (state, some symbol)) := by
change some (TM2.stepAux (loop initial) (state, register)
(tapeStacks (symbol :: input) accumulator)) = _
simp [loop, TM2.stepAux, tapeStacks, emitting]
rw [update_input_inline_MachineTransducer]
rfl
/-- Emission reads the old state from its label and installs its successor state. -/
theorem step_emit (initial : Q) (transition : Q → Bool → Q)
(emit : Q → Bool → List Bool) (oldState : Q) (symbol : Bool)
(input accumulator : List Bool) (state : Q × Option Bool) :
(machine initial transition emit).step
(emitting initial transition emit oldState symbol input accumulator state) =
some (running initial transition emit (transition oldState symbol) input
((emit oldState symbol).reverse ++ accumulator) state.2) := by
change some (TM2.stepAux (pushWord true (emit oldState symbol) (.goto fun _ => none))
(transition oldState symbol, state.2) (tapeStacks input accumulator)) = _
rw [stepAux_pushWord]
simp only [TM2.stepAux]
change some (⟨some none, (transition oldState symbol, state.2),
Function.update (tapeStacks input accumulator) true
((emit oldState symbol).reverse ++ accumulator)⟩ :
(machine initial transition emit).Cfg) = _
erw [update_output_inline_MachineTransducer]
rfl
def next (initial : Q) (transition : Q → Bool → Q) (emit : Q → Bool → List Bool)
(configuration : Option (machine initial transition emit).Cfg) :
Option (machine initial transition emit).Cfg :=
configuration.bind (machine initial transition emit).step
theorem two_steps_cons (initial : Q) (transition : Q → Bool → Q)
(emit : Q → Bool → List Bool) (state : Q) (symbol : Bool)
(input accumulator : List Bool) (register : Option Bool) :
(next initial transition emit)^[2]
(some (running initial transition emit state (symbol :: input) accumulator register)) =
some (running initial transition emit (transition state symbol) input
((emit state symbol).reverse ++ accumulator) (some symbol)) := by
change (machine initial transition emit).step
(running initial transition emit state (symbol :: input) accumulator register) >>=
(machine initial transition emit).step = _
rw [step_cons]
exact step_emit initial transition emit state symbol input accumulator (state, some symbol)
/-- Exact execution from any finite control state, accumulator, and symbol register. -/
theorem transduce_steps (initial : Q) (transition : Q → Bool → Q)
(emit : Q → Bool → List Bool) (state : Q) (input accumulator : List Bool)
(register : Option Bool) :
(next initial transition emit)^[2 * input.length + 1]
(some (running initial transition emit state input accumulator register)) =
some (halted initial transition emit ((output transition emit state input).reverse ++ accumulator)) := by
induction input generalizing state accumulator register with
| nil =>
simpa only [List.length_nil, Nat.mul_zero, Nat.zero_add, Function.iterate_one, next,
Option.bind_some, output, List.reverse_nil, List.nil_append]
using step_empty initial transition emit state accumulator register
| cons symbol input ih =>
rw [List.length_cons]
rw [show 2 * (input.length + 1) + 1 = (2 * input.length + 1) + 2 by omega]
rw [Function.iterate_add_apply, two_steps_cons, ih]
simp only [output, List.reverse_append, List.append_assoc]
theorem initList_eq (initial : Q) (transition : Q → Bool → Q)
(emit : Q → Bool → List Bool) (input : List Bool) :
initList (machine initial transition emit) input =
running initial transition emit initial input [] none := by
unfold initList running
congr 1
funext side
cases side <;> rfl
theorem haltList_eq (initial : Q) (transition : Q → Bool → Q)
(emit : Q → Bool → List Bool) (accumulator : List Bool) :
haltList (machine initial transition emit) accumulator =
halted initial transition emit accumulator := by
unfold haltList halted
congr 1
theorem transduce_init_steps (initial : Q) (transition : Q → Bool → Q)
(emit : Q → Bool → List Bool) (input : List Bool) :
(next initial transition emit)^[2 * input.length + 1]
(some (initList (machine initial transition emit) input)) =
some (haltList (machine initial transition emit) (output transition emit initial input).reverse) := by
rw [initList_eq, haltList_eq]
simpa only [List.append_nil] using transduce_steps initial transition emit initial input [] none
def outputsInTime (initial : Q) (transition : Q → Bool → Q)
(emit : Q → Bool → List Bool) (input : List Bool) :
TM2OutputsInTime (machine initial transition emit) input
(some (output transition emit initial input).reverse) (2 * input.length + 1) where
steps := 2 * input.length + 1
evals_in_steps := transduce_init_steps initial transition emit input
steps_le_m := Nat.le_refl _
@[simp] theorem outputsInTime_steps (initial : Q) (transition : Q → Bool → Q)
(emit : Q → Bool → List Bool) (input : List Bool) :
(outputsInTime initial transition emit input).steps = 2 * input.length + 1 := rfl
/-- Reversed transducer output in polynomial `2 * X + 1` time. -/
noncomputable def reversedComputableInPolyTime (initial : Q) (transition : Q → Bool → Q)
(emit : Q → Bool → List Bool) :
TM2ComputableInPolyTime (id : List Bool → List Bool) id
(fun input => (output transition emit initial input).reverse) where
tm := machine initial transition emit
inputAlphabet := Equiv.refl Bool
outputAlphabet := Equiv.refl Bool
time := 2 * Polynomial.X + 1
outputsFun input := by
change TM2OutputsInTime (machine initial transition emit) (input.map id)
(some ((output transition emit initial input).reverse.map id))
((2 * Polynomial.X + 1 : Polynomial Nat).eval input.length)
have hi := @List.map_id ((machine initial transition emit).Γ
(machine initial transition emit).k₀) input
have ho := @List.map_id ((machine initial transition emit).Γ
(machine initial transition emit).k₁) (output transition emit initial input).reverse
rw [hi, ho]
simpa only [Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_X,
Polynomial.eval_ofNat, Polynomial.eval_one] using outputsInTime initial transition emit input
/-- Forward transducer output, using the checked actual reversal composition. -/
noncomputable def computableInPolyTime (initial : Q) (transition : Q → Bool → Q)
(emit : Q → Bool → List Bool) :
TM2ComputableInPolyTime (id : List Bool → List Bool) id
(output transition emit initial) := by
simpa only [List.reverse_reverse] using
MachineSequential.composeBits (reversedComputableInPolyTime initial transition emit)
MachineReverse.computableInPolyTime
end MaxCutGames.Reduction.MachineTransducer
namespace MaxCutGames.Foundations.Complexity.MachineTransducerCopy
open Turing
open Reduction.MachineSubstitution (pushWord stepAux_pushWord)
variable {K Λ σ Q : Type} [DecidableEq K]
abbrev Alphabet (K : Type) (_ : K) := Bool
abbrev State (σ Q : Type) := (σ × Q) × Option Bool
def scanLoop (source scratch : K) (initial : Q)
(emitterLabel : Q → Bool → Λ) (restoreLabel : Λ) :
TM2.Stmt (Alphabet K) Λ (State σ Q) :=
.pop source (fun state head => (state.1, head))
(.branch (fun state => state.2.isSome)
(.push scratch (fun state => state.2.getD false)
(.goto fun state => emitterLabel state.1.2 (state.2.getD false)))
(.load (fun state => ((state.1.1, initial), none)) (.goto fun _ => restoreLabel)))
def emitter (destination : K) (transition : Q → Bool → Q)
(emit : Q → Bool → List Bool) (scanLabel : Λ) (control : Q) (symbol : Bool) :
TM2.Stmt (Alphabet K) Λ (State σ Q) :=
.load (fun state => ((state.1.1, transition control symbol), state.2))
(pushWord destination (emit control symbol) (.goto fun _ => scanLabel))
abbrev tapes (source scratch destination : K) (base : K → List Bool)
(input scratchWord outputWord : List Bool) : K → List Bool :=
MachineCopy.forkTapes source scratch destination base input scratchWord outputWord
private theorem update_source_inline_MachineTransducerCopy (source scratch destination : K)
(sourceScratch : source ≠ scratch) (sourceDestination : source ≠ destination)
(scratchDestination : scratch ≠ destination) (base : K → List Bool)
(input scratchWord outputWord replacement : List Bool) :
Function.update (tapes source scratch destination base input scratchWord outputWord)
source replacement = tapes source scratch destination base replacement scratchWord outputWord := by
funext k
by_cases hs : k = source
· subst k; simp [tapes, MachineCopy.forkTapes, sourceScratch, sourceDestination]
· by_cases ht : k = scratch
· subst k; simp [tapes, MachineCopy.forkTapes, Ne.symm sourceScratch, scratchDestination]
· by_cases hd : k = destination
· subst k; simp [tapes, MachineCopy.forkTapes, Ne.symm sourceDestination]
· simp [tapes, MachineCopy.forkTapes, hs, ht, hd]
private theorem update_scratch_inline_MachineTransducerCopy (source scratch destination : K)
(scratchDestination : scratch ≠ destination) (base : K → List Bool)
(input scratchWord outputWord replacement : List Bool) :
Function.update (tapes source scratch destination base input scratchWord outputWord)
scratch replacement = tapes source scratch destination base input replacement outputWord := by
funext k
by_cases ht : k = scratch
· subst k; simp [tapes, MachineCopy.forkTapes, scratchDestination]
· by_cases hd : k = destination
· subst k; simp [tapes, MachineCopy.forkTapes, Ne.symm scratchDestination]
· simp [tapes, MachineCopy.forkTapes, ht, hd]
private theorem update_output_inline_MachineTransducerCopy (source scratch destination : K) (base : K → List Bool)
(input scratchWord outputWord replacement : List Bool) :
Function.update (tapes source scratch destination base input scratchWord outputWord)
destination replacement = tapes source scratch destination base input scratchWord replacement := by
simp [tapes, MachineCopy.forkTapes]
theorem scanStep_empty (source scratch destination : K)
(sourceScratch : source ≠ scratch) (sourceDestination : source ≠ destination)
(scratchDestination : scratch ≠ destination)
(initial : Q) (scanLabel restoreLabel : Λ) (emitterLabel : Q → Bool → Λ)
(program : Λ → TM2.Stmt (Alphabet K) Λ (State σ Q))
(atScan : program scanLabel = scanLoop source scratch initial emitterLabel restoreLabel)
(base : K → List Bool) (scratchWord outputWord : List Bool)
(ambient : σ) (control : Q) (register : Option Bool) :
TM2.step program ⟨some scanLabel, ((ambient, control), register),
tapes source scratch destination base [] scratchWord outputWord⟩ =
some ⟨some restoreLabel, ((ambient, initial), none),
tapes source scratch destination base [] scratchWord outputWord⟩ := by
change some (TM2.stepAux (program scanLabel) ((ambient, control), register)
(tapes source scratch destination base [] scratchWord outputWord)) = _
rw [atScan]
simp [scanLoop, TM2.stepAux, sourceScratch, sourceDestination, scratchDestination,
update_source_inline_MachineTransducerCopy]
theorem scanStep_cons (source scratch destination : K)
(sourceScratch : source ≠ scratch) (sourceDestination : source ≠ destination)
(scratchDestination : scratch ≠ destination)
(initial : Q) (scanLabel restoreLabel : Λ) (emitterLabel : Q → Bool → Λ)
(program : Λ → TM2.Stmt (Alphabet K) Λ (State σ Q))
(atScan : program scanLabel = scanLoop source scratch initial emitterLabel restoreLabel)
(base : K → List Bool) (symbol : Bool) (input scratchWord outputWord : List Bool)
(ambient : σ) (control : Q) (register : Option Bool) :
TM2.step program ⟨some scanLabel, ((ambient, control), register),
tapes source scratch destination base (symbol :: input) scratchWord outputWord⟩ =
some ⟨some (emitterLabel control symbol), ((ambient, control), some symbol),
tapes source scratch destination base input (symbol :: scratchWord) outputWord⟩ := by
change some (TM2.stepAux (program scanLabel) ((ambient, control), register)
(tapes source scratch destination base (symbol :: input) scratchWord outputWord)) = _
rw [atScan]
simp [scanLoop, TM2.stepAux, sourceScratch, sourceDestination, scratchDestination,
update_source_inline_MachineTransducerCopy, update_scratch_inline_MachineTransducerCopy]
theorem emitterStep (source scratch destination : K)
(transition : Q → Bool → Q) (emit : Q → Bool → List Bool)
(scanLabel : Λ) (emitterLabel : Q → Bool → Λ)
(program : Λ → TM2.Stmt (Alphabet K) Λ (State σ Q))
(atEmitter : ∀ control symbol, program (emitterLabel control symbol) =
emitter destination transition emit scanLabel control symbol)
(base : K → List Bool) (input scratchWord outputWord : List Bool)
(ambient : σ) (control : Q) (symbol : Bool) :
TM2.step program ⟨some (emitterLabel control symbol), ((ambient, control), some symbol),
tapes source scratch destination base input scratchWord outputWord⟩ =
some ⟨some scanLabel, ((ambient, transition control symbol), some symbol),
tapes source scratch destination base input scratchWord
((emit control symbol).reverse ++ outputWord)⟩ := by
change some (TM2.stepAux (program (emitterLabel control symbol)) ((ambient, control), some symbol)
(tapes source scratch destination base input scratchWord outputWord)) = _
rw [atEmitter]
simp only [emitter, TM2.stepAux]
rw [stepAux_pushWord]
simp only [TM2.stepAux, MachineCopy.forkTapes_right]
rw [update_output_inline_MachineTransducerCopy]
theorem twoSteps_cons (source scratch destination : K)
(sourceScratch : source ≠ scratch) (sourceDestination : source ≠ destination)
(scratchDestination : scratch ≠ destination)
(initial : Q) (transition : Q → Bool → Q) (emit : Q → Bool → List Bool)
(scanLabel restoreLabel : Λ) (emitterLabel : Q → Bool → Λ)
(program : Λ → TM2.Stmt (Alphabet K) Λ (State σ Q))
(atScan : program scanLabel = scanLoop source scratch initial emitterLabel restoreLabel)
(atEmitter : ∀ control symbol, program (emitterLabel control symbol) =
emitter destination transition emit scanLabel control symbol)
(base : K → List Bool) (symbol : Bool) (input scratchWord outputWord : List Bool)
(ambient : σ) (control : Q) (register : Option Bool) :
(MachineComposition.advance (TM2.step program))^[2]
(some ⟨some scanLabel, ((ambient, control), register),
tapes source scratch destination base (symbol :: input) scratchWord outputWord⟩) =
some ⟨some scanLabel, ((ambient, transition control symbol), some symbol),
tapes source scratch destination base input (symbol :: scratchWord)
((emit control symbol).reverse ++ outputWord)⟩ := by
change (TM2.step program ⟨some scanLabel, ((ambient, control), register),
tapes source scratch destination base (symbol :: input) scratchWord outputWord⟩).bind
(TM2.step program) = _
rw [scanStep_cons source scratch destination sourceScratch sourceDestination scratchDestination
initial scanLabel restoreLabel emitterLabel program atScan base symbol input scratchWord
outputWord ambient control register]
exact emitterStep source scratch destination transition emit scanLabel emitterLabel program
atEmitter base input (symbol :: scratchWord) outputWord ambient control symbol
theorem scanTrace (source scratch destination : K)
(sourceScratch : source ≠ scratch) (sourceDestination : source ≠ destination)
(scratchDestination : scratch ≠ destination)
(initial : Q) (transition : Q → Bool → Q) (emit : Q → Bool → List Bool)
(scanLabel restoreLabel : Λ) (emitterLabel : Q → Bool → Λ)
(program : Λ → TM2.Stmt (Alphabet K) Λ (State σ Q))
(atScan : program scanLabel = scanLoop source scratch initial emitterLabel restoreLabel)
(atEmitter : ∀ control symbol, program (emitterLabel control symbol) =
emitter destination transition emit scanLabel control symbol)
(base : K → List Bool) (input scratchWord outputWord : List Bool)
(ambient : σ) (control : Q) (register : Option Bool) :
(MachineComposition.advance (TM2.step program))^[2 * input.length + 1]
(some ⟨some scanLabel, ((ambient, control), register),
tapes source scratch destination base input scratchWord outputWord⟩) =
some ⟨some restoreLabel, ((ambient, initial), none),
tapes source scratch destination base [] (input.reverse ++ scratchWord)
((Reduction.MachineTransducer.output transition emit control input).reverse ++ outputWord)⟩ := by
induction input generalizing control scratchWord outputWord register with
| nil =>
simpa only [List.length_nil, Nat.mul_zero, Nat.zero_add, Function.iterate_one,
MachineComposition.advance_some, Reduction.MachineTransducer.output, List.reverse_nil,
List.nil_append] using
scanStep_empty source scratch destination sourceScratch sourceDestination scratchDestination
initial scanLabel restoreLabel emitterLabel program atScan base scratchWord outputWord
ambient control register
| cons symbol input ih =>
rw [List.length_cons, show 2 * (input.length + 1) + 1 = (2 * input.length + 1) + 2 by omega,
Function.iterate_add_apply]
rw [twoSteps_cons source scratch destination sourceScratch sourceDestination scratchDestination
initial transition emit scanLabel restoreLabel emitterLabel program atScan atEmitter base
symbol input scratchWord outputWord ambient control register, ih]
simp only [Reduction.MachineTransducer.output, List.reverse_append, List.reverse_cons,
List.append_assoc, List.singleton_append]
theorem restoreTapes (source scratch destination : K)
(sourceScratch : source ≠ scratch) (sourceDestination : source ≠ destination)
(scratchDestination : scratch ≠ destination)
(base : K → List Bool) (scratchEmpty : base scratch = []) (outputWord : List Bool) :
Reduction.MachineTransfer.tapesAt scratch source
(tapes source scratch destination base [] (base source).reverse outputWord)
[] (base source) = Function.update base destination outputWord := by
funext k
by_cases hs : k = source
· subst k; simp [Reduction.MachineTransfer.tapesAt, sourceDestination]
· by_cases ht : k = scratch
· subst k
simp [Reduction.MachineTransfer.tapesAt, Ne.symm sourceScratch, scratchDestination, scratchEmpty]
· by_cases hd : k = destination
· subst k
simp [Reduction.MachineTransfer.tapesAt, tapes, MachineCopy.forkTapes,
Ne.symm sourceDestination, Ne.symm scratchDestination]
· simp [Reduction.MachineTransfer.tapesAt, tapes, MachineCopy.forkTapes, hs, ht, hd]
theorem transduceCopyTrace (source scratch destination : K)
(sourceScratch : source ≠ scratch) (sourceDestination : source ≠ destination)
(scratchDestination : scratch ≠ destination)
(initial : Q) (transition : Q → Bool → Q) (emit : Q → Bool → List Bool)
(scanLabel restoreLabel : Λ) (emitterLabel : Q → Bool → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet K) Λ (State σ Q))
(atScan : program scanLabel = scanLoop source scratch initial emitterLabel restoreLabel)
(atEmitter : ∀ control symbol, program (emitterLabel control symbol) =
emitter destination transition emit scanLabel control symbol)
(atRestore : program restoreLabel =
Reduction.MachineTransfer.loopAt scratch source id false restoreLabel exit)
(base : K → List Bool) (scratchEmpty : base scratch = [])
(ambient : σ) (control : Q) (register : Option Bool) :
(MachineComposition.advance (TM2.step program))^[3 * (base source).length + 2]
(some ⟨some scanLabel, ((ambient, control), register), base⟩) =
some ⟨exit, ((ambient, initial), none),
Function.update base destination
((Reduction.MachineTransducer.output transition emit control (base source)).reverse ++
base destination)⟩ := by
have scan := scanTrace source scratch destination sourceScratch sourceDestination scratchDestination
initial transition emit scanLabel restoreLabel emitterLabel program atScan atEmitter base
(base source) (base scratch) (base destination) ambient control register
have tapesSelf : tapes source scratch destination base (base source) (base scratch)
(base destination) = base := MachineCopy.forkTapes_self source scratch destination base
rw [tapesSelf] at scan
simp only [scratchEmpty, List.append_nil] at scan
let outputWord :=
(Reduction.MachineTransducer.output transition emit control (base source)).reverse ++ base destination
let scanned := tapes source scratch destination base [] (base source).reverse outputWord
have restore := Reduction.MachineTransfer.transferAt_fromTapes scratch source (Ne.symm sourceScratch)
id false restoreLabel exit program atRestore scanned (ambient, initial) none
change (MachineComposition.advance (TM2.step program))^[(scanned scratch).length + 1]
(some ⟨some restoreLabel, ((ambient, initial), none), scanned⟩) = _ at restore
have scratchWord : scanned scratch = (base source).reverse := by
simp [scanned, scratchDestination]
have sourceWord : scanned source = [] := by
simp [scanned, sourceScratch, sourceDestination]
rw [scratchWord, sourceWord] at restore
simp only [List.map_id, List.reverse_reverse, List.append_nil, List.length_reverse] at restore
rw [restoreTapes source scratch destination sourceScratch sourceDestination scratchDestination
base scratchEmpty outputWord] at restore
rw [show 3 * (base source).length + 2 =
((base source).length + 1) + (2 * (base source).length + 1) by omega,
Function.iterate_add_apply, scan]
exact restore
def transduceCopyInTime (source scratch destination : K)
(sourceScratch : source ≠ scratch) (sourceDestination : source ≠ destination)
(scratchDestination : scratch ≠ destination)
(initial : Q) (transition : Q → Bool → Q) (emit : Q → Bool → List Bool)
(scanLabel restoreLabel : Λ) (emitterLabel : Q → Bool → Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet K) Λ (State σ Q))
(atScan : program scanLabel = scanLoop source scratch initial emitterLabel restoreLabel)
(atEmitter : ∀ control symbol, program (emitterLabel control symbol) =
emitter destination transition emit scanLabel control symbol)
(atRestore : program restoreLabel =
Reduction.MachineTransfer.loopAt scratch source id false restoreLabel exit)
(base : K → List Bool) (scratchEmpty : base scratch = [])
(ambient : σ) (control : Q) (register : Option Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some scanLabel, ((ambient, control), register), base⟩
(some ⟨exit, ((ambient, initial), none), Function.update base destination
((Reduction.MachineTransducer.output transition emit control (base source)).reverse ++
base destination)⟩)
(3 * (base source).length + 2) where
steps := 3 * (base source).length + 2
evals_in_steps := transduceCopyTrace source scratch destination sourceScratch sourceDestination
scratchDestination initial transition emit scanLabel restoreLabel emitterLabel exit program
atScan atEmitter atRestore base scratchEmpty ambient control register
steps_le_m := Nat.le_refl _
omit [DecidableEq K] in
theorem scanLoop_pushBound (source scratch : K) (initial : Q)
(emitterLabel : Q → Bool → Λ) (restoreLabel : Λ) :
Runtime.statementPushBound (scanLoop (σ := σ) source scratch initial emitterLabel restoreLabel) = 1 := by
simp [scanLoop, Runtime.statementPushBound]
omit [DecidableEq K] in
theorem emitter_pushBound (destination : K) (transition : Q → Bool → Q)
(emit : Q → Bool → List Bool) (scanLabel : Λ) (control : Q) (symbol : Bool) :
Runtime.statementPushBound (emitter (σ := σ) destination transition emit scanLabel control symbol) =
(emit control symbol).length := by
simp [emitter, Runtime.statementPushBound, Reduction.MachineSubstitution.statementPushBound_pushWord]
end MaxCutGames.Foundations.Complexity.MachineTransducerCopy
end OAI

Xet Storage Details

Size:
49.7 kB
·
Xet hash:
d23231adef863fb71a4d70118ddb9dcbeb32b2157449ea0dec3b5f0dd35ae675

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