Download data/lean/OAI/Computability/UniqueGames/Machines/MachineCompositionLemmas.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 40.4 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/UniqueGames/Machines/MachineCompositionLemmas.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Computability/UniqueGames/Machines/MachineCompositionLemmas.lean
-
curl -L -o MachineCompositionLemmas.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/UniqueGames/Machines/MachineCompositionLemmas.lean
40.4 kB
| import OAI.Computability.UniqueGames.Machines.MachineControlLemmas | |
| import OAI.Computability.UniqueGames.Machines.Runtime | |
| namespace OAI | |
| section | |
| /-! | |
| 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 UniqueGamesTheorem.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 UniqueGamesTheorem.Foundations.Complexity.MachineComposition | |
| end | |
| section | |
| namespace UniqueGamesTheorem.Foundations.Complexity.MachineAlphabetTransport | |
| open Turing.TM2 | |
| variable {K Ξ Ο : Type} {Ξ Ξ : K β Type} | |
| def tapes (h : Ξ = Ξ) (source : β k, List (Ξ k)) : β k, List (Ξ k) := h βΈ source | |
| def statement (h : Ξ = Ξ) (source : Stmt Ξ Ξ Ο) : Stmt Ξ Ξ Ο := h βΈ source | |
| def configuration (h : Ξ = Ξ) (source : Cfg Ξ Ξ Ο) : Cfg Ξ Ξ Ο := h βΈ source | |
| def program (h : Ξ = Ξ) (source : Ξ β Stmt Ξ Ξ Ο) : Ξ β Stmt Ξ Ξ Ο := | |
| fun label => statement h (source label) | |
| abbrev castStatement (h : Ξ = Ξ) (source : Stmt Ξ Ξ Ο) : Stmt Ξ Ξ Ο := statement h source | |
| abbrev castConfiguration (h : Ξ = Ξ) (source : Cfg Ξ Ξ Ο) : Cfg Ξ Ξ Ο := configuration h source | |
| abbrev castProgram (h : Ξ = Ξ) (source : Ξ β Stmt Ξ Ξ Ο) : Ξ β Stmt Ξ Ξ Ο := program h source | |
| @[simp] theorem tapes_rfl (source : β k, List (Ξ k)) : tapes rfl source = source := rfl | |
| @[simp] theorem statement_rfl (source : Stmt Ξ Ξ Ο) : statement rfl source = source := rfl | |
| @[simp] theorem configuration_rfl (source : Cfg Ξ Ξ Ο) : configuration rfl source = source := rfl | |
| @[simp] theorem program_rfl (source : Ξ β Stmt Ξ Ξ Ο) : program rfl source = source := rfl | |
| @[simp] theorem program_apply (h : Ξ = Ξ) (source : Ξ β Stmt Ξ Ξ Ο) (label : Ξ) : | |
| program h source label = statement h (source label) := rfl | |
| @[simp] theorem statement_roundtrip (h : Ξ = Ξ) (source : Stmt Ξ Ξ Ο) : | |
| statement h (statement h.symm source) = source := by cases h; rfl | |
| @[simp] theorem statement_symm_roundtrip (h : Ξ = Ξ) (source : Stmt Ξ Ξ Ο) : | |
| statement h.symm (statement h source) = source := by cases h; rfl | |
| @[simp] theorem configuration_roundtrip (h : Ξ = Ξ) (source : Cfg Ξ Ξ Ο) : | |
| configuration h (configuration h.symm source) = source := by cases h; rfl | |
| @[simp] theorem configuration_symm_roundtrip (h : Ξ = Ξ) (source : Cfg Ξ Ξ Ο) : | |
| configuration h.symm (configuration h source) = source := by cases h; rfl | |
| @[simp] theorem program_roundtrip (h : Ξ = Ξ) (source : Ξ β Stmt Ξ Ξ Ο) : | |
| program h (program h.symm source) = source := by cases h; rfl | |
| @[simp] theorem program_symm_roundtrip (h : Ξ = Ξ) (source : Ξ β Stmt Ξ Ξ Ο) : | |
| program h.symm (program h source) = source := by cases h; rfl | |
| @[simp] theorem tapes_roundtrip (h : Ξ = Ξ) (source : β k, List (Ξ k)) : | |
| tapes h (tapes h.symm source) = source := by cases h; rfl | |
| @[simp] theorem tapes_symm_roundtrip (h : Ξ = Ξ) (source : β k, List (Ξ k)) : | |
| tapes h.symm (tapes h source) = source := by cases h; rfl | |
| @[simp] theorem configuration_label (h : Ξ = Ξ) (source : Cfg Ξ Ξ Ο) : | |
| (configuration h source).l = source.l := by cases h; rfl | |
| @[simp] theorem configuration_state (h : Ξ = Ξ) (source : Cfg Ξ Ξ Ο) : | |
| (configuration h source).var = source.var := by cases h; rfl | |
| @[simp] theorem configuration_tapes (h : Ξ = Ξ) (source : Cfg Ξ Ξ Ο) : | |
| (configuration h source).stk = tapes h source.stk := by cases h; rfl | |
| @[simp] theorem configuration_mk (h : Ξ = Ξ) (label : Option Ξ) (state : Ο) | |
| (source : β k, List (Ξ k)) : | |
| configuration h β¨label, state, sourceβ© = β¨label, state, tapes h sourceβ© := by cases h; rfl | |
| @[simp] theorem map_configuration_rfl (source : Option (Cfg Ξ Ξ Ο)) : | |
| source.map (configuration (rfl : Ξ = Ξ)) = source := by cases source <;> rfl | |
| variable [DecidableEq K] | |
| theorem stepAux_simulation (h : Ξ = Ξ) (source : Stmt Ξ Ξ Ο) (state : Ο) | |
| (sourceTapes : β k, List (Ξ k)) : | |
| stepAux (statement h source) state (tapes h sourceTapes) = | |
| configuration h (stepAux source state sourceTapes) := by cases h; rfl | |
| theorem step_simulation (h : Ξ = Ξ) (source : Ξ β Stmt Ξ Ξ Ο) (start : Cfg Ξ Ξ Ο) : | |
| step (program h source) (configuration h start) = | |
| (step source start).map (configuration h) := by | |
| cases h | |
| simp only [program_rfl, configuration_rfl, map_configuration_rfl] | |
| /-- Exact conjugacy of every finite trace, including a `none` endpoint. -/ | |
| theorem trace_transport (h : Ξ = Ξ) (source : Ξ β Stmt Ξ Ξ Ο) | |
| (n : Nat) (start : Option (Cfg Ξ Ξ Ο)) : | |
| (MachineComposition.advance (step (program h source)))^[n] | |
| (start.map (configuration h)) = | |
| ((MachineComposition.advance (step source))^[n] start).map (configuration h) := by | |
| cases h | |
| simp only [program_rfl, map_configuration_rfl] | |
| theorem successfulTrace (h : Ξ = Ξ) (source : Ξ β Stmt Ξ Ξ Ο) | |
| (n : Nat) (start finish : Cfg Ξ Ξ Ο) | |
| (trace : (MachineComposition.advance (step source))^[n] (some start) = some finish) : | |
| (MachineComposition.advance (step (program h source)))^[n] | |
| (some (configuration h start)) = some (configuration h finish) := by | |
| have transported := trace_transport h source n (some start) | |
| simpa only [Option.map_some, trace] using transported | |
| def executionInTime (h : Ξ = Ξ) (source : Ξ β Stmt Ξ Ξ Ο) | |
| {start : Cfg Ξ Ξ Ο} {finish : Option (Cfg Ξ Ξ Ο)} {budget : Nat} | |
| (execution : StateTransition.EvalsToInTime (step source) start finish budget) : | |
| StateTransition.EvalsToInTime (step (program h source)) (configuration h start) | |
| (finish.map (configuration h)) budget where | |
| steps := execution.steps | |
| evals_in_steps := by | |
| cases h | |
| simpa only [program_rfl, configuration_rfl, map_configuration_rfl] | |
| using execution.evals_in_steps | |
| steps_le_m := execution.steps_le_m | |
| @[simp] theorem executionInTime_steps (h : Ξ = Ξ) (source : Ξ β Stmt Ξ Ξ Ο) | |
| {start : Cfg Ξ Ξ Ο} {finish : Option (Cfg Ξ Ξ Ο)} {budget : Nat} | |
| (execution : StateTransition.EvalsToInTime (step source) start finish budget) : | |
| (executionInTime h source execution).steps = execution.steps := rfl | |
| def successfulExecutionInTime (h : Ξ = Ξ) (source : Ξ β Stmt Ξ Ξ Ο) | |
| {start finish : Cfg Ξ Ξ Ο} {budget : Nat} | |
| (execution : StateTransition.EvalsToInTime (step source) start (some finish) budget) : | |
| StateTransition.EvalsToInTime (step (program h source)) (configuration h start) | |
| (some (configuration h finish)) budget := executionInTime h source execution | |
| @[simp] theorem successfulExecutionInTime_steps (h : Ξ = Ξ) (source : Ξ β Stmt Ξ Ξ Ο) | |
| {start finish : Cfg Ξ Ξ Ο} {budget : Nat} | |
| (execution : StateTransition.EvalsToInTime (step source) start (some finish) budget) : | |
| (successfulExecutionInTime h source execution).steps = execution.steps := rfl | |
| end UniqueGamesTheorem.Foundations.Complexity.MachineAlphabetTransport | |
| end | |
| section | |
| /-! Static concatenation of finitely many concrete subroutines on common | |
| tapes. The labels are a finite disjoint sum; no runtime instruction counter | |
| or list is placed in the machine state. The simulation theorem composes the | |
| actual local traces with exactly their summed transition counts. -/ | |
| namespace UniqueGamesTheorem.Foundations.Complexity.MachineFiniteSequence | |
| open Turing MachineComposition | |
| variable {Command : Type} (LocalLabel : Command β Type) | |
| def Label : List Command β Type | |
| | [] => Empty | |
| | command :: commands => LocalLabel command β Label commands | |
| instance labelFintype [β command, Fintype (LocalLabel command)] | |
| (commands : List Command) : Fintype (Label LocalLabel commands) := by | |
| induction commands with | |
| | nil => exact inferInstanceAs (Fintype Empty) | |
| | cons command commands ih => | |
| letI := ih | |
| exact inferInstanceAs (Fintype (LocalLabel command β Label LocalLabel commands)) | |
| instance labelDecidableEq [β command, DecidableEq (LocalLabel command)] | |
| (commands : List Command) : DecidableEq (Label LocalLabel commands) := by | |
| induction commands with | |
| | nil => exact inferInstanceAs (DecidableEq Empty) | |
| | cons command commands ih => | |
| letI := ih | |
| exact inferInstanceAs (DecidableEq (LocalLabel command β Label LocalLabel commands)) | |
| variable (main : β command, LocalLabel command) | |
| variable {K Ξ Ο : Type} {Ξ : K β Type} | |
| def entry : (commands : List Command) β (Label LocalLabel commands β Ξ) β Option Ξ β Option Ξ | |
| | [], _, exit => exit | |
| | command :: _, labels, _ => some (labels (.inl (main command))) | |
| variable (localInstruction : β command, (LocalLabel command β Ξ) β Option Ξ β | |
| LocalLabel command β TM2.Stmt Ξ Ξ Ο) | |
| def instruction : (commands : List Command) β (Label LocalLabel commands β Ξ) β Option Ξ β | |
| Label LocalLabel commands β TM2.Stmt Ξ Ξ Ο | |
| | [], _, _, label => nomatch label | |
| | command :: commands, labels, exit, .inl label => | |
| localInstruction command (fun l => labels (.inl l)) | |
| (entry LocalLabel main commands (fun l => labels (.inr l)) exit) label | |
| | _ :: commands, labels, exit, .inr label => | |
| instruction commands (fun l => labels (.inr l)) exit label | |
| variable {Data : Type} (result : Command β Data β Data) (cost : Command β Data β Nat) | |
| def resultOf : List Command β Data β Data | |
| | [], data => data | |
| | command :: commands, data => resultOf commands (result command data) | |
| def steps : List Command β Data β Nat | |
| | [], _ => 0 | |
| | command :: commands, data => cost command data + steps commands (result command data) | |
| variable [DecidableEq K] | |
| /-- This generic composition lemma retains the local execution obligations. | |
| Concrete callers discharge them with the rotor, comparison and row programs. -/ | |
| theorem trace (program : Ξ β TM2.Stmt Ξ Ξ Ο) | |
| (invariant : Data β Prop) (state : Data β Ο) (tapes : Data β β k, List (Ξ k)) | |
| (commands : List Command) | |
| (localInvariant : β command β commands, β data, invariant data β | |
| invariant (result command data)) | |
| (localTrace : β command β commands, β (labels : LocalLabel command β Ξ) (exit : Option Ξ), | |
| (β l, program (labels l) = localInstruction command labels exit l) β | |
| β data, invariant data β | |
| (advance (TM2.step program))^[cost command data] | |
| (some β¨some (labels (main command)), state data, tapes dataβ©) = | |
| some β¨exit, state (result command data), tapes (result command data)β©) | |
| (labels : Label LocalLabel commands β Ξ) (exit : Option Ξ) | |
| (atLabels : β l, program (labels l) = | |
| instruction LocalLabel main localInstruction commands labels exit l) | |
| (data : Data) (valid : invariant data) : | |
| (advance (TM2.step program))^[steps result cost commands data] | |
| (some β¨entry LocalLabel main commands labels exit, state data, tapes dataβ©) = | |
| some β¨exit, state (resultOf result commands data), tapes (resultOf result commands data)β© := by | |
| induction commands generalizing data with | |
| | nil => rfl | |
| | cons command commands ih => | |
| have firstRun := localTrace command (by simp) | |
| (fun l => labels (.inl l)) | |
| (entry LocalLabel main commands (fun l => labels (.inr l)) exit) | |
| (fun l => atLabels (.inl l)) data valid | |
| have nextValid := localInvariant command (by simp) data valid | |
| have tailRun := ih | |
| (fun c hc d hd => localInvariant c (by simp [hc]) d hd) | |
| (fun c hc => localTrace c (by simp [hc])) | |
| (fun l => labels (.inr l)) (fun l => atLabels (.inr l)) | |
| (result command data) nextValid | |
| rw [steps, Nat.add_comm, Function.iterate_add_apply] | |
| change (advance (TM2.step program))^[steps result cost commands (result command data)] | |
| ((advance (TM2.step program))^[cost command data] | |
| (some β¨some (labels (.inl (main command))), state data, tapes dataβ©)) = _ | |
| rw [firstRun] | |
| exact tailRun | |
| end UniqueGamesTheorem.Foundations.Complexity.MachineFiniteSequence | |
| end | |
| section | |
| /-! | |
| # An actual finite machine for a fixed-size Boolean block map | |
| The program parameters `N`, `M`, and `F` are fixed. The local register has | |
| exactly `N` bits. A syntactic chain of `N` pops fills that register, and a | |
| syntactic chain of `M` pushes writes `F` of the register. No instruction | |
| applies `F` to an unbounded word. An arbitrary ambient state is preserved, | |
| and the local register is reset before the optional continuation. | |
| TM2 executes a whole finite statement in one transition. The push bound | |
| below separately records the exact `M` pushes in this statement. | |
| -/ | |
| namespace UniqueGamesTheorem.Foundations.Complexity.MachineFixedBlockMap | |
| open Turing | |
| abbrev Buffer (N : Nat) := Fin N β Bool | |
| def emptyBuffer (N : Nat) : Buffer N := fun _ => false | |
| section Chains | |
| variable {K Ξ Ο : Type} {N : Nat} | |
| def readSlots (src : K) : List (Fin N) β | |
| TM2.Stmt (fun _ : K => Bool) Ξ (Ο Γ Buffer N) β | |
| TM2.Stmt (fun _ : K => Bool) Ξ (Ο Γ Buffer N) | |
| | [], next => next | |
| | i :: slots, next => | |
| .pop src (fun state head => | |
| (state.1, Function.update state.2 i (head.getD false))) | |
| (readSlots src slots next) | |
| /-- Proof-level description of the finite register updates. -/ | |
| def fill (slots : List (Fin N)) (bits buffer : Buffer N) : Buffer N := | |
| slots.foldl (fun buffer i => Function.update buffer i (bits i)) buffer | |
| theorem fill_apply (slots : List (Fin N)) (bits buffer : Buffer N) (i : Fin N) : | |
| fill slots bits buffer i = if i β slots then bits i else buffer i := by | |
| induction slots generalizing buffer with | |
| | nil => simp [fill] | |
| | cons j slots ih => | |
| change fill slots bits (Function.update buffer j (bits j)) i = _ | |
| rw [ih] | |
| by_cases hm : i β slots | |
| Β· simp [hm] | |
| Β· by_cases he : i = j <;> simp [hm, he] | |
| @[simp] theorem fill_all (bits buffer : Buffer N) : | |
| fill (List.ofFn id) bits buffer = bits := by | |
| funext i | |
| simp [fill_apply, List.mem_ofFn] | |
| variable [DecidableEq K] | |
| /-- The input symbols are physically popped; the remaining suffix is exact. -/ | |
| theorem stepAux_readSlots (src : K) (slots : List (Fin N)) | |
| (next : TM2.Stmt (fun _ : K => Bool) Ξ (Ο Γ Buffer N)) | |
| (ambient : Ο) (bits buffer : Buffer N) (tapes : K β List Bool) | |
| (suffix : List Bool) : | |
| TM2.stepAux (readSlots src slots next) (ambient, buffer) | |
| (Function.update tapes src (slots.map bits ++ suffix)) = | |
| TM2.stepAux next (ambient, fill slots bits buffer) | |
| (Function.update tapes src suffix) := by | |
| induction slots generalizing buffer tapes with | |
| | nil => simp [readSlots, fill] | |
| | cons i slots ih => | |
| simpa only [readSlots, List.map_cons, List.cons_append, TM2.stepAux, | |
| Function.update_self, List.head?_cons, Option.getD_some, List.tail_cons, | |
| Function.update_idem, fill, List.foldl_cons] using | |
| ih (Function.update buffer i (bits i)) tapes | |
| theorem stepAux_readAll (src : K) | |
| (next : TM2.Stmt (fun _ : K => Bool) Ξ (Ο Γ Buffer N)) | |
| (state : Ο Γ Buffer N) (bits : Buffer N) (tapes : K β List Bool) | |
| (suffix : List Bool) (hinput : tapes src = List.ofFn bits ++ suffix) : | |
| TM2.stepAux (readSlots src (List.ofFn id) next) state tapes = | |
| TM2.stepAux next (state.1, bits) (Function.update tapes src suffix) := by | |
| have h := stepAux_readSlots src (List.ofFn id) next state.1 bits state.2 tapes suffix | |
| have hin : Function.update tapes src ((List.ofFn id).map bits ++ suffix) = tapes := by | |
| simpa only [List.map_ofFn, Function.comp_id, β hinput] using | |
| Function.update_eq_self src tapes | |
| rw [hin, fill_all] at h | |
| exact h | |
| omit [DecidableEq K] in | |
| theorem statementPushBound_readSlots (src : K) (slots : List (Fin N)) | |
| (next : TM2.Stmt (fun _ : K => Bool) Ξ (Ο Γ Buffer N)) : | |
| Runtime.statementPushBound (readSlots src slots next) = | |
| Runtime.statementPushBound next := by | |
| induction slots with | |
| | nil => rfl | |
| | cons i slots ih => exact ih | |
| /-- The slot list is a fixed part of the syntax; each instruction reads only | |
| the finite internal state. Push order is reversed on the stack. -/ | |
| def writeSlots {M : Nat} (dst : K) (emit : Ο β Buffer M) : List (Fin M) β | |
| TM2.Stmt (fun _ : K => Bool) Ξ Ο β TM2.Stmt (fun _ : K => Bool) Ξ Ο | |
| | [], next => next | |
| | i :: slots, next => .push dst (fun state => emit state i) (writeSlots dst emit slots next) | |
| theorem stepAux_writeSlots {M : Nat} (dst : K) (emit : Ο β Buffer M) | |
| (slots : List (Fin M)) (next : TM2.Stmt (fun _ : K => Bool) Ξ Ο) | |
| (state : Ο) (tapes : K β List Bool) : | |
| TM2.stepAux (writeSlots dst emit slots next) state tapes = | |
| TM2.stepAux next state | |
| (Function.update tapes dst ((slots.map (emit state)).reverse ++ tapes dst)) := by | |
| induction slots generalizing tapes with | |
| | nil => simp [writeSlots] | |
| | cons i slots ih => | |
| simp only [writeSlots, TM2.stepAux] | |
| rw [ih] | |
| simp only [Function.update_self, Function.update_idem, List.map_cons, | |
| List.reverse_cons, List.append_assoc, List.singleton_append] | |
| omit [DecidableEq K] in | |
| theorem statementPushBound_writeSlots {M : Nat} (dst : K) (emit : Ο β Buffer M) | |
| (slots : List (Fin M)) (next : TM2.Stmt (fun _ : K => Bool) Ξ Ο) : | |
| Runtime.statementPushBound (writeSlots dst emit slots next) = | |
| slots.length + Runtime.statementPushBound next := by | |
| induction slots with | |
| | nil => simp [writeSlots] | |
| | cons i slots ih => | |
| simp only [writeSlots, Runtime.statementPushBound, ih, List.length_cons] | |
| omega | |
| end Chains | |
| section Block | |
| variable {K Ξ Ο : Type} {N M : Nat} | |
| /-- Read a fixed block, write its fixed finite transformation, reset the local | |
| register, and execute the next finite statement. -/ | |
| def blockStmt (src dst : K) (F : Buffer N β Buffer M) | |
| (next : TM2.Stmt (fun _ : K => Bool) Ξ (Ο Γ Buffer N)) : | |
| TM2.Stmt (fun _ : K => Bool) Ξ (Ο Γ Buffer N) := | |
| readSlots src (List.ofFn id) | |
| (writeSlots dst (fun state => F state.2) (List.ofFn id).reverse | |
| (.load (fun state => (state.1, emptyBuffer N)) next)) | |
| def finishAt (exit : Option Ξ) : TM2.Stmt (fun _ : K => Bool) Ξ Ο := | |
| match exit with | |
| | none => .halt | |
| | some label => .goto fun _ => label | |
| def blockMapAt (src dst : K) (F : Buffer N β Buffer M) (exit : Option Ξ) : | |
| TM2.Stmt (fun _ : K => Bool) Ξ (Ο Γ Buffer N) := | |
| blockStmt src dst F (finishAt exit) | |
| theorem statementPushBound_blockStmt (src dst : K) (F : Buffer N β Buffer M) | |
| (next : TM2.Stmt (fun _ : K => Bool) Ξ (Ο Γ Buffer N)) : | |
| Runtime.statementPushBound (blockStmt src dst F next) = | |
| M + Runtime.statementPushBound next := by | |
| simp only [blockStmt, statementPushBound_readSlots, statementPushBound_writeSlots, | |
| Runtime.statementPushBound, List.length_reverse, List.length_ofFn] | |
| theorem statementPushBound_blockMapAt (src dst : K) (F : Buffer N β Buffer M) | |
| (exit : Option Ξ) : | |
| Runtime.statementPushBound (blockMapAt (Ο := Ο) src dst F exit) = M := by | |
| cases exit <;> simp [blockMapAt, statementPushBound_blockStmt, finishAt, | |
| Runtime.statementPushBound] | |
| variable [DecidableEq K] | |
| theorem stepAux_blockStmt (src dst : K) (F : Buffer N β Buffer M) | |
| (next : TM2.Stmt (fun _ : K => Bool) Ξ (Ο Γ Buffer N)) | |
| (hne : src β dst) (bits : Buffer N) (suffix : List Bool) | |
| (state : Ο Γ Buffer N) (tapes : K β List Bool) | |
| (hinput : tapes src = List.ofFn bits ++ suffix) : | |
| TM2.stepAux (blockStmt src dst F next) state tapes = | |
| TM2.stepAux next (state.1, emptyBuffer N) | |
| (Function.update (Function.update tapes src suffix) dst | |
| (List.ofFn (F bits) ++ tapes dst)) := by | |
| unfold blockStmt | |
| rw [stepAux_readAll src _ state bits tapes suffix hinput, stepAux_writeSlots] | |
| simp only [List.map_reverse, List.map_ofFn, Function.comp_id, List.reverse_reverse, | |
| Function.update_of_ne (Ne.symm hne), TM2.stepAux] | |
| /-- The suffix and all unmentioned tapes are preserved, and the local register | |
| is reset, in the actual transition semantics. -/ | |
| theorem stepAux_blockMapAt (src dst : K) (F : Buffer N β Buffer M) | |
| (exit : Option Ξ) (hne : src β dst) (bits : Buffer N) (suffix : List Bool) | |
| (state : Ο Γ Buffer N) (tapes : K β List Bool) | |
| (hinput : tapes src = List.ofFn bits ++ suffix) : | |
| TM2.stepAux (blockMapAt src dst F exit) state tapes = | |
| { l := exit, var := (state.1, emptyBuffer N), | |
| stk := Function.update (Function.update tapes src suffix) dst | |
| (List.ofFn (F bits) ++ tapes dst) } := by | |
| rw [blockMapAt, stepAux_blockStmt src dst F _ hne bits suffix state tapes hinput] | |
| cases exit <;> rfl | |
| /-- Placement in an arbitrary program gives one actual TM2 transition. -/ | |
| theorem step_blockMapAt (src dst : K) (F : Buffer N β Buffer M) | |
| (exit : Option Ξ) | |
| (program : Ξ β TM2.Stmt (fun _ : K => Bool) Ξ (Ο Γ Buffer N)) | |
| (label : Ξ) (hprogram : program label = blockMapAt src dst F exit) | |
| (hne : src β dst) (bits : Buffer N) (suffix : List Bool) | |
| (state : Ο Γ Buffer N) (tapes : K β List Bool) | |
| (hinput : tapes src = List.ofFn bits ++ suffix) : | |
| TM2.step program { l := some label, var := state, stk := tapes } = | |
| some { l := exit | |
| var := (state.1, emptyBuffer N) | |
| stk := Function.update (Function.update tapes src suffix) dst | |
| (List.ofFn (F bits) ++ tapes dst) } := by | |
| simp only [TM2.step, hprogram, | |
| stepAux_blockMapAt src dst F exit hne bits suffix state tapes hinput] | |
| end Block | |
| /-- The fixed block map is also a concrete machine with two Boolean tapes and | |
| a finite register, independently of any ambient placement. -/ | |
| def machine {N M : Nat} (F : Buffer N β Buffer M) : FinTM2 where | |
| K := Bool | |
| kβ := false | |
| kβ := true | |
| Ξ _ := Bool | |
| Ξ := Unit | |
| main := () | |
| Ο := Unit Γ Buffer N | |
| initialState := ((), emptyBuffer N) | |
| m _ := blockMapAt false true F none | |
| theorem machine_statementPushBound {N M : Nat} (F : Buffer N β Buffer M) : | |
| Runtime.statementPushBound ((machine F).m ()) = M := | |
| statementPushBound_blockMapAt false true F none | |
| end UniqueGamesTheorem.Foundations.Complexity.MachineFixedBlockMap | |
| end | |
| section | |
| /-! | |
| # Exact register-equivalence transport for finite-stack programs | |
| The instruction translation reuses `MachineControl.statement` with unchanged | |
| labels. An explicit register equivalence changes neither tapes nor labels. | |
| The transition function, every iterated trace, and runtime witnesses are | |
| transported in exactly the same number of transitions, including failed or | |
| halted traces. No simulation premise is assumed. | |
| -/ | |
| namespace UniqueGamesTheorem.Foundations.Complexity.MachineStateEquiv | |
| open Turing.TM2 | |
| variable {K Ξ Ο Ο : Type} {Ξ : K β Type} | |
| /-- Translate instructions using the checked recursive translator, keeping | |
| every tape and destination label unchanged. -/ | |
| abbrev statement (e : Ο β Ο) : Stmt Ξ Ξ Ο β Stmt Ξ Ξ Ο := | |
| MachineControl.statement (id : Ξ β Ξ) e | |
| /-- Translating every instruction back restores the original statement, | |
| including its state-dependent instruction functions. -/ | |
| @[simp] theorem statement_symm_statement (e : Ο β Ο) (q : Stmt Ξ Ξ Ο) : | |
| statement e.symm (statement e q) = q := by | |
| induction q <;> | |
| simp_all only [statement, MachineControl.statement, Equiv.symm_symm, | |
| Equiv.symm_apply_apply, id_eq] | |
| def configuration (e : Ο β Ο) (c : Cfg Ξ Ξ Ο) : Cfg Ξ Ξ Ο := | |
| β¨c.l, e c.var, c.stkβ© | |
| @[simp] theorem configuration_label (e : Ο β Ο) (c : Cfg Ξ Ξ Ο) : | |
| (configuration e c).l = c.l := rfl | |
| @[simp] theorem configuration_state (e : Ο β Ο) (c : Cfg Ξ Ξ Ο) : | |
| (configuration e c).var = e c.var := rfl | |
| @[simp] theorem configuration_tapes (e : Ο β Ο) (c : Cfg Ξ Ξ Ο) : | |
| (configuration e c).stk = c.stk := rfl | |
| @[simp] theorem configuration_symm_configuration (e : Ο β Ο) (c : Cfg Ξ Ξ Ο) : | |
| configuration e.symm (configuration e c) = c := by | |
| cases c | |
| simp only [configuration, Equiv.symm_apply_apply] | |
| @[simp] theorem configuration_configuration_symm (e : Ο β Ο) (c : Cfg Ξ Ξ Ο) : | |
| configuration e (configuration e.symm c) = c := by | |
| cases c | |
| simp only [configuration, Equiv.apply_symm_apply] | |
| /-- This is a bijection on actual machine configurations, not just on states. -/ | |
| def configurationEquiv (e : Ο β Ο) : Cfg Ξ Ξ Ο β Cfg Ξ Ξ Ο where | |
| toFun := configuration e | |
| invFun := configuration e.symm | |
| left_inv := configuration_symm_configuration e | |
| right_inv := configuration_configuration_symm e | |
| @[simp] theorem configurationEquiv_apply (e : Ο β Ο) (c : Cfg Ξ Ξ Ο) : | |
| configurationEquiv e c = configuration e c := rfl | |
| @[simp] theorem control_configuration (e : Ο β Ο) (c : Cfg Ξ Ξ Ο) : | |
| MachineControl.configuration (id : Ξ β Ξ) e c = configuration e c := by | |
| cases c | |
| simp [MachineControl.configuration, configuration] | |
| def program (e : Ο β Ο) (source : Ξ β Stmt Ξ Ξ Ο) : Ξ β Stmt Ξ Ξ Ο := | |
| fun l => statement e (source l) | |
| @[simp] theorem program_symm_program (e : Ο β Ο) (source : Ξ β Stmt Ξ Ξ Ο) : | |
| program e.symm (program e source) = source := by | |
| funext l | |
| exact statement_symm_statement e (source l) | |
| /-- The register change does not alter the finite statement's push bound. -/ | |
| theorem statementPushBound (e : Ο β Ο) (q : Stmt Ξ Ξ Ο) : | |
| Runtime.statementPushBound (statement e q) = Runtime.statementPushBound q := by | |
| induction q <;> | |
| simp_all only [statement, MachineControl.statement, Runtime.statementPushBound] | |
| /-- Exactly the same finite label set supports the translated statement. -/ | |
| theorem supportsStmt_iff (e : Ο β Ο) (S : Finset Ξ) (q : Stmt Ξ Ξ Ο) : | |
| SupportsStmt S (statement e q) β SupportsStmt S q := by | |
| induction q with | |
| | push k f next ih => | |
| simpa only [statement, MachineControl.statement, SupportsStmt] using ih | |
| | peek k f next ih => | |
| simpa only [statement, MachineControl.statement, SupportsStmt] using ih | |
| | pop k f next ih => | |
| simpa only [statement, MachineControl.statement, SupportsStmt] using ih | |
| | load f next ih => | |
| simpa only [statement, MachineControl.statement, SupportsStmt] using ih | |
| | branch f yes no ihYes ihNo => | |
| simp only [statement, MachineControl.statement, SupportsStmt, ihYes, ihNo] | |
| | goto f => | |
| change (β state : Ο, f (e.symm state) β S) β β state : Ο, f state β S | |
| constructor | |
| Β· intro h state | |
| simpa only [Equiv.symm_apply_apply] using h (e state) | |
| Β· intro h state | |
| exact h (e.symm state) | |
| | halt => rfl | |
| theorem supports_iff [Inhabited Ξ] (e : Ο β Ο) (source : Ξ β Stmt Ξ Ξ Ο) | |
| (S : Finset Ξ) : Supports (program e source) S β Supports source S := by | |
| simp only [Supports, program, supportsStmt_iff] | |
| variable [DecidableEq K] | |
| /-- The actual recursively interpreted statement commutes with the register | |
| equivalence, with exactly the same tape updates. -/ | |
| theorem stepAux_transport (e : Ο β Ο) (q : Stmt Ξ Ξ Ο) (state : Ο) | |
| (tapes : β k, List (Ξ k)) : | |
| stepAux (statement e q) (e state) tapes = | |
| configuration e (stepAux q state tapes) := by | |
| simpa only [statement, control_configuration] using | |
| MachineControl.stepAux_simulation (id : Ξ β Ξ) e q state tapes | |
| theorem stepAux_transport_symm (e : Ο β Ο) (q : Stmt Ξ Ξ Ο) (state : Ο) | |
| (tapes : β k, List (Ξ k)) : | |
| stepAux (statement e q) state tapes = | |
| configuration e (stepAux q (e.symm state) tapes) := by | |
| simpa only [Equiv.apply_symm_apply] using | |
| stepAux_transport e q (e.symm state) tapes | |
| /-- Exact conjugacy also covers the halted configuration and the `none` step. -/ | |
| theorem step_transport (e : Ο β Ο) (source : Ξ β Stmt Ξ Ξ Ο) (c : Cfg Ξ Ξ Ο) : | |
| step (program e source) (configuration e c) = | |
| (step source c).map (configuration e) := by | |
| cases c with | |
| | mk l state tapes => | |
| cases l with | |
| | none => rfl | |
| | some l => | |
| change some (stepAux (statement e (source l)) (e state) tapes) = _ | |
| rw [stepAux_transport] | |
| rfl | |
| theorem step_iff (e : Ο β Ο) (source : Ξ β Stmt Ξ Ξ Ο) (a b : Cfg Ξ Ξ Ο) : | |
| step (program e source) (configuration e a) = some (configuration e b) β | |
| step source a = some b := by | |
| rw [step_transport] | |
| change (step source a).map (configuration e) = (some b).map (configuration e) β _ | |
| constructor | |
| Β· intro h | |
| apply Option.map_injective (configurationEquiv e).injective | |
| change (step source a).map (configuration e) = (some b).map (configuration e) | |
| exact h | |
| Β· exact congrArg (Option.map (configuration e)) | |
| theorem advance_transport (e : Ο β Ο) (source : Ξ β Stmt Ξ Ξ Ο) | |
| (c : Option (Cfg Ξ Ξ Ο)) : | |
| MachineComposition.advance (step (program e source)) (c.map (configuration e)) = | |
| (MachineComposition.advance (step source) c).map (configuration e) := by | |
| cases c with | |
| | none => rfl | |
| | some c => exact step_transport e source c | |
| /-- Every intermediate configuration is transported at its original time | |
| index. This includes traces whose option-valued result is `none`. -/ | |
| theorem iterate_transport (e : Ο β Ο) (source : Ξ β Stmt Ξ Ξ Ο) (n : Nat) | |
| (c : Option (Cfg Ξ Ξ Ο)) : | |
| (MachineComposition.advance (step (program e source)))^[n] | |
| (c.map (configuration e)) = | |
| ((MachineComposition.advance (step source))^[n] c).map (configuration e) := by | |
| induction n with | |
| | zero => rfl | |
| | succ n ih => | |
| rw [Function.iterate_succ_apply', ih, advance_transport, | |
| Function.iterate_succ_apply'] | |
| /-- A successful running trace exists for exactly the same number of steps in | |
| the translated and original program. -/ | |
| theorem trace_iff (e : Ο β Ο) (source : Ξ β Stmt Ξ Ξ Ο) (n : Nat) | |
| (a b : Cfg Ξ Ξ Ο) : | |
| (MachineComposition.advance (step (program e source)))^[n] | |
| (some (configuration e a)) = some (configuration e b) β | |
| (MachineComposition.advance (step source))^[n] (some a) = some b := by | |
| change (MachineComposition.advance (step (program e source)))^[n] | |
| ((some a).map (configuration e)) = (some b).map (configuration e) β _ | |
| rw [iterate_transport] | |
| constructor | |
| Β· intro h | |
| apply Option.map_injective (configurationEquiv e).injective | |
| change ((MachineComposition.advance (step source))^[n] (some a)).map (configuration e) = | |
| (some b).map (configuration e) | |
| exact h | |
| Β· exact congrArg (Option.map (configuration e)) | |
| theorem trace (e : Ο β Ο) (source : Ξ β Stmt Ξ Ξ Ο) (n : Nat) | |
| (a b : Cfg Ξ Ξ Ο) | |
| (run : (MachineComposition.advance (step source))^[n] (some a) = some b) : | |
| (MachineComposition.advance (step (program e source)))^[n] | |
| (some (configuration e a)) = some (configuration e b) := | |
| (trace_iff e source n a b).2 run | |
| /-- Transport an actual runtime witness without changing its step count or | |
| budget; the final option may be either a configuration or `none`. -/ | |
| def execution (e : Ο β Ο) (source : Ξ β Stmt Ξ Ξ Ο) | |
| {start : Cfg Ξ Ξ Ο} {finish : Option (Cfg Ξ Ξ Ο)} {budget : Nat} | |
| (run : StateTransition.EvalsToInTime (step source) start finish budget) : | |
| StateTransition.EvalsToInTime (step (program e source)) | |
| (configuration e start) (finish.map (configuration e)) budget where | |
| steps := run.steps | |
| evals_in_steps := by | |
| have h := run.evals_in_steps | |
| change (MachineComposition.advance (step source))^[run.steps] (some start) = finish at h | |
| change (MachineComposition.advance (step (program e source)))^[run.steps] | |
| ((some start).map (configuration e)) = finish.map (configuration e) | |
| rw [iterate_transport, h] | |
| steps_le_m := run.steps_le_m | |
| @[simp] theorem execution_steps (e : Ο β Ο) (source : Ξ β Stmt Ξ Ξ Ο) | |
| {start : Cfg Ξ Ξ Ο} {finish : Option (Cfg Ξ Ξ Ο)} {budget : Nat} | |
| (run : StateTransition.EvalsToInTime (step source) start finish budget) : | |
| (execution e source run).steps = run.steps := rfl | |
| /-- Conversely, a translated runtime witness determines the original witness | |
| at exactly the same number of transitions. -/ | |
| def execution_reflect (e : Ο β Ο) (source : Ξ β Stmt Ξ Ξ Ο) | |
| {start : Cfg Ξ Ξ Ο} {finish : Option (Cfg Ξ Ξ Ο)} {budget : Nat} | |
| (run : StateTransition.EvalsToInTime (step (program e source)) | |
| (configuration e start) (finish.map (configuration e)) budget) : | |
| StateTransition.EvalsToInTime (step source) start finish budget where | |
| steps := run.steps | |
| evals_in_steps := by | |
| change (MachineComposition.advance (step source))^[run.steps] (some start) = finish | |
| apply Option.map_injective (configurationEquiv e).injective | |
| change ((MachineComposition.advance (step source))^[run.steps] (some start)).map | |
| (configuration e) = finish.map (configuration e) | |
| rw [β iterate_transport] | |
| exact run.evals_in_steps | |
| steps_le_m := run.steps_le_m | |
| @[simp] theorem execution_reflect_steps (e : Ο β Ο) (source : Ξ β Stmt Ξ Ξ Ο) | |
| {start : Cfg Ξ Ξ Ο} {finish : Option (Cfg Ξ Ξ Ο)} {budget : Nat} | |
| (run : StateTransition.EvalsToInTime (step (program e source)) | |
| (configuration e start) (finish.map (configuration e)) budget) : | |
| (execution_reflect e source run).steps = run.steps := rfl | |
| end UniqueGamesTheorem.Foundations.Complexity.MachineStateEquiv | |
| end | |
| end OAI | |