Lean / Reader
Lean readerGridExamples.lean
Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports GridLift, which it does not have: you can read it there, but it will not check. Build the project itself to check it.
import GridLiftnamespace ReciprocalGridopen Finsettheorem scale_harmonic (p : ℕ) (hp : 0 < p) (A : Finset ℕ) :harmonic (A.image (p * ·)) = harmonic A / (p : ℚ) := byrw [harmonic, sum_image (fun _ _ _ _ h => Nat.eq_of_mul_eq_mul_left hp h), harmonic, Finset.sum_div]apply Finset.sum_congr rflintro n _simp [mul_comm, div_eq_mul_inv]theorem disjoint_prime_copy {D p : ℕ} (h : ¬p ∣ D) {U V : Finset ℕ}(hu : ∀ n ∈ U, n ∣ D) : Disjoint U (V.image (p * ·)) := byapply Finset.disjoint_left.mprintro n hn hvobtain ⟨v, _, rfl⟩ := Finset.mem_image.mp hvexact h (dvd_trans (dvd_mul_right p v) (hu _ hn))theorem disjoint_prime_copies {D p q : ℕ} (hp : Nat.Prime p) (hq : Nat.Prime q)(hne : p ≠ q) (h : ¬p ∣ D) {U V : Finset ℕ} (hv : ∀ n ∈ V, n ∣ D) :Disjoint (U.image (p * ·)) (V.image (q * ·)) := byapply Finset.disjoint_left.mprintro n hn hn'obtain ⟨u, _, rfl⟩ := Finset.mem_image.mp hnobtain ⟨v, hV, he⟩ := Finset.mem_image.mp hn'have hd : p ∣ q*v := he ▸ dvd_mul_right p urcases hp.dvd_mul.mp hd with h₁ | h₂· exact hne ((Nat.prime_dvd_prime_iff_eq hp hq).mp h₁)· exact h (h₂.trans (hv v hV))theorem glue_harmonic {D : ℕ} (S A₀ : Finset ℕ) (A : ℕ → Finset ℕ)(hs : ∀ p ∈ S, Nat.Prime p) (hnew : ∀ p ∈ S, ¬p ∣ D)(h₀ : ∀ n ∈ A₀, n ∣ D) (hA : ∀ p ∈ S, ∀ n ∈ A p, n ∣ D) :harmonic (A₀ ∪ S.biUnion (fun p => (A p).image (p * ·))) =harmonic A₀ + ∑ p ∈ S, harmonic (A p) / (p : ℚ) := byhave hd : Disjoint A₀ (S.biUnion (fun p => (A p).image (p * ·))) :=(Finset.disjoint_biUnion_right ..).mpr fun p hp => disjoint_prime_copy (hnew p hp) h₀rw [append_mass hd]congr 1have hh : (↑S : Set ℕ).PairwiseDisjoint (fun p => (A p).image (p * ·)) := byintro p hp q hq hneexact disjoint_prime_copies (hs p hp) (hs q hq) hne (hnew p hp) (hA q hq)unfold harmonicrw [Finset.sum_biUnion hh]exact Finset.sum_congr rfl fun p hp => scale_harmonic p (hs p hp).pos (A p)theorem grid_value {D a b₀ : ℕ} (S : Finset ℕ) (b : ℕ → ℕ) (hd : 0 < D)(hs : ∀ p ∈ S, Nat.Prime p)(he : b₀*modulus S + ∑ p ∈ S, cofactor S p*b p = a) :(b₀:ℚ)/D + ∑ p ∈ S, ((b p : ℚ)/D)/(p:ℚ) = (a:ℚ)/(D*(modulus S:ℚ)) := byhave hP : 0 < modulus S := Finset.prod_pos fun p hp => (hs p hp).poshave hD' : (D : ℚ) ≠ 0 := by exact_mod_cast hd.ne'have hP' : (modulus S : ℚ) ≠ 0 := by exact_mod_cast hP.ne'apply (eq_div_iff (mul_ne_zero hD' hP')).mprrw [add_mul, Finset.sum_mul]have hw : ∀ p ∈ S, (b p : ℚ)/D/p*(D*(modulus S:ℚ)) = (cofactor S p:ℚ)*b p := byintro p hphave hp' : (p : ℚ) ≠ 0 := by exact_mod_cast (hs p hp).ne_zerorw [← cofactor_mul hp, Nat.cast_mul]field_simprw [Finset.sum_congr rfl hw]convert (show (b₀ : ℚ)*(modulus S:ℚ) + ∑ p ∈ S, (cofactor S p:ℚ)*(b p:ℚ) = a by exact_mod_cast he) using 1field_simptheorem lift_representation {D m a : ℕ} {S U : Finset ℕ} (hd : 0 < D)(hu : ∀ n ∈ U, n ∣ D) (hs : ∀ p ∈ S, Nat.Prime p) (hnew : ∀ p ∈ S, ¬p ∣ D)(hw : ∀ p ∈ S, 4*p ≤ D) (hm : 2*m ≤ D)(old : ∀ n, m ≤ n → n ≤ D → ∃ A ⊆ U, harmonic A = (n:ℚ)/D)(hlo : m*modulus S + ∑ p ∈ S, cofactor S p*D ≤ a) (hhi : a ≤ D*modulus S) :∃ V ⊆ U ∪ S.biUnion (fun p => U.image (p * ·)), harmonic V = (a:ℚ)/(D*(modulus S:ℚ)) := byclassicalobtain ⟨b₀,b,h₁,h₂,hb,he⟩ := bounded_grid_digits hs hw hm hlo hhiobtain ⟨A₀,hA₀,hv₀⟩ := old b₀ h₁ h₂have hex : ∀ p, ∃ A ⊆ U, p ∈ S → harmonic A = (b p:ℚ)/D := byintro pby_cases h : p ∈ S· obtain ⟨A,hA,hv⟩ := old _ (hb p h).1 (hb p h).2exact ⟨A,hA,fun _ => hv⟩· exact ⟨∅,Finset.empty_subset _, fun hp => False.elim (h hp)⟩choose A hA hv using hexrefine ⟨A₀ ∪ S.biUnion (fun p => (A p).image (p * ·)), Finset.union_subset_union hA₀ ?_, ?_⟩· apply Finset.biUnion_subset.mprintro p hpexact (Finset.image_subset_image (hA p)).trans (Finset.subset_biUnion_of_mem (fun p => U.image (p * ·)) hp)· rw [glue_harmonic S A₀ A hs hnew (fun n hn => hu n (hA₀ hn)) (fun p _ n hn => hu n (hA p hn)), hv₀]rw [Finset.sum_congr rfl (fun (p : ℕ) (hp : p ∈ S) => congrArg (fun z : ℚ => z / (p:ℚ)) (hv p hp))]exact grid_value S b hd hs heend ReciprocalGrid