roseking's picture
Add lean source and binary files (part 61)
ea76aac verified
Raw History Blame Contribute Delete
38.8 kB
import Mathlib
import OAI.Computability.MaxCut.Machines.Runtime
namespace OAI
namespace MaxCutGames.Foundations.Complexity.MachineCopy
open Turing
variable {K Λ σ β : Type} [DecidableEq K]
abbrev Alphabet (β : Type) (_ : K) := β
/-- Drain one tape into two reverse-order accumulators. -/
def forkLoop (source left right : K) (fallback : β) (loopLabel : Λ) (exit : Option Λ) :
TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β) :=
.pop source (fun state head => (state.1, head))
(.branch (fun state => state.2.isSome)
(.push left (fun state => state.2.getD fallback)
(.push right (fun state => state.2.getD fallback) (.goto fun _ => loopLabel)))
(Reduction.MachineTransfer.exitAt right exit))
def forkTapes (source left right : K) (base : K → List β)
(input leftOutput rightOutput : List β) : K → List β :=
Function.update (Function.update (Function.update base source input) left leftOutput)
right rightOutput
@[simp] theorem forkTapes_source (source left right : K)
(sourceLeft : source ≠ left) (sourceRight : source ≠ right)
(base : K → List β) (input leftOutput rightOutput : List β) :
forkTapes source left right base input leftOutput rightOutput source = input := by
simp [forkTapes, sourceLeft, sourceRight]
@[simp] theorem forkTapes_left (source left right : K) (leftRight : left ≠ right)
(base : K → List β) (input leftOutput rightOutput : List β) :
forkTapes source left right base input leftOutput rightOutput left = leftOutput := by
simp [forkTapes, leftRight]
@[simp] theorem forkTapes_right (source left right : K)
(base : K → List β) (input leftOutput rightOutput : List β) :
forkTapes source left right base input leftOutput rightOutput right = rightOutput := by
simp [forkTapes]
@[simp] theorem forkTapes_self (source left right : K) (base : K → List β) :
forkTapes source left right base (base source) (base left) (base right) = base := by
simp [forkTapes]
private theorem update_fork_source_inline_MachineCopy (source left right : K)
(sourceLeft : source ≠ left) (sourceRight : source ≠ right) (leftRight : left ≠ right)
(base : K → List β) (input leftOutput rightOutput replacement : List β) :
Function.update (forkTapes source left right base input leftOutput rightOutput)
source replacement = forkTapes source left right base replacement leftOutput rightOutput := by
funext k
by_cases hs : k = source
· subst k; simp [forkTapes, sourceLeft, sourceRight]
· by_cases hl : k = left
· subst k; simp [forkTapes, Ne.symm sourceLeft, leftRight]
· by_cases hr : k = right
· subst k; simp [forkTapes, Ne.symm sourceRight]
· simp [forkTapes, hs, hl, hr]
private theorem update_fork_left_inline_MachineCopy (source left right : K) (leftRight : left ≠ right)
(base : K → List β) (input leftOutput rightOutput replacement : List β) :
Function.update (forkTapes source left right base input leftOutput rightOutput)
left replacement = forkTapes source left right base input replacement rightOutput := by
funext k
by_cases hl : k = left
· subst k; simp [forkTapes, leftRight]
· by_cases hr : k = right
· subst k; simp [forkTapes, Ne.symm leftRight]
· simp [forkTapes, hl, hr]
private theorem update_fork_right_inline_MachineCopy (source left right : K)
(base : K → List β) (input leftOutput rightOutput replacement : List β) :
Function.update (forkTapes source left right base input leftOutput rightOutput)
right replacement = forkTapes source left right base input leftOutput replacement := by
simp [forkTapes]
theorem forkStep_empty (source left right : K)
(sourceLeft : source ≠ left) (sourceRight : source ≠ right) (leftRight : left ≠ right)
(fallback : β) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β))
(atLoop : program loopLabel = forkLoop source left right fallback loopLabel exit)
(base : K → List β) (leftOutput rightOutput : List β) (ambient : σ) (register : Option β) :
TM2.step program
⟨some loopLabel, (ambient, register), forkTapes source left right base [] leftOutput rightOutput⟩ =
some ⟨exit, (ambient, none), forkTapes source left right base [] leftOutput rightOutput⟩ := by
change some (TM2.stepAux (program loopLabel) (ambient, register)
(forkTapes source left right base [] leftOutput rightOutput)) = _
rw [atLoop]
cases exit <;>
simp [forkLoop, Reduction.MachineTransfer.exitAt, TM2.stepAux, sourceLeft, sourceRight,
leftRight, update_fork_source_inline_MachineCopy]
theorem forkStep_cons (source left right : K)
(sourceLeft : source ≠ left) (sourceRight : source ≠ right) (leftRight : left ≠ right)
(fallback : β) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β))
(atLoop : program loopLabel = forkLoop source left right fallback loopLabel exit)
(base : K → List β) (head : β) (input leftOutput rightOutput : List β)
(ambient : σ) (register : Option β) :
TM2.step program
⟨some loopLabel, (ambient, register),
forkTapes source left right base (head :: input) leftOutput rightOutput⟩ =
some ⟨some loopLabel, (ambient, some head),
forkTapes source left right base input (head :: leftOutput) (head :: rightOutput)⟩ := by
change some (TM2.stepAux (program loopLabel) (ambient, register)
(forkTapes source left right base (head :: input) leftOutput rightOutput)) = _
rw [atLoop]
simp [forkLoop, TM2.stepAux, sourceLeft, sourceRight, leftRight,
update_fork_source_inline_MachineCopy, update_fork_left_inline_MachineCopy, update_fork_right_inline_MachineCopy]
/-- Each symbol and the final empty test take one actual transition. -/
theorem forkTrace (source left right : K)
(sourceLeft : source ≠ left) (sourceRight : source ≠ right) (leftRight : left ≠ right)
(fallback : β) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β))
(atLoop : program loopLabel = forkLoop source left right fallback loopLabel exit)
(base : K → List β) (input leftOutput rightOutput : List β)
(ambient : σ) (register : Option β) :
(MachineComposition.advance (TM2.step program))^[input.length + 1]
(some ⟨some loopLabel, (ambient, register),
forkTapes source left right base input leftOutput rightOutput⟩) =
some ⟨exit, (ambient, none), forkTapes source left right base []
(input.reverse ++ leftOutput) (input.reverse ++ rightOutput)⟩ := by
induction input generalizing leftOutput rightOutput register with
| nil =>
simpa only [List.length_nil, Nat.zero_add, Function.iterate_one,
MachineComposition.advance_some, List.reverse_nil, List.nil_append] using
forkStep_empty source left right sourceLeft sourceRight leftRight fallback loopLabel exit
program atLoop base leftOutput rightOutput ambient register
| cons head input ih =>
rw [List.length_cons, Function.iterate_succ_apply]
change (MachineComposition.advance (TM2.step program))^[input.length + 1]
(TM2.step program ⟨some loopLabel, (ambient, register),
forkTapes source left right base (head :: input) leftOutput rightOutput⟩) = _
rw [forkStep_cons source left right sourceLeft sourceRight leftRight fallback loopLabel exit
program atLoop base head input leftOutput rightOutput ambient register, ih]
simp only [List.reverse_cons, List.append_assoc, List.singleton_append]
def forkInTime (source left right : K)
(sourceLeft : source ≠ left) (sourceRight : source ≠ right) (leftRight : left ≠ right)
(fallback : β) (loopLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β))
(atLoop : program loopLabel = forkLoop source left right fallback loopLabel exit)
(base : K → List β) (ambient : σ) (register : Option β) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some loopLabel, (ambient, register), base⟩
(some ⟨exit, (ambient, none), forkTapes source left right base []
((base source).reverse ++ base left) ((base source).reverse ++ base right)⟩)
((base source).length + 1) where
steps := (base source).length + 1
evals_in_steps := by
change (MachineComposition.advance (TM2.step program))^[(base source).length + 1]
(some ⟨some loopLabel, (ambient, register), base⟩) = _
simpa only [forkTapes_self] using
forkTrace source left right sourceLeft sourceRight leftRight fallback loopLabel exit
program atLoop base (base source) (base left) (base right) ambient register
steps_le_m := Nat.le_refl _
def drainedTapes (source scratch : K) (base : K → List β) : K → List β :=
Reduction.MachineTransfer.tapesAt source scratch base [] (base source).reverse
theorem restoredTapes (source destination scratch : K)
(sourceDestination : source ≠ destination) (sourceScratch : source ≠ scratch)
(destinationScratch : destination ≠ scratch) (base : K → List β)
(scratchEmpty : base scratch = []) :
forkTapes scratch source destination (drainedTapes source scratch base) []
(base source) (base source ++ base destination) =
Function.update base destination (base source ++ base destination) := by
funext k
by_cases hs : k = source
· subst k
simp [forkTapes, sourceDestination]
· by_cases hd : k = destination
· subst k; simp [forkTapes]
· by_cases ht : k = scratch
· subst k
simp [forkTapes, Ne.symm sourceScratch, Ne.symm destinationScratch, scratchEmpty]
· simp [forkTapes, drainedTapes, Reduction.MachineTransfer.tapesAt, hs, hd, ht]
theorem copyTrace (source destination scratch : K)
(sourceDestination : source ≠ destination) (sourceScratch : source ≠ scratch)
(destinationScratch : destination ≠ scratch)
(fallback : β) (firstLabel secondLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β))
(atFirst : program firstLabel =
Reduction.MachineTransfer.loopAt source scratch id fallback firstLabel (some secondLabel))
(atSecond : program secondLabel = forkLoop scratch source destination fallback secondLabel exit)
(base : K → List β) (scratchEmpty : base scratch = [])
(ambient : σ) (register : Option β) :
(MachineComposition.advance (TM2.step program))^[2 * ((base source).length + 1)]
(some ⟨some firstLabel, (ambient, register), base⟩) =
some ⟨exit, (ambient, none),
Function.update base destination (base source ++ base destination)⟩ := by
have first := Reduction.MachineTransfer.transferAt_fromTapes source scratch sourceScratch
id fallback firstLabel (some secondLabel) program atFirst base ambient register
change (MachineComposition.advance (TM2.step program))^[(base source).length + 1]
(some ⟨some firstLabel, (ambient, register), base⟩) = _ at first
simp only [List.map_id, scratchEmpty, List.append_nil] at first
have second := forkTrace scratch source destination (Ne.symm sourceScratch)
(Ne.symm destinationScratch) sourceDestination fallback secondLabel exit program atSecond
(drainedTapes source scratch base)
(drainedTapes source scratch base scratch) (drainedTapes source scratch base source)
(drainedTapes source scratch base destination) ambient none
simp only [forkTapes_self] at second
have scratchWord : drainedTapes source scratch base scratch = (base source).reverse := by
simp [drainedTapes]
have sourceWord : drainedTapes source scratch base source = [] := by
simp [drainedTapes, sourceScratch]
have destinationWord : drainedTapes source scratch base destination = base destination := by
simp [drainedTapes, Reduction.MachineTransfer.tapesAt, Ne.symm sourceDestination,
destinationScratch]
rw [scratchWord, sourceWord, destinationWord] at second
simp only [List.reverse_reverse, List.append_nil, List.length_reverse] at second
rw [restoredTapes source destination scratch sourceDestination sourceScratch destinationScratch
base scratchEmpty] at second
rw [show 2 * ((base source).length + 1) =
((base source).length + 1) + ((base source).length + 1) by omega,
Function.iterate_add_apply, first]
exact second
/-- A timed witness whose stored step count is the exact two-loop trace length. -/
def copyInTime (source destination scratch : K)
(sourceDestination : source ≠ destination) (sourceScratch : source ≠ scratch)
(destinationScratch : destination ≠ scratch)
(fallback : β) (firstLabel secondLabel : Λ) (exit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet (K := K) β) Λ (σ × Option β))
(atFirst : program firstLabel =
Reduction.MachineTransfer.loopAt source scratch id fallback firstLabel (some secondLabel))
(atSecond : program secondLabel = forkLoop scratch source destination fallback secondLabel exit)
(base : K → List β) (scratchEmpty : base scratch = [])
(ambient : σ) (register : Option β) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some firstLabel, (ambient, register), base⟩
(some ⟨exit, (ambient, none),
Function.update base destination (base source ++ base destination)⟩)
(2 * ((base source).length + 1)) where
steps := 2 * ((base source).length + 1)
evals_in_steps := copyTrace source destination scratch sourceDestination sourceScratch
destinationScratch fallback firstLabel secondLabel exit program atFirst atSecond base
scratchEmpty ambient register
steps_le_m := Nat.le_refl _
end MaxCutGames.Foundations.Complexity.MachineCopy
/-!
# A certified machine for fixed symbol substitution
For a fixed table `emit : Bool → List Bool`, this actual finite TM2 machine
produces `(input.flatMap emit).reverse` in exactly `2 * input.length + 1`
transitions. It has one input-pop transition and one literal-emission transition
per input symbol, followed by the final empty pop. Literal emission is a fixed
finite statement; its maximum number of pushes depends only on `emit`.
This is a machine primitive, not a runtime theorem for the Unique Games reduction.
-/
namespace MaxCutGames.Reduction.MachineSubstitution
open Turing
open MaxCutGames.Foundations.Complexity
section PushWord
variable {K Λ σ : Type} {Γ : K → Type} [DecidableEq K]
/-- Push a fixed word in its listed order, then execute the continuation. -/
def pushWord (dst : K) : List (Γ dst) → TM2.Stmt Γ Λ σ → TM2.Stmt Γ Λ σ
| [], continuation => continuation
| symbol :: word, continuation =>
.push dst (fun _ => symbol) (pushWord dst word continuation)
/-- Literal pushes preserve the state and every other tape. -/
theorem stepAux_pushWord (dst : K) (word : List (Γ dst))
(continuation : TM2.Stmt Γ Λ σ) (state : σ) (tapes : ∀ k, List (Γ k)) :
TM2.stepAux (pushWord dst word continuation) state tapes =
TM2.stepAux continuation state
(Function.update tapes dst (word.reverse ++ tapes dst)) := by
induction word generalizing tapes with
| nil => simp [pushWord]
| cons symbol word ih =>
simp only [pushWord, TM2.stepAux]
rw [ih]
simp only [Function.update_self, Function.update_idem, List.reverse_cons,
List.append_assoc, List.singleton_append]
omit [DecidableEq K] in
theorem statementPushBound_pushWord (dst : K) (word : List (Γ dst))
(continuation : TM2.Stmt Γ Λ σ) :
Runtime.statementPushBound (pushWord dst word continuation) =
word.length + Runtime.statementPushBound continuation := by
induction word with
| nil => simp [pushWord]
| cons symbol word ih =>
simp only [pushWord, Runtime.statementPushBound, ih, List.length_cons]
omega
end PushWord
def loop : TM2.Stmt (fun _ : Bool => Bool) (Option Bool) (Option Bool) :=
.pop false (fun _ head => head)
(.branch Option.isSome
(.goto fun state => some (state.getD false))
.halt)
def program (emit : Bool → List Bool) :
Option Bool → TM2.Stmt (fun _ : Bool => Bool) (Option Bool) (Option Bool)
| none => loop
| some symbol => pushWord true (emit symbol) (.goto fun _ => none)
def machine (emit : Bool → List Bool) : FinTM2 where
K := Bool
k₀ := false
k₁ := true
Γ _ := Bool
Λ := Option Bool
main := none
σ := Option Bool
initialState := none
m := program emit
/-- The fixed maximum word length bounds pushes in each emitted statement. -/
def maxEmission (emit : Bool → List Bool) : Nat :=
max (emit false).length (emit true).length
theorem statementPushBound_le (emit : Bool → List Bool) (label : Option Bool) :
Runtime.statementPushBound (program emit label) ≤ maxEmission emit := by
cases label with
| none => simp [program, loop, Runtime.statementPushBound]
| some symbol =>
simp only [program, statementPushBound_pushWord, Runtime.statementPushBound, Nat.add_zero]
cases symbol
· exact Nat.le_max_left _ _
· exact Nat.le_max_right _ _
theorem programPushBound_le (emit : Bool → List Bool) :
Runtime.programPushBound (machine emit) ≤ maxEmission emit := by
have allLabels (labels : List (Option Bool)) :
Runtime.maxLabelPushes (program emit) labels ≤ maxEmission emit := by
induction labels with
| nil => exact Nat.zero_le _
| cons label rest ih =>
exact max_le (statementPushBound_le emit label) ih
exact allLabels (machine emit).ΛFin.elems.toList
def tapeStacks (input output : List Bool) : Bool → List Bool :=
fun side => if side then output else input
def running (emit : Bool → List Bool) (input output : List Bool) (state : Option Bool) :
(machine emit).Cfg :=
⟨some none, state, tapeStacks input output⟩
def emitting (emit : Bool → List Bool) (symbol : Bool)
(input output : List Bool) (state : Option Bool) : (machine emit).Cfg :=
⟨some (some symbol), state, tapeStacks input output⟩
def halted (emit : Bool → List Bool) (output : List Bool) : (machine emit).Cfg :=
⟨none, none, tapeStacks [] output⟩
private theorem update_input_inline_MachineSubstitution (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_MachineSubstitution (input output replacement : List Bool) :
Function.update (tapeStacks input output) true replacement = tapeStacks input replacement := by
funext side
cases side <;> simp [tapeStacks]
theorem step_empty (emit : Bool → List Bool) (output : List Bool) (state : Option Bool) :
(machine emit).step (running emit [] output state) = some (halted emit output) := by
change some (TM2.stepAux loop state (tapeStacks [] output)) = some (halted emit output)
simp [loop, TM2.stepAux, tapeStacks, halted]
rw [update_input_inline_MachineSubstitution]
rfl
theorem step_cons (emit : Bool → List Bool) (symbol : Bool)
(input output : List Bool) (state : Option Bool) :
(machine emit).step (running emit (symbol :: input) output state) =
some (emitting emit symbol input output (some symbol)) := by
change some (TM2.stepAux loop state (tapeStacks (symbol :: input) output)) = _
simp [loop, TM2.stepAux, tapeStacks, emitting]
rw [update_input_inline_MachineSubstitution]
rfl
/-- Emission does not depend on the register; the symbol is held in the control label. -/
theorem step_emit (emit : Bool → List Bool) (symbol : Bool)
(input output : List Bool) (state : Option Bool) :
(machine emit).step (emitting emit symbol input output state) =
some (running emit input ((emit symbol).reverse ++ output) state) := by
change some (TM2.stepAux (pushWord true (emit symbol) (.goto fun _ => none))
state (tapeStacks input output)) = _
rw [stepAux_pushWord]
simp only [TM2.stepAux]
change some (⟨some none, state,
Function.update (tapeStacks input output) true ((emit symbol).reverse ++ output)⟩ :
(machine emit).Cfg) = _
erw [update_output_inline_MachineSubstitution]
rfl
def next (emit : Bool → List Bool) (configuration : Option (machine emit).Cfg) :
Option (machine emit).Cfg :=
configuration.bind (machine emit).step
theorem two_steps_cons (emit : Bool → List Bool) (symbol : Bool)
(input output : List Bool) (state : Option Bool) :
(next emit)^[2] (some (running emit (symbol :: input) output state)) =
some (running emit input ((emit symbol).reverse ++ output) (some symbol)) := by
change (machine emit).step (running emit (symbol :: input) output state) >>=
(machine emit).step = _
rw [step_cons]
exact step_emit emit symbol input output (some symbol)
/-- Exact execution, including an arbitrary initial accumulator and register. -/
theorem substitution_steps (emit : Bool → List Bool)
(input output : List Bool) (state : Option Bool) :
(next emit)^[2 * input.length + 1] (some (running emit input output state)) =
some (halted emit ((input.flatMap emit).reverse ++ output)) := by
induction input generalizing output state with
| nil =>
simpa only [List.length_nil, Nat.mul_zero, Nat.zero_add, Function.iterate_one, next,
Option.bind_some, List.flatMap_nil, List.reverse_nil, List.nil_append]
using step_empty emit output state
| 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 [List.flatMap_cons, List.reverse_append, List.append_assoc]
theorem initList_eq (emit : Bool → List Bool) (input : List Bool) :
initList (machine emit) input = running emit input [] none := by
unfold initList running
congr 1
funext side
cases side <;> rfl
theorem haltList_eq (emit : Bool → List Bool) (output : List Bool) :
haltList (machine emit) output = halted emit output := by
unfold haltList halted
congr 1
theorem substitution_init_steps (emit : Bool → List Bool) (input : List Bool) :
(next emit)^[2 * input.length + 1] (some (initList (machine emit) input)) =
some (haltList (machine emit) (input.flatMap emit).reverse) := by
rw [initList_eq, haltList_eq]
simpa only [List.append_nil] using substitution_steps emit input [] none
def outputsInTime (emit : Bool → List Bool) (input : List Bool) :
TM2OutputsInTime (machine emit) input (some (input.flatMap emit).reverse)
(2 * input.length + 1) where
steps := 2 * input.length + 1
evals_in_steps := substitution_init_steps emit input
steps_le_m := Nat.le_refl _
@[simp] theorem outputsInTime_steps (emit : Bool → List Bool) (input : List Bool) :
(outputsInTime emit input).steps = 2 * input.length + 1 := rfl
/-- A finite-machine polynomial-time certificate for reversed fixed substitution. -/
noncomputable def computableInPolyTime (emit : Bool → List Bool) :
TM2ComputableInPolyTime (id : List Bool → List Bool) id
(fun input => (input.flatMap emit).reverse) where
tm := machine emit
inputAlphabet := Equiv.refl Bool
outputAlphabet := Equiv.refl Bool
time := 2 * Polynomial.X + 1
outputsFun input := by
change TM2OutputsInTime (machine emit) (input.map id)
(some ((input.flatMap emit).reverse.map id))
((2 * Polynomial.X + 1 : Polynomial Nat).eval input.length)
have hi := @List.map_id ((machine emit).Γ (machine emit).k₀) input
have ho := @List.map_id ((machine emit).Γ (machine emit).k₁) (input.flatMap emit).reverse
rw [hi, ho]
simpa only [Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_X,
Polynomial.eval_ofNat, Polynomial.eval_one] using outputsInTime emit input
end MaxCutGames.Reduction.MachineSubstitution
namespace MaxCutGames.Reduction.MachineReverse
open Turing
def loop : TM2.Stmt (fun _ : Bool => Bool) Unit (Option Bool) :=
.pop false (fun _ head => head)
(.branch Option.isSome
(.push true (fun state => state.getD false) (.goto fun _ => ()))
.halt)
def machine : FinTM2 where
K := Bool
k₀ := false
k₁ := true
Γ _ := Bool
Λ := Unit
main := ()
σ := Option Bool
initialState := none
m _ := loop
def tapeStacks (input output : List Bool) : Bool → List Bool :=
fun side => if side then output else input
def running (input output : List Bool) (state : Option Bool) : machine.Cfg :=
⟨some (), state, tapeStacks input output⟩
def halted (output : List Bool) : machine.Cfg :=
⟨none, none, tapeStacks [] output⟩
private theorem update_input_inline_MachineReverse (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_MachineReverse (input output replacement : List Bool) :
Function.update (tapeStacks input output) true replacement = tapeStacks input replacement := by
funext side
cases side <;> simp [tapeStacks]
/-- Popping an empty input resets the internal state to `none` before halting. -/
theorem step_empty (output : List Bool) (state : Option Bool) :
machine.step (running [] output state) = some (halted output) := by
change some (TM2.stepAux loop state (tapeStacks [] output)) = some (halted output)
simp [loop, TM2.stepAux, tapeStacks, halted, Function.update]
rw [update_input_inline_MachineReverse]
rfl
/-- One transition moves the head symbol from the input onto the output. -/
theorem step_cons (head : Bool) (input output : List Bool) (state : Option Bool) :
machine.step (running (head :: input) output state) =
some (running input (head :: output) (some head)) := by
change some (TM2.stepAux loop state (tapeStacks (head :: input) output)) = _
simp [loop, TM2.stepAux, tapeStacks, running, Function.update]
rw [update_input_inline_MachineReverse, update_output_inline_MachineReverse]
rfl
def next (configuration : Option machine.Cfg) : Option machine.Cfg :=
configuration.bind machine.step
/-- An exact transition trace, with arbitrary initial accumulator and state. -/
theorem reverse_steps (input output : List Bool) (state : Option Bool) :
next^[input.length + 1] (some (running input output state)) =
some (halted (input.reverse ++ 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.nil_append] using step_empty output state
| cons head input ih =>
rw [List.length_cons, Function.iterate_succ_apply]
change next^[input.length + 1]
(machine.step (running (head :: input) output state)) = _
rw [step_cons, ih]
simp only [List.reverse_cons, List.append_assoc, List.singleton_append]
theorem initList_eq (input : List Bool) :
initList machine input = running input [] none := by
unfold initList running
congr 1
funext side
cases side <;> rfl
theorem haltList_eq (output : List Bool) : haltList machine output = halted output := by
unfold haltList halted
congr 1
/-- The exact trace starts and ends at the standard encoded TM2 configurations. -/
theorem reverse_init_steps (input : List Bool) :
next^[input.length + 1] (some (initList machine input)) =
some (haltList machine input.reverse) := by
rw [initList_eq, haltList_eq]
simpa only [List.append_nil] using reverse_steps input [] none
def outputsInTime (input : List Bool) :
TM2OutputsInTime machine input (some input.reverse) (input.length + 1) where
steps := input.length + 1
evals_in_steps := reverse_init_steps input
steps_le_m := Nat.le_refl _
@[simp] theorem outputsInTime_steps (input : List Bool) :
(outputsInTime input).steps = input.length + 1 := rfl
/-- Actual finite-machine polynomial-time computability, with identity encodings. -/
noncomputable def computableInPolyTime :
TM2ComputableInPolyTime (id : List Bool → List Bool) id List.reverse where
tm := machine
inputAlphabet := Equiv.refl Bool
outputAlphabet := Equiv.refl Bool
time := Polynomial.X + 1
outputsFun input := by
change TM2OutputsInTime machine (input.map id) (some (input.reverse.map id))
((Polynomial.X + 1 : Polynomial Nat).eval input.length)
have hi := @List.map_id (machine.Γ machine.k₀) input
have ho := @List.map_id (machine.Γ machine.k₁) input.reverse
rw [hi, ho]
simpa only [Polynomial.eval_add, Polynomial.eval_X, Polynomial.eval_one]
using outputsInTime input
end MaxCutGames.Reduction.MachineReverse
/-! Static renaming of labels and finite internal states. This does not execute
a data-copying phase and preserves every actual transition exactly. -/
namespace MaxCutGames.Foundations.Complexity.MachineControl
open Turing.TM2
variable {K Λ Λ' σ σ' : Type} {Γ : K → Type}
def configuration (labels : Λ → Λ') (states : σ ≃ σ') (c : Cfg Γ Λ σ) :
Cfg Γ Λ' σ' where
l := c.l.map labels
var := states c.var
stk := c.stk
def statement (labels : Λ → Λ') (states : σ ≃ σ') : Stmt Γ Λ σ → Stmt Γ Λ' σ'
| .push k f next => .push k (fun st => f (states.symm st)) (statement labels states next)
| .peek k f next => .peek k (fun st v => states (f (states.symm st) v))
(statement labels states next)
| .pop k f next => .pop k (fun st v => states (f (states.symm st) v))
(statement labels states next)
| .load f next => .load (fun st => states (f (states.symm st)))
(statement labels states next)
| .branch f yes no => .branch (fun st => f (states.symm st))
(statement labels states yes) (statement labels states no)
| .goto f => .goto (fun st => labels (f (states.symm st)))
| .halt => .halt
variable [DecidableEq K]
theorem stepAux_simulation (labels : Λ → Λ') (states : σ ≃ σ')
(q : Stmt Γ Λ σ) (state : σ) (tapes : ∀ k, List (Γ k)) :
stepAux (statement labels states q) (states state) tapes =
configuration labels states (stepAux q state tapes) := by
induction q generalizing state tapes with
| push k f next ih =>
simpa only [statement, stepAux, Equiv.symm_apply_apply] using
ih state (Function.update tapes k (f state :: tapes k))
| peek k f next ih =>
simpa only [statement, stepAux, Equiv.symm_apply_apply] using
ih (f state (tapes k).head?) tapes
| pop k f next ih =>
simpa only [statement, stepAux, Equiv.symm_apply_apply] using
ih (f state (tapes k).head?) (Function.update tapes k (tapes k).tail)
| load f next ih =>
simpa only [statement, stepAux, Equiv.symm_apply_apply] using ih (f state) tapes
| branch f yes no ihYes ihNo =>
cases h : f state with
| false =>
simpa only [statement, stepAux, Equiv.symm_apply_apply, h, Bool.cond_false] using
ihNo state tapes
| true =>
simpa only [statement, stepAux, Equiv.symm_apply_apply, h, Bool.cond_true] using
ihYes state tapes
| goto f => simp [statement, stepAux, configuration]
| halt => rfl
def program (labels : Λ ≃ Λ') (states : σ ≃ σ') (source : Λ → Stmt Γ Λ σ) :
Λ' → Stmt Γ Λ' σ' := fun l => statement labels states (source (labels.symm l))
/-- The renamed program is conjugate to the original transition function,
including the halted configuration. -/
theorem step_simulation (labels : Λ ≃ Λ') (states : σ ≃ σ')
(source : Λ → Stmt Γ Λ σ) (c : Cfg Γ Λ σ) :
step (program labels states source) (configuration labels states c) =
(step source c).map (configuration labels states) := by
cases c with
| mk l state tapes =>
cases l with
| none => rfl
| some l =>
change some (stepAux (statement labels states (source (labels.symm (labels l))))
(states state) tapes) = _
rw [Equiv.symm_apply_apply, stepAux_simulation]
rfl
end MaxCutGames.Foundations.Complexity.MachineControl
/-! Static exchange of the first two groups of stacks. Symbol types and stack
contents are retained exactly; no machine transition is spent on this layout
change. This aligns the two component machines in sequential composition. -/
namespace MaxCutGames.Foundations.Complexity.MachineStackSwap
open Turing.TM2
variable {A B C Λ σ : Type}
{ΓA : A → Type} {ΓB : B → Type} {ΓC : C → Type}
abbrev Alphabet (ΓA : A → Type) (ΓB : B → Type) (ΓC : C → Type) :=
MachineEmbedding.Alphabet ΓA (MachineEmbedding.Alphabet ΓB ΓC)
def tapes (source : ∀ k, List (Alphabet ΓA ΓB ΓC k)) :
∀ j, List (Alphabet ΓB ΓA ΓC j)
| .inl b => source (.inr (.inl b))
| .inr (.inl a) => source (.inl a)
| .inr (.inr c) => source (.inr (.inr c))
@[simp] theorem tapes_first (source : ∀ k, List (Alphabet ΓA ΓB ΓC k)) (a : A) :
tapes source (.inr (.inl a)) = source (.inl a) := rfl
@[simp] theorem tapes_second (source : ∀ k, List (Alphabet ΓA ΓB ΓC k)) (b : B) :
tapes source (.inl b) = source (.inr (.inl b)) := rfl
@[simp] theorem tapes_third (source : ∀ k, List (Alphabet ΓA ΓB ΓC k)) (c : C) :
tapes source (.inr (.inr c)) = source (.inr (.inr c)) := rfl
theorem tapes_involutive (source : ∀ k, List (Alphabet ΓA ΓB ΓC k)) :
tapes (tapes source) = source := by
funext k
rcases k with a | b | c <;> rfl
def configuration (c : Cfg (Alphabet ΓA ΓB ΓC) Λ σ) :
Cfg (Alphabet ΓB ΓA ΓC) Λ σ where
l := c.l
var := c.var
stk := tapes c.stk
def statement : Stmt (Alphabet ΓA ΓB ΓC) Λ σ → Stmt (Alphabet ΓB ΓA ΓC) Λ σ
| .push (.inl a) f next => .push (.inr (.inl a)) f (statement next)
| .push (.inr (.inl b)) f next => .push (.inl b) f (statement next)
| .push (.inr (.inr c)) f next => .push (.inr (.inr c)) f (statement next)
| .peek (.inl a) f next => .peek (.inr (.inl a)) f (statement next)
| .peek (.inr (.inl b)) f next => .peek (.inl b) f (statement next)
| .peek (.inr (.inr c)) f next => .peek (.inr (.inr c)) f (statement next)
| .pop (.inl a) f next => .pop (.inr (.inl a)) f (statement next)
| .pop (.inr (.inl b)) f next => .pop (.inl b) f (statement next)
| .pop (.inr (.inr c)) f next => .pop (.inr (.inr c)) f (statement next)
| .load f next => .load f (statement next)
| .branch f yes no => .branch f (statement yes) (statement no)
| .goto f => .goto f
| .halt => .halt
variable [DecidableEq A] [DecidableEq B] [DecidableEq C]
theorem tapes_update_first (source : ∀ k, List (Alphabet ΓA ΓB ΓC k))
(a : A) (value : List (ΓA a)) :
tapes (Function.update source (.inl a) value) =
Function.update (tapes source) (.inr (.inl a)) value := by
funext j
rcases j with b | a' | c
· simp [Function.update]
· by_cases h : a' = a
· subst a'; simp [Function.update]
· simp [Function.update, h]
· simp [Function.update]
theorem tapes_update_second (source : ∀ k, List (Alphabet ΓA ΓB ΓC k))
(b : B) (value : List (ΓB b)) :
tapes (Function.update source (.inr (.inl b)) value) =
Function.update (tapes source) (.inl b) value := by
funext j
rcases j with b' | a | c
· by_cases h : b' = b
· subst b'; simp [Function.update]
· simp [Function.update, h]
· simp [Function.update]
· simp [Function.update]
theorem tapes_update_third (source : ∀ k, List (Alphabet ΓA ΓB ΓC k))
(c : C) (value : List (ΓC c)) :
tapes (Function.update source (.inr (.inr c)) value) =
Function.update (tapes source) (.inr (.inr c)) value := by
funext j
rcases j with b | a | c'
· simp [Function.update]
· simp [Function.update]
· by_cases h : c' = c
· subst c'; simp [Function.update]
· simp [Function.update, h]
theorem stepAux_simulation (q : Stmt (Alphabet ΓA ΓB ΓC) Λ σ)
(state : σ) (source : ∀ k, List (Alphabet ΓA ΓB ΓC k)) :
stepAux (statement q) state (tapes source) =
configuration (stepAux q state source) := by
induction q generalizing state source with
| push k f next ih =>
rcases k with a | b | c
· simp only [statement, stepAux, tapes_first]
rw [← tapes_update_first]
exact ih state (Function.update source (.inl a) (f state :: source (.inl a)))
· simp only [statement, stepAux, tapes_second]
rw [← tapes_update_second]
exact ih state (Function.update source (.inr (.inl b))
(f state :: source (.inr (.inl b))))
· simp only [statement, stepAux, tapes_third]
rw [← tapes_update_third]
exact ih state (Function.update source (.inr (.inr c))
(f state :: source (.inr (.inr c))))
| peek k f next ih =>
rcases k with a | b | c
· simpa only [statement, stepAux, tapes_first] using
ih (f state (source (.inl a)).head?) source
· simpa only [statement, stepAux, tapes_second] using
ih (f state (source (.inr (.inl b))).head?) source
· simpa only [statement, stepAux, tapes_third] using
ih (f state (source (.inr (.inr c))).head?) source
| pop k f next ih =>
rcases k with a | b | c
· simp only [statement, stepAux, tapes_first]
rw [← tapes_update_first]
exact ih (f state (source (.inl a)).head?)
(Function.update source (.inl a) (source (.inl a)).tail)
· simp only [statement, stepAux, tapes_second]
rw [← tapes_update_second]
exact ih (f state (source (.inr (.inl b))).head?)
(Function.update source (.inr (.inl b)) (source (.inr (.inl b))).tail)
· simp only [statement, stepAux, tapes_third]
rw [← tapes_update_third]
exact ih (f state (source (.inr (.inr c))).head?)
(Function.update source (.inr (.inr c)) (source (.inr (.inr c))).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 => rfl
def program (source : Λ → Stmt (Alphabet ΓA ΓB ΓC) Λ σ) :
Λ → Stmt (Alphabet ΓB ΓA ΓC) Λ σ := fun l => statement (source l)
theorem step_simulation (source : Λ → Stmt (Alphabet ΓA ΓB ΓC) Λ σ)
(c : Cfg (Alphabet ΓA ΓB ΓC) Λ σ) :
step (program source) (configuration c) = (step source c).map configuration := by
cases c with
| mk l state sourceTapes =>
cases l with
| none => rfl
| some l =>
change some (stepAux (statement (source l)) state (tapes sourceTapes)) = _
rw [stepAux_simulation]
rfl
end MaxCutGames.Foundations.Complexity.MachineStackSwap
end OAI