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