File size: 12,384 Bytes
4a56cd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | 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
|