cfahlgren1's picture
download
raw
37.8 kB
import OAI.Computability.BinPacking.PCP.FinalBooleanVerifier
import OAI.Computability.BinPacking.PCP.Lookup
namespace OAI
namespace BinPackingGames.Foundations.Complexity.FinalCNFMachine
open Turing
open PCP
abbrev Buffer (N : Nat) := MachineFixedBlockMap.Buffer N
section RelationReader
variable {K Λ σ : Type} {N : Nat}
def readUnarySlots (source : K) : List (Fin N) →
TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N) →
TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N)
| [], next => next
| i :: slots, next =>
.pop source (fun state head =>
(state.1, Function.update state.2 i (head.getD false)))
(.branch (fun state => state.2 i)
(.pop source (fun state _ => state) (readUnarySlots source slots next))
(readUnarySlots source slots next))
def unaryBits (slots : List (Fin N)) (bits : Buffer N) : List Bool :=
slots.flatMap fun i => encodeWord (GraphTables.bitWord (bits i))
theorem unaryBits_eq_encodeWords (slots : List (Fin N)) (bits : Buffer N) :
unaryBits slots bits = encodeWords (slots.map fun i => GraphTables.bitWord (bits i)) := by
induction slots with
| nil => rfl
| cons i slots ih => simpa only [unaryBits, List.flatMap_cons, List.map_cons,
encodeWords] using congrArg (encodeWord (GraphTables.bitWord (bits i)) ++ ·) ih
theorem unaryBits_length_le (slots : List (Fin N)) (bits : Buffer N) :
(unaryBits slots bits).length ≤ 2 * slots.length := by
induction slots with
| nil => simp [unaryBits]
| cons i slots ih =>
have hi : (encodeWord (GraphTables.bitWord (bits i))).length ≤ 2 := by
cases bits i <;> simp [GraphTables.bitWord, encodeWord_length]
simp only [unaryBits, List.flatMap_cons, List.length_append, List.length_cons] at *
omega
theorem statementPushBound_readUnarySlots (source : K) (slots : List (Fin N))
(next : TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N)) :
Runtime.statementPushBound (readUnarySlots source slots next) =
Runtime.statementPushBound next := by
induction slots with
| nil => rfl
| cons i slots ih =>
simp only [readUnarySlots, Runtime.statementPushBound, ih, max_self]
variable [DecidableEq K]
theorem stepAux_readUnarySlots (source : K) (slots : List (Fin N))
(next : TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N))
(ambient : σ) (bits buffer : Buffer N) (tapes : K → List Bool)
(suffix : List Bool) :
TM2.stepAux (readUnarySlots source slots next) (ambient, buffer)
(Function.update tapes source (unaryBits slots bits ++ suffix)) =
TM2.stepAux next (ambient, MachineFixedBlockMap.fill slots bits buffer)
(Function.update tapes source suffix) := by
induction slots generalizing buffer tapes with
| nil => simp [readUnarySlots, unaryBits, MachineFixedBlockMap.fill]
| cons i slots ih =>
cases hi : bits i <;>
simp only [readUnarySlots, unaryBits, List.flatMap_cons, hi,
GraphTables.bitWord, Bool.false_eq_true, ite_false, ite_true, encodeWord,
List.replicate_zero, List.replicate_succ, List.nil_append,
List.cons_append,
TM2.stepAux, Function.update_self, List.head?_cons, Option.getD_some,
List.tail_cons, Function.update_idem, MachineFixedBlockMap.fill,
List.foldl_cons]
· simpa [unaryBits, MachineFixedBlockMap.fill, hi, encodeWord, GraphTables.bitWord] using
ih (Function.update buffer i false) tapes
· simpa [unaryBits, MachineFixedBlockMap.fill, hi, encodeWord, GraphTables.bitWord] using
ih (Function.update buffer i true) tapes
theorem stepAux_readUnaryAll (source : K)
(next : TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N))
(state : σ × Buffer N) (bits : Buffer N) (tapes : K → List Bool)
(suffix : List Bool)
(hinput : tapes source = unaryBits (List.ofFn id) bits ++ suffix) :
TM2.stepAux (readUnarySlots source (List.ofFn id) next) state tapes =
TM2.stepAux next (state.1, bits) (Function.update tapes source suffix) := by
have h := stepAux_readUnarySlots source (List.ofFn id) next state.1 bits state.2 tapes suffix
have hin : Function.update tapes source (unaryBits (List.ofFn id) bits ++ suffix) = tapes := by
simpa only [← hinput] using Function.update_eq_self source tapes
rw [hin, MachineFixedBlockMap.fill_all] at h
exact h
def unaryBlockMapAt {M : Nat} (source destination : K) (F : Buffer N → Buffer M)
(exit : Option Λ) : TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N) :=
readUnarySlots source (List.ofFn id)
(MachineFixedBlockMap.writeSlots destination (fun state => F state.2)
(List.ofFn id).reverse
(.load (fun state => (state.1, MachineFixedBlockMap.emptyBuffer N))
(MachineFixedBlockMap.finishAt exit)))
theorem stepAux_unaryBlockMapAt {M : Nat} (source destination : K)
(F : Buffer N → Buffer M) (exit : Option Λ) (hne : source ≠ destination)
(state : σ × Buffer N) (bits : Buffer N) (tapes : K → List Bool) (suffix : List Bool)
(hinput : tapes source = unaryBits (List.ofFn id) bits ++ suffix) :
TM2.stepAux (unaryBlockMapAt source destination F exit) state tapes =
{ l := exit, var := (state.1, MachineFixedBlockMap.emptyBuffer N),
stk := Function.update (Function.update tapes source suffix) destination
(List.ofFn (F bits) ++ tapes destination) } := by
unfold unaryBlockMapAt
rw [stepAux_readUnaryAll source _ state bits tapes suffix hinput,
MachineFixedBlockMap.stepAux_writeSlots]
simp only [List.map_reverse, List.map_ofFn, Function.comp_id, List.reverse_reverse,
Function.update_of_ne (Ne.symm hne), TM2.stepAux]
cases exit <;> rfl
theorem step_unaryBlockMapAt {M : Nat} (source destination : K)
(F : Buffer N → Buffer M) (exit : Option Λ) (hne : source ≠ destination)
(program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N))
(label : Λ) (atLabel : program label = unaryBlockMapAt source destination F exit)
(state : σ × Buffer N) (bits : Buffer N) (tapes : K → List Bool) (suffix : List Bool)
(hinput : tapes source = unaryBits (List.ofFn id) bits ++ suffix) :
TM2.step program ⟨some label, state, tapes⟩ =
some ⟨exit, (state.1, MachineFixedBlockMap.emptyBuffer N),
Function.update (Function.update tapes source suffix) destination
(List.ofFn (F bits) ++ tapes destination)⟩ := by
simp only [TM2.step, atLabel,
stepAux_unaryBlockMapAt source destination F exit hne state bits tapes suffix hinput]
def unaryBlockInTime {M : Nat} (source destination : K)
(F : Buffer N → Buffer M) (exit : Option Λ) (hne : source ≠ destination)
(program : Λ → TM2.Stmt (fun _ : K => Bool) Λ (σ × Buffer N))
(label : Λ) (atLabel : program label = unaryBlockMapAt source destination F exit)
(state : σ × Buffer N) (bits : Buffer N) (tapes : K → List Bool) (suffix : List Bool)
(hinput : tapes source = unaryBits (List.ofFn id) bits ++ suffix) :
StateTransition.EvalsToInTime (TM2.step program) ⟨some label, state, tapes⟩
(some ⟨exit, (state.1, MachineFixedBlockMap.emptyBuffer N),
Function.update (Function.update tapes source suffix) destination
(List.ofFn (F bits) ++ tapes destination)⟩) 1 where
steps := 1
evals_in_steps := step_unaryBlockMapAt source destination F exit hne program label
atLabel state bits tapes suffix hinput
steps_le_m := Nat.le_refl _
end RelationReader
theorem unaryBits_relation (relation : GraphTables.RelationTable) :
unaryBits (List.ofFn id) (fun i => relation[i]) =
encodeWords (GraphTables.relationWords relation) := by
have h : List.ofFn (fun i : Fin 4096 => relation[i]) = relation.toList := by
change List.ofFn (fun i : Fin 4096 => relation[i.val]) = relation.toList
rw [← Vector.toList_ofFn, Vector.ofFn_getElem]
rw [unaryBits_eq_encodeWords]
unfold GraphTables.relationWords
rw [← h]
simp only [List.map_ofFn, Function.comp_def, id_eq]
def readerStateEquiv (σ : Type) (N : Nat) :
(((σ × Unit) × Option Bool) × Buffer N) ≃ (((σ × Buffer N) × Unit) × Option Bool) where
toFun state := (((state.1.1.1, state.2), state.1.1.2), state.1.2)
invFun state := (((state.1.1.1, state.1.2), state.2), state.1.1.2)
left_inv _ := rfl
right_inv _ := rfl
section AmbientRelation
variable {K Λ σ : Type} [DecidableEq K]
abbrev State (σ : Type) := ((σ × Buffer 4096) × Unit) × Option Bool
def readRelationAt (source : K) (exit : Λ) :
TM2.Stmt (fun _ : K => Bool) Λ (State σ) :=
MachineControl.statement id (readerStateEquiv σ 4096)
(readUnarySlots source (List.ofFn id) (.goto fun _ => exit))
theorem stepAux_readRelationAt (source : K) (exit : Λ) (ambient : σ)
(buffer : Buffer 4096) (unitState : Unit) (register : Option Bool)
(relation : GraphTables.RelationTable) (tapes : K → List Bool) (suffix : List Bool)
(hinput : tapes source = encodeWords (GraphTables.relationWords relation) ++ suffix) :
TM2.stepAux (readRelationAt source exit) (((ambient, buffer), unitState), register) tapes =
⟨some exit, (((ambient, fun i => relation[i]), unitState), register),
Function.update tapes source suffix⟩ := by
change TM2.stepAux (MachineControl.statement id (readerStateEquiv σ 4096)
(readUnarySlots source (List.ofFn id) (.goto fun _ => exit)))
((readerStateEquiv σ 4096) (((ambient, unitState), register), buffer)) tapes = _
rw [MachineControl.stepAux_simulation]
rw [stepAux_readUnaryAll source _ _ (fun i => relation[i]) tapes suffix
(by simpa only [unaryBits_relation] using hinput)]
rfl
end AmbientRelation
namespace Program
open PCP.AlphabetTable
inductive Tape
| input | output | accumulator | scratch | vertices | darts | tail | head | rowIndex
| archive | reverseIndex | scanWork | indexWork
deriving DecidableEq
protected abbrev Tape.enumList : List Tape := [.input, .output, .accumulator, .scratch, .vertices,
.darts, .tail, .head, .rowIndex, .archive, .reverseIndex, .scanWork, .indexWork]
protected theorem Tape.enumList_getElem?_ctorIdx_eq (x : Tape) :
Tape.enumList[x.ctorIdx]? = some x := by
cases x <;> rfl
protected theorem Tape.enumList_nodup : Tape.enumList.Nodup := by decide
instance : Fintype Tape where
elems := ⟨Tape.enumList, Tape.enumList_nodup⟩
complete x := by cases x <;> decide
abbrev Ambient := Unit × Buffer 4096
abbrev Plan := List (Emitter.Command 5 Ambient 36864)
def values (vertices darts tail head row : Nat) : Fin 5 → Nat :=
Fin.cases vertices (Fin.cases darts (Fin.cases tail (Fin.cases head (fun _ => row))))
def headerPlan : Plan :=
Emitter.affineCommands [(0, 6), (1, 36864)] (fun _ => 0) ++
Emitter.affineCommands [(1, 40960)] (fun _ => 0)
theorem headerPlan_length : headerPlan.length = 7 := by
simp [headerPlan]
theorem headerPlan_bits (vertices darts tail head row : Nat) (ambient : Ambient) :
headerPlan.flatMap (Emitter.commandBits (values vertices darts tail head row) ambient) =
encodeWords [6 * vertices + 36864 * darts, 40960 * darts] := by
rw [headerPlan, List.flatMap_append, Emitter.affineCommands_bits,
Emitter.affineCommands_bits]
simp [Emitter.affineValue, values, encodeWords]
rfl
def source : Fin 5 → Tape :=
Fin.cases .vertices (Fin.cases .darts (Fin.cases .tail (Fin.cases .head (fun _ => .rowIndex))))
inductive Label (headerCount rowCount : Nat)
| copyFirst | copySecond | startVertices | readVertices | startDarts | readDarts | seedIndex
| header (label : Emitter.Label headerCount 36864)
| headerClearTail | headerClearHead
| guard | startTail | readTail | startReverse | readReverse
| headTableFirst | headTableSecond | headIndexFirst | headIndexSecond
| headHeaderVertices | headHeaderDarts | headLookup (label : Lookup.Label)
| readRelation
| row (label : Emitter.Label rowCount 36864)
| clearTail | clearHead | clearReverse | nextRow | reverseOutput
deriving DecidableEq, Fintype
def guard {hc rc : Nat} (again finish : Label hc rc) :
TM2.Stmt (fun _ : Tape => Bool) (Label hc rc) (State Unit) :=
.peek .input (fun state head => (state.1, head))
(.branch (fun state => state.2.isSome)
(.load (fun state => (state.1, none)) (.goto fun _ => again))
(.load (fun state => (state.1, none)) (.goto fun _ => finish)))
def program (headerPlan rowPlan : Plan) :
Label headerPlan.length rowPlan.length →
TM2.Stmt (fun _ : Tape => Bool) (Label headerPlan.length rowPlan.length) (State Unit)
| .copyFirst => Reduction.MachineTransfer.loopAt .input .scratch id false .copyFirst
(some .copySecond)
| .copySecond => MachineCopy.forkLoop .scratch .input .archive false .copySecond
(some .startVertices)
| .startVertices => Hastad.SourceMachine.fieldStart .vertices .readVertices
| .readVertices => Hastad.SourceMachine.fieldLoop .input .vertices .readVertices (some .startDarts)
| .startDarts => Hastad.SourceMachine.fieldStart .darts .readDarts
| .readDarts => Hastad.SourceMachine.fieldLoop .input .darts .readDarts (some .seedIndex)
| .seedIndex => .push .rowIndex (fun _ => false)
(.push .tail (fun _ => false) (.push .head (fun _ => false)
(.goto fun _ => .header (Emitter.labelAt headerPlan.length 36864 0 .entry))))
| .header label => Emitter.statement (Emitter.listCommands headerPlan)
source .scratch .accumulator Label.header (some .headerClearTail) label
| .headerClearTail => MachineLookup.discard .tail .headerClearTail .headerClearHead
| .headerClearHead => MachineLookup.discard .head .headerClearHead .guard
| .guard => guard .startTail .reverseOutput
| .startTail => Hastad.SourceMachine.fieldStart .tail .readTail
| .readTail => Hastad.SourceMachine.fieldLoop .input .tail .readTail (some .startReverse)
| .startReverse => Hastad.SourceMachine.fieldStart .reverseIndex .readReverse
| .readReverse => Hastad.SourceMachine.fieldLoop .input .reverseIndex .readReverse
(some .headTableFirst)
| .headTableFirst => Reduction.MachineTransfer.loopAt .archive .scratch id false
.headTableFirst (some .headTableSecond)
| .headTableSecond => MachineCopy.forkLoop .scratch .archive .scanWork false
.headTableSecond (some .headIndexFirst)
| .headIndexFirst => Reduction.MachineTransfer.loopAt .reverseIndex .scratch id false
.headIndexFirst (some .headIndexSecond)
| .headIndexSecond => MachineCopy.forkLoop .scratch .reverseIndex .indexWork false
.headIndexSecond (some .headHeaderVertices)
| .headHeaderVertices => MachineLookup.discard .scanWork .headHeaderVertices .headHeaderDarts
| .headHeaderDarts => MachineLookup.discard .scanWork .headHeaderDarts (.headLookup .guard)
| .headLookup label => Lookup.statement 64 .indexWork .scanWork .head Label.headLookup
(some .readRelation) label
| .readRelation => readRelationAt .input
(.row (Emitter.labelAt rowPlan.length 36864 0 .entry))
| .row label => Emitter.statement (Emitter.listCommands rowPlan)
source .scratch .accumulator Label.row (some .clearTail) label
| .clearTail => MachineLookup.discard .tail .clearTail .clearHead
| .clearHead => MachineLookup.discard .head .clearHead .clearReverse
| .clearReverse => MachineLookup.discard .reverseIndex .clearReverse .nextRow
| .nextRow => .push .rowIndex (fun _ => true) (.goto fun _ => .guard)
| .reverseOutput => Reduction.MachineTransfer.loopAt .accumulator .output id false
.reverseOutput none
def machine (headerPlan rowPlan : Plan) : FinTM2 where
K := Tape
k₀ := .input
k₁ := .output
Γ _ := Bool
Λ := Label headerPlan.length rowPlan.length
main := .copyFirst
σ := State Unit
initialState := ((((), fun _ => false), ()), none)
m := program headerPlan rowPlan
def headRoles : Fin 6 → Tape :=
Fin.cases .archive (Fin.cases .reverseIndex (Fin.cases .scanWork
(Fin.cases .indexWork (Fin.cases .head (fun _ => .scratch)))))
theorem headRoles_injective : Function.Injective headRoles := by
decide
def headPhaseInTime (headerPlan rowPlan : Plan) (base : Tape → List Bool)
(table : GenericGraphTables.Table 64) (e : Fin table.darts)
(hTable : base .archive = GenericGraphTables.tableBits table)
(hReverse : base .reverseIndex = encodeWord (Lookup.headIndex table e).val)
(hHead : base .head = []) (hScratch : base .scratch = []) (ambient : Ambient) :
StateTransition.EvalsToInTime (TM2.step (program headerPlan rowPlan))
⟨some .headTableFirst, ((ambient, ()), none), base⟩
(some ⟨some .readRelation, ((ambient, ()), none),
Lookup.headLookupTapes headRoles base table e⟩)
(Lookup.headTimePolynomial.eval (GenericGraphTables.tableBits table).length) :=
Lookup.headLookupInTime headRoles headRoles_injective
.headTableFirst .headTableSecond .headIndexFirst .headIndexSecond
.headHeaderVertices .headHeaderDarts Label.headLookup (some .readRelation)
(program headerPlan rowPlan) rfl rfl rfl rfl rfl rfl (fun _ => rfl)
base table e hTable hReverse hHead hScratch (ambient, ()) none
def workingTapes (vertices darts row : Nat) (input tail head accumulator : List Bool) :
Tape → List Bool
| .input => input
| .output => []
| .accumulator => accumulator
| .scratch => []
| .vertices => encodeWord vertices
| .darts => encodeWord darts
| .tail => tail
| .head => head
| .rowIndex => encodeWord row
| .archive | .reverseIndex | .scanWork | .indexWork => []
theorem workingTapes_operands (vertices darts row tail head : Nat)
(input accumulator : List Bool) :
∀ i, workingTapes vertices darts row input (encodeWord tail) (encodeWord head)
accumulator (source i) = encodeWord (values vertices darts tail head row i) := by
intro i
fin_cases i <;> rfl
theorem update_working_input (vertices darts row : Nat)
(input tail head accumulator replacement : List Bool) :
Function.update (workingTapes vertices darts row input tail head accumulator) Tape.input
replacement = workingTapes vertices darts row replacement tail head accumulator := by
funext tape
cases tape <;> simp [workingTapes, Function.update]
theorem update_working_tail (vertices darts row : Nat)
(input tail head accumulator replacement : List Bool) :
Function.update (workingTapes vertices darts row input tail head accumulator) Tape.tail
replacement = workingTapes vertices darts row input replacement head accumulator := by
funext tape
cases tape <;> simp [workingTapes, Function.update]
theorem update_working_head (vertices darts row : Nat)
(input tail head accumulator replacement : List Bool) :
Function.update (workingTapes vertices darts row input tail head accumulator) Tape.head
replacement = workingTapes vertices darts row input tail replacement accumulator := by
funext tape
cases tape <;> simp [workingTapes, Function.update]
theorem update_working_accumulator (vertices darts row : Nat)
(input tail head accumulator replacement : List Bool) :
Function.update (workingTapes vertices darts row input tail head accumulator) Tape.accumulator
replacement = workingTapes vertices darts row input tail head replacement := by
funext tape
cases tape <;> simp [workingTapes, Function.update]
theorem update_working_rowIndex (vertices darts row : Nat)
(input tail head accumulator : List Bool) :
Function.update (workingTapes vertices darts row input tail head accumulator) Tape.rowIndex
(true :: encodeWord row) =
workingTapes vertices darts (row + 1) input tail head accumulator := by
funext tape
cases tape <;> simp [workingTapes, Function.update, encodeWord, List.replicate_succ]
theorem source_ne_scratch (i : Fin 5) : source i ≠ Tape.scratch := by
fin_cases i <;> decide
theorem source_ne_accumulator (i : Fin 5) : source i ≠ Tape.accumulator := by
fin_cases i <;> decide
end Program
end BinPackingGames.Foundations.Complexity.FinalCNFMachine
namespace BinPackingGames.Foundations.Complexity.FinalCNFMachine.Program
section
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
open Turing PCP PCP.AlphabetTable
def endpointTapes (base : Tape → List Bool) (tail reverse : Nat) (rest : List Bool) :=
Hastad.SourceMachine.fieldTapes Tape.input Tape.reverseIndex
(Hastad.SourceMachine.fieldTapes Tape.input Tape.tail base
(encodeWord reverse ++ rest) (encodeWord tail ++ base .tail))
rest (encodeWord reverse ++ base .reverseIndex)
def endpointsInTime (headerPlan plan : Plan) (base : Tape → List Bool)
(tail reverse : Nat) (rest : List Bool)
(hinput : base .input = encodeWord tail ++ encodeWord reverse ++ rest)
(ambient : Ambient) :
StateTransition.EvalsToInTime (TM2.step (program headerPlan plan))
⟨some .startTail, ((ambient, ()), none), base⟩
(some ⟨some .headTableFirst, ((ambient, ()), none),
endpointTapes base tail reverse rest⟩) (tail + reverse + 4) := by
let first := Hastad.SourceMachine.fieldTapes Tape.input Tape.tail base
(encodeWord reverse ++ rest) (encodeWord tail ++ base .tail)
let run₁ := Hastad.SourceMachine.fieldInTime Tape.input Tape.tail (by decide)
.startTail .readTail (some .startReverse) (program headerPlan plan) rfl rfl
base tail (encodeWord reverse ++ rest) (by simpa only [List.append_assoc] using hinput)
(ambient, ()) none
have input₁ : first .input = encodeWord reverse ++ rest := by
simp [first, Hastad.SourceMachine.fieldTapes]
have reverse₁ : first .reverseIndex = base .reverseIndex := by
simp [first, Hastad.SourceMachine.fieldTapes]
let run₂ := Hastad.SourceMachine.fieldInTime Tape.input Tape.reverseIndex (by decide)
.startReverse .readReverse (some .headTableFirst) (program headerPlan plan) rfl rfl
first reverse rest input₁ (ambient, ()) none
have joined := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ run₁ run₂
simpa only [first, reverse₁, endpointTapes, Nat.add_assoc,
Nat.add_left_comm, Nat.add_comm] using joined
def relationInTime (headerPlan plan : Plan) (base : Tape → List Bool)
(relation : GraphTables.RelationTable) (rest : List Bool)
(hinput : base .input = encodeWords (GraphTables.relationWords relation) ++ rest)
(ambient : Ambient) :
StateTransition.EvalsToInTime (TM2.step (program headerPlan plan))
⟨some .readRelation, ((ambient, ()), none), base⟩
(some ⟨some (.row (Emitter.labelAt plan.length 36864 0 .entry)),
((((), fun i => relation[i]), ()), none), Function.update base .input rest⟩) 1 where
steps := 1
evals_in_steps := by
change some (TM2.stepAux (program headerPlan plan .readRelation) _ _) = _
rw [program, stepAux_readRelationAt _ _ _ _ _ _ _ _ _ hinput]
steps_le_m := Nat.le_refl _
def emitInTime (headerPlan plan : Plan) (base : Tape → List Bool)
(operands : Fin 5 → Nat) (hoperands : ∀ i, base (source i) = encodeWord (operands i))
(hscratch : base .scratch = []) (ambient : Ambient)
(N : Nat) (hbounded : ∀ i, operands i ≤ N) :
StateTransition.EvalsToInTime (TM2.step (program headerPlan plan))
⟨some (.row (Emitter.labelAt plan.length 36864 0 .entry)),
((ambient, ()), none), base⟩
(some ⟨some .clearTail, ((ambient, ()), none),
Function.update base .accumulator
((plan.flatMap (Emitter.commandBits operands ambient)).reverse ++ base .accumulator)⟩)
(plan.length * (3 * (N + 1) + 3) + 1) := by
have run := Emitter.planInTime (Emitter.listCommands plan)
source Tape.scratch Tape.accumulator source_ne_scratch source_ne_accumulator (by decide)
Label.row (some .clearTail) (program headerPlan plan) (fun _ => rfl)
operands base hoperands hscratch ambient N hbounded
simpa only [Emitter.bits_listCommands, Emitter.resultTapes] using run
def finishTapes (base : Tape → List Bool) (row : Nat) : Tape → List Bool :=
Function.update
(Function.update (Function.update (Function.update base Tape.tail []) Tape.head [])
Tape.reverseIndex []) Tape.rowIndex (encodeWord (row + 1))
def finishInTime (headerPlan plan : Plan) (base : Tape → List Bool)
(tail head reverse row : Nat)
(htail : base .tail = encodeWord tail) (hhead : base .head = encodeWord head)
(hreverse : base .reverseIndex = encodeWord reverse)
(hrow : base .rowIndex = encodeWord row) (ambient : Ambient) :
StateTransition.EvalsToInTime (TM2.step (program headerPlan plan))
⟨some .clearTail, ((ambient, ()), none), base⟩
(some ⟨some .guard, ((ambient, ()), none), finishTapes base row⟩)
(tail + head + reverse + 4) := by
let t₁ := Function.update base Tape.tail []
let t₂ := Function.update t₁ Tape.head []
let t₃ := Function.update t₂ Tape.reverseIndex []
let run₁ := MachineLookup.discardInTime Tape.tail .clearTail .clearHead
(program headerPlan plan) rfl base tail [] (by simpa using htail) (ambient, ()) none
have hhead₁ : t₁ .head = encodeWord head := by simp [t₁, hhead]
let run₂ := MachineLookup.discardInTime Tape.head .clearHead .clearReverse
(program headerPlan plan) rfl t₁ head [] (by simpa using hhead₁) (ambient, ()) none
have hreverse₂ : t₂ .reverseIndex = encodeWord reverse := by simp [t₂, t₁, hreverse]
let run₃ := MachineLookup.discardInTime Tape.reverseIndex .clearReverse .nextRow
(program headerPlan plan) rfl t₂ reverse [] (by simpa using hreverse₂) (ambient, ()) none
let run₄ : StateTransition.EvalsToInTime (TM2.step (program headerPlan plan))
⟨some .nextRow, ((ambient, ()), none), t₃⟩
(some ⟨some .guard, ((ambient, ()), none), finishTapes base row⟩) 1 := {
steps := 1
evals_in_steps := by
change some (TM2.stepAux (program headerPlan plan .nextRow) _ _) = _
simp only [program, TM2.stepAux]
congr 2
simp [t₃, t₂, t₁, finishTapes, hrow, encodeWord, List.replicate_succ]
steps_le_m := Nat.le_refl _ }
let r₁₂ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ run₁ run₂
let r₁₂₃ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ r₁₂ run₃
have run := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ r₁₂₃ run₄
simpa only [Nat.add_assoc, Nat.add_left_comm, Nat.add_comm] using run
end BinPackingGames.Foundations.Complexity.FinalCNFMachine.Program
end OAI

Xet Storage Details

Size:
37.8 kB
·
Xet hash:
88ae271e6f08a478291a084deaa0aa7c78a5765b121fb06ea21ae24e16b3bf27

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