Lean / Reader
Lean reader

LazyLucasEncoding.lean

308 lines33 declarationsno sorryimports LazyLucasCore

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.

  1. import LazyLucasCore
  2. namespace LazyLucas
  3. /-- Digit words are most-significant first; leading zeroes may pad the width. -/
  4. def weight : List Bool → ℕ
  5. | [] => 0
  6. | b::l => (if b then lucas l.length else 0) + weight l
  7. def mask : List Bool → ℕ
  8. | [] => 0
  9. | b::l => (if b then 2^l.length else 0) + mask l
  10. private def build : ℕ → ℕ → List Bool
  11. | 0, _ => []
  12. | 1, n => [decide (n=2)]
  13. | 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]
  14. | k+3, n => if n≤cap (k+2) then false::build (k+2) n else true::build (k+2) (n-lucas (k+2))
  15. private theorem build_step {k n : ℕ} (h : 2≤k) : build (k+1) n =
  16. if n≤cap k then false::build k n else true::build k (n-lucas k) := by
  17. obtain ⟨u,rfl⟩ := Nat.exists_eq_add_of_le h
  18. rw [Nat.add_comm 2 u]; rfl
  19. private theorem len_build (k n : ℕ) : (build k n).length=k := by
  20. induction k using Nat.strong_induction_on generalizing n with | h k ih =>
  21. rcases k with _|_|_|k
  22. · rfl
  23. · rfl
  24. · simp only [build]; split_ifs <;> rfl
  25. · rw [build_step (by omega)]; split <;> simp [ih (k+2) (by omega)]
  26. private theorem ones_build {k n : ℕ} (hk : 2≤k) : (build k n).count true = bounded k n := by
  27. induction k, hk using Nat.le_induction generalizing n with
  28. | base => simp only [build,bounded_two]; split_ifs <;> first | rfl | omega
  29. | succ k hk ih => rw [build_step hk,bounded_step hk]; split <;> simp [ih,Nat.add_comm]
  30. private theorem weight_cap (w : List Bool) : weight w ≤ cap w.length := by
  31. induction w with
  32. | nil => decide
  33. | cons b w ih => cases b <;> simp [weight,cap] <;> omega
  34. private theorem mask_lt (w : List Bool) : mask w < 2^w.length := by
  35. induction w with
  36. | nil => decide
  37. | cons b w ih => cases b <;> simp [mask,pow_succ] <;> omega
  38. private theorem lucas_pos (k : ℕ) : 0<lucas k := by
  39. induction k using Nat.strong_induction_on with | h k ih =>
  40. rcases k with _|_|k
  41. · decide
  42. · decide
  43. · rw [lucas]; have := ih k (by omega); omega
  44. private theorem le_cap (k : ℕ) : k≤cap k := by
  45. induction k with
  46. | zero => decide
  47. | succ k ih => rw [cap]; have := lucas_pos k; omega
  48. private theorem cap_mono : Monotone cap := monotone_nat_of_le_succ fun k => by simp [cap]
  49. private theorem lucas_le_cap {k : ℕ} (hk : 2≤k) : lucas k≤cap k := by
  50. obtain ⟨u,rfl⟩ := Nat.exists_eq_add_of_le hk
  51. rw [Nat.add_comm 2 u,lucas,cap,cap]; omega
  52. private theorem build_spec {k n : ℕ} (hk : 2≤k) (hn : n≤cap k) :
  53. weight (build k n)=n ∧ ∀ w : List Bool, w.length=k → weight w=n → mask (build k n)≤mask w := by
  54. induction k, hk using Nat.le_induction generalizing n with
  55. | base =>
  56. change n≤3 at hn
  57. constructor
  58. · interval_cases n <;> decide
  59. · intro w hw hv
  60. rcases w with _|⟨a,w⟩ <;> simp at hw
  61. rcases w with _|⟨b,w⟩ <;> simp at hw
  62. subst w
  63. cases a <;> cases b <;> subst n <;> decide
  64. | succ k hk ih =>
  65. rw [build_step hk]
  66. by_cases low : n≤cap k
  67. · rw [if_pos low]
  68. obtain ⟨hval, hbest⟩ := ih low
  69. refine ⟨by simpa [weight] using hval,?_⟩
  70. rintro (_|⟨b,t⟩) hlen hv
  71. · simp at hlen
  72. have ht : t.length=k := by simpa using hlen
  73. cases b
  74. · simpa [mask] using hbest t ht (by simpa [weight] using hv)
  75. · have h := mask_lt (build k n)
  76. rw [len_build] at h
  77. simp [mask,ht]; omega
  78. · rw [if_neg low]
  79. have hl := lucas_le_cap hk
  80. have hn' : n-lucas k≤cap k := by rw [cap] at hn; omega
  81. obtain ⟨hval,hbest⟩ := ih hn'
  82. refine ⟨by simp [weight,len_build,hval]; omega,?_⟩
  83. rintro (_|⟨b,t⟩) hlen hv
  84. · simp at hlen
  85. have ht : t.length=k := by simpa using hlen
  86. cases b
  87. · have h := weight_cap t
  88. rw [ht] at h
  89. simp [weight] at hv; omega
  90. · simp [weight,ht] at hv
  91. have e : weight t=n-lucas k := by omega
  92. simpa [mask,ht,len_build] using hbest t ht e
  93. private theorem stable {k n : ℕ} (hk : 2≤k) (hn : n≤cap k) (m : ℕ) (hm : k≤m) :
  94. mask (build m n)=mask (build k n) ∧ bounded m n=bounded k n := by
  95. induction m, hm using Nat.le_induction with
  96. | base => simp
  97. | succ m hm ih =>
  98. have hl : n≤cap m := hn.trans (cap_mono hm)
  99. rw [build_step (by omega),bounded_step (by omega),if_pos hl,if_pos hl]
  100. simpa [mask] using ih
  101. /-- The computed minimal Lucas word, padded with high-order zeros. -/
  102. def lazyWord (n : ℕ) : List Bool := build (n+2) n
  103. def digits (n : ℕ) : ℕ := (lazyWord n).count true
  104. theorem digits_eq_bounded {k n : ℕ} (hk : 2≤k) (hn : n≤cap k) : digits n=bounded k n := by
  105. unfold digits lazyWord
  106. rw [ones_build (by omega),←(stable hk hn (max k (n+2)) (by omega)).2]
  107. exact (stable (k:=n+2) (by omega) (by have := le_cap (n+2); omega) _ (by omega)).2.symm
  108. theorem lazyWord_weight (n : ℕ) : weight (lazyWord n)=n :=
  109. (build_spec (k:=n+2) (by omega) (by have := le_cap (n+2); omega)).1
  110. private theorem pad (w : List Bool) (d : ℕ) :
  111. mask (List.replicate d false ++ w) = mask w ∧ weight (List.replicate d false ++ w) = weight w := by
  112. induction d with
  113. | zero => simp
  114. | succ d ih => simpa [List.replicate_succ,mask,weight] using ih
  115. /-- Among all finite binary words with Lucas value n this one has smallest
  116. ordinary base-two mask. -/
  117. theorem lazyWord_min {n : ℕ} {w : List Bool} (hw : weight w=n) : mask (lazyWord n) ≤ mask w := by
  118. let k := max (n+2) w.length
  119. have hk : n+2≤k := le_max_left ..
  120. have hn : n≤cap (n+2) := by have := le_cap (n+2); omega
  121. have fst := (stable (k:=n+2) (by omega) hn k hk).1
  122. have h := (build_spec (k:=k) (n:=n) (by omega) (hn.trans (cap_mono hk))).2
  123. let w' := List.replicate (k-w.length) false ++ w
  124. have hp := pad w (k-w.length)
  125. have hl : w'.length=k := by dsimp [w']; simp; have := le_max_right (n+2) w.length; omega
  126. have h' := h w' hl (hp.2.trans hw)
  127. rwa [fst,hp.1] at h'
  128. private theorem exchange (u v : List Bool) :
  129. weight (u++true::false::false::v) = weight (u++false::true::true::v) ∧
  130. mask (u++false::true::true::v) < mask (u++true::false::false::v) := by
  131. induction u with
  132. | nil =>
  133. have hh : 0 < 2^v.length := by positivity
  134. simp [weight, mask, lucas, pow_succ]
  135. omega
  136. | cons b u ih =>
  137. simp only [List.cons_append, weight, mask, List.length_append, List.length_cons]
  138. obtain ⟨he,ht⟩ := ih
  139. constructor <;> omega
  140. theorem lazyWord_no100 (n : ℕ) (u v : List Bool) : lazyWord n ≠ u++true::false::false::v := by
  141. intro he
  142. obtain ⟨hval,hlt⟩ := exchange u v
  143. have hv : weight (u++false::true::true::v)=n := hval.symm.trans (he ▸ lazyWord_weight n)
  144. have hmask := lazyWord_min hv
  145. rw [he] at hmask
  146. omega
  147. /-- A pair of consecutive zeroes below the first set bit contains the local
  148. replacement pattern 100 somewhere in the zero run. -/
  149. private theorem interior_contains100 (u v : List Bool) (h : true ∈ u) :
  150. ∃ a b : List Bool, u++false::false::v = a++true::false::false::b := by
  151. induction u using List.reverseRecOn generalizing v with
  152. | nil => simp at h
  153. | append_singleton u t ih =>
  154. cases t
  155. · simpa [List.append_assoc] using ih (false::v) (by simpa using h)
  156. · exact ⟨u,v,by simp⟩
  157. /-- Leading zeros carry no value and can be discarded when applying the
  158. source program's `IntegerDigits` convention. -/
  159. private def strip : List Bool → List Bool
  160. | false::s => strip s
  161. | w => w
  162. private theorem strip_spec (w : List Bool) : mask (strip w)=mask w ∧ weight (strip w)=weight w ∧
  163. (strip w).count true = w.count true ∧ ∃ k, w=List.replicate k false ++ strip w := by
  164. induction w with
  165. | nil => exact ⟨rfl,rfl,rfl,0,rfl⟩
  166. | cons b w ih =>
  167. cases b
  168. · obtain ⟨hm,hv,hc,k,hk⟩ := ih
  169. exact ⟨by simpa [strip,mask] using hm,by simpa [strip,weight] using hv,
  170. by simpa [strip] using hc,k+1,by simpa only [strip,List.replicate_succ,List.cons_append] using congrArg (List.cons false) hk⟩
  171. · exact ⟨rfl,rfl,rfl,0,rfl⟩
  172. private theorem strip_first (w : List Bool) : strip w=[] ∨ ∃ t, strip w=true::t := by
  173. induction w with
  174. | nil => exact Or.inl rfl
  175. | cons b w ih => cases b; exact ih; exact Or.inr ⟨w,rfl⟩
  176. def noDoubleZero (w : List Bool) : Prop := ∀ u v : List Bool, w≠u++false::false::v
  177. def canonicalWord (n : ℕ) : List Bool := strip (lazyWord n)
  178. /-- The canonical word is accepted by the source program's two-zero filter
  179. and is the first in its enumeration of ordinary binary masks. -/
  180. theorem source_word (n : ℕ) :
  181. weight (canonicalWord n) = n ∧ noDoubleZero (canonicalWord n) ∧
  182. (∀ w, weight w=n → mask (canonicalWord n)≤mask w) ∧
  183. (canonicalWord n).count true=digits n := by
  184. obtain ⟨hm,hv,hc,k,hk⟩ := strip_spec (lazyWord n)
  185. refine ⟨hv.trans (lazyWord_weight n),?_,fun w hw => hm.le.trans (lazyWord_min hw),hc⟩
  186. intro u v he
  187. have hf := strip_first (lazyWord n)
  188. change strip (lazyWord n) = u++false::false::v at he
  189. have hm : true∈u := by
  190. rcases hf with hz|⟨t,ht⟩
  191. · rw [hz] at he; simp at he
  192. · rw [ht] at he
  193. cases u with
  194. | nil => simp at he
  195. | cons b u => simp only [List.cons_append,List.cons.injEq] at he; cases he.1; simp
  196. obtain ⟨a,b,hab⟩ := interior_contains100 u v hm
  197. apply lazyWord_no100 n (List.replicate k false ++ a) b
  198. rw [hk,he,hab,List.append_assoc]
  199. #print axioms source_word
  200. #print axioms digits_eq_bounded
  201. private theorem max_small (w : List Bool) (h : w.length≤4) : w.count true ≤ bounded w.length (weight w) := by
  202. match w with
  203. | [] => decide
  204. | [a] => cases a <;> decide
  205. | [a,b] => cases a <;> cases b <;> decide
  206. | [a,b,c] => cases a <;> cases b <;> cases c <;> decide
  207. | [a,b,c,d] => cases a <;> cases b <;> cases c <;> cases d <;> decide
  208. | _::_::_::_::_::_ => simp at h
  209. private theorem cap_two (j : ℕ) : cap (j+2) = lucas (j+2)+cap j := by
  210. rw [cap,cap,lucas]; ring
  211. /-- The minimum binary mask also maximizes the number of chosen Lucas terms.
  212. If an optional high bit is set, its optimal residue can be expressed below
  213. the next two positions, freeing both for the exchange L(k)=L(k-1)+L(k-2). -/
  214. private theorem max_count_width (k : ℕ) (w : List Bool) (len : w.length=k) :
  215. w.count true ≤ bounded k (weight w) := by
  216. induction k using Nat.strong_induction_on generalizing w with | h k ih =>
  217. by_cases hs : k≤4
  218. · simpa [len] using max_small w (len ▸ hs)
  219. obtain ⟨j,hj⟩ := Nat.exists_eq_add_of_le (by omega : 5≤k)
  220. rw [Nat.add_comm 5 j] at hj
  221. rw [hj] at len ih ⊢
  222. rcases w with _|⟨b,t⟩
  223. · simp at len
  224. have ht : t.length=j+4 := by simpa using len
  225. have row := ih (j+4) (by omega) t ht
  226. have hcap := weight_cap t
  227. rw [ht] at hcap
  228. cases b
  229. · rw [bounded_step (by omega)]
  230. simpa [weight,hcap] using row
  231. · let n := lucas (j+4)+weight t
  232. have hval : weight (true::t)=n := by simp [weight,n,ht]
  233. rw [hval,bounded_step (by omega)]
  234. split
  235. · rename_i low
  236. have e : cap (j+4) = lucas (j+4) + cap (j+2) := cap_two (j+2)
  237. have small : weight t≤cap (j+2) := by simpa [n,e] using low
  238. let z := build (j+2) (weight t)
  239. have hw : weight z = weight t := (build_spec (by omega) small).1
  240. have hl : z.length=j+2 := len_build _ _
  241. have hz : (true::true::z).length=j+4 := by simp [hl]
  242. have rep : weight (true::true::z) = n := by
  243. have e : lucas (j+4) = lucas (j+2) + lucas (j+3) := rfl
  244. simp [weight,hl,hw,n,e,show j+2+1=j+3 by omega, Nat.add_comm, Nat.add_left_comm, Nat.add_assoc]
  245. have hnew := ih (j+4) (by omega) (true::true::z) hz
  246. rw [rep] at hnew
  247. have stablecount := (stable (by omega : 2≤j+2) small (j+4) (by omega)).2
  248. have countz : z.count true = bounded (j+2) (weight t) := ones_build (by omega)
  249. simp only [List.count_cons_self] at hnew ⊢
  250. omega
  251. · have sub : n-lucas (j+4)=weight t := by simp [n]
  252. rw [sub]
  253. simpa [Nat.add_comm] using Nat.add_le_add_left row 1
  254. theorem maximum_count {n : ℕ} {w : List Bool} (h : weight w=n) : w.count true ≤ digits n := by
  255. let k := max 2 w.length
  256. let w' := List.replicate (k-w.length) false ++ w
  257. have hw : w'.length=k := by dsimp [w']; simp; have := le_max_right 2 w.length; omega
  258. have hpad := pad w (k-w.length)
  259. have hn : weight w'=n := hpad.2.trans h
  260. have hcap := weight_cap w'
  261. rw [hw,hn] at hcap
  262. have hb := max_count_width k w' hw
  263. rw [hn,← digits_eq_bounded (le_max_left ..) hcap] at hb
  264. have hz (l : ℕ) : (List.replicate l false).count true = 0 := by
  265. induction l with
  266. | zero => rfl
  267. | succ l ih => simpa [List.replicate_succ] using ih
  268. simpa [w',hz] using hb
  269. #print axioms maximum_count
  270. end LazyLucas