cfahlgren1/math / lean /OAI /Computability /MaxCut /Machines /PoweringMachineRuntime.lean
cfahlgren1's picture
download
raw
11.5 kB
import Mathlib
import OAI.Computability.MaxCut.Games.PoweringInitializeFrame
import OAI.Computability.MaxCut.Machines.PoweringRuntimeBudget
import OAI.Computability.MaxCut.Games.PoweringPolynomialBudget
namespace OAI
/-! Complete execution of the one fixed powering machine on the actual
port-table input and generic graph-table output encodings. The varying vertex
count remains on unary tapes throughout. -/
namespace MaxCutGames.Foundations.Complexity.PoweringMachineRuntime
open Turing MachineComposition PCP PoweringMachineGlobal
variable {vertices d : Nat}
/-- The actual work tapes after all vertex blocks, before final headers. -/
def beforeFinish (graph : PortTables.Table vertices d) (n : Nat) : Tape n → List Bool :=
PoweringMachineOuterLoop.finalTapes graph n (placement n) (outputTape n)
vertices (Nat.le_refl _) (PoweringInitializeFrame.preparedTapes graph n)
def finishConfiguration (graph : PortTables.Table vertices d) (n : Nat) : (machine d n).Cfg :=
⟨some (finishEntry d n), PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n),
beforeFinish graph n⟩
/-- Initialization and the actual outer loop's proved budgets. -/
def beforeFinishBudget (graph : PortTables.Table vertices d) (n : Nat) : Nat :=
(3 * vertices + 5) + (vertices *
(PoweringMachineVertex.budget d n (PortTables.tableBits graph).length + 2) + 1)
theorem beforeFinish_ready (graph : PortTables.Table vertices d) (n : Nat) :
PoweringMachineOuterLoop.Ready graph n (placement n) (beforeFinish graph n) :=
PoweringMachineOuterLoop.finalTapes_ready graph n (placement n)
(PoweringMachineInitialize.commonPlacement_injective _)
(outputTape n) (fun i => Ne.symm (PoweringMachineInitialize.commonPlacement_ne_finalOutput _ i))
vertices (Nat.le_refl _) (PoweringInitializeFrame.preparedTapes graph n)
(PoweringInitializeFrame.prepared_ready graph n)
theorem beforeFinish_output (graph : PortTables.Table vertices d) (n : Nat) :
beforeFinish graph n (outputTape n) =
(List.finRange vertices).flatMap (PoweringTableLayout.vertexRowBits graph n) := by
rw [beforeFinish, PoweringMachineOuterLoop.finalTapes_output graph n (placement n)
(PoweringMachineInitialize.commonPlacement_injective _) (outputTape n)
(fun i => Ne.symm (PoweringMachineInitialize.commonPlacement_ne_finalOutput _ i))
vertices (Nat.le_refl _) (PoweringInitializeFrame.preparedTapes graph n)
(PoweringInitializeFrame.prepared_ready graph n),
PoweringMachineOuterLoop.prefixRows_all, PoweringInitializeFrame.prepared_output,
List.append_nil]
theorem beforeFinish_vertices (graph : PortTables.Table vertices d) (n : Nat) :
beforeFinish graph n (headerVertices n) = encodeWord vertices := by
rw [beforeFinish, PoweringMachineOuterLoop.finalTapes_other graph n (placement n)
(outputTape n) vertices (Nat.le_refl _) (PoweringInitializeFrame.preparedTapes graph n)
(headerVertices n) (PoweringGlobalConfiguration.headerVertices_ne_output n)
(fun i => Ne.symm (PoweringMachineInitialize.commonPlacement_ne_header _ i .vertices (by decide)))]
exact PoweringInitializeFrame.prepared_vertices graph n
theorem beforeFinish_darts (graph : PortTables.Table vertices d) (n : Nat) :
beforeFinish graph n (headerDarts n) =
encodeWord (PoweringTableLayout.blockSize d n * vertices) := by
rw [beforeFinish, PoweringMachineOuterLoop.finalTapes_other graph n (placement n)
(outputTape n) vertices (Nat.le_refl _) (PoweringInitializeFrame.preparedTapes graph n)
(headerDarts n) (PoweringGlobalConfiguration.headerDarts_ne_output n)
(fun i => Ne.symm (PoweringMachineInitialize.commonPlacement_ne_header _ i .darts (by decide)))]
exact PoweringInitializeFrame.prepared_darts graph n
/-- The serialized result is exactly the actual powering table, including its
header and every relation bit, in the existing output codec. -/
theorem serialized_eq (graph : PortTables.Table vertices d) (n : Nat) :
encodeWords [vertices, PoweringTableLayout.blockSize d n * vertices] ++
beforeFinish graph n (outputTape n) = PoweringTables.outputBits graph n := by
rw [beforeFinish_output, PoweringTableLayout.outputBits_vertexRows]
congr 2
rw [PoweringTableLayout.dartCount_eq_vertex_blocks, Nat.mul_comm]
/-- Actual execution from the literal single-input configuration through every
vertex. No graph-dependent instruction or supplied body-execution premise. -/
def beforeFinishExecution (graph : PortTables.Table vertices d) (n : Nat) :
StateTransition.EvalsToInTime (machine d n).step
(initList (machine d n) (PortTables.tableBits graph))
(some (finishConfiguration graph n)) (beforeFinishBudget graph n) := by
have initialRun := PoweringInitializeFrame.initializeInTime graph n
have loop := PoweringMachineOuterLoop.loopInTime graph n (placement n)
(PoweringMachineInitialize.commonPlacement_injective _) (outputTape n)
(fun i => Ne.symm (PoweringMachineInitialize.commonPlacement_ne_finalOutput _ i))
(guardLabel d n) (bridgeLabel d n) (finishEntry d n) (vertexLabels d n)
(program d n) (atGuard d n) (atBridge d n) (atVertex d n)
vertices (Nat.le_refl _) (PoweringInitializeFrame.preparedTapes graph n)
(PoweringInitializeFrame.prepared_ready graph n)
rw [PoweringInitializeFrame.prepared_counter] at loop
simpa only [beforeFinishBudget, Nat.add_comm] using
(StateTransition.EvalsToInTime.trans (machine d n).step
(3 * vertices + 5)
(vertices * (PoweringMachineVertex.budget d n (PortTables.tableBits graph).length + 2) + 1)
(initList (machine d n) (PortTables.tableBits graph))
(PoweringInitializeFrame.preparedCfg graph n) (some (finishConfiguration graph n))
initialRun loop)
/-- The actual final-copy/cleanup cost on the tapes obtained from the loop. -/
def finishSteps (graph : PortTables.Table vertices d) (n : Nat) : Nat :=
PoweringMachineFinish.steps (enumeration n) (outputTape n) (beforeFinish graph n)
vertices (PoweringTableLayout.blockSize d n * vertices)
/-- The terminal configuration has precisely the actual output stream and no
remaining work tapes. -/
def finishExecution (graph : PortTables.Table vertices d) (n : Nat) :
StateTransition.EvalsToInTime (machine d n).step (finishConfiguration graph n)
(some (haltList (machine d n) (PoweringTables.outputBits graph n)))
(finishSteps graph n) := by
have run := PoweringMachineFinish.traceAt (enumeration n)
(headerVertices n) (headerDarts n) (outputTape n)
(placement n (PoweringMachineTapes.scratch (PoweringMachineRowBody.capacity n)))
(PoweringGlobalConfiguration.headerVertices_ne_scratch n)
(PoweringGlobalConfiguration.headerVertices_ne_output n)
(PoweringGlobalConfiguration.headerDarts_ne_scratch n)
(PoweringGlobalConfiguration.headerDarts_ne_output n)
(PoweringMachineInitialize.commonPlacement_ne_finalOutput _ _)
(finishLabels d n) (program d n) (atFinish d n)
(beforeFinish graph n) vertices (PoweringTableLayout.blockSize d n * vertices) [] []
(by simpa only [List.append_nil] using beforeFinish_vertices graph n)
(by simpa only [List.append_nil] using beforeFinish_darts graph n)
(beforeFinish_ready graph n).scratch
(PoweringMasterState.clean (PoweringMachineRowBody.bufferSize d n)).1 none
rw [serialized_eq] at run
refine { steps := finishSteps graph n, evals_in_steps := ?_, steps_le_m := Nat.le_refl _ }
change (advance (TM2.step (program d n)))^[finishSteps graph n]
(some (finishConfiguration graph n)) = _
rw [PoweringGlobalConfiguration.haltList_eq]
simpa only [finishConfiguration, finishEntry, finishSteps, PoweringMasterState.clean,
PoweringMasterState.withBuffer] using! run
/-- Whole-machine execution on the concrete input and output encodings. -/
def execution (graph : PortTables.Table vertices d) (n : Nat) :
TM2OutputsInTime (machine d n) (PortTables.tableBits graph)
(some (PoweringTables.outputBits graph n))
(beforeFinishBudget graph n + finishSteps graph n) := by
simpa only [TM2OutputsInTime, Option.map_some, Nat.add_comm] using!
(StateTransition.EvalsToInTime.trans (machine d n).step
(beforeFinishBudget graph n) (finishSteps graph n)
(initList (machine d n) (PortTables.tableBits graph))
(finishConfiguration graph n)
(some (haltList (machine d n) (PoweringTables.outputBits graph n)))
(beforeFinishExecution graph n) (finishExecution graph n))
theorem finishSteps_le (graph : PortTables.Table vertices d) (n : Nat) :
finishSteps graph n ≤
2 * (vertices + PoweringTableLayout.blockSize d n * vertices) + 6 +
PoweringRuntimeBudget.tapeCount n *
((PortTables.tableBits graph).length + beforeFinishBudget graph n *
Runtime.programPushBound (machine d n) + vertices +
PoweringTableLayout.blockSize d n * vertices + 3) :=
PoweringRuntimeBudget.finish_steps_le_of_execution d n (PortTables.tableBits graph)
(finishConfiguration graph n) (beforeFinishBudget graph n) vertices
(PoweringTableLayout.blockSize d n * vertices) (beforeFinishExecution graph n)
theorem execution_budget_le (graph : PortTables.Table vertices d) (n : Nat) :
beforeFinishBudget graph n + finishSteps graph n ≤
(PoweringPolynomialBudget.time d n (Runtime.programPushBound (machine d n))
(PoweringRuntimeBudget.tapeCount n)).eval (PortTables.tableBits graph).length := by
exact PoweringPolynomialBudget.execution_budget_le_time d n
(Runtime.programPushBound (machine d n)) (PoweringRuntimeBudget.tapeCount n)
vertices (PortTables.tableBits graph).length (PortTables.vertices_le_tableBits_length graph)
(Nat.le_refl _) (finishSteps_le graph n)
/-- A genuine polynomial-time certificate for the actual finite powering
constructor, with the existing concrete codecs on both sides. -/
noncomputable def computableInPolyTime (d n : Nat) :
TM2ComputableInPolyTime (@PortTables.inputBits d) GenericGraphTables.tableBits
(PoweringTables.transform d n) where
tm := machine d n
inputAlphabet := Equiv.refl Bool
outputAlphabet := Equiv.refl Bool
time := PoweringPolynomialBudget.time d n (Runtime.programPushBound (machine d n))
(PoweringRuntimeBudget.tapeCount n)
outputsFun input := by
rcases input with ⟨vertices, graph⟩
have run := execution graph n
let bounded : TM2OutputsInTime (machine d n) (PortTables.tableBits graph)
(some (PoweringTables.outputBits graph n))
((PoweringPolynomialBudget.time d n (Runtime.programPushBound (machine d n))
(PoweringRuntimeBudget.tapeCount n)).eval (PortTables.tableBits graph).length) :=
⟨run.toEvalsTo, run.steps_le_m.trans (execution_budget_le graph n)⟩
change TM2OutputsInTime (machine d n) ((PortTables.tableBits graph).map id)
(some ((PoweringTables.outputBits graph n).map id)) _
have hi := @List.map_id ((machine d n).Γ (machine d n).k₀)
(PortTables.tableBits graph)
have ho := @List.map_id ((machine d n).Γ (machine d n).k₁)
(PoweringTables.outputBits graph n)
simpa only [hi, ho, PortTables.inputBits] using bounded
/-- Every work tape, as well as the input/output tapes, has a finite alphabet. -/
theorem computableInPolyTime_finite_alphabet (d n : Nat) :
∀ k, Finite ((computableInPolyTime d n).tm.Γ k) := by
intro k
change Finite Bool
infer_instance
end MaxCutGames.Foundations.Complexity.PoweringMachineRuntime
end OAI

Xet Storage Details

Size:
11.5 kB
·
Xet hash:
7c6459670850652aa1a37242526253fea7a967cbee05bf7e76fcf57e0f7d1dd2

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