Download data/lean/OAI/Computability/MaxCut/Estimates/Divides.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 6.47 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/MaxCut/Estimates/Divides.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Computability/MaxCut/Estimates/Divides.lean
-
curl -L -o Divides.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/MaxCut/Estimates/Divides.lean
6.47 kB
| import Mathlib | |
| import OAI.Computability.MaxCut.Estimates.Rename | |
| namespace OAI | |
| noncomputable section | |
| namespace OptimalMaxCut.CounterMachine.Expr | |
| open scoped BigOperators | |
| open Finset | |
| attribute [local instance] Classical.propDecidable | |
| theorem indicator_and (P Q : Prop) [Decidable P] [Decidable Q] : | |
| (if P then (1:β) else 0)*(if Q then 1 else 0) = if P β§ Q then 1 else 0 := by | |
| by_cases hP : P <;> by_cases hQ : Q <;> simp [hP,hQ] | |
| def divides (a b : Expr) : Expr := .zero (remainder b a) | |
| @[simp] theorem divides_eval (a b : Expr) (input : List Bool) (args : β β β) : | |
| (divides a b).eval input args = if a.eval input args β£ b.eval input args then 1 else 0 := by | |
| simp [divides, eval, Nat.dvd_iff_mod_eq_zero] | |
| def prime (n : Expr) : Expr := .mul (le (.const 2) n) | |
| (.zero (.sum n (.mul (le (.const 2) (.arg 0)) (divides (.arg 0) (n.rename Nat.succ))))) | |
| @[simp] theorem prime_eval (n : Expr) (input : List Bool) (args : β β β) : | |
| (prime n).eval input args = if (n.eval input args).Prime then 1 else 0 := by | |
| simp only [prime, eval, le_eval, divides_eval, rename_eval, bind] | |
| have h : (β i β range (n.eval input args), | |
| (if 2 β€ i then 1 else 0) * (if i β£ n.eval input args then 1 else 0) : β) = 0 β | |
| β i, 2 β€ i β i < n.eval input args β Β¬ i β£ n.eval input args := by | |
| rw [sum_eq_zero_iff_of_nonneg (fun _ _ => Nat.zero_le _)] | |
| constructor | |
| Β· intro h i hi hn hd | |
| have hh := h i (mem_range.mpr hn) | |
| simp [hi, hd] at hh | |
| Β· intro h i hi | |
| by_cases h2 : 2 β€ i | |
| Β· simp [h2, h i h2 (mem_range.mp hi)] | |
| Β· simp [h2] | |
| have hx : (if 2 β€ n.eval input args then 1 else 0) * | |
| (if (β i β range (n.eval input args), | |
| (if 2 β€ i then 1 else 0)*(if i β£ n.eval input args then 1 else 0) : β)=0 then 1 else 0) = | |
| (if (n.eval input args).Prime then 1 else 0) := by | |
| simp only [h] | |
| exact (indicator_and _ _).trans (by simp only [Nat.prime_def_lt']) | |
| exact hx | |
| def powTwo (n : Expr) : Expr := .mul (positive n) | |
| (.zero (.sum (.add n (.const 1)) | |
| (.mul (prime (.arg 0)) (.mul (divides (.arg 0) (n.rename Nat.succ)) | |
| (.zero (equal (.arg 0) (.const 2))))))) | |
| @[simp] theorem powTwo_eval (n : Expr) (input : List Bool) (args : β β β) : | |
| (powTwo n).eval input args = if β k : β, n.eval input args = 2^k then 1 else 0 := by | |
| let N := n.eval input args | |
| have h : (β i β range (N+1), (if i.Prime then 1 else 0)* | |
| ((if i β£ N then 1 else 0)*(if (if i=2 then 1 else 0)=0 then 1 else 0)) : β) = 0 β | |
| β i β€ N, i.Prime β i β£ N β i=2 := by | |
| rw [sum_eq_zero_iff_of_nonneg (fun _ _ => Nat.zero_le _)] | |
| constructor | |
| Β· intro h i hi hp hd | |
| have hh := h i (mem_range.mpr (by omega)) | |
| by_contra hn | |
| simp [hp,hd,hn] at hh | |
| Β· intro h i hi | |
| by_cases hp : i.Prime <;> by_cases hd : i β£ N <;> simp_all | |
| exact h i (by omega) hp hd | |
| have hh : (N β 0 β§ β i β€ N, i.Prime β i β£ N β i=2) β β k : β, N=2^k := by | |
| constructor | |
| Β· rintro β¨hn,hpβ© | |
| exact β¨_, Nat.eq_prime_pow_of_unique_prime_dvd hn (fun {d} hd hdn => hp d (Nat.le_of_dvd (by omega) hdn) hd hdn)β© | |
| Β· rintro β¨k,hkβ© | |
| rw [hk] | |
| refine β¨by positivity, ?_β© | |
| intro i hi hp hd | |
| exact (Nat.prime_dvd_prime_iff_eq hp Nat.prime_two).mp (hp.dvd_of_dvd_pow hd) | |
| simp only [powTwo, eval, positive_eval, prime_eval, divides_eval, equal_eval, rename_eval, bind] | |
| change (if N β 0 then 1 else 0)*(if _=0 then 1 else 0) = if β k, N=2^k then 1 else 0 | |
| have hx : (if N β 0 then 1 else 0)*(if | |
| (β i β range (N+1), (if i.Prime then 1 else 0)* | |
| ((if i β£ N then 1 else 0)*(if (if i=2 then 1 else 0)=0 then 1 else 0)) : β) = 0 | |
| then 1 else 0) = (if β k, N=2^k then 1 else 0) := by | |
| simp only [h] | |
| exact (indicator_and _ _).trans (by simp only [hh]) | |
| exact hx | |
| /-- Cardinality of powers of two not exceeding an integer, hence its bit length. -/ | |
| def size (n : Expr) : Expr := .sum (.add n (.const 1)) (powTwo (.arg 0)) | |
| theorem count_powers (N : β) : | |
| (β i β range (N+1), if β k : β, i=2^k then (1:β) else 0) = N.size := by | |
| rw [sum_boole] | |
| have hb : {i β range (N+1) | β k : β, i=2^k} = (range N.size).image (fun k => 2^k) := by | |
| ext i | |
| simp only [mem_filter, mem_range, mem_image] | |
| constructor | |
| Β· rintro β¨hi,k,rflβ© | |
| exact β¨k, Nat.lt_size.mpr (by omega), rflβ© | |
| Β· rintro β¨k,hk,rflβ© | |
| exact β¨by have := Nat.lt_size.mp hk; omega, k, rflβ© | |
| rw [hb, card_image_of_injective _ (by intro a b h; exact (Nat.pow_right_injective (by omega : 2 β€ 2)) h), card_range, Nat.cast_id] | |
| @[simp] theorem size_eval (n : Expr) (input : List Bool) (args : β β β) : | |
| (size n).eval input args = (n.eval input args).size := by | |
| simp only [size, eval, powTwo_eval, bind] | |
| exact count_powers (n.eval input args) | |
| /-- Locate the unique power of two having the requested binary digit index. | |
| Search is bounded by n+1; no exponentially large unary number is ever built. -/ | |
| def digitPower (n i : Expr) : Expr := .sum (.add n (.const 1)) | |
| (.mul (.arg 0) (.mul (powTwo (.arg 0)) | |
| (equal (size (.arg 0)) (.add (i.rename Nat.succ) (.const 1))))) | |
| theorem digitPower_eval (n i : Expr) (input : List Bool) (args : β β β) | |
| (hi : i.eval input args < (n.eval input args).size) : | |
| (digitPower n i).eval input args = 2^(i.eval input args) := by | |
| simp only [digitPower, eval, powTwo_eval, size_eval, equal_eval, rename_eval, bind] | |
| let N := n.eval input args | |
| let I := i.eval input args | |
| have hpow : 2^I β range (N+1) := mem_range.mpr (by have hh := Nat.lt_size.mp hi; change 2^I β€ N at hh; omega) | |
| rw [sum_eq_single_of_mem (2^I) hpow] | |
| Β· simp [Nat.size_pow, I] | |
| Β· intro b hb hne | |
| by_cases hp : β k : β, b=2^k | |
| Β· obtain β¨k,rflβ© := hp | |
| have hk : k β I := by rintro rfl; exact hne rfl | |
| simp only [Nat.size_pow, Nat.add_right_cancel_iff] | |
| have hk' : k β i.eval input (fun j => args j) := hk | |
| simp [hk'] | |
| Β· simp [hp] | |
| def digit (n i : Expr) : Expr := remainder (quotient n (digitPower n i)) (.const 2) | |
| theorem digit_eval (n i : Expr) (input : List Bool) (args : β β β) | |
| (hi : i.eval input args < (n.eval input args).size) : | |
| (digit n i).eval input args = n.eval input args / 2^(i.eval input args) % 2 := by | |
| simp [digit, eval, digitPower_eval n i input args hi] | |
| end OptimalMaxCut.CounterMachine.Expr | |
| end | |
| end OAI | |