Skip to content

Commit 076eee0

Browse files
committed
feat: @[simp] Nat.count / Nat.nth of fun _ ↦ True, False (#13378)
Co-authored-by: L Lllvvuu <git@llllvvuu.dev>
1 parent 5e9f733 commit 076eee0

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Mathlib/Data/Nat/Count.lean

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,19 @@ theorem count_lt_card {n : ℕ} (hp : (setOf p).Finite) (hpn : p n) : count p n
146146
(count_lt_count_succ_iff.2 hpn).trans_le (count_le_card hp _)
147147
#align nat.count_lt_card Nat.count_lt_card
148148

149+
theorem count_of_forall {n : ℕ} (hp : ∀ n' < n, p n') : count p n = n := by
150+
rw [count_eq_card_filter_range, filter_true_of_mem, card_range]
151+
· simpa only [Finset.mem_range]
152+
153+
@[simp] theorem count_true (n : ℕ) : count (fun _ ↦ True) n = n := count_of_forall fun _ _ ↦ trivial
154+
155+
theorem count_of_forall_not {n : ℕ} (hp : ∀ n' < n, ¬p n') : count p n = 0 := by
156+
rw [count_eq_card_filter_range, filter_false_of_mem, card_empty]
157+
· simpa only [Finset.mem_range]
158+
159+
@[simp] theorem count_false (n : ℕ) : count (fun _ ↦ False) n = 0 :=
160+
count_of_forall_not fun _ _ ↦ id
161+
149162
variable {q : ℕ → Prop}
150163
variable [DecidablePred q]
151164

Mathlib/Data/Nat/Nth.lean

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,4 +413,20 @@ theorem lt_nth_iff_count_lt (hp : (setOf p).Infinite) {a b : ℕ} : a < count p
413413

414414
end Count
415415

416+
theorem nth_of_forall {n : ℕ} (hp : ∀ n' ≤ n, p n') : nth p n = n := by
417+
classical nth_rw 1 [← count_of_forall (hp · ·.le), nth_count (hp n le_rfl)]
418+
419+
@[simp] theorem nth_true (n : ℕ) : nth (fun _ ↦ True) n = n := nth_of_forall fun _ _ ↦ trivial
420+
421+
theorem nth_of_forall_not {n : ℕ} (hp : ∀ n' ≥ n, ¬p n') : nth p n = 0 := by
422+
have : setOf p ⊆ Finset.range n := by
423+
intro n' hn'
424+
contrapose! hp
425+
exact ⟨n', by simpa using hp, Set.mem_setOf.mp hn'⟩
426+
rw [nth_of_card_le ((finite_toSet _).subset this)]
427+
· refine (Finset.card_le_card ?_).trans_eq (Finset.card_range n)
428+
exact Set.Finite.toFinset_subset.mpr this
429+
430+
@[simp] theorem nth_false (n : ℕ) : nth (fun _ ↦ False) n = 0 := nth_of_forall_not fun _ _ ↦ id
431+
416432
end Nat

0 commit comments

Comments
 (0)