File size: 34,228 Bytes
ea76aac | 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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 | import Mathlib
import OAI.Computability.DirectedFeedback.Machines.MachineSubdivisionLoop
namespace OAI
namespace DFVSGames.Foundations.PCP.NameCompaction
open DFVSGames.Foundations.Target
def clauseNames {n : Nat} (c : Clause n) : List (Fin n) :=
[(c)[0].variableIndex, (c)[1].variableIndex, (c)[2].variableIndex]
def sourceNames (F : Formula) : List (Fin F.«variables») :=
F.clauses.flatMap clauseNames
def activeNames (F : Formula) : List (Fin F.«variables») :=
(sourceNames F).eraseDups
theorem literal_mem_active (F : Formula) (c : Clause F.«variables»)
(hc : c ∈ F.clauses) (i : Fin 3) :
(c)[i].variableIndex ∈ activeNames F := by
simp only [activeNames, List.mem_eraseDups]
apply List.mem_flatMap.mpr
refine ⟨c, hc, ?_⟩
have hi : i = 0 ∨ i = 1 ∨ i = 2 := by omega
rcases hi with rfl | rfl | rfl <;> simp [clauseNames]
def compactIndex (F : Formula) (v : Fin F.«variables»)
(hv : v ∈ activeNames F) : Fin (activeNames F).length :=
⟨(activeNames F).idxOf v, List.idxOf_lt_length_iff.mpr hv⟩
def decodeName (F : Formula) (v : Fin (activeNames F).length) : Fin F.«variables» :=
(activeNames F)[v.val]
def compactLiteral (F : Formula) (l : Literal F.«variables»)
(hl : l.variableIndex ∈ activeNames F) : Literal (activeNames F).length :=
⟨compactIndex F l.variableIndex hl, l.positive⟩
def compactClause (F : Formula) (c : Clause F.«variables»)
(hc : c ∈ F.clauses) : Clause (activeNames F).length :=
#v[compactLiteral F (c)[0] (literal_mem_active F c hc 0),
compactLiteral F (c)[1] (literal_mem_active F c hc 1),
compactLiteral F (c)[2] (literal_mem_active F c hc 2)]
def compactClauses (F : Formula) : List (Clause (activeNames F).length) :=
F.clauses.attach.map (fun c => compactClause F c.val c.property)
def compact (F : Formula) : Formula where
«variables» := (activeNames F).length
clauses := compactClauses F
def restrictAssignment (F : Formula) (A : Fin F.«variables» → Bool) :
Fin (activeNames F).length → Bool := fun v => A (decodeName F v)
def extendAssignment (F : Formula) (B : Fin (activeNames F).length → Bool)
(v : Fin F.«variables») : Bool :=
if hv : v ∈ activeNames F then B (compactIndex F v hv) else false
@[simp] theorem decode_compactIndex (F : Formula) (v : Fin F.«variables»)
(hv : v ∈ activeNames F) : decodeName F (compactIndex F v hv) = v := by
exact List.getElem_idxOf (List.idxOf_lt_length_iff.mpr hv)
@[simp] theorem eval_compact_restrict (F : Formula) (c : Clause F.«variables»)
(hc : c ∈ F.clauses) (A : Fin F.«variables» → Bool) :
(compactClause F c hc).eval (restrictAssignment F A) = c.eval A := by
simp [compactClause, compactLiteral, Clause.eval, Literal.eval,
restrictAssignment, decode_compactIndex]
@[simp] theorem eval_compact_extend (F : Formula) (c : Clause F.«variables»)
(hc : c ∈ F.clauses) (B : Fin (activeNames F).length → Bool) :
(compactClause F c hc).eval B = c.eval (extendAssignment F B) := by
have h₀ : (c)[0].variableIndex ∈ activeNames F := literal_mem_active F c hc 0
have h₁ : (c)[1].variableIndex ∈ activeNames F := literal_mem_active F c hc 1
have h₂ : (c)[2].variableIndex ∈ activeNames F := literal_mem_active F c hc 2
simp [compactClause, compactLiteral, Clause.eval, Literal.eval, extendAssignment,
h₀, h₁, h₂]
rfl
theorem map_attach_val {α β : Type} (xs : List α) (f : α → β) :
xs.attach.map (fun x => f x.val) = xs.map f := by
exact List.attach_map_val
def evaluationList (F : Formula) (A : Fin F.«variables» → Bool) : List Bool :=
F.clauses.map (fun c => c.eval A)
def satisfiedCount (F : Formula) (A : Fin F.«variables» → Bool) : Nat :=
(evaluationList F A).count true
def failedCount (F : Formula) (A : Fin F.«variables» → Bool) : Nat :=
(evaluationList F A).count false
theorem evaluationList_restrict (F : Formula) (A : Fin F.«variables» → Bool) :
evaluationList (compact F) (restrictAssignment F A) = evaluationList F A := by
simp only [evaluationList, compact, compactClauses, List.map_map]
simpa only [Function.comp_def, eval_compact_restrict] using
map_attach_val F.clauses (fun c => c.eval A)
theorem evaluationList_extend (F : Formula) (B : Fin (compact F).«variables» → Bool) :
evaluationList (compact F) B = evaluationList F (extendAssignment F B) := by
simp only [evaluationList, compact, compactClauses, List.map_map]
change F.clauses.attach.map (fun c => (compactClause F c.val c.property).eval B) = _
calc
_ = F.clauses.attach.map (fun c => c.val.eval (extendAssignment F B)) := by
apply List.map_congr_left
intro c _
exact eval_compact_extend F c.val c.property B
_ = _ := map_attach_val F.clauses (fun c => c.eval (extendAssignment F B))
theorem satisfiedCount_restrict (F : Formula) (A : Fin F.«variables» → Bool) :
satisfiedCount (compact F) (restrictAssignment F A) = satisfiedCount F A := by
simp only [satisfiedCount, evaluationList_restrict]
theorem failedCount_restrict (F : Formula) (A : Fin F.«variables» → Bool) :
failedCount (compact F) (restrictAssignment F A) = failedCount F A := by
simp only [failedCount, evaluationList_restrict]
theorem satisfiedCount_extend (F : Formula) (B : Fin (compact F).«variables» → Bool) :
satisfiedCount (compact F) B = satisfiedCount F (extendAssignment F B) := by
simp only [satisfiedCount, evaluationList_extend]
theorem failedCount_extend (F : Formula) (B : Fin (compact F).«variables» → Bool) :
failedCount (compact F) B = failedCount F (extendAssignment F B) := by
simp only [failedCount, evaluationList_extend]
theorem clauseNames_flat_length {n : Nat} (cs : List (Clause n)) :
(cs.flatMap clauseNames).length = 3 * cs.length := by
induction cs with
| nil => simp
| cons c cs ih =>
simp only [List.flatMap_cons, List.length_append, clauseNames,
List.length_cons, List.length_nil, ih]
omega
@[simp] theorem compact_clause_count (F : Formula) :
(compact F).clauses.length = F.clauses.length := by
simp [compact, compactClauses]
theorem length_eraseDups_le {α : Type} [BEq α] (xs : List α) :
xs.eraseDups.length ≤ xs.length := by
match xs with
| [] => simp
| a :: rest =>
rw [List.eraseDups_cons]
have filtered := List.length_filter_le (fun b => !(b == a)) rest
have ih := length_eraseDups_le (rest.filter (fun b => !(b == a)))
simp only [List.length_cons]
omega
termination_by xs.length
decreasing_by
exact Nat.lt_succ_of_le (List.length_filter_le _ _)
theorem compact_active_bound (F : Formula) :
(compact F).«variables» ≤ 3 * F.clauses.length := by
have h := length_eraseDups_le (sourceNames F)
simpa only [sourceNames, clauseNames_flat_length, compact, activeNames] using h
theorem compact_completeness (F : Formula) (hs : F.Satisfiable) :
(compact F).Satisfiable := by
rcases hs with ⟨A, hA⟩
refine ⟨restrictAssignment F A, ?_⟩
intro d hd
change d ∈ F.clauses.attach.map (fun c => compactClause F c.val c.property) at hd
rcases List.mem_map.mp hd with ⟨c, _, rfl⟩
exact (eval_compact_restrict F c.val c.property A).trans (hA c.val c.property)
theorem compact_reflects (F : Formula) (hs : (compact F).Satisfiable) :
F.Satisfiable := by
rcases hs with ⟨B, hB⟩
refine ⟨extendAssignment F B, ?_⟩
intro c hc
have hm : compactClause F c hc ∈ (compact F).clauses := by
change _ ∈ F.clauses.attach.map (fun d => compactClause F d.val d.property)
exact List.mem_map.mpr ⟨⟨c, hc⟩, by simp, rfl⟩
rw [← eval_compact_extend F c hc B]
exact hB _ hm
theorem compact_satisfiable_iff (F : Formula) :
(compact F).Satisfiable ↔ F.Satisfiable :=
⟨compact_reflects F, compact_completeness F⟩
theorem compact_preserves_gap (F : Formula) (a b : Nat)
(gap : ∀ A, a * F.clauses.length ≤ b * failedCount F A)
(B : Fin (compact F).«variables» → Bool) :
a * (compact F).clauses.length ≤ b * failedCount (compact F) B := by
rw [compact_clause_count, failedCount_extend]
exact gap (extendAssignment F B)
theorem compact_encoding_bound (F : Formula) :
(Complexity.formulaBits (compact F)).length ≤
9 * F.clauses.length * F.clauses.length + 10 * F.clauses.length + 2 := by
have enc := Complexity.formulaBits_length_le (compact F)
rw [compact_clause_count] at enc
have active := compact_active_bound F
have h₁ := Nat.add_le_add_right active (F.clauses.length + 2)
have h₂ := Nat.mul_le_mul_left F.clauses.length
(Nat.mul_le_mul_left 3 (Nat.add_le_add_right active 2))
have total := Nat.add_le_add h₁ h₂
have polynomial : 3 * F.clauses.length + (F.clauses.length + 2) +
F.clauses.length * (3 * (3 * F.clauses.length + 2)) =
9 * F.clauses.length * F.clauses.length + 10 * F.clauses.length + 2 := by
simp [Nat.mul_add, Nat.mul_comm, Nat.mul_left_comm]
omega
rw [polynomial] at total
omega
theorem failureCount_evaluations (F : Formula) (A : Fin F.«variables» → Bool)
(indices : List (Fin F.clauses.length)) :
failureCount F A indices =
(indices.map (fun i => (clauseAt F i).eval A)).count false := by
induction indices with
| nil => simp [failureCount]
| cons i rest ih =>
cases h : (clauseAt F i).eval A <;>
simp [failureCount, clauseFailure, h, ih, Nat.add_comm]
theorem evaluations_allIndices (F : Formula) (A : Fin F.«variables» → Bool) :
((allIndices F).map (fun i => (clauseAt F i).eval A)) = evaluationList F A := by
calc
_ = (List.ofFn (fun i : Fin F.clauses.length => F.clauses[i.val])).map
(fun c => c.eval A) := by
simp only [allIndices, List.finRange, clauseAt, List.map_ofFn, Function.comp_def]
_ = _ := by rw [List.ofFn_getElem]; rfl
theorem verifier_failureCount (F : Formula) (A : Fin F.«variables» → Bool) :
failureCount F A (allIndices F) = failedCount F A := by
rw [failureCount_evaluations, evaluations_allIndices]
rfl
theorem compact_verifier_soundness (F : Formula) (a b : Nat)
(gap : ∀ A, a * F.clauses.length ≤ b * failedCount F A)
(alice : AliceStrategy (compact F)) (bob : BobStrategy (compact F)) :
b * ((allEvents (compact F)).filter (accepts (compact F) alice bob)).length +
a * F.clauses.length ≤ b * (3 * F.clauses.length) := by
have sourceGap : ∀ A : BobStrategy (compact F),
a * (compact F).clauses.length ≤
b * failureCount (compact F) A (allIndices (compact F)) := by
intro A
rw [verifier_failureCount]
exact compact_preserves_gap F a b gap A
have h := verifier_event_soundness (compact F) a b sourceGap alice bob
simpa only [length_allEvents, compact_clause_count] using h
end DFVSGames.Foundations.PCP.NameCompaction
namespace DFVSGames.BinaryFormula
open DFVSGames.Foundations
def sourceNames (F : Formula) : List Nat :=
F.clauses.flatMap clauseNames
def activeNames (F : Formula) : List Nat :=
(sourceNames F).eraseDups
theorem literal_mem_active (F : Formula) (c : Clause)
(hc : c ∈ F.clauses) (i : Fin 3) : (c)[i].name ∈ activeNames F := by
simp only [activeNames, List.mem_eraseDups]
apply List.mem_flatMap.mpr
refine ⟨c, hc, ?_⟩
have hi : i = 0 ∨ i = 1 ∨ i = 2 := by omega
rcases hi with rfl | rfl | rfl <;> simp [clauseNames]
def compactIndex (F : Formula) (v : Nat)
(hv : v ∈ activeNames F) : Fin (activeNames F).length :=
⟨(activeNames F).idxOf v, List.idxOf_lt_length_iff.mpr hv⟩
def decodeName (F : Formula) (v : Fin (activeNames F).length) : Nat :=
(activeNames F)[v.val]
def compactLiteral (F : Formula) (l : Literal)
(hl : l.name ∈ activeNames F) : Target.Literal (activeNames F).length :=
⟨compactIndex F l.name hl, l.positive⟩
def compactClause (F : Formula) (c : Clause)
(hc : c ∈ F.clauses) : Target.Clause (activeNames F).length :=
#v[compactLiteral F (c)[0] (literal_mem_active F c hc 0),
compactLiteral F (c)[1] (literal_mem_active F c hc 1),
compactLiteral F (c)[2] (literal_mem_active F c hc 2)]
def compactClauses (F : Formula) : List (Target.Clause (activeNames F).length) :=
F.clauses.attach.map (fun c => compactClause F c.val c.property)
def dense (F : Formula) : Target.Formula where
«variables» := (activeNames F).length
clauses := compactClauses F
def restrictAssignment (F : Formula) (A : Nat → Bool) :
Fin (activeNames F).length → Bool := fun v => A (decodeName F v)
def extendAssignment (F : Formula) (B : Fin (activeNames F).length → Bool)
(v : Nat) : Bool :=
if hv : v ∈ activeNames F then B (compactIndex F v hv) else false
@[simp] theorem decode_compactIndex (F : Formula) (v : Nat)
(hv : v ∈ activeNames F) : decodeName F (compactIndex F v hv) = v := by
exact List.getElem_idxOf (List.idxOf_lt_length_iff.mpr hv)
theorem compactIndex_injective (F : Formula) (u v : Nat)
(hu : u ∈ activeNames F) (hv : v ∈ activeNames F)
(h : compactIndex F u hu = compactIndex F v hv) : u = v := by
have decoded := congrArg (decodeName F) h
simpa only [decode_compactIndex] using decoded
@[simp] theorem compactLiteral_positive (F : Formula) (l : Literal)
(hl : l.name ∈ activeNames F) : (compactLiteral F l hl).positive = l.positive := rfl
@[simp] theorem eval_compact_restrict (F : Formula) (c : Clause)
(hc : c ∈ F.clauses) (A : Nat → Bool) :
(compactClause F c hc).eval (restrictAssignment F A) = c.eval A := by
simp [compactClause, compactLiteral, Target.Clause.eval, Target.Literal.eval,
Clause.eval, Literal.eval, restrictAssignment, decode_compactIndex]
@[simp] theorem eval_compact_extend (F : Formula) (c : Clause)
(hc : c ∈ F.clauses) (B : Fin (activeNames F).length → Bool) :
(compactClause F c hc).eval B = c.eval (extendAssignment F B) := by
have h₀ : (c)[0].name ∈ activeNames F := literal_mem_active F c hc 0
have h₁ : (c)[1].name ∈ activeNames F := literal_mem_active F c hc 1
have h₂ : (c)[2].name ∈ activeNames F := literal_mem_active F c hc 2
simp [compactClause, compactLiteral, Target.Clause.eval, Target.Literal.eval,
Clause.eval, Literal.eval, extendAssignment, h₀, h₁, h₂] ; rfl
def evaluationList (F : Formula) (A : Nat → Bool) : List Bool :=
F.clauses.map (fun c => c.eval A)
theorem evaluationList_restrict (F : Formula) (A : Nat → Bool) :
PCP.NameCompaction.evaluationList (dense F) (restrictAssignment F A) =
evaluationList F A := by
simp only [PCP.NameCompaction.evaluationList, evaluationList, dense, compactClauses,
List.map_map]
simpa only [Function.comp_def, eval_compact_restrict] using
PCP.NameCompaction.map_attach_val F.clauses (fun c => c.eval A)
theorem evaluationList_extend (F : Formula) (B : Fin (dense F).«variables» → Bool) :
PCP.NameCompaction.evaluationList (dense F) B =
evaluationList F (extendAssignment F B) := by
simp only [PCP.NameCompaction.evaluationList, evaluationList, dense, compactClauses,
List.map_map]
change F.clauses.attach.map (fun c => (compactClause F c.val c.property).eval B) = _
calc
_ = F.clauses.attach.map (fun c => c.val.eval (extendAssignment F B)) := by
apply List.map_congr_left
intro c _
exact eval_compact_extend F c.val c.property B
_ = _ := PCP.NameCompaction.map_attach_val F.clauses
(fun c => c.eval (extendAssignment F B))
def failedCount (F : Formula) (A : Nat → Bool) : Nat :=
(evaluationList F A).count false
theorem failedCount_restrict (F : Formula) (A : Nat → Bool) :
PCP.NameCompaction.failedCount (dense F) (restrictAssignment F A) = failedCount F A := by
simp only [PCP.NameCompaction.failedCount, failedCount, evaluationList_restrict]
theorem failedCount_extend (F : Formula) (B : Fin (dense F).«variables» → Bool) :
PCP.NameCompaction.failedCount (dense F) B = failedCount F (extendAssignment F B) := by
simp only [PCP.NameCompaction.failedCount, failedCount, evaluationList_extend]
theorem clauseNames_flat_length (cs : List Clause) :
(cs.flatMap clauseNames).length = 3 * cs.length := by
induction cs with
| nil => simp
| cons c cs ih =>
simp only [List.flatMap_cons, List.length_append, clauseNames,
List.length_cons, List.length_nil, ih]
omega
@[simp] theorem dense_clause_count (F : Formula) :
(dense F).clauses.length = F.clauses.length := by
simp [dense, compactClauses]
theorem dense_variable_bound (F : Formula) :
(dense F).«variables» ≤ 3 * F.clauses.length := by
have h := PCP.NameCompaction.length_eraseDups_le (sourceNames F)
simpa only [sourceNames, clauseNames_flat_length, dense, activeNames] using h
theorem dense_completeness (F : Formula) (hs : F.Satisfiable) :
(dense F).Satisfiable := by
rcases hs with ⟨A, hA⟩
refine ⟨restrictAssignment F A, ?_⟩
intro d hd
change d ∈ F.clauses.attach.map (fun c => compactClause F c.val c.property) at hd
rcases List.mem_map.mp hd with ⟨c, _, rfl⟩
exact (eval_compact_restrict F c.val c.property A).trans (hA c.val c.property)
theorem dense_reflects (F : Formula) (hs : (dense F).Satisfiable) : F.Satisfiable := by
rcases hs with ⟨B, hB⟩
refine ⟨extendAssignment F B, ?_⟩
intro c hc
have hm : compactClause F c hc ∈ (dense F).clauses := by
change _ ∈ F.clauses.attach.map (fun d => compactClause F d.val d.property)
exact List.mem_map.mpr ⟨⟨c, hc⟩, by simp, rfl⟩
rw [← eval_compact_extend F c hc B]
exact hB _ hm
theorem dense_satisfiable_iff (F : Formula) :
(dense F).Satisfiable ↔ F.Satisfiable :=
⟨dense_reflects F, dense_completeness F⟩
theorem dense_preserves_gap (F : Formula) (a b : Nat)
(gap : ∀ A, a * F.clauses.length ≤ b * failedCount F A)
(B : Fin (dense F).«variables» → Bool) :
a * (dense F).clauses.length ≤ b * PCP.NameCompaction.failedCount (dense F) B := by
rw [dense_clause_count, failedCount_extend]
exact gap (extendAssignment F B)
theorem dense_encoding_bound (F : Formula) :
(Complexity.formulaBits (dense F)).length ≤
9 * F.clauses.length * F.clauses.length + 10 * F.clauses.length + 2 := by
have enc := Complexity.formulaBits_length_le (dense F)
rw [dense_clause_count] at enc
have active := dense_variable_bound F
have h₁ := Nat.add_le_add_right active (F.clauses.length + 2)
have h₂ := Nat.mul_le_mul_left F.clauses.length
(Nat.mul_le_mul_left 3 (Nat.add_le_add_right active 2))
have total := Nat.add_le_add h₁ h₂
have polynomial : 3 * F.clauses.length + (F.clauses.length + 2) +
F.clauses.length * (3 * (3 * F.clauses.length + 2)) =
9 * F.clauses.length * F.clauses.length + 10 * F.clauses.length + 2 := by
ring
rw [polynomial] at total
omega
end DFVSGames.BinaryFormula
namespace DFVSGames.BinaryEncoding
open BinaryFormula
theorem dense_encoding_bound (formula : Formula) :
(DFVSGames.Foundations.Complexity.formulaBits (dense formula)).length ≤
9 * (formulaBits formula).length * (formulaBits formula).length +
10 * (formulaBits formula).length + 2 := by
have h := BinaryFormula.dense_encoding_bound formula
have hm : formula.clauses.length ≤ (formulaBits formula).length :=
(clauses_length_lt_bits formula.clauses).le
have hsq := Nat.mul_self_le_mul_self hm
nlinarith
end DFVSGames.BinaryEncoding
namespace DFVSGames.Foundations.Complexity.MachineCompare
open Turing
open DFVSGames.Reduction.MachineTransfer
variable {K Λ σ : Type} [DecidableEq K]
abbrev Alphabet (K : Type) : K → Type := fun _ => Bool
abbrev State (σ : Type) := σ × Bool × Option Bool × Option Bool
def exitAt (exit : Option Λ) : TM2.Stmt (Alphabet K) Λ (State σ) :=
match exit with
| none => .halt
| some label => .goto fun _ => label
def finish (exit : Option Λ) : TM2.Stmt (Alphabet K) Λ (State σ) :=
.load (fun state => (state.1, false, none, none)) (exitAt exit)
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))))
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_inline_MachineCompare (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_inline_MachineCompare (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_inline_MachineCompare, update_right_inline_MachineCompare]
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_inline_MachineCompare, update_right_inline_MachineCompare]
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_inline_MachineCompare, update_right_inline_MachineCompare]
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_inline_MachineCompare, update_right_inline_MachineCompare]
private theorem trace_left_empty_inline_MachineCompare (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
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_inline_MachineCompare 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
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
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 [] []
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 DFVSGames.Foundations.Complexity.MachineCompare
end OAI
|