File size: 15,951 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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | import Mathlib.Tactic.Linarith
import OAI.Computability.UniqueGames.Machines.MachineBinaryNameCompare
namespace OAI
section
/-!
First-occurrence search over signed, framed binary names. The stream has no
clause markers. Every iteration discards one sign, scans and compares the
actual candidate payload, and increments a Boolean unary counter only after a
mismatch. The preserved key is a reversed payload, never a unary name.
The six explicitly distinct tapes are: stream, preserved key, candidate,
copied key, copy scratch, and unary occurrence counter. Successful search
consumes precisely the prefix through the first matching name, preserves the
unread suffix and all ambient tapes, and returns the three work tapes empty.
The time theorem follows from literal instruction equations and checked
comparison executions, with no supplied loop-body trace premise.
-/
namespace UniqueGamesTheorem.BinaryNameSearch
open Turing
open UniqueGamesTheorem.Foundations.Complexity
open MachineComposition
open UniqueGamesTheorem.Reduction.MachineTransfer
abbrev Token := Bool × List Bool
def tokenBits (token : Token) : List Bool :=
token.1 :: BinaryNameMachine.frame token.2
def stream : List Token → List Bool
| [] => []
| token :: tokens => tokenBits token ++ stream tokens
def payloads (tokens : List Token) : List (List Bool) := tokens.map Prod.snd
def afterMatch : List Token → List Bool → List Token
| [], _ => []
| token :: tokens, key => if token.2 = key then tokens else afterMatch tokens key
def steps : List Token → List Bool → Nat
| [], _ => 0
| token :: tokens, key =>
1 + BinaryNameCompare.steps token.2.length key.length +
if token.2 = key then 0 else 1 + steps tokens key
def payloadSize (tokens : List Token) : Nat := (payloads tokens |>.map List.length).sum
@[simp] theorem stream_nil : stream [] = [] := rfl
@[simp] theorem stream_cons (token : Token) (tokens : List Token) :
stream (token :: tokens) = tokenBits token ++ stream tokens := rfl
@[simp] theorem payloads_nil : payloads [] = [] := rfl
@[simp] theorem payloads_cons (token : Token) (tokens : List Token) :
payloads (token :: tokens) = token.2 :: payloads tokens := rfl
@[simp] theorem payloadSize_nil : payloadSize [] = 0 := rfl
@[simp] theorem payloadSize_cons (token : Token) (tokens : List Token) :
payloadSize (token :: tokens) = token.2.length + payloadSize tokens := rfl
@[simp] theorem stream_length (tokens : List Token) :
(stream tokens).length = 2 * payloadSize tokens + 2 * tokens.length := by
induction tokens with
| nil => rfl
| cons token tokens ih =>
simp only [stream_cons, List.length_append, tokenBits, List.length_cons,
BinaryNameMachine.frame_length, ih, payloadSize_cons]
omega
/-- Work is linear in the scanned payloads plus one key-copy cost per token. -/
theorem steps_le (tokens : List Token) (key : List Bool) :
steps tokens key ≤ 2 * payloadSize tokens + (3 * key.length + 6) * tokens.length := by
induction tokens with
| nil => simp [steps]
| cons token tokens ih =>
have h := BinaryNameCompare.steps_le token.2.length key.length
simp only [steps, payloadSize_cons, List.length_cons]
split <;> nlinarith
/-- This bound uses lengths of actual encoded words, not decoded name values. -/
theorem steps_le_stream (tokens : List Token) (key : List Bool) :
steps tokens key ≤ (3 * key.length + 7) * (stream tokens).length := by
have h := steps_le tokens key
rw [stream_length]
nlinarith
section Program
variable {K Λ A : Type} [DecidableEq K]
abbrev Alphabet (_ : K) := Bool
abbrev State (A : Type) := BinaryNameCompare.State A
abbrev clean (ambient : A) : State A := BinaryNameCompare.clean ambient
inductive Label
| sign
| comparison (label : BinaryNameCompare.Label)
| increment
deriving DecidableEq, Fintype
def compareTapes (tape : Fin 6 → K) : Fin 5 → K := fun i => tape i.castSucc
omit [DecidableEq K] in
theorem compareTapes_injective (tape : Fin 6 → K) (distinct : Function.Injective tape) :
Function.Injective (compareTapes tape) := by
intro i j h
apply Fin.ext
exact congrArg (fun i : Fin 6 => i.val) (distinct h)
def exitAt (exit : Option Λ) : TM2.Stmt (Alphabet (K := K)) Λ (State A) :=
match exit with
| none => .halt
| some label => .goto fun _ => label
def finish (exit : Option Λ) : TM2.Stmt (Alphabet (K := K)) Λ (State A) :=
.load (fun state => clean state.1.1) (exitAt exit)
def instruction (tape : Fin 6 → K) (labels : Label → Λ)
(foundExit missingExit malformedExit : Option Λ) :
Label → TM2.Stmt (Alphabet (K := K)) Λ (State A)
| .sign =>
.pop (tape 0) (fun state head => (state.1, head))
(.branch (fun state => state.2.isSome)
(finish (some (labels (.comparison .scan)))) (finish missingExit))
| .comparison l => BinaryNameCompare.instruction (compareTapes tape)
(fun l => labels (.comparison l)) foundExit (some (labels .increment)) malformedExit l
| .increment => .push (tape 5) (fun _ => true) (.goto fun _ => labels .sign)
@[simp] theorem stepAux_finish (exit : Option Λ) (state : State A) (base : K → List Bool) :
TM2.stepAux (finish exit) state base = ⟨exit, clean state.1.1, base⟩ := by
cases exit <;> rfl
private theorem update_source (source counter : K) (distinct : source ≠ counter)
(base : K → List Bool) (input count replacement : List Bool) :
Function.update (tapesAt source counter base input count) source replacement =
tapesAt source counter base replacement count := by
funext k
by_cases hs : k = source
· subst k; simp [tapesAt, distinct]
· by_cases hc : k = counter
· subst k; simp [tapesAt, Ne.symm distinct]
· simp [tapesAt, hs, hc]
private theorem update_counter (source counter : K)
(base : K → List Bool) (input count replacement : List Bool) :
Function.update (tapesAt source counter base input count) counter replacement =
tapesAt source counter base input replacement := by
simp [tapesAt]
private theorem joinTrace {X : Type*} {f : X → X} {a b c : X} {n m : Nat}
(first : f^[n] a = b) (second : f^[m] b = c) : f^[n + m] a = c := by
rw [Nat.add_comm, Function.iterate_add_apply, first, second]
variable (tape : Fin 6 → K) (distinct : Function.Injective tape)
variable (labels : Label → Λ) (foundExit missingExit malformedExit : Option Λ)
variable (program : Λ → TM2.Stmt (Alphabet (K := K)) Λ (State A))
variable (atLabels : ∀ l, program (labels l) =
instruction tape labels foundExit missingExit malformedExit l)
variable (base : K → List Bool) (ambient : A)
include distinct atLabels
theorem signStep (sign : Bool) (input counter : List Bool) :
TM2.step program
⟨some (labels .sign), clean ambient,
tapesAt (tape 0) (tape 5) base (sign :: input) counter⟩ =
some ⟨some (labels (.comparison .scan)), clean ambient,
tapesAt (tape 0) (tape 5) base input counter⟩ := by
have hd : tape 0 ≠ tape 5 := fun h => (by decide : (0 : Fin 6) ≠ 5) (distinct h)
change some (TM2.stepAux (program (labels .sign)) _ _) = _
rw [atLabels .sign]
simp [instruction, TM2.stepAux, clean, BinaryNameCompare.clean, hd, update_source]
theorem emptyStep (counter : List Bool) :
TM2.step program
⟨some (labels .sign), clean ambient,
tapesAt (tape 0) (tape 5) base [] counter⟩ =
some ⟨missingExit, clean ambient, tapesAt (tape 0) (tape 5) base [] counter⟩ := by
have hd : tape 0 ≠ tape 5 := fun h => (by decide : (0 : Fin 6) ≠ 5) (distinct h)
change some (TM2.stepAux (program (labels .sign)) _ _) = _
rw [atLabels .sign]
simp [instruction, TM2.stepAux, clean, BinaryNameCompare.clean, hd, update_source]
omit distinct in
theorem incrementStep (input counter : List Bool) :
TM2.step program
⟨some (labels .increment), clean ambient,
tapesAt (tape 0) (tape 5) base input counter⟩ =
some ⟨some (labels .sign), clean ambient,
tapesAt (tape 0) (tape 5) base input (true :: counter)⟩ := by
change some (TM2.stepAux (program (labels .increment)) _ _) = _
rw [atLabels .increment]
simp [instruction, TM2.stepAux, update_counter]
/-- One sign is consumed, then the actual shared scan/copy/compare routine runs. -/
theorem tokenTrace (token : Token) (key suffix counter : List Bool)
(canonical : BinaryNameMachine.canonical token.2 = true)
(keyWord : base (tape 1) = key.reverse)
(candidateEmpty : base (tape 2) = []) (copyEmpty : base (tape 3) = [])
(scratchEmpty : base (tape 4) = []) :
(advance (TM2.step program))^[1 + BinaryNameCompare.steps token.2.length key.length]
(some ⟨some (labels .sign), clean ambient,
tapesAt (tape 0) (tape 5) base (tokenBits token ++ suffix) counter⟩) =
some ⟨if token.2 = key then foundExit else some (labels .increment), clean ambient,
tapesAt (tape 0) (tape 5) base suffix counter⟩ := by
have hd (i j : Fin 6) (hne : i ≠ j) : tape i ≠ tape j := fun h => hne (distinct h)
let initial := tapesAt (tape 0) (tape 5) base
(BinaryNameMachine.frame token.2 ++ suffix) counter
have first : (advance (TM2.step program))^[1]
(some ⟨some (labels .sign), clean ambient,
tapesAt (tape 0) (tape 5) base (tokenBits token ++ suffix) counter⟩) =
some ⟨some (labels (.comparison .scan)), clean ambient, initial⟩ := by
simpa only [Function.iterate_one, advance_some, tokenBits, List.cons_append] using
signStep tape distinct labels foundExit missingExit malformedExit program atLabels
base ambient token.1 (BinaryNameMachine.frame token.2 ++ suffix) counter
have second := BinaryNameCompare.compareTrace (compareTapes tape)
(compareTapes_injective tape distinct) (fun l => labels (.comparison l))
foundExit (some (labels .increment)) malformedExit program
(fun l => atLabels (.comparison l)) initial token.2 suffix key.reverse canonical
(by simp [initial, compareTapes, hd 0 5 (by decide)])
(by simpa [initial, compareTapes, tapesAt,
hd 1 0 (by decide), hd 1 5 (by decide)] using keyWord)
(by simpa [initial, compareTapes, tapesAt, hd 2 0 (by decide), hd 2 5 (by decide)]
using candidateEmpty)
(by simpa [initial, compareTapes, tapesAt,
hd 3 0 (by decide), hd 3 5 (by decide)] using copyEmpty)
(by simpa [initial, compareTapes, tapesAt,
hd 4 0 (by decide), hd 4 5 (by decide)] using scratchEmpty)
ambient
have heq : token.2.reverse = key.reverse ↔ token.2 = key := by
constructor
· intro h
have := congrArg List.reverse h
simpa only [List.reverse_reverse] using this
· intro h; rw [h]
simp only [List.length_reverse, heq] at second
rw [show Function.update initial (compareTapes tape 0) suffix =
tapesAt (tape 0) (tape 5) base suffix counter by
exact update_source _ _ (hd 0 5 (by decide)) base _ counter suffix] at second
exact joinTrace first second
/-- Successful search returns exactly the first occurrence index. All labels
are literal instruction equations, so this theorem supplies its own body runs. -/
theorem searchTrace (tokens : List Token) (key suffix counter : List Bool)
(canonical : ∀ token ∈ tokens, BinaryNameMachine.canonical token.2 = true)
(present : key ∈ payloads tokens)
(keyWord : base (tape 1) = key.reverse)
(candidateEmpty : base (tape 2) = []) (copyEmpty : base (tape 3) = [])
(scratchEmpty : base (tape 4) = []) :
(advance (TM2.step program))^[steps tokens key]
(some ⟨some (labels .sign), clean ambient,
tapesAt (tape 0) (tape 5) base (stream tokens ++ suffix) counter⟩) =
some ⟨foundExit, clean ambient,
tapesAt (tape 0) (tape 5) base (stream (afterMatch tokens key) ++ suffix)
(List.replicate ((payloads tokens).idxOf key) true ++ counter)⟩ := by
induction tokens generalizing counter with
| nil => simp at present
| cons token tokens ih =>
have headCanonical := canonical token (by simp)
have tailCanonical : ∀ t ∈ tokens, BinaryNameMachine.canonical t.2 = true :=
fun t ht => canonical t (by simp [ht])
have first := tokenTrace tape distinct labels foundExit missingExit malformedExit
program atLabels base ambient token key (stream tokens ++ suffix) counter
headCanonical keyWord candidateEmpty copyEmpty scratchEmpty
by_cases same : token.2 = key
· simpa only [steps, same, ite_true, Nat.add_zero, stream_cons,
List.append_assoc, afterMatch, payloads_cons, List.idxOf_cons_self,
List.replicate_zero, List.nil_append] using first
· have remaining : key ∈ payloads tokens := by
rcases List.mem_cons.mp present with equal | remaining
· exact False.elim (same equal.symm)
· exact remaining
have increment : (advance (TM2.step program))^[1]
(some ⟨some (labels .increment), clean ambient,
tapesAt (tape 0) (tape 5) base (stream tokens ++ suffix) counter⟩) =
some ⟨some (labels .sign), clean ambient,
tapesAt (tape 0) (tape 5) base (stream tokens ++ suffix) (true :: counter)⟩ := by
simpa only [Function.iterate_one, advance_some] using
incrementStep tape labels foundExit missingExit malformedExit program
atLabels base ambient (stream tokens ++ suffix) counter
have rest := ih (true :: counter) tailCanonical remaining
simp only [same, ite_false] at first
have full := joinTrace first (joinTrace increment rest)
have counterEq : List.replicate ((payloads tokens).idxOf key) true ++
(true :: counter) =
List.replicate ((payloads tokens).idxOf key + 1) true ++ counter := by
rw [List.replicate_succ']
simp only [List.append_assoc, List.singleton_append]
have indexEq : (token.2 :: payloads tokens).idxOf key =
(payloads tokens).idxOf key + 1 := by
exact List.idxOf_cons_ne (payloads tokens) same
simpa only [steps, same, ite_false, stream_cons, List.append_assoc, afterMatch,
payloads_cons, indexEq, counterEq] using full
/-- Explicit polynomial bound for the actual successful run. -/
def searchInTime (tokens : List Token) (key suffix counter : List Bool)
(canonical : ∀ token ∈ tokens, BinaryNameMachine.canonical token.2 = true)
(present : key ∈ payloads tokens)
(keyWord : base (tape 1) = key.reverse)
(candidateEmpty : base (tape 2) = []) (copyEmpty : base (tape 3) = [])
(scratchEmpty : base (tape 4) = []) :
StateTransition.EvalsToInTime (TM2.step program)
⟨some (labels .sign), clean ambient,
tapesAt (tape 0) (tape 5) base (stream tokens ++ suffix) counter⟩
(some ⟨foundExit, clean ambient,
tapesAt (tape 0) (tape 5) base (stream (afterMatch tokens key) ++ suffix)
(List.replicate ((payloads tokens).idxOf key) true ++ counter)⟩)
((3 * key.length + 7) * (stream tokens).length) where
steps := steps tokens key
evals_in_steps := searchTrace tape distinct labels foundExit missingExit malformedExit
program atLabels base ambient tokens key suffix counter canonical present keyWord
candidateEmpty copyEmpty scratchEmpty
steps_le_m := steps_le_stream tokens key
end Program
/-- Concrete finite-control instantiation; the counter is its designated output. -/
def machine : FinTM2 where
K := Fin 6
k₀ := 0
k₁ := 5
Γ _ := Bool
Λ := Label ⊕ Fin 3
main := .inl .sign
σ := State Unit
initialState := clean ()
m label := match label with
| .inl l => instruction id Sum.inl (some (.inr 0)) (some (.inr 1)) (some (.inr 2)) l
| .inr _ => .halt
theorem machine_finiteAlphabet (k : machine.K) : Finite (machine.Γ k) := by
change Finite Bool
infer_instance
end UniqueGamesTheorem.BinaryNameSearch
end
end OAI
|