Lean / Reader
Lean readerLazyLucasEncoding.lean
Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports LazyLucasCore, which it does not have: you can read it there, but it will not check. Build the project itself to check it.
import LazyLucasCorenamespace LazyLucas/-- Digit words are most-significant first; leading zeroes may pad the width. -/def weight : List Bool → ℕ| [] => 0| b::l => (if b then lucas l.length else 0) + weight ldef mask : List Bool → ℕ| [] => 0| b::l => (if b then 2^l.length else 0) + mask lprivate def build : ℕ → ℕ → List Bool| 0, _ => []| 1, n => [decide (n=2)]| 2, n => if n=0 then [false,false] else if n=1 then [true,false] else if n=3 then [true,true] else [false,true]| k+3, n => if n≤cap (k+2) then false::build (k+2) n else true::build (k+2) (n-lucas (k+2))private theorem build_step {k n : ℕ} (h : 2≤k) : build (k+1) n =if n≤cap k then false::build k n else true::build k (n-lucas k) := byobtain ⟨u,rfl⟩ := Nat.exists_eq_add_of_le hrw [Nat.add_comm 2 u]; rflprivate theorem len_build (k n : ℕ) : (build k n).length=k := byinduction k using Nat.strong_induction_on generalizing n with | h k ih =>rcases k with _|_|_|k· rfl· rfl· simp only [build]; split_ifs <;> rfl· rw [build_step (by omega)]; split <;> simp [ih (k+2) (by omega)]private theorem ones_build {k n : ℕ} (hk : 2≤k) : (build k n).count true = bounded k n := byinduction k, hk using Nat.le_induction generalizing n with| base => simp only [build,bounded_two]; split_ifs <;> first | rfl | omega| succ k hk ih => rw [build_step hk,bounded_step hk]; split <;> simp [ih,Nat.add_comm]private theorem weight_cap (w : List Bool) : weight w ≤ cap w.length := byinduction w with| nil => decide| cons b w ih => cases b <;> simp [weight,cap] <;> omegaprivate theorem mask_lt (w : List Bool) : mask w < 2^w.length := byinduction w with| nil => decide| cons b w ih => cases b <;> simp [mask,pow_succ] <;> omegaprivate theorem lucas_pos (k : ℕ) : 0<lucas k := byinduction k using Nat.strong_induction_on with | h k ih =>rcases k with _|_|k· decide· decide· rw [lucas]; have := ih k (by omega); omegaprivate theorem le_cap (k : ℕ) : k≤cap k := byinduction k with| zero => decide| succ k ih => rw [cap]; have := lucas_pos k; omegaprivate theorem cap_mono : Monotone cap := monotone_nat_of_le_succ fun k => by simp [cap]private theorem lucas_le_cap {k : ℕ} (hk : 2≤k) : lucas k≤cap k := byobtain ⟨u,rfl⟩ := Nat.exists_eq_add_of_le hkrw [Nat.add_comm 2 u,lucas,cap,cap]; omegaprivate theorem build_spec {k n : ℕ} (hk : 2≤k) (hn : n≤cap k) :weight (build k n)=n ∧ ∀ w : List Bool, w.length=k → weight w=n → mask (build k n)≤mask w := byinduction k, hk using Nat.le_induction generalizing n with| base =>change n≤3 at hnconstructor· interval_cases n <;> decide· intro w hw hvrcases w with _|⟨a,w⟩ <;> simp at hwrcases w with _|⟨b,w⟩ <;> simp at hwsubst wcases a <;> cases b <;> subst n <;> decide| succ k hk ih =>rw [build_step hk]by_cases low : n≤cap k· rw [if_pos low]obtain ⟨hval, hbest⟩ := ih lowrefine ⟨by simpa [weight] using hval,?_⟩rintro (_|⟨b,t⟩) hlen hv· simp at hlenhave ht : t.length=k := by simpa using hlencases b· simpa [mask] using hbest t ht (by simpa [weight] using hv)· have h := mask_lt (build k n)rw [len_build] at hsimp [mask,ht]; omega· rw [if_neg low]have hl := lucas_le_cap hkhave hn' : n-lucas k≤cap k := by rw [cap] at hn; omegaobtain ⟨hval,hbest⟩ := ih hn'refine ⟨by simp [weight,len_build,hval]; omega,?_⟩rintro (_|⟨b,t⟩) hlen hv· simp at hlenhave ht : t.length=k := by simpa using hlencases b· have h := weight_cap trw [ht] at hsimp [weight] at hv; omega· simp [weight,ht] at hvhave e : weight t=n-lucas k := by omegasimpa [mask,ht,len_build] using hbest t ht eprivate theorem stable {k n : ℕ} (hk : 2≤k) (hn : n≤cap k) (m : ℕ) (hm : k≤m) :mask (build m n)=mask (build k n) ∧ bounded m n=bounded k n := byinduction m, hm using Nat.le_induction with| base => simp| succ m hm ih =>have hl : n≤cap m := hn.trans (cap_mono hm)rw [build_step (by omega),bounded_step (by omega),if_pos hl,if_pos hl]simpa [mask] using ih/-- The computed minimal Lucas word, padded with high-order zeros. -/def lazyWord (n : ℕ) : List Bool := build (n+2) ndef digits (n : ℕ) : ℕ := (lazyWord n).count truetheorem digits_eq_bounded {k n : ℕ} (hk : 2≤k) (hn : n≤cap k) : digits n=bounded k n := byunfold digits lazyWordrw [ones_build (by omega),←(stable hk hn (max k (n+2)) (by omega)).2]exact (stable (k:=n+2) (by omega) (by have := le_cap (n+2); omega) _ (by omega)).2.symmtheorem lazyWord_weight (n : ℕ) : weight (lazyWord n)=n :=(build_spec (k:=n+2) (by omega) (by have := le_cap (n+2); omega)).1private theorem pad (w : List Bool) (d : ℕ) :mask (List.replicate d false ++ w) = mask w ∧ weight (List.replicate d false ++ w) = weight w := byinduction d with| zero => simp| succ d ih => simpa [List.replicate_succ,mask,weight] using ih/-- Among all finite binary words with Lucas value n this one has smallestordinary base-two mask. -/theorem lazyWord_min {n : ℕ} {w : List Bool} (hw : weight w=n) : mask (lazyWord n) ≤ mask w := bylet k := max (n+2) w.lengthhave hk : n+2≤k := le_max_left ..have hn : n≤cap (n+2) := by have := le_cap (n+2); omegahave fst := (stable (k:=n+2) (by omega) hn k hk).1have h := (build_spec (k:=k) (n:=n) (by omega) (hn.trans (cap_mono hk))).2let w' := List.replicate (k-w.length) false ++ whave hp := pad w (k-w.length)have hl : w'.length=k := by dsimp [w']; simp; have := le_max_right (n+2) w.length; omegahave h' := h w' hl (hp.2.trans hw)rwa [fst,hp.1] at h'private theorem exchange (u v : List Bool) :weight (u++true::false::false::v) = weight (u++false::true::true::v) ∧mask (u++false::true::true::v) < mask (u++true::false::false::v) := byinduction u with| nil =>have hh : 0 < 2^v.length := by positivitysimp [weight, mask, lucas, pow_succ]omega| cons b u ih =>simp only [List.cons_append, weight, mask, List.length_append, List.length_cons]obtain ⟨he,ht⟩ := ihconstructor <;> omegatheorem lazyWord_no100 (n : ℕ) (u v : List Bool) : lazyWord n ≠ u++true::false::false::v := byintro heobtain ⟨hval,hlt⟩ := exchange u vhave hv : weight (u++false::true::true::v)=n := hval.symm.trans (he ▸ lazyWord_weight n)have hmask := lazyWord_min hvrw [he] at hmaskomega/-- A pair of consecutive zeroes below the first set bit contains the localreplacement pattern 100 somewhere in the zero run. -/private theorem interior_contains100 (u v : List Bool) (h : true ∈ u) :∃ a b : List Bool, u++false::false::v = a++true::false::false::b := byinduction u using List.reverseRecOn generalizing v with| nil => simp at h| append_singleton u t ih =>cases t· simpa [List.append_assoc] using ih (false::v) (by simpa using h)· exact ⟨u,v,by simp⟩/-- Leading zeros carry no value and can be discarded when applying thesource program's `IntegerDigits` convention. -/private def strip : List Bool → List Bool| false::s => strip s| w => wprivate theorem strip_spec (w : List Bool) : mask (strip w)=mask w ∧ weight (strip w)=weight w ∧(strip w).count true = w.count true ∧ ∃ k, w=List.replicate k false ++ strip w := byinduction w with| nil => exact ⟨rfl,rfl,rfl,0,rfl⟩| cons b w ih =>cases b· obtain ⟨hm,hv,hc,k,hk⟩ := ihexact ⟨by simpa [strip,mask] using hm,by simpa [strip,weight] using hv,by simpa [strip] using hc,k+1,by simpa only [strip,List.replicate_succ,List.cons_append] using congrArg (List.cons false) hk⟩· exact ⟨rfl,rfl,rfl,0,rfl⟩private theorem strip_first (w : List Bool) : strip w=[] ∨ ∃ t, strip w=true::t := byinduction w with| nil => exact Or.inl rfl| cons b w ih => cases b; exact ih; exact Or.inr ⟨w,rfl⟩def noDoubleZero (w : List Bool) : Prop := ∀ u v : List Bool, w≠u++false::false::vdef canonicalWord (n : ℕ) : List Bool := strip (lazyWord n)/-- The canonical word is accepted by the source program's two-zero filterand is the first in its enumeration of ordinary binary masks. -/theorem source_word (n : ℕ) :weight (canonicalWord n) = n ∧ noDoubleZero (canonicalWord n) ∧(∀ w, weight w=n → mask (canonicalWord n)≤mask w) ∧(canonicalWord n).count true=digits n := byobtain ⟨hm,hv,hc,k,hk⟩ := strip_spec (lazyWord n)refine ⟨hv.trans (lazyWord_weight n),?_,fun w hw => hm.le.trans (lazyWord_min hw),hc⟩intro u v hehave hf := strip_first (lazyWord n)change strip (lazyWord n) = u++false::false::v at hehave hm : true∈u := byrcases hf with hz|⟨t,ht⟩· rw [hz] at he; simp at he· rw [ht] at hecases u with| nil => simp at he| cons b u => simp only [List.cons_append,List.cons.injEq] at he; cases he.1; simpobtain ⟨a,b,hab⟩ := interior_contains100 u v hmapply lazyWord_no100 n (List.replicate k false ++ a) brw [hk,he,hab,List.append_assoc]#print axioms source_word#print axioms digits_eq_boundedprivate theorem max_small (w : List Bool) (h : w.length≤4) : w.count true ≤ bounded w.length (weight w) := bymatch w with| [] => decide| [a] => cases a <;> decide| [a,b] => cases a <;> cases b <;> decide| [a,b,c] => cases a <;> cases b <;> cases c <;> decide| [a,b,c,d] => cases a <;> cases b <;> cases c <;> cases d <;> decide| _::_::_::_::_::_ => simp at hprivate theorem cap_two (j : ℕ) : cap (j+2) = lucas (j+2)+cap j := byrw [cap,cap,lucas]; ring/-- The minimum binary mask also maximizes the number of chosen Lucas terms.If an optional high bit is set, its optimal residue can be expressed belowthe next two positions, freeing both for the exchange L(k)=L(k-1)+L(k-2). -/private theorem max_count_width (k : ℕ) (w : List Bool) (len : w.length=k) :w.count true ≤ bounded k (weight w) := byinduction k using Nat.strong_induction_on generalizing w with | h k ih =>by_cases hs : k≤4· simpa [len] using max_small w (len ▸ hs)obtain ⟨j,hj⟩ := Nat.exists_eq_add_of_le (by omega : 5≤k)rw [Nat.add_comm 5 j] at hjrw [hj] at len ih ⊢rcases w with _|⟨b,t⟩· simp at lenhave ht : t.length=j+4 := by simpa using lenhave row := ih (j+4) (by omega) t hthave hcap := weight_cap trw [ht] at hcapcases b· rw [bounded_step (by omega)]simpa [weight,hcap] using row· let n := lucas (j+4)+weight thave hval : weight (true::t)=n := by simp [weight,n,ht]rw [hval,bounded_step (by omega)]split· rename_i lowhave e : cap (j+4) = lucas (j+4) + cap (j+2) := cap_two (j+2)have small : weight t≤cap (j+2) := by simpa [n,e] using lowlet z := build (j+2) (weight t)have hw : weight z = weight t := (build_spec (by omega) small).1have hl : z.length=j+2 := len_build _ _have hz : (true::true::z).length=j+4 := by simp [hl]have rep : weight (true::true::z) = n := byhave e : lucas (j+4) = lucas (j+2) + lucas (j+3) := rflsimp [weight,hl,hw,n,e,show j+2+1=j+3 by omega, Nat.add_comm, Nat.add_left_comm, Nat.add_assoc]have hnew := ih (j+4) (by omega) (true::true::z) hzrw [rep] at hnewhave stablecount := (stable (by omega : 2≤j+2) small (j+4) (by omega)).2have countz : z.count true = bounded (j+2) (weight t) := ones_build (by omega)simp only [List.count_cons_self] at hnew ⊢omega· have sub : n-lucas (j+4)=weight t := by simp [n]rw [sub]simpa [Nat.add_comm] using Nat.add_le_add_left row 1theorem maximum_count {n : ℕ} {w : List Bool} (h : weight w=n) : w.count true ≤ digits n := bylet k := max 2 w.lengthlet w' := List.replicate (k-w.length) false ++ whave hw : w'.length=k := by dsimp [w']; simp; have := le_max_right 2 w.length; omegahave hpad := pad w (k-w.length)have hn : weight w'=n := hpad.2.trans hhave hcap := weight_cap w'rw [hw,hn] at hcaphave hb := max_count_width k w' hwrw [hn,← digits_eq_bounded (le_max_left ..) hcap] at hbhave hz (l : ℕ) : (List.replicate l false).count true = 0 := byinduction l with| zero => rfl| succ l ih => simpa [List.replicate_succ] using ihsimpa [w',hz] using hb#print axioms maximum_countend LazyLucas