cfahlgren1/math / lean /OAI /Computability /MinUncut /PCP /ContextClear.lean
cfahlgren1's picture
download
raw
16.3 kB
import Mathlib
import OAI.Computability.MinUncut.PCP.ContextLoad
import OAI.Computability.MinUncut.Machines.MachineFiniteSequence
namespace OAI
namespace MinUncutGames.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 {K : Type} : (chosen : List K) → MachineDrainMany.Label chosen ⊕ Unit
| [] => .inr ()
| _ :: _ => .inl (.inl ())
def main : Label u Extra := entryLabel 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 {K : Type} (chosen : List K) :
MachineDrainMany.entry chosen
(Sum.inl : MachineDrainMany.Label chosen → MachineDrainMany.Label chosen ⊕ Unit)
(some (.inr ())) = some (entryLabel chosen) := by
cases chosen <;> rfl
omit [DecidableEq Extra] in
private theorem entry_eq :
MachineDrainMany.entry (fields (u := u) (Extra := Extra)) Sum.inl (some (.inr ())) =
some (main (u := u) (Extra := Extra)) := entryLabel_eq 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 {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)]
private theorem steps_eq_of_nodup {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 chosen base source hn]
simp only [MachineDrainMany.lengthSum, List.map_cons, List.sum_cons, List.length_cons]
omega
theorem trace_trans {α : 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, finalTapes_eq, steps_eq_of_nodup 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 _ 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 {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 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)
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 MinUncutGames.Foundations.Hastad.SourceContextClear
namespace MinUncutGames.Foundations.Hastad.SourceOccurrences.Encoding
variable {α β γ : Type}
theorem prod_decode_pair (a : Encoding α) (b : Encoding β)
(i : Fin a.size) (j : Fin b.size) :
(a.prod b).code.symm (finProdFinEquiv (i, j)) =
(a.code.symm i, b.code.symm j) := by
change (a.code.symm (finProdFinEquiv.symm (finProdFinEquiv (i, j))).1,
b.code.symm (finProdFinEquiv.symm (finProdFinEquiv (i, j))).2) = _
rw [Equiv.symm_apply_apply]
theorem prod_decode_mk (a : Encoding α) (b : Encoding β)
(i : Fin a.size) (j : Fin b.size)
(h : i.val * b.size + j.val < a.size * b.size) :
(a.prod b).code.symm ⟨i.val * b.size + j.val, h⟩ =
(a.code.symm i, b.code.symm j) := by
have he : (⟨i.val * b.size + j.val, h⟩ : Fin (a.size * b.size)) =
finProdFinEquiv (i, j) := by
apply Fin.ext
simp [finProdFinEquiv, Nat.mul_comm, Nat.add_comm]
change (a.code.symm (finProdFinEquiv.symm (⟨i.val * b.size + j.val, h⟩ :
Fin (a.size * b.size))).1,
b.code.symm (finProdFinEquiv.symm (⟨i.val * b.size + j.val, h⟩ :
Fin (a.size * b.size))).2) = _
rw [he]
exact prod_decode_pair a b i j
theorem enumerate_prod (a : Encoding α) (b : Encoding β) :
(a.prod b).enumerate =
a.enumerate.flatMap (fun x => b.enumerate.map (fun y => (x, y))) := by
change List.ofFn (fun q : Fin (a.size * b.size) =>
(a.code.symm (finProdFinEquiv.symm q).1,
b.code.symm (finProdFinEquiv.symm q).2)) = _
rw [List.ofFn_mul]
simp only [enumerate, List.flatMap_def, List.map_ofFn]
apply congrArg List.flatten
apply congrArg List.ofFn
funext i
apply congrArg List.ofFn
funext j
exact prod_decode_mk a b i j _
theorem map_enumerate_prod (a : Encoding α) (b : Encoding β) (emit : α × β → γ) :
(a.prod b).enumerate.map emit =
a.enumerate.flatMap (fun x => b.enumerate.map (fun y => emit (x, y))) := by
rw [enumerate_prod]
simp only [List.map_flatMap, List.map_map, Function.comp_def]
end MinUncutGames.Foundations.Hastad.SourceOccurrences.Encoding
namespace MinUncutGames.Foundations.Hastad.SourceEnumeration
open Target SourceContexts SourceOccurrences
theorem testTapeEncoding_enumerate (u D : ℕ) :
(testTapeEncoding u D).enumerate =
((iEncoding u).function Encoding.bool).enumerate.flatMap (fun f =>
((jEncoding u).function (Encoding.fin D)).enumerate.flatMap (fun z =>
((jEncoding u).function Encoding.bool).enumerate.map (fun g => (f, z, g)))) := by
simp only [testTapeEncoding, Encoding.enumerate_prod, List.map_flatMap,
List.map_map, Function.comp_def]
theorem sourceIndexEncoding_enumerate (F : Formula) (u D : ℕ) :
(sourceIndexEncoding F u D).enumerate =
(clauseEncoding F u).enumerate.flatMap (fun c =>
(slotContextEncoding u).enumerate.flatMap (fun s =>
((iEncoding u).function Encoding.bool).enumerate.flatMap (fun f =>
((jEncoding u).function (Encoding.fin D)).enumerate.flatMap (fun z =>
((jEncoding u).function Encoding.bool).enumerate.map
(fun g => ((c, s), (f, z, g))))))) := by
simp only [sourceIndexEncoding, Encoding.enumerate_prod, testTapeEncoding_enumerate,
List.flatMap_assoc, List.flatMap_map, List.map_flatMap, List.map_map, Function.comp_def]
theorem sourceIndexEncoding_map {α : Type} (F : Formula) (u D : ℕ)
(emit : SourceIndex F u D → α) :
(sourceIndexEncoding F u D).enumerate.map emit =
(clauseEncoding F u).enumerate.flatMap (fun c =>
(slotContextEncoding u).enumerate.flatMap (fun s =>
((iEncoding u).function Encoding.bool).enumerate.flatMap (fun f =>
((jEncoding u).function (Encoding.fin D)).enumerate.flatMap (fun z =>
((jEncoding u).function Encoding.bool).enumerate.map
(fun g => emit ((c, s), (f, z, g))))))) := by
rw [sourceIndexEncoding_enumerate]
simp only [List.map_flatMap, List.map_map, Function.comp_def]
theorem rawSourceList_eq_nestedLoops (F : Formula) (u D : ℕ) :
rawSourceList F u D =
(clauseEncoding F u).enumerate.flatMap (fun c =>
(slotContextEncoding u).enumerate.flatMap (fun s =>
((iEncoding u).function Encoding.bool).enumerate.flatMap (fun f =>
((jEncoding u).function (Encoding.fin D)).enumerate.flatMap (fun z =>
((jEncoding u).function Encoding.bool).enumerate.map
(fun g => contextEquation F u D c (sampledVariables F c s) (f, z, g)))))) := by
exact sourceIndexEncoding_map F u D (sourceEquation F u D)
theorem sourceList_eq_nestedLoops (F : Formula) (u D : ℕ) (hne : F.clauses ≠ []) :
sourceList F u D =
(clauseEncoding F u).enumerate.flatMap (fun c =>
(slotContextEncoding u).enumerate.flatMap (fun s =>
((iEncoding u).function Encoding.bool).enumerate.flatMap (fun f =>
((jEncoding u).function (Encoding.fin D)).enumerate.flatMap (fun z =>
((jEncoding u).function Encoding.bool).enumerate.map
(fun g => contextEquation F u D c (sampledVariables F c s) (f, z, g)))))) := by
rw [sourceList_nonempty F u D hne]
exact rawSourceList_eq_nestedLoops F u D
theorem rawSourceList_flatMap {α : Type} (F : Formula) (u D : ℕ)
(emit : MinUncutGames.Reduction.CloneGap.Equation (Fin (nBits F u)) → List α) :
(rawSourceList F u D).flatMap emit =
(clauseEncoding F u).enumerate.flatMap (fun c =>
(slotContextEncoding u).enumerate.flatMap (fun s =>
((iEncoding u).function Encoding.bool).enumerate.flatMap (fun f =>
((jEncoding u).function (Encoding.fin D)).enumerate.flatMap (fun z =>
((jEncoding u).function Encoding.bool).enumerate.flatMap
(fun g => emit (contextEquation F u D c (sampledVariables F c s) (f, z, g))))))) := by
rw [rawSourceList_eq_nestedLoops]
simp only [List.flatMap_assoc, List.flatMap_map]
end MinUncutGames.Foundations.Hastad.SourceEnumeration
end OAI

Xet Storage Details

Size:
16.3 kB
·
Xet hash:
7c02dc923c1b68f7187a445dfcdbab47978cef4362b499bc0aef3573f92e398c

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.