import Mathlib import OAI.Computability.MaxCut.PCP.LoopOrder import OAI.Computability.MaxCut.Machines.RuntimeModel namespace OAI namespace MaxCutGames.Foundations.Hastad.SourceContextClear open Turing Complexity MachineComposition abbrev Tape := SourceContextLoad.Tape variable {u : Nat} {Extra : Type} def fieldAt (i : Fin (u * 6)) : Tape u Extra := .field (finProdFinEquiv.symm i).1 (finProdFinEquiv.symm i).2 def fields : List (Tape u Extra) := List.ofFn fieldAt theorem fieldAt_injective : Function.Injective (fieldAt (u := u) (Extra := Extra)) := by intro i j h have hpair := SourceContextLoad.Tape.field.inj h have hp : finProdFinEquiv.symm i = finProdFinEquiv.symm j := Prod.ext hpair.1 hpair.2 exact finProdFinEquiv.symm.injective hp @[simp] theorem fields_length : (fields (u := u) (Extra := Extra)).length = 6 * u := by simp [fields, Nat.mul_comm] theorem fields_nodup : (fields (u := u) (Extra := Extra)).Nodup := by exact List.nodup_ofFn.mpr fieldAt_injective theorem field_mem (j : Fin u) (s : Fin 6) : (SourceContextLoad.Tape.field j s : Tape u Extra) ∈ fields := by apply List.mem_ofFn.mpr exact ⟨finProdFinEquiv (j, s), by simp [fieldAt]⟩ theorem mem_fields_iff (p : Tape u Extra) : p ∈ fields ↔ ∃ j s, p = .field j s := by constructor · intro h obtain ⟨i, hi⟩ := List.mem_ofFn.mp h exact ⟨(finProdFinEquiv.symm i).1, (finProdFinEquiv.symm i).2, hi.symm⟩ · rintro ⟨j, s, rfl⟩ exact field_mem j s def outputTapes (base : Tape u Extra → List Bool) : Tape u Extra → List Bool | .field _ _ => [] | p => base p @[simp] theorem output_field (base : Tape u Extra → List Bool) (j : Fin u) (s : Fin 6) : outputTapes base (.field j s) = [] := rfl theorem output_frame (base : Tape u Extra → List Bool) (p : Tape u Extra) (hp : ∀ j s, p ≠ .field j s) : outputTapes base p = base p := by cases p <;> simp_all [outputTapes] abbrev Label (u : Nat) (Extra : Type) := MachineDrainMany.Label (fields (u := u) (Extra := Extra)) ⊕ Unit private def entryLabel_inline_SourceContextClear {K : Type} : (chosen : List K) → MachineDrainMany.Label chosen ⊕ Unit | [] => .inr () | _ :: _ => .inl (.inl ()) def main : Label u Extra := entryLabel_inline_SourceContextClear fields variable [DecidableEq Extra] def program : Label u Extra → TM2.Stmt (fun _ : Tape u Extra => Bool) (Label u Extra) (Unit × Option Bool) | .inl l => MachineDrainMany.instruction fields Sum.inl (some (.inr ())) l | .inr _ => .load (fun _ => ((), none)) .halt private theorem entryLabel_eq_inline_SourceContextClear {K : Type} (chosen : List K) : MachineDrainMany.entry chosen (Sum.inl : MachineDrainMany.Label chosen → MachineDrainMany.Label chosen ⊕ Unit) (some (.inr ())) = some (entryLabel_inline_SourceContextClear chosen) := by cases chosen <;> rfl omit [DecidableEq Extra] in private theorem entry_eq_inline_SourceContextClear : MachineDrainMany.entry (fields (u := u) (Extra := Extra)) Sum.inl (some (.inr ())) = some (main (u := u) (Extra := Extra)) := entryLabel_eq_inline_SourceContextClear fields theorem finalTapes_eq (base : Tape u Extra → List Bool) : MachineDrainMany.finalTapes fields base = outputTapes base := by funext p rw [MachineDrainMany.finalTapes_apply] cases p <;> simp [mem_fields_iff, outputTapes] private theorem lengthSum_update_inline_SourceContextClear {K : Type} [DecidableEq K] (chosen : List K) (base : K → List Bool) (source : K) (h : source ∉ chosen) : MachineDrainMany.lengthSum chosen (Function.update base source []) = MachineDrainMany.lengthSum chosen base := by unfold MachineDrainMany.lengthSum congr 1 apply List.map_congr_left intro k hk rw [Function.update_of_ne (by intro he; subst k; exact h hk)] /-- Distinct chosen tapes make the pre-cleanup length sum the exact drain cost. -/ private theorem steps_eq_of_nodup_inline_SourceContextClear {K : Type} [DecidableEq K] (chosen : List K) (h : chosen.Nodup) (base : K → List Bool) : MachineDrainMany.steps chosen base = MachineDrainMany.lengthSum chosen base + chosen.length := by induction chosen generalizing base with | nil => rfl | cons source chosen ih => obtain ⟨hn, hd⟩ := List.nodup_cons.mp h rw [MachineDrainMany.steps, ih hd, lengthSum_update_inline_SourceContextClear chosen base source hn] simp only [MachineDrainMany.lengthSum, List.map_cons, List.sum_cons, List.length_cons] omega private theorem trace_trans_inline_SourceContextClear {α : Type*} (f : α → α) {a b : Nat} {x y z : α} (first : f^[a] x = y) (second : f^[b] y = z) : f^[a + b] x = z := by rw [Nat.add_comm, Function.iterate_add_apply, first, second] theorem clearTrace (base : Tape u Extra → List Bool) (register : Option Bool) : (advance (TM2.step program))^[MachineDrainMany.lengthSum fields base + 6 * u + 1] (some ⟨some main, ((), register), base⟩) = some ⟨none, ((), none), outputTapes base⟩ := by have drain := MachineDrainMany.trace fields Sum.inl (some (.inr ())) program (fun _ => rfl) base () register rw [entry_eq_inline_SourceContextClear, finalTapes_eq, steps_eq_of_nodup_inline_SourceContextClear fields fields_nodup, fields_length] at drain have last : (advance (TM2.step program))^[1] (some ⟨some (.inr ()), ((), MachineDrainMany.finalRegister (fields (u := u) (Extra := Extra)) register), outputTapes base⟩) = some ⟨none, ((), none), outputTapes base⟩ := rfl exact trace_trans_inline_SourceContextClear _ drain last def clearInTime (base : Tape u Extra → List Bool) (register : Option Bool) : StateTransition.EvalsToInTime (TM2.step program) ⟨some main, ((), register), base⟩ (some ⟨none, ((), none), outputTapes base⟩) (MachineDrainMany.lengthSum fields base + 6 * u + 1) where steps := MachineDrainMany.lengthSum fields base + 6 * u + 1 evals_in_steps := clearTrace base register steps_le_m := Nat.le_refl _ private theorem lengthSum_le_selected_inline_SourceContextClear {K : Type} (chosen : List K) (base : K → List Bool) (B : Nat) (h : ∀ k ∈ chosen, (base k).length ≤ B) : MachineDrainMany.lengthSum chosen base ≤ chosen.length * B := by induction chosen with | nil => simp [MachineDrainMany.lengthSum] | cons source chosen ih => have hs := h source (by simp) have ht := ih (by intro k hk; exact h k (by simp [hk])) simp only [MachineDrainMany.lengthSum, List.map_cons, List.sum_cons, List.length_cons, Nat.add_mul, Nat.one_mul] at ht ⊢ omega theorem clauseField_length_le {n : Nat} (c : Target.Clause n) (s : Fin 6) : (encodeWord ((clauseWords c)[s.val]'(by simp))).length ≤ n + 2 := by have h0 := (c)[0].variableIndex.isLt have h1 := (c)[1].variableIndex.isLt have h2 := (c)[2].variableIndex.isLt fin_cases s <;> simp [clauseWords, literalWords, encodeWord_length] all_goals first | omega | (split <;> omega) theorem variables_add_two_le_input (F : Target.Formula) : F.«variables» + 2 ≤ (formulaBits F).length := by simp only [formulaBits, formulaWords, encodeWords_append, List.length_append, encodeWords, encodeWord_length, List.length_nil] omega omit [DecidableEq Extra] in theorem loaded_field_length_le (F : Target.Formula) (tuple : Fin u → Fin F.clauses.length) (base : Tape u Extra → List Bool) (hfields : ∀ j s, base (.field j s) = []) (j : Fin u) (s : Fin 6) : (SourceContextLoad.stageTapes F tuple base u (.field j s)).length ≤ (formulaBits F).length := by rw [SourceContextLoad.stageTapes_field, ite_eq_left j.isLt, hfields, List.append_nil] exact (clauseField_length_le _ s).trans (variables_add_two_le_input F) omit [DecidableEq Extra] in theorem loaded_cost_le (F : Target.Formula) (tuple : Fin u → Fin F.clauses.length) (base : Tape u Extra → List Bool) (hfields : ∀ j s, base (.field j s) = []) : MachineDrainMany.lengthSum fields (SourceContextLoad.stageTapes F tuple base u) + 6 * u + 1 ≤ 6 * u * ((formulaBits F).length + 1) + 1 := by have h := lengthSum_le_selected_inline_SourceContextClear fields (SourceContextLoad.stageTapes F tuple base u) (formulaBits F).length (by intro p hp obtain ⟨j, s, rfl⟩ := (mem_fields_iff p).mp hp exact loaded_field_length_le F tuple base hfields j s) rw [fields_length] at h rw [Nat.mul_add, Nat.mul_one] omega def clearLoadedInTime (F : Target.Formula) (tuple : Fin u → Fin F.clauses.length) (base : Tape u Extra → List Bool) (hfields : ∀ j s, base (.field j s) = []) (register : Option Bool) : StateTransition.EvalsToInTime (TM2.step program) ⟨some main, ((), register), SourceContextLoad.stageTapes F tuple base u⟩ (some ⟨none, ((), none), outputTapes (SourceContextLoad.stageTapes F tuple base u)⟩) (6 * u * ((formulaBits F).length + 1) + 1) where steps := MachineDrainMany.lengthSum fields (SourceContextLoad.stageTapes F tuple base u) + 6 * u + 1 evals_in_steps := clearTrace _ register steps_le_m := loaded_cost_le F tuple base hfields omit [DecidableEq Extra] in theorem output_loaded_eq_base (F : Target.Formula) (tuple : Fin u → Fin F.clauses.length) (base : Tape u Extra → List Bool) (hfields : ∀ j s, base (.field j s) = []) (hindex : base .index = []) (hwork : base .work = []) : outputTapes (SourceContextLoad.stageTapes F tuple base u) = base := by funext p cases p with | field j s => exact (hfields j s).symm | index => exact (SourceContextLoad.stageTapes_clean F tuple base u hindex hwork).1.trans hindex.symm | work => exact (SourceContextLoad.stageTapes_clean F tuple base u hindex hwork).2.trans hwork.symm | _ => exact SourceContextLoad.stageTapes_frame F tuple base u _ (by simp) (by simp) (by simp) /-- With initially clean fields and lookup work tapes, loading followed by this cleanup restores the caller's original frame exactly. -/ def clearLoadedToBaseInTime (F : Target.Formula) (tuple : Fin u → Fin F.clauses.length) (base : Tape u Extra → List Bool) (hfields : ∀ j s, base (.field j s) = []) (hindex : base .index = []) (hwork : base .work = []) (register : Option Bool) : StateTransition.EvalsToInTime (TM2.step program) ⟨some main, ((), register), SourceContextLoad.stageTapes F tuple base u⟩ (some ⟨none, ((), none), base⟩) (6 * u * ((formulaBits F).length + 1) + 1) := by simpa only [output_loaded_eq_base F tuple base hfields hindex hwork] using clearLoadedInTime F tuple base hfields register def machine (u : Nat) (Extra : Type) [DecidableEq Extra] [Fintype Extra] : FinTM2 where K := Tape u Extra k₀ := .formula k₁ := .formula Γ _ := Bool Λ := Label u Extra main := main σ := Unit × Option Bool initialState := ((), none) m := program end MaxCutGames.Foundations.Hastad.SourceContextClear namespace MaxCutGames.Foundations.Hastad.SourceQueryOrder open Turing Complexity Target SourceContexts SourceOccurrences open SourceLocalSignature SourceAddressDescriptors SourceLoopOrder def occurrenceBits (F : Formula) (u D : Nat) (p : SourceIndex F u D) : List Bool := encodeWords (MaxCutGames.Reduction.SourceEncoding.equationWords (sourceEquation F u D p)) def slotBits (F : Formula) (u D : Nat) (c : ClauseContext F u) (s : SlotContext u) : List Bool := (testTapeEncoding u D).enumerate.flatMap (fun t => occurrenceBits F u D ((c, s), t)) def tupleBits (F : Formula) (u D : Nat) (c : ClauseContext F u) : List Bool := (slotContextEncoding u).enumerate.flatMap (slotBits F u D c) theorem equationBits_eq (F : Formula) (u D : Nat) (c : ClauseContext F u) (s : SlotContext u) (t : SourceQueryLoop.Query u D) : SourceTestAppend.equationBits u D (baseValue F u c (sampledVariables F c s)) (ofContext F c s, t) = occurrenceBits F u D ((c, s), t) := by rw [occurrenceBits, sourceEquation_words] rfl theorem prefixBits_eq_slotBits (F : Formula) (u D : Nat) (c : ClauseContext F u) (s : SlotContext u) (fallback : SourceQueryLoop.Query u D) : SourceQueryLoop.prefixBits u D (rankedQuery u D fallback) (baseValue F u c (sampledVariables F c s)) (ofContext F c s) (testTapeEncoding u D).size = slotBits F u D c s := by rw [query_prefixBits_eq_enumerate] unfold slotBits congr 1 funext t exact equationBits_eq F u D c s t variable {K Λ : Type} [DecidableEq K] theorem sourceLoopInTime (F : Formula) (u D : Nat) (c : ClauseContext F u) (s : SlotContext u) (fallback initialQuery : SourceQueryLoop.Query u D) (layout : SourceTestAppend.Layout K) (labels : SourceQueryLoop.Label u D (testTapeEncoding u D).size → Λ) (exit : Option Λ) (p : Λ → TM2.Stmt (fun _ : K => Bool) Λ (SourceQueryLoop.State u D)) (atLabels : ∀ l, p (labels l) = SourceQueryLoop.statement u D (testTapeEncoding u D).size (rankedQuery u D fallback) layout labels exit l) (base : K → List Bool) (sourceWords : ∀ side, base (layout.sources side) = encodeWord (baseValue F u c (sampledVariables F c s) side)) (scratchEmpty : base layout.scratch = []) (temporaryEmpty : base layout.temporary = []) : ∃ lastQuery : SourceQueryLoop.Query u D, Nonempty (StateTransition.EvalsToInTime (TM2.step p) ⟨some (labels (.load ⟨0, Nat.zero_lt_succ _⟩)), ((ofContext F c s, initialQuery), none), base⟩ (some ⟨exit, ((ofContext F c s, lastQuery), none), SourceTestAppend.resultTapes layout base (slotBits F u D c s)⟩) ((testTapeEncoding u D).size * (9 * nBits F u + 21) + 1)) := by have hbound : ∀ i, i < (testTapeEncoding u D).size → SourceTestAppend.steps u D (baseValue F u c (sampledVariables F c s)) (ofContext F c s, rankedQuery u D fallback i) ≤ 9 * nBits F u + 20 := by intro i hi let run := SourceTestAppend.sourceAppendInTime F u D ((c, s), rankedQuery u D fallback i) layout (fun l => labels (.emit ⟨i, hi⟩ l)) (some (labels (.load ⟨i + 1, by omega⟩))) p (fun l => atLabels (.emit ⟨i, hi⟩ l)) base sourceWords scratchEmpty temporaryEmpty none exact run.steps_le_m have h := SourceQueryLoop.loopInTime u D (testTapeEncoding u D).size (9 * nBits F u + 20) (rankedQuery u D fallback) layout labels exit p atLabels (ofContext F c s) initialQuery base (baseValue F u c (sampledVariables F c s)) sourceWords scratchEmpty temporaryEmpty hbound rw [prefixBits_eq_slotBits] at h exact h end MaxCutGames.Foundations.Hastad.SourceQueryOrder namespace MaxCutGames.Foundations.Hastad.SourceTupleTape open Turing Complexity SourceContexts SourceRuntimeModel structure Snapshot where rank : List Bool := [] rightBase : List Bool := [] leftBase : List Bool := [] zero : List Bool := [] emitted : List Bool := [] deriving DecidableEq, Inhabited variable {u : Nat} {Extra : Type} def tapes (base : Arena u Extra → List Bool) (s : Snapshot) : Arena u Extra → List Bool | .extra (.inr .rank) => s.rank | .extra (.inr .rightBase) => s.rightBase | .extra (.inr .leftBase) => s.leftBase | .extra (.inr .zero) => s.zero | .extra (.inr .accumulator) => s.emitted.reverse ++ base (workTape .accumulator) | k => base k @[simp] theorem tapes_rank (base : Arena u Extra → List Bool) (s : Snapshot) : tapes base s (workTape .rank) = s.rank := rfl @[simp] theorem tapes_rightBase (base : Arena u Extra → List Bool) (s : Snapshot) : tapes base s (workTape .rightBase) = s.rightBase := rfl @[simp] theorem tapes_leftBase (base : Arena u Extra → List Bool) (s : Snapshot) : tapes base s (workTape .leftBase) = s.leftBase := rfl @[simp] theorem tapes_zero (base : Arena u Extra → List Bool) (s : Snapshot) : tapes base s (workTape .zero) = s.zero := rfl @[simp] theorem tapes_accumulator (base : Arena u Extra → List Bool) (s : Snapshot) : tapes base s (workTape .accumulator) = s.emitted.reverse ++ base (workTape .accumulator) := rfl @[simp] theorem tapes_coefficient (base : Arena u Extra → List Bool) (s : Snapshot) : tapes base s (workTape .coefficient) = base (workTape .coefficient) := rfl @[simp] theorem tapes_dummy (base : Arena u Extra → List Bool) (s : Snapshot) : tapes base s (workTape .dummy) = base (workTape .dummy) := rfl @[simp] theorem tapes_leftBlock (base : Arena u Extra → List Bool) (s : Snapshot) : tapes base s (workTape .leftBlock) = base (workTape .leftBlock) := rfl @[simp] theorem tapes_queryTemporary (base : Arena u Extra → List Bool) (s : Snapshot) : tapes base s (workTape .queryTemporary) = base (workTape .queryTemporary) := rfl @[simp] theorem tapes_queryScratch (base : Arena u Extra → List Bool) (s : Snapshot) : tapes base s (workTape .queryScratch) = base (workTape .queryScratch) := rfl @[simp] theorem tapes_field (base : Arena u Extra → List Bool) (s : Snapshot) (j : Fin u) (k : Fin 6) : tapes base s (.field j k) = base (.field j k) := rfl @[simp] theorem tapes_current (base : Arena u Extra → List Bool) (s : Snapshot) (j : Fin u) : tapes base s (.current j) = base (.current j) := rfl @[simp] theorem tapes_formula (base : Arena u Extra → List Bool) (s : Snapshot) : tapes base s .formula = base .formula := rfl @[simp] theorem tapes_index (base : Arena u Extra → List Bool) (s : Snapshot) : tapes base s .index = base .index := rfl @[simp] theorem tapes_work (base : Arena u Extra → List Bool) (s : Snapshot) : tapes base s .work = base .work := rfl @[simp] theorem tapes_variableHeader (base : Arena u Extra → List Bool) (s : Snapshot) : tapes base s variableHeader = base variableHeader := rfl @[simp] theorem tapes_clauseHeader (base : Arena u Extra → List Bool) (s : Snapshot) : tapes base s clauseHeader = base clauseHeader := rfl variable [DecidableEq Extra] theorem update_rank (base : Arena u Extra → List Bool) (s : Snapshot) (value : List Bool) : Function.update (tapes base s) (workTape .rank) value = tapes base {s with rank := value} := by funext k cases k <;> try rfl rename_i extra cases extra with | inl header => simp [tapes, workTape] | inr role => cases role <;> simp [tapes, workTape] theorem update_rightBase (base : Arena u Extra → List Bool) (s : Snapshot) (value : List Bool) : Function.update (tapes base s) (workTape .rightBase) value = tapes base {s with rightBase := value} := by funext k cases k <;> try rfl rename_i extra cases extra with | inl header => simp [tapes, workTape] | inr role => cases role <;> simp [tapes, workTape] theorem update_leftBase (base : Arena u Extra → List Bool) (s : Snapshot) (value : List Bool) : Function.update (tapes base s) (workTape .leftBase) value = tapes base {s with leftBase := value} := by funext k cases k <;> try rfl rename_i extra cases extra with | inl header => simp [tapes, workTape] | inr role => cases role <;> simp [tapes, workTape] theorem update_zero (base : Arena u Extra → List Bool) (s : Snapshot) (value : List Bool) : Function.update (tapes base s) (workTape .zero) value = tapes base {s with zero := value} := by funext k cases k <;> try rfl rename_i extra cases extra with | inl header => simp [tapes, workTape] | inr role => cases role <;> simp [tapes, workTape] theorem update_emitted (base : Arena u Extra → List Bool) (s : Snapshot) (value : List Bool) : Function.update (tapes base s) (workTape .accumulator) (value.reverse ++ base (workTape .accumulator)) = tapes base {s with emitted := value} := by funext k cases k <;> try rfl rename_i extra cases extra with | inl header => simp [tapes, workTape] | inr role => cases role <;> simp [tapes, workTape] theorem query_result (base : Arena u Extra → List Bool) (s : Snapshot) (bits : List Bool) : SourceTestAppend.resultTapes queryLayout (tapes base s) bits = tapes base {s with emitted := s.emitted ++ bits} := by change Function.update (tapes base s) (workTape .accumulator) (bits.reverse ++ tapes base s (workTape .accumulator)) = _ rw [tapes_accumulator] have h := update_emitted base s (s.emitted ++ bits) simpa only [List.reverse_append, List.append_assoc] using h theorem horner_clause_result (base : Arena u Extra → List Bool) (s : Snapshot) (value : Nat) : MachineHorner.resultTapes clauseRankSlots (tapes base s) value = tapes base {s with rank := encodeWord value ++ s.rank} := by change Function.update (tapes base s) (workTape .rank) (encodeWord value ++ tapes base s (workTape .rank)) = _ rw [tapes_rank, update_rank] theorem horner_variable_result (base : Arena u Extra → List Bool) (s : Snapshot) (selected : SlotContext u) (value : Nat) : MachineHorner.resultTapes (variableRankSlots selected) (tapes base s) value = tapes base {s with rank := encodeWord value ++ s.rank} := by change Function.update (tapes base s) (workTape .rank) (encodeWord value ++ tapes base s (workTape .rank)) = _ rw [tapes_rank, update_rank] theorem horner_right_result (base : Arena u Extra → List Bool) (s : Snapshot) (value : Nat) : MachineHorner.resultTapes (baseSlots true) (tapes base s) value = tapes base {s with rightBase := encodeWord value ++ s.rightBase} := by change Function.update (tapes base s) (workTape .rightBase) (encodeWord value ++ tapes base s (workTape .rightBase)) = _ rw [tapes_rightBase, update_rightBase] theorem horner_left_result (base : Arena u Extra → List Bool) (s : Snapshot) (value : Nat) : MachineHorner.resultTapes (baseSlots false) (tapes base s) value = tapes base {s with leftBase := encodeWord value ++ s.leftBase} := by change Function.update (tapes base s) (workTape .leftBase) (encodeWord value ++ tapes base s (workTape .leftBase)) = _ rw [tapes_leftBase, update_leftBase] structure WorkClean (base : Arena u Extra → List Bool) : Prop where accA : base (workTape .accA) = [] accB : base (workTape .accB) = [] counter : base (workTape .counter) = [] scratch : base (workTape .arithScratch) = [] omit [DecidableEq Extra] in theorem clean_clause (base : Arena u Extra → List Bool) (s : Snapshot) (clean : WorkClean base) : MachineHorner.Clean clauseRankSlots (tapes base s) where accA := clean.accA accB := clean.accB counter := clean.counter scratch := clean.scratch omit [DecidableEq Extra] in theorem clean_variable (base : Arena u Extra → List Bool) (s : Snapshot) (selected : SlotContext u) (clean : WorkClean base) : MachineHorner.Clean (variableRankSlots selected) (tapes base s) where accA := clean.accA accB := clean.accB counter := clean.counter scratch := clean.scratch omit [DecidableEq Extra] in theorem clean_base (base : Arena u Extra → List Bool) (s : Snapshot) (right : Bool) (clean : WorkClean base) : MachineHorner.Clean (baseSlots right) (tapes base s) where accA := clean.accA accB := clean.accB counter := clean.counter scratch := clean.scratch omit [DecidableEq Extra] in theorem tapes_coordinateOutput {variableCount : Nat} (j : Fin u) (clause : Target.Clause variableCount) (base : Arena u Extra → List Bool) (s : Snapshot) : tapes (SourceContextLoad.coordinateOutput j clause base) s = SourceContextLoad.coordinateOutput j clause (tapes base s) := by funext k cases k <;> simp [tapes, SourceContextLoad.coordinateOutput, workTape] rename_i extra cases extra with | inl header => rfl | inr role => cases role <;> rfl omit [DecidableEq Extra] in theorem tapes_stageTapes (F : Target.Formula) (c : ClauseContext F u) (base : Arena u Extra → List Bool) (s : Snapshot) (r : Nat) : tapes (SourceContextLoad.stageTapes F c base r) s = SourceContextLoad.stageTapes F c (tapes base s) r := by induction r with | zero => rfl | succ r ih => simp only [SourceContextLoad.stageTapes] split · rw [tapes_coordinateOutput, ih] · exact ih theorem final_tapes (base : Arena u Extra → List Bool) (emitted : List Bool) (hrank : base (workTape .rank) = []) (hright : base (workTape .rightBase) = []) (hleft : base (workTape .leftBase) = []) (hzero : base (workTape .zero) = []) : tapes base {emitted := emitted} = Function.update base (workTape .accumulator) (emitted.reverse ++ base (workTape .accumulator)) := by funext k cases k <;> try rfl rename_i extra cases extra with | inl header => simp [tapes, workTape] | inr role => cases role <;> simp_all [tapes, workTape] theorem empty_tapes (base : Arena u Extra → List Bool) (hrank : base (workTape .rank) = []) (hright : base (workTape .rightBase) = []) (hleft : base (workTape .leftBase) = []) (hzero : base (workTape .zero) = []) : tapes base {} = base := by simpa using final_tapes base [] hrank hright hleft hzero omit [DecidableEq Extra] in theorem clear_loaded (F : Target.Formula) (c : ClauseContext F u) (base : Arena u Extra → List Bool) (s : Snapshot) (hfields : ∀ j k, base (.field j k) = []) (hindex : base .index = []) (hwork : base .work = []) : SourceContextClear.outputTapes (tapes (SourceContextLoad.stageTapes F c base u) s) = tapes base s := by rw [tapes_stageTapes] exact SourceContextClear.output_loaded_eq_base F c (tapes base s) hfields hindex hwork /-- Apply the checked actual field cleanup directly to an overlaid loaded context. -/ def clearLoadedSnapshotInTime (F : Target.Formula) (c : ClauseContext F u) (base : Arena u Extra → List Bool) (s : Snapshot) (hfields : ∀ j k, base (.field j k) = []) (hindex : base .index = []) (hwork : base .work = []) (register : Option Bool) : StateTransition.EvalsToInTime (TM2.step SourceContextClear.program) ⟨some SourceContextClear.main, ((), register), tapes (SourceContextLoad.stageTapes F c base u) s⟩ (some ⟨none, ((), none), tapes base s⟩) (6 * u * ((formulaBits F).length + 1) + 1) := by rw [tapes_stageTapes] exact SourceContextClear.clearLoadedToBaseInTime F c (tapes base s) hfields hindex hwork register end MaxCutGames.Foundations.Hastad.SourceTupleTape end OAI