-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathDenumerable.lean
More file actions
337 lines (263 loc) · 11.6 KB
/
Copy pathDenumerable.lean
File metadata and controls
337 lines (263 loc) · 11.6 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
module
public import Mathlib.Data.Fintype.EquivFin
public import Mathlib.Data.List.MinMax
public import Mathlib.Data.Nat.Order.Lemmas
public import Mathlib.Logic.Encodable.Basic
/-!
# Denumerable types
This file defines denumerable (countably infinite) types as a typeclass extending `Encodable`. This
is used to provide explicit encode/decode functions from and to `ℕ`, with the information that those
functions are inverses of each other.
## Implementation notes
This property already has a name, namely `α ≃ ℕ`, but here we are interested in using it as a
typeclass.
-/
@[expose] public section
assert_not_exists Monoid
variable {α β : Type*}
/-- A denumerable type is (constructively) bijective with `ℕ`. Typeclass equivalent of `α ≃ ℕ`. -/
class Denumerable (α : Type*) extends Encodable α where
/-- `decode` and `encode` are inverses. -/
decode_inv : ∀ n, ∃ a ∈ decode n, encode a = n
open Finset Nat
namespace Denumerable
section
variable [Denumerable α] [Denumerable β]
open Encodable
theorem decode_isSome (α) [Denumerable α] (n : ℕ) : (decode (α := α) n).isSome :=
Option.isSome_iff_exists.2 <| (decode_inv n).imp fun _ => And.left
/-- Returns the `n`-th element of `α` indexed by the decoding. -/
def ofNat (α) [Denumerable α] (n : ℕ) : α :=
Option.get _ (decode_isSome α n)
@[simp]
theorem decode_eq_ofNat (α) [Denumerable α] (n : ℕ) : decode (α := α) n = some (ofNat α n) :=
Option.eq_some_of_isSome _
theorem ofNat_of_decode {n b} (h : decode (α := α) n = some b) : ofNat (α := α) n = b := by
simpa using h
@[simp]
theorem encode_ofNat (n) : encode (ofNat α n) = n := by
obtain ⟨a, h, e⟩ := decode_inv (α := α) n
rwa [ofNat_of_decode h]
@[simp]
theorem ofNat_encode (a) : ofNat α (encode a) = a :=
ofNat_of_decode (encodek _)
/-- A denumerable type is equivalent to `ℕ`. -/
def eqv (α) [Denumerable α] : α ≃ ℕ :=
⟨encode, ofNat α, ofNat_encode, encode_ofNat⟩
-- See Note [lower instance priority]
instance (priority := 100) : Infinite α :=
Infinite.of_surjective _ (eqv α).surjective
/-- A type equivalent to `ℕ` is denumerable. -/
@[instance_reducible]
def mk' {α} (e : α ≃ ℕ) : Denumerable α where
encode := e
decode := some ∘ e.symm
encodek _ := congr_arg some (e.symm_apply_apply _)
decode_inv _ := ⟨_, rfl, e.apply_symm_apply _⟩
/-- Denumerability is conserved by equivalences. This is transitivity of equivalence the denumerable
way. -/
@[instance_reducible]
def ofEquiv (α) {β} [Denumerable α] (e : β ≃ α) : Denumerable β :=
{ Encodable.ofEquiv _ e with
decode_inv := fun n => by
simp [decode_ofEquiv, encode_ofEquiv] }
@[simp]
theorem ofEquiv_ofNat (α) {β} [Denumerable α] (e : β ≃ α) (n) :
@ofNat β (ofEquiv _ e) n = e.symm (ofNat α n) := by
let := ofEquiv _ e
refine ofNat_of_decode ?_
rw [decode_ofEquiv e]
simp
/-- All denumerable types are equivalent. -/
def equiv₂ (α β) [Denumerable α] [Denumerable β] : α ≃ β :=
(eqv α).trans (eqv β).symm
instance nat : Denumerable ℕ :=
⟨fun _ => ⟨_, rfl, rfl⟩⟩
@[simp]
theorem ofNat_nat (n) : ofNat ℕ n = n :=
rfl
/-- If `α` is denumerable, then so is `Option α`. -/
instance option : Denumerable (Option α) :=
⟨fun n => by
cases n with
| zero =>
refine ⟨none, ?_, encode_none⟩
rw [decode_option_zero, Option.mem_def]
| succ n =>
refine ⟨some (ofNat α n), ?_, ?_⟩
· rw [decode_option_succ, decode_eq_ofNat, Option.map_some, Option.mem_def]
rw [encode_some, encode_ofNat]⟩
/-- If `α` and `β` are denumerable, then so is their sum. -/
instance sum : Denumerable (α ⊕ β) :=
⟨fun n => by
suffices ∃ a ∈ @decodeSum α β _ _ n, encodeSum a = bit (bodd n) (div2 n) by
simpa [bit_bodd_div2]
simp only [decodeSum, decode_eq_ofNat, Option.map_some, Sum.exists]
cases bodd n <;> simp [bit_val, encodeSum]⟩
section Sigma
variable {γ : α → Type*} [∀ a, Denumerable (γ a)]
/-- A denumerable collection of denumerable types is denumerable. -/
instance sigma : Denumerable (Sigma γ) :=
⟨fun n => by simp⟩
@[simp]
theorem sigma_ofNat_val (n : ℕ) :
ofNat (Sigma γ) n = ⟨ofNat α (unpair n).1, ofNat (γ _) (unpair n).2⟩ :=
Option.some.inj <| by rw [← decode_eq_ofNat, decode_sigma_val]; simp
end Sigma
/-- If `α` and `β` are denumerable, then so is their product. -/
instance prod : Denumerable (α × β) :=
ofEquiv _ (Equiv.sigmaEquivProd α β).symm
theorem prod_ofNat_val (n : ℕ) :
ofNat (α × β) n = (ofNat α (unpair n).1, ofNat β (unpair n).2) := by simp
@[simp]
theorem prod_nat_ofNat : ofNat (ℕ × ℕ) = unpair := by funext; simp
instance int : Denumerable ℤ :=
fast_instance% Denumerable.mk' Equiv.intEquivNat
instance pnat : Denumerable ℕ+ :=
fast_instance% Denumerable.mk' Equiv.pnatEquivNat
/-- The lift of a denumerable type is denumerable. -/
instance ulift : Denumerable (ULift α) :=
ofEquiv _ Equiv.ulift
/-- The lift of a denumerable type is denumerable. -/
instance plift : Denumerable (PLift α) :=
ofEquiv _ Equiv.plift
/-- If `α` is denumerable, then `α × α` and `α` are equivalent. -/
def pair : α × α ≃ α :=
equiv₂ _ _
end
end Denumerable
namespace Nat.Subtype
open Function Encodable
/-! ### Subsets of `ℕ` -/
variable {s : Set ℕ} [Infinite s]
section Classical
theorem exists_succ (x : s) : ∃ n, (x : ℕ) + n + 1 ∈ s := by
by_contra h
have (a : ℕ) (ha : a ∈ s) : a < x + 1 :=
lt_of_not_ge fun hax => h ⟨a - (x + 1), by rwa [Nat.add_right_comm, Nat.add_sub_cancel' hax]⟩
classical
exact Fintype.false
⟨(((Multiset.range (succ x)).filter (· ∈ s)).pmap
(fun (y : ℕ) (hy : y ∈ s) => Subtype.mk y hy) (by simp [-Multiset.range_succ])).toFinset,
by simpa [Subtype.ext_iff, Multiset.mem_filter, -Multiset.range_succ] ⟩
end Classical
variable [DecidablePred (· ∈ s)]
/-- Returns the next natural in a set, according to the usual ordering of `ℕ`. -/
def succ (x : s) : s :=
have h : ∃ m, (x : ℕ) + m + 1 ∈ s := exists_succ x
⟨↑x + Nat.find h + 1, Nat.find_spec h⟩
theorem succ_le_of_lt {x y : s} (h : y < x) : succ y ≤ x :=
have hx : ∃ m, (y : ℕ) + m + 1 ∈ s := exists_succ _
let ⟨k, hk⟩ := Nat.exists_eq_add_of_lt h
have : Nat.find hx ≤ k := Nat.find_min' _ (hk ▸ x.2)
show (y : ℕ) + Nat.find hx + 1 ≤ x by lia
theorem le_succ_of_forall_lt_le {x y : s} (h : ∀ z < x, z ≤ y) : x ≤ succ y :=
have hx : ∃ m, (y : ℕ) + m + 1 ∈ s := exists_succ _
show (x : ℕ) ≤ (y : ℕ) + Nat.find hx + 1 from
le_of_not_gt fun hxy =>
(h ⟨_, Nat.find_spec hx⟩ hxy).not_gt <|
(by lia : (y : ℕ) < (y : ℕ) + Nat.find hx + 1)
theorem lt_succ_self (x : s) : x < succ x :=
calc
(x : ℕ) ≤ (x + _) := le_add_right ..
_ < (succ x) := Nat.lt_succ_self (x + _)
theorem lt_succ_iff_le {x y : s} : x < succ y ↔ x ≤ y :=
⟨fun h => le_of_not_gt fun h' => not_le_of_gt h (succ_le_of_lt h'), fun h =>
lt_of_le_of_lt h (lt_succ_self _)⟩
/-- Returns the `n`-th element of a set, according to the usual ordering of `ℕ`. -/
def ofNat (s : Set ℕ) [DecidablePred (· ∈ s)] [Infinite s] : ℕ → s
| 0 => ⊥
| n + 1 => succ (ofNat s n)
theorem ofNat_surjective : Surjective (ofNat s)
| ⟨x, hx⟩ => by
set t : List s :=
((List.range x).filter fun y => y ∈ s).pmap
(fun (y : ℕ) (hy : y ∈ s) => ⟨y, hy⟩)
(by intro a ha; simpa using! (List.mem_filter.mp ha).2) with ht
have hmt : ∀ {y : s}, y ∈ t ↔ y < ⟨x, hx⟩ := by
simp [List.mem_filter, Subtype.ext_iff, ht]
cases hmax : List.maximum t with
| bot =>
refine ⟨0, le_antisymm bot_le (le_of_not_gt fun h => List.not_mem_nil (a := (⊥ : s)) ?_)⟩
rwa [← List.maximum_eq_bot.1 hmax, hmt]
| coe m =>
have wf : ↑m < x := by simpa using! hmt.mp (List.maximum_mem hmax)
rcases ofNat_surjective m with ⟨a, rfl⟩
refine ⟨a + 1, le_antisymm (succ_le_of_lt wf) ?_⟩
exact le_succ_of_forall_lt_le fun z hz => List.le_maximum_of_mem (hmt.2 hz) hmax
termination_by n => n.val
@[simp]
theorem ofNat_range : Set.range (ofNat s) = Set.univ :=
ofNat_surjective.range_eq
@[simp]
theorem coe_comp_ofNat_range : Set.range ((↑) ∘ ofNat s : ℕ → ℕ) = s := by
rw [Set.range_comp Subtype.val, ofNat_range, Set.image_univ, Subtype.range_coe]
set_option backward.privateInPublic true in
private def toFunAux (x : s) : ℕ :=
(List.range x).countP (· ∈ s)
private theorem toFunAux_eq {s : Set ℕ} [DecidablePred (· ∈ s)] (x : s) :
toFunAux x = #{y ∈ Finset.range x | y ∈ s} := by
rw [toFunAux, List.countP_eq_length_filter]
rfl
set_option backward.privateInPublic true in
private theorem right_inverse_aux : ∀ n, toFunAux (ofNat s n) = n
| 0 => by
rw [toFunAux_eq, card_eq_zero, eq_empty_iff_forall_notMem]
rintro n hn
rw [mem_filter, ofNat, mem_range] at hn
exact bot_le.not_gt (show (⟨n, hn.2⟩ : s) < ⊥ from hn.1)
| n + 1 => by
have ih : toFunAux (ofNat s n) = n := right_inverse_aux n
have h₁ : (ofNat s n : ℕ) ∉ {x ∈ range (ofNat s n) | x ∈ s} := by simp
have h₂ : {x ∈ range (succ (ofNat s n)) | x ∈ s} =
insert ↑(ofNat s n) {x ∈ range (ofNat s n) | x ∈ s} := by
simp only [Finset.ext_iff, mem_insert, mem_range, mem_filter]
exact fun m =>
⟨fun h => by
simp only [h.2, and_true]
exact Or.symm (lt_or_eq_of_le ((@lt_succ_iff_le _ _ _ ⟨m, h.2⟩ _).1 h.1)),
fun h =>
h.elim (fun h => h.symm ▸ ⟨lt_succ_self _, (ofNat s n).prop⟩) fun h =>
⟨h.1.trans (lt_succ_self _), h.2⟩⟩
simp only [toFunAux_eq, ofNat] at ih ⊢
conv =>
rhs
rw [← ih, ← card_insert_of_notMem h₁, ← h₂]
set_option backward.privateInPublic true in
set_option backward.privateInPublic.warn false in
/-- Any infinite set of naturals is denumerable. -/
@[instance_reducible]
def denumerable (s : Set ℕ) [DecidablePred (· ∈ s)] [Infinite s] : Denumerable s :=
Denumerable.ofEquiv ℕ
{ toFun := toFunAux
invFun := ofNat s
left_inv := leftInverse_of_surjective_of_rightInverse ofNat_surjective right_inverse_aux
right_inv := right_inverse_aux }
end Nat.Subtype
namespace Denumerable
open Encodable
/-- An infinite encodable type is denumerable. -/
@[instance_reducible]
def ofEncodableOfInfinite (α : Type*) [Encodable α] [Infinite α] : Denumerable α := by
letI := @decidableRangeEncode α _
letI : Infinite (Set.range (@encode α _)) :=
Infinite.of_injective _ (Equiv.ofInjective _ encode_injective).injective
letI := Nat.Subtype.denumerable (Set.range (@encode α _))
exact Denumerable.ofEquiv (Set.range (@encode α _)) (equivRangeEncode α)
end Denumerable
/-- See also `nonempty_encodable`, `nonempty_fintype`. -/
theorem nonempty_denumerable (α : Type*) [Countable α] [Infinite α] : Nonempty (Denumerable α) :=
(nonempty_encodable α).map fun h => @Denumerable.ofEncodableOfInfinite _ h _
theorem nonempty_denumerable_iff {α : Type*} :
Nonempty (Denumerable α) ↔ Countable α ∧ Infinite α :=
⟨fun ⟨_⟩ ↦ ⟨inferInstance, inferInstance⟩, fun ⟨_, _⟩ ↦ nonempty_denumerable _⟩
instance nonempty_equiv_of_countable [Countable α] [Infinite α] [Countable β] [Infinite β] :
Nonempty (α ≃ β) := by
cases nonempty_denumerable α
cases nonempty_denumerable β
exact ⟨(Denumerable.eqv _).trans (Denumerable.eqv _).symm⟩