|
| 1 | +/- |
| 2 | +Copyright (c) 2021 Yaël Dillies. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Yaël Dillies |
| 5 | +-/ |
| 6 | +import data.int.basic |
| 7 | +import data.nat.interval |
| 8 | + |
| 9 | +/-! |
| 10 | +# Finite intervals of integers |
| 11 | +
|
| 12 | +This file proves that `ℤ` is a `locally_finite_order` and calculates the cardinality of its |
| 13 | +intervals as finsets and fintypes. |
| 14 | +-/ |
| 15 | + |
| 16 | +open finset int |
| 17 | + |
| 18 | +instance : locally_finite_order ℤ := |
| 19 | +{ finset_Icc := λ a b, (finset.range (b + 1 - a).to_nat).map $ |
| 20 | + nat.cast_embedding.trans $ add_left_embedding a, |
| 21 | + finset_Ico := λ a b, (finset.range (b - a).to_nat).map $ |
| 22 | + nat.cast_embedding.trans $ add_left_embedding a, |
| 23 | + finset_Ioc := λ a b, (finset.range (b - a).to_nat).map $ |
| 24 | + nat.cast_embedding.trans $ add_left_embedding (a + 1), |
| 25 | + finset_Ioo := λ a b, (finset.range (b - a - 1).to_nat).map $ |
| 26 | + nat.cast_embedding.trans $ add_left_embedding (a + 1), |
| 27 | + finset_mem_Icc := λ a b x, begin |
| 28 | + simp_rw [mem_map, exists_prop, mem_range, int.lt_to_nat, function.embedding.trans_apply, |
| 29 | + nat.cast_embedding_apply, add_left_embedding_apply, nat_cast_eq_coe_nat], |
| 30 | + split, |
| 31 | + { rintro ⟨a, h, rfl⟩, |
| 32 | + rw [lt_sub_iff_add_lt, int.lt_add_one_iff, add_comm] at h, |
| 33 | + exact ⟨int.le.intro rfl, h⟩ }, |
| 34 | + { rintro ⟨ha, hb⟩, |
| 35 | + use (x - a).to_nat, |
| 36 | + rw ←lt_add_one_iff at hb, |
| 37 | + rw to_nat_sub_of_le ha, |
| 38 | + exact ⟨sub_lt_sub_right hb _, add_sub_cancel'_right _ _⟩ } |
| 39 | + end, |
| 40 | + finset_mem_Ico := λ a b x, begin |
| 41 | + simp_rw [mem_map, exists_prop, mem_range, int.lt_to_nat, function.embedding.trans_apply, |
| 42 | + nat.cast_embedding_apply, add_left_embedding_apply, nat_cast_eq_coe_nat], |
| 43 | + split, |
| 44 | + { rintro ⟨a, h, rfl⟩, |
| 45 | + exact ⟨int.le.intro rfl, lt_sub_iff_add_lt'.mp h⟩ }, |
| 46 | + { rintro ⟨ha, hb⟩, |
| 47 | + use (x - a).to_nat, |
| 48 | + rw to_nat_sub_of_le ha, |
| 49 | + exact ⟨sub_lt_sub_right hb _, add_sub_cancel'_right _ _⟩ } |
| 50 | + end, |
| 51 | + finset_mem_Ioc := λ a b x, begin |
| 52 | + simp_rw [mem_map, exists_prop, mem_range, int.lt_to_nat, function.embedding.trans_apply, |
| 53 | + nat.cast_embedding_apply, add_left_embedding_apply, nat_cast_eq_coe_nat], |
| 54 | + split, |
| 55 | + { rintro ⟨a, h, rfl⟩, |
| 56 | + rw [←add_one_le_iff, le_sub_iff_add_le', add_comm _ (1 : ℤ), ←add_assoc] at h, |
| 57 | + exact ⟨int.le.intro rfl, h⟩ }, |
| 58 | + { rintro ⟨ha, hb⟩, |
| 59 | + use (x - (a + 1)).to_nat, |
| 60 | + rw [to_nat_sub_of_le ha, ←add_one_le_iff, sub_add, add_sub_cancel], |
| 61 | + exact ⟨sub_le_sub_right hb _, add_sub_cancel'_right _ _⟩ } |
| 62 | + end, |
| 63 | + finset_mem_Ioo := λ a b x, begin |
| 64 | + simp_rw [mem_map, exists_prop, mem_range, int.lt_to_nat, function.embedding.trans_apply, |
| 65 | + nat.cast_embedding_apply, add_left_embedding_apply, nat_cast_eq_coe_nat], |
| 66 | + split, |
| 67 | + { rintro ⟨a, h, rfl⟩, |
| 68 | + rw [sub_sub, lt_sub_iff_add_lt'] at h, |
| 69 | + exact ⟨int.le.intro rfl, h⟩ }, |
| 70 | + { rintro ⟨ha, hb⟩, |
| 71 | + use (x - (a + 1)).to_nat, |
| 72 | + rw [to_nat_sub_of_le ha, sub_sub], |
| 73 | + exact ⟨sub_lt_sub_right hb _, add_sub_cancel'_right _ _⟩ } |
| 74 | + end } |
| 75 | + |
| 76 | +namespace int |
| 77 | +variables (a b : ℤ) |
| 78 | + |
| 79 | +lemma Icc_eq_finset_map : |
| 80 | + Icc a b = (finset.range (b + 1 - a).to_nat).map |
| 81 | + (nat.cast_embedding.trans $ add_left_embedding a) := rfl |
| 82 | +lemma Ioc_eq_finset_map : |
| 83 | + Ioc a b = (finset.range (b - a).to_nat).map |
| 84 | + (nat.cast_embedding.trans $ add_left_embedding (a + 1)) := rfl |
| 85 | +lemma Ioo_eq_finset_map : |
| 86 | + Ioo a b = (finset.range (b - a - 1).to_nat).map |
| 87 | + (nat.cast_embedding.trans $ add_left_embedding (a + 1)) := rfl |
| 88 | + |
| 89 | +@[simp] lemma card_Icc : (Icc a b).card = (b + 1 - a).to_nat := |
| 90 | +by { change (finset.map _ _).card = _, rw [finset.card_map, finset.card_range] } |
| 91 | + |
| 92 | +@[simp] lemma card_Ioc : (Ioc a b).card = (b - a).to_nat := |
| 93 | +by { change (finset.map _ _).card = _, rw [finset.card_map, finset.card_range] } |
| 94 | + |
| 95 | +@[simp] lemma card_Ioo : (Ioo a b).card = (b - a - 1).to_nat := |
| 96 | +by { change (finset.map _ _).card = _, rw [finset.card_map, finset.card_range] } |
| 97 | + |
| 98 | +lemma card_Icc_of_le (h : a ≤ b + 1) : ((Icc a b).card : ℤ) = b + 1 - a := |
| 99 | +by rw [card_Icc, to_nat_sub_of_le h] |
| 100 | + |
| 101 | +lemma card_Ioc_of_le (h : a ≤ b) : ((Ioc a b).card : ℤ) = b - a := |
| 102 | +by rw [card_Ioc, to_nat_sub_of_le h] |
| 103 | + |
| 104 | +lemma card_Ioo_of_lt (h : a < b) : ((Ioo a b).card : ℤ) = b - a - 1 := |
| 105 | +by rw [card_Ioo, sub_sub, to_nat_sub_of_le h] |
| 106 | + |
| 107 | +@[simp] lemma card_fintype_Icc : fintype.card (set.Icc a b) = (b + 1 - a).to_nat := |
| 108 | +by rw [←card_Icc, fintype.card_of_finset] |
| 109 | + |
| 110 | +@[simp] lemma card_fintype_Ioc : fintype.card (set.Ioc a b) = (b - a).to_nat := |
| 111 | +by rw [←card_Ioc, fintype.card_of_finset] |
| 112 | + |
| 113 | +@[simp] lemma card_fintype_Ioo : fintype.card (set.Ioo a b) = (b - a - 1).to_nat := |
| 114 | +by rw [←card_Ioo, fintype.card_of_finset] |
| 115 | + |
| 116 | +lemma card_fintype_Icc_of_le (h : a ≤ b + 1) : (fintype.card (set.Icc a b) : ℤ) = b + 1 - a := |
| 117 | +by rw [card_fintype_Icc, to_nat_sub_of_le h] |
| 118 | + |
| 119 | +lemma card_fintype_Ioc_of_le (h : a ≤ b) : (fintype.card (set.Ioc a b) : ℤ) = b - a := |
| 120 | +by rw [card_fintype_Ioc, to_nat_sub_of_le h] |
| 121 | + |
| 122 | +lemma card_fintype_Ioo_of_lt (h : a < b) : (fintype.card (set.Ioo a b) : ℤ) = b - a - 1 := |
| 123 | +by rw [card_fintype_Ioo, sub_sub, to_nat_sub_of_le h] |
| 124 | + |
| 125 | +end int |
0 commit comments