roseking's picture
Add lean source and binary files (part 62)
2f887db verified
Raw History Blame Contribute Delete
38.3 kB
import Mathlib
import OAI.Computability.MinUncut.Machines.MachineLookup
import OAI.Computability.MinUncut.Machines.Addresses
namespace OAI
namespace MinUncutGames.Foundations.PCP.AlphabetTable.Driver
open Turing
open MinUncutGames.Foundations.Complexity
open MinUncutGames.Foundations.Hastad
open RuntimeModel
def headerPlan (q : Nat) := EmitRows.headerCommands q (Ambient q)
def rowPlan (q : Nat) := EmitRows.blockCommandsIn q (@context q)
inductive Label (q : Nat)
| setup (label : Setup.Label)
| header (label : Emitter.Label (headerPlan q).length (Addresses.fieldBound q))
| guard | tailStart | tailLoop | reverseStart | reverseLoop | readPredicate
| headCopyFirst | headCopySecond | indexCopyFirst | indexCopySecond
| headVertices | headDarts | headLookup (label : Lookup.Label)
| compare (label : CompareLoop.Label)
| emit (label : Emitter.Label (rowPlan q).length (Addresses.fieldBound q))
| clearField (slot : Fin 3)
| increment | reverseOutput | cleanup (slot : Fin 16)
deriving DecidableEq, Fintype
def headerEntry (q : Nat) : Label q :=
.header (Emitter.labelAt (headerPlan q).length (Addresses.fieldBound q) 0 .entry)
def rowEntry (q : Nat) : Label q :=
.emit (Emitter.labelAt (rowPlan q).length (Addresses.fieldBound q) 0 .entry)
def clearNext {q : Nat} (slot : Fin 3) : Label q :=
if h : slot.val < 2 then .clearField ⟨slot.val + 1, by omega⟩ else .increment
def program (q : Nat) : Label q → TM2.Stmt Alphabet (Label q) (State q)
| .setup label => Setup.statement setupPorts Label.setup (some (headerEntry q)) label
| .header label => emitterStatement
(Emitter.statement (Emitter.listCommands (headerPlan q)) headerSources .scratch .reversed
Label.header (some .guard) label)
| .guard => MachineUnaryCounter.guard .counter .tailStart .reverseOutput
| .tailStart => SourceMachine.fieldStart .tail .tailLoop
| .tailLoop => SourceMachine.fieldLoop .cursor .tail .tailLoop (some .reverseStart)
| .reverseStart => SourceMachine.fieldStart .reverseIndex .reverseLoop
| .reverseLoop => SourceMachine.fieldLoop .cursor .reverseIndex .reverseLoop (some .readPredicate)
| .readPredicate => ReadRelation.parser .cursor .headCopyFirst
| .headCopyFirst => Reduction.MachineTransfer.loopAt .original .scratch id false
.headCopyFirst (some .headCopySecond)
| .headCopySecond => MachineCopy.forkLoop .scratch .original .scan false
.headCopySecond (some .indexCopyFirst)
| .indexCopyFirst => Reduction.MachineTransfer.loopAt .reverseIndex .scratch id false
.indexCopyFirst (some .indexCopySecond)
| .indexCopySecond => MachineCopy.forkLoop .scratch .reverseIndex .indexScan false
.indexCopySecond (some .headVertices)
| .headVertices => MachineLookup.discard .scan .headVertices .headDarts
| .headDarts => MachineLookup.discard .scan .headDarts (.headLookup .guard)
| .headLookup label => Lookup.statement q .indexScan .scan .head Label.headLookup
(some (.compare .leftFirst)) label
| .compare label => CompareLoop.statement Label.compare (rowEntry q) label
| .emit label => emitterStatement
(Emitter.statement (Emitter.listCommands (rowPlan q)) rowSources .scratch .reversed
Label.emit (some (.clearField 0)) label)
| .clearField slot => MachineDrain.drain (fieldPorts slot) (.clearField slot)
(some (clearNext slot))
| .increment => nextRow .guard
| .reverseOutput => Reduction.MachineTransfer.loopAt .reversed .output id false
.reverseOutput (some (.cleanup 0))
| .cleanup slot => Cleanup.instruction cleanupPorts Label.cleanup (initial q).1 none slot
def machine (q : Nat) : FinTM2 := by
letI := ReadRelation.stateFintype Flags q
exact {
K := Tape
k₀ := .original
k₁ := .output
Γ := Alphabet
Λ := Label q
main := .setup .copyFirst
σ := State q
initialState := initial q
m := program q }
@[simp] theorem program_setup (q : Nat) (label : Setup.Label) :
program q (.setup label) =
Setup.statement setupPorts Label.setup (some (headerEntry q)) label := rfl
@[simp] theorem program_guard (q : Nat) : program q .guard =
MachineUnaryCounter.guard .counter .tailStart .reverseOutput := rfl
@[simp] theorem program_headLookup (q : Nat) (label : Lookup.Label) :
program q (.headLookup label) = Lookup.statement q .indexScan .scan .head
Label.headLookup (some (.compare .leftFirst)) label := rfl
@[simp] theorem program_compare (q : Nat) (label : CompareLoop.Label) :
program q (.compare label) = CompareLoop.statement Label.compare (rowEntry q) label := rfl
@[simp] theorem program_increment (q : Nat) : program q .increment = nextRow .guard := rfl
end MinUncutGames.Foundations.PCP.AlphabetTable.Driver
namespace MinUncutGames.Foundations.PCP.AlphabetTable.LoopState
open MinUncutGames.Foundations.Complexity
open RuntimeModel
variable {q : Nat}
def inputBits (input : GenericGraphTables.Table q) : List Bool :=
GenericGraphTables.tableBits input
def prefixWords (input : GenericGraphTables.Table q) (e : Nat) : List Nat :=
[Enumeration.vertexCount input.vertices input.darts q,
Enumeration.dartCount input.darts q] ++
((List.finRange input.darts).take e).flatMap (EmitRows.blockWords input)
def prefixBits (input : GenericGraphTables.Table q) (e : Nat) : List Bool :=
encodeWords (prefixWords input e)
def cursorWords (input : GenericGraphTables.Table q) (e : Nat) : List Nat :=
((GenericGraphTables.rowList input).drop e).flatMap GenericGraphTables.rowWords
def cursorBits (input : GenericGraphTables.Table q) (e : Nat) : List Bool :=
encodeWords (cursorWords input e)
@[simp] theorem prefixWords_zero (input : GenericGraphTables.Table q) :
prefixWords input 0 =
[Enumeration.vertexCount input.vertices input.darts q,
Enumeration.dartCount input.darts q] := by
simp [prefixWords]
@[simp] theorem prefixBits_zero (input : GenericGraphTables.Table q) :
prefixBits input 0 = encodeWords
[Enumeration.vertexCount input.vertices input.darts q,
Enumeration.dartCount input.darts q] := by
rw [prefixBits, prefixWords_zero]
theorem prefixWords_succ (input : GenericGraphTables.Table q) (e : Nat)
(he : e < input.darts) :
prefixWords input (e + 1) =
prefixWords input e ++ EmitRows.blockWords input ⟨e, he⟩ := by
have bound : e < (List.finRange input.darts).length := by simpa using he
have take := List.take_succ_eq_append_getElem bound
have atIndex : (List.finRange input.darts)[e]'bound = (⟨e, he⟩ : Fin input.darts) := by
simp
rw [atIndex] at take
unfold prefixWords
rw [take, List.flatMap_append]
simp only [List.flatMap_cons, List.flatMap_nil, List.append_nil, List.append_assoc]
theorem prefixBits_succ (input : GenericGraphTables.Table q) (e : Nat)
(he : e < input.darts) :
prefixBits input (e + 1) =
prefixBits input e ++ encodeWords (EmitRows.blockWords input ⟨e, he⟩) := by
rw [prefixBits, prefixWords_succ input e he, encodeWords_append]
rfl
theorem prefixBits_reverse_succ (input : GenericGraphTables.Table q) (e : Nat)
(he : e < input.darts) :
(prefixBits input (e + 1)).reverse =
(encodeWords (EmitRows.blockWords input ⟨e, he⟩)).reverse ++
(prefixBits input e).reverse := by
rw [prefixBits_succ input e he, List.reverse_append]
theorem prefixWords_full (input : GenericGraphTables.Table q) :
prefixWords input input.darts = GraphTables.tableWords (Table.build input) := by
have allRows : (List.finRange input.darts).take input.darts = List.finRange input.darts := by
apply List.take_of_length_le
simp
unfold prefixWords
rw [allRows]
exact (EmitRows.tableWords_eq_blocks input).symm
theorem prefixBits_full (input : GenericGraphTables.Table q) :
prefixBits input input.darts = GraphTables.tableBits (Table.build input) := by
rw [prefixBits, prefixWords_full]
rfl
@[simp] theorem cursorBits_zero (input : GenericGraphTables.Table q) :
cursorBits input 0 =
encodeWords ((GenericGraphTables.rowList input).flatMap GenericGraphTables.rowWords) := by
simp [cursorBits, cursorWords]
theorem cursorWords_succ (input : GenericGraphTables.Table q) (e : Nat)
(he : e < input.darts) :
cursorWords input e =
GenericGraphTables.rowWords input.rows[(⟨e, he⟩ : Fin input.darts)] ++
cursorWords input (e + 1) := by
have bound : e < (GenericGraphTables.rowList input).length := by
simpa only [GenericGraphTables.rowList_length] using he
unfold cursorWords
rw [List.drop_eq_getElem_cons bound, List.flatMap_cons]
simp only [GenericGraphTables.rowList, Vector.getElem_toList, Fin.getElem_fin]
theorem cursorBits_succ (input : GenericGraphTables.Table q) (e : Nat)
(he : e < input.darts) :
cursorBits input e =
encodeWords (GenericGraphTables.rowWords input.rows[(⟨e, he⟩ : Fin input.darts)]) ++
cursorBits input (e + 1) := by
rw [cursorBits, cursorWords_succ input e he, encodeWords_append]
rfl
@[simp] theorem cursorBits_full (input : GenericGraphTables.Table q) :
cursorBits input input.darts = [] := by
have finished := List.drop_length (l := GenericGraphTables.rowList input)
rw [GenericGraphTables.rowList_length] at finished
unfold cursorBits cursorWords
rw [finished]
rfl
structure Ready (input : GenericGraphTables.Table q) (e : Nat)
(base : Tape → List Bool) : Prop where
le_darts : e ≤ input.darts
original : base .original = inputBits input
cursor : base .cursor = cursorBits input e
vertices : base .vertices = encodeWord input.vertices
darts : base .darts = encodeWord input.darts
counter : base .counter = encodeWord (input.darts - e)
edge : base .edge = encodeWord e
tail : base .tail = []
reverseIndex : base .reverseIndex = []
head : base .head = []
scratch : base .scratch = []
compareLeft : base .compareLeft = []
compareRight : base .compareRight = []
output : base .output = []
reversed : base .reversed = (prefixBits input e).reverse
theorem Ready.of_agree {input : GenericGraphTables.Table q} {e : Nat}
{base next : Tape → List Bool} (ready : Ready input e base)
(agree : ∀ tape, tape ≠ .scan → tape ≠ .indexScan → next tape = base tape) :
Ready input e next where
le_darts := ready.le_darts
original := (agree .original (by decide) (by decide)).trans ready.original
cursor := (agree .cursor (by decide) (by decide)).trans ready.cursor
vertices := (agree .vertices (by decide) (by decide)).trans ready.vertices
darts := (agree .darts (by decide) (by decide)).trans ready.darts
counter := (agree .counter (by decide) (by decide)).trans ready.counter
edge := (agree .edge (by decide) (by decide)).trans ready.edge
tail := (agree .tail (by decide) (by decide)).trans ready.tail
reverseIndex := (agree .reverseIndex (by decide) (by decide)).trans ready.reverseIndex
head := (agree .head (by decide) (by decide)).trans ready.head
scratch := (agree .scratch (by decide) (by decide)).trans ready.scratch
compareLeft := (agree .compareLeft (by decide) (by decide)).trans ready.compareLeft
compareRight := (agree .compareRight (by decide) (by decide)).trans ready.compareRight
output := (agree .output (by decide) (by decide)).trans ready.output
reversed := (agree .reversed (by decide) (by decide)).trans ready.reversed
theorem Ready.update_scan {input : GenericGraphTables.Table q} {e : Nat}
{base : Tape → List Bool} (ready : Ready input e base) (word : List Bool) :
Ready input e (Function.update base .scan word) := by
apply ready.of_agree
intro tape notScan _
simp [notScan]
theorem Ready.update_indexScan {input : GenericGraphTables.Table q} {e : Nat}
{base : Tape → List Bool} (ready : Ready input e base) (word : List Bool) :
Ready input e (Function.update base .indexScan word) := by
apply ready.of_agree
intro tape _ notIndexScan
simp [notIndexScan]
theorem Ready.counter_succ {input : GenericGraphTables.Table q} {e : Nat}
{base : Tape → List Bool} (ready : Ready input e base) (he : e < input.darts) :
base .counter = encodeWord (input.darts - (e + 1) + 1) := by
rw [ready.counter]
congr 1
omega
theorem Ready.counter_at_end {input : GenericGraphTables.Table q}
{base : Tape → List Bool} (ready : Ready input input.darts base) :
base .counter = encodeWord 0 := by
simpa only [Nat.sub_self] using ready.counter
theorem Ready.cursor_at_end {input : GenericGraphTables.Table q}
{base : Tape → List Bool} (ready : Ready input input.darts base) :
base .cursor = [] := ready.cursor.trans (cursorBits_full input)
theorem Ready.reversed_at_end {input : GenericGraphTables.Table q}
{base : Tape → List Bool} (ready : Ready input input.darts base) :
base .reversed = (GraphTables.tableBits (Table.build input)).reverse := by
rw [ready.reversed, prefixBits_full]
end MinUncutGames.Foundations.PCP.AlphabetTable.LoopState
namespace MinUncutGames.Foundations.PCP.AlphabetTable.Body
open Turing
open MinUncutGames.Foundations.Complexity
open RuntimeModel
variable {q : Nat}
def bodyTime (q N : Nat) : Nat :=
(32 + 8 * (Enumeration.localCount q * (6 * (2 * 4104)))) * (N + 1)
structure RowStepResult (input : GenericGraphTables.Table q) (e : Fin input.darts)
(base : Tape → List Bool) (oldRelation : GenericGraphTables.RelationTable q)
(oldFlag : Bool) where
tapes : Tape → List Bool
ready : LoopState.Ready input (e.val + 1) tapes
run : StateTransition.EvalsToInTime (TM2.step (Driver.program q))
⟨some .guard, normal oldRelation oldFlag, base⟩
(some ⟨some .guard, CompareLoop.rowState input e, tapes⟩)
(bodyTime q (LoopState.inputBits input).length)
def guardTapes (input : GenericGraphTables.Table q) (e : Fin input.darts)
(base : Tape → List Bool) : Tape → List Bool :=
Function.update base .counter (encodeWord (input.darts - (e.val + 1)))
def parsedTapes (input : GenericGraphTables.Table q) (e : Fin input.darts)
(base : Tape → List Bool) : Tape → List Bool :=
Lookup.readRowTapes .cursor .tail .reverseIndex (guardTapes input e base)
input.rows[e] (LoopState.cursorBits input (e.val + 1))
def workingTapes (input : GenericGraphTables.Table q) (e : Fin input.darts)
(base : Tape → List Bool) : Tape → List Bool :=
Lookup.headLookupTapes headPorts (parsedTapes input e base) input e
def emittedTapes (input : GenericGraphTables.Table q) (e : Fin input.darts)
(base : Tape → List Bool) : Tape → List Bool :=
Emitter.resultTapes .reversed (workingTapes input e base)
(encodeWords (EmitRows.blockWords input e))
def clearFieldsTapes (base : Tape → List Bool) : Tape → List Bool :=
Function.update (Function.update (Function.update base .tail []) .reverseIndex []) .head []
def resultTapes (input : GenericGraphTables.Table q) (e : Fin input.darts)
(base : Tape → List Bool) : Tape → List Bool :=
Function.update (clearFieldsTapes (emittedTapes input e base)) .edge
(encodeWord (e.val + 1))
private theorem workingTapes_other (input : GenericGraphTables.Table q)
(e : Fin input.darts) (base : Tape → List Bool) (other : Tape)
(hcounter : other ≠ .counter) (hcursor : other ≠ .cursor)
(htail : other ≠ .tail) (hreverse : other ≠ .reverseIndex)
(hscan : other ≠ .scan) (hindex : other ≠ .indexScan) (hhead : other ≠ .head) :
workingTapes input e base other = base other := by
simp [workingTapes, Lookup.headLookupTapes, MachineLookup.tapes, headPorts,
parsedTapes, Lookup.readRowTapes, guardTapes, hcounter, hcursor, htail,
hreverse, hscan, hindex, hhead]
private theorem workingTapes_counter (input : GenericGraphTables.Table q)
(e : Fin input.darts) (base : Tape → List Bool) :
workingTapes input e base .counter = encodeWord (input.darts - (e.val + 1)) := by
simp [workingTapes, Lookup.headLookupTapes, MachineLookup.tapes, headPorts,
parsedTapes, Lookup.readRowTapes, guardTapes]
private theorem workingTapes_cursor (input : GenericGraphTables.Table q)
(e : Fin input.darts) (base : Tape → List Bool) :
workingTapes input e base .cursor = LoopState.cursorBits input (e.val + 1) := by
simp [workingTapes, Lookup.headLookupTapes, MachineLookup.tapes, headPorts,
parsedTapes, Lookup.readRowTapes]
private theorem workingTapes_tail (input : GenericGraphTables.Table q)
(e : Fin input.darts) (base : Tape → List Bool) :
workingTapes input e base .tail = encodeWord input.rows[e].tail.val := by
simp [workingTapes, Lookup.headLookupTapes, MachineLookup.tapes, headPorts,
parsedTapes, Lookup.readRowTapes]
private theorem workingTapes_reverse (input : GenericGraphTables.Table q)
(e : Fin input.darts) (base : Tape → List Bool) :
workingTapes input e base .reverseIndex = encodeWord input.rows[e].reverseIndex.val := by
simp [workingTapes, Lookup.headLookupTapes, MachineLookup.tapes, headPorts,
parsedTapes, Lookup.readRowTapes]
private theorem workingTapes_head (input : GenericGraphTables.Table q)
(e : Fin input.darts) (base : Tape → List Bool) :
workingTapes input e base .head =
encodeWord input.rows[input.rows[e].reverseIndex].tail.val := by
simp [workingTapes, Lookup.headLookupTapes, MachineLookup.tapes, headPorts,
Lookup.headValue, Lookup.headIndex]
def clearFieldsInTime (base : Tape → List Bool) (ambient : Ambient q) :
StateTransition.EvalsToInTime (TM2.step (Driver.program q))
⟨some (.clearField 0), (ambient, none), base⟩
(some ⟨some .increment, (ambient, none), clearFieldsTapes base⟩)
((base .tail).length + (base .reverseIndex).length + (base .head).length + 3) := by
let first := MachineDrain.drainInTime Tape.tail (Driver.Label.clearField 0)
(some (.clearField 1)) (Driver.program q) (by rfl) base ambient none
let second := MachineDrain.drainInTime Tape.reverseIndex (Driver.Label.clearField 1)
(some (.clearField 2)) (Driver.program q) (by rfl)
(Function.update base .tail []) ambient none
let third := MachineDrain.drainInTime Tape.head (Driver.Label.clearField 2)
(some .increment) (Driver.program q) (by rfl)
(Function.update (Function.update base .tail []) .reverseIndex []) ambient none
let firstTwo := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ first second
let run := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ firstTwo third
refine { toEvalsTo := run.toEvalsTo, steps_le_m := ?_ }
have h := run.steps_le_m
simp only [Function.update_of_ne (by decide : Tape.reverseIndex ≠ .tail),
Function.update_of_ne (by decide : Tape.head ≠ .reverseIndex),
Function.update_of_ne (by decide : Tape.head ≠ .tail)] at h
omega
def incrementInTime (base : Tape → List Bool) (ambient : Ambient q)
(e : Nat) (he : base .edge = encodeWord e) :
StateTransition.EvalsToInTime (TM2.step (Driver.program q))
⟨some .increment, (ambient, none), base⟩
(some ⟨some .guard, (ambient, none), Function.update base .edge (encodeWord (e + 1))⟩)
1 where
steps := 1
evals_in_steps := by
change some (TM2.stepAux (nextRow (Driver.Label.guard : Driver.Label q))
(ambient, none) base) = _
simpa only [List.append_nil] using congrArg some
(stepAux_nextRow (Driver.Label.guard : Driver.Label q) (ambient, none) base e []
(by simpa only [List.append_nil] using he))
steps_le_m := Nat.le_refl _
private theorem emitterView_at (label :
Emitter.Label (Driver.rowPlan q).length (Addresses.fieldBound q)) :
(MachineControl.program (Equiv.refl (Driver.Label q)) (emitterEquiv q).symm
(Driver.program q)) (.emit label) =
Emitter.statement (Emitter.listCommands (Driver.rowPlan q)) rowSources
Tape.scratch Tape.reversed Driver.Label.emit (some (.clearField 0)) label := by
change MachineControl.statement id (emitterEquiv q).symm
(MachineControl.statement id (emitterEquiv q) _) = _
exact MachineFieldProfile.statement_roundtrip (emitterEquiv q).symm _
private theorem context_rowState (input : GenericGraphTables.Table q)
(e : Fin input.darts) :
context (CompareLoop.rowState input e).1 = EmitRows.rowContext input e := by
change (input.rows[e].relation,
decide (input.rows[e].tail.val = input.rows[input.rows[e].reverseIndex].tail.val)) =
(input.rows[e].relation,
decide (input.rows[e].tail = input.rows[input.rows[e].reverseIndex].tail))
simp only [Fin.val_inj]
def emitInTime (input : GenericGraphTables.Table q) (e : Fin input.darts)
(base : Tape → List Bool)
(operands : ∀ i, base (rowSources i) = encodeWord (EmitRows.rowValues input e i))
(scratchEmpty : base .scratch = []) :
StateTransition.EvalsToInTime (TM2.step (Driver.program q))
⟨some (Driver.rowEntry q), CompareLoop.rowState input e, base⟩
(some ⟨some (.clearField 0), CompareLoop.rowState input e,
Emitter.resultTapes .reversed base (encodeWords (EmitRows.blockWords input e))⟩)
(Enumeration.localCount q * (6 * (2 * 4104)) *
(3 * ((LoopState.inputBits input).length + 1) + 3) + 1) := by
let view := MachineControl.program (Equiv.refl (Driver.Label q)) (emitterEquiv q).symm
(Driver.program q)
have sourceScratch : ∀ i : Fin 5, rowSources i ≠ Tape.scratch := by
intro i; fin_cases i <;> decide
have sourceOutput : ∀ i : Fin 5, rowSources i ≠ Tape.reversed := by
intro i; fin_cases i <;> decide
have bounded : ∀ i, EmitRows.rowValues input e i ≤ (LoopState.inputBits input).length := by
have hn := GenericGraphTables.vertices_le_tableBits_length input
have hm := GenericGraphTables.darts_le_tableBits_length input
have he := e.isLt
have ht := input.rows[e].tail.isLt
have hh := input.rows[input.rows[e].reverseIndex].tail.isLt
intro i
fin_cases i <;> simp [EmitRows.rowValues, Addresses.values, LoopState.inputBits] <;> omega
have run := EmitRows.blockInTimeAmbient input e (@context q)
(CompareLoop.rowState input e).1 (context_rowState input e)
rowSources Tape.scratch Tape.reversed sourceScratch sourceOutput (by decide)
Driver.Label.emit (some (.clearField 0)) view emitterView_at base operands
scratchEmpty (LoopState.inputBits input).length bounded
have restored : MachineControl.program (Equiv.refl (Driver.Label q)) (emitterEquiv q) view =
Driver.program q := by
funext label
exact MachineFieldProfile.statement_roundtrip (emitterEquiv q) (Driver.program q label)
have transported := transportInTime (emitterEquiv q) view run
rw [restored] at transported
simpa [MachineControl.configuration, emitterEquiv, Option.map_some, id_eq,
Driver.rowEntry, Driver.rowPlan, CompareLoop.rowState, normal] using transported
private theorem result_ready (input : GenericGraphTables.Table q) (e : Fin input.darts)
(base : Tape → List Bool) (ready : LoopState.Ready input e.val base) :
LoopState.Ready input (e.val + 1) (resultTapes input e base) := by
constructor
· exact Nat.succ_le_of_lt e.isLt
all_goals simp [resultTapes, clearFieldsTapes, emittedTapes, Emitter.resultTapes,
workingTapes, Lookup.headLookupTapes, MachineLookup.tapes, headPorts,
parsedTapes, Lookup.readRowTapes, guardTapes, ready.original, ready.vertices,
ready.darts, ready.scratch, ready.compareLeft, ready.compareRight, ready.output,
ready.reversed, LoopState.prefixBits_reverse_succ input e.val e.isLt]
private theorem workingOperands (input : GenericGraphTables.Table q) (e : Fin input.darts)
(base : Tape → List Bool) (ready : LoopState.Ready input e.val base) :
∀ i, workingTapes input e base (rowSources i) =
encodeWord (EmitRows.rowValues input e i) := by
intro i
fin_cases i <;> simp [rowSources, EmitRows.rowValues, Addresses.values,
workingTapes, Lookup.headLookupTapes, MachineLookup.tapes, headPorts,
parsedTapes, Lookup.readRowTapes, guardTapes, Lookup.headValue, Lookup.headIndex,
ready.vertices, ready.darts, ready.edge]
def rowStep (input : GenericGraphTables.Table q) (e : Fin input.darts)
(base : Tape → List Bool) (ready : LoopState.Ready input e.val base)
(oldRelation : GenericGraphTables.RelationTable q) (oldFlag : Bool) :
RowStepResult input e base oldRelation oldFlag := by
let N := (LoopState.inputBits input).length
have guarded : StateTransition.EvalsToInTime (TM2.step (Driver.program q))
⟨some .guard, normal oldRelation oldFlag, base⟩
(some ⟨some .tailStart, normal oldRelation oldFlag, guardTapes input e base⟩) 1 := by
have hbase : MachineUnaryCounter.counterTapes .counter base
(input.darts - (e.val + 1) + 1) [] = base := by
simp only [MachineUnaryCounter.counterTapes, List.append_nil,
← ready.counter_succ e.isLt, Function.update_eq_self]
have run := MachineUnaryCounter.guardInTime_succ Tape.counter Driver.Label.guard
.tailStart .reverseOutput (Driver.program q) (by rfl) base
(input.darts - (e.val + 1)) [] (normal oldRelation oldFlag).1 none
rw [hbase] at run
simpa only [MachineUnaryCounter.counterTapes, List.append_nil, guardTapes,
normal] using run
have parsed : StateTransition.EvalsToInTime (TM2.step (Driver.program q))
⟨some .tailStart, normal oldRelation oldFlag, guardTapes input e base⟩
(some ⟨some .headCopyFirst, normal input.rows[e].relation oldFlag,
parsedTapes input e base⟩) (Lookup.readRowCost input.rows[e]) := by
have hcursor : guardTapes input e base .cursor = Lookup.rowBits input.rows[e] ++
LoopState.cursorBits input (e.val + 1) := by
simpa only [guardTapes, Function.update_of_ne (by decide : Tape.cursor ≠ .counter),
Lookup.rowBits] using ready.cursor.trans (LoopState.cursorBits_succ input e.val e.isLt)
have htail : guardTapes input e base .tail = [] := by simp [guardTapes, ready.tail]
have hreverse : guardTapes input e base .reverseIndex = [] := by
simp [guardTapes, ready.reverseIndex]
simpa only [normal, parsedTapes] using
Lookup.readRowInTime Tape.cursor Tape.tail Tape.reverseIndex
(by decide) (by decide) (by decide) Driver.Label.tailStart .tailLoop
.reverseStart .reverseLoop .readPredicate .headCopyFirst (Driver.program q)
(by rfl) (by rfl) (by rfl) (by rfl) (by rfl)
(guardTapes input e base) input.rows[e] (LoopState.cursorBits input (e.val + 1))
hcursor htail hreverse (oldFlag, false, none) oldRelation none
have looked : StateTransition.EvalsToInTime (TM2.step (Driver.program q))
⟨some .headCopyFirst, normal input.rows[e].relation oldFlag, parsedTapes input e base⟩
(some ⟨some (.compare .leftFirst), normal input.rows[e].relation oldFlag,
workingTapes input e base⟩) (7 * N + 5) := by
have htable : parsedTapes input e base (headPorts 0) = GenericGraphTables.tableBits input := by
simpa [headPorts, parsedTapes, Lookup.readRowTapes, guardTapes,
LoopState.inputBits] using ready.original
have hreverse : parsedTapes input e base (headPorts 1) =
encodeWord (Lookup.headIndex input e).val := by
simp [headPorts, parsedTapes, Lookup.readRowTapes, Lookup.headIndex]
have hhead : parsedTapes input e base (headPorts 4) = [] := by
simp [headPorts, parsedTapes, Lookup.readRowTapes, guardTapes, ready.head]
have hscratch : parsedTapes input e base (headPorts 5) = [] := by
simp [headPorts, parsedTapes, Lookup.readRowTapes, guardTapes, ready.scratch]
have run := Lookup.headLookupInTime headPorts headPorts_injective
Driver.Label.headCopyFirst .headCopySecond .indexCopyFirst .indexCopySecond
.headVertices .headDarts Driver.Label.headLookup (some (.compare .leftFirst))
(Driver.program q) (by rfl) (by rfl) (by rfl) (by rfl) (by rfl) (by rfl)
(fun _ => rfl) (parsedTapes input e base) input e htable hreverse hhead hscratch
(normal input.rows[e].relation oldFlag).1 none
simpa only [workingTapes, normal, N, LoopState.inputBits, Lookup.headTimePolynomial,
Polynomial.eval_add, Polynomial.eval_mul, Polynomial.eval_C, Polynomial.eval_X] using run
have hleft : workingTapes input e base .compareLeft = [] :=
(workingTapes_other input e base .compareLeft (by decide) (by decide) (by decide)
(by decide) (by decide) (by decide) (by decide)).trans ready.compareLeft
have hright : workingTapes input e base .compareRight = [] :=
(workingTapes_other input e base .compareRight (by decide) (by decide) (by decide)
(by decide) (by decide) (by decide) (by decide)).trans ready.compareRight
have hscratch : workingTapes input e base .scratch = [] :=
(workingTapes_other input e base .scratch (by decide) (by decide) (by decide)
(by decide) (by decide) (by decide) (by decide)).trans ready.scratch
let compared := CompareLoop.compareRowInTime Driver.Label.compare (Driver.rowEntry q)
(Driver.program q) (fun _ => rfl) input e (workingTapes input e base)
(workingTapes_tail input e base) (workingTapes_head input e base)
hleft hright hscratch oldFlag
let emitted := emitInTime input e (workingTapes input e base)
(workingOperands input e base ready) hscratch
let cleared := clearFieldsInTime (emittedTapes input e base) (CompareLoop.rowState input e).1
have hedge : clearFieldsTapes (emittedTapes input e base) .edge = encodeWord e.val := by
simp [clearFieldsTapes, emittedTapes, Emitter.resultTapes, workingTapes,
Lookup.headLookupTapes, MachineLookup.tapes, headPorts, parsedTapes,
Lookup.readRowTapes, guardTapes, ready.edge]
let incremented := incrementInTime (clearFieldsTapes (emittedTapes input e base))
(CompareLoop.rowState input e).1 e.val hedge
let run₁ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ guarded parsed
let run₂ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ run₁ looked
let run₃ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ run₂ compared
let run₄ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ run₃ emitted
let run₅ := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ run₄ cleared
let run := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ run₅ incremented
refine {
tapes := resultTapes input e base
ready := result_ready input e base ready
run := { toEvalsTo := run.toEvalsTo, steps_le_m := ?_ }
}
have hn : input.vertices ≤ N := GenericGraphTables.vertices_le_tableBits_length input
have hm : input.darts ≤ N := GenericGraphTables.darts_le_tableBits_length input
have hr : Lookup.readRowCost input.rows[e] ≤ 2 * N + 5 :=
Lookup.readRowCost_table_le input e
have ht : (emittedTapes input e base .tail).length ≤ N := by
simp only [emittedTapes, Emitter.resultTapes,
Function.update_of_ne (by decide : Tape.tail ≠ .reversed), workingTapes_tail,
encodeWord_length]
exact (Nat.succ_le_of_lt input.rows[e].tail.isLt).trans hn
have hv : (emittedTapes input e base .reverseIndex).length ≤ N := by
simp only [emittedTapes, Emitter.resultTapes,
Function.update_of_ne (by decide : Tape.reverseIndex ≠ .reversed), workingTapes_reverse,
encodeWord_length]
exact (Nat.succ_le_of_lt input.rows[e].reverseIndex.isLt).trans hm
have hh : (emittedTapes input e base .head).length ≤ N := by
simp only [emittedTapes, Emitter.resultTapes,
Function.update_of_ne (by decide : Tape.head ≠ .reversed), workingTapes_head,
encodeWord_length]
exact (Nat.succ_le_of_lt input.rows[input.rows[e].reverseIndex].tail.isLt).trans hn
have budget := run.steps_le_m
change run.steps ≤ (32 + 8 * (Enumeration.localCount q * (6 * (2 * 4104)))) * (N + 1)
nlinarith
end MinUncutGames.Foundations.PCP.AlphabetTable.Body
namespace MinUncutGames.Foundations.PCP.AlphabetTable.Initialization
open Turing
open MinUncutGames.Foundations.Complexity
open RuntimeModel
variable {q : Nat}
def inputTapes (input : GenericGraphTables.Table q) : Tape → List Bool
| .original => GenericGraphTables.tableBits input
| _ => []
def rowsBits (input : GenericGraphTables.Table q) : List Bool :=
encodeWords ((GenericGraphTables.rowList input).flatMap GenericGraphTables.rowWords)
def headerBits (input : GenericGraphTables.Table q) : List Bool :=
encodeWords [Enumeration.vertexCount input.vertices input.darts q,
Enumeration.dartCount input.darts q]
def setupTapes (input : GenericGraphTables.Table q) : Tape → List Bool
| .original => GenericGraphTables.tableBits input
| .cursor => rowsBits input
| .vertices => encodeWord input.vertices
| .darts | .counter => encodeWord input.darts
| .edge => encodeWord 0
| _ => []
def loopTapes (input : GenericGraphTables.Table q) : Tape → List Bool
| .original => GenericGraphTables.tableBits input
| .cursor => rowsBits input
| .vertices => encodeWord input.vertices
| .darts | .counter => encodeWord input.darts
| .edge => encodeWord 0
| .reversed => (headerBits input).reverse
| _ => []
def loopStart (input : GenericGraphTables.Table q) :
TM2.Cfg Alphabet (Driver.Label q) (State q) :=
⟨some .guard, initial q, loopTapes input⟩
theorem setupResult_eq (input : GenericGraphTables.Table q) :
Setup.resultTapes setupPorts (inputTapes input) input.vertices input.darts (rowsBits input) =
setupTapes input := by
funext tape
cases tape <;> simp [Setup.resultTapes, setupPorts, inputTapes, setupTapes]
theorem headerResult_eq (input : GenericGraphTables.Table q) :
Emitter.resultTapes Tape.reversed (setupTapes input) (headerBits input) = loopTapes input := by
funext tape
cases tape <;> simp [Emitter.resultTapes, setupTapes, loopTapes]
theorem machineInitial_eq (input : GenericGraphTables.Table q) :
initList (Driver.machine q) (GenericGraphTables.tableBits input) =
(⟨some (.setup .copyFirst), initial q, inputTapes input⟩ :
TM2.Cfg Alphabet (Driver.Label q) (State q)) := by
change (⟨some (.setup .copyFirst), initial q, _⟩ :
TM2.Cfg Alphabet (Driver.Label q) (State q)) = _
congr 1
funext tape
cases tape <;> rfl
def setupInTime (input : GenericGraphTables.Table q) :
StateTransition.EvalsToInTime (TM2.step (Driver.program q))
(initList (Driver.machine q) (GenericGraphTables.tableBits input))
(some ⟨some (Driver.headerEntry q), initial q, setupTapes input⟩)
(6 * (GenericGraphTables.tableBits input).length + 11) := by
have initialized : ∀ i : Fin 7, inputTapes input (setupPorts i) =
if i = 0 then GenericGraphTables.tableBits input else [] := by
intro i
fin_cases i <;> rfl
have run := Setup.tableSetupInTime setupPorts setupPorts_injective Driver.Label.setup
(some (Driver.headerEntry q)) (Driver.program q) (Driver.program_setup q)
(inputTapes input) input initialized (initial q).1 none
have initialState : ((initial q).1, (none : Option Bool)) = initial q := rfl
have result := setupResult_eq input
unfold rowsBits at result
rw [initialState, result] at run
rw [machineInitial_eq]
exact run
private theorem headerView_at (label : Emitter.Label (Driver.headerPlan q).length
(Addresses.fieldBound q)) :
(MachineControl.program (Equiv.refl (Driver.Label q)) (emitterEquiv q).symm
(Driver.program q)) (.header label) =
Emitter.statement (Emitter.listCommands (Driver.headerPlan q)) headerSources
Tape.scratch Tape.reversed Driver.Label.header (some .guard) label := by
change MachineControl.statement id (emitterEquiv q).symm
(MachineControl.statement id (emitterEquiv q) _) = _
exact MachineFieldProfile.statement_roundtrip (emitterEquiv q).symm _
def headerInTime (input : GenericGraphTables.Table q) :
StateTransition.EvalsToInTime (TM2.step (Driver.program q))
⟨some (Driver.headerEntry q), initial q, setupTapes input⟩
(some (loopStart input))
(7 * (3 * ((GenericGraphTables.tableBits input).length + 1) + 3) + 1) := by
let view := MachineControl.program (Equiv.refl (Driver.Label q)) (emitterEquiv q).symm
(Driver.program q)
have sourceScratch : ∀ i : Fin 2, headerSources i ≠ Tape.scratch := by
intro i
fin_cases i <;> decide
have sourceOutput : ∀ i : Fin 2, headerSources i ≠ Tape.reversed := by
intro i
fin_cases i <;> decide
have operands : ∀ i : Fin 2, setupTapes input (headerSources i) =
encodeWord (![input.vertices, input.darts] i) := by
intro i
fin_cases i <;> rfl
have run := EmitRows.headerInTime q input.vertices input.darts headerSources
Tape.scratch Tape.reversed sourceScratch sourceOutput (by decide)
Driver.Label.header (some .guard) view headerView_at
(setupTapes input) operands (by rfl) (initial q).1
(GenericGraphTables.tableBits input).length
(GenericGraphTables.vertices_le_tableBits_length input)
(GenericGraphTables.darts_le_tableBits_length input)
have restored : MachineControl.program (Equiv.refl (Driver.Label q)) (emitterEquiv q) view =
Driver.program q := by
funext label
exact MachineFieldProfile.statement_roundtrip (emitterEquiv q) (Driver.program q label)
have transported := transportInTime (emitterEquiv q) view run
rw [restored] at transported
change StateTransition.EvalsToInTime (TM2.step (Driver.program q))
⟨some (Driver.headerEntry q), initial q, setupTapes input⟩
(some ⟨some .guard, initial q,
Emitter.resultTapes Tape.reversed (setupTapes input) (headerBits input)⟩)
(7 * (3 * ((GenericGraphTables.tableBits input).length + 1) + 3) + 1) at transported
rw [headerResult_eq] at transported
exact transported
def time (N : Nat) : Nat := (6 * N + 11) + (7 * (3 * (N + 1) + 3) + 1)
theorem time_eq (N : Nat) : time N = 27 * N + 54 := by
unfold time
omega
def initializeInTime (input : GenericGraphTables.Table q) :
StateTransition.EvalsToInTime (TM2.step (Driver.program q))
(initList (Driver.machine q) (GenericGraphTables.tableBits input))
(some (loopStart input)) (time (GenericGraphTables.tableBits input).length) := by
let run := StateTransition.EvalsToInTime.trans _ _ _ _ _ _
(setupInTime input) (headerInTime input)
refine { toEvalsTo := run.toEvalsTo, steps_le_m := ?_ }
have bound := run.steps_le_m
unfold time
omega
end MinUncutGames.Foundations.PCP.AlphabetTable.Initialization
end OAI