roseking's picture
Add lean source and binary files (part 44)
e320e82 verified
Raw History Blame Contribute Delete
11.8 kB
import Mathlib
import OAI.Combinatorics.IndependentSets.Machines.MachineLookupDiscard
import OAI.Combinatorics.IndependentSets.Machines.AmbientRelation
namespace OAI
namespace IndependentSetsGames.Foundations.Complexity.FinalCNFMachine.Program
open Turing
open PCP.AlphabetTable
def headerFrame (input archive vertices darts rowIndex tail head accumulator : List Bool) :
Tape → List Bool
| .input => input
| .archive => archive
| .vertices => vertices
| .darts => darts
| .rowIndex => rowIndex
| .tail => tail
| .head => head
| .accumulator => accumulator
| _ => []
def headerInputTapes (input : List Bool) : Tape → List Bool :=
headerFrame input [] [] [] [] [] [] []
def headerResultTapes (n m : Nat) (rowsBits : List Bool) : Tape → List Bool :=
headerFrame rowsBits (encodeWords [n, m] ++ rowsBits)
(encodeWord n) (encodeWord m) (encodeWord 0) [] []
(encodeWords [6 * n + 36864 * m, 40960 * m]).reverse
@[simp] theorem headerResultTapes_input (n m : Nat) (rowsBits : List Bool) :
headerResultTapes n m rowsBits .input = rowsBits := rfl
@[simp] theorem headerResultTapes_archive (n m : Nat) (rowsBits : List Bool) :
headerResultTapes n m rowsBits .archive = encodeWords [n, m] ++ rowsBits := rfl
@[simp] theorem headerResultTapes_accumulator (n m : Nat) (rowsBits : List Bool) :
headerResultTapes n m rowsBits .accumulator =
(encodeWords [6 * n + 36864 * m, 40960 * m]).reverse := rfl
@[simp] theorem headerResultTapes_rowIndex (n m : Nat) (rowsBits : List Bool) :
headerResultTapes n m rowsBits .rowIndex = encodeWord 0 := rfl
noncomputable def headerTimePolynomial : Polynomial Nat :=
Polynomial.C 25 * Polynomial.X + Polynomial.C 52
@[simp] theorem headerTimePolynomial_eval (length : Nat) :
headerTimePolynomial.eval length = 25 * length + 52 := by
simp [headerTimePolynomial]
def headerInTime (rowPlan : Plan) (n m : Nat) (rowsBits : List Bool) (ambient : Ambient) :
StateTransition.EvalsToInTime (TM2.step (program headerPlan rowPlan))
⟨some .copyFirst, ((ambient, ()), none),
headerInputTapes (encodeWords [n, m] ++ rowsBits)⟩
(some ⟨some .guard, ((ambient, ()), none), headerResultTapes n m rowsBits⟩)
(headerTimePolynomial.eval (encodeWords [n, m] ++ rowsBits).length) := by
let word := encodeWords [n, m] ++ rowsBits
let bits := encodeWords [6 * n + 36864 * m, 40960 * m]
let b₀ := headerInputTapes word
let b₁ := headerFrame word word [] [] [] [] [] []
let b₂ := headerFrame (encodeWord m ++ rowsBits) word (encodeWord n) [] [] [] [] []
let b₃ := headerFrame rowsBits word (encodeWord n) (encodeWord m) [] [] [] []
let b₄ := headerFrame rowsBits word (encodeWord n) (encodeWord m)
(encodeWord 0) (encodeWord 0) (encodeWord 0) []
let b₅ := headerFrame rowsBits word (encodeWord n) (encodeWord m)
(encodeWord 0) (encodeWord 0) (encodeWord 0) bits.reverse
let b₆ := headerFrame rowsBits word (encodeWord n) (encodeWord m)
(encodeWord 0) [] (encodeWord 0) bits.reverse
let b₇ := headerResultTapes n m rowsBits
have h₀ : Function.update b₀ Tape.archive (b₀ .input ++ b₀ .archive) = b₁ := by
funext tape; cases tape <;> simp [b₀, b₁, headerInputTapes, headerFrame]
have h₁ : Hastad.SourceMachine.fieldTapes Tape.input Tape.vertices b₁
(encodeWord m ++ rowsBits) (encodeWord n ++ b₁ .vertices) = b₂ := by
funext tape; cases tape <;> simp [Hastad.SourceMachine.fieldTapes, b₁, b₂, headerFrame]
have h₂ : Hastad.SourceMachine.fieldTapes Tape.input Tape.darts b₂
rowsBits (encodeWord m ++ b₂ .darts) = b₃ := by
funext tape; cases tape <;> simp [Hastad.SourceMachine.fieldTapes, b₂, b₃, headerFrame]
have h₄ : Emitter.resultTapes Tape.accumulator b₄
(Emitter.prefixBits (Emitter.listCommands headerPlan) (values n m 0 0 0)
ambient headerPlan.length) = b₅ := by
rw [Emitter.bits_listCommands, headerPlan_bits]
funext tape; cases tape <;> simp [Emitter.resultTapes, b₄, b₅, bits, headerFrame]
have h₅ : Function.update b₅ Tape.tail [] = b₆ := by
funext tape; cases tape <;> simp [b₅, b₆, headerFrame]
have h₆ : Function.update b₆ Tape.head [] = b₇ := by
funext tape; cases tape <;> simp [b₆, b₇, headerResultTapes, bits, word, headerFrame]
have hn : n ≤ word.length := by
simp only [word, List.length_append, encodeWords_length, List.sum_cons,
List.sum_nil, List.length_cons, List.length_nil] ; omega
have hm : m ≤ word.length := by
simp only [word, List.length_append, encodeWords_length, List.sum_cons,
List.sum_nil, List.length_cons, List.length_nil] ; omega
let p₀ := MachineCopy.copyInTime Tape.input Tape.archive Tape.scratch
(by decide) (by decide) (by decide) false .copyFirst .copySecond (some .startVertices)
(program headerPlan rowPlan) rfl rfl b₀ (by rfl) (ambient, ()) none
have p₀' : StateTransition.EvalsToInTime (TM2.step (program headerPlan rowPlan))
⟨some .copyFirst, ((ambient, ()), none), b₀⟩
(some ⟨some .startVertices, ((ambient, ()), none), b₁⟩) (2 * (word.length + 1)) := by
have h := p₀
rw [h₀] at h
simpa only [show b₀ .input = word from rfl] using h
let p₁ := Hastad.SourceMachine.fieldInTime Tape.input Tape.vertices (by decide)
.startVertices .readVertices (some .startDarts) (program headerPlan rowPlan)
rfl rfl b₁ n (encodeWord m ++ rowsBits)
(by simp [b₁, headerFrame, word, encodeWords, List.append_assoc]) (ambient, ()) none
have p₁' : StateTransition.EvalsToInTime (TM2.step (program headerPlan rowPlan))
⟨some .startVertices, ((ambient, ()), none), b₁⟩
(some ⟨some .startDarts, ((ambient, ()), none), b₂⟩) (n + 2) := by
simpa only [h₁] using p₁
let p₂ := Hastad.SourceMachine.fieldInTime Tape.input Tape.darts (by decide)
.startDarts .readDarts (some .seedIndex) (program headerPlan rowPlan)
rfl rfl b₂ m rowsBits rfl (ambient, ()) none
have p₂' : StateTransition.EvalsToInTime (TM2.step (program headerPlan rowPlan))
⟨some .startDarts, ((ambient, ()), none), b₂⟩
(some ⟨some .seedIndex, ((ambient, ()), none), b₃⟩) (m + 2) := by
simpa only [h₂] using p₂
let p₃ : StateTransition.EvalsToInTime (TM2.step (program headerPlan rowPlan))
⟨some .seedIndex, ((ambient, ()), none), b₃⟩
(some ⟨some (.header (Emitter.labelAt headerPlan.length 36864 0 .entry)),
((ambient, ()), none), b₄⟩) 1 := {
steps := 1
evals_in_steps := by
simp only [Function.iterate_one]
change TM2.step (program headerPlan rowPlan)
⟨some .seedIndex, ((ambient, ()), none), b₃⟩ = _
have ht : Function.update (Function.update (Function.update b₃ Tape.rowIndex [false])
Tape.tail [false]) Tape.head [false] = b₄ := by
funext tape; cases tape <;> simp [b₃, b₄, headerFrame, encodeWord]
simp only [TM2.step, program, TM2.stepAux]
apply congrArg some
exact congrArg (TM2.Cfg.mk _ _) ht
steps_le_m := Nat.le_refl _ }
let p₄ := Emitter.planInTime (Emitter.listCommands headerPlan) source Tape.scratch
Tape.accumulator source_ne_scratch source_ne_accumulator (by decide)
Label.header (some .headerClearTail) (program headerPlan rowPlan) (fun _ => rfl)
(values n m 0 0 0) b₄
(by intro i; fin_cases i <;> rfl) rfl ambient word.length
(by
intro i
fin_cases i
· exact hn
· exact hm
· exact Nat.zero_le _
· exact Nat.zero_le _
· exact Nat.zero_le _)
have p₄' : StateTransition.EvalsToInTime (TM2.step (program headerPlan rowPlan))
⟨some (.header (Emitter.labelAt headerPlan.length 36864 0 .entry)),
((ambient, ()), none), b₄⟩
(some ⟨some .headerClearTail, ((ambient, ()), none), b₅⟩)
(7 * (3 * (word.length + 1) + 3) + 1) := by
have h := p₄
rw [h₄] at h
simpa only [headerPlan_length] using h
let p₅ := MachineLookup.discardInTime Tape.tail .headerClearTail .headerClearHead
(program headerPlan rowPlan) rfl b₅ 0 [] (by simp [b₅, headerFrame]) (ambient, ()) none
have p₅' : StateTransition.EvalsToInTime (TM2.step (program headerPlan rowPlan))
⟨some .headerClearTail, ((ambient, ()), none), b₅⟩
(some ⟨some .headerClearHead, ((ambient, ()), none), b₆⟩) 1 := by
simpa only [h₅] using p₅
let p₆ := MachineLookup.discardInTime Tape.head .headerClearHead .guard
(program headerPlan rowPlan) rfl b₆ 0 [] (by simp [b₆, headerFrame]) (ambient, ()) none
have p₆' : StateTransition.EvalsToInTime (TM2.step (program headerPlan rowPlan))
⟨some .headerClearHead, ((ambient, ()), none), b₆⟩
(some ⟨some .guard, ((ambient, ()), none), b₇⟩) 1 := by
simpa only [h₆] using p₆
let p₀₁ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ p₀' p₁'
let p₀₁₂ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ p₀₁ p₂'
let p₀₁₂₃ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ p₀₁₂ p₃
let p₀₁₂₃₄ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ p₀₁₂₃ p₄'
let p₀₁₂₃₄₅ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ p₀₁₂₃₄ p₅'
let p := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ p₀₁₂₃₄₅ p₆'
exact {
toEvalsTo := p.toEvalsTo
steps_le_m := by
have h := p.steps_le_m
change _ ≤ headerTimePolynomial.eval word.length
rw [headerTimePolynomial_eval]
omega }
def tableRowsBits (table : PCP.GenericGraphTables.Table 64) : List Bool :=
encodeWords ((PCP.GenericGraphTables.rowList table).flatMap PCP.GenericGraphTables.rowWords)
theorem tableBits_header_rows (table : PCP.GenericGraphTables.Table 64) :
PCP.GenericGraphTables.tableBits table =
encodeWords [table.vertices, table.darts] ++ tableRowsBits table := by
simp only [PCP.GenericGraphTables.tableBits, PCP.GenericGraphTables.tableWords,
encodeWords_append, tableRowsBits]
def tableHeaderInTime (rowPlan : Plan) (table : PCP.GenericGraphTables.Table 64)
(ambient : Ambient) :
StateTransition.EvalsToInTime (TM2.step (program headerPlan rowPlan))
⟨some .copyFirst, ((ambient, ()), none),
headerInputTapes (PCP.GenericGraphTables.tableBits table)⟩
(some ⟨some .guard, ((ambient, ()), none),
headerResultTapes table.vertices table.darts (tableRowsBits table)⟩)
(headerTimePolynomial.eval (PCP.GenericGraphTables.tableBits table).length) := by
rw [tableBits_header_rows]
exact headerInTime rowPlan table.vertices table.darts (tableRowsBits table) ambient
theorem headerInput_configuration (rowPlan : Plan) (input : List Bool) :
initList (machine headerPlan rowPlan) input =
⟨some .copyFirst, ((((), fun _ => false), ()), none), headerInputTapes input⟩ := by
have ht : (initList (machine headerPlan rowPlan) input).stk = headerInputTapes input := by
funext tape
cases tape <;> simp [initList, machine, headerInputTapes, headerFrame]
rfl
exact congrArg (TM2.Cfg.mk _ _) ht
def initializedTableHeaderInTime (rowPlan : Plan) (table : PCP.GenericGraphTables.Table 64) :
StateTransition.EvalsToInTime (machine headerPlan rowPlan).step
(initList (machine headerPlan rowPlan) (PCP.GenericGraphTables.tableBits table))
(some ⟨some .guard, ((((), fun _ => false), ()), none),
headerResultTapes table.vertices table.darts (tableRowsBits table)⟩)
(headerTimePolynomial.eval (PCP.GenericGraphTables.tableBits table).length) := by
rw [headerInput_configuration]
exact tableHeaderInTime rowPlan table ((), fun _ => false)
end IndependentSetsGames.Foundations.Complexity.FinalCNFMachine.Program
end OAI