File size: 12,129 Bytes
760f90e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | import OAI.Computability.UniqueGames.Machines.MachineDrainManyLemmas
import OAI.Computability.UniqueGames.PCP.QueriesLemmas
import OAI.Computability.UniqueGames.PCP.RuntimeModel
namespace OAI
namespace UniqueGamesTheorem.Foundations.PCP.AlphabetTable.CompareLoop
open Turing
open UniqueGamesTheorem.Foundations.Complexity
open RuntimeModel
inductive Label
| leftFirst | leftSecond | rightFirst | rightSecond | compare
| equalStore | differentStore
deriving DecidableEq
protected abbrev Label.enumList : List Label := [.leftFirst, .leftSecond, .rightFirst,
.rightSecond, .compare, .equalStore, .differentStore]
protected theorem Label.enumList_getElem?_ctorIdx_eq (x : Label) :
Label.enumList[x.ctorIdx]? = some x := by
cases x <;> rfl
protected theorem Label.enumList_nodup : Label.enumList.Nodup := by decide
instance : Fintype Label where
elems := ⟨Label.enumList, Label.enumList_nodup⟩
complete x := by cases x <;> decide
variable {q : Nat} {Λ : Type}
/-- A composable block with exactly seven internal control labels. -/
def statement (labels : Label → Λ) (next : Λ) :
Label → TM2.Stmt Alphabet Λ (State q)
| .leftFirst => compareStatement (MachineFieldProfile.copyStatement
(Reduction.MachineTransfer.loopAt .tail .scratch id false
(labels .leftFirst) (some (labels .leftSecond))))
| .leftSecond => compareStatement (MachineFieldProfile.copyStatement
(MachineCopy.forkLoop .scratch .tail .compareLeft false
(labels .leftSecond) (some (labels .rightFirst))))
| .rightFirst => compareStatement (MachineFieldProfile.copyStatement
(Reduction.MachineTransfer.loopAt .head .scratch id false
(labels .rightFirst) (some (labels .rightSecond))))
| .rightSecond => compareStatement (MachineFieldProfile.copyStatement
(MachineCopy.forkLoop .scratch .head .compareRight false
(labels .rightSecond) (some (labels .compare))))
| .compare => compareStatement (MachineCompare.loop .compareLeft .compareRight
(labels .compare) (some (labels .equalStore)) (some (labels .differentStore)))
| .equalStore => storeLoop true next
| .differentStore => storeLoop false next
private theorem compareProgram_at
(program : Λ → TM2.Stmt Alphabet Λ (State q)) (label : Λ)
(stmt : TM2.Stmt Alphabet Λ
(MachineCompare.State (GenericGraphTables.RelationTable q × Bool)))
(atLabel : program label = compareStatement stmt) :
(MachineControl.program (Equiv.refl Λ) (compareEquiv q) program) label = stmt := by
change MachineControl.statement id (compareEquiv q) (program label) = stmt
rw [atLabel]
exact MachineFieldProfile.statement_roundtrip (compareEquiv q) stmt
/-- Compare arbitrary words while restoring every tape, including work tapes.
All program hypotheses specify only the statements at the seven labels. -/
def compareWordsInTime (labels : Label → Λ) (next : Λ)
(program : Λ → TM2.Stmt Alphabet Λ (State q))
(atLabel : ∀ label, program (labels label) = statement labels next label)
(base : Tape → List Bool)
(hLeft : base .compareLeft = []) (hRight : base .compareRight = [])
(hScratch : base .scratch = [])
(relation : GenericGraphTables.RelationTable q) (oldLoop : Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some (labels .leftFirst), normal relation oldLoop, base⟩
(some ⟨some next, normal relation (decide (base .tail = base .head)), base⟩)
(2 * ((base .tail).length + 1) + 2 * ((base .head).length + 1) +
max (base .tail).length (base .head).length + 2) := by
let view := MachineControl.program (Equiv.refl Λ) (compareEquiv q) program
have compared := MachineFieldProfile.nonDestructiveCompareInTime
Tape.tail Tape.head Tape.compareLeft Tape.compareRight Tape.scratch
(by decide) (by decide) (by decide)
(labels .leftFirst) (labels .leftSecond) (labels .rightFirst)
(labels .rightSecond) (labels .compare)
(some (labels .equalStore)) (some (labels .differentStore)) view
(compareProgram_at program _ _ (atLabel .leftFirst))
(compareProgram_at program _ _ (atLabel .leftSecond))
(compareProgram_at program _ _ (atLabel .rightFirst))
(compareProgram_at program _ _ (atLabel .rightSecond))
(compareProgram_at program _ _ (atLabel .compare))
base hLeft hRight hScratch (relation, oldLoop)
have restoredProgram :
MachineControl.program (Equiv.refl Λ) (compareEquiv q).symm view = program := by
funext label
exact MachineFieldProfile.statement_roundtrip (compareEquiv q).symm (program label)
have transported := transportInTime (compareEquiv q).symm view compared
rw [restoredProgram] at transported
have copied : StateTransition.EvalsToInTime (TM2.step program)
⟨some (labels .leftFirst), normal relation oldLoop, base⟩
(some ⟨if base .tail = base .head then some (labels .equalStore)
else some (labels .differentStore), normal relation oldLoop, base⟩)
(2 * ((base .tail).length + 1) + 2 * ((base .head).length + 1) +
max (base .tail).length (base .head).length + 1) := by
simpa [MachineControl.configuration, compareEquiv, MachineFieldProfile.normalState,
normal] using transported
have stored : StateTransition.EvalsToInTime (TM2.step program)
⟨if base .tail = base .head then some (labels .equalStore)
else some (labels .differentStore), normal relation oldLoop, base⟩
(some ⟨some next, normal relation (decide (base .tail = base .head)), base⟩) 1 := {
steps := 1
evals_in_steps := by
change TM2.step program
⟨if base .tail = base .head then some (labels .equalStore)
else some (labels .differentStore), normal relation oldLoop, base⟩ = _
by_cases heq : base .tail = base .head
· simp [heq, TM2.step, atLabel, statement, stepAux_storeLoop, normal]
· simp [heq, TM2.step, atLabel, statement, stepAux_storeLoop, normal]
steps_le_m := Nat.le_refl _
}
let run := StateTransition.EvalsToInTime.trans _ _ _ _ _ _ copied stored
exact { toEvalsTo := run.toEvalsTo, steps_le_m := by have h := run.steps_le_m; omega }
def comparisonTime (tailName headName : Nat) : Nat :=
2 * (tailName + 2) + 2 * (headName + 2) + max (tailName + 1) (headName + 1) + 2
theorem encodeWord_eq_iff (left right : Nat) :
encodeWord left = encodeWord right ↔ left = right := by
constructor
· intro h
have hh := congrArg List.length h
simpa only [encodeWord_length, Nat.add_right_cancel_iff] using hh
· rintro rfl
rfl
/-- The stored flag is equality of the actual unary endpoint names. -/
def compareInTime (labels : Label → Λ) (next : Λ)
(program : Λ → TM2.Stmt Alphabet Λ (State q))
(atLabel : ∀ label, program (labels label) = statement labels next label)
(base : Tape → List Bool) (tailName headName : Nat)
(hTail : base .tail = encodeWord tailName) (hHead : base .head = encodeWord headName)
(hLeft : base .compareLeft = []) (hRight : base .compareRight = [])
(hScratch : base .scratch = [])
(relation : GenericGraphTables.RelationTable q) (oldLoop : Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some (labels .leftFirst), normal relation oldLoop, base⟩
(some ⟨some next, normal relation (decide (tailName = headName)), base⟩)
(comparisonTime tailName headName) := by
have run := compareWordsInTime labels next program atLabel base hLeft hRight hScratch
relation oldLoop
simpa only [hTail, hHead, encodeWord_eq_iff, encodeWord_length, comparisonTime,
Nat.add_assoc] using run
theorem comparisonTime_le (tailName headName N : Nat)
(hTail : tailName ≤ N) (hHead : headName ≤ N) :
comparisonTime tailName headName ≤ 5 * N + 11 := by
have hm : max (tailName + 1) (headName + 1) ≤ N + 1 := max_le (by omega) (by omega)
unfold comparisonTime
omega
theorem comparisonTime_le_of_lt (tailName headName N : Nat)
(hTail : tailName < N) (hHead : headName < N) :
comparisonTime tailName headName ≤ 5 * N + 6 := by
have hm : max (tailName + 1) (headName + 1) ≤ N := max_le (by omega) (by omega)
unfold comparisonTime
omega
/-- A linear word-length budget, with the actual complete execution retained. -/
def compareWordsInTime_bounded (labels : Label → Λ) (next : Λ)
(program : Λ → TM2.Stmt Alphabet Λ (State q))
(atLabel : ∀ label, program (labels label) = statement labels next label)
(base : Tape → List Bool)
(hLeft : base .compareLeft = []) (hRight : base .compareRight = [])
(hScratch : base .scratch = [])
(relation : GenericGraphTables.RelationTable q) (oldLoop : Bool)
(N : Nat) (hTail : (base .tail).length ≤ N) (hHead : (base .head).length ≤ N) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some (labels .leftFirst), normal relation oldLoop, base⟩
(some ⟨some next, normal relation (decide (base .tail = base .head)), base⟩)
(5 * N + 6) := by
let run := compareWordsInTime labels next program atLabel base hLeft hRight hScratch
relation oldLoop
refine { toEvalsTo := run.toEvalsTo, steps_le_m := ?_ }
have hm := max_le hTail hHead
have h := run.steps_le_m
omega
def rowLoop (table : GenericGraphTables.Table q) (e : Fin table.darts) : Bool :=
decide (table.rows[e].tail.val = table.rows[table.rows[e].reverseIndex].tail.val)
def rowState (table : GenericGraphTables.Table q) (e : Fin table.darts) : State q :=
normal table.rows[e].relation (rowLoop table e)
theorem rowLoop_eq_graph (table : GenericGraphTables.Table q) (e : Fin table.darts) :
rowLoop table e = decide ((GenericGraphTables.semantics table).tail e =
(GenericGraphTables.semantics table).head e) := by
simp only [rowLoop, ConstraintGraph.head, GenericGraphTables.semantics_tail,
GenericGraphTables.semantics_reverse, Fin.val_inj]
rfl
theorem rowContext_eq (table : GenericGraphTables.Table q) (e : Fin table.darts) :
context (rowState table e).1 =
(table.rows[e].relation, decide ((GenericGraphTables.semantics table).tail e =
(GenericGraphTables.semantics table).head e)) := by
change (table.rows[e].relation, rowLoop table e) = _
rw [rowLoop_eq_graph]
def compareRowInTime (labels : Label → Λ) (next : Λ)
(program : Λ → TM2.Stmt Alphabet Λ (State q))
(atLabel : ∀ label, program (labels label) = statement labels next label)
(table : GenericGraphTables.Table q) (e : Fin table.darts)
(base : Tape → List Bool)
(hTail : base .tail = encodeWord table.rows[e].tail.val)
(hHead : base .head = encodeWord table.rows[table.rows[e].reverseIndex].tail.val)
(hLeft : base .compareLeft = []) (hRight : base .compareRight = [])
(hScratch : base .scratch = []) (oldLoop : Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some (labels .leftFirst), normal table.rows[e].relation oldLoop, base⟩
(some ⟨some next, rowState table e, base⟩) (5 * table.vertices + 6) := by
let run := compareInTime labels next program atLabel base _ _ hTail hHead
hLeft hRight hScratch table.rows[e].relation oldLoop
refine { toEvalsTo := run.toEvalsTo, steps_le_m := ?_ }
exact run.steps_le_m.trans (comparisonTime_le_of_lt _ _ _
table.rows[e].tail.isLt table.rows[table.rows[e].reverseIndex].tail.isLt)
theorem relationFromRowState (table : GenericGraphTables.Table q) (e : Fin table.darts)
(event : AlphabetGraph.LocalEvent (Fin q)) (slot : Fin 6) (orientation : Bool)
(a b : GraphTables.Label) :
GraphTables.relationAt
(Relations.relationFromTable (context (rowState table e).1).1
(context (rowState table e).1).2 event slot orientation) a b =
(AlphabetGraph.graph (GenericGraphTables.semantics table)).accepts
(((e, event), slot), orientation)
(Enumeration.labelEquiv.symm a) (Enumeration.labelEquiv.symm b) := by
rw [rowContext_eq]
exact Relations.relationAt_relation_graph (GenericGraphTables.semantics table)
e event slot orientation a b
end UniqueGamesTheorem.Foundations.PCP.AlphabetTable.CompareLoop
end OAI
|