cfahlgren1/math / lean /OAI /Computability /MinUncut /Machines /MachineSubstitution.lean
cfahlgren1's picture
download
raw
38.8 kB
import Mathlib
import OAI.Computability.MinUncut.Machines.MachineCompare
namespace OAI
section
namespace MinUncutGames.Reduction.MachineSubstitution
open Turing
open MinUncutGames.Foundations.Complexity
section PushWord
variable {K Λ σ : Type} {Γ : K → Type} [DecidableEq K]
def pushWord (dst : K) : List (Γ dst) → TM2.Stmt Γ Λ σ → TM2.Stmt Γ Λ σ
| [], continuation => continuation
| symbol :: word, continuation =>
.push dst (fun _ => symbol) (pushWord dst word continuation)
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
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⟩
theorem update_input (input output replacement : List Bool) :
Function.update (tapeStacks input output) false replacement = tapeStacks replacement output := by
funext side
cases side <;> simp [tapeStacks]
theorem update_output (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]
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]
rfl
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) = _
exact congrArg (fun st => (some ⟨some none, state, st⟩ :
Option (TM2.Cfg (fun _ : Bool => Bool) (Option Bool) (Option Bool))))
(update_output input output ((emit symbol).reverse ++ output))
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)
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
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 MinUncutGames.Reduction.MachineSubstitution
end
section
namespace MinUncutGames.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⟩
theorem update_input (input output replacement : List Bool) :
Function.update (tapeStacks input output) false replacement = tapeStacks replacement output := by
funext side
cases side <;> simp [tapeStacks]
theorem update_output (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 (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]
rfl
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, update_output]
rfl
def next (configuration : Option machine.Cfg) : Option machine.Cfg :=
configuration.bind machine.step
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
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
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 MinUncutGames.Reduction.MachineReverse
end
section
namespace MinUncutGames.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 MinUncutGames.Foundations.Complexity.MachineStackSwap
end
section
namespace MinUncutGames.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 MinUncutGames.Foundations.Complexity.MachineSequential
end
end OAI

Xet Storage Details

Size:
38.8 kB
·
Xet hash:
5287855a85a9c27cf99aaf33f29424f191861070f496b0183f19c47fd66364bf

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