File size: 15,984 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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | import OAI.Computability.UniqueGames.Machines.MachineCompositionLemmas
import OAI.Computability.UniqueGames.Reduction.MachineTransfer
namespace OAI
section
/-!
# A destructive equality test on two actual Bool stacks
Each loop transition pops both stacks and compares only the two finite head
registers. A sticky mismatch flag makes the loop continue after disagreement.
The final empty/empty test selects an equal or different continuation and resets
all comparison registers. The ambient state and every other tape are preserved.
-/
namespace UniqueGamesTheorem.Foundations.Complexity.MachineCompare
open Turing
open UniqueGamesTheorem.Reduction.MachineTransfer
variable {K Λ σ : Type} [DecidableEq K]
abbrev Alphabet (K : Type) : K → Type := fun _ => Bool
abbrev State (σ : Type) := σ × Bool × Option Bool × Option Bool
/-- An optional continuation, without reading or writing any tape. -/
def exitAt (exit : Option Λ) : TM2.Stmt (Alphabet K) Λ (State σ) :=
match exit with
| none => .halt
| some label => .goto fun _ => label
/-- Clear both head registers and the mismatch flag after choosing the exit. -/
def finish (exit : Option Λ) : TM2.Stmt (Alphabet K) Λ (State σ) :=
.load (fun state => (state.1, false, none, none)) (exitAt exit)
/-- The only comparison performed by the program is on two `Option Bool` heads.
In particular, no statement is given a whole-list equality oracle. -/
def loop (left right : K) (loopLabel : Λ) (equalExit differentExit : Option Λ) :
TM2.Stmt (Alphabet K) Λ (State σ) :=
.pop left (fun state head => (state.1, state.2.1, head, state.2.2.2))
(.pop right (fun state head => (state.1, state.2.1, state.2.2.1, head))
(.branch (fun state => state.2.2.1.isNone && state.2.2.2.isNone)
(.branch (fun state => state.2.1) (finish differentExit) (finish equalExit))
(.load (fun state =>
(state.1, state.2.1 || decide (state.2.2.1 ≠ state.2.2.2), none, none))
(.goto fun _ => loopLabel))))
/-- Specification-only outcome, also recording an earlier mismatch. -/
def mismatch (flag : Bool) (leftWord rightWord : List Bool) : Bool :=
flag || decide (leftWord ≠ rightWord)
@[simp] theorem mismatch_nil_nil (flag : Bool) : mismatch flag [] [] = flag := by
simp [mismatch]
@[simp] theorem mismatch_nil_cons (flag head : Bool) (tail : List Bool) :
mismatch flag [] (head :: tail) = true := by simp [mismatch]
@[simp] theorem mismatch_cons_nil (flag head : Bool) (tail : List Bool) :
mismatch flag (head :: tail) [] = true := by simp [mismatch]
@[simp] theorem mismatch_true (leftWord rightWord : List Bool) :
mismatch true leftWord rightWord = true := by simp [mismatch]
theorem mismatch_cons (flag leftHead rightHead : Bool) (leftTail rightTail : List Bool) :
mismatch (flag || decide (leftHead ≠ rightHead)) leftTail rightTail =
mismatch flag (leftHead :: leftTail) (rightHead :: rightTail) := by
cases flag <;> cases leftHead <;> cases rightHead <;> simp [mismatch]
@[simp] theorem stepAux_exitAt (exit : Option Λ) (state : State σ)
(tapes : K → List Bool) :
TM2.stepAux (exitAt exit) state tapes = ⟨exit, state, tapes⟩ := by
cases exit <;> rfl
@[simp] theorem stepAux_finish (exit : Option Λ) (state : State σ)
(tapes : K → List Bool) :
TM2.stepAux (finish exit) state tapes =
⟨exit, (state.1, false, none, none), tapes⟩ := by
simp [finish, TM2.stepAux]
private theorem update_left (left right : K) (distinct : left ≠ right)
(base : K → List Bool) (leftWord rightWord replacement : List Bool) :
Function.update (tapesAt left right base leftWord rightWord) left replacement =
tapesAt left right base replacement rightWord := by
funext k
by_cases hl : k = left
· subst k
simp [tapesAt, distinct]
· by_cases hr : k = right
· subst k
simp [tapesAt, Ne.symm distinct]
· simp [tapesAt, hl, hr]
private theorem update_right (left right : K) (base : K → List Bool)
(leftWord rightWord replacement : List Bool) :
Function.update (tapesAt left right base leftWord rightWord) right replacement =
tapesAt left right base leftWord replacement := by
simp [tapesAt]
theorem step_empty (left right : K) (distinct : left ≠ right)
(loopLabel : Λ) (equalExit differentExit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet K) Λ (State σ))
(atLoop : program loopLabel = loop left right loopLabel equalExit differentExit)
(base : K → List Bool) (ambient : σ) (flag : Bool)
(leftRegister rightRegister : Option Bool) :
TM2.step program
⟨some loopLabel, (ambient, flag, leftRegister, rightRegister),
tapesAt left right base [] []⟩ =
some ⟨if flag then differentExit else equalExit, (ambient, false, none, none),
tapesAt left right base [] []⟩ := by
change some (TM2.stepAux (program loopLabel) (ambient, flag, leftRegister, rightRegister)
(tapesAt left right base [] [])) = _
rw [atLoop]
cases flag <;>
simp [loop, TM2.stepAux, distinct, update_left, update_right]
theorem step_nil_cons (left right : K) (distinct : left ≠ right)
(loopLabel : Λ) (equalExit differentExit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet K) Λ (State σ))
(atLoop : program loopLabel = loop left right loopLabel equalExit differentExit)
(base : K → List Bool) (head : Bool) (tail : List Bool) (ambient : σ) (flag : Bool)
(leftRegister rightRegister : Option Bool) :
TM2.step program
⟨some loopLabel, (ambient, flag, leftRegister, rightRegister),
tapesAt left right base [] (head :: tail)⟩ =
some ⟨some loopLabel, (ambient, true, none, none), tapesAt left right base [] tail⟩ := by
change some (TM2.stepAux (program loopLabel) (ambient, flag, leftRegister, rightRegister)
(tapesAt left right base [] (head :: tail))) = _
rw [atLoop]
simp [loop, TM2.stepAux, distinct, update_left, update_right]
theorem step_cons_nil (left right : K) (distinct : left ≠ right)
(loopLabel : Λ) (equalExit differentExit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet K) Λ (State σ))
(atLoop : program loopLabel = loop left right loopLabel equalExit differentExit)
(base : K → List Bool) (head : Bool) (tail : List Bool) (ambient : σ) (flag : Bool)
(leftRegister rightRegister : Option Bool) :
TM2.step program
⟨some loopLabel, (ambient, flag, leftRegister, rightRegister),
tapesAt left right base (head :: tail) []⟩ =
some ⟨some loopLabel, (ambient, true, none, none), tapesAt left right base tail []⟩ := by
change some (TM2.stepAux (program loopLabel) (ambient, flag, leftRegister, rightRegister)
(tapesAt left right base (head :: tail) [])) = _
rw [atLoop]
simp [loop, TM2.stepAux, distinct, update_left, update_right]
theorem step_cons_cons (left right : K) (distinct : left ≠ right)
(loopLabel : Λ) (equalExit differentExit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet K) Λ (State σ))
(atLoop : program loopLabel = loop left right loopLabel equalExit differentExit)
(base : K → List Bool) (leftHead rightHead : Bool) (leftTail rightTail : List Bool)
(ambient : σ) (flag : Bool) (leftRegister rightRegister : Option Bool) :
TM2.step program
⟨some loopLabel, (ambient, flag, leftRegister, rightRegister),
tapesAt left right base (leftHead :: leftTail) (rightHead :: rightTail)⟩ =
some ⟨some loopLabel,
(ambient, flag || decide (leftHead ≠ rightHead), none, none),
tapesAt left right base leftTail rightTail⟩ := by
change some (TM2.stepAux (program loopLabel) (ambient, flag, leftRegister, rightRegister)
(tapesAt left right base (leftHead :: leftTail) (rightHead :: rightTail))) = _
rw [atLoop]
simp [loop, TM2.stepAux, distinct, update_left, update_right]
private theorem trace_left_empty (left right : K) (distinct : left ≠ right)
(loopLabel : Λ) (equalExit differentExit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet K) Λ (State σ))
(atLoop : program loopLabel = loop left right loopLabel equalExit differentExit)
(base : K → List Bool) (rightWord : List Bool) (ambient : σ) (flag : Bool)
(leftRegister rightRegister : Option Bool) :
(MachineComposition.advance (TM2.step program))^[rightWord.length + 1]
(some ⟨some loopLabel, (ambient, flag, leftRegister, rightRegister),
tapesAt left right base [] rightWord⟩) =
some ⟨if mismatch flag [] rightWord then differentExit else equalExit,
(ambient, false, none, none), tapesAt left right base [] []⟩ := by
induction rightWord generalizing flag leftRegister rightRegister with
| nil =>
simpa only [List.length_nil, Nat.zero_add, Function.iterate_one,
MachineComposition.advance_some, mismatch_nil_nil] using
step_empty left right distinct loopLabel equalExit differentExit program atLoop base
ambient flag leftRegister rightRegister
| cons head tail ih =>
rw [List.length_cons, Function.iterate_succ_apply]
change (MachineComposition.advance (TM2.step program))^[tail.length + 1]
(TM2.step program ⟨some loopLabel, (ambient, flag, leftRegister, rightRegister),
tapesAt left right base [] (head :: tail)⟩) = _
rw [step_nil_cons left right distinct loopLabel equalExit differentExit program atLoop]
simpa only [mismatch_true, mismatch_nil_cons] using ih true none none
/-- The exact trace with a possibly pre-existing mismatch. Every input symbol
is consumed even when disagreement has already been detected. -/
theorem compareTrace_flag (left right : K) (distinct : left ≠ right)
(loopLabel : Λ) (equalExit differentExit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet K) Λ (State σ))
(atLoop : program loopLabel = loop left right loopLabel equalExit differentExit)
(base : K → List Bool) (leftWord rightWord : List Bool) (ambient : σ) (flag : Bool)
(leftRegister rightRegister : Option Bool) :
(MachineComposition.advance (TM2.step program))^[max leftWord.length rightWord.length + 1]
(some ⟨some loopLabel, (ambient, flag, leftRegister, rightRegister),
tapesAt left right base leftWord rightWord⟩) =
some ⟨if mismatch flag leftWord rightWord then differentExit else equalExit,
(ambient, false, none, none), tapesAt left right base [] []⟩ := by
induction leftWord generalizing rightWord flag leftRegister rightRegister with
| nil =>
simpa only [List.length_nil, Nat.zero_max] using
trace_left_empty left right distinct loopLabel equalExit differentExit program atLoop
base rightWord ambient flag leftRegister rightRegister
| cons head tail ih =>
cases rightWord with
| nil =>
rw [List.length_cons, List.length_nil, Nat.max_zero, Function.iterate_succ_apply]
change (MachineComposition.advance (TM2.step program))^[tail.length + 1]
(TM2.step program ⟨some loopLabel, (ambient, flag, leftRegister, rightRegister),
tapesAt left right base (head :: tail) []⟩) = _
rw [step_cons_nil left right distinct loopLabel equalExit differentExit program atLoop]
simpa only [List.length_nil, Nat.max_zero, mismatch_true, mismatch_cons_nil] using
ih [] true none none
| cons rightHead rightTail =>
rw [List.length_cons, List.length_cons, Nat.succ_max_succ, Function.iterate_succ_apply]
change (MachineComposition.advance (TM2.step program))^[max tail.length rightTail.length + 1]
(TM2.step program ⟨some loopLabel, (ambient, flag, leftRegister, rightRegister),
tapesAt left right base (head :: tail) (rightHead :: rightTail)⟩) = _
rw [step_cons_cons left right distinct loopLabel equalExit differentExit program atLoop]
simpa only [mismatch_cons] using
ih rightTail (flag || decide (head ≠ rightHead)) none none
/-- With a clear initial flag, the final continuation tests equality of the
two complete original words. Both tapes and all comparison registers are empty. -/
theorem compareTrace (left right : K) (distinct : left ≠ right)
(loopLabel : Λ) (equalExit differentExit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet K) Λ (State σ))
(atLoop : program loopLabel = loop left right loopLabel equalExit differentExit)
(base : K → List Bool) (leftWord rightWord : List Bool) (ambient : σ)
(leftRegister rightRegister : Option Bool) :
(MachineComposition.advance (TM2.step program))^[max leftWord.length rightWord.length + 1]
(some ⟨some loopLabel, (ambient, false, leftRegister, rightRegister),
tapesAt left right base leftWord rightWord⟩) =
some ⟨if leftWord = rightWord then equalExit else differentExit,
(ambient, false, none, none), tapesAt left right base [] []⟩ := by
have h := compareTrace_flag left right distinct loopLabel equalExit differentExit program
atLoop base leftWord rightWord ambient false leftRegister rightRegister
by_cases heq : leftWord = rightWord <;> simpa [mismatch, heq] using h
theorem compareTrace_fromTapes (left right : K) (distinct : left ≠ right)
(loopLabel : Λ) (equalExit differentExit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet K) Λ (State σ))
(atLoop : program loopLabel = loop left right loopLabel equalExit differentExit)
(base : K → List Bool) (ambient : σ) (leftRegister rightRegister : Option Bool) :
(MachineComposition.advance (TM2.step program))^[max (base left).length (base right).length + 1]
(some ⟨some loopLabel, (ambient, false, leftRegister, rightRegister), base⟩) =
some ⟨if base left = base right then equalExit else differentExit,
(ambient, false, none, none), tapesAt left right base [] []⟩ := by
simpa only [tapesAt_self] using
compareTrace left right distinct loopLabel equalExit differentExit program atLoop base
(base left) (base right) ambient leftRegister rightRegister
/-- The final tape family agrees with the original at every unselected tape. -/
theorem drained_other (left right k : K) (notLeft : k ≠ left) (notRight : k ≠ right)
(base : K → List Bool) : tapesAt left right base [] [] k = base k :=
tapesAt_other left right k notLeft notRight base [] []
/-- An actual timed execution witness, with the exact stored transition count. -/
def compareInTime (left right : K) (distinct : left ≠ right)
(loopLabel : Λ) (equalExit differentExit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet K) Λ (State σ))
(atLoop : program loopLabel = loop left right loopLabel equalExit differentExit)
(base : K → List Bool) (ambient : σ) (leftRegister rightRegister : Option Bool) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some loopLabel, (ambient, false, leftRegister, rightRegister), base⟩
(some ⟨if base left = base right then equalExit else differentExit,
(ambient, false, none, none), tapesAt left right base [] []⟩)
(max (base left).length (base right).length + 1) where
steps := max (base left).length (base right).length + 1
evals_in_steps := compareTrace_fromTapes left right distinct loopLabel equalExit differentExit
program atLoop base ambient leftRegister rightRegister
steps_le_m := Nat.le_refl _
@[simp] theorem compareInTime_steps (left right : K) (distinct : left ≠ right)
(loopLabel : Λ) (equalExit differentExit : Option Λ)
(program : Λ → TM2.Stmt (Alphabet K) Λ (State σ))
(atLoop : program loopLabel = loop left right loopLabel equalExit differentExit)
(base : K → List Bool) (ambient : σ) (leftRegister rightRegister : Option Bool) :
(compareInTime left right distinct loopLabel equalExit differentExit program atLoop base
ambient leftRegister rightRegister).steps =
max (base left).length (base right).length + 1 := rfl
end UniqueGamesTheorem.Foundations.Complexity.MachineCompare
end
end OAI
|