forked from ImperialCollegeLondon/M40001_lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partition_challenge_solution2.lean
218 lines (188 loc) · 5.45 KB
/
partition_challenge_solution2.lean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import data.equiv.basic
import tactic
/-!
# Definition and basic API for partitions
-/
/-- The structure of a partition on a Type α. -/
-- Let α be a type. A *partition* on α is defined to
-- be the following data.
@[ext] structure partition (α : Type) :=
-- A set C of subsets of α, called "blocks".
(C : set (set α))
-- A hypothesis (a.k.a. a proof) that all the blocks are non-empty.
(Hnonempty : ∀ X ∈ C, (X : set α).nonempty)
-- A hypothesis that every term of type α is in one of the blocks.
(Hcover : ∀ a, ∃ X ∈ C, a ∈ X)
-- A hypothesis that two blocks with non-empty intersection are equal.
(Hdisjoint : ∀ X Y ∈ C, (X ∩ Y : set α).nonempty → X = Y)
namespace partition
-- let α be a type, and fix a partition P on α. Let X and Y be subsets of α.
variables {α : Type} {P : partition α} {X Y : set α}
-- a more convenient way of putting it.
theorem Hdisjoint' (hX : X ∈ P.C) (hY : Y ∈ P.C) : (X ∩ Y).nonempty → X = Y :=
P.Hdisjoint X Y hX hY
-- another way
theorem Hdisjoint'' (hX : X ∈ P.C) (hY : Y ∈ P.C) {a : α} (haX : a ∈ X)
(haY : a ∈ Y) : X = Y :=
P.Hdisjoint _ _ hX hY ⟨a, haX, haY⟩
end partition
section equivalence_classes
/-!
# Definition and basic API for equivalence classes
We define equivalence classes and prove a few basic results about them.
-/
-- Notation and variables for this section:
-- let α be a type, and let R be an equivalence relation on R.
variables {α : Type} {R : α → α → Prop} (hR : equivalence R)
-- Always assume R is an equivalence relation, even when we don't need it.
include hR
/-- The equivalence class of `x` is all the `y` such that `y` is related to `x`. -/
def cl (x : α) :=
{y : α | R y x}
/-- Useful for rewriting -- `y` is in the equivalence class of `x` iff
`y` is related to `x`. True by definition. -/
theorem mem_cl_iff {x y : α} : x ∈ cl hR y ↔ R x y := iff.rfl
/-- x is in cl(x) -/
lemma mem_class_self (x : α) :
x ∈ cl hR x :=
begin
rcases hR with ⟨hrefl, hsymm, htrans⟩,
exact hrefl x,
end
lemma class_sub {x y : α} :
x ∈ cl hR y →
cl hR x ⊆ cl hR y :=
begin
rcases hR with ⟨hrefl, hsymm, htrans⟩,
intro hxy,
intro z,
intro hzx,
exact htrans hzx hxy,
end
lemma class_eq {x y : α} :
x ∈ cl hR y →
cl hR x = cl hR y :=
begin
intro hxy,
apply set.subset.antisymm,
apply class_sub hR hxy,
apply class_sub hR,
rcases hR with ⟨hrefl, hsymm, htrans⟩,
exact hsymm hxy,
end
end equivalence_classes -- section
/-!
# Statement of the theorem
-/
open partition
-- There is a bijection between equivalence relations and partitions
example (α : Type) : {R : α → α → Prop // equivalence R} ≃ partition α :=
{ -- Let R be an equivalence relation.
to_fun := λ R, {
-- Let C be the set of equivalence classes for R.
C := { B : set α | ∃ x : α, B = cl R.2 x},
-- I claim that C is a partition. We need to check three things.
Hnonempty := begin
-- If c is a block then c is nonempty.
rintros c ⟨x, rfl⟩,
use x,
apply mem_class_self R.2,
end,
Hcover := begin
intro x,
use (cl R.2 x),
split,
{ use x,
},
{ apply mem_class_self,
}
end,
Hdisjoint := begin
rintros c d ⟨x, rfl⟩ ⟨y, rfl⟩ ⟨z, hzx, hzy⟩,
cases R with R hR,
erw ← class_eq hR hzx,
erw ← class_eq hR hzy,
end },
-- Conversely, say P is an partition.
inv_fun := λ P,
-- Let's define a binary relation by x ~ y iff there's a block they're both in
⟨λ a b, ∀ X ∈ P.C, a ∈ X → b ∈ X, begin
-- I claim this is an equivalence relation.
split,
{ -- It's reflexive
intros a C hC haC,
exact haC,
},
split,
{ -- it's symmetric
intros x y h C hC hyC,
rcases P.Hcover x with ⟨D, hD, hxD⟩,
convert hxD,
apply P.Hdisjoint _ _ hC hD,
use [y, hyC],
exact h D hD hxD,
},
{ -- it's transitive
intros x y z hxy hyx C hC hxC,
apply hyx C hC,
apply hxy C hC,
exact hxC,
}
end⟩,
-- If you start with the equivalence relation, and then make the partition
-- and a new equivalence relation, you get back to where you started.
left_inv := begin
rintro ⟨R, hR⟩,
simp,
ext a b,
rcases hR with ⟨hRr,hRs,hRt⟩,
split,
{ intro f,
specialize f a (hRr a),
exact hRs f,
},
{ intros hab t hat,
refine hRt _ hat,
exact hRs hab,
},
end,
-- Similarly, if you start with the partition, and then make the
-- equivalence relation, and then construct the corresponding partition
-- into equivalence classes, you have the same partition you started with.
right_inv := begin
intro P,
ext W,
simp,
split,
{ rintro ⟨a, rfl⟩,
rcases P.Hcover a with ⟨X, hX, haX⟩,
convert hX,
ext b,
rw mem_cl_iff,
split,
{ intro h,
rcases P.Hcover b with ⟨Y, hY, hbY⟩,
specialize h Y hY hbY,
rwa Hdisjoint'' hX hY haX h,
},
{ intros hbX Y hY hbY,
rwa Hdisjoint'' hY hX hbY hbX,
}
},
{ intro hW,
cases P.Hnonempty W hW with a haW,
use a,
ext b,
rw mem_cl_iff,
split,
{ intro hbW,
intros X hX hbX,
rwa Hdisjoint'' hX hW hbX hbW
},
{ intro haX,
rcases P.Hcover b with ⟨X, hX, hbX⟩,
specialize haX X hX hbX,
rwa Hdisjoint'' hW hX haW haX
}
}
end }