Skip to content

Commit c819e08

Browse files
feat(List): a list contains a single element iff it's a singleton (#20488)
Adds `setOf_eq_singleton_iff_of_nodup` with the generalizing `setOf_nodup_perm`. Co-authored-by: Eric Wieser <wieser.eric@gmail.com> Co-authored-by: Tristan F.-R. <tristanf@reed.edu>
1 parent 592a5a0 commit c819e08

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Mathlib/Data/List/Lemmas.lean

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ variable {α β γ : Type*}
1717

1818
namespace List
1919

20+
@[simp]
21+
theorem setOf_mem_eq_empty_iff {l : List α} : { x | x ∈ l } = ∅ ↔ l = [] :=
22+
Set.eq_empty_iff_forall_not_mem.trans eq_nil_iff_forall_not_mem.symm
23+
2024
set_option linter.deprecated false in
2125
@[simp, deprecated "No deprecation message was provided." (since := "2024-10-17")]
2226
lemma Nat.sum_eq_listSum (l : List ℕ) : Nat.sum l = l.sum := rfl

Mathlib/Data/Set/Basic.lean

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ theorem mem_of_mem_of_subset {x : α} {s t : Set α} (hx : x ∈ s) (h : s ⊆ t
195195
theorem forall_in_swap {p : α → β → Prop} : (∀ a ∈ s, ∀ (b), p a b) ↔ ∀ (b), ∀ a ∈ s, p a b := by
196196
tauto
197197

198+
theorem setOf_injective : Function.Injective (@setOf α) := injective_id
199+
200+
theorem setOf_inj {p q : α → Prop} : { x | p x } = { x | q x } ↔ p = q := Iff.rfl
201+
198202
/-! ### Lemmas about `mem` and `setOf` -/
199203

200204
theorem mem_setOf {a : α} {p : α → Prop} : a ∈ { x | p x } ↔ p a :=

Mathlib/Data/Set/Insert.lean

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,21 @@ theorem eq_singleton_iff_nonempty_unique_mem : s = {a} ↔ s.Nonempty ∧ ∀ x
264264
eq_singleton_iff_unique_mem.trans <|
265265
and_congr_left fun H => ⟨fun h' => ⟨_, h'⟩, fun ⟨x, h⟩ => H x h ▸ h⟩
266266

267+
theorem setOf_mem_list_eq_replicate {l : List α} {a : α} :
268+
{ x | x ∈ l } = {a} ↔ ∃ n > 0, l = List.replicate n a := by
269+
simpa +contextual [Set.ext_iff, iff_iff_implies_and_implies, forall_and, List.eq_replicate_iff,
270+
List.length_pos_iff_exists_mem] using ⟨fun _ _ ↦ ⟨_, ‹_›⟩, fun x hx h ↦ h _ hx ▸ hx⟩
271+
272+
theorem setOf_mem_list_eq_singleton_of_nodup {l : List α} (H : l.Nodup) {a : α} :
273+
{ x | x ∈ l } = {a} ↔ l = [a] := by
274+
constructor
275+
· rw [setOf_mem_list_eq_replicate]
276+
rintro ⟨n, hn, rfl⟩
277+
simp only [List.nodup_replicate] at H
278+
simp [show n = 1 by omega]
279+
· rintro rfl
280+
simp
281+
267282
-- while `simp` is capable of proving this, it is not capable of turning the LHS into the RHS.
268283
@[simp]
269284
theorem default_coe_singleton (x : α) : (default : ({x} : Set α)) = ⟨x, rfl⟩ :=

0 commit comments

Comments
 (0)