cfahlgren1's picture
download
raw
8.13 kB
import Mathlib
import OAI.Combinatorics.IndependentSets.Machines.MachineDummyRows
namespace OAI
namespace IndependentSetsGames.Foundations.Complexity.MachinePaddingRows
open Turing MachineComposition
inductive Control | initialize | guard | bump
deriving DecidableEq
instance : Fintype Control where
elems := {.initialize, .guard, .bump}
complete control := by cases control <;> simp
abbrev Tape := Fin 6 ⊕ Unit
abbrev Label (d : Nat) := MachineDummyRows.Label d ⊕ Control
abbrev Alphabet : Tape → Type := MachineEmbedding.Alphabet
(fun _ : Fin 6 => Bool) (fun _ : Unit => Bool)
abbrev State := (Unit × Option Bool) × Unit
def bodyEntry (d : Nat) (hd : 0 < d) : Label d :=
.inl (MachineDummyRows.rowLabel ⟨0, hd⟩ .relationRead)
def extra (d : Nat) (hd : 0 < d) : Control → TM2.Stmt Alphabet (Label d) State
| .initialize => Reduction.MachineSubstitution.pushWord (.inl 2)
MachineDummyRows.trueBits.reverse
(.load (fun _ => (((), none), ())) (.goto (fun _ => .inr .guard)))
| .guard => .pop (.inr ()) (fun _ bit => (((), bit), ()))
(.branch (fun s => s.1.2.getD false)
(.load (fun _ => (((), none), ())) (.goto (fun _ => bodyEntry d hd)))
(.push (.inr ()) (fun _ => false)
(.load (fun _ => (((), none), ())) .halt)))
| .bump => .push (.inl 0) (fun _ => true) (.goto (fun _ => .inr .guard))
def program (d : Nat) (hd : 0 < d) : Label d → TM2.Stmt Alphabet (Label d) State :=
MachineEmbedding.program (some (.inr .bump)) (MachineDummyRows.program d) (extra d hd)
def tapes (v e fuel : Nat) (output : List Bool) : ∀ k, List (Alphabet k) :=
MachineEmbedding.tapes (MachineDummyRows.fieldTapes v e output)
(fun _ : Unit => encodeWord fuel)
def cfg (d : Nat) (label : Option (Label d)) (v e fuel : Nat)
(output : List Bool) : TM2.Cfg Alphabet (Label d) State :=
⟨label, (((), none), ()), tapes v e fuel output⟩
theorem bodyTrace (d : Nat) (hd : 0 < d) (v e fuel : Nat) (output : List Bool) :
(advance (TM2.step (program d hd)))^[MachineDummyRows.steps v e output d]
(some (cfg d (some (bodyEntry d hd)) v e fuel output)) =
some (cfg d (some (.inr .bump)) v (e + d) fuel
(output ++ MachineDummyRows.rowsBits v e d)) := by
have run := MachineDummyRows.suffixTrace d ⟨0, hd⟩ (by simp)
v e output () none
have lifted := liftSuccessfulTrace
(TM2.step (MachineDummyRows.program d)) (TM2.step (program d hd))
(MachineEmbedding.configuration (some (.inr .bump)) ()
(fun _ : Unit => encodeWord fuel))
(by
intro a b h
exact MachineEmbedding.step_simulation (some (.inr Control.bump)) ()
(fun _ : Unit => encodeWord fuel) (MachineDummyRows.program d) (extra d hd) a b h)
(MachineDummyRows.steps v e output d) _ _ run
exact lifted
theorem guard_succ (d : Nat) (hd : 0 < d) (v e fuel : Nat) (output : List Bool) :
TM2.step (program d hd) (cfg d (some (.inr .guard)) v e (fuel + 1) output) =
some (cfg d (some (bodyEntry d hd)) v e fuel output) := by
change some (TM2.stepAux (extra d hd .guard) _ _) = _
simp only [extra, TM2.stepAux, tapes, MachineEmbedding.tapes_inr,
encodeWord, List.replicate_succ, List.cons_append, List.head?_cons,
List.tail_cons, Option.getD_some, Bool.cond_true]
congr 2
funext k
cases k <;> simp [tapes, MachineEmbedding.tapes, Function.update, encodeWord]
theorem guard_zero (d : Nat) (hd : 0 < d) (v e : Nat) (output : List Bool) :
TM2.step (program d hd) (cfg d (some (.inr .guard)) v e 0 output) =
some (cfg d none v e 0 output) := by
change some (TM2.stepAux (extra d hd .guard) _ _) = _
simp only [extra, TM2.stepAux, tapes, MachineEmbedding.tapes_inr,
encodeWord, List.replicate_zero, List.nil_append, List.head?_cons,
List.tail_cons, Option.getD_some, Bool.cond_false]
congr 2
funext k
cases k <;> simp [tapes, MachineEmbedding.tapes, Function.update, encodeWord]
theorem bump_step (d : Nat) (hd : 0 < d) (v e fuel : Nat) (output : List Bool) :
TM2.step (program d hd) (cfg d (some (.inr .bump)) v e fuel output) =
some (cfg d (some (.inr .guard)) (v + 1) e fuel output) := by
change some (TM2.stepAux (extra d hd .bump) _ _) = _
simp only [extra, TM2.stepAux]
congr 2
funext k
cases k with
| inl k => fin_cases k <;> simp [tapes, MachineEmbedding.tapes,
MachineDummyRows.fieldTapes, encodeWord, List.replicate_succ]
| inr k => simp [tapes, MachineEmbedding.tapes]
def paddingBits (d v e : Nat) : Nat → List Bool
| 0 => []
| count + 1 => MachineDummyRows.rowsBits v e d ++ paddingBits d (v + 1) (e + d) count
def steps (d v e : Nat) (output : List Bool) : Nat → Nat
| 0 => 1
| count + 1 => (MachineDummyRows.steps v e output d + 2) +
steps d (v + 1) (e + d) (output ++ MachineDummyRows.rowsBits v e d) count
theorem loopTrace (d : Nat) (hd : 0 < d) (count v e : Nat) (output : List Bool) :
(advance (TM2.step (program d hd)))^[steps d v e output count]
(some (cfg d (some (.inr .guard)) v e count output)) =
some (cfg d none (v + count) (e + count * d) 0
(output ++ paddingBits d v e count)) := by
induction count generalizing v e output with
| zero => simpa [steps, paddingBits] using guard_zero d hd v e output
| succ count ih =>
rw [steps, Nat.add_comm (MachineDummyRows.steps v e output d + 2),
Function.iterate_add_apply]
have hb : (advance (TM2.step (program d hd)))^[MachineDummyRows.steps v e output d + 2]
(some (cfg d (some (.inr .guard)) v e (count + 1) output)) =
some (cfg d (some (.inr .guard)) (v + 1) (e + d) count
(output ++ MachineDummyRows.rowsBits v e d)) := by
rw [show MachineDummyRows.steps v e output d + 2 =
(MachineDummyRows.steps v e output d + 1) + 1 by omega,
Function.iterate_succ_apply, advance_some, guard_succ,
Function.iterate_succ_apply', bodyTrace, advance_some, bump_step]
rw [hb, ih]
simp only [paddingBits, List.append_assoc, Nat.succ_mul]
congr 2 <;> omega
def initialTapes (v e fuel : Nat) (output : List Bool) : ∀ k, List (Alphabet k) :=
Function.update (tapes v e fuel output) (.inl 2) []
theorem initialize_step (d : Nat) (hd : 0 < d) (v e fuel : Nat) (output : List Bool) :
TM2.step (program d hd)
⟨some (.inr .initialize), (((), none), ()), initialTapes v e fuel output⟩ =
some (cfg d (some (.inr .guard)) v e fuel output) := by
change some (TM2.stepAux (extra d hd .initialize) _ _) = _
simp only [extra, Reduction.MachineSubstitution.stepAux_pushWord,
List.reverse_reverse, initialTapes, Function.update_self, List.append_nil,
Function.update_idem, TM2.stepAux]
congr 2
funext k
cases k with
| inl k => fin_cases k <;> simp [tapes, MachineEmbedding.tapes,
MachineDummyRows.fieldTapes]
| inr k => simp [tapes, MachineEmbedding.tapes]
theorem paddingTrace (d : Nat) (hd : 0 < d) (count v e : Nat) (output : List Bool) :
(advance (TM2.step (program d hd)))^[steps d v e output count + 1]
(some ⟨some (.inr .initialize), (((), none), ()), initialTapes v e count output⟩) =
some (cfg d none (v + count) (e + count * d) 0
(output ++ paddingBits d v e count)) := by
rw [Function.iterate_succ_apply, advance_some, initialize_step, loopTrace]
def machine (d : Nat) (hd : 0 < d) : FinTM2 where
K := Tape
k₀ := .inl 0
k₁ := .inl 4
Γ := Alphabet
Λ := Label d
main := .inr .initialize
σ := State
initialState := (((), none), ())
m := program d hd
def execution (d : Nat) (hd : 0 < d) (count v e : Nat) (output : List Bool) :
StateTransition.EvalsToInTime (machine d hd).step
⟨some (.inr .initialize), (((), none), ()), initialTapes v e count output⟩
(some (cfg d none (v + count) (e + count * d) 0
(output ++ paddingBits d v e count)))
(steps d v e output count + 1) where
steps := steps d v e output count + 1
evals_in_steps := by
convert paddingTrace d hd count v e output using 1
rfl
steps_le_m := le_rfl
end IndependentSetsGames.Foundations.Complexity.MachinePaddingRows
end OAI

Xet Storage Details

Size:
8.13 kB
·
Xet hash:
a5dbccae165d4088200ba401ace3d0b68f31995d84f84ac36c82ddfce6191ffa

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