cfahlgren1/math / lean /OAI /Algorithms /SubexponentialSAT /SubexpUnaryFamily.lean
cfahlgren1's picture
download
raw
13.1 kB
import OAI.Algorithms.SubexponentialSAT.LeanCompat
import OAI.Algorithms.SubexponentialSAT.SubexpUnaryFamilyProgram
import OAI.Algorithms.SubexponentialSAT.SubexpChunkDescriptor
import OAI.Algorithms.SubexponentialSAT.SubexpUnaryTableCorrectness
namespace OAI.Algorithms
/-! The complete unary-factor family from binary n, ell, and p words.
Chunk widths and singleton scopes are physically generated by the descriptor
program, then the closed p-row constructor is mapped over those widths. -/
namespace SubexponentialSAT.SubexpUnaryFamily
open BitProgram StructuredCalls
open SubexpUnaryFamilyCallback (Data bankStack)
open SubexpChunkDescriptorEncoding
def descriptorReg (i : Fin 3) : Fin 16 := i.castAdd 13
def descriptorStack : Fin 10 → Fin 20 := ![1, 3, 4, 5, 6, 7, 8, 9, 0, 19]
theorem descriptorReg_injective : Function.Injective descriptorReg := by
intro i j h
exact Fin.ext (congrArg (fun i : Fin 16 => i.val) h)
theorem descriptorStack_injective : Function.Injective descriptorStack := by decide
def initialState (regs : Fin 16 → Bool) (nBits ellBits modulus : List Bool) : Data :=
⟨regs, fun i => if i = 1 then nBits else if i = 3 then ellBits
else if i = 16 then modulus else []⟩
def describedStacks (n ell : ℕ) (modulus : List Bool) : Fin 20 → List Bool :=
fun i => if i = 0 then widthShapes n ell else if i = 3 then List.replicate ell true
else if i = 6 then List.replicate (ChunkPartition.count n ell) true
else if i = 16 then modulus else if i = 19 then scopes 0 (ChunkPartition.count n ell)
else []
abbrev descriptorCall : Cmd 16 20 := .call (ProgramLinking.liftProgram
SubexpChunkDescriptor.program descriptorReg descriptorStack)
theorem descriptor_eval (regs : Fin 16 → Bool) (nBits ellBits modulus : List Bool)
(hell : 0 < CounterPrograms.value ellBits) :
∃ n ≤ SubexpChunkDescriptor.runtimeBound nBits ellBits + 1, ∃ e : Data,
Eval descriptorCall n (initialState regs nBits ellBits modulus) e ∧
e.stacks = describedStacks (CounterPrograms.value nBits) (CounterPrograms.value ellBits) modulus := by
let d := initialState regs nBits ellBits modulus
have ha : projectState descriptorReg descriptorStack d =
SubexpChunkDescriptor.initialState (regs ∘ descriptorReg) nBits ellBits := by
apply congrArg₂ StructuredProgram.State.mk
· rfl
· funext i
fin_cases i <;> simp [ d, initialState, descriptorStack
]
obtain ⟨rf, n, hn, ht⟩ := SubexpChunkDescriptor.runs (regs ∘ descriptorReg) nBits ellBits hell
rw [← ha] at ht
let b := SubexpChunkDescriptor.finalState rf (CounterPrograms.value nBits) (CounterPrograms.value ellBits)
let e := embedState descriptorReg descriptorStack d b
have he := eval_call_from_state SubexpChunkDescriptor.program descriptorReg descriptorStack d
descriptorReg_injective descriptorStack_injective ht
refine ⟨n, hn, e, he, ?_⟩
funext i
by_cases hi : ∃ j, descriptorStack j = i
· obtain ⟨j, rfl⟩ := hi
rw [embedState_stack descriptorReg descriptorStack d b descriptorStack_injective]
fin_cases j <;> simp [b, descriptorStack, describedStacks,
SubexpChunkDescriptor.finalState, SubexpChunkDescriptorOutput.outputState]
· rw [embedState_stack_frame descriptorReg descriptorStack d b i hi]
have h0 : i ≠ 0 := fun h => hi ⟨8, h.symm⟩
have h1 : i ≠ 1 := fun h => hi ⟨0, h.symm⟩
have h3 : i ≠ 3 := fun h => hi ⟨1, h.symm⟩
have h6 : i ≠ 6 := fun h => hi ⟨4, h.symm⟩
have h19 : i ≠ 19 := fun h => hi ⟨9, h.symm⟩
simp [d, initialState, describedStacks, h0, h1, h3, h6, h19]
def chunkTables (n ell : ℕ) (modulus : List Bool) : List (List Bool) :=
(widths n ell).map (fun b => SubexpUnaryFactorTable.table b (CounterPrograms.value modulus))
@[simp] theorem chunkTables_length (n ell : ℕ) (modulus : List Bool) :
(chunkTables n ell modulus).length = ChunkPartition.count n ell := by simp [chunkTables]
theorem tables_widths (n ell : ℕ) (modulus : List Bool) :
SubexpUnaryFamilyProgram.tables modulus ((widths n ell).map (fun b => List.replicate b true)) =
chunkTables n ell modulus := by
simp [SubexpUnaryFamilyProgram.tables, SubexpUnaryFamilyProgram.rowTable, chunkTables, List.map_map]
theorem chunkTables_row_length (n ell : ℕ) (modulus row : List Bool)
(hr : row ∈ chunkTables n ell modulus) : row.length = CounterPrograms.value modulus := by
obtain ⟨width, _, rfl⟩ := List.mem_map.mp hr
exact SubexpUnaryFactorTable.table_length _ _
theorem chunkTables_encoding_length (n ell : ℕ) (modulus : List Bool) :
(Encoding.encodeList id (chunkTables n ell modulus)).length =
(2 * CounterPrograms.value modulus + 2) * ChunkPartition.count n ell + 1 := by
rw [← tables_widths, SubexpUnaryFamilyProgram.tables_encoding_length]
simp
theorem chunkTables_getElem (n ell : ℕ) (modulus : List Bool) (j : Fin (ChunkPartition.count n ell)) :
(chunkTables n ell modulus)[j.val]'(by simp) =
SubexpUnaryFactorTable.table (ChunkPartition.size n ell j) (CounterPrograms.value modulus) := by
simp [chunkTables, widths, ChunkPartition.size]
abbrev command : Cmd 16 20 := .seq descriptorCall
(.seq (SubexpUnaryFamilyCallback.clear 3)
(.seq (SubexpUnaryFamilyCallback.clear 6) (.call SubexpUnaryFamilyProgram.program)))
def runtimeBound (nBits ellBits modulus : List Bool) : ℕ :=
SubexpChunkDescriptor.runtimeBound nBits ellBits + 1 +
(2 * CounterPrograms.value ellBits + 3) +
(2 * ChunkPartition.count (CounterPrograms.value nBits) (CounterPrograms.value ellBits) + 3) +
400 * (CounterPrograms.value modulus + modulus.length + 1) ^ 2 *
((widthShapes (CounterPrograms.value nBits) (CounterPrograms.value ellBits)).length + 1) + 1
/-- An explicit polynomial bound in the represented dimensions and word
lengths. The input-adapter term itself is the two checked counter-conversion
polynomials plus linear transfer costs. -/
theorem runtimeBound_polynomial (nBits ellBits modulus : List Bool)
(hell : 0 < CounterPrograms.value ellBits) :
runtimeBound nBits ellBits modulus ≤
SubexpChunkDescriptorInput.runtimeBound nBits ellBits +
200 * (CounterPrograms.value nBits + CounterPrograms.value ellBits + 1) ^ 2 +
2 * CounterPrograms.value ellBits + 2 * CounterPrograms.value nBits + 8 +
400 * (CounterPrograms.value modulus + modulus.length + 1) ^ 2 *
(4 * CounterPrograms.value nBits + 2) := by
have hD : ChunkPartition.count (CounterPrograms.value nBits) (CounterPrograms.value ellBits) ≤
CounterPrograms.value nBits := by
apply (ChunkPartition.count_le_iff hell).mpr
exact Nat.le_mul_of_pos_right _ hell
have hwidth := (SubexpChunkDescriptorOutput.descriptor_lengths
(CounterPrograms.value nBits) (CounterPrograms.value ellBits) hell).1
have hwidth' : (widthShapes (CounterPrograms.value nBits) (CounterPrograms.value ellBits)).length + 1 ≤
4 * CounterPrograms.value nBits + 2 := by omega
have htable := Nat.mul_le_mul_left
(400 * (CounterPrograms.value modulus + modulus.length + 1) ^ 2) hwidth'
have hdescriptor := SubexpChunkDescriptorOutput.unaryBound_polynomial
(CounterPrograms.value nBits) (CounterPrograms.value ellBits) hell
dsimp [runtimeBound, SubexpChunkDescriptor.runtimeBound]
omega
theorem command_eval (regs : Fin 16 → Bool) (nBits ellBits modulus : List Bool)
(hell : 0 < CounterPrograms.value ellBits)
(hwidth : CounterPrograms.value ellBits ≤ modulus.length) :
∃ n, n + 1 ≤ runtimeBound nBits ellBits modulus ∧ ∃ e : Data,
Eval command n (initialState regs nBits ellBits modulus) e ∧
SubexpUnaryFamilyProgram.Ready e ∧ e.stacks 16 = modulus ∧
e.stacks 19 = scopes 0 (ChunkPartition.count (CounterPrograms.value nBits) (CounterPrograms.value ellBits)) ∧
e.stacks 0 = Encoding.encodeList id
(chunkTables (CounterPrograms.value nBits) (CounterPrograms.value ellBits) modulus) := by
obtain ⟨n, hn, d, hd, hs⟩ := descriptor_eval regs nBits ellBits modulus hell
let a := SubexpUnaryFamilyCallback.cleared 3 d
let b := SubexpUnaryFamilyCallback.cleared 6 a
have hp : b.stacks 16 = modulus := by
simp [b, a, SubexpUnaryFamilyCallback.cleared, StackRoutines.clearStacks, hs, describedStacks]
have hsource : b.stacks 0 = widthShapes (CounterPrograms.value nBits) (CounterPrograms.value ellBits) := by
simp [b, a, SubexpUnaryFamilyCallback.cleared, StackRoutines.clearStacks, hs, describedStacks]
have hscope : b.stacks 19 = scopes 0
(ChunkPartition.count (CounterPrograms.value nBits) (CounterPrograms.value ellBits)) := by
simp [b, a, SubexpUnaryFamilyCallback.cleared, StackRoutines.clearStacks, hs, describedStacks]
have hclean : ∀ i : Fin 20, i ≠ 0 → i ≠ 16 → i ≠ 19 → b.stacks i = [] := by
intro i h0 h16 h19
by_cases h3 : i = 3
· subst i
simp [b, a, SubexpUnaryFamilyCallback.cleared, StackRoutines.clearStacks]
by_cases h6 : i = 6
· subst i
simp [b, a, SubexpUnaryFamilyCallback.cleared, StackRoutines.clearStacks]
simp [b, a, SubexpUnaryFamilyCallback.cleared, StackRoutines.clearStacks,
hs, describedStacks, h0, h3, h6, h16, h19]
have hready : SubexpUnaryFamilyProgram.Ready b := by
refine ⟨?_, hclean 1 (by decide) (by decide) (by decide),
hclean 2 (by decide) (by decide) (by decide)⟩
· intro i hi
exact hclean (bankStack i) (SubexpUnaryFamilyCallback.bankStack_ne_zero i)
(fun h => hi (SubexpUnaryFamilyCallback.bankStack_injective h))
(SubexpUnaryFamilyCallback.bankStack_ne_scope i)
have hvalid : ∀ width ∈ ((widths (CounterPrograms.value nBits)
(CounterPrograms.value ellBits)).map (fun x => List.replicate x true)),
width.length ≤ modulus.length := by
intro width hw
obtain ⟨x, hx, rfl⟩ := List.mem_map.mp hw
simpa only [List.length_replicate] using (widths_positive hell hx).2.trans hwidth
obtain ⟨e, heReady, heP, heScope, heOut, m, hm, ht⟩ :=
SubexpUnaryFamilyProgram.program_runsIn modulus _ b hp hsource hvalid hready
let steps := n + ((2 * (d.stacks 3).length + 3) + ((2 * (a.stacks 6).length + 3) + m))
have hlen3 : (d.stacks 3).length = CounterPrograms.value ellBits := by
simp [hs, describedStacks]
have hlen6 : (a.stacks 6).length =
ChunkPartition.count (CounterPrograms.value nBits) (CounterPrograms.value ellBits) := by
simp [a, SubexpUnaryFamilyCallback.cleared, StackRoutines.clearStacks, hs, describedStacks]
refine ⟨steps, ?_, e, ?_, heReady, heP, heScope.trans hscope, ?_⟩
· dsimp only [steps]
rw [hlen3, hlen6]
dsimp only [runtimeBound]
change m ≤ 400 * (CounterPrograms.value modulus + modulus.length + 1) ^ 2 *
((widthShapes (CounterPrograms.value nBits) (CounterPrograms.value ellBits)).length + 1) at hm
omega
· exact hd.seq ((SubexpUnaryFamilyCallback.clear_eval 3 d).seq
((SubexpUnaryFamilyCallback.clear_eval 6 a).seq (.call _ ht)))
· simpa only [tables_widths] using heOut
def program := compiled command 1 0
theorem program_runsIn (regs : Fin 16 → Bool) (nBits ellBits modulus : List Bool)
(hell : 0 < CounterPrograms.value ellBits)
(hwidth : CounterPrograms.value ellBits ≤ modulus.length) :
∃ e : Data, SubexpUnaryFamilyProgram.Ready e ∧ e.stacks 16 = modulus ∧
e.stacks 19 = scopes 0 (ChunkPartition.count (CounterPrograms.value nBits) (CounterPrograms.value ellBits)) ∧
e.stacks 0 = Encoding.encodeList id
(chunkTables (CounterPrograms.value nBits) (CounterPrograms.value ellBits) modulus) ∧
RunsIn program (runtimeBound nBits ellBits modulus)
((initialState regs nBits ellBits modulus).withPC (some program.entry)) (e.withPC none) := by
obtain ⟨n, hn, e, he, hr, hp, hscope, hout⟩ := command_eval regs nBits ellBits modulus hell hwidth
exact ⟨e, hr, hp, hscope, hout, n + 1, hn, compiled_halt_trace he 1 0⟩
theorem primitive_execution (regs : Fin 16 → Bool) (nBits ellBits modulus : List Bool)
(hell : 0 < CounterPrograms.value ellBits)
(hwidth : CounterPrograms.value ellBits ≤ modulus.length) :
∃ e : Data, SubexpUnaryFamilyProgram.Ready e ∧ e.stacks 16 = modulus ∧
e.stacks 19 = scopes 0 (ChunkPartition.count (CounterPrograms.value nBits) (CounterPrograms.value ellBits)) ∧
e.stacks 0 = Encoding.encodeList id
(chunkTables (CounterPrograms.value nBits) (CounterPrograms.value ellBits) modulus) ∧
Computation.RunsIn (primitive program).step
(primitiveOverhead program * runtimeBound nBits ellBits modulus)
(primitiveCfg program ((initialState regs nBits ellBits modulus).withPC (some program.entry)))
(primitiveCfg program (e.withPC none)) := by
obtain ⟨e, hr, hp, hscope, hout, hrun⟩ := program_runsIn regs nBits ellBits modulus hell hwidth
exact ⟨e, hr, hp, hscope, hout, BitProgram.primitive_execution program hrun⟩
end SubexponentialSAT.SubexpUnaryFamily
end OAI.Algorithms

Xet Storage Details

Size:
13.1 kB
·
Xet hash:
8c5ecda7e7fdb56b9bec9754880abafadeac7146893e7d1b57e3a577f8520266

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