Download data/lean/OAI/Algorithms/SubexponentialSAT/SqrtLogSpace/DifferencePairs.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 12.4 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Algorithms/SubexponentialSAT/SqrtLogSpace/DifferencePairs.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Algorithms/SubexponentialSAT/SqrtLogSpace/DifferencePairs.lean
-
curl -L -o DifferencePairs.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Algorithms/SubexponentialSAT/SqrtLogSpace/DifferencePairs.lean
12.4 kB
| import OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.BooleanDifferences | |
| import Mathlib.Data.List.Nodup | |
| import Mathlib.Data.List.OfFn | |
| import Mathlib.Algebra.BigOperators.Fin | |
| import Mathlib.Tactic.Linarith | |
| import Mathlib.Tactic.Ring | |
| /-! Pruned finite-difference traversal. `none`, `some false`, and `some true` | |
| encode membership in neither set, in `E \ U`, and in `U`, respectively. | |
| `words` specifies visit order; the streaming fold does not construct it. -/ | |
| namespace OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.DifferencePairs | |
| open scoped BigOperators | |
| open BooleanDifferences | |
| def words : β β β β List (List (Option Bool)) | |
| | _, 0 => [] | |
| | 0, _ + 1 => [[]] | |
| | n + 1, J + 1 => | |
| (words n (J + 1)).map (none :: Β·) ++ | |
| (words n J).map (some false :: Β·) ++ | |
| (words n J).map (some true :: Β·) | |
| @[simp] theorem words_zero (n : β) : words n 0 = [] := by cases n <;> rfl | |
| @[simp] theorem mem_words (n J : β) (w : List (Option Bool)) : | |
| w β words n J β w.length = n β§ w.countP Option.isSome < J := by | |
| induction n generalizing J w with | |
| | zero => | |
| cases J with | |
| | zero => simp [words] | |
| | succ J => cases w <;> simp [words] | |
| | succ n ih => | |
| cases J with | |
| | zero => simp | |
| | succ J => | |
| cases w with | |
| | nil => simp [words] | |
| | cons b w => | |
| cases b with | |
| | none => simp [words, ih] | |
| | some b => cases b <;> simp [words, ih] | |
| theorem words_nodup (n J : β) : (words n J).Nodup := by | |
| induction n generalizing J with | |
| | zero => cases J <;> simp [words] | |
| | succ n ih => | |
| cases J with | |
| | zero => simp | |
| | succ J => | |
| simp only [words, List.nodup_append] | |
| refine β¨β¨(ih (J + 1)).map (by intro a b h; exact List.cons.inj h |>.2), | |
| (ih J).map (by intro a b h; exact List.cons.inj h |>.2), ?_β©, | |
| (ih J).map (by intro a b h; exact List.cons.inj h |>.2), ?_β© | |
| Β· intro a ha b hb hab | |
| obtain β¨u, _, rflβ© := List.mem_map.mp ha | |
| obtain β¨v, _, rflβ© := List.mem_map.mp hb | |
| simp at hab | |
| Β· intro a ha b hb hab | |
| obtain β¨v, _, rflβ© := List.mem_map.mp hb | |
| rcases List.mem_append.mp ha with ha | ha <;> | |
| obtain β¨u, _, rflβ© := List.mem_map.mp ha <;> | |
| simp at hab | |
| def pairCount : β β β β β | |
| | _, 0 => 0 | |
| | 0, _ + 1 => 1 | |
| | n + 1, J + 1 => pairCount n (J + 1) + 2 * pairCount n J | |
| @[simp] theorem pairCount_zero (n : β) : pairCount n 0 = 0 := by cases n <;> rfl | |
| @[simp] theorem words_length (n J : β) : (words n J).length = pairCount n J := by | |
| induction n generalizing J with | |
| | zero => cases J <;> rfl | |
| | succ n ih => | |
| cases J with | |
| | zero => rfl | |
| | succ J => simp [words, pairCount, ih]; omega | |
| theorem pairCount_pos (n J : β) (hJ : 0 < J) : 0 < pairCount n J := by | |
| have hw : List.replicate n none β words n J := by simp [List.countP_replicate, hJ] | |
| rw [β words_length] | |
| exact List.length_pos_iff.mpr (by intro h; simp [h] at hw) | |
| theorem pairCount_eq_sum (n J : β) : | |
| pairCount n J = β j β Finset.range J, 2 ^ j * n.choose j := by | |
| have pascal (n J : β) : | |
| (β j β Finset.range (J + 1), 2 ^ j * (n + 1).choose j) = | |
| (β j β Finset.range (J + 1), 2 ^ j * n.choose j) + | |
| 2 * β j β Finset.range J, 2 ^ j * n.choose j := by | |
| induction J with | |
| | zero => simp | |
| | succ J ih => | |
| rw [Finset.sum_range_succ _ (J + 1), ih, | |
| Nat.choose_succ_succ, Finset.sum_range_succ _ (J + 1), | |
| Finset.sum_range_succ _ J, pow_succ] | |
| ring | |
| induction n generalizing J with | |
| | zero => | |
| cases J with | |
| | zero => simp | |
| | succ J => simp [pairCount, Finset.sum_range_succ'] | |
| | succ n ih => | |
| cases J with | |
| | zero => simp | |
| | succ J => rw [pairCount, ih, ih, pascal] | |
| theorem pairFamily_card (n J : β) : (pairFamily n J).card = pairCount n J := by | |
| rw [pairFamily_eq_biUnion] | |
| have disjoint : (β(supportFamily n J) : Set (Finset (Fin n))).PairwiseDisjoint | |
| (fun E => E.powerset.image (fun U => (E, U))) := by | |
| intro E _ F _ hEF | |
| apply Finset.disjoint_left.mpr | |
| intro p hp hq | |
| obtain β¨U, _, hUβ© := Finset.mem_image.mp hp | |
| obtain β¨V, _, hVβ© := Finset.mem_image.mp hq | |
| exact hEF (congrArg Prod.fst (hU.trans hV.symm)) | |
| rw [Finset.card_biUnion disjoint] | |
| simp_rw [Finset.card_image_of_injective _ (fun _ _ h => (Prod.mk.inj h).2), | |
| Finset.card_powerset] | |
| have hdisjoint : (β(Finset.range J) : Set β).PairwiseDisjoint | |
| (fun j => (Finset.univ : Finset (Fin n)).powersetCard j) := by | |
| intro i _ j _ hij | |
| exact Finset.disjoint_powersetCard_of_ne hij _ _ | |
| have hs : supportFamily n J = (Finset.range J).disjiUnion | |
| (fun j => (Finset.univ : Finset (Fin n)).powersetCard j) hdisjoint := by | |
| ext E | |
| rw [mem_supportFamily, Finset.mem_disjiUnion] | |
| constructor | |
| Β· intro hE | |
| exact β¨E.card, Finset.mem_range.mpr hE, | |
| Finset.mem_powersetCard.mpr β¨Finset.subset_univ E, rflβ©β© | |
| Β· rintro β¨j, hj, hEβ© | |
| exact (Finset.mem_powersetCard.mp hE).2 βΈ Finset.mem_range.mp hj | |
| rw [hs, Finset.sum_disjiUnion, pairCount_eq_sum] | |
| apply Finset.sum_congr rfl | |
| intro j _ | |
| have h := Finset.sum_powersetCard j (Finset.univ : Finset (Fin n)) (fun a => 2 ^ a) | |
| rw [nsmul_eq_mul] at h | |
| simpa [Nat.mul_comm] using h | |
| theorem pairCount_le_square (n J : β) : pairCount n J β€ (supportFamily n J).card ^ 2 := by | |
| rw [β pairFamily_card] | |
| exact pairFamily_card_le n J | |
| def encodePair {n : β} (E U : Finset (Fin n)) : List (Option Bool) := | |
| List.ofFn (fun i => if i β E then some (decide (i β U)) else none) | |
| theorem countP_ofFn {Ξ± : Type*} (p : Ξ± β Bool) (n : β) (f : Fin n β Ξ±) : | |
| (List.ofFn f).countP p = β i, if p (f i) then 1 else 0 := by | |
| induction n with | |
| | zero => simp | |
| | succ n ih => | |
| simp only [List.ofFn_succ, List.countP_cons, Fin.sum_univ_succ, ih] | |
| omega | |
| @[simp] theorem encodePair_weight {n : β} (E U : Finset (Fin n)) : | |
| (encodePair E U).countP Option.isSome = E.card := by | |
| rw [encodePair, countP_ofFn] | |
| have h (i : Fin n) : | |
| (if (if i β E then some (decide (i β U)) else none).isSome then 1 else 0) = | |
| (if i β E then 1 else 0 : β) := by | |
| by_cases hi : i β E <;> simp [hi] | |
| simp_rw [h] | |
| simp | |
| @[simp] theorem encodePair_outer_count {n : β} (E U : Finset (Fin n)) : | |
| ((encodePair E U).map Option.isSome).reverse.count true = E.card := by | |
| simp [List.count_eq_countP, List.countP_map, Function.comp_def] | |
| theorem encodePair_inner_count {n : β} (E U : Finset (Fin n)) (hU : U β E) : | |
| ((encodePair E U).map (fun a => a.getD false)).reverse.count true = U.card := by | |
| have hm : (encodePair E U).map (fun a => a.getD false) = | |
| List.ofFn (fun i => decide (i β U)) := by | |
| rw [encodePair, List.map_ofFn] | |
| congr 1 | |
| funext i | |
| by_cases hi : i β E | |
| Β· simp [hi] | |
| Β· have hu : i β U := fun h => hi (hU h) | |
| simp [hi, hu] | |
| rw [hm, List.count_reverse, List.count_eq_countP, countP_ofFn] | |
| simp | |
| theorem encodePair_injective {n : β} {E U F V : Finset (Fin n)} | |
| (hU : U β E) (hV : V β F) (h : encodePair E U = encodePair F V) : | |
| E = F β§ U = V := by | |
| have hf := List.ofFn_injective h | |
| have hE : E = F := by | |
| ext i | |
| have hi := congrArg Option.isSome (congrFun hf i) | |
| by_cases he : i β E <;> by_cases hf : i β F <;> simp_all | |
| subst F | |
| refine β¨rfl, ?_β© | |
| ext i | |
| have hi := congrFun hf i | |
| by_cases he : i β E | |
| Β· simpa [he] using hi | |
| Β· have hu : i β U := fun h => he (hU h) | |
| have hv : i β V := fun h => he (hV h) | |
| simp [hu, hv] | |
| theorem encoded_pairs_eq_words (n J : β) : | |
| (pairFamily n J).image (fun p => encodePair p.1 p.2) = (words n J).toFinset := by | |
| have hsub : (pairFamily n J).image (fun p => encodePair p.1 p.2) β | |
| (words n J).toFinset := by | |
| intro w hw | |
| obtain β¨β¨E, Uβ©, hEU, rflβ© := Finset.mem_image.mp hw | |
| simp only [List.mem_toFinset, mem_words] | |
| exact β¨by simp [encodePair], by rw [encodePair_weight]; exact (mem_pairFamily.mp hEU).1β© | |
| have hinj : Set.InjOn (fun p : Finset (Fin n) Γ Finset (Fin n) => encodePair p.1 p.2) | |
| (pairFamily n J : Set _) := by | |
| intro p hp q hq heq | |
| rcases p with β¨E, Uβ© | |
| rcases q with β¨F, Vβ© | |
| obtain β¨rfl, rflβ© := encodePair_injective (mem_pairFamily.mp hp).2 | |
| (mem_pairFamily.mp hq).2 heq | |
| rfl | |
| apply Finset.eq_of_subset_of_card_le hsub | |
| rw [Finset.card_image_iff.mpr hinj, List.toFinset_card_of_nodup (words_nodup n J), | |
| words_length, pairFamily_card] | |
| def calls : β β β β β | |
| | _, 0 => 1 | |
| | 0, _ + 1 => 1 | |
| | n + 1, J + 1 => 1 + calls n (J + 1) + 2 * calls n J | |
| @[simp] theorem calls_zero (n : β) : calls n 0 = 1 := by cases n <;> rfl | |
| theorem calls_le (n J : β) (hJ : 0 < J) : | |
| calls n J β€ (3 * n + 1) * pairCount n J := by | |
| induction n generalizing J with | |
| | zero => cases J <;> simp_all [calls, pairCount] | |
| | succ n ih => | |
| cases J with | |
| | zero => omega | |
| | succ J => | |
| have hl := ih (J + 1) (Nat.succ_pos J) | |
| have hp := pairCount_pos n (J + 1) (Nat.succ_pos J) | |
| cases J with | |
| | zero => simp only [calls, pairCount, calls_zero, pairCount_zero]; nlinarith | |
| | succ J => | |
| have hr := ih (J + 1) (Nat.succ_pos J) | |
| simp only [calls, pairCount] | |
| nlinarith | |
| /-- Runtime recursion retains one prefix and invokes its callback immediately. | |
| The three branches share the accumulator; no list of completed pairs is built. -/ | |
| def fold {Ξ± : Type*} (visit : List (Option Bool) β Ξ± β Ξ±) : | |
| β β β β List (Option Bool) β Ξ± β Ξ± | |
| | _, 0, _, acc => acc | |
| | 0, _ + 1, pre, acc => visit pre acc | |
| | n + 1, J + 1, pre, acc => | |
| let acc := fold visit n (J + 1) (pre ++ [none]) acc | |
| let acc := fold visit n J (pre ++ [some false]) acc | |
| fold visit n J (pre ++ [some true]) acc | |
| theorem fold_eq_foldl {Ξ± : Type*} (visit : List (Option Bool) β Ξ± β Ξ±) | |
| (n J : β) (pre : List (Option Bool)) (acc : Ξ±) : | |
| fold visit n J pre acc = | |
| (words n J).foldl (fun a w => visit (pre ++ w) a) acc := by | |
| induction n generalizing J pre acc with | |
| | zero => cases J <;> simp [fold, words] | |
| | succ n ih => | |
| cases J with | |
| | zero => simp [fold] | |
| | succ J => simp [fold, words, ih, List.foldl_append, List.foldl_map] | |
| /-- The reference visit order contains each admissible pair exactly once. -/ | |
| theorem sum_words_eq_sum_pairs {A : Type*} [AddCommMonoid A] | |
| (n J : β) (f : List (Option Bool) β A) : | |
| ((words n J).map f).sum = β p β pairFamily n J, f (encodePair p.1 p.2) := by | |
| classical | |
| rw [β List.sum_toFinset f (words_nodup n J), β encoded_pairs_eq_words] | |
| apply Finset.sum_image | |
| intro p hp q hq heq | |
| obtain β¨he, huβ© := encodePair_injective (mem_pairFamily.mp hp).2 | |
| (mem_pairFamily.mp hq).2 heq | |
| exact Prod.ext he hu | |
| /-- A decoded accumulator may have a validity condition; it is retained at | |
| all callback boundaries, while the decoded result is the exact pair sum. -/ | |
| theorem fold_decode_sum {A B : Type*} [AddCommMonoid B] | |
| (visit : List (Option Bool) β A β A) (decode : A β B) (P : A β Prop) | |
| (f : List (Option Bool) β B) (n J : β) (acc : A) (ha : P acc) | |
| (step : β w β words n J, β a, P a β | |
| P (visit w a) β§ decode (visit w a) = decode a + f w) : | |
| P (fold visit n J [] acc) β§ | |
| decode (fold visit n J [] acc) = | |
| decode acc + β p β pairFamily n J, f (encodePair p.1 p.2) := by | |
| have hlist : β (ws : List (List (Option Bool))) (a : A), P a β | |
| (β w β ws, β b, P b β P (visit w b) β§ decode (visit w b) = decode b + f w) β | |
| P (ws.foldl (fun b w => visit w b) a) β§ | |
| decode (ws.foldl (fun b w => visit w b) a) = decode a + (ws.map f).sum := by | |
| intro ws | |
| induction ws with | |
| | nil => intro a ha _; exact β¨ha, by simpβ© | |
| | cons w ws ih => | |
| intro a ha hs | |
| obtain β¨ha', heβ© := hs w (by simp) a ha | |
| obtain β¨hf, hvβ© := ih (visit w a) ha' (fun v hv => hs v (by simp [hv])) | |
| refine β¨hf, ?_β© | |
| simpa [he, add_assoc] using hv | |
| simpa [fold_eq_foldl, sum_words_eq_sum_pairs] using hlist (words n J) acc ha step | |
| end OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.DifferencePairs | |