openAI-math-dataset / lean /OAI /Computability /BinPacking /Computation /MachineFiniteAlphabet.lean
roseking's picture
Add lean source and binary files (part 59)
8df23f4 verified
Raw History Blame Contribute Delete
57.9 kB
import Mathlib.Computability.TuringMachine.StackTuringMachine
import Mathlib.Basic.Finite.Sigma
import Mathlib.Logic.Equiv.Basic
import Mathlib.Tactic.FinCases
import OAI.Computability.BinPacking.Computation.MachineUnaryLessAt
namespace OAI
namespace BinPackingGames.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))
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 BinPackingGames.Foundations.Complexity.MachineControl
namespace BinPackingGames.Foundations.Complexity.MachineStateEquiv
open Turing.TM2
variable {K Λ σ τ : Type} {Γ : K → Type}
abbrev statement (e : σ ≃ τ) : Stmt Γ Λ σ → Stmt Γ Λ τ :=
MachineControl.statement (id : Λ → Λ) e
@[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]
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)
theorem statementPushBound (e : σ ≃ τ) (q : Stmt Γ Λ σ) :
Runtime.statementPushBound (statement e q) = Runtime.statementPushBound q := by
induction q <;>
simp_all only [statement, MachineControl.statement, Runtime.statementPushBound]
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]
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
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
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']
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
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
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 BinPackingGames.Foundations.Complexity.MachineStateEquiv
namespace BinPackingGames.Foundations.Complexity.MachineSequential
open Turing
abbrev Tape (first second : FinTM2) := first.K ⊕ (second.K ⊕ Unit)
abbrev Symbols (first second : FinTM2) : Tape first second → Type :=
MachineEmbedding.Alphabet first.Γ
(MachineEmbedding.Alphabet second.Γ (fun _ : Unit => second.Γ second.k₀))
abbrev Label (first second : FinTM2) := first.Λ ⊕ (Bool ⊕ second.Λ)
abbrev Register (second : FinTM2) := Option (second.Γ second.k₀)
abbrev State (first second : FinTM2) := (first.σ × second.σ) × Register second
def firstStates (first second : FinTM2) :
first.σ × (second.σ × Register second) ≃ State first second where
toFun st := ((st.1, st.2.1), st.2.2)
invFun st := (st.1.1, (st.1.2, st.2))
left_inv _ := rfl
right_inv _ := rfl
def secondStates (first second : FinTM2) :
second.σ × (first.σ × Register second) ≃ State first second where
toFun st := ((st.2.1, st.1), st.2.2)
invFun st := (st.1.2, (st.1.1, st.2))
left_inv _ := rfl
right_inv _ := rfl
def secondLabels (first second : FinTM2) :
second.Λ ⊕ (Bool ⊕ first.Λ) → Label first second
| .inl label => .inr (.inr label)
| .inr (.inl bit) => .inr (.inl bit)
| .inr (.inr label) => .inl label
def bridgeLabel (first second : FinTM2) (bit : Bool) : Label first second :=
.inr (.inl bit)
def firstStatement (first second : FinTM2) (q : first.Stmt) :
TM2.Stmt (Symbols first second) (Label first second) (State first second) :=
MachineControl.statement id (firstStates first second)
(MachineEmbedding.statement (some (bridgeLabel first second false)) q)
def secondStatement (first second : FinTM2) (q : second.Stmt) :
TM2.Stmt (Symbols first second) (Label first second) (State first second) :=
MachineControl.statement (secondLabels first second) (secondStates first second)
(MachineStackSwap.statement
(MachineEmbedding.statement
(Δ := MachineEmbedding.Alphabet first.Γ (fun _ : Unit => second.Γ second.k₀))
(Λextra := Bool ⊕ first.Λ) none q))
def program (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀) :
Label first second → TM2.Stmt (Symbols first second) (Label first second)
(State first second)
| .inl label => firstStatement first second (first.m label)
| .inr (.inr label) => secondStatement first second (second.m label)
| .inr (.inl false) =>
Reduction.MachineTransfer.loopAt (Γ := Symbols first second)
(.inl first.k₁) (.inr (.inr ())) relabel fallback
(bridgeLabel first second false) (some (bridgeLabel first second true))
| .inr (.inl true) =>
Reduction.MachineTransfer.loopAt (Γ := Symbols first second)
(.inr (.inr ())) (.inr (.inl second.k₀)) id fallback
(bridgeLabel first second true) (some (.inr (.inr second.main)))
def machine (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀) : FinTM2 where
K := Tape first second
kFin := by
letI := first.kFin
letI := second.kFin
exact inferInstanceAs (Fintype (first.K ⊕ (second.K ⊕ Unit)))
k₀ := .inl first.k₀
k₁ := .inr (.inl second.k₁)
Γ := Symbols first second
Λ := Label first second
ΛFin := by
letI := first.ΛFin
letI := second.ΛFin
exact inferInstanceAs (Fintype (first.Λ ⊕ (Bool ⊕ second.Λ)))
main := .inl first.main
σ := State first second
σFin := by
letI := first.σFin
letI := second.σFin
letI := second.Γk₀Fin
exact inferInstanceAs (Fintype ((first.σ × second.σ) × Option (second.Γ second.k₀)))
initialState := ((first.initialState, second.initialState), none)
Γk₀Fin := first.Γk₀Fin
m := program first second relabel fallback
def firstConfiguration (first second : FinTM2) (c : first.Cfg) :
TM2.Cfg (Symbols first second) (Label first second) (State first second) :=
MachineControl.configuration id (firstStates first second)
(MachineEmbedding.configuration (some (bridgeLabel first second false))
(second.initialState, (none : Register second)) (fun _ => []) c)
def secondConfiguration (first second : FinTM2) (c : second.Cfg) :
TM2.Cfg (Symbols first second) (Label first second) (State first second) :=
MachineControl.configuration (secondLabels first second) (secondStates first second)
(MachineStackSwap.configuration
(MachineEmbedding.configuration
(Δ := MachineEmbedding.Alphabet first.Γ (fun _ : Unit => second.Γ second.k₀))
(Λextra := Bool ⊕ first.Λ) none
(first.initialState, (none : Register second)) (fun _ => []) c))
theorem firstStep (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀) (a b : first.Cfg)
(transition : first.step a = some b) :
(machine first second relabel fallback).step (firstConfiguration first second a) =
some (firstConfiguration first second b) := by
cases a with
| mk label state tapes =>
cases label with
| none => cases transition
| some label =>
have hb := Option.some.inj transition
subst b
change some (TM2.stepAux
(MachineControl.statement id (firstStates first second)
(MachineEmbedding.statement (some (bridgeLabel first second false)) (first.m label)))
((firstStates first second) (state, (second.initialState, none)))
(MachineEmbedding.tapes tapes (fun _ => []))) = _
have controlled := MachineControl.stepAux_simulation id (firstStates first second)
(MachineEmbedding.statement
(Δ := MachineEmbedding.Alphabet second.Γ (fun _ : Unit => second.Γ second.k₀))
(some (bridgeLabel first second false)) (first.m label))
(state, (second.initialState, none)) (MachineEmbedding.tapes tapes (fun _ => []))
have embedded := MachineEmbedding.stepAux_simulation
(Δ := MachineEmbedding.Alphabet second.Γ (fun _ : Unit => second.Γ second.k₀))
(some (bridgeLabel first second false)) (second.initialState, (none : Register second))
(fun _ => []) (first.m label) state tapes
exact congrArg some (controlled.trans
(congrArg (MachineControl.configuration id (firstStates first second)) embedded))
theorem secondStep (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀) (a b : second.Cfg)
(transition : second.step a = some b) :
(machine first second relabel fallback).step (secondConfiguration first second a) =
some (secondConfiguration first second b) := by
cases a with
| mk label state tapes =>
cases label with
| none => cases transition
| some label =>
have hb := Option.some.inj transition
subst b
change some (TM2.stepAux
(MachineControl.statement (secondLabels first second) (secondStates first second)
(MachineStackSwap.statement
(MachineEmbedding.statement
(Δ := MachineEmbedding.Alphabet first.Γ (fun _ : Unit => second.Γ second.k₀))
(Λextra := Bool ⊕ first.Λ) none (second.m label))))
((secondStates first second) (state, (first.initialState, none)))
(MachineStackSwap.tapes (MachineEmbedding.tapes tapes (fun _ => [])))) = _
rw [MachineControl.stepAux_simulation, MachineStackSwap.stepAux_simulation,
MachineEmbedding.stepAux_simulation]
rfl
theorem firstConfiguration_init (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀) (input : List (first.Γ first.k₀)) :
firstConfiguration first second (initList first input) =
initList (machine first second relabel fallback) input := by
unfold firstConfiguration MachineControl.configuration MachineEmbedding.configuration initList
congr 1
funext k
rcases k with k | k | k
· by_cases h : k = first.k₀
· subst k
simp [machine, MachineEmbedding.tapes]
rfl
· simp [machine, MachineEmbedding.tapes, h]
· simp [machine, MachineEmbedding.tapes]
· simp [machine, MachineEmbedding.tapes]
theorem secondConfiguration_halt (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀) (output : List (second.Γ second.k₁)) :
secondConfiguration first second (haltList second output) =
haltList (machine first second relabel fallback) output := by
unfold secondConfiguration MachineControl.configuration MachineStackSwap.configuration
MachineEmbedding.configuration haltList
congr 1
funext k
rcases k with k | k | k
· simp [machine, MachineStackSwap.tapes, MachineEmbedding.tapes]
· by_cases h : k = second.k₁
· subst k
simp [machine, MachineStackSwap.tapes, MachineEmbedding.tapes]
rfl
· simp [machine, MachineStackSwap.tapes, MachineEmbedding.tapes, h]
· simp [machine, MachineStackSwap.tapes, MachineEmbedding.tapes]
def temporaryTapes (first second : FinTM2) (word : List (second.Γ second.k₀)) :
∀ k, List (Symbols first second k)
| .inl _ => []
| .inr (.inl _) => []
| .inr (.inr _) => word
def temporaryConfiguration (first second : FinTM2) (word : List (second.Γ second.k₀)) :
TM2.Cfg (Symbols first second) (Label first second) (State first second) :=
⟨some (bridgeLabel first second true),
((first.initialState, second.initialState), none), temporaryTapes first second word⟩
theorem firstTransfer_tapes (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(word : List (first.Γ first.k₁)) :
Reduction.MachineTransfer.tapesAt (.inl first.k₁) (.inr (.inr ()))
(firstConfiguration first second (haltList first word)).stk [] (word.reverse.map relabel) =
temporaryTapes first second (word.reverse.map relabel) := by
funext k
rcases k with k | k | k
· by_cases h : k = first.k₁
· subst k
simp [Reduction.MachineTransfer.tapesAt, temporaryTapes]
· simp [Reduction.MachineTransfer.tapesAt, temporaryTapes, firstConfiguration,
MachineControl.configuration, MachineEmbedding.configuration, MachineEmbedding.tapes,
haltList, h]
· simp [Reduction.MachineTransfer.tapesAt, temporaryTapes, firstConfiguration,
MachineControl.configuration, MachineEmbedding.configuration, MachineEmbedding.tapes]
· cases k
simp [Reduction.MachineTransfer.tapesAt, temporaryTapes]
theorem secondTransfer_tapes (first second : FinTM2) (word : List (second.Γ second.k₀)) :
Reduction.MachineTransfer.tapesAt (Γ := Symbols first second)
(.inr (.inr ())) (.inr (.inl second.k₀))
(temporaryTapes first second word) [] word.reverse =
(secondConfiguration first second (initList second word.reverse)).stk := by
funext k
rcases k with k | k | k
· simp [Reduction.MachineTransfer.tapesAt, temporaryTapes, secondConfiguration,
MachineControl.configuration, MachineStackSwap.configuration, MachineStackSwap.tapes,
MachineEmbedding.configuration, MachineEmbedding.tapes]
· by_cases h : k = second.k₀
· subst k
simp [Reduction.MachineTransfer.tapesAt, secondConfiguration,
MachineControl.configuration, MachineStackSwap.configuration, MachineStackSwap.tapes,
MachineEmbedding.configuration, MachineEmbedding.tapes, initList]
· simp [Reduction.MachineTransfer.tapesAt, temporaryTapes, secondConfiguration,
MachineControl.configuration, MachineStackSwap.configuration, MachineStackSwap.tapes,
MachineEmbedding.configuration, MachineEmbedding.tapes, initList, h]
· cases k
simp [Reduction.MachineTransfer.tapesAt, secondConfiguration,
MachineControl.configuration, MachineStackSwap.configuration, MachineStackSwap.tapes,
MachineEmbedding.configuration, MachineEmbedding.tapes]
def firstTransfer (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀) (word : List (first.Γ first.k₁)) :
StateTransition.EvalsToInTime (machine first second relabel fallback).step
(firstConfiguration first second (haltList first word))
(some (temporaryConfiguration first second (word.reverse.map relabel)))
(word.length + 1) := by
let run := Reduction.MachineTransfer.transferAtInTime
(.inl first.k₁ : Tape first second) (.inr (.inr ())) (by intro h; cases h)
relabel fallback (bridgeLabel first second false)
(some (bridgeLabel first second true)) (program first second relabel fallback) rfl
(firstConfiguration first second (haltList first word)).stk
(first.initialState, second.initialState) none
have inputTape : (firstConfiguration first second (haltList first word)).stk (.inl first.k₁) =
word := by simp [firstConfiguration, MachineControl.configuration,
MachineEmbedding.configuration, MachineEmbedding.tapes, haltList]
have outputTape :
(firstConfiguration first second (haltList first word)).stk (.inr (.inr ())) = [] := rfl
rw [inputTape, outputTape, List.append_nil, firstTransfer_tapes] at run
exact run
def secondTransfer (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀) (word : List (second.Γ second.k₀)) :
StateTransition.EvalsToInTime (machine first second relabel fallback).step
(temporaryConfiguration first second word)
(some (secondConfiguration first second (initList second word.reverse)))
(word.length + 1) := by
let run := Reduction.MachineTransfer.transferAtInTime
(.inr (.inr ()) : Tape first second) (.inr (.inl second.k₀)) (by intro h; cases h)
id fallback (bridgeLabel first second true) (some (.inr (.inr second.main)))
(program first second relabel fallback) rfl (temporaryTapes first second word)
(first.initialState, second.initialState) none
simp only [temporaryTapes, List.map_id, List.append_nil] at run
rw [secondTransfer_tapes] at run
exact run
def execute (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀)
(input : List (first.Γ first.k₀)) (middle : List (first.Γ first.k₁))
(output : List (second.Γ second.k₁)) (firstBudget secondBudget : Nat)
(runFirst : TM2OutputsInTime first input (some middle) firstBudget)
(runSecond : TM2OutputsInTime second (middle.map relabel) (some output) secondBudget) :
TM2OutputsInTime (machine first second relabel fallback) input (some output)
(firstBudget + 2 * (middle.length + 1) + secondBudget) := by
let start := MachineComposition.liftExecutionInTime first.step
(machine first second relabel fallback).step (firstConfiguration first second)
(firstStep first second relabel fallback) runFirst
let bridge₁ := firstTransfer first second relabel fallback middle
have bridge₂ := secondTransfer first second relabel fallback (middle.reverse.map relabel)
have reversal : (middle.reverse.map relabel).reverse = middle.map relabel := by
simp only [List.map_reverse, List.reverse_reverse]
rw [reversal] at bridge₂
simp only [List.length_map, List.length_reverse] at bridge₂
let finish := MachineComposition.liftExecutionInTime second.step
(machine first second relabel fallback).step (secondConfiguration first second)
(secondStep first second relabel fallback) runSecond
let phase₁₂ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ start bridge₁
let phase₁₂₃ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ phase₁₂ bridge₂
have full := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ phase₁₂₃ finish
rw [firstConfiguration_init, secondConfiguration_halt] at full
exact {
toEvalsTo := full.toEvalsTo
steps_le_m := by have h := full.steps_le_m; omega
}
noncomputable def compose {α β γ αΓ βΓ γΓ : Type}
{ea : α → List αΓ} {eb : β → List βΓ} {ec : γ → List γΓ}
{f : α → β} {g : β → γ}
(first : TM2ComputableInPolyTime ea eb f)
(second : TM2ComputableInPolyTime eb ec g) (fallback : βΓ) :
TM2ComputableInPolyTime ea ec (fun a => g (f a)) := by
let relabel : first.tm.Γ first.tm.k₁ → second.tm.Γ second.tm.k₀ :=
fun symbol => second.inputAlphabet.symm (first.outputAlphabet symbol)
let defaultSymbol := second.inputAlphabet.symm fallback
let intermediate : Polynomial Nat := Polynomial.X +
Polynomial.C (Runtime.programPushBound first.tm) * first.time
refine {
tm := machine first.tm second.tm relabel defaultSymbol
inputAlphabet := first.inputAlphabet
outputAlphabet := second.outputAlphabet
time := MachineComposition.compositionPolynomial first.time second.time intermediate
outputsFun := ?_
}
intro a
let input := (ea a).map first.inputAlphabet.invFun
let middle := (eb (f a)).map first.outputAlphabet.invFun
let output := (ec (g (f a))).map second.outputAlphabet.invFun
have handoff : middle.map relabel = (eb (f a)).map second.inputAlphabet.invFun := by
dsimp only [middle, relabel]
simp only [List.map_map]
apply List.map_congr_left
intro symbol _
exact congrArg second.inputAlphabet.symm (first.outputAlphabet.apply_symm_apply symbol)
have runSecond : TM2OutputsInTime second.tm (middle.map relabel) (some output)
(second.time.eval (eb (f a)).length) := by
rw [handoff]
exact second.outputsFun (f a)
have full := execute first.tm second.tm relabel defaultSymbol input middle output
(first.time.eval (ea a).length) (second.time.eval (eb (f a)).length)
(first.outputsFun a) runSecond
have intermediateBound : (eb (f a)).length ≤ intermediate.eval (ea a).length :=
Runtime.encodedOutputLength first a
have totalBound := MachineComposition.compositionBudget_le first.time second.time
intermediate (ea a).length (eb (f a)).length intermediateBound
have middleLength : middle.length = (eb (f a)).length := by simp only [middle, List.length_map]
exact {
toEvalsTo := full.toEvalsTo
steps_le_m := by
apply Nat.le_trans full.steps_le_m
simpa only [middleLength] using totalBound
}
noncomputable def composeBits {α β γ : Type}
{ea : α → List Bool} {eb : β → List Bool} {ec : γ → List Bool}
{f : α → β} {g : β → γ}
(first : TM2ComputableInPolyTime ea eb f)
(second : TM2ComputableInPolyTime eb ec g) :
TM2ComputableInPolyTime ea ec (fun a => g (f a)) :=
compose first second false
end BinPackingGames.Foundations.Complexity.MachineSequential
namespace BinPackingGames.Foundations.Complexity.MachineRepeat
open Turing
inductive ExtraTape
| counter | temporary | output
deriving DecidableEq
protected abbrev ExtraTape.enumList : List ExtraTape := [.counter, .temporary, .output]
protected theorem ExtraTape.enumList_getElem?_ctorIdx_eq (x : ExtraTape) :
ExtraTape.enumList[x.ctorIdx]? = some x := by
cases x <;> rfl
protected theorem ExtraTape.enumList_nodup : ExtraTape.enumList.Nodup := by decide
instance : Fintype ExtraTape where
elems := ⟨ExtraTape.enumList, ExtraTape.enumList_nodup⟩
complete x := by cases x <;> decide
inductive Phase
| parse | guard | bodyToTemp | tempToBody | finalToTemp | tempToOutput
deriving DecidableEq
protected abbrev Phase.enumList : List Phase := [.parse, .guard, .bodyToTemp, .tempToBody,
.finalToTemp, .tempToOutput]
protected theorem Phase.enumList_getElem?_ctorIdx_eq (x : Phase) :
Phase.enumList[x.ctorIdx]? = some x := by
cases x <;> rfl
protected theorem Phase.enumList_nodup : Phase.enumList.Nodup := by decide
instance : Fintype Phase where
elems := ⟨Phase.enumList, Phase.enumList_nodup⟩
complete x := by cases x <;> decide
abbrev Tape (M : FinTM2) := M.K ⊕ ExtraTape
abbrev Symbols (M : FinTM2) := MachineEmbedding.Alphabet M.Γ (fun _ : ExtraTape => M.Γ M.k₀)
abbrev Label (M : FinTM2) := M.Λ ⊕ Phase
abbrev State (M : FinTM2) := M.σ × Option (M.Γ M.k₀)
def extraTapes (M : FinTM2) (counter temporary output : List (M.Γ M.k₀)) :
ExtraTape → List (M.Γ M.k₀)
| .counter => counter
| .temporary => temporary
| .output => output
def auxProgram (M : FinTM2) (input : M.Γ M.k₀ ≃ Bool) (output : M.Γ M.k₁ ≃ Bool) :
Phase → TM2.Stmt (Symbols M) (Label M) (State M)
| .parse => .pop (.inl M.k₀) (fun s v => (s.1,v))
(.branch (fun s => input (s.2.getD (input.symm false)))
(.push (.inr .counter) (fun _ => input.symm true)
(.load (fun s => (s.1,none)) (.goto (fun _ => .inr .parse))))
(.load (fun s => (s.1,none)) (.goto (fun _ => .inr .guard))))
| .guard => .pop (.inr .counter) (fun s v => (s.1,v))
(.branch (fun s => s.2.isSome)
(.load (fun s => (s.1,none)) (.goto (fun _ => .inl M.main)))
(.load (fun s => (s.1,none)) (.goto (fun _ => .inr .finalToTemp))))
| .bodyToTemp => Reduction.MachineTransfer.loopAt (Γ := Symbols M) (Λ := Label M) (.inl M.k₁) (.inr .temporary)
(fun b => input.symm (output b)) (input.symm false)
(.inr .bodyToTemp) (some (.inr .tempToBody))
| .tempToBody => Reduction.MachineTransfer.loopAt (Γ := Symbols M) (Λ := Label M) (.inr .temporary) (.inl M.k₀)
id (input.symm false) (.inr .tempToBody) (some (.inr .guard))
| .finalToTemp => Reduction.MachineTransfer.loopAt (Γ := Symbols M) (Λ := Label M) (.inl M.k₀) (.inr .temporary)
id (input.symm false) (.inr .finalToTemp) (some (.inr .tempToOutput))
| .tempToOutput => Reduction.MachineTransfer.loopAt (Γ := Symbols M) (Λ := Label M) (.inr .temporary) (.inr .output)
id (input.symm false) (.inr .tempToOutput) none
def program (M : FinTM2) (input : M.Γ M.k₀ ≃ Bool) (output : M.Γ M.k₁ ≃ Bool) :
Label M → TM2.Stmt (Symbols M) (Label M) (State M) :=
MachineEmbedding.program (some (.inr .bodyToTemp)) M.m (auxProgram M input output)
def machine (M : FinTM2) (input : M.Γ M.k₀ ≃ Bool) (output : M.Γ M.k₁ ≃ Bool) : FinTM2 where
K := Tape M
kFin := by letI := M.kFin; exact inferInstanceAs (Fintype (Tape M))
k₀ := .inl M.k₀
k₁ := .inr .output
Γ := Symbols M
Λ := Label M
ΛFin := by letI := M.ΛFin; exact inferInstanceAs (Fintype (Label M))
main := .inr .parse
σ := State M
σFin := by
letI := M.σFin
letI := M.Γk₀Fin
exact inferInstanceAs (Fintype (State M))
initialState := (M.initialState,none)
Γk₀Fin := M.Γk₀Fin
m := program M input output
def inputTapes (M : FinTM2) (word counter : List (M.Γ M.k₀)) : ∀k, List (Symbols M k) :=
MachineEmbedding.tapes (initList M word).stk (extraTapes M counter [] [])
@[simp] theorem inputTapes_input (M : FinTM2) (word counter : List (M.Γ M.k₀)) :
inputTapes M word counter (.inl M.k₀) = word := by
simp [inputTapes, initList]
@[simp] theorem inputTapes_counter (M : FinTM2) (word counter : List (M.Γ M.k₀)) :
inputTapes M word counter (.inr .counter) = counter := rfl
def inputConfig (M : FinTM2) (phase : Phase) (word counter : List (M.Γ M.k₀)) :
TM2.Cfg (Symbols M) (Label M) (State M) :=
⟨some (.inr phase),(M.initialState,none),inputTapes M word counter⟩
def embedded (M : FinTM2) (counter : List (M.Γ M.k₀)) (c : M.Cfg) :
TM2.Cfg (Symbols M) (Label M) (State M) :=
MachineEmbedding.configuration (some (.inr .bodyToTemp)) (none : Option (M.Γ M.k₀))
(extraTapes M counter [] []) c
def temporaryTapes (M : FinTM2) (word counter : List (M.Γ M.k₀)) : ∀k, List (Symbols M k) :=
MachineEmbedding.tapes (fun _ => []) (extraTapes M counter word [])
def temporaryConfig (M : FinTM2) (phase : Phase) (word counter : List (M.Γ M.k₀)) :
TM2.Cfg (Symbols M) (Label M) (State M) :=
⟨some (.inr phase),(M.initialState,none),temporaryTapes M word counter⟩
private theorem inputTapes_update_input (M : FinTM2)
(word counter replacement : List (M.Γ M.k₀)) :
Function.update (inputTapes M word counter) (.inl M.k₀) replacement =
inputTapes M replacement counter := by
funext k
cases k with
| inl k =>
by_cases h : k = M.k₀
· subst k; simp [inputTapes, initList]
· simp [inputTapes, initList, h]
| inr k => simp [inputTapes]
private theorem inputTapes_update_counter (M : FinTM2)
(word counter replacement : List (M.Γ M.k₀)) :
Function.update (inputTapes M word counter) (.inr .counter) replacement =
inputTapes M word replacement := by
funext k
cases k with
| inl k => simp [inputTapes]
| inr k => cases k <;> simp [inputTapes, extraTapes]
private def oneStep {α : Type*} (step : α → Option α) (a b : α)
(h : step a = some b) : StateTransition.EvalsToInTime step a (some b) 1 where
steps := 1
evals_in_steps := by change step a = some b; exact h
steps_le_m := le_refl _
variable (M : FinTM2) (input : M.Γ M.k₀ ≃ Bool) (output : M.Γ M.k₁ ≃ Bool)
theorem parseStep_true (word counter : List (M.Γ M.k₀)) :
(machine M input output).step
(inputConfig M .parse (input.symm true :: word) counter) =
some (inputConfig M .parse word (input.symm true :: counter)) := by
change some (TM2.stepAux (auxProgram M input output .parse)
(M.initialState,none) (inputTapes M (input.symm true :: word) counter)) = _
simp [auxProgram, TM2.stepAux, inputTapes_update_input, inputTapes_update_counter,
inputConfig]
theorem parseStep_false (word counter : List (M.Γ M.k₀)) :
(machine M input output).step
(inputConfig M .parse (input.symm false :: word) counter) =
some (inputConfig M .guard word counter) := by
change some (TM2.stepAux (auxProgram M input output .parse)
(M.initialState,none) (inputTapes M (input.symm false :: word) counter)) = _
simp [auxProgram, TM2.stepAux, inputTapes_update_input, inputTapes_update_counter,
inputConfig]
theorem parseTrace (count : Nat) (word counter : List (M.Γ M.k₀)) :
(MachineComposition.advance (machine M input output).step)^[count+1]
(some (inputConfig M .parse ((encodeWord count).map input.symm ++ word) counter)) =
some (inputConfig M .guard word (List.replicate count (input.symm true) ++ counter)) := by
induction count generalizing counter with
| zero =>
simpa only [encodeWord, List.replicate_zero, List.nil_append, List.map_singleton,
List.singleton_append, Nat.zero_add, Function.iterate_one, MachineComposition.advance_some]
using! parseStep_false M input output word counter
| succ count ih =>
rw [Function.iterate_succ_apply]
change (MachineComposition.advance (machine M input output).step)^[count+1]
((machine M input output).step
(inputConfig M .parse ((encodeWord (count+1)).map input.symm ++ word) counter)) = _
simp only [encodeWord, List.replicate_succ, List.cons_append, List.map_cons,
List.map_append, List.map_replicate]
rw [parseStep_true]
have h := ih (input.symm true :: counter)
have hc : List.replicate count (input.symm true) ++ input.symm true :: counter =
input.symm true :: (List.replicate count (input.symm true) ++ counter) := by
rw [← List.singleton_append, ← List.append_assoc,
← List.replicate_succ', List.replicate_succ, List.cons_append]
rw [hc] at h
simpa only [encodeWord, List.map_append, List.map_replicate, List.map_singleton,
List.map_nil, List.singleton_append, List.append_assoc] using! h
theorem guardStep_some (word counter : List (M.Γ M.k₀)) (symbol : M.Γ M.k₀) :
(machine M input output).step (inputConfig M .guard word (symbol :: counter)) =
some (embedded M counter (initList M word)) := by
change some (TM2.stepAux (auxProgram M input output .guard)
(M.initialState,none) (inputTapes M word (symbol :: counter))) = _
simp [auxProgram, TM2.stepAux, inputTapes_update_counter,
embedded, MachineEmbedding.configuration]
exact ⟨rfl,rfl,rfl⟩
theorem guardStep_none (word : List (M.Γ M.k₀)) :
(machine M input output).step (inputConfig M .guard word []) =
some (inputConfig M .finalToTemp word []) := by
change some (TM2.stepAux (auxProgram M input output .guard)
(M.initialState,none) (inputTapes M word [])) = _
simp [auxProgram, TM2.stepAux, inputTapes_update_counter, inputConfig]
def bodyExecution (counter : List (M.Γ M.k₀))
(word : List (M.Γ M.k₀)) (result : List (M.Γ M.k₁)) (budget : Nat)
(run : TM2OutputsInTime M word (some result) budget) :
StateTransition.EvalsToInTime (machine M input output).step
(embedded M counter (initList M word))
(some (embedded M counter (haltList M result))) budget :=
MachineComposition.embeddedExecution (some (.inr .bodyToTemp : Label M))
(none : Option (M.Γ M.k₀)) (extraTapes M counter [] []) M.m
(auxProgram M input output) run
private theorem bodyToTemp_tapes (result : List (M.Γ M.k₁)) (counter : List (M.Γ M.k₀)) :
Reduction.MachineTransfer.tapesAt (.inl M.k₁) (.inr .temporary)
(embedded M counter (haltList M result)).stk []
(result.reverse.map (fun b => input.symm (output b))) =
temporaryTapes M (result.reverse.map (fun b => input.symm (output b))) counter := by
funext k
cases k with
| inl k =>
by_cases h : k = M.k₁
· subst k; simp [Reduction.MachineTransfer.tapesAt, temporaryTapes]
· simp [Reduction.MachineTransfer.tapesAt, temporaryTapes, embedded,
MachineEmbedding.configuration, haltList, h]
| inr k =>
cases k <;> simp [Reduction.MachineTransfer.tapesAt, temporaryTapes, embedded,
MachineEmbedding.configuration, extraTapes]
private theorem tempToBody_tapes (word counter : List (M.Γ M.k₀)) :
Reduction.MachineTransfer.tapesAt (Γ := Symbols M) (.inr .temporary) (.inl M.k₀)
(temporaryTapes M word counter) [] word.reverse = inputTapes M word.reverse counter := by
funext k
cases k with
| inl k =>
by_cases h : k = M.k₀
· subst k; simp [Reduction.MachineTransfer.tapesAt, inputTapes, initList]
· simp [Reduction.MachineTransfer.tapesAt, inputTapes, temporaryTapes, initList, h]
| inr k =>
cases k <;> simp [Reduction.MachineTransfer.tapesAt, inputTapes, temporaryTapes, extraTapes]
private theorem finalToTemp_tapes (word : List (M.Γ M.k₀)) :
Reduction.MachineTransfer.tapesAt (Γ := Symbols M) (.inl M.k₀) (.inr .temporary)
(inputTapes M word []) [] word.reverse = temporaryTapes M word.reverse [] := by
funext k
cases k with
| inl k =>
by_cases h : k = M.k₀
· subst k; simp [Reduction.MachineTransfer.tapesAt, temporaryTapes]
· simp [Reduction.MachineTransfer.tapesAt, inputTapes, temporaryTapes, initList, h]
| inr k =>
cases k <;> simp [Reduction.MachineTransfer.tapesAt, inputTapes, temporaryTapes, extraTapes]
private theorem tempToOutput_tapes (word : List (M.Γ M.k₀)) :
Reduction.MachineTransfer.tapesAt (Γ := Symbols M) (.inr .temporary) (.inr .output)
(temporaryTapes M word []) [] word.reverse =
(haltList (machine M input output) word.reverse).stk := by
funext k
cases k with
| inl k => simp [Reduction.MachineTransfer.tapesAt, temporaryTapes, haltList, machine]
| inr k =>
cases k <;> simp [Reduction.MachineTransfer.tapesAt, temporaryTapes, haltList,
machine, extraTapes]
rfl
def bodyToTemp (result : List (M.Γ M.k₁)) (counter : List (M.Γ M.k₀)) :
StateTransition.EvalsToInTime (machine M input output).step
(embedded M counter (haltList M result))
(some (temporaryConfig M .tempToBody
(result.reverse.map (fun b => input.symm (output b))) counter)) (result.length+1) := by
have run := Reduction.MachineTransfer.transferAtInTime
(Γ := Symbols M) (.inl M.k₁) (.inr .temporary) (by intro h; cases h)
(fun b => input.symm (output b)) (input.symm false)
(.inr .bodyToTemp) (some (.inr .tempToBody)) (program M input output) rfl
(embedded M counter (haltList M result)).stk M.initialState none
have hs : (embedded M counter (haltList M result)).stk (.inl M.k₁) = result := by
simp [embedded, MachineEmbedding.configuration, haltList]
have hd : (embedded M counter (haltList M result)).stk (.inr .temporary) = [] := rfl
rw [hs,hd,List.append_nil,bodyToTemp_tapes] at run
exact run
def tempToBody (word counter : List (M.Γ M.k₀)) :
StateTransition.EvalsToInTime (machine M input output).step
(temporaryConfig M .tempToBody word counter)
(some (inputConfig M .guard word.reverse counter)) (word.length+1) := by
have run := Reduction.MachineTransfer.transferAtInTime
(Γ := Symbols M) (.inr .temporary) (.inl M.k₀) (by intro h; cases h)
id (input.symm false) (.inr .tempToBody) (some (.inr .guard))
(program M input output) rfl (temporaryTapes M word counter) M.initialState none
simp only [temporaryTapes, MachineEmbedding.tapes, extraTapes,
List.map_id, List.append_nil] at run
rw [show Reduction.MachineTransfer.tapesAt (Γ := Symbols M) (.inr .temporary) (.inl M.k₀)
(MachineEmbedding.tapes (fun _ => []) (extraTapes M counter word [])) [] word.reverse =
inputTapes M word.reverse counter from tempToBody_tapes M word counter] at run
exact run
def finalToTemp (word : List (M.Γ M.k₀)) :
StateTransition.EvalsToInTime (machine M input output).step
(inputConfig M .finalToTemp word [])
(some (temporaryConfig M .tempToOutput word.reverse [])) (word.length+1) := by
have run := Reduction.MachineTransfer.transferAtInTime
(Γ := Symbols M) (.inl M.k₀) (.inr .temporary) (by intro h; cases h)
id (input.symm false) (.inr .finalToTemp) (some (.inr .tempToOutput))
(program M input output) rfl (inputTapes M word []) M.initialState none
have hd : inputTapes M word [] (.inr .temporary) = [] := rfl
rw [inputTapes_input,hd,List.map_id,List.append_nil,finalToTemp_tapes] at run
exact run
def tempToOutput (word : List (M.Γ M.k₀)) :
StateTransition.EvalsToInTime (machine M input output).step
(temporaryConfig M .tempToOutput word [])
(some (haltList (machine M input output) word.reverse)) (word.length+1) := by
have run := Reduction.MachineTransfer.transferAtInTime
(Γ := Symbols M) (.inr .temporary) (.inr .output) (by intro h; cases h)
id (input.symm false) (.inr .tempToOutput) none
(program M input output) rfl (temporaryTapes M word []) M.initialState none
have hs : temporaryTapes M word [] (.inr .temporary) = word := rfl
have hd : temporaryTapes M word [] (.inr .output) = [] := rfl
rw [hs,hd,List.map_id,List.append_nil,tempToOutput_tapes] at run
exact run
def bodyReturn (result : List (M.Γ M.k₁)) (counter : List (M.Γ M.k₀)) :
StateTransition.EvalsToInTime (machine M input output).step
(embedded M counter (haltList M result))
(some (inputConfig M .guard (result.map (fun b => input.symm (output b))) counter))
(2*(result.length+1)) := by
have first := bodyToTemp M input output result counter
have second := tempToBody M input output (result.reverse.map (fun b => input.symm (output b))) counter
simp only [List.map_reverse,List.reverse_reverse,List.length_reverse,List.length_map] at first second
have full := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ first second
exact { toEvalsTo := full.toEvalsTo, steps_le_m := by have h := full.steps_le_m; omega }
def finish (word : List (M.Γ M.k₀)) :
StateTransition.EvalsToInTime (machine M input output).step
(inputConfig M .guard word []) (some (haltList (machine M input output) word))
(2*word.length+3) := by
have guard := oneStep _ _ _ (guardStep_none M input output word)
have first := finalToTemp M input output word
have second := tempToOutput M input output word.reverse
simp only [List.reverse_reverse,List.length_reverse] at second
have gfirst := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ guard first
have full := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ gfirst second
exact { toEvalsTo := full.toEvalsTo, steps_le_m := by have h := full.steps_le_m; omega }
def loopBudget : Nat → (Nat → List Bool) → (Nat → Nat) → Nat
| 0, words, _ => 2*(words 0).length+3
| count+1, words, budgets => budgets 0 + 2*(words 1).length+3 +
loopBudget count (fun i => words (i+1)) (fun i => budgets (i+1))
def loopExecution (count : Nat) (words : Nat → List Bool) (budgets : Nat → Nat)
(runs : ∀ i, i < count → TM2OutputsInTime M ((words i).map input.symm)
(some ((words (i+1)).map output.symm)) (budgets i)) :
StateTransition.EvalsToInTime (machine M input output).step
(inputConfig M .guard ((words 0).map input.symm)
(List.replicate count (input.symm true)))
(some (haltList (machine M input output) ((words count).map input.symm)))
(loopBudget count words budgets) := by
induction count generalizing words budgets with
| zero =>
simpa only [loopBudget,List.replicate_zero,List.length_map] using!
finish M input output ((words 0).map input.symm)
| succ count ih =>
rw [List.replicate_succ]
have guard := oneStep _ _ _ (guardStep_some M input output ((words 0).map input.symm)
(List.replicate count (input.symm true)) (input.symm true))
have body := bodyExecution M input output (List.replicate count (input.symm true))
((words 0).map input.symm) ((words 1).map output.symm) (budgets 0)
(runs 0 (Nat.zero_lt_succ count))
have returned := bodyReturn M input output ((words 1).map output.symm)
(List.replicate count (input.symm true))
have handoff : ((words 1).map output.symm).map (fun b => input.symm (output b)) =
(words 1).map input.symm := by
simp only [List.map_map,Function.comp_def,Equiv.apply_symm_apply]
rw [handoff,List.length_map] at returned
have tail := ih (fun i => words (i+1)) (fun i => budgets (i+1))
(fun i hi => runs (i+1) (Nat.succ_lt_succ hi))
have first := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ guard body
have second := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ first returned
have full := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ second tail
exact {
toEvalsTo := full.toEvalsTo
steps_le_m := by
have h := full.steps_le_m
simp only [loopBudget]
omega
}
private theorem inputConfig_init (word : List (M.Γ M.k₀)) :
inputConfig M .parse word [] = initList (machine M input output) word := by
unfold inputConfig initList
congr 1
funext k
cases k with
| inl k =>
by_cases h : k = M.k₀
· subst k; simp [inputTapes,initList,machine]; rfl
· simp [inputTapes,initList,machine,h]
| inr k => cases k <;> simp [inputTapes,initList,machine,extraTapes]
def executeSequence (count : Nat) (words : Nat → List Bool) (budgets : Nat → Nat)
(runs : ∀ i, i < count → TM2OutputsInTime M ((words i).map input.symm)
(some ((words (i+1)).map output.symm)) (budgets i)) :
TM2OutputsInTime (machine M input output)
((encodeWord count ++ words 0).map input.symm)
(some ((words count).map input.symm))
(count+1 + loopBudget count words budgets) := by
let parsed : StateTransition.EvalsToInTime (machine M input output).step
(inputConfig M .parse ((encodeWord count).map input.symm ++ (words 0).map input.symm) [])
(some (inputConfig M .guard ((words 0).map input.symm)
(List.replicate count (input.symm true)))) (count+1) := {
steps := count+1
evals_in_steps := by
change (MachineComposition.advance (machine M input output).step)^[count+1]
(some (inputConfig M .parse ((encodeWord count).map input.symm ++ (words 0).map input.symm) [])) = _
simpa only [List.append_nil] using! parseTrace M input output count
((words 0).map input.symm) []
steps_le_m := le_refl _
}
have body := loopExecution M input output count words budgets runs
have full := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ parsed body
rw [inputConfig_init M input output] at full
simp only [TM2OutputsInTime, Option.map_some]
change StateTransition.EvalsToInTime (machine M input output).step
(initList (machine M input output) ((encodeWord count ++ words 0).map input.symm))
(some (haltList (machine M input output) ((words count).map input.symm))) _
erw [List.map_append]
simpa only [Nat.add_comm] using! full
omit M input output in
theorem loopBudget_eq_sum (count : Nat) (words : Nat → List Bool) (budgets : Nat → Nat) :
loopBudget count words budgets =
(∑ i ∈ Finset.range count, (budgets i + 2*(words (i+1)).length+3)) +
2*(words count).length+3 := by
induction count generalizing words budgets with
| zero => simp [loopBudget]
| succ count ih =>
rw [loopBudget, ih, Finset.sum_range_succ']
simp only [Nat.zero_add]
omega
def executeSequenceSum (count : Nat) (words : Nat → List Bool) (budgets : Nat → Nat)
(runs : ∀ i, i < count → TM2OutputsInTime M ((words i).map input.symm)
(some ((words (i+1)).map output.symm)) (budgets i)) :
TM2OutputsInTime (machine M input output)
((encodeWord count ++ words 0).map input.symm)
(some ((words count).map input.symm))
(count+1 + ((∑ i ∈ Finset.range count, (budgets i+2*(words (i+1)).length+3)) +
2*(words count).length+3)) := by
rw [← loopBudget_eq_sum]
exact executeSequence M input output count words budgets runs
omit M input output in
theorem loopBudget_le (count : Nat) (words : Nat → List Bool) (budgets : Nat → Nat)
(bodyBound lengthBound : Nat)
(hb : ∀i, i<count → budgets i ≤ bodyBound)
(hl : ∀i, i≤count → (words i).length ≤ lengthBound) :
loopBudget count words budgets ≤ count*(bodyBound+2*lengthBound+3)+2*lengthBound+3 := by
rw [loopBudget_eq_sum]
have hs : (∑ i ∈ Finset.range count, (budgets i+2*(words (i+1)).length+3)) ≤
count*(bodyBound+2*lengthBound+3) := by
calc
_ ≤ ∑ i ∈ Finset.range count, (bodyBound+2*lengthBound+3) := by
apply Finset.sum_le_sum
intro i hi
have hi' := Finset.mem_range.mp hi
have hbi := hb i hi'
have hli := hl (i+1) hi'
omega
_ = _ := by simp
have hlast := hl count (le_refl _)
omega
end BinPackingGames.Foundations.Complexity.MachineRepeat
namespace BinPackingGames.Foundations.Complexity.MachineFiniteAlphabet
open Turing
theorem input_finite (M : FinTM2) : Finite (M.Γ M.k₀) := by
let := M.Γk₀Fin
infer_instance
theorem of_homogeneous (M : FinTM2) {α : Type} [Finite α]
(alphabet : ∀ k, M.Γ k = α) : FiniteAlphabet M := by
intro k
rw [alphabet k]
infer_instance
theorem of_bool (M : FinTM2) (alphabet : ∀ k, M.Γ k = Bool) : FiniteAlphabet M :=
of_homogeneous M alphabet
theorem of_equiv (M : FinTM2) {α : Type} [Finite α]
(alphabet : ∀ k, M.Γ k ≃ α) : FiniteAlphabet M := by
intro k
exact Finite.of_equiv α (alphabet k).symm
theorem symbols_finite (M : FinTM2) (finiteAlphabet : FiniteAlphabet M) :
Finite (Σ k, M.Γ k) := by
let := M.kFin
let : ∀ k, Finite (M.Γ k) := finiteAlphabet
infer_instance
theorem sequential_machine (first second : FinTM2)
(relabel : first.Γ first.k₁ → second.Γ second.k₀)
(fallback : second.Γ second.k₀)
(hfirst : FiniteAlphabet first) (hsecond : FiniteAlphabet second) :
FiniteAlphabet (MachineSequential.machine first second relabel fallback) := by
intro tape
rcases tape with tape | tape | tape
· exact hfirst tape
· exact hsecond tape
· exact input_finite second
theorem compose {α β γ αΓ βΓ γΓ : Type}
{ea : α → List αΓ} {eb : β → List βΓ} {ec : γ → List γΓ}
{f : α → β} {g : β → γ}
(first : TM2ComputableInPolyTime ea eb f)
(second : TM2ComputableInPolyTime eb ec g) (fallback : βΓ)
(hfirst : FiniteAlphabet first.tm) (hsecond : FiniteAlphabet second.tm) :
FiniteAlphabet (MachineSequential.compose first second fallback).tm := by
exact sequential_machine first.tm second.tm
(fun symbol => second.inputAlphabet.symm (first.outputAlphabet symbol))
(second.inputAlphabet.symm fallback) hfirst hsecond
theorem composeBits {α β γ : Type}
{ea : α → List Bool} {eb : β → List Bool} {ec : γ → List Bool}
{f : α → β} {g : β → γ}
(first : TM2ComputableInPolyTime ea eb f)
(second : TM2ComputableInPolyTime eb ec g)
(hfirst : FiniteAlphabet first.tm) (hsecond : FiniteAlphabet second.tm) :
FiniteAlphabet (MachineSequential.composeBits first second).tm :=
compose first second false hfirst hsecond
theorem repeat_machine (body : FinTM2)
(input : body.Γ body.k₀ ≃ Bool) (output : body.Γ body.k₁ ≃ Bool)
(hbody : FiniteAlphabet body) :
FiniteAlphabet (MachineRepeat.machine body input output) := by
intro tape
cases tape with
| inl tape => exact hbody tape
| inr _ => exact input_finite body
end BinPackingGames.Foundations.Complexity.MachineFiniteAlphabet
end OAI