Skip to content

Commit 52762e2

Browse files
eric-wiesermiguelmarcokmillmmarcowinstonyin
committed
feat(Set): API for subset restriction and coercion to/from subtypes (#9940)
Api for subset restriction and coercion to/from subtypes Co-authored-by: Miguel Marco <mmarco@unizar.es> Co-authored-by: Miguel Marco <mmarco@unizar.es> Co-authored-by: Kyle Miller <kmill31415@gmail.com> Co-authored-by: mmarco <mmarco@kali> Co-authored-by: Winston Yin <winstonyin@gmail.com>
1 parent 656e737 commit 52762e2

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed

Mathlib.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,6 +2092,7 @@ import Mathlib.Data.Set.Pointwise.Support
20922092
import Mathlib.Data.Set.Prod
20932093
import Mathlib.Data.Set.Semiring
20942094
import Mathlib.Data.Set.Sigma
2095+
import Mathlib.Data.Set.Subset
20952096
import Mathlib.Data.Set.Sups
20962097
import Mathlib.Data.Set.UnionLift
20972098
import Mathlib.Data.SetLike.Basic

Mathlib/Data/Set/Functor.lean

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Authors: Leonardo de Moura
66
import Mathlib.Data.Set.Lattice
77
import Mathlib.Init.Set
88
import Mathlib.Control.Basic
9+
import Mathlib.Lean.Expr.ExtraRecognizers
910

1011
#align_import data.set.functor from "leanprover-community/mathlib"@"207cfac9fcd06138865b5d04f7091e46d9320432"
1112

@@ -115,6 +116,23 @@ We define this coercion here. -/
115116
/-- Coercion using `(Subtype.val '' ·)` -/
116117
instance : CoeHead (Set s) (Set α) := ⟨fun t => (Subtype.val '' t)⟩
117118

119+
namespace Notation
120+
121+
open Lean PrettyPrinter Delaborator SubExpr in
122+
/--
123+
If the `Set.Notation` namespace is open, sets of a subtype coerced to the ambient type are
124+
represented with `↑`.
125+
-/
126+
@[scoped delab app.Set.image]
127+
def delab_set_image_subtype : Delab := whenPPOption getPPCoercions do
128+
let #[α, _, f, _] := (← getExpr).getAppArgs | failure
129+
guard <| f.isAppOfArity ``Subtype.val 2
130+
let some _ := α.coeTypeSet? | failure
131+
let e ← withAppArg delab
132+
`(↑$e)
133+
134+
end Notation
135+
118136
/-- The coercion from `Set.monad` as an instance is equal to the coercion defined above. -/
119137
theorem coe_eq_image_val (t : Set s) :
120138
@Lean.Internal.coeM Set s α _ Set.monad t = (t : Set α) := by

Mathlib/Data/Set/Image.lean

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,14 @@ theorem preimage_val_eq_preimage_val_iff (s t u : Set α) :
14441444
preimage_coe_eq_preimage_coe_iff
14451445
#align subtype.preimage_val_eq_preimage_val_iff Subtype.preimage_val_eq_preimage_val_iff
14461446

1447+
lemma preimage_val_subset_preimage_val_iff (s t u : Set α) :
1448+
(Subtype.val ⁻¹' t : Set s) ⊆ Subtype.val ⁻¹' u ↔ s ∩ t ⊆ s ∩ u := by
1449+
constructor
1450+
· rw [← image_preimage_coe, ← image_preimage_coe]
1451+
exact image_subset _
1452+
· intro h x a
1453+
exact (h ⟨x.2, a⟩).2
1454+
14471455
theorem exists_set_subtype {t : Set α} (p : Set α → Prop) :
14481456
(∃ s : Set t, p (((↑) : t → α) '' s)) ↔ ∃ s : Set α, s ⊆ t ∧ p s := by
14491457
rw [← exists_subset_range_and_iff, range_coe]

Mathlib/Data/Set/Subset.lean

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/-
2+
Copyright (c) 2024 Miguel Marco. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Miguel Marco
5+
-/
6+
import Mathlib.Data.Set.Function
7+
import Mathlib.Data.Set.Functor
8+
9+
/-!
10+
# Sets in subtypes
11+
12+
This file is about sets in `Set A` when `A` is a set.
13+
14+
It defines notation `↓∩` for sets in a type pulled down to sets in a subtype, as an inverse
15+
operation to the coercion that lifts sets in a subtype up to sets in the ambient type.
16+
17+
This module also provides lemmas for `↓∩` and this coercion.
18+
19+
## Notation
20+
21+
Let `α` be a `Type`, `A B : Set α` two sets in `α`, and `C : Set A` a set in the subtype `↑A`.
22+
23+
- `A ↓∩ B` denotes `(Subtype.val ⁻¹' B : Set A)` (that is, `{x : ↑A | ↑x ∈ B}`).
24+
- `↑C` denotes `Subtype.val '' C` (that is, `{x : α | ∃ y ∈ C, ↑y = x}`).
25+
26+
This notation, (together with the `↑` notation for `Set.CoeHead` in `Mathlib.Data.Set.Functor`)
27+
is scoped to the `Set.Notation` namespace.
28+
To enable it, use `open Set.Notation`.
29+
30+
31+
## Naming conventions
32+
33+
Theorem names refer to `↓∩` as `preimage_val`.
34+
35+
## Tags
36+
37+
subsets
38+
-/
39+
40+
open Set
41+
42+
variable {ι : Sort*} {α : Type*} {A B C : Set α} {D E : Set A}
43+
variable {S : Set (Set α)} {T : Set (Set A)} {s : ι → Set α} {t : ι → Set A}
44+
45+
namespace Set.Notation
46+
47+
/--
48+
Given two sets `A` and `B`, `A ↓∩ B` denotes the intersection of `A` and `B` as a set in `Set A`.
49+
50+
The notation is short for `((↑) ⁻¹' B : Set A)`, while giving hints to the elaborator
51+
that both `A` and `B` are terms of `Set α` for the same `α`.
52+
This set is the same as `{x : ↑A | ↑x ∈ B}`.
53+
-/
54+
scoped notation3 A:67 " ↓∩ " B:67 => (Subtype.val ⁻¹' (B : type_of% A) : Set (A : Set _))
55+
56+
end Set.Notation
57+
58+
namespace Set
59+
60+
open Notation
61+
62+
lemma preimage_val_eq_univ_of_subset (h : A ⊆ B) : A ↓∩ B = univ := by
63+
rw [eq_univ_iff_forall, Subtype.forall]
64+
exact h
65+
66+
lemma preimage_val_sUnion : A ↓∩ (⋃₀ S) = ⋃₀ { (A ↓∩ B) | B ∈ S } := by
67+
erw [sUnion_image]
68+
simp_rw [sUnion_eq_biUnion, preimage_iUnion]
69+
70+
@[simp]
71+
lemma preimage_val_iInter : A ↓∩ (⋂ i, s i) = ⋂ i, A ↓∩ s i := preimage_iInter
72+
73+
lemma preimage_val_sInter : A ↓∩ (⋂₀ S) = ⋂₀ { (A ↓∩ B) | B ∈ S } := by
74+
erw [sInter_image]
75+
simp_rw [sInter_eq_biInter, preimage_iInter]
76+
77+
lemma preimage_val_sInter_eq_sInter : A ↓∩ (⋂₀ S) = ⋂₀ ((A ↓∩ .) '' S) := by
78+
simp only [preimage_sInter, sInter_image]
79+
80+
lemma eq_of_preimage_val_eq_of_subset (hB : B ⊆ A) (hC : C ⊆ A) (h : A ↓∩ B = A ↓∩ C) : B = C := by
81+
simp only [← inter_eq_right] at hB hC
82+
simp only [Subtype.preimage_val_eq_preimage_val_iff, hB, hC] at h
83+
exact h
84+
85+
/-!
86+
The following simp lemmas try to transform operations in the subtype into operations in the ambient
87+
type, if possible.
88+
-/
89+
90+
@[simp]
91+
lemma image_val_union : (↑(D ∪ E) : Set α) = ↑D ∪ ↑E := image_union _ _ _
92+
93+
@[simp]
94+
lemma image_val_inter : (↑(D ∩ E) : Set α) = ↑D ∩ ↑E := image_inter Subtype.val_injective
95+
96+
@[simp]
97+
lemma image_val_diff : (↑(D \ E) : Set α) = ↑D \ ↑E := image_diff Subtype.val_injective _ _
98+
99+
@[simp]
100+
lemma image_val_compl : ↑(Dᶜ) = A \ ↑D := by
101+
rw [compl_eq_univ_diff, image_val_diff, image_univ, Subtype.range_coe_subtype, setOf_mem_eq]
102+
103+
@[simp]
104+
lemma image_val_sUnion : ↑(⋃₀ T) = ⋃₀ { (B : Set α) | B ∈ T} := by
105+
-- TODO: missing `image_sUnion` lemma
106+
erw [sUnion_image]
107+
simp_rw [sUnion_eq_biUnion, image_iUnion]
108+
109+
@[simp]
110+
lemma image_val_iUnion : ↑(⋃ i, t i) = ⋃ i, (t i : Set α) := image_iUnion
111+
112+
@[simp]
113+
lemma image_val_sInter (hT : T.Nonempty) : (↑(⋂₀ T) : Set α) = ⋂₀ { (↑B : Set α) | B ∈ T } := by
114+
erw [sInter_image]
115+
rw [sInter_eq_biInter, (Subtype.val_injective.injOn _).image_biInter_eq hT]
116+
117+
@[simp]
118+
lemma image_val_iInter [Nonempty ι] : (↑(⋂ i, t i) : Set α) = ⋂ i, (↑(t i) : Set α) :=
119+
(Subtype.val_injective.injOn _).image_iInter_eq
120+
121+
@[simp]
122+
lemma image_val_union_self_right_eq : A ∪ ↑D = A :=
123+
union_eq_left.2 image_val_subset
124+
125+
@[simp]
126+
lemma image_val_union_self_left_eq : ↑D ∪ A = A :=
127+
union_eq_right.2 image_val_subset
128+
129+
@[simp]
130+
lemma cou_inter_self_right_eq_coe : A ∩ ↑D = ↑D :=
131+
inter_eq_right.2 image_val_subset
132+
133+
@[simp]
134+
lemma image_val_inter_self_left_eq_coe : ↑D ∩ A = ↑D :=
135+
inter_eq_left.2 image_val_subset
136+
137+
lemma subset_preimage_val_image_val_iff : D ⊆ A ↓∩ ↑E ↔ D ⊆ E := by
138+
rw [preimage_image_eq _ Subtype.val_injective]
139+
140+
@[simp]
141+
lemma image_val_inj : (D : Set α) = ↑E ↔ D = E := Subtype.val_injective.image_injective.eq_iff
142+
143+
lemma image_val_injective : Function.Injective ((↑) : Set A → Set α) :=
144+
Subtype.val_injective.image_injective
145+
146+
lemma subset_of_image_val_subset_image_val (h : (↑D : Set α) ⊆ ↑E) : D ⊆ E :=
147+
(image_subset_image_iff Subtype.val_injective).1 h
148+
149+
@[mono]
150+
lemma image_val_mono (h : D ⊆ E) : (↑D : Set α) ⊆ ↑E :=
151+
(image_subset_image_iff Subtype.val_injective).2 h
152+
153+
/-!
154+
Relations between restriction and coercion.
155+
-/
156+
157+
lemma image_val_preimage_val_subset_self : ↑(A ↓∩ B) ⊆ B :=
158+
image_preimage_subset _ _
159+
160+
lemma preimage_val_image_val_eq_self : A ↓∩ ↑D = D :=
161+
Function.Injective.preimage_image Subtype.val_injective _
162+
163+
end Set

0 commit comments

Comments
 (0)