Download data/lean/OAI/Algorithms/SubexponentialSAT/Subexp/BlockCoefficientProgram.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 18.1 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Algorithms/SubexponentialSAT/Subexp/BlockCoefficientProgram.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Algorithms/SubexponentialSAT/Subexp/BlockCoefficientProgram.lean
-
curl -L -o BlockCoefficientProgram.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Algorithms/SubexponentialSAT/Subexp/BlockCoefficientProgram.lean
18.1 kB
| import OAI.Algorithms.SubexponentialSAT.LeanCompat | |
| import OAI.Algorithms.SubexponentialSAT.Subexp.MatrixEncodedProgram | |
| import OAI.Algorithms.SubexponentialSAT.Subexp.MatrixWordEncoding | |
| import OAI.Algorithms.SubexponentialSAT.Subexp.BlockSearchLoop | |
| import OAI.Algorithms.SubexponentialSAT.Subexp.BlockCoefficientContext | |
| import OAI.Algorithms.SubexponentialSAT.Subexp.BlockCoefficientAssembly | |
| import OAI.Algorithms.SubexponentialSAT.Subexp.MatrixZeroSemantics | |
| import OAI.Algorithms.SubexponentialSAT.Subexp.BlockSearchPredicate | |
| import OAI.Algorithms.SubexponentialSAT.Subexp.BlockSearchTailSemantics | |
| namespace OAI.Algorithms | |
| /-! | |
| # The local arithmetic call inside the encoded basis search | |
| The two-product table is relocated into work stacks 22--41. Its finite | |
| instructions use only registers 0--7; both surrounding iterator controllers | |
| are framed by construction. | |
| -/ | |
| namespace SubexponentialSAT.Subexp.BlockCoefficientProgram | |
| open BitProgram StructuredCalls | |
| -- Explicit closed-port projections also discharge the dependent conditions in | |
| -- the installed 20-stack state and the surrounding 42-stack work area. | |
| def matrixRegisters (i : Fin 8) : Fin 24 := β¨i.val, by omegaβ© | |
| def matrixStacks (i : Fin 20) : Fin 42 := β¨22 + i.val, by omegaβ© | |
| theorem matrixRegisters_injective : Function.Injective matrixRegisters := by | |
| intro i j h | |
| apply Fin.ext | |
| exact congrArg (fun z : Fin 24 => z.val) h | |
| theorem matrixStacks_injective : Function.Injective matrixStacks := by | |
| intro i j h | |
| apply Fin.ext | |
| have hv := congrArg Fin.val h | |
| simp only [matrixStacks] at hv | |
| omega | |
| matrixStacks i = (30 : Fin 42) β i = 8 := | |
| matrixStacks_injective.eq_iff' (a := i) (b := 8) rfl | |
| @[local simp] private theorem matrixStacks_eq_33 (i : Fin 20) : | |
| matrixStacks i = (33 : Fin 42) β i = 11 := | |
| matrixStacks_injective.eq_iff' (a := i) (b := 11) rfl | |
| matrixStacks i = (34 : Fin 42) β i = 12 := | |
| matrixStacks_injective.eq_iff' (a := i) (b := 12) rfl | |
| @[local simp] private theorem matrixStacks_eq_37 (i : Fin 20) : | |
| matrixStacks i = (37 : Fin 42) β i = 15 := | |
| matrixStacks_injective.eq_iff' (a := i) (b := 15) rfl | |
| matrixStacks i = (40 : Fin 42) β i = 18 := | |
| matrixStacks_injective.eq_iff' (a := i) (b := 18) rfl | |
| abbrev matrixPair : Cmd 24 42 := | |
| .call (ProgramLinking.liftProgram MatrixPairProgram.program matrixRegisters matrixStacks) | |
| /-- A proof-state embedding, not an executed data transformation. The | |
| relocated instruction table reads and writes the physical ports directly. -/ | |
| noncomputable def install (base : StructuredProgram.State 24 42) | |
| (localState : StructuredProgram.State 8 20) : StructuredProgram.State 24 42 := | |
| embedState matrixRegisters matrixStacks base localState | |
| theorem install_stack (base : StructuredProgram.State 24 42) | |
| (localState : StructuredProgram.State 8 20) (i : Fin 20) : | |
| (install base localState).stacks (matrixStacks i) = localState.stacks i := | |
| embedState_stack _ _ _ _ matrixStacks_injective _ | |
| theorem install_register (base : StructuredProgram.State 24 42) | |
| (localState : StructuredProgram.State 8 20) (i : Fin 8) : | |
| (install base localState).regs (matrixRegisters i) = localState.regs i := | |
| embedState_register _ _ _ _ matrixRegisters_injective _ | |
| theorem install_register_frame (base : StructuredProgram.State 24 42) | |
| (localState : StructuredProgram.State 8 20) (j : Fin 24) (hj : 8 β€ j.val) : | |
| (install base localState).regs j = base.regs j := by | |
| apply embedState_register_frame | |
| rintro β¨i, hiβ© | |
| have hv := congrArg Fin.val hi | |
| simp only [matrixRegisters] at hv | |
| omega | |
| theorem install_stack_frame (base : StructuredProgram.State 24 42) | |
| (localState : StructuredProgram.State 8 20) (j : Fin 42) (hj : j.val < 22) : | |
| (install base localState).stacks j = base.stacks j := by | |
| apply embedState_stack_frame | |
| rintro β¨i, hiβ© | |
| have hv := congrArg Fin.val hi | |
| simp only [matrixStacks] at hv | |
| omega | |
| /-- Every execution premise is a trace of the concrete two-product program. -/ | |
| theorem matrixPair_eval (base : StructuredProgram.State 24 42) | |
| {a b : StructuredProgram.State 8 20} {n : β} | |
| (h : Trace MatrixPairProgram.program n | |
| (a.withPC (some MatrixPairProgram.program.entry)) (b.withPC none)) : | |
| Eval matrixPair n (install base a) (install base b) := | |
| eval_call_lifted MatrixPairProgram.program matrixRegisters matrixStacks base | |
| matrixRegisters_injective matrixStacks_injective h | |
| theorem install_eq (base : StructuredProgram.State 24 42) | |
| (localState : StructuredProgram.State 8 20) | |
| (hr : β i, localState.regs i = base.regs (matrixRegisters i)) | |
| (hs : β i, localState.stacks i = base.stacks (matrixStacks i)) : | |
| install base localState = base := by | |
| apply congrArgβ StructuredProgram.State.mk | |
| Β· funext j | |
| change (install base localState).regs j = base.regs j | |
| by_cases hj : j.val < 8 | |
| Β· let i : Fin 8 := β¨j.val, hjβ© | |
| have hi : matrixRegisters i = j := Fin.ext rfl | |
| rw [β hi, install_register, hr] | |
| Β· exact install_register_frame _ _ j (by omega) | |
| Β· funext j | |
| change (install base localState).stacks j = base.stacks j | |
| by_cases hj : j.val < 22 | |
| Β· exact install_stack_frame _ _ j hj | |
| Β· let i : Fin 20 := β¨j.val - 22, by omegaβ© | |
| have hi : matrixStacks i = j := by apply Fin.ext; dsimp [matrixStacks, i]; omega | |
| rw [β hi, install_stack, hs] | |
| theorem prepared_eq_install (p : β) [Fact p.Prime] {m N : β} | |
| (M : Fin m β Fin N β ZMod p) (C : Fin N β Fin N β ZMod p) | |
| (x : Fin N β ZMod p) (split tail : List Bool) (base : StructuredProgram.State 24 42) | |
| (hentry : BlockSearchLoop.CoefficientEntry (context p M) (tableBits p C) split base) : | |
| let a := BlockCoefficientAssembly.prepared | |
| (RecordPrograms.state BlockSearchLoop.coefficientPorts.toPorts | |
| base false true false tail (MatrixProgramSupport.words (vectorWords p x)).reverse) | |
| install a (MatrixEncodedProgram.initial p M C x (fun i => a.regs (matrixRegisters i))) = a := by | |
| dsimp only | |
| apply install_eq | |
| Β· intro i; rfl | |
| Β· rcases hentry with β¨h27, h36, h38, h39, h10, h41, h8, h9, hworkβ© | |
| intro i | |
| fin_cases i <;> | |
| simp [MatrixEncodedProgram.initial, MatrixPairProgram.wide, | |
| MatrixScalarProgram.scalarStacks, matrixStacks, | |
| BlockCoefficientAssembly.prepared, BlockCoefficientAssembly.transferState, | |
| BlockCoefficientFrames.copyState, StackRoutines.copyStacks, StackRoutines.transferStacks, | |
| RecordPrograms.state, BlockSearchLoop.coefficientPorts, context, | |
| h27, h36, h38, h39, h10, h41, hwork] | |
| theorem installed_final_stacks (p : β) [Fact p.Prime] {m N : β} | |
| (M : Fin m β Fin N β ZMod p) (C : Fin N β Fin N β ZMod p) | |
| (x : Fin N β ZMod p) (base : StructuredProgram.State 24 42) | |
| (ri rf : Fin 8 β Bool) : | |
| (install base (MatrixEncodedProgram.final p M C x rf)).stacks = | |
| BlockCoefficientAssembly.matrixStacks | |
| (install base (MatrixEncodedProgram.initial p M C x ri)).stacks | |
| (vectorWords p (BlockSearch.matrixMap C x)) (vectorWords p x) | |
| (vectorWords p (BlockSearch.matrixMap M (BlockSearch.matrixMap C x))) := by | |
| funext j | |
| by_cases hj : j.val < 22 | |
| Β· have h30 : j β 30 := by intro h; subst j; change 30 < 22 at hj; omega | |
| have h33 : j β 33 := by intro h; subst j; change 33 < 22 at hj; omega | |
| have h34 : j β 34 := by intro h; subst j; change 34 < 22 at hj; omega | |
| have h37 : j β 37 := by intro h; subst j; change 37 < 22 at hj; omega | |
| have h40 : j β 40 := by intro h; subst j; change 40 < 22 at hj; omega | |
| simp only [BlockCoefficientAssembly.matrixStacks, Function.update_of_ne h30, | |
| Function.update_of_ne h33, Function.update_of_ne h34, Function.update_of_ne h37, | |
| Function.update_of_ne h40, install_stack_frame _ _ _ hj] | |
| Β· let i : Fin 20 := β¨j.val - 22, by omegaβ© | |
| have hi : matrixStacks i = j := by apply Fin.ext; dsimp [matrixStacks, i]; omega | |
| rw [β hi, install_stack] | |
| clear_value i | |
| fin_cases i <;> | |
| simp [BlockCoefficientAssembly.matrixStacks, Function.update_apply, install_stack, | |
| MatrixEncodedProgram.initial, MatrixEncodedProgram.final, MatrixPairProgram.wide, | |
| MatrixScalarProgram.scalarStacks, | |
| MatrixZeroSemantics.frames_eq_words] | |
| /-- The actual arithmetic command satisfies the state effect needed by the | |
| coefficient iterator. The only premises are the encoded inputs and ordinary | |
| prime-field data; its execution is supplied by the two-product trace. -/ | |
| theorem coefficient_matrix_eval (p : β) [Fact p.Prime] {m N : β} | |
| (M : Fin m β Fin N β ZMod p) (C : Fin N β Fin N β ZMod p) | |
| (x : Fin N β ZMod p) (split tail : List Bool) (base : StructuredProgram.State 24 42) | |
| (hentry : BlockSearchLoop.CoefficientEntry (context p M) (tableBits p C) split base) : | |
| let a := BlockCoefficientAssembly.prepared | |
| (RecordPrograms.state BlockSearchLoop.coefficientPorts.toPorts | |
| base false true false tail (MatrixProgramSupport.words (vectorWords p x)).reverse) | |
| β n b, n β€ MatrixPairProgram.budget m N (MatrixWordEncoding.width p) β§ | |
| Eval matrixPair n a b β§ | |
| b.stacks = BlockCoefficientAssembly.matrixStacks a.stacks | |
| (vectorWords p (BlockSearch.matrixMap C x)) (vectorWords p x) | |
| (vectorWords p (BlockSearch.matrixMap M (BlockSearch.matrixMap C x))) β§ | |
| BlockCoefficientAssembly.ParentFrame a b := by | |
| dsimp only | |
| let a := BlockCoefficientAssembly.prepared | |
| (RecordPrograms.state BlockSearchLoop.coefficientPorts.toPorts | |
| base false true false tail (MatrixProgramSupport.words (vectorWords p x)).reverse) | |
| let ri : Fin 8 β Bool := fun i => a.regs (matrixRegisters i) | |
| obtain β¨n, hn, rf, htβ© := MatrixEncodedProgram.program_trace p M C x ri | |
| have hstart : install a (MatrixEncodedProgram.initial p M C x ri) = a := | |
| prepared_eq_install p M C x split tail base hentry | |
| have he := matrixPair_eval a ht | |
| rw [hstart] at he | |
| refine β¨n, install a (MatrixEncodedProgram.final p M C x rf), hn, he, ?_, ?_β© | |
| Β· rw [installed_final_stacks p M C x a ri rf, hstart] | |
| Β· intro j hj _ | |
| exact install_register_frame _ _ j hj | |
| theorem matrixBits_length (p : β) {m N : β} (M : Fin m β Fin N β ZMod p) : | |
| (matrixBits p M).length = m * (N * (2 * MatrixWordEncoding.width p + 1)) := by | |
| unfold matrixBits | |
| rw [β MatrixVectorProgram.matrixWords_eq_words_flatten] | |
| simpa only [matrixRows_length] using MatrixPairProgram.matrixWords_length | |
| (matrixRows p M) N (MatrixWordEncoding.width p) | |
| (fun row hr => β¨(matrixRows_shape p M row hr).1.symm, | |
| (matrixRows_shape p M row hr).2β©) | |
| theorem vectorBits_length (p : β) {N : β} (x : Fin N β ZMod p) : | |
| (MatrixProgramSupport.words (vectorWords p x)).length = | |
| N * (2 * MatrixWordEncoding.width p + 1) := by | |
| simpa only [vectorWords_length] using MatrixProgramSupport.words_length | |
| (vectorWords p x) (MatrixWordEncoding.width p) (vectorWords_width p x) | |
| theorem callbackCost_le (p : β) [Fact p.Prime] {m N : β} | |
| (M : Fin m β Fin N β ZMod p) (C : Fin N β Fin N β ZMod p) | |
| (d : Fin (N + 1)) (x : Fin N β ZMod p) : | |
| BlockCoefficientAssembly.callbackCost | |
| (MatrixPairProgram.budget m N (MatrixWordEncoding.width p)) (context p M) | |
| (tableBits p C) (List.replicate d.val true) | |
| (MatrixProgramSupport.words (vectorWords p x)) | |
| (vectorWords p (BlockSearch.matrixMap C x)) (vectorWords p x) | |
| (vectorWords p (BlockSearch.matrixMap M (BlockSearch.matrixMap C x))) β€ | |
| coefficientCost p m N := by | |
| have hd : d.val β€ N := by have := d.isLt; omega | |
| unfold BlockCoefficientAssembly.callbackCost BlockCoefficientAssembly.suffixCost | |
| simp only [MatrixZeroSemantics.frames_eq_words, vectorBits_length, tableBits, | |
| matrixBits_length, context, List.length_replicate] | |
| exact MatrixPairProgram.callback_bound m N (MatrixWordEncoding.width p) d.val hd | |
| theorem result_eq_test (p : β) [Fact p.Prime] {m N : β} | |
| (M : Fin m β Fin N β ZMod p) (C : Fin N β Fin N β ZMod p) | |
| (d : Fin (N + 1)) (test : List Bool β Bool) | |
| (htest : β x : Fin N β ZMod p, | |
| test (MatrixProgramSupport.words (vectorWords p x)) = | |
| BlockSearchProgram.coefficientTest M (BlockSearchEncoding.splitPair d C) | |
| (BlockSearchEncoding.headCoefficients d x) (BlockSearchEncoding.tailCoefficients d x)) | |
| (x : Fin N β ZMod p) : | |
| BlockCoefficientAssembly.result (List.replicate d.val true) | |
| (vectorWords p (BlockSearch.matrixMap C x)) (vectorWords p x) | |
| (vectorWords p (BlockSearch.matrixMap M (BlockSearch.matrixMap C x))) = | |
| test (MatrixProgramSupport.words (vectorWords p x)) := by | |
| rw [htest] | |
| unfold BlockCoefficientAssembly.result | |
| simp only [List.length_replicate, vectorWords, MatrixZeroSemantics.zero_encoded_vector, | |
| BlockSearchTailSemantics.zero_tail_words] | |
| exact BlockSearchPredicate.classify_eq_coefficientTest M d C x | |
| /-- Full concrete callback contract for the basis search. Arithmetic, | |
| framed zero scans, prefix skipping, classification, and work-buffer cleanup | |
| all execute fixed instruction tables with a common polynomial bound. -/ | |
| theorem coefficientContract (p : β) [Fact p.Prime] {m N : β} | |
| (M : Fin m β Fin N β ZMod p) (C : Fin N β Fin N β ZMod p) | |
| (d : Fin (N + 1)) (test : List Bool β Bool) | |
| (htest : β x : Fin N β ZMod p, | |
| test (MatrixProgramSupport.words (vectorWords p x)) = | |
| BlockSearchProgram.coefficientTest M (BlockSearchEncoding.splitPair d C) | |
| (BlockSearchEncoding.headCoefficients d x) (BlockSearchEncoding.tailCoefficients d x)) : | |
| BlockSearchLoop.CoefficientContract matrixPair (context p M) (tableBits p C) | |
| (List.replicate d.val true) test (fun _ => coefficientCost p m N) (validCoefficient p N) := by | |
| intro bits tail base hv hentry | |
| obtain β¨x, rflβ© := hv | |
| have hmatrix := coefficient_matrix_eval p M C x (List.replicate d.val true) tail base hentry | |
| obtain β¨n, next, hn, hs, hr, heβ© := BlockCoefficientAssembly.coefficient_eval | |
| matrixPair (context p M) (tableBits p C) (List.replicate d.val true) | |
| (MatrixProgramSupport.words (vectorWords p x)) tail base hentry | |
| (vectorWords p (BlockSearch.matrixMap C x)) (vectorWords p x) | |
| (vectorWords p (BlockSearch.matrixMap M (BlockSearch.matrixMap C x))) | |
| (by simp only [List.length_replicate, vectorWords_length]; have := d.isLt; omega) | |
| (MatrixPairProgram.budget m N (MatrixWordEncoding.width p)) hmatrix | |
| rw [result_eq_test p M C d test htest x] at he | |
| exact β¨n, next, hn.trans (callbackCost_le p M C d x), hs, hr, heβ© | |
| end SubexponentialSAT.Subexp.BlockCoefficientProgram | |
| end OAI.Algorithms | |