math / data /lean /OAI /Algorithms /SubexponentialSAT /SqrtLogSpace /DisjointReuse /SparseDFSBoundarySpace.lean
Download data/lean/OAI/Algorithms/SubexponentialSAT/SqrtLogSpace/DisjointReuse/SparseDFSBoundarySpace.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 17.9 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Algorithms/SubexponentialSAT/SqrtLogSpace/DisjointReuse/SparseDFSBoundarySpace.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Algorithms/SubexponentialSAT/SqrtLogSpace/DisjointReuse/SparseDFSBoundarySpace.lean
-
curl -L -o SparseDFSBoundarySpace.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Algorithms/SubexponentialSAT/SqrtLogSpace/DisjointReuse/SparseDFSBoundarySpace.lean
17.9 kB
| import OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.DisjointReuse.SparseFrontierProgram | |
| import OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.DisjointReuse.SparseAccumulatorProgram | |
| import OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.StructuredSpace | |
| /-! Physical storage at the depth-first controller boundary. | |
| Only framed formula bodies occur on the pending stack. The mathematical depth | |
| is a proof index and is not serialized. Both children, the current formula, | |
| the declared-variable word, and the two count words are charged simultaneously. | |
| These bounds apply to the actual fixed frontier and accumulator routines after | |
| injective port relocation. The final DFS must still establish its boundary | |
| invariant and bounds for the concrete selector, splitter, and leaf commands. | |
| -/ | |
| namespace OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.DisjointReuse.SparseDFSBoundarySpace | |
| open BitProgram StructuredCalls | |
| private theorem stackBits_single_le {S : β} (words : Fin S β List Bool) (i : Fin S) : | |
| (words i).length β€ stackBits words := | |
| Finset.single_le_sum (fun j _ => Nat.zero_le ((words j).length)) (Finset.mem_univ i) | |
| /-- Actual cleanup never increases any stack, at any prefix. No bound on | |
| elapsed time is needed: its finite table contains only pop, branch and halt. -/ | |
| theorem clearProgram_stack_length_le {r s steps : β} | |
| (src : Fin s) (value present : Fin r) {a b : Cfg r s 3} | |
| (htrace : Trace (StackRoutines.clearProgram src value present) steps a b) : | |
| β i, (b.stacks i).length β€ (a.stacks i).length := by | |
| have hstep {a b : Cfg r s 3} | |
| (hs : step (StackRoutines.clearProgram src value present) a = some b) : | |
| β i, (b.stacks i).length β€ (a.stacks i).length := by | |
| cases hpc : a.pc with | |
| | none => simp [step, hpc] at hs | |
| | some label => | |
| simp only [step, hpc, Option.map, Option.some.injEq] at hs | |
| rw [β hs] | |
| intro i | |
| fin_cases label | |
| Β· by_cases hi : i = src | |
| Β· subst i | |
| simp [execInstr] | |
| Β· simp [execInstr, hi] | |
| Β· simp [execInstr] | |
| Β· simp [execInstr] | |
| induction htrace with | |
| | refl => exact fun _ => le_rfl | |
| | cons hs ht ih => exact fun i => (ih i).trans (hstep hs i) | |
| def frontierBound (bodyBound depth : β) : β := | |
| 1 + (2 * bodyBound + 2) * (depth + 2) | |
| def storageBound (bodyBound depth thresholdBound n : β) : β := | |
| frontierBound bodyBound depth + thresholdBound + 100 * (bodyBound + n + 1) | |
| /-- The delimiter and every escaped body bit are included. -/ | |
| theorem frontier_length_le (bodies : List (List Bool)) (bodyBound depth : β) | |
| (hdepth : bodies.length β€ depth + 2) | |
| (hbodies : β body β bodies, body.length β€ bodyBound) : | |
| (Encoding.encodeList id bodies).length β€ frontierBound bodyBound depth := by | |
| exact (Encoding.length_encodeList_upper id bodies bodyBound hbodies).trans | |
| (Nat.add_le_add_left (Nat.mul_le_mul_left _ hdepth) _) | |
| /-- The finite DFS port view. Scratch and all other selector ports are empty | |
| at this boundary; `rootEmbedding` puts persistent words above the leaf bank. -/ | |
| def boundaryStacks (current threshold pending saved left right total leaf : List Bool) : | |
| Fin 31 β List Bool := | |
| ![[], [], [], [], [], [], [], [], [], [], [], [], [], current, [], [], [], [], [], | |
| threshold, [], [], [], [], pending, saved, left, right, total, leaf, []] | |
| theorem boundary_stackBits (current threshold pending saved left right total leaf : List Bool) : | |
| stackBits (boundaryStacks current threshold pending saved left right total leaf) = | |
| current.length + threshold.length + pending.length + saved.length + | |
| left.length + right.length + total.length + leaf.length := by | |
| simp [stackBits, boundaryStacks, Fin.sum_univ_succ, Nat.add_assoc] | |
| /-- Count words are canonical, so cancellation or redundant zero padding | |
| cannot invalidate this actual byte-length bound. The two framed children may | |
| coexist with the original formula and the complete pending frontier. -/ | |
| theorem boundary_space (current threshold left right : List Bool) | |
| (bodies : List (List Bool)) (a b n bodyBound depth thresholdBound : β) | |
| (hcurrent : current.length β€ bodyBound) | |
| (hthreshold : threshold.length β€ thresholdBound) | |
| (hleft : left.length β€ 2 * bodyBound + 2) | |
| (hright : right.length β€ 2 * bodyBound + 2) | |
| (hdepth : bodies.length β€ depth + 2) | |
| (hbodies : β body β bodies, body.length β€ bodyBound) | |
| (hcount : a + b β€ 2 ^ n) : | |
| stackBits (boundaryStacks current threshold (Encoding.encodeList id bodies) | |
| (Encoding.encodeNat n) left right (binaryNat a) (binaryNat b)) β€ | |
| storageBound bodyBound depth thresholdBound n := by | |
| have hpending := frontier_length_le bodies bodyBound depth hdepth hbodies | |
| have ha := binaryNat_length_le ((Nat.le_add_right a b).trans hcount) | |
| have hb := binaryNat_length_le ((Nat.le_add_left b a).trans hcount) | |
| rw [boundary_stackBits, Encoding.length_encodeNat] | |
| unfold storageBound | |
| omega | |
| /-- Local current, saved-n, pending and scratch ports. -/ | |
| def popStack : Fin 4 βͺ Fin 31 where | |
| toFun := ![13, 25, 24, 23] | |
| inj' := by decide | |
| /-- Local pending, saved-n, framed children and scratch ports. -/ | |
| def pairStack : Fin 5 βͺ Fin 31 where | |
| toFun := ![24, 25, 26, 27, 0] | |
| inj' := by decide | |
| /-- Local running total, current leaf count and scratch ports. -/ | |
| def countStack : Fin 3 βͺ Fin 31 where | |
| toFun := ![28, 29, 30] | |
| inj' := by decide | |
| /-- DFS bookkeeping retains its original ports inside the frozen 1220-stack ABI. | |
| Current formula, threshold, frontier, header and counts lie above its low scratch bank. | |
| View port zero remains scratch, while view port thirteen is the formula. -/ | |
| def rootEmbedding : Fin 31 βͺ Fin 1220 where | |
| toFun := ![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 265, 14, 15, 16, 17, 18, | |
| 259, 20, 21, 22, 23, 257, 258, 261, 262, 260, 263, 30] | |
| inj' := by decide | |
| private theorem rootEmbedding_avoids (i : Fin 31) : | |
| rootEmbedding i β 256 β§ rootEmbedding i β 264 β§ rootEmbedding i β 266 := by | |
| exact (by decide : β j : Fin 31, | |
| rootEmbedding j β 256 β§ rootEmbedding j β 264 β§ rootEmbedding j β 266) i | |
| /-- Full root boundary stacks, including retained raw input and the two | |
| binary setup parameters. Every unassigned stack is empty. -/ | |
| noncomputable def rootStacks (raw current threshold pending saved left right total leaf | |
| d sparseParameter : List Bool) : Fin 1220 β List Bool := | |
| Function.update (Function.update (Function.update | |
| (Function.extend rootEmbedding | |
| (boundaryStacks current threshold pending saved left right total leaf) (fun _ => [])) | |
| 256 raw) 264 d) 266 sparseParameter | |
| theorem rootStacks_view (raw current threshold pending saved left right total leaf | |
| d sparseParameter : List Bool) (i : Fin 31) : | |
| rootStacks raw current threshold pending saved left right total leaf d sparseParameter | |
| (rootEmbedding i) = | |
| boundaryStacks current threshold pending saved left right total leaf i := by | |
| obtain β¨hraw, hd, hsβ© := rootEmbedding_avoids i | |
| simp only [rootStacks, Function.update_of_ne hs, Function.update_of_ne hd, | |
| Function.update_of_ne hraw, rootEmbedding.injective.extend_apply] | |
| theorem rootStacks_reserved (raw current threshold pending saved left right total leaf | |
| d sparseParameter : List Bool) (i : Fin 1220) (hi : 267 β€ i.val) : | |
| rootStacks raw current threshold pending saved left right total leaf d sparseParameter i = [] := by | |
| have hraw : i β 256 := by intro h; subst i; exact (by omega) | |
| have hd : i β 264 := by intro h; subst i; exact (by omega) | |
| have hs : i β 266 := by intro h; subst i; exact (by omega) | |
| have hview : Β¬ β j, rootEmbedding j = i := by | |
| rintro β¨j, rflβ© | |
| have hj := (by decide : β q : Fin 31, (rootEmbedding q).val < 267) j | |
| omega | |
| simp only [rootStacks, Function.update_of_ne hs, Function.update_of_ne hd, | |
| Function.update_of_ne hraw, Function.extend_apply' _ _ _ hview] | |
| /-- All 1220 stacks are bounded, including raw input and setup data outside | |
| the finite DFS view. The reserved high bank is empty in this exact state. -/ | |
| theorem root_boundary_space (raw current threshold left right d sparseParameter : List Bool) | |
| (bodies : List (List Bool)) (a b n bodyBound depth thresholdBound : β) | |
| (hcurrent : current.length β€ bodyBound) | |
| (hthreshold : threshold.length β€ thresholdBound) | |
| (hleft : left.length β€ 2 * bodyBound + 2) | |
| (hright : right.length β€ 2 * bodyBound + 2) | |
| (hdepth : bodies.length β€ depth + 2) | |
| (hbodies : β body β bodies, body.length β€ bodyBound) | |
| (hcount : a + b β€ 2 ^ n) : | |
| β i, (rootStacks raw current threshold (Encoding.encodeList id bodies) | |
| (Encoding.encodeNat n) left right (binaryNat a) (binaryNat b) d sparseParameter i).length β€ | |
| storageBound bodyBound depth | |
| (thresholdBound + raw.length + d.length + sparseParameter.length) n := by | |
| have hbits := boundary_space current threshold left right bodies a b n bodyBound depth | |
| thresholdBound hcurrent hthreshold hleft hright hdepth hbodies hcount | |
| intro i | |
| by_cases hraw : i = 256 | |
| Β· subst i | |
| simp only [rootStacks, Function.update_of_ne (by decide : (256 : Fin 1220) β 266), | |
| Function.update_of_ne (by decide : (256 : Fin 1220) β 264), Function.update_self] | |
| unfold storageBound | |
| omega | |
| by_cases hd : i = 264 | |
| Β· subst i | |
| simp only [rootStacks, Function.update_of_ne (by decide : (264 : Fin 1220) β 266), | |
| Function.update_self] | |
| unfold storageBound | |
| omega | |
| by_cases hs : i = 266 | |
| Β· subst i | |
| simp only [rootStacks, Function.update_self] | |
| unfold storageBound | |
| omega | |
| by_cases hi : β j, rootEmbedding j = i | |
| Β· obtain β¨j, rflβ© := hi | |
| rw [rootStacks_view] | |
| have hj : (boundaryStacks current threshold (Encoding.encodeList id bodies) | |
| (Encoding.encodeNat n) left right (binaryNat a) (binaryNat b) j).length β€ | |
| stackBits (boundaryStacks current threshold (Encoding.encodeList id bodies) | |
| (Encoding.encodeNat n) left right (binaryNat a) (binaryNat b)) := | |
| stackBits_single_le _ j | |
| unfold storageBound at * | |
| omega | |
| Β· simp only [rootStacks, Function.update_of_ne hs, Function.update_of_ne hd, | |
| Function.update_of_ne hraw, Function.extend_apply' _ _ _ hi, List.length_nil, | |
| Nat.zero_le] | |
| /-- The pop's actual transient scratch is bounded independently of how many | |
| earlier DFS iterations have run. Unmapped caller stacks retain their bound. -/ | |
| theorem pop_lifted_space {R S : β} (register : Fin 3 βͺ Fin R) | |
| (ambient : Fin 31 βͺ Fin S) | |
| (frame : Cfg R S (size SparseFrontierProgram.popIfPresent + 1)) | |
| (body saved pending : List Bool) (v p flag : Bool) | |
| (bodyBound depth thresholdBound n : β) | |
| (hbody : body.length β€ bodyBound) (hsaved : saved.length β€ n + 1) | |
| (hpending : pending.length β€ frontierBound bodyBound depth) | |
| (hframe : β i, (frame.stacks i).length β€ | |
| storageBound bodyBound depth thresholdBound n) : | |
| β prefixSteps c, prefixSteps β€ 9 * body.length + 11 β | |
| Trace (ProgramLinking.liftProgram SparseFrontierProgram.popIfPresentProgram | |
| register (ambient β popStack)) prefixSteps | |
| (ProgramLinking.embedCfg register (ambient β popStack) id none frame | |
| ((SparseFrontierProgram.state v p flag [] saved | |
| (true :: (Encoding.frame body ++ pending)) []).withPC | |
| (some SparseFrontierProgram.popIfPresentProgram.entry))) c β | |
| β i, (c.stacks i).length β€ storageBound bodyBound depth thresholdBound n := by | |
| have hlocal : β t c, t β€ 9 * body.length + 11 β | |
| Trace SparseFrontierProgram.popIfPresentProgram t | |
| ((SparseFrontierProgram.state v p flag [] saved | |
| (true :: (Encoding.frame body ++ pending)) []).withPC | |
| (some SparseFrontierProgram.popIfPresentProgram.entry)) c β | |
| β i, (c.stacks i).length β€ storageBound bodyBound depth thresholdBound n := by | |
| intro t c ht hc i | |
| have hbits := SparseFrontierProgram.popIfPresent_prefix_space body saved pending | |
| v p flag t c ht hc | |
| have hi : (c.stacks i).length β€ dataBits c := | |
| stackBits_single_le c.stacks i | |
| unfold storageBound | |
| omega | |
| simpa only [max_self] using StructuredSpace.liftProgram_stack_bound | |
| SparseFrontierProgram.popIfPresentProgram register (ambient β popStack) frame | |
| register.injective (ambient.injective.comp popStack.injective) | |
| (SparseFrontierProgram.popIfPresent_cons_trace body saved pending v p flag) hlocal hframe | |
| /-- Both framed children are present at entry. The existing concrete pair | |
| copy consumes each child; its prefix bound includes both copies and scratch. -/ | |
| theorem pair_lifted_space {R S : β} (register : Fin 4 βͺ Fin R) | |
| (ambient : Fin 31 βͺ Fin S) | |
| (frame : Cfg R S (size SparseFrontierProgram.pushPair + 1)) | |
| (left right saved pending : List Bool) (terminal v p flag : Bool) | |
| (bodyBound depth thresholdBound n : β) | |
| (hleft : left.length β€ bodyBound) (hright : right.length β€ bodyBound) | |
| (hsaved : saved.length β€ n + 1) | |
| (hpending : pending.length β€ frontierBound bodyBound depth) | |
| (hframe : β i, (frame.stacks i).length β€ | |
| storageBound bodyBound depth thresholdBound n) : | |
| β prefixSteps c, prefixSteps β€ 13 * right.length + 13 * left.length + 37 β | |
| Trace (ProgramLinking.liftProgram SparseFrontierProgram.pushPairProgram | |
| register (ambient β pairStack)) prefixSteps | |
| (ProgramLinking.embedCfg register (ambient β pairStack) id none frame | |
| ((ExecWorklistProgram.data terminal v p flag pending saved | |
| (ExecWorklistProgram.record left) (ExecWorklistProgram.record right)).withPC | |
| (some SparseFrontierProgram.pushPairProgram.entry))) c β | |
| β i, (c.stacks i).length β€ storageBound bodyBound depth thresholdBound n := by | |
| have hlocal : β t c, t β€ 13 * right.length + 13 * left.length + 37 β | |
| Trace SparseFrontierProgram.pushPairProgram t | |
| ((ExecWorklistProgram.data terminal v p flag pending saved | |
| (ExecWorklistProgram.record left) (ExecWorklistProgram.record right)).withPC | |
| (some SparseFrontierProgram.pushPairProgram.entry)) c β | |
| β i, (c.stacks i).length β€ storageBound bodyBound depth thresholdBound n := by | |
| intro t c ht hc i | |
| have hbits := SparseFrontierProgram.pushPair_prefix_space left right saved pending | |
| terminal v p flag t c ht hc | |
| have hi : (c.stacks i).length β€ dataBits c := | |
| stackBits_single_le c.stacks i | |
| unfold storageBound | |
| omega | |
| simpa only [max_self] using StructuredSpace.liftProgram_stack_bound | |
| SparseFrontierProgram.pushPairProgram register (ambient β pairStack) frame | |
| register.injective (ambient.injective.comp pairStack.injective) | |
| (SparseFrontierProgram.pushPair_trace left right saved pending terminal v p flag) | |
| hlocal hframe | |
| /-- One concrete accumulator call returns the canonical new total and clears | |
| both temporary ports. Its prefix bound is uniform across all visited leaves. -/ | |
| theorem count_lifted_execution {R S : β} (register : Fin 8 βͺ Fin R) | |
| (ambient : Fin 31 βͺ Fin S) | |
| (frame : Cfg R S (size SparseCountProgram.Accumulator.command + 1)) | |
| (regs : Fin 8 β Bool) (a b n bodyBound depth thresholdBound : β) | |
| (hcount : a + b β€ 2 ^ n) | |
| (hframe : β i, (frame.stacks i).length β€ | |
| storageBound bodyBound depth thresholdBound n) : | |
| β (t : β) (final : StructuredProgram.State 8 3), | |
| t β€ 100 * (n + 1) β§ | |
| Trace (ProgramLinking.liftProgram SparseCountProgram.Accumulator.program | |
| register (ambient β countStack)) t | |
| (ProgramLinking.embedCfg register (ambient β countStack) id none frame | |
| ((SparseCountProgram.Accumulator.state regs (binaryNat a) (binaryNat b)).withPC | |
| (some SparseCountProgram.Accumulator.program.entry))) | |
| (ProgramLinking.embedCfg register (ambient β countStack) id none frame | |
| (final.withPC none)) β§ | |
| final.stacks 0 = binaryNat (a + b) β§ final.stacks 1 = [] β§ final.stacks 2 = [] β§ | |
| β prefixSteps c, prefixSteps β€ t β | |
| Trace (ProgramLinking.liftProgram SparseCountProgram.Accumulator.program | |
| register (ambient β countStack)) prefixSteps | |
| (ProgramLinking.embedCfg register (ambient β countStack) id none frame | |
| ((SparseCountProgram.Accumulator.state regs (binaryNat a) (binaryNat b)).withPC | |
| (some SparseCountProgram.Accumulator.program.entry))) c β | |
| β i, (c.stacks i).length β€ storageBound bodyBound depth thresholdBound n := by | |
| obtain β¨t, final, ht, htrace, houtput, hclear1, hclear2, hprefixβ© := | |
| SparseCountProgram.Accumulator.canonical_execution regs a b n hcount | |
| have hlocal : β prefixSteps c, prefixSteps β€ t β | |
| Trace SparseCountProgram.Accumulator.program prefixSteps | |
| ((SparseCountProgram.Accumulator.state regs (binaryNat a) (binaryNat b)).withPC | |
| (some SparseCountProgram.Accumulator.program.entry)) c β | |
| β i, (c.stacks i).length β€ storageBound bodyBound depth thresholdBound n := by | |
| intro prefixSteps c hle hc i | |
| have hbits := hprefix prefixSteps c hle hc | |
| have hi : (c.stacks i).length β€ dataBits c := | |
| stackBits_single_le c.stacks i | |
| unfold storageBound | |
| omega | |
| refine β¨t, final, ht, ?_, houtput, hclear1, hclear2, ?_β© | |
| Β· exact ProgramLinking.relocate_trace SparseCountProgram.Accumulator.program _ | |
| register (ambient β countStack) id none frame register.injective | |
| (ambient.injective.comp countStack.injective) (fun _ => rfl) htrace | |
| Β· simpa only [max_self] using StructuredSpace.liftProgram_stack_bound | |
| SparseCountProgram.Accumulator.program register (ambient β countStack) frame | |
| register.injective (ambient.injective.comp countStack.injective) htrace hlocal hframe | |
| end OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.DisjointReuse.SparseDFSBoundarySpace | |