cfahlgren1's picture
download
raw
36.8 kB
import Mathlib
import OAI.Computability.MaxCut.Model
namespace OAI
/-!
Stack-length bounds derived from actual machine transitions. A finite program
has a maximum number of pushes in any one statement. Iterating its transitions
therefore bounds output length by input length plus that constant times elapsed
steps. This proves a consequence of a runtime certificate, not its converse.
-/
namespace MaxCutGames.Foundations.Complexity.Runtime
def statementPushBound {K : Type} {Γ : K → Type} {Λ σ : Type} :
Turing.TM2.Stmt Γ Λ σ → Nat
| .push _ _ next => statementPushBound next + 1
| .peek _ _ next => statementPushBound next
| .pop _ _ next => statementPushBound next
| .load _ next => statementPushBound next
| .branch _ yes no => max (statementPushBound yes) (statementPushBound no)
| .goto _ => 0
| .halt => 0
def maxLabelPushes {K : Type} {Γ : K → Type} {Λ σ : Type}
(program : Λ → Turing.TM2.Stmt Γ Λ σ) : List Λ → Nat
| [] => 0
| label :: rest => max (statementPushBound (program label)) (maxLabelPushes program rest)
theorem maxLabelPushes_ge {K : Type} {Γ : K → Type} {Λ σ : Type}
(program : Λ → Turing.TM2.Stmt Γ Λ σ) (labels : List Λ) (label : Λ)
(member : label ∈ labels) :
statementPushBound (program label) ≤ maxLabelPushes program labels := by
induction labels with
| nil => simp at member
| cons first rest ih =>
simp only [List.mem_cons] at member
rcases member with rfl | member
· exact Nat.le_max_left _ _
· exact Nat.le_trans (ih member) (Nat.le_max_right _ _)
/-- The finite maximum is a proof-bound constant, not an operation of the
machine being analyzed. -/
noncomputable def programPushBound (tm : Turing.FinTM2) : Nat :=
maxLabelPushes tm.m tm.ΛFin.elems.toList
theorem statement_le_programPushBound (tm : Turing.FinTM2) (label : tm.Λ) :
statementPushBound (tm.m label) ≤ programPushBound tm := by
apply maxLabelPushes_ge
simpa using tm.ΛFin.complete label
theorem stepAuxStackLength {K : Type} {Γ : K → Type} {Λ σ : Type} [DecidableEq K]
(stmt : Turing.TM2.Stmt Γ Λ σ) (state : σ) (tapes : ∀ k, List (Γ k)) (k : K) :
((Turing.TM2.stepAux stmt state tapes).stk k).length ≤
(tapes k).length + statementPushBound stmt := by
induction stmt generalizing state tapes with
| push j f next ih =>
have changed : ((Function.update tapes j (f state :: tapes j)) k).length ≤
(tapes k).length + 1 := by
by_cases same : k = j
· subst k; simp
· simp [Function.update, same]
have h := ih state (Function.update tapes j (f state :: tapes j))
simp only [Turing.TM2.stepAux, statementPushBound]
omega
| peek j f next ih =>
simpa only [Turing.TM2.stepAux, statementPushBound] using
ih (f state (tapes j).head?) tapes
| pop j f next ih =>
have changed : ((Function.update tapes j (tapes j).tail) k).length ≤
(tapes k).length := by
by_cases same : k = j
· subst k; simp
· simp [Function.update, same]
have h := ih (f state (tapes j).head?) (Function.update tapes j (tapes j).tail)
simp only [Turing.TM2.stepAux, statementPushBound]
omega
| load f next ih =>
simpa only [Turing.TM2.stepAux, statementPushBound] using ih (f state) tapes
| branch condition yes no ihYes ihNo =>
cases decision : condition state with
| false =>
have h := ihNo state tapes
have hm := Nat.le_max_right (statementPushBound yes) (statementPushBound no)
simp only [Turing.TM2.stepAux, statementPushBound, decision, Bool.cond_false]
omega
| true =>
have h := ihYes state tapes
have hm := Nat.le_max_left (statementPushBound yes) (statementPushBound no)
simp only [Turing.TM2.stepAux, statementPushBound, decision, Bool.cond_true]
omega
| goto f => simp [Turing.TM2.stepAux, statementPushBound]
| halt => simp [Turing.TM2.stepAux, statementPushBound]
private theorem iterateSizeBound_inline_Runtime {α : Type} (f : α → α) (size : α → Nat) (C : Nat)
(oneStep : ∀ x, size (f x) ≤ size x + C) (n : Nat) (x : α) :
size ((f^[n]) x) ≤ size x + n * C := by
induction n with
| zero => simp
| succ n ih =>
have hs := oneStep ((f^[n]) x)
have bound : size (f ((f^[n]) x)) ≤ size x + (n + 1) * C := by
rw [Nat.add_mul, Nat.one_mul]
omega
simpa only [Function.iterate_succ_apply'] using bound
/-- Resource growth along the actual iterated transition witness. -/
theorem executionSizeBound {σ : Type} (step : σ → Option σ) (size : σ → Nat) (C : Nat)
(oneStep : ∀ a b, step a = some b → size b ≤ size a + C)
{start finish : σ} {budget : Nat}
(execution : StateTransition.EvalsToInTime step start (some finish) budget) :
size finish ≤ size start + budget * C := by
let liftedSize : Option σ → Nat := fun state => (state.map size).getD 0
have liftedStep : ∀ state : Option σ,
liftedSize (state.bind step) ≤ liftedSize state + C := by
intro state
cases state with
| none => simp [liftedSize]
| some a =>
cases transition : step a with
| none => simp [liftedSize, transition]
| some b => simpa [liftedSize, transition] using oneStep a b transition
have bound := iterateSizeBound_inline_Runtime (fun state : Option σ => state.bind step)
liftedSize C liftedStep execution.steps (some start)
change liftedSize ((flip bind step)^[execution.steps] (some start)) ≤ _ at bound
rw [execution.evals_in_steps] at bound
simp only [liftedSize, Option.map_some, Option.getD_some] at bound
exact Nat.le_trans bound (Nat.add_le_add_left
(Nat.mul_le_mul_right C execution.steps_le_m) _)
theorem stepStackLength (tm : Turing.FinTM2) (k : tm.K) (a b : tm.Cfg)
(transition : tm.step a = some b) :
(b.stk k).length ≤ (a.stk k).length + programPushBound tm := by
cases a with
| mk label state tapes =>
cases label with
| none => simp [Turing.FinTM2.step, Turing.TM2.step] at transition
| some label =>
have result := Option.some.inj transition
rw [← result]
exact Nat.le_trans (stepAuxStackLength _ _ _ _)
(Nat.add_le_add_left (statement_le_programPushBound tm label) _)
theorem initialStackLength (tm : Turing.FinTM2) (input : List (tm.Γ tm.k₀)) (k : tm.K) :
((Turing.initList tm input).stk k).length ≤ input.length := by
simp only [Turing.initList]
split
next same => subst k; exact Nat.le_refl _
next _ => simp
@[simp] theorem haltedOutputLength (tm : Turing.FinTM2) (output : List (tm.Γ tm.k₁)) :
((Turing.haltList tm output).stk tm.k₁).length = output.length := by
simp [Turing.haltList]
theorem outputLength_le (tm : Turing.FinTM2) (input : List (tm.Γ tm.k₀))
(output : List (tm.Γ tm.k₁)) (budget : Nat)
(execution : Turing.TM2OutputsInTime tm input (some output) budget) :
output.length ≤ input.length + budget * programPushBound tm := by
have bound := executionSizeBound tm.step (fun cfg => (cfg.stk tm.k₁).length)
(programPushBound tm) (stepStackLength tm tm.k₁) execution
calc
output.length = ((Turing.haltList tm output).stk tm.k₁).length := (haltedOutputLength tm output).symm
_ ≤ ((Turing.initList tm input).stk tm.k₁).length + budget * programPushBound tm := bound
_ ≤ input.length + budget * programPushBound tm :=
Nat.add_le_add_right (initialStackLength tm input tm.k₁) _
/-- Polynomial-time computation implies a polynomial bound on serialized
output length. This direction is used for intermediate data in composition. -/
theorem encodedOutputLength {α β αΓ βΓ : Type}
{ea : α → List αΓ} {eb : β → List βΓ} {f : α → β}
(certificate : Turing.TM2ComputableInPolyTime ea eb f) (a : α) :
(eb (f a)).length ≤
(Polynomial.X + Polynomial.C (programPushBound certificate.tm) * certificate.time).eval
(ea a).length := by
have bound := outputLength_le certificate.tm _ _ _ (certificate.outputsFun a)
simpa only [List.length_map, Polynomial.eval_add, Polynomial.eval_X,
Polynomial.eval_mul, Polynomial.eval_C, Nat.mul_comm] using bound
end MaxCutGames.Foundations.Complexity.Runtime
/-! Exact embedding of a finite-stack program into disjoint stacks and labels.
An original halt can either halt or enter an explicit continuation label.
The simulation preserves all extra stacks and the second state component. -/
namespace MaxCutGames.Foundations.Complexity.MachineEmbedding
open Turing.TM2
variable {K E Λ Λextra σ τ : Type} {Γ : K → Type} {Δ : E → Type}
abbrev Alphabet (Γ : K → Type) (Δ : E → Type) : K ⊕ E → Type
| .inl k => Γ k
| .inr e => Δ e
def tapes (source : ∀ k, List (Γ k)) (extra : ∀ e, List (Δ e)) :
∀ j, List (Alphabet Γ Δ j)
| .inl k => source k
| .inr e => extra e
@[simp] theorem tapes_inl (source : ∀ k, List (Γ k)) (extra : ∀ e, List (Δ e))
(k : K) : tapes source extra (.inl k) = source k := rfl
@[simp] theorem tapes_inr (source : ∀ k, List (Γ k)) (extra : ∀ e, List (Δ e))
(e : E) : tapes source extra (.inr e) = extra e := rfl
def label (haltTarget : Option (Λ ⊕ Λextra)) : Option Λ → Option (Λ ⊕ Λextra)
| none => haltTarget
| some l => some (.inl l)
def configuration (haltTarget : Option (Λ ⊕ Λextra)) (extraState : τ)
(extraTapes : ∀ e, List (Δ e)) (c : Cfg Γ Λ σ) :
Cfg (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ) where
l := label haltTarget c.l
var := (c.var, extraState)
stk := tapes c.stk extraTapes
def statement (haltTarget : Option (Λ ⊕ Λextra)) :
Stmt Γ Λ σ → Stmt (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ)
| .push k f next => .push (.inl k) (fun st => f st.1) (statement haltTarget next)
| .peek k f next => .peek (.inl k) (fun st v => (f st.1 v, st.2))
(statement haltTarget next)
| .pop k f next => .pop (.inl k) (fun st v => (f st.1 v, st.2))
(statement haltTarget next)
| .load f next => .load (fun st => (f st.1, st.2)) (statement haltTarget next)
| .branch f yes no => .branch (fun st => f st.1)
(statement haltTarget yes) (statement haltTarget no)
| .goto f => .goto (fun st => .inl (f st.1))
| .halt => match haltTarget with
| none => .halt
| some l => .goto (fun _ => l)
variable [DecidableEq K] [DecidableEq E]
theorem tapes_update (source : ∀ k, List (Γ k)) (extra : ∀ e, List (Δ e))
(k : K) (value : List (Γ k)) :
tapes (Function.update source k value) extra =
Function.update (tapes source extra) (.inl k) value := by
funext j
cases j with
| inl j =>
by_cases h : j = k
· subst j
simp [Function.update]
· simp [Function.update, h]
| inr e => simp [Function.update]
/-- One finite statement executes identically on the original stacks and state.
The entire statement is simulated in exactly one TM2 transition. -/
theorem stepAux_simulation (haltTarget : Option (Λ ⊕ Λextra))
(extraState : τ) (extraTapes : ∀ e, List (Δ e))
(q : Stmt Γ Λ σ) (state : σ) (source : ∀ k, List (Γ k)) :
stepAux (statement haltTarget q) (state, extraState) (tapes source extraTapes) =
configuration haltTarget extraState extraTapes (stepAux q state source) := by
induction q generalizing state source with
| push k f next ih =>
simp only [statement, stepAux, tapes_inl]
rw [← tapes_update]
exact ih state (Function.update source k (f state :: source k))
| peek k f next ih =>
simpa only [statement, stepAux, tapes_inl] using
ih (f state (source k).head?) source
| pop k f next ih =>
simp only [statement, stepAux, tapes_inl]
rw [← tapes_update]
exact ih (f state (source k).head?) (Function.update source k (source k).tail)
| load f next ih =>
simpa only [statement, stepAux] using ih (f state) source
| branch f yes no ihYes ihNo =>
cases h : f state with
| false => simpa only [statement, stepAux, h, Bool.cond_false] using ihNo state source
| true => simpa only [statement, stepAux, h, Bool.cond_true] using ihYes state source
| goto f => rfl
| halt => cases haltTarget <;> rfl
theorem stepAux_preserves_extra_state (haltTarget : Option (Λ ⊕ Λextra))
(extraState : τ) (extraTapes : ∀ e, List (Δ e))
(q : Stmt Γ Λ σ) (state : σ) (source : ∀ k, List (Γ k)) :
(stepAux (statement haltTarget q) (state, extraState)
(tapes source extraTapes)).var.2 = extraState := by
rw [stepAux_simulation]
rfl
theorem stepAux_preserves_extra_tape (haltTarget : Option (Λ ⊕ Λextra))
(extraState : τ) (extraTapes : ∀ e, List (Δ e))
(q : Stmt Γ Λ σ) (state : σ) (source : ∀ k, List (Γ k)) (e : E) :
(stepAux (statement haltTarget q) (state, extraState)
(tapes source extraTapes)).stk (.inr e) = extraTapes e := by
rw [stepAux_simulation]
rfl
def program (haltTarget : Option (Λ ⊕ Λextra)) (source : Λ → Stmt Γ Λ σ)
(extra : Λextra → Stmt (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ)) :
Λ ⊕ Λextra → Stmt (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ)
| .inl l => statement haltTarget (source l)
| .inr l => extra l
theorem step_running (haltTarget : Option (Λ ⊕ Λextra))
(extraState : τ) (extraTapes : ∀ e, List (Δ e))
(source : Λ → Stmt Γ Λ σ)
(extra : Λextra → Stmt (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ))
(l : Λ) (state : σ) (sourceTapes : ∀ k, List (Γ k)) :
step (program haltTarget source extra)
(configuration haltTarget extraState extraTapes ⟨some l, state, sourceTapes⟩) =
some (configuration haltTarget extraState extraTapes
(stepAux (source l) state sourceTapes)) := by
change some (stepAux (statement haltTarget (source l)) (state, extraState)
(tapes sourceTapes extraTapes)) = _
rw [stepAux_simulation]
theorem step_simulation (haltTarget : Option (Λ ⊕ Λextra))
(extraState : τ) (extraTapes : ∀ e, List (Δ e))
(source : Λ → Stmt Γ Λ σ)
(extra : Λextra → Stmt (Alphabet Γ Δ) (Λ ⊕ Λextra) (σ × τ))
(a b : Cfg Γ Λ σ) (h : step source a = some b) :
step (program haltTarget source extra)
(configuration haltTarget extraState extraTapes a) =
some (configuration haltTarget extraState extraTapes b) := by
cases a with
| mk l state sourceTapes =>
cases l with
| none => simp [step] at h
| some l =>
have hb : stepAux (source l) state sourceTapes = b := Option.some.inj h
rw [← hb]
exact step_running haltTarget extraState extraTapes source extra l state sourceTapes
end MaxCutGames.Foundations.Complexity.MachineEmbedding
/-!
Execution transport and polynomial budget arithmetic for genuine TM2 sequential
composition. These lemmas preserve actual transition witnesses. The four-phase
lemma requires all phases to be executions of the same combined program; it is
not a replacement for constructing that program and proving its tape handoffs.
-/
namespace MaxCutGames.Foundations.Complexity.MachineComposition
def advance {σ : Type} (step : σ → Option σ) (state : Option σ) : Option σ :=
state.bind step
@[simp] theorem advance_none {σ : Type} (step : σ → Option σ) :
advance step none = none := rfl
@[simp] theorem advance_some {σ : Type} (step : σ → Option σ) (state : σ) :
advance step (some state) = step state := rfl
@[simp] theorem advance_iterate_none {σ : Type} (step : σ → Option σ) (n : Nat) :
(advance step)^[n] none = none := by
induction n with
| zero => rfl
| succ n ih => rw [Function.iterate_succ_apply', ih, advance_none]
theorem liftSuccessfulTrace {σ τ : Type} (source : σ → Option σ)
(target : τ → Option τ) (embed : σ → τ)
(simulation : ∀ a b, source a = some b → target (embed a) = some (embed b))
(n : Nat) (start finish : σ)
(trace : (advance source)^[n] (some start) = some finish) :
(advance target)^[n] (some (embed start)) = some (embed finish) := by
induction n generalizing start with
| zero =>
have same := Option.some.inj trace
cases same
rfl
| succ n ih =>
rw [Function.iterate_succ_apply] at trace ⊢
change (advance source)^[n] (source start) = some finish at trace
change (advance target)^[n] (target (embed start)) = some (embed finish)
cases firstStep : source start with
| none =>
rw [firstStep, advance_iterate_none] at trace
contradiction
| some intermediate =>
rw [firstStep] at trace
rw [simulation start intermediate firstStep]
exact ih intermediate trace
def liftExecutionInTime {σ τ : Type} (source : σ → Option σ)
(target : τ → Option τ) (embed : σ → τ)
(simulation : ∀ a b, source a = some b → target (embed a) = some (embed b))
{start finish : σ} {budget : Nat}
(execution : StateTransition.EvalsToInTime source start (some finish) budget) :
StateTransition.EvalsToInTime target (embed start) (some (embed finish)) budget where
steps := execution.steps
evals_in_steps := by
have sourceTrace := execution.evals_in_steps
change (advance source)^[execution.steps] (some start) = some finish at sourceTrace
change (advance target)^[execution.steps] (some (embed start)) = some (embed finish)
exact liftSuccessfulTrace source target embed simulation execution.steps start finish sourceTrace
steps_le_m := execution.steps_le_m
@[simp] theorem liftExecutionInTime_steps {σ τ : Type} (source : σ → Option σ)
(target : τ → Option τ) (embed : σ → τ)
(simulation : ∀ a b, source a = some b → target (embed a) = some (embed b))
{start finish : σ} {budget : Nat}
(execution : StateTransition.EvalsToInTime source start (some finish) budget) :
(liftExecutionInTime source target embed simulation execution).steps = execution.steps := rfl
/-- The checked disjoint-stack embedding transports a complete successful run
with no additional transitions, including its final jump to a bridge label. -/
def embeddedExecution {K E Λ Λextra σ τ : Type} {Γ : K → Type} {Δ : E → Type}
[DecidableEq K] [DecidableEq E]
(haltTarget : Option (Λ ⊕ Λextra)) (extraState : τ)
(extraTapes : ∀ e, List (Δ e))
(source : Λ → Turing.TM2.Stmt Γ Λ σ)
(extra : Λextra → Turing.TM2.Stmt (MachineEmbedding.Alphabet Γ Δ)
(Λ ⊕ Λextra) (σ × τ))
{start finish : Turing.TM2.Cfg Γ Λ σ} {budget : Nat}
(execution : StateTransition.EvalsToInTime (Turing.TM2.step source)
start (some finish) budget) :
StateTransition.EvalsToInTime (Turing.TM2.step (MachineEmbedding.program haltTarget source extra))
(MachineEmbedding.configuration haltTarget extraState extraTapes start)
(some (MachineEmbedding.configuration haltTarget extraState extraTapes finish)) budget :=
liftExecutionInTime (Turing.TM2.step source)
(Turing.TM2.step (MachineEmbedding.program haltTarget source extra))
(MachineEmbedding.configuration haltTarget extraState extraTapes)
(MachineEmbedding.step_simulation haltTarget extraState extraTapes source extra) execution
theorem natPolynomial_eval_mono (p : Polynomial Nat) {a b : Nat} (h : a ≤ b) :
p.eval a ≤ p.eval b := by
induction p using Polynomial.induction_on' with
| add p q hp hq =>
simpa only [Polynomial.eval_add] using Nat.add_le_add hp hq
| monomial degree coefficient =>
simp only [Polynomial.eval_monomial]
exact Nat.mul_le_mul_left coefficient (Nat.pow_le_pow_left h degree)
/-- Two reversal transfers each consume `intermediateLength + 1` transitions. -/
noncomputable def compositionPolynomial (first second intermediate : Polynomial Nat) : Polynomial Nat :=
first + Polynomial.C 2 * (intermediate + 1) + second.comp intermediate
theorem compositionBudget_le (first second intermediate : Polynomial Nat)
(inputLength intermediateLength : Nat)
(intermediateBound : intermediateLength ≤ intermediate.eval inputLength) :
first.eval inputLength + 2 * (intermediateLength + 1) + second.eval intermediateLength ≤
(compositionPolynomial first second intermediate).eval inputLength := by
have secondBound := natPolynomial_eval_mono second intermediateBound
have transferBound := Nat.mul_le_mul_left 2 (Nat.add_le_add_right intermediateBound 1)
simp only [compositionPolynomial, Polynomial.eval_add, Polynomial.eval_mul,
Polynomial.eval_C, Polynomial.eval_one, Polynomial.eval_comp]
omega
def fourPhaseExecution {σ : Type} (step : σ → Option σ)
(start afterFirst afterTransfer₁ afterTransfer₂ : σ) (finish : Option σ)
(first second intermediate : Polynomial Nat) (inputLength intermediateLength : Nat)
(intermediateBound : intermediateLength ≤ intermediate.eval inputLength)
(runFirst : StateTransition.EvalsToInTime step start (some afterFirst) (first.eval inputLength))
(transfer₁ : StateTransition.EvalsToInTime step afterFirst (some afterTransfer₁)
(intermediateLength + 1))
(transfer₂ : StateTransition.EvalsToInTime step afterTransfer₁ (some afterTransfer₂)
(intermediateLength + 1))
(runSecond : StateTransition.EvalsToInTime step afterTransfer₂ finish
(second.eval intermediateLength)) :
StateTransition.EvalsToInTime step start finish
((compositionPolynomial first second intermediate).eval inputLength) := by
let firstTwo := StateTransition.EvalsToInTime.trans step _ _ start afterFirst
(some afterTransfer₁) runFirst transfer₁
let firstThree := StateTransition.EvalsToInTime.trans step _ _ start afterTransfer₁
(some afterTransfer₂) firstTwo transfer₂
let allFour := StateTransition.EvalsToInTime.trans step _ _ start afterTransfer₂
finish firstThree runSecond
refine {
toEvalsTo := allFour.toEvalsTo
steps_le_m := Nat.le_trans allFour.steps_le_m ?_
}
have bounded := compositionBudget_le first second intermediate inputLength intermediateLength
intermediateBound
omega
end MaxCutGames.Foundations.Complexity.MachineComposition
/-!
# Certified reversal and relabeling between finite alphabets
The actual TM2 machine transfers a list over a finite alphabet `α` to a second
stack over `β`, reverses its order, and applies a fixed map `f : α → β` to each
symbol. The trace has exactly `input.length + 1` transitions. An explicit
fallback symbol makes the push expression total, including the unused branch
where the internal register is empty.
This primitive supports machine composition when intermediate stack alphabets
differ. It does not yet implement the Unique Games reduction.
-/
namespace MaxCutGames.Reduction.MachineTransfer
open Turing
abbrev alphabet (α β : Type) : Bool → Type
| false => α
| true => β
variable {α β : Type} [Fintype α] [Fintype β]
def loop (f : α → β) (fallback : β) :
TM2.Stmt (alphabet α β) Unit (Option β) :=
.pop false (fun _ head => head.map f)
(.branch Option.isSome
(.push true (fun state => state.getD fallback) (.goto fun _ => ()))
.halt)
def machine (f : α → β) (fallback : β) : FinTM2 where
K := Bool
k₀ := false
k₁ := true
Γ := alphabet α β
Λ := Unit
main := ()
σ := Option β
initialState := none
Γk₀Fin := inferInstanceAs (Fintype α)
m _ := loop f fallback
def tapeStacks (input : List α) (output : List β) :
(side : Bool) → List (alphabet α β side)
| false => input
| true => output
def running (f : α → β) (fallback : β) (input : List α) (output : List β)
(state : Option β) : (machine f fallback).Cfg :=
⟨some (), state, tapeStacks input output⟩
def halted (f : α → β) (fallback : β) (output : List β) : (machine f fallback).Cfg :=
⟨none, none, tapeStacks (α := α) [] output⟩
omit [Fintype α] [Fintype β] in
private theorem update_input_inline_MachineTransfer (input replacement : List α) (output : List β) :
Function.update (tapeStacks input output) false replacement = tapeStacks replacement output := by
funext side
cases side <;> rfl
omit [Fintype α] [Fintype β] in
private theorem update_output_inline_MachineTransfer (input : List α) (output replacement : List β) :
Function.update (tapeStacks input output) true replacement = tapeStacks input replacement := by
funext side
cases side <;> rfl
theorem step_empty (f : α → β) (fallback : β) (output : List β) (state : Option β) :
(machine f fallback).step (running f fallback [] output state) =
some (halted f fallback output) := by
change some (TM2.stepAux (loop f fallback) state (tapeStacks (α := α) [] output)) = _
simp [loop, TM2.stepAux, tapeStacks, halted, Function.update]
rw [update_input_inline_MachineTransfer]
rfl
theorem step_cons (f : α → β) (fallback : β) (head : α) (input : List α)
(output : List β) (state : Option β) :
(machine f fallback).step (running f fallback (head :: input) output state) =
some (running f fallback input (f head :: output) (some (f head))) := by
change some (TM2.stepAux (loop f fallback) state (tapeStacks (head :: input) output)) = _
simp [loop, TM2.stepAux, tapeStacks, running, Function.update]
rw [update_input_inline_MachineTransfer, update_output_inline_MachineTransfer]
rfl
def next (f : α → β) (fallback : β) (configuration : Option (machine f fallback).Cfg) :
Option (machine f fallback).Cfg := configuration.bind (machine f fallback).step
/-- Exact actual-machine trace, for any accumulator and internal register. -/
theorem transfer_steps (f : α → β) (fallback : β) (input : List α) (output : List β)
(state : Option β) :
(next f fallback)^[input.length + 1] (some (running f fallback input output state)) =
some (halted f fallback (input.reverse.map f ++ output)) := by
induction input generalizing output state with
| nil =>
simpa only [List.length_nil, Nat.zero_add, Function.iterate_one, next,
Option.bind_some, List.reverse_nil, List.map_nil, List.nil_append]
using step_empty f fallback output state
| cons head input ih =>
rw [List.length_cons, Function.iterate_succ_apply]
change (next f fallback)^[input.length + 1]
((machine f fallback).step (running f fallback (head :: input) output state)) = _
rw [step_cons, ih]
simp only [List.reverse_cons, List.map_append, List.map_singleton,
List.append_assoc, List.singleton_append]
theorem initList_eq (f : α → β) (fallback : β) (input : List α) :
initList (machine f fallback) input = running f fallback input [] none := by
unfold initList running
congr 1
funext side
cases side <;> rfl
theorem haltList_eq (f : α → β) (fallback : β) (output : List β) :
haltList (machine f fallback) output = halted f fallback output := by
unfold haltList halted
congr 1
funext side
cases side <;> rfl
theorem transfer_init_steps (f : α → β) (fallback : β) (input : List α) :
(next f fallback)^[input.length + 1] (some (initList (machine f fallback) input)) =
some (haltList (machine f fallback) (input.reverse.map f)) := by
erw [initList_eq, haltList_eq]
simpa only [List.append_nil] using! transfer_steps f fallback input [] none
def outputsInTime (f : α → β) (fallback : β) (input : List α) :
TM2OutputsInTime (machine f fallback) input (some (input.reverse.map f))
(input.length + 1) where
steps := input.length + 1
evals_in_steps := transfer_init_steps f fallback input
steps_le_m := Nat.le_refl _
@[simp] theorem outputsInTime_steps (f : α → β) (fallback : β) (input : List α) :
(outputsInTime f fallback input).steps = input.length + 1 := rfl
/-- A genuine finite-machine certificate with identity encodings and bound `X+1`. -/
noncomputable def computableInPolyTime (f : α → β) (fallback : β) :
TM2ComputableInPolyTime (id : List α → List α) (id : List β → List β)
(fun input => input.reverse.map f) where
tm := machine f fallback
inputAlphabet := Equiv.refl α
outputAlphabet := Equiv.refl β
time := Polynomial.X + 1
outputsFun input := by
change TM2OutputsInTime (machine f fallback) (input.map id)
(some ((input.reverse.map f).map id))
((Polynomial.X + 1 : Polynomial Nat).eval input.length)
have hi := @List.map_id ((machine f fallback).Γ (machine f fallback).k₀) input
have ho := @List.map_id ((machine f fallback).Γ (machine f fallback).k₁) (input.reverse.map f)
erw [hi, ho]
simpa only [Polynomial.eval_add, Polynomial.eval_X, Polynomial.eval_one]
using outputsInTime f fallback input
variable {K Λ σ : Type} {Γ : K → Type} [DecidableEq K]
def exitAt (dst : K) (exit : Option Λ) : TM2.Stmt Γ Λ (σ × Option (Γ dst)) :=
match exit with
| none => .halt
| some label => .goto fun _ => label
def loopAt (src dst : K) (f : Γ src → Γ dst) (fallback : Γ dst)
(loopLabel : Λ) (exit : Option Λ) : TM2.Stmt Γ Λ (σ × Option (Γ dst)) :=
.pop src (fun state head => (state.1, head.map f))
(.branch (fun state => state.2.isSome)
(.push dst (fun state => state.2.getD fallback) (.goto fun _ => loopLabel))
(exitAt dst exit))
def tapesAt (src dst : K) (base : (k : K) → List (Γ k))
(input : List (Γ src)) (output : List (Γ dst)) : (k : K) → List (Γ k) :=
Function.update (Function.update base src input) dst output
@[simp] theorem tapesAt_src (src dst : K) (distinct : src ≠ dst)
(base : (k : K) → List (Γ k)) (input : List (Γ src)) (output : List (Γ dst)) :
tapesAt src dst base input output src = input := by
simp [tapesAt, distinct]
@[simp] theorem tapesAt_dst (src dst : K)
(base : (k : K) → List (Γ k)) (input : List (Γ src)) (output : List (Γ dst)) :
tapesAt src dst base input output dst = output := by
simp [tapesAt]
theorem tapesAt_other (src dst k : K) (notSrc : k ≠ src) (notDst : k ≠ dst)
(base : (k : K) → List (Γ k)) (input : List (Γ src)) (output : List (Γ dst)) :
tapesAt src dst base input output k = base k := by
simp [tapesAt, notSrc, notDst]
@[simp] theorem tapesAt_self (src dst : K) (base : (k : K) → List (Γ k)) :
tapesAt src dst base (base src) (base dst) = base := by
simp [tapesAt]
private theorem update_tapesAt_src_inline_MachineTransfer (src dst : K) (distinct : src ≠ dst)
(base : (k : K) → List (Γ k)) (input replacement : List (Γ src))
(output : List (Γ dst)) :
Function.update (tapesAt src dst base input output) src replacement =
tapesAt src dst base replacement output := by
funext k
by_cases hs : k = src
· subst k
simp [tapesAt, distinct]
· by_cases hd : k = dst
· subst k
simp [tapesAt, Ne.symm distinct]
· simp [tapesAt, hs, hd]
private theorem update_tapesAt_dst_inline_MachineTransfer (src dst : K)
(base : (k : K) → List (Γ k)) (input : List (Γ src))
(output replacement : List (Γ dst)) :
Function.update (tapesAt src dst base input output) dst replacement =
tapesAt src dst base input replacement := by
funext k
by_cases hd : k = dst
· subst k
simp [tapesAt]
· simp [tapesAt, hd]
def nextAt (dst : K) (program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst)))
(configuration : Option (TM2.Cfg Γ Λ (σ × Option (Γ dst)))) :
Option (TM2.Cfg Γ Λ (σ × Option (Γ dst))) :=
configuration.bind (TM2.step program)
theorem stepAt_empty (src dst : K) (distinct : src ≠ dst)
(f : Γ src → Γ dst) (fallback : Γ dst) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst)))
(atLoop : program loopLabel = loopAt src dst f fallback loopLabel exit)
(base : (k : K) → List (Γ k)) (output : List (Γ dst))
(ambient : σ) (register : Option (Γ dst)) :
TM2.step program ⟨some loopLabel, (ambient, register), tapesAt src dst base [] output⟩ =
some ⟨exit, (ambient, none), tapesAt src dst base [] output⟩ := by
change some (TM2.stepAux (program loopLabel) (ambient, register)
(tapesAt src dst base [] output)) = _
rw [atLoop]
cases exit <;>
simp [loopAt, exitAt, TM2.stepAux, tapesAt_src, distinct, update_tapesAt_src_inline_MachineTransfer]
theorem stepAt_cons (src dst : K) (distinct : src ≠ dst)
(f : Γ src → Γ dst) (fallback : Γ dst) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst)))
(atLoop : program loopLabel = loopAt src dst f fallback loopLabel exit)
(base : (k : K) → List (Γ k)) (head : Γ src) (input : List (Γ src))
(output : List (Γ dst)) (ambient : σ) (register : Option (Γ dst)) :
TM2.step program
⟨some loopLabel, (ambient, register), tapesAt src dst base (head :: input) output⟩ =
some ⟨some loopLabel, (ambient, some (f head)),
tapesAt src dst base input (f head :: output)⟩ := by
change some (TM2.stepAux (program loopLabel) (ambient, register)
(tapesAt src dst base (head :: input) output)) = _
rw [atLoop]
simp [loopAt, TM2.stepAux, tapesAt_src, tapesAt_dst, distinct,
update_tapesAt_src_inline_MachineTransfer, update_tapesAt_dst_inline_MachineTransfer]
/-- Placement-aware exact transfer, preserving all ambient state and other tapes.
The optional exit label is reached in the same final empty-pop transition. -/
theorem transferAt_steps (src dst : K) (distinct : src ≠ dst)
(f : Γ src → Γ dst) (fallback : Γ dst) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst)))
(atLoop : program loopLabel = loopAt src dst f fallback loopLabel exit)
(base : (k : K) → List (Γ k)) (input : List (Γ src)) (output : List (Γ dst))
(ambient : σ) (register : Option (Γ dst)) :
(nextAt dst program)^[input.length + 1]
(some ⟨some loopLabel, (ambient, register), tapesAt src dst base input output⟩) =
some ⟨exit, (ambient, none), tapesAt src dst base [] (input.reverse.map f ++ output)⟩ := by
induction input generalizing output register with
| nil =>
simpa only [List.length_nil, Nat.zero_add, Function.iterate_one, nextAt,
Option.bind_some, List.reverse_nil, List.map_nil, List.nil_append]
using stepAt_empty src dst distinct f fallback loopLabel exit program atLoop base output
ambient register
| cons head input ih =>
rw [List.length_cons, Function.iterate_succ_apply]
change (nextAt dst program)^[input.length + 1]
(TM2.step program
⟨some loopLabel, (ambient, register), tapesAt src dst base (head :: input) output⟩) = _
rw [stepAt_cons src dst distinct f fallback loopLabel exit program atLoop, ih]
simp only [List.reverse_cons, List.map_append, List.map_singleton,
List.append_assoc, List.singleton_append]
/-- The same trace starting with an arbitrary actual ambient tape family. -/
theorem transferAt_fromTapes (src dst : K) (distinct : src ≠ dst)
(f : Γ src → Γ dst) (fallback : Γ dst) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst)))
(atLoop : program loopLabel = loopAt src dst f fallback loopLabel exit)
(base : (k : K) → List (Γ k)) (ambient : σ) (register : Option (Γ dst)) :
(nextAt dst program)^[(base src).length + 1]
(some ⟨some loopLabel, (ambient, register), base⟩) =
some ⟨exit, (ambient, none),
tapesAt src dst base [] ((base src).reverse.map f ++ base dst)⟩ := by
simpa only [tapesAt_self] using
transferAt_steps src dst distinct f fallback loopLabel exit program atLoop base
(base src) (base dst) ambient register
/-- A timed execution witness suitable for composing this bridge with other
phases of the same actual transition system. -/
def transferAtInTime (src dst : K) (distinct : src ≠ dst)
(f : Γ src → Γ dst) (fallback : Γ dst) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt Γ Λ (σ × Option (Γ dst)))
(atLoop : program loopLabel = loopAt src dst f fallback loopLabel exit)
(base : (k : K) → List (Γ k)) (ambient : σ) (register : Option (Γ dst)) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some loopLabel, (ambient, register), base⟩
(some ⟨exit, (ambient, none),
tapesAt src dst base [] ((base src).reverse.map f ++ base dst)⟩)
((base src).length + 1) where
steps := (base src).length + 1
evals_in_steps := transferAt_fromTapes src dst distinct f fallback loopLabel exit
program atLoop base ambient register
steps_le_m := Nat.le_refl _
end MaxCutGames.Reduction.MachineTransfer
end OAI

Xet Storage Details

Size:
36.8 kB
·
Xet hash:
2d0a32bbd1f16de016665e311b72a8ca5dcab238379cfe3497859834b922386c

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