blob_id
stringlengths
40
40
directory_id
stringlengths
40
40
path
stringlengths
7
139
content_id
stringlengths
40
40
detected_licenses
listlengths
0
16
license_type
stringclasses
2 values
repo_name
stringlengths
7
55
snapshot_id
stringlengths
40
40
revision_id
stringlengths
40
40
branch_name
stringclasses
6 values
visit_date
int64
1,471B
1,694B
revision_date
int64
1,378B
1,694B
committer_date
int64
1,378B
1,694B
github_id
float64
1.33M
604M
star_events_count
int64
0
43.5k
fork_events_count
int64
0
1.5k
gha_license_id
stringclasses
6 values
gha_event_created_at
int64
1,402B
1,695B
gha_created_at
int64
1,359B
1,637B
gha_language
stringclasses
19 values
src_encoding
stringclasses
2 values
language
stringclasses
1 value
is_vendor
bool
1 class
is_generated
bool
1 class
length_bytes
int64
3
6.4M
extension
stringclasses
4 values
content
stringlengths
3
6.12M
5bf55399cebf19012151e7180d1d0df46f72955a
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/topology/algebra/open_subgroup.lean
c2bec04ddf70fa9e748cc58e0da929a097c97d48
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
8,441
lean
/- Copyright (c) 2019 Johan Commelin All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import order.filter.lift import topology.opens import topology.algebra.ring open topological_space open_locale topological_space set_option old_structure_cmd true /-- The type of open subgroups of a topological additive group. -/ @[ancestor add_subgroup] structure open_add_subgroup (G : Type*) [add_group G] [topological_space G] extends add_subgroup G := (is_open' : is_open carrier) /-- The type of open subgroups of a topological group. -/ @[ancestor subgroup, to_additive] structure open_subgroup (G : Type*) [group G] [topological_space G] extends subgroup G := (is_open' : is_open carrier) /-- Reinterpret an `open_subgroup` as a `subgroup`. -/ add_decl_doc open_subgroup.to_subgroup /-- Reinterpret an `open_add_subgroup` as an `add_subgroup`. -/ add_decl_doc open_add_subgroup.to_add_subgroup -- Tell Lean that `open_add_subgroup` is a namespace namespace open_add_subgroup end open_add_subgroup namespace open_subgroup open function topological_space variables {G : Type*} [group G] [topological_space G] variables {U V : open_subgroup G} {g : G} @[to_additive] instance has_coe_set : has_coe_t (open_subgroup G) (set G) := ⟨λ U, U.1⟩ @[to_additive] instance : has_mem G (open_subgroup G) := ⟨λ g U, g ∈ (U : set G)⟩ @[to_additive] instance has_coe_subgroup : has_coe_t (open_subgroup G) (subgroup G) := ⟨to_subgroup⟩ @[to_additive] instance has_coe_opens : has_coe_t (open_subgroup G) (opens G) := ⟨λ U, ⟨U, U.is_open'⟩⟩ @[simp, to_additive] lemma mem_coe : g ∈ (U : set G) ↔ g ∈ U := iff.rfl @[simp, to_additive] lemma mem_coe_opens : g ∈ (U : opens G) ↔ g ∈ U := iff.rfl @[simp, to_additive] lemma mem_coe_subgroup : g ∈ (U : subgroup G) ↔ g ∈ U := iff.rfl attribute [norm_cast] mem_coe mem_coe_opens mem_coe_subgroup open_add_subgroup.mem_coe open_add_subgroup.mem_coe_opens open_add_subgroup.mem_coe_add_subgroup @[to_additive] lemma coe_injective : injective (coe : open_subgroup G → set G) := λ U V h, by cases U; cases V; congr; assumption @[ext, to_additive] lemma ext (h : ∀ x, x ∈ U ↔ x ∈ V) : (U = V) := coe_injective $ set.ext h @[to_additive] lemma ext_iff : (U = V) ↔ (∀ x, x ∈ U ↔ x ∈ V) := ⟨λ h x, h ▸ iff.rfl, ext⟩ variable (U) @[to_additive] protected lemma is_open : is_open (U : set G) := U.is_open' @[to_additive] protected lemma one_mem : (1 : G) ∈ U := U.one_mem' @[to_additive] protected lemma inv_mem {g : G} (h : g ∈ U) : g⁻¹ ∈ U := U.inv_mem' h @[to_additive] protected lemma mul_mem {g₁ g₂ : G} (h₁ : g₁ ∈ U) (h₂ : g₂ ∈ U) : g₁ * g₂ ∈ U := U.mul_mem' h₁ h₂ @[to_additive] lemma mem_nhds_one : (U : set G) ∈ 𝓝 (1 : G) := is_open.mem_nhds U.is_open U.one_mem variable {U} @[to_additive] instance : has_top (open_subgroup G) := ⟨{ is_open' := is_open_univ, .. (⊤ : subgroup G) }⟩ @[to_additive] instance : inhabited (open_subgroup G) := ⟨⊤⟩ @[to_additive] lemma is_closed [has_continuous_mul G] (U : open_subgroup G) : is_closed (U : set G) := begin apply is_open_compl_iff.1, refine is_open_iff_forall_mem_open.2 (λ x hx, ⟨(λ y, y * x⁻¹) ⁻¹' U, _, _, _⟩), { intros u hux, simp only [set.mem_preimage, set.mem_compl_iff, mem_coe] at hux hx ⊢, refine mt (λ hu, _) hx, convert U.mul_mem (U.inv_mem hux) hu, simp }, { exact U.is_open.preimage (continuous_mul_right _) }, { simp [U.one_mem] } end section variables {H : Type*} [group H] [topological_space H] /-- The product of two open subgroups as an open subgroup of the product group. -/ @[to_additive "The product of two open subgroups as an open subgroup of the product group."] def prod (U : open_subgroup G) (V : open_subgroup H) : open_subgroup (G × H) := { carrier := (U : set G).prod (V : set H), is_open' := U.is_open.prod V.is_open, .. (U : subgroup G).prod (V : subgroup H) } end @[to_additive] instance : partial_order (open_subgroup G) := { le := λ U V, ∀ ⦃x⦄, x ∈ U → x ∈ V, .. partial_order.lift (coe : open_subgroup G → set G) coe_injective } @[to_additive] instance : semilattice_inf_top (open_subgroup G) := { inf := λ U V, { is_open' := is_open.inter U.is_open V.is_open, .. (U : subgroup G) ⊓ V }, inf_le_left := λ U V, set.inter_subset_left _ _, inf_le_right := λ U V, set.inter_subset_right _ _, le_inf := λ U V W hV hW, set.subset_inter hV hW, top := ⊤, le_top := λ U, set.subset_univ _, ..open_subgroup.partial_order } @[simp, to_additive] lemma coe_inf : (↑(U ⊓ V) : set G) = (U : set G) ∩ V := rfl @[simp, to_additive] lemma coe_subset : (U : set G) ⊆ V ↔ U ≤ V := iff.rfl @[simp, to_additive] lemma coe_subgroup_le : (U : subgroup G) ≤ (V : subgroup G) ↔ U ≤ V := iff.rfl attribute [norm_cast] coe_inf coe_subset coe_subgroup_le open_add_subgroup.coe_inf open_add_subgroup.coe_subset open_add_subgroup.coe_add_subgroup_le variables {N : Type*} [group N] [topological_space N] /-- The preimage of an `open_subgroup` along a continuous `monoid` homomorphism is an `open_subgroup`. -/ @[to_additive "The preimage of an `open_add_subgroup` along a continuous `add_monoid` homomorphism is an `open_add_subgroup`."] def comap (f : G →* N) (hf : continuous f) (H : open_subgroup N) : open_subgroup G := { is_open' := H.is_open.preimage hf, .. (H : subgroup N).comap f } @[simp, to_additive] lemma coe_comap (H : open_subgroup N) (f : G →* N) (hf : continuous f) : (H.comap f hf : set G) = f ⁻¹' H := rfl @[simp, to_additive] lemma mem_comap {H : open_subgroup N} {f : G →* N} {hf : continuous f} {x : G} : x ∈ H.comap f hf ↔ f x ∈ H := iff.rfl @[to_additive] lemma comap_comap {P : Type*} [group P] [topological_space P] (K : open_subgroup P) (f₂ : N →* P) (hf₂ : continuous f₂) (f₁ : G →* N) (hf₁ : continuous f₁) : (K.comap f₂ hf₂).comap f₁ hf₁ = K.comap (f₂.comp f₁) (hf₂.comp hf₁) := rfl end open_subgroup namespace subgroup variables {G : Type*} [group G] [topological_space G] [has_continuous_mul G] (H : subgroup G) @[to_additive] lemma is_open_of_mem_nhds {g : G} (hg : (H : set G) ∈ 𝓝 g) : is_open (H : set G) := begin simp only [is_open_iff_mem_nhds, set_like.mem_coe] at hg ⊢, intros x hx, have : filter.tendsto (λ y, y * (x⁻¹ * g)) (𝓝 x) (𝓝 $ x * (x⁻¹ * g)) := (continuous_id.mul continuous_const).tendsto _, rw [mul_inv_cancel_left] at this, have := filter.mem_map.1 (this hg), replace hg : g ∈ H := set_like.mem_coe.1 (mem_of_mem_nhds hg), simp only [set_like.mem_coe, H.mul_mem_cancel_right (H.mul_mem (H.inv_mem hx) hg)] at this, exact this end @[to_additive] lemma is_open_of_open_subgroup {U : open_subgroup G} (h : U.1 ≤ H) : is_open (H : set G) := H.is_open_of_mem_nhds (filter.mem_sets_of_superset U.mem_nhds_one h) @[to_additive] lemma is_open_mono {H₁ H₂ : subgroup G} (h : H₁ ≤ H₂) (h₁ : is_open (H₁ :set G)) : is_open (H₂ : set G) := @is_open_of_open_subgroup _ _ _ _ H₂ { is_open' := h₁, .. H₁ } h end subgroup namespace open_subgroup variables {G : Type*} [group G] [topological_space G] [has_continuous_mul G] @[to_additive] instance : semilattice_sup_top (open_subgroup G) := { sup := λ U V, { is_open' := show is_open (((U : subgroup G) ⊔ V : subgroup G) : set G), from subgroup.is_open_mono le_sup_left U.is_open, .. ((U : subgroup G) ⊔ V) }, le_sup_left := λ U V, coe_subgroup_le.1 le_sup_left, le_sup_right := λ U V, coe_subgroup_le.1 le_sup_right, sup_le := λ U V W hU hV, coe_subgroup_le.1 (sup_le hU hV), ..open_subgroup.semilattice_inf_top } end open_subgroup namespace submodule open open_add_subgroup variables {R : Type*} {M : Type*} [comm_ring R] variables [add_comm_group M] [topological_space M] [topological_add_group M] [module R M] lemma is_open_mono {U P : submodule R M} (h : U ≤ P) (hU : is_open (U : set M)) : is_open (P : set M) := @add_subgroup.is_open_mono M _ _ _ U.to_add_subgroup P.to_add_subgroup h hU end submodule namespace ideal variables {R : Type*} [comm_ring R] variables [topological_space R] [topological_ring R] lemma is_open_of_open_subideal {U I : ideal R} (h : U ≤ I) (hU : is_open (U : set R)) : is_open (I : set R) := submodule.is_open_mono h hU end ideal
245edc439b8042ed43b702cd0b73f5cd8f5e0fe6
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch4/ex0505.lean
c0f64399ab5f17f068dbaa5c7863223b572b4dff
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
37
lean
example (n : ℕ) : ℕ := ‹ℕ›
6f655db6c51e7c3410af467c2903d8cb29e02d42
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/group_theory/submonoid/center.lean
08f6b5a7c815a58e59475ab2a48ead4ff040bf1c
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,802
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import group_theory.submonoid.operations import data.fintype.basic /-! # Centers of magmas and monoids ## Main definitions * `set.center`: the center of a magma * `submonoid.center`: the center of a monoid * `set.add_center`: the center of an additive magma * `add_submonoid.center`: the center of an additive monoid We provide `subgroup.center`, `add_subgroup.center`, `subsemiring.center`, and `subring.center` in other files. -/ variables {M : Type*} namespace set variables (M) /-- The center of a magma. -/ @[to_additive add_center /-" The center of an additive magma. "-/] def center [has_mul M] : set M := {z | ∀ m, m * z = z * m} @[to_additive mem_add_center] lemma mem_center_iff [has_mul M] {z : M} : z ∈ center M ↔ ∀ g, g * z = z * g := iff.rfl instance decidable_mem_center [has_mul M] [decidable_eq M] [fintype M] : decidable_pred (∈ center M) := λ _, decidable_of_iff' _ (mem_center_iff M) @[simp, to_additive zero_mem_add_center] lemma one_mem_center [mul_one_class M] : (1 : M) ∈ set.center M := by simp [mem_center_iff] @[simp] lemma zero_mem_center [mul_zero_class M] : (0 : M) ∈ set.center M := by simp [mem_center_iff] variables {M} @[simp, to_additive add_mem_add_center] lemma mul_mem_center [semigroup M] {a b : M} (ha : a ∈ set.center M) (hb : b ∈ set.center M) : a * b ∈ set.center M := λ g, by rw [mul_assoc, ←hb g, ← mul_assoc, ha g, mul_assoc] @[simp, to_additive neg_mem_add_center] lemma inv_mem_center [group M] {a : M} (ha : a ∈ set.center M) : a⁻¹ ∈ set.center M := λ g, by rw [← inv_inj, mul_inv_rev, inv_inv, ← ha, mul_inv_rev, inv_inv] @[simp] lemma add_mem_center [distrib M] {a b : M} (ha : a ∈ set.center M) (hb : b ∈ set.center M) : a + b ∈ set.center M := λ c, by rw [add_mul, mul_add, ha c, hb c] @[simp] lemma neg_mem_center [ring M] {a : M} (ha : a ∈ set.center M) : -a ∈ set.center M := λ c, by rw [←neg_mul_comm, ha (-c), neg_mul_comm] @[to_additive subset_add_center_add_units] lemma subset_center_units [monoid M] : (coe : units M → M) ⁻¹' center M ⊆ set.center (units M) := λ a ha b, units.ext $ ha _ lemma center_units_subset [group_with_zero M] : set.center (units M) ⊆ (coe : units M → M) ⁻¹' center M := λ a ha b, begin obtain rfl | hb := eq_or_ne b 0, { rw [zero_mul, mul_zero], }, { exact units.ext_iff.mp (ha (units.mk0 _ hb)) } end /-- In a group with zero, the center of the units is the preimage of the center. -/ lemma center_units_eq [group_with_zero M] : set.center (units M) = (coe : units M → M) ⁻¹' center M := subset.antisymm center_units_subset subset_center_units @[simp] lemma inv_mem_center₀ [group_with_zero M] {a : M} (ha : a ∈ set.center M) : a⁻¹ ∈ set.center M := begin obtain rfl | ha0 := eq_or_ne a 0, { rw inv_zero, exact zero_mem_center M }, lift a to units M using ha0, rw ←units.coe_inv', exact center_units_subset (inv_mem_center (subset_center_units ha)), end @[simp, to_additive sub_mem_add_center] lemma div_mem_center [group M] {a b : M} (ha : a ∈ set.center M) (hb : b ∈ set.center M) : a / b ∈ set.center M := begin rw [div_eq_mul_inv], exact mul_mem_center ha (inv_mem_center hb), end @[simp] lemma div_mem_center₀ [group_with_zero M] {a b : M} (ha : a ∈ set.center M) (hb : b ∈ set.center M) : a / b ∈ set.center M := begin rw div_eq_mul_inv, exact mul_mem_center ha (inv_mem_center₀ hb), end variables (M) @[simp, to_additive add_center_eq_univ] lemma center_eq_univ [comm_semigroup M] : center M = set.univ := subset.antisymm (subset_univ _) $ λ x _ y, mul_comm y x end set namespace submonoid section variables (M) [monoid M] /-- The center of a monoid `M` is the set of elements that commute with everything in `M` -/ @[to_additive "The center of a monoid `M` is the set of elements that commute with everything in `M`"] def center : submonoid M := { carrier := set.center M, one_mem' := set.one_mem_center M, mul_mem' := λ a b, set.mul_mem_center } @[to_additive] lemma coe_center : ↑(center M) = set.center M := rfl variables {M} @[to_additive] lemma mem_center_iff {z : M} : z ∈ center M ↔ ∀ g, g * z = z * g := iff.rfl instance decidable_mem_center [decidable_eq M] [fintype M] : decidable_pred (∈ center M) := λ _, decidable_of_iff' _ mem_center_iff /-- The center of a monoid is commutative. -/ instance : comm_monoid (center M) := { mul_comm := λ a b, subtype.ext $ b.prop _, .. (center M).to_monoid } end section variables (M) [comm_monoid M] @[simp] lemma center_eq_top : center M = ⊤ := set_like.coe_injective (set.center_eq_univ M) end end submonoid
cf18693a2b058bbcaef255be8edc9db2197ec0bf
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/fintype/order.lean
0a2de3b661247587e9e4230a18ecedb7cb542884
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
6,045
lean
/- Copyright (c) 2021 Peter Nelson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Peter Nelson, Yaël Dillies -/ import data.fintype.lattice import data.finset.order /-! # Order structures on finite types This file provides order instances on fintypes. ## Computable instances On a `fintype`, we can construct * an `order_bot` from `semilattice_inf`. * an `order_top` from `semilattice_sup`. * a `bounded_order` from `lattice`. Those are marked as `def` to avoid defeqness issues. ## Completion instances Those instances are noncomputable because the definitions of `Sup` and `Inf` use `set.to_finset` and set membership is undecidable in general. On a `fintype`, we can promote: * a `lattice` to a `complete_lattice`. * a `distrib_lattice` to a `complete_distrib_lattice`. * a `linear_order` to a `complete_linear_order`. * a `boolean_algebra` to a `complete_boolean_algebra`. Those are marked as `def` to avoid typeclass loops. ## Concrete instances We provide a few instances for concrete types: * `fin.complete_linear_order` * `bool.complete_linear_order` * `bool.complete_boolean_algebra` -/ open finset namespace fintype variables {ι α : Type*} [fintype ι] [fintype α] section nonempty variables (α) [nonempty α] /-- Constructs the `⊥` of a finite nonempty `semilattice_inf`. -/ @[reducible] -- See note [reducible non-instances] def to_order_bot [semilattice_inf α] : order_bot α := { bot := univ.inf' univ_nonempty id, bot_le := λ a, inf'_le _ $ mem_univ a } /-- Constructs the `⊤` of a finite nonempty `semilattice_sup` -/ @[reducible] -- See note [reducible non-instances] def to_order_top [semilattice_sup α] : order_top α := { top := univ.sup' univ_nonempty id, le_top := λ a, le_sup' _ $ mem_univ a } /-- Constructs the `⊤` and `⊥` of a finite nonempty `lattice`. -/ @[reducible] -- See note [reducible non-instances] def to_bounded_order [lattice α] : bounded_order α := { ..to_order_bot α, ..to_order_top α } end nonempty section bounded_order variables (α) open_locale classical /-- A finite bounded lattice is complete. -/ @[reducible] -- See note [reducible non-instances] noncomputable def to_complete_lattice [lattice α] [bounded_order α] : complete_lattice α := { Sup := λ s, s.to_finset.sup id, Inf := λ s, s.to_finset.inf id, le_Sup := λ _ _ ha, finset.le_sup (set.mem_to_finset.mpr ha), Sup_le := λ s _ ha, finset.sup_le (λ b hb, ha _ $ set.mem_to_finset.mp hb), Inf_le := λ _ _ ha, finset.inf_le (set.mem_to_finset.mpr ha), le_Inf := λ s _ ha, finset.le_inf (λ b hb, ha _ $ set.mem_to_finset.mp hb), .. ‹lattice α›, .. ‹bounded_order α› } /-- A finite bounded distributive lattice is completely distributive. -/ @[reducible] -- See note [reducible non-instances] noncomputable def to_complete_distrib_lattice [distrib_lattice α] [bounded_order α] : complete_distrib_lattice α := { infi_sup_le_sup_Inf := λ a s, begin convert (finset.inf_sup_distrib_left _ _ _).ge, convert (finset.inf_eq_infi _ _).symm, simp_rw set.mem_to_finset, refl, end, inf_Sup_le_supr_inf := λ a s, begin convert (finset.sup_inf_distrib_left _ _ _).le, convert (finset.sup_eq_supr _ _).symm, simp_rw set.mem_to_finset, refl, end, ..to_complete_lattice α } /-- A finite bounded linear order is complete. -/ @[reducible] -- See note [reducible non-instances] noncomputable def to_complete_linear_order [linear_order α] [bounded_order α] : complete_linear_order α := { ..to_complete_lattice α, .. ‹linear_order α› } /-- A finite boolean algebra is complete. -/ @[reducible] -- See note [reducible non-instances] noncomputable def to_complete_boolean_algebra [boolean_algebra α] : complete_boolean_algebra α := { ..fintype.to_complete_distrib_lattice α, .. ‹boolean_algebra α› } end bounded_order section nonempty variables (α) [nonempty α] /-- A nonempty finite lattice is complete. If the lattice is already a `bounded_order`, then use `fintype.to_complete_lattice` instead, as this gives definitional equality for `⊥` and `⊤`. -/ @[reducible] -- See note [reducible non-instances] noncomputable def to_complete_lattice_of_nonempty [lattice α] : complete_lattice α := @to_complete_lattice _ _ _ $ @to_bounded_order α _ ⟨classical.arbitrary α⟩ _ /-- A nonempty finite linear order is complete. If the linear order is already a `bounded_order`, then use `fintype.to_complete_linear_order` instead, as this gives definitional equality for `⊥` and `⊤`. -/ @[reducible] -- See note [reducible non-instances] noncomputable def to_complete_linear_order_of_nonempty [linear_order α] : complete_linear_order α := { ..to_complete_lattice_of_nonempty α, .. ‹linear_order α› } end nonempty end fintype /-! ### Concrete instances -/ noncomputable instance {n : ℕ} : complete_linear_order (fin (n + 1)) := fintype.to_complete_linear_order _ noncomputable instance : complete_linear_order bool := fintype.to_complete_linear_order _ noncomputable instance : complete_boolean_algebra bool := fintype.to_complete_boolean_algebra _ /-! ### Directed Orders -/ variable {α : Type*} theorem directed.fintype_le {r : α → α → Prop} [is_trans α r] {β γ : Type*} [nonempty γ] {f : γ → α} [fintype β] (D : directed r f) (g : β → γ) : ∃ z, ∀ i, r (f (g i)) (f z) := begin classical, obtain ⟨z, hz⟩ := D.finset_le (finset.image g finset.univ), exact ⟨z, λ i, hz (g i) (finset.mem_image_of_mem g (finset.mem_univ i))⟩, end lemma fintype.exists_le [nonempty α] [preorder α] [is_directed α (≤)] {β : Type*} [fintype β] (f : β → α) : ∃ M, ∀ i, (f i) ≤ M := directed_id.fintype_le _ lemma fintype.bdd_above_range [nonempty α] [preorder α] [is_directed α (≤)] {β : Type*} [fintype β] (f : β → α) : bdd_above (set.range f) := begin obtain ⟨M, hM⟩ := fintype.exists_le f, refine ⟨M, λ a ha, _⟩, obtain ⟨b, rfl⟩ := ha, exact hM b, end
9ace349e6da25812a3005bdd77a4756535ba3db1
94e33a31faa76775069b071adea97e86e218a8ee
/src/analysis/normed/group/SemiNormedGroup.lean
4c1ec7aa17b989acb4a81da421d4802f323087ce
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
6,746
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Riccardo Brasca -/ import analysis.normed.group.hom import category_theory.limits.shapes.zero_morphisms import category_theory.concrete_category.bundled_hom import category_theory.elementwise /-! # The category of seminormed groups We define `SemiNormedGroup`, the category of seminormed groups and normed group homs between them, as well as `SemiNormedGroup₁`, the subcategory of norm non-increasing morphisms. -/ noncomputable theory universes u open category_theory /-- The category of seminormed abelian groups and bounded group homomorphisms. -/ def SemiNormedGroup : Type (u+1) := bundled semi_normed_group namespace SemiNormedGroup instance bundled_hom : bundled_hom @normed_group_hom := ⟨@normed_group_hom.to_fun, @normed_group_hom.id, @normed_group_hom.comp, @normed_group_hom.coe_inj⟩ attribute [derive [large_category, concrete_category]] SemiNormedGroup instance : has_coe_to_sort SemiNormedGroup (Type u) := bundled.has_coe_to_sort /-- Construct a bundled `SemiNormedGroup` from the underlying type and typeclass. -/ def of (M : Type u) [semi_normed_group M] : SemiNormedGroup := bundled.of M instance (M : SemiNormedGroup) : semi_normed_group M := M.str @[simp] lemma coe_of (V : Type u) [semi_normed_group V] : (SemiNormedGroup.of V : Type u) = V := rfl @[simp] lemma coe_id (V : SemiNormedGroup) : ⇑(𝟙 V) = id := rfl @[simp] lemma coe_comp {M N K : SemiNormedGroup} (f : M ⟶ N) (g : N ⟶ K) : ((f ≫ g) : M → K) = g ∘ f := rfl instance : inhabited SemiNormedGroup := ⟨of punit⟩ instance of_unique (V : Type u) [semi_normed_group V] [i : unique V] : unique (SemiNormedGroup.of V) := i instance : limits.has_zero_morphisms.{u (u+1)} SemiNormedGroup := {} @[simp] lemma zero_apply {V W : SemiNormedGroup} (x : V) : (0 : V ⟶ W) x = 0 := rfl lemma is_zero_of_subsingleton (V : SemiNormedGroup) [subsingleton V] : limits.is_zero V := begin refine ⟨λ X, ⟨⟨⟨0⟩, λ f, _⟩⟩, λ X, ⟨⟨⟨0⟩, λ f, _⟩⟩⟩, { ext, have : x = 0 := subsingleton.elim _ _, simp only [this, map_zero], }, { ext, apply subsingleton.elim } end instance has_zero_object : limits.has_zero_object SemiNormedGroup.{u} := ⟨⟨of punit, is_zero_of_subsingleton _⟩⟩ lemma iso_isometry_of_norm_noninc {V W : SemiNormedGroup} (i : V ≅ W) (h1 : i.hom.norm_noninc) (h2 : i.inv.norm_noninc) : isometry i.hom := begin apply add_monoid_hom_class.isometry_of_norm, intro v, apply le_antisymm (h1 v), calc ∥v∥ = ∥i.inv (i.hom v)∥ : by rw [iso.hom_inv_id_apply] ... ≤ ∥i.hom v∥ : h2 _, end end SemiNormedGroup /-- `SemiNormedGroup₁` is a type synonym for `SemiNormedGroup`, which we shall equip with the category structure consisting only of the norm non-increasing maps. -/ def SemiNormedGroup₁ : Type (u+1) := bundled semi_normed_group namespace SemiNormedGroup₁ instance : has_coe_to_sort SemiNormedGroup₁ (Type u) := bundled.has_coe_to_sort instance : large_category.{u} SemiNormedGroup₁ := { hom := λ X Y, { f : normed_group_hom X Y // f.norm_noninc }, id := λ X, ⟨normed_group_hom.id X, normed_group_hom.norm_noninc.id⟩, comp := λ X Y Z f g, ⟨(g : normed_group_hom Y Z).comp (f : normed_group_hom X Y), g.2.comp f.2⟩, } @[ext] lemma hom_ext {M N : SemiNormedGroup₁} (f g : M ⟶ N) (w : (f : M → N) = (g : M → N)) : f = g := subtype.eq (normed_group_hom.ext (congr_fun w)) instance : concrete_category.{u} SemiNormedGroup₁ := { forget := { obj := λ X, X, map := λ X Y f, f, }, forget_faithful := {} } /-- Construct a bundled `SemiNormedGroup₁` from the underlying type and typeclass. -/ def of (M : Type u) [semi_normed_group M] : SemiNormedGroup₁ := bundled.of M instance (M : SemiNormedGroup₁) : semi_normed_group M := M.str /-- Promote a morphism in `SemiNormedGroup` to a morphism in `SemiNormedGroup₁`. -/ def mk_hom {M N : SemiNormedGroup} (f : M ⟶ N) (i : f.norm_noninc) : SemiNormedGroup₁.of M ⟶ SemiNormedGroup₁.of N := ⟨f, i⟩ @[simp] lemma mk_hom_apply {M N : SemiNormedGroup} (f : M ⟶ N) (i : f.norm_noninc) (x) : mk_hom f i x = f x := rfl /-- Promote an isomorphism in `SemiNormedGroup` to an isomorphism in `SemiNormedGroup₁`. -/ @[simps] def mk_iso {M N : SemiNormedGroup} (f : M ≅ N) (i : f.hom.norm_noninc) (i' : f.inv.norm_noninc) : SemiNormedGroup₁.of M ≅ SemiNormedGroup₁.of N := { hom := mk_hom f.hom i, inv := mk_hom f.inv i', hom_inv_id' := by { apply subtype.eq, exact f.hom_inv_id, }, inv_hom_id' := by { apply subtype.eq, exact f.inv_hom_id, }, } instance : has_forget₂ SemiNormedGroup₁ SemiNormedGroup := { forget₂ := { obj := λ X, X, map := λ X Y f, f.1, }, } @[simp] lemma coe_of (V : Type u) [semi_normed_group V] : (SemiNormedGroup₁.of V : Type u) = V := rfl @[simp] lemma coe_id (V : SemiNormedGroup₁) : ⇑(𝟙 V) = id := rfl @[simp] lemma coe_comp {M N K : SemiNormedGroup₁} (f : M ⟶ N) (g : N ⟶ K) : ((f ≫ g) : M → K) = g ∘ f := rfl -- If `coe_fn_coe_base` fires before `coe_comp`, `coe_comp'` puts us back in normal form. @[simp] lemma coe_comp' {M N K : SemiNormedGroup₁} (f : M ⟶ N) (g : N ⟶ K) : ((f ≫ g) : normed_group_hom M K) = (↑g : normed_group_hom N K).comp ↑f := rfl instance : inhabited SemiNormedGroup₁ := ⟨of punit⟩ instance of_unique (V : Type u) [semi_normed_group V] [i : unique V] : unique (SemiNormedGroup₁.of V) := i instance : limits.has_zero_morphisms.{u (u+1)} SemiNormedGroup₁ := { has_zero := λ X Y, { zero := ⟨0, normed_group_hom.norm_noninc.zero⟩, }, comp_zero' := λ X Y f Z, by { ext, refl, }, zero_comp' := λ X Y Z f, by { ext, simp [coe_fn_coe_base'] } } @[simp] lemma zero_apply {V W : SemiNormedGroup₁} (x : V) : (0 : V ⟶ W) x = 0 := rfl lemma is_zero_of_subsingleton (V : SemiNormedGroup₁) [subsingleton V] : limits.is_zero V := begin refine ⟨λ X, ⟨⟨⟨0⟩, λ f, _⟩⟩, λ X, ⟨⟨⟨0⟩, λ f, _⟩⟩⟩, { ext, have : x = 0 := subsingleton.elim _ _, simp only [this, map_zero], exact map_zero f.1 }, { ext, apply subsingleton.elim } end instance has_zero_object : limits.has_zero_object SemiNormedGroup₁.{u} := ⟨⟨of punit, is_zero_of_subsingleton _⟩⟩ lemma iso_isometry {V W : SemiNormedGroup₁} (i : V ≅ W) : isometry i.hom := begin change isometry (i.hom : V →+ W), refine add_monoid_hom_class.isometry_of_norm i.hom _, intro v, apply le_antisymm (i.hom.2 v), calc ∥v∥ = ∥i.inv (i.hom v)∥ : by rw [iso.hom_inv_id_apply] ... ≤ ∥i.hom v∥ : i.inv.2 _, end end SemiNormedGroup₁
7b5dbb0d28d72001f59e1274b08984e6da993889
80d0f8071ea62262937ab36f5887a61735adea09
/src/certigrad/reparam.lean
c0afe47ec51222835e12511c6de98a29a1e0e56f
[ "Apache-2.0" ]
permissive
wudcscheme/certigrad
94805fa6a61f993c69a824429a103c9613a65a48
c9a06e93f1ec58196d6d3b8563b29868d916727f
refs/heads/master
1,679,386,475,077
1,551,651,022,000
1,551,651,022,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,108
lean
/- Copyright (c) 2017 Daniel Selsam. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Daniel Selsam Certified graph transformation that "reparameterizes" a specific occurrence of a stochastic choice. -/ import .util .tensor .tfacts .compute_grad .graph .tactics .ops .predicates .lemmas .env namespace certigrad open list section algebra open T lemma mvn_transform {shape : S} (μ σ x : T shape) (H_σ : σ > 0) : mvn_pdf μ σ x = (prod σ⁻¹) * mvn_pdf 0 1 ((x - μ) / σ) := calc mvn_pdf μ σ x = prod ((sqrt ((2 * pi shape) * square σ))⁻¹ * exp ((- 2⁻¹) * (square $ (x - μ) / σ))) : rfl ... = prod ((sqrt (2 * pi shape) * σ)⁻¹ * exp ((- 2⁻¹) * (square $ (x - μ) / σ))) : by rw [sqrt_mul, sqrt_square] ... = prod (((sqrt (2 * pi shape))⁻¹ * σ⁻¹) * exp ((- 2⁻¹) * (square $ (x - μ) / σ))) : by rw [T.mul_inv_pos (sqrt_pos two_pi_pos) H_σ] ... = (prod σ⁻¹) * prod ((sqrt (2 * pi shape))⁻¹ * exp ((- 2⁻¹) * (square $ (x - μ) / σ))) : by simp [prod_mul] ... = (prod σ⁻¹) * prod ((sqrt ((2 * pi shape) * square 1))⁻¹ * exp ((- 2⁻¹) * (square ((((x - μ) / σ) - 0) / 1)))) : by simp [T.div_one, square] ... = (prod σ⁻¹) * mvn_pdf 0 1 ((x - μ) / σ) : rfl end algebra open sprog lemma mvn_reparam_same {shape oshape : S} {μ σ : T shape} (f : dvec T [shape] → T oshape) : σ > 0 → E (prim (rand.op.mvn shape) ⟦μ, σ⟧) f = E (bind (prim (rand.op.mvn_std shape) ⟦⟧) (λ (x : dvec T [shape]), ret ⟦(x^.head * σ) + μ⟧)) f := assume (H_σ_pos : σ > 0), begin simp only [E.E_bind, E.E_ret], dunfold E rand.op.mvn rand.op.pdf T.dintegral dvec.head rand.pdf.mvn rand.pdf.mvn_std, simp only [λ x, mvn_transform μ σ x H_σ_pos], assert H : ∀ (x : T shape), ((σ * x + μ + -μ) / σ) = x, { intro x, simp only [add_assoc, add_neg_self, add_zero], rw mul_comm, rw -T.mul_div_mul_alt, rw T.div_self H_σ_pos, rw mul_one}, definev g : T shape → T oshape := λ (x : T shape), T.mvn_pdf 0 1 ((x - μ) / σ) ⬝ f ⟦x⟧, assert H_rhs : ∀ (x : T shape), T.mvn_pdf 0 1 x ⬝ f ⟦x * σ + μ⟧ = g (σ * x + μ), { intro x, dsimp, rw H, simp }, rw funext H_rhs, rw T.integral_scale_shift_var g, dsimp, simp [T.smul_group] end def reparameterize_pre (eshape : S) : list node → env → Prop | [] inputs := true | (⟨⟨ref, shape⟩, [⟨μ, .(shape)⟩, ⟨σ, .(shape)⟩], operator.rand (rand.op.mvn .(shape))⟩::nodes) inputs := eshape = shape ∧ σ ≠ μ ∧ 0 < env.get (σ, shape) inputs | (⟨ref, parents, operator.det op⟩::nodes) inputs := reparameterize_pre nodes (env.insert ref (op^.f (env.get_ks parents inputs)) inputs) | (⟨ref, parents, operator.rand op⟩::nodes) inputs := ∀ x, reparameterize_pre nodes (env.insert ref x inputs) def reparameterize (fname : ID) : list node → list node | [] := [] | (⟨⟨ident, shape⟩, [⟨μ, .(shape)⟩, ⟨σ, .(shape)⟩], operator.rand (rand.op.mvn .(shape))⟩::nodes) := (⟨(fname, shape), [], operator.rand (rand.op.mvn_std shape)⟩ ::⟨(ident, shape), [(fname, shape), (σ, shape), (μ, shape)], operator.det (ops.mul_add shape)⟩ ::nodes) | (n::nodes) := n :: reparameterize nodes theorem reparameterize_correct (costs : list ID) : ∀ (nodes : list node) (inputs : env) (fref : reference), reparameterize_pre fref.2 nodes inputs → uniq_ids nodes inputs → all_parents_in_env inputs nodes → (¬ env.has_key fref inputs) → fref ∉ map node.ref nodes → (fref.1 ∉ costs) → E (graph.to_dist (λ env₀, ⟦sum_costs env₀ costs⟧) inputs (reparameterize fref.1 nodes)) dvec.head = E (graph.to_dist (λ env₀, ⟦sum_costs env₀ costs⟧) inputs nodes) dvec.head | [] _ _ _ _ _ _ _ _ := rfl | (⟨⟨ident, shape⟩, [⟨μ, .(shape)⟩, ⟨σ, .(shape)⟩], operator.rand (rand.op.mvn .(shape))⟩::nodes) inputs fref H_pre H_uids H_ps_in_env H_fresh₁ H_fresh₂ H_not_cost := begin dunfold reparameterize, assertv H_eshape : fref.2 = shape := H_pre^.left, assert H_fref : fref = (fref.1, shape), { clear reparameterize_correct, cases fref with fref₁ fref₂, dsimp at H_eshape, rw H_eshape }, assertv H_σ_μ : σ ≠ μ := H_pre^.right^.left, dunfold graph.to_dist operator.to_dist, dsimp, simp [E.E_bind], erw (mvn_reparam_same _ H_pre^.right^.right), simp [E.E_bind, E.E_ret], dunfold dvec.head, dsimp, apply congr_arg, apply funext, intro x, assertv H_μ_in : env.has_key (μ, shape) inputs := H_ps_in_env^.left (μ, shape) (mem_cons_self _ _), assertv H_σ_in : env.has_key (σ, shape) inputs := H_ps_in_env^.left (σ, shape) (mem_cons_of_mem _ (mem_cons_self _ _)), assertv H_ident_nin : ¬ env.has_key (ident, shape) inputs := H_uids^.left, assertv H_μ_neq_ident : (μ, shape) ≠ (ident, shape) := env_in_nin_ne H_μ_in H_ident_nin, assertv H_σ_neq_ident : (σ, shape) ≠ (ident, shape) := env_in_nin_ne H_σ_in H_ident_nin, assertv H_μ_neq_fref : (μ, shape) ≠ (fref.1, shape) := eq.rec_on H_fref (env_in_nin_ne H_μ_in H_fresh₁), assertv H_σ_neq_fref : (σ, shape) ≠ (fref.1, shape) := eq.rec_on H_fref (env_in_nin_ne H_σ_in H_fresh₁), assertv H_ident_neq_fref : (ident, shape) ≠ (fref.1, shape) := eq.rec_on H_fref (mem_not_mem_neq mem_of_cons_same H_fresh₂), dunfold env.get_ks, tactic.dget_dinsert, rw (env.insert_insert_flip _ _ _ H_ident_neq_fref), dsimp, definev fval : T shape := dvec.head x, definev fval_inputs : env := env.insert (ident, shape) (det.op.f (ops.mul_add shape) ⟦dvec.head x, (env.get (σ, shape) inputs : T shape), (env.get (μ, shape) inputs : T shape)⟧) inputs, assertv H_ps_in_env_next : all_parents_in_env fval_inputs nodes := H_ps_in_env^. right _, assertv H_fresh₁_next : ¬ env.has_key (fref.1, shape) fval_inputs := eq.rec_on H_fref (env_not_has_key_insert (eq.rec_on (eq.symm H_fref) $ ne.symm H_ident_neq_fref) H_fresh₁), assertv H_fresh₂_next : (fref.1, shape) ∉ map node.ref nodes := eq.rec_on H_fref (not_mem_of_not_mem_cons H_fresh₂), erw (@to_dist_congr_insert costs nodes fval_inputs (fref.1, shape) fval H_ps_in_env_next H_fresh₁_next H_fresh₂_next H_not_cost), dsimp, dunfold det.op.f, rw [add_comm, mul_comm], reflexivity end | (⟨(ref, shape), [], operator.det op⟩::nodes) inputs fref H_pre H_uids H_ps_in_env H_fresh₁ H_fresh₂ H_not_cost := begin dunfold reparameterize graph.to_dist operator.to_dist, simp [E.E_bind, E.E_ret], definev x : T shape := op^.f (env.get_ks [] inputs), assertv H_pre_next : reparameterize_pre fref.2 nodes (env.insert (ref, shape) x inputs) := by apply H_pre, assertv H_ps_in_env_next : all_parents_in_env (env.insert (ref, shape) x inputs) nodes := H_ps_in_env^.right _, assertv H_fresh₁_next : ¬ env.has_key fref (env.insert (ref, shape) x inputs) := env_not_has_key_insert (ne_of_not_mem_cons H_fresh₂) H_fresh₁, assertv H_fresh₂_next : fref ∉ map node.ref nodes := not_mem_of_not_mem_cons H_fresh₂, apply (reparameterize_correct _ _ fref H_pre_next (H_uids^.right _) H_ps_in_env_next H_fresh₁_next H_fresh₂_next H_not_cost) end | (⟨(ref, shape), [], operator.rand op⟩::nodes) inputs fref H_pre H_uids H_ps_in_env H_fresh₁ H_fresh₂ H_not_cost := begin dunfold reparameterize graph.to_dist, simp [E.E_bind], apply congr_arg, apply funext, intro x, assertv H_pre_next : reparameterize_pre fref.2 nodes (env.insert (ref, shape) (dvec.head x) inputs) := by apply H_pre, assertv H_ps_in_env_next : all_parents_in_env (env.insert (ref, shape) (dvec.head x) inputs) nodes := H_ps_in_env^.right x^.head, assertv H_fresh₁_next : ¬ env.has_key fref (env.insert (ref, shape) x^.head inputs) := env_not_has_key_insert (ne_of_not_mem_cons H_fresh₂) H_fresh₁, assertv H_fresh₂_next : fref ∉ map node.ref nodes := not_mem_of_not_mem_cons H_fresh₂, apply (reparameterize_correct _ _ fref H_pre_next (H_uids^.right _) H_ps_in_env_next H_fresh₁_next H_fresh₂_next H_not_cost) end | (⟨(ref, shape), [(parent₁, shape₁)], operator.det op⟩::nodes) inputs fref H_pre H_uids H_ps_in_env H_fresh₁ H_fresh₂ H_not_cost := begin dunfold reparameterize graph.to_dist operator.to_dist, simp [E.E_bind, E.E_ret], definev x : T shape := det.op.f op (env.get_ks [(parent₁, shape₁)] inputs), assertv H_pre_next : reparameterize_pre fref.2 nodes (env.insert (ref, shape) x inputs) := by apply H_pre, assertv H_ps_in_env_next : all_parents_in_env (env.insert (ref, shape) x inputs) nodes := H_ps_in_env^.right x, assertv H_fresh₁_next : ¬ env.has_key fref (env.insert (ref, shape) x inputs) := env_not_has_key_insert (ne_of_not_mem_cons H_fresh₂) H_fresh₁, assertv H_fresh₂_next : fref ∉ map node.ref nodes := not_mem_of_not_mem_cons H_fresh₂, apply (reparameterize_correct _ _ fref H_pre_next (H_uids^.right _) H_ps_in_env_next H_fresh₁_next H_fresh₂_next H_not_cost) end | (⟨(ref, shape), [(parent₁, shape₁), (parent₂, shape₂)], operator.det op⟩::nodes) inputs fref H_pre H_uids H_ps_in_env H_fresh₁ H_fresh₂ H_not_cost := begin dunfold reparameterize graph.to_dist operator.to_dist, simp [E.E_bind, E.E_ret], definev x : T shape := det.op.f op (env.get_ks [(parent₁, shape₁), (parent₂, shape₂)] inputs), assertv H_pre_next : reparameterize_pre fref.2 nodes (env.insert (ref, shape) x inputs) := by apply H_pre, assertv H_ps_in_env_next : all_parents_in_env (env.insert (ref, shape) x inputs) nodes := H_ps_in_env^.right x, assertv H_fresh₁_next : ¬ env.has_key fref (env.insert (ref, shape) x inputs) := env_not_has_key_insert (ne_of_not_mem_cons H_fresh₂) H_fresh₁, assertv H_fresh₂_next : fref ∉ map node.ref nodes := not_mem_of_not_mem_cons H_fresh₂, apply (reparameterize_correct _ _ fref H_pre_next (H_uids^.right _) H_ps_in_env_next H_fresh₁_next H_fresh₂_next H_not_cost) end | (⟨(ref, shape), (parent₁, shape₁) :: (parent₂, shape₂) :: (parent₃, shape₃) :: parents, operator.det op⟩::nodes) inputs fref H_pre H_uids H_ps_in_env H_fresh₁ H_fresh₂ H_not_cost := begin dunfold reparameterize graph.to_dist operator.to_dist, simp [E.E_bind, E.E_ret], definev x : T shape := det.op.f op (env.get_ks ((parent₁, shape₁) :: (parent₂, shape₂) :: (parent₃, shape₃) :: parents) inputs), assertv H_pre_next : reparameterize_pre fref.2 nodes (env.insert (ref, shape) x inputs) := by apply H_pre, assertv H_ps_in_env_next : all_parents_in_env (env.insert (ref, shape) x inputs) nodes := H_ps_in_env^.right x, assertv H_fresh₁_next : ¬ env.has_key fref (env.insert (ref, shape) x inputs) := env_not_has_key_insert (ne_of_not_mem_cons H_fresh₂) H_fresh₁, assertv H_fresh₂_next : fref ∉ map node.ref nodes := not_mem_of_not_mem_cons H_fresh₂, apply (reparameterize_correct _ _ fref H_pre_next (H_uids^.right _) H_ps_in_env_next H_fresh₁_next H_fresh₂_next H_not_cost) end def reparam : graph → graph | g := ⟨reparameterize (ID.str label.ε) g^.nodes, g^.costs, g^.targets, g^.inputs⟩ end certigrad
81a8a10d19e7672d9bc033422c902d5a3c1a5fdd
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/tests/lean/run/dofun_prec.lean
388a21e0cb4c88fe87cca56d22ecc442b81c49f4
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
179
lean
new_frontend def tst1 (x : Nat) : IO Unit := when (x > 0) do IO.println "hello"; IO.println "world" def tst2 (xs : List Nat) : IO Unit := xs.forM fun x => do IO.println x
a83e0fae73a4423a8ceb6aac572f503036e84bb1
a721fe7446524f18ba361625fc01033d9c8b7a78
/elaborate/rev_concat_nat.lean
9a125defe64b713add5781552524d1ffc721b0ee
[]
no_license
Sterrs/leaning
8fd80d1f0a6117a220bb2e57ece639b9a63deadc
3901cc953694b33adda86cb88ca30ba99594db31
refs/heads/master
1,627,023,822,744
1,616,515,221,000
1,616,515,221,000
245,512,190
2
0
null
1,616,429,050,000
1,583,527,118,000
Lean
UTF-8
Lean
false
false
10,617
lean
λ {lst1 lst2 : mylist mynat}, mylist.rec (eq.rec true.intro (eq.rec (eq.refl (rev lst2 = rev lst2 ++ empty)) (eq.rec (eq.rec (eq.refl (rev lst2 = rev lst2 ++ empty)) (eq.rec (eq.refl (rev lst2 ++ empty)) (mylist.rec (eq.rec true.intro (eq.rec (eq.refl (empty = empty)) (eq.rec (eq.refl (empty = empty)) (propext {mp := λ (hl : empty = empty), true.intro, mpr := λ (hr : true), eq.refl empty})))) (λ (lst_head : mynat) (lst_tail : mylist mynat) (lst_ih : lst_tail ++ empty = lst_tail), eq.rec true.intro (eq.rec (eq.refl (lst_head :: (lst_tail ++ empty) = lst_head :: lst_tail)) (eq.rec (eq.rec (eq.rec (eq.rec (eq.refl (lst_head :: (lst_tail ++ empty) = lst_head :: lst_tail)) (eq.rec (eq.refl (eq (lst_head :: (lst_tail ++ empty)))) (eq.rec (eq.refl (lst_head :: (lst_tail ++ empty))) (eq.rec (eq.refl (lst_head :: (lst_tail ++ empty))) lst_ih)))) (propext {mp := λ (h : lst_head :: lst_tail = lst_head :: lst_tail), ⟨eq.refl lst_head, eq.refl lst_tail⟩, mpr := λ (a : lst_head = lst_head ∧ lst_tail = lst_tail), and.rec (λ (left : lst_head = lst_head) (right : lst_tail = lst_tail) («_» : lst_head = lst_head ∧ lst_tail = lst_tail), eq.refl (lst_head :: lst_tail)) a a})) (eq.rec (eq.rec (eq.refl (lst_head = lst_head ∧ lst_tail = lst_tail)) (propext {mp := λ (hl : lst_tail = lst_tail), true.intro, mpr := λ (hr : true), eq.refl lst_tail})) (eq.rec (eq.refl (and (lst_head = lst_head))) (propext {mp := λ (hl : lst_head = lst_head), true.intro, mpr := λ (hr : true), eq.refl lst_head})))) (propext {mp := and.left true, mpr := λ (h : true), ⟨h, h⟩})))) (rev lst2)))) (propext {mp := λ (hl : rev lst2 = rev lst2), true.intro, mpr := λ (hr : true), eq.refl (rev lst2)})))) (λ (lst1_head : mynat) (lst1_tail : mylist mynat) (lst1_ih : rev (lst1_tail ++ lst2) = rev lst2 ++ rev lst1_tail), eq.rec (eq.rec (eq.refl (rev lst2 ++ (rev lst1_tail ++ lst1_head :: empty))) (eq.rec (eq.refl (rev lst2 ++ rev lst1_tail ++ lst1_head :: empty = rev lst2 ++ (rev lst1_tail ++ lst1_head :: empty))) (eq.rec (eq.refl (rev lst2 ++ rev lst1_tail ++ lst1_head :: empty = rev lst2 ++ (rev lst1_tail ++ lst1_head :: empty))) (mylist.rec (eq.rec true.intro (eq.rec (eq.refl (rev lst1_tail ++ lst1_head :: empty = rev lst1_tail ++ lst1_head :: empty)) (eq.rec (eq.refl (rev lst1_tail ++ lst1_head :: empty = rev lst1_tail ++ lst1_head :: empty)) (propext {mp := λ (hl : rev lst1_tail ++ lst1_head :: empty = rev lst1_tail ++ lst1_head :: empty), true.intro, mpr := λ (hr : true), eq.refl (rev lst1_tail ++ lst1_head :: empty)})))) (λ (lst1_head_1 : mynat) (lst1_tail_1 : mylist mynat) (lst1_ih : lst1_tail_1 ++ rev lst1_tail ++ lst1_head :: empty = lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty)), eq.rec true.intro (eq.rec (eq.refl (lst1_head_1 :: (lst1_tail_1 ++ rev lst1_tail ++ lst1_head :: empty) = lst1_head_1 :: (lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty)))) (eq.rec (eq.rec (eq.rec (eq.rec (eq.refl (lst1_head_1 :: (lst1_tail_1 ++ rev lst1_tail ++ lst1_head :: empty) = lst1_head_1 :: (lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty)))) (eq.rec (eq.refl (eq (lst1_head_1 :: (lst1_tail_1 ++ rev lst1_tail ++ lst1_head :: empty)))) (eq.rec (eq.refl (lst1_head_1 :: (lst1_tail_1 ++ rev lst1_tail ++ lst1_head :: empty))) (eq.rec (eq.refl (lst1_head_1 :: (lst1_tail_1 ++ rev lst1_tail ++ lst1_head :: empty))) lst1_ih)))) (propext {mp := λ (h : lst1_head_1 :: (lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty)) = lst1_head_1 :: (lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty))), ⟨eq.refl lst1_head_1, eq.refl (lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty))⟩, mpr := λ (a : lst1_head_1 = lst1_head_1 ∧ lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty) = lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty)), and.rec (λ (left : lst1_head_1 = lst1_head_1) (right : lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty) = lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty)) («_» : lst1_head_1 = lst1_head_1 ∧ lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty) = lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty)), eq.refl (lst1_head_1 :: (lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty)))) a a})) (eq.rec (eq.rec (eq.refl (lst1_head_1 = lst1_head_1 ∧ lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty) = lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty))) (propext {mp := λ (hl : lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty) = lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty)), true.intro, mpr := λ (hr : true), eq.refl (lst1_tail_1 ++ (rev lst1_tail ++ lst1_head :: empty))})) (eq.rec (eq.refl (and (lst1_head_1 = lst1_head_1))) (propext {mp := λ (hl : lst1_head_1 = lst1_head_1), true.intro, mpr := λ (hr : true), eq.refl lst1_head_1})))) (propext {mp := and.left true, mpr := λ (h : true), ⟨h, h⟩})))) (rev lst2))))) (eq.rec (eq.refl (rev (lst1_tail ++ lst2) ++ lst1_head :: empty = rev lst2 ++ (rev lst1_tail ++ lst1_head :: empty))) (eq.rec (eq.refl (rev (lst1_tail ++ lst2) ++ lst1_head :: empty = rev lst2 ++ (rev lst1_tail ++ lst1_head :: empty))) lst1_ih))) lst1
43c4f5c7d3c1e62cf88f1f4df24bb43926c9c2b3
ad0c7d243dc1bd563419e2767ed42fb323d7beea
/analysis/topology/continuity.lean
184e2ab0504be3849c3fc8c719fb3d1a935f8f31
[ "Apache-2.0" ]
permissive
sebzim4500/mathlib
e0b5a63b1655f910dee30badf09bd7e191d3cf30
6997cafbd3a7325af5cb318561768c316ceb7757
refs/heads/master
1,585,549,958,618
1,538,221,723,000
1,538,221,723,000
150,869,076
0
0
Apache-2.0
1,538,229,323,000
1,538,229,323,000
null
UTF-8
Lean
false
false
52,444
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot Continuous functions. Parts of the formalization is based on the books: N. Bourbaki: General Topology I. M. James: Topologies and Uniformities A major difference is that this formalization is heavily based on the filter library. -/ import analysis.topology.topological_space noncomputable theory open set filter lattice local attribute [instance] classical.prop_decidable variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} section variables [topological_space α] [topological_space β] [topological_space γ] /-- A function between topological spaces is continuous if the preimage of every open set is open. -/ def continuous (f : α → β) := ∀s, is_open s → is_open (f ⁻¹' s) lemma continuous_id : continuous (id : α → α) := assume s h, h lemma continuous.comp {f : α → β} {g : β → γ} (hf : continuous f) (hg : continuous g): continuous (g ∘ f) := assume s h, hf _ (hg s h) lemma continuous.tendsto {f : α → β} (hf : continuous f) (x) : tendsto f (nhds x) (nhds (f x)) | s := show s ∈ (nhds (f x)).sets → s ∈ (map f (nhds x)).sets, by simp [nhds_sets]; exact assume t t_subset t_open fx_in_t, ⟨f ⁻¹' t, preimage_mono t_subset, hf t t_open, fx_in_t⟩ lemma continuous_iff_tendsto {f : α → β} : continuous f ↔ (∀x, tendsto f (nhds x) (nhds (f x))) := ⟨continuous.tendsto, assume hf : ∀x, tendsto f (nhds x) (nhds (f x)), assume s, assume hs : is_open s, have ∀a, f a ∈ s → s ∈ (nhds (f a)).sets, by simp [nhds_sets]; exact assume a ha, ⟨s, subset.refl s, hs, ha⟩, show is_open (f ⁻¹' s), by simp [is_open_iff_nhds]; exact assume a ha, hf a (this a ha)⟩ lemma continuous_const {b : β} : continuous (λa:α, b) := continuous_iff_tendsto.mpr $ assume a, tendsto_const_nhds lemma continuous_iff_is_closed {f : α → β} : continuous f ↔ (∀s, is_closed s → is_closed (f ⁻¹' s)) := ⟨assume hf s hs, hf (-s) hs, assume hf s, by rw [←is_closed_compl_iff, ←is_closed_compl_iff]; exact hf _⟩ lemma continuous_if {p : α → Prop} {f g : α → β} {h : ∀a, decidable (p a)} (hp : ∀a∈frontier {a | p a}, f a = g a) (hf : continuous f) (hg : continuous g) : continuous (λa, @ite (p a) (h a) β (f a) (g a)) := continuous_iff_is_closed.mpr $ assume s hs, have (λa, ite (p a) (f a) (g a)) ⁻¹' s = (closure {a | p a} ∩ f ⁻¹' s) ∪ (closure {a | ¬ p a} ∩ g ⁻¹' s), from set.ext $ assume a, classical.by_cases (assume : a ∈ frontier {a | p a}, have hac : a ∈ closure {a | p a}, from this.left, have hai : a ∈ closure {a | ¬ p a}, from have a ∈ - interior {a | p a}, from this.right, by rwa [←closure_compl] at this, by by_cases p a; simp [h, hp a this, hac, hai, iff_def] {contextual := tt}) (assume hf : a ∈ - frontier {a | p a}, classical.by_cases (assume : p a, have hc : a ∈ closure {a | p a}, from subset_closure this, have hnc : a ∉ closure {a | ¬ p a}, by show a ∉ closure (- {a | p a}); rw [closure_compl]; simpa [frontier, hc] using hf, by simp [this, hc, hnc]) (assume : ¬ p a, have hc : a ∈ closure {a | ¬ p a}, from subset_closure this, have hnc : a ∉ closure {a | p a}, begin have hc : a ∈ closure (- {a | p a}), from hc, simp [closure_compl] at hc, simpa [frontier, hc] using hf end, by simp [this, hc, hnc])), by rw [this]; exact is_closed_union (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hf s hs) (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hg s hs) lemma image_closure_subset_closure_image {f : α → β} {s : set α} (h : continuous f) : f '' closure s ⊆ closure (f '' s) := have ∀ (a : α), nhds a ⊓ principal s ≠ ⊥ → nhds (f a) ⊓ principal (f '' s) ≠ ⊥, from assume a ha, have h₁ : ¬ map f (nhds a ⊓ principal s) = ⊥, by rwa[map_eq_bot_iff], have h₂ : map f (nhds a ⊓ principal s) ≤ nhds (f a) ⊓ principal (f '' s), from le_inf (le_trans (map_mono inf_le_left) $ by rw [continuous_iff_tendsto] at h; exact h a) (le_trans (map_mono inf_le_right) $ by simp; exact subset.refl _), neq_bot_of_le_neq_bot h₁ h₂, by simp [image_subset_iff, closure_eq_nhds]; assumption lemma compact_image {s : set α} {f : α → β} (hs : compact s) (hf : continuous f) : compact (f '' s) := compact_of_finite_subcover $ assume c hco hcs, have hdo : ∀t∈c, is_open (f ⁻¹' t), from assume t' ht, hf _ $ hco _ ht, have hds : s ⊆ ⋃i∈c, f ⁻¹' i, by simpa [subset_def, -mem_image] using hcs, let ⟨d', hcd', hfd', hd'⟩ := compact_elim_finite_subcover_image hs hdo hds in ⟨d', hcd', hfd', by simpa [subset_def, -mem_image, image_subset_iff] using hd'⟩ end section constructions local notation `cont` := @continuous _ _ local notation `tspace` := topological_space open topological_space variables {f : α → β} {ι : Sort*} lemma continuous_iff_le_coinduced {t₁ : tspace α} {t₂ : tspace β} : cont t₁ t₂ f ↔ t₂ ≤ coinduced f t₁ := iff.rfl lemma continuous_iff_induced_le {t₁ : tspace α} {t₂ : tspace β} : cont t₁ t₂ f ↔ induced f t₂ ≤ t₁ := iff.trans continuous_iff_le_coinduced (gc_induced_coinduced f _ _).symm theorem continuous_generated_from {t : tspace α} {b : set (set β)} (h : ∀s∈b, is_open (f ⁻¹' s)) : cont t (generate_from b) f := continuous_iff_le_coinduced.2 $ generate_from_le h lemma continuous_induced_dom {t : tspace β} : cont (induced f t) t f := assume s h, ⟨_, h, rfl⟩ lemma continuous_induced_rng {g : γ → α} {t₂ : tspace β} {t₁ : tspace γ} (h : cont t₁ t₂ (f ∘ g)) : cont t₁ (induced f t₂) g := assume s ⟨t, ht, s_eq⟩, s_eq.symm ▸ h t ht lemma continuous_coinduced_rng {t : tspace α} : cont t (coinduced f t) f := assume s h, h lemma continuous_coinduced_dom {g : β → γ} {t₁ : tspace α} {t₂ : tspace γ} (h : cont t₁ t₂ (g ∘ f)) : cont (coinduced f t₁) t₂ g := assume s hs, h s hs lemma continuous_le_dom {t₁ t₂ : tspace α} {t₃ : tspace β} (h₁ : t₁ ≤ t₂) (h₂ : cont t₁ t₃ f) : cont t₂ t₃ f := assume s h, h₁ _ (h₂ s h) lemma continuous_le_rng {t₁ : tspace α} {t₂ t₃ : tspace β} (h₁ : t₃ ≤ t₂) (h₂ : cont t₁ t₂ f) : cont t₁ t₃ f := assume s h, h₂ s (h₁ s h) lemma continuous_inf_dom {t₁ t₂ : tspace α} {t₃ : tspace β} (h₁ : cont t₁ t₃ f) (h₂ : cont t₂ t₃ f) : cont (t₁ ⊓ t₂) t₃ f := assume s h, ⟨h₁ s h, h₂ s h⟩ lemma continuous_inf_rng_left {t₁ : tspace α} {t₃ t₂ : tspace β} : cont t₁ t₂ f → cont t₁ (t₂ ⊓ t₃) f := continuous_le_rng inf_le_left lemma continuous_inf_rng_right {t₁ : tspace α} {t₃ t₂ : tspace β} : cont t₁ t₃ f → cont t₁ (t₂ ⊓ t₃) f := continuous_le_rng inf_le_right lemma continuous_Inf_dom {t₁ : set (tspace α)} {t₂ : tspace β} (h : ∀t∈t₁, cont t t₂ f) : cont (Inf t₁) t₂ f := continuous_iff_induced_le.2 $ le_Inf $ assume t ht, continuous_iff_induced_le.1 $ h t ht lemma continuous_Inf_rng {t₁ : tspace α} {t₂ : set (tspace β)} {t : tspace β} (h₁ : t ∈ t₂) (hf : cont t₁ t f) : cont t₁ (Inf t₂) f := continuous_iff_le_coinduced.2 $ Inf_le_of_le h₁ $ continuous_iff_le_coinduced.1 hf lemma continuous_infi_dom {t₁ : ι → tspace α} {t₂ : tspace β} (h : ∀i, cont (t₁ i) t₂ f) : cont (infi t₁) t₂ f := continuous_Inf_dom $ assume t ⟨i, (t_eq : t = t₁ i)⟩, t_eq.symm ▸ h i lemma continuous_infi_rng {t₁ : tspace α} {t₂ : ι → tspace β} {i : ι} (h : cont t₁ (t₂ i) f) : cont t₁ (infi t₂) f := continuous_Inf_rng ⟨i, rfl⟩ h lemma continuous_sup_rng {t₁ : tspace α} {t₂ t₃ : tspace β} (h₁ : cont t₁ t₂ f) (h₂ : cont t₁ t₃ f) : cont t₁ (t₂ ⊔ t₃) f := continuous_iff_le_coinduced.2 $ sup_le (continuous_iff_le_coinduced.1 h₁) (continuous_iff_le_coinduced.1 h₂) lemma continuous_sup_dom_left {t₁ t₂ : tspace α} {t₃ : tspace β} : cont t₁ t₃ f → cont (t₁ ⊔ t₂) t₃ f := continuous_le_dom le_sup_left lemma continuous_sup_dom_right {t₁ t₂ : tspace α} {t₃ : tspace β} : cont t₂ t₃ f → cont (t₁ ⊔ t₂) t₃ f := continuous_le_dom le_sup_right lemma continuous_Sup_dom {t₁ : set (tspace α)} {t₂ : tspace β} {t : tspace α} (h₁ : t ∈ t₁) : cont t t₂ f → cont (Sup t₁) t₂ f := continuous_le_dom $ le_Sup h₁ lemma continuous_Sup_rng {t₁ : tspace α} {t₂ : set (tspace β)} (h : ∀t∈t₂, cont t₁ t f) : cont t₁ (Sup t₂) f := continuous_iff_le_coinduced.2 $ Sup_le $ assume b hb, continuous_iff_le_coinduced.1 $ h b hb lemma continuous_supr_dom {t₁ : ι → tspace α} {t₂ : tspace β} {i : ι} : cont (t₁ i) t₂ f → cont (supr t₁) t₂ f := continuous_le_dom $ le_supr _ _ lemma continuous_supr_rng {t₁ : tspace α} {t₂ : ι → tspace β} (h : ∀i, cont t₁ (t₂ i) f) : cont t₁ (supr t₂) f := continuous_iff_le_coinduced.2 $ supr_le $ assume i, continuous_iff_le_coinduced.1 $ h i lemma continuous_top {t : tspace β} : cont ⊤ t f := continuous_iff_induced_le.2 $ le_top lemma continuous_bot {t : tspace α} : cont t ⊥ f := continuous_iff_le_coinduced.2 $ bot_le end constructions section induced open topological_space variables [t : topological_space β] {f : α → β} theorem is_open_induced {s : set β} (h : is_open s) : (induced f t).is_open (f ⁻¹' s) := ⟨s, h, rfl⟩ lemma nhds_induced_eq_comap {a : α} : @nhds α (induced f t) a = comap f (nhds (f a)) := le_antisymm (assume s ⟨s', hs', (h_s : f ⁻¹' s' ⊆ s)⟩, let ⟨t', hsub, ht', hin⟩ := mem_nhds_sets_iff.1 hs' in (@nhds α (induced f t) a).sets_of_superset begin simp [mem_nhds_sets_iff], exact ⟨preimage f t', preimage_mono hsub, is_open_induced ht', hin⟩ end h_s) (le_infi $ assume s, le_infi $ assume ⟨as, s', is_open_s', s_eq⟩, begin simp [comap, mem_nhds_sets_iff, s_eq], exact ⟨s', ⟨s', subset.refl _, is_open_s', by rwa [s_eq] at as⟩, subset.refl _⟩ end) lemma map_nhds_induced_eq {a : α} (h : image f univ ∈ (nhds (f a)).sets) : map f (@nhds α (induced f t) a) = nhds (f a) := le_antisymm (@continuous.tendsto α β (induced f t) _ _ continuous_induced_dom a) (assume s, assume hs : f ⁻¹' s ∈ (@nhds α (induced f t) a).sets, let ⟨t', t_subset, is_open_t, a_in_t⟩ := mem_nhds_sets_iff.mp h in let ⟨s', s'_subset, ⟨s'', is_open_s'', s'_eq⟩, a_in_s'⟩ := (@mem_nhds_sets_iff _ (induced f t) _ _).mp hs in by subst s'_eq; exact (mem_nhds_sets_iff.mpr $ ⟨t' ∩ s'', assume x ⟨h₁, h₂⟩, match x, h₂, t_subset h₁ with | x, h₂, ⟨y, _, y_eq⟩ := begin subst y_eq, exact s'_subset h₂ end end, is_open_inter is_open_t is_open_s'', ⟨a_in_t, a_in_s'⟩⟩)) lemma closure_induced [t : topological_space β] {f : α → β} {a : α} {s : set α} (hf : ∀x y, f x = f y → x = y) : a ∈ @closure α (topological_space.induced f t) s ↔ f a ∈ closure (f '' s) := have comap f (nhds (f a) ⊓ principal (f '' s)) ≠ ⊥ ↔ nhds (f a) ⊓ principal (f '' s) ≠ ⊥, from ⟨assume h₁ h₂, h₁ $ h₂.symm ▸ comap_bot, assume h, forall_sets_neq_empty_iff_neq_bot.mp $ assume s₁ ⟨s₂, hs₂, (hs : f ⁻¹' s₂ ⊆ s₁)⟩, have f '' s ∈ (nhds (f a) ⊓ principal (f '' s)).sets, from mem_inf_sets_of_right $ by simp [subset.refl], have s₂ ∩ f '' s ∈ (nhds (f a) ⊓ principal (f '' s)).sets, from inter_mem_sets hs₂ this, let ⟨b, hb₁, ⟨a, ha, ha₂⟩⟩ := inhabited_of_mem_sets h this in ne_empty_of_mem $ hs $ by rwa [←ha₂] at hb₁⟩, calc a ∈ @closure α (topological_space.induced f t) s ↔ (@nhds α (topological_space.induced f t) a) ⊓ principal s ≠ ⊥ : by rw [closure_eq_nhds]; refl ... ↔ comap f (nhds (f a)) ⊓ principal (f ⁻¹' (f '' s)) ≠ ⊥ : by rw [nhds_induced_eq_comap, preimage_image_eq _ hf] ... ↔ comap f (nhds (f a) ⊓ principal (f '' s)) ≠ ⊥ : by rw [comap_inf, ←comap_principal] ... ↔ _ : by rwa [closure_eq_nhds] end induced section embedding /-- A function between topological spaces is an embedding if it is injective, and for all `s : set α`, `s` is open iff it is the preimage of an open set. -/ def embedding [tα : topological_space α] [tβ : topological_space β] (f : α → β) : Prop := function.injective f ∧ tα = tβ.induced f variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] lemma embedding_id : embedding (@id α) := ⟨assume a₁ a₂ h, h, induced_id.symm⟩ lemma embedding_compose {f : α → β} {g : β → γ} (hf : embedding f) (hg : embedding g) : embedding (g ∘ f) := ⟨assume a₁ a₂ h, hf.left $ hg.left h, by rw [hf.right, hg.right, induced_compose]⟩ lemma embedding_prod_mk {f : α → β} {g : γ → δ} (hf : embedding f) (hg : embedding g) : embedding (λx:α×γ, (f x.1, g x.2)) := ⟨assume ⟨x₁, x₂⟩ ⟨y₁, y₂⟩, by simp; exact assume h₁ h₂, ⟨hf.left h₁, hg.left h₂⟩, by rw [prod.topological_space, prod.topological_space, hf.right, hg.right, induced_compose, induced_compose, induced_sup, induced_compose, induced_compose]⟩ lemma embedding_of_embedding_compose {f : α → β} {g : β → γ} (hf : continuous f) (hg : continuous g) (hgf : embedding (g ∘ f)) : embedding f := ⟨assume a₁ a₂ h, hgf.left $ by simp [h, (∘)], le_antisymm (by rw [hgf.right, ← continuous_iff_induced_le]; apply continuous_induced_dom.comp hg) (by rwa ← continuous_iff_induced_le)⟩ lemma embedding_open {f : α → β} {s : set α} (hf : embedding f) (h : is_open (range f)) (hs : is_open s) : is_open (f '' s) := let ⟨t, ht, h_eq⟩ := by rw [hf.right] at hs; exact hs in have is_open (t ∩ range f), from is_open_inter ht h, h_eq.symm ▸ by rwa [image_preimage_eq_inter_range] lemma embedding_is_closed {f : α → β} {s : set α} (hf : embedding f) (h : is_closed (range f)) (hs : is_closed s) : is_closed (f '' s) := let ⟨t, ht, h_eq⟩ := by rw [hf.right, is_closed_induced_iff] at hs; exact hs in have is_closed (t ∩ range f), from is_closed_inter ht h, h_eq.symm ▸ by rwa [image_preimage_eq_inter_range] lemma embedding.map_nhds_eq [topological_space α] [topological_space β] {f : α → β} (hf : embedding f) (a : α) (h : f '' univ ∈ (nhds (f a)).sets) : (nhds a).map f = nhds (f a) := by rw [hf.2]; exact map_nhds_induced_eq h lemma embedding.tendsto_nhds_iff {ι : Type*} {f : ι → β} {g : β → γ} {a : filter ι} {b : β} (hg : embedding g) : tendsto f a (nhds b) ↔ tendsto (g ∘ f) a (nhds (g b)) := by rw [tendsto, tendsto, hg.right, nhds_induced_eq_comap, ← map_le_iff_le_comap, filter.map_map] lemma embedding.continuous_iff {f : α → β} {g : β → γ} (hg : embedding g) : continuous f ↔ continuous (g ∘ f) := by simp [continuous_iff_tendsto, embedding.tendsto_nhds_iff hg] lemma embedding.continuous {f : α → β} (hf : embedding f) : continuous f := hf.continuous_iff.mp continuous_id lemma compact_iff_compact_image_of_embedding {s : set α} {f : α → β} (hf : embedding f) : compact s ↔ compact (f '' s) := iff.intro (assume h, compact_image h hf.continuous) $ assume h, begin rw compact_iff_ultrafilter_le_nhds at ⊢ h, intros u hu us', let u' : filter β := map f u, have : u' ≤ principal (f '' s), begin rw [map_le_iff_le_comap, comap_principal], convert us', exact preimage_image_eq _ hf.1 end, rcases h u' (ultrafilter_map hu) this with ⟨_, ⟨a, ha, ⟨⟩⟩, _⟩, refine ⟨a, ha, _⟩, rwa [hf.2, nhds_induced_eq_comap, ←map_le_iff_le_comap] end end embedding section quotient_map /-- A function between topological spaces is a quotient map if it is surjective, and for all `s : set β`, `s` is open iff its preimage is an open set. -/ def quotient_map [tα : topological_space α] [tβ : topological_space β] (f : α → β) : Prop := function.surjective f ∧ tβ = tα.coinduced f variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] lemma quotient_map_id : quotient_map (@id α) := ⟨assume a, ⟨a, rfl⟩, coinduced_id.symm⟩ lemma quotient_map_compose {f : α → β} {g : β → γ} (hf : quotient_map f) (hg : quotient_map g) : quotient_map (g ∘ f) := ⟨function.surjective_comp hg.left hf.left, by rw [hg.right, hf.right, coinduced_compose]⟩ lemma quotient_map_of_quotient_map_compose {f : α → β} {g : β → γ} (hf : continuous f) (hg : continuous g) (hgf : quotient_map (g ∘ f)) : quotient_map g := ⟨assume b, let ⟨a, h⟩ := hgf.left b in ⟨f a, h⟩, le_antisymm (by rwa ← continuous_iff_le_coinduced) (by rw [hgf.right, ← continuous_iff_le_coinduced]; apply hf.comp continuous_coinduced_rng)⟩ lemma quotient_map.continuous_iff {f : α → β} {g : β → γ} (hf : quotient_map f) : continuous g ↔ continuous (g ∘ f) := by rw [continuous_iff_le_coinduced, continuous_iff_le_coinduced, hf.right, coinduced_compose] lemma quotient_map.continuous {f : α → β} (hf : quotient_map f) : continuous f := hf.continuous_iff.mp continuous_id end quotient_map section sierpinski variables [topological_space α] @[simp] lemma is_open_singleton_true : is_open ({true} : set Prop) := topological_space.generate_open.basic _ (by simp) lemma continuous_Prop {p : α → Prop} : continuous p ↔ is_open {x | p x} := ⟨assume h : continuous p, have is_open (p ⁻¹' {true}), from h _ is_open_singleton_true, by simp [preimage, eq_true] at this; assumption, assume h : is_open {x | p x}, continuous_generated_from $ assume s (hs : s ∈ {{true}}), by simp at hs; simp [hs, preimage, eq_true, h]⟩ end sierpinski section prod open topological_space variables [topological_space α] [topological_space β] [topological_space γ] lemma continuous_fst : continuous (@prod.fst α β) := continuous_sup_dom_left continuous_induced_dom lemma continuous_snd : continuous (@prod.snd α β) := continuous_sup_dom_right continuous_induced_dom lemma continuous.prod_mk {f : γ → α} {g : γ → β} (hf : continuous f) (hg : continuous g) : continuous (λx, prod.mk (f x) (g x)) := continuous_sup_rng (continuous_induced_rng hf) (continuous_induced_rng hg) lemma continuous_swap : continuous (prod.swap : α × β → β × α) := continuous.prod_mk continuous_snd continuous_fst lemma is_open_prod {s : set α} {t : set β} (hs : is_open s) (ht : is_open t) : is_open (set.prod s t) := is_open_inter (continuous_fst s hs) (continuous_snd t ht) lemma nhds_prod_eq {a : α} {b : β} : nhds (a, b) = filter.prod (nhds a) (nhds b) := by rw [filter.prod, prod.topological_space, nhds_sup, nhds_induced_eq_comap, nhds_induced_eq_comap] lemma prod_mem_nhds_sets {s : set α} {t : set β} {a : α} {b : β} (ha : s ∈ (nhds a).sets) (hb : t ∈ (nhds b).sets) : set.prod s t ∈ (nhds (a, b)).sets := by rw [nhds_prod_eq]; exact prod_mem_prod ha hb lemma nhds_swap (a : α) (b : β) : nhds (a, b) = (nhds (b, a)).map prod.swap := by rw [nhds_prod_eq, filter.prod_comm, nhds_prod_eq]; refl lemma tendsto_prod_mk_nhds {γ} {a : α} {b : β} {f : filter γ} {ma : γ → α} {mb : γ → β} (ha : tendsto ma f (nhds a)) (hb : tendsto mb f (nhds b)) : tendsto (λc, (ma c, mb c)) f (nhds (a, b)) := by rw [nhds_prod_eq]; exact filter.tendsto.prod_mk ha hb lemma prod_generate_from_generate_from_eq {s : set (set α)} {t : set (set β)} (hs : ⋃₀ s = univ) (ht : ⋃₀ t = univ) : @prod.topological_space α β (generate_from s) (generate_from t) = generate_from {g | ∃u∈s, ∃v∈t, g = set.prod u v} := let G := generate_from {g | ∃u∈s, ∃v∈t, g = set.prod u v} in le_antisymm (sup_le (induced_le_iff_le_coinduced.mpr $ generate_from_le $ assume u hu, have (⋃v∈t, set.prod u v) = prod.fst ⁻¹' u, from calc (⋃v∈t, set.prod u v) = set.prod u univ : set.ext $ assume ⟨a, b⟩, by rw ← ht; simp [and.left_comm] {contextual:=tt} ... = prod.fst ⁻¹' u : by simp [set.prod, preimage], show G.is_open (prod.fst ⁻¹' u), from this ▸ @is_open_Union _ _ G _ $ assume v, @is_open_Union _ _ G _ $ assume hv, generate_open.basic _ ⟨_, hu, _, hv, rfl⟩) (induced_le_iff_le_coinduced.mpr $ generate_from_le $ assume v hv, have (⋃u∈s, set.prod u v) = prod.snd ⁻¹' v, from calc (⋃u∈s, set.prod u v) = set.prod univ v: set.ext $ assume ⟨a, b⟩, by rw [←hs]; by_cases b ∈ v; simp [h] {contextual:=tt} ... = prod.snd ⁻¹' v : by simp [set.prod, preimage], show G.is_open (prod.snd ⁻¹' v), from this ▸ @is_open_Union _ _ G _ $ assume u, @is_open_Union _ _ G _ $ assume hu, generate_open.basic _ ⟨_, hu, _, hv, rfl⟩)) (generate_from_le $ assume g ⟨u, hu, v, hv, g_eq⟩, g_eq.symm ▸ @is_open_prod _ _ (generate_from s) (generate_from t) _ _ (generate_open.basic _ hu) (generate_open.basic _ hv)) lemma prod_eq_generate_from [tα : topological_space α] [tβ : topological_space β] : prod.topological_space = generate_from {g | ∃(s:set α) (t:set β), is_open s ∧ is_open t ∧ g = set.prod s t} := le_antisymm (sup_le (assume s ⟨t, ht, s_eq⟩, have set.prod t univ = s, by simp [s_eq, preimage, set.prod], this ▸ (generate_open.basic _ ⟨t, univ, ht, is_open_univ, rfl⟩)) (assume s ⟨t, ht, s_eq⟩, have set.prod univ t = s, by simp [s_eq, preimage, set.prod], this ▸ (generate_open.basic _ ⟨univ, t, is_open_univ, ht, rfl⟩))) (generate_from_le $ assume g ⟨s, t, hs, ht, g_eq⟩, g_eq.symm ▸ is_open_prod hs ht) lemma is_open_prod_iff {s : set (α×β)} : is_open s ↔ (∀a b, (a, b) ∈ s → ∃u v, is_open u ∧ is_open v ∧ a ∈ u ∧ b ∈ v ∧ set.prod u v ⊆ s) := begin rw [is_open_iff_nhds], simp [nhds_prod_eq, mem_prod_iff], simp [mem_nhds_sets_iff], exact forall_congr (assume a, ball_congr $ assume b h, ⟨assume ⟨u', ⟨u, us, uo, au⟩, v', ⟨v, vs, vo, bv⟩, h⟩, ⟨u, uo, v, vo, au, bv, subset.trans (set.prod_mono us vs) h⟩, assume ⟨u, uo, v, vo, au, bv, h⟩, ⟨u, ⟨u, subset.refl u, uo, au⟩, v, ⟨v, subset.refl v, vo, bv⟩, h⟩⟩) end lemma closure_prod_eq {s : set α} {t : set β} : closure (set.prod s t) = set.prod (closure s) (closure t) := set.ext $ assume ⟨a, b⟩, have filter.prod (nhds a) (nhds b) ⊓ principal (set.prod s t) = filter.prod (nhds a ⊓ principal s) (nhds b ⊓ principal t), by rw [←prod_inf_prod, prod_principal_principal], by simp [closure_eq_nhds, nhds_prod_eq, this]; exact prod_neq_bot lemma is_closed_prod [topological_space α] [topological_space β] {s₁ : set α} {s₂ : set β} (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (set.prod s₁ s₂) := closure_eq_iff_is_closed.mp $ by simp [h₁, h₂, closure_prod_eq, closure_eq_of_is_closed] section tube_lemma def nhds_contain_boxes (s : set α) (t : set β) : Prop := ∀ (n : set (α × β)) (hn : is_open n) (hp : set.prod s t ⊆ n), ∃ (u : set α) (v : set β), is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ set.prod u v ⊆ n lemma nhds_contain_boxes.symm {s : set α} {t : set β} : nhds_contain_boxes s t → nhds_contain_boxes t s := assume H n hn hp, let ⟨u, v, uo, vo, su, tv, p⟩ := H (prod.swap ⁻¹' n) (continuous_swap n hn) (by rwa [←image_subset_iff, prod.swap, image_swap_prod]) in ⟨v, u, vo, uo, tv, su, by rwa [←image_subset_iff, prod.swap, image_swap_prod] at p⟩ lemma nhds_contain_boxes.comm {s : set α} {t : set β} : nhds_contain_boxes s t ↔ nhds_contain_boxes t s := iff.intro nhds_contain_boxes.symm nhds_contain_boxes.symm lemma nhds_contain_boxes_of_singleton {x : α} {y : β} : nhds_contain_boxes ({x} : set α) ({y} : set β) := assume n hn hp, let ⟨u, v, uo, vo, xu, yv, hp'⟩ := is_open_prod_iff.mp hn x y (hp $ by simp) in ⟨u, v, uo, vo, by simpa, by simpa, hp'⟩ lemma nhds_contain_boxes_of_compact {s : set α} (hs : compact s) (t : set β) (H : ∀ x ∈ s, nhds_contain_boxes ({x} : set α) t) : nhds_contain_boxes s t := assume n hn hp, have ∀x : subtype s, ∃uv : set α × set β, is_open uv.1 ∧ is_open uv.2 ∧ {↑x} ⊆ uv.1 ∧ t ⊆ uv.2 ∧ set.prod uv.1 uv.2 ⊆ n, from assume ⟨x, hx⟩, have set.prod {x} t ⊆ n, from subset.trans (prod_mono (by simpa) (subset.refl _)) hp, let ⟨ux,vx,H1⟩ := H x hx n hn this in ⟨⟨ux,vx⟩,H1⟩, let ⟨uvs, h⟩ := classical.axiom_of_choice this in have us_cover : s ⊆ ⋃i, (uvs i).1, from assume x hx, set.subset_Union _ ⟨x,hx⟩ (by simpa using (h ⟨x,hx⟩).2.2.1), let ⟨s0, _, s0_fin, s0_cover⟩ := compact_elim_finite_subcover_image hs (λi _, (h i).1) $ by rw bUnion_univ; exact us_cover in let u := ⋃(i ∈ s0), (uvs i).1 in let v := ⋂(i ∈ s0), (uvs i).2 in have is_open u, from is_open_bUnion (λi _, (h i).1), have is_open v, from is_open_bInter s0_fin (λi _, (h i).2.1), have t ⊆ v, from subset_bInter (λi _, (h i).2.2.2.1), have set.prod u v ⊆ n, from assume ⟨x',y'⟩ ⟨hx',hy'⟩, have ∃i ∈ s0, x' ∈ (uvs i).1, by simpa using hx', let ⟨i,is0,hi⟩ := this in (h i).2.2.2.2 ⟨hi, (bInter_subset_of_mem is0 : v ⊆ (uvs i).2) hy'⟩, ⟨u, v, ‹is_open u›, ‹is_open v›, s0_cover, ‹t ⊆ v›, ‹set.prod u v ⊆ n›⟩ lemma generalized_tube_lemma {s : set α} (hs : compact s) {t : set β} (ht : compact t) {n : set (α × β)} (hn : is_open n) (hp : set.prod s t ⊆ n) : ∃ (u : set α) (v : set β), is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ set.prod u v ⊆ n := have _, from nhds_contain_boxes_of_compact hs t $ assume x _, nhds_contain_boxes.symm $ nhds_contain_boxes_of_compact ht {x} $ assume y _, nhds_contain_boxes_of_singleton, this n hn hp end tube_lemma lemma is_closed_diagonal [topological_space α] [t2_space α] : is_closed {p:α×α | p.1 = p.2} := is_closed_iff_nhds.mpr $ assume ⟨a₁, a₂⟩ h, eq_of_nhds_neq_bot $ assume : nhds a₁ ⊓ nhds a₂ = ⊥, h $ let ⟨t₁, ht₁, t₂, ht₂, (h' : t₁ ∩ t₂ ⊆ ∅)⟩ := by rw [←empty_in_sets_eq_bot, mem_inf_sets] at this; exact this in begin rw [nhds_prod_eq, ←empty_in_sets_eq_bot], apply filter.sets_of_superset, apply inter_mem_inf_sets (prod_mem_prod ht₁ ht₂) (mem_principal_sets.mpr (subset.refl _)), exact assume ⟨x₁, x₂⟩ ⟨⟨hx₁, hx₂⟩, (heq : x₁ = x₂)⟩, show false, from @h' x₁ ⟨hx₁, heq.symm ▸ hx₂⟩ end lemma is_closed_eq [topological_space α] [t2_space α] [topological_space β] {f g : β → α} (hf : continuous f) (hg : continuous g) : is_closed {x:β | f x = g x} := continuous_iff_is_closed.mp (hf.prod_mk hg) _ is_closed_diagonal lemma diagonal_eq_range_diagonal_map : {p:α×α | p.1 = p.2} = range (λx, (x,x)) := ext $ assume p, iff.intro (assume h, ⟨p.1, prod.ext_iff.2 ⟨rfl, h⟩⟩) (assume ⟨x, hx⟩, show p.1 = p.2, by rw ←hx) lemma prod_subset_compl_diagonal_iff_disjoint {s t : set α} : set.prod s t ⊆ - {p:α×α | p.1 = p.2} ↔ s ∩ t = ∅ := by rw [eq_empty_iff_forall_not_mem, subset_compl_comm, diagonal_eq_range_diagonal_map, range_subset_iff]; simp lemma compact_compact_separated [t2_space α] {s t : set α} (hs : compact s) (ht : compact t) (hst : s ∩ t = ∅) : ∃u v : set α, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ u ∩ v = ∅ := by simp only [prod_subset_compl_diagonal_iff_disjoint.symm] at ⊢ hst; exact generalized_tube_lemma hs ht is_closed_diagonal hst lemma closed_of_compact [t2_space α] (s : set α) (hs : compact s) : is_closed s := is_open_compl_iff.mpr $ is_open_iff_forall_mem_open.mpr $ assume x hx, let ⟨u, v, uo, vo, su, xv, uv⟩ := compact_compact_separated hs (compact_singleton : compact {x}) (by rwa [inter_comm, ←subset_compl_iff_disjoint, singleton_subset_iff]) in have v ⊆ -s, from subset_compl_comm.mp (subset.trans su (subset_compl_iff_disjoint.mpr uv)), ⟨v, this, vo, by simpa using xv⟩ /- TODO: more fine grained instances for first_countable_topology, separable_space, t2_space, ... -/ instance [second_countable_topology α] [second_countable_topology β] : second_countable_topology (α × β) := ⟨let ⟨a, ha₁, ha₂, ha₃, ha₄, ha₅⟩ := is_open_generated_countable_inter α in let ⟨b, hb₁, hb₂, hb₃, hb₄, hb₅⟩ := is_open_generated_countable_inter β in ⟨{g | ∃u∈a, ∃v∈b, g = set.prod u v}, have {g | ∃u∈a, ∃v∈b, g = set.prod u v} = (⋃u∈a, ⋃v∈b, {set.prod u v}), by apply set.ext; simp, by rw [this]; exact (countable_bUnion ha₁ $ assume u hu, countable_bUnion hb₁ $ by simp), by rw [ha₅, hb₅, prod_generate_from_generate_from_eq ha₄ hb₄]⟩⟩ end prod section sum variables [topological_space α] [topological_space β] [topological_space γ] lemma continuous_inl : continuous (@sum.inl α β) := continuous_inf_rng_left continuous_coinduced_rng lemma continuous_inr : continuous (@sum.inr α β) := continuous_inf_rng_right continuous_coinduced_rng lemma continuous_sum_rec {f : α → γ} {g : β → γ} (hf : continuous f) (hg : continuous g) : @continuous (α ⊕ β) γ _ _ (@sum.rec α β (λ_, γ) f g) := continuous_inf_dom hf hg end sum section subtype variables [topological_space α] [topological_space β] [topological_space γ] {p : α → Prop} lemma embedding_graph {f : α → β} (hf : continuous f) : embedding (λx, (x, f x)) := embedding_of_embedding_compose (continuous_id.prod_mk hf) continuous_fst embedding_id lemma embedding_subtype_val : embedding (@subtype.val α p) := ⟨assume a₁ a₂, subtype.eq, rfl⟩ lemma continuous_subtype_val : continuous (@subtype.val α p) := continuous_induced_dom lemma continuous_subtype_mk {f : β → α} (hp : ∀x, p (f x)) (h : continuous f) : continuous (λx, (⟨f x, hp x⟩ : subtype p)) := continuous_induced_rng h lemma embedding_inl : embedding (@sum.inl α β) := ⟨λ _ _, sum.inl.inj_iff.mp, begin unfold sum.topological_space, apply le_antisymm, { intros u hu, existsi (sum.inl '' u), change (is_open (sum.inl ⁻¹' (@sum.inl α β '' u)) ∧ is_open (sum.inr ⁻¹' (@sum.inl α β '' u))) ∧ u = sum.inl ⁻¹' (sum.inl '' u), have : sum.inl ⁻¹' (@sum.inl α β '' u) = u := preimage_image_eq u (λ _ _, sum.inl.inj_iff.mp), rw this, have : sum.inr ⁻¹' (@sum.inl α β '' u) = ∅ := eq_empty_iff_forall_not_mem.mpr (assume a ⟨b, _, h⟩, sum.inl_ne_inr h), rw this, exact ⟨⟨hu, is_open_empty⟩, rfl⟩ }, { rw induced_le_iff_le_coinduced, exact lattice.inf_le_left } end⟩ lemma embedding_inr : embedding (@sum.inr α β) := ⟨λ _ _, sum.inr.inj_iff.mp, begin unfold sum.topological_space, apply le_antisymm, { intros u hu, existsi (sum.inr '' u), change (is_open (sum.inl ⁻¹' (@sum.inr α β '' u)) ∧ is_open (sum.inr ⁻¹' (@sum.inr α β '' u))) ∧ u = sum.inr ⁻¹' (sum.inr '' u), have : sum.inl ⁻¹' (@sum.inr α β '' u) = ∅ := eq_empty_iff_forall_not_mem.mpr (assume b ⟨a, _, h⟩, sum.inr_ne_inl h), rw this, have : sum.inr ⁻¹' (@sum.inr α β '' u) = u := preimage_image_eq u (λ _ _, sum.inr.inj_iff.mp), rw this, exact ⟨⟨is_open_empty, hu⟩, rfl⟩ }, { rw induced_le_iff_le_coinduced, exact lattice.inf_le_right } end⟩ lemma map_nhds_subtype_val_eq {a : α} (ha : p a) (h : {a | p a} ∈ (nhds a).sets) : map (@subtype.val α p) (nhds ⟨a, ha⟩) = nhds a := map_nhds_induced_eq (by simp [subtype_val_image, h]) lemma nhds_subtype_eq_comap {a : α} {h : p a} : nhds (⟨a, h⟩ : subtype p) = comap subtype.val (nhds a) := nhds_induced_eq_comap lemma continuous_subtype_nhds_cover {ι : Sort*} {f : α → β} {c : ι → α → Prop} (c_cover : ∀x:α, ∃i, {x | c i x} ∈ (nhds x).sets) (f_cont : ∀i, continuous (λ(x : subtype (c i)), f x.val)) : continuous f := continuous_iff_tendsto.mpr $ assume x, let ⟨i, (c_sets : {x | c i x} ∈ (nhds x).sets)⟩ := c_cover x in let x' : subtype (c i) := ⟨x, mem_of_nhds c_sets⟩ in calc map f (nhds x) = map f (map subtype.val (nhds x')) : congr_arg (map f) (map_nhds_subtype_val_eq _ $ c_sets).symm ... = map (λx:subtype (c i), f x.val) (nhds x') : rfl ... ≤ nhds (f x) : continuous_iff_tendsto.mp (f_cont i) x' lemma continuous_subtype_is_closed_cover {ι : Sort*} {f : α → β} (c : ι → α → Prop) (h_lf : locally_finite (λi, {x | c i x})) (h_is_closed : ∀i, is_closed {x | c i x}) (h_cover : ∀x, ∃i, c i x) (f_cont : ∀i, continuous (λ(x : subtype (c i)), f x.val)) : continuous f := continuous_iff_is_closed.mpr $ assume s hs, have ∀i, is_closed (@subtype.val α {x | c i x} '' (f ∘ subtype.val ⁻¹' s)), from assume i, embedding_is_closed embedding_subtype_val (by simp [subtype_val_range]; exact h_is_closed i) (continuous_iff_is_closed.mp (f_cont i) _ hs), have is_closed (⋃i, @subtype.val α {x | c i x} '' (f ∘ subtype.val ⁻¹' s)), from is_closed_Union_of_locally_finite (locally_finite_subset h_lf $ assume i x ⟨⟨x', hx'⟩, _, heq⟩, heq ▸ hx') this, have f ⁻¹' s = (⋃i, @subtype.val α {x | c i x} '' (f ∘ subtype.val ⁻¹' s)), begin apply set.ext, have : ∀ (x : α), f x ∈ s ↔ ∃ (i : ι), c i x ∧ f x ∈ s := λ x, ⟨λ hx, let ⟨i, hi⟩ := h_cover x in ⟨i, hi, hx⟩, λ ⟨i, hi, hx⟩, hx⟩, simp [and.comm, and.left_comm], simpa [(∘)], end, by rwa [this] lemma closure_subtype {x : {a // p a}} {s : set {a // p a}}: x ∈ closure s ↔ x.val ∈ closure (subtype.val '' s) := closure_induced $ assume x y, subtype.eq lemma compact_iff_compact_in_subtype {s : set {a // p a}} : compact s ↔ compact (subtype.val '' s) := compact_iff_compact_image_of_embedding embedding_subtype_val lemma compact_iff_compact_univ {s : set α} : compact s ↔ compact (univ : set (subtype s)) := by rw [compact_iff_compact_in_subtype, image_univ, subtype_val_range]; refl end subtype section quotient variables [topological_space α] [topological_space β] [topological_space γ] variables {r : α → α → Prop} {s : setoid α} lemma quotient_map_quot_mk : quotient_map (@quot.mk α r) := ⟨quot.exists_rep, rfl⟩ lemma continuous_quot_mk : continuous (@quot.mk α r) := continuous_coinduced_rng lemma continuous_quot_lift {f : α → β} (hr : ∀ a b, r a b → f a = f b) (h : continuous f) : continuous (quot.lift f hr : quot r → β) := continuous_coinduced_dom h lemma quotient_map_quotient_mk : quotient_map (@quotient.mk α s) := quotient_map_quot_mk lemma continuous_quotient_mk : continuous (@quotient.mk α s) := continuous_coinduced_rng lemma continuous_quotient_lift {f : α → β} (hs : ∀ a b, a ≈ b → f a = f b) (h : continuous f) : continuous (quotient.lift f hs : quotient s → β) := continuous_coinduced_dom h end quotient section pi variables {ι : Type*} {π : ι → Type*} open topological_space lemma continuous_pi [topological_space α] [∀i, topological_space (π i)] {f : α → Πi:ι, π i} (h : ∀i, continuous (λa, f a i)) : continuous f := continuous_supr_rng $ assume i, continuous_induced_rng $ h i lemma continuous_apply [∀i, topological_space (π i)] (i : ι) : continuous (λp:Πi, π i, p i) := continuous_supr_dom continuous_induced_dom lemma nhds_pi [t : ∀i, topological_space (π i)] {a : Πi, π i} : nhds a = (⨅i, comap (λx, x i) (nhds (a i))) := calc nhds a = (⨅i, @nhds _ (@topological_space.induced _ _ (λx:Πi, π i, x i) (t i)) a) : nhds_supr ... = (⨅i, comap (λx, x i) (nhds (a i))) : by simp [nhds_induced_eq_comap] /-- Tychonoff's theorem -/ lemma compact_pi_infinite [∀i, topological_space (π i)] {s : Πi:ι, set (π i)} : (∀i, compact (s i)) → compact {x : Πi:ι, π i | ∀i, x i ∈ s i} := begin simp [compact_iff_ultrafilter_le_nhds, nhds_pi], exact assume h f hf hfs, let p : Πi:ι, filter (π i) := λi, map (λx:Πi:ι, π i, x i) f in have ∀i:ι, ∃a, a∈s i ∧ p i ≤ nhds a, from assume i, h i (p i) (ultrafilter_map hf) $ show (λx:Πi:ι, π i, x i) ⁻¹' s i ∈ f.sets, from mem_sets_of_superset hfs $ assume x (hx : ∀i, x i ∈ s i), hx i, let ⟨a, ha⟩ := classical.axiom_of_choice this in ⟨a, assume i, (ha i).left, assume i, map_le_iff_le_comap.mp $ (ha i).right⟩ end lemma is_open_set_pi [∀a, topological_space (π a)] {i : set ι} {s : Πa, set (π a)} (hi : finite i) (hs : ∀a∈i, is_open (s a)) : is_open (pi i s) := by rw [pi_def]; exact (is_open_bInter hi $ assume a ha, continuous_apply a _ $ hs a ha) lemma pi_eq_generate_from [∀a, topological_space (π a)] : Pi.topological_space = generate_from {g | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, is_open (s a)) ∧ g = pi ↑i s} := le_antisymm (supr_le $ assume a s ⟨t, ht, s_eq⟩, generate_open.basic _ $ ⟨function.update (λa, univ) a t, {a}, by simpa using ht, by ext f; simp [s_eq, pi]⟩) (generate_from_le $ assume g ⟨s, i, hi, eq⟩, eq.symm ▸ is_open_set_pi (finset.finite_to_set _) hi) lemma pi_generate_from_eq {g : Πa, set (set (π a))} : @Pi.topological_space ι π (λa, generate_from (g a)) = generate_from {t | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, s a ∈ g a) ∧ t = pi ↑i s} := let G := {t | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, s a ∈ g a) ∧ t = pi ↑i s} in begin rw [pi_eq_generate_from], refine le_antisymm (generate_from_le _) (generate_from_mono _), { rintros s ⟨t, i, hi, rfl⟩, rw [pi_def], apply is_open_bInter (finset.finite_to_set _), assume a ha, show ((generate_from G).coinduced (λf:Πa, π a, f a)).is_open (t a), refine generate_from_le _ _ (hi a ha), exact assume s hs, generate_open.basic _ ⟨function.update (λa, univ) a s, {a}, by simp [hs]⟩ }, exact assume s ⟨t, i, ht, eq⟩, ⟨t, i, assume a ha, generate_open.basic _ (ht a ha), eq⟩ end lemma pi_generate_from_eq_fintype {g : Πa, set (set (π a))} [fintype ι] (hg : ∀a, ⋃₀ g a = univ) : @Pi.topological_space ι π (λa, generate_from (g a)) = generate_from {t | ∃(s:Πa, set (π a)), (∀a, s a ∈ g a) ∧ t = pi univ s} := let G := {t | ∃(s:Πa, set (π a)), (∀a, s a ∈ g a) ∧ t = pi univ s} in begin rw [pi_generate_from_eq], refine le_antisymm (generate_from_le _) (generate_from_mono _), { rintros s ⟨t, i, ht, rfl⟩, apply is_open_iff_forall_mem_open.2 _, assume f hf, have : ∀a, ∃s, s ∈ g a ∧ f a ∈ s, { assume a, have : f a ∈ ⋃₀ g a, { rw [hg], apply mem_univ }, simpa }, rcases classical.axiom_of_choice this with ⟨c, hc⟩, refine ⟨pi univ (λa, if a ∈ i then t a else (c : Πa, set (π a)) a), _, _, _⟩, { simp [pi_if] }, { refine generate_open.basic _ ⟨_, assume a, _, rfl⟩, by_cases a ∈ i; simp [*, pi] at * }, { have : f ∈ pi {a | a ∉ i} c, { simp [*, pi] at * }, simpa [pi_if, hf] } }, exact assume s ⟨t, ht, eq⟩, ⟨t, finset.univ, by simp [ht, eq]⟩ end instance second_countable_topology_fintype [fintype ι] [t : ∀a, topological_space (π a)] [sc : ∀a, second_countable_topology (π a)] : second_countable_topology (∀a, π a) := have ∀i, ∃b : set (set (π i)), countable b ∧ ∅ ∉ b ∧ is_topological_basis b, from assume a, @is_open_generated_countable_inter (π a) _ (sc a), let ⟨g, hg⟩ := classical.axiom_of_choice this in have t = (λa, generate_from (g a)), from funext $ assume a, (hg a).2.2.2.2, begin constructor, refine ⟨pi univ '' pi univ g, countable_image _ _, _⟩, { suffices : countable {f : Πa, set (π a) | ∀a, f a ∈ g a}, { simpa [pi] }, exact countable_pi (assume i, (hg i).1), }, rw [this, pi_generate_from_eq_fintype], { congr' 1, ext f, simp [pi, eq_comm] }, exact assume a, (hg a).2.2.2.1 end end pi -- TODO: use embeddings from above! structure dense_embedding [topological_space α] [topological_space β] (e : α → β) : Prop := (dense : ∀x, x ∈ closure (range e)) (inj : function.injective e) (induced : ∀a, comap e (nhds (e a)) = nhds a) theorem dense_embedding.mk' [topological_space α] [topological_space β] (e : α → β) (c : continuous e) (dense : ∀x, x ∈ closure (range e)) (inj : function.injective e) (H : ∀ (a:α) s ∈ (nhds a).sets, ∃t ∈ (nhds (e a)).sets, ∀ b, e b ∈ t → b ∈ s) : dense_embedding e := ⟨dense, inj, λ a, le_antisymm (by simpa [le_def] using H a) (tendsto_iff_comap.1 $ c.tendsto _)⟩ namespace dense_embedding variables [topological_space α] [topological_space β] variables {e : α → β} (de : dense_embedding e) protected lemma embedding (de : dense_embedding e) : embedding e := ⟨de.inj, eq_of_nhds_eq_nhds begin intro a, rw [← de.induced a, nhds_induced_eq_comap] end⟩ protected lemma tendsto (de : dense_embedding e) {a : α} : tendsto e (nhds a) (nhds (e a)) := by rw [←de.induced a]; exact tendsto_comap protected lemma continuous (de : dense_embedding e) {a : α} : continuous e := continuous_iff_tendsto.2 $ λ a, de.tendsto lemma inj_iff (de : dense_embedding e) {x y} : e x = e y ↔ x = y := de.inj.eq_iff lemma closure_range : closure (range e) = univ := let h := de.dense in set.ext $ assume x, ⟨assume _, trivial, assume _, @h x⟩ lemma self_sub_closure_image_preimage_of_open {s : set β} (de : dense_embedding e) : is_open s → s ⊆ closure (e '' (e ⁻¹' s)) := begin intros s_op b b_in_s, rw [image_preimage_eq_inter_range, mem_closure_iff], intros U U_op b_in, rw ←inter_assoc, have ne_e : U ∩ s ≠ ∅ := ne_empty_of_mem ⟨b_in, b_in_s⟩, exact (dense_iff_inter_open.1 de.closure_range) _ (is_open_inter U_op s_op) ne_e end lemma closure_image_nhds_of_nhds {s : set α} {a : α} (de : dense_embedding e) : s ∈ (nhds a).sets → closure (e '' s) ∈ (nhds (e a)).sets := begin rw [← de.induced a, mem_comap_sets], intro h, rcases h with ⟨t, t_nhd, sub⟩, rw mem_nhds_sets_iff at t_nhd, rcases t_nhd with ⟨U, U_sub, ⟨U_op, e_a_in_U⟩⟩, have := calc e ⁻¹' U ⊆ e⁻¹' t : preimage_mono U_sub ... ⊆ s : sub, have := calc U ⊆ closure (e '' (e ⁻¹' U)) : self_sub_closure_image_preimage_of_open de U_op ... ⊆ closure (e '' s) : closure_mono (image_subset e this), have U_nhd : U ∈ (nhds (e a)).sets := mem_nhds_sets U_op e_a_in_U, exact (nhds (e a)).sets_of_superset U_nhd this end variables [topological_space δ] {f : γ → α} {g : γ → δ} {h : δ → β} /-- γ -f→ α g↓ ↓e δ -h→ β -/ lemma tendsto_comap_nhds_nhds {d : δ} {a : α} (de : dense_embedding e) (H : tendsto h (nhds d) (nhds (e a))) (comm : h ∘ g = e ∘ f) : tendsto f (comap g (nhds d)) (nhds a) := begin have lim1 : map g (comap g (nhds d)) ≤ nhds d := map_comap_le, replace lim1 : map h (map g (comap g (nhds d))) ≤ map h (nhds d) := map_mono lim1, rw [filter.map_map, comm, ← filter.map_map, map_le_iff_le_comap] at lim1, have lim2 : comap e (map h (nhds d)) ≤ comap e (nhds (e a)) := comap_mono H, rw de.induced at lim2, exact le_trans lim1 lim2, end protected lemma nhds_inf_neq_bot (de : dense_embedding e) {b : β} : nhds b ⊓ principal (range e) ≠ ⊥ := begin have h := de.dense, simp [closure_eq_nhds] at h, exact h _ end lemma comap_nhds_neq_bot (de : dense_embedding e) {b : β} : comap e (nhds b) ≠ ⊥ := forall_sets_neq_empty_iff_neq_bot.mp $ assume s ⟨t, ht, (hs : e ⁻¹' t ⊆ s)⟩, have t ∩ range e ∈ (nhds b ⊓ principal (range e)).sets, from inter_mem_inf_sets ht (subset.refl _), let ⟨_, ⟨hx₁, y, rfl⟩⟩ := inhabited_of_mem_sets de.nhds_inf_neq_bot this in subset_ne_empty hs $ ne_empty_of_mem hx₁ variables [topological_space γ] /-- If `e : α → β` is a dense embedding, then any function `α → γ` extends to a function `β → γ`. -/ def extend (de : dense_embedding e) (f : α → γ) (b : β) : γ := have nonempty γ, from let ⟨_, ⟨_, a, _⟩⟩ := exists_mem_of_ne_empty (mem_closure_iff.1 (de.dense b) _ is_open_univ trivial) in ⟨f a⟩, @lim _ (classical.inhabited_of_nonempty this) _ (map f (comap e (nhds b))) lemma extend_eq [t2_space γ] {b : β} {c : γ} {f : α → γ} (hf : map f (comap e (nhds b)) ≤ nhds c) : de.extend f b = c := @lim_eq _ (id _) _ _ _ _ (by simp; exact comap_nhds_neq_bot de) hf lemma extend_e_eq [t2_space γ] {a : α} {f : α → γ} (de : dense_embedding e) (hf : map f (nhds a) ≤ nhds (f a)) : de.extend f (e a) = f a := de.extend_eq begin rw de.induced; exact hf end lemma tendsto_extend [regular_space γ] {b : β} {f : α → γ} (de : dense_embedding e) (hf : {b | ∃c, tendsto f (comap e $ nhds b) (nhds c)} ∈ (nhds b).sets) : tendsto (de.extend f) (nhds b) (nhds (de.extend f b)) := let φ := {b | tendsto f (comap e $ nhds b) (nhds $ de.extend f b)} in have hφ : φ ∈ (nhds b).sets, from (nhds b).sets_of_superset hf $ assume b ⟨c, hc⟩, show tendsto f (comap e (nhds b)) (nhds (de.extend f b)), from (de.extend_eq hc).symm ▸ hc, assume s hs, let ⟨s'', hs''₁, hs''₂, hs''₃⟩ := nhds_is_closed hs in let ⟨s', hs'₁, (hs'₂ : e ⁻¹' s' ⊆ f ⁻¹' s'')⟩ := mem_of_nhds hφ hs''₁ in let ⟨t, (ht₁ : t ⊆ φ ∩ s'), ht₂, ht₃⟩ := mem_nhds_sets_iff.mp $ inter_mem_sets hφ hs'₁ in have h₁ : closure (f '' (e ⁻¹' s')) ⊆ s'', by rw [closure_subset_iff_subset_of_is_closed hs''₃, image_subset_iff]; exact hs'₂, have h₂ : t ⊆ de.extend f ⁻¹' closure (f '' (e ⁻¹' t)), from assume b' hb', have nhds b' ≤ principal t, by simp; exact mem_nhds_sets ht₂ hb', have map f (comap e (nhds b')) ≤ nhds (de.extend f b') ⊓ principal (f '' (e ⁻¹' t)), from calc _ ≤ map f (comap e (nhds b' ⊓ principal t)) : map_mono $ comap_mono $ le_inf (le_refl _) this ... ≤ map f (comap e (nhds b')) ⊓ map f (comap e (principal t)) : le_inf (map_mono $ comap_mono $ inf_le_left) (map_mono $ comap_mono $ inf_le_right) ... ≤ map f (comap e (nhds b')) ⊓ principal (f '' (e ⁻¹' t)) : by simp [le_refl] ... ≤ _ : inf_le_inf ((ht₁ hb').left) (le_refl _), show de.extend f b' ∈ closure (f '' (e ⁻¹' t)), begin rw [closure_eq_nhds], apply neq_bot_of_le_neq_bot _ this, simp, exact de.comap_nhds_neq_bot end, (nhds b).sets_of_superset (show t ∈ (nhds b).sets, from mem_nhds_sets ht₂ ht₃) (calc t ⊆ de.extend f ⁻¹' closure (f '' (e ⁻¹' t)) : h₂ ... ⊆ de.extend f ⁻¹' closure (f '' (e ⁻¹' s')) : preimage_mono $ closure_mono $ image_subset f $ preimage_mono $ subset.trans ht₁ $ inter_subset_right _ _ ... ⊆ de.extend f ⁻¹' s'' : preimage_mono h₁ ... ⊆ de.extend f ⁻¹' s : preimage_mono hs''₂) lemma continuous_extend [regular_space γ] {f : α → γ} (de : dense_embedding e) (hf : ∀b, ∃c, tendsto f (comap e (nhds b)) (nhds c)) : continuous (de.extend f) := continuous_iff_tendsto.mpr $ assume b, de.tendsto_extend $ univ_mem_sets' hf end dense_embedding namespace dense_embedding variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] /-- The product of two dense embeddings is a dense embedding -/ protected def prod {e₁ : α → β} {e₂ : γ → δ} (de₁ : dense_embedding e₁) (de₂ : dense_embedding e₂) : dense_embedding (λ(p : α × γ), (e₁ p.1, e₂ p.2)) := { dense_embedding . dense := have closure (range (λ(p : α × γ), (e₁ p.1, e₂ p.2))) = set.prod (closure (range e₁)) (closure (range e₂)), by rw [←closure_prod_eq, prod_range_range_eq], assume ⟨b, d⟩, begin rw [this], simp, constructor, apply de₁.dense, apply de₂.dense end, inj := assume ⟨x₁, x₂⟩ ⟨y₁, y₂⟩, by simp; exact assume h₁ h₂, ⟨de₁.inj h₁, de₂.inj h₂⟩, induced := assume ⟨a, b⟩, by rw [nhds_prod_eq, nhds_prod_eq, ←prod_comap_comap_eq, de₁.induced, de₂.induced] } def subtype_emb (p : α → Prop) {e : α → β} (de : dense_embedding e) (x : {x // p x}) : {x // x ∈ closure (e '' {x | p x})} := ⟨e x.1, subset_closure $ mem_image_of_mem e x.2⟩ protected def subtype (p : α → Prop) {e : α → β} (de : dense_embedding e) : dense_embedding (de.subtype_emb p) := { dense_embedding . dense := assume ⟨x, hx⟩, closure_subtype.mpr $ have (λ (x : {x // p x}), e (x.val)) = e ∘ subtype.val, from rfl, begin rw ← image_univ, simp [(image_comp _ _ _).symm, (∘), subtype_emb, -image_univ], rw [this, image_comp, subtype_val_image], simp, assumption end, inj := assume ⟨x, hx⟩ ⟨y, hy⟩ h, subtype.eq $ de.inj $ @@congr_arg subtype.val h, induced := assume ⟨x, hx⟩, by simp [subtype_emb, nhds_subtype_eq_comap, comap_comap_comp, (∘), (de.induced x).symm] } end dense_embedding lemma is_closed_property [topological_space α] [topological_space β] {e : α → β} {p : β → Prop} (he : closure (range e) = univ) (hp : is_closed {x | p x}) (h : ∀a, p (e a)) : ∀b, p b := have univ ⊆ {b | p b}, from calc univ = closure (range e) : he.symm ... ⊆ closure {b | p b} : closure_mono $ range_subset_iff.mpr h ... = _ : closure_eq_of_is_closed hp, assume b, this trivial lemma is_closed_property2 [topological_space α] [topological_space β] {e : α → β} {p : β → β → Prop} (he : dense_embedding e) (hp : is_closed {q:β×β | p q.1 q.2}) (h : ∀a₁ a₂, p (e a₁) (e a₂)) : ∀b₁ b₂, p b₁ b₂ := have ∀q:β×β, p q.1 q.2, from is_closed_property (he.prod he).closure_range hp $ assume a, h _ _, assume b₁ b₂, this ⟨b₁, b₂⟩ lemma is_closed_property3 [topological_space α] [topological_space β] {e : α → β} {p : β → β → β → Prop} (he : dense_embedding e) (hp : is_closed {q:β×β×β | p q.1 q.2.1 q.2.2}) (h : ∀a₁ a₂ a₃, p (e a₁) (e a₂) (e a₃)) : ∀b₁ b₂ b₃, p b₁ b₂ b₃ := have ∀q:β×β×β, p q.1 q.2.1 q.2.2, from is_closed_property (he.prod $ he.prod he).closure_range hp $ assume ⟨a₁, a₂, a₃⟩, h _ _ _, assume b₁ b₂ b₃, this ⟨b₁, b₂, b₃⟩ lemma mem_closure_of_continuous [topological_space α] [topological_space β] {f : α → β} {a : α} {s : set α} {t : set β} (hf : continuous f) (ha : a ∈ closure s) (h : ∀a∈s, f a ∈ closure t) : f a ∈ closure t := calc f a ∈ f '' closure s : mem_image_of_mem _ ha ... ⊆ closure (f '' s) : image_closure_subset_closure_image hf ... ⊆ closure (closure t) : closure_mono $ image_subset_iff.mpr $ h ... ⊆ closure t : begin rw [closure_eq_of_is_closed], exact subset.refl _, exact is_closed_closure end lemma mem_closure_of_continuous2 [topological_space α] [topological_space β] [topological_space γ] {f : α → β → γ} {a : α} {b : β} {s : set α} {t : set β} {u : set γ} (hf : continuous (λp:α×β, f p.1 p.2)) (ha : a ∈ closure s) (hb : b ∈ closure t) (h : ∀a∈s, ∀b∈t, f a b ∈ closure u) : f a b ∈ closure u := have (a,b) ∈ closure (set.prod s t), by simp [closure_prod_eq, ha, hb], show f (a, b).1 (a, b).2 ∈ closure u, from @mem_closure_of_continuous (α×β) _ _ _ (λp:α×β, f p.1 p.2) (a,b) _ u hf this $ assume ⟨p₁, p₂⟩ ⟨h₁, h₂⟩, h p₁ h₁ p₂ h₂
696a208b8d6677604e02bdc806bad121d712324e
6e9cd8d58e550c481a3b45806bd34a3514c6b3e0
/src/data/pnat/basic.lean
c8047021118d2834d89bc046da2311bd1ac0bcb5
[ "Apache-2.0" ]
permissive
sflicht/mathlib
220fd16e463928110e7b0a50bbed7b731979407f
1b2048d7195314a7e34e06770948ee00f0ac3545
refs/heads/master
1,665,934,056,043
1,591,373,803,000
1,591,373,803,000
269,815,267
0
0
Apache-2.0
1,591,402,068,000
1,591,402,067,000
null
UTF-8
Lean
false
false
13,480
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro, Neil Strickland -/ import data.nat.prime /-- `ℕ+` is the type of positive natural numbers. It is defined as a subtype, and the VM representation of `ℕ+` is the same as `ℕ` because the proof is not stored. -/ def pnat := {n : ℕ // 0 < n} notation `ℕ+` := pnat instance coe_pnat_nat : has_coe ℕ+ ℕ := ⟨subtype.val⟩ instance : has_repr ℕ+ := ⟨λ n, repr n.1⟩ namespace nat /-- Convert a natural number to a positive natural number. The positivity assumption is inferred by `dec_trivial`. -/ def to_pnat (n : ℕ) (h : 0 < n . tactic.exact_dec_trivial) : ℕ+ := ⟨n, h⟩ /-- Write a successor as an element of `ℕ+`. -/ def succ_pnat (n : ℕ) : ℕ+ := ⟨succ n, succ_pos n⟩ @[simp] theorem succ_pnat_coe (n : ℕ) : (succ_pnat n : ℕ) = succ n := rfl theorem succ_pnat_inj {n m : ℕ} : succ_pnat n = succ_pnat m → n = m := λ h, by { let h' := congr_arg (coe : ℕ+ → ℕ) h, exact nat.succ_inj h' } /-- Convert a natural number to a pnat. `n+1` is mapped to itself, and `0` becomes `1`. -/ def to_pnat' (n : ℕ) : ℕ+ := succ_pnat (pred n) @[simp] theorem to_pnat'_coe : ∀ (n : ℕ), ((to_pnat' n) : ℕ) = ite (0 < n) n 1 | 0 := rfl | (m + 1) := by {rw [if_pos (succ_pos m)], refl} namespace primes instance coe_pnat : has_coe nat.primes ℕ+ := ⟨λ p, ⟨(p : ℕ), p.property.pos⟩⟩ theorem coe_pnat_nat (p : nat.primes) : ((p : ℕ+) : ℕ) = p := rfl theorem coe_pnat_inj (p q : nat.primes) : (p : ℕ+) = (q : ℕ+) → p = q := λ h, begin replace h : ((p : ℕ+) : ℕ) = ((q : ℕ+) : ℕ) := congr_arg subtype.val h, rw [coe_pnat_nat, coe_pnat_nat] at h, exact subtype.eq h, end end primes end nat namespace pnat open nat /-- We now define a long list of structures on ℕ+ induced by similar structures on ℕ. Most of these behave in a completely obvious way, but there are a few things to be said about subtraction, division and powers. -/ instance : decidable_eq ℕ+ := λ (a b : ℕ+), by apply_instance instance : decidable_linear_order ℕ+ := subtype.decidable_linear_order _ @[simp] lemma mk_le_mk (n k : ℕ) (hn : 0 < n) (hk : 0 < k) : (⟨n, hn⟩ : ℕ+) ≤ ⟨k, hk⟩ ↔ n ≤ k := iff.rfl @[simp] lemma mk_lt_mk (n k : ℕ) (hn : 0 < n) (hk : 0 < k) : (⟨n, hn⟩ : ℕ+) < ⟨k, hk⟩ ↔ n < k := iff.rfl @[simp, norm_cast] lemma coe_le_coe (n k : ℕ+) : (n:ℕ) ≤ k ↔ n ≤ k := iff.rfl @[simp, norm_cast] lemma coe_lt_coe (n k : ℕ+) : (n:ℕ) < k ↔ n < k := iff.rfl @[simp] theorem pos (n : ℕ+) : 0 < (n : ℕ) := n.2 theorem eq {m n : ℕ+} : (m : ℕ) = n → m = n := subtype.eq @[simp] theorem mk_coe (n h) : ((⟨n, h⟩ : ℕ+) : ℕ) = n := rfl instance : add_comm_semigroup ℕ+ := { add := λ a b, ⟨(a + b : ℕ), add_pos a.pos b.pos⟩, add_comm := λ a b, subtype.eq (add_comm a b), add_assoc := λ a b c, subtype.eq (add_assoc a b c) } @[simp] theorem add_coe (m n : ℕ+) : ((m + n : ℕ+) : ℕ) = m + n := rfl instance coe_add_hom : is_add_hom (coe : ℕ+ → ℕ) := ⟨add_coe⟩ instance : add_left_cancel_semigroup ℕ+ := { add_left_cancel := λ a b c h, by { replace h := congr_arg (coe : ℕ+ → ℕ) h, rw [add_coe, add_coe] at h, exact eq ((add_right_inj (a : ℕ)).mp h)}, .. (pnat.add_comm_semigroup) } instance : add_right_cancel_semigroup ℕ+ := { add_right_cancel := λ a b c h, by { replace h := congr_arg (coe : ℕ+ → ℕ) h, rw [add_coe, add_coe] at h, exact eq ((add_left_inj (b : ℕ)).mp h)}, .. (pnat.add_comm_semigroup) } @[simp] theorem ne_zero (n : ℕ+) : (n : ℕ) ≠ 0 := ne_of_gt n.2 theorem to_pnat'_coe {n : ℕ} : 0 < n → (n.to_pnat' : ℕ) = n := succ_pred_eq_of_pos @[simp] theorem coe_to_pnat' (n : ℕ+) : (n : ℕ).to_pnat' = n := eq (to_pnat'_coe n.pos) instance : comm_monoid ℕ+ := { mul := λ m n, ⟨m.1 * n.1, mul_pos m.2 n.2⟩, mul_assoc := λ a b c, subtype.eq (mul_assoc _ _ _), one := succ_pnat 0, one_mul := λ a, subtype.eq (one_mul _), mul_one := λ a, subtype.eq (mul_one _), mul_comm := λ a b, subtype.eq (mul_comm _ _) } theorem lt_add_one_iff : ∀ {a b : ℕ+}, a < b + 1 ↔ a ≤ b := λ a b, nat.lt_add_one_iff theorem add_one_le_iff : ∀ {a b : ℕ+}, a + 1 ≤ b ↔ a < b := λ a b, nat.add_one_le_iff @[simp] lemma one_le (n : ℕ+) : (1 : ℕ+) ≤ n := n.2 instance : order_bot ℕ+ := { bot := 1, bot_le := λ a, a.property, ..(by apply_instance : partial_order ℕ+) } @[simp] lemma bot_eq_zero : (⊥ : ℕ+) = 1 := rfl instance : inhabited ℕ+ := ⟨1⟩ -- Some lemmas that rewrite `pnat.mk n h`, for `n` an explicit numeral, into explicit numerals. @[simp] lemma mk_one {h} : (⟨1, h⟩ : ℕ+) = (1 : ℕ+) := rfl @[simp] lemma mk_bit0 (n) {h} : (⟨bit0 n, h⟩ : ℕ+) = (bit0 ⟨n, pos_of_bit0_pos h⟩ : ℕ+) := rfl @[simp] lemma mk_bit1 (n) {h} {k} : (⟨bit1 n, h⟩ : ℕ+) = (bit1 ⟨n, k⟩ : ℕ+) := rfl -- Some lemmas that rewrite inequalities between explicit numerals in `pnat` -- into the corresponding inequalities in `nat`. -- TODO: perhaps this should not be attempted by `simp`, -- and instead we should expect `norm_num` to take care of these directly? -- TODO: these lemmas are perhaps incomplete: -- * 1 is not represented as a bit0 or bit1 -- * strict inequalities? @[simp] lemma bit0_le_bit0 (n m : ℕ+) : (bit0 n) ≤ (bit0 m) ↔ (bit0 (n : ℕ)) ≤ (bit0 (m : ℕ)) := iff.rfl @[simp] lemma bit0_le_bit1 (n m : ℕ+) : (bit0 n) ≤ (bit1 m) ↔ (bit0 (n : ℕ)) ≤ (bit1 (m : ℕ)) := iff.rfl @[simp] lemma bit1_le_bit0 (n m : ℕ+) : (bit1 n) ≤ (bit0 m) ↔ (bit1 (n : ℕ)) ≤ (bit0 (m : ℕ)) := iff.rfl @[simp] lemma bit1_le_bit1 (n m : ℕ+) : (bit1 n) ≤ (bit1 m) ↔ (bit1 (n : ℕ)) ≤ (bit1 (m : ℕ)) := iff.rfl @[simp] theorem one_coe : ((1 : ℕ+) : ℕ) = 1 := rfl @[simp] theorem mul_coe (m n : ℕ+) : ((m * n : ℕ+) : ℕ) = m * n := rfl instance coe_mul_hom : is_monoid_hom (coe : ℕ+ → ℕ) := {map_one := one_coe, map_mul := mul_coe} @[simp] lemma coe_bit0 (a : ℕ+) : ((bit0 a : ℕ+) : ℕ) = bit0 (a : ℕ) := rfl @[simp] lemma coe_bit1 (a : ℕ+) : ((bit1 a : ℕ+) : ℕ) = bit1 (a : ℕ) := rfl @[simp] theorem pow_coe (m : ℕ+) (n : ℕ) : ((m ^ n : ℕ+) : ℕ) = (m : ℕ) ^ n := by induction n with n ih; [refl, rw [nat.pow_succ, _root_.pow_succ, mul_coe, mul_comm, ih]] instance : left_cancel_semigroup ℕ+ := { mul_left_cancel := λ a b c h, by { replace h := congr_arg (coe : ℕ+ → ℕ) h, exact eq ((nat.mul_right_inj a.pos).mp h)}, .. (pnat.comm_monoid) } instance : right_cancel_semigroup ℕ+ := { mul_right_cancel := λ a b c h, by { replace h := congr_arg (coe : ℕ+ → ℕ) h, exact eq ((nat.mul_left_inj b.pos).mp h)}, .. (pnat.comm_monoid) } instance : distrib ℕ+ := { left_distrib := λ a b c, eq (mul_add a b c), right_distrib := λ a b c, eq (add_mul a b c), ..(pnat.add_comm_semigroup), ..(pnat.comm_monoid) } /-- Subtraction a - b is defined in the obvious way when a > b, and by a - b = 1 if a ≤ b. -/ instance : has_sub ℕ+ := ⟨λ a b, to_pnat' (a - b : ℕ)⟩ theorem sub_coe (a b : ℕ+) : ((a - b : ℕ+) : ℕ) = ite (b < a) (a - b : ℕ) 1 := begin change ((to_pnat' ((a : ℕ) - (b : ℕ)) : ℕ)) = ite ((a : ℕ) > (b : ℕ)) ((a : ℕ) - (b : ℕ)) 1, split_ifs with h, { exact to_pnat'_coe (nat.sub_pos_of_lt h) }, { rw [nat.sub_eq_zero_iff_le.mpr (le_of_not_gt h)], refl } end theorem add_sub_of_lt {a b : ℕ+} : a < b → a + (b - a) = b := λ h, eq $ by { rw [add_coe, sub_coe, if_pos h], exact nat.add_sub_of_le (le_of_lt h) } /-- We define m % k and m / k in the same way as for nat except that when m = n * k we take m % k = k and m / k = n - 1. This ensures that m % k is always positive and m = (m % k) + k * (m / k) in all cases. Later we define a function div_exact which gives the usual m / k in the case where k divides m. -/ def mod_div_aux : ℕ+ → ℕ → ℕ → ℕ+ × ℕ | k 0 q := ⟨k, q.pred⟩ | k (r + 1) q := ⟨⟨r + 1, nat.succ_pos r⟩, q⟩ lemma mod_div_aux_spec : ∀ (k : ℕ+) (r q : ℕ) (h : ¬ (r = 0 ∧ q = 0)), (((mod_div_aux k r q).1 : ℕ) + k * (mod_div_aux k r q).2 = (r + k * q)) | k 0 0 h := (h ⟨rfl, rfl⟩).elim | k 0 (q + 1) h := by { change (k : ℕ) + (k : ℕ) * (q + 1).pred = 0 + (k : ℕ) * (q + 1), rw [nat.pred_succ, nat.mul_succ, zero_add, add_comm]} | k (r + 1) q h := rfl def mod_div (m k : ℕ+) : ℕ+ × ℕ := mod_div_aux k ((m : ℕ) % (k : ℕ)) ((m : ℕ) / (k : ℕ)) def mod (m k : ℕ+) : ℕ+ := (mod_div m k).1 def div (m k : ℕ+) : ℕ := (mod_div m k).2 theorem mod_add_div (m k : ℕ+) : (m : ℕ) = (mod m k) + k * (div m k) := begin let h₀ := nat.mod_add_div (m : ℕ) (k : ℕ), have : ¬ ((m : ℕ) % (k : ℕ) = 0 ∧ (m : ℕ) / (k : ℕ) = 0), by { rintro ⟨hr, hq⟩, rw [hr, hq, mul_zero, zero_add] at h₀, exact (m.ne_zero h₀.symm).elim }, have := mod_div_aux_spec k ((m : ℕ) % (k : ℕ)) ((m : ℕ) / (k : ℕ)) this, exact (this.trans h₀).symm, end theorem mod_coe (m k : ℕ+) : ((mod m k) : ℕ) = ite ((m : ℕ) % (k : ℕ) = 0) (k : ℕ) ((m : ℕ) % (k : ℕ)) := begin dsimp [mod, mod_div], cases (m : ℕ) % (k : ℕ), { rw [if_pos rfl], refl }, { rw [if_neg n.succ_ne_zero], refl } end theorem div_coe (m k : ℕ+) : ((div m k) : ℕ) = ite ((m : ℕ) % (k : ℕ) = 0) ((m : ℕ) / (k : ℕ)).pred ((m : ℕ) / (k : ℕ)) := begin dsimp [div, mod_div], cases (m : ℕ) % (k : ℕ), { rw [if_pos rfl], refl }, { rw [if_neg n.succ_ne_zero], refl } end theorem mod_le (m k : ℕ+) : mod m k ≤ m ∧ mod m k ≤ k := begin change ((mod m k) : ℕ) ≤ (m : ℕ) ∧ ((mod m k) : ℕ) ≤ (k : ℕ), rw [mod_coe], split_ifs, { have hm : (m : ℕ) > 0 := m.pos, rw [← nat.mod_add_div (m : ℕ) (k : ℕ), h, zero_add] at hm ⊢, by_cases h' : ((m : ℕ) / (k : ℕ)) = 0, { rw [h', mul_zero] at hm, exact (lt_irrefl _ hm).elim}, { let h' := nat.mul_le_mul_left (k : ℕ) (nat.succ_le_of_lt (nat.pos_of_ne_zero h')), rw [mul_one] at h', exact ⟨h', le_refl (k : ℕ)⟩ } }, { exact ⟨nat.mod_le (m : ℕ) (k : ℕ), le_of_lt (nat.mod_lt (m : ℕ) k.pos)⟩ } end instance : has_dvd ℕ+ := ⟨λ k m, (k : ℕ) ∣ (m : ℕ)⟩ theorem dvd_iff {k m : ℕ+} : k ∣ m ↔ (k : ℕ) ∣ (m : ℕ) := by {refl} theorem dvd_iff' {k m : ℕ+} : k ∣ m ↔ mod m k = k := begin change (k : ℕ) ∣ (m : ℕ) ↔ mod m k = k, rw [nat.dvd_iff_mod_eq_zero], split, { intro h, apply eq, rw [mod_coe, if_pos h] }, { intro h, by_cases h' : (m : ℕ) % (k : ℕ) = 0, { exact h'}, { replace h : ((mod m k) : ℕ) = (k : ℕ) := congr_arg _ h, rw [mod_coe, if_neg h'] at h, exact (ne_of_lt (nat.mod_lt (m : ℕ) k.pos) h).elim } } end def div_exact {m k : ℕ+} (h : k ∣ m) : ℕ+ := ⟨(div m k).succ, nat.succ_pos _⟩ theorem mul_div_exact {m k : ℕ+} (h : k ∣ m) : k * (div_exact h) = m := begin apply eq, rw [mul_coe], change (k : ℕ) * (div m k).succ = m, rw [mod_add_div m k, dvd_iff'.mp h, nat.mul_succ, add_comm], end theorem dvd_iff'' {k n : ℕ+} : k ∣ n ↔ ∃ m, k * m = n := ⟨λ h, ⟨div_exact h, mul_div_exact h⟩, λ ⟨m, h⟩, dvd.intro (m : ℕ) ((mul_coe k m).symm.trans (congr_arg subtype.val h))⟩ theorem dvd_intro {k n : ℕ+} (m : ℕ+) (h : k * m = n) : k ∣ n := dvd_iff''.mpr ⟨m, h⟩ theorem dvd_refl (m : ℕ+) : m ∣ m := dvd_intro 1 (mul_one m) theorem dvd_antisymm {m n : ℕ+} : m ∣ n → n ∣ m → m = n := λ hmn hnm, subtype.eq (nat.dvd_antisymm hmn hnm) protected theorem dvd_trans {k m n : ℕ+} : k ∣ m → m ∣ n → k ∣ n := @dvd_trans ℕ _ (k : ℕ) (m : ℕ) (n : ℕ) theorem one_dvd (n : ℕ+) : 1 ∣ n := dvd_intro n (one_mul n) theorem dvd_one_iff (n : ℕ+) : n ∣ 1 ↔ n = 1 := ⟨λ h, dvd_antisymm h (one_dvd n), λ h, h.symm ▸ (dvd_refl 1)⟩ def gcd (n m : ℕ+) : ℕ+ := ⟨nat.gcd (n : ℕ) (m : ℕ), nat.gcd_pos_of_pos_left (m : ℕ) n.pos⟩ def lcm (n m : ℕ+) : ℕ+ := ⟨nat.lcm (n : ℕ) (m : ℕ), by { let h := mul_pos n.pos m.pos, rw [← gcd_mul_lcm (n : ℕ) (m : ℕ), mul_comm] at h, exact pos_of_dvd_of_pos (dvd.intro (nat.gcd (n : ℕ) (m : ℕ)) rfl) h }⟩ @[simp] theorem gcd_coe (n m : ℕ+) : ((gcd n m) : ℕ) = nat.gcd n m := rfl @[simp] theorem lcm_coe (n m : ℕ+) : ((lcm n m) : ℕ) = nat.lcm n m := rfl theorem gcd_dvd_left (n m : ℕ+) : (gcd n m) ∣ n := nat.gcd_dvd_left (n : ℕ) (m : ℕ) theorem gcd_dvd_right (n m : ℕ+) : (gcd n m) ∣ m := nat.gcd_dvd_right (n : ℕ) (m : ℕ) theorem dvd_gcd {m n k : ℕ+} (hm : k ∣ m) (hn : k ∣ n) : k ∣ gcd m n := @nat.dvd_gcd (m : ℕ) (n : ℕ) (k : ℕ) hm hn theorem dvd_lcm_left (n m : ℕ+) : n ∣ lcm n m := nat.dvd_lcm_left (n : ℕ) (m : ℕ) theorem dvd_lcm_right (n m : ℕ+) : m ∣ lcm n m := nat.dvd_lcm_right (n : ℕ) (m : ℕ) theorem lcm_dvd {m n k : ℕ+} (hm : m ∣ k) (hn : n ∣ k) : lcm m n ∣ k := @nat.lcm_dvd (m : ℕ) (n : ℕ) (k : ℕ) hm hn theorem gcd_mul_lcm (n m : ℕ+) : (gcd n m) * (lcm n m) = n * m := subtype.eq (nat.gcd_mul_lcm (n : ℕ) (m : ℕ)) def prime (p : ℕ+) : Prop := (p : ℕ).prime end pnat
a46f2d3e038e1140953366a593b1ff1000f44b5e
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/ring/pi.lean
7b0ff31115171d9cabd259cb678e92d28a64cfde
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
5,725
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Patrick Massot -/ import tactic.pi_instances import algebra.group.pi import algebra.ring.basic /-! # Pi instances for ring This file defines instances for ring, semiring and related structures on Pi Types -/ namespace pi universes u v w variable {I : Type u} -- The indexing type variable {f : I → Type v} -- The family of types already equipped with instances variables (x y : Π i, f i) (i : I) instance distrib [Π i, distrib $ f i] : distrib (Π i : I, f i) := by refine_struct { add := (+), mul := (*), .. }; tactic.pi_instance_derive_field instance non_unital_non_assoc_semiring [∀ i, non_unital_non_assoc_semiring $ f i] : non_unital_non_assoc_semiring (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), add := (+), mul := (*), .. }; tactic.pi_instance_derive_field instance non_unital_semiring [∀ i, non_unital_semiring $ f i] : non_unital_semiring (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), add := (+), mul := (*), .. }; tactic.pi_instance_derive_field instance non_assoc_semiring [∀ i, non_assoc_semiring $ f i] : non_assoc_semiring (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), one := 1, add := (+), mul := (*), .. }; tactic.pi_instance_derive_field instance semiring [∀ i, semiring $ f i] : semiring (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), one := 1, add := (+), mul := (*), nsmul := add_monoid.nsmul, npow := monoid.npow }; tactic.pi_instance_derive_field instance non_unital_comm_semiring [∀ i, non_unital_comm_semiring $ f i] : non_unital_comm_semiring (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), add := (+), mul := (*), nsmul := add_monoid.nsmul }; tactic.pi_instance_derive_field instance comm_semiring [∀ i, comm_semiring $ f i] : comm_semiring (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), one := 1, add := (+), mul := (*), nsmul := add_monoid.nsmul, npow := monoid.npow }; tactic.pi_instance_derive_field instance non_unital_non_assoc_ring [∀ i, non_unital_non_assoc_ring $ f i] : non_unital_non_assoc_ring (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), add := (+), mul := (*), neg := has_neg.neg, nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul }; tactic.pi_instance_derive_field instance non_unital_ring [∀ i, non_unital_ring $ f i] : non_unital_ring (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), add := (+), mul := (*), neg := has_neg.neg, nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul }; tactic.pi_instance_derive_field instance non_assoc_ring [∀ i, non_assoc_ring $ f i] : non_assoc_ring (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), add := (+), mul := (*), neg := has_neg.neg, nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul }; tactic.pi_instance_derive_field instance ring [∀ i, ring $ f i] : ring (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), one := 1, add := (+), mul := (*), neg := has_neg.neg, nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul, npow := monoid.npow }; tactic.pi_instance_derive_field instance non_unital_comm_ring [∀ i, non_unital_comm_ring $ f i] : non_unital_comm_ring (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), add := (+), mul := (*), neg := has_neg.neg, nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul }; tactic.pi_instance_derive_field instance comm_ring [∀ i, comm_ring $ f i] : comm_ring (Π i : I, f i) := by refine_struct { zero := (0 : Π i, f i), one := 1, add := (+), mul := (*), neg := has_neg.neg, nsmul := add_monoid.nsmul, zsmul := sub_neg_monoid.zsmul, npow := monoid.npow }; tactic.pi_instance_derive_field /-- A family of ring homomorphisms `f a : γ →+* β a` defines a ring homomorphism `pi.ring_hom f : γ →+* Π a, β a` given by `pi.ring_hom f x b = f b x`. -/ @[simps] protected def ring_hom {γ : Type w} [Π i, non_assoc_semiring (f i)] [non_assoc_semiring γ] (g : Π i, γ →+* f i) : γ →+* Π i, f i := { to_fun := λ x b, g b x, map_add' := λ x y, funext $ λ z, (g z).map_add x y, map_mul' := λ x y, funext $ λ z, (g z).map_mul x y, map_one' := funext $ λ z, (g z).map_one, map_zero' := funext $ λ z, (g z).map_zero } lemma ring_hom_injective {γ : Type w} [nonempty I] [Π i, non_assoc_semiring (f i)] [non_assoc_semiring γ] (g : Π i, γ →+* f i) (hg : ∀ i, function.injective (g i)) : function.injective (pi.ring_hom g) := λ x y h, let ⟨i⟩ := ‹nonempty I› in hg i ((function.funext_iff.mp h : _) i) end pi section ring_hom universes u v variable {I : Type u} /-- Evaluation of functions into an indexed collection of rings at a point is a ring homomorphism. This is `function.eval` as a `ring_hom`. -/ @[simps] def pi.eval_ring_hom (f : I → Type v) [Π i, non_assoc_semiring (f i)] (i : I) : (Π i, f i) →+* f i := { ..(pi.eval_monoid_hom f i), ..(pi.eval_add_monoid_hom f i) } /-- `function.const` as a `ring_hom`. -/ @[simps] def pi.const_ring_hom (α β : Type*) [non_assoc_semiring β] : β →+* (α → β) := { to_fun := function.const _, .. pi.ring_hom (λ _, ring_hom.id β) } /-- Ring homomorphism between the function spaces `I → α` and `I → β`, induced by a ring homomorphism `f` between `α` and `β`. -/ @[simps] protected def ring_hom.comp_left {α β : Type*} [non_assoc_semiring α] [non_assoc_semiring β] (f : α →+* β) (I : Type*) : (I → α) →+* (I → β) := { to_fun := λ h, f ∘ h, .. f.to_monoid_hom.comp_left I, .. f.to_add_monoid_hom.comp_left I } end ring_hom
187eb07efb4f05bfffbd312b802dc7659810a9db
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/algebra/group_action_hom.lean
a689d75d654baf5db6887190a5033432cc5937d2
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
10,470
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.group_ring_action /-! # Equivariant homomorphisms ## Main definitions * `mul_action_hom M X Y`, the type of equivariant functions from `X` to `Y`, where `M` is a monoid that acts on the types `X` and `Y`. * `distrib_mul_action_hom M A B`, the type of equivariant additive monoid homomorphisms from `A` to `B`, where `M` is a monoid that acts on the additive monoids `A` and `B`. * `mul_semiring_action_hom M R S`, the type of equivariant ring homomorphisms from `R` to `S`, where `M` is a monoid that acts on the rings `R` and `S`. ## Notations * `X →[M] Y` is `mul_action_hom M X Y`. * `A →+[M] B` is `distrib_mul_action_hom M X Y`. * `R →+*[M] S` is `mul_semiring_action_hom M X Y`. -/ variables (M : Type*) [monoid M] variables (X : Type*) [mul_action M X] variables (Y : Type*) [mul_action M Y] variables (Z : Type*) [mul_action M Z] variables (A : Type*) [add_monoid A] [distrib_mul_action M A] variables (A' : Type*) [add_group A'] [distrib_mul_action M A'] variables (B : Type*) [add_monoid B] [distrib_mul_action M B] variables (B' : Type*) [add_group B'] [distrib_mul_action M B'] variables (C : Type*) [add_monoid C] [distrib_mul_action M C] variables (R : Type*) [semiring R] [mul_semiring_action M R] variables (R' : Type*) [ring R'] [mul_semiring_action M R'] variables (S : Type*) [semiring S] [mul_semiring_action M S] variables (S' : Type*) [ring S'] [mul_semiring_action M S'] variables (T : Type*) [semiring T] [mul_semiring_action M T] variables (G : Type*) [group G] (H : subgroup G) set_option old_structure_cmd true /-- Equivariant functions. -/ @[nolint has_inhabited_instance] structure mul_action_hom := (to_fun : X → Y) (map_smul' : ∀ (m : M) (x : X), to_fun (m • x) = m • to_fun x) notation X ` →[`:25 M:25 `] `:0 Y:0 := mul_action_hom M X Y namespace mul_action_hom instance : has_coe_to_fun (X →[M] Y) := ⟨_, λ c, c.to_fun⟩ variables {M X Y} @[simp] lemma map_smul (f : X →[M] Y) (m : M) (x : X) : f (m • x) = m • f x := f.map_smul' m x @[ext] theorem ext : ∀ {f g : X →[M] Y}, (∀ x, f x = g x) → f = g | ⟨f, _⟩ ⟨g, _⟩ H := by { congr' 1 with x, exact H x } theorem ext_iff {f g : X →[M] Y} : f = g ↔ ∀ x, f x = g x := ⟨λ H x, by rw H, ext⟩ variables (M) {X} /-- The identity map as an equivariant map. -/ protected def id : X →[M] X := ⟨id, λ _ _, rfl⟩ @[simp] lemma id_apply (x : X) : mul_action_hom.id M x = x := rfl variables {M X Y Z} /-- Composition of two equivariant maps. -/ def comp (g : Y →[M] Z) (f : X →[M] Y) : X →[M] Z := ⟨g ∘ f, λ m x, calc g (f (m • x)) = g (m • f x) : by rw f.map_smul ... = m • g (f x) : g.map_smul _ _⟩ @[simp] lemma comp_apply (g : Y →[M] Z) (f : X →[M] Y) (x : X) : g.comp f x = g (f x) := rfl @[simp] lemma id_comp (f : X →[M] Y) : (mul_action_hom.id M).comp f = f := ext $ λ x, by rw [comp_apply, id_apply] @[simp] lemma comp_id (f : X →[M] Y) : f.comp (mul_action_hom.id M) = f := ext $ λ x, by rw [comp_apply, id_apply] local attribute [instance] mul_action.regular variables {G} (H) /-- The canonical map to the left cosets. -/ def to_quotient : G →[G] quotient_group.quotient H := ⟨coe, λ g x, rfl⟩ @[simp] lemma to_quotient_apply (g : G) : to_quotient H g = g := rfl end mul_action_hom /-- Equivariant additive monoid homomorphisms. -/ @[nolint has_inhabited_instance] structure distrib_mul_action_hom extends A →[M] B, A →+ B. /-- Reinterpret an equivariant additive monoid homomorphism as an additive monoid homomorphism. -/ add_decl_doc distrib_mul_action_hom.to_add_monoid_hom /-- Reinterpret an equivariant additive monoid homomorphism as an equivariant function. -/ add_decl_doc distrib_mul_action_hom.to_mul_action_hom notation A ` →+[`:25 M:25 `] `:0 B:0 := distrib_mul_action_hom M A B namespace distrib_mul_action_hom instance has_coe : has_coe (A →+[M] B) (A →+ B) := ⟨to_add_monoid_hom⟩ instance has_coe' : has_coe (A →+[M] B) (A →[M] B) := ⟨to_mul_action_hom⟩ instance : has_coe_to_fun (A →+[M] B) := ⟨_, λ c, c.to_fun⟩ variables {M A B} @[norm_cast] lemma coe_fn_coe (f : A →+[M] B) : ((f : A →+ B) : A → B) = f := rfl @[norm_cast] lemma coe_fn_coe' (f : A →+[M] B) : ((f : A →[M] B) : A → B) = f := rfl @[ext] theorem ext : ∀ {f g : A →+[M] B}, (∀ x, f x = g x) → f = g | ⟨f, _, _, _⟩ ⟨g, _, _, _⟩ H := by { congr' 1 with x, exact H x } theorem ext_iff {f g : A →+[M] B} : f = g ↔ ∀ x, f x = g x := ⟨λ H x, by rw H, ext⟩ @[simp] lemma map_zero (f : A →+[M] B) : f 0 = 0 := f.map_zero' @[simp] lemma map_add (f : A →+[M] B) (x y : A) : f (x + y) = f x + f y := f.map_add' x y @[simp] lemma map_neg (f : A' →+[M] B') (x : A') : f (-x) = -f x := (f : A' →+ B').map_neg x @[simp] lemma map_sub (f : A' →+[M] B') (x y : A') : f (x - y) = f x - f y := (f : A' →+ B').map_sub x y @[simp] lemma map_smul (f : A →+[M] B) (m : M) (x : A) : f (m • x) = m • f x := f.map_smul' m x variables (M) {A} /-- The identity map as an equivariant additive monoid homomorphism. -/ protected def id : A →+[M] A := ⟨id, λ _ _, rfl, rfl, λ _ _, rfl⟩ @[simp] lemma id_apply (x : A) : distrib_mul_action_hom.id M x = x := rfl variables {M A B C} /-- Composition of two equivariant additive monoid homomorphisms. -/ def comp (g : B →+[M] C) (f : A →+[M] B) : A →+[M] C := { .. mul_action_hom.comp (g : B →[M] C) (f : A →[M] B), .. add_monoid_hom.comp (g : B →+ C) (f : A →+ B), } @[simp] lemma comp_apply (g : B →+[M] C) (f : A →+[M] B) (x : A) : g.comp f x = g (f x) := rfl @[simp] lemma id_comp (f : A →+[M] B) : (distrib_mul_action_hom.id M).comp f = f := ext $ λ x, by rw [comp_apply, id_apply] @[simp] lemma comp_id (f : A →+[M] B) : f.comp (distrib_mul_action_hom.id M) = f := ext $ λ x, by rw [comp_apply, id_apply] end distrib_mul_action_hom /-- Equivariant ring homomorphisms. -/ @[nolint has_inhabited_instance] structure mul_semiring_action_hom extends R →+[M] S, R →+* S. /-- Reinterpret an equivariant ring homomorphism as a ring homomorphism. -/ add_decl_doc mul_semiring_action_hom.to_ring_hom /-- Reinterpret an equivariant ring homomorphism as an equivariant additive monoid homomorphism. -/ add_decl_doc mul_semiring_action_hom.to_distrib_mul_action_hom notation R ` →+*[`:25 M:25 `] `:0 S:0 := mul_semiring_action_hom M R S namespace mul_semiring_action_hom instance has_coe : has_coe (R →+*[M] S) (R →+* S) := ⟨to_ring_hom⟩ instance has_coe' : has_coe (R →+*[M] S) (R →+[M] S) := ⟨to_distrib_mul_action_hom⟩ instance : has_coe_to_fun (R →+*[M] S) := ⟨_, λ c, c.to_fun⟩ variables {M R S} @[norm_cast] lemma coe_fn_coe (f : R →+*[M] S) : ((f : R →+* S) : R → S) = f := rfl @[norm_cast] lemma coe_fn_coe' (f : R →+*[M] S) : ((f : R →+[M] S) : R → S) = f := rfl @[ext] theorem ext : ∀ {f g : R →+*[M] S}, (∀ x, f x = g x) → f = g | ⟨f, _, _, _, _, _⟩ ⟨g, _, _, _, _, _⟩ H := by { congr' 1 with x, exact H x } theorem ext_iff {f g : R →+*[M] S} : f = g ↔ ∀ x, f x = g x := ⟨λ H x, by rw H, ext⟩ @[simp] lemma map_zero (f : R →+*[M] S) : f 0 = 0 := f.map_zero' @[simp] lemma map_add (f : R →+*[M] S) (x y : R) : f (x + y) = f x + f y := f.map_add' x y @[simp] lemma map_neg (f : R' →+*[M] S') (x : R') : f (-x) = -f x := (f : R' →+* S').map_neg x @[simp] lemma map_sub (f : R' →+*[M] S') (x y : R') : f (x - y) = f x - f y := (f : R' →+* S').map_sub x y @[simp] lemma map_one (f : R →+*[M] S) : f 1 = 1 := f.map_one' @[simp] lemma map_mul (f : R →+*[M] S) (x y : R) : f (x * y) = f x * f y := f.map_mul' x y @[simp] lemma map_smul (f : R →+*[M] S) (m : M) (x : R) : f (m • x) = m • f x := f.map_smul' m x variables (M) {R} /-- The identity map as an equivariant ring homomorphism. -/ protected def id : R →+*[M] R := ⟨id, λ _ _, rfl, rfl, λ _ _, rfl, rfl, λ _ _, rfl⟩ @[simp] lemma id_apply (x : R) : mul_semiring_action_hom.id M x = x := rfl variables {M R S T} /-- Composition of two equivariant additive monoid homomorphisms. -/ def comp (g : S →+*[M] T) (f : R →+*[M] S) : R →+*[M] T := { .. distrib_mul_action_hom.comp (g : S →+[M] T) (f : R →+[M] S), .. ring_hom.comp (g : S →+* T) (f : R →+* S), } @[simp] lemma comp_apply (g : S →+*[M] T) (f : R →+*[M] S) (x : R) : g.comp f x = g (f x) := rfl @[simp] lemma id_comp (f : R →+*[M] S) : (mul_semiring_action_hom.id M).comp f = f := ext $ λ x, by rw [comp_apply, id_apply] @[simp] lemma comp_id (f : R →+*[M] S) : f.comp (mul_semiring_action_hom.id M) = f := ext $ λ x, by rw [comp_apply, id_apply] variables {P : Type*} [comm_semiring P] [mul_semiring_action M P] variables {Q : Type*} [comm_semiring Q] [mul_semiring_action M Q] open polynomial /-- An equivariant map induces an equivariant map on polynomials. -/ protected noncomputable def polynomial (g : P →+*[M] Q) : polynomial P →+*[M] polynomial Q := { to_fun := map g, map_smul' := λ m p, polynomial.induction_on p (λ b, by rw [smul_C, map_C, coe_fn_coe, g.map_smul, map_C, coe_fn_coe, smul_C]) (λ p q ihp ihq, by rw [smul_add, polynomial.map_add, ihp, ihq, polynomial.map_add, smul_add]) (λ n b ih, by rw [smul_mul', smul_C, smul_pow, smul_X, polynomial.map_mul, map_C, polynomial.map_pow, map_X, coe_fn_coe, g.map_smul, polynomial.map_mul, map_C, polynomial.map_pow, map_X, smul_mul', smul_C, smul_pow, smul_X, coe_fn_coe]), map_zero' := polynomial.map_zero g, map_add' := λ p q, polynomial.map_add g, map_one' := polynomial.map_one g, map_mul' := λ p q, polynomial.map_mul g } @[simp] theorem coe_polynomial (g : P →+*[M] Q) : (g.polynomial : polynomial P → polynomial Q) = map g := rfl end mul_semiring_action_hom variables (M) {R'} (U : set R') [is_subring U] [is_invariant_subring M U] /-- The canonical inclusion from an invariant subring. -/ def is_invariant_subring.subtype_hom : U →+*[M] R' := { map_smul' := λ m s, rfl, .. is_subring.subtype U } @[simp] theorem is_invariant_subring.coe_subtype_hom : (is_invariant_subring.subtype_hom M U : U → R') = coe := rfl @[simp] theorem is_invariant_subring.coe_subtype_hom' : (is_invariant_subring.subtype_hom M U : U →+* R') = is_subring.subtype U := rfl
f057c0485bf3ac5a3a84a8b11656237e782cfd85
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/combinatorics/simple_graph/basic_auto.lean
531ac05916a32eac5fafb9ae837bb33f1d9e63c5
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
17,348
lean
/- Copyright (c) 2020 Aaron Anderson, Jalex Stark, Kyle Miller. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Aaron Anderson, Jalex Stark, Kyle Miller, Alena Gusakov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.fintype.basic import Mathlib.data.sym2 import Mathlib.data.set.finite import Mathlib.PostPort universes u l namespace Mathlib /-! # Simple graphs This module defines simple graphs on a vertex type `V` as an irreflexive symmetric relation. There is a basic API for locally finite graphs and for graphs with finitely many vertices. ## Main definitions * `simple_graph` is a structure for symmetric, irreflexive relations * `neighbor_set` is the `set` of vertices adjacent to a given vertex * `neighbor_finset` is the `finset` of vertices adjacent to a given vertex, if `neighbor_set` is finite * `incidence_set` is the `set` of edges containing a given vertex * `incidence_finset` is the `finset` of edges containing a given vertex, if `incidence_set` is finite ## Implementation notes * A locally finite graph is one with instances `∀ v, fintype (G.neighbor_set v)`. * Given instances `decidable_rel G.adj` and `fintype V`, then the graph is locally finite, too. ## Naming Conventions * If the vertex type of a graph is finite, we refer to its cardinality as `card_verts`. TODO: This is the simplest notion of an unoriented graph. This should eventually fit into a more complete combinatorics hierarchy which includes multigraphs and directed graphs. We begin with simple graphs in order to start learning what the combinatorics hierarchy should look like. TODO: Part of this would include defining, for example, subgraphs of a simple graph. -/ /-- A simple graph is an irreflexive symmetric relation `adj` on a vertex type `V`. The relation describes which pairs of vertices are adjacent. There is exactly one edge for every pair of adjacent edges; see `simple_graph.edge_set` for the corresponding edge set. -/ structure simple_graph (V : Type u) where adj : V → V → Prop sym : autoParam (symmetric adj) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) loopless : autoParam (irreflexive adj) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) /-- Construct the simple graph induced by the given relation. It symmetrizes the relation and makes it irreflexive. -/ def simple_graph.from_rel {V : Type u} (r : V → V → Prop) : simple_graph V := simple_graph.mk fun (a b : V) => a ≠ b ∧ (r a b ∨ r b a) protected instance simple_graph.fintype {V : Type u} [fintype V] : fintype (simple_graph V) := fintype.of_injective simple_graph.adj simple_graph.ext @[simp] theorem simple_graph.from_rel_adj {V : Type u} (r : V → V → Prop) (v : V) (w : V) : simple_graph.adj (simple_graph.from_rel r) v w ↔ v ≠ w ∧ (r v w ∨ r w v) := iff.rfl /-- The complete graph on a type `V` is the simple graph with all pairs of distinct vertices adjacent. -/ def complete_graph (V : Type u) : simple_graph V := simple_graph.mk ne protected instance simple_graph.inhabited (V : Type u) : Inhabited (simple_graph V) := { default := complete_graph V } protected instance complete_graph_adj_decidable (V : Type u) [DecidableEq V] : DecidableRel (simple_graph.adj (complete_graph V)) := fun (v w : V) => not.decidable namespace simple_graph /-- `G.neighbor_set v` is the set of vertices adjacent to `v` in `G`. -/ def neighbor_set {V : Type u} (G : simple_graph V) (v : V) : set V := set_of (adj G v) protected instance neighbor_set.mem_decidable {V : Type u} (G : simple_graph V) (v : V) [DecidableRel (adj G)] : decidable_pred fun (_x : V) => _x ∈ neighbor_set G v := eq.mpr sorry fun (a : V) => set.decidable_mem (set_of (adj G v)) a theorem ne_of_adj {V : Type u} (G : simple_graph V) {a : V} {b : V} (hab : adj G a b) : a ≠ b := id fun (ᾰ : a = b) => Eq._oldrec (fun (hab : adj G a a) => loopless G a hab) ᾰ hab /-- The edges of G consist of the unordered pairs of vertices related by `G.adj`. -/ def edge_set {V : Type u} (G : simple_graph V) : set (sym2 V) := sym2.from_rel (sym G) /-- The `incidence_set` is the set of edges incident to a given vertex. -/ def incidence_set {V : Type u} (G : simple_graph V) (v : V) : set (sym2 V) := has_sep.sep (fun (e : sym2 V) => v ∈ e) (edge_set G) theorem incidence_set_subset {V : Type u} (G : simple_graph V) (v : V) : incidence_set G v ⊆ edge_set G := fun (_x : sym2 V) (h : _x ∈ incidence_set G v) => and.left h @[simp] theorem mem_edge_set {V : Type u} (G : simple_graph V) {v : V} {w : V} : quotient.mk (v, w) ∈ edge_set G ↔ adj G v w := iff.refl (quotient.mk (v, w) ∈ edge_set G) /-- Two vertices are adjacent iff there is an edge between them. The condition `v ≠ w` ensures they are different endpoints of the edge, which is necessary since when `v = w` the existential `∃ (e ∈ G.edge_set), v ∈ e ∧ w ∈ e` is satisfied by every edge incident to `v`. -/ theorem adj_iff_exists_edge {V : Type u} (G : simple_graph V) {v : V} {w : V} : adj G v w ↔ v ≠ w ∧ ∃ (e : sym2 V), ∃ (H : e ∈ edge_set G), v ∈ e ∧ w ∈ e := sorry theorem edge_other_ne {V : Type u} (G : simple_graph V) {e : sym2 V} (he : e ∈ edge_set G) {v : V} (h : v ∈ e) : sym2.mem.other h ≠ v := ne_of_adj G (eq.mp (Eq._oldrec (Eq.refl (quotient.mk (v, sym2.mem.other h) ∈ edge_set G)) sym2.eq_swap) (eq.mp (Eq._oldrec (Eq.refl (e ∈ edge_set G)) (Eq.symm (sym2.mem_other_spec h))) he)) protected instance edge_set_decidable_pred {V : Type u} (G : simple_graph V) [DecidableRel (adj G)] : decidable_pred (edge_set G) := sym2.from_rel.decidable_pred (sym G) protected instance edges_fintype {V : Type u} (G : simple_graph V) [DecidableEq V] [fintype V] [DecidableRel (adj G)] : fintype ↥(edge_set G) := subtype.fintype fun (x : sym2 V) => x ∈ edge_set G protected instance incidence_set_decidable_pred {V : Type u} (G : simple_graph V) [DecidableEq V] [DecidableRel (adj G)] (v : V) : decidable_pred (incidence_set G v) := fun (e : sym2 V) => and.decidable /-- The `edge_set` of the graph as a `finset`. -/ def edge_finset {V : Type u} (G : simple_graph V) [DecidableEq V] [fintype V] [DecidableRel (adj G)] : finset (sym2 V) := set.to_finset (edge_set G) @[simp] theorem mem_edge_finset {V : Type u} (G : simple_graph V) [DecidableEq V] [fintype V] [DecidableRel (adj G)] (e : sym2 V) : e ∈ edge_finset G ↔ e ∈ edge_set G := set.mem_to_finset @[simp] theorem edge_set_univ_card {V : Type u} (G : simple_graph V) [DecidableEq V] [fintype V] [DecidableRel (adj G)] : finset.card finset.univ = finset.card (edge_finset G) := fintype.card_of_subtype (edge_finset G) (mem_edge_finset G) @[simp] theorem irrefl {V : Type u} (G : simple_graph V) {v : V} : ¬adj G v v := loopless G v theorem edge_symm {V : Type u} (G : simple_graph V) (u : V) (v : V) : adj G u v ↔ adj G v u := { mp := fun (x : adj G u v) => sym G x, mpr := fun (x : adj G v u) => sym G x } @[simp] theorem mem_neighbor_set {V : Type u} (G : simple_graph V) (v : V) (w : V) : w ∈ neighbor_set G v ↔ adj G v w := iff.rfl @[simp] theorem mem_incidence_set {V : Type u} (G : simple_graph V) (v : V) (w : V) : quotient.mk (v, w) ∈ incidence_set G v ↔ adj G v w := sorry theorem mem_incidence_iff_neighbor {V : Type u} (G : simple_graph V) {v : V} {w : V} : quotient.mk (v, w) ∈ incidence_set G v ↔ w ∈ neighbor_set G v := sorry theorem adj_incidence_set_inter {V : Type u} (G : simple_graph V) {v : V} {e : sym2 V} (he : e ∈ edge_set G) (h : v ∈ e) : incidence_set G v ∩ incidence_set G (sym2.mem.other h) = singleton e := sorry /-- The set of common neighbors between two vertices `v` and `w` in a graph `G` is the intersection of the neighbor sets of `v` and `w`. -/ def common_neighbors {V : Type u} (G : simple_graph V) (v : V) (w : V) : set V := neighbor_set G v ∩ neighbor_set G w theorem common_neighbors_eq {V : Type u} (G : simple_graph V) (v : V) (w : V) : common_neighbors G v w = neighbor_set G v ∩ neighbor_set G w := rfl theorem mem_common_neighbors {V : Type u} (G : simple_graph V) {u : V} {v : V} {w : V} : u ∈ common_neighbors G v w ↔ adj G v u ∧ adj G w u := sorry theorem common_neighbors_symm {V : Type u} (G : simple_graph V) (v : V) (w : V) : common_neighbors G v w = common_neighbors G w v := sorry theorem not_mem_common_neighbors_left {V : Type u} (G : simple_graph V) (v : V) (w : V) : ¬v ∈ common_neighbors G v w := sorry theorem not_mem_common_neighbors_right {V : Type u} (G : simple_graph V) (v : V) (w : V) : ¬w ∈ common_neighbors G v w := sorry theorem common_neighbors_subset_neighbor_set {V : Type u} (G : simple_graph V) (v : V) (w : V) : common_neighbors G v w ⊆ neighbor_set G v := sorry /-- Given an edge incident to a particular vertex, get the other vertex on the edge. -/ def other_vertex_of_incident {V : Type u} (G : simple_graph V) [DecidableEq V] {v : V} {e : sym2 V} (h : e ∈ incidence_set G v) : V := sym2.mem.other' sorry theorem edge_mem_other_incident_set {V : Type u} (G : simple_graph V) [DecidableEq V] {v : V} {e : sym2 V} (h : e ∈ incidence_set G v) : e ∈ incidence_set G (other_vertex_of_incident G h) := sorry theorem incidence_other_prop {V : Type u} (G : simple_graph V) [DecidableEq V] {v : V} {e : sym2 V} (h : e ∈ incidence_set G v) : other_vertex_of_incident G h ∈ neighbor_set G v := sorry @[simp] theorem incidence_other_neighbor_edge {V : Type u} (G : simple_graph V) [DecidableEq V] {v : V} {w : V} (h : w ∈ neighbor_set G v) : other_vertex_of_incident G (iff.mpr (mem_incidence_iff_neighbor G) h) = w := iff.mp sym2.congr_right (sym2.mem_other_spec' (and.right (iff.mpr (mem_incidence_iff_neighbor G) h))) /-- There is an equivalence between the set of edges incident to a given vertex and the set of vertices adjacent to the vertex. -/ def incidence_set_equiv_neighbor_set {V : Type u} (G : simple_graph V) [DecidableEq V] (v : V) : ↥(incidence_set G v) ≃ ↥(neighbor_set G v) := equiv.mk (fun (e : ↥(incidence_set G v)) => { val := other_vertex_of_incident G sorry, property := sorry }) (fun (w : ↥(neighbor_set G v)) => { val := quotient.mk (v, subtype.val w), property := sorry }) sorry sorry /-! ## Finiteness at a vertex This section contains definitions and lemmas concerning vertices that have finitely many adjacent vertices. We denote this condition by `fintype (G.neighbor_set v)`. We define `G.neighbor_finset v` to be the `finset` version of `G.neighbor_set v`. Use `neighbor_finset_eq_filter` to rewrite this definition as a `filter`. -/ /-- `G.neighbors v` is the `finset` version of `G.adj v` in case `G` is locally finite at `v`. -/ def neighbor_finset {V : Type u} (G : simple_graph V) (v : V) [fintype ↥(neighbor_set G v)] : finset V := set.to_finset (neighbor_set G v) @[simp] theorem mem_neighbor_finset {V : Type u} (G : simple_graph V) (v : V) [fintype ↥(neighbor_set G v)] (w : V) : w ∈ neighbor_finset G v ↔ adj G v w := set.mem_to_finset /-- `G.degree v` is the number of vertices adjacent to `v`. -/ def degree {V : Type u} (G : simple_graph V) (v : V) [fintype ↥(neighbor_set G v)] : ℕ := finset.card (neighbor_finset G v) @[simp] theorem card_neighbor_set_eq_degree {V : Type u} (G : simple_graph V) (v : V) [fintype ↥(neighbor_set G v)] : fintype.card ↥(neighbor_set G v) = degree G v := Eq.symm (set.to_finset_card (neighbor_set G v)) theorem degree_pos_iff_exists_adj {V : Type u} (G : simple_graph V) (v : V) [fintype ↥(neighbor_set G v)] : 0 < degree G v ↔ ∃ (w : V), adj G v w := sorry protected instance incidence_set_fintype {V : Type u} (G : simple_graph V) (v : V) [fintype ↥(neighbor_set G v)] [DecidableEq V] : fintype ↥(incidence_set G v) := fintype.of_equiv (↥(neighbor_set G v)) (equiv.symm (incidence_set_equiv_neighbor_set G v)) /-- This is the `finset` version of `incidence_set`. -/ def incidence_finset {V : Type u} (G : simple_graph V) (v : V) [fintype ↥(neighbor_set G v)] [DecidableEq V] : finset (sym2 V) := set.to_finset (incidence_set G v) @[simp] theorem card_incidence_set_eq_degree {V : Type u} (G : simple_graph V) (v : V) [fintype ↥(neighbor_set G v)] [DecidableEq V] : fintype.card ↥(incidence_set G v) = degree G v := sorry @[simp] theorem mem_incidence_finset {V : Type u} (G : simple_graph V) (v : V) [fintype ↥(neighbor_set G v)] [DecidableEq V] (e : sym2 V) : e ∈ incidence_finset G v ↔ e ∈ incidence_set G v := set.mem_to_finset /-- A graph is locally finite if every vertex has a finite neighbor set. -/ def locally_finite {V : Type u} (G : simple_graph V) := (v : V) → fintype ↥(neighbor_set G v) /-- A locally finite simple graph is regular of degree `d` if every vertex has degree `d`. -/ def is_regular_of_degree {V : Type u} (G : simple_graph V) [locally_finite G] (d : ℕ) := ∀ (v : V), degree G v = d theorem is_regular_of_degree_eq {V : Type u} (G : simple_graph V) [locally_finite G] {d : ℕ} (h : is_regular_of_degree G d) (v : V) : degree G v = d := h v protected instance neighbor_set_fintype {V : Type u} (G : simple_graph V) [fintype V] [DecidableRel (adj G)] (v : V) : fintype ↥(neighbor_set G v) := subtype.fintype fun (x : V) => x ∈ neighbor_set G v theorem neighbor_finset_eq_filter {V : Type u} (G : simple_graph V) [fintype V] {v : V} [DecidableRel (adj G)] : neighbor_finset G v = finset.filter (adj G v) finset.univ := sorry @[simp] theorem complete_graph_degree {V : Type u} [fintype V] [DecidableEq V] (v : V) : degree (complete_graph V) v = fintype.card V - 1 := sorry theorem complete_graph_is_regular {V : Type u} [fintype V] [DecidableEq V] : is_regular_of_degree (complete_graph V) (fintype.card V - 1) := sorry /-- The minimum degree of all vertices -/ def min_degree {V : Type u} [fintype V] (G : simple_graph V) [Nonempty V] [DecidableRel (adj G)] : ℕ := finset.min' (finset.image (fun (v : V) => degree G v) finset.univ) sorry /-- The maximum degree of all vertices -/ def max_degree {V : Type u} [fintype V] (G : simple_graph V) [Nonempty V] [DecidableRel (adj G)] : ℕ := finset.max' (finset.image (fun (v : V) => degree G v) finset.univ) sorry /-! The following lemmas about `fintype.card` use noncomputable decidable instances to get fintype assumptions. -/ theorem degree_lt_card_verts {V : Type u} [fintype V] (G : simple_graph V) (v : V) : degree G v < fintype.card V := sorry theorem card_common_neighbors_le_degree_left {V : Type u} (G : simple_graph V) [fintype V] (v : V) (w : V) : fintype.card ↥(common_neighbors G v w) ≤ degree G v := sorry theorem card_common_neighbors_le_degree_right {V : Type u} (G : simple_graph V) [fintype V] (v : V) (w : V) : fintype.card ↥(common_neighbors G v w) ≤ degree G w := eq.mpr (id (Eq._oldrec (Eq.refl (fintype.card ↥(common_neighbors G v w) ≤ degree G w)) (common_neighbors_symm G v w))) (card_common_neighbors_le_degree_left G w v) theorem card_common_neighbors_lt_card_verts {V : Type u} (G : simple_graph V) [fintype V] (v : V) (w : V) : fintype.card ↥(common_neighbors G v w) < fintype.card V := nat.lt_of_le_of_lt (card_common_neighbors_le_degree_left G v w) (degree_lt_card_verts G v) /-- If the condition `G.adj v w` fails, then `card_common_neighbors_le_degree` is the best we can do in general. -/ theorem adj.card_common_neighbors_lt_degree {V : Type u} [fintype V] {G : simple_graph V} {v : V} {w : V} (h : adj G v w) : fintype.card ↥(common_neighbors G v w) < degree G v := sorry /-! ## Complement of a simple graph This section contains definitions and lemmas concerning the complement of a simple graph. -/ /-- We define `compl G` to be the `simple_graph V` such that no two adjacent vertices in `G` are adjacent in the complement, and every nonadjacent pair of vertices is adjacent (still ensuring that vertices are not adjacent to themselves.) -/ def compl {V : Type u} (G : simple_graph V) : simple_graph V := mk fun (v w : V) => v ≠ w ∧ ¬adj G v w protected instance has_compl {V : Type u} : has_compl (simple_graph V) := has_compl.mk compl @[simp] theorem compl_adj {V : Type u} (G : simple_graph V) (v : V) (w : V) : adj (Gᶜ) v w ↔ v ≠ w ∧ ¬adj G v w := iff.rfl @[simp] theorem compl_compl {V : Type u} (G : simple_graph V) : Gᶜᶜ = G := sorry @[simp] theorem compl_involutive {V : Type u} : function.involutive compl := compl_compl theorem compl_neighbor_set_disjoint {V : Type u} (G : simple_graph V) (v : V) : disjoint (neighbor_set G v) (neighbor_set (Gᶜ) v) := sorry theorem neighbor_set_union_compl_neighbor_set_eq {V : Type u} (G : simple_graph V) (v : V) : neighbor_set G v ∪ neighbor_set (Gᶜ) v = (singleton vᶜ) := sorry end Mathlib
8e207d8691617bcf4aecf2acb0d9041b0e4d657a
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/category_theory/full_subcategory.lean
13c62f3ce0eaccefb415bf243ce8352ab3b1c28a
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,703
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Reid Barton -/ import category_theory.fully_faithful namespace category_theory universes v u₁ u₂ -- morphism levels before object levels. See note [category_theory universes]. section induced /- Induced categories. Given a category D and a function F : C → D from a type C to the objects of D, there is an essentially unique way to give C a category structure such that F becomes a fully faithful functor, namely by taking Hom_C(X, Y) = Hom_D(FX, FY). We call this the category induced from D along F. As a special case, if C is a subtype of D, this produces the full subcategory of D on the objects belonging to C. In general the induced category is equivalent to the full subcategory of D on the image of F. -/ /- It looks odd to make D an explicit argument of `induced_category`, when it is determined by the argument F anyways. The reason to make D explicit is in order to control its syntactic form, so that instances like `induced_category.has_forget₂` (elsewhere) refer to the correct form of D. This is used to set up several algebraic categories like def CommMon : Type (u+1) := induced_category Mon (bundled.map @comm_monoid.to_monoid) -- not `induced_category (bundled monoid) (bundled.map @comm_monoid.to_monoid)`, -- even though `Mon = bundled monoid`! -/ variables {C : Type u₁} (D : Type u₂) [category.{v} D] variables (F : C → D) include F /-- `induced_category D F`, where `F : C → D`, is a typeclass synonym for `C`, which provides a category structure so that the morphisms `X ⟶ Y` are the morphisms in `D` from `F X` to `F Y`. -/ @[nolint has_inhabited_instance unused_arguments] def induced_category : Type u₁ := C variables {D} instance induced_category.has_coe_to_sort {α : Sort*} [has_coe_to_sort D α] : has_coe_to_sort (induced_category D F) α := ⟨λ c, ↥(F c)⟩ instance induced_category.category : category.{v} (induced_category D F) := { hom := λ X Y, F X ⟶ F Y, id := λ X, 𝟙 (F X), comp := λ _ _ _ f g, f ≫ g } /-- The forgetful functor from an induced category to the original category, forgetting the extra data. -/ @[simps] def induced_functor : induced_category D F ⥤ D := { obj := F, map := λ x y f, f } instance induced_category.full : full (induced_functor F) := { preimage := λ x y f, f } instance induced_category.faithful : faithful (induced_functor F) := {} end induced section full_subcategory /- A full subcategory is the special case of an induced category with F = subtype.val. -/ variables {C : Type u₂} [category.{v} C] variables (Z : C → Prop) /-- The category structure on a subtype; morphisms just ignore the property. See https://stacks.math.columbia.edu/tag/001D. We do not define 'strictly full' subcategories. -/ instance full_subcategory : category.{v} {X : C // Z X} := induced_category.category subtype.val /-- The forgetful functor from a full subcategory into the original category ("forgetting" the condition). -/ def full_subcategory_inclusion : {X : C // Z X} ⥤ C := induced_functor subtype.val @[simp] lemma full_subcategory_inclusion.obj {X} : (full_subcategory_inclusion Z).obj X = X.val := rfl @[simp] lemma full_subcategory_inclusion.map {X Y} {f : X ⟶ Y} : (full_subcategory_inclusion Z).map f = f := rfl instance full_subcategory.full : full (full_subcategory_inclusion Z) := induced_category.full subtype.val instance full_subcategory.faithful : faithful (full_subcategory_inclusion Z) := induced_category.faithful subtype.val end full_subcategory end category_theory
92cfe6363c5a4306a7f2457e844b6ac9abf5e9fc
64874bd1010548c7f5a6e3e8902efa63baaff785
/tests/lean/slow/list_elab2.lean
632e862b6620f68df3b399ee1127acf9908b41b9
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,584
lean
---------------------------------------------------------------------------------------------------- --- Copyright (c) 2014 Parikshit Khanna. All rights reserved. --- Released under Apache 2.0 license as described in the file LICENSE. --- Authors: Parikshit Khanna, Jeremy Avigad ---------------------------------------------------------------------------------------------------- -- Theory list -- =========== -- -- Basic properties of lists. import logic data.nat -- import congr open nat -- open congr open eq.ops eq inductive list (T : Type) : Type := nil {} : list T, cons : T → list T → list T definition refl := @eq.refl namespace list -- Type -- ---- infixr `::` := cons section variable {T : Type} theorem list_induction_on {P : list T → Prop} (l : list T) (Hnil : P nil) (Hind : forall x : T, forall l : list T, forall H : P l, P (cons x l)) : P l := list.rec Hnil Hind l theorem list_cases_on {P : list T → Prop} (l : list T) (Hnil : P nil) (Hcons : forall x : T, forall l : list T, P (cons x l)) : P l := list_induction_on l Hnil (take x l IH, Hcons x l) notation `[` l:(foldr `,` (h t, cons h t) nil) `]` := l -- Concat -- ------ definition concat (s t : list T) : list T := list.rec t (fun x : T, fun l : list T, fun u : list T, cons x u) s infixl `++` := concat theorem nil_concat (t : list T) : nil ++ t = t := refl _ theorem cons_concat (x : T) (s t : list T) : (x :: s) ++ t = x :: (s ++ t) := refl _ theorem concat_nil (t : list T) : t ++ nil = t := list_induction_on t (refl _) (take (x : T) (l : list T) (H : concat l nil = l), H ▸ (refl (cons x (concat l nil)))) attribute concat [reducible] theorem concat_nil2 (t : list T) : t ++ nil = t := list_induction_on t (refl _) (take (x : T) (l : list T) (H : concat l nil = l), -- H ▸ (refl (cons x (concat l nil)))) H ▸ (refl (concat (cons x l) nil))) theorem concat_assoc (s t u : list T) : s ++ t ++ u = s ++ (t ++ u) := list_induction_on s (refl _) (take x l, assume H : concat (concat l t) u = concat l (concat t u), H ▸ refl _) theorem concat_assoc2 (s t u : list T) : s ++ t ++ u = s ++ (t ++ u) := list_induction_on s (refl _) (take x l, assume H : concat (concat l t) u = concat l (concat t u), calc concat (concat (cons x l) t) u = cons x (concat (concat l t) u) : refl _ ... = concat (cons x l) (concat t u) : { H }) theorem concat_assoc3 (s t u : list T) : s ++ t ++ u = s ++ (t ++ u) := list_induction_on s (refl _) (take x l, assume H : concat (concat l t) u = concat l (concat t u), calc concat (concat (cons x l) t) u = cons x (concat l (concat t u)) : { H } ... = concat (cons x l) (concat t u) : refl _) theorem concat_assoc4 (s t u : list T) : s ++ t ++ u = s ++ (t ++ u) := list_induction_on s (refl _) (take x l, assume H : concat (concat l t) u = concat l (concat t u), calc concat (concat (cons x l) t) u = cons x (concat (concat l t) u) : refl _ ... = cons x (concat l (concat t u)) : { H } ... = concat (cons x l) (concat t u) : refl _) -- Length -- ------ definition length : list T → ℕ := list.rec 0 (fun x l m, succ m) -- TODO: cannot replace zero by 0 theorem length_nil : length (@nil T) = zero := refl _ theorem length_cons (x : T) (t : list T) : length (x :: t) = succ (length t) := refl _ theorem length_concat (s t : list T) : length (s ++ t) = length s + length t := list_induction_on s (calc length (concat nil t) = length t : refl _ ... = 0 + length t : {symm !zero_add} ... = length (@nil T) + length t : refl _) (take x s, assume H : length (concat s t) = length s + length t, calc length (concat (cons x s) t ) = succ (length (concat s t)) : refl _ ... = succ (length s + length t) : { H } ... = succ (length s) + length t : {symm !add.succ_left} ... = length (cons x s) + length t : refl _) -- Reverse -- ------- definition reverse : list T → list T := list.rec nil (fun x l r, r ++ [x]) theorem reverse_nil : reverse (@nil T) = nil := refl _ theorem reverse_cons (x : T) (l : list T) : reverse (x :: l) = (reverse l) ++ (cons x nil) := refl _ -- opaque_hint (hiding reverse) theorem reverse_concat (s t : list T) : reverse (s ++ t) = (reverse t) ++ (reverse s) := list_induction_on s (calc reverse (concat nil t) = reverse t : { nil_concat _ } ... = concat (reverse t) nil : symm (concat_nil _) ... = concat (reverse t) (reverse nil) : {symm (reverse_nil)}) (take x l, assume H : reverse (concat l t) = concat (reverse t) (reverse l), calc reverse (concat (cons x l) t) = concat (reverse (concat l t)) (cons x nil) : refl _ ... = concat (concat (reverse t) (reverse l)) (cons x nil) : { H } ... = concat (reverse t) (concat (reverse l) (cons x nil)) : concat_assoc _ _ _ ... = concat (reverse t) (reverse (cons x l)) : refl _) -- -- add_rewrite length_nil length_cons theorem reverse_reverse (l : list T) : reverse (reverse l) = l := list_induction_on l (refl _) (take x l', assume H: reverse (reverse l') = l', show reverse (reverse (cons x l')) = cons x l', from calc reverse (reverse (cons x l')) = concat (reverse (cons x nil)) (reverse (reverse l')) : {reverse_concat _ _} ... = cons x l' : {H}) -- Append -- ------ -- TODO: define reverse from append definition append (x : T) : list T → list T := list.rec (x :: nil) (fun y l l', y :: l') theorem append_nil (x : T) : append x nil = [x] := refl _ theorem append_cons (x : T) (y : T) (l : list T) : append x (y :: l) = y :: (append x l) := refl _ theorem append_eq_concat (x : T) (l : list T) : append x l = l ++ [x] := list_induction_on l (refl _) (take y l, assume P : append x l = concat l [x], P ▸ refl _) theorem append_eq_reverse_cons (x : T) (l : list T) : append x l = reverse (x :: reverse l) := list_induction_on l (calc append x nil = [x] : (refl _) ... = concat nil [x] : {symm (nil_concat _)} ... = concat (reverse nil) [x] : {symm (reverse_nil)} ... = reverse [x] : {symm (reverse_cons _ _)} ... = reverse (x :: (reverse nil)) : {symm (reverse_nil)}) (take y l', assume H : append x l' = reverse (x :: reverse l'), calc append x (y :: l') = (y :: l') ++ [ x ] : append_eq_concat _ _ ... = concat (reverse (reverse (y :: l'))) [ x ] : {symm (reverse_reverse _)} ... = reverse (x :: (reverse (y :: l'))) : refl _) end end list
00dabdb41d1d6fbe0c21c5c844c3a2d240b078ac
f7315930643edc12e76c229a742d5446dad77097
/hott/types/pi.hlean
3d69832e00ffac8ec9fd8139e6d247019ee849b5
[ "Apache-2.0" ]
permissive
bmalehorn/lean
8f77b762a76c59afff7b7403f9eb5fc2c3ce70c1
53653c352643751c4b62ff63ec5e555f11dae8eb
refs/heads/master
1,610,945,684,489
1,429,681,220,000
1,429,681,449,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,406
hlean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: types.pi Author: Floris van Doorn Ported from Coq HoTT Theorems about pi-types (dependent function spaces) -/ import types.sigma open eq equiv is_equiv funext namespace pi variables {A A' : Type} {B : A → Type} {B' : A' → Type} {C : Πa, B a → Type} {D : Πa b, C a b → Type} {a a' a'' : A} {b b₁ b₂ : B a} {b' : B a'} {b'' : B a''} {f g : Πa, B a} /- Paths -/ /- Paths [p : f ≈ g] in a function type [Πx:X, P x] are equivalent to functions taking values in path types, [H : Πx:X, f x ≈ g x], or concisely, [H : f ∼ g]. This equivalence, however, is just the combination of [apD10] and function extensionality [funext], and as such, [path_forall], et seq. are given in axioms.funext and path: -/ /- Now we show how these things compute. -/ definition apD10_eq_of_homotopy (h : f ∼ g) : apD10 (eq_of_homotopy h) ∼ h := apD10 (retr apD10 h) definition eq_of_homotopy_eta (p : f = g) : eq_of_homotopy (apD10 p) = p := sect apD10 p definition eq_of_homotopy_idp (f : Πa, B a) : eq_of_homotopy (λx : A, refl (f x)) = refl f := !eq_of_homotopy_eta /- The identification of the path space of a dependent function space, up to equivalence, is of course just funext. -/ definition eq_equiv_homotopy (f g : Πx, B x) : (f = g) ≃ (f ∼ g) := equiv.mk _ !is_equiv_apD definition is_equiv_eq_of_homotopy [instance] (f g : Πx, B x) : is_equiv (@eq_of_homotopy _ _ f g) := is_equiv_inv apD10 definition homotopy_equiv_eq (f g : Πx, B x) : (f ∼ g) ≃ (f = g) := equiv.mk _ !is_equiv_eq_of_homotopy /- Transport -/ definition pi_transport (p : a = a') (f : Π(b : B a), C a b) : (transport (λa, Π(b : B a), C a b) p f) ∼ (λb, transport (C a') !tr_inv_tr (transportD _ _ p _ (f (p⁻¹ ▹ b)))) := eq.rec_on p (λx, idp) /- A special case of [transport_pi] where the type [B] does not depend on [A], and so it is just a fixed type [B]. -/ definition pi_transport_constant {C : A → A' → Type} (p : a = a') (f : Π(b : A'), C a b) : Π(b : A'), (transport (λa, Π(b : A'), C a b) p f) b = transport (λa, C a b) p (f b) := eq.rec_on p (λx, idp) /- Maps on paths -/ /- The action of maps given by lambda. -/ definition ap_lambdaD {C : A' → Type} (p : a = a') (f : Πa b, C b) : ap (λa b, f a b) p = eq_of_homotopy (λb, ap (λa, f a b) p) := begin apply (eq.rec_on p), apply inverse, apply eq_of_homotopy_idp end /- Dependent paths -/ /- with more implicit arguments the conclusion of the following theorem is (Π(b : B a), transportD B C p b (f b) = g (transport B p b)) ≃ (transport (λa, Π(b : B a), C a b) p f = g) -/ definition heq_piD (p : a = a') (f : Π(b : B a), C a b) (g : Π(b' : B a'), C a' b') : (Π(b : B a), p ▹D (f b) = g (p ▹ b)) ≃ (p ▹ f = g) := eq.rec_on p (λg, !homotopy_equiv_eq) g definition heq_pi {C : A → Type} (p : a = a') (f : Π(b : B a), C a) (g : Π(b' : B a'), C a') : (Π(b : B a), p ▹ (f b) = g (p ▹ b)) ≃ (p ▹ f = g) := eq.rec_on p (λg, !homotopy_equiv_eq) g section open sigma sigma.ops /- more implicit arguments: (Π(b : B a), transport C (sigma_eq p idp) (f b) = g (p ▹ b)) ≃ (Π(b : B a), transportD B (λ(a : A) (b : B a), C ⟨a, b⟩) p b (f b) = g (transport B p b)) -/ definition heq_pi_sigma {C : (Σa, B a) → Type} (p : a = a') (f : Π(b : B a), C ⟨a, b⟩) (g : Π(b' : B a'), C ⟨a', b'⟩) : (Π(b : B a), (sigma_eq p idp) ▹ (f b) = g (p ▹ b)) ≃ (Π(b : B a), p ▹D (f b) = g (p ▹ b)) := eq.rec_on p (λg, !equiv.refl) g end /- Functorial action -/ variables (f0 : A' → A) (f1 : Π(a':A'), B (f0 a') → B' a') /- The functoriality of [forall] is slightly subtle: it is contravariant in the domain type and covariant in the codomain, but the codomain is dependent on the domain. -/ definition pi_functor : (Π(a:A), B a) → (Π(a':A'), B' a') := (λg a', f1 a' (g (f0 a'))) definition ap_pi_functor {g g' : Π(a:A), B a} (h : g ∼ g') : ap (pi_functor f0 f1) (eq_of_homotopy h) = eq_of_homotopy (λa':A', (ap (f1 a') (h (f0 a')))) := begin apply (equiv_rect (@apD10 A B g g')), intro p, clear h, cases p, apply concat, exact (ap (ap (pi_functor f0 f1)) (eq_of_homotopy_idp g)), apply symm, apply eq_of_homotopy_idp end /- Equivalences -/ definition is_equiv_pi_functor [instance] [H0 : is_equiv f0] [H1 : Πa', @is_equiv (B (f0 a')) (B' a') (f1 a')] : is_equiv (pi_functor f0 f1) := begin apply (adjointify (pi_functor f0 f1) (pi_functor f0⁻¹ (λ(a : A) (b' : B' (f0⁻¹ a)), transport B (retr f0 a) ((f1 (f0⁻¹ a))⁻¹ b')))), intro h, apply eq_of_homotopy, unfold pi_functor, unfold function.compose, unfold function.id, begin intro a', apply (tr_inv _ (adj f0 a')), apply (transport (λx, f1 a' x = h a') (transport_compose B f0 (sect f0 a') _)), apply (tr_inv (λx, x = h a') (fn_tr_eq_tr_fn _ f1 _)), unfold function.compose, apply (tr_inv (λx, sect f0 a' ▹ x = h a') (retr (f1 _) _)), unfold function.id, apply apD end, begin intro h, apply eq_of_homotopy, intro a, apply (tr_inv (λx, retr f0 a ▹ x = h a) (sect (f1 _) _)), unfold function.id, apply apD end end definition pi_equiv_pi_of_is_equiv [H : is_equiv f0] [H1 : Πa', @is_equiv (B (f0 a')) (B' a') (f1 a')] : (Πa, B a) ≃ (Πa', B' a') := equiv.mk (pi_functor f0 f1) _ definition pi_equiv_pi (f0 : A' ≃ A) (f1 : Πa', (B (to_fun f0 a') ≃ B' a')) : (Πa, B a) ≃ (Πa', B' a') := pi_equiv_pi_of_is_equiv (to_fun f0) (λa', to_fun (f1 a')) definition pi_equiv_pi_id {P Q : A → Type} (g : Πa, P a ≃ Q a) : (Πa, P a) ≃ (Πa, Q a) := pi_equiv_pi equiv.refl g /- Truncatedness: any dependent product of n-types is an n-type -/ open is_trunc definition is_trunc_pi [instance] (B : A → Type) (n : trunc_index) [H : ∀a, is_trunc n (B a)] : is_trunc n (Πa, B a) := begin reverts [B, H], apply (trunc_index.rec_on n), {intros [B, H], fapply is_contr.mk, intro a, apply center, intro f, apply eq_of_homotopy, intro x, apply (contr (f x))}, {intros [n, IH, B, H], fapply is_trunc_succ_intro, intros [f, g], fapply is_trunc_equiv_closed, apply equiv.symm, apply eq_equiv_homotopy, apply IH, intro a, show is_trunc n (f a = g a), from is_trunc_eq n (f a) (g a)} end definition is_trunc_eq_pi [instance] (n : trunc_index) (f g : Πa, B a) [H : ∀a, is_trunc n (f a = g a)] : is_trunc n (f = g) := begin apply is_trunc_equiv_closed, apply equiv.symm, apply eq_equiv_homotopy end /- Symmetry of Π -/ definition is_equiv_flip [instance] {P : A → A' → Type} : is_equiv (@function.flip _ _ P) := begin fapply is_equiv.mk, exact (@function.flip _ _ (function.flip P)), repeat (intro f; apply idp) end definition pi_comm_equiv {P : A → A' → Type} : (Πa b, P a b) ≃ (Πb a, P a b) := equiv.mk (@function.flip _ _ P) _ end pi attribute pi.is_trunc_pi [instance]
93ce4a80a43678b51f659f0704219039f05e006c
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/ring_theory/ideal_operations.lean
2922524c94f7f1ac200956fdf5df75f1a55aa311
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
24,790
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau More operations on modules and ideals. -/ import ring_theory.ideals data.nat.choose order.zorn import linear_algebra.tensor_product import data.equiv.algebra import ring_theory.algebra_operations universes u v w x open lattice namespace submodule variables {R : Type u} {M : Type v} variables [comm_ring R] [add_comm_group M] [module R M] instance has_scalar' : has_scalar (ideal R) (submodule R M) := ⟨λ I N, ⨆ r : I, N.map (r.1 • linear_map.id)⟩ def annihilator (N : submodule R M) : ideal R := (linear_map.lsmul R N).ker def colon (N P : submodule R M) : ideal R := annihilator (P.map N.mkq) variables {I J : ideal R} {N N₁ N₂ P P₁ P₂ : submodule R M} theorem mem_annihilator {r} : r ∈ N.annihilator ↔ ∀ n ∈ N, r • n = (0:M) := ⟨λ hr n hn, congr_arg subtype.val (linear_map.ext_iff.1 (linear_map.mem_ker.1 hr) ⟨n, hn⟩), λ h, linear_map.mem_ker.2 $ linear_map.ext $ λ n, subtype.eq $ h n.1 n.2⟩ theorem mem_annihilator' {r} : r ∈ N.annihilator ↔ N ≤ comap (r • linear_map.id) ⊥ := mem_annihilator.trans ⟨λ H n hn, (mem_bot R).2 $ H n hn, λ H n hn, (mem_bot R).1 $ H hn⟩ theorem annihilator_bot : (⊥ : submodule R M).annihilator = ⊤ := (ideal.eq_top_iff_one _).2 $ mem_annihilator'.2 bot_le theorem annihilator_eq_top_iff : N.annihilator = ⊤ ↔ N = ⊥ := ⟨λ H, eq_bot_iff.2 $ λ (n:M) hn, (mem_bot R).2 $ one_smul R n ▸ mem_annihilator.1 ((ideal.eq_top_iff_one _).1 H) n hn, λ H, H.symm ▸ annihilator_bot⟩ theorem annihilator_mono (h : N ≤ P) : P.annihilator ≤ N.annihilator := λ r hrp, mem_annihilator.2 $ λ n hn, mem_annihilator.1 hrp n $ h hn theorem annihilator_supr (ι : Type w) (f : ι → submodule R M) : (annihilator ⨆ i, f i) = ⨅ i, annihilator (f i) := le_antisymm (le_infi $ λ i, annihilator_mono $ le_supr _ _) (λ r H, mem_annihilator'.2 $ supr_le $ λ i, have _ := (mem_infi _).1 H i, mem_annihilator'.1 this) theorem mem_colon {r} : r ∈ N.colon P ↔ ∀ p ∈ P, r • p ∈ N := mem_annihilator.trans ⟨λ H p hp, (quotient.mk_eq_zero N).1 (H (quotient.mk p) (mem_map_of_mem hp)), λ H m ⟨p, hp, hpm⟩, hpm ▸ (N.mkq).map_smul r p ▸ (quotient.mk_eq_zero N).2 $ H p hp⟩ theorem mem_colon' {r} : r ∈ N.colon P ↔ P ≤ comap (r • linear_map.id) N := mem_colon theorem colon_mono (hn : N₁ ≤ N₂) (hp : P₁ ≤ P₂) : N₁.colon P₂ ≤ N₂.colon P₁ := λ r hrnp, mem_colon.2 $ λ p₁ hp₁, hn $ mem_colon.1 hrnp p₁ $ hp hp₁ theorem infi_colon_supr (ι₁ : Type w) (f : ι₁ → submodule R M) (ι₂ : Type x) (g : ι₂ → submodule R M) : (⨅ i, f i).colon (⨆ j, g j) = ⨅ i j, (f i).colon (g j) := le_antisymm (le_infi $ λ i, le_infi $ λ j, colon_mono (infi_le _ _) (le_supr _ _)) (λ r H, mem_colon'.2 $ supr_le $ λ j, map_le_iff_le_comap.1 $ le_infi $ λ i, map_le_iff_le_comap.2 $ mem_colon'.1 $ have _ := ((mem_infi _).1 H i), have _ := ((mem_infi _).1 this j), this) theorem smul_mem_smul {r} {n} (hr : r ∈ I) (hn : n ∈ N) : r • n ∈ I • N := (le_supr _ ⟨r, hr⟩ : _ ≤ I • N) ⟨n, hn, rfl⟩ theorem smul_le {P : submodule R M} : I • N ≤ P ↔ ∀ (r ∈ I) (n ∈ N), r • n ∈ P := ⟨λ H r hr n hn, H $ smul_mem_smul hr hn, λ H, supr_le $ λ r, map_le_iff_le_comap.2 $ λ n hn, H r.1 r.2 n hn⟩ @[elab_as_eliminator] theorem smul_induction_on {p : M → Prop} {x} (H : x ∈ I • N) (Hb : ∀ (r ∈ I) (n ∈ N), p (r • n)) (H0 : p 0) (H1 : ∀ x y, p x → p y → p (x + y)) (H2 : ∀ (c:R) n, p n → p (c • n)) : p x := (@smul_le _ _ _ _ _ _ _ ⟨p, H0, H1, H2⟩).2 Hb H theorem mem_smul_span_singleton {I : ideal R} {m : M} {x : M} : x ∈ I • span R ({m} : set M) ↔ ∃ y ∈ I, y • m = x := ⟨λ hx, smul_induction_on hx (λ r hri n hnm, let ⟨s, hs⟩ := mem_span_singleton.1 hnm in ⟨r * s, I.mul_mem_right hri, hs ▸ mul_smul r s m⟩) ⟨0, I.zero_mem, by rw [zero_smul]⟩ (λ m1 m2 ⟨y1, hyi1, hy1⟩ ⟨y2, hyi2, hy2⟩, ⟨y1 + y2, I.add_mem hyi1 hyi2, by rw [add_smul, hy1, hy2]⟩) (λ c r ⟨y, hyi, hy⟩, ⟨c * y, I.mul_mem_left hyi, by rw [mul_smul, hy]⟩), λ ⟨y, hyi, hy⟩, hy ▸ smul_mem_smul hyi (subset_span $ set.mem_singleton m)⟩ theorem smul_le_right : I • N ≤ N := smul_le.2 $ λ r hr n, N.smul_mem r theorem smul_mono (hij : I ≤ J) (hnp : N ≤ P) : I • N ≤ J • P := smul_le.2 $ λ r hr n hn, smul_mem_smul (hij hr) (hnp hn) theorem smul_mono_left (h : I ≤ J) : I • N ≤ J • N := smul_mono h (le_refl N) theorem smul_mono_right (h : N ≤ P) : I • N ≤ I • P := smul_mono (le_refl I) h variables (I J N P) @[simp] theorem smul_bot : I • (⊥ : submodule R M) = ⊥ := eq_bot_iff.2 $ smul_le.2 $ λ r hri s hsb, (submodule.mem_bot R).2 $ ((submodule.mem_bot R).1 hsb).symm ▸ smul_zero r @[simp] theorem bot_smul : (⊥ : ideal R) • N = ⊥ := eq_bot_iff.2 $ smul_le.2 $ λ r hrb s hsi, (submodule.mem_bot R).2 $ ((submodule.mem_bot R).1 hrb).symm ▸ zero_smul _ s @[simp] theorem top_smul : (⊤ : ideal R) • N = N := le_antisymm smul_le_right $ λ r hri, one_smul R r ▸ smul_mem_smul mem_top hri theorem smul_sup : I • (N ⊔ P) = I • N ⊔ I • P := le_antisymm (smul_le.2 $ λ r hri m hmnp, let ⟨n, hn, p, hp, hnpm⟩ := mem_sup.1 hmnp in mem_sup.2 ⟨_, smul_mem_smul hri hn, _, smul_mem_smul hri hp, hnpm ▸ (smul_add _ _ _).symm⟩) (sup_le (smul_mono_right le_sup_left) (smul_mono_right le_sup_right)) theorem sup_smul : (I ⊔ J) • N = I • N ⊔ J • N := le_antisymm (smul_le.2 $ λ r hrij n hn, let ⟨ri, hri, rj, hrj, hrijr⟩ := mem_sup.1 hrij in mem_sup.2 ⟨_, smul_mem_smul hri hn, _, smul_mem_smul hrj hn, hrijr ▸ (add_smul _ _ _).symm⟩) (sup_le (smul_mono_left le_sup_left) (smul_mono_left le_sup_right)) theorem smul_assoc : (I • J) • N = I • (J • N) := le_antisymm (smul_le.2 $ λ rs hrsij t htn, smul_induction_on hrsij (λ r hr s hs, (@smul_eq_mul R _ r s).symm ▸ smul_smul _ r s t ▸ smul_mem_smul hr (smul_mem_smul hs htn)) ((zero_smul R t).symm ▸ submodule.zero_mem _) (λ x y, (add_smul x y t).symm ▸ submodule.add_mem _) (λ r s h, (@smul_eq_mul R _ r s).symm ▸ smul_smul _ r s t ▸ submodule.smul_mem _ _ h)) (smul_le.2 $ λ r hr sn hsn, suffices J • N ≤ submodule.comap (r • linear_map.id) ((I • J) • N), from this hsn, smul_le.2 $ λ s hs n hn, show r • (s • n) ∈ (I • J) • N, from mul_smul r s n ▸ smul_mem_smul (smul_mem_smul hr hs) hn) variables (S : set R) (T : set M) theorem span_smul_span : (ideal.span S) • (span R T) = span R (⋃ (s ∈ S) (t ∈ T), {s • t}) := le_antisymm (smul_le.2 $ λ r hrS n hnT, span_induction hrS (λ r hrS, span_induction hnT (λ n hnT, subset_span $ set.mem_bUnion hrS $ set.mem_bUnion hnT $ set.mem_singleton _) ((smul_zero r : r • 0 = (0:M)).symm ▸ submodule.zero_mem _) (λ x y, (smul_add r x y).symm ▸ submodule.add_mem _) (λ c m, by rw [smul_smul, mul_comm, mul_smul]; exact submodule.smul_mem _ _)) ((zero_smul R n).symm ▸ submodule.zero_mem _) (λ r s, (add_smul r s n).symm ▸ submodule.add_mem _) (λ c r, by rw [smul_eq_mul, mul_smul]; exact submodule.smul_mem _ _)) $ span_le.2 $ set.bUnion_subset $ λ r hrS, set.bUnion_subset $ λ n hnT, set.singleton_subset_iff.2 $ smul_mem_smul (subset_span hrS) (subset_span hnT) end submodule namespace ideal section chinese_remainder variables {R : Type u} [comm_ring R] {ι : Type v} theorem exists_sub_one_mem_and_mem (s : finset ι) {f : ι → ideal R} (hf : ∀ i ∈ s, ∀ j ∈ s, i ≠ j → f i ⊔ f j = ⊤) (i : ι) (his : i ∈ s) : ∃ r : R, r - 1 ∈ f i ∧ ∀ j ∈ s, j ≠ i → r ∈ f j := begin have : ∀ j ∈ s, j ≠ i → ∃ r : R, ∃ H : r - 1 ∈ f i, r ∈ f j, { intros j hjs hji, specialize hf i his j hjs hji.symm, rw [eq_top_iff_one, submodule.mem_sup] at hf, rcases hf with ⟨r, hri, s, hsj, hrs⟩, refine ⟨1 - r, _, _⟩, { rw [sub_right_comm, sub_self, zero_sub], exact (f i).neg_mem hri }, { rw [← hrs, add_sub_cancel'], exact hsj } }, classical, have : ∃ g : ι → R, (∀ j, g j - 1 ∈ f i) ∧ ∀ j ∈ s, j ≠ i → g j ∈ f j, { choose g hg1 hg2, refine ⟨λ j, if H : j ∈ s ∧ j ≠ i then g j H.1 H.2 else 1, λ j, _, λ j, _⟩, { split_ifs with h, { apply hg1 }, rw sub_self, exact (f i).zero_mem }, { intros hjs hji, rw dif_pos, { apply hg2 }, exact ⟨hjs, hji⟩ } }, rcases this with ⟨g, hgi, hgj⟩, use (s.erase i).prod g, split, { rw [← quotient.eq, quotient.mk_one, ← finset.prod_hom (quotient.mk (f i))], apply finset.prod_eq_one, intros, rw [← quotient.mk_one, quotient.eq], apply hgi }, intros j hjs hji, rw [← quotient.eq_zero_iff_mem, ← finset.prod_hom (quotient.mk (f j))], refine finset.prod_eq_zero (finset.mem_erase_of_ne_of_mem hji hjs) _, rw quotient.eq_zero_iff_mem, exact hgj j hjs hji end theorem exists_sub_mem [fintype ι] {f : ι → ideal R} (hf : ∀ i j, i ≠ j → f i ⊔ f j = ⊤) (g : ι → R) : ∃ r : R, ∀ i, r - g i ∈ f i := begin have : ∃ φ : ι → R, (∀ i, φ i - 1 ∈ f i) ∧ (∀ i j, i ≠ j → φ i ∈ f j), { have := exists_sub_one_mem_and_mem (finset.univ : finset ι) (λ i _ j _ hij, hf i j hij), choose φ hφ, use λ i, φ i (finset.mem_univ i), exact ⟨λ i, (hφ i _).1, λ i j hij, (hφ i _).2 j (finset.mem_univ j) hij.symm⟩ }, rcases this with ⟨φ, hφ1, hφ2⟩, use finset.univ.sum (λ i, g i * φ i), intros i, rw [← quotient.eq, ← finset.sum_hom (quotient.mk (f i))], refine eq.trans (finset.sum_eq_single i _ _) _, { intros j _ hji, rw quotient.eq_zero_iff_mem, exact (f i).mul_mem_left (hφ2 j i hji) }, { intros hi, exact (hi $ finset.mem_univ i).elim }, specialize hφ1 i, rw [← quotient.eq, quotient.mk_one] at hφ1, rw [quotient.mk_mul, hφ1, mul_one] end def quotient_inf_to_pi_quotient (f : ι → ideal R) : (⨅ i, f i).quotient → Π i, (f i).quotient := @@quotient.lift _ _ (⨅ i, f i) (λ r i, ideal.quotient.mk (f i) r) (@pi.is_ring_hom_pi ι (λ i, (f i).quotient) _ R _ _ _) (λ r hr, funext $ λ i, quotient.eq_zero_iff_mem.2 $ (submodule.mem_infi _).1 hr i) theorem is_ring_hom_quotient_inf_to_pi_quotient (f : ι → ideal R) : is_ring_hom (quotient_inf_to_pi_quotient f) := @@quotient.is_ring_hom _ _ _ (@pi.is_ring_hom_pi ι (λ i, (f i).quotient) _ R _ _ _) _ theorem bijective_quotient_inf_to_pi_quotient [fintype ι] {f : ι → ideal R} (hf : ∀ i j, i ≠ j → f i ⊔ f j = ⊤) : function.bijective (quotient_inf_to_pi_quotient f) := ⟨λ x y, quotient.induction_on₂' x y $ λ r s hrs, quotient.eq.2 $ (submodule.mem_infi _).2 $ λ i, quotient.eq.1 $ show quotient_inf_to_pi_quotient f (quotient.mk' r) i = _, by rw hrs; refl, λ g, let ⟨r, hr⟩ := exists_sub_mem hf (λ i, quotient.out' (g i)) in ⟨quotient.mk _ r, funext $ λ i, quotient.out_eq' (g i) ▸ quotient.eq.2 (hr i)⟩⟩ /-- Chinese Remainder Theorem. Eisenbud Ex.2.6. Similar to Atiyah-Macdonald 1.10 and Stacks 00DT -/ noncomputable def quotient_inf_ring_equiv_pi_quotient [fintype ι] (f : ι → ideal R) (hf : ∀ i j, i ≠ j → f i ⊔ f j = ⊤) : (⨅ i, f i).quotient ≃r Π i, (f i).quotient := { hom := is_ring_hom_quotient_inf_to_pi_quotient f, .. equiv.of_bijective (bijective_quotient_inf_to_pi_quotient hf) } end chinese_remainder section mul_and_radical variables {R : Type u} [comm_ring R] variables {I J K L: ideal R} instance : has_mul (ideal R) := ⟨(•)⟩ theorem mul_mem_mul {r s} (hr : r ∈ I) (hs : s ∈ J) : r * s ∈ I * J := submodule.smul_mem_smul hr hs theorem mul_mem_mul_rev {r s} (hr : r ∈ I) (hs : s ∈ J) : s * r ∈ I * J := mul_comm r s ▸ mul_mem_mul hr hs theorem mul_le : I * J ≤ K ↔ ∀ (r ∈ I) (s ∈ J), r * s ∈ K := submodule.smul_le variables (I J K) protected theorem mul_comm : I * J = J * I := le_antisymm (mul_le.2 $ λ r hrI s hsJ, mul_mem_mul_rev hsJ hrI) (mul_le.2 $ λ r hrJ s hsI, mul_mem_mul_rev hsI hrJ) protected theorem mul_assoc : (I * J) * K = I * (J * K) := submodule.smul_assoc I J K theorem span_mul_span (S T : set R) : span S * span T = span ⋃ (s ∈ S) (t ∈ T), {s * t} := submodule.span_smul_span S T variables {I J K} theorem mul_le_inf : I * J ≤ I ⊓ J := mul_le.2 $ λ r hri s hsj, ⟨I.mul_mem_right hri, J.mul_mem_left hsj⟩ theorem mul_eq_inf_of_coprime (h : I ⊔ J = ⊤) : I * J = I ⊓ J := le_antisymm mul_le_inf $ λ r ⟨hri, hrj⟩, let ⟨s, hsi, t, htj, hst⟩ := submodule.mem_sup.1 ((eq_top_iff_one _).1 h) in mul_one r ▸ hst ▸ (mul_add r s t).symm ▸ ideal.add_mem (I * J) (mul_mem_mul_rev hsi hrj) (mul_mem_mul hri htj) variables (I) theorem mul_bot : I * ⊥ = ⊥ := submodule.smul_bot I theorem bot_mul : ⊥ * I = ⊥ := submodule.bot_smul I theorem mul_top : I * ⊤ = I := ideal.mul_comm ⊤ I ▸ submodule.top_smul I theorem top_mul : ⊤ * I = I := submodule.top_smul I variables {I} theorem mul_mono (hik : I ≤ K) (hjl : J ≤ L) : I * J ≤ K * L := submodule.smul_mono hik hjl theorem mul_mono_left (h : I ≤ J) : I * K ≤ J * K := submodule.smul_mono_left h theorem mul_mono_right (h : J ≤ K) : I * J ≤ I * K := submodule.smul_mono_right h variables (I J K) theorem mul_sup : I * (J ⊔ K) = I * J ⊔ I * K := submodule.smul_sup I J K theorem sup_mul : (I ⊔ J) * K = I * K ⊔ J * K := submodule.sup_smul I J K variables {I J K} def radical (I : ideal R) : ideal R := { carrier := { r | ∃ n : ℕ, r ^ n ∈ I }, zero := ⟨1, (pow_one (0:R)).symm ▸ I.zero_mem⟩, add := λ x y ⟨m, hxmi⟩ ⟨n, hyni⟩, ⟨m + n, (add_pow x y (m + n)).symm ▸ I.sum_mem $ show ∀ c ∈ finset.range (nat.succ (m + n)), x ^ c * y ^ (m + n - c) * (nat.choose (m + n) c) ∈ I, from λ c hc, or.cases_on (le_total c m) (λ hcm, I.mul_mem_right $ I.mul_mem_left $ nat.add_comm n m ▸ (nat.add_sub_assoc hcm n).symm ▸ (pow_add y n (m-c)).symm ▸ I.mul_mem_right hyni) (λ hmc, I.mul_mem_right $ I.mul_mem_right $ nat.add_sub_cancel' hmc ▸ (pow_add x m (c-m)).symm ▸ I.mul_mem_right hxmi)⟩, smul := λ r s ⟨n, hsni⟩, ⟨n, show (r * s)^n ∈ I, from (mul_pow r s n).symm ▸ I.mul_mem_left hsni⟩ } theorem le_radical : I ≤ radical I := λ r hri, ⟨1, (pow_one r).symm ▸ hri⟩ variables (R) theorem radical_top : (radical ⊤ : ideal R) = ⊤ := (eq_top_iff_one _).2 ⟨0, submodule.mem_top⟩ variables {R} theorem radical_mono (H : I ≤ J) : radical I ≤ radical J := λ r ⟨n, hrni⟩, ⟨n, H hrni⟩ variables (I) theorem radical_idem : radical (radical I) = radical I := le_antisymm (λ r ⟨n, k, hrnki⟩, ⟨n * k, (pow_mul r n k).symm ▸ hrnki⟩) le_radical variables {I} theorem radical_eq_top : radical I = ⊤ ↔ I = ⊤ := ⟨λ h, (eq_top_iff_one _).2 $ let ⟨n, hn⟩ := (eq_top_iff_one _).1 h in @one_pow R _ n ▸ hn, λ h, h.symm ▸ radical_top R⟩ theorem is_prime.radical (H : is_prime I) : radical I = I := le_antisymm (λ r ⟨n, hrni⟩, H.mem_of_pow_mem n hrni) le_radical variables (I J) theorem radical_sup : radical (I ⊔ J) = radical (radical I ⊔ radical J) := le_antisymm (radical_mono $ sup_le_sup le_radical le_radical) $ λ r ⟨n, hrnij⟩, let ⟨s, hs, t, ht, hst⟩ := submodule.mem_sup.1 hrnij in @radical_idem _ _ (I ⊔ J) ▸ ⟨n, hst ▸ ideal.add_mem _ (radical_mono le_sup_left hs) (radical_mono le_sup_right ht)⟩ theorem radical_inf : radical (I ⊓ J) = radical I ⊓ radical J := le_antisymm (le_inf (radical_mono inf_le_left) (radical_mono inf_le_right)) (λ r ⟨⟨m, hrm⟩, ⟨n, hrn⟩⟩, ⟨m + n, (pow_add r m n).symm ▸ I.mul_mem_right hrm, (pow_add r m n).symm ▸ J.mul_mem_left hrn⟩) theorem radical_mul : radical (I * J) = radical I ⊓ radical J := le_antisymm (radical_inf I J ▸ radical_mono $ @mul_le_inf _ _ I J) (λ r ⟨⟨m, hrm⟩, ⟨n, hrn⟩⟩, ⟨m + n, (pow_add r m n).symm ▸ mul_mem_mul hrm hrn⟩) variables {I J} theorem is_prime.radical_le_iff (hj : is_prime J) : radical I ≤ J ↔ I ≤ J := ⟨le_trans le_radical, λ hij r ⟨n, hrni⟩, hj.mem_of_pow_mem n $ hij hrni⟩ theorem radical_eq_Inf (I : ideal R) : radical I = Inf { J : ideal R | I ≤ J ∧ is_prime J } := le_antisymm (le_Inf $ λ J hJ, hJ.2.radical_le_iff.2 hJ.1) $ λ r hr, classical.by_contradiction $ λ hri, let ⟨m, (hrm : r ∉ radical m), him, hm⟩ := zorn.zorn_partial_order₀ {K : ideal R | r ∉ radical K} (λ c hc hcc y hyc, ⟨Sup c, λ ⟨n, hrnc⟩, let ⟨y, hyc, hrny⟩ := submodule.mem_Sup_of_directed hrnc y hyc hcc.directed_on in hc hyc ⟨n, hrny⟩, λ z, le_Sup⟩) I hri in have ∀ x ∉ m, r ∈ radical (m ⊔ span {x}) := λ x hxm, classical.by_contradiction $ λ hrmx, hxm $ hm (m ⊔ span {x}) hrmx le_sup_left ▸ (le_sup_right : _ ≤ m ⊔ span {x}) (subset_span $ set.mem_singleton _), have is_prime m, from ⟨by rintro rfl; rw radical_top at hrm; exact hrm trivial, λ x y hxym, classical.or_iff_not_imp_left.2 $ λ hxm, classical.by_contradiction $ λ hym, let ⟨n, hrn⟩ := this _ hxm, ⟨p, hpm, q, hq, hpqrn⟩ := submodule.mem_sup.1 hrn, ⟨c, hcxq⟩ := mem_span_singleton'.1 hq in let ⟨k, hrk⟩ := this _ hym, ⟨f, hfm, g, hg, hfgrk⟩ := submodule.mem_sup.1 hrk, ⟨d, hdyg⟩ := mem_span_singleton'.1 hg in hrm ⟨n + k, by rw [pow_add, ← hpqrn, ← hcxq, ← hfgrk, ← hdyg, add_mul, mul_add (c*x), mul_assoc c x (d*y), mul_left_comm x, ← mul_assoc]; refine m.add_mem (m.mul_mem_right hpm) (m.add_mem (m.mul_mem_left hfm) (m.mul_mem_left hxym))⟩⟩, hrm $ this.radical.symm ▸ (Inf_le ⟨him, this⟩ : Inf {J : ideal R | I ≤ J ∧ is_prime J} ≤ m) hr instance : comm_semiring (ideal R) := submodule.comm_semiring @[simp] lemma add_eq_sup : I + J = I ⊔ J := rfl @[simp] lemma zero_eq_bot : (0 : ideal R) = ⊥ := rfl @[simp] lemma one_eq_top : (1 : ideal R) = ⊤ := by erw [submodule.one_eq_map_top, submodule.map_id] variables (I) theorem radical_pow (n : ℕ) (H : n > 0) : radical (I^n) = radical I := nat.rec_on n (not.elim dec_trivial) (λ n ih H, or.cases_on (lt_or_eq_of_le $ nat.le_of_lt_succ H) (λ H, calc radical (I^(n+1)) = radical I ⊓ radical (I^n) : radical_mul _ _ ... = radical I ⊓ radical I : by rw ih H ... = radical I : inf_idem) (λ H, H ▸ (pow_one I).symm ▸ rfl)) H end mul_and_radical section map_and_comap variables {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] variables (f : R → S) [is_ring_hom f] variables {I J : ideal R} {K L : ideal S} def map (I : ideal R) : ideal S := span (f '' I) def comap (I : ideal S) : ideal R := { carrier := f ⁻¹' I, zero := show f 0 ∈ I, by rw is_ring_hom.map_zero f; exact I.zero_mem, add := λ x y hx hy, show f (x + y) ∈ I, by rw is_ring_hom.map_add f; exact I.add_mem hx hy, smul := λ c x hx, show f (c * x) ∈ I, by rw is_ring_hom.map_mul f; exact I.mul_mem_left hx } variables {f} theorem map_mono (h : I ≤ J) : map f I ≤ map f J := span_mono $ set.image_subset _ h theorem mem_map_of_mem {x} (h : x ∈ I) : f x ∈ map f I := subset_span ⟨x, h, rfl⟩ theorem map_le_iff_le_comap : map f I ≤ K ↔ I ≤ comap f K := span_le.trans set.image_subset_iff @[simp] theorem mem_comap {x} : x ∈ comap f K ↔ f x ∈ K := iff.rfl theorem comap_mono (h : K ≤ L) : comap f K ≤ comap f L := set.preimage_mono h variables (f) theorem comap_ne_top (hK : K ≠ ⊤) : comap f K ≠ ⊤ := (ne_top_iff_one _).2 $ by rw [mem_comap, is_ring_hom.map_one f]; exact (ne_top_iff_one _).1 hK instance is_prime.comap {hK : K.is_prime} : (comap f K).is_prime := ⟨comap_ne_top _ hK.1, λ x y, by simp only [mem_comap, is_ring_hom.map_mul f]; apply hK.2⟩ variables (I J K L) theorem map_bot : map f ⊥ = ⊥ := le_antisymm (map_le_iff_le_comap.2 bot_le) bot_le theorem map_top : map f ⊤ = ⊤ := (eq_top_iff_one _).2 $ subset_span ⟨1, trivial, is_ring_hom.map_one f⟩ theorem comap_top : comap f ⊤ = ⊤ := (eq_top_iff_one _).2 trivial theorem map_sup : map f (I ⊔ J) = map f I ⊔ map f J := le_antisymm (map_le_iff_le_comap.2 $ sup_le (map_le_iff_le_comap.1 le_sup_left) (map_le_iff_le_comap.1 le_sup_right)) (sup_le (map_mono le_sup_left) (map_mono le_sup_right)) theorem map_mul : map f (I * J) = map f I * map f J := le_antisymm (map_le_iff_le_comap.2 $ mul_le.2 $ λ r hri s hsj, show f (r * s) ∈ _, by rw is_ring_hom.map_mul f; exact mul_mem_mul (mem_map_of_mem hri) (mem_map_of_mem hsj)) (trans_rel_right _ (span_mul_span _ _) $ span_le.2 $ set.bUnion_subset $ λ i ⟨r, hri, hfri⟩, set.bUnion_subset $ λ j ⟨s, hsj, hfsj⟩, set.singleton_subset_iff.2 $ hfri ▸ hfsj ▸ by rw [← is_ring_hom.map_mul f]; exact mem_map_of_mem (mul_mem_mul hri hsj)) theorem comap_inf : comap f (K ⊓ L) = comap f K ⊓ comap f L := rfl theorem comap_radical : comap f (radical K) = radical (comap f K) := le_antisymm (λ r ⟨n, hfrnk⟩, ⟨n, show f (r ^ n) ∈ K, from (is_semiring_hom.map_pow f r n).symm ▸ hfrnk⟩) (λ r ⟨n, hfrnk⟩, ⟨n, is_semiring_hom.map_pow f r n ▸ hfrnk⟩) variables {I J K L} theorem map_inf_le : map f (I ⊓ J) ≤ map f I ⊓ map f J := map_le_iff_le_comap.2 $ (comap_inf f (map f I) (map f J)).symm ▸ inf_le_inf (map_le_iff_le_comap.1 $ le_refl _) (map_le_iff_le_comap.1 $ le_refl _) theorem map_radical_le : map f (radical I) ≤ radical (map f I) := map_le_iff_le_comap.2 $ λ r ⟨n, hrni⟩, ⟨n, is_semiring_hom.map_pow f r n ▸ mem_map_of_mem hrni⟩ theorem le_comap_sup : comap f K ⊔ comap f L ≤ comap f (K ⊔ L) := map_le_iff_le_comap.1 $ (map_sup f (comap f K) (comap f L)).symm ▸ sup_le_sup (map_le_iff_le_comap.2 $ le_refl _) (map_le_iff_le_comap.2 $ le_refl _) theorem le_comap_mul : comap f K * comap f L ≤ comap f (K * L) := map_le_iff_le_comap.1 $ (map_mul f (comap f K) (comap f L)).symm ▸ mul_mono (map_le_iff_le_comap.2 $ le_refl _) (map_le_iff_le_comap.2 $ le_refl _) section surjective variables (hf : function.surjective f) include hf theorem map_comap_of_surjective (I : ideal S) : map f (comap f I) = I := le_antisymm (map_le_iff_le_comap.2 (le_refl _)) (λ s hsi, let ⟨r, hfrs⟩ := hf s in hfrs ▸ (mem_map_of_mem $ show f r ∈ I, from hfrs.symm ▸ hsi)) theorem mem_image_of_mem_map_of_surjective {I : ideal R} {y} (H : y ∈ map f I) : y ∈ f '' I := submodule.span_induction H (λ _, id) ⟨0, I.zero_mem, is_ring_hom.map_zero f⟩ (λ y1 y2 ⟨x1, hx1i, hxy1⟩ ⟨x2, hx2i, hxy2⟩, ⟨x1 + x2, I.add_mem hx1i hx2i, hxy1 ▸ hxy2 ▸ is_ring_hom.map_add f⟩) (λ c y ⟨x, hxi, hxy⟩, let ⟨d, hdc⟩ := hf c in ⟨d • x, I.smul_mem _ hxi, hdc ▸ hxy ▸ is_ring_hom.map_mul f⟩) theorem comap_map_of_surjective (I : ideal R) : comap f (map f I) = I ⊔ comap f ⊥ := le_antisymm (assume r h, let ⟨s, hsi, hfsr⟩ := mem_image_of_mem_map_of_surjective f hf h in submodule.mem_sup.2 ⟨s, hsi, r - s, (submodule.mem_bot S).2 $ by rw [is_ring_hom.map_sub f, hfsr, sub_self], add_sub_cancel'_right s r⟩) (sup_le (map_le_iff_le_comap.1 (le_refl _)) (comap_mono bot_le)) /-- Correspondence theorem -/ def order_iso_of_surjective : ((≤) : ideal S → ideal S → Prop) ≃o ((≤) : { p : ideal R // comap f ⊥ ≤ p } → { p : ideal R // comap f ⊥ ≤ p } → Prop) := { to_fun := λ J, ⟨comap f J, comap_mono bot_le⟩, inv_fun := λ I, map f I.1, left_inv := λ J, map_comap_of_surjective f hf J, right_inv := λ I, subtype.eq $ show comap f (map f I.1) = I.1, from (comap_map_of_surjective f hf I).symm ▸ le_antisymm (sup_le (le_refl _) I.2) le_sup_left, ord := λ I1 I2, ⟨comap_mono, λ H, map_comap_of_surjective f hf I1 ▸ map_comap_of_surjective f hf I2 ▸ map_mono H⟩ } def le_order_embedding_of_surjective : ((≤) : ideal S → ideal S → Prop) ≼o ((≤) : ideal R → ideal R → Prop) := (order_iso_of_surjective f hf).to_order_embedding.trans (subtype.order_embedding _ _) def lt_order_embedding_of_surjective : ((<) : ideal S → ideal S → Prop) ≼o ((<) : ideal R → ideal R → Prop) := (le_order_embedding_of_surjective f hf).lt_embedding_of_le_embedding end surjective end map_and_comap end ideal namespace submodule variables {R : Type u} {M : Type v} variables [comm_ring R] [add_comm_group M] [module R M] -- It is even a semialgebra. But those aren't in mathlib yet. instance : semimodule (ideal R) (submodule R M) := { smul_add := smul_sup, add_smul := sup_smul, mul_smul := smul_assoc, one_smul := by simp, zero_smul := bot_smul, smul_zero := smul_bot } end submodule
2e554eb789b044c8130d3abaa15fe9025231b933
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/test/solve_by_elim.lean
e738eb097828f415b5b13c7f783d0bd1724b4379
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
4,169
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Scott Morrison -/ import tactic.solve_by_elim example {a b : Prop} (h₀ : a → b) (h₁ : a) : b := begin apply_assumption, apply_assumption, end example {X : Type} (x : X) : x = x := by solve_by_elim example : true := by solve_by_elim example {a b : Prop} (h₀ : a → b) (h₁ : a) : b := by solve_by_elim example {α : Type} {a b : α → Prop} (h₀ : ∀ x : α, b x = a x) (y : α) : a y = b y := by solve_by_elim example {α : Type} {a b : α → Prop} (h₀ : b = a) (y : α) : a y = b y := by solve_by_elim example {α : Type} {a b : α → Prop} (h₀ : b = a) (y : α) : a y = b y := begin success_if_fail { solve_by_elim only [], }, success_if_fail { solve_by_elim only [h₀], }, solve_by_elim only [h₀, congr_fun] end example {α : Type} {a b : α → Prop} (h₀ : b = a) (y : α) : a y = b y := by solve_by_elim [h₀] example {α : Type} {a b : α → Prop} (h₀ : b = a) (y : α) : a y = b y := begin success_if_fail { solve_by_elim [*, -h₀] }, solve_by_elim [*] end example {α β : Type} (a b : α) (f : α → β) (i : function.injective f) (h : f a = f b) : a = b := begin success_if_fail { solve_by_elim only [i] }, success_if_fail { solve_by_elim only [h] }, solve_by_elim only [i,h] end @[user_attribute] meta def ex : user_attribute := { name := `ex, descr := "An example attribute for testing solve_by_elim." } @[ex] def f : ℕ := 0 example : ℕ := by solve_by_elim [f] example : ℕ := begin success_if_fail { solve_by_elim }, success_if_fail { solve_by_elim [-f] with ex }, solve_by_elim with ex, end example {α : Type} {p : α → Prop} (h₀ : ∀ x, p x) (y : α) : p y := begin apply_assumption, end open tactic example : true := begin (do gs ← get_goals, set_goals [], success_if_fail `[solve_by_elim], set_goals gs), trivial end example {α : Type} (r : α → α → Prop) (f : α → α → α) (l : ∀ a b c : α, r a b → r a (f b c) → r a c) (a b c : α) (h₁ : r a b) (h₂ : r a (f b c)) : r a c := begin solve_by_elim, end -- Verifying that `solve_by_elim*` acts on all remaining goals. example (n : ℕ) : ℕ × ℕ := begin split, solve_by_elim*, end -- Verifying that `solve_by_elim*` backtracks when given multiple goals. example (n m : ℕ) (f : ℕ → ℕ → Prop) (h : f n m) : ∃ p : ℕ × ℕ, f p.1 p.2 := begin repeat { fsplit }, solve_by_elim*, end example {a b c : ℕ} (h₁ : a ≤ b) (h₂ : b ≤ c) : a ≤ c := begin apply le_trans, solve_by_elim { backtrack_all_goals := true }, end -- test that metavariables created for implicit arguments don't get stuck example (P : ℕ → Type) (f : Π {n : ℕ}, P n) : P 2 × P 3 := begin fsplit, solve_by_elim* only [f], end example : 6 = 6 ∧ [7] = [7] := begin split, solve_by_elim* only [@rfl _], end example (P Q R : Prop) : P ∧ Q → P ∧ Q := begin solve_by_elim [and.imp, id], end /- We now test the `accept` feature of `solve_by_elim`. Recall that the `accept` parameter has type `list expr → tactic unit`. At each branch (not just leaf) of the backtracking search tree, `accept` is invoked with the list of metavariables reported by `get_goals` when `solve_by_elim` was called (which by now may have been partially solved by previous `apply` steps), and if it fails this branch of the search is ignored. Non-leaf nodes of the search tree will contain metavariables, so we can test using `expr.has_meta_var` when we're only interesting in filtering complete solutions. In this example, we only accept solutions that contain a given subexpression. -/ def solve_by_elim_use_b (a b : ℕ) : ℕ × ℕ × ℕ := begin split; [skip, split], (do b ← get_local `b, tactic.solve_by_elim { backtrack_all_goals := tt, -- We require that in some goal, the expression `b` is used. accept := (λ gs, gs.any_of (λ g, guard $ g.contains_expr_or_mvar b)) }) end -- We verify that the solution did use `b`. example : solve_by_elim_use_b 1 2 = (1, 1, 2) := rfl
d234863b82ac0a787bda0c8c662d7e5aec0577fe
d5bef83c55d40cb88f9a01b755c882a93348a847
/tests/lean/hole_issue2.lean
3769da03823ad00624985a51b3ba933e185133bb
[ "Apache-2.0" ]
permissive
urkud/lean
587d78216e1f0c7f651566e9e92cf8ade285d58d
3526539070ea6268df5dd373deeb3ac8b9621952
refs/heads/master
1,660,171,634,921
1,657,873,466,000
1,657,873,466,000
249,789,456
0
0
Apache-2.0
1,585,075,263,000
1,585,075,263,000
null
UTF-8
Lean
false
false
1,821
lean
constant bag_setoid : ∀ A, setoid (list A) attribute [instance] bag_setoid definition bag (A : Type) : Type := quotient (bag_setoid A) constant subcount : ∀ {A}, list A → list A → bool constant list.count : ∀ {A}, A → list A → nat constant all_of_subcount_eq_tt : ∀ {A} {l₁ l₂ : list A}, subcount l₁ l₂ = tt → ∀ a, list.count a l₁ ≤ list.count a l₂ constant ex_of_subcount_eq_ff : ∀ {A} {l₁ l₂ : list A}, subcount l₁ l₂ = ff → ∃ a, ¬ list.count a l₁ ≤ list.count a l₂ noncomputable definition count {A} (a : A) (b : bag A) : nat := quotient.lift_on b (λ l, list.count a l) (λ l₁ l₂ h, sorry) definition subbag {A} (b₁ b₂ : bag A) := ∀ a, count a b₁ ≤ count a b₂ infix ` ⊆ ` := subbag noncomputable definition decidable_subbag_1 {A} (b₁ b₂ : bag A) : decidable (b₁ ⊆ b₂) := quotient.rec_on_subsingleton₂ b₁ b₂ (λ l₁ l₂, match subcount l₁ l₂, rfl : ∀ (b : _), subcount l₁ l₂ = b → _ with | tt, H := is_true (all_of_subcount_eq_tt H) | ff, H := is_false (λ h, exists.elim (ex_of_subcount_eq_ff H) (λ w hw, _)) end) noncomputable definition decidable_subbag_2 {A} (b₁ b₂ : bag A) : decidable (b₁ ⊆ b₂) := quotient.rec_on_subsingleton₂ b₁ b₂ (λ l₁ l₂, match subcount l₁ l₂, rfl : ∀ (b : _), subcount l₁ l₂ = b → _ with | tt, H := is_true (all_of_subcount_eq_tt H) | ff, H := is_false (λ h, exists.elim (ex_of_subcount_eq_ff H) _) end) noncomputable definition decidable_subbag_3 {A} (b₁ b₂ : bag A) : decidable (b₁ ⊆ b₂) := quotient.rec_on_subsingleton₂ b₁ b₂ (λ l₁ l₂, match subcount l₁ l₂, rfl : ∀ (b : _), subcount l₁ l₂ = b → _ with | tt, H := is_true (all_of_subcount_eq_tt H) | ff, H := is_false (λ h, _) end)
6055104b52b26b04b37026191be49b3c5a3e3682
bb31430994044506fa42fd667e2d556327e18dfe
/src/algebra/big_operators/finprod.lean
1f10a690f35a133f0143d031ded7d0aaf5491ddd
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
47,691
lean
/- Copyright (c) 2020 Kexing Ying and Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying, Kevin Buzzard, Yury Kudryashov -/ import algebra.big_operators.order import algebra.indicator_function /-! # Finite products and sums over types and sets We define products and sums over types and subsets of types, with no finiteness hypotheses. All infinite products and sums are defined to be junk values (i.e. one or zero). This approach is sometimes easier to use than `finset.sum`, when issues arise with `finset` and `fintype` being data. ## Main definitions We use the following variables: * `α`, `β` - types with no structure; * `s`, `t` - sets * `M`, `N` - additive or multiplicative commutative monoids * `f`, `g` - functions Definitions in this file: * `finsum f : M` : the sum of `f x` as `x` ranges over the support of `f`, if it's finite. Zero otherwise. * `finprod f : M` : the product of `f x` as `x` ranges over the multiplicative support of `f`, if it's finite. One otherwise. ## Notation * `∑ᶠ i, f i` and `∑ᶠ i : α, f i` for `finsum f` * `∏ᶠ i, f i` and `∏ᶠ i : α, f i` for `finprod f` This notation works for functions `f : p → M`, where `p : Prop`, so the following works: * `∑ᶠ i ∈ s, f i`, where `f : α → M`, `s : set α` : sum over the set `s`; * `∑ᶠ n < 5, f n`, where `f : ℕ → M` : same as `f 0 + f 1 + f 2 + f 3 + f 4`; * `∏ᶠ (n >= -2) (hn : n < 3), f n`, where `f : ℤ → M` : same as `f (-2) * f (-1) * f 0 * f 1 * f 2`. ## Implementation notes `finsum` and `finprod` is "yet another way of doing finite sums and products in Lean". However experiments in the wild (e.g. with matroids) indicate that it is a helpful approach in settings where the user is not interested in computability and wants to do reasoning without running into typeclass diamonds caused by the constructive finiteness used in definitions such as `finset` and `fintype`. By sticking solely to `set.finite` we avoid these problems. We are aware that there are other solutions but for beginner mathematicians this approach is easier in practice. Another application is the construction of a partition of unity from a collection of “bump” function. In this case the finite set depends on the point and it's convenient to have a definition that does not mention the set explicitly. The first arguments in all definitions and lemmas is the codomain of the function of the big operator. This is necessary for the heuristic in `@[to_additive]`. See the documentation of `to_additive.attr` for more information. We did not add `is_finite (X : Type) : Prop`, because it is simply `nonempty (fintype X)`. ## Tags finsum, finprod, finite sum, finite product -/ open function set /-! ### Definition and relation to `finset.sum` and `finset.prod` -/ section sort variables {G M N : Type*} {α β ι : Sort*} [comm_monoid M] [comm_monoid N] open_locale big_operators section /- Note: we use classical logic only for these definitions, to ensure that we do not write lemmas with `classical.dec` in their statement. -/ open_locale classical /-- Sum of `f x` as `x` ranges over the elements of the support of `f`, if it's finite. Zero otherwise. -/ @[irreducible] noncomputable def finsum {M α} [add_comm_monoid M] (f : α → M) : M := if h : (support (f ∘ plift.down)).finite then ∑ i in h.to_finset, f i.down else 0 /-- Product of `f x` as `x` ranges over the elements of the multiplicative support of `f`, if it's finite. One otherwise. -/ @[irreducible, to_additive] noncomputable def finprod (f : α → M) : M := if h : (mul_support (f ∘ plift.down)).finite then ∏ i in h.to_finset, f i.down else 1 end localized "notation (name := finsum) `∑ᶠ` binders `, ` r:(scoped:67 f, finsum f) := r" in big_operators localized "notation (name := finprod) `∏ᶠ` binders `, ` r:(scoped:67 f, finprod f) := r" in big_operators @[to_additive] lemma finprod_eq_prod_plift_of_mul_support_to_finset_subset {f : α → M} (hf : (mul_support (f ∘ plift.down)).finite) {s : finset (plift α)} (hs : hf.to_finset ⊆ s) : ∏ᶠ i, f i = ∏ i in s, f i.down := begin rw [finprod, dif_pos], refine finset.prod_subset hs (λ x hx hxf, _), rwa [hf.mem_to_finset, nmem_mul_support] at hxf end @[to_additive] lemma finprod_eq_prod_plift_of_mul_support_subset {f : α → M} {s : finset (plift α)} (hs : mul_support (f ∘ plift.down) ⊆ s) : ∏ᶠ i, f i = ∏ i in s, f i.down := finprod_eq_prod_plift_of_mul_support_to_finset_subset (s.finite_to_set.subset hs) $ λ x hx, by { rw finite.mem_to_finset at hx, exact hs hx } @[simp, to_additive] lemma finprod_one : ∏ᶠ i : α, (1 : M) = 1 := begin have : mul_support (λ x : plift α, (λ _, 1 : α → M) x.down) ⊆ (∅ : finset (plift α)), from λ x h, h rfl, rw [finprod_eq_prod_plift_of_mul_support_subset this, finset.prod_empty] end @[to_additive] lemma finprod_of_is_empty [is_empty α] (f : α → M) : ∏ᶠ i, f i = 1 := by { rw ← finprod_one, congr } @[simp, to_additive] lemma finprod_false (f : false → M) : ∏ᶠ i, f i = 1 := finprod_of_is_empty _ @[to_additive] lemma finprod_eq_single (f : α → M) (a : α) (ha : ∀ x ≠ a, f x = 1) : ∏ᶠ x, f x = f a := begin have : mul_support (f ∘ plift.down) ⊆ ({plift.up a} : finset (plift α)), { intro x, contrapose, simpa [plift.eq_up_iff_down_eq] using ha x.down }, rw [finprod_eq_prod_plift_of_mul_support_subset this, finset.prod_singleton], end @[to_additive] lemma finprod_unique [unique α] (f : α → M) : ∏ᶠ i, f i = f default := finprod_eq_single f default $ λ x hx, (hx $ unique.eq_default _).elim @[simp, to_additive] lemma finprod_true (f : true → M) : ∏ᶠ i, f i = f trivial := @finprod_unique M true _ ⟨⟨trivial⟩, λ _, rfl⟩ f @[to_additive] lemma finprod_eq_dif {p : Prop} [decidable p] (f : p → M) : ∏ᶠ i, f i = if h : p then f h else 1 := begin split_ifs, { haveI : unique p := ⟨⟨h⟩, λ _, rfl⟩, exact finprod_unique f }, { haveI : is_empty p := ⟨h⟩, exact finprod_of_is_empty f } end @[to_additive] lemma finprod_eq_if {p : Prop} [decidable p] {x : M} : ∏ᶠ i : p, x = if p then x else 1 := finprod_eq_dif (λ _, x) @[to_additive] lemma finprod_congr {f g : α → M} (h : ∀ x, f x = g x) : finprod f = finprod g := congr_arg _ $ funext h @[congr, to_additive] lemma finprod_congr_Prop {p q : Prop} {f : p → M} {g : q → M} (hpq : p = q) (hfg : ∀ h : q, f (hpq.mpr h) = g h) : finprod f = finprod g := by { subst q, exact finprod_congr hfg } attribute [congr] finsum_congr_Prop /-- To prove a property of a finite product, it suffices to prove that the property is multiplicative and holds on the factors. -/ @[to_additive "To prove a property of a finite sum, it suffices to prove that the property is additive and holds on the summands."] lemma finprod_induction {f : α → M} (p : M → Prop) (hp₀ : p 1) (hp₁ : ∀ x y, p x → p y → p (x * y)) (hp₂ : ∀ i, p (f i)) : p (∏ᶠ i, f i) := begin rw finprod, split_ifs, exacts [finset.prod_induction _ _ hp₁ hp₀ (λ i hi, hp₂ _), hp₀] end lemma finprod_nonneg {R : Type*} [ordered_comm_semiring R] {f : α → R} (hf : ∀ x, 0 ≤ f x) : 0 ≤ ∏ᶠ x, f x := finprod_induction (λ x, 0 ≤ x) zero_le_one (λ x y, mul_nonneg) hf @[to_additive finsum_nonneg] lemma one_le_finprod' {M : Type*} [ordered_comm_monoid M] {f : α → M} (hf : ∀ i, 1 ≤ f i) : 1 ≤ ∏ᶠ i, f i := finprod_induction _ le_rfl (λ _ _, one_le_mul) hf @[to_additive] lemma monoid_hom.map_finprod_plift (f : M →* N) (g : α → M) (h : (mul_support $ g ∘ plift.down).finite) : f (∏ᶠ x, g x) = ∏ᶠ x, f (g x) := begin rw [finprod_eq_prod_plift_of_mul_support_subset h.coe_to_finset.ge, finprod_eq_prod_plift_of_mul_support_subset, f.map_prod], rw [h.coe_to_finset], exact mul_support_comp_subset f.map_one (g ∘ plift.down) end @[to_additive] lemma monoid_hom.map_finprod_Prop {p : Prop} (f : M →* N) (g : p → M) : f (∏ᶠ x, g x) = ∏ᶠ x, f (g x) := f.map_finprod_plift g (set.to_finite _) @[to_additive] lemma monoid_hom.map_finprod_of_preimage_one (f : M →* N) (hf : ∀ x, f x = 1 → x = 1) (g : α → M) : f (∏ᶠ i, g i) = ∏ᶠ i, f (g i) := begin by_cases hg : (mul_support $ g ∘ plift.down).finite, { exact f.map_finprod_plift g hg }, rw [finprod, dif_neg, f.map_one, finprod, dif_neg], exacts [infinite.mono (λ x hx, mt (hf (g x.down)) hx) hg, hg] end @[to_additive] lemma monoid_hom.map_finprod_of_injective (g : M →* N) (hg : injective g) (f : α → M) : g (∏ᶠ i, f i) = ∏ᶠ i, g (f i) := g.map_finprod_of_preimage_one (λ x, (hg.eq_iff' g.map_one).mp) f @[to_additive] lemma mul_equiv.map_finprod (g : M ≃* N) (f : α → M) : g (∏ᶠ i, f i) = ∏ᶠ i, g (f i) := g.to_monoid_hom.map_finprod_of_injective g.injective f lemma finsum_smul {R M : Type*} [ring R] [add_comm_group M] [module R M] [no_zero_smul_divisors R M] (f : ι → R) (x : M) : (∑ᶠ i, f i) • x = ∑ᶠ i, f i • x := begin rcases eq_or_ne x 0 with rfl|hx, { simp }, exact ((smul_add_hom R M).flip x).map_finsum_of_injective (smul_left_injective R hx) _ end lemma smul_finsum {R M : Type*} [ring R] [add_comm_group M] [module R M] [no_zero_smul_divisors R M] (c : R) (f : ι → M) : c • (∑ᶠ i, f i) = (∑ᶠ i, c • f i) := begin rcases eq_or_ne c 0 with rfl|hc, { simp }, exact (smul_add_hom R M c).map_finsum_of_injective (smul_right_injective M hc) _ end @[to_additive] lemma finprod_inv_distrib [division_comm_monoid G] (f : α → G) : ∏ᶠ x, (f x)⁻¹ = (∏ᶠ x, f x)⁻¹ := ((mul_equiv.inv G).map_finprod f).symm end sort section type variables {α β ι G M N : Type*} [comm_monoid M] [comm_monoid N] open_locale big_operators @[to_additive] lemma finprod_eq_mul_indicator_apply (s : set α) (f : α → M) (a : α) : ∏ᶠ (h : a ∈ s), f a = mul_indicator s f a := by convert finprod_eq_if @[simp, to_additive] lemma finprod_mem_mul_support (f : α → M) (a : α) : ∏ᶠ (h : f a ≠ 1), f a = f a := by rw [← mem_mul_support, finprod_eq_mul_indicator_apply, mul_indicator_mul_support] @[to_additive] lemma finprod_mem_def (s : set α) (f : α → M) : ∏ᶠ a ∈ s, f a = ∏ᶠ a, mul_indicator s f a := finprod_congr $ finprod_eq_mul_indicator_apply s f @[to_additive] lemma finprod_eq_prod_of_mul_support_subset (f : α → M) {s : finset α} (h : mul_support f ⊆ s) : ∏ᶠ i, f i = ∏ i in s, f i := begin have A : mul_support (f ∘ plift.down) = equiv.plift.symm '' mul_support f, { rw mul_support_comp_eq_preimage, exact (equiv.plift.symm.image_eq_preimage _).symm }, have : mul_support (f ∘ plift.down) ⊆ s.map equiv.plift.symm.to_embedding, { rw [A, finset.coe_map], exact image_subset _ h }, rw [finprod_eq_prod_plift_of_mul_support_subset this], simp end @[to_additive] lemma finprod_eq_prod_of_mul_support_to_finset_subset (f : α → M) (hf : (mul_support f).finite) {s : finset α} (h : hf.to_finset ⊆ s) : ∏ᶠ i, f i = ∏ i in s, f i := finprod_eq_prod_of_mul_support_subset _ $ λ x hx, h $ hf.mem_to_finset.2 hx @[to_additive] lemma finprod_eq_finset_prod_of_mul_support_subset (f : α → M) {s : finset α} (h : mul_support f ⊆ (s : set α)) : ∏ᶠ i, f i = ∏ i in s, f i := begin have h' : (s.finite_to_set.subset h).to_finset ⊆ s, { simpa [← finset.coe_subset, set.coe_to_finset], }, exact finprod_eq_prod_of_mul_support_to_finset_subset _ _ h', end @[to_additive] lemma finprod_def (f : α → M) [decidable (mul_support f).finite] : ∏ᶠ i : α, f i = if h : (mul_support f).finite then ∏ i in h.to_finset, f i else 1 := begin split_ifs, { exact finprod_eq_prod_of_mul_support_to_finset_subset _ h (finset.subset.refl _) }, { rw [finprod, dif_neg], rw [mul_support_comp_eq_preimage], exact mt (λ hf, hf.of_preimage equiv.plift.surjective) h} end @[to_additive] lemma finprod_of_infinite_mul_support {f : α → M} (hf : (mul_support f).infinite) : ∏ᶠ i, f i = 1 := by { classical, rw [finprod_def, dif_neg hf] } @[to_additive] lemma finprod_eq_prod (f : α → M) (hf : (mul_support f).finite) : ∏ᶠ i : α, f i = ∏ i in hf.to_finset, f i := by { classical, rw [finprod_def, dif_pos hf] } @[to_additive] lemma finprod_eq_prod_of_fintype [fintype α] (f : α → M) : ∏ᶠ i : α, f i = ∏ i, f i := finprod_eq_prod_of_mul_support_to_finset_subset _ (set.to_finite _) $ finset.subset_univ _ @[to_additive] lemma finprod_cond_eq_prod_of_cond_iff (f : α → M) {p : α → Prop} {t : finset α} (h : ∀ {x}, f x ≠ 1 → (p x ↔ x ∈ t)) : ∏ᶠ i (hi : p i), f i = ∏ i in t, f i := begin set s := {x | p x}, have : mul_support (s.mul_indicator f) ⊆ t, { rw [set.mul_support_mul_indicator], intros x hx, exact (h hx.2).1 hx.1 }, erw [finprod_mem_def, finprod_eq_prod_of_mul_support_subset _ this], refine finset.prod_congr rfl (λ x hx, mul_indicator_apply_eq_self.2 $ λ hxs, _), contrapose! hxs, exact (h hxs).2 hx end @[to_additive] lemma finprod_cond_ne (f : α → M) (a : α) [decidable_eq α] (hf : (mul_support f).finite) : (∏ᶠ i ≠ a, f i) = ∏ i in hf.to_finset.erase a, f i := begin apply finprod_cond_eq_prod_of_cond_iff, intros x hx, rw [finset.mem_erase, finite.mem_to_finset, mem_mul_support], exact ⟨λ h, and.intro h hx, λ h, h.1⟩ end @[to_additive] lemma finprod_mem_eq_prod_of_inter_mul_support_eq (f : α → M) {s : set α} {t : finset α} (h : s ∩ mul_support f = t ∩ mul_support f) : ∏ᶠ i ∈ s, f i = ∏ i in t, f i := finprod_cond_eq_prod_of_cond_iff _ $ by simpa [set.ext_iff] using h @[to_additive] lemma finprod_mem_eq_prod_of_subset (f : α → M) {s : set α} {t : finset α} (h₁ : s ∩ mul_support f ⊆ t) (h₂ : ↑t ⊆ s) : ∏ᶠ i ∈ s, f i = ∏ i in t, f i := finprod_cond_eq_prod_of_cond_iff _ $ λ x hx, ⟨λ h, h₁ ⟨h, hx⟩, λ h, h₂ h⟩ @[to_additive] lemma finprod_mem_eq_prod (f : α → M) {s : set α} (hf : (s ∩ mul_support f).finite) : ∏ᶠ i ∈ s, f i = ∏ i in hf.to_finset, f i := finprod_mem_eq_prod_of_inter_mul_support_eq _ $ by simp [inter_assoc] @[to_additive] lemma finprod_mem_eq_prod_filter (f : α → M) (s : set α) [decidable_pred (∈ s)] (hf : (mul_support f).finite) : ∏ᶠ i ∈ s, f i = ∏ i in finset.filter (∈ s) hf.to_finset, f i := finprod_mem_eq_prod_of_inter_mul_support_eq _ $ by simp [inter_comm, inter_left_comm] @[to_additive] lemma finprod_mem_eq_to_finset_prod (f : α → M) (s : set α) [fintype s] : ∏ᶠ i ∈ s, f i = ∏ i in s.to_finset, f i := finprod_mem_eq_prod_of_inter_mul_support_eq _ $ by rw [coe_to_finset] @[to_additive] lemma finprod_mem_eq_finite_to_finset_prod (f : α → M) {s : set α} (hs : s.finite) : ∏ᶠ i ∈ s, f i = ∏ i in hs.to_finset, f i := finprod_mem_eq_prod_of_inter_mul_support_eq _ $ by rw [hs.coe_to_finset] @[to_additive] lemma finprod_mem_finset_eq_prod (f : α → M) (s : finset α) : ∏ᶠ i ∈ s, f i = ∏ i in s, f i := finprod_mem_eq_prod_of_inter_mul_support_eq _ rfl @[to_additive] lemma finprod_mem_coe_finset (f : α → M) (s : finset α) : ∏ᶠ i ∈ (s : set α), f i = ∏ i in s, f i := finprod_mem_eq_prod_of_inter_mul_support_eq _ rfl @[to_additive] lemma finprod_mem_eq_one_of_infinite {f : α → M} {s : set α} (hs : (s ∩ mul_support f).infinite) : ∏ᶠ i ∈ s, f i = 1 := begin rw finprod_mem_def, apply finprod_of_infinite_mul_support, rwa [← mul_support_mul_indicator] at hs end @[to_additive] lemma finprod_mem_eq_one_of_forall_eq_one {f : α → M} {s : set α} (h : ∀ x ∈ s, f x = 1) : ∏ᶠ i ∈ s, f i = 1 := by simp [h] {contextual := tt} @[to_additive] lemma finprod_mem_inter_mul_support (f : α → M) (s : set α) : ∏ᶠ i ∈ (s ∩ mul_support f), f i = ∏ᶠ i ∈ s, f i := by rw [finprod_mem_def, finprod_mem_def, mul_indicator_inter_mul_support] @[to_additive] lemma finprod_mem_inter_mul_support_eq (f : α → M) (s t : set α) (h : s ∩ mul_support f = t ∩ mul_support f) : ∏ᶠ i ∈ s, f i = ∏ᶠ i ∈ t, f i := by rw [← finprod_mem_inter_mul_support, h, finprod_mem_inter_mul_support] @[to_additive] lemma finprod_mem_inter_mul_support_eq' (f : α → M) (s t : set α) (h : ∀ x ∈ mul_support f, x ∈ s ↔ x ∈ t) : ∏ᶠ i ∈ s, f i = ∏ᶠ i ∈ t, f i := begin apply finprod_mem_inter_mul_support_eq, ext x, exact and_congr_left (h x) end @[to_additive] lemma finprod_mem_univ (f : α → M) : ∏ᶠ i ∈ @set.univ α, f i = ∏ᶠ i : α, f i := finprod_congr $ λ i, finprod_true _ variables {f g : α → M} {a b : α} {s t : set α} @[to_additive] lemma finprod_mem_congr (h₀ : s = t) (h₁ : ∀ x ∈ t, f x = g x) : ∏ᶠ i ∈ s, f i = ∏ᶠ i ∈ t, g i := h₀.symm ▸ (finprod_congr $ λ i, finprod_congr_Prop rfl (h₁ i)) @[to_additive] lemma finprod_eq_one_of_forall_eq_one {f : α → M} (h : ∀ x, f x = 1) : ∏ᶠ i, f i = 1 := by simp [h] {contextual := tt} /-! ### Distributivity w.r.t. addition, subtraction, and (scalar) multiplication -/ /-- If the multiplicative supports of `f` and `g` are finite, then the product of `f i * g i` equals the product of `f i` multiplied by the product of `g i`. -/ @[to_additive "If the additive supports of `f` and `g` are finite, then the sum of `f i + g i` equals the sum of `f i` plus the sum of `g i`."] lemma finprod_mul_distrib (hf : (mul_support f).finite) (hg : (mul_support g).finite) : ∏ᶠ i, f i * g i = (∏ᶠ i, f i) * ∏ᶠ i, g i := begin classical, rw [finprod_eq_prod_of_mul_support_to_finset_subset _ hf (finset.subset_union_left _ _), finprod_eq_prod_of_mul_support_to_finset_subset _ hg (finset.subset_union_right _ _), ← finset.prod_mul_distrib], refine finprod_eq_prod_of_mul_support_subset _ _, simp [mul_support_mul] end /-- If the multiplicative supports of `f` and `g` are finite, then the product of `f i / g i` equals the product of `f i` divided by the product of `g i`. -/ @[to_additive "If the additive supports of `f` and `g` are finite, then the sum of `f i - g i` equals the sum of `f i` minus the sum of `g i`."] lemma finprod_div_distrib [division_comm_monoid G] {f g : α → G} (hf : (mul_support f).finite) (hg : (mul_support g).finite) : ∏ᶠ i, f i / g i = (∏ᶠ i, f i) / ∏ᶠ i, g i := by simp only [div_eq_mul_inv, finprod_mul_distrib hf ((mul_support_inv g).symm.rec hg), finprod_inv_distrib] /-- A more general version of `finprod_mem_mul_distrib` that only requires `s ∩ mul_support f` and `s ∩ mul_support g` rather than `s` to be finite. -/ @[to_additive "A more general version of `finsum_mem_add_distrib` that only requires `s ∩ support f` and `s ∩ support g` rather than `s` to be finite."] lemma finprod_mem_mul_distrib' (hf : (s ∩ mul_support f).finite) (hg : (s ∩ mul_support g).finite) : ∏ᶠ i ∈ s, f i * g i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ s, g i := begin rw [← mul_support_mul_indicator] at hf hg, simp only [finprod_mem_def, mul_indicator_mul, finprod_mul_distrib hf hg] end /-- The product of the constant function `1` over any set equals `1`. -/ @[to_additive "The product of the constant function `0` over any set equals `0`."] lemma finprod_mem_one (s : set α) : ∏ᶠ i ∈ s, (1 : M) = 1 := by simp /-- If a function `f` equals `1` on a set `s`, then the product of `f i` over `i ∈ s` equals `1`. -/ @[to_additive "If a function `f` equals `0` on a set `s`, then the product of `f i` over `i ∈ s` equals `0`."] lemma finprod_mem_of_eq_on_one (hf : s.eq_on f 1) : ∏ᶠ i ∈ s, f i = 1 := by { rw ← finprod_mem_one s, exact finprod_mem_congr rfl hf } /-- If the product of `f i` over `i ∈ s` is not equal to `1`, then there is some `x ∈ s` such that `f x ≠ 1`. -/ @[to_additive "If the product of `f i` over `i ∈ s` is not equal to `0`, then there is some `x ∈ s` such that `f x ≠ 0`."] lemma exists_ne_one_of_finprod_mem_ne_one (h : ∏ᶠ i ∈ s, f i ≠ 1) : ∃ x ∈ s, f x ≠ 1 := begin by_contra' h', exact h (finprod_mem_of_eq_on_one h') end /-- Given a finite set `s`, the product of `f i * g i` over `i ∈ s` equals the product of `f i` over `i ∈ s` times the product of `g i` over `i ∈ s`. -/ @[to_additive "Given a finite set `s`, the sum of `f i + g i` over `i ∈ s` equals the sum of `f i` over `i ∈ s` plus the sum of `g i` over `i ∈ s`."] lemma finprod_mem_mul_distrib (hs : s.finite) : ∏ᶠ i ∈ s, f i * g i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ s, g i := finprod_mem_mul_distrib' (hs.inter_of_left _) (hs.inter_of_left _) @[to_additive] lemma monoid_hom.map_finprod {f : α → M} (g : M →* N) (hf : (mul_support f).finite) : g (∏ᶠ i, f i) = ∏ᶠ i, g (f i) := g.map_finprod_plift f $ hf.preimage $ equiv.plift.injective.inj_on _ @[to_additive] lemma finprod_pow (hf : (mul_support f).finite) (n : ℕ) : (∏ᶠ i, f i) ^ n = ∏ᶠ i, f i ^ n := (pow_monoid_hom n).map_finprod hf /-- A more general version of `monoid_hom.map_finprod_mem` that requires `s ∩ mul_support f` rather than `s` to be finite. -/ @[to_additive "A more general version of `add_monoid_hom.map_finsum_mem` that requires `s ∩ support f` rather than `s` to be finite."] lemma monoid_hom.map_finprod_mem' {f : α → M} (g : M →* N) (h₀ : (s ∩ mul_support f).finite) : g (∏ᶠ j ∈ s, f j) = ∏ᶠ i ∈ s, g (f i) := begin rw [g.map_finprod], { simp only [g.map_finprod_Prop] }, { simpa only [finprod_eq_mul_indicator_apply, mul_support_mul_indicator] } end /-- Given a monoid homomorphism `g : M →* N` and a function `f : α → M`, the value of `g` at the product of `f i` over `i ∈ s` equals the product of `g (f i)` over `s`. -/ @[to_additive "Given an additive monoid homomorphism `g : M →* N` and a function `f : α → M`, the value of `g` at the sum of `f i` over `i ∈ s` equals the sum of `g (f i)` over `s`."] lemma monoid_hom.map_finprod_mem (f : α → M) (g : M →* N) (hs : s.finite) : g (∏ᶠ j ∈ s, f j) = ∏ᶠ i ∈ s, g (f i) := g.map_finprod_mem' (hs.inter_of_left _) @[to_additive] lemma mul_equiv.map_finprod_mem (g : M ≃* N) (f : α → M) {s : set α} (hs : s.finite) : g (∏ᶠ i ∈ s, f i) = ∏ᶠ i ∈ s, g (f i) := g.to_monoid_hom.map_finprod_mem f hs @[to_additive] lemma finprod_mem_inv_distrib [division_comm_monoid G] (f : α → G) (hs : s.finite) : ∏ᶠ x ∈ s, (f x)⁻¹ = (∏ᶠ x ∈ s, f x)⁻¹ := ((mul_equiv.inv G).map_finprod_mem f hs).symm /-- Given a finite set `s`, the product of `f i / g i` over `i ∈ s` equals the product of `f i` over `i ∈ s` divided by the product of `g i` over `i ∈ s`. -/ @[to_additive "Given a finite set `s`, the sum of `f i / g i` over `i ∈ s` equals the sum of `f i` over `i ∈ s` minus the sum of `g i` over `i ∈ s`."] lemma finprod_mem_div_distrib [division_comm_monoid G] (f g : α → G) (hs : s.finite) : ∏ᶠ i ∈ s, f i / g i = (∏ᶠ i ∈ s, f i) / ∏ᶠ i ∈ s, g i := by simp only [div_eq_mul_inv, finprod_mem_mul_distrib hs, finprod_mem_inv_distrib g hs] /-! ### `∏ᶠ x ∈ s, f x` and set operations -/ /-- The product of any function over an empty set is `1`. -/ @[to_additive "The sum of any function over an empty set is `0`."] lemma finprod_mem_empty : ∏ᶠ i ∈ (∅ : set α), f i = 1 := by simp /-- A set `s` is nonempty if the product of some function over `s` is not equal to `1`. -/ @[to_additive "A set `s` is nonempty if the sum of some function over `s` is not equal to `0`."] lemma nonempty_of_finprod_mem_ne_one (h : ∏ᶠ i ∈ s, f i ≠ 1) : s.nonempty := nonempty_iff_ne_empty.2 $ λ h', h $ h'.symm ▸ finprod_mem_empty /-- Given finite sets `s` and `t`, the product of `f i` over `i ∈ s ∪ t` times the product of `f i` over `i ∈ s ∩ t` equals the product of `f i` over `i ∈ s` times the product of `f i` over `i ∈ t`. -/ @[to_additive "Given finite sets `s` and `t`, the sum of `f i` over `i ∈ s ∪ t` plus the sum of `f i` over `i ∈ s ∩ t` equals the sum of `f i` over `i ∈ s` plus the sum of `f i` over `i ∈ t`."] lemma finprod_mem_union_inter (hs : s.finite) (ht : t.finite) : (∏ᶠ i ∈ s ∪ t, f i) * ∏ᶠ i ∈ s ∩ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := begin lift s to finset α using hs, lift t to finset α using ht, classical, rw [← finset.coe_union, ← finset.coe_inter], simp only [finprod_mem_coe_finset, finset.prod_union_inter] end /-- A more general version of `finprod_mem_union_inter` that requires `s ∩ mul_support f` and `t ∩ mul_support f` rather than `s` and `t` to be finite. -/ @[to_additive "A more general version of `finsum_mem_union_inter` that requires `s ∩ support f` and `t ∩ support f` rather than `s` and `t` to be finite."] lemma finprod_mem_union_inter' (hs : (s ∩ mul_support f).finite) (ht : (t ∩ mul_support f).finite) : (∏ᶠ i ∈ s ∪ t, f i) * ∏ᶠ i ∈ s ∩ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := begin rw [← finprod_mem_inter_mul_support f s, ← finprod_mem_inter_mul_support f t, ← finprod_mem_union_inter hs ht, ← union_inter_distrib_right, finprod_mem_inter_mul_support, ← finprod_mem_inter_mul_support f (s ∩ t)], congr' 2, rw [inter_left_comm, inter_assoc, inter_assoc, inter_self, inter_left_comm] end /-- A more general version of `finprod_mem_union` that requires `s ∩ mul_support f` and `t ∩ mul_support f` rather than `s` and `t` to be finite. -/ @[to_additive "A more general version of `finsum_mem_union` that requires `s ∩ support f` and `t ∩ support f` rather than `s` and `t` to be finite."] lemma finprod_mem_union' (hst : disjoint s t) (hs : (s ∩ mul_support f).finite) (ht : (t ∩ mul_support f).finite) : ∏ᶠ i ∈ s ∪ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := by rw [← finprod_mem_union_inter' hs ht, disjoint_iff_inter_eq_empty.1 hst, finprod_mem_empty, mul_one] /-- Given two finite disjoint sets `s` and `t`, the product of `f i` over `i ∈ s ∪ t` equals the product of `f i` over `i ∈ s` times the product of `f i` over `i ∈ t`. -/ @[to_additive "Given two finite disjoint sets `s` and `t`, the sum of `f i` over `i ∈ s ∪ t` equals the sum of `f i` over `i ∈ s` plus the sum of `f i` over `i ∈ t`."] lemma finprod_mem_union (hst : disjoint s t) (hs : s.finite) (ht : t.finite) : ∏ᶠ i ∈ s ∪ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := finprod_mem_union' hst (hs.inter_of_left _) (ht.inter_of_left _) /-- A more general version of `finprod_mem_union'` that requires `s ∩ mul_support f` and `t ∩ mul_support f` rather than `s` and `t` to be disjoint -/ @[to_additive "A more general version of `finsum_mem_union'` that requires `s ∩ support f` and `t ∩ support f` rather than `s` and `t` to be disjoint"] lemma finprod_mem_union'' (hst : disjoint (s ∩ mul_support f) (t ∩ mul_support f)) (hs : (s ∩ mul_support f).finite) (ht : (t ∩ mul_support f).finite) : ∏ᶠ i ∈ s ∪ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := by rw [← finprod_mem_inter_mul_support f s, ← finprod_mem_inter_mul_support f t, ← finprod_mem_union hst hs ht, ← union_inter_distrib_right, finprod_mem_inter_mul_support] /-- The product of `f i` over `i ∈ {a}` equals `f a`. -/ @[to_additive "The sum of `f i` over `i ∈ {a}` equals `f a`."] lemma finprod_mem_singleton : ∏ᶠ i ∈ ({a} : set α), f i = f a := by rw [← finset.coe_singleton, finprod_mem_coe_finset, finset.prod_singleton] @[simp, to_additive] lemma finprod_cond_eq_left : ∏ᶠ i = a, f i = f a := finprod_mem_singleton @[simp, to_additive] lemma finprod_cond_eq_right : ∏ᶠ i (hi : a = i), f i = f a := by simp [@eq_comm _ a] /-- A more general version of `finprod_mem_insert` that requires `s ∩ mul_support f` rather than `s` to be finite. -/ @[to_additive "A more general version of `finsum_mem_insert` that requires `s ∩ support f` rather than `s` to be finite."] lemma finprod_mem_insert' (f : α → M) (h : a ∉ s) (hs : (s ∩ mul_support f).finite) : ∏ᶠ i ∈ insert a s, f i = f a * ∏ᶠ i ∈ s, f i := begin rw [insert_eq, finprod_mem_union' _ _ hs, finprod_mem_singleton], { rwa disjoint_singleton_left }, { exact (finite_singleton a).inter_of_left _ } end /-- Given a finite set `s` and an element `a ∉ s`, the product of `f i` over `i ∈ insert a s` equals `f a` times the product of `f i` over `i ∈ s`. -/ @[to_additive "Given a finite set `s` and an element `a ∉ s`, the sum of `f i` over `i ∈ insert a s` equals `f a` plus the sum of `f i` over `i ∈ s`."] lemma finprod_mem_insert (f : α → M) (h : a ∉ s) (hs : s.finite) : ∏ᶠ i ∈ insert a s, f i = f a * ∏ᶠ i ∈ s, f i := finprod_mem_insert' f h $ hs.inter_of_left _ /-- If `f a = 1` when `a ∉ s`, then the product of `f i` over `i ∈ insert a s` equals the product of `f i` over `i ∈ s`. -/ @[to_additive "If `f a = 0` when `a ∉ s`, then the sum of `f i` over `i ∈ insert a s` equals the sum of `f i` over `i ∈ s`."] lemma finprod_mem_insert_of_eq_one_if_not_mem (h : a ∉ s → f a = 1) : ∏ᶠ i ∈ insert a s, f i = ∏ᶠ i ∈ s, f i := begin refine finprod_mem_inter_mul_support_eq' _ _ _ (λ x hx, ⟨_, or.inr⟩), rintro (rfl|hxs), exacts [not_imp_comm.1 h hx, hxs] end /-- If `f a = 1`, then the product of `f i` over `i ∈ insert a s` equals the product of `f i` over `i ∈ s`. -/ @[to_additive "If `f a = 0`, then the sum of `f i` over `i ∈ insert a s` equals the sum of `f i` over `i ∈ s`."] lemma finprod_mem_insert_one (h : f a = 1) : ∏ᶠ i ∈ insert a s, f i = ∏ᶠ i ∈ s, f i := finprod_mem_insert_of_eq_one_if_not_mem (λ _, h) /-- If the multiplicative support of `f` is finite, then for every `x` in the domain of `f`, `f x` divides `finprod f`. -/ lemma finprod_mem_dvd {f : α → N} (a : α) (hf : (mul_support f).finite) : f a ∣ finprod f := begin by_cases ha : a ∈ mul_support f, { rw finprod_eq_prod_of_mul_support_to_finset_subset f hf (set.subset.refl _), exact finset.dvd_prod_of_mem f ((finite.mem_to_finset hf).mpr ha) }, { rw nmem_mul_support.mp ha, exact one_dvd (finprod f) } end /-- The product of `f i` over `i ∈ {a, b}`, `a ≠ b`, is equal to `f a * f b`. -/ @[to_additive "The sum of `f i` over `i ∈ {a, b}`, `a ≠ b`, is equal to `f a + f b`."] lemma finprod_mem_pair (h : a ≠ b) : ∏ᶠ i ∈ ({a, b} : set α), f i = f a * f b := by { rw [finprod_mem_insert, finprod_mem_singleton], exacts [h, finite_singleton b] } /-- The product of `f y` over `y ∈ g '' s` equals the product of `f (g i)` over `s` provided that `g` is injective on `s ∩ mul_support (f ∘ g)`. -/ @[to_additive "The sum of `f y` over `y ∈ g '' s` equals the sum of `f (g i)` over `s` provided that `g` is injective on `s ∩ support (f ∘ g)`."] lemma finprod_mem_image' {s : set β} {g : β → α} (hg : (s ∩ mul_support (f ∘ g)).inj_on g) : ∏ᶠ i ∈ g '' s, f i = ∏ᶠ j ∈ s, f (g j) := begin classical, by_cases hs : (s ∩ mul_support (f ∘ g)).finite, { have hg : ∀ (x ∈ hs.to_finset) (y ∈ hs.to_finset), g x = g y → x = y, by simpa only [hs.mem_to_finset], rw [finprod_mem_eq_prod _ hs, ← finset.prod_image hg], refine finprod_mem_eq_prod_of_inter_mul_support_eq f _, rw [finset.coe_image, hs.coe_to_finset, ← image_inter_mul_support_eq, inter_assoc, inter_self] }, { rw [finprod_mem_eq_one_of_infinite hs, finprod_mem_eq_one_of_infinite], rwa [image_inter_mul_support_eq, infinite_image_iff hg] } end /-- The product of `f y` over `y ∈ g '' s` equals the product of `f (g i)` over `s` provided that `g` is injective on `s`. -/ @[to_additive "The sum of `f y` over `y ∈ g '' s` equals the sum of `f (g i)` over `s` provided that `g` is injective on `s`."] lemma finprod_mem_image {s : set β} {g : β → α} (hg : s.inj_on g) : ∏ᶠ i ∈ g '' s, f i = ∏ᶠ j ∈ s, f (g j) := finprod_mem_image' $ hg.mono $ inter_subset_left _ _ /-- The product of `f y` over `y ∈ set.range g` equals the product of `f (g i)` over all `i` provided that `g` is injective on `mul_support (f ∘ g)`. -/ @[to_additive "The sum of `f y` over `y ∈ set.range g` equals the sum of `f (g i)` over all `i` provided that `g` is injective on `support (f ∘ g)`."] lemma finprod_mem_range' {g : β → α} (hg : (mul_support (f ∘ g)).inj_on g) : ∏ᶠ i ∈ range g, f i = ∏ᶠ j, f (g j) := begin rw [← image_univ, finprod_mem_image', finprod_mem_univ], rwa univ_inter end /-- The product of `f y` over `y ∈ set.range g` equals the product of `f (g i)` over all `i` provided that `g` is injective. -/ @[to_additive "The sum of `f y` over `y ∈ set.range g` equals the sum of `f (g i)` over all `i` provided that `g` is injective."] lemma finprod_mem_range {g : β → α} (hg : injective g) : ∏ᶠ i ∈ range g, f i = ∏ᶠ j, f (g j) := finprod_mem_range' (hg.inj_on _) /-- See also `finset.prod_bij`. -/ @[to_additive "See also `finset.sum_bij`."] lemma finprod_mem_eq_of_bij_on {s : set α} {t : set β} {f : α → M} {g : β → M} (e : α → β) (he₀ : s.bij_on e t) (he₁ : ∀ x ∈ s, f x = g (e x)) : ∏ᶠ i ∈ s, f i = ∏ᶠ j ∈ t, g j := begin rw [← set.bij_on.image_eq he₀, finprod_mem_image he₀.2.1], exact finprod_mem_congr rfl he₁ end /-- See `finprod_comp`, `fintype.prod_bijective` and `finset.prod_bij`. -/ @[to_additive "See `finsum_comp`, `fintype.sum_bijective` and `finset.sum_bij`."] lemma finprod_eq_of_bijective {f : α → M} {g : β → M} (e : α → β) (he₀ : bijective e) (he₁ : ∀ x, f x = g (e x)) : ∏ᶠ i, f i = ∏ᶠ j, g j := begin rw [← finprod_mem_univ f, ← finprod_mem_univ g], exact finprod_mem_eq_of_bij_on _ (bijective_iff_bij_on_univ.mp he₀) (λ x _, he₁ x), end /-- See also `finprod_eq_of_bijective`, `fintype.prod_bijective` and `finset.prod_bij`. -/ @[to_additive "See also `finsum_eq_of_bijective`, `fintype.sum_bijective` and `finset.sum_bij`."] lemma finprod_comp {g : β → M} (e : α → β) (he₀ : function.bijective e) : ∏ᶠ i, g (e i) = ∏ᶠ j, g j := finprod_eq_of_bijective e he₀ (λ x, rfl) @[to_additive] lemma finprod_comp_equiv (e : α ≃ β) {f : β → M} : ∏ᶠ i, f (e i) = ∏ᶠ i', f i' := finprod_comp e e.bijective @[to_additive] lemma finprod_set_coe_eq_finprod_mem (s : set α) : ∏ᶠ j : s, f j = ∏ᶠ i ∈ s, f i := begin rw [← finprod_mem_range, subtype.range_coe], exact subtype.coe_injective end @[to_additive] lemma finprod_subtype_eq_finprod_cond (p : α → Prop) : ∏ᶠ j : subtype p, f j = ∏ᶠ i (hi : p i), f i := finprod_set_coe_eq_finprod_mem {i | p i} @[to_additive] lemma finprod_mem_inter_mul_diff' (t : set α) (h : (s ∩ mul_support f).finite) : (∏ᶠ i ∈ s ∩ t, f i) * ∏ᶠ i ∈ s \ t, f i = ∏ᶠ i ∈ s, f i := begin rw [← finprod_mem_union', inter_union_diff], rw disjoint_iff_inf_le, exacts [λ x hx, hx.2.2 hx.1.2, h.subset (λ x hx, ⟨hx.1.1, hx.2⟩), h.subset (λ x hx, ⟨hx.1.1, hx.2⟩)], end @[to_additive] lemma finprod_mem_inter_mul_diff (t : set α) (h : s.finite) : (∏ᶠ i ∈ s ∩ t, f i) * ∏ᶠ i ∈ s \ t, f i = ∏ᶠ i ∈ s, f i := finprod_mem_inter_mul_diff' _ $ h.inter_of_left _ /-- A more general version of `finprod_mem_mul_diff` that requires `t ∩ mul_support f` rather than `t` to be finite. -/ @[to_additive "A more general version of `finsum_mem_add_diff` that requires `t ∩ support f` rather than `t` to be finite."] lemma finprod_mem_mul_diff' (hst : s ⊆ t) (ht : (t ∩ mul_support f).finite) : (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t \ s, f i = ∏ᶠ i ∈ t, f i := by rw [← finprod_mem_inter_mul_diff' _ ht, inter_eq_self_of_subset_right hst] /-- Given a finite set `t` and a subset `s` of `t`, the product of `f i` over `i ∈ s` times the product of `f i` over `t \ s` equals the product of `f i` over `i ∈ t`. -/ @[to_additive "Given a finite set `t` and a subset `s` of `t`, the sum of `f i` over `i ∈ s` plus the sum of `f i` over `t \\ s` equals the sum of `f i` over `i ∈ t`."] lemma finprod_mem_mul_diff (hst : s ⊆ t) (ht : t.finite) : (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t \ s, f i = ∏ᶠ i ∈ t, f i := finprod_mem_mul_diff' hst (ht.inter_of_left _) /-- Given a family of pairwise disjoint finite sets `t i` indexed by a finite type, the product of `f a` over the union `⋃ i, t i` is equal to the product over all indexes `i` of the products of `f a` over `a ∈ t i`. -/ @[to_additive "Given a family of pairwise disjoint finite sets `t i` indexed by a finite type, the sum of `f a` over the union `⋃ i, t i` is equal to the sum over all indexes `i` of the sums of `f a` over `a ∈ t i`."] lemma finprod_mem_Union [finite ι] {t : ι → set α} (h : pairwise (disjoint on t)) (ht : ∀ i, (t i).finite) : ∏ᶠ a ∈ (⋃ i : ι, t i), f a = ∏ᶠ i, ∏ᶠ a ∈ t i, f a := begin casesI nonempty_fintype ι, lift t to ι → finset α using ht, classical, rw [← bUnion_univ, ← finset.coe_univ, ← finset.coe_bUnion, finprod_mem_coe_finset, finset.prod_bUnion], { simp only [finprod_mem_coe_finset, finprod_eq_prod_of_fintype] }, { exact λ x _ y _ hxy, finset.disjoint_coe.1 (h hxy) } end /-- Given a family of sets `t : ι → set α`, a finite set `I` in the index type such that all sets `t i`, `i ∈ I`, are finite, if all `t i`, `i ∈ I`, are pairwise disjoint, then the product of `f a` over `a ∈ ⋃ i ∈ I, t i` is equal to the product over `i ∈ I` of the products of `f a` over `a ∈ t i`. -/ @[to_additive "Given a family of sets `t : ι → set α`, a finite set `I` in the index type such that all sets `t i`, `i ∈ I`, are finite, if all `t i`, `i ∈ I`, are pairwise disjoint, then the sum of `f a` over `a ∈ ⋃ i ∈ I, t i` is equal to the sum over `i ∈ I` of the sums of `f a` over `a ∈ t i`."] lemma finprod_mem_bUnion {I : set ι} {t : ι → set α} (h : I.pairwise_disjoint t) (hI : I.finite) (ht : ∀ i ∈ I, (t i).finite) : ∏ᶠ a ∈ ⋃ x ∈ I, t x, f a = ∏ᶠ i ∈ I, ∏ᶠ j ∈ t i, f j := begin haveI := hI.fintype, rw [bUnion_eq_Union, finprod_mem_Union, ← finprod_set_coe_eq_finprod_mem], exacts [λ x y hxy, h x.2 y.2 (subtype.coe_injective.ne hxy), λ b, ht b b.2] end /-- If `t` is a finite set of pairwise disjoint finite sets, then the product of `f a` over `a ∈ ⋃₀ t` is the product over `s ∈ t` of the products of `f a` over `a ∈ s`. -/ @[to_additive "If `t` is a finite set of pairwise disjoint finite sets, then the sum of `f a` over `a ∈ ⋃₀ t` is the sum over `s ∈ t` of the sums of `f a` over `a ∈ s`."] lemma finprod_mem_sUnion {t : set (set α)} (h : t.pairwise_disjoint id) (ht₀ : t.finite) (ht₁ : ∀ x ∈ t, set.finite x) : ∏ᶠ a ∈ ⋃₀ t, f a = ∏ᶠ s ∈ t, ∏ᶠ a ∈ s, f a := by { rw set.sUnion_eq_bUnion, exact finprod_mem_bUnion h ht₀ ht₁ } @[to_additive] lemma mul_finprod_cond_ne (a : α) (hf : (mul_support f).finite) : f a * (∏ᶠ i ≠ a, f i) = ∏ᶠ i, f i := begin classical, rw [finprod_eq_prod _ hf], have h : ∀ x : α, f x ≠ 1 → (x ≠ a ↔ x ∈ hf.to_finset \ {a}), { intros x hx, rw [finset.mem_sdiff, finset.mem_singleton, finite.mem_to_finset, mem_mul_support], exact ⟨λ h, and.intro hx h, λ h, h.2⟩,}, rw [finprod_cond_eq_prod_of_cond_iff f h, finset.sdiff_singleton_eq_erase], by_cases ha : a ∈ mul_support f, { apply finset.mul_prod_erase _ _ ((finite.mem_to_finset _ ).mpr ha), }, { rw [mem_mul_support, not_not] at ha, rw [ha, one_mul], apply finset.prod_erase _ ha, } end /-- If `s : set α` and `t : set β` are finite sets, then taking the product over `s` commutes with taking the product over `t`. -/ @[to_additive "If `s : set α` and `t : set β` are finite sets, then summing over `s` commutes with summing over `t`."] lemma finprod_mem_comm {s : set α} {t : set β} (f : α → β → M) (hs : s.finite) (ht : t.finite) : ∏ᶠ i ∈ s, ∏ᶠ j ∈ t, f i j = ∏ᶠ j ∈ t, ∏ᶠ i ∈ s, f i j := begin lift s to finset α using hs, lift t to finset β using ht, simp only [finprod_mem_coe_finset], exact finset.prod_comm end /-- To prove a property of a finite product, it suffices to prove that the property is multiplicative and holds on factors. -/ @[to_additive "To prove a property of a finite sum, it suffices to prove that the property is additive and holds on summands."] lemma finprod_mem_induction (p : M → Prop) (hp₀ : p 1) (hp₁ : ∀ x y, p x → p y → p (x * y)) (hp₂ : ∀ x ∈ s, p $ f x) : p (∏ᶠ i ∈ s, f i) := finprod_induction _ hp₀ hp₁ $ λ x, finprod_induction _ hp₀ hp₁ $ hp₂ x lemma finprod_cond_nonneg {R : Type*} [ordered_comm_semiring R] {p : α → Prop} {f : α → R} (hf : ∀ x, p x → 0 ≤ f x) : 0 ≤ ∏ᶠ x (h : p x), f x := finprod_nonneg $ λ x, finprod_nonneg $ hf x @[to_additive] lemma single_le_finprod {M : Type*} [ordered_comm_monoid M] (i : α) {f : α → M} (hf : (mul_support f).finite) (h : ∀ j, 1 ≤ f j) : f i ≤ ∏ᶠ j, f j := by classical; calc f i ≤ ∏ j in insert i hf.to_finset, f j : finset.single_le_prod' (λ j hj, h j) (finset.mem_insert_self _ _) ... = ∏ᶠ j, f j : (finprod_eq_prod_of_mul_support_to_finset_subset _ hf (finset.subset_insert _ _)).symm lemma finprod_eq_zero {M₀ : Type*} [comm_monoid_with_zero M₀] (f : α → M₀) (x : α) (hx : f x = 0) (hf : (mul_support f).finite) : ∏ᶠ x, f x = 0 := begin nontriviality, rw [finprod_eq_prod f hf], refine finset.prod_eq_zero (hf.mem_to_finset.2 _) hx, simp [hx] end @[to_additive] lemma finprod_prod_comm (s : finset β) (f : α → β → M) (h : ∀ b ∈ s, (mul_support (λ a, f a b)).finite) : ∏ᶠ a : α, ∏ b in s, f a b = ∏ b in s, ∏ᶠ a : α, f a b := begin have hU : mul_support (λ a, ∏ b in s, f a b) ⊆ (s.finite_to_set.bUnion (λ b hb, h b (finset.mem_coe.1 hb))).to_finset, { rw finite.coe_to_finset, intros x hx, simp only [exists_prop, mem_Union, ne.def, mem_mul_support, finset.mem_coe], contrapose! hx, rw [mem_mul_support, not_not, finset.prod_congr rfl hx, finset.prod_const_one] }, rw [finprod_eq_prod_of_mul_support_subset _ hU, finset.prod_comm], refine finset.prod_congr rfl (λ b hb, (finprod_eq_prod_of_mul_support_subset _ _).symm), intros a ha, simp only [finite.coe_to_finset, mem_Union], exact ⟨b, hb, ha⟩ end @[to_additive] lemma prod_finprod_comm (s : finset α) (f : α → β → M) (h : ∀ a ∈ s, (mul_support (f a)).finite) : ∏ a in s, ∏ᶠ b : β, f a b = ∏ᶠ b : β, ∏ a in s, f a b := (finprod_prod_comm s (λ b a, f a b) h).symm lemma mul_finsum {R : Type*} [semiring R] (f : α → R) (r : R) (h : (support f).finite) : r * ∑ᶠ a : α, f a = ∑ᶠ a : α, r * f a := (add_monoid_hom.mul_left r).map_finsum h lemma finsum_mul {R : Type*} [semiring R] (f : α → R) (r : R) (h : (support f).finite) : (∑ᶠ a : α, f a) * r = ∑ᶠ a : α, f a * r := (add_monoid_hom.mul_right r).map_finsum h @[to_additive] lemma finset.mul_support_of_fiberwise_prod_subset_image [decidable_eq β] (s : finset α) (f : α → M) (g : α → β) : mul_support (λ b, (s.filter (λ a, g a = b)).prod f) ⊆ s.image g := begin simp only [finset.coe_image, set.mem_image, finset.mem_coe, function.support_subset_iff], intros b h, suffices : (s.filter (λ (a : α), g a = b)).nonempty, { simpa only [s.fiber_nonempty_iff_mem_image g b, finset.mem_image, exists_prop], }, exact finset.nonempty_of_prod_ne_one h, end /-- Note that `b ∈ (s.filter (λ ab, prod.fst ab = a)).image prod.snd` iff `(a, b) ∈ s` so we can simplify the right hand side of this lemma. However the form stated here is more useful for iterating this lemma, e.g., if we have `f : α × β × γ → M`. -/ @[to_additive "Note that `b ∈ (s.filter (λ ab, prod.fst ab = a)).image prod.snd` iff `(a, b) ∈ s` so we can simplify the right hand side of this lemma. However the form stated here is more useful for iterating this lemma, e.g., if we have `f : α × β × γ → M`."] lemma finprod_mem_finset_product' [decidable_eq α] [decidable_eq β] (s : finset (α × β)) (f : α × β → M) : ∏ᶠ ab (h : ab ∈ s), f ab = ∏ᶠ a b (h : b ∈ (s.filter (λ ab, prod.fst ab = a)).image prod.snd), f (a, b) := begin have : ∀ a, ∏ (i : β) in (s.filter (λ ab, prod.fst ab = a)).image prod.snd, f (a, i) = (finset.filter (λ ab, prod.fst ab = a) s).prod f, { refine (λ a, finset.prod_bij (λ b _, (a, b)) _ _ _ _); -- `finish` closes these goals try { simp, done }, suffices : ∀ a' b, (a', b) ∈ s → a' = a → (a, b) ∈ s ∧ a' = a, by simpa, rintros a' b hp rfl, exact ⟨hp, rfl⟩ }, rw finprod_mem_finset_eq_prod, simp_rw [finprod_mem_finset_eq_prod, this], rw [finprod_eq_prod_of_mul_support_subset _ (s.mul_support_of_fiberwise_prod_subset_image f prod.fst), ← finset.prod_fiberwise_of_maps_to _ f], -- `finish` could close the goal here simp only [finset.mem_image, prod.mk.eta], exact λ x hx, ⟨x, hx, rfl⟩, end /-- See also `finprod_mem_finset_product'`. -/ @[to_additive "See also `finsum_mem_finset_product'`."] lemma finprod_mem_finset_product (s : finset (α × β)) (f : α × β → M) : ∏ᶠ ab (h : ab ∈ s), f ab = ∏ᶠ a b (h : (a, b) ∈ s), f (a, b) := by { classical, rw finprod_mem_finset_product', simp, } @[to_additive] lemma finprod_mem_finset_product₃ {γ : Type*} (s : finset (α × β × γ)) (f : α × β × γ → M) : ∏ᶠ abc (h : abc ∈ s), f abc = ∏ᶠ a b c (h : (a, b, c) ∈ s), f (a, b, c) := by { classical, rw finprod_mem_finset_product', simp_rw finprod_mem_finset_product', simp, } @[to_additive] lemma finprod_curry (f : α × β → M) (hf : (mul_support f).finite) : ∏ᶠ ab, f ab = ∏ᶠ a b, f (a, b) := begin have h₁ : ∀ a, ∏ᶠ (h : a ∈ hf.to_finset), f a = f a, { simp, }, have h₂ : ∏ᶠ a, f a = ∏ᶠ a (h : a ∈ hf.to_finset), f a, { simp, }, simp_rw [h₂, finprod_mem_finset_product, h₁], end @[to_additive] lemma finprod_curry₃ {γ : Type*} (f : α × β × γ → M) (h : (mul_support f).finite) : ∏ᶠ abc, f abc = ∏ᶠ a b c, f (a, b, c) := by { rw finprod_curry f h, congr, ext a, rw finprod_curry, simp [h], } @[to_additive] lemma finprod_dmem {s : set α} [decidable_pred (∈ s)] (f : (Π (a : α), a ∈ s → M)) : ∏ᶠ (a : α) (h : a ∈ s), f a h = ∏ᶠ (a : α) (h : a ∈ s), if h' : a ∈ s then f a h' else 1 := finprod_congr (λ a, finprod_congr (λ ha, (dif_pos ha).symm)) @[to_additive] lemma finprod_emb_domain' {f : α → β} (hf : injective f) [decidable_pred (∈ set.range f)] (g : α → M) : ∏ᶠ (b : β), (if h : b ∈ set.range f then g (classical.some h) else 1) = ∏ᶠ (a : α), g a := begin simp_rw [← finprod_eq_dif], rw [finprod_dmem, finprod_mem_range hf, finprod_congr (λ a, _)], rw [dif_pos (set.mem_range_self a), hf (classical.some_spec (set.mem_range_self a))] end @[to_additive] lemma finprod_emb_domain (f : α ↪ β) [decidable_pred (∈ set.range f)] (g : α → M) : ∏ᶠ (b : β), (if h : b ∈ set.range f then g (classical.some h) else 1) = ∏ᶠ (a : α), g a := finprod_emb_domain' f.injective g end type
220e2ffcf40183ba1e12e71cdd7555eaed2daa2d
3ed5a65c1ab3ce5d1a094edce8fa3287980f197b
/src/herstein/ex2_3/Q_02.lean
8064c5218677091f65bcf1a5a456cee80e269d47
[]
no_license
group-study-group/herstein
35d32e77158efa2cc303c84e1ee5e3bc80831137
f5a1a72eb56fa19c19ece0cb3ab6cf7ffd161f66
refs/heads/master
1,586,202,191,519
1,548,969,759,000
1,548,969,759,000
157,746,953
0
0
null
1,542,412,901,000
1,542,302,366,000
Lean
UTF-8
Lean
false
false
769
lean
import algebra.group algebra.group_power variable A: Type* theorem Q_02 (a b: A) [comm_group A]: ∀ n: ℕ, (a * b) ^ n = a ^ n * b ^ n | 0 := calc (a * b) ^ 0 = 1 : by rw pow_zero ... = 1 * 1 : (mul_one 1).symm ... = (a ^ 0) * (b ^ 0) : by rw [pow_zero, pow_zero] | (k + 1) := calc (a * b) ^ (k + 1) = a * b * (a * b) ^ k : pow_succ (a * b) k ... = b * a * (a * b) ^ k : by rw mul_comm a b ... = b * a * (a ^ k * b ^ k) : by rw (Q_02 k) ... = b * (a * a ^ k) * b ^ k : by rw [← mul_assoc, mul_assoc b a (a ^ k)] ... = b * a ^ (k + 1) * b ^ k : by rw pow_succ ... = a ^ (k + 1) * (b * b ^ k) : by rw [mul_comm b (a ^ (k + 1)), mul_assoc] ... = a ^ (k + 1) * b ^ (k + 1) : by rw [←pow_succ]
d6656ce2798585468075ec03ba728fa844533b08
d0f9af2b0ace5ce352570d61b09019c8ef4a3b96
/hw9/induction.lean
d95482fee841baa3958fa60b6c0aceff888f4b08
[]
no_license
jngo13/Discrete-Mathematics
8671540ef2da7c75915d32332dd20c02f001474e
bf674a866e61f60e6e6d128df85fa73819091787
refs/heads/master
1,675,615,657,924
1,609,142,011,000
1,609,142,011,000
267,190,341
0
0
null
null
null
null
UTF-8
Lean
false
false
4,453
lean
/- Proof strategies. - direct proof: use established facts - by negation: to prove ¬ P, assume P; show that this yields a contradiction, from which a proof of false can then be derived. This shows P → false, and that is the definition of ¬ P. - by contradiction : to prove P, assume ¬ P and show that this leads to a contradiction, from which a proof of false can be derived. This proves ¬ P → false, which is equivalent to ¬ ¬ P. The apply the *classical* rule of negation elimination to deduce P.with - Classical rule of negation elimination: ∀ (P : Prop), ¬ ¬ P ¬ P Prove 0 = 0 by contradiction We want to prove P (0 = 0). Assume ¬ 0 = 0, and show that this leads to a contradiction. But by the reflexive property of equality, which says everything is equal to itself, we know timmedate that 0 = 0. That gives us a direct contradiction between ¬ (0 = 0) and (0 = 0). From such a contradiction we can derive a proof of false, showing that ¬ (0 = 0) → false. And this just means ¬ (0 = 0). Then by the classical negation elimination, this imlies 0 = 0. - Today: proof by induction. -/ -- Answer to question about why contradictions imply false axiom P : Prop axiom p : P axiom np : ¬ P -- P → false #check np p /- Proof by negation english language example Prove that the square root of two is NOT rational Prove ¬ sqrt(2) is rational.#check Proof: Assume sqrt(2) is rational. In this case, we can write sqrt(2) as some fraction a/b. We now want to show that this leads to a contradiction... Proof by negation ≠ proof by contradiction -/ /- Prove this: ∀ (m : ℕ), 0 + m = m Proof: By the definition of addition, and specifically by the first of the two cases, which tells us that for any m, 0 + m = m. -/ def my_add : ℕ → ℕ → ℕ | nat.zero m := m | (nat.succ n') m := nat.succ (my_add n' m) -- 0 + m = m -- (1 + n') + m = 1 + (n' + m) /- Not needed for final example : ∀ (m : ℕ), my_add 0 m = m | _ := by simp [my_add] -/ /- Many proofs are accomplished by mere simplification of both sides of some equation *using function definitions* that are already known. -/ /- If one is being precise, however, there are some unexpected consequences. One is that sometimes something that looks easy turns out to be a bit more complicated. For example, try to prove this (using) only what we know so far. -/ example : ∀ (n : ℕ), my_add n 0 = n | _ := by simp [my_add] /- We have no rule (yet) for adding zero on the right, so simplifying using the definition of my_add doesn't work.with Instead, we need to try a whole different proof strategy. -/ /- Here's the idea: - every inductively defined type has a corresponding induction rule - It's a rule for showing that some proposition is true for *every* value of the given type - The induction principle for ℕ is this: ∀ P : ℕ → Prop, P 0 → ((∀ n' : ℕ), P n' → P (nat.succ n')) → ∀ n, P n In other words, for any predicate/property P, to show ∀ (n : ℕ), P n, it *suffices* to show the following: (1) P 0, (2) (∀ n' : ℕ), P n' → P (nat.succ n') -/ /- Example: We want to prove ∀ n, n + 0 = n. (1) Case: n = 0. By first rule of my_add. (2) Case: n = nat.succ n': Show P n' → P (nat.succ n'). What does P n' say? my_add n' 0 = n' So *assume* P n', now show P (nat.succ n') That is, show (my_add (nat.succ n') 0 = (nat.succ n')) Using my_add Simp: nat.succ (my_add n' 0) = nat.succ n' Apply induction hypothesis: nat.succ n' = nat.succ n' Finish by reflexive property of equality -/ /- Sum numbers from 0 to n is n * (succ n) / 2. Proof by induction. We will apply the principle of induction for the natural numbers to two smaller proofs: one for n = 0, and one that shows that if the formula is true for some n > 0, then it must also be true succ n'. Base case: prove (P 0): show sum from 0 to 0 = (0 * 1)/ 2 = 0 Inductive case: Show P n' → P (n' + 1) Assume P n'. The sum from 0 to n' = n' * (n' + 1) / 2. Show (P (n' + 1)): The sum from 0 to n'++1 = (n' + 1)((n' +1)+1)/2. 1+2+3+4+5 if we assume this is 5*6/2 (1+2+3+4+5)+6 show this is 6*7/2 sum 0 to 5 + 6! = 5*6/2 +6 The sum from 0 to (n' + 1) = sum from 0 to n' + (n' + 1) = (n'*(n+1)/2) + (n' + 1) ... = (n' + 1)((n'+1)+1)/2 -/
25f2efcc666ee306997bd1eaa5cfa5b1a87ba7eb
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/algebraic_geometry/stalks.lean
6c9e49f6a2a82233a1645dcc4b5e61df8574c93b
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
2,094
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebraic_geometry.presheafed_space import topology.sheaves.stalks /-! # Stalks for presheaved spaces This file lifts constructions of stalks and pushforwards of stalks to work with the category of presheafed spaces. -/ universes v u v' u' open category_theory open category_theory.limits category_theory.category category_theory.functor open algebraic_geometry open topological_space variables {C : Type u} [category.{v} C] [has_colimits C] local attribute [tidy] tactic.op_induction' open Top.presheaf namespace algebraic_geometry.PresheafedSpace def stalk (X : PresheafedSpace C) (x : X) : C := X.𝒪.stalk x def stalk_map {X Y : PresheafedSpace C} (α : X ⟶ Y) (x : X) : Y.stalk (α.base x) ⟶ X.stalk x := (stalk_functor C (α.base x)).map (α.c) ≫ X.𝒪.stalk_pushforward C α.base x namespace stalk_map @[simp] lemma id (X : PresheafedSpace C) (x : X) : stalk_map (𝟙 X) x = 𝟙 (X.stalk x) := begin dsimp [stalk_map], simp only [stalk_pushforward.id], rw [←map_comp], convert (stalk_functor C x).map_id X.𝒪, tidy, end -- TODO understand why this proof is still gross (i.e. requires using `erw`) @[simp] lemma comp {X Y Z : PresheafedSpace C} (α : X ⟶ Y) (β : Y ⟶ Z) (x : X) : stalk_map (α ≫ β) x = (stalk_map β (α.base x) : Z.stalk (β.base (α.base x)) ⟶ Y.stalk (α.base x)) ≫ (stalk_map α x : Y.stalk (α.base x) ⟶ X.stalk x) := begin dsimp [stalk_map, stalk_functor, stalk_pushforward], ext U, op_induction U, cases U, simp only [colimit.ι_map_assoc, colimit.ι_pre_assoc, colimit.ι_pre, whisker_left_app, whisker_right_app, assoc, id_comp, map_id, map_comp], dsimp, simp only [map_id, assoc, pushforward.comp_inv_app], -- FIXME Why doesn't simp do this: erw [category_theory.functor.map_id], erw [category_theory.functor.map_id], erw [id_comp, id_comp, id_comp], end end stalk_map end algebraic_geometry.PresheafedSpace
42bb01eeda530d804b05fcfb469402cff5697f6e
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/analysis/calculus/lhopital.lean
7291cda2f3529d9bd4bcfeddbbc8363909bf2897
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
25,020
lean
/- Copyright (c) 2020 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker -/ import analysis.calculus.mean_value /-! # L'Hôpital's rule for 0/0 indeterminate forms In this file, we prove several forms of "L'Hopital's rule" for computing 0/0 indeterminate forms. The proof of `has_deriv_at.lhopital_zero_right_on_Ioo` is based on the one given in the corresponding [Wikibooks](https://en.wikibooks.org/wiki/Calculus/L%27H%C3%B4pital%27s_Rule) chapter, and all other statements are derived from this one by composing by carefully chosen functions. Note that the filter `f'/g'` tends to isn't required to be one of `𝓝 a`, `at_top` or `at_bot`. In fact, we give a slightly stronger statement by allowing it to be any filter on `ℝ`. Each statement is available in a `has_deriv_at` form and a `deriv` form, which is denoted by each statement being in either the `has_deriv_at` or the `deriv` namespace. ## Tags L'Hôpital's rule, L'Hopital's rule -/ open filter set open_locale filter topological_space pointwise variables {a b : ℝ} (hab : a < b) {l : filter ℝ} {f f' g g' : ℝ → ℝ} /-! ## Interval-based versions We start by proving statements where all conditions (derivability, `g' ≠ 0`) have to be satisfied on an explicitely-provided interval. -/ namespace has_deriv_at include hab theorem lhopital_zero_right_on_Ioo (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hgg' : ∀ x ∈ Ioo a b, has_deriv_at g (g' x) x) (hg' : ∀ x ∈ Ioo a b, g' x ≠ 0) (hfa : tendsto f (𝓝[Ioi a] a) (𝓝 0)) (hga : tendsto g (𝓝[Ioi a] a) (𝓝 0)) (hdiv : tendsto (λ x, (f' x) / (g' x)) (𝓝[Ioi a] a) l) : tendsto (λ x, (f x) / (g x)) (𝓝[Ioi a] a) l := begin have sub : ∀ x ∈ Ioo a b, Ioo a x ⊆ Ioo a b := λ x hx, Ioo_subset_Ioo (le_refl a) (le_of_lt hx.2), have hg : ∀ x ∈ (Ioo a b), g x ≠ 0, { intros x hx h, have : tendsto g (𝓝[Iio x] x) (𝓝 0), { rw [← h, ← nhds_within_Ioo_eq_nhds_within_Iio hx.1], exact ((hgg' x hx).continuous_at.continuous_within_at.mono $ sub x hx).tendsto }, obtain ⟨y, hyx, hy⟩ : ∃ c ∈ Ioo a x, g' c = 0, from exists_has_deriv_at_eq_zero' hx.1 hga this (λ y hy, hgg' y $ sub x hx hy), exact hg' y (sub x hx hyx) hy }, have : ∀ x ∈ Ioo a b, ∃ c ∈ Ioo a x, (f x) * (g' c) = (g x) * (f' c), { intros x hx, rw [← sub_zero (f x), ← sub_zero (g x)], exact exists_ratio_has_deriv_at_eq_ratio_slope' g g' hx.1 f f' (λ y hy, hgg' y $ sub x hx hy) (λ y hy, hff' y $ sub x hx hy) hga hfa (tendsto_nhds_within_of_tendsto_nhds (hgg' x hx).continuous_at.tendsto) (tendsto_nhds_within_of_tendsto_nhds (hff' x hx).continuous_at.tendsto) }, choose! c hc using this, have : ∀ x ∈ Ioo a b, ((λ x', (f' x') / (g' x')) ∘ c) x = f x / g x, { intros x hx, rcases hc x hx with ⟨h₁, h₂⟩, field_simp [hg x hx, hg' (c x) ((sub x hx) h₁)], simp only [h₂], rwa mul_comm }, have cmp : ∀ x ∈ Ioo a b, a < c x ∧ c x < x, from λ x hx, (hc x hx).1, rw ← nhds_within_Ioo_eq_nhds_within_Ioi hab, apply tendsto_nhds_within_congr this, simp only, apply hdiv.comp, refine tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ (tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds (tendsto_nhds_within_of_tendsto_nhds tendsto_id) _ _) _, all_goals { apply eventually_nhds_with_of_forall, intros x hx, have := cmp x hx, try {simp}, linarith [this] } end theorem lhopital_zero_right_on_Ico (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hgg' : ∀ x ∈ Ioo a b, has_deriv_at g (g' x) x) (hcf : continuous_on f (Ico a b)) (hcg : continuous_on g (Ico a b)) (hg' : ∀ x ∈ Ioo a b, g' x ≠ 0) (hfa : f a = 0) (hga : g a = 0) (hdiv : tendsto (λ x, (f' x) / (g' x)) (nhds_within a (Ioi a)) l) : tendsto (λ x, (f x) / (g x)) (nhds_within a (Ioi a)) l := begin refine lhopital_zero_right_on_Ioo hab hff' hgg' hg' _ _ hdiv, { rw [← hfa, ← nhds_within_Ioo_eq_nhds_within_Ioi hab], exact ((hcf a $ left_mem_Ico.mpr hab).mono Ioo_subset_Ico_self).tendsto }, { rw [← hga, ← nhds_within_Ioo_eq_nhds_within_Ioi hab], exact ((hcg a $ left_mem_Ico.mpr hab).mono Ioo_subset_Ico_self).tendsto }, end theorem lhopital_zero_left_on_Ioo (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hgg' : ∀ x ∈ Ioo a b, has_deriv_at g (g' x) x) (hg' : ∀ x ∈ Ioo a b, g' x ≠ 0) (hfb : tendsto f (nhds_within b (Iio b)) (𝓝 0)) (hgb : tendsto g (nhds_within b (Iio b)) (𝓝 0)) (hdiv : tendsto (λ x, (f' x) / (g' x)) (nhds_within b (Iio b)) l) : tendsto (λ x, (f x) / (g x)) (nhds_within b (Iio b)) l := begin -- Here, we essentially compose by `has_neg.neg`. The following is mostly technical details. have hdnf : ∀ x ∈ -Ioo a b, has_deriv_at (f ∘ has_neg.neg) (f' (-x) * (-1)) x, from λ x hx, comp x (hff' (-x) hx) (has_deriv_at_neg x), have hdng : ∀ x ∈ -Ioo a b, has_deriv_at (g ∘ has_neg.neg) (g' (-x) * (-1)) x, from λ x hx, comp x (hgg' (-x) hx) (has_deriv_at_neg x), rw preimage_neg_Ioo at hdnf, rw preimage_neg_Ioo at hdng, have := lhopital_zero_right_on_Ioo (neg_lt_neg hab) hdnf hdng (by { intros x hx h, apply hg' _ (by {rw ← preimage_neg_Ioo at hx, exact hx}), rwa [mul_comm, ← neg_eq_neg_one_mul, neg_eq_zero] at h }) (hfb.comp tendsto_neg_nhds_within_Ioi_neg) (hgb.comp tendsto_neg_nhds_within_Ioi_neg) (by { simp only [neg_div_neg_eq, mul_one, mul_neg_eq_neg_mul_symm], exact (tendsto_congr $ λ x, rfl).mp (hdiv.comp tendsto_neg_nhds_within_Ioi_neg) }), have := this.comp tendsto_neg_nhds_within_Iio, unfold function.comp at this, simpa only [neg_neg] end theorem lhopital_zero_left_on_Ioc (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hgg' : ∀ x ∈ Ioo a b, has_deriv_at g (g' x) x) (hcf : continuous_on f (Ioc a b)) (hcg : continuous_on g (Ioc a b)) (hg' : ∀ x ∈ Ioo a b, g' x ≠ 0) (hfb : f b = 0) (hgb : g b = 0) (hdiv : tendsto (λ x, (f' x) / (g' x)) (nhds_within b (Iio b)) l) : tendsto (λ x, (f x) / (g x)) (nhds_within b (Iio b)) l := begin refine lhopital_zero_left_on_Ioo hab hff' hgg' hg' _ _ hdiv, { rw [← hfb, ← nhds_within_Ioo_eq_nhds_within_Iio hab], exact ((hcf b $ right_mem_Ioc.mpr hab).mono Ioo_subset_Ioc_self).tendsto }, { rw [← hgb, ← nhds_within_Ioo_eq_nhds_within_Iio hab], exact ((hcg b $ right_mem_Ioc.mpr hab).mono Ioo_subset_Ioc_self).tendsto }, end omit hab theorem lhopital_zero_at_top_on_Ioi (hff' : ∀ x ∈ Ioi a, has_deriv_at f (f' x) x) (hgg' : ∀ x ∈ Ioi a, has_deriv_at g (g' x) x) (hg' : ∀ x ∈ Ioi a, g' x ≠ 0) (hftop : tendsto f at_top (𝓝 0)) (hgtop : tendsto g at_top (𝓝 0)) (hdiv : tendsto (λ x, (f' x) / (g' x)) at_top l) : tendsto (λ x, (f x) / (g x)) at_top l := begin obtain ⟨ a', haa', ha'⟩ : ∃ a', a < a' ∧ 0 < a' := ⟨1 + max a 0, ⟨lt_of_le_of_lt (le_max_left a 0) (lt_one_add _), lt_of_le_of_lt (le_max_right a 0) (lt_one_add _)⟩⟩, have fact1 : ∀ (x:ℝ), x ∈ Ioo 0 a'⁻¹ → x ≠ 0 := λ _ hx, (ne_of_lt hx.1).symm, have fact2 : ∀ x ∈ Ioo 0 a'⁻¹, a < x⁻¹, from λ _ hx, lt_trans haa' ((lt_inv ha' hx.1).mpr hx.2), have hdnf : ∀ x ∈ Ioo 0 a'⁻¹, has_deriv_at (f ∘ has_inv.inv) (f' (x⁻¹) * (-(x^2)⁻¹)) x, from λ x hx, comp x (hff' (x⁻¹) $ fact2 x hx) (has_deriv_at_inv $ fact1 x hx), have hdng : ∀ x ∈ Ioo 0 a'⁻¹, has_deriv_at (g ∘ has_inv.inv) (g' (x⁻¹) * (-(x^2)⁻¹)) x, from λ x hx, comp x (hgg' (x⁻¹) $ fact2 x hx) (has_deriv_at_inv $ fact1 x hx), have := lhopital_zero_right_on_Ioo (inv_pos.mpr ha') hdnf hdng (by { intros x hx, refine mul_ne_zero _ (neg_ne_zero.mpr $ inv_ne_zero $ pow_ne_zero _ $ fact1 x hx), exact hg' _ (fact2 x hx) }) (hftop.comp tendsto_inv_zero_at_top) (hgtop.comp tendsto_inv_zero_at_top) (by { refine (tendsto_congr' _).mp (hdiv.comp tendsto_inv_zero_at_top), rw eventually_eq_iff_exists_mem, use [Ioi 0, self_mem_nhds_within], intros x hx, unfold function.comp, erw mul_div_mul_right, refine neg_ne_zero.mpr (inv_ne_zero $ pow_ne_zero _ $ ne_of_gt hx) }), have := this.comp tendsto_inv_at_top_zero', unfold function.comp at this, simpa only [inv_inv'], end theorem lhopital_zero_at_bot_on_Iio (hff' : ∀ x ∈ Iio a, has_deriv_at f (f' x) x) (hgg' : ∀ x ∈ Iio a, has_deriv_at g (g' x) x) (hg' : ∀ x ∈ Iio a, g' x ≠ 0) (hfbot : tendsto f at_bot (𝓝 0)) (hgbot : tendsto g at_bot (𝓝 0)) (hdiv : tendsto (λ x, (f' x) / (g' x)) at_bot l) : tendsto (λ x, (f x) / (g x)) at_bot l := begin -- Here, we essentially compose by `has_neg.neg`. The following is mostly technical details. have hdnf : ∀ x ∈ -Iio a, has_deriv_at (f ∘ has_neg.neg) (f' (-x) * (-1)) x, from λ x hx, comp x (hff' (-x) hx) (has_deriv_at_neg x), have hdng : ∀ x ∈ -Iio a, has_deriv_at (g ∘ has_neg.neg) (g' (-x) * (-1)) x, from λ x hx, comp x (hgg' (-x) hx) (has_deriv_at_neg x), rw preimage_neg_Iio at hdnf, rw preimage_neg_Iio at hdng, have := lhopital_zero_at_top_on_Ioi hdnf hdng (by { intros x hx h, apply hg' _ (by {rw ← preimage_neg_Iio at hx, exact hx}), rwa [mul_comm, ← neg_eq_neg_one_mul, neg_eq_zero] at h }) (hfbot.comp tendsto_neg_at_top_at_bot) (hgbot.comp tendsto_neg_at_top_at_bot) (by { simp only [mul_one, mul_neg_eq_neg_mul_symm, neg_div_neg_eq], exact (tendsto_congr $ λ x, rfl).mp (hdiv.comp tendsto_neg_at_top_at_bot) }), have := this.comp tendsto_neg_at_bot_at_top, unfold function.comp at this, simpa only [neg_neg], end end has_deriv_at namespace deriv include hab theorem lhopital_zero_right_on_Ioo (hdf : differentiable_on ℝ f (Ioo a b)) (hg' : ∀ x ∈ Ioo a b, deriv g x ≠ 0) (hfa : tendsto f (𝓝[Ioi a] a) (𝓝 0)) (hga : tendsto g (𝓝[Ioi a] a) (𝓝 0)) (hdiv : tendsto (λ x, ((deriv f) x) / ((deriv g) x)) (𝓝[Ioi a] a) l) : tendsto (λ x, (f x) / (g x)) (𝓝[Ioi a] a) l := begin have hdf : ∀ x ∈ Ioo a b, differentiable_at ℝ f x, from λ x hx, (hdf x hx).differentiable_at (Ioo_mem_nhds hx.1 hx.2), have hdg : ∀ x ∈ Ioo a b, differentiable_at ℝ g x, from λ x hx, classical.by_contradiction (λ h, hg' x hx (deriv_zero_of_not_differentiable_at h)), exact has_deriv_at.lhopital_zero_right_on_Ioo hab (λ x hx, (hdf x hx).has_deriv_at) (λ x hx, (hdg x hx).has_deriv_at) hg' hfa hga hdiv end theorem lhopital_zero_right_on_Ico (hdf : differentiable_on ℝ f (Ioo a b)) (hcf : continuous_on f (Ico a b)) (hcg : continuous_on g (Ico a b)) (hg' : ∀ x ∈ (Ioo a b), (deriv g) x ≠ 0) (hfa : f a = 0) (hga : g a = 0) (hdiv : tendsto (λ x, ((deriv f) x) / ((deriv g) x)) (nhds_within a (Ioi a)) l) : tendsto (λ x, (f x) / (g x)) (nhds_within a (Ioi a)) l := begin refine lhopital_zero_right_on_Ioo hab hdf hg' _ _ hdiv, { rw [← hfa, ← nhds_within_Ioo_eq_nhds_within_Ioi hab], exact ((hcf a $ left_mem_Ico.mpr hab).mono Ioo_subset_Ico_self).tendsto }, { rw [← hga, ← nhds_within_Ioo_eq_nhds_within_Ioi hab], exact ((hcg a $ left_mem_Ico.mpr hab).mono Ioo_subset_Ico_self).tendsto }, end theorem lhopital_zero_left_on_Ioo (hdf : differentiable_on ℝ f (Ioo a b)) (hg' : ∀ x ∈ (Ioo a b), (deriv g) x ≠ 0) (hfb : tendsto f (nhds_within b (Iio b)) (𝓝 0)) (hgb : tendsto g (nhds_within b (Iio b)) (𝓝 0)) (hdiv : tendsto (λ x, ((deriv f) x) / ((deriv g) x)) (nhds_within b (Iio b)) l) : tendsto (λ x, (f x) / (g x)) (nhds_within b (Iio b)) l := begin have hdf : ∀ x ∈ Ioo a b, differentiable_at ℝ f x, from λ x hx, (hdf x hx).differentiable_at (Ioo_mem_nhds hx.1 hx.2), have hdg : ∀ x ∈ Ioo a b, differentiable_at ℝ g x, from λ x hx, classical.by_contradiction (λ h, hg' x hx (deriv_zero_of_not_differentiable_at h)), exact has_deriv_at.lhopital_zero_left_on_Ioo hab (λ x hx, (hdf x hx).has_deriv_at) (λ x hx, (hdg x hx).has_deriv_at) hg' hfb hgb hdiv end omit hab theorem lhopital_zero_at_top_on_Ioi (hdf : differentiable_on ℝ f (Ioi a)) (hg' : ∀ x ∈ (Ioi a), (deriv g) x ≠ 0) (hftop : tendsto f at_top (𝓝 0)) (hgtop : tendsto g at_top (𝓝 0)) (hdiv : tendsto (λ x, ((deriv f) x) / ((deriv g) x)) at_top l) : tendsto (λ x, (f x) / (g x)) at_top l := begin have hdf : ∀ x ∈ Ioi a, differentiable_at ℝ f x, from λ x hx, (hdf x hx).differentiable_at (Ioi_mem_nhds hx), have hdg : ∀ x ∈ Ioi a, differentiable_at ℝ g x, from λ x hx, classical.by_contradiction (λ h, hg' x hx (deriv_zero_of_not_differentiable_at h)), exact has_deriv_at.lhopital_zero_at_top_on_Ioi (λ x hx, (hdf x hx).has_deriv_at) (λ x hx, (hdg x hx).has_deriv_at) hg' hftop hgtop hdiv, end theorem lhopital_zero_at_bot_on_Iio (hdf : differentiable_on ℝ f (Iio a)) (hg' : ∀ x ∈ (Iio a), (deriv g) x ≠ 0) (hfbot : tendsto f at_bot (𝓝 0)) (hgbot : tendsto g at_bot (𝓝 0)) (hdiv : tendsto (λ x, ((deriv f) x) / ((deriv g) x)) at_bot l) : tendsto (λ x, (f x) / (g x)) at_bot l := begin have hdf : ∀ x ∈ Iio a, differentiable_at ℝ f x, from λ x hx, (hdf x hx).differentiable_at (Iio_mem_nhds hx), have hdg : ∀ x ∈ Iio a, differentiable_at ℝ g x, from λ x hx, classical.by_contradiction (λ h, hg' x hx (deriv_zero_of_not_differentiable_at h)), exact has_deriv_at.lhopital_zero_at_bot_on_Iio (λ x hx, (hdf x hx).has_deriv_at) (λ x hx, (hdg x hx).has_deriv_at) hg' hfbot hgbot hdiv, end end deriv /-! ## Generic versions The following statements no longer any explicit interval, as they only require conditions holding eventually. -/ namespace has_deriv_at /-- L'Hôpital's rule for approaching a real from the right, `has_deriv_at` version -/ theorem lhopital_zero_nhds_right (hff' : ∀ᶠ x in 𝓝[Ioi a] a, has_deriv_at f (f' x) x) (hgg' : ∀ᶠ x in 𝓝[Ioi a] a, has_deriv_at g (g' x) x) (hg' : ∀ᶠ x in 𝓝[Ioi a] a, g' x ≠ 0) (hfa : tendsto f (𝓝[Ioi a] a) (𝓝 0)) (hga : tendsto g (𝓝[Ioi a] a) (𝓝 0)) (hdiv : tendsto (λ x, (f' x) / (g' x)) (𝓝[Ioi a] a) l) : tendsto (λ x, (f x) / (g x)) (𝓝[Ioi a] a) l := begin rw eventually_iff_exists_mem at *, rcases hff' with ⟨s₁, hs₁, hff'⟩, rcases hgg' with ⟨s₂, hs₂, hgg'⟩, rcases hg' with ⟨s₃, hs₃, hg'⟩, let s := s₁ ∩ s₂ ∩ s₃, have hs : s ∈ 𝓝[Ioi a] a := inter_mem (inter_mem hs₁ hs₂) hs₃, rw mem_nhds_within_Ioi_iff_exists_Ioo_subset at hs, rcases hs with ⟨u, hau, hu⟩, refine lhopital_zero_right_on_Ioo hau _ _ _ hfa hga hdiv; intros x hx; apply_assumption; exact (hu hx).1.1 <|> exact (hu hx).1.2 <|> exact (hu hx).2 end /-- L'Hôpital's rule for approaching a real from the left, `has_deriv_at` version -/ theorem lhopital_zero_nhds_left (hff' : ∀ᶠ x in 𝓝[Iio a] a, has_deriv_at f (f' x) x) (hgg' : ∀ᶠ x in 𝓝[Iio a] a, has_deriv_at g (g' x) x) (hg' : ∀ᶠ x in 𝓝[Iio a] a, g' x ≠ 0) (hfa : tendsto f (𝓝[Iio a] a) (𝓝 0)) (hga : tendsto g (𝓝[Iio a] a) (𝓝 0)) (hdiv : tendsto (λ x, (f' x) / (g' x)) (𝓝[Iio a] a) l) : tendsto (λ x, (f x) / (g x)) (𝓝[Iio a] a) l := begin rw eventually_iff_exists_mem at *, rcases hff' with ⟨s₁, hs₁, hff'⟩, rcases hgg' with ⟨s₂, hs₂, hgg'⟩, rcases hg' with ⟨s₃, hs₃, hg'⟩, let s := s₁ ∩ s₂ ∩ s₃, have hs : s ∈ 𝓝[Iio a] a := inter_mem (inter_mem hs₁ hs₂) hs₃, rw mem_nhds_within_Iio_iff_exists_Ioo_subset at hs, rcases hs with ⟨l, hal, hl⟩, refine lhopital_zero_left_on_Ioo hal _ _ _ hfa hga hdiv; intros x hx; apply_assumption; exact (hl hx).1.1 <|> exact (hl hx).1.2 <|> exact (hl hx).2 end /-- L'Hôpital's rule for approaching a real, `has_deriv_at` version. This does not require anything about the situation at `a` -/ theorem lhopital_zero_nhds' (hff' : ∀ᶠ x in 𝓝[univ \ {a}] a, has_deriv_at f (f' x) x) (hgg' : ∀ᶠ x in 𝓝[univ \ {a}] a, has_deriv_at g (g' x) x) (hg' : ∀ᶠ x in 𝓝[univ \ {a}] a, g' x ≠ 0) (hfa : tendsto f (𝓝[univ \ {a}] a) (𝓝 0)) (hga : tendsto g (𝓝[univ \ {a}] a) (𝓝 0)) (hdiv : tendsto (λ x, (f' x) / (g' x)) (𝓝[univ \ {a}] a) l) : tendsto (λ x, (f x) / (g x)) (𝓝[univ \ {a}] a) l := begin have : univ \ {a} = Iio a ∪ Ioi a, { ext, rw [mem_diff_singleton, eq_true_intro $ mem_univ x, true_and, ne_iff_lt_or_gt], refl }, simp only [this, nhds_within_union, tendsto_sup, eventually_sup] at *, exact ⟨lhopital_zero_nhds_left hff'.1 hgg'.1 hg'.1 hfa.1 hga.1 hdiv.1, lhopital_zero_nhds_right hff'.2 hgg'.2 hg'.2 hfa.2 hga.2 hdiv.2⟩ end /-- **L'Hôpital's rule** for approaching a real, `has_deriv_at` version -/ theorem lhopital_zero_nhds (hff' : ∀ᶠ x in 𝓝 a, has_deriv_at f (f' x) x) (hgg' : ∀ᶠ x in 𝓝 a, has_deriv_at g (g' x) x) (hg' : ∀ᶠ x in 𝓝 a, g' x ≠ 0) (hfa : tendsto f (𝓝 a) (𝓝 0)) (hga : tendsto g (𝓝 a) (𝓝 0)) (hdiv : tendsto (λ x, f' x / g' x) (𝓝 a) l) : tendsto (λ x, f x / g x) (𝓝[univ \ {a}] a) l := begin apply @lhopital_zero_nhds' _ _ _ f' _ g'; apply eventually_nhds_within_of_eventually_nhds <|> apply tendsto_nhds_within_of_tendsto_nhds; assumption end /-- L'Hôpital's rule for approaching +∞, `has_deriv_at` version -/ theorem lhopital_zero_at_top (hff' : ∀ᶠ x in at_top, has_deriv_at f (f' x) x) (hgg' : ∀ᶠ x in at_top, has_deriv_at g (g' x) x) (hg' : ∀ᶠ x in at_top, g' x ≠ 0) (hftop : tendsto f at_top (𝓝 0)) (hgtop : tendsto g at_top (𝓝 0)) (hdiv : tendsto (λ x, (f' x) / (g' x)) at_top l) : tendsto (λ x, (f x) / (g x)) at_top l := begin rw eventually_iff_exists_mem at *, rcases hff' with ⟨s₁, hs₁, hff'⟩, rcases hgg' with ⟨s₂, hs₂, hgg'⟩, rcases hg' with ⟨s₃, hs₃, hg'⟩, let s := s₁ ∩ s₂ ∩ s₃, have hs : s ∈ at_top := inter_mem (inter_mem hs₁ hs₂) hs₃, rw mem_at_top_sets at hs, rcases hs with ⟨l, hl⟩, have hl' : Ioi l ⊆ s := λ x hx, hl x (le_of_lt hx), refine lhopital_zero_at_top_on_Ioi _ _ (λ x hx, hg' x $ (hl' hx).2) hftop hgtop hdiv; intros x hx; apply_assumption; exact (hl' hx).1.1 <|> exact (hl' hx).1.2 end /-- L'Hôpital's rule for approaching -∞, `has_deriv_at` version -/ theorem lhopital_zero_at_bot (hff' : ∀ᶠ x in at_bot, has_deriv_at f (f' x) x) (hgg' : ∀ᶠ x in at_bot, has_deriv_at g (g' x) x) (hg' : ∀ᶠ x in at_bot, g' x ≠ 0) (hfbot : tendsto f at_bot (𝓝 0)) (hgbot : tendsto g at_bot (𝓝 0)) (hdiv : tendsto (λ x, (f' x) / (g' x)) at_bot l) : tendsto (λ x, (f x) / (g x)) at_bot l := begin rw eventually_iff_exists_mem at *, rcases hff' with ⟨s₁, hs₁, hff'⟩, rcases hgg' with ⟨s₂, hs₂, hgg'⟩, rcases hg' with ⟨s₃, hs₃, hg'⟩, let s := s₁ ∩ s₂ ∩ s₃, have hs : s ∈ at_bot := inter_mem (inter_mem hs₁ hs₂) hs₃, rw mem_at_bot_sets at hs, rcases hs with ⟨l, hl⟩, have hl' : Iio l ⊆ s := λ x hx, hl x (le_of_lt hx), refine lhopital_zero_at_bot_on_Iio _ _ (λ x hx, hg' x $ (hl' hx).2) hfbot hgbot hdiv; intros x hx; apply_assumption; exact (hl' hx).1.1 <|> exact (hl' hx).1.2 end end has_deriv_at namespace deriv /-- **L'Hôpital's rule** for approaching a real from the right, `deriv` version -/ theorem lhopital_zero_nhds_right (hdf : ∀ᶠ x in 𝓝[Ioi a] a, differentiable_at ℝ f x) (hg' : ∀ᶠ x in 𝓝[Ioi a] a, deriv g x ≠ 0) (hfa : tendsto f (𝓝[Ioi a] a) (𝓝 0)) (hga : tendsto g (𝓝[Ioi a] a) (𝓝 0)) (hdiv : tendsto (λ x, ((deriv f) x) / ((deriv g) x)) (𝓝[Ioi a] a) l) : tendsto (λ x, (f x) / (g x)) (𝓝[Ioi a] a) l := begin have hdg : ∀ᶠ x in 𝓝[Ioi a] a, differentiable_at ℝ g x, from hg'.mp (eventually_of_forall $ λ _ hg', classical.by_contradiction (λ h, hg' (deriv_zero_of_not_differentiable_at h))), have hdf' : ∀ᶠ x in 𝓝[Ioi a] a, has_deriv_at f (deriv f x) x, from hdf.mp (eventually_of_forall $ λ _, differentiable_at.has_deriv_at), have hdg' : ∀ᶠ x in 𝓝[Ioi a] a, has_deriv_at g (deriv g x) x, from hdg.mp (eventually_of_forall $ λ _, differentiable_at.has_deriv_at), exact has_deriv_at.lhopital_zero_nhds_right hdf' hdg' hg' hfa hga hdiv end /-- **L'Hôpital's rule** for approaching a real from the left, `deriv` version -/ theorem lhopital_zero_nhds_left (hdf : ∀ᶠ x in 𝓝[Iio a] a, differentiable_at ℝ f x) (hg' : ∀ᶠ x in 𝓝[Iio a] a, deriv g x ≠ 0) (hfa : tendsto f (𝓝[Iio a] a) (𝓝 0)) (hga : tendsto g (𝓝[Iio a] a) (𝓝 0)) (hdiv : tendsto (λ x, ((deriv f) x) / ((deriv g) x)) (𝓝[Iio a] a) l) : tendsto (λ x, (f x) / (g x)) (𝓝[Iio a] a) l := begin have hdg : ∀ᶠ x in 𝓝[Iio a] a, differentiable_at ℝ g x, from hg'.mp (eventually_of_forall $ λ _ hg', classical.by_contradiction (λ h, hg' (deriv_zero_of_not_differentiable_at h))), have hdf' : ∀ᶠ x in 𝓝[Iio a] a, has_deriv_at f (deriv f x) x, from hdf.mp (eventually_of_forall $ λ _, differentiable_at.has_deriv_at), have hdg' : ∀ᶠ x in 𝓝[Iio a] a, has_deriv_at g (deriv g x) x, from hdg.mp (eventually_of_forall $ λ _, differentiable_at.has_deriv_at), exact has_deriv_at.lhopital_zero_nhds_left hdf' hdg' hg' hfa hga hdiv end /-- **L'Hôpital's rule** for approaching a real, `deriv` version. This does not require anything about the situation at `a` -/ theorem lhopital_zero_nhds' (hdf : ∀ᶠ x in 𝓝[univ \ {a}] a, differentiable_at ℝ f x) (hg' : ∀ᶠ x in 𝓝[univ \ {a}] a, deriv g x ≠ 0) (hfa : tendsto f (𝓝[univ \ {a}] a) (𝓝 0)) (hga : tendsto g (𝓝[univ \ {a}] a) (𝓝 0)) (hdiv : tendsto (λ x, ((deriv f) x) / ((deriv g) x)) (𝓝[univ \ {a}] a) l) : tendsto (λ x, (f x) / (g x)) (𝓝[univ \ {a}] a) l := begin have : univ \ {a} = Iio a ∪ Ioi a, { ext, rw [mem_diff_singleton, eq_true_intro $ mem_univ x, true_and, ne_iff_lt_or_gt], refl }, simp only [this, nhds_within_union, tendsto_sup, eventually_sup] at *, exact ⟨lhopital_zero_nhds_left hdf.1 hg'.1 hfa.1 hga.1 hdiv.1, lhopital_zero_nhds_right hdf.2 hg'.2 hfa.2 hga.2 hdiv.2⟩, end /-- **L'Hôpital's rule** for approaching a real, `deriv` version -/ theorem lhopital_zero_nhds (hdf : ∀ᶠ x in 𝓝 a, differentiable_at ℝ f x) (hg' : ∀ᶠ x in 𝓝 a, deriv g x ≠ 0) (hfa : tendsto f (𝓝 a) (𝓝 0)) (hga : tendsto g (𝓝 a) (𝓝 0)) (hdiv : tendsto (λ x, ((deriv f) x) / ((deriv g) x)) (𝓝 a) l) : tendsto (λ x, (f x) / (g x)) (𝓝[univ \ {a}] a) l := begin apply lhopital_zero_nhds'; apply eventually_nhds_within_of_eventually_nhds <|> apply tendsto_nhds_within_of_tendsto_nhds; assumption end /-- **L'Hôpital's rule** for approaching +∞, `deriv` version -/ theorem lhopital_zero_at_top (hdf : ∀ᶠ (x : ℝ) in at_top, differentiable_at ℝ f x) (hg' : ∀ᶠ (x : ℝ) in at_top, deriv g x ≠ 0) (hftop : tendsto f at_top (𝓝 0)) (hgtop : tendsto g at_top (𝓝 0)) (hdiv : tendsto (λ x, ((deriv f) x) / ((deriv g) x)) at_top l) : tendsto (λ x, (f x) / (g x)) at_top l := begin have hdg : ∀ᶠ x in at_top, differentiable_at ℝ g x, from hg'.mp (eventually_of_forall $ λ _ hg', classical.by_contradiction (λ h, hg' (deriv_zero_of_not_differentiable_at h))), have hdf' : ∀ᶠ x in at_top, has_deriv_at f (deriv f x) x, from hdf.mp (eventually_of_forall $ λ _, differentiable_at.has_deriv_at), have hdg' : ∀ᶠ x in at_top, has_deriv_at g (deriv g x) x, from hdg.mp (eventually_of_forall $ λ _, differentiable_at.has_deriv_at), exact has_deriv_at.lhopital_zero_at_top hdf' hdg' hg' hftop hgtop hdiv end /-- **L'Hôpital's rule** for approaching -∞, `deriv` version -/ theorem lhopital_zero_at_bot (hdf : ∀ᶠ (x : ℝ) in at_bot, differentiable_at ℝ f x) (hg' : ∀ᶠ (x : ℝ) in at_bot, deriv g x ≠ 0) (hfbot : tendsto f at_bot (𝓝 0)) (hgbot : tendsto g at_bot (𝓝 0)) (hdiv : tendsto (λ x, ((deriv f) x) / ((deriv g) x)) at_bot l) : tendsto (λ x, (f x) / (g x)) at_bot l := begin have hdg : ∀ᶠ x in at_bot, differentiable_at ℝ g x, from hg'.mp (eventually_of_forall $ λ _ hg', classical.by_contradiction (λ h, hg' (deriv_zero_of_not_differentiable_at h))), have hdf' : ∀ᶠ x in at_bot, has_deriv_at f (deriv f x) x, from hdf.mp (eventually_of_forall $ λ _, differentiable_at.has_deriv_at), have hdg' : ∀ᶠ x in at_bot, has_deriv_at g (deriv g x) x, from hdg.mp (eventually_of_forall $ λ _, differentiable_at.has_deriv_at), exact has_deriv_at.lhopital_zero_at_bot hdf' hdg' hg' hfbot hgbot hdiv end end deriv
eb811efee6b7e57378d0a71759254e3f04a253f3
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/sum/interval.lean
e831e4564adfcb7879797ff23b01771a1dbebefe
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
14,892
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import data.finset.sum import data.sum.order import order.locally_finite /-! # Finite intervals in a disjoint union > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file provides the `locally_finite_order` instance for the disjoint sum and linear sum of two orders and calculates the cardinality of their finite intervals. -/ open function sum namespace finset variables {α₁ α₂ β₁ β₂ γ₁ γ₂ : Type*} section sum_lift₂ variables (f f₁ g₁ : α₁ → β₁ → finset γ₁) (g f₂ g₂ : α₂ → β₂ → finset γ₂) /-- Lifts maps `α₁ → β₁ → finset γ₁` and `α₂ → β₂ → finset γ₂` to a map `α₁ ⊕ α₂ → β₁ ⊕ β₂ → finset (γ₁ ⊕ γ₂)`. Could be generalized to `alternative` functors if we can make sure to keep computability and universe polymorphism. -/ @[simp] def sum_lift₂ : Π (a : α₁ ⊕ α₂) (b : β₁ ⊕ β₂), finset (γ₁ ⊕ γ₂) | (inl a) (inl b) := (f a b).map embedding.inl | (inl a) (inr b) := ∅ | (inr a) (inl b) := ∅ | (inr a) (inr b) := (g a b).map embedding.inr variables {f f₁ g₁ g f₂ g₂} {a : α₁ ⊕ α₂} {b : β₁ ⊕ β₂} {c : γ₁ ⊕ γ₂} lemma mem_sum_lift₂ : c ∈ sum_lift₂ f g a b ↔ (∃ a₁ b₁ c₁, a = inl a₁ ∧ b = inl b₁ ∧ c = inl c₁ ∧ c₁ ∈ f a₁ b₁) ∨ ∃ a₂ b₂ c₂, a = inr a₂ ∧ b = inr b₂ ∧ c = inr c₂ ∧ c₂ ∈ g a₂ b₂ := begin split, { cases a; cases b, { rw [sum_lift₂, mem_map], rintro ⟨c, hc, rfl⟩, exact or.inl ⟨a, b, c, rfl, rfl, rfl, hc⟩ }, { refine λ h, (not_mem_empty _ h).elim }, { refine λ h, (not_mem_empty _ h).elim }, { rw [sum_lift₂, mem_map], rintro ⟨c, hc, rfl⟩, exact or.inr ⟨a, b, c, rfl, rfl, rfl, hc⟩ } }, { rintro (⟨a, b, c, rfl, rfl, rfl, h⟩ | ⟨a, b, c, rfl, rfl, rfl, h⟩); exact mem_map_of_mem _ h } end lemma inl_mem_sum_lift₂ {c₁ : γ₁} : inl c₁ ∈ sum_lift₂ f g a b ↔ ∃ a₁ b₁, a = inl a₁ ∧ b = inl b₁ ∧ c₁ ∈ f a₁ b₁ := begin rw [mem_sum_lift₂, or_iff_left], simp only [exists_and_distrib_left, exists_eq_left'], rintro ⟨_, _, c₂, _, _, h, _⟩, exact inl_ne_inr h, end lemma inr_mem_sum_lift₂ {c₂ : γ₂} : inr c₂ ∈ sum_lift₂ f g a b ↔ ∃ a₂ b₂, a = inr a₂ ∧ b = inr b₂ ∧ c₂ ∈ g a₂ b₂ := begin rw [mem_sum_lift₂, or_iff_right], simp only [exists_and_distrib_left, exists_eq_left'], rintro ⟨_, _, c₂, _, _, h, _⟩, exact inr_ne_inl h, end lemma sum_lift₂_eq_empty : (sum_lift₂ f g a b) = ∅ ↔ (∀ a₁ b₁, a = inl a₁ → b = inl b₁ → f a₁ b₁ = ∅) ∧ ∀ a₂ b₂, a = inr a₂ → b = inr b₂ → g a₂ b₂ = ∅ := begin refine ⟨λ h, _, λ h, _⟩, { split; { rintro a b rfl rfl, exact map_eq_empty.1 h } }, cases a; cases b, { exact map_eq_empty.2 (h.1 _ _ rfl rfl) }, { refl }, { refl }, { exact map_eq_empty.2 (h.2 _ _ rfl rfl) } end lemma sum_lift₂_nonempty : (sum_lift₂ f g a b).nonempty ↔ (∃ a₁ b₁, a = inl a₁ ∧ b = inl b₁ ∧ (f a₁ b₁).nonempty) ∨ ∃ a₂ b₂, a = inr a₂ ∧ b = inr b₂ ∧ (g a₂ b₂).nonempty := by simp [nonempty_iff_ne_empty, sum_lift₂_eq_empty, not_and_distrib] lemma sum_lift₂_mono (h₁ : ∀ a b, f₁ a b ⊆ g₁ a b) (h₂ : ∀ a b, f₂ a b ⊆ g₂ a b) : ∀ a b, sum_lift₂ f₁ f₂ a b ⊆ sum_lift₂ g₁ g₂ a b | (inl a) (inl b) := map_subset_map.2 (h₁ _ _) | (inl a) (inr b) := subset.rfl | (inr a) (inl b) := subset.rfl | (inr a) (inr b) := map_subset_map.2 (h₂ _ _) end sum_lift₂ section sum_lex_lift variables (f₁ f₁' : α₁ → β₁ → finset γ₁) (f₂ f₂' : α₂ → β₂ → finset γ₂) (g₁ g₁' : α₁ → β₂ → finset γ₁) (g₂ g₂' : α₁ → β₂ → finset γ₂) /-- Lifts maps `α₁ → β₁ → finset γ₁`, `α₂ → β₂ → finset γ₂`, `α₁ → β₂ → finset γ₁`, `α₂ → β₂ → finset γ₂` to a map `α₁ ⊕ α₂ → β₁ ⊕ β₂ → finset (γ₁ ⊕ γ₂)`. Could be generalized to alternative monads if we can make sure to keep computability and universe polymorphism. -/ def sum_lex_lift : Π (a : α₁ ⊕ α₂) (b : β₁ ⊕ β₂), finset (γ₁ ⊕ γ₂) | (inl a) (inl b) := (f₁ a b).map embedding.inl | (inl a) (inr b) := (g₁ a b).disj_sum (g₂ a b) | (inr a) (inl b) := ∅ | (inr a) (inr b) := (f₂ a b).map ⟨_, inr_injective⟩ @[simp] lemma sum_lex_lift_inl_inl (a : α₁) (b : β₁) : sum_lex_lift f₁ f₂ g₁ g₂ (inl a) (inl b) = (f₁ a b).map embedding.inl := rfl @[simp] lemma sum_lex_lift_inl_inr (a : α₁) (b : β₂) : sum_lex_lift f₁ f₂ g₁ g₂ (inl a) (inr b) = (g₁ a b).disj_sum (g₂ a b) := rfl @[simp] lemma sum_lex_lift_inr_inl (a : α₂) (b : β₁) : sum_lex_lift f₁ f₂ g₁ g₂ (inr a) (inl b) = ∅ := rfl @[simp] lemma sum_lex_lift_inr_inr (a : α₂) (b : β₂) : sum_lex_lift f₁ f₂ g₁ g₂ (inr a) (inr b) = (f₂ a b).map ⟨_, inr_injective⟩ := rfl variables {f₁ g₁ f₂ g₂ f₁' g₁' f₂' g₂'} {a : α₁ ⊕ α₂} {b : β₁ ⊕ β₂} {c : γ₁ ⊕ γ₂} lemma mem_sum_lex_lift : c ∈ sum_lex_lift f₁ f₂ g₁ g₂ a b ↔ (∃ a₁ b₁ c₁, a = inl a₁ ∧ b = inl b₁ ∧ c = inl c₁ ∧ c₁ ∈ f₁ a₁ b₁) ∨ (∃ a₁ b₂ c₁, a = inl a₁ ∧ b = inr b₂ ∧ c = inl c₁ ∧ c₁ ∈ g₁ a₁ b₂) ∨ (∃ a₁ b₂ c₂, a = inl a₁ ∧ b = inr b₂ ∧ c = inr c₂ ∧ c₂ ∈ g₂ a₁ b₂) ∨ ∃ a₂ b₂ c₂, a = inr a₂ ∧ b = inr b₂ ∧ c = inr c₂ ∧ c₂ ∈ f₂ a₂ b₂ := begin split, { cases a; cases b, { rw [sum_lex_lift, mem_map], rintro ⟨c, hc, rfl⟩, exact or.inl ⟨a, b, c, rfl, rfl, rfl, hc⟩ }, { refine λ h, (mem_disj_sum.1 h).elim _ _, { rintro ⟨c, hc, rfl⟩, refine or.inr (or.inl ⟨a, b, c, rfl, rfl, rfl, hc⟩) }, { rintro ⟨c, hc, rfl⟩, refine or.inr (or.inr $ or.inl ⟨a, b, c, rfl, rfl, rfl, hc⟩) } }, { refine λ h, (not_mem_empty _ h).elim }, { rw [sum_lex_lift, mem_map], rintro ⟨c, hc, rfl⟩, exact or.inr (or.inr $ or.inr $ ⟨a, b, c, rfl, rfl, rfl, hc⟩) } }, { rintro (⟨a, b, c, rfl, rfl, rfl, hc⟩ | ⟨a, b, c, rfl, rfl, rfl, hc⟩ | ⟨a, b, c, rfl, rfl, rfl, hc⟩ | ⟨a, b, c, rfl, rfl, rfl, hc⟩), { exact mem_map_of_mem _ hc }, { exact inl_mem_disj_sum.2 hc }, { exact inr_mem_disj_sum.2 hc }, { exact mem_map_of_mem _ hc } } end lemma inl_mem_sum_lex_lift {c₁ : γ₁} : inl c₁ ∈ sum_lex_lift f₁ f₂ g₁ g₂ a b ↔ (∃ a₁ b₁, a = inl a₁ ∧ b = inl b₁ ∧ c₁ ∈ f₁ a₁ b₁) ∨ ∃ a₁ b₂, a = inl a₁ ∧ b = inr b₂ ∧ c₁ ∈ g₁ a₁ b₂ := by simp [mem_sum_lex_lift] lemma inr_mem_sum_lex_lift {c₂ : γ₂} : inr c₂ ∈ sum_lex_lift f₁ f₂ g₁ g₂ a b ↔ (∃ a₁ b₂, a = inl a₁ ∧ b = inr b₂ ∧ c₂ ∈ g₂ a₁ b₂) ∨ ∃ a₂ b₂, a = inr a₂ ∧ b = inr b₂ ∧ c₂ ∈ f₂ a₂ b₂ := by simp [mem_sum_lex_lift] lemma sum_lex_lift_mono (hf₁ : ∀ a b, f₁ a b ⊆ f₁' a b) (hf₂ : ∀ a b, f₂ a b ⊆ f₂' a b) (hg₁ : ∀ a b, g₁ a b ⊆ g₁' a b) (hg₂ : ∀ a b, g₂ a b ⊆ g₂' a b) (a : α₁ ⊕ α₂) (b : β₁ ⊕ β₂) : sum_lex_lift f₁ f₂ g₁ g₂ a b ⊆ sum_lex_lift f₁' f₂' g₁' g₂' a b := begin cases a; cases b, exacts [map_subset_map.2 (hf₁ _ _), disj_sum_mono (hg₁ _ _) (hg₂ _ _), subset.rfl, map_subset_map.2 (hf₂ _ _)], end lemma sum_lex_lift_eq_empty : (sum_lex_lift f₁ f₂ g₁ g₂ a b) = ∅ ↔ (∀ a₁ b₁, a = inl a₁ → b = inl b₁ → f₁ a₁ b₁ = ∅) ∧ (∀ a₁ b₂, a = inl a₁ → b = inr b₂ → g₁ a₁ b₂ = ∅ ∧ g₂ a₁ b₂ = ∅) ∧ ∀ a₂ b₂, a = inr a₂ → b = inr b₂ → f₂ a₂ b₂ = ∅ := begin refine ⟨λ h, ⟨_, _, _⟩, λ h, _⟩, any_goals { rintro a b rfl rfl, exact map_eq_empty.1 h }, { rintro a b rfl rfl, exact disj_sum_eq_empty.1 h }, cases a; cases b, { exact map_eq_empty.2 (h.1 _ _ rfl rfl) }, { simp [h.2.1 _ _ rfl rfl] }, { refl }, { exact map_eq_empty.2 (h.2.2 _ _ rfl rfl) } end lemma sum_lex_lift_nonempty : (sum_lex_lift f₁ f₂ g₁ g₂ a b).nonempty ↔ (∃ a₁ b₁, a = inl a₁ ∧ b = inl b₁ ∧ (f₁ a₁ b₁).nonempty) ∨ (∃ a₁ b₂, a = inl a₁ ∧ b = inr b₂ ∧ ((g₁ a₁ b₂).nonempty ∨ (g₂ a₁ b₂).nonempty)) ∨ ∃ a₂ b₂, a = inr a₂ ∧ b = inr b₂ ∧ (f₂ a₂ b₂).nonempty := by simp [nonempty_iff_ne_empty, sum_lex_lift_eq_empty, not_and_distrib] end sum_lex_lift end finset open finset function namespace sum variables {α β : Type*} /-! ### Disjoint sum of orders -/ section disjoint variables [preorder α] [preorder β] [locally_finite_order α] [locally_finite_order β] instance : locally_finite_order (α ⊕ β) := { finset_Icc := sum_lift₂ Icc Icc, finset_Ico := sum_lift₂ Ico Ico, finset_Ioc := sum_lift₂ Ioc Ioc, finset_Ioo := sum_lift₂ Ioo Ioo, finset_mem_Icc := by rintro (a | a) (b | b) (x | x); simp, finset_mem_Ico := by rintro (a | a) (b | b) (x | x); simp, finset_mem_Ioc := by rintro (a | a) (b | b) (x | x); simp, finset_mem_Ioo := by rintro (a | a) (b | b) (x | x); simp } variables (a₁ a₂ : α) (b₁ b₂ : β) (a b : α ⊕ β) lemma Icc_inl_inl : Icc (inl a₁ : α ⊕ β) (inl a₂) = (Icc a₁ a₂).map embedding.inl := rfl lemma Ico_inl_inl : Ico (inl a₁ : α ⊕ β) (inl a₂) = (Ico a₁ a₂).map embedding.inl := rfl lemma Ioc_inl_inl : Ioc (inl a₁ : α ⊕ β) (inl a₂) = (Ioc a₁ a₂).map embedding.inl := rfl lemma Ioo_inl_inl : Ioo (inl a₁ : α ⊕ β) (inl a₂) = (Ioo a₁ a₂).map embedding.inl := rfl @[simp] lemma Icc_inl_inr : Icc (inl a₁) (inr b₂) = ∅ := rfl @[simp] lemma Ico_inl_inr : Ico (inl a₁) (inr b₂) = ∅ := rfl @[simp] lemma Ioc_inl_inr : Ioc (inl a₁) (inr b₂) = ∅ := rfl @[simp] lemma Ioo_inl_inr : Ioo (inl a₁) (inr b₂) = ∅ := rfl @[simp] lemma Icc_inr_inl : Icc (inr b₁) (inl a₂) = ∅ := rfl @[simp] lemma Ico_inr_inl : Ico (inr b₁) (inl a₂) = ∅ := rfl @[simp] lemma Ioc_inr_inl : Ioc (inr b₁) (inl a₂) = ∅ := rfl @[simp] lemma Ioo_inr_inl : Ioo (inr b₁) (inl a₂) = ∅ := rfl lemma Icc_inr_inr : Icc (inr b₁ : α ⊕ β) (inr b₂) = (Icc b₁ b₂).map embedding.inr := rfl lemma Ico_inr_inr : Ico (inr b₁ : α ⊕ β) (inr b₂) = (Ico b₁ b₂).map embedding.inr := rfl lemma Ioc_inr_inr : Ioc (inr b₁ : α ⊕ β) (inr b₂) = (Ioc b₁ b₂).map embedding.inr := rfl lemma Ioo_inr_inr : Ioo (inr b₁ : α ⊕ β) (inr b₂) = (Ioo b₁ b₂).map embedding.inr := rfl end disjoint /-! ### Lexicographical sum of orders -/ namespace lex variables [preorder α] [preorder β] [order_top α] [order_bot β] [locally_finite_order α] [locally_finite_order β] /-- Throwaway tactic. -/ private meta def simp_lex : tactic unit := `[refine to_lex.surjective.forall₃.2 _, rintro (a | a) (b | b) (c | c); simp only [sum_lex_lift_inl_inl, sum_lex_lift_inl_inr, sum_lex_lift_inr_inl, sum_lex_lift_inr_inr, inl_le_inl_iff, inl_le_inr, not_inr_le_inl, inr_le_inr_iff, inl_lt_inl_iff, inl_lt_inr, not_inr_lt_inl, inr_lt_inr_iff, mem_Icc, mem_Ico, mem_Ioc, mem_Ioo, mem_Ici, mem_Ioi, mem_Iic, mem_Iio, equiv.coe_to_embedding, to_lex_inj, exists_false, and_false, false_and, map_empty, not_mem_empty, true_and, inl_mem_disj_sum, inr_mem_disj_sum, and_true, of_lex_to_lex, mem_map, embedding.coe_fn_mk, exists_prop, exists_eq_right, embedding.inl_apply]] instance locally_finite_order : locally_finite_order (α ⊕ₗ β) := { finset_Icc := λ a b, (sum_lex_lift Icc Icc (λ a _, Ici a) (λ _, Iic) (of_lex a) (of_lex b)).map to_lex.to_embedding, finset_Ico := λ a b, (sum_lex_lift Ico Ico (λ a _, Ici a) (λ _, Iio) (of_lex a) (of_lex b)).map to_lex.to_embedding, finset_Ioc := λ a b, (sum_lex_lift Ioc Ioc (λ a _, Ioi a) (λ _, Iic) (of_lex a) (of_lex b)).map to_lex.to_embedding, finset_Ioo := λ a b, (sum_lex_lift Ioo Ioo (λ a _, Ioi a) (λ _, Iio) (of_lex a) (of_lex b)).map to_lex.to_embedding, finset_mem_Icc := by simp_lex, finset_mem_Ico := by simp_lex, finset_mem_Ioc := by simp_lex, finset_mem_Ioo := by simp_lex } variables (a a₁ a₂ : α) (b b₁ b₂ : β) lemma Icc_inl_inl : Icc (inlₗ a₁ : α ⊕ₗ β) (inlₗ a₂) = (Icc a₁ a₂).map (embedding.inl.trans to_lex.to_embedding) := by { rw ←finset.map_map, refl } lemma Ico_inl_inl : Ico (inlₗ a₁ : α ⊕ₗ β) (inlₗ a₂) = (Ico a₁ a₂).map (embedding.inl.trans to_lex.to_embedding) := by { rw ←finset.map_map, refl } lemma Ioc_inl_inl : Ioc (inlₗ a₁ : α ⊕ₗ β) (inlₗ a₂) = (Ioc a₁ a₂).map (embedding.inl.trans to_lex.to_embedding) := by { rw ←finset.map_map, refl } lemma Ioo_inl_inl : Ioo (inlₗ a₁ : α ⊕ₗ β) (inlₗ a₂) = (Ioo a₁ a₂).map (embedding.inl.trans to_lex.to_embedding) := by { rw ←finset.map_map, refl } @[simp] lemma Icc_inl_inr : Icc (inlₗ a) (inrₗ b) = ((Ici a).disj_sum (Iic b)).map to_lex.to_embedding := rfl @[simp] lemma Ico_inl_inr : Ico (inlₗ a) (inrₗ b) = ((Ici a).disj_sum (Iio b)).map to_lex.to_embedding := rfl @[simp] lemma Ioc_inl_inr : Ioc (inlₗ a) (inrₗ b) = ((Ioi a).disj_sum (Iic b)).map to_lex.to_embedding := rfl @[simp] lemma Ioo_inl_inr : Ioo (inlₗ a) (inrₗ b) = ((Ioi a).disj_sum (Iio b)).map to_lex.to_embedding := rfl @[simp] lemma Icc_inr_inl : Icc (inrₗ b) (inlₗ a) = ∅ := rfl @[simp] lemma Ico_inr_inl : Ico (inrₗ b) (inlₗ a) = ∅ := rfl @[simp] lemma Ioc_inr_inl : Ioc (inrₗ b) (inlₗ a) = ∅ := rfl @[simp] lemma Ioo_inr_inl : Ioo (inrₗ b) (inlₗ a) = ∅ := rfl lemma Icc_inr_inr : Icc (inrₗ b₁ : α ⊕ₗ β) (inrₗ b₂) = (Icc b₁ b₂).map (embedding.inr.trans to_lex.to_embedding) := by { rw ←finset.map_map, refl } lemma Ico_inr_inr : Ico (inrₗ b₁ : α ⊕ₗ β) (inrₗ b₂) = (Ico b₁ b₂).map (embedding.inr.trans to_lex.to_embedding) := by { rw ←finset.map_map, refl } lemma Ioc_inr_inr : Ioc (inrₗ b₁ : α ⊕ₗ β) (inrₗ b₂) = (Ioc b₁ b₂).map (embedding.inr.trans to_lex.to_embedding) := by { rw ←finset.map_map, refl } lemma Ioo_inr_inr : Ioo (inrₗ b₁ : α ⊕ₗ β) (inrₗ b₂) = (Ioo b₁ b₂).map (embedding.inr.trans to_lex.to_embedding) := by { rw ←finset.map_map, refl } end lex end sum
3b93172f409fe950beb006e2efcc58561eebaaf9
0ce12e02063e04aa70b96c893caa58114f7abc87
/leancrawler/crawler.lean
0b8952ee3e5d81d82791d84a25add15e8a95b085
[ "Apache-2.0" ]
permissive
leanprover-community/leancrawler
9d027f9e06327a024aa6130d451cbc34e4e6dae1
951527886bcf22a1dcb823ce6b2d84f7f66ba25b
refs/heads/master
1,664,420,201,941
1,663,091,473,000
1,663,096,065,000
146,176,705
16
4
Apache-2.0
1,663,096,066,000
1,535,284,760,000
Python
UTF-8
Lean
false
false
6,384
lean
import meta.expr import system.io open tactic declaration environment io io.fs (put_str_ln close) -- The next instance is there to prevent PyYAML trying to be too smart meta def my_name_to_string : has_to_string name := ⟨λ n, "\"" ++ to_string n ++ "\""⟩ local attribute [instance] my_name_to_string meta def expr.get_pi_app_fn : expr → expr | (expr.pi _ _ _ e) := e.get_pi_app_fn | e := e.get_app_fn namespace name_set meta def partition (P : name → bool) (s : name_set) : name_set × name_set := s.fold (s, s) (λ a m, if P a then (m.1, m.2.erase a) else (m.1.erase a, m.2)) end name_set /-- `pre.list_items_aux nm` returns the list of names occuring in the declaration `nm` or (recusively) in any declarations occurring in the value of `nm` with namespace `pre` and whose last component starts with `_`. Auxiliary function for `list_items`. -/ meta def list_items_aux (pre : name) : name → tactic name_set | nm := do env ← get_env, decl ← get_decl nm, let l := decl.value.list_constant, let (aux, l₂) := l.partition (λ nm : name, nm.get_prefix = pre ∧ nm.last.front = '_'), aux.mfold l₂ (λ nm l', list_items_aux nm >>= λ l'', return (l'.union l'')) /-- `list_value_items nm` returns the list of names occuring in the declaration `nm` or (recusively) in any declarations `nm._proof_i` (or to be more precise: any declaration in namespace `nm` whose last part of the name starts with `_`). -/ meta def list_value_items (nm : name) : tactic (list name) := do l ← list_items_aux nm nm, return l.to_list -- let l := l.to_list.map (λ nm : name, if nm.last.front = '_' then nm.get_prefix else nm), -- return l.dedup /-- `list_value_items nm` returns the list of names occuring in the declaration `nm` or (recusively) in any declarations `nm._proof_i` (or to be more precise: any declaration in namespace `nm` whose last part of the name starts with `_`). -/ meta def list_type_items (nm₀ : name) : tactic (list name) := do env ← get_env, decl ← get_decl nm₀, let l := decl.type.list_constant, let (aux, l₂) := l.partition (λ nm : name, nm.get_prefix = nm₀ ∧ nm.last.front = '_'), l₃ ← aux.mfold l₂ (λ nm l', list_items_aux nm₀ nm >>= λ l'', return (l'.union l'')), return l₃.to_list meta def mnot : bool → tactic bool := λ p, return (¬ p) meta def pos_line (p : option pos) : string := match p with | some x := to_string x.line | _ := "" end meta def file_name (p : option string) : string := match p with | some x := x | _ := "Unknown file" end section structure declaration.modifiers := (Class := ff) (Structure := ff) (StructureField := ff) (Inductive := ff) (Instance := ff) (IsRecursor := ff) (IsConstructor := ff) def bool.to_string_python : has_to_string bool := ⟨λ k, match k with tt := "True" | ff := "False" end⟩ local attribute [instance] bool.to_string_python instance : has_to_string declaration.modifiers := ⟨λ m, "{ class: " ++ to_string m.Class ++ ", structure: " ++ to_string m.Structure ++ ", structure_field: " ++ to_string m.StructureField ++ ", is_recursor: " ++ to_string m.IsRecursor ++ ", is_constructor: " ++ to_string m.IsConstructor ++ ", inductive: " ++ to_string m.Inductive ++ ", instance: " ++ to_string m.Instance ++ " }"⟩ open tactic declaration environment meta def declaration.get_kind_string : declaration → string | (thm _ _ _ _) := "lemma" | (defn _ _ _ _ _ _) := "definition" | (cnst _ _ _ _) := "constant" | (ax _ _ _) := "axiom" meta def environment.get_modifiers (env : environment) (n : name) : tactic declaration.modifiers := do c ← (has_attribute `class n >> return tt) <|> return ff, i ← (has_attribute `instance n >> return tt) <|> return ff, return { Class := c, Structure := env.is_structure n, StructureField := (env.is_projection n).is_some, IsConstructor := env.is_constructor n, IsRecursor := env.is_recursor n, Inductive := env.is_ginductive n, Instance := i } end meta def print_item_crawl (env : environment) (decl : declaration) : tactic string := let name := decl.to_name, pos := pos_line (env.decl_pos name), fname := file_name (env.decl_olean name) in do let res := "- Name: " ++ to_string name ++ "\n", let res := res ++ " File: " ++ fname ++ "\n", let res := res ++ " Line: " ++ pos ++ "\n", let res := res ++ " Kind: " ++ decl.get_kind_string ++ "\n", mods ← env.get_modifiers name, let res := res ++ " Modifiers: " ++ to_string mods ++ "\n", pp_type ← pp decl.type, let res := res ++ " Type: " ++ (to_string pp_type).quote ++ "\n", type_decls ← list_type_items name, type_proofs ← type_decls.mfilter $ λ c, mk_const c >>= is_proof, type_others ← type_decls.mfilter $ λ c, mk_const c >>= is_proof >>= mnot, let res := res ++ " Type uses proofs: " ++ to_string type_proofs ++ "\n", let res := res ++ " Type uses others: " ++ to_string type_others ++ "\n", pp_value ← pp decl.value, let res := res ++ " Value: " ++ (to_string pp_value).quote ++ "\n", value_decls ← list_value_items name, value_proofs ← value_decls.mfilter $ λ c, mk_const c >>= is_proof, value_others ← value_decls.mfilter $ λ c, mk_const c >>= is_proof >>= mnot, let res := res ++ " Value uses proofs: " ++ to_string value_proofs ++ "\n", let res := res ++ " Value uses others: " ++ to_string value_others ++ "\n", let res := res ++ (" Target class: " ++ (if mods.Instance then to_string decl.type.get_pi_app_fn else "") ++ "\n"), let res := res ++ (" Parent: " ++ match env.is_projection name with | some info := to_string info.cname ++ "\n" | none := "\n" end), let res := res ++ (" Fields: " ++ (to_string $ (env.structure_fields_full name).get_or_else []) ++ "\n"), return res meta def main : tactic unit := do curr_env ← get_env, h ← unsafe_run_io (mk_file_handle "data.yaml" mode.write), let decls := curr_env.fold [] list.cons, let filtered_decls := decls.filter (λ x, not (to_name x).is_internal), filtered_decls.mmap' (λ d, do s ← (print_item_crawl curr_env d), unsafe_run_io (do io.fs.put_str_ln h s, io.fs.flush h), skip), unsafe_run_io (close h), skip
f8f77e26f71a61b98a21ff2105cc223bcebbf9ef
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/lie/quotient.lean
ad058650538188375b94d1fce07b2790a37fe64d
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
7,625
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.lie.submodule import algebra.lie.of_associative import linear_algebra.isomorphisms /-! # Quotients of Lie algebras and Lie modules Given a Lie submodule of a Lie module, the quotient carries a natural Lie module structure. In the special case that the Lie module is the Lie algebra itself via the adjoint action, the submodule is a Lie ideal and the quotient carries a natural Lie algebra structure. We define these quotient structures here. A notable omission at the time of writing (February 2021) is a statement and proof of the universal property of these quotients. ## Main definitions * `lie_submodule.quotient.lie_quotient_lie_module` * `lie_submodule.quotient.lie_quotient_lie_algebra` ## Tags lie algebra, quotient -/ universes u v w w₁ w₂ namespace lie_submodule variables {R : Type u} {L : Type v} {M : Type w} variables [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] variables [lie_ring_module L M] [lie_module R L M] variables (N N' : lie_submodule R L M) (I J : lie_ideal R L) /-- The quotient of a Lie module by a Lie submodule. It is a Lie module. -/ instance : has_quotient M (lie_submodule R L M) := ⟨λ N, M ⧸ N.to_submodule⟩ namespace quotient variables {N I} instance add_comm_group : add_comm_group (M ⧸ N) := submodule.quotient.add_comm_group _ instance module' {S : Type*} [semiring S] [has_smul S R] [module S M] [is_scalar_tower S R M] : module S (M ⧸ N) := submodule.quotient.module' _ instance module : module R (M ⧸ N) := submodule.quotient.module _ instance is_central_scalar {S : Type*} [semiring S] [has_smul S R] [module S M] [is_scalar_tower S R M] [has_smul Sᵐᵒᵖ R] [module Sᵐᵒᵖ M] [is_scalar_tower Sᵐᵒᵖ R M] [is_central_scalar S M] : is_central_scalar S (M ⧸ N) := submodule.quotient.is_central_scalar _ instance inhabited : inhabited (M ⧸ N) := ⟨0⟩ /-- Map sending an element of `M` to the corresponding element of `M/N`, when `N` is a lie_submodule of the lie_module `N`. -/ abbreviation mk : M → M ⧸ N := submodule.quotient.mk lemma is_quotient_mk (m : M) : quotient.mk' m = (mk m : M ⧸ N) := rfl /-- Given a Lie module `M` over a Lie algebra `L`, together with a Lie submodule `N ⊆ M`, there is a natural linear map from `L` to the endomorphisms of `M` leaving `N` invariant. -/ def lie_submodule_invariant : L →ₗ[R] submodule.compatible_maps N.to_submodule N.to_submodule := linear_map.cod_restrict _ (lie_module.to_endomorphism R L M) $ λ _ _, N.lie_mem variables (N) /-- Given a Lie module `M` over a Lie algebra `L`, together with a Lie submodule `N ⊆ M`, there is a natural Lie algebra morphism from `L` to the linear endomorphism of the quotient `M/N`. -/ def action_as_endo_map : L →ₗ⁅R⁆ module.End R (M ⧸ N) := { map_lie' := λ x y, submodule.linear_map_qext _ $ linear_map.ext $ λ m, congr_arg mk $ lie_lie _ _ _, ..linear_map.comp (submodule.mapq_linear (N : submodule R M) ↑N) lie_submodule_invariant } /-- Given a Lie module `M` over a Lie algebra `L`, together with a Lie submodule `N ⊆ M`, there is a natural bracket action of `L` on the quotient `M/N`. -/ instance action_as_endo_map_bracket : has_bracket L (M ⧸ N) := ⟨λ x n, action_as_endo_map N x n⟩ instance lie_quotient_lie_ring_module : lie_ring_module L (M ⧸ N) := { bracket := has_bracket.bracket, ..lie_ring_module.comp_lie_hom _ (action_as_endo_map N) } /-- The quotient of a Lie module by a Lie submodule, is a Lie module. -/ instance lie_quotient_lie_module : lie_module R L (M ⧸ N) := lie_module.comp_lie_hom _ (action_as_endo_map N) instance lie_quotient_has_bracket : has_bracket (L ⧸ I) (L ⧸ I) := ⟨begin intros x y, apply quotient.lift_on₂' x y (λ x' y', mk ⁅x', y'⁆), intros x₁ x₂ y₁ y₂ h₁ h₂, apply (submodule.quotient.eq I.to_submodule).2, rw submodule.quotient_rel_r_def at h₁ h₂, have h : ⁅x₁, x₂⁆ - ⁅y₁, y₂⁆ = ⁅x₁, x₂ - y₂⁆ + ⁅x₁ - y₁, y₂⁆, by simp [-lie_skew, sub_eq_add_neg, add_assoc], rw h, apply submodule.add_mem, { apply lie_mem_right R L I x₁ (x₂ - y₂) h₂, }, { apply lie_mem_left R L I (x₁ - y₁) y₂ h₁, }, end⟩ @[simp] lemma mk_bracket (x y : L) : mk ⁅x, y⁆ = ⁅(mk x : L ⧸ I), (mk y : L ⧸ I)⁆ := rfl instance lie_quotient_lie_ring : lie_ring (L ⧸ I) := { add_lie := by { intros x' y' z', apply quotient.induction_on₃' x' y' z', intros x y z, repeat { rw is_quotient_mk <|> rw ←mk_bracket <|> rw ←submodule.quotient.mk_add, }, apply congr_arg, apply add_lie, }, lie_add := by { intros x' y' z', apply quotient.induction_on₃' x' y' z', intros x y z, repeat { rw is_quotient_mk <|> rw ←mk_bracket <|> rw ←submodule.quotient.mk_add, }, apply congr_arg, apply lie_add, }, lie_self := by { intros x', apply quotient.induction_on' x', intros x, rw [is_quotient_mk, ←mk_bracket], apply congr_arg, apply lie_self, }, leibniz_lie := by { intros x' y' z', apply quotient.induction_on₃' x' y' z', intros x y z, repeat { rw is_quotient_mk <|> rw ←mk_bracket <|> rw ←submodule.quotient.mk_add, }, apply congr_arg, apply leibniz_lie, } } instance lie_quotient_lie_algebra : lie_algebra R (L ⧸ I) := { lie_smul := by { intros t x' y', apply quotient.induction_on₂' x' y', intros x y, repeat { rw is_quotient_mk <|> rw ←mk_bracket <|> rw ←submodule.quotient.mk_smul, }, apply congr_arg, apply lie_smul, } } /-- `lie_submodule.quotient.mk` as a `lie_module_hom`. -/ @[simps] def mk' : M →ₗ⁅R,L⁆ M ⧸ N := { to_fun := mk, map_lie' := λ r m, rfl, ..N.to_submodule.mkq} @[simp] lemma mk_eq_zero {m : M} : mk' N m = 0 ↔ m ∈ N := submodule.quotient.mk_eq_zero N.to_submodule @[simp] lemma mk'_ker : (mk' N).ker = N := by { ext, simp, } @[simp] lemma map_mk'_eq_bot_le : map (mk' N) N' = ⊥ ↔ N' ≤ N := by rw [← lie_module_hom.le_ker_iff_map, mk'_ker] /-- Two `lie_module_hom`s from a quotient lie module are equal if their compositions with `lie_submodule.quotient.mk'` are equal. See note [partially-applied ext lemmas]. -/ @[ext] lemma lie_module_hom_ext ⦃f g : M ⧸ N →ₗ⁅R,L⁆ M⦄ (h : f.comp (mk' N) = g.comp (mk' N)) : f = g := lie_module_hom.ext $ λ x, quotient.induction_on' x $ lie_module_hom.congr_fun h end quotient end lie_submodule namespace lie_hom variables {R L L' : Type*} variables [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] variables (f : L →ₗ⁅R⁆ L') /-- The first isomorphism theorem for morphisms of Lie algebras. -/ @[simps] noncomputable def quot_ker_equiv_range : L ⧸ f.ker ≃ₗ⁅R⁆ f.range := { to_fun := (f : L →ₗ[R] L').quot_ker_equiv_range, map_lie' := begin rintros ⟨x⟩ ⟨y⟩, rw [← set_like.coe_eq_coe, lie_subalgebra.coe_bracket], simp only [submodule.quotient.quot_mk_eq_mk, linear_map.quot_ker_equiv_range_apply_mk, ← lie_submodule.quotient.mk_bracket, coe_to_linear_map, map_lie], end, .. (f : L →ₗ[R] L').quot_ker_equiv_range, } end lie_hom
0da331b59da15d6168c96469bf3e80b33a77c536
38ee9024fb5974f555fb578fcf5a5a7b71e669b5
/Mathlib/Init/Data/Nat/Lemmas.lean
8c30b38d989b4e240dd92f192858c660b6fcc327
[ "Apache-2.0" ]
permissive
denayd/mathlib4
750e0dcd106554640a1ac701e51517501a574715
7f40a5c514066801ab3c6d431e9f405baa9b9c58
refs/heads/master
1,693,743,991,894
1,636,618,048,000
1,636,618,048,000
373,926,241
0
0
null
null
null
null
UTF-8
Lean
false
false
37,217
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad -/ import Mathlib.Init.Data.Nat.Basic import Mathlib.Init.Algebra.Functions universe u namespace Nat lemma succ_ne_self : ∀ n : ℕ, succ n ≠ n | 0, h => absurd h (Nat.succ_ne_zero 0) | n+1, h => succ_ne_self n (Nat.noConfusion h id) protected lemma eq_zero_of_add_eq_zero_right : ∀ {n m : ℕ}, n + m = 0 → n = 0 | 0, m => by simp [Nat.zero_add] | n+1, m => fun h => by exfalso rw [add_one, succ_add] at h apply succ_ne_zero _ h protected lemma eq_zero_of_add_eq_zero_left {n m : ℕ} (h : n + m = 0) : m = 0 := @Nat.eq_zero_of_add_eq_zero_right m n (Nat.add_comm n m ▸ h) @[simp] lemma pred_zero : pred 0 = 0 := rfl @[simp] lemma pred_succ (n : ℕ) : pred (succ n) = n := rfl /- private meta def sort_add := `[simp [nat.add_assoc, nat.add_comm, nat.add_left_comm]] -/ /- properties of inequality -/ protected lemma pos_of_ne_zero {n : ℕ} : n ≠ 0 → 0 < n := Or.resolve_left (eq_zero_or_pos n) protected lemma lt_iff_le_not_le {m n : ℕ} : m < n ↔ m ≤ n ∧ ¬ n ≤ m := ⟨fun h => ⟨Nat.le_of_lt h, Nat.not_le_of_gt h⟩, fun h => Nat.gt_of_not_le h.2⟩ instance : LinearOrder ℕ := { le := Nat.le, le_refl := @Nat.le_refl, le_trans := @Nat.le_trans, le_antisymm := @Nat.le_antisymm, le_total := @Nat.le_total, lt := Nat.lt, lt_iff_le_not_le := @Nat.lt_iff_le_not_le, decidable_lt := inferInstance, decidable_le := inferInstance, decidable_eq := inferInstance } lemma pred_lt_pred : ∀ {n m : ℕ}, n ≠ 0 → n < m → pred n < pred m | 0, _, h, _ => (h rfl).elim | n+1, m+1, _, h => lt_of_succ_lt_succ h -- Port note: this lemma was not present in Lean 3 protected lemma add_left_cancel_iff {n m k : ℕ} : n + m = n + k ↔ m = k := ⟨Nat.add_left_cancel, fun | rfl => rfl⟩ protected lemma le_of_add_le_add_left {k n m : ℕ} (h : k + n ≤ k + m) : n ≤ m := by let ⟨w, hw⟩ := le.dest h rw [Nat.add_assoc, Nat.add_left_cancel_iff] at hw exact Nat.le.intro hw protected lemma le_of_add_le_add_right {k n m : ℕ} : n + k ≤ m + k → n ≤ m := by rw [Nat.add_comm _ k, Nat.add_comm _ k] apply Nat.le_of_add_le_add_left protected lemma add_le_add_iff_le_right (k n m : ℕ) : n + k ≤ m + k ↔ n ≤ m := ⟨Nat.le_of_add_le_add_right, fun h => Nat.add_le_add_right h _⟩ protected lemma lt_of_add_lt_add_left {k n m : ℕ} (h : k + n < k + m) : n < m := Nat.lt_of_le_of_ne (Nat.le_of_add_le_add_left (Nat.le_of_lt h)) fun heq => Nat.lt_irrefl (k + m) $ by rwa [heq] at h protected lemma lt_of_add_lt_add_right {a b c : ℕ} (h : a + b < c + b) : a < c := Nat.lt_of_add_lt_add_left ((by rwa [Nat.add_comm b a, Nat.add_comm b c]): b + a < b + c) protected lemma lt_add_right (a b c : Nat) : a < b -> a < b + c := fun h => lt_of_lt_of_le h (Nat.le_add_right _ _) protected lemma lt_add_of_pos_right {n k : ℕ} (h : 0 < k) : n < n + k := Nat.add_lt_add_left h n protected lemma lt_add_of_pos_left {n k : ℕ} (h : 0 < k) : n < k + n := by rw [Nat.add_comm]; exact Nat.lt_add_of_pos_right h protected lemma le_of_mul_le_mul_left {a b c : ℕ} (h : c * a ≤ c * b) (hc : 0 < c) : a ≤ b := not_lt.1 fun h1 => not_le.2 (Nat.mul_lt_mul_of_pos_left h1 hc) h protected theorem eq_of_mul_eq_mul_left {m k n : ℕ} (Hn : 0 < n) (H : n * m = n * k) : m = k := Nat.le_antisymm (Nat.le_of_mul_le_mul_left (Nat.le_of_eq H) Hn) (Nat.le_of_mul_le_mul_left (Nat.le_of_eq H.symm) Hn) /- sub properties -/ @[simp] protected lemma zero_sub : ∀ a : ℕ, 0 - a = 0 | 0 => rfl | succ a => congr_arg pred (Nat.zero_sub a) lemma sub_lt_succ (a b : ℕ) : a - b < succ a := lt_succ_of_le (sub_le a b) protected lemma sub_le_sub_right {n m : ℕ} (h : n ≤ m) : ∀ k, n - k ≤ m - k | 0 => h | z+1 => pred_le_pred (Nat.sub_le_sub_right h z) -- port note: bit0/bit1 items have been omitted. protected lemma add_self_ne_one : ∀ (n : ℕ), n + n ≠ 1 | n+1, h => have h1 : succ (succ (n + n)) = 1 := succ_add n n ▸ h Nat.noConfusion h1 fun. /- subtraction -/ protected lemma add_sub_add_right : ∀ (n k m : ℕ), (n + k) - (m + k) = n - m | n, 0, m => by rw [Nat.add_zero, Nat.add_zero] | n, k+1, m => by rw [add_succ, add_succ, succ_sub_succ, Nat.add_sub_add_right n k m] protected lemma add_sub_add_left (k n m : ℕ) : (k + n) - (k + m) = n - m := by rw [Nat.add_comm k n, Nat.add_comm k m, Nat.add_sub_add_right] protected lemma add_sub_cancel (n m : ℕ) : n + m - m = n := suffices n + m - (0 + m) = n by rwa [Nat.zero_add] at this by rw [Nat.add_sub_add_right, Nat.sub_zero] protected lemma add_sub_cancel_left (n m : ℕ) : n + m - n = m := show n + m - (n + 0) = m by rw [Nat.add_sub_add_left, Nat.sub_zero] protected lemma sub_sub : ∀ (n m k : ℕ), n - m - k = n - (m + k) | n, m, 0 => by rw [Nat.add_zero, Nat.sub_zero] | n, m, (succ k) => by rw [add_succ, sub_succ, sub_succ, Nat.sub_sub n m k] protected lemma le_of_le_of_sub_le_sub_right {n m k : ℕ} (h₀ : k ≤ m) (h₁ : n - k ≤ m - k) : n ≤ m := by revert k m induction n with intros m k h₀ h₁ | zero => exact m.zero_le | succ pn hpn => cases k with | zero => apply h₁ | succ k => cases m with | zero => cases not_succ_le_zero k h₀ | succ m => simp [succ_sub_succ] at h₁ apply succ_le_succ apply hpn _ h₁ apply le_of_succ_le_succ h₀ protected lemma sub_le_sub_right_iff {n m k : ℕ} (h : k ≤ m) : n - k ≤ m - k ↔ n ≤ m := ⟨Nat.le_of_le_of_sub_le_sub_right h, fun h => Nat.sub_le_sub_right h k⟩ protected theorem sub_self_add (n m : ℕ) : n - (n + m) = 0 := show (n + 0) - (n + m) = 0 by rw [Nat.add_sub_add_left, Nat.zero_sub] protected theorem add_le_to_le_sub (x : ℕ) {y k : ℕ} (h : k ≤ y) : x + k ≤ y ↔ x ≤ y - k := by rw [← Nat.add_sub_cancel x k, Nat.sub_le_sub_right_iff h, Nat.add_sub_cancel] protected lemma sub_lt_of_pos_le (a b : ℕ) (h₀ : 0 < a) (h₁ : a ≤ b) : b - a < b := by apply Nat.sub_lt _ h₀ apply lt_of_lt_of_le h₀ h₁ protected theorem sub_one (n : ℕ) : n - 1 = pred n := rfl theorem succ_sub_one (n : ℕ) : succ n - 1 = n := rfl theorem succ_pred_eq_of_pos : ∀ {n : ℕ}, 0 < n → succ (pred n) = n | succ k, h => rfl protected theorem sub_eq_zero_of_le {n m : ℕ} (h : n ≤ m) : n - m = 0 := Exists.elim (Nat.le.dest h) (λ k => λ hk : n + k = m => by rw [← hk, Nat.sub_self_add]) protected theorem le_of_sub_eq_zero : ∀{n m : ℕ}, n - m = 0 → n ≤ m | n, 0, H => by rw [Nat.sub_zero] at H; simp [H] | 0, m+1, H => Nat.zero_le (m + 1) | n+1, m+1, H => Nat.add_le_add_right (Nat.le_of_sub_eq_zero (by simp [Nat.add_sub_add_right] at H; exact H)) _ protected theorem eq_zero_of_nonpos : ∀ (n : Nat), ¬0 < n -> n = 0 | 0 => fun _ => rfl | n+1 => fun h => absurd (Nat.zero_lt_succ n) h protected lemma sub_eq_zero_iff_le : n - m = 0 ↔ n ≤ m := ⟨Nat.le_of_sub_eq_zero, Nat.sub_eq_zero_of_le⟩ protected lemma add_sub_of_le {n m : ℕ} (h : n ≤ m) : n + (m - n) = m := let ⟨k, hk⟩ := Nat.le.dest h; by rw [← hk, Nat.add_sub_cancel_left] protected lemma sub_add_cancel {n m : ℕ} (h : m ≤ n) : n - m + m = n := by rw [Nat.add_comm, Nat.add_sub_of_le h] protected lemma add_sub_assoc {m k : ℕ} (h : k ≤ m) (n : ℕ) : n + m - k = n + (m - k) := let ⟨l, hl⟩ := Nat.le.dest h by rw [← hl, Nat.add_sub_cancel_left, Nat.add_comm k, ← Nat.add_assoc, Nat.add_sub_cancel] protected lemma sub_eq_iff_eq_add {a b c : ℕ} (ab : b ≤ a) : a - b = c ↔ a = c + b := ⟨fun c_eq => by rw [c_eq.symm, Nat.sub_add_cancel ab], fun a_eq => by rw [a_eq, Nat.add_sub_cancel]⟩ protected lemma lt_of_sub_eq_succ (H : m - n = succ l) : n < m := not_le.1 fun H' => by simp [Nat.sub_eq_zero_of_le H'] at H protected lemma zero_min (a : ℕ) : Nat.min 0 a = 0 := min_eq_left (zero_le a) protected lemma min_zero (a : ℕ) : Nat.min a 0 = 0 := min_eq_right (zero_le a) -- Distribute succ over min theorem min_succ_succ (x y : ℕ) : min (succ x) (succ y) = succ (min x y) := have f : x ≤ y → min (succ x) (succ y) = succ (min x y) := by intro p have h1 : min (succ x) (succ y) = succ x := if_pos (succ_le_succ p) have h2 : succ x = succ (min x y) := congr_arg succ (Eq.symm (if_pos p)) rwa [←h2] have g : ¬ (x ≤ y) → min (succ x) (succ y) = succ (min x y) := by intro p have h1 : min (succ x) (succ y) = succ y := if_neg (λeq => let hp : pred (succ x) ≤ pred (succ y) := (pred_le_pred eq) let hp2 : x ≤ y := hp p hp) have h2 : succ y = succ (min x y) := congr_arg succ (Eq.symm (if_neg p)) rwa [←h2] Decidable.by_cases f g theorem sub_eq_sub_min (n m : ℕ) : n - m = n - min n m := if h : n ≥ m then by rw [min_eq_right h] else by rw [Nat.sub_eq_zero_of_le (le_of_not_ge h), min_eq_left (le_of_not_ge h), Nat.sub_self] @[simp] protected theorem sub_add_min_cancel (n m : ℕ) : n - m + min n m = n := by rw [sub_eq_sub_min, Nat.sub_add_cancel (min_le_left n m)] /- TODO(Leo): sub + inequalities -/ protected def strong_rec_on {p : ℕ → Sort u} (n : ℕ) (H : ∀ n, (∀ m, m < n → p m) → p n) : p n := Nat.lt_wfRel.wf.fix' H n protected lemma strong_induction_on {p : Nat → Prop} (n : Nat) (h : ∀ n, (∀ m, m < n → p m) → p n) : p n := Nat.strong_rec_on n h protected lemma case_strong_induction_on {p : Nat → Prop} (a : Nat) (hz : p 0) (hi : ∀ n, (∀ m, m ≤ n → p m) → p (succ n)) : p a := Nat.strong_induction_on a $ λ n => match n with | 0 => λ _ => hz | (n+1) => λ h₁ => hi n (λ m h₂ => h₁ _ (lt_succ_of_le h₂)) /- mod -/ -- TODO mod_core_congr, mod_def lemma mod_two_eq_zero_or_one (n : ℕ) : n % 2 = 0 ∨ n % 2 = 1 := match n % 2, @Nat.mod_lt n 2 (by simp) with | 0, _ => Or.inl rfl | 1, _ => Or.inr rfl | k+2, h => absurd h (λ h => not_lt_zero k (lt_of_succ_lt_succ (lt_of_succ_lt_succ h))) /- div & mod -/ -- TODO div_core_congr, div_def lemma mod_add_div (m k : ℕ) : m % k + k * (m / k) = m := by induction m, k using mod.inductionOn with rw [div_eq, mod_eq] | base x y h => simp [h] | ind x y h IH => simp [h]; rw [Nat.mul_succ, ← Nat.add_assoc, IH, Nat.sub_add_cancel h.2] /- div -/ @[simp] protected lemma div_one (n : ℕ) : n / 1 = n := have this : n % 1 + 1 * (n / 1) = n := mod_add_div _ _ by rwa [mod_one, Nat.zero_add, Nat.one_mul] at this @[simp] protected lemma div_zero (n : ℕ) : n / 0 = 0 := by rw [div_eq]; simp [Nat.lt_irrefl] @[simp] protected lemma zero_div (b : ℕ) : 0 / b = 0 := (div_eq 0 b).trans $ if_neg $ And.rec Nat.not_le_of_gt lemma le_div_iff_mul_le (k0 : 0 < k) : x ≤ y / k ↔ x * k ≤ y := by induction y, k using mod.inductionOn generalizing x with (rw [div_eq]; simp [h]; cases x with simp [zero_le] | succ x => ?_) | base y k h => simp [eq_false (not_succ_le_zero x), succ_mul, Nat.add_comm] refine Nat.lt_of_lt_of_le ?_ (Nat.le_add_right _ _) exact not_le.1 fun h' => h ⟨k0, h'⟩ | ind y k h IH => rw [← add_one, Nat.add_le_add_iff_le_right, IH k0, succ_mul, ← Nat.add_sub_cancel (x*k) k, Nat.sub_le_sub_right_iff h.2, Nat.add_sub_cancel] protected lemma div_le_of_le_mul {m n : ℕ} : ∀ {k}, m ≤ k * n → m / k ≤ n | 0, h => by simp [Nat.div_zero, n.zero_le] | succ k, h => suffices succ k * (m / succ k) ≤ succ k * n from Nat.le_of_mul_le_mul_left this (zero_lt_succ _) by have h1 : succ k * (m / succ k) ≤ m % succ k + succ k * (m / succ k) := Nat.le_add_left _ _ have h2 : m % succ k + succ k * (m / succ k) = m := by rw [mod_add_div] have h3 : m ≤ succ k * n := h rw [←h2] at h3 exact le_trans h1 h3 protected lemma div_le_self : ∀ (m n : ℕ), m / n ≤ m | m, 0 => by simp [Nat.div_zero]; apply zero_le | m, n+1 => Nat.div_le_of_le_mul $ by have := Nat.mul_le_mul_right m (succ_pos n) rwa [Nat.one_mul] at this lemma div_eq_sub_div (h₁ : 0 < b) (h₂ : b ≤ a) : a / b = (a - b) / b + 1 := by rw [div_eq a, if_pos]; constructor <;> assumption lemma div_eq_of_lt (h₀ : a < b) : a / b = 0 := by rw [div_eq a, if_neg] intro h₁ apply Nat.not_le_of_gt h₀ h₁.right lemma div_lt_iff_lt_mul (Hk : 0 < k) : x / k < y ↔ x < y * k := by rw [← not_le, ← not_le]; apply not_congr; apply le_div_iff_mul_le Hk def iterate {α : Sort u} (op : α → α) : ℕ → α → α | 0, a => a | succ k, a => iterate op k (op a) notation f "^["n"]" => iterate f n /- successor and predecessor -/ theorem add_one_ne_zero (n : ℕ) : n + 1 ≠ 0 := succ_ne_zero _ lemma eq_zero_or_eq_succ_pred (n : ℕ) : n = 0 ∨ n = succ (pred n) := by cases n <;> simp theorem exists_eq_succ_of_ne_zero {n : ℕ} (H : n ≠ 0) : ∃k : ℕ, n = succ k := ⟨_, (eq_zero_or_eq_succ_pred _).resolve_left H⟩ def discriminate (H1: n = 0 → α) (H2 : ∀m, n = succ m → α) : α := match e: n with | 0 => H1 e | succ m => H2 m e lemma one_succ_zero : 1 = succ 0 := rfl def two_step_induction {P : ℕ → Sort u} (H1 : P 0) (H2 : P 1) (H3 : ∀ (n : ℕ) (IH1 : P n) (IH2 : P (succ n)), P (succ (succ n))) : (a : ℕ) → P a | 0 => H1 | 1 => H2 | n+2 => H3 _ (two_step_induction H1 H2 H3 _) (two_step_induction H1 H2 H3 _) def sub_induction {P : ℕ → ℕ → Sort u} (H1 : ∀m, P 0 m) (H2 : ∀n, P (succ n) 0) (H3 : ∀n m, P n m → P (succ n) (succ m)) : (n m : ℕ) → P n m | 0, m => H1 _ | n+1, 0 => H2 _ | n+1, m+1 => H3 _ _ (sub_induction H1 H2 H3 n m) /- addition -/ theorem succ_add_eq_succ_add (n m : ℕ) : succ n + m = n + succ m := by simp [succ_add, add_succ] theorem one_add (n : ℕ) : 1 + n = succ n := by simp [Nat.add_comm] lemma eq_zero_of_add_eq_zero {n m : ℕ} (H : n + m = 0) : n = 0 ∧ m = 0 := ⟨Nat.eq_zero_of_add_eq_zero_right H, Nat.eq_zero_of_add_eq_zero_left H⟩ lemma eq_zero_of_mul_eq_zero : ∀ {n m : ℕ}, n * m = 0 → n = 0 ∨ m = 0 | 0, m, _ => Or.inl rfl | n+1, m, h => by rw [succ_mul] at h; exact Or.inr (Nat.eq_zero_of_add_eq_zero_left h) /- properties of inequality -/ lemma le_succ_of_pred_le {n m : ℕ} : pred n ≤ m → n ≤ succ m := match n with | 0 => fun _ => zero_le _ | a+1 => succ_le_succ lemma le_lt_antisymm {n m : ℕ} (h₁ : n ≤ m) (h₂ : m < n) : False := Nat.lt_irrefl n (Nat.lt_of_le_of_lt h₁ h₂) lemma lt_le_antisymm {n m : ℕ} (h₁ : n < m) (h₂ : m ≤ n) : False := le_lt_antisymm h₂ h₁ protected lemma lt_asymm {n m : ℕ} (h₁ : n < m) : ¬ m < n := le_lt_antisymm (Nat.le_of_lt h₁) protected def lt_ge_by_cases {a b : ℕ} {C : Sort u} (h₁ : a < b → C) (h₂ : b ≤ a → C) : C := if h : a < b then h₁ h else h₂ (not_lt.1 h) protected def lt_by_cases {a b : ℕ} {C : Sort u} (h₁ : a < b → C) (h₂ : a = b → C) (h₃ : b < a → C) : C := Nat.lt_ge_by_cases h₁ fun h₁ => Nat.lt_ge_by_cases h₃ fun h => h₂ (Nat.le_antisymm h h₁) protected lemma lt_trichotomy (a b : ℕ) : a < b ∨ a = b ∨ b < a := Nat.lt_by_cases Or.inl (Or.inr ∘ Or.inl) (Or.inr ∘ Or.inr) protected lemma eq_or_lt_of_not_lt {a b : ℕ} (hnlt : ¬ a < b) : a = b ∨ b < a := (Nat.lt_trichotomy a b).resolve_left hnlt lemma lt_succ_of_lt (h : a < b) : a < succ b := le_succ_of_le h lemma one_pos : 0 < 1 := Nat.zero_lt_one /- subtraction -/ protected lemma sub_add_eq_max {a b : Nat} : a - b + b = max a b := by cases (@le_total Nat Nat.instLinearOrderNat a b) with | inl hl => rw [max_eq_right hl, Nat.sub_eq_zero_iff_le.mpr hl, Nat.zero_add] | inr hr => rw [max_eq_left hr, Nat.sub_add_cancel hr] protected lemma sub_le_sub_left (k : ℕ) (h : n ≤ m) : k - m ≤ k - n := match m, le.dest h with | _, ⟨a, rfl⟩ => by rw [← Nat.sub_sub]; apply sub_le theorem succ_sub_sub_succ (n m k : ℕ) : succ n - m - succ k = n - m - k := by rw [Nat.sub_sub, Nat.sub_sub, add_succ, succ_sub_succ] protected lemma sub.right_comm (m n k : ℕ) : m - n - k = m - k - n := by rw [Nat.sub_sub, Nat.sub_sub, Nat.add_comm] lemma mul_pred_left : ∀ (n m : ℕ), pred n * m = n * m - m | 0, m => by simp [Nat.zero_sub, pred_zero, Nat.zero_mul] | n+1, m => by rw [pred_succ, succ_mul, Nat.add_sub_cancel] lemma mul_pred_right (n m : ℕ) : n * pred m = n * m - n := by rw [Nat.mul_comm, mul_pred_left, Nat.mul_comm] protected lemma mul_sub_right_distrib (n) : ∀ (m k : ℕ), (n - m) * k = n * k - m * k | 0, k => by simp [Nat.sub_zero, Nat.zero_mul] | m+1, k => by rw [Nat.sub_succ, mul_pred_left, Nat.mul_sub_right_distrib, succ_mul, Nat.sub_sub] protected lemma mul_sub_left_distrib (n m k : ℕ) : n * (m - k) = n * m - n * k := by rw [Nat.mul_comm, Nat.mul_sub_right_distrib, Nat.mul_comm m n, Nat.mul_comm n k] protected lemma mul_self_sub_mul_self_eq (a b : Nat) : a * a - b * b = (a + b) * (a - b) := by rw [Nat.mul_sub_left_distrib, Nat.right_distrib, Nat.right_distrib, Nat.mul_comm b a, Nat.add_comm (a*a) (a*b), Nat.add_sub_add_left] lemma succ_mul_succ_eq (a b : Nat) : succ a * succ b = a*b + a + b + 1 := by rw [mul_succ, succ_mul, Nat.add_right_comm _ a]; rfl theorem succ_sub {m n : ℕ} (h : n ≤ m) : succ m - n = succ (m - n) := Exists.elim (Nat.le.dest h) (λ k => λ hk : n + k = m => by rw [← hk, Nat.add_sub_cancel_left, ← add_succ, Nat.add_sub_cancel_left]) protected lemma sub_pos_of_lt (h : m < n) : 0 < n - m := by apply Nat.lt_of_add_lt_add_right (b := m) rw [Nat.zero_add, Nat.sub_add_cancel (Nat.le_of_lt h)]; exact h protected lemma sub_sub_self {n m : ℕ} (h : m ≤ n) : n - (n - m) = m := (Nat.sub_eq_iff_eq_add (Nat.sub_le _ _)).2 (Nat.add_sub_of_le h).symm protected lemma sub_add_comm {n m k : ℕ} (h : k ≤ n) : n + m - k = n - k + m := (Nat.sub_eq_iff_eq_add (Nat.le_trans h (Nat.le_add_right _ _))).2 (by rwa [Nat.add_right_comm, Nat.sub_add_cancel]) lemma sub_one_sub_lt (h : i < n) : n - 1 - i < n := by rw [Nat.sub_sub] apply Nat.sub_lt apply Nat.lt_of_lt_of_le (Nat.zero_lt_succ _) h rw [Nat.add_comm] apply Nat.zero_lt_succ lemma pred_inj : ∀ {a b : ℕ}, 0 < a → 0 < b → Nat.pred a = Nat.pred b → a = b | a+1, b+1, ha, hb, h => by rw [show a = b from h] | a+1, 0, ha, hb, h => absurd hb (Nat.lt_irrefl _) | 0, b+1, ha, hb, h => absurd ha (Nat.lt_irrefl _) | 0, 0, ha, hb, h => rfl /- find -/ section find variable (p : ℕ → Prop) private def lbp (m n : ℕ) : Prop := m = n + 1 ∧ ∀ k, k ≤ n → ¬p k variable [DecidablePred p] (H : ∃ n, p n) private def wf_lbp : WellFounded (lbp p) := by refine ⟨let ⟨n, pn⟩ := H; ?_⟩ suffices ∀ m k, n ≤ k + m → Acc (lbp p) k from fun a => this _ _ (Nat.le_add_left _ _) intro m induction m with refine fun k kn => ⟨_, fun | _, ⟨rfl, a⟩ => ?_⟩ | zero => exact absurd pn (a _ kn) | succ m IH => exact IH _ (by rw [Nat.add_right_comm]; exact kn) protected def find_x : {n // p n ∧ ∀ m, m < n → ¬p m} := (wf_lbp p H).fix' (C := fun k => (∀n, n < k → ¬p n) → {n // p n ∧ ∀ m, m < n → ¬p m}) (fun m IH al => if pm : p m then ⟨m, pm, al⟩ else have this : ∀ n, n ≤ m → ¬p n := fun n h => (lt_or_eq_of_le h).elim (al n) fun e => by rw [e]; exact pm IH _ ⟨rfl, this⟩ fun n h => this n $ Nat.le_of_succ_le_succ h) 0 fun n h => absurd h (Nat.not_lt_zero _) /-- If `p` is a (decidable) predicate on `ℕ` and `hp : ∃ (n : ℕ), p n` is a proof that there exists some natural number satisfying `p`, then `nat.find hp` is the smallest natural number satisfying `p`. Note that `nat.find` is protected, meaning that you can't just write `find`, even if the `nat` namespace is open. The API for `nat.find` is: * `nat.find_spec` is the proof that `nat.find hp` satisfies `p`. * `nat.find_min` is the proof that if `m < nat.find hp` then `m` does not satisfy `p`. * `nat.find_min'` is the proof that if `m` does satisfy `p` then `nat.find hp ≤ m`. -/ protected def find : ℕ := (Nat.find_x p H).1 protected lemma find_spec : p (Nat.find p H) := (Nat.find_x p H).2.1 protected lemma find_min : ∀ {m : ℕ}, m < Nat.find p H → ¬p m := @(Nat.find_x p H).2.2 protected lemma find_min' {m : ℕ} (h : p m) : Nat.find p H ≤ m := not_lt.1 fun l => Nat.find_min p H l h end find /- mod -/ @[simp] theorem add_mod_right (x z : ℕ) : (x + z) % z = x % z := by rw [mod_eq_sub_mod (Nat.le_add_left _ _), Nat.add_sub_cancel] @[simp] lemma add_mod_left (x z : ℕ) : (x + z) % x = z % x := by rw [Nat.add_comm, add_mod_right] @[simp] lemma add_mul_mod_self_left (x y z : ℕ) : (x + y * z) % y = x % y := by induction z with | zero => rw [Nat.mul_zero, Nat.add_zero] | succ z ih => rw [mul_succ, ← Nat.add_assoc, add_mod_right, ih] @[simp] lemma add_mul_mod_self_right (x y z : ℕ) : (x + y * z) % z = x % z := by rw [Nat.mul_comm, add_mul_mod_self_left] @[simp] lemma mul_mod_right (m n : ℕ) : (m * n) % m = 0 := by rw [← Nat.zero_add (m*n), add_mul_mod_self_left, zero_mod] @[simp] theorem mul_mod_left (m n : ℕ) : (m * n) % n = 0 := by rw [Nat.mul_comm, mul_mod_right] lemma mul_mod_mul_left (z x y : ℕ) : (z * x) % (z * y) = z * (x % y) := if y0 : y = 0 then by rw [y0, Nat.mul_zero, mod_zero, mod_zero] else if z0 : z = 0 then by rw [z0, Nat.zero_mul, Nat.zero_mul, Nat.zero_mul, mod_zero] else by induction x using Nat.strong_rec_on with | _ n IH => have y0 : y > 0 := Nat.pos_of_ne_zero y0 have z0 : z > 0 := Nat.pos_of_ne_zero z0 cases Nat.lt_or_ge n y with | inl yn => rw [mod_eq_of_lt yn, mod_eq_of_lt (Nat.mul_lt_mul_of_pos_left yn z0)] | inr yn => rw [mod_eq_sub_mod yn, mod_eq_sub_mod (Nat.mul_le_mul_left z yn), ← Nat.mul_sub_left_distrib] exact IH _ (sub_lt (Nat.lt_of_lt_of_le y0 yn) y0) lemma mul_mod_mul_right (z x y : ℕ) : (x * z) % (y * z) = (x % y) * z := by rw [Nat.mul_comm x z, Nat.mul_comm y z, Nat.mul_comm (x % y) z]; apply mul_mod_mul_left -- TODO cont_to_bool_mod_two lemma sub_mul_mod (x k n : ℕ) (h₁ : n*k ≤ x) : (x - n*k) % n = x % n := by induction k with | zero => rw [Nat.mul_zero, Nat.sub_zero] | succ k IH => have h₂ : n * k ≤ x := by rw [mul_succ] at h₁ apply Nat.le_trans _ h₁ apply le_add_right _ n have h₄ : x - n * k ≥ n := by apply Nat.le_of_add_le_add_right (k := n*k) rw [Nat.sub_add_cancel h₂] simp [mul_succ, Nat.add_comm] at h₁; simp [h₁] rw [mul_succ, ← Nat.sub_sub, ← mod_eq_sub_mod h₄, IH h₂] /- div -/ lemma sub_mul_div (x n p : ℕ) (h₁ : n*p ≤ x) : (x - n*p) / n = x / n - p := by cases eq_zero_or_pos n with | inl h₀ => rw [h₀, Nat.div_zero, Nat.div_zero, Nat.zero_sub] | inr h₀ => induction p with | zero => rw [Nat.mul_zero, Nat.sub_zero, Nat.sub_zero] | succ p IH => have h₂ : n*p ≤ x := by transitivity · apply Nat.mul_le_mul_left; apply le_succ · apply h₁ have h₃ : x - n * p ≥ n := by apply Nat.le_of_add_le_add_right rw [Nat.sub_add_cancel h₂, Nat.add_comm] rw [mul_succ] at h₁ apply h₁ rw [sub_succ, ← IH h₂] rw [div_eq_sub_div h₀ h₃] simp [add_one, pred_succ, mul_succ, Nat.sub_sub] lemma div_mul_le_self : ∀ (m n : ℕ), m / n * n ≤ m | m, 0 => by simp; apply zero_le | m, n+1 => (le_div_iff_mul_le (Nat.succ_pos _)).1 (Nat.le_refl _) @[simp] lemma add_div_right (x : ℕ) {z : ℕ} (H : 0 < z) : (x + z) / z = succ (x / z) := by rw [div_eq_sub_div H (Nat.le_add_left _ _), Nat.add_sub_cancel] @[simp] lemma add_div_left (x : ℕ) {z : ℕ} (H : 0 < z) : (z + x) / z = succ (x / z) := by rw [Nat.add_comm, add_div_right x H] @[simp] lemma mul_div_right (n : ℕ) {m : ℕ} (H : 0 < m) : m * n / m = n := by induction n <;> simp_all [mul_succ] @[simp] lemma mul_div_left (m : ℕ) {n : ℕ} (H : 0 < n) : m * n / n = m := by rw [Nat.mul_comm, mul_div_right _ H] protected lemma div_self (H : 0 < n) : n / n = 1 := let t := add_div_right 0 H; by rwa [Nat.zero_add, Nat.zero_div] at t lemma add_mul_div_left (x z : ℕ) {y : ℕ} (H : 0 < y) : (x + y * z) / y = x / y + z := by induction z with | zero => rw [Nat.mul_zero, Nat.add_zero, Nat.add_zero] | succ z ih => rw [mul_succ, ← Nat.add_assoc, add_div_right _ H, ih]; rfl lemma add_mul_div_right (x y : ℕ) {z : ℕ} (H : 0 < z) : (x + y * z) / z = x / z + y := by rw [Nat.mul_comm, add_mul_div_left _ _ H] protected lemma mul_div_cancel (m : ℕ) {n : ℕ} (H : 0 < n) : m * n / n = m := let t := add_mul_div_right 0 m H; by rwa [Nat.zero_add, Nat.zero_div, Nat.zero_add] at t protected lemma mul_div_cancel_left (m : ℕ) {n : ℕ} (H : 0 < n) : n * m / n = m := by rw [Nat.mul_comm, Nat.mul_div_cancel _ H] protected lemma div_eq_of_eq_mul_left (H1 : 0 < n) (H2 : m = k * n) : m / n = k := by rw [H2, Nat.mul_div_cancel _ H1] protected lemma div_eq_of_eq_mul_right (H1 : 0 < n) (H2 : m = n * k) : m / n = k := by rw [H2, Nat.mul_div_cancel_left _ H1] protected lemma div_eq_of_lt_le (lo : k * n ≤ m) (hi : m < succ k * n) : m / n = k := have npos : 0 < n := (eq_zero_or_pos _).resolve_left fun hn => by rw [hn, Nat.mul_zero] at hi lo; exact absurd lo (Nat.not_le_of_gt hi) Nat.le_antisymm (le_of_lt_succ ((Nat.div_lt_iff_lt_mul npos).2 hi)) ((Nat.le_div_iff_mul_le npos).2 lo) lemma mul_sub_div (x n p : ℕ) (h₁ : x < n*p) : (n * p - succ x) / n = p - succ (x / n) := by have npos : 0 < n := (eq_zero_or_pos _).resolve_left fun n0 => by rw [n0, Nat.zero_mul] at h₁; exact not_lt_zero _ h₁ apply Nat.div_eq_of_lt_le · rw [Nat.mul_sub_right_distrib, Nat.mul_comm] apply Nat.sub_le_sub_left exact (div_lt_iff_lt_mul npos).1 (lt_succ_self _) · change succ (pred (n * p - x)) ≤ (succ (pred (p - x / n))) * n rw [succ_pred_eq_of_pos (Nat.sub_pos_of_lt h₁), fun h => succ_pred_eq_of_pos (Nat.sub_pos_of_lt h)] -- TODO: why is the function needed? · rw [Nat.mul_sub_right_distrib, Nat.mul_comm] apply Nat.sub_le_sub_left; apply div_mul_le_self · apply (div_lt_iff_lt_mul npos).2; rwa [Nat.mul_comm] protected lemma div_div_eq_div_mul (m n k : ℕ) : m / n / k = m / (n * k) := by cases eq_zero_or_pos k with | inl k0 => rw [k0, Nat.mul_zero, Nat.div_zero, Nat.div_zero] | inr kpos => ?_ cases eq_zero_or_pos n with | inl n0 => rw [n0, Nat.zero_mul, Nat.div_zero, Nat.zero_div] | inr npos => ?_ apply Nat.le_antisymm · apply (le_div_iff_mul_le (Nat.mul_pos npos kpos)).2 rw [Nat.mul_comm n k, ← Nat.mul_assoc] apply (le_div_iff_mul_le npos).1 apply (le_div_iff_mul_le kpos).1 (apply Nat.le_refl) · apply (le_div_iff_mul_le kpos).2 apply (le_div_iff_mul_le npos).2 rw [Nat.mul_assoc, Nat.mul_comm n k] apply (le_div_iff_mul_le (Nat.mul_pos kpos npos)).1 apply Nat.le_refl protected lemma mul_div_mul {m : ℕ} (n k : ℕ) (H : 0 < m) : m * n / (m * k) = n / k := by rw [← Nat.div_div_eq_div_mul, Nat.mul_div_cancel_left _ H] /- dvd -/ protected theorem dvd_mul_right (a b : ℕ) : a ∣ a * b := ⟨b, rfl⟩ protected theorem dvd_trans {a b c : ℕ} (h₁ : a ∣ b) (h₂ : b ∣ c) : a ∣ c := match h₁, h₂ with | ⟨d, (h₃ : b = a * d)⟩, ⟨e, (h₄ : c = b * e)⟩ => ⟨d * e, show c = a * (d * e) by simp[h₃,h₄, Nat.mul_assoc]⟩ protected theorem eq_zero_of_zero_dvd {a : ℕ} (h : 0 ∣ a) : a = 0 := Exists.elim h (λ c (H' : a = 0 * c) => Eq.trans H' (Nat.zero_mul c)) protected theorem dvd_add {a b c : ℕ} (h₁ : a ∣ b) (h₂ : a ∣ c) : a ∣ b + c := Exists.elim h₁ (λ d hd => Exists.elim h₂ (λ e he => ⟨d + e, by simp[Nat.left_distrib, hd, he]⟩)) protected theorem dvd_add_iff_right {k m n : ℕ} (h : k ∣ m) : k ∣ n ↔ k ∣ m + n := ⟨Nat.dvd_add h, Exists.elim h $ λd hd => match m, hd with | _, rfl => λh₂ => Exists.elim h₂ $ λe he => ⟨e - d, by rw [Nat.mul_sub_left_distrib, ←he, Nat.add_sub_cancel_left]⟩⟩ protected theorem dvd_add_iff_left {k m n : ℕ} (h : k ∣ n) : k ∣ m ↔ k ∣ m + n := by rw [Nat.add_comm] exact Nat.dvd_add_iff_right h theorem dvd_sub {k m n : ℕ} (H : n ≤ m) (h₁ : k ∣ m) (h₂ : k ∣ n) : k ∣ m - n := (Nat.dvd_add_iff_left h₂).mpr $ by rw [Nat.sub_add_cancel H] exact h₁ theorem dvd_mod_iff {k m n : ℕ} (h: k ∣ n) : k ∣ m % n ↔ k ∣ m := let t := @Nat.dvd_add_iff_left _ (m % n) _ (Nat.dvd_trans h (Nat.dvd_mul_right n (m / n))) by rwa [mod_add_div] at t theorem le_of_dvd {m n : ℕ} (h : 0 < n) : m ∣ n → m ≤ n := λ⟨k,e⟩ => by revert h rw [e] match k with | 0 => intro hn simp at hn | pk+1 => intro _ let t := Nat.mul_le_mul_left m (succ_pos pk) rwa [Nat.mul_one] at t theorem dvd_antisymm : ∀ {m n : ℕ}, m ∣ n → n ∣ m → m = n | m, 0, h₁, h₂ => Nat.eq_zero_of_zero_dvd h₂ | 0, n, h₁, h₂ => (Nat.eq_zero_of_zero_dvd h₁).symm | m+1, n+1, h₁, h₂ => Nat.le_antisymm (le_of_dvd (succ_pos _) h₁) (le_of_dvd (succ_pos _) h₂) theorem pos_of_dvd_of_pos {m n : ℕ} (H1 : m ∣ n) (H2 : 0 < n) : 0 < m := Nat.pos_of_ne_zero $ λ m0 => by rw [m0] at H1 rw [Nat.eq_zero_of_zero_dvd H1] at H2 exact Nat.lt_irrefl _ H2 theorem eq_one_of_dvd_one {n : ℕ} (H : n ∣ 1) : n = 1 := Nat.le_antisymm (le_of_dvd (of_decide_eq_true (by trivial)) H) (pos_of_dvd_of_pos H (of_decide_eq_true (by trivial))) theorem dvd_of_mod_eq_zero {m n : ℕ} (H : n % m = 0) : m ∣ n := Exists.intro (n / m) (by have t := (mod_add_div n m).symm rwa [H, Nat.zero_add] at t) theorem mod_eq_zero_of_dvd {m n : ℕ} (H : m ∣ n) : n % m = 0 := Exists.elim H (λ z H1 => by rw [H1, mul_mod_right]) theorem dvd_iff_mod_eq_zero (m n : ℕ) : m ∣ n ↔ n % m = 0 := Iff.intro mod_eq_zero_of_dvd dvd_of_mod_eq_zero instance decidable_dvd : @DecidableRel ℕ (·∣·) := λm n => decidable_of_decidable_of_iff inferInstance (dvd_iff_mod_eq_zero _ _).symm protected theorem mul_div_cancel' {m n : ℕ} (H : n ∣ m) : n * (m / n) = m := let t := mod_add_div m n by rwa [mod_eq_zero_of_dvd H, Nat.zero_add] at t protected theorem div_mul_cancel {m n : ℕ} (H: n ∣ m) : m / n * n = m := by rw [Nat.mul_comm, Nat.mul_div_cancel' H] protected theorem mul_div_assoc (m : ℕ) {n k : ℕ} (H : k ∣ n) : m * n / k = m * (n / k) := match Nat.eq_zero_or_pos k with | Or.inl h0 => by rw [h0, Nat.div_zero, Nat.div_zero, Nat.mul_zero] | Or.inr hpos => by have h1 : m * n / k = m *(n / k * k) / k := by rw [Nat.div_mul_cancel H] rw [h1, ← Nat.mul_assoc, Nat.mul_div_cancel _ hpos] protected theorem dvd_of_mul_dvd_mul_left {m n k : ℕ} (kpos : 0 < k) (H : k * m ∣ k * n) : m ∣ n := Exists.elim H (λ l H1 => by rw [Nat.mul_assoc] at H1 exact ⟨_, Nat.eq_of_mul_eq_mul_left kpos H1⟩) protected theorem dvd_of_mul_dvd_mul_right {m n k : ℕ} (kpos : 0 < k) (H : m * k ∣ n * k) : m ∣ n := by rw [Nat.mul_comm m k, Nat.mul_comm n k] at H; exact Nat.dvd_of_mul_dvd_mul_left kpos H /- --- -/ protected lemma mul_le_mul_of_nonneg_left {a b c : ℕ} (h₁ : a ≤ b) : c * a ≤ c * b := by by_cases hba: b ≤ a; { simp [Nat.le_antisymm hba h₁]; apply Nat.le_refl } by_cases hc0 : c ≤ 0; { simp [Nat.le_antisymm hc0 (zero_le c), Nat.zero_mul] } exact Nat.le_of_lt (Nat.mul_lt_mul_of_pos_left (not_le.1 hba) (not_le.1 hc0)) protected lemma mul_le_mul_of_nonneg_right {a b c : ℕ} (h₁ : a ≤ b) : a * c ≤ b * c := by by_cases hba : b ≤ a; { simp [Nat.le_antisymm hba h₁]; apply Nat.le_refl } by_cases hc0 : c ≤ 0; { simp [Nat.le_antisymm hc0 (zero_le c), Nat.mul_zero] } exact Nat.le_of_lt (Nat.mul_lt_mul_of_pos_right (not_le.1 hba) (not_le.1 hc0)) protected lemma mul_lt_mul (hac : a < c) (hbd : b ≤ d) (pos_b : 0 < b) : a * b < c * d := Nat.lt_of_lt_of_le (Nat.mul_lt_mul_of_pos_right hac pos_b) (Nat.mul_le_mul_of_nonneg_left hbd) protected lemma mul_lt_mul' (h1 : a ≤ c) (h2 : b < d) (h3 : 0 < c) : a * b < c * d := Nat.lt_of_le_of_lt (Nat.mul_le_mul_of_nonneg_right h1) (Nat.mul_lt_mul_of_pos_left h2 h3) lemma div_lt_self (h₁ : 0 < n) (h₂ : 1 < m) : n / m < n := by have m_pos : 0 < m := Nat.lt_trans Nat.zero_lt_one h₂ suffices 1 * n < m * n by rw [Nat.one_mul, Nat.mul_comm] at this exact (Nat.div_lt_iff_lt_mul m_pos).2 this exact Nat.mul_lt_mul h₂ (Nat.le_refl _) h₁ @[simp] theorem mod_mod (a n : ℕ) : (a % n) % n = a % n := (eq_zero_or_pos n).elim (λ n0 => by simp [n0, mod_zero]) (λ npos => Nat.mod_eq_of_lt (mod_lt _ npos)) lemma mul_mod (a b n : ℕ) : (a * b) % n = ((a % n) * (b % n)) % n := by let hy := (a * b) % n have hx : (a * b) % n = hy := rfl rw [←mod_add_div a n, ←mod_add_div b n, Nat.right_distrib, Nat.left_distrib, Nat.left_distrib, Nat.mul_assoc, Nat.mul_assoc, ←Nat.left_distrib n _ _, add_mul_mod_self_left, Nat.mul_comm _ (n * (b / n)), Nat.mul_assoc, add_mul_mod_self_left] at hx rw [hx] done @[simp] theorem mod_add_mod (m n k : ℕ) : (m % n + k) % n = (m + k) % n := by have := (add_mul_mod_self_left (m % n + k) n (m / n)).symm rwa [Nat.add_right_comm, mod_add_div] at this @[simp] theorem add_mod_mod (m n k : ℕ) : (m + n % k) % k = (m + n) % k := by rw [Nat.add_comm, mod_add_mod, Nat.add_comm] lemma add_mod (a b n : ℕ) : (a + b) % n = ((a % n) + (b % n)) % n := by rw [add_mod_mod, mod_add_mod] lemma to_digits_core_lens_eq_aux (b f : Nat) : ∀ (n : Nat) (l1 l2 : List Char) (h0 : l1.length = l2.length), (Nat.toDigitsCore b f n l1).length = (Nat.toDigitsCore b f n l2).length := by induction f with simp only [Nat.toDigitsCore, List.length] <;> intro n l1 l2 hlen | zero => assumption | succ f ih => byCases hx : n / b = 0 case inl => simp only [hx, if_true, List.length, congrArg (fun l => l + 1) hlen] case inr => simp only [hx, if_false] specialize ih (n / b) (Nat.digitChar (n % b) :: l1) (Nat.digitChar (n % b) :: l2) simp only [List.length, congrArg (fun l => l + 1) hlen] at ih exact ih trivial lemma to_digits_core_lens_eq (b f : Nat) : ∀ (n : Nat) (c : Char) (tl : List Char), (Nat.toDigitsCore b f n (c :: tl)).length = (Nat.toDigitsCore b f n tl).length + 1 := by induction f with intro n c tl <;> simp only [Nat.toDigitsCore, List.length] | succ f ih => byCases hnb : (n / b) = 0 case inl => simp only [hnb, if_true, List.length] case inr => generalize hx: Nat.digitChar (n % b) = x simp only [hx, hnb, if_false] at ih simp only [hnb, if_false] specialize ih (n / b) c (x :: tl) rw [<- ih] have lens_eq : (x :: (c :: tl)).length = (c :: x :: tl).length := by simp apply to_digits_core_lens_eq_aux exact lens_eq lemma nat_repr_len_aux (n b e : Nat) (h_b_pos : 0 < b) : n < b ^ e.succ -> n / b < b ^ e := by simp only [Nat.pow_succ] exact (@Nat.div_lt_iff_lt_mul b n (b ^ e) h_b_pos).mpr /-- The String representation produced by toDigitsCore has the proper length relative to the number of digits in `n < e` for some base `b`. Since this works with any base greater than one, it can be used for binary, decimal, and hex. -/ lemma to_digits_core_length (b : Nat) (h : 2 <= b) (f n e : Nat) (hlt : n < b ^ e) (h_e_pos: 0 < e) : (Nat.toDigitsCore b f n []).length <= e := by induction f generalizing n e hlt h_e_pos with simp only [Nat.toDigitsCore, List.length, Nat.zero_le] | succ f ih => cases e with | zero => exact False.elim (Nat.lt_irrefl 0 h_e_pos) | succ e => byCases h_pred_pos : 0 < e case inl => have _ : 0 < b := Nat.lt_trans (by decide) h specialize ih (n / b) e (nat_repr_len_aux n b e ‹0 < b› hlt) h_pred_pos byCases hdiv_ten : n / b = 0 case inl => simp only [hdiv_ten]; exact Nat.le.step h_pred_pos case inr => simp only [hdiv_ten, to_digits_core_lens_eq b f (n / b) (Nat.digitChar $ n % b), if_false] exact Nat.succ_le_succ ih case inr => have _ : e = 0 := Nat.eq_zero_of_nonpos e h_pred_pos rw [‹e = 0›] have _ : b ^ 1 = b := by simp only [pow_succ, pow_zero, Nat.one_mul] have _ : n < b := ‹b ^ 1 = b› ▸ (‹e = 0› ▸ hlt : n < b ^ Nat.succ 0) simp only [(@Nat.div_eq_of_lt n b ‹n < b› : n / b = 0), if_true, List.length] /-- The core implementation of `Nat.repr` returns a String with length less than or equal to the number of digits in the decimal number (represented by `e`). For example, the decimal string representation of any number less than 1000 (10 ^ 3) has a length less than or equal to 3. -/ lemma repr_length (n e : Nat) : 0 < e -> n < 10 ^ e -> (Nat.repr n).length <= e := by cases n with intro _ _ <;> simp only [Nat.repr, Nat.toDigits, String.length, List.asString] | zero => assumption | succ n => byCases hterm : n.succ / 10 = 0 case inl => simp only [hterm, Nat.toDigitsCore]; assumption case inr => simp only [hterm] exact to_digits_core_length 10 (by decide) (Nat.succ n + 1) (Nat.succ n) e ‹n.succ < 10 ^ e› ‹0 < e› end Nat
9551198bc87ed384e58aa528f9645a7bf28bcaf9
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/limits/lattice.lean
96a6dced81d5e10698d79cfc0cca7e953fa47e9d
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
4,775
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.limits.shapes.finite_limits import Mathlib.order.complete_lattice import Mathlib.PostPort universes u namespace Mathlib namespace category_theory.limits.complete_lattice protected instance has_finite_limits_of_semilattice_inf_top {α : Type u} [semilattice_inf_top α] : has_finite_limits α := fun (J : Type u) (𝒥₁ : small_category J) (𝒥₂ : fin_category J) => has_limits_of_shape.mk fun (F : J ⥤ α) => has_limit.mk (limit_cone.mk (cone.mk (finset.inf finset.univ (functor.obj F)) (nat_trans.mk fun (j : J) => hom_of_le (finset.inf_le (fintype.complete j)))) (is_limit.mk fun (s : cone F) => hom_of_le (finset.le_inf fun (j : J) (_x : j ∈ finset.univ) => plift.down (ulift.down (nat_trans.app (cone.π s) j))))) protected instance has_finite_colimits_of_semilattice_sup_bot {α : Type u} [semilattice_sup_bot α] : has_finite_colimits α := fun (J : Type u) (𝒥₁ : small_category J) (𝒥₂ : fin_category J) => has_colimits_of_shape.mk fun (F : J ⥤ α) => has_colimit.mk (colimit_cocone.mk (cocone.mk (finset.sup finset.univ (functor.obj F)) (nat_trans.mk fun (i : J) => hom_of_le (finset.le_sup (fintype.complete i)))) (is_colimit.mk fun (s : cocone F) => hom_of_le (finset.sup_le fun (j : J) (_x : j ∈ finset.univ) => plift.down (ulift.down (nat_trans.app (cocone.ι s) j))))) /-- The limit cone over any functor into a complete lattice. -/ def limit_cone {α : Type u} {J : Type u} [small_category J] [complete_lattice α] (F : J ⥤ α) : limit_cone F := limit_cone.mk (cone.mk (infi (functor.obj F)) (nat_trans.mk fun (j : J) => hom_of_le sorry)) (is_limit.mk fun (s : cone F) => hom_of_le sorry) /-- The colimit cocone over any functor into a complete lattice. -/ def colimit_cocone {α : Type u} {J : Type u} [small_category J] [complete_lattice α] (F : J ⥤ α) : colimit_cocone F := colimit_cocone.mk (cocone.mk (supr (functor.obj F)) (nat_trans.mk fun (j : J) => hom_of_le sorry)) (is_colimit.mk fun (s : cocone F) => hom_of_le sorry) -- It would be nice to only use the `Inf` half of the complete lattice, but -- this seems not to have been described separately. protected instance has_limits_of_complete_lattice {α : Type u} [complete_lattice α] : has_limits α := has_limits.mk fun (J : Type u) (𝒥 : small_category J) => has_limits_of_shape.mk fun (F : J ⥤ α) => has_limit.mk (limit_cone F) protected instance has_colimits_of_complete_lattice {α : Type u} [complete_lattice α] : has_colimits α := has_colimits.mk fun (J : Type u) (𝒥 : small_category J) => has_colimits_of_shape.mk fun (F : J ⥤ α) => has_colimit.mk (colimit_cocone F) /-- The limit of a functor into a complete lattice is the infimum of the objects in the image. -/ def limit_iso_infi {α : Type u} {J : Type u} [small_category J] [complete_lattice α] (F : J ⥤ α) : limit F ≅ infi (functor.obj F) := is_limit.cone_point_unique_up_to_iso (limit.is_limit F) (limit_cone.is_limit (limit_cone F)) @[simp] theorem limit_iso_infi_hom {α : Type u} {J : Type u} [small_category J] [complete_lattice α] (F : J ⥤ α) (j : J) : iso.hom (limit_iso_infi F) ≫ hom_of_le (infi_le (functor.obj F) j) = limit.π F j := of_as_true trivial @[simp] theorem limit_iso_infi_inv {α : Type u} {J : Type u} [small_category J] [complete_lattice α] (F : J ⥤ α) (j : J) : iso.inv (limit_iso_infi F) ≫ limit.π F j = hom_of_le (infi_le (functor.obj F) j) := rfl /-- The colimit of a functor into a complete lattice is the supremum of the objects in the image. -/ def colimit_iso_supr {α : Type u} {J : Type u} [small_category J] [complete_lattice α] (F : J ⥤ α) : colimit F ≅ supr (functor.obj F) := is_colimit.cocone_point_unique_up_to_iso (colimit.is_colimit F) (colimit_cocone.is_colimit (colimit_cocone F)) @[simp] theorem colimit_iso_supr_hom {α : Type u} {J : Type u} [small_category J] [complete_lattice α] (F : J ⥤ α) (j : J) : colimit.ι F j ≫ iso.hom (colimit_iso_supr F) = hom_of_le (le_supr (functor.obj F) j) := rfl @[simp] theorem colimit_iso_supr_inv {α : Type u} {J : Type u} [small_category J] [complete_lattice α] (F : J ⥤ α) (j : J) : hom_of_le (le_supr (functor.obj F) j) ≫ iso.inv (colimit_iso_supr F) = colimit.ι F j := of_as_true trivial
39be58b85c0775a1ee987f8eacc25029f26ded7b
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/stage0/src/Lean/Compiler/LCNF/Simp/SimpM.lean
57324d00bf7e8c26605d1f4f464fc435cc5e2f21
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach" ]
permissive
tobiasgrosser/lean4
ce0fd9cca0feba1100656679bf41f0bffdbabb71
ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f
refs/heads/master
1,673,103,412,948
1,664,930,501,000
1,664,930,501,000
186,870,185
0
0
Apache-2.0
1,665,129,237,000
1,557,939,901,000
Lean
UTF-8
Lean
false
false
11,830
lean
/- Copyright (c) 2022 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Compiler.ImplementedByAttr import Lean.Compiler.LCNF.Renaming import Lean.Compiler.LCNF.ElimDead import Lean.Compiler.LCNF.AlphaEqv import Lean.Compiler.LCNF.PrettyPrinter import Lean.Compiler.LCNF.Bind import Lean.Compiler.LCNF.Simp.JpCases import Lean.Compiler.LCNF.Simp.FunDeclInfo import Lean.Compiler.LCNF.Simp.Config namespace Lean.Compiler.LCNF namespace Simp structure Context where /-- Name of the declaration being simplified. We currently use this information because we are generating phase1 declarations on demand, and it may trigger non-termination when trying to access the phase1 declaration. -/ declName : Name config : Config := {} /-- A mapping from discriminant to constructor application it is equal to in the current context. -/ discrCtorMap : FVarIdMap Expr := {} /-- A mapping from constructor application to discriminant it is equal to in the current context. -/ ctorDiscrMap : PersistentExprMap FVarId := {} /-- Stack of global declarations being recursively inlined. -/ inlineStack : List Name := [] /-- Mapping from declaration names to number of occurrences at `inlineStack` -/ inlineStackOccs : PHashMap Name Nat := {} structure State where /-- Free variable substitution. We use it to implement inlining and removing redundant variables `let _x.i := _x.j` -/ subst : FVarSubst := {} /-- Track used local declarations to be able to eliminate dead variables. -/ used : UsedLocalDecls := {} /-- Mapping containing free variables ids that need to be renamed (i.e., the `binderName`). We use this map to preserve user provide names. -/ binderRenaming : Renaming := {} /-- Mapping used to decide whether a local function declaration must be inlined or not. -/ funDeclInfoMap : FunDeclInfoMap := {} /-- `true` if some simplification was performed in the current simplification pass. -/ simplified : Bool := false /-- Number of visited `let-declarations` and terminal values. This is a performance counter, and currently has no impact on code generation. -/ visited : Nat := 0 /-- Number of definitions inlined. This is a performance counter. -/ inline : Nat := 0 /-- Number of local functions inlined. This is a performance counter. -/ inlineLocal : Nat := 0 abbrev SimpM := ReaderT Context $ StateRefT State CompilerM instance : MonadFVarSubst SimpM false where getSubst := return (← get).subst instance : MonadFVarSubstState SimpM where modifySubst f := modify fun s => { s with subst := f s.subst } /-- Use `findExpr`, and if the result is a free variable, check whether it is in the map `discrCtorMap`. We use this method when simplifying projections and cases-constructor. -/ def findCtor (e : Expr) : SimpM Expr := do let e ← findExpr e let .fvar fvarId := e | return e let some ctor := (← read).discrCtorMap.find? fvarId | return e return ctor /-- If `type` is an inductive datatype, return its universe levels and parameters. -/ def getIndInfo? (type : Expr) : CoreM (Option (List Level × Array Expr)) := do let type := type.headBeta let .const declName us := type.getAppFn | return none let .inductInfo info ← getConstInfo declName | return none unless type.getAppNumArgs >= info.numParams do return none return some (us, type.getAppArgs[:info.numParams]) /-- Execute `x` with the information that `discr = ctorName ctorFields`. We use this information to simplify nested cases on the same discriminant. Remark: we do not perform the reverse direction at this phase. That is, we do not replace occurrences of `ctorName ctorFields` with `discr`. We wait more type information to be erased. -/ def withDiscrCtor (discr : FVarId) (ctorName : Name) (ctorFields : Array Param) (x : SimpM α) : SimpM α := do let ctorInfo ← getConstInfoCtor ctorName let fieldArgs := ctorFields.map (.fvar ·.fvarId) if let some (us, params) ← getIndInfo? (← getType discr) then let ctor := mkAppN (mkAppN (mkConst ctorName us) params) fieldArgs withReader (fun ctx => { ctx with discrCtorMap := ctx.discrCtorMap.insert discr ctor, ctorDiscrMap := ctx.ctorDiscrMap.insert ctor discr }) do x else -- For the discrCtor map, the constructor parameters are irrelevant for optimizations that use this information let ctor := mkAppN (mkAppN (mkConst ctorName) (mkArray ctorInfo.numParams erasedExpr)) fieldArgs withReader (fun ctx => { ctx with discrCtorMap := ctx.discrCtorMap.insert discr ctor }) do x /-- Set the `simplified` flag to `true`. -/ def markSimplified : SimpM Unit := modify fun s => { s with simplified := true } /-- Increment `visited` performance counter. -/ def incVisited : SimpM Unit := modify fun s => { s with visited := s.visited + 1 } /-- Increment `inline` performance counter. It is the number of inlined global declarations. -/ def incInline : SimpM Unit := modify fun s => { s with inline := s.inline + 1 } /-- Increment `inlineLocal` performance counter. It is the number of inlined local function and join point declarations. -/ def incInlineLocal : SimpM Unit := modify fun s => { s with inlineLocal := s.inlineLocal + 1 } /-- Mark the local function declaration or join point with the given id as a "must inline". -/ def addMustInline (fvarId : FVarId) : SimpM Unit := modify fun s => { s with funDeclInfoMap := s.funDeclInfoMap.addMustInline fvarId } /-- Add a new occurrence of local function `fvarId`. -/ def addFunOcc (fvarId : FVarId) : SimpM Unit := modify fun s => { s with funDeclInfoMap := s.funDeclInfoMap.add fvarId } /-- Add a new occurrence of local function `fvarId` in argument position . -/ def addFunHoOcc (fvarId : FVarId) : SimpM Unit := modify fun s => { s with funDeclInfoMap := s.funDeclInfoMap.addHo fvarId } @[inheritDoc FunDeclInfoMap.update] partial def updateFunDeclInfo (code : Code) (mustInline := false) : SimpM Unit := do let map ← modifyGet fun s => (s.funDeclInfoMap, { s with funDeclInfoMap := {} }) let map ← map.update code mustInline modify fun s => { s with funDeclInfoMap := map } /-- Execute `x` with an updated `inlineStack`. If `value` is of the form `const ...`, add `const` to the stack. Otherwise, do not change the `inlineStack`. -/ def withInlining (value : Expr) (recursive : Bool) (x : SimpM α) : SimpM α := do let f := value.getAppFn if let .const declName _ := f then trace[Compiler.simp.inline] "{declName}" let numOccs := (← read).inlineStackOccs.find? declName |>.getD 0 let numOccs := numOccs + 1 if recursive && hasInlineIfReduceAttribute (← getEnv) declName && numOccs > (← getConfig).maxRecInlineIfReduce then throwError "function `{declName}` has been recursively inlined more than #{(← getConfig).maxRecInlineIfReduce}, consider removing the attribute `[inlineIfReduce]` from this declaration or increasing the limit using `set_option compiler.maxRecInlineIfReduce <num>`" withReader (fun ctx => { ctx with inlineStack := declName :: ctx.inlineStack, inlineStackOccs := ctx.inlineStackOccs.insert declName numOccs }) x else x /-- Similar to the default `Lean.withIncRecDepth`, but include the `inlineStack` in the error messsage. -/ @[inline] def withIncRecDepth (x : SimpM α) : SimpM α := do let curr ← MonadRecDepth.getRecDepth let max ← MonadRecDepth.getMaxRecDepth if curr == max then throwMaxRecDepth else MonadRecDepth.withRecDepth (curr+1) x where throwMaxRecDepth : SimpM α := do match (← read).inlineStack with | [] => throwError maxRecDepthErrorMessage | declName :: stack => let mut fmt := f!"{declName}\n" let mut prev := declName let mut ellipsis := false for declName in stack do if prev == declName then unless ellipsis do ellipsis := true fmt := fmt ++ "...\n" else fmt := fmt ++ f!"{declName}\n" prev := declName ellipsis := false throwError "maximum recursion depth reached in the code generator\nfunction inline stack:\n{fmt}" /-- Execute `x` with `fvarId` set as `mustInline`. After execution the original setting is restored. -/ def withAddMustInline (fvarId : FVarId) (x : SimpM α) : SimpM α := do let saved? := (← get).funDeclInfoMap.map.find? fvarId try addMustInline fvarId x finally modify fun s => { s with funDeclInfoMap := s.funDeclInfoMap.restore fvarId saved? } /-- Return true if the given local function declaration or join point id is marked as `once` or `mustInline`. We use this information to decide whether to inline them. -/ def isOnceOrMustInline (fvarId : FVarId) : SimpM Bool := do match (← get).funDeclInfoMap.map.find? fvarId with | some .once | some .mustInline => return true | _ => return false /-- Return `true` if the given code is considered "small". -/ def isSmall (code : Code) : SimpM Bool := return code.sizeLe (← getConfig).smallThreshold /-- Return `true` if the given local function declaration should be inlined. -/ def shouldInlineLocal (decl : FunDecl) : SimpM Bool := do if (← isOnceOrMustInline decl.fvarId) then return true else isSmall decl.value /-- LCNF "Beta-reduce". The equivalent of `(fun params => code) args`. If `mustInline` is true, the local function declarations in the resulting code are marked as `.mustInline`. See comment at `updateFunDeclInfo`. -/ def betaReduce (params : Array Param) (code : Code) (args : Array Expr) (mustInline := false) : SimpM Code := do let mut subst := {} let mut castDecls := #[] for param in params, arg in args do /- If `param` hast type `⊤` but `arg` does not, we must insert a cast. Otherwise, the resulting code may be type incorrect. For example, the following code is type correct before inlining `f` because `x : ⊤`. ``` def foo (g : A → A) (a : B) := fun f (x : ⊤) := let _x.1 := g x ... let _x.2 := f a ... ``` We must introduce a cast around `a` to make sure the resulting expression is type correct. -/ if param.type.isAnyType && !(← inferType arg).isAnyType then let castArg ← mkLcCast arg anyTypeExpr let castDecl ← mkAuxLetDecl castArg castDecls := castDecls.push (CodeDecl.let castDecl) subst := subst.insert param.fvarId (.fvar castDecl.fvarId) else subst := subst.insert param.fvarId arg let code ← code.internalize subst let code := LCNF.attachCodeDecls castDecls code updateFunDeclInfo code mustInline return code /-- Erase the given let-declaration from the local context, and set the `simplified` flag to true. -/ def eraseLetDecl (decl : LetDecl) : SimpM Unit := do LCNF.eraseLetDecl decl markSimplified /-- Erase the given local function declaration from the local context, and set the `simplified` flag to true. -/ def eraseFunDecl (decl : FunDecl) : SimpM Unit := do LCNF.eraseFunDecl decl markSimplified /-- Similar to `LCNF.addFVarSubst`. That is, add the entry `fvarId ↦ fvarId'` to the free variable substitution. If `fvarId` has a non-internal binder name `n`, but `fvarId'` does not, this method also adds the entry `fvarId' ↦ n` to the `binderRenaming` map. The goal is to preserve user provided names. -/ def addFVarSubst (fvarId : FVarId) (fvarId' : FVarId) : SimpM Unit := do LCNF.addFVarSubst fvarId fvarId' let binderName ← getBinderName fvarId unless binderName.isInternal do let binderName' ← getBinderName fvarId' if binderName'.isInternal then modify fun s => { s with binderRenaming := s.binderRenaming.insert fvarId' binderName }
f11d6db98b7e2caf7ed5bf3109db592b18847cd0
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/ring/fin.lean
c465c67f7a78e13d4ccf4511b7e8bc080470bcd8
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
762
lean
/- Copyright (c) 2022 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import logic.equiv.fin import algebra.ring.equiv import algebra.group.prod /-! # Rings and `fin` This file collects some basic results involving rings and the `fin` type ## Main results * `ring_equiv.fin_two`: The product over `fin 2` of some rings is the cartesian product -/ /-- The product over `fin 2` of some rings is just the cartesian product of these rings. -/ @[simps] def ring_equiv.pi_fin_two (R : fin 2 → Type*) [Π i, semiring (R i)] : (Π (i : fin 2), R i) ≃+* R 0 × R 1 := { to_fun := pi_fin_two_equiv R, map_add' := λ a b, rfl, map_mul' := λ a b, rfl, .. pi_fin_two_equiv R }
c7bac4458e26938527297d08d0f660c241b7a33a
a46270e2f76a375564f3b3e9c1bf7b635edc1f2c
/5.8.2.lean
b6cd689b7804fb9a1013df81e7c500efe573e84b
[ "CC0-1.0" ]
permissive
wudcscheme/lean-exercise
88ea2506714eac343de2a294d1132ee8ee6d3a20
5b23b9be3d361fff5e981d5be3a0a1175504b9f6
refs/heads/master
1,678,958,930,293
1,583,197,205,000
1,583,197,205,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
171
lean
example (p q r : Prop) (hp : p) : (p ∨ q ∨ r) ∧ (q ∨ p ∨ r) ∧ (q ∨ r ∨ p) := by split; try{split}; repeat {{left, assumption} <|> right <|> assumption}
180d53eaf1b46247fac8c5c7765a0c73ec95d98e
4f065978c49388d188224610d9984673079f7d91
/localization_alt.lean
c448bcf7cdcb802626726deb0a88310c87c3b1b0
[]
no_license
kckennylau/Lean
b323103f52706304907adcfaee6f5cb8095d4a33
907d0a4d2bd8f23785abd6142ad53d308c54fdcb
refs/heads/master
1,624,623,720,653
1,563,901,820,000
1,563,901,820,000
109,506,702
3
1
null
null
null
null
UTF-8
Lean
false
false
9,294
lean
import algebra.ring group_theory.submonoid ring_theory.ideal_operations linear_algebra.basic namespace localization_alt universes u v w variables {A : Type u} {B : Type v} {C : Type w} variables [comm_ring A] [comm_ring B] [comm_ring C] variables (S : set A) [is_submonoid S] (f : A → B) [is_ring_hom f] /- This is essentially the same logic as units.ext, but in more convenient form. -/ lemma comm_monoid.inv_unique {M : Type*} [comm_monoid M] {a ai₁ ai₂ : M} (e₁ : a * ai₁ = 1) (e₂ : a * ai₂ = 1) : ai₁ = ai₂ := by rw [← mul_one ai₁, ← e₂, ← mul_assoc, mul_comm ai₁, e₁, one_mul] /-- preserve definitional equality -/ def ideal.mk (I : set A) (J : ideal A) (H : I = J) : ideal A := { carrier := I, zero := H.symm ▸ J.zero, add := H.symm ▸ J.add, smul := H.symm ▸ J.smul } def ker : ideal A := ideal.mk {x | f x = 0} (ideal.comap f ⊥) $ set.ext $ λ x, submodule.mem_bot.symm def inverts_data (S : set A) (f : A → B) : Type* := Π s : S, {si : B // (f s) * si = 1} def inverts (S : set A) (f : A → B) : Prop := ∀ s : S, ∃ si : B, (f s) * si = 1 lemma inverts_subsingleton (S : set A) (f : A → B) : subsingleton (inverts_data S f) := ⟨λ fi1 fi2, funext $ λ s, subtype.eq $ comm_monoid.inv_unique (fi1 s).2 (fi2 s).2⟩ def inverts_of_data (h : inverts_data S f) : inverts S f := λ s, ⟨(h s).1,(h s).2⟩ noncomputable def inverts_some (h : inverts S f) : inverts_data S f := λ s, classical.indefinite_description _ (h s) def has_denom_data (S : set A) (f : A → B) := Π b : B, {sa : S × A // (f sa.1) * b = f sa.2 } def has_denom (S : set A) (f : A → B) : Prop := ∀ b : B, ∃ (sa : S × A), (f sa.1) * b = (f sa.2) def has_denom_of_data (h : has_denom_data S f) : has_denom S f := λ b, subtype.exists_of_subtype (h b) noncomputable def has_denom_some (h : has_denom S f) : has_denom_data S f := λ b, classical.indefinite_description _ (h b) def ann_aux (S : set A) [is_submonoid S] : Type* := { as : A × S // as.1 * as.2 = 0 } namespace ann_aux def zero : ann_aux S := ⟨(0, 1), mul_one _⟩ def add (as bt : ann_aux S) : ann_aux S := ⟨(as.1.1 + bt.1.1, as.1.2 * bt.1.2), show (as.1.1 + bt.1.1) * (as.1.2 * bt.1.2) = 0, by rw [add_mul, ← mul_assoc, as.2, zero_mul, zero_add, mul_left_comm, bt.2, mul_zero]⟩ def smul (a : A) (bt : ann_aux S) : ann_aux S := ⟨(a * bt.1.1, bt.1.2), show (a * bt.1.1) * bt.1.2 = 0, by rw [mul_assoc, bt.2, mul_zero]⟩ end ann_aux def submonoid_ann (S : set A) [is_submonoid S] : ideal A := { carrier := set.range (λ as : ann_aux S, as.1.1), zero := ⟨ann_aux.zero S, rfl⟩, add := λ _ _ ⟨as,has⟩ ⟨bt,hbt⟩, ⟨ann_aux.add S as bt, has ▸ hbt ▸ rfl⟩, smul := λ a _ ⟨bt,h⟩, ⟨ann_aux.smul S a bt, h ▸ rfl⟩ } lemma inverts_ker (hf : inverts S f) : submonoid_ann S ≤ ker f := λ x ⟨⟨⟨a,s⟩,asz⟩,rfl⟩, let ⟨si,e1⟩ := hf s in show f x = 0, by rw [← mul_one (f x), ← e1, ← mul_assoc, ← is_ring_hom.map_mul f, asz, is_ring_hom.map_zero f, zero_mul] structure is_localization_data := (inverts : inverts_data S f) (has_denom : has_denom_data S f) (ker_le : ker f ≤ submonoid_ann S) def is_localization : Prop := (inverts S f) ∧ (has_denom S f) ∧ (ker f = submonoid_ann S) lemma localization_epi (hf : is_localization S f) (g₁ g₂ : B → C) [is_ring_hom g₁] [is_ring_hom g₂] (e : g₁ ∘ f = g₂ ∘ f) : g₁ = g₂ := begin have e' : ∀ x, g₁ (f x) = g₂ (f x) := λ x, by convert congr_fun e x, ext b, rcases hf.2.1 b with ⟨⟨s,a⟩,e1⟩, rcases hf.1 s with ⟨si,e2⟩, have e4 : g₁ (f s) * (g₁ si) = 1, { rw [← is_ring_hom.map_mul g₁, e2, is_ring_hom.map_one g₁] }, have e5 : g₁ (f s) * (g₂ si) = 1, { rw [e', ← is_ring_hom.map_mul g₂, e2, is_ring_hom.map_one g₂] }, rw [← mul_one b, ← e2, mul_left_comm, ← mul_assoc, e1], rw [is_ring_hom.map_mul g₁, is_ring_hom.map_mul g₂, e', comm_monoid.inv_unique e4 e5] end section localization_initial variables (hf : is_localization_data S f) (g : A → C) [is_ring_hom g] (hg : inverts_data S g) def is_localization_initial (hf : is_localization_data S f) (g : A → C) [is_ring_hom g] (hg : inverts_data S g) : B → C := λ b, g (hf.has_denom b).1.2 * hg (hf.has_denom b).1.1 lemma useful (hf : is_localization_data S f) (g : A → C) [is_ring_hom g] (hg : inverts_data S g) {a₁ a₂ : A} (H : f a₁ = f a₂) : g a₁ = g a₂ := begin rw [← sub_eq_zero, ← is_ring_hom.map_sub f] at H, rw [← sub_eq_zero, ← is_ring_hom.map_sub g], generalize_hyp : a₁ - a₂ = a at H ⊢, rcases hf.3 H with ⟨⟨as,h1⟩,h2⟩, rw [← h2], cases hg as.2 with c h3, rw [← mul_one (g as.1), ← h3, ← mul_assoc, ← is_ring_hom.map_mul g, h1, is_ring_hom.map_zero g, zero_mul] end lemma useful2 {x y z w : A} (h : x * y = 1) : z * x = w * x ↔ z = w := ⟨λ H, have _ := congr_arg (* y) H, by rwa [mul_assoc, h, mul_one, mul_assoc, h, mul_one] at this, λ H, by rw H⟩ instance (hf : is_localization_data S f) (g : A → C) [is_ring_hom g] (hg : inverts_data S g) : is_ring_hom (is_localization_initial S f hf g hg) := { map_one := begin unfold is_localization_initial, rcases hf.has_denom 1 with ⟨⟨s,a⟩,h⟩, dsimp only at *, rw mul_one at h, replace h := useful S f hf g hg h, cases hg s with c hc, rw ← h, exact hc end, map_mul := λ x y, begin unfold is_localization_initial, rcases hf.has_denom x with ⟨⟨sx,ax⟩,h1⟩, rcases hf.has_denom y with ⟨⟨sy,ay⟩,h2⟩, rcases hf.has_denom (x*y) with ⟨⟨sxy,axy⟩,h3⟩, cases hg sx with sigx h4, cases hg sy with sigy h5, cases hg sxy with sigxy h6, cases hf.inverts sx with sifx h7, cases hf.inverts sy with sify h8, cases hf.inverts sxy with sifxy h9, dsimp only [subtype.coe_mk] at *, replace h1 : _ * _ = _ * _ := by convert congr_arg (* sifx) h1, rw [mul_right_comm, h7, one_mul] at h1, replace h2 : _ * _ = _ * _ := by convert congr_arg (* sify) h2, rw [mul_right_comm, h8, one_mul] at h2, rw [h1, h2] at h3, rw [← useful2 h4, ← useful2 h5, ← useful2 h6], have : g axy * sigxy * g ↑sx * g ↑sy * g ↑sxy = g axy * g ↑sx * g ↑sy * (g ↑sxy * sigxy), { simp only [mul_assoc, mul_comm, mul_left_comm] }, rw [this, h6, mul_one], clear this, have : g ax * sigx * (g ay * sigy) * g ↑sx * g ↑sy * g ↑sxy = g ax * g ay * g ↑sxy * (g ↑sx * sigx) * (g ↑sy * sigy), { simp only [mul_assoc, mul_comm, mul_left_comm] }, rw [this, h4, h5, mul_one, mul_one], clear this, iterate 4 { rw ← is_ring_hom.map_mul g }, apply useful S f hf g hg, iterate 4 { rw is_ring_hom.map_mul f }, rw ← h3, have : f ↑sxy * (f ax * sifx * (f ay * sify)) * f ↑sx * f ↑sy = f ax * f ay * f ↑sxy * (f ↑sx * sifx) * (f ↑sy * sify), { simp only [mul_assoc, mul_comm, mul_left_comm] }, rw [this, h7, h8, mul_one, mul_one] end, map_add := λ x y, begin unfold is_localization_initial, rcases hf.has_denom x with ⟨⟨sx,ax⟩,h1⟩, rcases hf.has_denom y with ⟨⟨sy,ay⟩,h2⟩, rcases hf.has_denom (x+y) with ⟨⟨sxy,axy⟩,h3⟩, cases hg sx with sigx h4, cases hg sy with sigy h5, cases hg sxy with sigxy h6, cases hf.inverts sx with sifx h7, cases hf.inverts sy with sify h8, cases hf.inverts sxy with sifxy h9, dsimp only [subtype.coe_mk] at *, replace h1 : _ * _ = _ * _ := by convert congr_arg (* sifx) h1, rw [mul_right_comm, h7, one_mul] at h1, replace h2 : _ * _ = _ * _ := by convert congr_arg (* sify) h2, rw [mul_right_comm, h8, one_mul] at h2, rw [h1, h2] at h3, rw [← useful2 h4, ← useful2 h5, ← useful2 h6], have : g axy * sigxy * g ↑sx * g ↑sy * g ↑sxy = g axy * g ↑sx * g ↑sy * (g ↑sxy * sigxy), { simp only [mul_assoc, mul_comm, mul_left_comm] }, rw [this, h6, mul_one], clear this, have : (g ax * sigx + g ay * sigy) * g ↑sx * g ↑sy * g ↑sxy = g ax * g ↑sy * g ↑sxy * (g ↑sx * sigx) + g ay * g ↑sx * g ↑sxy * (g ↑sy * sigy), { simp only [add_mul, mul_add, mul_assoc, mul_comm, mul_left_comm] }, rw [this, h4, h5, mul_one, mul_one], clear this, iterate 6 { rw ← is_ring_hom.map_mul g }, rw ← is_ring_hom.map_add g, apply useful S f hf g hg, rw is_ring_hom.map_add f, iterate 6 { rw is_ring_hom.map_mul f }, rw ← h3, have : f ↑sxy * (f ax * sifx + f ay * sify) * f ↑sx * f ↑sy = f ax * f ↑sy * f ↑sxy * (f ↑sx * sifx) + f ay * f ↑sx * f ↑sxy * (f ↑sy * sify), { simp only [add_mul, mul_add, mul_assoc, mul_comm, mul_left_comm] }, rw [this, h7, h8, mul_one, mul_one] end } lemma is_localization_initial_comp (hf : is_localization_data S f) (g : A → C) [is_ring_hom g] (hg : inverts_data S g) (a : A) : is_localization_initial S f hf g hg (f a) = g a := begin unfold is_localization_initial, rcases hf.has_denom (f a) with ⟨⟨s,x⟩,h1⟩, cases hg s with si h2, dsimp only [subtype.coe_mk] at *, rw [← useful2 h2, mul_right_comm, mul_assoc, h2, mul_one, ← is_ring_hom.map_mul g], apply useful S f hf g hg, rw [← h1, is_ring_hom.map_mul f, mul_comm] end end localization_initial end localization_alt
f4974b06fd5011ace7532d36f274e8821ee25d25
3dd1b66af77106badae6edb1c4dea91a146ead30
/tests/lean/run/tactic17.lean
a34a03069c783de7573ead20321d4803e48b99f1
[ "Apache-2.0" ]
permissive
silky/lean
79c20c15c93feef47bb659a2cc139b26f3614642
df8b88dca2f8da1a422cb618cd476ef5be730546
refs/heads/master
1,610,737,587,697
1,406,574,534,000
1,406,574,534,000
22,362,176
1
0
null
null
null
null
UTF-8
Lean
false
false
228
lean
import standard using tactic variable A : Type.{1} variable f : A → A → A theorem tst {a b c : A} (H1 : a = b) (H2 : b = c) : f a b = f b c := by apply (@congr A A (f a) (f b)); apply (congr2 f); !assumption
864c8dbda39554fac02e2892c89e3cc0fe6cc7f2
e030b0259b777fedcdf73dd966f3f1556d392178
/library/init/algebra/functions.lean
9c4fb5f616e768ed81d41e2e74b4387e1be3adad
[ "Apache-2.0" ]
permissive
fgdorais/lean
17b46a095b70b21fa0790ce74876658dc5faca06
c3b7c54d7cca7aaa25328f0a5660b6b75fe26055
refs/heads/master
1,611,523,590,686
1,484,412,902,000
1,484,412,902,000
38,489,734
0
0
null
1,435,923,380,000
1,435,923,379,000
null
UTF-8
Lean
false
false
17,261
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ prelude import init.algebra.ordered_field universe variables u definition min {α : Type u} [decidable_linear_order α] (a b : α) : α := if a ≤ b then a else b definition max {α : Type u} [decidable_linear_order α] (a b : α) : α := if a ≤ b then b else a definition abs {α : Type u} [decidable_linear_ordered_comm_group α] (a : α) : α := max a (-a) section open decidable tactic variables {α : Type u} [decidable_linear_order α] private meta def min_tac_step : tactic unit := solve1 $ intros >> `[unfold min max] >> try `[simp_using_hs [if_pos, if_neg]] >> try `[apply le_refl] >> try `[apply le_of_not_le, assumption] meta def tactic.interactive.min_tac (a b : interactive.types.qexpr) : tactic unit := `[apply @by_cases (%%a ≤ %%b), repeat {min_tac_step}] lemma min_le_left (a b : α) : min a b ≤ a := by min_tac a b lemma min_le_right (a b : α) : min a b ≤ b := by min_tac a b lemma le_min {a b c : α} (h₁ : c ≤ a) (h₂ : c ≤ b) : c ≤ min a b := by min_tac a b lemma le_max_left (a b : α) : a ≤ max a b := by min_tac a b lemma le_max_right (a b : α) : b ≤ max a b := by min_tac a b lemma max_le {a b c : α} (h₁ : a ≤ c) (h₂ : b ≤ c) : max a b ≤ c := by min_tac a b lemma eq_min {a b c : α} (h₁ : c ≤ a) (h₂ : c ≤ b) (h₃ : ∀{d}, d ≤ a → d ≤ b → d ≤ c) : c = min a b := le_antisymm (le_min h₁ h₂) (h₃ (min_le_left a b) (min_le_right a b)) lemma min_comm (a b : α) : min a b = min b a := eq_min (min_le_right a b) (min_le_left a b) (λ c h₁ h₂, le_min h₂ h₁) lemma min_assoc (a b c : α) : min (min a b) c = min a (min b c) := begin apply eq_min, { apply le_trans, apply min_le_left, apply min_le_left }, { apply le_min, apply le_trans, apply min_le_left, apply min_le_right, apply min_le_right }, { intros d h₁ h₂, apply le_min, apply le_min h₁, apply le_trans h₂, apply min_le_left, apply le_trans h₂, apply min_le_right } end lemma min_left_comm : ∀ (a b c : α), min a (min b c) = min b (min a c) := left_comm (@min α _) (@min_comm α _) (@min_assoc α _) lemma min_self (a : α) : min a a = a := by min_tac a a lemma min_eq_left {a b : α} (h : a ≤ b) : min a b = a := begin apply eq.symm, apply eq_min (le_refl _) h, intros, assumption end lemma min_eq_right {a b : α} (h : b ≤ a) : min a b = b := eq.subst (min_comm b a) (min_eq_left h) lemma eq_max {a b c : α} (h₁ : a ≤ c) (h₂ : b ≤ c) (h₃ : ∀{d}, a ≤ d → b ≤ d → c ≤ d) : c = max a b := le_antisymm (h₃ (le_max_left a b) (le_max_right a b)) (max_le h₁ h₂) lemma max_comm (a b : α) : max a b = max b a := eq_max (le_max_right a b) (le_max_left a b) (λ c h₁ h₂, max_le h₂ h₁) lemma max_assoc (a b c : α) : max (max a b) c = max a (max b c) := begin apply eq_max, { apply le_trans, apply le_max_left a b, apply le_max_left }, { apply max_le, apply le_trans, apply le_max_right a b, apply le_max_left, apply le_max_right }, { intros d h₁ h₂, apply max_le, apply max_le h₁, apply le_trans (le_max_left _ _) h₂, apply le_trans (le_max_right _ _) h₂} end lemma max_left_comm : ∀ (a b c : α), max a (max b c) = max b (max a c) := left_comm (@max α _) (@max_comm α _) (@max_assoc α _) lemma max_self (a : α) : max a a = a := by min_tac a a lemma max_eq_left {a b : α} (h : b ≤ a) : max a b = a := begin apply eq.symm, apply eq_max (le_refl _) h, intros, assumption end lemma max_eq_right {a b : α} (h : a ≤ b) : max a b = b := eq.subst (max_comm b a) (max_eq_left h) /- these rely on lt_of_lt -/ lemma min_eq_left_of_lt {a b : α} (h : a < b) : min a b = a := min_eq_left (le_of_lt h) lemma min_eq_right_of_lt {a b : α} (h : b < a) : min a b = b := min_eq_right (le_of_lt h) lemma max_eq_left_of_lt {a b : α} (h : b < a) : max a b = a := max_eq_left (le_of_lt h) lemma max_eq_right_of_lt {a b : α} (h : a < b) : max a b = b := max_eq_right (le_of_lt h) /- these use the fact that it is a linear ordering -/ lemma lt_min {a b c : α} (h₁ : a < b) (h₂ : a < c) : a < min b c := or.elim (le_or_gt b c) (assume h : b ≤ c, by min_tac b c) (assume h : b > c, by min_tac b c) lemma max_lt {a b c : α} (h₁ : a < c) (h₂ : b < c) : max a b < c := or.elim (le_or_gt a b) (assume h : a ≤ b, by min_tac a b) (assume h : a > b, by min_tac a b) end section variables {α : Type u} [decidable_linear_ordered_cancel_comm_monoid α] lemma min_add_add_left (a b c : α) : min (a + b) (a + c) = a + min b c := eq.symm (eq_min (show a + min b c ≤ a + b, from add_le_add_left (min_le_left _ _) _) (show a + min b c ≤ a + c, from add_le_add_left (min_le_right _ _) _) (take d, suppose d ≤ a + b, suppose d ≤ a + c, decidable.by_cases (suppose b ≤ c, by rwa [min_eq_left this]) (suppose ¬ b ≤ c, by rwa [min_eq_right (le_of_lt (lt_of_not_ge this))]))) lemma min_add_add_right (a b c : α) : min (a + c) (b + c) = min a b + c := begin rw [add_comm a c, add_comm b c, add_comm _ c], apply min_add_add_left end lemma max_add_add_left (a b c : α) : max (a + b) (a + c) = a + max b c := eq.symm (eq_max (add_le_add_left (le_max_left _ _) _) (add_le_add_left (le_max_right _ _) _) (take d, suppose a + b ≤ d, suppose a + c ≤ d, decidable.by_cases (suppose b ≤ c, by rwa [max_eq_right this]) (suppose ¬ b ≤ c, by rwa [max_eq_left (le_of_lt (lt_of_not_ge this))]))) lemma max_add_add_right (a b c : α) : max (a + c) (b + c) = max a b + c := begin rw [add_comm a c, add_comm b c, add_comm _ c], apply max_add_add_left end end section variables {α : Type u} [decidable_linear_ordered_comm_group α] lemma max_neg_neg (a b : α) : max (-a) (-b) = - min a b := eq.symm (eq_max (show -a ≤ -(min a b), from neg_le_neg $ min_le_left a b) (show -b ≤ -(min a b), from neg_le_neg $ min_le_right a b) (take d, assume H₁ : -a ≤ d, assume H₂ : -b ≤ d, have H : -d ≤ min a b, from le_min (neg_le_of_neg_le H₁) (neg_le_of_neg_le H₂), show -(min a b) ≤ d, from neg_le_of_neg_le H)) lemma min_eq_neg_max_neg_neg (a b : α) : min a b = - max (-a) (-b) := by rw [max_neg_neg, neg_neg] lemma min_neg_neg (a b : α) : min (-a) (-b) = - max a b := by rw [min_eq_neg_max_neg_neg, neg_neg, neg_neg] lemma max_eq_neg_min_neg_neg (a b : α) : max a b = - min (-a) (-b) := by rw [min_neg_neg, neg_neg] end section decidable_linear_ordered_comm_group variables {α : Type u} [decidable_linear_ordered_comm_group α] lemma abs_of_nonneg {a : α} (h : a ≥ 0) : abs a = a := have h' : -a ≤ a, from le_trans (neg_nonpos_of_nonneg h) h, max_eq_left h' lemma abs_of_pos {a : α} (h : a > 0) : abs a = a := abs_of_nonneg (le_of_lt h) lemma abs_of_nonpos {a : α} (h : a ≤ 0) : abs a = -a := have h' : a ≤ -a, from le_trans h (neg_nonneg_of_nonpos h), max_eq_right h' lemma abs_of_neg {a : α} (h : a < 0) : abs a = -a := abs_of_nonpos (le_of_lt h) lemma abs_zero : abs 0 = (0:α) := abs_of_nonneg (le_refl _) lemma abs_neg (a : α) : abs (-a) = abs a := begin unfold abs, rw [max_comm, neg_neg] end lemma abs_pos_of_pos {a : α} (h : a > 0) : abs a > 0 := by rwa (abs_of_pos h) lemma abs_pos_of_neg {a : α} (h : a < 0) : abs a > 0 := abs_neg a ▸ abs_pos_of_pos (neg_pos_of_neg h) lemma abs_sub (a b : α) : abs (a - b) = abs (b - a) := by rw [-neg_sub, abs_neg] lemma ne_zero_of_abs_ne_zero {a : α} (h : abs a ≠ 0) : a ≠ 0 := assume ha, h (eq.symm ha ▸ abs_zero) /- these assume a linear order -/ lemma eq_zero_of_neg_eq {a : α} (h : -a = a) : a = 0 := match lt_trichotomy a 0 with | or.inl h₁ := have a > 0, from h ▸ neg_pos_of_neg h₁, absurd h₁ (lt_asymm this) | or.inr (or.inl h₁) := h₁ | or.inr (or.inr h₁) := have a < 0, from h ▸ neg_neg_of_pos h₁, absurd h₁ (lt_asymm this) end lemma abs_nonneg (a : α) : abs a ≥ 0 := or.elim (le_total 0 a) (assume h : 0 ≤ a, by rwa (abs_of_nonneg h)) (assume h : a ≤ 0, calc 0 ≤ -a : neg_nonneg_of_nonpos h ... = abs a : eq.symm (abs_of_nonpos h)) lemma abs_abs (a : α) : abs (abs a) = abs a := abs_of_nonneg $ abs_nonneg a lemma le_abs_self (a : α) : a ≤ abs a := or.elim (le_total 0 a) (assume h : 0 ≤ a, begin rw [abs_of_nonneg h], apply le_refl end) (assume h : a ≤ 0, le_trans h $ abs_nonneg a) lemma neg_le_abs_self (a : α) : -a ≤ abs a := abs_neg a ▸ le_abs_self (-a) lemma eq_zero_of_abs_eq_zero {a : α} (h : abs a = 0) : a = 0 := have h₁ : a ≤ 0, from h ▸ le_abs_self a, have h₂ : -a ≤ 0, from h ▸ abs_neg a ▸ le_abs_self (-a), le_antisymm h₁ (nonneg_of_neg_nonpos h₂) lemma eq_of_abs_sub_eq_zero {a b : α} (h : abs (a - b) = 0) : a = b := have a - b = 0, from eq_zero_of_abs_eq_zero h, show a = b, from eq_of_sub_eq_zero this lemma abs_pos_of_ne_zero {a : α} (h : a ≠ 0) : abs a > 0 := or.elim (lt_or_gt_of_ne h) abs_pos_of_neg abs_pos_of_pos lemma abs_by_cases (P : α → Prop) {a : α} (h1 : P a) (h2 : P (-a)) : P (abs a) := or.elim (le_total 0 a) (assume h : 0 ≤ a, eq.symm (abs_of_nonneg h) ▸ h1) (assume h : a ≤ 0, eq.symm (abs_of_nonpos h) ▸ h2) lemma abs_le_of_le_of_neg_le {a b : α} (h1 : a ≤ b) (h2 : -a ≤ b) : abs a ≤ b := abs_by_cases (λ x : α, x ≤ b) h1 h2 lemma abs_lt_of_lt_of_neg_lt {a b : α} (h1 : a < b) (h2 : -a < b) : abs a < b := abs_by_cases (λ x : α, x < b) h1 h2 private lemma aux1 {a b : α} (h1 : a + b ≥ 0) (h2 : a ≥ 0) : abs (a + b) ≤ abs a + abs b := decidable.by_cases (assume h3 : b ≥ 0, calc abs (a + b) ≤ abs (a + b) : by apply le_refl ... = a + b : by rw (abs_of_nonneg h1) ... = abs a + b : by rw (abs_of_nonneg h2) ... = abs a + abs b : by rw (abs_of_nonneg h3)) (assume h3 : ¬ b ≥ 0, have h4 : b ≤ 0, from le_of_lt (lt_of_not_ge h3), calc abs (a + b) = a + b : by rw (abs_of_nonneg h1) ... = abs a + b : by rw (abs_of_nonneg h2) ... ≤ abs a + 0 : add_le_add_left h4 _ ... ≤ abs a + -b : add_le_add_left (neg_nonneg_of_nonpos h4) _ ... = abs a + abs b : by rw (abs_of_nonpos h4)) private lemma aux2 {a b : α} (h1 : a + b ≥ 0) : abs (a + b) ≤ abs a + abs b := or.elim (le_total b 0) (assume h2 : b ≤ 0, have h3 : ¬ a < 0, from assume h4 : a < 0, have h5 : a + b < 0, begin note aux := add_lt_add_of_lt_of_le h4 h2, rwa [add_zero] at aux end, not_lt_of_ge h1 h5, aux1 h1 (le_of_not_gt h3)) (assume h2 : 0 ≤ b, begin assert h3 : abs (b + a) ≤ abs b + abs a, begin rw add_comm at h1, exact aux1 h1 h2 end, rw [add_comm, add_comm (abs a)], exact h3 end) lemma abs_add_le_abs_add_abs (a b : α) : abs (a + b) ≤ abs a + abs b := or.elim (le_total 0 (a + b)) (assume h2 : 0 ≤ a + b, aux2 h2) (assume h2 : a + b ≤ 0, have h3 : -a + -b = -(a + b), by rw neg_add, have h4 : -(a + b) ≥ 0, from neg_nonneg_of_nonpos h2, have h5 : -a + -b ≥ 0, begin rw -h3 at h4, exact h4 end, calc abs (a + b) = abs (-a + -b) : by rw [-abs_neg, neg_add] ... ≤ abs (-a) + abs (-b) : aux2 h5 ... = abs a + abs b : by rw [abs_neg, abs_neg]) lemma abs_sub_abs_le_abs_sub (a b : α) : abs a - abs b ≤ abs (a - b) := have h1 : abs a - abs b + abs b ≤ abs (a - b) + abs b, from calc abs a - abs b + abs b = abs a : by rw sub_add_cancel ... = abs (a - b + b) : by rw sub_add_cancel ... ≤ abs (a - b) + abs b : by apply abs_add_le_abs_add_abs, le_of_add_le_add_right h1 lemma abs_sub_le (a b c : α) : abs (a - c) ≤ abs (a - b) + abs (b - c) := calc abs (a - c) = abs (a - b + (b - c)) : by rw [sub_eq_add_neg, sub_eq_add_neg, sub_eq_add_neg, add_assoc, neg_add_cancel_left] ... ≤ abs (a - b) + abs (b - c) : by apply abs_add_le_abs_add_abs lemma abs_add_three (a b c : α) : abs (a + b + c) ≤ abs a + abs b + abs c := begin apply le_trans, apply abs_add_le_abs_add_abs, apply le_trans, apply add_le_add_right, apply abs_add_le_abs_add_abs, apply le_refl end lemma dist_bdd_within_interval {a b lb ub : α} (h : lb < ub) (hal : lb ≤ a) (hau : a ≤ ub) (hbl : lb ≤ b) (hbu : b ≤ ub) : abs (a - b) ≤ ub - lb := begin cases (decidable.em (b ≤ a)) with hba hba, rw (abs_of_nonneg (sub_nonneg_of_le hba)), apply sub_le_sub, apply hau, apply hbl, rw [abs_of_neg (sub_neg_of_lt (lt_of_not_ge hba)), neg_sub], apply sub_le_sub, apply hbu, apply hal end end decidable_linear_ordered_comm_group section decidable_linear_ordered_comm_ring variables {α : Type u} [decidable_linear_ordered_comm_ring α] lemma abs_mul (a b : α) : abs (a * b) = abs a * abs b := or.elim (le_total 0 a) (assume h1 : 0 ≤ a, or.elim (le_total 0 b) (assume h2 : 0 ≤ b, calc abs (a * b) = a * b : abs_of_nonneg (mul_nonneg h1 h2) ... = abs a * b : by rw (abs_of_nonneg h1) ... = abs a * abs b : by rw (abs_of_nonneg h2)) (assume h2 : b ≤ 0, calc abs (a * b) = -(a * b) : abs_of_nonpos (mul_nonpos_of_nonneg_of_nonpos h1 h2) ... = a * -b : by rw neg_mul_eq_mul_neg ... = abs a * -b : by rw (abs_of_nonneg h1) ... = abs a * abs b : by rw (abs_of_nonpos h2))) (assume h1 : a ≤ 0, or.elim (le_total 0 b) (assume h2 : 0 ≤ b, calc abs (a * b) = -(a * b) : abs_of_nonpos (mul_nonpos_of_nonpos_of_nonneg h1 h2) ... = -a * b : by rw neg_mul_eq_neg_mul ... = abs a * b : by rw (abs_of_nonpos h1) ... = abs a * abs b : by rw (abs_of_nonneg h2)) (assume h2 : b ≤ 0, calc abs (a * b) = a * b : abs_of_nonneg (mul_nonneg_of_nonpos_of_nonpos h1 h2) ... = -a * -b : by rw neg_mul_neg ... = abs a * -b : by rw (abs_of_nonpos h1) ... = abs a * abs b : by rw (abs_of_nonpos h2))) lemma abs_mul_abs_self (a : α) : abs a * abs a = a * a := abs_by_cases (λ x, x * x = a * a) rfl (neg_mul_neg a a) lemma abs_mul_self (a : α) : abs (a * a) = a * a := by rw [abs_mul, abs_mul_abs_self] lemma sub_le_of_abs_sub_le_left {a b c : α} (h : abs (a - b) ≤ c) : b - c ≤ a := if hz : 0 ≤ a - b then (calc a ≥ b : le_of_sub_nonneg hz ... ≥ b - c : sub_le_self _ (le_trans (abs_nonneg _) h)) else have habs : b - a ≤ c, by rwa [abs_of_neg (lt_of_not_ge hz), neg_sub] at h, have habs' : b ≤ c + a, from le_add_of_sub_right_le habs, sub_left_le_of_le_add habs' lemma sub_le_of_abs_sub_le_right {a b c : α} (h : abs (a - b) ≤ c) : a - c ≤ b := sub_le_of_abs_sub_le_left (abs_sub a b ▸ h) lemma sub_lt_of_abs_sub_lt_left {a b c : α} (h : abs (a - b) < c) : b - c < a := if hz : 0 ≤ a - b then (calc a ≥ b : le_of_sub_nonneg hz ... > b - c : sub_lt_self _ (lt_of_le_of_lt (abs_nonneg _) h)) else have habs : b - a < c, by rwa [abs_of_neg (lt_of_not_ge hz), neg_sub] at h, have habs' : b < c + a, from lt_add_of_sub_right_lt habs, sub_left_lt_of_lt_add habs' lemma sub_lt_of_abs_sub_lt_right {a b c : α} (h : abs (a - b) < c) : a - c < b := sub_lt_of_abs_sub_lt_left (abs_sub a b ▸ h) lemma abs_sub_square (a b : α) : abs (a - b) * abs (a - b) = a * a + b * b - (1 + 1) * a * b := begin rw abs_mul_abs_self, simp [left_distrib, right_distrib] end lemma eq_zero_of_mul_self_add_mul_self_eq_zero {x y : α} (h : x * x + y * y = 0) : x = 0 := have x * x ≤ (0 : α), from calc x * x ≤ x * x + y * y : le_add_of_nonneg_right (mul_self_nonneg y) ... = 0 : h, eq_zero_of_mul_self_eq_zero (le_antisymm this (mul_self_nonneg x)) lemma abs_abs_sub_abs_le_abs_sub (a b : α) : abs (abs a - abs b) ≤ abs (a - b) := begin apply nonneg_le_nonneg_of_squares_le, repeat {apply abs_nonneg}, repeat {rw abs_sub_square}, repeat {rw abs_abs}, repeat {rw abs_mul_abs_self}, apply sub_le_sub_left, repeat {rw mul_assoc}, apply mul_le_mul_of_nonneg_left, rw -abs_mul, apply le_abs_self, apply le_of_lt, apply add_pos, apply zero_lt_one, apply zero_lt_one end end decidable_linear_ordered_comm_ring section discrete_linear_ordered_field variables {α : Type u} [discrete_linear_ordered_field α] lemma abs_div (a b : α) : abs (a / b) = abs a / abs b := decidable.by_cases (suppose h : b = 0, by rw [h, abs_zero, div_zero, div_zero, abs_zero]) (suppose h : b ≠ 0, have h₁ : abs b ≠ 0, from assume h₂, h (eq_zero_of_abs_eq_zero h₂), eq_div_of_mul_eq _ _ h₁ (show abs (a / b) * abs b = abs a, by rw [-abs_mul, div_mul_cancel _ h])) lemma abs_one_div (a : α) : abs (1 / a) = 1 / abs a := by rw [abs_div, abs_of_nonneg (zero_le_one : 1 ≥ (0 : α))] end discrete_linear_ordered_field
16d7397215f4f92df981f5c03f2856ee45166cc0
e151e9053bfd6d71740066474fc500a087837323
/src/hott/init/equiv.lean
08b269a3a897df0a905a67809dd4518439d32e6c
[ "Apache-2.0" ]
permissive
daniel-carranza/hott3
15bac2d90589dbb952ef15e74b2837722491963d
913811e8a1371d3a5751d7d32ff9dec8aa6815d9
refs/heads/master
1,610,091,349,670
1,596,222,336,000
1,596,222,336,000
241,957,822
0
0
Apache-2.0
1,582,222,839,000
1,582,222,838,000
null
UTF-8
Lean
false
false
20,136
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad, Jakob von Raumer, Floris van Doorn Ported from Coq HoTT -/ import .path .meta.rewrite universes u v w hott_theory namespace hott open function /- Equivalences -/ -- This is our definition of equivalence. In the HoTT-book it's called -- ihae (half-adjoint equivalence). class is_equiv {A : Type u} {B : Type v} (f : A → B) := mk' :: (inv : B → A) (right_inv : Πb, f (inv b) = b) (left_inv : Πa, inv (f a) = a) (adj : Πx, right_inv (f x) = ap f (left_inv x)) attribute [reducible] is_equiv.inv -- A more bundled version of equivalence structure equiv (A : Type u) (B : Type v) := (to_fun : A → B) (to_is_equiv : is_equiv to_fun) namespace is_equiv /- Some instances and closure properties of equivalences -/ postfix `⁻¹ᶠ`:std.prec.max_plus := hott.is_equiv.inv section variables {A : Type u} {B : Type v} {C : Type w} (g : B → C) (f : A → B) {f' : A → B} -- The variant of mk' where f is explicit. @[hott, hsimp] protected def mk := @is_equiv.mk' A B f -- The identity function is an equivalence. @[hott, instance, hsimp] def is_equiv_id (A : Type v) : (is_equiv (id : A → A)) := is_equiv.mk id id (λa, idp) (λa, idp) (λa, idp) -- The composition of two equivalences is, again, an equivalence. @[hott, instance, hsimp] def is_equiv_compose [Hf : is_equiv f] [Hg : is_equiv g] : is_equiv (g ∘ f) := is_equiv.mk (g ∘ f) (f⁻¹ᶠ ∘ g⁻¹ᶠ) begin intro c, apply (⬝), tactic.swap, apply right_inv g, apply ap g, apply right_inv f end begin intro a, dsimp [(∘)], apply (⬝), {apply ap (inv f), apply left_inv g}, {apply left_inv} end begin abstract { exact (λa, (whisker_left _ (adj g (f a))) ⬝ (ap_con g _ _)⁻¹ ⬝ ap02 g ((ap_con_eq_con (right_inv f) (left_inv g (f a)))⁻¹ ⬝ (ap_compose f (inv f) _ ◾ adj f a) ⬝ (ap_con f _ _)⁻¹ ) ⬝ (ap_compose g f _)⁻¹) } end -- Any function equal to an equivalence is an equivlance as well. @[hott] def is_equiv_eq_closed {f : A → B} [Hf : is_equiv f] (Heq : f = f') : is_equiv f' := transport is_equiv Heq Hf end section parameters {A : Type u} {B : Type v} (f : A → B) (g : B → A) (ret : Πb, f (g b) = b) (sec : Πa, g (f a) = a) @[hott] def adjointify_left_inv' (a : A) : g (f a) = a := ap g (ap f (inverse (sec a))) ⬝ ap g (ret (f a)) ⬝ sec a def adjointify_adj' (a : A) : ret (f a) = ap f (adjointify_left_inv' a) := let fgretrfa := ap f (ap g (ret (f a))) in let fgfinvsect := ap f (ap g (ap f (sec a)⁻¹)) in let fgfa := f (g (f a)) in let retrfa := ret (f a) in have eq1 : ap f (sec a) = _, from calc ap f (sec a) = idp ⬝ ap f (sec a) : by rwr idp_con ... = (ret (f a) ⬝ (ret (f a))⁻¹) ⬝ ap f (sec a) : by rwr con.right_inv ... = ((ret fgfa)⁻¹ ⬝ ap (f ∘ g) (ret (f a))) ⬝ ap f (sec a) : by rwr con_ap_eq_con (λ x, (ret x)⁻¹) ... = ((ret fgfa)⁻¹ ⬝ fgretrfa) ⬝ ap f (sec a) : by rwr ap_compose ... = (ret fgfa)⁻¹ ⬝ (fgretrfa ⬝ ap f (sec a)) : by rwr con.assoc, have eq2 : ap f (sec a) ⬝ idp = (ret fgfa)⁻¹ ⬝ (fgretrfa ⬝ ap f (sec a)), from con_idp _ ⬝ eq1, have eq3 : idp = _, from calc idp = (ap f (sec a))⁻¹ ⬝ ((ret fgfa)⁻¹ ⬝ (fgretrfa ⬝ ap f (sec a))) : eq_inv_con_of_con_eq eq2 ... = ((ap f (sec a))⁻¹ ⬝ (ret fgfa)⁻¹) ⬝ (fgretrfa ⬝ ap f (sec a)) : by rwr con.assoc' ... = (ap f (sec a)⁻¹ ⬝ (ret fgfa)⁻¹) ⬝ (fgretrfa ⬝ ap f (sec a)) : by rwr ap_inv ... = ((ap f (sec a)⁻¹ ⬝ (ret fgfa)⁻¹) ⬝ fgretrfa) ⬝ ap f (sec a) : by rwr con.assoc' ... = ((retrfa⁻¹ ⬝ ap (f ∘ g) (ap f (sec a)⁻¹)) ⬝ fgretrfa) ⬝ ap f (sec a) : by rwr con_ap_eq_con (λ x, (ret x)⁻¹) ... = ((retrfa⁻¹ ⬝ fgfinvsect) ⬝ fgretrfa) ⬝ ap f (sec a) : by rwr ap_compose ... = (retrfa⁻¹ ⬝ (fgfinvsect ⬝ fgretrfa)) ⬝ ap f (sec a) : by rwr con.assoc' ... = retrfa⁻¹ ⬝ ap f (ap g (ap f (sec a)⁻¹) ⬝ ap g (ret (f a))) ⬝ ap f (sec a) : by rwr ap_con ... = retrfa⁻¹ ⬝ (ap f (ap g (ap f (sec a)⁻¹) ⬝ ap g (ret (f a))) ⬝ ap f (sec a)) : by rwr con.assoc' ... = retrfa⁻¹ ⬝ ap f ((ap g (ap f (sec a)⁻¹) ⬝ ap g (ret (f a))) ⬝ sec a) : by rwr ← ap_con, show ret (f a) = ap f ((ap g (ap f (sec a)⁻¹) ⬝ ap g (ret (f a))) ⬝ sec a), from eq_of_idp_eq_inv_con eq3 @[hott, hsimp] def adjointify : is_equiv f := is_equiv.mk f g ret adjointify_left_inv' adjointify_adj' end -- Any function pointwise equal to an equivalence is an equivalence as well. @[hott, hsimp] def homotopy_closed {A B : Type _} (f : A → B) {f' : A → B} [Hf : is_equiv f] (Hty : f ~ f') : is_equiv f' := adjointify f' (inv f) (λ b, (Hty (inv f b))⁻¹ ⬝ right_inv f b) (λ a, (ap (inv f) (Hty a))⁻¹ ⬝ left_inv f a) @[hott, hsimp] def inv_homotopy_closed {A B : Type _} {f : A → B} {f' : B → A} [Hf : is_equiv f] (Hty : f⁻¹ᶠ ~ f') : is_equiv f := adjointify f f' (λ b, ap f (Hty _)⁻¹ᵖ ⬝ right_inv f b) (λ a, (Hty _)⁻¹ᵖ ⬝ left_inv f a) @[hott] def inv_homotopy_inv {A B : Type _} {f g : A → B} [is_equiv f] [is_equiv g] (p : f ~ g) : inv f ~ inv g := λb, (left_inv g (f⁻¹ᶠ b))⁻¹ ⬝ ap g⁻¹ᶠ ((p (f⁻¹ᶠ b))⁻¹ ⬝ right_inv f b) @[hott, hsimp] instance is_equiv_up (A : Type _) : is_equiv (ulift.up : A → ulift A) := adjointify ulift.up ulift.down (λa, by induction a;reflexivity) (λa, idp) section variables {A : Type _} {B: Type _} {C : Type _} (f : A → B) {f' : A → B} [Hf : is_equiv f] (g : B → C) include Hf -- The function equiv_rect says that given an equivalence f : A → B, -- and a hypothesis from B, one may always assume that the hypothesis -- is in the image of e. -- In fibrational terms, if we have a fibration over B which has a section -- once pulled back along an equivalence f : A → B, then it has a section -- over all of B. @[hott] def is_equiv_rect (P : B → Type _) (g : Πa, P (f a)) (b : B) : P b := right_inv f b ▸ g (f⁻¹ᶠ b) @[hott] def is_equiv_rect' (P : A → B → Type _) (g : Πb, P (f⁻¹ᶠ b) b) (a : A) : P a (f a) := transport (λ x, P x (f a)) (left_inv f a) (g (f a)) @[hott, hsimp] def is_equiv_rect_comp (P : B → Type _) (df : Π (x : A), P (f x)) (x : A) : is_equiv_rect f P df (f x) = df x := calc is_equiv_rect f P df (f x) = right_inv f (f x) ▸ df (f⁻¹ᶠ (f x)) : by refl ... = ap f (left_inv f x) ▸ df (f⁻¹ᶠ (f x)) : by rwr adj ... = transport (P∘f) (left_inv f x) (df (f⁻¹ᶠ (f x))) : by rwr tr_compose ... = df x : by rwr (apdt df (left_inv f x)) @[hott] def adj_inv (b : B) : left_inv f (f⁻¹ᶠ b) = ap f⁻¹ᶠ (right_inv f b) := (is_equiv_rect f (λ fa, left_inv f (f⁻¹ᶠ fa) = ap f⁻¹ᶠ (right_inv f fa): _) (λa, (eq.cancel_right (left_inv f (id a)): _) (whisker_left _ (ap_id _)⁻¹ᵖ ⬝ (ap_con_eq_con_ap (left_inv f) (left_inv f a))⁻¹) ⬝ ap_compose _ _ _ ⬝ (ap02 f⁻¹ᶠ (adj f a).inverse): _) b: _) --The inverse of an equivalence is, again, an equivalence. @[instance,hott] def is_equiv_inv : is_equiv f⁻¹ᶠ := is_equiv.mk f⁻¹ᶠ f (left_inv f) (right_inv f) (adj_inv f) -- The 2-out-of-3 properties @[hott, hsimp] def cancel_right (g : B → C) [Hgf : is_equiv (g ∘ f)] : is_equiv g := have Hfinv : is_equiv f⁻¹ᶠ, from is_equiv_inv f, @homotopy_closed _ _ _ _ (is_equiv_compose (g ∘ f) f⁻¹ᶠ) (λb, ap g (@right_inv _ _ f _ b)) @[hott, hsimp] def cancel_left (g : C → A) [Hgf : is_equiv (f ∘ g)] : is_equiv g := have Hfinv : is_equiv f⁻¹ᶠ, from is_equiv_inv f, @homotopy_closed _ _ _ _ (is_equiv_compose f⁻¹ᶠ (f ∘ g)) (λa, left_inv f (g a)) @[hott] def eq_of_fn_eq_fn' {x y : A} (q : f x = f y) : x = y := (left_inv f x)⁻¹ ⬝ ap f⁻¹ᶠ q ⬝ left_inv f y @[hott, hsimp] def ap_eq_of_fn_eq_fn' {x y : A} (q : f x = f y) : ap f (eq_of_fn_eq_fn' f q) = q := ap_con _ _ _ ⬝ whisker_right _ (ap_con _ _ _) ⬝ ((ap_inv _ _ ⬝ inverse2 (adj f _)⁻¹) ◾ (inverse (ap_compose f f⁻¹ᶠ _)) ◾ (adj f _)⁻¹) ⬝ con_ap_con_eq_con_con (right_inv f) _ _ ⬝ whisker_right _ (con.left_inv _) ⬝ idp_con _ @[hott, hsimp] def eq_of_fn_eq_fn'_ap {x y : A} (q : x = y) : eq_of_fn_eq_fn' f (ap f q) = q := by induction q; apply con.left_inv @[instance, hott, hsimp] def is_equiv_ap (x y : A) : is_equiv (ap f : x = y → f x = f y) := adjointify (ap f) (eq_of_fn_eq_fn' f) (ap_eq_of_fn_eq_fn' f) (eq_of_fn_eq_fn'_ap f) end section variables {A : Type u} {B : Type v} {C : Type w} {f : A → B} [Hf : is_equiv f] include Hf section rewrite_rules variables {a : A} {b : B} @[hott] def eq_of_eq_inv (p : a = f⁻¹ᶠ b) : f a = b := ap f p ⬝ right_inv f b @[hott] def eq_of_inv_eq (p : f⁻¹ᶠ b = a) : b = f a := (right_inv f b)⁻¹ ⬝ ap f p @[hott] def inv_eq_of_eq (p : b = f a) : f⁻¹ᶠ b = a := ap f⁻¹ᶠ p ⬝ left_inv f a @[hott] def eq_inv_of_eq (p : f a = b) : a = f⁻¹ᶠ b := (left_inv f a)⁻¹ ⬝ ap f⁻¹ᶠ p end rewrite_rules variable (f) section pre_compose variables (α : A → C) (β : B → C) @[hott] def homotopy_of_homotopy_inv_pre (p : β ~ α ∘ f⁻¹ᶠ) : β ∘ f ~ α := λ a, p (f a) ⬝ ap α (left_inv f a) @[hott] def homotopy_of_inv_homotopy_pre (p : α ∘ f⁻¹ᶠ ~ β) : α ~ β ∘ f := λ a, (ap α (left_inv f a))⁻¹ ⬝ p (f a) @[hott] def inv_homotopy_of_homotopy_pre (p : α ~ β ∘ f) : α ∘ f⁻¹ᶠ ~ β := λ b, p (f⁻¹ᶠ b) ⬝ ap β (right_inv f b) @[hott] def homotopy_inv_of_homotopy_pre (p : β ∘ f ~ α) : β ~ α ∘ f⁻¹ᶠ := λ b, (ap β (right_inv f b))⁻¹ ⬝ p (f⁻¹ᶠ b) end pre_compose section post_compose variables (α : C → A) (β : C → B) @[hott] def homotopy_of_homotopy_inv_post (p : α ~ f⁻¹ᶠ ∘ β) : f ∘ α ~ β := λ c, ap f (p c) ⬝ right_inv f (β c) @[hott] def homotopy_of_inv_homotopy_post (p : f⁻¹ᶠ ∘ β ~ α) : β ~ f ∘ α := λ c, (right_inv f (β c))⁻¹ ⬝ ap f (p c) @[hott] def inv_homotopy_of_homotopy_post (p : β ~ f ∘ α) : f⁻¹ᶠ ∘ β ~ α := λ c, ap f⁻¹ᶠ (p c) ⬝ left_inv f (α c) @[hott] def homotopy_inv_of_homotopy_post (p : f ∘ α ~ β) : α ~ f⁻¹ᶠ ∘ β := λ c, (left_inv f (α c))⁻¹ ⬝ ap f⁻¹ᶠ (p c) end post_compose end --Transporting is an equivalence @[hott] def is_equiv_tr {A : Type u} (P : A → Type v) {x y : A} (p : x = y) : (is_equiv (transport P p)) := is_equiv.mk _ (transport P p⁻¹) (tr_inv_tr p) (inv_tr_tr p) (tr_inv_tr_lemma p) -- a version where the transport is a cast. Note: A and B live in the same universe here. @[hott, instance] def is_equiv_cast {A B : Type _} (H : A = B) : is_equiv (cast H) := is_equiv_tr (λX, X) H section variables {A : Type _} {B : A → Type _} {C : A → Type _} (f : Π{a}, B a → C a) [H : Πa, is_equiv (@f a)] {g : A → A} {g' : A → A} (h : Π{a}, B (g' a) → B (g a)) (h' : Π{a}, C (g' a) → C (g a)) include H @[hott] def inv_commute' (p : Π⦃a : A⦄ (b : B (g' a)), f (h b) = h' (f b)) {a : A} (c : C (g' a)) : f⁻¹ᶠ (h' c) = h (f⁻¹ᶠ c) := eq_of_fn_eq_fn' f (right_inv f (h' c) ⬝ ap h' (right_inv f c)⁻¹ ⬝ (p (f⁻¹ᶠ c))⁻¹) @[hott] def fun_commute_of_inv_commute' (p : Π⦃a : A⦄ (c : C (g' a)), f⁻¹ᶠ (h' c) = h (f⁻¹ᶠ c)) {a : A} (b : B (g' a)) : f (h b) = h' (f b) := eq_of_fn_eq_fn' f⁻¹ᶠ (left_inv f (h b) ⬝ ap h (left_inv f b)⁻¹ ⬝ (p (f b))⁻¹) @[hott] def ap_inv_commute' (p : Π⦃a : A⦄ (b : B (g' a)), f (h b) = h' (f b)) {a : A} (c : C (g' a)) : ap f (inv_commute' @f @h @h' p c) = right_inv f (h' c) ⬝ ap h' (right_inv f c)⁻¹ ⬝ (p (f⁻¹ᶠ c))⁻¹ := ap_eq_of_fn_eq_fn' _ _ -- inv_commute'_fn is in types.equiv end -- This is inv_commute' for A ≡ unit @[hott] def inv_commute1' {B C : Type _} (f : B → C) [is_equiv f] (h : B → B) (h' : C → C) (p : Π(b : B), f (h b) = h' (f b)) (c : C) : f⁻¹ᶠ (h' c) = h (f⁻¹ᶠ c) := eq_of_fn_eq_fn' f (right_inv f (h' c) ⬝ ap h' (right_inv f c)⁻¹ ⬝ (p (f⁻¹ᶠ c))⁻¹) end is_equiv open hott.is_equiv namespace eq local attribute [instance] is_equiv_tr @[hott] def tr_inv_fn {A : Type _} {B : A → Type _} {a a' : A} (p : a = a') : transport B p⁻¹ = (transport B p)⁻¹ᶠ := idp @[hott] def tr_inv {A : Type _} {B : A → Type _} {a a' : A} (p : a = a') (b : B a') : p⁻¹ ▸ b = (transport B p)⁻¹ᶠ b := idp @[hott] def cast_inv_fn {A B : Type _} (p : A = B) : cast p⁻¹ = (cast p)⁻¹ᶠ := idp @[hott] def cast_inv {A B : Type _} (p : A = B) (b : B) : cast p⁻¹ b = (cast p)⁻¹ᶠ b := idp end eq infix ` ≃ `:25 := equiv attribute [instance] equiv.to_is_equiv namespace equiv section variables {A : Type u} {B : Type v} {C : Type w} instance: has_coe_to_fun (A ≃ B) := ⟨_, to_fun⟩ @[hott, hsimp] def coe_equiv {A B : Type _} (e : A ≃ B) : ⇑e = e.to_fun := idp open is_equiv @[hott, hsimp] protected def MK (f : A → B) (g : B → A) (right_inv : Πb, f (g b) = b) (left_inv : Πa, g (f a) = a) : A ≃ B := equiv.mk f (adjointify f g right_inv left_inv) @[hott, hsimp, reducible] def to_inv (f : A ≃ B) : B → A := f.to_fun⁻¹ᶠ @[hott, hsimp] def to_right_inv (f : A ≃ B) (b : B) : f (f⁻¹ᶠ b) = b := right_inv f b @[hott, hsimp] def to_left_inv (f : A ≃ B) (a : A) : f⁻¹ᶠ (f a) = a := left_inv f a @[refl, hott, hsimp] protected def rfl : A ≃ A := equiv.mk id (hott.is_equiv.is_equiv_id _) @[hott, reducible, hsimp] protected def refl (A : Type _) : A ≃ A := @equiv.rfl A @[symm, hott] protected def symm (f : A ≃ B) : B ≃ A := equiv.mk f⁻¹ᶠ (is_equiv.is_equiv_inv f) @[trans, hott] protected def trans (f : A ≃ B) (g : B ≃ C) : A ≃ C := equiv.mk (g ∘ f) (is_equiv_compose _ _) infixl ` ⬝e `:75 := equiv.trans postfix `⁻¹ᵉ`:(max + 1) := equiv.symm @[reducible, hott] def erfl := @equiv.rfl @[hott, hsimp] def to_fun_symm (f : A ≃ B) (b : B) : f⁻¹ᵉ.to_fun b = to_inv f b := by refl @[hott, hsimp] def to_inv_symm (f : A ≃ B) (a : A) : f⁻¹ᵉ.to_fun⁻¹ᶠ a = f a := by refl @[hott, hsimp] def to_fun_trans (f : A ≃ B) (g : B ≃ C) (a : A) : (f ⬝e g).to_fun a = g (f a) := by refl @[hott, hsimp] def to_inv_trans (f : A ≃ B) (g : B ≃ C) (c : C) : (f ⬝e g).to_fun⁻¹ᶠ c = f.to_fun⁻¹ᶠ (g.to_fun⁻¹ᶠ c) := by refl @[hott] def is_equiv_to_inv (f : A ≃ B) : is_equiv f⁻¹ᶠ := by apply_instance @[hott, hsimp] def equiv_change_fun (f : A ≃ B) {f' : A → B} (Heq : f ~ f') : A ≃ B := equiv.mk f' (is_equiv.homotopy_closed f Heq) @[hott] def equiv_change_inv (f : A ≃ B) {f' : B → A} (Heq : f⁻¹ᶠ ~ f') : A ≃ B := equiv.mk f (inv_homotopy_closed Heq) --rename: eq_equiv_fn_eq_fn_of_is_equiv @[hott] def eq_equiv_fn_eq (f : A → B) [H : is_equiv f] (a b : A) : (a = b) ≃ (f a = f b) := equiv.mk (ap f) (is_equiv_ap _ _ _) --rename: eq_equiv_fn_eq_fn @[hott] def eq_equiv_fn_eq_of_equiv (f : A ≃ B) (a b : A) : (a = b) ≃ (f a = f b) := equiv.mk (ap f) (is_equiv_ap _ _ _) @[hott] def equiv_ap (P : A → Type _) {a b : A} (p : a = b) : P a ≃ P b := equiv.mk (transport P p) (is_equiv_tr _ _) @[hott] def equiv_of_eq {A B : Type u} (p : A = B) : A ≃ B := equiv.mk (cast p) (is_equiv_tr id _) @[hott, hsimp] def equiv_of_eq_refl (A : Type _) : equiv_of_eq (refl A) = equiv.refl A := idp @[hott] def eq_of_fn_eq_fn (f : A ≃ B) {x y : A} (q : f x = f y) : x = y := (left_inv _ x)⁻¹ ⬝ ap f⁻¹ᶠ q ⬝ left_inv _ y @[hott] def eq_of_fn_eq_fn_inv (f : A ≃ B) {x y : B} (q : f⁻¹ᵉ x = f⁻¹ᵉ y) : x = y := (right_inv _ x)⁻¹ ⬝ ap f q ⬝ right_inv f y @[hott] def ap_eq_of_fn_eq_fn (f : A ≃ B) {x y : A} (q : f x = f y) : ap f (eq_of_fn_eq_fn f q) = q := ap_eq_of_fn_eq_fn' f q @[hott] def eq_of_fn_eq_fn_ap (f : A ≃ B) {x y : A} (q : x = y) : eq_of_fn_eq_fn f (ap f q) = q := eq_of_fn_eq_fn'_ap f q @[hott] def to_inv_homotopy_inv {f g : A ≃ B} (p : f ~ g) : f⁻¹ᵉ ~ g⁻¹ᵉ := inv_homotopy_inv p --we need this theorem for the funext_of_ua proof @[hott] theorem inv_eq {A B : Type _} (eqf eqg : A ≃ B) (p : eqf = eqg) : eqf⁻¹ᶠ = eqg⁻¹ᶠ := eq.rec_on p idp @[trans, hott] def equiv_of_equiv_of_eq {A B C : Type _} (p : A = B) (q : B ≃ C) : A ≃ C := equiv_of_eq p ⬝e q @[trans, hott] def equiv_of_eq_of_equiv {A B C : Type _} (p : A ≃ B) (q : B = C) : A ≃ C := p ⬝e equiv_of_eq q @[hott] def equiv_ulift (A : Type _) : A ≃ ulift A := equiv.mk ulift.up (by apply_instance) @[hott] def equiv_rect (f : A ≃ B) (P : B → Type _) (g : Πa, P (f a)) (b : B) : P b := right_inv f b ▸ g (f⁻¹ᶠ b) @[hott] def equiv_rect' (f : A ≃ B) (P : A → B → Type _) (g : Πb, P (f⁻¹ᶠ b) b) (a : A) : P a (f a) := transport (λ x : A, P x (f a)) (left_inv f a) (g (f a)) @[hott] def equiv_rect_comp (f : A ≃ B) (P : B → Type _) (df : Π (x : A), P (f x)) (x : A) : equiv_rect f P df (f x) = df x := calc equiv_rect f P df (f x) = right_inv f (f x) ▸ df (f⁻¹ᶠ (f x)) : by refl ... = ap f (left_inv f x) ▸ df (f⁻¹ᶠ (f x)) : by rwr ← adj ... = transport (P∘f) (left_inv f x) (df (f⁻¹ᶠ (f x))) : by rwr tr_compose ... = df x : by apply apdt df (left_inv f x) end section variables {A : Type _} {B : Type _} (f : A ≃ B) {a : A} {b : B} @[hott] def to_eq_of_eq_inv (p : a = f⁻¹ᶠ b) : f a = b := ap f p ⬝ to_right_inv f b @[hott] def to_eq_of_inv_eq (p : f⁻¹ᶠ b = a) : b = f a := (to_right_inv f b)⁻¹ ⬝ ap f p @[hott] def to_inv_eq_of_eq (p : b = f a) : f⁻¹ᶠ b = a := ap _ p ⬝ left_inv f a @[hott] def to_eq_inv_of_eq (p : f a = b) : a = f⁻¹ᶠ b := (left_inv f a)⁻¹ ⬝ ap _ p end section variables {A : Type _} {B : A → Type _} {C : A → Type _} (f : Π{a}, B a ≃ C a) {g : A → A} {g' : A → A} (h : Π{a}, B (g' a) → B (g a)) (h' : Π{a}, C (g' a) → C (g a)) @[hott] def inv_commute (p : Π⦃a : A⦄ (b : B (g' a)), f.to_fun (h b) = h' (f.to_fun b)) {a : A} (c : C (g' a)) : f.to_inv (h' c) = h (f.to_inv c) := @inv_commute' A B C (λ a, f.to_fun) _ g g' @h @h' p _ c @[hott] def fun_commute_of_inv_commute (p : Π⦃a : A⦄ (c : C (g' a)), f.to_inv (h' c) = h (f.to_inv c)) {a : A} (b : B (g' a)) : f.to_fun (h b) = h' (f.to_fun b) := @fun_commute_of_inv_commute' A B C (λ a, f.to_fun) _ g g' @h @h' p _ b @[hott] def inv_commute1 {B C : Type _} (f : B ≃ C) (h : B → B) (h' : C → C) (p : Π(b : B), f (h b) = h' (f b)) (c : C) : f⁻¹ᶠ (h' c) = h (f⁻¹ᶠ c) := inv_commute1' f h h' p c end infixl ` ⬝pe `:75 := equiv_of_equiv_of_eq infixl ` ⬝ep `:75 := equiv_of_eq_of_equiv end equiv open equiv namespace is_equiv @[hott] def is_equiv_of_equiv_of_homotopy {A B : Type _} (f : A ≃ B) {f' : A → B} (Hty : f ~ f') : is_equiv f' := @homotopy_closed _ _ f f' _ Hty end is_equiv end hott
f4f75f389afa88cc96992a95ff31f4652608fb52
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/concrete_category/bundled_auto.lean
0c0b518d643f68531b99ace7c83bcd141d19c41a
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
2,571
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johannes Hölzl, Reid Barton, Sean Leather -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.category.default import Mathlib.PostPort universes u v l namespace Mathlib /-! # Bundled types `bundled c` provides a uniform structure for bundling a type equipped with a type class. We provide `category` instances for these in `category_theory/unbundled_hom.lean` (for categories with unbundled homs, e.g. topological spaces) and in `category_theory/bundled_hom.lean` (for categories with bundled homs, e.g. monoids). -/ namespace category_theory /-- `bundled` is a type bundled with a type class instance for that type. Only the type class is exposed as a parameter. -/ structure bundled (c : Type u → Type v) where α : Type u str : autoParam (c α) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.tactic.apply_instance") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "tactic") "apply_instance") []) namespace bundled /-- A generic function for lifting a type equipped with an instance to a bundled object. -/ -- Usually explicit instances will provide their own version of this, e.g. `Mon.of` and `Top.of`. def of {c : Type u → Type v} (α : Type u) [str : c α] : bundled c := mk α protected instance has_coe_to_sort {c : Type u → Type v} : has_coe_to_sort (bundled c) := has_coe_to_sort.mk (Type u) α @[simp] theorem coe_mk {c : Type u → Type v} (α : Type u) (str : autoParam (c α) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.tactic.apply_instance") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "tactic") "apply_instance") [])) : ↥(mk α) = α := rfl /- `bundled.map` is reducible so that, if we define a category def Ring : Type (u+1) := induced_category SemiRing (bundled.map @ring.to_semiring) instance search is able to "see" that a morphism R ⟶ S in Ring is really a (semi)ring homomorphism from R.α to S.α, and not merely from `(bundled.map @ring.to_semiring R).α` to `(bundled.map @ring.to_semiring S).α`. -/ /-- Map over the bundled structure -/ def map {c : Type u → Type v} {d : Type u → Type v} (f : {α : Type u} → c α → d α) (b : bundled c) : bundled d := mk ↥b end Mathlib
0ab70adbdf6c8c4bc74e10eb5eb8f5c5a4c4bef9
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/ring_theory/polynomial/chebyshev/basic_auto.lean
cb9b492b89fd4817456014061f06efc8dff78631
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
3,345
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.ring_theory.polynomial.chebyshev.defs import Mathlib.analysis.special_functions.trigonometric import Mathlib.ring_theory.localization import Mathlib.data.zmod.basic import Mathlib.algebra.invertible import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Chebyshev polynomials The Chebyshev polynomials are two families of polynomials indexed by `ℕ`, with integral coefficients. In this file, we only consider Chebyshev polynomials of the first kind. ## Main declarations * `polynomial.chebyshev₁_mul`, the `(m * n)`-th Chebyshev polynomial is the composition of the `m`-th and `n`-th Chebyshev polynomials. * `polynomial.lambdashev_mul`, the `(m * n)`-th lambdashev polynomial is the composition of the `m`-th and `n`-th lambdashev polynomials. * `polynomial.lambdashev_char_p`, for a prime number `p`, the `p`-th lambdashev polynomial is congruent to `X ^ p` modulo `p`. ## Implementation details Since Chebyshev polynomials have interesting behaviour over the complex numbers and modulo `p`, we define them to have coefficients in an arbitrary commutative ring, even though technically `ℤ` would suffice. The benefit of allowing arbitrary coefficient rings, is that the statements afterwards are clean, and do not have `map (int.cast_ring_hom R)` interfering all the time. -/ namespace polynomial /-- The `(m * n)`-th Chebyshev polynomial is the composition of the `m`-th and `n`-th -/ theorem chebyshev₁_mul (R : Type u_1) [comm_ring R] (m : ℕ) (n : ℕ) : chebyshev₁ R (m * n) = comp (chebyshev₁ R m) (chebyshev₁ R n) := sorry /-! ### A Lambda structure on `polynomial ℤ` Mathlib doesn't currently know what a Lambda ring is. But once it does, we can endow `polynomial ℤ` with a Lambda structure in terms of the `lambdashev` polynomials defined below. There is exactly one other Lambda structure on `polynomial ℤ` in terms of binomial polynomials. -/ theorem lambdashev_eval_add_inv {R : Type u_1} [comm_ring R] (x : R) (y : R) (h : x * y = 1) (n : ℕ) : eval (x + y) (lambdashev R n) = x ^ n + y ^ n := sorry theorem lambdashev_eq_chebyshev₁ (R : Type u_1) [comm_ring R] [invertible (bit0 1)] (n : ℕ) : lambdashev R n = bit0 1 * comp (chebyshev₁ R n) (coe_fn C ⅟ * X) := sorry theorem chebyshev₁_eq_lambdashev (R : Type u_1) [comm_ring R] [invertible (bit0 1)] (n : ℕ) : chebyshev₁ R n = coe_fn C ⅟ * comp (lambdashev R n) (bit0 1 * X) := sorry /-- the `(m * n)`-th lambdashev polynomial is the composition of the `m`-th and `n`-th -/ theorem lambdashev_mul (R : Type u_1) [comm_ring R] (m : ℕ) (n : ℕ) : lambdashev R (m * n) = comp (lambdashev R m) (lambdashev R n) := sorry theorem lambdashev_comp_comm (R : Type u_1) [comm_ring R] (m : ℕ) (n : ℕ) : comp (lambdashev R m) (lambdashev R n) = comp (lambdashev R n) (lambdashev R m) := sorry theorem lambdashev_zmod_p (p : ℕ) [fact (nat.prime p)] : lambdashev (zmod p) p = X ^ p := sorry theorem lambdashev_char_p (R : Type u_1) [comm_ring R] (p : ℕ) [fact (nat.prime p)] [char_p R p] : lambdashev R p = X ^ p := sorry end Mathlib
9eb8c8d9e33a39cd7d7d52dc291d8bb05ef5e240
02fbe05a45fda5abde7583464416db4366eedfbf
/tests/lean/run/term_app2.lean
506df05a977762cfc9136099a3e698bb3518601d
[ "Apache-2.0" ]
permissive
jasonrute/lean
cc12807e11f9ac6b01b8951a8bfb9c2eb35a0154
4be962c167ca442a0ec5e84472d7ff9f5302788f
refs/heads/master
1,672,036,664,637
1,601,642,826,000
1,601,642,826,000
260,777,966
0
0
Apache-2.0
1,588,454,819,000
1,588,454,818,000
null
UTF-8
Lean
false
false
2,267
lean
local attribute [simp] nat.add_comm nat.add_left_comm lemma nat.lt_add_of_lt {a b c : nat} : a < b → a < c + b := begin intro h, have aux₁ := nat.le_add_right b c, have aux₂ := lt_of_lt_of_le h aux₁, rwa [nat.add_comm] at aux₂ end lemma nat.lt_one_add_of_lt {a b : nat} : a < b → a < 1 + b := begin intro h, have aux := lt.trans h (nat.lt_succ_self _), rwa [<- nat.add_one, nat.add_comm] at aux end namespace list def attach_aux {α} (l : list α) : Π (c : list α), (∀ x : α, x ∈ c → x ∈ l) → list {a : α // a ∈ l} | [] h := [] | (a::as) h := ⟨a, h a (list.mem_cons_self _ _)⟩ :: attach_aux as (λ x hin, h x (list.mem_cons_of_mem _ hin)) def attach {α} (l : list α) : list {a : α // a ∈ l} := attach_aux l l (λ x h, h) open well_founded_tactics lemma sizeof_lt_sizeof_of_mem {α} [has_sizeof α] {a : α} : ∀ {l : list α}, a ∈ l → sizeof a < sizeof l | [] h := absurd h (not_mem_nil _) | (b::bs) h := begin cases eq_or_mem_of_mem_cons h with h_1 h_2, subst h_1, {unfold_sizeof, cancel_nat_add_lt, trivial_nat_lt}, {have aux₁ := sizeof_lt_sizeof_of_mem h_2, unfold_sizeof, exact nat.lt_one_add_of_lt (nat.lt_add_of_lt aux₁)} end end list inductive term | const : string → term | app : string → list term → term def num_consts : term → nat | (term.const n) := 1 | (term.app n ts) := ts.attach.foldl (λ r p, have sizeof p.1 < n.length + (1 + sizeof ts), from calc sizeof p.1 < 1 + (n.length + sizeof ts) : nat.lt_one_add_of_lt (nat.lt_add_of_lt (list.sizeof_lt_sizeof_of_mem p.2)) ... = n.length + (1 + sizeof ts) : by simp, r + num_consts p.1) 0 #eval num_consts (term.app "f" [term.const "x", term.app "g" [term.const "x", term.const "y"]]) #check num_consts.equations._eqn_2 def num_consts' : term → nat | (term.const n) := 1 | (term.app n ts) := ts.attach.foldl (λ r ⟨t, h⟩, have sizeof t < n.length + (1 + sizeof ts), from calc sizeof t < 1 + (n.length + sizeof ts) : nat.lt_one_add_of_lt (nat.lt_add_of_lt (list.sizeof_lt_sizeof_of_mem h)) ... = n.length + (1 + sizeof ts) : by simp, r + num_consts' t) 0 #check num_consts'.equations._eqn_2
a6e9381b60f470403e633eab2de80673fcead387
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/stage0/src/Lean/Parser/Basic.lean
6683bcb56848e51988fe58c231b37b59fe745bf4
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
76,164
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ /-! # Basic Lean parser infrastructure The Lean parser was developed with the following primary goals in mind: * flexibility: Lean's grammar is complex and includes indentation and other whitespace sensitivity. It should be possible to introduce such custom "tweaks" locally without having to adjust the fundamental parsing approach. * extensibility: Lean's grammar can be extended on the fly within a Lean file, and with Lean 4 we want to extend this to cover embedding domain-specific languages that may look nothing like Lean, down to using a separate set of tokens. * losslessness: The parser should produce a concrete syntax tree that preserves all whitespace and other "sub-token" information for the use in tooling. * performance: The overhead of the parser building blocks, and the overall parser performance on average-complexity input, should be comparable with that of the previous parser hand-written in C++. No fancy optimizations should be necessary for this. Given these constraints, we decided to implement a combinatoric, non-monadic, lexer-less, memoizing recursive-descent parser. Using combinators instead of some more formal and introspectible grammar representation ensures ultimate flexibility as well as efficient extensibility: there is (almost) no pre-processing necessary when extending the grammar with a new parser. However, because the all results the combinators produce are of the homogeneous `Syntax` type, the basic parser type is not actually a monad but a monomorphic linear function `ParserState → ParserState`, avoiding constructing and deconstructing countless monadic return values. Instead of explicitly returning syntax objects, parsers push (zero or more of) them onto a syntax stack inside the linear state. Chaining parsers via `>>` accumulates their output on the stack. Combinators such as `node` then pop off all syntax objects produced during their invocation and wrap them in a single `Syntax.node` object that is again pushed on this stack. Instead of calling `node` directly, we usually use the macro `leading_parser p`, which unfolds to `node k p` where the new syntax node kind `k` is the name of the declaration being defined. The lack of a dedicated lexer ensures we can modify and replace the lexical grammar at any point, and simplifies detecting and propagating whitespace. The parser still has a concept of "tokens", however, and caches the most recent one for performance: when `tokenFn` is called twice at the same position in the input, it will reuse the result of the first call. `tokenFn` recognizes some built-in variable-length tokens such as identifiers as well as any fixed token in the `ParserContext`'s `TokenTable` (a trie); however, the same cache field and strategy could be reused by custom token parsers. Tokens also play a central role in the `prattParser` combinator, which selects a *leading* parser followed by zero or more *trailing* parsers based on the current token (via `peekToken`); see the documentation of `prattParser` for more details. Tokens are specified via the `symbol` parser, or with `symbolNoWs` for tokens that should not be preceded by whitespace. The `Parser` type is extended with additional metadata over the mere parsing function to propagate token information: `collectTokens` collects all tokens within a parser for registering. `firstTokens` holds information about the "FIRST" token set used to speed up parser selection in `prattParser`. This approach of combining static and dynamic information in the parser type is inspired by the paper "Deterministic, Error-Correcting Combinator Parsers" by Swierstra and Duponcheel. If multiple parsers accept the same current token, `prattParser` tries all of them using the backtracking `longestMatchFn` combinator. This is the only case where standard parsers might execute arbitrary backtracking. At the moment there is no memoization shared by these parallel parsers apart from the first token, though we might change this in the future if the need arises. Finally, error reporting follows the standard combinatoric approach of collecting a single unexpected token/... and zero or more expected tokens (see `Error` below). Expected tokens are e.g. set by `symbol` and merged by `<|>`. Combinators running multiple parsers should check if an error message is set in the parser state (`hasError`) and act accordingly. Error recovery is left to the designer of the specific language; for example, Lean's top-level `parseCommand` loop skips tokens until the next command keyword on error. -/ import Lean.Data.Trie import Lean.Data.Position import Lean.Syntax import Lean.ToExpr import Lean.Environment import Lean.Attributes import Lean.Message import Lean.Compiler.InitAttr import Lean.ResolveName namespace Lean namespace Parser def isLitKind (k : SyntaxNodeKind) : Bool := k == strLitKind || k == numLitKind || k == charLitKind || k == nameLitKind || k == scientificLitKind abbrev mkAtom (info : SourceInfo) (val : String) : Syntax := Syntax.atom info val abbrev mkIdent (info : SourceInfo) (rawVal : Substring) (val : Name) : Syntax := Syntax.ident info rawVal val [] /- Return character after position `pos` -/ def getNext (input : String) (pos : Nat) : Char := input.get (input.next pos) /- Maximal (and function application) precedence. In the standard lean language, no parser has precedence higher than `maxPrec`. Note that nothing prevents users from using a higher precedence, but we strongly discourage them from doing it. -/ def maxPrec : Nat := eval_prec max def argPrec : Nat := eval_prec arg def leadPrec : Nat := eval_prec lead def minPrec : Nat := eval_prec min abbrev Token := String structure TokenCacheEntry where startPos : String.Pos := 0 stopPos : String.Pos := 0 token : Syntax := Syntax.missing structure ParserCache where tokenCache : TokenCacheEntry def initCacheForInput (input : String) : ParserCache := { tokenCache := { startPos := input.bsize + 1 /- make sure it is not a valid position -/} } abbrev TokenTable := Trie Token abbrev SyntaxNodeKindSet := Std.PersistentHashMap SyntaxNodeKind Unit def SyntaxNodeKindSet.insert (s : SyntaxNodeKindSet) (k : SyntaxNodeKind) : SyntaxNodeKindSet := Std.PersistentHashMap.insert s k () /- Input string and related data. Recall that the `FileMap` is a helper structure for mapping `String.Pos` in the input string to line/column information. -/ structure InputContext where input : String fileName : String fileMap : FileMap deriving Inhabited /-- Input context derived from elaboration of previous commands. -/ structure ParserModuleContext where env : Environment options : Options -- for name lookup currNamespace : Name := Name.anonymous openDecls : List OpenDecl := [] structure ParserContext extends InputContext, ParserModuleContext where prec : Nat tokens : TokenTable quotDepth : Nat := 0 suppressInsideQuot : Bool := false savedPos? : Option String.Pos := none forbiddenTk? : Option Token := none def ParserContext.resolveName (ctx : ParserContext) (id : Name) : List (Name × List String) := ResolveName.resolveGlobalName ctx.env ctx.currNamespace ctx.openDecls id structure Error where unexpected : String := "" expected : List String := [] deriving Inhabited, BEq namespace Error private def expectedToString : List String → String | [] => "" | [e] => e | [e1, e2] => e1 ++ " or " ++ e2 | e::es => e ++ ", " ++ expectedToString es protected def toString (e : Error) : String := let unexpected := if e.unexpected == "" then [] else [e.unexpected] let expected := if e.expected == [] then [] else let expected := e.expected.toArray.qsort (fun e e' => e < e') let expected := expected.toList.eraseReps ["expected " ++ expectedToString expected] "; ".intercalate $ unexpected ++ expected instance : ToString Error := ⟨Error.toString⟩ def merge (e₁ e₂ : Error) : Error := match e₂ with | { unexpected := u, .. } => { unexpected := if u == "" then e₁.unexpected else u, expected := e₁.expected ++ e₂.expected } end Error structure ParserState where stxStack : Array Syntax := #[] /-- Set to the precedence of the preceding (not surrounding) parser by `runLongestMatchParser` for the use of `checkLhsPrec` in trailing parsers. Note that with chaining, the preceding parser can be another trailing parser: in `1 * 2 + 3`, the preceding parser is '*' when '+' is executed. -/ lhsPrec : Nat := 0 pos : String.Pos := 0 cache : ParserCache errorMsg : Option Error := none namespace ParserState @[inline] def hasError (s : ParserState) : Bool := s.errorMsg != none @[inline] def stackSize (s : ParserState) : Nat := s.stxStack.size def restore (s : ParserState) (iniStackSz : Nat) (iniPos : Nat) : ParserState := { s with stxStack := s.stxStack.shrink iniStackSz, errorMsg := none, pos := iniPos } def setPos (s : ParserState) (pos : Nat) : ParserState := { s with pos := pos } def setCache (s : ParserState) (cache : ParserCache) : ParserState := { s with cache := cache } def pushSyntax (s : ParserState) (n : Syntax) : ParserState := { s with stxStack := s.stxStack.push n } def popSyntax (s : ParserState) : ParserState := { s with stxStack := s.stxStack.pop } def shrinkStack (s : ParserState) (iniStackSz : Nat) : ParserState := { s with stxStack := s.stxStack.shrink iniStackSz } def next (s : ParserState) (input : String) (pos : Nat) : ParserState := { s with pos := input.next pos } def toErrorMsg (ctx : ParserContext) (s : ParserState) : String := match s.errorMsg with | none => "" | some msg => let pos := ctx.fileMap.toPosition s.pos mkErrorStringWithPos ctx.fileName pos (toString msg) def mkNode (s : ParserState) (k : SyntaxNodeKind) (iniStackSz : Nat) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, err⟩ => if err != none && stack.size == iniStackSz then -- If there is an error but there are no new nodes on the stack, use `missing` instead. -- Thus we ensure the property that an syntax tree contains (at least) one `missing` node -- if (and only if) there was a parse error. -- We should not create an actual node of kind `k` in this case because it would mean we -- choose an "arbitrary" node (in practice the last one) in an alternative of the form -- `node k1 p1 <|> ... <|> node kn pn` when all parsers fail. With the code below we -- instead return a less misleading single `missing` node without randomly selecting any `ki`. let stack := stack.push Syntax.missing ⟨stack, lhsPrec, pos, cache, err⟩ else let newNode := Syntax.node k (stack.extract iniStackSz stack.size) let stack := stack.shrink iniStackSz let stack := stack.push newNode ⟨stack, lhsPrec, pos, cache, err⟩ def mkTrailingNode (s : ParserState) (k : SyntaxNodeKind) (iniStackSz : Nat) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, err⟩ => let newNode := Syntax.node k (stack.extract (iniStackSz - 1) stack.size) let stack := stack.shrink (iniStackSz - 1) let stack := stack.push newNode ⟨stack, lhsPrec, pos, cache, err⟩ def setError (s : ParserState) (msg : String) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, _⟩ => ⟨stack, lhsPrec, pos, cache, some { expected := [ msg ] }⟩ def mkError (s : ParserState) (msg : String) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, _⟩ => ⟨stack.push Syntax.missing, lhsPrec, pos, cache, some { expected := [ msg ] }⟩ def mkUnexpectedError (s : ParserState) (msg : String) (expected : List String := []) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, _⟩ => ⟨stack.push Syntax.missing, lhsPrec, pos, cache, some { unexpected := msg, expected := expected }⟩ def mkEOIError (s : ParserState) (expected : List String := []) : ParserState := s.mkUnexpectedError "unexpected end of input" expected def mkErrorAt (s : ParserState) (msg : String) (pos : String.Pos) (initStackSz? : Option Nat := none) : ParserState := match s, initStackSz? with | ⟨stack, lhsPrec, _, cache, _⟩, none => ⟨stack.push Syntax.missing, lhsPrec, pos, cache, some { expected := [ msg ] }⟩ | ⟨stack, lhsPrec, _, cache, _⟩, some sz => ⟨stack.shrink sz |>.push Syntax.missing, lhsPrec, pos, cache, some { expected := [ msg ] }⟩ def mkErrorsAt (s : ParserState) (ex : List String) (pos : String.Pos) (initStackSz? : Option Nat := none) : ParserState := match s, initStackSz? with | ⟨stack, lhsPrec, _, cache, _⟩, none => ⟨stack.push Syntax.missing, lhsPrec, pos, cache, some { expected := ex }⟩ | ⟨stack, lhsPrec, _, cache, _⟩, some sz => ⟨stack.shrink sz |>.push Syntax.missing, lhsPrec, pos, cache, some { expected := ex }⟩ def mkUnexpectedErrorAt (s : ParserState) (msg : String) (pos : String.Pos) : ParserState := match s with | ⟨stack, lhsPrec, _, cache, _⟩ => ⟨stack.push Syntax.missing, lhsPrec, pos, cache, some { unexpected := msg }⟩ end ParserState def ParserFn := ParserContext → ParserState → ParserState instance : Inhabited ParserFn where default := fun ctx s => s inductive FirstTokens where | epsilon : FirstTokens | unknown : FirstTokens | tokens : List Token → FirstTokens | optTokens : List Token → FirstTokens deriving Inhabited namespace FirstTokens def seq : FirstTokens → FirstTokens → FirstTokens | epsilon, tks => tks | optTokens s₁, optTokens s₂ => optTokens (s₁ ++ s₂) | optTokens s₁, tokens s₂ => tokens (s₁ ++ s₂) | tks, _ => tks def toOptional : FirstTokens → FirstTokens | tokens tks => optTokens tks | tks => tks def merge : FirstTokens → FirstTokens → FirstTokens | epsilon, tks => toOptional tks | tks, epsilon => toOptional tks | tokens s₁, tokens s₂ => tokens (s₁ ++ s₂) | optTokens s₁, optTokens s₂ => optTokens (s₁ ++ s₂) | tokens s₁, optTokens s₂ => optTokens (s₁ ++ s₂) | optTokens s₁, tokens s₂ => optTokens (s₁ ++ s₂) | _, _ => unknown def toStr : FirstTokens → String | epsilon => "epsilon" | unknown => "unknown" | tokens tks => toString tks | optTokens tks => "?" ++ toString tks instance : ToString FirstTokens := ⟨toStr⟩ end FirstTokens structure ParserInfo where collectTokens : List Token → List Token := id collectKinds : SyntaxNodeKindSet → SyntaxNodeKindSet := id firstTokens : FirstTokens := FirstTokens.unknown deriving Inhabited structure Parser where info : ParserInfo := {} fn : ParserFn deriving Inhabited abbrev TrailingParser := Parser def dbgTraceStateFn (label : String) (p : ParserFn) : ParserFn := fun c s => let sz := s.stxStack.size let s' := p c s dbg_trace "{label} pos: {s'.pos} err: {s'.errorMsg} out: {s'.stxStack.extract sz s'.stxStack.size}" s' def dbgTraceState (label : String) (p : Parser) : Parser where fn := dbgTraceStateFn label p.fn info := p.info @[noinline] def epsilonInfo : ParserInfo := { firstTokens := FirstTokens.epsilon } @[inline] def checkStackTopFn (p : Syntax → Bool) (msg : String) : ParserFn := fun c s => if p s.stxStack.back then s else s.mkUnexpectedError msg @[inline] def checkStackTop (p : Syntax → Bool) (msg : String) : Parser := { info := epsilonInfo, fn := checkStackTopFn p msg } @[inline] def andthenFn (p q : ParserFn) : ParserFn := fun c s => let s := p c s if s.hasError then s else q c s @[noinline] def andthenInfo (p q : ParserInfo) : ParserInfo := { collectTokens := p.collectTokens ∘ q.collectTokens, collectKinds := p.collectKinds ∘ q.collectKinds, firstTokens := p.firstTokens.seq q.firstTokens } @[inline] def andthen (p q : Parser) : Parser := { info := andthenInfo p.info q.info, fn := andthenFn p.fn q.fn } instance : AndThen Parser := ⟨andthen⟩ @[inline] def nodeFn (n : SyntaxNodeKind) (p : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize let s := p c s s.mkNode n iniSz @[inline] def trailingNodeFn (n : SyntaxNodeKind) (p : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize let s := p c s s.mkTrailingNode n iniSz @[noinline] def nodeInfo (n : SyntaxNodeKind) (p : ParserInfo) : ParserInfo := { collectTokens := p.collectTokens, collectKinds := fun s => (p.collectKinds s).insert n, firstTokens := p.firstTokens } @[inline] def node (n : SyntaxNodeKind) (p : Parser) : Parser := { info := nodeInfo n p.info, fn := nodeFn n p.fn } def errorFn (msg : String) : ParserFn := fun _ s => s.mkUnexpectedError msg @[inline] def error (msg : String) : Parser := { info := epsilonInfo, fn := errorFn msg } def errorAtSavedPosFn (msg : String) (delta : Bool) : ParserFn := fun c s => match c.savedPos? with | none => s | some pos => let pos := if delta then c.input.next pos else pos match s with | ⟨stack, lhsPrec, _, cache, _⟩ => ⟨stack.push Syntax.missing, lhsPrec, pos, cache, some { unexpected := msg }⟩ /- Generate an error at the position saved with the `withPosition` combinator. If `delta == true`, then it reports at saved position+1. This useful to make sure a parser consumed at least one character. -/ @[inline] def errorAtSavedPos (msg : String) (delta : Bool) : Parser := { fn := errorAtSavedPosFn msg delta } /- Succeeds if `c.prec <= prec` -/ def checkPrecFn (prec : Nat) : ParserFn := fun c s => if c.prec <= prec then s else s.mkUnexpectedError "unexpected token at this precedence level; consider parenthesizing the term" @[inline] def checkPrec (prec : Nat) : Parser := { info := epsilonInfo, fn := checkPrecFn prec } /- Succeeds if `c.lhsPrec >= prec` -/ def checkLhsPrecFn (prec : Nat) : ParserFn := fun c s => if s.lhsPrec >= prec then s else s.mkUnexpectedError "unexpected token at this precedence level; consider parenthesizing the term" @[inline] def checkLhsPrec (prec : Nat) : Parser := { info := epsilonInfo, fn := checkLhsPrecFn prec } def setLhsPrecFn (prec : Nat) : ParserFn := fun c s => if s.hasError then s else { s with lhsPrec := prec } @[inline] def setLhsPrec (prec : Nat) : Parser := { info := epsilonInfo, fn := setLhsPrecFn prec } def checkInsideQuotFn : ParserFn := fun c s => if c.quotDepth > 0 && !c.suppressInsideQuot then s else s.mkUnexpectedError "unexpected syntax outside syntax quotation" @[inline] def checkInsideQuot : Parser := { info := epsilonInfo, fn := checkInsideQuotFn } def checkOutsideQuotFn : ParserFn := fun c s => if !c.quotDepth == 0 || c.suppressInsideQuot then s else s.mkUnexpectedError "unexpected syntax inside syntax quotation" @[inline] def checkOutsideQuot : Parser := { info := epsilonInfo, fn := checkOutsideQuotFn } def addQuotDepthFn (i : Int) (p : ParserFn) : ParserFn := fun c s => p { c with quotDepth := c.quotDepth + i |>.toNat } s @[inline] def incQuotDepth (p : Parser) : Parser := { info := p.info, fn := addQuotDepthFn 1 p.fn } @[inline] def decQuotDepth (p : Parser) : Parser := { info := p.info, fn := addQuotDepthFn (-1) p.fn } def suppressInsideQuotFn (p : ParserFn) : ParserFn := fun c s => p { c with suppressInsideQuot := true } s @[inline] def suppressInsideQuot (p : Parser) : Parser := { info := p.info, fn := suppressInsideQuotFn p.fn } @[inline] def leadingNode (n : SyntaxNodeKind) (prec : Nat) (p : Parser) : Parser := checkPrec prec >> node n p >> setLhsPrec prec @[inline] def trailingNodeAux (n : SyntaxNodeKind) (p : Parser) : TrailingParser := { info := nodeInfo n p.info, fn := trailingNodeFn n p.fn } @[inline] def trailingNode (n : SyntaxNodeKind) (prec lhsPrec : Nat) (p : Parser) : TrailingParser := checkPrec prec >> checkLhsPrec lhsPrec >> trailingNodeAux n p >> setLhsPrec prec def mergeOrElseErrors (s : ParserState) (error1 : Error) (iniPos : Nat) (mergeErrors : Bool) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, some error2⟩ => if pos == iniPos then ⟨stack, lhsPrec, pos, cache, some (if mergeErrors then error1.merge error2 else error2)⟩ else s | other => other def orelseFnCore (p q : ParserFn) (mergeErrors : Bool) : ParserFn := fun c s => let iniSz := s.stackSize let iniPos := s.pos let s := p c s match s.errorMsg with | some errorMsg => if s.pos == iniPos then mergeOrElseErrors (q c (s.restore iniSz iniPos)) errorMsg iniPos mergeErrors else s | none => s @[inline] def orelseFn (p q : ParserFn) : ParserFn := orelseFnCore p q true @[noinline] def orelseInfo (p q : ParserInfo) : ParserInfo := { collectTokens := p.collectTokens ∘ q.collectTokens, collectKinds := p.collectKinds ∘ q.collectKinds, firstTokens := p.firstTokens.merge q.firstTokens } /-- Run `p`, falling back to `q` if `p` failed without consuming any input. NOTE: In order for the pretty printer to retrace an `orelse`, `p` must be a call to `node` or some other parser producing a single node kind. Nested `orelse` calls are flattened for this, i.e. `(node k1 p1 <|> node k2 p2) <|> ...` is fine as well. -/ @[inline] def orelse (p q : Parser) : Parser := { info := orelseInfo p.info q.info, fn := orelseFn p.fn q.fn } instance : OrElse Parser := ⟨orelse⟩ @[noinline] def noFirstTokenInfo (info : ParserInfo) : ParserInfo := { collectTokens := info.collectTokens, collectKinds := info.collectKinds } def atomicFn (p : ParserFn) : ParserFn := fun c s => let iniPos := s.pos match p c s with | ⟨stack, lhsPrec, _, cache, some msg⟩ => ⟨stack, lhsPrec, iniPos, cache, some msg⟩ | other => other @[inline] def atomic (p : Parser) : Parser := { info := p.info, fn := atomicFn p.fn } def optionalFn (p : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize let iniPos := s.pos let s := p c s let s := if s.hasError && s.pos == iniPos then s.restore iniSz iniPos else s s.mkNode nullKind iniSz @[noinline] def optionaInfo (p : ParserInfo) : ParserInfo := { collectTokens := p.collectTokens, collectKinds := p.collectKinds, firstTokens := p.firstTokens.toOptional } @[inline] def optionalNoAntiquot (p : Parser) : Parser := { info := optionaInfo p.info, fn := optionalFn p.fn } def lookaheadFn (p : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize let iniPos := s.pos let s := p c s if s.hasError then s else s.restore iniSz iniPos @[inline] def lookahead (p : Parser) : Parser := { info := p.info, fn := lookaheadFn p.fn } def notFollowedByFn (p : ParserFn) (msg : String) : ParserFn := fun c s => let iniSz := s.stackSize let iniPos := s.pos let s := p c s if s.hasError then s.restore iniSz iniPos else let s := s.restore iniSz iniPos s.mkUnexpectedError s!"unexpected {msg}" @[inline] def notFollowedBy (p : Parser) (msg : String) : Parser := { fn := notFollowedByFn p.fn msg } partial def manyAux (p : ParserFn) : ParserFn := fun c s => do let iniSz := s.stackSize let iniPos := s.pos let mut s := p c s if s.hasError then return if iniPos == s.pos then s.restore iniSz iniPos else s if iniPos == s.pos then return s.mkUnexpectedError "invalid 'many' parser combinator application, parser did not consume anything" if s.stackSize > iniSz + 1 then s := s.mkNode nullKind iniSz manyAux p c s @[inline] def manyFn (p : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize let s := manyAux p c s s.mkNode nullKind iniSz @[inline] def manyNoAntiquot (p : Parser) : Parser := { info := noFirstTokenInfo p.info, fn := manyFn p.fn } @[inline] def many1Fn (p : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize let s := andthenFn p (manyAux p) c s s.mkNode nullKind iniSz @[inline] def many1NoAntiquot (p : Parser) : Parser := { info := p.info, fn := many1Fn p.fn } private partial def sepByFnAux (p : ParserFn) (sep : ParserFn) (allowTrailingSep : Bool) (iniSz : Nat) (pOpt : Bool) : ParserFn := let rec parse (pOpt : Bool) (c s) := do let sz := s.stackSize let pos := s.pos let mut s := p c s if s.hasError then if s.pos > pos then return s.mkNode nullKind iniSz else if pOpt then s := s.restore sz pos return s.mkNode nullKind iniSz else -- append `Syntax.missing` to make clear that List is incomplete s := s.pushSyntax Syntax.missing return s.mkNode nullKind iniSz if s.stackSize > sz + 1 then s := s.mkNode nullKind sz let sz := s.stackSize let pos := s.pos s := sep c s if s.hasError then s := s.restore sz pos return s.mkNode nullKind iniSz if s.stackSize > sz + 1 then s := s.mkNode nullKind sz parse allowTrailingSep c s parse pOpt def sepByFn (allowTrailingSep : Bool) (p : ParserFn) (sep : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize sepByFnAux p sep allowTrailingSep iniSz true c s def sepBy1Fn (allowTrailingSep : Bool) (p : ParserFn) (sep : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize sepByFnAux p sep allowTrailingSep iniSz false c s @[noinline] def sepByInfo (p sep : ParserInfo) : ParserInfo := { collectTokens := p.collectTokens ∘ sep.collectTokens, collectKinds := p.collectKinds ∘ sep.collectKinds } @[noinline] def sepBy1Info (p sep : ParserInfo) : ParserInfo := { collectTokens := p.collectTokens ∘ sep.collectTokens, collectKinds := p.collectKinds ∘ sep.collectKinds, firstTokens := p.firstTokens } @[inline] def sepByNoAntiquot (p sep : Parser) (allowTrailingSep : Bool := false) : Parser := { info := sepByInfo p.info sep.info, fn := sepByFn allowTrailingSep p.fn sep.fn } @[inline] def sepBy1NoAntiquot (p sep : Parser) (allowTrailingSep : Bool := false) : Parser := { info := sepBy1Info p.info sep.info, fn := sepBy1Fn allowTrailingSep p.fn sep.fn } /- Apply `f` to the syntax object produced by `p` -/ def withResultOfFn (p : ParserFn) (f : Syntax → Syntax) : ParserFn := fun c s => let s := p c s if s.hasError then s else let stx := s.stxStack.back s.popSyntax.pushSyntax (f stx) @[noinline] def withResultOfInfo (p : ParserInfo) : ParserInfo := { collectTokens := p.collectTokens, collectKinds := p.collectKinds } @[inline] def withResultOf (p : Parser) (f : Syntax → Syntax) : Parser := { info := withResultOfInfo p.info, fn := withResultOfFn p.fn f } @[inline] def many1Unbox (p : Parser) : Parser := withResultOf (many1NoAntiquot p) fun stx => if stx.getNumArgs == 1 then stx.getArg 0 else stx partial def satisfyFn (p : Char → Bool) (errorMsg : String := "unexpected character") : ParserFn := fun c s => let i := s.pos if c.input.atEnd i then s.mkEOIError else if p (c.input.get i) then s.next c.input i else s.mkUnexpectedError errorMsg partial def takeUntilFn (p : Char → Bool) : ParserFn := fun c s => let i := s.pos if c.input.atEnd i then s else if p (c.input.get i) then s else takeUntilFn p c (s.next c.input i) def takeWhileFn (p : Char → Bool) : ParserFn := takeUntilFn (fun c => !p c) @[inline] def takeWhile1Fn (p : Char → Bool) (errorMsg : String) : ParserFn := andthenFn (satisfyFn p errorMsg) (takeWhileFn p) variable (startPos : String.Pos) in partial def finishCommentBlock (nesting : Nat) : ParserFn := fun c s => let input := c.input let i := s.pos if input.atEnd i then eoi s else let curr := input.get i let i := input.next i if curr == '-' then if input.atEnd i then eoi s else let curr := input.get i if curr == '/' then -- "-/" end of comment if nesting == 1 then s.next input i else finishCommentBlock (nesting-1) c (s.next input i) else finishCommentBlock nesting c (s.next input i) else if curr == '/' then if input.atEnd i then eoi s else let curr := input.get i if curr == '-' then finishCommentBlock (nesting+1) c (s.next input i) else finishCommentBlock nesting c (s.setPos i) else finishCommentBlock nesting c (s.setPos i) where eoi s := s.mkUnexpectedErrorAt "unterminated comment" startPos /- Consume whitespace and comments -/ partial def whitespace : ParserFn := fun c s => let input := c.input let i := s.pos if input.atEnd i then s else let curr := input.get i if curr == '\t' then s.mkUnexpectedError "tabs are not allowed; please configure your editor to expand them" else if curr.isWhitespace then whitespace c (s.next input i) else if curr == '-' then let i := input.next i let curr := input.get i if curr == '-' then andthenFn (takeUntilFn (fun c => c = '\n')) whitespace c (s.next input i) else s else if curr == '/' then let startPos := i let i := input.next i let curr := input.get i if curr == '-' then let i := input.next i let curr := input.get i if curr == '-' then s -- "/--" doc comment is an actual token else andthenFn (finishCommentBlock startPos 1) whitespace c (s.next input i) else s else s def mkEmptySubstringAt (s : String) (p : Nat) : Substring := { str := s, startPos := p, stopPos := p } private def rawAux (startPos : Nat) (trailingWs : Bool) : ParserFn := fun c s => let input := c.input let stopPos := s.pos let leading := mkEmptySubstringAt input startPos let val := input.extract startPos stopPos if trailingWs then let s := whitespace c s let stopPos' := s.pos let trailing := { str := input, startPos := stopPos, stopPos := stopPos' : Substring } let atom := mkAtom (SourceInfo.original leading startPos trailing (startPos + val.bsize)) val s.pushSyntax atom else let trailing := mkEmptySubstringAt input stopPos let atom := mkAtom (SourceInfo.original leading startPos trailing (startPos + val.bsize)) val s.pushSyntax atom /-- Match an arbitrary Parser and return the consumed String in a `Syntax.atom`. -/ @[inline] def rawFn (p : ParserFn) (trailingWs := false) : ParserFn := fun c s => let startPos := s.pos let s := p c s if s.hasError then s else rawAux startPos trailingWs c s @[inline] def chFn (c : Char) (trailingWs := false) : ParserFn := rawFn (satisfyFn (fun d => c == d) ("'" ++ toString c ++ "'")) trailingWs def rawCh (c : Char) (trailingWs := false) : Parser := { fn := chFn c trailingWs } def hexDigitFn : ParserFn := fun c s => let input := c.input let i := s.pos if input.atEnd i then s.mkEOIError else let curr := input.get i let i := input.next i if curr.isDigit || ('a' <= curr && curr <= 'f') || ('A' <= curr && curr <= 'F') then s.setPos i else s.mkUnexpectedError "invalid hexadecimal numeral" def quotedCharCoreFn (isQuotable : Char → Bool) : ParserFn := fun c s => let input := c.input let i := s.pos if input.atEnd i then s.mkEOIError else let curr := input.get i if isQuotable curr then s.next input i else if curr == 'x' then andthenFn hexDigitFn hexDigitFn c (s.next input i) else if curr == 'u' then andthenFn hexDigitFn (andthenFn hexDigitFn (andthenFn hexDigitFn hexDigitFn)) c (s.next input i) else s.mkUnexpectedError "invalid escape sequence" def isQuotableCharDefault (c : Char) : Bool := c == '\\' || c == '\"' || c == '\'' || c == 'r' || c == 'n' || c == 't' def quotedCharFn : ParserFn := quotedCharCoreFn isQuotableCharDefault /-- Push `(Syntax.node tk <new-atom>)` into syntax stack -/ def mkNodeToken (n : SyntaxNodeKind) (startPos : Nat) : ParserFn := fun c s => let input := c.input let stopPos := s.pos let leading := mkEmptySubstringAt input startPos let val := input.extract startPos stopPos let s := whitespace c s let wsStopPos := s.pos let trailing := { str := input, startPos := stopPos, stopPos := wsStopPos : Substring } let info := SourceInfo.original leading startPos trailing stopPos s.pushSyntax (Syntax.mkLit n val info) def charLitFnAux (startPos : Nat) : ParserFn := fun c s => let input := c.input let i := s.pos if input.atEnd i then s.mkEOIError else let curr := input.get i let s := s.setPos (input.next i) let s := if curr == '\\' then quotedCharFn c s else s if s.hasError then s else let i := s.pos let curr := input.get i let s := s.setPos (input.next i) if curr == '\'' then mkNodeToken charLitKind startPos c s else s.mkUnexpectedError "missing end of character literal" partial def strLitFnAux (startPos : Nat) : ParserFn := fun c s => let input := c.input let i := s.pos if input.atEnd i then s.mkUnexpectedErrorAt "unterminated string literal" startPos else let curr := input.get i let s := s.setPos (input.next i) if curr == '\"' then mkNodeToken strLitKind startPos c s else if curr == '\\' then andthenFn quotedCharFn (strLitFnAux startPos) c s else strLitFnAux startPos c s def decimalNumberFn (startPos : Nat) (c : ParserContext) : ParserState → ParserState := fun s => let s := takeWhileFn (fun c => c.isDigit) c s let input := c.input let i := s.pos let curr := input.get i if curr == '.' || curr == 'e' || curr == 'E' then let s := parseOptDot s let s := parseOptExp s mkNodeToken scientificLitKind startPos c s else mkNodeToken numLitKind startPos c s where parseOptDot s := let input := c.input let i := s.pos let curr := input.get i if curr == '.' then let i := input.next i let curr := input.get i if curr.isDigit then takeWhileFn (fun c => c.isDigit) c (s.setPos i) else s.setPos i else s parseOptExp s := let input := c.input let i := s.pos let curr := input.get i if curr == 'e' || curr == 'E' then let i := input.next i let i := if input.get i == '-' then input.next i else i let curr := input.get i if curr.isDigit then takeWhileFn (fun c => c.isDigit) c (s.setPos i) else s.setPos i else s def binNumberFn (startPos : Nat) : ParserFn := fun c s => let s := takeWhile1Fn (fun c => c == '0' || c == '1') "binary number" c s mkNodeToken numLitKind startPos c s def octalNumberFn (startPos : Nat) : ParserFn := fun c s => let s := takeWhile1Fn (fun c => '0' ≤ c && c ≤ '7') "octal number" c s mkNodeToken numLitKind startPos c s def hexNumberFn (startPos : Nat) : ParserFn := fun c s => let s := takeWhile1Fn (fun c => ('0' ≤ c && c ≤ '9') || ('a' ≤ c && c ≤ 'f') || ('A' ≤ c && c ≤ 'F')) "hexadecimal number" c s mkNodeToken numLitKind startPos c s def numberFnAux : ParserFn := fun c s => let input := c.input let startPos := s.pos if input.atEnd startPos then s.mkEOIError else let curr := input.get startPos if curr == '0' then let i := input.next startPos let curr := input.get i if curr == 'b' || curr == 'B' then binNumberFn startPos c (s.next input i) else if curr == 'o' || curr == 'O' then octalNumberFn startPos c (s.next input i) else if curr == 'x' || curr == 'X' then hexNumberFn startPos c (s.next input i) else decimalNumberFn startPos c (s.setPos i) else if curr.isDigit then decimalNumberFn startPos c (s.next input startPos) else s.mkError "numeral" def isIdCont : String → ParserState → Bool := fun input s => let i := s.pos let curr := input.get i if curr == '.' then let i := input.next i if input.atEnd i then false else let curr := input.get i isIdFirst curr || isIdBeginEscape curr else false private def isToken (idStartPos idStopPos : Nat) (tk : Option Token) : Bool := match tk with | none => false | some tk => -- if a token is both a symbol and a valid identifier (i.e. a keyword), -- we want it to be recognized as a symbol tk.bsize ≥ idStopPos - idStartPos def mkTokenAndFixPos (startPos : Nat) (tk : Option Token) : ParserFn := fun c s => match tk with | none => s.mkErrorAt "token" startPos | some tk => if c.forbiddenTk? == some tk then s.mkErrorAt "forbidden token" startPos else let input := c.input let leading := mkEmptySubstringAt input startPos let stopPos := startPos + tk.bsize let s := s.setPos stopPos let s := whitespace c s let wsStopPos := s.pos let trailing := { str := input, startPos := stopPos, stopPos := wsStopPos : Substring } let atom := mkAtom (SourceInfo.original leading startPos trailing stopPos) tk s.pushSyntax atom def mkIdResult (startPos : Nat) (tk : Option Token) (val : Name) : ParserFn := fun c s => let stopPos := s.pos if isToken startPos stopPos tk then mkTokenAndFixPos startPos tk c s else let input := c.input let rawVal := { str := input, startPos := startPos, stopPos := stopPos : Substring } let s := whitespace c s let trailingStopPos := s.pos let leading := mkEmptySubstringAt input startPos let trailing := { str := input, startPos := stopPos, stopPos := trailingStopPos : Substring } let info := SourceInfo.original leading startPos trailing stopPos let atom := mkIdent info rawVal val s.pushSyntax atom partial def identFnAux (startPos : Nat) (tk : Option Token) (r : Name) : ParserFn := let rec parse (r : Name) (c s) := do let input := c.input let i := s.pos if input.atEnd i then return s.mkEOIError let curr := input.get i if isIdBeginEscape curr then let startPart := input.next i let s := takeUntilFn isIdEndEscape c (s.setPos startPart) if input.atEnd s.pos then return s.mkUnexpectedErrorAt "unterminated identifier escape" startPart let stopPart := s.pos let s := s.next c.input s.pos let r := Name.mkStr r (input.extract startPart stopPart) if isIdCont input s then let s := s.next input s.pos parse r c s else mkIdResult startPos tk r c s else if isIdFirst curr then let startPart := i let s := takeWhileFn isIdRest c (s.next input i) let stopPart := s.pos let r := Name.mkStr r (input.extract startPart stopPart) if isIdCont input s then let s := s.next input s.pos parse r c s else mkIdResult startPos tk r c s else mkTokenAndFixPos startPos tk c s parse r private def isIdFirstOrBeginEscape (c : Char) : Bool := isIdFirst c || isIdBeginEscape c private def nameLitAux (startPos : Nat) : ParserFn := fun c s => let input := c.input let s := identFnAux startPos none Name.anonymous c (s.next input startPos) if s.hasError then s else let stx := s.stxStack.back match stx with | Syntax.ident info rawStr _ _ => let s := s.popSyntax s.pushSyntax (Syntax.mkNameLit rawStr.toString info) | _ => s.mkError "invalid Name literal" private def tokenFnAux : ParserFn := fun c s => let input := c.input let i := s.pos let curr := input.get i if curr == '\"' then strLitFnAux i c (s.next input i) else if curr == '\'' then charLitFnAux i c (s.next input i) else if curr.isDigit then numberFnAux c s else if curr == '`' && isIdFirstOrBeginEscape (getNext input i) then nameLitAux i c s else let (_, tk) := c.tokens.matchPrefix input i identFnAux i tk Name.anonymous c s private def updateCache (startPos : Nat) (s : ParserState) : ParserState := -- do not cache token parsing errors, which are rare and usually fatal and thus not worth an extra field in `TokenCache` match s with | ⟨stack, lhsPrec, pos, cache, none⟩ => if stack.size == 0 then s else let tk := stack.back ⟨stack, lhsPrec, pos, { tokenCache := { startPos := startPos, stopPos := pos, token := tk } }, none⟩ | other => other def tokenFn (expected : List String := []) : ParserFn := fun c s => let input := c.input let i := s.pos if input.atEnd i then s.mkEOIError expected else let tkc := s.cache.tokenCache if tkc.startPos == i then let s := s.pushSyntax tkc.token s.setPos tkc.stopPos else let s := tokenFnAux c s updateCache i s def peekTokenAux (c : ParserContext) (s : ParserState) : ParserState × Except ParserState Syntax := let iniSz := s.stackSize let iniPos := s.pos let s := tokenFn [] c s if let some e := s.errorMsg then (s.restore iniSz iniPos, Except.error s) else let stx := s.stxStack.back (s.restore iniSz iniPos, Except.ok stx) def peekToken (c : ParserContext) (s : ParserState) : ParserState × Except ParserState Syntax := let tkc := s.cache.tokenCache if tkc.startPos == s.pos then (s, Except.ok tkc.token) else peekTokenAux c s /- Treat keywords as identifiers. -/ def rawIdentFn : ParserFn := fun c s => let input := c.input let i := s.pos if input.atEnd i then s.mkEOIError else identFnAux i none Name.anonymous c s @[inline] def satisfySymbolFn (p : String → Bool) (expected : List String) : ParserFn := fun c s => let initStackSz := s.stackSize let startPos := s.pos let s := tokenFn expected c s if s.hasError then s else match s.stxStack.back with | Syntax.atom _ sym => if p sym then s else s.mkErrorsAt expected startPos initStackSz | _ => s.mkErrorsAt expected startPos initStackSz def symbolFnAux (sym : String) (errorMsg : String) : ParserFn := satisfySymbolFn (fun s => s == sym) [errorMsg] def symbolInfo (sym : String) : ParserInfo := { collectTokens := fun tks => sym :: tks, firstTokens := FirstTokens.tokens [ sym ] } @[inline] def symbolFn (sym : String) : ParserFn := symbolFnAux sym ("'" ++ sym ++ "'") @[inline] def symbolNoAntiquot (sym : String) : Parser := let sym := sym.trim { info := symbolInfo sym, fn := symbolFn sym } def checkTailNoWs (prev : Syntax) : Bool := match prev.getTailInfo with | SourceInfo.original _ _ trailing _ => trailing.stopPos == trailing.startPos | _ => false /-- Check if the following token is the symbol _or_ identifier `sym`. Useful for parsing local tokens that have not been added to the token table (but may have been so by some unrelated code). For example, the universe `max` Function is parsed using this combinator so that it can still be used as an identifier outside of universe (but registering it as a token in a Term Syntax would not break the universe Parser). -/ def nonReservedSymbolFnAux (sym : String) (errorMsg : String) : ParserFn := fun c s => let initStackSz := s.stackSize let startPos := s.pos let s := tokenFn [errorMsg] c s if s.hasError then s else match s.stxStack.back with | Syntax.atom _ sym' => if sym == sym' then s else s.mkErrorAt errorMsg startPos initStackSz | Syntax.ident info rawVal _ _ => if sym == rawVal.toString then let s := s.popSyntax s.pushSyntax (Syntax.atom info sym) else s.mkErrorAt errorMsg startPos initStackSz | _ => s.mkErrorAt errorMsg startPos initStackSz @[inline] def nonReservedSymbolFn (sym : String) : ParserFn := nonReservedSymbolFnAux sym ("'" ++ sym ++ "'") def nonReservedSymbolInfo (sym : String) (includeIdent : Bool) : ParserInfo := { firstTokens := if includeIdent then FirstTokens.tokens [ sym, "ident" ] else FirstTokens.tokens [ sym ] } @[inline] def nonReservedSymbolNoAntiquot (sym : String) (includeIdent := false) : Parser := let sym := sym.trim { info := nonReservedSymbolInfo sym includeIdent, fn := nonReservedSymbolFn sym } partial def strAux (sym : String) (errorMsg : String) (j : Nat) :ParserFn := let rec parse (j c s) := if sym.atEnd j then s else let i := s.pos let input := c.input if input.atEnd i || sym.get j != input.get i then s.mkError errorMsg else parse (sym.next j) c (s.next input i) parse j def checkTailWs (prev : Syntax) : Bool := match prev.getTailInfo with | SourceInfo.original _ _ trailing _ => trailing.stopPos > trailing.startPos | _ => false def checkWsBeforeFn (errorMsg : String) : ParserFn := fun c s => let prev := s.stxStack.back if checkTailWs prev then s else s.mkError errorMsg def checkWsBefore (errorMsg : String := "space before") : Parser := { info := epsilonInfo, fn := checkWsBeforeFn errorMsg } def checkTailLinebreak (prev : Syntax) : Bool := match prev.getTailInfo with | SourceInfo.original _ _ trailing _ => trailing.contains '\n' | _ => false def checkLinebreakBeforeFn (errorMsg : String) : ParserFn := fun c s => let prev := s.stxStack.back if checkTailLinebreak prev then s else s.mkError errorMsg def checkLinebreakBefore (errorMsg : String := "line break") : Parser := { info := epsilonInfo fn := checkLinebreakBeforeFn errorMsg } private def pickNonNone (stack : Array Syntax) : Syntax := match stack.findRev? $ fun stx => !stx.isNone with | none => Syntax.missing | some stx => stx def checkNoWsBeforeFn (errorMsg : String) : ParserFn := fun c s => let prev := pickNonNone s.stxStack if checkTailNoWs prev then s else s.mkError errorMsg def checkNoWsBefore (errorMsg : String := "no space before") : Parser := { info := epsilonInfo, fn := checkNoWsBeforeFn errorMsg } def unicodeSymbolFnAux (sym asciiSym : String) (expected : List String) : ParserFn := satisfySymbolFn (fun s => s == sym || s == asciiSym) expected def unicodeSymbolInfo (sym asciiSym : String) : ParserInfo := { collectTokens := fun tks => sym :: asciiSym :: tks, firstTokens := FirstTokens.tokens [ sym, asciiSym ] } @[inline] def unicodeSymbolFn (sym asciiSym : String) : ParserFn := unicodeSymbolFnAux sym asciiSym ["'" ++ sym ++ "', '" ++ asciiSym ++ "'"] @[inline] def unicodeSymbolNoAntiquot (sym asciiSym : String) : Parser := let sym := sym.trim let asciiSym := asciiSym.trim { info := unicodeSymbolInfo sym asciiSym, fn := unicodeSymbolFn sym asciiSym } def mkAtomicInfo (k : String) : ParserInfo := { firstTokens := FirstTokens.tokens [ k ] } def numLitFn : ParserFn := fun c s => let initStackSz := s.stackSize let iniPos := s.pos let s := tokenFn ["numeral"] c s if !s.hasError && !(s.stxStack.back.isOfKind numLitKind) then s.mkErrorAt "numeral" iniPos initStackSz else s @[inline] def numLitNoAntiquot : Parser := { fn := numLitFn, info := mkAtomicInfo "numLit" } def scientificLitFn : ParserFn := fun c s => let initStackSz := s.stackSize let iniPos := s.pos let s := tokenFn ["scientific number"] c s if !s.hasError && !(s.stxStack.back.isOfKind scientificLitKind) then s.mkErrorAt "scientific number" iniPos initStackSz else s @[inline] def scientificLitNoAntiquot : Parser := { fn := scientificLitFn, info := mkAtomicInfo "scientificLit" } def strLitFn : ParserFn := fun c s => let initStackSz := s.stackSize let iniPos := s.pos let s := tokenFn ["string literal"] c s if !s.hasError && !(s.stxStack.back.isOfKind strLitKind) then s.mkErrorAt "string literal" iniPos initStackSz else s @[inline] def strLitNoAntiquot : Parser := { fn := strLitFn, info := mkAtomicInfo "strLit" } def charLitFn : ParserFn := fun c s => let initStackSz := s.stackSize let iniPos := s.pos let s := tokenFn ["char literal"] c s if !s.hasError && !(s.stxStack.back.isOfKind charLitKind) then s.mkErrorAt "character literal" iniPos initStackSz else s @[inline] def charLitNoAntiquot : Parser := { fn := charLitFn, info := mkAtomicInfo "charLit" } def nameLitFn : ParserFn := fun c s => let initStackSz := s.stackSize let iniPos := s.pos let s := tokenFn ["Name literal"] c s if !s.hasError && !(s.stxStack.back.isOfKind nameLitKind) then s.mkErrorAt "Name literal" iniPos initStackSz else s @[inline] def nameLitNoAntiquot : Parser := { fn := nameLitFn, info := mkAtomicInfo "nameLit" } def identFn : ParserFn := fun c s => let initStackSz := s.stackSize let iniPos := s.pos let s := tokenFn ["identifier"] c s if !s.hasError && !(s.stxStack.back.isIdent) then s.mkErrorAt "identifier" iniPos initStackSz else s @[inline] def identNoAntiquot : Parser := { fn := identFn, info := mkAtomicInfo "ident" } @[inline] def rawIdentNoAntiquot : Parser := { fn := rawIdentFn } def identEqFn (id : Name) : ParserFn := fun c s => let initStackSz := s.stackSize let iniPos := s.pos let s := tokenFn ["identifier"] c s if s.hasError then s else match s.stxStack.back with | Syntax.ident _ _ val _ => if val != id then s.mkErrorAt ("expected identifier '" ++ toString id ++ "'") iniPos initStackSz else s | _ => s.mkErrorAt "identifier" iniPos initStackSz @[inline] def identEq (id : Name) : Parser := { fn := identEqFn id, info := mkAtomicInfo "ident" } namespace ParserState def keepTop (s : Array Syntax) (startStackSize : Nat) : Array Syntax := let node := s.back s.shrink startStackSize |>.push node def keepNewError (s : ParserState) (oldStackSize : Nat) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, err⟩ => ⟨keepTop stack oldStackSize, lhsPrec, pos, cache, err⟩ def keepPrevError (s : ParserState) (oldStackSize : Nat) (oldStopPos : String.Pos) (oldError : Option Error) : ParserState := match s with | ⟨stack, lhsPrec, _, cache, _⟩ => ⟨stack.shrink oldStackSize, lhsPrec, oldStopPos, cache, oldError⟩ def mergeErrors (s : ParserState) (oldStackSize : Nat) (oldError : Error) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, some err⟩ => if oldError == err then s else ⟨stack.shrink oldStackSize, lhsPrec, pos, cache, some (oldError.merge err)⟩ | other => other def keepLatest (s : ParserState) (startStackSize : Nat) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, _⟩ => ⟨keepTop stack startStackSize, lhsPrec, pos, cache, none⟩ def replaceLongest (s : ParserState) (startStackSize : Nat) : ParserState := s.keepLatest startStackSize end ParserState def invalidLongestMatchParser (s : ParserState) : ParserState := s.mkError "longestMatch parsers must generate exactly one Syntax node" /-- Auxiliary function used to execute parsers provided to `longestMatchFn`. Push `left?` into the stack if it is not `none`, and execute `p`. Remark: `p` must produce exactly one syntax node. Remark: the `left?` is not none when we are processing trailing parsers. -/ def runLongestMatchParser (left? : Option Syntax) (startLhsPrec : Nat) (p : ParserFn) : ParserFn := fun c s => do /- We assume any registered parser `p` has one of two forms: * a direct call to `leadingParser` or `trailingParser` * a direct call to a (leading) token parser In the first case, we can extract the precedence of the parser by having `leadingParser/trailingParser` set `ParserState.lhsPrec` to it in the very end so that no nested parser can interfere. In the second case, the precedence is effectively `max` (there is a `checkPrec` merely for the convenience of the pretty printer) and there are no nested `leadingParser/trailingParser` calls, so the value of `lhsPrec` will not be changed by the parser (nor will it be read by any leading parser). Thus we initialize the field to `maxPrec` in the leading case. -/ let mut s := { s with lhsPrec := if left?.isSome then startLhsPrec else maxPrec } let startSize := s.stackSize if let some left := left? then s := s.pushSyntax left s := p c s -- stack contains `[..., result ]` if s.stackSize == startSize + 1 then s -- success or error with the expected number of nodes else if s.hasError then -- error with an unexpected number of nodes. s.shrinkStack startSize |>.pushSyntax Syntax.missing else -- parser succeded with incorrect number of nodes invalidLongestMatchParser s def longestMatchStep (left? : Option Syntax) (startSize startLhsPrec : Nat) (startPos : String.Pos) (prevPrio : Nat) (prio : Nat) (p : ParserFn) : ParserContext → ParserState → ParserState × Nat := fun c s => let prevErrorMsg := s.errorMsg let prevStopPos := s.pos let prevSize := s.stackSize let prevLhsPrec := s.lhsPrec let s := s.restore prevSize startPos let s := runLongestMatchParser left? startLhsPrec p c s match prevErrorMsg, s.errorMsg with | none, none => -- both succeeded if s.pos > prevStopPos || (s.pos == prevStopPos && prio > prevPrio) then (s.replaceLongest startSize, prio) else if s.pos < prevStopPos || (s.pos == prevStopPos && prio < prevPrio) then ({ s.restore prevSize prevStopPos with lhsPrec := prevLhsPrec }, prevPrio) -- keep prev -- it is not clear what the precedence of a choice node should be, so we conservatively take the minimum else ({s with lhsPrec := s.lhsPrec.min prevLhsPrec }, prio) | none, some _ => -- prev succeeded, current failed ({ s.restore prevSize prevStopPos with lhsPrec := prevLhsPrec }, prevPrio) | some oldError, some _ => -- both failed if s.pos > prevStopPos || (s.pos == prevStopPos && prio > prevPrio) then (s.keepNewError startSize, prio) else if s.pos < prevStopPos || (s.pos == prevStopPos && prio < prevPrio) then (s.keepPrevError prevSize prevStopPos prevErrorMsg, prevPrio) else (s.mergeErrors prevSize oldError, prio) | some _, none => -- prev failed, current succeeded let successNode := s.stxStack.back let s := s.shrinkStack startSize -- restore stack to initial size to make sure (failure) nodes are removed from the stack (s.pushSyntax successNode, prio) -- put successNode back on the stack def longestMatchMkResult (startSize : Nat) (s : ParserState) : ParserState := if !s.hasError && s.stackSize > startSize + 1 then s.mkNode choiceKind startSize else s def longestMatchFnAux (left? : Option Syntax) (startSize startLhsPrec : Nat) (startPos : String.Pos) (prevPrio : Nat) (ps : List (Parser × Nat)) : ParserFn := let rec parse (prevPrio : Nat) (ps : List (Parser × Nat)) := match ps with | [] => fun _ s => longestMatchMkResult startSize s | p::ps => fun c s => let (s, prevPrio) := longestMatchStep left? startSize startLhsPrec startPos prevPrio p.2 p.1.fn c s parse prevPrio ps c s parse prevPrio ps def longestMatchFn (left? : Option Syntax) : List (Parser × Nat) → ParserFn | [] => fun _ s => s.mkError "longestMatch: empty list" | [p] => fun c s => runLongestMatchParser left? s.lhsPrec p.1.fn c s | p::ps => fun c s => let startSize := s.stackSize let startLhsPrec := s.lhsPrec let startPos := s.pos let s := runLongestMatchParser left? s.lhsPrec p.1.fn c s longestMatchFnAux left? startSize startLhsPrec startPos p.2 ps c s def anyOfFn : List Parser → ParserFn | [], _, s => s.mkError "anyOf: empty list" | [p], c, s => p.fn c s | p::ps, c, s => orelseFn p.fn (anyOfFn ps) c s @[inline] def checkColGeFn (errorMsg : String) : ParserFn := fun c s => match c.savedPos? with | none => s | some savedPos => let savedPos := c.fileMap.toPosition savedPos let pos := c.fileMap.toPosition s.pos if pos.column ≥ savedPos.column then s else s.mkError errorMsg @[inline] def checkColGe (errorMsg : String := "checkColGe") : Parser := { fn := checkColGeFn errorMsg } @[inline] def checkColGtFn (errorMsg : String) : ParserFn := fun c s => match c.savedPos? with | none => s | some savedPos => let savedPos := c.fileMap.toPosition savedPos let pos := c.fileMap.toPosition s.pos if pos.column > savedPos.column then s else s.mkError errorMsg @[inline] def checkColGt (errorMsg : String := "checkColGt") : Parser := { fn := checkColGtFn errorMsg } @[inline] def checkLineEqFn (errorMsg : String) : ParserFn := fun c s => match c.savedPos? with | none => s | some savedPos => let savedPos := c.fileMap.toPosition savedPos let pos := c.fileMap.toPosition s.pos if pos.line == savedPos.line then s else s.mkError errorMsg @[inline] def checkLineEq (errorMsg : String := "checkLineEq") : Parser := { fn := checkLineEqFn errorMsg } @[inline] def withPosition (p : Parser) : Parser := { info := p.info, fn := fun c s => p.fn { c with savedPos? := s.pos } s } @[inline] def withoutPosition (p : Parser) : Parser := { info := p.info, fn := fun c s => let pos := c.fileMap.toPosition s.pos p.fn { c with savedPos? := none } s } @[inline] def withForbidden (tk : Token) (p : Parser) : Parser := { info := p.info, fn := fun c s => p.fn { c with forbiddenTk? := tk } s } @[inline] def withoutForbidden (p : Parser) : Parser := { info := p.info, fn := fun c s => p.fn { c with forbiddenTk? := none } s } def eoiFn : ParserFn := fun c s => let i := s.pos if c.input.atEnd i then s else s.mkError "expected end of file" @[inline] def eoi : Parser := { fn := eoiFn } open Std (RBMap RBMap.empty) /-- A multimap indexed by tokens. Used for indexing parsers by their leading token. -/ def TokenMap (α : Type) := RBMap Name (List α) Name.quickCmp namespace TokenMap def insert (map : TokenMap α) (k : Name) (v : α) : TokenMap α := match map.find? k with | none => Std.RBMap.insert map k [v] | some vs => Std.RBMap.insert map k (v::vs) instance : Inhabited (TokenMap α) := ⟨RBMap.empty⟩ instance : EmptyCollection (TokenMap α) := ⟨RBMap.empty⟩ end TokenMap structure PrattParsingTables where leadingTable : TokenMap (Parser × Nat) := {} leadingParsers : List (Parser × Nat) := [] -- for supporting parsers we cannot obtain first token trailingTable : TokenMap (Parser × Nat) := {} trailingParsers : List (Parser × Nat) := [] -- for supporting parsers such as function application instance : Inhabited PrattParsingTables := ⟨{}⟩ /- The type `leadingIdentBehavior` specifies how the parsing table lookup function behaves for identifiers. The function `prattParser` uses two tables `leadingTable` and `trailingTable`. They map tokens to parsers. - `LeadingIdentBehavior.default`: if the leading token is an identifier, then `prattParser` just executes the parsers associated with the auxiliary token "ident". - `LeadingIdentBehavior.symbol`: if the leading token is an identifier `<foo>`, and there are parsers `P` associated with the toek `<foo>`, then it executes `P`. Otherwise, it executes only the parsers associated with the auxiliary token "ident". - `LeadingIdentBehavior.both`: if the leading token an identifier `<foo>`, the it executes the parsers associated with token `<foo>` and parsers associated with the auxiliary token "ident". We use `LeadingIdentBehavior.symbol` and `LeadingIdentBehavior.both` and `nonReservedSymbol` parser to implement the `tactic` parsers. The idea is to avoid creating a reserved symbol for each builtin tactic (e.g., `apply`, `assumption`, etc.). That is, users may still use these symbols as identifiers (e.g., naming a function). -/ inductive LeadingIdentBehavior where | default | symbol | both deriving Inhabited, BEq /-- Each parser category is implemented using a Pratt's parser. The system comes equipped with the following categories: `level`, `term`, `tactic`, and `command`. Users and plugins may define extra categories. The method ``` categoryParser `term prec ``` executes the Pratt's parser for category `term` with precedence `prec`. That is, only parsers with precedence at least `prec` are considered. The method `termParser prec` is equivalent to the method above. -/ structure ParserCategory where tables : PrattParsingTables behavior : LeadingIdentBehavior deriving Inhabited abbrev ParserCategories := Std.PersistentHashMap Name ParserCategory def indexed {α : Type} (map : TokenMap α) (c : ParserContext) (s : ParserState) (behavior : LeadingIdentBehavior) : ParserState × List α := let (s, stx) := peekToken c s let find (n : Name) : ParserState × List α := match map.find? n with | some as => (s, as) | _ => (s, []) match stx with | Except.ok (Syntax.atom _ sym) => find (Name.mkSimple sym) | Except.ok (Syntax.ident _ _ val _) => match behavior with | LeadingIdentBehavior.default => find identKind | LeadingIdentBehavior.symbol => match map.find? val with | some as => (s, as) | none => find identKind | LeadingIdentBehavior.both => match map.find? val with | some as => match map.find? identKind with | some as' => (s, as ++ as') | _ => (s, as) | none => find identKind | Except.ok (Syntax.node k _) => find k | Except.ok _ => (s, []) | Except.error s' => (s', []) abbrev CategoryParserFn := Name → ParserFn builtin_initialize categoryParserFnRef : IO.Ref CategoryParserFn ← IO.mkRef fun _ => whitespace builtin_initialize categoryParserFnExtension : EnvExtension CategoryParserFn ← registerEnvExtension $ categoryParserFnRef.get def categoryParserFn (catName : Name) : ParserFn := fun ctx s => categoryParserFnExtension.getState ctx.env catName ctx s def categoryParser (catName : Name) (prec : Nat) : Parser := { fn := fun c s => categoryParserFn catName { c with prec := prec } s } -- Define `termParser` here because we need it for antiquotations @[inline] def termParser (prec : Nat := 0) : Parser := categoryParser `term prec /- ============== -/ /- Antiquotations -/ /- ============== -/ /-- Fail if previous token is immediately followed by ':'. -/ def checkNoImmediateColon : Parser := { fn := fun c s => let prev := s.stxStack.back if checkTailNoWs prev then let input := c.input let i := s.pos if input.atEnd i then s else let curr := input.get i if curr == ':' then s.mkUnexpectedError "unexpected ':'" else s else s } def setExpectedFn (expected : List String) (p : ParserFn) : ParserFn := fun c s => match p c s with | s'@{ errorMsg := some msg, .. } => { s' with errorMsg := some { msg with expected := [] } } | s' => s' def setExpected (expected : List String) (p : Parser) : Parser := { fn := setExpectedFn expected p.fn, info := p.info } def pushNone : Parser := { fn := fun c s => s.pushSyntax mkNullNode } -- We support two kinds of antiquotations: `$id` and `$(t)`, where `id` is a term identifier and `t` is a term. def antiquotNestedExpr : Parser := node `antiquotNestedExpr (symbolNoAntiquot "(" >> decQuotDepth termParser >> symbolNoAntiquot ")") def antiquotExpr : Parser := identNoAntiquot <|> antiquotNestedExpr @[inline] def tokenWithAntiquotFn (p : ParserFn) : ParserFn := fun c s => do let s := p c s if s.hasError || c.quotDepth == 0 then return s let iniSz := s.stackSize let iniPos := s.pos let s := (checkNoWsBefore >> symbolNoAntiquot "%" >> symbolNoAntiquot "$" >> checkNoWsBefore >> antiquotExpr).fn c s if s.hasError then return s.restore iniSz iniPos s.mkNode (`token_antiquot) (iniSz - 1) @[inline] def tokenWithAntiquot (p : Parser) : Parser where fn := tokenWithAntiquotFn p.fn info := p.info @[inline] def symbol (sym : String) : Parser := tokenWithAntiquot (symbolNoAntiquot sym) instance : Coe String Parser := ⟨fun s => symbol s ⟩ @[inline] def nonReservedSymbol (sym : String) (includeIdent := false) : Parser := tokenWithAntiquot (nonReservedSymbolNoAntiquot sym includeIdent) @[inline] def unicodeSymbol (sym asciiSym : String) : Parser := tokenWithAntiquot (unicodeSymbolNoAntiquot sym asciiSym) /-- Define parser for `$e` (if anonymous == true) and `$e:name`. Both forms can also be used with an appended `*` to turn them into an antiquotation "splice". If `kind` is given, it will additionally be checked when evaluating `match_syntax`. Antiquotations can be escaped as in `$$e`, which produces the syntax tree for `$e`. -/ def mkAntiquot (name : String) (kind : Option SyntaxNodeKind) (anonymous := true) : Parser := let kind := (kind.getD Name.anonymous) ++ `antiquot let nameP := node `antiquotName $ checkNoWsBefore ("no space before ':" ++ name ++ "'") >> symbol ":" >> nonReservedSymbol name -- if parsing the kind fails and `anonymous` is true, check that we're not ignoring a different -- antiquotation kind via `noImmediateColon` let nameP := if anonymous then nameP <|> checkNoImmediateColon >> pushNone else nameP -- antiquotations are not part of the "standard" syntax, so hide "expected '$'" on error leadingNode kind maxPrec $ atomic $ setExpected [] "$" >> manyNoAntiquot (checkNoWsBefore "" >> "$") >> checkNoWsBefore "no space before spliced term" >> antiquotExpr >> nameP def tryAnti (c : ParserContext) (s : ParserState) : Bool := do if c.quotDepth == 0 then return false let (s, stx) := peekToken c s match stx with | Except.ok stx@(Syntax.atom _ sym) => sym == "$" | _ => false @[inline] def withAntiquotFn (antiquotP p : ParserFn) : ParserFn := fun c s => if tryAnti c s then orelseFn antiquotP p c s else p c s /-- Optimized version of `mkAntiquot ... <|> p`. -/ @[inline] def withAntiquot (antiquotP p : Parser) : Parser := { fn := withAntiquotFn antiquotP.fn p.fn, info := orelseInfo antiquotP.info p.info } def withoutInfo (p : Parser) : Parser := { fn := p.fn } /-- Parse `$[p]suffix`, e.g. `$[p],*`. -/ def mkAntiquotSplice (kind : SyntaxNodeKind) (p suffix : Parser) : Parser := let kind := kind ++ `antiquot_scope leadingNode kind maxPrec $ atomic $ setExpected [] "$" >> manyNoAntiquot (checkNoWsBefore "" >> "$") >> checkNoWsBefore "no space before spliced term" >> symbol "[" >> node nullKind p >> symbol "]" >> suffix @[inline] def withAntiquotSuffixSpliceFn (kind : SyntaxNodeKind) (p suffix : ParserFn) : ParserFn := fun c s => do let s := p c s if s.hasError || c.quotDepth == 0 || !s.stxStack.back.isAntiquot then return s let iniSz := s.stackSize let iniPos := s.pos let s := suffix c s if s.hasError then return s.restore iniSz iniPos s.mkNode (kind ++ `antiquot_suffix_splice) (s.stxStack.size - 2) /-- Parse `suffix` after an antiquotation, e.g. `$x,*`, and put both into a new node. -/ @[inline] def withAntiquotSuffixSplice (kind : SyntaxNodeKind) (p suffix : Parser) : Parser := { info := andthenInfo p.info suffix.info, fn := withAntiquotSuffixSpliceFn kind p.fn suffix.fn } def withAntiquotSpliceAndSuffix (kind : SyntaxNodeKind) (p suffix : Parser) := -- prevent `p`'s info from being collected twice withAntiquot (mkAntiquotSplice kind (withoutInfo p) suffix) (withAntiquotSuffixSplice kind p suffix) def nodeWithAntiquot (name : String) (kind : SyntaxNodeKind) (p : Parser) (anonymous := false) : Parser := withAntiquot (mkAntiquot name kind anonymous) $ node kind p /- ===================== -/ /- End of Antiquotations -/ /- ===================== -/ def sepByElemParser (p : Parser) (sep : String) : Parser := withAntiquotSpliceAndSuffix `sepBy p (symbol (sep.trim ++ "*")) def sepBy (p : Parser) (sep : String) (psep : Parser := symbol sep) (allowTrailingSep : Bool := false) : Parser := sepByNoAntiquot (sepByElemParser p sep) psep allowTrailingSep def sepBy1 (p : Parser) (sep : String) (psep : Parser := symbol sep) (allowTrailingSep : Bool := false) : Parser := sepBy1NoAntiquot (sepByElemParser p sep) psep allowTrailingSep def categoryParserOfStackFn (offset : Nat) : ParserFn := fun ctx s => let stack := s.stxStack if stack.size < offset + 1 then s.mkUnexpectedError ("failed to determine parser category using syntax stack, stack is too small") else match stack.get! (stack.size - offset - 1) with | Syntax.ident _ _ catName _ => categoryParserFn catName ctx s | _ => s.mkUnexpectedError ("failed to determine parser category using syntax stack, the specified element on the stack is not an identifier") def categoryParserOfStack (offset : Nat) (prec : Nat := 0) : Parser := { fn := fun c s => categoryParserOfStackFn offset { c with prec := prec } s } unsafe def evalParserConstUnsafe (declName : Name) : ParserFn := fun ctx s => match ctx.env.evalConstCheck Parser ctx.options `Lean.Parser.Parser declName <|> ctx.env.evalConstCheck Parser ctx.options `Lean.Parser.TrailingParser declName with | Except.ok p => p.fn ctx s | Except.error e => s.mkUnexpectedError s!"error running parser {declName}: {e}" @[implementedBy evalParserConstUnsafe] constant evalParserConst (declName : Name) : ParserFn unsafe def parserOfStackFnUnsafe (offset : Nat) : ParserFn := fun ctx s => let stack := s.stxStack if stack.size < offset + 1 then s.mkUnexpectedError ("failed to determine parser using syntax stack, stack is too small") else match stack.get! (stack.size - offset - 1) with | Syntax.ident (val := parserName) .. => match ctx.resolveName parserName with | [(parserName, [])] => let iniSz := s.stackSize let s := evalParserConst parserName ctx s if !s.hasError && s.stackSize != iniSz + 1 then s.mkUnexpectedError "expected parser to return exactly one syntax object" else s | _::_::_ => s.mkUnexpectedError s!"ambiguous parser name {parserName}" | _ => s.mkUnexpectedError s!"unknown parser {parserName}" | _ => s.mkUnexpectedError ("failed to determine parser using syntax stack, the specified element on the stack is not an identifier") @[implementedBy parserOfStackFnUnsafe] constant parserOfStackFn (offset : Nat) : ParserFn def parserOfStack (offset : Nat) (prec : Nat := 0) : Parser := { fn := fun c s => parserOfStackFn offset { c with prec := prec } s } /-- Run `declName` if possible and inside a quotation, or else `p`. The `ParserInfo` will always be taken from `p`. -/ def evalInsideQuot (declName : Name) (p : Parser) : Parser := { p with fn := fun c s => if c.quotDepth > 0 && !c.suppressInsideQuot && c.env.contains declName then evalParserConst declName c s else p.fn c s } private def mkResult (s : ParserState) (iniSz : Nat) : ParserState := if s.stackSize == iniSz + 1 then s else s.mkNode nullKind iniSz -- throw error instead? def leadingParserAux (kind : Name) (tables : PrattParsingTables) (behavior : LeadingIdentBehavior) : ParserFn := fun c s => do let iniSz := s.stackSize let (s, ps) := indexed tables.leadingTable c s behavior if s.hasError then return s let ps := tables.leadingParsers ++ ps if ps.isEmpty then return s.mkError (toString kind) let s := longestMatchFn none ps c s mkResult s iniSz @[inline] def leadingParser (kind : Name) (tables : PrattParsingTables) (behavior : LeadingIdentBehavior) (antiquotParser : ParserFn) : ParserFn := withAntiquotFn antiquotParser (leadingParserAux kind tables behavior) def trailingLoopStep (tables : PrattParsingTables) (left : Syntax) (ps : List (Parser × Nat)) : ParserFn := fun c s => longestMatchFn left (ps ++ tables.trailingParsers) c s partial def trailingLoop (tables : PrattParsingTables) (c : ParserContext) (s : ParserState) : ParserState := do let iniSz := s.stackSize let iniPos := s.pos let (s, ps) := indexed tables.trailingTable c s LeadingIdentBehavior.default if s.hasError then -- Discard token parse errors and break the trailing loop instead. -- The error will be flagged when the next leading position is parsed, unless the token -- is in fact valid there (e.g. EOI at command level, no-longer forbidden token) return s.restore iniSz iniPos if ps.isEmpty && tables.trailingParsers.isEmpty then return s -- no available trailing parser let left := s.stxStack.back let s := s.popSyntax let s := trailingLoopStep tables left ps c s if s.hasError then -- Discard non-consuming parse errors and break the trailing loop instead, restoring `left`. -- This is necessary for fallback parsers like `app` that pretend to be always applicable. return if s.pos == iniPos then s.restore (iniSz - 1) iniPos |>.pushSyntax left else s trailingLoop tables c s /-- Implements a variant of Pratt's algorithm. In Pratt's algorithms tokens have a right and left binding power. In our implementation, parsers have precedence instead. This method selects a parser (or more, via `longestMatchFn`) from `leadingTable` based on the current token. Note that the unindexed `leadingParsers` parsers are also tried. We have the unidexed `leadingParsers` because some parsers do not have a "first token". Example: ``` syntax term:51 "≤" ident "<" term "|" term : index ``` Example, in principle, the set of first tokens for this parser is any token that can start a term, but this set is always changing. Thus, this parsing rule is stored as an unindexed leading parser at `leadingParsers`. After processing the leading parser, we chain with parsers from `trailingTable`/`trailingParsers` that have precedence at least `c.prec` where `c` is the `ParsingContext`. Recall that `c.prec` is set by `categoryParser`. Note that in the original Pratt's algorith, precedences are only checked before calling trailing parsers. In our implementation, leading *and* trailing parsers check the precendece. We claim our algorithm is more flexible, modular and easier to understand. `antiquotParser` should be a `mkAntiquot` parser (or always fail) and is tried before all other parsers. It should not be added to the regular leading parsers because it would heavily overlap with antiquotation parsers nested inside them. -/ @[inline] def prattParser (kind : Name) (tables : PrattParsingTables) (behavior : LeadingIdentBehavior) (antiquotParser : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize let iniPos := s.pos let s := leadingParser kind tables behavior antiquotParser c s if s.hasError then s else trailingLoop tables c s def fieldIdxFn : ParserFn := fun c s => let initStackSz := s.stackSize let iniPos := s.pos let curr := c.input.get iniPos if curr.isDigit && curr != '0' then let s := takeWhileFn (fun c => c.isDigit) c s mkNodeToken fieldIdxKind iniPos c s else s.mkErrorAt "field index" iniPos initStackSz @[inline] def fieldIdx : Parser := withAntiquot (mkAntiquot "fieldIdx" `fieldIdx) { fn := fieldIdxFn, info := mkAtomicInfo "fieldIdx" } @[inline] def skip : Parser := { fn := fun c s => s, info := epsilonInfo } end Parser namespace Syntax section variable {β : Type} {m : Type → Type} [Monad m] @[inline] def foldArgsM (s : Syntax) (f : Syntax → β → m β) (b : β) : m β := s.getArgs.foldlM (flip f) b @[inline] def foldArgs (s : Syntax) (f : Syntax → β → β) (b : β) : β := Id.run (s.foldArgsM f b) @[inline] def forArgsM (s : Syntax) (f : Syntax → m Unit) : m Unit := s.foldArgsM (fun s _ => f s) () end end Syntax end Lean
4a9821eeeeda6705bb25f91f43475e90c097a19b
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/category_theory/monoidal/natural_transformation.lean
1b9787e64a5b6974ad5a15a64babaeb15a1220bb
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
5,036
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.monoidal.functor import category_theory.full_subcategory /-! # Monoidal natural transformations Natural transformations between (lax) monoidal functors must satisfy an additional compatibility relation with the tensorators: `F.μ X Y ≫ app (X ⊗ Y) = (app X ⊗ app Y) ≫ G.μ X Y`. (Lax) monoidal functors between a fixed pair of monoidal categories themselves form a category. -/ open category_theory universes v₁ v₂ v₃ u₁ u₂ u₃ open category_theory.category open category_theory.functor namespace category_theory open monoidal_category variables {C : Type u₁} [category.{v₁} C] [monoidal_category.{v₁} C] {D : Type u₂} [category.{v₂} D] [monoidal_category.{v₂} D] /-- A monoidal natural transformation is a natural transformation between (lax) monoidal functors additionally satisfying: `F.μ X Y ≫ app (X ⊗ Y) = (app X ⊗ app Y) ≫ G.μ X Y` -/ @[ext] structure monoidal_nat_trans (F G : lax_monoidal_functor C D) extends nat_trans F.to_functor G.to_functor := (unit' : F.ε ≫ app (𝟙_ C) = G.ε . obviously) (tensor' : ∀ X Y, F.μ _ _ ≫ app (X ⊗ Y) = (app X ⊗ app Y) ≫ G.μ _ _ . obviously) restate_axiom monoidal_nat_trans.tensor' attribute [simp, reassoc] monoidal_nat_trans.tensor restate_axiom monoidal_nat_trans.unit' attribute [simp, reassoc] monoidal_nat_trans.unit namespace monoidal_nat_trans /-- The identity monoidal natural transformation. -/ @[simps] def id (F : lax_monoidal_functor C D) : monoidal_nat_trans F F := { ..(𝟙 F.to_functor) } instance (F : lax_monoidal_functor C D) : inhabited (monoidal_nat_trans F F) := ⟨id F⟩ /-- Vertical composition of monoidal natural transformations. -/ @[simps] def vcomp {F G H : lax_monoidal_functor C D} (α : monoidal_nat_trans F G) (β : monoidal_nat_trans G H) : monoidal_nat_trans F H := { ..(nat_trans.vcomp α.to_nat_trans β.to_nat_trans) } instance category_lax_monoidal_functor : category (lax_monoidal_functor C D) := { hom := monoidal_nat_trans, id := id, comp := λ F G H α β, vcomp α β, } @[simp] lemma comp_to_nat_trans_lax {F G H : lax_monoidal_functor C D} {α : F ⟶ G} {β : G ⟶ H} : (α ≫ β).to_nat_trans = @category_struct.comp (C ⥤ D) _ _ _ _ (α.to_nat_trans) (β.to_nat_trans) := rfl instance category_monoidal_functor : category (monoidal_functor C D) := induced_category.category monoidal_functor.to_lax_monoidal_functor @[simp] lemma comp_to_nat_trans {F G H : monoidal_functor C D} {α : F ⟶ G} {β : G ⟶ H} : (α ≫ β).to_nat_trans = @category_struct.comp (C ⥤ D) _ _ _ _ (α.to_nat_trans) (β.to_nat_trans) := rfl variables {E : Type u₃} [category.{v₃} E] [monoidal_category.{v₃} E] /-- Horizontal composition of monoidal natural transformations. -/ @[simps] def hcomp {F G : lax_monoidal_functor C D} {H K : lax_monoidal_functor D E} (α : monoidal_nat_trans F G) (β : monoidal_nat_trans H K) : monoidal_nat_trans (F ⊗⋙ H) (G ⊗⋙ K) := { unit' := begin dsimp, simp, conv_lhs { rw [←K.to_functor.map_comp, α.unit], }, end, tensor' := λ X Y, begin dsimp, simp, conv_lhs { rw [←K.to_functor.map_comp, α.tensor, K.to_functor.map_comp], }, end, ..(nat_trans.hcomp α.to_nat_trans β.to_nat_trans) } end monoidal_nat_trans namespace monoidal_nat_iso variables {F G : lax_monoidal_functor C D} /-- Construct a monoidal natural isomorphism from object level isomorphisms, and the monoidal naturality in the forward direction. -/ def of_components (app : ∀ X : C, F.obj X ≅ G.obj X) (naturality : ∀ {X Y : C} (f : X ⟶ Y), F.map f ≫ (app Y).hom = (app X).hom ≫ G.map f) (unit : F.ε ≫ (app (𝟙_ C)).hom = G.ε) (tensor : ∀ X Y, F.μ X Y ≫ (app (X ⊗ Y)).hom = ((app X).hom ⊗ (app Y).hom) ≫ G.μ X Y) : F ≅ G := { hom := { app := λ X, (app X).hom, }, inv := { app := λ X, (app X).inv, unit' := by { dsimp, rw [←unit, assoc, iso.hom_inv_id, comp_id], }, tensor' := λ X Y, begin dsimp, rw [iso.comp_inv_eq, assoc, tensor, ←tensor_comp_assoc, iso.inv_hom_id, iso.inv_hom_id, tensor_id, id_comp], end, ..(nat_iso.of_components app @naturality).inv, }, } @[simp] lemma of_components.hom_app (app : ∀ X : C, F.obj X ≅ G.obj X) (naturality) (unit) (tensor) (X) : (of_components app naturality unit tensor).hom.app X = (app X).hom := rfl @[simp] lemma of_components.inv_app (app : ∀ X : C, F.obj X ≅ G.obj X) (naturality) (unit) (tensor) (X) : (of_components app naturality unit tensor).inv.app X = (app X).inv := by simp [of_components] instance is_iso_of_is_iso_app (α : F ⟶ G) [∀ X : C, is_iso (α.app X)] : is_iso α := ⟨(is_iso.of_iso (of_components (λ X, as_iso (α.app X)) (λ X Y f, α.to_nat_trans.naturality f) α.unit α.tensor)).1⟩ end monoidal_nat_iso end category_theory
45ef9086f177127b122e015f38b39abde64a2a5e
b3fced0f3ff82d577384fe81653e47df68bb2fa1
/src/algebraic_geometry/presheafed_space.lean
ecde18812d14440a9f44e7fdb28eae90b6f0683f
[ "Apache-2.0" ]
permissive
ratmice/mathlib
93b251ef5df08b6fd55074650ff47fdcc41a4c75
3a948a6a4cd5968d60e15ed914b1ad2f4423af8d
refs/heads/master
1,599,240,104,318
1,572,981,183,000
1,572,981,183,000
219,830,178
0
0
Apache-2.0
1,572,980,897,000
1,572,980,896,000
null
UTF-8
Lean
false
false
7,611
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import topology.sheaves.presheaf /-! # Presheafed spaces Introduces the category of topological spaces equipped with a presheaf (taking values in an arbitrary target category `C`.) We further describe how to apply functors and natural transformations to the values of the presheaves. -/ universes v u open category_theory open Top open topological_space open opposite open category_theory.category category_theory.functor variables (C : Type u) [𝒞 : category.{v} C] include 𝒞 local attribute [tidy] tactic.op_induction' namespace algebraic_geometry /-- A `PresheafedSpace C` is a topological space equipped with a presheaf of `C`s. -/ structure PresheafedSpace := (to_Top : Top.{v}) (𝒪 : to_Top.presheaf C) variables {C} namespace PresheafedSpace instance coe_to_Top : has_coe (PresheafedSpace.{v} C) Top := { coe := λ X, X.to_Top } @[simp] lemma as_coe (X : PresheafedSpace.{v} C) : X.to_Top = (X : Top.{v}) := rfl @[simp] lemma mk_coe (to_Top) (𝒪) : (({ to_Top := to_Top, 𝒪 := 𝒪 } : PresheafedSpace.{v} C) : Top.{v}) = to_Top := rfl instance (X : PresheafedSpace.{v} C) : topological_space X := X.to_Top.str /-- A morphism between presheafed spaces `X` and `Y` consists of a continuous map `f` between the underlying topological spaces, and a (notice contravariant!) map from the presheaf on `Y` to the pushforward of the presheaf on `X` via `f`. -/ structure hom (X Y : PresheafedSpace.{v} C) := (f : (X : Top.{v}) ⟶ (Y : Top.{v})) (c : Y.𝒪 ⟶ f _* X.𝒪) @[extensionality] lemma ext {X Y : PresheafedSpace.{v} C} (α β : hom X Y) (w : α.f = β.f) (h : α.c ≫ (whisker_right (nat_trans.op (opens.map_iso _ _ w).inv) X.𝒪) = β.c) : α = β := begin cases α, cases β, dsimp [presheaf.pushforward] at *, tidy, -- TODO including `injections` would make tidy work earlier. end . def id (X : PresheafedSpace.{v} C) : hom X X := { f := 𝟙 (X : Top.{v}), c := ((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id (X.to_Top)).hom) _) } def comp (X Y Z : PresheafedSpace.{v} C) (α : hom X Y) (β : hom Y Z) : hom X Z := { f := α.f ≫ β.f, c := β.c ≫ (whisker_left (opens.map β.f).op α.c) } variables (C) section local attribute [simp] id comp presheaf.pushforward /- The proofs below can be done by `tidy`, but it is too slow, and we don't have a tactic caching mechanism. -/ /-- The category of PresheafedSpaces. Morphisms are pairs, a continuous map and a presheaf map from the presheaf on the target to the pushforward of the presheaf on the source. -/ instance category_of_PresheafedSpaces : category (PresheafedSpace.{v} C) := { hom := hom, id := id, comp := comp, id_comp' := λ X Y f, begin ext1, swap, { dsimp, simp only [id_comp] }, { ext1 U, op_induction, cases U, dsimp, simp only [comp_id, map_id] }, end, comp_id' := λ X Y f, begin ext1, swap, { dsimp, simp only [comp_id] }, { ext1 U, op_induction, cases U, dsimp, simp only [comp_id, id_comp, map_id] } end, assoc' := λ W X Y Z f g h, begin simp only [true_and, presheaf.pushforward, id, comp, whisker_left_twice, whisker_left_comp, heq_iff_eq, category.assoc], split; refl end } end variables {C} instance {X Y : PresheafedSpace.{v} C} : has_coe (X ⟶ Y) (X.to_Top ⟶ Y.to_Top) := { coe := λ α, α.f } @[simp] lemma hom_mk_coe {X Y : PresheafedSpace.{v} C} (f) (c) : (({ f := f, c := c } : X ⟶ Y) : (X : Top.{v}) ⟶ (Y : Top.{v})) = f := rfl @[simp] lemma f_as_coe {X Y : PresheafedSpace.{v} C} (α : X ⟶ Y) : α.f = (α : (X : Top.{v}) ⟶ (Y : Top.{v})) := rfl @[simp] lemma id_coe (X : PresheafedSpace.{v} C) : (((𝟙 X) : X ⟶ X) : (X : Top.{v}) ⟶ X) = 𝟙 (X : Top.{v}) := rfl @[simp] lemma comp_coe {X Y Z : PresheafedSpace.{v} C} (α : X ⟶ Y) (β : Y ⟶ Z) : ((α ≫ β : X ⟶ Z) : (X : Top.{v}) ⟶ Z) = (α : (X : Top.{v}) ⟶ Y) ≫ (β : Y ⟶ Z) := rfl lemma id_c (X : PresheafedSpace.{v} C) : ((𝟙 X) : X ⟶ X).c = (((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id (X.to_Top)).hom) _)) := rfl -- Implementation note: this harmless looking lemma causes deterministic timeouts, -- but happily we can survive without it. -- lemma comp_c {X Y Z : PresheafedSpace.{v} C} (α : X ⟶ Y) (β : Y ⟶ Z) : -- (α ≫ β).c = (β.c ≫ (whisker_left (opens.map β.f).op α.c)) := rfl @[simp] lemma id_c_app (X : PresheafedSpace.{v} C) (U) : ((𝟙 X) : X ⟶ X).c.app U = eq_to_hom (by { op_induction U, cases U, refl }) := by { op_induction U, cases U, simp only [id_c], dsimp, simp, } @[simp] lemma comp_c_app {X Y Z : PresheafedSpace.{v} C} (α : X ⟶ Y) (β : Y ⟶ Z) (U) : (α ≫ β).c.app U = (β.c).app U ≫ (α.c).app (op ((opens.map (β.f)).obj (unop U))) := rfl /-- The forgetful functor from `PresheafedSpace` to `Top`. -/ def forget : PresheafedSpace.{v} C ⥤ Top := { obj := λ X, (X : Top.{v}), map := λ X Y f, f } end PresheafedSpace end algebraic_geometry open algebraic_geometry algebraic_geometry.PresheafedSpace variables {C} namespace category_theory variables {D : Type u} [𝒟 : category.{v} D] include 𝒟 local attribute [simp] presheaf.pushforward namespace functor /-- We can apply a functor `F : C ⥤ D` to the values of the presheaf in any `PresheafedSpace C`, giving a functor `PresheafedSpace C ⥤ PresheafedSpace D` -/ /- The proofs below can be done by `tidy`, but it is too slow, and we don't have a tactic caching mechanism. -/ def map_presheaf (F : C ⥤ D) : PresheafedSpace.{v} C ⥤ PresheafedSpace.{v} D := { obj := λ X, { to_Top := X.to_Top, 𝒪 := X.𝒪 ⋙ F }, map := λ X Y f, { f := f.f, c := whisker_right f.c F }, map_id' := λ X, begin ext1, swap, { refl }, { ext1, dsimp, simp only [presheaf.pushforward, eq_to_hom_map, map_id, comp_id, id_c_app], refl } end, map_comp' := λ X Y Z f g, begin ext1, swap, { refl, }, { ext, dsimp, simp only [comp_id, assoc, map_comp, map_id], }, end } @[simp] lemma map_presheaf_obj_X (F : C ⥤ D) (X : PresheafedSpace.{v} C) : ((F.map_presheaf.obj X) : Top.{v}) = (X : Top.{v}) := rfl @[simp] lemma map_presheaf_obj_𝒪 (F : C ⥤ D) (X : PresheafedSpace.{v} C) : (F.map_presheaf.obj X).𝒪 = X.𝒪 ⋙ F := rfl @[simp] lemma map_presheaf_map_f (F : C ⥤ D) {X Y : PresheafedSpace.{v} C} (f : X ⟶ Y) : ((F.map_presheaf.map f) : (X : Top.{v}) ⟶ (Y : Top.{v})) = f := rfl @[simp] lemma map_presheaf_map_c (F : C ⥤ D) {X Y : PresheafedSpace.{v} C} (f : X ⟶ Y) : (F.map_presheaf.map f).c = whisker_right f.c F := rfl end functor namespace nat_trans /- The proofs below can be done by `tidy`, but it is too slow, and we don't have a tactic caching mechanism. -/ def on_presheaf {F G : C ⥤ D} (α : F ⟶ G) : G.map_presheaf ⟶ F.map_presheaf := { app := λ X, { f := 𝟙 _, c := whisker_left X.𝒪 α ≫ ((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id X.to_Top).hom) _) }, naturality' := λ X Y f, begin ext1, swap, { refl }, { ext1 U, op_induction, cases U, dsimp, simp only [comp_id, assoc, map_id, nat_trans.naturality] } end } -- TODO Assemble the last two constructions into a functor -- `(C ⥤ D) ⥤ (PresheafedSpace C ⥤ PresheafedSpace D)` end nat_trans end category_theory
7bb86a8cce469be52f0b0d2d6c7efe4f9e826cf8
500f65bb93c499cd35c3254d894d762208cae042
/src/tactic/basic.lean
0d1ab73395a61838cb30cb73b2df70364096fe17
[ "Apache-2.0" ]
permissive
PatrickMassot/mathlib
c39dc0ff18bbde42f1c93a1642f6e429adad538c
45df75b3c9da159fe3192fa7f769dfbec0bd6bda
refs/heads/master
1,623,168,646,390
1,566,940,765,000
1,566,940,765,000
115,220,590
0
1
null
1,514,061,524,000
1,514,061,524,000
null
UTF-8
Lean
false
false
392
lean
import tactic.alias tactic.cache tactic.converter.interactive tactic.core tactic.doc_blame tactic.ext tactic.generalize_proofs tactic.interactive tactic.library_search tactic.lift tactic.mk_iff_of_inductive_prop tactic.push_neg tactic.rcases tactic.replacer tactic.restate_axiom tactic.rewrite tactic.simpa tactic.split_ifs tactic.squeeze tactic.where
428cad15fb9f19f3185ac20894d782daaf1588bd
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/linear_algebra/tensor_product_basis.lean
b25b27a330581e4acb093ceb55ace53d269858ff
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
1,493
lean
/- Copyright (c) 2021 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer -/ import linear_algebra.direct_sum.finsupp import linear_algebra.finsupp_vector_space /-! # Bases and dimensionality of tensor products of modules These can not go into `linear_algebra.tensor_product` since they depend on `linear_algebra.finsupp_vector_space` which in turn imports `linear_algebra.tensor_product`. -/ noncomputable theory open set linear_map submodule section comm_ring variables {R : Type*} {M : Type*} {N : Type*} {ι : Type*} {κ : Type*} variables [comm_ring R] [add_comm_group M] [module R M] [add_comm_group N] [module R N] /-- If b : ι → M and c : κ → N are bases then so is λ i, b i.1 ⊗ₜ c i.2 : ι × κ → M ⊗ N. -/ def basis.tensor_product (b : basis ι R M) (c : basis κ R N) : basis (ι × κ) R (tensor_product R M N) := finsupp.basis_single_one.map ((tensor_product.congr b.repr c.repr).trans $ (finsupp_tensor_finsupp R _ _ _ _).trans $ finsupp.lcongr (equiv.refl _) (tensor_product.lid R R)).symm @[simp] lemma basis.tensor_product_apply (b : basis ι R M) (c : basis κ R N) (i : ι) (j : κ) : basis.tensor_product b c (i, j) = b i ⊗ₜ c j := by simp [basis.tensor_product] lemma basis.tensor_product_apply' (b : basis ι R M) (c : basis κ R N) (i : ι × κ) : basis.tensor_product b c i = b i.1 ⊗ₜ c i.2 := by simp [basis.tensor_product] end comm_ring
f38f716f7c1c6b5f3648ae5020d320e359568f92
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/03_Propositions_and_Proofs.org.27.lean
e4cc920073490d65c0e7c11c0b34b1de4dcd3561
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
118
lean
/- page 41 -/ import standard variables p q : Prop -- BEGIN example (Hp : p) (Hnp : ¬p) : q := absurd Hp Hnp -- END
11aef50c1ca47f0e59063d6233b75b9ee387b9de
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/metric_space/baire.lean
6b9127f042a52650caabf46ffc0c3ee41088676d
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
14,045
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.specific_limits import order.filter.countable_Inter import topology.G_delta /-! # Baire theorem In a complete metric space, a countable intersection of dense open subsets is dense. The good concept underlying the theorem is that of a Gδ set, i.e., a countable intersection of open sets. Then Baire theorem can also be formulated as the fact that a countable intersection of dense Gδ sets is a dense Gδ set. We prove Baire theorem, giving several different formulations that can be handy. We also prove the important consequence that, if the space is covered by a countable union of closed sets, then the union of their interiors is dense. The names of the theorems do not contain the string "Baire", but are instead built from the form of the statement. "Baire" is however in the docstring of all the theorems, to facilitate grep searches. We also define the filter `residual α` generated by dense `Gδ` sets and prove that this filter has the countable intersection property. -/ noncomputable theory open_locale classical topological_space filter ennreal open filter encodable set variables {α : Type*} {β : Type*} {γ : Type*} {ι : Type*} section Baire_theorem open emetric ennreal variables [pseudo_emetric_space α] [complete_space α] /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here when the source space is ℕ (and subsumed below by `dense_Inter_of_open` working with any encodable source space). -/ theorem dense_Inter_of_open_nat {f : ℕ → set α} (ho : ∀n, is_open (f n)) (hd : ∀n, dense (f n)) : dense (⋂n, f n) := begin let B : ℕ → ℝ≥0∞ := λn, 1/2^n, have Bpos : ∀n, 0 < B n, { intro n, simp only [B, one_div, one_mul, ennreal.inv_pos], exact pow_ne_top two_ne_top }, /- Translate the density assumption into two functions `center` and `radius` associating to any n, x, δ, δpos a center and a positive radius such that `closed_ball center radius` is included both in `f n` and in `closed_ball x δ`. We can also require `radius ≤ (1/2)^(n+1)`, to ensure we get a Cauchy sequence later. -/ have : ∀n x δ, δ ≠ 0 → ∃y r, 0 < r ∧ r ≤ B (n+1) ∧ closed_ball y r ⊆ (closed_ball x δ) ∩ f n, { assume n x δ δpos, have : x ∈ closure (f n) := hd n x, rcases emetric.mem_closure_iff.1 this (δ/2) (ennreal.half_pos δpos) with ⟨y, ys, xy⟩, rw edist_comm at xy, obtain ⟨r, rpos, hr⟩ : ∃ r > 0, closed_ball y r ⊆ f n := nhds_basis_closed_eball.mem_iff.1 (is_open_iff_mem_nhds.1 (ho n) y ys), refine ⟨y, min (min (δ/2) r) (B (n+1)), _, _, λz hz, ⟨_, _⟩⟩, show 0 < min (min (δ / 2) r) (B (n+1)), from lt_min (lt_min (ennreal.half_pos δpos) rpos) (Bpos (n+1)), show min (min (δ / 2) r) (B (n+1)) ≤ B (n+1), from min_le_right _ _, show z ∈ closed_ball x δ, from calc edist z x ≤ edist z y + edist y x : edist_triangle _ _ _ ... ≤ (min (min (δ / 2) r) (B (n+1))) + (δ/2) : add_le_add hz (le_of_lt xy) ... ≤ δ/2 + δ/2 : add_le_add (le_trans (min_le_left _ _) (min_le_left _ _)) le_rfl ... = δ : ennreal.add_halves δ, show z ∈ f n, from hr (calc edist z y ≤ min (min (δ / 2) r) (B (n+1)) : hz ... ≤ r : le_trans (min_le_left _ _) (min_le_right _ _)) }, choose! center radius Hpos HB Hball using this, refine λ x, (mem_closure_iff_nhds_basis nhds_basis_closed_eball).2 (λ ε εpos, _), /- `ε` is positive. We have to find a point in the ball of radius `ε` around `x` belonging to all `f n`. For this, we construct inductively a sequence `F n = (c n, r n)` such that the closed ball `closed_ball (c n) (r n)` is included in the previous ball and in `f n`, and such that `r n` is small enough to ensure that `c n` is a Cauchy sequence. Then `c n` converges to a limit which belongs to all the `f n`. -/ let F : ℕ → (α × ℝ≥0∞) := λn, nat.rec_on n (prod.mk x (min ε (B 0))) (λn p, prod.mk (center n p.1 p.2) (radius n p.1 p.2)), let c : ℕ → α := λn, (F n).1, let r : ℕ → ℝ≥0∞ := λn, (F n).2, have rpos : ∀ n, 0 < r n, { assume n, induction n with n hn, exact lt_min εpos (Bpos 0), exact Hpos n (c n) (r n) hn.ne' }, have r0 : ∀ n, r n ≠ 0 := λ n, (rpos n).ne', have rB : ∀n, r n ≤ B n, { assume n, induction n with n hn, exact min_le_right _ _, exact HB n (c n) (r n) (r0 n) }, have incl : ∀n, closed_ball (c (n+1)) (r (n+1)) ⊆ (closed_ball (c n) (r n)) ∩ (f n) := λ n, Hball n (c n) (r n) (r0 n), have cdist : ∀n, edist (c n) (c (n+1)) ≤ B n, { assume n, rw edist_comm, have A : c (n+1) ∈ closed_ball (c (n+1)) (r (n+1)) := mem_closed_ball_self, have I := calc closed_ball (c (n+1)) (r (n+1)) ⊆ closed_ball (c n) (r n) : subset.trans (incl n) (inter_subset_left _ _) ... ⊆ closed_ball (c n) (B n) : closed_ball_subset_closed_ball (rB n), exact I A }, have : cauchy_seq c := cauchy_seq_of_edist_le_geometric_two _ one_ne_top cdist, -- as the sequence `c n` is Cauchy in a complete space, it converges to a limit `y`. rcases cauchy_seq_tendsto_of_complete this with ⟨y, ylim⟩, -- this point `y` will be the desired point. We will check that it belongs to all -- `f n` and to `ball x ε`. use y, simp only [exists_prop, set.mem_Inter], have I : ∀n, ∀m ≥ n, closed_ball (c m) (r m) ⊆ closed_ball (c n) (r n), { assume n, refine nat.le_induction _ (λm hnm h, _), { exact subset.refl _ }, { exact subset.trans (incl m) (subset.trans (inter_subset_left _ _) h) }}, have yball : ∀n, y ∈ closed_ball (c n) (r n), { assume n, refine is_closed_ball.mem_of_tendsto ylim _, refine (filter.eventually_ge_at_top n).mono (λ m hm, _), exact I n m hm mem_closed_ball_self }, split, show ∀n, y ∈ f n, { assume n, have : closed_ball (c (n+1)) (r (n+1)) ⊆ f n := subset.trans (incl n) (inter_subset_right _ _), exact this (yball (n+1)) }, show edist y x ≤ ε, from le_trans (yball 0) (min_le_left _ _), end /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with ⋂₀. -/ theorem dense_sInter_of_open {S : set (set α)} (ho : ∀s∈S, is_open s) (hS : countable S) (hd : ∀s∈S, dense s) : dense (⋂₀S) := begin cases S.eq_empty_or_nonempty with h h, { simp [h] }, { rcases hS.exists_surjective h with ⟨f, hf⟩, have F : ∀n, f n ∈ S := λn, by rw hf; exact mem_range_self _, rw [hf, sInter_range], exact dense_Inter_of_open_nat (λn, ho _ (F n)) (λn, hd _ (F n)) } end /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with an index set which is a countable set in any type. -/ theorem dense_bInter_of_open {S : set β} {f : β → set α} (ho : ∀s∈S, is_open (f s)) (hS : countable S) (hd : ∀s∈S, dense (f s)) : dense (⋂s∈S, f s) := begin rw ← sInter_image, apply dense_sInter_of_open, { rwa ball_image_iff }, { exact hS.image _ }, { rwa ball_image_iff } end /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with an index set which is an encodable type. -/ theorem dense_Inter_of_open [encodable β] {f : β → set α} (ho : ∀s, is_open (f s)) (hd : ∀s, dense (f s)) : dense (⋂s, f s) := begin rw ← sInter_range, apply dense_sInter_of_open, { rwa forall_range_iff }, { exact countable_range _ }, { rwa forall_range_iff } end /-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with ⋂₀. -/ theorem dense_sInter_of_Gδ {S : set (set α)} (ho : ∀s∈S, is_Gδ s) (hS : countable S) (hd : ∀s∈S, dense s) : dense (⋂₀S) := begin -- the result follows from the result for a countable intersection of dense open sets, -- by rewriting each set as a countable intersection of open sets, which are of course dense. choose T hTo hTc hsT using ho, have : ⋂₀ S = ⋂₀ (⋃ s ∈ S, T s ‹_›), -- := (sInter_bUnion (λs hs, (hT s hs).2.2)).symm, by simp only [sInter_Union, (hsT _ _).symm, ← sInter_eq_bInter], rw this, refine dense_sInter_of_open _ (hS.bUnion hTc) _; simp only [mem_Union]; rintro t ⟨s, hs, tTs⟩, show is_open t, from hTo s hs t tTs, show dense t, { intro x, have := hd s hs x, rw hsT s hs at this, exact closure_mono (sInter_subset_of_mem tTs) this } end /-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with an index set which is an encodable type. -/ theorem dense_Inter_of_Gδ [encodable β] {f : β → set α} (ho : ∀s, is_Gδ (f s)) (hd : ∀s, dense (f s)) : dense (⋂s, f s) := begin rw ← sInter_range, exact dense_sInter_of_Gδ (forall_range_iff.2 ‹_›) (countable_range _) (forall_range_iff.2 ‹_›) end /-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with an index set which is a countable set in any type. -/ theorem dense_bInter_of_Gδ {S : set β} {f : Π x ∈ S, set α} (ho : ∀s∈S, is_Gδ (f s ‹_›)) (hS : countable S) (hd : ∀s∈S, dense (f s ‹_›)) : dense (⋂s∈S, f s ‹_›) := begin rw bInter_eq_Inter, haveI := hS.to_encodable, exact dense_Inter_of_Gδ (λ s, ho s s.2) (λ s, hd s s.2) end /-- Baire theorem: the intersection of two dense Gδ sets is dense. -/ theorem dense.inter_of_Gδ {s t : set α} (hs : is_Gδ s) (ht : is_Gδ t) (hsc : dense s) (htc : dense t) : dense (s ∩ t) := begin rw [inter_eq_Inter], apply dense_Inter_of_Gδ; simp [bool.forall_bool, *] end /-- A property holds on a residual (comeagre) set if and only if it holds on some dense `Gδ` set. -/ lemma eventually_residual {p : α → Prop} : (∀ᶠ x in residual α, p x) ↔ ∃ (t : set α), is_Gδ t ∧ dense t ∧ ∀ x ∈ t, p x := calc (∀ᶠ x in residual α, p x) ↔ ∀ᶠ x in ⨅ (t : set α) (ht : is_Gδ t ∧ dense t), 𝓟 t, p x : by simp only [residual, infi_and] ... ↔ ∃ (t : set α) (ht : is_Gδ t ∧ dense t), ∀ᶠ x in 𝓟 t, p x : mem_binfi_of_directed (λ t₁ h₁ t₂ h₂, ⟨t₁ ∩ t₂, ⟨h₁.1.inter h₂.1, dense.inter_of_Gδ h₁.1 h₂.1 h₁.2 h₂.2⟩, by simp⟩) ⟨univ, is_Gδ_univ, dense_univ⟩ ... ↔ _ : by simp [and_assoc] /-- A set is residual (comeagre) if and only if it includes a dense `Gδ` set. -/ lemma mem_residual {s : set α} : s ∈ residual α ↔ ∃ t ⊆ s, is_Gδ t ∧ dense t := (@eventually_residual α _ _ (λ x, x ∈ s)).trans $ exists_congr $ λ t, by rw [exists_prop, and_comm (t ⊆ s), subset_def, and_assoc] lemma dense_of_mem_residual {s : set α} (hs : s ∈ residual α) : dense s := let ⟨t, hts, _, hd⟩ := mem_residual.1 hs in hd.mono hts instance : countable_Inter_filter (residual α) := ⟨begin intros S hSc hS, simp only [mem_residual] at *, choose T hTs hT using hS, refine ⟨⋂ s ∈ S, T s ‹_›, _, _, _⟩, { rw [sInter_eq_bInter], exact Inter₂_mono hTs }, { exact is_Gδ_bInter hSc (λ s hs, (hT s hs).1) }, { exact dense_bInter_of_Gδ (λ s hs, (hT s hs).1) hSc (λ s hs, (hT s hs).2) } end⟩ /-- Baire theorem: if countably many closed sets cover the whole space, then their interiors are dense. Formulated here with an index set which is a countable set in any type. -/ theorem dense_bUnion_interior_of_closed {S : set β} {f : β → set α} (hc : ∀s∈S, is_closed (f s)) (hS : countable S) (hU : (⋃s∈S, f s) = univ) : dense (⋃s∈S, interior (f s)) := begin let g := λs, (frontier (f s))ᶜ, have : dense (⋂s∈S, g s), { refine dense_bInter_of_open (λs hs, _) hS (λs hs, _), show is_open (g s), from is_open_compl_iff.2 is_closed_frontier, show dense (g s), { intro x, simp [interior_frontier (hc s hs)] }}, refine this.mono _, show (⋂s∈S, g s) ⊆ (⋃s∈S, interior (f s)), assume x hx, have : x ∈ ⋃s∈S, f s, { have := mem_univ x, rwa ← hU at this }, rcases mem_Union₂.1 this with ⟨s, hs, xs⟩, have : x ∈ g s := mem_Inter₂.1 hx s hs, have : x ∈ interior (f s), { have : x ∈ f s \ (frontier (f s)) := mem_inter xs this, simpa [frontier, xs, (hc s hs).closure_eq] using this }, exact mem_Union₂.2 ⟨s, ⟨hs, this⟩⟩ end /-- Baire theorem: if countably many closed sets cover the whole space, then their interiors are dense. Formulated here with `⋃₀`. -/ theorem dense_sUnion_interior_of_closed {S : set (set α)} (hc : ∀s∈S, is_closed s) (hS : countable S) (hU : (⋃₀ S) = univ) : dense (⋃s∈S, interior s) := by rw sUnion_eq_bUnion at hU; exact dense_bUnion_interior_of_closed hc hS hU /-- Baire theorem: if countably many closed sets cover the whole space, then their interiors are dense. Formulated here with an index set which is an encodable type. -/ theorem dense_Union_interior_of_closed [encodable β] {f : β → set α} (hc : ∀s, is_closed (f s)) (hU : (⋃s, f s) = univ) : dense (⋃s, interior (f s)) := begin rw ← bUnion_univ, apply dense_bUnion_interior_of_closed, { simp [hc] }, { apply countable_encodable }, { rwa ← bUnion_univ at hU } end /-- One of the most useful consequences of Baire theorem: if a countable union of closed sets covers the space, then one of the sets has nonempty interior. -/ theorem nonempty_interior_of_Union_of_closed [nonempty α] [encodable β] {f : β → set α} (hc : ∀s, is_closed (f s)) (hU : (⋃s, f s) = univ) : ∃s, (interior $ f s).nonempty := begin by_contradiction h, simp only [not_exists, not_nonempty_iff_eq_empty] at h, have := calc ∅ = closure (⋃s, interior (f s)) : by simp [h] ... = univ : (dense_Union_interior_of_closed hc hU).closure_eq, exact univ_nonempty.ne_empty this.symm end end Baire_theorem
ce286a232548c5729263d790bbab6e6512e3cedb
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/equiv/ring_aut.lean
6abad08abb8fff4ec7f8aaf1a4a5657f030d2b2f
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
2,199
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Callum Sutton, Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.equiv.ring import Mathlib.data.equiv.mul_add_aut import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Ring automorphisms This file defines the automorphism group structure on `ring_aut R := ring_equiv R R`. ## Implementation notes The definition of multiplication in the automorphism group agrees with function composition, multiplication in `equiv.perm`, and multiplication in `category_theory.End`, but not with `category_theory.comp`. This file is kept separate from `data/equiv/ring` so that `group_theory.perm` is free to use equivalences (and other files that use them) before the group structure is defined. ## Tags ring_aut -/ /-- The group of ring automorphisms. -/ def ring_aut (R : Type u_1) [Mul R] [Add R] := R ≃+* R namespace ring_aut /-- The group operation on automorphisms of a ring is defined by `λ g h, ring_equiv.trans h g`. This means that multiplication agrees with composition, `(g*h)(x) = g (h x)`. -/ protected instance group (R : Type u_1) [Mul R] [Add R] : group (ring_aut R) := group.mk (fun (g h : R ≃+* R) => ring_equiv.trans h g) sorry (ring_equiv.refl R) sorry sorry ring_equiv.symm (fun (a b : R ≃+* R) => a * ring_equiv.symm b) sorry protected instance inhabited (R : Type u_1) [Mul R] [Add R] : Inhabited (ring_aut R) := { default := 1 } /-- Monoid homomorphism from ring automorphisms to additive automorphisms. -/ def to_add_aut (R : Type u_1) [Mul R] [Add R] : ring_aut R →* add_aut R := monoid_hom.mk ring_equiv.to_add_equiv sorry sorry /-- Monoid homomorphism from ring automorphisms to multiplicative automorphisms. -/ def to_mul_aut (R : Type u_1) [Mul R] [Add R] : ring_aut R →* mul_aut R := monoid_hom.mk ring_equiv.to_mul_equiv sorry sorry /-- Monoid homomorphism from ring automorphisms to permutations. -/ def to_perm (R : Type u_1) [Mul R] [Add R] : ring_aut R →* equiv.perm R := monoid_hom.mk ring_equiv.to_equiv sorry sorry
8c92674371e617c7ee07d925b8b8d408d5a11d0d
367134ba5a65885e863bdc4507601606690974c1
/src/order/filter/interval.lean
73b46af8609a88582f6a846fbe65490a7e3a6c8f
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
9,978
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury G. Kudryashov -/ import data.set.intervals.ord_connected import order.filter.lift import order.filter.at_top_bot /-! # Convergence of intervals If both `a` and `b` tend to some filter `l₁`, sometimes this implies that `Ixx a b` tends to `l₂.lift' powerset`, i.e., for any `s ∈ l₂` eventually `Ixx a b` becomes a subset of `s`. Here and below `Ixx` is one of `Icc`, `Ico`, `Ioc`, and `Ioo`. We define `filter.tendsto_Ixx_class Ixx l₁ l₂` to be a typeclass representing this property. The instances provide the best `l₂` for a given `l₁`. In many cases `l₁ = l₂` but sometimes we can drop an endpoint from an interval: e.g., we prove `tendsto_Ixx_class Ico (𝓟 $ Iic a) (𝓟 $ Iio a)`, i.e., if `u₁ n` and `u₂ n` belong eventually to `Iic a`, then the interval `Ico (u₁ n) (u₂ n)` is eventually included in `Iio a`. The next table shows “output” filters `l₂` for different values of `Ixx` and `l₁`. The instances that need topology are defined in `topology/algebra/ordered`. | Input filter | `Ixx = Icc` | `Ixx = Ico` | `Ixx = Ioc` | `Ixx = Ioo` | | -----------: | :-----------: | :-----------: | :-----------: | :-----------: | | `at_top` | `at_top` | `at_top` | `at_top` | `at_top` | | `at_bot` | `at_bot` | `at_bot` | `at_bot` | `at_bot` | | `pure a` | `pure a` | `⊥` | `⊥` | `⊥` | | `𝓟 (Iic a)` | `𝓟 (Iic a)` | `𝓟 (Iio a)` | `𝓟 (Iic a)` | `𝓟 (Iio a)` | | `𝓟 (Ici a)` | `𝓟 (Ici a)` | `𝓟 (Ici a)` | `𝓟 (Ioi a)` | `𝓟 (Ioi a)` | | `𝓟 (Ioi a)` | `𝓟 (Ioi a)` | `𝓟 (Ioi a)` | `𝓟 (Ioi a)` | `𝓟 (Ioi a)` | | `𝓟 (Iio a)` | `𝓟 (Iio a)` | `𝓟 (Iio a)` | `𝓟 (Iio a)` | `𝓟 (Iio a)` | | `𝓝 a` | `𝓝 a` | `𝓝 a` | `𝓝 a` | `𝓝 a` | | `𝓝[Iic a] b` | `𝓝[Iic a] b` | `𝓝[Iio a] b` | `𝓝[Iic a] b` | `𝓝[Iio a] b` | | `𝓝[Ici a] b` | `𝓝[Ici a] b` | `𝓝[Ici a] b` | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | | `𝓝[Iio a] b` | `𝓝[Iio a] b` | `𝓝[Iio a] b` | `𝓝[Iio a] b` | `𝓝[Iio a] b` | -/ variables {α β : Type*} open_locale classical filter open set function variables [preorder α] namespace filter /-- A pair of filters `l₁`, `l₂` has `tendsto_Ixx_class Ixx` property if `Ixx a b` tends to `l₂.lift' powerset` as `a` and `b` tend to `l₁`. In all instances `Ixx` is one of `Icc`, `Ico`, `Ioc`, or `Ioo`. The instances provide the best `l₂` for a given `l₁`. In many cases `l₁ = l₂` but sometimes we can drop an endpoint from an interval: e.g., we prove `tendsto_Ixx_class Ico (𝓟 $ Iic a) (𝓟 $ Iio a)`, i.e., if `u₁ n` and `u₂ n` belong eventually to `Iic a`, then the interval `Ico (u₁ n) (u₂ n)` is eventually included in `Iio a`. We mark `l₂` as an `out_param` so that Lean can automatically find an appropriate `l₂` based on `Ixx` and `l₁`. This way, e.g., `tendsto.Ico h₁ h₂` works without specifying explicitly `l₂`. -/ class tendsto_Ixx_class (Ixx : α → α → set α) (l₁ : filter α) (l₂ : out_param $ filter α) : Prop := (tendsto_Ixx : tendsto (λ p : α × α, Ixx p.1 p.2) (l₁ ×ᶠ l₁) (l₂.lift' powerset)) lemma tendsto.Icc {l₁ l₂ : filter α} [tendsto_Ixx_class Icc l₁ l₂] {lb : filter β} {u₁ u₂ : β → α} (h₁ : tendsto u₁ lb l₁) (h₂ : tendsto u₂ lb l₁) : tendsto (λ x, Icc (u₁ x) (u₂ x)) lb (l₂.lift' powerset) := tendsto_Ixx_class.tendsto_Ixx.comp $ h₁.prod_mk h₂ lemma tendsto.Ioc {l₁ l₂ : filter α} [tendsto_Ixx_class Ioc l₁ l₂] {lb : filter β} {u₁ u₂ : β → α} (h₁ : tendsto u₁ lb l₁) (h₂ : tendsto u₂ lb l₁) : tendsto (λ x, Ioc (u₁ x) (u₂ x)) lb (l₂.lift' powerset) := tendsto_Ixx_class.tendsto_Ixx.comp $ h₁.prod_mk h₂ lemma tendsto.Ico {l₁ l₂ : filter α} [tendsto_Ixx_class Ico l₁ l₂] {lb : filter β} {u₁ u₂ : β → α} (h₁ : tendsto u₁ lb l₁) (h₂ : tendsto u₂ lb l₁) : tendsto (λ x, Ico (u₁ x) (u₂ x)) lb (l₂.lift' powerset) := tendsto_Ixx_class.tendsto_Ixx.comp $ h₁.prod_mk h₂ lemma tendsto.Ioo {l₁ l₂ : filter α} [tendsto_Ixx_class Ioo l₁ l₂] {lb : filter β} {u₁ u₂ : β → α} (h₁ : tendsto u₁ lb l₁) (h₂ : tendsto u₂ lb l₁) : tendsto (λ x, Ioo (u₁ x) (u₂ x)) lb (l₂.lift' powerset) := tendsto_Ixx_class.tendsto_Ixx.comp $ h₁.prod_mk h₂ lemma tendsto_Ixx_class_principal {s t : set α} {Ixx : α → α → set α} : tendsto_Ixx_class Ixx (𝓟 s) (𝓟 t) ↔ ∀ (x ∈ s) (y ∈ s), Ixx x y ⊆ t := begin refine iff.trans ⟨λ h, h.1, λ h, ⟨h⟩⟩ _, simp [lift'_principal monotone_powerset, -mem_prod, -prod.forall, forall_prod_set] end lemma tendsto_Ixx_class_inf {l₁ l₁' l₂ l₂' : filter α} {Ixx} [h : tendsto_Ixx_class Ixx l₁ l₂] [h' : tendsto_Ixx_class Ixx l₁' l₂'] : tendsto_Ixx_class Ixx (l₁ ⊓ l₁') (l₂ ⊓ l₂') := ⟨by simpa only [prod_inf_prod, lift'_inf_powerset] using h.1.inf h'.1⟩ lemma tendsto_Ixx_class_of_subset {l₁ l₂ : filter α} {Ixx Ixx' : α → α → set α} (h : ∀ a b, Ixx a b ⊆ Ixx' a b) [h' : tendsto_Ixx_class Ixx' l₁ l₂] : tendsto_Ixx_class Ixx l₁ l₂ := ⟨tendsto_lift'_powerset_mono h'.1 $ eventually_of_forall $ prod.forall.2 h⟩ lemma has_basis.tendsto_Ixx_class {ι : Type*} {p : ι → Prop} {s} {l : filter α} (hl : l.has_basis p s) {Ixx : α → α → set α} (H : ∀ i, p i → ∀ (x ∈ s i) (y ∈ s i), Ixx x y ⊆ s i) : tendsto_Ixx_class Ixx l l := ⟨(hl.prod_self.tendsto_iff (hl.lift' monotone_powerset)).2 $ λ i hi, ⟨i, hi, λ x hx, H i hi _ hx.1 _ hx.2⟩⟩ instance tendsto_Icc_at_top_at_top : tendsto_Ixx_class Icc (at_top : filter α) at_top := (has_basis_infi_principal_finite _).tendsto_Ixx_class $ λ s hs, ord_connected_bInter $ λ i hi, ord_connected_Ici instance tendsto_Ico_at_top_at_top : tendsto_Ixx_class Ico (at_top : filter α) at_top := tendsto_Ixx_class_of_subset (λ _ _, Ico_subset_Icc_self) instance tendsto_Ioc_at_top_at_top : tendsto_Ixx_class Ioc (at_top : filter α) at_top := tendsto_Ixx_class_of_subset (λ _ _, Ioc_subset_Icc_self) instance tendsto_Ioo_at_top_at_top : tendsto_Ixx_class Ioo (at_top : filter α) at_top := tendsto_Ixx_class_of_subset (λ _ _, Ioo_subset_Icc_self) instance tendsto_Icc_at_bot_at_bot : tendsto_Ixx_class Icc (at_bot : filter α) at_bot := (has_basis_infi_principal_finite _).tendsto_Ixx_class $ λ s hs, ord_connected_bInter $ λ i hi, ord_connected_Iic instance tendsto_Ico_at_bot_at_bot : tendsto_Ixx_class Ico (at_bot : filter α) at_bot := tendsto_Ixx_class_of_subset (λ _ _, Ico_subset_Icc_self) instance tendsto_Ioc_at_bot_at_bot : tendsto_Ixx_class Ioc (at_bot : filter α) at_bot := tendsto_Ixx_class_of_subset (λ _ _, Ioc_subset_Icc_self) instance tendsto_Ioo_at_bot_at_bot : tendsto_Ixx_class Ioo (at_bot : filter α) at_bot := tendsto_Ixx_class_of_subset (λ _ _, Ioo_subset_Icc_self) instance ord_connected.tendsto_Icc {s : set α} [ord_connected s] : tendsto_Ixx_class Icc (𝓟 s) (𝓟 s) := tendsto_Ixx_class_principal.2 ‹_› instance tendsto_Ico_Ici_Ici {a : α} : tendsto_Ixx_class Ico (𝓟 (Ici a)) (𝓟 (Ici a)) := tendsto_Ixx_class_of_subset (λ _ _, Ico_subset_Icc_self) instance tendsto_Ico_Ioi_Ioi {a : α} : tendsto_Ixx_class Ico (𝓟 (Ioi a)) (𝓟 (Ioi a)) := tendsto_Ixx_class_of_subset (λ _ _, Ico_subset_Icc_self) instance tendsto_Ico_Iic_Iio {a : α} : tendsto_Ixx_class Ico (𝓟 (Iic a)) (𝓟 (Iio a)) := tendsto_Ixx_class_principal.2 $ λ a ha b hb x hx, lt_of_lt_of_le hx.2 hb instance tendsto_Ico_Iio_Iio {a : α} : tendsto_Ixx_class Ico (𝓟 (Iio a)) (𝓟 (Iio a)) := tendsto_Ixx_class_of_subset (λ _ _, Ico_subset_Icc_self) instance tendsto_Ioc_Ici_Ioi {a : α} : tendsto_Ixx_class Ioc (𝓟 (Ici a)) (𝓟 (Ioi a)) := tendsto_Ixx_class_principal.2 $ λ x hx y hy t ht, lt_of_le_of_lt hx ht.1 instance tendsto_Ioc_Iic_Iic {a : α} : tendsto_Ixx_class Ioc (𝓟 (Iic a)) (𝓟 (Iic a)) := tendsto_Ixx_class_of_subset (λ _ _, Ioc_subset_Icc_self) instance tendsto_Ioc_Iio_Iio {a : α} : tendsto_Ixx_class Ioc (𝓟 (Iio a)) (𝓟 (Iio a)) := tendsto_Ixx_class_of_subset (λ _ _, Ioc_subset_Icc_self) instance tendsto_Ioc_Ioi_Ioi {a : α} : tendsto_Ixx_class Ioc (𝓟 (Ioi a)) (𝓟 (Ioi a)) := tendsto_Ixx_class_of_subset (λ _ _, Ioc_subset_Icc_self) instance tendsto_Ioo_Ici_Ioi {a : α} : tendsto_Ixx_class Ioo (𝓟 (Ici a)) (𝓟 (Ioi a)) := tendsto_Ixx_class_of_subset (λ _ _, Ioo_subset_Ioc_self) instance tendsto_Ioo_Iic_Iio {a : α} : tendsto_Ixx_class Ioo (𝓟 (Iic a)) (𝓟 (Iio a)) := tendsto_Ixx_class_of_subset (λ _ _, Ioo_subset_Ico_self) instance tendsto_Ioo_Ioi_Ioi {a : α} : tendsto_Ixx_class Ioo (𝓟 (Ioi a)) (𝓟 (Ioi a)) := tendsto_Ixx_class_of_subset (λ _ _, Ioo_subset_Ioc_self) instance tendsto_Ioo_Iio_Iio {a : α} : tendsto_Ixx_class Ioo (𝓟 (Iio a)) (𝓟 (Iio a)) := tendsto_Ixx_class_of_subset (λ _ _, Ioo_subset_Ioc_self) variable [partial_order β] instance tendsto_Icc_pure_pure {a : β} : tendsto_Ixx_class Icc (pure a) (pure a : filter β) := by { rw ← principal_singleton, exact tendsto_Ixx_class_principal.2 ord_connected_singleton } instance tendsto_Ico_pure_bot {a : β} : tendsto_Ixx_class Ico (pure a) ⊥ := ⟨by simp [lift'_bot monotone_powerset]⟩ instance tendsto_Ioc_pure_bot {a : β} : tendsto_Ixx_class Ioc (pure a) ⊥ := ⟨by simp [lift'_bot monotone_powerset]⟩ instance tendsto_Ioo_pure_bot {a : β} : tendsto_Ixx_class Ioo (pure a) ⊥ := tendsto_Ixx_class_of_subset (λ _ _, Ioo_subset_Ioc_self) end filter
882fe12865ee337ebff3364d674a624be0ebfa3b
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/library/init/meta/lean/parser.lean
d18526a29c1501fac14aa34aeecfb23ca9e992f6
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,243
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ prelude import init.data.option.basic init.category.monad init.category.alternative import init.meta.pexpr init.meta.interaction_monad namespace lean -- TODO: make inspectable (and pure) meta constant parser_state : Type meta constant parser_state.cur_pos : parser_state → pos @[reducible] meta def parser := interaction_monad parser_state @[reducible] meta def parser_result := interaction_monad.result parser_state open interaction_monad open interaction_monad.result namespace parser /-- Make sure the next token is an identifier, consume it, and produce the quoted name `t, where t is the identifier. -/ meta constant ident : parser name /-- Check that the next token is `tk` and consume it. `tk` must be a registered token. -/ meta constant tk (tk : string) : parser unit /-- Parse an unelaborated expression using the given right-binding power. The expression may contain antiquotations (`%%e`). -/ meta constant qexpr (rbp := nat.of_num std.prec.max) : parser pexpr /-- Return the current parser position without consuming any input. -/ meta def cur_pos : parser pos := λ s, success (parser_state.cur_pos s) s meta def parser_orelse {α : Type} (p₁ p₂ : parser α) : parser α := λ s, let pos₁ := parser_state.cur_pos s in result.cases_on (p₁ s) success (λ e₁ ref₁ s', let pos₂ := parser_state.cur_pos s' in if pos₁ ≠ pos₂ then exception e₁ ref₁ s' else result.cases_on (p₂ s) success exception) meta instance : alternative parser := ⟨@interaction_monad_fmap parser_state, (λ α a s, success a s), (@fapp _ _), @interaction_monad.failed parser_state, @parser_orelse⟩ -- TODO: move meta def {u v} many {f : Type u → Type v} [monad f] [alternative f] {a : Type u} : f a → f (list a) | x := (do y ← x, ys ← many x, return $ y::ys) <|> pure list.nil local postfix ?:100 := optional local postfix * := many meta def sep_by {α : Type} : string → parser α → parser (list α) | s p := (list.cons <$> p <*> (tk s *> p)*) <|> return [] end parser end lean
a1d609871ef0bfefdc5363bdff4633b8e7bcc151
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/bornology/constructions.lean
3b9212fa47b0fddc455aa586f1e4f80109576d87
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
6,245
lean
/- Copyright (c) 2022 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import topology.bornology.basic /-! # Bornology structure on products and subtypes > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define `bornology` and `bounded_space` instances on `α × β`, `Π i, π i`, and `{x // p x}`. We also prove basic lemmas about `bornology.cobounded` and `bornology.is_bounded` on these types. -/ open set filter bornology function open_locale filter variables {α β ι : Type*} {π : ι → Type*} [fintype ι] [bornology α] [bornology β] [Π i, bornology (π i)] instance : bornology (α × β) := { cobounded := (cobounded α).coprod (cobounded β), le_cofinite := @coprod_cofinite α β ▸ coprod_mono ‹bornology α›.le_cofinite ‹bornology β›.le_cofinite } instance : bornology (Π i, π i) := { cobounded := filter.Coprod (λ i, cobounded (π i)), le_cofinite := @Coprod_cofinite ι π _ ▸ (filter.Coprod_mono $ λ i, bornology.le_cofinite _) } /-- Inverse image of a bornology. -/ @[reducible] def bornology.induced {α β : Type*} [bornology β] (f : α → β) : bornology α := { cobounded := comap f (cobounded β), le_cofinite := (comap_mono (bornology.le_cofinite β)).trans (comap_cofinite_le _) } instance {p : α → Prop} : bornology (subtype p) := bornology.induced (coe : subtype p → α) namespace bornology /-! ### Bounded sets in `α × β` -/ lemma cobounded_prod : cobounded (α × β) = (cobounded α).coprod (cobounded β) := rfl lemma is_bounded_image_fst_and_snd {s : set (α × β)} : is_bounded (prod.fst '' s) ∧ is_bounded (prod.snd '' s) ↔ is_bounded s := compl_mem_coprod.symm variables {s : set α} {t : set β} {S : Π i, set (π i)} lemma is_bounded.fst_of_prod (h : is_bounded (s ×ˢ t)) (ht : t.nonempty) : is_bounded s := fst_image_prod s ht ▸ (is_bounded_image_fst_and_snd.2 h).1 lemma is_bounded.snd_of_prod (h : is_bounded (s ×ˢ t)) (hs : s.nonempty) : is_bounded t := snd_image_prod hs t ▸ (is_bounded_image_fst_and_snd.2 h).2 lemma is_bounded.prod (hs : is_bounded s) (ht : is_bounded t) : is_bounded (s ×ˢ t) := is_bounded_image_fst_and_snd.1 ⟨hs.subset $ fst_image_prod_subset _ _, ht.subset $ snd_image_prod_subset _ _⟩ lemma is_bounded_prod_of_nonempty (hne : set.nonempty (s ×ˢ t)) : is_bounded (s ×ˢ t) ↔ is_bounded s ∧ is_bounded t := ⟨λ h, ⟨h.fst_of_prod hne.snd, h.snd_of_prod hne.fst⟩, λ h, h.1.prod h.2⟩ lemma is_bounded_prod : is_bounded (s ×ˢ t) ↔ s = ∅ ∨ t = ∅ ∨ is_bounded s ∧ is_bounded t := begin rcases s.eq_empty_or_nonempty with rfl|hs, { simp }, rcases t.eq_empty_or_nonempty with rfl|ht, { simp }, simp only [hs.ne_empty, ht.ne_empty, is_bounded_prod_of_nonempty (hs.prod ht), false_or] end lemma is_bounded_prod_self : is_bounded (s ×ˢ s) ↔ is_bounded s := begin rcases s.eq_empty_or_nonempty with rfl|hs, { simp }, exact (is_bounded_prod_of_nonempty (hs.prod hs)).trans (and_self _) end /-! ### Bounded sets in `Π i, π i` -/ lemma cobounded_pi : cobounded (Π i, π i) = filter.Coprod (λ i, cobounded (π i)) := rfl lemma forall_is_bounded_image_eval_iff {s : set (Π i, π i)} : (∀ i, is_bounded (eval i '' s)) ↔ is_bounded s := compl_mem_Coprod.symm lemma is_bounded.pi (h : ∀ i, is_bounded (S i)) : is_bounded (pi univ S) := forall_is_bounded_image_eval_iff.1 $ λ i, (h i).subset eval_image_univ_pi_subset lemma is_bounded_pi_of_nonempty (hne : (pi univ S).nonempty) : is_bounded (pi univ S) ↔ ∀ i, is_bounded (S i) := ⟨λ H i, @eval_image_univ_pi _ _ _ i hne ▸ forall_is_bounded_image_eval_iff.2 H i, is_bounded.pi⟩ lemma is_bounded_pi : is_bounded (pi univ S) ↔ (∃ i, S i = ∅) ∨ ∀ i, is_bounded (S i) := begin by_cases hne : ∃ i, S i = ∅, { simp [hne, univ_pi_eq_empty_iff.2 hne] }, { simp only [hne, false_or], simp only [not_exists, ← ne.def, ←nonempty_iff_ne_empty, ← univ_pi_nonempty_iff] at hne, exact is_bounded_pi_of_nonempty hne } end /-! ### Bounded sets in `{x // p x}` -/ lemma is_bounded_induced {α β : Type*} [bornology β] {f : α → β} {s : set α} : @is_bounded α (bornology.induced f) s ↔ is_bounded (f '' s) := compl_mem_comap lemma is_bounded_image_subtype_coe {p : α → Prop} {s : set {x // p x}} : is_bounded (coe '' s : set α) ↔ is_bounded s := is_bounded_induced.symm end bornology /-! ### Bounded spaces -/ open bornology instance [bounded_space α] [bounded_space β] : bounded_space (α × β) := by simp [← cobounded_eq_bot_iff, cobounded_prod] instance [∀ i, bounded_space (π i)] : bounded_space (Π i, π i) := by simp [← cobounded_eq_bot_iff, cobounded_pi] lemma bounded_space_induced_iff {α β : Type*} [bornology β] {f : α → β} : @bounded_space α (bornology.induced f) ↔ is_bounded (range f) := by rw [← is_bounded_univ, is_bounded_induced, image_univ] lemma bounded_space_subtype_iff {p : α → Prop} : bounded_space (subtype p) ↔ is_bounded {x | p x} := by rw [bounded_space_induced_iff, subtype.range_coe_subtype] lemma bounded_space_coe_set_iff {s : set α} : bounded_space s ↔ is_bounded s := bounded_space_subtype_iff alias bounded_space_subtype_iff ↔ _ bornology.is_bounded.bounded_space_subtype alias bounded_space_coe_set_iff ↔ _ bornology.is_bounded.bounded_space_coe instance [bounded_space α] {p : α → Prop} : bounded_space (subtype p) := (is_bounded.all {x | p x}).bounded_space_subtype /-! ### `additive`, `multiplicative` The bornology on those type synonyms is inherited without change. -/ instance : bornology (additive α) := ‹bornology α› instance : bornology (multiplicative α) := ‹bornology α› instance [bounded_space α] : bounded_space (additive α) := ‹bounded_space α› instance [bounded_space α] : bounded_space (multiplicative α) := ‹bounded_space α› /-! ### Order dual The bornology on this type synonym is inherited without change. -/ instance : bornology αᵒᵈ := ‹bornology α› instance [bounded_space α] : bounded_space αᵒᵈ := ‹bounded_space α›
02a70a005c046f8905c379f8063814a439e65b86
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/homology/differential_object.lean
e2d2410ae4fd722b6fdf0822268d16aa671fb23b
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
4,964
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.homology.homological_complex import category_theory.differential_object /-! # Homological complexes are differential graded objects. We verify that a `homological_complex` indexed by an `add_comm_group` is essentially the same thing as a differential graded object. This equivalence is probably not particularly useful in practice; it's here to check that definitions match up as expected. -/ open category_theory open category_theory.limits open_locale classical noncomputable theory namespace homological_complex variables {β : Type*} [add_comm_group β] {b : β} variables {V : Type*} [category V] [has_zero_morphisms V] /-- Since `eq_to_hom` only preserves the fact that `X.X i = X.X j` but not `i = j`, this definition is used to aid the simplifier. -/ abbreviation _root_.category_theory.differential_object.X_eq_to_hom (X : differential_object (graded_object_with_shift b V)) {i j : β} (h : i = j) : X.X i ⟶ X.X j := eq_to_hom (congr_arg X.X h) @[simp] lemma _root_.category_theory.differential_object.X_eq_to_hom_refl (X : differential_object (graded_object_with_shift b V)) (i : β) : X.X_eq_to_hom (refl i) = 𝟙 _ := rfl @[simp, reassoc] lemma eq_to_hom_d (X : differential_object (graded_object_with_shift b V)) {x y : β} (h : x = y) : X.X_eq_to_hom h ≫ X.d y = X.d x ≫ X.X_eq_to_hom (by { cases h, refl }) := by { cases h, dsimp, simp } @[simp, reassoc] lemma d_eq_to_hom (X : homological_complex V (complex_shape.up' b)) {x y z : β} (h : y = z) : X.d x y ≫ eq_to_hom (congr_arg X.X h) = X.d x z := by { cases h, simp } @[simp, reassoc] lemma eq_to_hom_f {X Y : differential_object (graded_object_with_shift b V)} (f : X ⟶ Y) {x y : β} (h : x = y) : X.X_eq_to_hom h ≫ f.f y = f.f x ≫ Y.X_eq_to_hom h := by { cases h, simp } variables (b V) local attribute [reducible] graded_object.has_shift /-- The functor from differential graded objects to homological complexes. -/ @[simps] def dgo_to_homological_complex : differential_object (graded_object_with_shift b V) ⥤ homological_complex V (complex_shape.up' b) := { obj := λ X, { X := λ i, X.X i, d := λ i j, if h : i + b = j then X.d i ≫ X.X_eq_to_hom (show i + (1 : ℤ) • b = j, by simp [h]) else 0, shape' := λ i j w, by { dsimp at w, convert dif_neg w }, d_comp_d' := λ i j k hij hjk, begin dsimp at hij hjk, substs hij hjk, have : X.d i ≫ X.d _ = _ := (congr_fun X.d_squared i : _), reassoc! this, simp [this], end }, map := λ X Y f, { f := f.f, comm' := λ i j h, begin dsimp at h ⊢, subst h, have : f.f i ≫ Y.d i = X.d i ≫ f.f (i + 1 • b) := (congr_fun f.comm i).symm, reassoc! this, simp only [category.comp_id, eq_to_hom_refl, dif_pos rfl, this, category.assoc, eq_to_hom_f] end, } } /-- The functor from homological complexes to differential graded objects. -/ @[simps] def homological_complex_to_dgo : homological_complex V (complex_shape.up' b) ⥤ differential_object (graded_object_with_shift b V) := { obj := λ X, { X := λ i, X.X i, d := λ i, X.d i (i + 1 • b), d_squared' := by { ext i, dsimp, simp, } }, map := λ X Y f, { f := f.f, comm' := by { ext i, dsimp, simp, }, } } /-- The unit isomorphism for `dgo_equiv_homological_complex`. -/ @[simps] def dgo_equiv_homological_complex_unit_iso : 𝟭 (differential_object (graded_object_with_shift b V)) ≅ dgo_to_homological_complex b V ⋙ homological_complex_to_dgo b V := nat_iso.of_components (λ X, { hom := { f := λ i, 𝟙 (X.X i), }, inv := { f := λ i, 𝟙 (X.X i), }, }) (by tidy) /-- The counit isomorphism for `dgo_equiv_homological_complex`. -/ @[simps] def dgo_equiv_homological_complex_counit_iso : homological_complex_to_dgo b V ⋙ dgo_to_homological_complex b V ≅ 𝟭 (homological_complex V (complex_shape.up' b)) := nat_iso.of_components (λ X, { hom := { f := λ i, 𝟙 (X.X i), comm' := λ i j h, begin dsimp at h ⊢, subst h, delta homological_complex_to_dgo, simp, end }, inv := { f := λ i, 𝟙 (X.X i), comm' := λ i j h, begin dsimp at h ⊢, subst h, delta homological_complex_to_dgo, simp, end }, }) (by tidy) /-- The category of differential graded objects in `V` is equivalent to the category of homological complexes in `V`. -/ @[simps] def dgo_equiv_homological_complex : differential_object (graded_object_with_shift b V) ≌ homological_complex V (complex_shape.up' b) := { functor := dgo_to_homological_complex b V, inverse := homological_complex_to_dgo b V, unit_iso := dgo_equiv_homological_complex_unit_iso b V, counit_iso := dgo_equiv_homological_complex_counit_iso b V, } end homological_complex
b1daa107a4bb8603fd8c201657b04a8c93256145
c3de33d4701e6113627153fe1103b255e752ed7d
/algebra/lattice/zorn.lean
e3deb8a71796560d452a37bc4e2f326247965cfc
[]
no_license
jroesch/library_dev
77d2b246ff47ab05d55cb9706a37d3de97038388
4faa0a45c6aa7eee6e661113c2072b8840bff79b
refs/heads/master
1,611,281,606,352
1,495,661,644,000
1,495,661,644,000
92,340,430
0
0
null
1,495,663,344,000
1,495,663,344,000
null
UTF-8
Lean
false
false
9,478
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Zorn's lemmas. Ported from Isabelle/HOL (written by Jacques D. Fleuriot, Tobias Nipkow, and Christian Sternagel). -/ import ...data.set noncomputable theory open set classical local attribute [instance] decidable_inhabited local attribute [instance] prop_decidable universes u section variable {α : Type u} lemma or_of_not_implies {a b : Prop} (h : ¬ b → a) : (a ∨ b) := classical.by_cases (suppose b, or.inr this) (suppose ¬ b, or.inl $ h this) theorem sUnion_subset_iff {s : set (set α)} {t : set α} : (⋃₀ s) ⊆ t ↔ ∀t' ∈ s, t' ⊆ t := ⟨take h t' ht', subset.trans (subset_sUnion_of_mem ht') h, sUnion_subset⟩ theorem not_subset_iff {s t : set α} : ¬ (s ⊆ t) ↔ (∃a∈s, a ∉ t) := show ¬ (∀a∈s, a ∈ t) ↔ (∃a∈s, a ∉ t), by simp [not_forall_iff_exists_not, not_implies_iff_and_not] lemma ssubset_insert {s : set α} {a : α} (h : a ∉ s) : s ⊂ insert a s := ⟨subset_insert _ _, suppose s = insert a s, h $ this.symm ▸ mem_insert _ _⟩ def pairwise_on (s : set α) (r : α → α → Prop) := ∀x∈s, ∀y∈s, x ≠ y → r x y end namespace zorn section chain parameters {α : Type u} {r : α → α → Prop} local infix ` ≺ `:50 := r def chain (c : set α) := pairwise_on c (λx y, x ≺ y ∨ y ≺ x) lemma chain_insert {c : set α} {a : α} (hc : chain c) (ha : ∀b∈c, b ≠ a → a ≺ b ∨ b ≺ a) : chain (insert a c) := forall_insert_of_forall (take x hx, forall_insert_of_forall (hc x hx) (take hneq, (ha x hx hneq).symm)) (forall_insert_of_forall (take x hx hneq, ha x hx $ take h', hneq h'.symm) (take h, (h rfl).rec _)) def super_chain (c₁ c₂ : set α) := chain c₂ ∧ c₁ ⊂ c₂ def is_max_chain (c : set α) := chain c ∧ ¬ (∃c', super_chain c c') def succ_chain (c : set α) := if h : ∃c', chain c ∧ super_chain c c' then some h else c lemma succ_spec {c : set α} (h : ∃c', chain c ∧ super_chain c c') : super_chain c (succ_chain c) := let ⟨c', hc'⟩ := h in have chain c ∧ super_chain c (some h), from @some_spec _ (λc', chain c ∧ super_chain c c') _, by simp [succ_chain, dif_pos, h, this.right] lemma chain_succ {c : set α} (hc : chain c) : chain (succ_chain c) := if h : ∃c', chain c ∧ super_chain c c' then (succ_spec h).left else by simp [succ_chain, dif_neg, h]; exact hc lemma super_of_not_max {c : set α} (hc₁ : chain c) (hc₂ : ¬ is_max_chain c) : super_chain c (succ_chain c) := begin simp [is_max_chain, not_and_iff, not_not_iff] at hc₂, have ∃c', super_chain c c', from hc₂.neg_resolve_left hc₁, let ⟨c', hc'⟩ := this in show super_chain c (succ_chain c), from succ_spec ⟨c', hc₁, hc'⟩ end lemma succ_increasing {c : set α} : c ⊆ succ_chain c := if h : ∃c', chain c ∧ super_chain c c' then have super_chain c (succ_chain c), from succ_spec h, this.right.left else by simp [succ_chain, dif_neg, h, subset.refl] inductive chain_closure : set α → Prop | succ : ∀{s}, chain_closure s → chain_closure (succ_chain s) | union : ∀{s}, (∀a∈s, chain_closure a) → chain_closure (⋃₀ s) lemma chain_closure_empty : chain_closure ∅ := have chain_closure (⋃₀ ∅), from chain_closure.union $ take a h, h.rec _, by simp at this; assumption lemma chain_closure_closure : chain_closure (⋃₀ {s | chain_closure s}) := chain_closure.union $ take s hs, hs variables {c c₁ c₂ c₃ : set α} private lemma chain_closure_succ_total_aux (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h : ∀{c₃}, chain_closure c₃ → c₃ ⊆ c₂ → c₂ = c₃ ∨ succ_chain c₃ ⊆ c₂) : c₁ ⊆ c₂ ∨ succ_chain c₂ ⊆ c₁ := begin induction hc₁, case _root_.zorn.chain_closure.succ c₃ hc₃ ih { cases ih with ih ih, { note h := h hc₃ ih, cases h with h h, { exact (or.inr $ h ▸ subset.refl _) }, { exact (or.inl h) } }, { exact (or.inr $ subset.trans ih succ_increasing) } }, case _root_.zorn.chain_closure.union s hs ih { exact (or_of_not_implies $ take hn, sUnion_subset $ take a ha, have a ⊆ c₂ ∨ succ_chain c₂ ⊆ a, from ih a ha, this.resolve_right $ take h, hn $ subset.trans h $ subset_sUnion_of_mem ha) } end private lemma chain_closure_succ_total (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h : c₁ ⊆ c₂) : c₂ = c₁ ∨ succ_chain c₁ ⊆ c₂ := begin induction hc₂ generalizing c₁ hc₁ h, case _root_.zorn.chain_closure.succ c₂ hc₂ ih { note h₁ : c₁ ⊆ c₂ ∨ @succ_chain α r c₂ ⊆ c₁ := (chain_closure_succ_total_aux hc₁ hc₂ $ take c₁, ih), cases h₁ with h₁ h₁, { note h₂ := ih hc₁ h₁, cases h₂ with h₂ h₂, { exact (or.inr $ h₂ ▸ subset.refl _) }, { exact (or.inr $ subset.trans h₂ succ_increasing) } }, { exact (or.inl $ subset.antisymm h₁ h) } }, case _root_.zorn.chain_closure.union s hs ih { apply or.imp (take h', subset.antisymm h' h) id, apply classical.by_contradiction, simp [not_or_iff, sUnion_subset_iff, not_forall_iff_exists_not, not_implies_iff_and_not], intro h, cases h with h₁ h₂, cases h₂ with c₃ h₂, cases h₂ with h₂ hc₃, note h := chain_closure_succ_total_aux hc₁ (hs c₃ hc₃) (take c₄, ih _ hc₃), cases h with h h, { note h' := ih c₃ hc₃ hc₁ h, cases h' with h' h', { exact (h₂ $ h' ▸ subset.refl _) }, { exact (h₁ $ subset.trans h' $ subset_sUnion_of_mem hc₃) } }, { exact (h₂ $ subset.trans succ_increasing h) } } end lemma chain_closure_total (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) : c₁ ⊆ c₂ ∨ c₂ ⊆ c₁ := have c₁ ⊆ c₂ ∨ succ_chain c₂ ⊆ c₁, from chain_closure_succ_total_aux hc₁ hc₂ $ take c₃ hc₃, chain_closure_succ_total hc₃ hc₂, or.imp_right (suppose succ_chain c₂ ⊆ c₁, subset.trans succ_increasing this) this lemma chain_closure_succ_fixpoint (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h_eq : succ_chain c₂ = c₂) : c₁ ⊆ c₂ := begin induction hc₁, case _root_.zorn.chain_closure.succ c₁ hc₁ h { exact or.elim (chain_closure_succ_total hc₁ hc₂ h) (take h, h ▸ h_eq.symm ▸ subset.refl c₂) id }, case _root_.zorn.chain_closure.union s hs ih { exact (sUnion_subset $ take c₁ hc₁, ih c₁ hc₁) } end lemma chain_closure_succ_fixpoint_iff (hc : chain_closure c) : succ_chain c = c ↔ c = ⋃₀ {c | chain_closure c} := ⟨take h, subset.antisymm (subset_sUnion_of_mem hc) (chain_closure_succ_fixpoint chain_closure_closure hc h), suppose c = ⋃₀{c : set α | chain_closure c}, subset.antisymm (calc succ_chain c ⊆ ⋃₀{c : set α | chain_closure c} : subset_sUnion_of_mem $ chain_closure.succ hc ... = c : this.symm) succ_increasing⟩ lemma chain_chain_closure (hc : chain_closure c) : chain c := begin induction hc, case _root_.zorn.chain_closure.succ c hc h { exact chain_succ h }, case _root_.zorn.chain_closure.union s hs h { note h : ∀c∈s, zorn.chain c := h, exact take c₁ ⟨t₁, ht₁, (hc₁ : c₁ ∈ t₁)⟩ c₂ ⟨t₂, ht₂, (hc₂ : c₂ ∈ t₂)⟩ hneq, have t₁ ⊆ t₂ ∨ t₂ ⊆ t₁, from chain_closure_total (hs _ ht₁) (hs _ ht₂), or.elim this (suppose t₁ ⊆ t₂, h t₂ ht₂ c₁ (this hc₁) c₂ hc₂ hneq) (suppose t₂ ⊆ t₁, h t₁ ht₁ c₁ hc₁ c₂ (this hc₂) hneq) } end def max_chain := (⋃₀ {c | chain_closure c}) /-- Hausdorff's maximality principle There exists a maximal totally ordered subset of `α`. Note that we do not require `α` to be partially ordered by `r`. -/ lemma max_chain_spec : is_max_chain max_chain := classical.by_contradiction $ suppose ¬ is_max_chain (⋃₀ {c | chain_closure c}), have super_chain (⋃₀ {c | chain_closure c}) (succ_chain (⋃₀ {c | chain_closure c})), from super_of_not_max (chain_chain_closure chain_closure_closure) this, let ⟨h₁, h₂, (h₃ : (⋃₀ {c | chain_closure c}) ≠ succ_chain (⋃₀ {c | chain_closure c}))⟩ := this in have succ_chain (⋃₀ {c | chain_closure c}) = (⋃₀ {c | chain_closure c}), from (chain_closure_succ_fixpoint_iff chain_closure_closure).mpr rfl, h₃ this.symm /-- Zorn's lemma If every chain has an upper bound, then there is a maximal element -/ lemma zorn (h : ∀c, chain c → ∃ub, ∀a∈c, a ≺ ub) (trans : ∀{a b c}, a ≺ b → b ≺ c → a ≺ c) : ∃m, ∀a, m ≺ a → a ≺ m := have ∃ub, ∀a∈max_chain, a ≺ ub, from h _ $ max_chain_spec.left, let ⟨ub, (hub : ∀a∈max_chain, a ≺ ub)⟩ := this in ⟨ub, take a ha, have chain (insert a max_chain), from chain_insert max_chain_spec.left $ take b hb _, or.inr $ trans (hub b hb) ha, have a ∈ max_chain, from classical.by_contradiction $ assume h : a ∉ max_chain, max_chain_spec.right $ ⟨insert a max_chain, this, ssubset_insert h⟩, hub a this⟩ end chain lemma zorn_weak_order {α : Type u} [weak_order α] (h : ∀c:set α, @chain α (≤) c → ∃ub, ∀a∈c, a ≤ ub) : ∃m:α, ∀a, m ≤ a → a = m := let ⟨m, hm⟩ := @zorn α (≤) h (take a b c, le_trans) in ⟨m, take a ha, le_antisymm (hm a ha) ha⟩ end zorn
f82cf32bc960074e64405d61187a68f898da1870
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/tests/lean/run/level.lean
d246389ce9e35b1e9b0c814a88da8a43fb958eeb
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
274
lean
import Lean.Level open Lean #eval levelZero == levelZero #eval levelZero == mkLevelSucc levelZero #eval mkLevelMax (mkLevelSucc levelZero) levelZero == mkLevelSucc levelZero #eval mkLevelMax (mkLevelSucc levelZero) levelZero == mkLevelMax (mkLevelSucc levelZero) levelZero
d057c34625bcf4539b1eefda5ca97ebf927f00f1
7cef822f3b952965621309e88eadf618da0c8ae9
/src/topology/algebra/continuous_functions.lean
ae5ae5e37b43f14cf628f77d9b7ffd64a8d92140
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
2,285
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import topology.basic import topology.algebra.ring import ring_theory.subring universes u v local attribute [elab_simple] continuous.comp @[to_additive continuous_add_submonoid] instance continuous_submonoid (α : Type u) (β : Type v) [topological_space α] [topological_space β] [monoid β] [topological_monoid β] : is_submonoid { f : α → β | continuous f } := { one_mem := @continuous_const _ _ _ _ 1, mul_mem := λ f g fc gc, continuous.comp (topological_monoid.continuous_mul β) (continuous.prod_mk fc gc) }. @[to_additive continuous_add_subgroup] instance continuous_subgroup (α : Type u) (β : Type v) [topological_space α] [topological_space β] [group β] [topological_group β] : is_subgroup { f : α → β | continuous f } := { inv_mem := λ f fc, continuous.comp (topological_group.continuous_inv β) fc, ..continuous_submonoid α β, }. @[to_additive continuous_add_monoid] instance continuous_monoid {α : Type u} {β : Type v} [topological_space α] [topological_space β] [monoid β] [topological_monoid β] : monoid { f : α → β | continuous f } := subtype.monoid @[to_additive continuous_add_group] instance continuous_group {α : Type u} {β : Type v} [topological_space α] [topological_space β] [group β] [topological_group β] : group { f : α → β | continuous f } := subtype.group instance continuous_subring (α : Type u) (β : Type v) [topological_space α] [topological_space β] [ring β] [topological_ring β] : is_subring { f : α → β | continuous f } := { ..continuous_add_subgroup α β, ..continuous_submonoid α β }. instance continuous_ring {α : Type u} {β : Type v} [topological_space α] [topological_space β] [ring β] [topological_ring β] : ring { f : α → β | continuous f } := @subtype.ring _ _ _ (continuous_subring α β) -- infer_instance doesn't work?! instance continuous_comm_ring {α : Type u} {β : Type v} [topological_space α] [topological_space β] [comm_ring β] [topological_ring β] : comm_ring { f : α → β | continuous f } := @subtype.comm_ring _ _ _ (continuous_subring α β) -- infer_instance doesn't work?!
e3e3988bead7a82378528ea282f35cf270ed7bcd
1b8f093752ba748c5ca0083afef2959aaa7dace5
/src/category_theory/Grothendieck_topology.lean
aa001f4f1219fee77b88537b1926cb5644a18523
[]
no_license
khoek/lean-category-theory
7ec4cda9cc64a5a4ffeb84712ac7d020dbbba386
63dcb598e9270a3e8b56d1769eb4f825a177cd95
refs/heads/master
1,585,251,725,759
1,539,344,445,000
1,539,344,445,000
145,281,070
0
0
null
1,534,662,376,000
1,534,662,376,000
null
UTF-8
Lean
false
false
1,696
lean
import category_theory.limits import category_theory.over open category_theory open category_theory.limits universes u₁ v₁ variables {C : Type u₁} [𝒞 : category.{u₁ v₁} C] include 𝒞 variables {X Y : C} structure cover (X : C) := (I : Type) (U : I → C) (π : Π i : I, U i ⟶ X) def pullback_cover [has_pullbacks.{u₁ v₁} C] {X Y : C} (c : cover.{u₁ v₁} X) (f : Y ⟶ X) : cover.{u₁ v₁} Y := { I := c.I, U := λ i, (pullback (c.π i) f), π := λ i, (pullback.square (c.π i) f).π₂ } def covers_of_cover {X : C} (c : cover.{u₁ v₁} X) (d : Π i : c.I, cover.{u₁ v₁} (c.U i)) : cover.{u₁ v₁} X := { I := Σ i : c.I, (d i).I, U := λ i, (d i.1).U i.2, π := λ i, ((d i.1).π i.2) ≫ (c.π i.1) } def singleton_cover {Y X : C} (f : Y ⟶ X) : cover.{u₁ v₁} X := { I := unit, U := λ i, Y, π := λ i, f } structure Grothendieck_topology [has_pullbacks.{u₁ v₁} C] := (covers (X : C) : set (cover.{u₁ v₁} X)) (pullback {X : C} (c ∈ covers X) {Y : C} (f : Y ⟶ X) : pullback_cover c f ∈ covers Y) (cover_of_covers {X : C} (c ∈ covers X) (d : Π (i : cover.I.{u₁ v₁} c), {P | covers (c.U i) P}) : covers_of_cover c (λ i, (d i).1) ∈ covers X) (isomorphism_cover {Y X : C} (f : Y ≅ X) : singleton_cover (f : Y ⟶ X) ∈ covers X) -- structure site (C : Type u₁) -- Or, we could do this in terms of sieves: structure sieve (X : C) := (S : set (over.{u₁ v₁} X)) (closed (f : { f // S f }) {Z : C} (g : Z ⟶ f.1.1) : (⟨ Z, g ≫ f.val.2 ⟩ : over X) ∈ S) -- example : a topology is a Grothendieck topology -- example : etale maps over a scheme X, with covers jointly surjective (U_i ⟶ X)_i
6cd43187f4dc7469d622a6abadd4d75ff306e574
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/linear_algebra/pi.lean
d6bb124ef56df74e8a20c90dc873183f1a9eb89c
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,807
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Kevin Buzzard, Yury Kudryashov, Eric Wieser -/ import linear_algebra.basic /-! # Pi types of modules This file defines constructors for linear maps whose domains or codomains are pi types. It contains theorems relating these to each other, as well as to `linear_map.ker`. ## Main definitions - pi types in the codomain: - `linear_map.pi` - `linear_map.single` - pi types in the domain: - `linear_map.proj` - `linear_map.diag` -/ universes u v w x y z u' v' w' x' y' variables {R : Type u} {K : Type u'} {M : Type v} {V : Type v'} {M₂ : Type w} {V₂ : Type w'} variables {M₃ : Type y} {V₃ : Type y'} {M₄ : Type z} {ι : Type x} {ι' : Type x'} open function submodule open_locale big_operators namespace linear_map universe i variables [semiring R] [add_comm_monoid M₂] [module R M₂] [add_comm_monoid M₃] [module R M₃] {φ : ι → Type i} [∀i, add_comm_monoid (φ i)] [∀i, module R (φ i)] /-- `pi` construction for linear functions. From a family of linear functions it produces a linear function into a family of modules. -/ def pi (f : Πi, M₂ →ₗ[R] φ i) : M₂ →ₗ[R] (Πi, φ i) := { to_fun := λ c i, f i c, map_add' := λ c d, funext $ λ i, (f i).map_add _ _, map_smul' := λ c d, funext $ λ i, (f i).map_smul _ _ } @[simp] lemma pi_apply (f : Πi, M₂ →ₗ[R] φ i) (c : M₂) (i : ι) : pi f c i = f i c := rfl lemma ker_pi (f : Πi, M₂ →ₗ[R] φ i) : ker (pi f) = (⨅i:ι, ker (f i)) := by ext c; simp [funext_iff]; refl lemma pi_eq_zero (f : Πi, M₂ →ₗ[R] φ i) : pi f = 0 ↔ (∀i, f i = 0) := by simp only [linear_map.ext_iff, pi_apply, funext_iff]; exact ⟨λh a b, h b a, λh a b, h b a⟩ lemma pi_zero : pi (λi, 0 : Πi, M₂ →ₗ[R] φ i) = 0 := by ext; refl lemma pi_comp (f : Πi, M₂ →ₗ[R] φ i) (g : M₃ →ₗ[R] M₂) : (pi f).comp g = pi (λi, (f i).comp g) := rfl /-- The projections from a family of modules are linear maps. Note: known here as `linear_map.proj`, this construction is in other categories called `eval`, for example `pi.eval_monoid_hom`, `pi.eval_ring_hom`. -/ def proj (i : ι) : (Πi, φ i) →ₗ[R] φ i := { to_fun := function.eval i, map_add' := λ f g, rfl, map_smul' := λ c f, rfl } @[simp] lemma coe_proj (i : ι) : ⇑(proj i : (Πi, φ i) →ₗ[R] φ i) = function.eval i := rfl lemma proj_apply (i : ι) (b : Πi, φ i) : (proj i : (Πi, φ i) →ₗ[R] φ i) b = b i := rfl lemma proj_pi (f : Πi, M₂ →ₗ[R] φ i) (i : ι) : (proj i).comp (pi f) = f i := ext $ assume c, rfl lemma infi_ker_proj : (⨅i, ker (proj i) : submodule R (Πi, φ i)) = ⊥ := bot_unique $ set_like.le_def.2 $ assume a h, begin simp only [mem_infi, mem_ker, proj_apply] at h, exact (mem_bot _).2 (funext $ assume i, h i) end /-- Linear map between the function spaces `I → M₂` and `I → M₃`, induced by a linear map `f` between `M₂` and `M₃`. -/ @[simps] protected def comp_left (f : M₂ →ₗ[R] M₃) (I : Type*) : (I → M₂) →ₗ[R] (I → M₃) := { to_fun := λ h, f ∘ h, map_smul' := λ c h, by { ext x, exact f.map_smul' c (h x) }, .. f.to_add_monoid_hom.comp_left I } lemma apply_single [add_comm_monoid M] [module R M] [decidable_eq ι] (f : Π i, φ i →ₗ[R] M) (i j : ι) (x : φ i) : f j (pi.single i x j) = pi.single i (f i x) j := pi.apply_single (λ i, f i) (λ i, (f i).map_zero) _ _ _ /-- The `linear_map` version of `add_monoid_hom.single` and `pi.single`. -/ def single [decidable_eq ι] (i : ι) : φ i →ₗ[R] (Πi, φ i) := { to_fun := pi.single i, map_smul' := pi.single_smul i, .. add_monoid_hom.single φ i} @[simp] lemma coe_single [decidable_eq ι] (i : ι) : ⇑(single i : φ i →ₗ[R] (Π i, φ i)) = pi.single i := rfl variables (R φ) /-- The linear equivalence between linear functions on a finite product of modules and families of functions on these modules. See note [bundled maps over different rings]. -/ @[simps] def lsum (S) [add_comm_monoid M] [module R M] [fintype ι] [decidable_eq ι] [semiring S] [module S M] [smul_comm_class R S M] : (Π i, φ i →ₗ[R] M) ≃ₗ[S] ((Π i, φ i) →ₗ[R] M) := { to_fun := λ f, ∑ i : ι, (f i).comp (proj i), inv_fun := λ f i, f.comp (single i), map_add' := λ f g, by simp only [pi.add_apply, add_comp, finset.sum_add_distrib], map_smul' := λ c f, by simp only [pi.smul_apply, smul_comp, finset.smul_sum, ring_hom.id_apply], left_inv := λ f, by { ext i x, simp [apply_single] }, right_inv := λ f, begin ext, suffices : f (∑ j, pi.single j (x j)) = f x, by simpa [apply_single], rw finset.univ_sum_single end } variables {R φ} section ext variables [fintype ι] [decidable_eq ι] [add_comm_monoid M] [module R M] {f g : (Π i, φ i) →ₗ[R] M} lemma pi_ext (h : ∀ i x, f (pi.single i x) = g (pi.single i x)) : f = g := to_add_monoid_hom_injective $ add_monoid_hom.functions_ext _ _ _ h lemma pi_ext_iff : f = g ↔ ∀ i x, f (pi.single i x) = g (pi.single i x) := ⟨λ h i x, h ▸ rfl, pi_ext⟩ /-- This is used as the ext lemma instead of `linear_map.pi_ext` for reasons explained in note [partially-applied ext lemmas]. -/ @[ext] lemma pi_ext' (h : ∀ i, f.comp (single i) = g.comp (single i)) : f = g := begin refine pi_ext (λ i x, _), convert linear_map.congr_fun (h i) x end lemma pi_ext'_iff : f = g ↔ ∀ i, f.comp (single i) = g.comp (single i) := ⟨λ h i, h ▸ rfl, pi_ext'⟩ end ext section variables (R φ) /-- If `I` and `J` are disjoint index sets, the product of the kernels of the `J`th projections of `φ` is linearly equivalent to the product over `I`. -/ def infi_ker_proj_equiv {I J : set ι} [decidable_pred (λi, i ∈ I)] (hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) : (⨅i ∈ J, ker (proj i) : submodule R (Πi, φ i)) ≃ₗ[R] (Πi:I, φ i) := begin refine linear_equiv.of_linear (pi $ λi, (proj (i:ι)).comp (submodule.subtype _)) (cod_restrict _ (pi $ λi, if h : i ∈ I then proj (⟨i, h⟩ : I) else 0) _) _ _, { assume b, simp only [mem_infi, mem_ker, funext_iff, proj_apply, pi_apply], assume j hjJ, have : j ∉ I := assume hjI, hd ⟨hjI, hjJ⟩, rw [dif_neg this, zero_apply] }, { simp only [pi_comp, comp_assoc, subtype_comp_cod_restrict, proj_pi, subtype.coe_prop], ext b ⟨j, hj⟩, simp only [dif_pos, function.comp_app, function.eval_apply, linear_map.cod_restrict_apply, linear_map.coe_comp, linear_map.coe_proj, linear_map.pi_apply, submodule.subtype_apply, subtype.coe_prop], refl }, { ext1 ⟨b, hb⟩, apply subtype.ext, ext j, have hb : ∀i ∈ J, b i = 0, { simpa only [mem_infi, mem_ker, proj_apply] using (mem_infi _).1 hb }, simp only [comp_apply, pi_apply, id_apply, proj_apply, subtype_apply, cod_restrict_apply], split_ifs, { refl }, { exact (hb _ $ (hu trivial).resolve_left h).symm } } end end section variable [decidable_eq ι] /-- `diag i j` is the identity map if `i = j`. Otherwise it is the constant 0 map. -/ def diag (i j : ι) : φ i →ₗ[R] φ j := @function.update ι (λj, φ i →ₗ[R] φ j) _ 0 i id j lemma update_apply (f : Πi, M₂ →ₗ[R] φ i) (c : M₂) (i j : ι) (b : M₂ →ₗ[R] φ i) : (update f i b j) c = update (λi, f i c) i (b c) j := begin by_cases j = i, { rw [h, update_same, update_same] }, { rw [update_noteq h, update_noteq h] } end end end linear_map namespace submodule variables [semiring R] {φ : ι → Type*} [∀ i, add_comm_monoid (φ i)] [∀ i, module R (φ i)] open linear_map /-- A version of `set.pi` for submodules. Given an index set `I` and a family of submodules `p : Π i, submodule R (φ i)`, `pi I s` is the submodule of dependent functions `f : Π i, φ i` such that `f i` belongs to `p a` whenever `i ∈ I`. -/ def pi (I : set ι) (p : Π i, submodule R (φ i)) : submodule R (Π i, φ i) := { carrier := set.pi I (λ i, p i), zero_mem' := λ i hi, (p i).zero_mem, add_mem' := λ x y hx hy i hi, (p i).add_mem (hx i hi) (hy i hi), smul_mem' := λ c x hx i hi, (p i).smul_mem c (hx i hi) } variables {I : set ι} {p : Π i, submodule R (φ i)} {x : Π i, φ i} @[simp] lemma mem_pi : x ∈ pi I p ↔ ∀ i ∈ I, x i ∈ p i := iff.rfl @[simp, norm_cast] lemma coe_pi : (pi I p : set (Π i, φ i)) = set.pi I (λ i, p i) := rfl lemma binfi_comap_proj : (⨅ i ∈ I, comap (proj i) (p i)) = pi I p := by { ext x, simp } lemma infi_comap_proj : (⨅ i, comap (proj i) (p i)) = pi set.univ p := by { ext x, simp } lemma supr_map_single [decidable_eq ι] [fintype ι] : (⨆ i, map (linear_map.single i) (p i)) = pi set.univ p := begin refine (supr_le $ λ i, _).antisymm _, { rintro _ ⟨x, hx : x ∈ p i, rfl⟩ j -, rcases em (j = i) with rfl|hj; simp * }, { intros x hx, rw [← finset.univ_sum_single x], exact sum_mem_supr (λ i, mem_map_of_mem (hx i trivial)) } end end submodule namespace linear_equiv variables [semiring R] {φ ψ χ : ι → Type*} [∀ i, add_comm_monoid (φ i)] [∀ i, module R (φ i)] variables [∀ i, add_comm_monoid (ψ i)] [∀ i, module R (ψ i)] variables [∀ i, add_comm_monoid (χ i)] [∀ i, module R (χ i)] /-- Combine a family of linear equivalences into a linear equivalence of `pi`-types. This is `equiv.Pi_congr_right` as a `linear_equiv` -/ @[simps apply] def Pi_congr_right (e : Π i, φ i ≃ₗ[R] ψ i) : (Π i, φ i) ≃ₗ[R] (Π i, ψ i) := { to_fun := λ f i, e i (f i), inv_fun := λ f i, (e i).symm (f i), map_smul' := λ c f, by { ext, simp }, .. add_equiv.Pi_congr_right (λ j, (e j).to_add_equiv) } @[simp] lemma Pi_congr_right_refl : Pi_congr_right (λ j, refl R (φ j)) = refl _ _ := rfl @[simp] lemma Pi_congr_right_symm (e : Π i, φ i ≃ₗ[R] ψ i) : (Pi_congr_right e).symm = (Pi_congr_right $ λ i, (e i).symm) := rfl @[simp] lemma Pi_congr_right_trans (e : Π i, φ i ≃ₗ[R] ψ i) (f : Π i, ψ i ≃ₗ[R] χ i) : (Pi_congr_right e).trans (Pi_congr_right f) = (Pi_congr_right $ λ i, (e i).trans (f i)) := rfl variables (R φ) /-- Transport dependent functions through an equivalence of the base space. This is `equiv.Pi_congr_left'` as a `linear_equiv`. -/ @[simps {simp_rhs := tt}] def Pi_congr_left' (e : ι ≃ ι') : (Π i', φ i') ≃ₗ[R] (Π i, φ $ e.symm i) := { map_add' := λ x y, rfl, map_smul' := λ x y, rfl, .. equiv.Pi_congr_left' φ e } /-- Transporting dependent functions through an equivalence of the base, expressed as a "simplification". This is `equiv.Pi_congr_left` as a `linear_equiv` -/ def Pi_congr_left (e : ι' ≃ ι) : (Π i', φ (e i')) ≃ₗ[R] (Π i, φ i) := (Pi_congr_left' R φ e.symm).symm /-- This is `equiv.pi_option_equiv_prod` as a `linear_equiv` -/ def pi_option_equiv_prod {ι : Type*} {M : option ι → Type*} [Π i, add_comm_group (M i)] [Π i, module R (M i)] : (Π i : option ι, M i) ≃ₗ[R] (M none × Π i : ι, M (some i)) := { map_add' := by simp [function.funext_iff], map_smul' := by simp [function.funext_iff], ..equiv.pi_option_equiv_prod } variables (ι R M) (S : Type*) [fintype ι] [decidable_eq ι] [semiring S] [add_comm_monoid M] [module R M] [module S M] [smul_comm_class R S M] /-- Linear equivalence between linear functions `Rⁿ → M` and `Mⁿ`. The spaces `Rⁿ` and `Mⁿ` are represented as `ι → R` and `ι → M`, respectively, where `ι` is a finite type. This as an `S`-linear equivalence, under the assumption that `S` acts on `M` commuting with `R`. When `R` is commutative, we can take this to be the usual action with `S = R`. Otherwise, `S = ℕ` shows that the equivalence is additive. See note [bundled maps over different rings]. -/ def pi_ring : ((ι → R) →ₗ[R] M) ≃ₗ[S] (ι → M) := (linear_map.lsum R (λ i : ι, R) S).symm.trans (Pi_congr_right $ λ i, linear_map.ring_lmap_equiv_self R M S) variables {ι R M} @[simp] lemma pi_ring_apply (f : (ι → R) →ₗ[R] M) (i : ι) : pi_ring R M ι S f i = f (pi.single i 1) := rfl @[simp] lemma pi_ring_symm_apply (f : ι → M) (g : ι → R) : (pi_ring R M ι S).symm f g = ∑ i, g i • f i := by simp [pi_ring, linear_map.lsum] /-- `equiv.sum_arrow_equiv_prod_arrow` as a linear equivalence. -/ -- TODO additive version? def sum_arrow_lequiv_prod_arrow (α β R M : Type*) [semiring R] [add_comm_monoid M] [module R M] : ((α ⊕ β) → M) ≃ₗ[R] (α → M) × (β → M) := { map_add' := by { intros f g, ext; refl }, map_smul' := by { intros r f, ext; refl, }, .. equiv.sum_arrow_equiv_prod_arrow α β M, } @[simp] lemma sum_arrow_lequiv_prod_arrow_apply_fst {α β} (f : (α ⊕ β) → M) (a : α) : (sum_arrow_lequiv_prod_arrow α β R M f).1 a = f (sum.inl a) := rfl @[simp] lemma sum_arrow_lequiv_prod_arrow_apply_snd {α β} (f : (α ⊕ β) → M) (b : β) : (sum_arrow_lequiv_prod_arrow α β R M f).2 b = f (sum.inr b) := rfl @[simp] lemma sum_arrow_lequiv_prod_arrow_symm_apply_inl {α β} (f : α → M) (g : β → M) (a : α) : ((sum_arrow_lequiv_prod_arrow α β R M).symm (f, g)) (sum.inl a) = f a := rfl @[simp] lemma sum_arrow_lequiv_prod_arrow_symm_apply_inr {α β} (f : α → M) (g : β → M) (b : β) : ((sum_arrow_lequiv_prod_arrow α β R M).symm (f, g)) (sum.inr b) = g b := rfl end linear_equiv section extend variables (R) {η : Type x} [semiring R] (s : ι → η) /-- `function.extend s f 0` as a bundled linear map. -/ @[simps] noncomputable def function.extend_by_zero.linear_map : (ι → R) →ₗ[R] (η → R) := { to_fun := λ f, function.extend s f 0, map_smul' := λ r f, by { simpa using function.extend_smul r s f 0 }, ..function.extend_by_zero.hom R s } end extend
c8aae2fad414682c84762696a199f680c34a7d09
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/topology/order.lean
a1377c4cfd675300af195cb2f4257f95a5a1df58
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
37,904
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import topology.tactic /-! # Ordering on topologies and (co)induced topologies Topologies on a fixed type `α` are ordered, by reverse inclusion. That is, for topologies `t₁` and `t₂` on `α`, we write `t₁ ≤ t₂` if every set open in `t₂` is also open in `t₁`. (One also calls `t₁` finer than `t₂`, and `t₂` coarser than `t₁`.) Any function `f : α → β` induces `induced f : topological_space β → topological_space α` and `coinduced f : topological_space α → topological_space β`. Continuity, the ordering on topologies and (co)induced topologies are related as follows: * The identity map (α, t₁) → (α, t₂) is continuous iff t₁ ≤ t₂. * A map f : (α, t) → (β, u) is continuous iff t ≤ induced f u (`continuous_iff_le_induced`) iff coinduced f t ≤ u (`continuous_iff_coinduced_le`). Topologies on α form a complete lattice, with ⊥ the discrete topology and ⊤ the indiscrete topology. For a function f : α → β, (coinduced f, induced f) is a Galois connection between topologies on α and topologies on β. ## Implementation notes There is a Galois insertion between topologies on α (with the inclusion ordering) and all collections of sets in α. The complete lattice structure on topologies on α is defined as the reverse of the one obtained via this Galois insertion. ## Tags finer, coarser, induced topology, coinduced topology -/ open set filter classical open_locale classical topological_space filter universes u v w namespace topological_space variables {α : Type u} /-- The open sets of the least topology containing a collection of basic sets. -/ inductive generate_open (g : set (set α)) : set α → Prop | basic : ∀s∈g, generate_open s | univ : generate_open univ | inter : ∀s t, generate_open s → generate_open t → generate_open (s ∩ t) | sUnion : ∀k, (∀s∈k, generate_open s) → generate_open (⋃₀ k) /-- The smallest topological space containing the collection `g` of basic sets -/ def generate_from (g : set (set α)) : topological_space α := { is_open := generate_open g, is_open_univ := generate_open.univ, is_open_inter := generate_open.inter, is_open_sUnion := generate_open.sUnion } lemma is_open_generate_from_of_mem {g : set (set α)} {s : set α} (hs : s ∈ g) : @is_open _ (generate_from g) s := generate_open.basic s hs lemma nhds_generate_from {g : set (set α)} {a : α} : @nhds α (generate_from g) a = (⨅s∈{s | a ∈ s ∧ s ∈ g}, 𝓟 s) := by rw nhds_def; exact le_antisymm (binfi_mono $ λ s ⟨as, sg⟩, ⟨as, generate_open.basic _ sg⟩) (le_infi $ assume s, le_infi $ assume ⟨as, hs⟩, begin revert as, clear_, induction hs, case generate_open.basic : s hs { exact assume as, infi_le_of_le s $ infi_le _ ⟨as, hs⟩ }, case generate_open.univ { rw [principal_univ], exact assume _, le_top }, case generate_open.inter : s t hs' ht' hs ht { exact assume ⟨has, hat⟩, calc _ ≤ 𝓟 s ⊓ 𝓟 t : le_inf (hs has) (ht hat) ... = _ : inf_principal }, case generate_open.sUnion : k hk' hk { exact λ ⟨t, htk, hat⟩, calc _ ≤ 𝓟 t : hk t htk hat ... ≤ _ : le_principal_iff.2 $ subset_sUnion_of_mem htk } end) lemma tendsto_nhds_generate_from {β : Type*} {m : α → β} {f : filter α} {g : set (set β)} {b : β} (h : ∀s∈g, b ∈ s → m ⁻¹' s ∈ f) : tendsto m f (@nhds β (generate_from g) b) := by rw [nhds_generate_from]; exact (tendsto_infi.2 $ assume s, tendsto_infi.2 $ assume ⟨hbs, hsg⟩, tendsto_principal.2 $ h s hsg hbs) /-- Construct a topology on α given the filter of neighborhoods of each point of α. -/ protected def mk_of_nhds (n : α → filter α) : topological_space α := { is_open := λs, ∀a∈s, s ∈ n a, is_open_univ := assume x h, univ_mem, is_open_inter := assume s t hs ht x ⟨hxs, hxt⟩, inter_mem (hs x hxs) (ht x hxt), is_open_sUnion := assume s hs a ⟨x, hx, hxa⟩, mem_of_superset (hs x hx _ hxa) (set.subset_sUnion_of_mem hx) } lemma nhds_mk_of_nhds (n : α → filter α) (a : α) (h₀ : pure ≤ n) (h₁ : ∀{a s}, s ∈ n a → ∃ t ∈ n a, t ⊆ s ∧ ∀a' ∈ t, s ∈ n a') : @nhds α (topological_space.mk_of_nhds n) a = n a := begin letI := topological_space.mk_of_nhds n, refine le_antisymm (assume s hs, _) (assume s hs, _), { have h₀ : {b | s ∈ n b} ⊆ s := assume b hb, mem_pure.1 $ h₀ b hb, have h₁ : {b | s ∈ n b} ∈ 𝓝 a, { refine is_open.mem_nhds (assume b (hb : s ∈ n b), _) hs, rcases h₁ hb with ⟨t, ht, hts, h⟩, exact mem_of_superset ht h }, exact mem_of_superset h₁ h₀ }, { rcases (@mem_nhds_iff α (topological_space.mk_of_nhds n) _ _).1 hs with ⟨t, hts, ht, hat⟩, exact (n a).sets_of_superset (ht _ hat) hts }, end lemma nhds_mk_of_nhds_filter_basis (B : α → filter_basis α) (a : α) (h₀ : ∀ x (n ∈ B x), x ∈ n) (h₁ : ∀ x (n ∈ B x), ∃ n₁ ∈ B x, n₁ ⊆ n ∧ ∀ x' ∈ n₁, ∃ n₂ ∈ B x', n₂ ⊆ n) : @nhds α (topological_space.mk_of_nhds (λ x, (B x).filter)) a = (B a).filter := begin rw topological_space.nhds_mk_of_nhds; intros x n hn; obtain ⟨m, hm₁, hm₂⟩ := (B x).mem_filter_iff.mp hn, { exact hm₂ (h₀ _ _ hm₁), }, { obtain ⟨n₁, hn₁, hn₂, hn₃⟩ := h₁ x m hm₁, refine ⟨n₁, (B x).mem_filter_of_mem hn₁, hn₂.trans hm₂, λ x' hx', (B x').mem_filter_iff.mp _⟩, obtain ⟨n₂, hn₄, hn₅⟩ := hn₃ x' hx', exact ⟨n₂, hn₄, hn₅.trans hm₂⟩, }, end end topological_space section lattice variables {α : Type u} {β : Type v} /-- The inclusion ordering on topologies on α. We use it to get a complete lattice instance via the Galois insertion method, but the partial order that we will eventually impose on `topological_space α` is the reverse one. -/ def tmp_order : partial_order (topological_space α) := { le := λt s, t.is_open ≤ s.is_open, le_antisymm := assume t s h₁ h₂, topological_space_eq $ le_antisymm h₁ h₂, le_refl := assume t, le_refl t.is_open, le_trans := assume a b c h₁ h₂, @le_trans _ _ a.is_open b.is_open c.is_open h₁ h₂ } local attribute [instance] tmp_order /- We'll later restate this lemma in terms of the correct order on `topological_space α`. -/ private lemma generate_from_le_iff_subset_is_open {g : set (set α)} {t : topological_space α} : topological_space.generate_from g ≤ t ↔ g ⊆ {s | t.is_open s} := iff.intro (assume ht s hs, ht _ $ topological_space.generate_open.basic s hs) (assume hg s hs, hs.rec_on (assume v hv, hg hv) t.is_open_univ (assume u v _ _, t.is_open_inter u v) (assume k _, t.is_open_sUnion k)) /-- If `s` equals the collection of open sets in the topology it generates, then `s` defines a topology. -/ protected def mk_of_closure (s : set (set α)) (hs : {u | (topological_space.generate_from s).is_open u} = s) : topological_space α := { is_open := λu, u ∈ s, is_open_univ := hs ▸ topological_space.generate_open.univ, is_open_inter := hs ▸ topological_space.generate_open.inter, is_open_sUnion := hs ▸ topological_space.generate_open.sUnion } lemma mk_of_closure_sets {s : set (set α)} {hs : {u | (topological_space.generate_from s).is_open u} = s} : mk_of_closure s hs = topological_space.generate_from s := topological_space_eq hs.symm /-- The Galois insertion between `set (set α)` and `topological_space α` whose lower part sends a collection of subsets of α to the topology they generate, and whose upper part sends a topology to its collection of open subsets. -/ def gi_generate_from (α : Type*) : galois_insertion topological_space.generate_from (λt:topological_space α, {s | t.is_open s}) := { gc := assume g t, generate_from_le_iff_subset_is_open, le_l_u := assume ts s hs, topological_space.generate_open.basic s hs, choice := λg hg, mk_of_closure g (subset.antisymm hg $ generate_from_le_iff_subset_is_open.1 $ le_rfl), choice_eq := assume s hs, mk_of_closure_sets } lemma generate_from_mono {α} {g₁ g₂ : set (set α)} (h : g₁ ⊆ g₂) : topological_space.generate_from g₁ ≤ topological_space.generate_from g₂ := (gi_generate_from _).gc.monotone_l h lemma generate_from_set_of_is_open (t : topological_space α) : topological_space.generate_from {s | t.is_open s} = t := (gi_generate_from α).l_u_eq t lemma left_inverse_generate_from : function.left_inverse topological_space.generate_from (λ t : topological_space α, {s | t.is_open s}) := (gi_generate_from α).left_inverse_l_u lemma generate_from_surjective : function.surjective (topological_space.generate_from : set (set α) → topological_space α) := (gi_generate_from α).l_surjective lemma set_of_is_open_injective : function.injective (λ t : topological_space α, {s | t.is_open s}) := (gi_generate_from α).u_injective /-- The "temporary" order `tmp_order` on `topological_space α`, i.e. the inclusion order, is a complete lattice. (Note that later `topological_space α` will equipped with the dual order to `tmp_order`). -/ def tmp_complete_lattice {α : Type u} : complete_lattice (topological_space α) := (gi_generate_from α).lift_complete_lattice instance : has_le (topological_space α) := { le := λ t s, s.is_open ≤ t.is_open } protected lemma topological_space.le_def {α} {t s : topological_space α} : t ≤ s ↔ s.is_open ≤ t.is_open := iff.rfl /-- The ordering on topologies on the type `α`. `t ≤ s` if every set open in `s` is also open in `t` (`t` is finer than `s`). -/ instance : partial_order (topological_space α) := { le_antisymm := assume t s h₁ h₂, topological_space_eq $ le_antisymm h₂ h₁, le_refl := assume t, le_refl t.is_open, le_trans := assume a b c h₁ h₂, topological_space.le_def.mpr (le_trans h₂ h₁), ..topological_space.has_le } lemma le_generate_from_iff_subset_is_open {g : set (set α)} {t : topological_space α} : t ≤ topological_space.generate_from g ↔ g ⊆ {s | t.is_open s} := generate_from_le_iff_subset_is_open /-- Topologies on `α` form a complete lattice, with `⊥` the discrete topology and `⊤` the indiscrete topology. The infimum of a collection of topologies is the topology generated by all their open sets, while the supremum is the topology whose open sets are those sets open in every member of the collection. -/ instance : complete_lattice (topological_space α) := @order_dual.complete_lattice _ tmp_complete_lattice lemma is_open_implies_is_open_iff {a b : topological_space α} : (∀ s, a.is_open s → b.is_open s) ↔ b ≤ a := iff.rfl /-- A topological space is discrete if every set is open, that is, its topology equals the discrete topology `⊥`. -/ class discrete_topology (α : Type*) [t : topological_space α] : Prop := (eq_bot [] : t = ⊥) @[priority 100] instance discrete_topology_bot (α : Type*) : @discrete_topology α ⊥ := { eq_bot := rfl } @[simp] lemma is_open_discrete [topological_space α] [discrete_topology α] (s : set α) : is_open s := (discrete_topology.eq_bot α).symm ▸ trivial @[simp] lemma is_closed_discrete [topological_space α] [discrete_topology α] (s : set α) : is_closed s := is_open_compl_iff.1 $ (discrete_topology.eq_bot α).symm ▸ trivial @[nontriviality] lemma continuous_of_discrete_topology [topological_space α] [discrete_topology α] [topological_space β] {f : α → β} : continuous f := continuous_def.2 $ λs hs, is_open_discrete _ lemma nhds_bot (α : Type*) : (@nhds α ⊥) = pure := begin refine le_antisymm _ (@pure_le_nhds α ⊥), assume a s hs, exact @is_open.mem_nhds α ⊥ a s trivial hs end lemma nhds_discrete (α : Type*) [topological_space α] [discrete_topology α] : (@nhds α _) = pure := (discrete_topology.eq_bot α).symm ▸ nhds_bot α lemma mem_nhds_discrete [topological_space α] [discrete_topology α] {x : α} {s : set α} : s ∈ 𝓝 x ↔ x ∈ s := by rw [nhds_discrete, mem_pure] lemma le_of_nhds_le_nhds {t₁ t₂ : topological_space α} (h : ∀x, @nhds α t₁ x ≤ @nhds α t₂ x) : t₁ ≤ t₂ := assume s, show @is_open α t₂ s → @is_open α t₁ s, by { simp only [is_open_iff_nhds, le_principal_iff], exact assume hs a ha, h _ $ hs _ ha } lemma eq_of_nhds_eq_nhds {t₁ t₂ : topological_space α} (h : ∀x, @nhds α t₁ x = @nhds α t₂ x) : t₁ = t₂ := le_antisymm (le_of_nhds_le_nhds $ assume x, le_of_eq $ h x) (le_of_nhds_le_nhds $ assume x, le_of_eq $ (h x).symm) lemma eq_bot_of_singletons_open {t : topological_space α} (h : ∀ x, t.is_open {x}) : t = ⊥ := bot_unique $ λ s hs, bUnion_of_singleton s ▸ is_open_bUnion (λ x _, h x) lemma forall_open_iff_discrete {X : Type*} [topological_space X] : (∀ s : set X, is_open s) ↔ discrete_topology X := ⟨λ h, ⟨by { ext U , show is_open U ↔ true, simp [h U] }⟩, λ a, @is_open_discrete _ _ a⟩ lemma singletons_open_iff_discrete {X : Type*} [topological_space X] : (∀ a : X, is_open ({a} : set X)) ↔ discrete_topology X := ⟨λ h, ⟨eq_bot_of_singletons_open h⟩, λ a _, @is_open_discrete _ _ a _⟩ end lattice section galois_connection variables {α : Type*} {β : Type*} {γ : Type*} /-- Given `f : α → β` and a topology on `β`, the induced topology on `α` is the collection of sets that are preimages of some open set in `β`. This is the coarsest topology that makes `f` continuous. -/ def topological_space.induced {α : Type u} {β : Type v} (f : α → β) (t : topological_space β) : topological_space α := { is_open := λs, ∃s', t.is_open s' ∧ f ⁻¹' s' = s, is_open_univ := ⟨univ, t.is_open_univ, preimage_univ⟩, is_open_inter := by rintro s₁ s₂ ⟨s'₁, hs₁, rfl⟩ ⟨s'₂, hs₂, rfl⟩; exact ⟨s'₁ ∩ s'₂, t.is_open_inter _ _ hs₁ hs₂, preimage_inter⟩, is_open_sUnion := assume s h, begin simp only [classical.skolem] at h, cases h with f hf, apply exists.intro (⋃(x : set α) (h : x ∈ s), f x h), simp only [sUnion_eq_bUnion, preimage_Union, (λx h, (hf x h).right)], refine ⟨_, rfl⟩, exact (@is_open_Union β _ t _ $ assume i, show is_open (⋃h, f i h), from @is_open_Union β _ t _ $ assume h, (hf i h).left) end } lemma is_open_induced_iff [t : topological_space β] {s : set α} {f : α → β} : @is_open α (t.induced f) s ↔ (∃t, is_open t ∧ f ⁻¹' t = s) := iff.rfl lemma is_open_induced_iff' [t : topological_space β] {s : set α} {f : α → β} : (t.induced f).is_open s ↔ (∃t, is_open t ∧ f ⁻¹' t = s) := iff.rfl lemma is_closed_induced_iff [t : topological_space β] {s : set α} {f : α → β} : @is_closed α (t.induced f) s ↔ (∃t, is_closed t ∧ f ⁻¹' t = s) := begin simp only [← is_open_compl_iff, is_open_induced_iff], exact compl_surjective.exists.trans (by simp only [preimage_compl, compl_inj_iff]) end /-- Given `f : α → β` and a topology on `α`, the coinduced topology on `β` is defined such that `s:set β` is open if the preimage of `s` is open. This is the finest topology that makes `f` continuous. -/ def topological_space.coinduced {α : Type u} {β : Type v} (f : α → β) (t : topological_space α) : topological_space β := { is_open := λs, t.is_open (f ⁻¹' s), is_open_univ := by rw preimage_univ; exact t.is_open_univ, is_open_inter := assume s₁ s₂ h₁ h₂, by rw preimage_inter; exact t.is_open_inter _ _ h₁ h₂, is_open_sUnion := assume s h, by rw [preimage_sUnion]; exact (@is_open_Union _ _ t _ $ assume i, show is_open (⋃ (H : i ∈ s), f ⁻¹' i), from @is_open_Union _ _ t _ $ assume hi, h i hi) } lemma is_open_coinduced {t : topological_space α} {s : set β} {f : α → β} : @is_open β (topological_space.coinduced f t) s ↔ is_open (f ⁻¹' s) := iff.rfl lemma preimage_nhds_coinduced [topological_space α] {π : α → β} {s : set β} {a : α} (hs : s ∈ @nhds β (topological_space.coinduced π ‹_›) (π a)) : π ⁻¹' s ∈ 𝓝 a := begin letI := topological_space.coinduced π ‹_›, rcases mem_nhds_iff.mp hs with ⟨V, hVs, V_op, mem_V⟩, exact mem_nhds_iff.mpr ⟨π ⁻¹' V, set.preimage_mono hVs, V_op, mem_V⟩ end variables {t t₁ t₂ : topological_space α} {t' : topological_space β} {f : α → β} {g : β → α} lemma continuous.coinduced_le (h : @continuous α β t t' f) : t.coinduced f ≤ t' := λ s hs, (continuous_def.1 h s hs : _) lemma coinduced_le_iff_le_induced {f : α → β} {tα : topological_space α} {tβ : topological_space β} : tα.coinduced f ≤ tβ ↔ tα ≤ tβ.induced f := iff.intro (assume h s ⟨t, ht, hst⟩, hst ▸ h _ ht) (assume h s hs, show tα.is_open (f ⁻¹' s), from h _ ⟨s, hs, rfl⟩) lemma continuous.le_induced (h : @continuous α β t t' f) : t ≤ t'.induced f := coinduced_le_iff_le_induced.1 h.coinduced_le lemma gc_coinduced_induced (f : α → β) : galois_connection (topological_space.coinduced f) (topological_space.induced f) := assume f g, coinduced_le_iff_le_induced lemma induced_mono (h : t₁ ≤ t₂) : t₁.induced g ≤ t₂.induced g := (gc_coinduced_induced g).monotone_u h lemma coinduced_mono (h : t₁ ≤ t₂) : t₁.coinduced f ≤ t₂.coinduced f := (gc_coinduced_induced f).monotone_l h @[simp] lemma induced_top : (⊤ : topological_space α).induced g = ⊤ := (gc_coinduced_induced g).u_top @[simp] lemma induced_inf : (t₁ ⊓ t₂).induced g = t₁.induced g ⊓ t₂.induced g := (gc_coinduced_induced g).u_inf @[simp] lemma induced_infi {ι : Sort w} {t : ι → topological_space α} : (⨅i, t i).induced g = (⨅i, (t i).induced g) := (gc_coinduced_induced g).u_infi @[simp] lemma coinduced_bot : (⊥ : topological_space α).coinduced f = ⊥ := (gc_coinduced_induced f).l_bot @[simp] lemma coinduced_sup : (t₁ ⊔ t₂).coinduced f = t₁.coinduced f ⊔ t₂.coinduced f := (gc_coinduced_induced f).l_sup @[simp] lemma coinduced_supr {ι : Sort w} {t : ι → topological_space α} : (⨆i, t i).coinduced f = (⨆i, (t i).coinduced f) := (gc_coinduced_induced f).l_supr lemma induced_id [t : topological_space α] : t.induced id = t := topological_space_eq $ funext $ assume s, propext $ ⟨assume ⟨s', hs, h⟩, h ▸ hs, assume hs, ⟨s, hs, rfl⟩⟩ lemma induced_compose [tγ : topological_space γ] {f : α → β} {g : β → γ} : (tγ.induced g).induced f = tγ.induced (g ∘ f) := topological_space_eq $ funext $ assume s, propext $ ⟨assume ⟨s', ⟨s, hs, h₂⟩, h₁⟩, h₁ ▸ h₂ ▸ ⟨s, hs, rfl⟩, assume ⟨s, hs, h⟩, ⟨preimage g s, ⟨s, hs, rfl⟩, h ▸ rfl⟩⟩ lemma induced_const [t : topological_space α] {x : α} : t.induced (λ y : β, x) = ⊤ := le_antisymm le_top (@continuous_const β α ⊤ t x).le_induced lemma coinduced_id [t : topological_space α] : t.coinduced id = t := topological_space_eq rfl lemma coinduced_compose [tα : topological_space α] {f : α → β} {g : β → γ} : (tα.coinduced f).coinduced g = tα.coinduced (g ∘ f) := topological_space_eq rfl lemma equiv.induced_symm {α β : Type*} (e : α ≃ β) : topological_space.induced e.symm = topological_space.coinduced e := begin ext t U, split, { rintros ⟨V, hV, rfl⟩, change t.is_open (e ⁻¹' _), rwa [← preimage_comp, ← equiv.coe_trans, equiv.self_trans_symm] }, { intros hU, refine ⟨e ⁻¹' U, hU, _⟩, rw [← preimage_comp, ← equiv.coe_trans, equiv.symm_trans_self, equiv.coe_refl, preimage_id] } end lemma equiv.coinduced_symm {α β : Type*} (e : α ≃ β) : topological_space.coinduced e.symm = topological_space.induced e := by rw [← e.symm.induced_symm, e.symm_symm] end galois_connection /- constructions using the complete lattice structure -/ section constructions open topological_space variables {α : Type u} {β : Type v} instance inhabited_topological_space {α : Type u} : inhabited (topological_space α) := ⟨⊤⟩ @[priority 100] instance subsingleton.unique_topological_space [subsingleton α] : unique (topological_space α) := { default := ⊥, uniq := λ t, eq_bot_of_singletons_open $ λ x, subsingleton.set_cases (@is_open_empty _ t) (@is_open_univ _ t) ({x} : set α) } @[priority 100] instance subsingleton.discrete_topology [t : topological_space α] [subsingleton α] : discrete_topology α := ⟨unique.eq_default t⟩ instance : topological_space empty := ⊥ instance : discrete_topology empty := ⟨rfl⟩ instance : topological_space pempty := ⊥ instance : discrete_topology pempty := ⟨rfl⟩ instance : topological_space punit := ⊥ instance : discrete_topology punit := ⟨rfl⟩ instance : topological_space bool := ⊥ instance : discrete_topology bool := ⟨rfl⟩ instance : topological_space ℕ := ⊥ instance : discrete_topology ℕ := ⟨rfl⟩ instance : topological_space ℤ := ⊥ instance : discrete_topology ℤ := ⟨rfl⟩ instance sierpinski_space : topological_space Prop := generate_from {{true}} lemma continuous_empty_function [topological_space α] [topological_space β] [is_empty β] (f : α → β) : continuous f := by { letI := function.is_empty f, exact continuous_of_discrete_topology } lemma le_generate_from {t : topological_space α} { g : set (set α) } (h : ∀s∈g, is_open s) : t ≤ generate_from g := le_generate_from_iff_subset_is_open.2 h lemma induced_generate_from_eq {α β} {b : set (set β)} {f : α → β} : (generate_from b).induced f = topological_space.generate_from (preimage f '' b) := le_antisymm (le_generate_from $ ball_image_iff.2 $ assume s hs, ⟨s, generate_open.basic _ hs, rfl⟩) (coinduced_le_iff_le_induced.1 $ le_generate_from $ assume s hs, generate_open.basic _ $ mem_image_of_mem _ hs) lemma le_induced_generate_from {α β} [t : topological_space α] {b : set (set β)} {f : α → β} (h : ∀ (a : set β), a ∈ b → is_open (f ⁻¹' a)) : t ≤ induced f (generate_from b) := begin rw induced_generate_from_eq, apply le_generate_from, simp only [mem_image, and_imp, forall_apply_eq_imp_iff₂, exists_imp_distrib], exact h, end /-- This construction is left adjoint to the operation sending a topology on `α` to its neighborhood filter at a fixed point `a : α`. -/ def nhds_adjoint (a : α) (f : filter α) : topological_space α := { is_open := λs, a ∈ s → s ∈ f, is_open_univ := assume s, univ_mem, is_open_inter := assume s t hs ht ⟨has, hat⟩, inter_mem (hs has) (ht hat), is_open_sUnion := assume k hk ⟨u, hu, hau⟩, mem_of_superset (hk u hu hau) (subset_sUnion_of_mem hu) } lemma gc_nhds (a : α) : galois_connection (nhds_adjoint a) (λt, @nhds α t a) := assume f t, by { rw le_nhds_iff, exact ⟨λ H s hs has, H _ has hs, λ H s has hs, H _ hs has⟩ } lemma nhds_mono {t₁ t₂ : topological_space α} {a : α} (h : t₁ ≤ t₂) : @nhds α t₁ a ≤ @nhds α t₂ a := (gc_nhds a).monotone_u h lemma le_iff_nhds {α : Type*} (t t' : topological_space α) : t ≤ t' ↔ ∀ x, @nhds α t x ≤ @nhds α t' x := ⟨λ h x, nhds_mono h, le_of_nhds_le_nhds⟩ lemma nhds_adjoint_nhds {α : Type*} (a : α) (f : filter α) : @nhds α (nhds_adjoint a f) a = pure a ⊔ f := begin ext U, rw mem_nhds_iff, split, { rintros ⟨t, htU, ht, hat⟩, exact ⟨htU hat, mem_of_superset (ht hat) htU⟩}, { rintros ⟨haU, hU⟩, exact ⟨U, subset.rfl, λ h, hU, haU⟩ } end lemma nhds_adjoint_nhds_of_ne {α : Type*} (a : α) (f : filter α) {b : α} (h : b ≠ a) : @nhds α (nhds_adjoint a f) b = pure b := begin apply le_antisymm, { intros U hU, rw mem_nhds_iff, use {b}, simp only [and_true, singleton_subset_iff, mem_singleton], refine ⟨hU, λ ha, (h.symm ha).elim⟩ }, { exact @pure_le_nhds α (nhds_adjoint a f) b }, end lemma is_open_singleton_nhds_adjoint {α : Type*} {a b : α} (f : filter α) (hb : b ≠ a) : @is_open α (nhds_adjoint a f) {b} := begin rw is_open_singleton_iff_nhds_eq_pure, exact nhds_adjoint_nhds_of_ne a f hb end lemma le_nhds_adjoint_iff' {α : Type*} (a : α) (f : filter α) (t : topological_space α) : t ≤ nhds_adjoint a f ↔ @nhds α t a ≤ pure a ⊔ f ∧ ∀ b ≠ a, @nhds α t b = pure b := begin rw le_iff_nhds, split, { intros h, split, { specialize h a, rwa nhds_adjoint_nhds at h }, { intros b hb, apply le_antisymm _ (pure_le_nhds b), specialize h b, rwa nhds_adjoint_nhds_of_ne a f hb at h } }, { rintros ⟨h, h'⟩ b, by_cases hb : b = a, { rwa [hb, nhds_adjoint_nhds] }, { simp [nhds_adjoint_nhds_of_ne a f hb, h' b hb] } } end lemma le_nhds_adjoint_iff {α : Type*} (a : α) (f : filter α) (t : topological_space α) : t ≤ nhds_adjoint a f ↔ (@nhds α t a ≤ pure a ⊔ f ∧ ∀ b, b ≠ a → t.is_open {b}) := begin change _ ↔ _ ∧ ∀ (b : α), b ≠ a → is_open {b}, rw [le_nhds_adjoint_iff', and.congr_right_iff], apply λ h, forall_congr (λ b, _), rw @is_open_singleton_iff_nhds_eq_pure α t b end lemma nhds_infi {ι : Sort*} {t : ι → topological_space α} {a : α} : @nhds α (infi t) a = (⨅i, @nhds α (t i) a) := (gc_nhds a).u_infi lemma nhds_Inf {s : set (topological_space α)} {a : α} : @nhds α (Inf s) a = (⨅t∈s, @nhds α t a) := (gc_nhds a).u_Inf lemma nhds_inf {t₁ t₂ : topological_space α} {a : α} : @nhds α (t₁ ⊓ t₂) a = @nhds α t₁ a ⊓ @nhds α t₂ a := (gc_nhds a).u_inf lemma nhds_top {a : α} : @nhds α ⊤ a = ⊤ := (gc_nhds a).u_top lemma is_open_sup {t₁ t₂ : topological_space α} {s : set α} : @is_open α (t₁ ⊔ t₂) s ↔ @is_open α t₁ s ∧ @is_open α t₂ s := iff.rfl local notation `cont` := @continuous _ _ local notation `tspace` := topological_space open topological_space variables {γ : Type*} {f : α → β} {ι : Sort*} lemma continuous_iff_coinduced_le {t₁ : tspace α} {t₂ : tspace β} : cont t₁ t₂ f ↔ coinduced f t₁ ≤ t₂ := continuous_def.trans iff.rfl lemma continuous_iff_le_induced {t₁ : tspace α} {t₂ : tspace β} : cont t₁ t₂ f ↔ t₁ ≤ induced f t₂ := iff.trans continuous_iff_coinduced_le (gc_coinduced_induced f _ _) theorem continuous_generated_from {t : tspace α} {b : set (set β)} (h : ∀s∈b, is_open (f ⁻¹' s)) : cont t (generate_from b) f := continuous_iff_coinduced_le.2 $ le_generate_from h @[continuity] lemma continuous_induced_dom {t : tspace β} : cont (induced f t) t f := by { rw continuous_def, assume s h, exact ⟨_, h, rfl⟩ } lemma continuous_induced_rng {g : γ → α} {t₂ : tspace β} {t₁ : tspace γ} : cont t₁ (induced f t₂) g ↔ cont t₁ t₂ (f ∘ g) := by simp only [continuous_iff_le_induced, induced_compose] lemma continuous_coinduced_rng {t : tspace α} : cont t (coinduced f t) f := by { rw continuous_def, assume s h, exact h } lemma continuous_coinduced_dom {g : β → γ} {t₁ : tspace α} {t₂ : tspace γ} : cont (coinduced f t₁) t₂ g ↔ cont t₁ t₂ (g ∘ f) := by simp only [continuous_iff_coinduced_le, coinduced_compose] lemma continuous_le_dom {t₁ t₂ : tspace α} {t₃ : tspace β} (h₁ : t₂ ≤ t₁) (h₂ : cont t₁ t₃ f) : cont t₂ t₃ f := begin rw continuous_def at h₂ ⊢, assume s h, exact h₁ _ (h₂ s h) end lemma continuous_le_rng {t₁ : tspace α} {t₂ t₃ : tspace β} (h₁ : t₂ ≤ t₃) (h₂ : cont t₁ t₂ f) : cont t₁ t₃ f := begin rw continuous_def at h₂ ⊢, assume s h, exact h₂ s (h₁ s h) end lemma continuous_sup_dom {t₁ t₂ : tspace α} {t₃ : tspace β} : cont (t₁ ⊔ t₂) t₃ f ↔ cont t₁ t₃ f ∧ cont t₂ t₃ f := by simp only [continuous_iff_le_induced, sup_le_iff] lemma continuous_sup_rng_left {t₁ : tspace α} {t₃ t₂ : tspace β} : cont t₁ t₂ f → cont t₁ (t₂ ⊔ t₃) f := continuous_le_rng le_sup_left lemma continuous_sup_rng_right {t₁ : tspace α} {t₃ t₂ : tspace β} : cont t₁ t₃ f → cont t₁ (t₂ ⊔ t₃) f := continuous_le_rng le_sup_right lemma continuous_Sup_dom {T : set (tspace α)} {t₂ : tspace β} : cont (Sup T) t₂ f ↔ ∀ t ∈ T, cont t t₂ f := by simp only [continuous_iff_le_induced, Sup_le_iff] lemma continuous_Sup_rng {t₁ : tspace α} {t₂ : set (tspace β)} {t : tspace β} (h₁ : t ∈ t₂) (hf : cont t₁ t f) : cont t₁ (Sup t₂) f := continuous_iff_coinduced_le.2 $ le_Sup_of_le h₁ $ continuous_iff_coinduced_le.1 hf lemma continuous_supr_dom {t₁ : ι → tspace α} {t₂ : tspace β} : cont (supr t₁) t₂ f ↔ ∀ i, cont (t₁ i) t₂ f := by simp only [continuous_iff_le_induced, supr_le_iff] lemma continuous_supr_rng {t₁ : tspace α} {t₂ : ι → tspace β} {i : ι} (h : cont t₁ (t₂ i) f) : cont t₁ (supr t₂) f := continuous_Sup_rng ⟨i, rfl⟩ h lemma continuous_inf_rng {t₁ : tspace α} {t₂ t₃ : tspace β} : cont t₁ (t₂ ⊓ t₃) f ↔ cont t₁ t₂ f ∧ cont t₁ t₃ f := by simp only [continuous_iff_coinduced_le, le_inf_iff] lemma continuous_inf_dom_left {t₁ t₂ : tspace α} {t₃ : tspace β} : cont t₁ t₃ f → cont (t₁ ⊓ t₂) t₃ f := continuous_le_dom inf_le_left lemma continuous_inf_dom_right {t₁ t₂ : tspace α} {t₃ : tspace β} : cont t₂ t₃ f → cont (t₁ ⊓ t₂) t₃ f := continuous_le_dom inf_le_right lemma continuous_Inf_dom {t₁ : set (tspace α)} {t₂ : tspace β} {t : tspace α} (h₁ : t ∈ t₁) : cont t t₂ f → cont (Inf t₁) t₂ f := continuous_le_dom $ Inf_le h₁ lemma continuous_Inf_rng {t₁ : tspace α} {T : set (tspace β)} : cont t₁ (Inf T) f ↔ ∀ t ∈ T, cont t₁ t f := by simp only [continuous_iff_coinduced_le, le_Inf_iff] lemma continuous_infi_dom {t₁ : ι → tspace α} {t₂ : tspace β} {i : ι} : cont (t₁ i) t₂ f → cont (infi t₁) t₂ f := continuous_le_dom $ infi_le _ _ lemma continuous_infi_rng {t₁ : tspace α} {t₂ : ι → tspace β} : cont t₁ (infi t₂) f ↔ ∀ i, cont t₁ (t₂ i) f := by simp only [continuous_iff_coinduced_le, le_infi_iff] @[continuity] lemma continuous_bot {t : tspace β} : cont ⊥ t f := continuous_iff_le_induced.2 $ bot_le @[continuity] lemma continuous_top {t : tspace α} : cont t ⊤ f := continuous_iff_coinduced_le.2 $ le_top lemma continuous_id_iff_le {t t' : tspace α} : cont t t' id ↔ t ≤ t' := @continuous_def _ _ t t' id lemma continuous_id_of_le {t t' : tspace α} (h : t ≤ t') : cont t t' id := continuous_id_iff_le.2 h /- 𝓝 in the induced topology -/ theorem mem_nhds_induced [T : topological_space α] (f : β → α) (a : β) (s : set β) : s ∈ @nhds β (topological_space.induced f T) a ↔ ∃ u ∈ 𝓝 (f a), f ⁻¹' u ⊆ s := begin simp only [mem_nhds_iff, is_open_induced_iff, exists_prop, set.mem_set_of_eq], split, { rintros ⟨u, usub, ⟨v, openv, ueq⟩, au⟩, exact ⟨v, ⟨v, set.subset.refl v, openv, by rwa ←ueq at au⟩, by rw ueq; exact usub⟩ }, rintros ⟨u, ⟨v, vsubu, openv, amem⟩, finvsub⟩, exact ⟨f ⁻¹' v, set.subset.trans (set.preimage_mono vsubu) finvsub, ⟨⟨v, openv, rfl⟩, amem⟩⟩ end theorem nhds_induced [T : topological_space α] (f : β → α) (a : β) : @nhds β (topological_space.induced f T) a = comap f (𝓝 (f a)) := by { ext s, rw [mem_nhds_induced, mem_comap] } lemma induced_iff_nhds_eq [tα : topological_space α] [tβ : topological_space β] (f : β → α) : tβ = tα.induced f ↔ ∀ b, 𝓝 b = comap f (𝓝 $ f b) := ⟨λ h a, h.symm ▸ nhds_induced f a, λ h, eq_of_nhds_eq_nhds $ λ x, by rw [h, nhds_induced]⟩ theorem map_nhds_induced_of_surjective [T : topological_space α] {f : β → α} (hf : function.surjective f) (a : β) : map f (@nhds β (topological_space.induced f T) a) = 𝓝 (f a) := by rw [nhds_induced, map_comap_of_surjective hf] end constructions section induced open topological_space variables {α : Type*} {β : Type*} variables [t : topological_space β] {f : α → β} theorem is_open_induced_eq {s : set α} : @is_open _ (induced f t) s ↔ s ∈ preimage f '' {s | is_open s} := iff.rfl theorem is_open_induced {s : set β} (h : is_open s) : (induced f t).is_open (f ⁻¹' s) := ⟨s, h, rfl⟩ lemma map_nhds_induced_eq (a : α) : map f (@nhds α (induced f t) a) = 𝓝[range f] (f a) := by rw [nhds_induced, filter.map_comap, nhds_within] lemma map_nhds_induced_of_mem {a : α} (h : range f ∈ 𝓝 (f a)) : map f (@nhds α (induced f t) a) = 𝓝 (f a) := by rw [nhds_induced, filter.map_comap_of_mem h] lemma closure_induced [t : topological_space β] {f : α → β} {a : α} {s : set α} : a ∈ @closure α (topological_space.induced f t) s ↔ f a ∈ closure (f '' s) := by simp only [mem_closure_iff_frequently, nhds_induced, frequently_comap, mem_image, and_comm] lemma is_closed_induced_iff' [t : topological_space β] {f : α → β} {s : set α} : @is_closed α (t.induced f) s ↔ ∀ a, f a ∈ closure (f '' s) → a ∈ s := by simp only [← closure_subset_iff_is_closed, subset_def, closure_induced] end induced section sierpinski variables {α : Type*} [topological_space α] @[simp] lemma is_open_singleton_true : is_open ({true} : set Prop) := topological_space.generate_open.basic _ (mem_singleton _) @[simp] lemma nhds_true : 𝓝 true = pure true := le_antisymm (le_pure_iff.2 $ is_open_singleton_true.mem_nhds $ mem_singleton _) (pure_le_nhds _) @[simp] lemma nhds_false : 𝓝 false = ⊤ := topological_space.nhds_generate_from.trans $ by simp [@and.comm (_ ∈ _)] lemma continuous_Prop {p : α → Prop} : continuous p ↔ is_open {x | p x} := ⟨assume h : continuous p, have is_open (p ⁻¹' {true}), from is_open_singleton_true.preimage h, by simpa [preimage, eq_true] using this, assume h : is_open {x | p x}, continuous_generated_from $ assume s (hs : s = {true}), by simp [hs, preimage, eq_true, h]⟩ lemma is_open_iff_continuous_mem {s : set α} : is_open s ↔ continuous (λ x, x ∈ s) := continuous_Prop.symm end sierpinski section infi variables {α : Type u} {ι : Sort v} lemma generate_from_union (a₁ a₂ : set (set α)) : topological_space.generate_from (a₁ ∪ a₂) = topological_space.generate_from a₁ ⊓ topological_space.generate_from a₂ := @galois_connection.l_sup _ (topological_space α)ᵒᵈ a₁ a₂ _ _ _ _ (λ g t, generate_from_le_iff_subset_is_open) lemma set_of_is_open_sup (t₁ t₂ : topological_space α) : {s | (t₁ ⊔ t₂).is_open s} = {s | t₁.is_open s} ∩ {s | t₂.is_open s} := @galois_connection.u_inf _ (topological_space α)ᵒᵈ t₁ t₂ _ _ _ _ (λ g t, generate_from_le_iff_subset_is_open) lemma generate_from_Union {f : ι → set (set α)} : topological_space.generate_from (⋃ i, f i) = (⨅ i, topological_space.generate_from (f i)) := @galois_connection.l_supr _ (topological_space α)ᵒᵈ _ _ _ _ _ (λ g t, generate_from_le_iff_subset_is_open) f lemma set_of_is_open_supr {t : ι → topological_space α} : {s | (⨆ i, t i).is_open s} = ⋂ i, {s | (t i).is_open s} := @galois_connection.u_infi _ (topological_space α)ᵒᵈ _ _ _ _ _ (λ g t, generate_from_le_iff_subset_is_open) t lemma generate_from_sUnion {S : set (set (set α))} : topological_space.generate_from (⋃₀ S) = (⨅ s ∈ S, topological_space.generate_from s) := @galois_connection.l_Sup _ (topological_space α)ᵒᵈ _ _ _ _ (λ g t, generate_from_le_iff_subset_is_open) S lemma set_of_is_open_Sup {T : set (topological_space α)} : {s | (Sup T).is_open s} = ⋂ t ∈ T, {s | (t : topological_space α).is_open s} := @galois_connection.u_Inf _ (topological_space α)ᵒᵈ _ _ _ _ (λ g t, generate_from_le_iff_subset_is_open) T lemma generate_from_union_is_open (a b : topological_space α) : topological_space.generate_from ({s | a.is_open s} ∪ {s | b.is_open s}) = a ⊓ b := @galois_insertion.l_sup_u _ (topological_space α)ᵒᵈ _ _ _ _ (gi_generate_from α) a b lemma generate_from_Union_is_open (f : ι → topological_space α) : topological_space.generate_from (⋃ i, {s | (f i).is_open s}) = ⨅ i, (f i) := @galois_insertion.l_supr_u _ (topological_space α)ᵒᵈ _ _ _ _ (gi_generate_from α) _ f lemma generate_from_inter (a b : topological_space α) : topological_space.generate_from ({s | a.is_open s} ∩ {s | b.is_open s}) = a ⊔ b := @galois_insertion.l_inf_u _ (topological_space α)ᵒᵈ _ _ _ _ (gi_generate_from α) a b lemma generate_from_Inter (f : ι → topological_space α) : topological_space.generate_from (⋂ i, {s | (f i).is_open s}) = ⨆ i, (f i) := @galois_insertion.l_infi_u _ (topological_space α)ᵒᵈ _ _ _ _ (gi_generate_from α) _ f lemma generate_from_Inter_of_generate_from_eq_self (f : ι → set (set α)) (hf : ∀ i, {s | (topological_space.generate_from (f i)).is_open s} = f i) : topological_space.generate_from (⋂ i, (f i)) = ⨆ i, topological_space.generate_from (f i) := @galois_insertion.l_infi_of_ul_eq_self _ (topological_space α)ᵒᵈ _ _ _ _ (gi_generate_from α) _ f hf variables {t : ι → topological_space α} lemma is_open_supr_iff {s : set α} : @is_open _ (⨆ i, t i) s ↔ ∀ i, @is_open _ (t i) s := show s ∈ set_of (supr t).is_open ↔ s ∈ {x : set α | ∀ (i : ι), (t i).is_open x}, by simp [set_of_is_open_supr] lemma is_closed_supr_iff {s : set α} : @is_closed _ (⨆ i, t i) s ↔ ∀ i, @is_closed _ (t i) s := by simp [← is_open_compl_iff, is_open_supr_iff] end infi
15107c0edf1edfeca7d975773a50bf82b3bc72f2
d744d97b07fc1e61b0ebea192b2d624125898128
/src/nat_util.lean
3f150a60514e543a370d55bf84535afce3ffa83b
[ "MIT" ]
permissive
parkersullivan/leanstuff
019c757848dd8f5db185e6973969141aee6fef7c
3da132c44d365ecd933de64fa04ba66b9e620683
refs/heads/master
1,599,036,040,845
1,573,316,625,000
1,573,316,625,000
219,181,711
0
0
null
null
null
null
UTF-8
Lean
false
false
171
lean
def ltb : ℕ → ℕ → bool | 0 0 := ff | 0 (m + 1) := tt | (n + 1) 0 := ff | (n + 1) (m + 1) := ltb n m def my_max (n m : ℕ) : ℕ := if (ltb n m) then m else n
f9c4328370c5ea51e826331fa33c9d859cd818d0
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/special_functions/pow/real.lean
26582880834d2d4df6fca940d3d6d3e1e4a0414e
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
28,420
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Sébastien Gouëzel, Rémy Degenne, David Loeffler -/ import analysis.special_functions.pow.complex /-! # Power function on `ℝ` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We construct the power functions `x ^ y`, where `x` and `y` are real numbers. -/ noncomputable theory open_locale classical real big_operators complex_conjugate open finset set /- ## Definitions -/ namespace real /-- The real power function `x ^ y`, defined as the real part of the complex power function. For `x > 0`, it is equal to `exp (y log x)`. For `x = 0`, one sets `0 ^ 0=1` and `0 ^ y=0` for `y ≠ 0`. For `x < 0`, the definition is somewhat arbitary as it depends on the choice of a complex determination of the logarithm. With our conventions, it is equal to `exp (y log x) cos (π y)`. -/ noncomputable def rpow (x y : ℝ) := ((x : ℂ) ^ (y : ℂ)).re noncomputable instance : has_pow ℝ ℝ := ⟨rpow⟩ @[simp] lemma rpow_eq_pow (x y : ℝ) : rpow x y = x ^ y := rfl lemma rpow_def (x y : ℝ) : x ^ y = ((x : ℂ) ^ (y : ℂ)).re := rfl lemma rpow_def_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) := by simp only [rpow_def, complex.cpow_def]; split_ifs; simp [*, (complex.of_real_log hx).symm, -complex.of_real_mul, -is_R_or_C.of_real_mul, (complex.of_real_mul _ _).symm, complex.exp_of_real_re] at * lemma rpow_def_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : x ^ y = exp (log x * y) := by rw [rpow_def_of_nonneg (le_of_lt hx), if_neg (ne_of_gt hx)] lemma exp_mul (x y : ℝ) : exp (x * y) = (exp x) ^ y := by rw [rpow_def_of_pos (exp_pos _), log_exp] @[simp] lemma exp_one_rpow (x : ℝ) : exp 1 ^ x = exp x := by rw [←exp_mul, one_mul] lemma rpow_eq_zero_iff_of_nonneg {x y : ℝ} (hx : 0 ≤ x) : x ^ y = 0 ↔ x = 0 ∧ y ≠ 0 := by { simp only [rpow_def_of_nonneg hx], split_ifs; simp [*, exp_ne_zero] } open_locale real lemma rpow_def_of_neg {x : ℝ} (hx : x < 0) (y : ℝ) : x ^ y = exp (log x * y) * cos (y * π) := begin rw [rpow_def, complex.cpow_def, if_neg], have : complex.log x * y = ↑(log(-x) * y) + ↑(y * π) * complex.I, { simp only [complex.log, abs_of_neg hx, complex.arg_of_real_of_neg hx, complex.abs_of_real, complex.of_real_mul], ring }, { rw [this, complex.exp_add_mul_I, ← complex.of_real_exp, ← complex.of_real_cos, ← complex.of_real_sin, mul_add, ← complex.of_real_mul, ← mul_assoc, ← complex.of_real_mul, complex.add_re, complex.of_real_re, complex.mul_re, complex.I_re, complex.of_real_im, real.log_neg_eq_log], ring }, { rw complex.of_real_eq_zero, exact ne_of_lt hx } end lemma rpow_def_of_nonpos {x : ℝ} (hx : x ≤ 0) (y : ℝ) : x ^ y = if x = 0 then if y = 0 then 1 else 0 else exp (log x * y) * cos (y * π) := by split_ifs; simp [rpow_def, *]; exact rpow_def_of_neg (lt_of_le_of_ne hx h) _ lemma rpow_pos_of_pos {x : ℝ} (hx : 0 < x) (y : ℝ) : 0 < x ^ y := by rw rpow_def_of_pos hx; apply exp_pos @[simp] lemma rpow_zero (x : ℝ) : x ^ (0 : ℝ) = 1 := by simp [rpow_def] @[simp] lemma zero_rpow {x : ℝ} (h : x ≠ 0) : (0 : ℝ) ^ x = 0 := by simp [rpow_def, *] lemma zero_rpow_eq_iff {x : ℝ} {a : ℝ} : 0 ^ x = a ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) := begin split, { intros hyp, simp only [rpow_def, complex.of_real_zero] at hyp, by_cases x = 0, { subst h, simp only [complex.one_re, complex.of_real_zero, complex.cpow_zero] at hyp, exact or.inr ⟨rfl, hyp.symm⟩}, { rw complex.zero_cpow (complex.of_real_ne_zero.mpr h) at hyp, exact or.inl ⟨h, hyp.symm⟩, }, }, { rintro (⟨h,rfl⟩|⟨rfl,rfl⟩), { exact zero_rpow h, }, { exact rpow_zero _, }, }, end lemma eq_zero_rpow_iff {x : ℝ} {a : ℝ} : a = 0 ^ x ↔ (x ≠ 0 ∧ a = 0) ∨ (x = 0 ∧ a = 1) := by rw [←zero_rpow_eq_iff, eq_comm] @[simp] lemma rpow_one (x : ℝ) : x ^ (1 : ℝ) = x := by simp [rpow_def] @[simp] lemma one_rpow (x : ℝ) : (1 : ℝ) ^ x = 1 := by simp [rpow_def] lemma zero_rpow_le_one (x : ℝ) : (0 : ℝ) ^ x ≤ 1 := by { by_cases h : x = 0; simp [h, zero_le_one] } lemma zero_rpow_nonneg (x : ℝ) : 0 ≤ (0 : ℝ) ^ x := by { by_cases h : x = 0; simp [h, zero_le_one] } lemma rpow_nonneg_of_nonneg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : 0 ≤ x ^ y := by rw [rpow_def_of_nonneg hx]; split_ifs; simp only [zero_le_one, le_refl, le_of_lt (exp_pos _)] lemma abs_rpow_of_nonneg {x y : ℝ} (hx_nonneg : 0 ≤ x) : |x ^ y| = |x| ^ y := begin have h_rpow_nonneg : 0 ≤ x ^ y, from real.rpow_nonneg_of_nonneg hx_nonneg _, rw [abs_eq_self.mpr hx_nonneg, abs_eq_self.mpr h_rpow_nonneg], end lemma abs_rpow_le_abs_rpow (x y : ℝ) : |x ^ y| ≤ |x| ^ y := begin cases le_or_lt 0 x with hx hx, { rw [abs_rpow_of_nonneg hx] }, { rw [abs_of_neg hx, rpow_def_of_neg hx, rpow_def_of_pos (neg_pos.2 hx), log_neg_eq_log, abs_mul, abs_of_pos (exp_pos _)], exact mul_le_of_le_one_right (exp_pos _).le (abs_cos_le_one _) } end lemma abs_rpow_le_exp_log_mul (x y : ℝ) : |x ^ y| ≤ exp (log x * y) := begin refine (abs_rpow_le_abs_rpow x y).trans _, by_cases hx : x = 0, { by_cases hy : y = 0; simp [hx, hy, zero_le_one] }, { rw [rpow_def_of_pos (abs_pos.2 hx), log_abs] } end lemma norm_rpow_of_nonneg {x y : ℝ} (hx_nonneg : 0 ≤ x) : ‖x ^ y‖ = ‖x‖ ^ y := by { simp_rw real.norm_eq_abs, exact abs_rpow_of_nonneg hx_nonneg, } variables {x y z : ℝ} lemma rpow_add (hx : 0 < x) (y z : ℝ) : x ^ (y + z) = x ^ y * x ^ z := by simp only [rpow_def_of_pos hx, mul_add, exp_add] lemma rpow_add' (hx : 0 ≤ x) (h : y + z ≠ 0) : x ^ (y + z) = x ^ y * x ^ z := begin rcases hx.eq_or_lt with rfl|pos, { rw [zero_rpow h, zero_eq_mul], have : y ≠ 0 ∨ z ≠ 0, from not_and_distrib.1 (λ ⟨hy, hz⟩, h $ hy.symm ▸ hz.symm ▸ zero_add 0), exact this.imp zero_rpow zero_rpow }, { exact rpow_add pos _ _ } end lemma rpow_add_of_nonneg (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 ≤ z) : x ^ (y + z) = x ^ y * x ^ z := begin rcases hy.eq_or_lt with rfl|hy, { rw [zero_add, rpow_zero, one_mul] }, exact rpow_add' hx (ne_of_gt $ add_pos_of_pos_of_nonneg hy hz) end /-- For `0 ≤ x`, the only problematic case in the equality `x ^ y * x ^ z = x ^ (y + z)` is for `x = 0` and `y + z = 0`, where the right hand side is `1` while the left hand side can vanish. The inequality is always true, though, and given in this lemma. -/ lemma le_rpow_add {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ y * x ^ z ≤ x ^ (y + z) := begin rcases le_iff_eq_or_lt.1 hx with H|pos, { by_cases h : y + z = 0, { simp only [H.symm, h, rpow_zero], calc (0 : ℝ) ^ y * 0 ^ z ≤ 1 * 1 : mul_le_mul (zero_rpow_le_one y) (zero_rpow_le_one z) (zero_rpow_nonneg z) zero_le_one ... = 1 : by simp }, { simp [rpow_add', ← H, h] } }, { simp [rpow_add pos] } end lemma rpow_sum_of_pos {ι : Type*} {a : ℝ} (ha : 0 < a) (f : ι → ℝ) (s : finset ι) : a ^ (∑ x in s, f x) = ∏ x in s, a ^ f x := @add_monoid_hom.map_sum ℝ ι (additive ℝ) _ _ ⟨λ x : ℝ, (a ^ x : ℝ), rpow_zero a, rpow_add ha⟩ f s lemma rpow_sum_of_nonneg {ι : Type*} {a : ℝ} (ha : 0 ≤ a) {s : finset ι} {f : ι → ℝ} (h : ∀ x ∈ s, 0 ≤ f x) : a ^ (∑ x in s, f x) = ∏ x in s, a ^ f x := begin induction s using finset.cons_induction with i s hi ihs, { rw [sum_empty, finset.prod_empty, rpow_zero] }, { rw forall_mem_cons at h, rw [sum_cons, prod_cons, ← ihs h.2, rpow_add_of_nonneg ha h.1 (sum_nonneg h.2)] } end lemma rpow_neg {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : x ^ -y = (x ^ y)⁻¹ := by simp only [rpow_def_of_nonneg hx]; split_ifs; simp [*, exp_neg] at * lemma rpow_sub {x : ℝ} (hx : 0 < x) (y z : ℝ) : x ^ (y - z) = x ^ y / x ^ z := by simp only [sub_eq_add_neg, rpow_add hx, rpow_neg (le_of_lt hx), div_eq_mul_inv] lemma rpow_sub' {x : ℝ} (hx : 0 ≤ x) {y z : ℝ} (h : y - z ≠ 0) : x ^ (y - z) = x ^ y / x ^ z := by { simp only [sub_eq_add_neg] at h ⊢, simp only [rpow_add' hx h, rpow_neg hx, div_eq_mul_inv] } end real /-! ## Comparing real and complex powers -/ namespace complex lemma of_real_cpow {x : ℝ} (hx : 0 ≤ x) (y : ℝ) : ((x ^ y : ℝ) : ℂ) = (x : ℂ) ^ (y : ℂ) := by simp only [real.rpow_def_of_nonneg hx, complex.cpow_def, of_real_eq_zero]; split_ifs; simp [complex.of_real_log hx] lemma of_real_cpow_of_nonpos {x : ℝ} (hx : x ≤ 0) (y : ℂ) : (x : ℂ) ^ y = ((-x) : ℂ) ^ y * exp (π * I * y) := begin rcases hx.eq_or_lt with rfl|hlt, { rcases eq_or_ne y 0 with rfl|hy; simp * }, have hne : (x : ℂ) ≠ 0, from of_real_ne_zero.mpr hlt.ne, rw [cpow_def_of_ne_zero hne, cpow_def_of_ne_zero (neg_ne_zero.2 hne), ← exp_add, ← add_mul, log, log, abs.map_neg, arg_of_real_of_neg hlt, ← of_real_neg, arg_of_real_of_nonneg (neg_nonneg.2 hx), of_real_zero, zero_mul, add_zero] end lemma abs_cpow_of_ne_zero {z : ℂ} (hz : z ≠ 0) (w : ℂ) : abs (z ^ w) = abs z ^ w.re / real.exp (arg z * im w) := by rw [cpow_def_of_ne_zero hz, abs_exp, mul_re, log_re, log_im, real.exp_sub, real.rpow_def_of_pos (abs.pos hz)] lemma abs_cpow_of_imp {z w : ℂ} (h : z = 0 → w.re = 0 → w = 0) : abs (z ^ w) = abs z ^ w.re / real.exp (arg z * im w) := begin rcases ne_or_eq z 0 with hz|rfl; [exact (abs_cpow_of_ne_zero hz w), rw map_zero], cases eq_or_ne w.re 0 with hw hw, { simp [hw, h rfl hw] }, { rw [real.zero_rpow hw, zero_div, zero_cpow, map_zero], exact ne_of_apply_ne re hw } end lemma abs_cpow_le (z w : ℂ) : abs (z ^ w) ≤ abs z ^ w.re / real.exp (arg z * im w) := begin rcases ne_or_eq z 0 with hz|rfl; [exact (abs_cpow_of_ne_zero hz w).le, rw map_zero], rcases eq_or_ne w 0 with rfl|hw, { simp }, rw [zero_cpow hw, map_zero], exact div_nonneg (real.rpow_nonneg_of_nonneg le_rfl _) (real.exp_pos _).le end @[simp] lemma abs_cpow_real (x : ℂ) (y : ℝ) : abs (x ^ (y : ℂ)) = x.abs ^ y := by rcases eq_or_ne x 0 with rfl|hx; [rcases eq_or_ne y 0 with rfl|hy, skip]; simp [*, abs_cpow_of_ne_zero] @[simp] lemma abs_cpow_inv_nat (x : ℂ) (n : ℕ) : abs (x ^ (n⁻¹ : ℂ)) = x.abs ^ (n⁻¹ : ℝ) := by rw ← abs_cpow_real; simp [-abs_cpow_real] lemma abs_cpow_eq_rpow_re_of_pos {x : ℝ} (hx : 0 < x) (y : ℂ) : abs (x ^ y) = x ^ y.re := by rw [abs_cpow_of_ne_zero (of_real_ne_zero.mpr hx.ne'), arg_of_real_of_nonneg hx.le, zero_mul, real.exp_zero, div_one, abs_of_nonneg hx.le] lemma abs_cpow_eq_rpow_re_of_nonneg {x : ℝ} (hx : 0 ≤ x) {y : ℂ} (hy : re y ≠ 0) : abs (x ^ y) = x ^ re y := begin rcases hx.eq_or_lt with rfl|hlt, { rw [of_real_zero, zero_cpow, map_zero, real.zero_rpow hy], exact ne_of_apply_ne re hy }, { exact abs_cpow_eq_rpow_re_of_pos hlt y } end end complex /-! ## Further algebraic properties of `rpow` -/ namespace real variables {x y z : ℝ} lemma rpow_mul {x : ℝ} (hx : 0 ≤ x) (y z : ℝ) : x ^ (y * z) = (x ^ y) ^ z := by rw [← complex.of_real_inj, complex.of_real_cpow (rpow_nonneg_of_nonneg hx _), complex.of_real_cpow hx, complex.of_real_mul, complex.cpow_mul, complex.of_real_cpow hx]; simp only [(complex.of_real_mul _ _).symm, (complex.of_real_log hx).symm, complex.of_real_im, neg_lt_zero, pi_pos, le_of_lt pi_pos] lemma rpow_add_int {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℤ) : x ^ (y + n) = x ^ y * x ^ n := by rw [rpow_def, complex.of_real_add, complex.cpow_add _ _ (complex.of_real_ne_zero.mpr hx), complex.of_real_int_cast, complex.cpow_int_cast, ← complex.of_real_zpow, mul_comm, complex.of_real_mul_re, ← rpow_def, mul_comm] lemma rpow_add_nat {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y + n) = x ^ y * x ^ n := by simpa using rpow_add_int hx y n lemma rpow_sub_int {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℤ) : x ^ (y - n) = x ^ y / x ^ n := by simpa using rpow_add_int hx y (-n) lemma rpow_sub_nat {x : ℝ} (hx : x ≠ 0) (y : ℝ) (n : ℕ) : x ^ (y - n) = x ^ y / x ^ n := by simpa using rpow_sub_int hx y n lemma rpow_add_one {x : ℝ} (hx : x ≠ 0) (y : ℝ) : x ^ (y + 1) = x ^ y * x := by simpa using rpow_add_nat hx y 1 lemma rpow_sub_one {x : ℝ} (hx : x ≠ 0) (y : ℝ) : x ^ (y - 1) = x ^ y / x := by simpa using rpow_sub_nat hx y 1 @[simp, norm_cast] lemma rpow_int_cast (x : ℝ) (n : ℤ) : x ^ (n : ℝ) = x ^ n := by simp only [rpow_def, ← complex.of_real_zpow, complex.cpow_int_cast, complex.of_real_int_cast, complex.of_real_re] @[simp, norm_cast] lemma rpow_nat_cast (x : ℝ) (n : ℕ) : x ^ (n : ℝ) = x ^ n := by simpa using rpow_int_cast x n @[simp] lemma rpow_two (x : ℝ) : x ^ (2 : ℝ) = x ^ 2 := by { rw ← rpow_nat_cast, simp only [nat.cast_bit0, nat.cast_one] } lemma rpow_neg_one (x : ℝ) : x ^ (-1 : ℝ) = x⁻¹ := begin suffices H : x ^ ((-1 : ℤ) : ℝ) = x⁻¹, by rwa [int.cast_neg, int.cast_one] at H, simp only [rpow_int_cast, zpow_one, zpow_neg], end lemma mul_rpow {x y z : ℝ} (h : 0 ≤ x) (h₁ : 0 ≤ y) : (x*y)^z = x^z * y^z := begin iterate 3 { rw real.rpow_def_of_nonneg }, split_ifs; simp * at *, { have hx : 0 < x, { cases lt_or_eq_of_le h with h₂ h₂, { exact h₂ }, exfalso, apply h_2, exact eq.symm h₂ }, have hy : 0 < y, { cases lt_or_eq_of_le h₁ with h₂ h₂, { exact h₂ }, exfalso, apply h_3, exact eq.symm h₂ }, rw [log_mul (ne_of_gt hx) (ne_of_gt hy), add_mul, exp_add]}, { exact h₁ }, { exact h }, { exact mul_nonneg h h₁ }, end lemma inv_rpow (hx : 0 ≤ x) (y : ℝ) : (x⁻¹)^y = (x^y)⁻¹ := by simp only [← rpow_neg_one, ← rpow_mul hx, mul_comm] lemma div_rpow (hx : 0 ≤ x) (hy : 0 ≤ y) (z : ℝ) : (x / y) ^ z = x^z / y^z := by simp only [div_eq_mul_inv, mul_rpow hx (inv_nonneg.2 hy), inv_rpow hy] lemma log_rpow {x : ℝ} (hx : 0 < x) (y : ℝ) : log (x^y) = y * (log x) := begin apply exp_injective, rw [exp_log (rpow_pos_of_pos hx y), ← exp_log hx, mul_comm, rpow_def_of_pos (exp_pos (log x)) y], end /-! ## Order and monotonicity -/ lemma rpow_lt_rpow (hx : 0 ≤ x) (hxy : x < y) (hz : 0 < z) : x^z < y^z := begin rw le_iff_eq_or_lt at hx, cases hx, { rw [← hx, zero_rpow (ne_of_gt hz)], exact rpow_pos_of_pos (by rwa ← hx at hxy) _ }, rw [rpow_def_of_pos hx, rpow_def_of_pos (lt_trans hx hxy), exp_lt_exp], exact mul_lt_mul_of_pos_right (log_lt_log hx hxy) hz end lemma rpow_le_rpow {x y z: ℝ} (h : 0 ≤ x) (h₁ : x ≤ y) (h₂ : 0 ≤ z) : x^z ≤ y^z := begin rcases eq_or_lt_of_le h₁ with rfl|h₁', { refl }, rcases eq_or_lt_of_le h₂ with rfl|h₂', { simp }, exact le_of_lt (rpow_lt_rpow h h₁' h₂') end lemma rpow_lt_rpow_iff (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z < y ^ z ↔ x < y := ⟨lt_imp_lt_of_le_imp_le $ λ h, rpow_le_rpow hy h (le_of_lt hz), λ h, rpow_lt_rpow hx h hz⟩ lemma rpow_le_rpow_iff (hx : 0 ≤ x) (hy : 0 ≤ y) (hz : 0 < z) : x ^ z ≤ y ^ z ↔ x ≤ y := le_iff_le_iff_lt_iff_lt.2 $ rpow_lt_rpow_iff hy hx hz lemma le_rpow_inv_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ≤ y ^ z⁻¹ ↔ y ≤ x ^ z := begin have hz' : 0 < -z := by rwa [lt_neg, neg_zero], have hxz : 0 < x ^ (-z) := real.rpow_pos_of_pos hx _, have hyz : 0 < y ^ z⁻¹ := real.rpow_pos_of_pos hy _, rw [←real.rpow_le_rpow_iff hx.le hyz.le hz', ←real.rpow_mul hy.le], simp only [ne_of_lt hz, real.rpow_neg_one, mul_neg, inv_mul_cancel, ne.def, not_false_iff], rw [le_inv hxz hy, ←real.rpow_neg_one, ←real.rpow_mul hx.le], simp, end lemma lt_rpow_inv_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x < y ^ z⁻¹ ↔ y < x ^ z := begin have hz' : 0 < -z := by rwa [lt_neg, neg_zero], have hxz : 0 < x ^ (-z) := real.rpow_pos_of_pos hx _, have hyz : 0 < y ^ z⁻¹ := real.rpow_pos_of_pos hy _, rw [←real.rpow_lt_rpow_iff hx.le hyz.le hz', ←real.rpow_mul hy.le], simp only [ne_of_lt hz, real.rpow_neg_one, mul_neg, inv_mul_cancel, ne.def, not_false_iff], rw [lt_inv hxz hy, ←real.rpow_neg_one, ←real.rpow_mul hx.le], simp, end lemma rpow_inv_lt_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z⁻¹ < y ↔ y ^ z < x := begin convert lt_rpow_inv_iff_of_neg (real.rpow_pos_of_pos hx _) (real.rpow_pos_of_pos hy _) hz; simp [←real.rpow_mul hx.le, ←real.rpow_mul hy.le, ne_of_lt hz], end lemma rpow_inv_le_iff_of_neg (hx : 0 < x) (hy : 0 < y) (hz : z < 0) : x ^ z⁻¹ ≤ y ↔ y ^ z ≤ x := begin convert le_rpow_inv_iff_of_neg (real.rpow_pos_of_pos hx _) (real.rpow_pos_of_pos hy _) hz; simp [←real.rpow_mul hx.le, ←real.rpow_mul hy.le, ne_of_lt hz], end lemma rpow_lt_rpow_of_exponent_lt (hx : 1 < x) (hyz : y < z) : x^y < x^z := begin repeat {rw [rpow_def_of_pos (lt_trans zero_lt_one hx)]}, rw exp_lt_exp, exact mul_lt_mul_of_pos_left hyz (log_pos hx), end lemma rpow_le_rpow_of_exponent_le (hx : 1 ≤ x) (hyz : y ≤ z) : x^y ≤ x^z := begin repeat {rw [rpow_def_of_pos (lt_of_lt_of_le zero_lt_one hx)]}, rw exp_le_exp, exact mul_le_mul_of_nonneg_left hyz (log_nonneg hx), end @[simp] lemma rpow_le_rpow_left_iff (hx : 1 < x) : x ^ y ≤ x ^ z ↔ y ≤ z := begin have x_pos : 0 < x := lt_trans zero_lt_one hx, rw [←log_le_log (rpow_pos_of_pos x_pos y) (rpow_pos_of_pos x_pos z), log_rpow x_pos, log_rpow x_pos, mul_le_mul_right (log_pos hx)], end @[simp] lemma rpow_lt_rpow_left_iff (hx : 1 < x) : x ^ y < x ^ z ↔ y < z := by rw [lt_iff_not_le, rpow_le_rpow_left_iff hx, lt_iff_not_le] lemma rpow_lt_rpow_of_exponent_gt (hx0 : 0 < x) (hx1 : x < 1) (hyz : z < y) : x^y < x^z := begin repeat {rw [rpow_def_of_pos hx0]}, rw exp_lt_exp, exact mul_lt_mul_of_neg_left hyz (log_neg hx0 hx1), end lemma rpow_le_rpow_of_exponent_ge (hx0 : 0 < x) (hx1 : x ≤ 1) (hyz : z ≤ y) : x^y ≤ x^z := begin repeat {rw [rpow_def_of_pos hx0]}, rw exp_le_exp, exact mul_le_mul_of_nonpos_left hyz (log_nonpos (le_of_lt hx0) hx1), end @[simp] lemma rpow_le_rpow_left_iff_of_base_lt_one (hx0 : 0 < x) (hx1 : x < 1) : x ^ y ≤ x ^ z ↔ z ≤ y := begin rw [←log_le_log (rpow_pos_of_pos hx0 y) (rpow_pos_of_pos hx0 z), log_rpow hx0, log_rpow hx0, mul_le_mul_right_of_neg (log_neg hx0 hx1)], end @[simp] lemma rpow_lt_rpow_left_iff_of_base_lt_one (hx0 : 0 < x) (hx1 : x < 1) : x ^ y < x ^ z ↔ z < y := by rw [lt_iff_not_le, rpow_le_rpow_left_iff_of_base_lt_one hx0 hx1, lt_iff_not_le] lemma rpow_lt_one {x z : ℝ} (hx1 : 0 ≤ x) (hx2 : x < 1) (hz : 0 < z) : x^z < 1 := by { rw ← one_rpow z, exact rpow_lt_rpow hx1 hx2 hz } lemma rpow_le_one {x z : ℝ} (hx1 : 0 ≤ x) (hx2 : x ≤ 1) (hz : 0 ≤ z) : x^z ≤ 1 := by { rw ← one_rpow z, exact rpow_le_rpow hx1 hx2 hz } lemma rpow_lt_one_of_one_lt_of_neg {x z : ℝ} (hx : 1 < x) (hz : z < 0) : x^z < 1 := by { convert rpow_lt_rpow_of_exponent_lt hx hz, exact (rpow_zero x).symm } lemma rpow_le_one_of_one_le_of_nonpos {x z : ℝ} (hx : 1 ≤ x) (hz : z ≤ 0) : x^z ≤ 1 := by { convert rpow_le_rpow_of_exponent_le hx hz, exact (rpow_zero x).symm } lemma one_lt_rpow {x z : ℝ} (hx : 1 < x) (hz : 0 < z) : 1 < x^z := by { rw ← one_rpow z, exact rpow_lt_rpow zero_le_one hx hz } lemma one_le_rpow {x z : ℝ} (hx : 1 ≤ x) (hz : 0 ≤ z) : 1 ≤ x^z := by { rw ← one_rpow z, exact rpow_le_rpow zero_le_one hx hz } lemma one_lt_rpow_of_pos_of_lt_one_of_neg (hx1 : 0 < x) (hx2 : x < 1) (hz : z < 0) : 1 < x^z := by { convert rpow_lt_rpow_of_exponent_gt hx1 hx2 hz, exact (rpow_zero x).symm } lemma one_le_rpow_of_pos_of_le_one_of_nonpos (hx1 : 0 < x) (hx2 : x ≤ 1) (hz : z ≤ 0) : 1 ≤ x^z := by { convert rpow_le_rpow_of_exponent_ge hx1 hx2 hz, exact (rpow_zero x).symm } lemma rpow_lt_one_iff_of_pos (hx : 0 < x) : x ^ y < 1 ↔ 1 < x ∧ y < 0 ∨ x < 1 ∧ 0 < y := by rw [rpow_def_of_pos hx, exp_lt_one_iff, mul_neg_iff, log_pos_iff hx, log_neg_iff hx] lemma rpow_lt_one_iff (hx : 0 ≤ x) : x ^ y < 1 ↔ x = 0 ∧ y ≠ 0 ∨ 1 < x ∧ y < 0 ∨ x < 1 ∧ 0 < y := begin rcases hx.eq_or_lt with (rfl|hx), { rcases em (y = 0) with (rfl|hy); simp [*, lt_irrefl, zero_lt_one] }, { simp [rpow_lt_one_iff_of_pos hx, hx.ne.symm] } end lemma one_lt_rpow_iff_of_pos (hx : 0 < x) : 1 < x ^ y ↔ 1 < x ∧ 0 < y ∨ x < 1 ∧ y < 0 := by rw [rpow_def_of_pos hx, one_lt_exp_iff, mul_pos_iff, log_pos_iff hx, log_neg_iff hx] lemma one_lt_rpow_iff (hx : 0 ≤ x) : 1 < x ^ y ↔ 1 < x ∧ 0 < y ∨ 0 < x ∧ x < 1 ∧ y < 0 := begin rcases hx.eq_or_lt with (rfl|hx), { rcases em (y = 0) with (rfl|hy); simp [*, lt_irrefl, (zero_lt_one' ℝ).not_lt] }, { simp [one_lt_rpow_iff_of_pos hx, hx] } end lemma rpow_le_rpow_of_exponent_ge' (hx0 : 0 ≤ x) (hx1 : x ≤ 1) (hz : 0 ≤ z) (hyz : z ≤ y) : x^y ≤ x^z := begin rcases eq_or_lt_of_le hx0 with rfl | hx0', { rcases eq_or_lt_of_le hz with rfl | hz', { exact (rpow_zero 0).symm ▸ (rpow_le_one hx0 hx1 hyz), }, rw [zero_rpow, zero_rpow]; linarith, }, { exact rpow_le_rpow_of_exponent_ge hx0' hx1 hyz, }, end lemma rpow_left_inj_on {x : ℝ} (hx : x ≠ 0) : inj_on (λ y : ℝ, y^x) {y : ℝ | 0 ≤ y} := begin rintros y hy z hz (hyz : y ^ x = z ^ x), rw [←rpow_one y, ←rpow_one z, ←_root_.mul_inv_cancel hx, rpow_mul hy, rpow_mul hz, hyz] end lemma le_rpow_iff_log_le (hx : 0 < x) (hy : 0 < y) : x ≤ y^z ↔ real.log x ≤ z * real.log y := by rw [←real.log_le_log hx (real.rpow_pos_of_pos hy z), real.log_rpow hy] lemma le_rpow_of_log_le (hx : 0 ≤ x) (hy : 0 < y) (h : real.log x ≤ z * real.log y) : x ≤ y^z := begin obtain hx | rfl := hx.lt_or_eq, { exact (le_rpow_iff_log_le hx hy).2 h }, exact (real.rpow_pos_of_pos hy z).le, end lemma lt_rpow_iff_log_lt (hx : 0 < x) (hy : 0 < y) : x < y^z ↔ real.log x < z * real.log y := by rw [←real.log_lt_log_iff hx (real.rpow_pos_of_pos hy z), real.log_rpow hy] lemma lt_rpow_of_log_lt (hx : 0 ≤ x) (hy : 0 < y) (h : real.log x < z * real.log y) : x < y^z := begin obtain hx | rfl := hx.lt_or_eq, { exact (lt_rpow_iff_log_lt hx hy).2 h }, exact real.rpow_pos_of_pos hy z, end lemma rpow_le_one_iff_of_pos (hx : 0 < x) : x ^ y ≤ 1 ↔ 1 ≤ x ∧ y ≤ 0 ∨ x ≤ 1 ∧ 0 ≤ y := by rw [rpow_def_of_pos hx, exp_le_one_iff, mul_nonpos_iff, log_nonneg_iff hx, log_nonpos_iff hx] /-- Bound for `|log x * x ^ t|` in the interval `(0, 1]`, for positive real `t`. -/ lemma abs_log_mul_self_rpow_lt (x t : ℝ) (h1 : 0 < x) (h2 : x ≤ 1) (ht : 0 < t) : |log x * x ^ t| < 1 / t := begin rw lt_div_iff ht, have := abs_log_mul_self_lt (x ^ t) (rpow_pos_of_pos h1 t) (rpow_le_one h1.le h2 ht.le), rwa [log_rpow h1, mul_assoc, abs_mul, abs_of_pos ht, mul_comm] at this end lemma pow_nat_rpow_nat_inv {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : n ≠ 0) : (x ^ n) ^ (n⁻¹ : ℝ) = x := have hn0 : (n : ℝ) ≠ 0, from nat.cast_ne_zero.2 hn, by rw [← rpow_nat_cast, ← rpow_mul hx, mul_inv_cancel hn0, rpow_one] lemma rpow_nat_inv_pow_nat {x : ℝ} (hx : 0 ≤ x) {n : ℕ} (hn : n ≠ 0) : (x ^ (n⁻¹ : ℝ)) ^ n = x := have hn0 : (n : ℝ) ≠ 0, from nat.cast_ne_zero.2 hn, by rw [← rpow_nat_cast, ← rpow_mul hx, inv_mul_cancel hn0, rpow_one] end real /-! ## Square roots of reals -/ namespace real variables {z x y : ℝ} section sqrt lemma sqrt_eq_rpow (x : ℝ) : sqrt x = x ^ (1/(2:ℝ)) := begin obtain h | h := le_or_lt 0 x, { rw [← mul_self_inj_of_nonneg (sqrt_nonneg _) (rpow_nonneg_of_nonneg h _), mul_self_sqrt h, ← sq, ← rpow_nat_cast, ← rpow_mul h], norm_num }, { have : 1 / (2:ℝ) * π = π / (2:ℝ), ring, rw [sqrt_eq_zero_of_nonpos h.le, rpow_def_of_neg h, this, cos_pi_div_two, mul_zero] } end lemma rpow_div_two_eq_sqrt {x : ℝ} (r : ℝ) (hx : 0 ≤ x) : x ^ (r/2) = (sqrt x) ^ r := begin rw [sqrt_eq_rpow, ← rpow_mul hx], congr, ring, end end sqrt variables {n : ℕ} lemma exists_rat_pow_btwn_rat_aux (hn : n ≠ 0) (x y : ℝ) (h : x < y) (hy : 0 < y) : ∃ q : ℚ, 0 < q ∧ x < q^n ∧ ↑q^n < y := begin have hn' : 0 < (n : ℝ) := by exact_mod_cast hn.bot_lt, obtain ⟨q, hxq, hqy⟩ := exists_rat_btwn (rpow_lt_rpow (le_max_left 0 x) (max_lt hy h) $ inv_pos.mpr hn'), have := rpow_nonneg_of_nonneg (le_max_left 0 x) n⁻¹, have hq := this.trans_lt hxq, replace hxq := rpow_lt_rpow this hxq hn', replace hqy := rpow_lt_rpow hq.le hqy hn', rw [rpow_nat_cast, rpow_nat_cast, rpow_nat_inv_pow_nat _ hn] at hxq hqy, exact ⟨q, by exact_mod_cast hq, (le_max_right _ _).trans_lt hxq, hqy⟩, { exact le_max_left _ _ }, { exact hy.le } end lemma exists_rat_pow_btwn_rat (hn : n ≠ 0) {x y : ℚ} (h : x < y) (hy : 0 < y) : ∃ q : ℚ, 0 < q ∧ x < q^n ∧ q^n < y := by apply_mod_cast exists_rat_pow_btwn_rat_aux hn x y; assumption /-- There is a rational power between any two positive elements of an archimedean ordered field. -/ lemma exists_rat_pow_btwn {α : Type*} [linear_ordered_field α] [archimedean α] (hn : n ≠ 0) {x y : α} (h : x < y) (hy : 0 < y) : ∃ q : ℚ, 0 < q ∧ x < q^n ∧ (q^n : α) < y := begin obtain ⟨q₂, hx₂, hy₂⟩ := exists_rat_btwn (max_lt h hy), obtain ⟨q₁, hx₁, hq₁₂⟩ := exists_rat_btwn hx₂, have : (0 : α) < q₂ := (le_max_right _ _).trans_lt hx₂, norm_cast at hq₁₂ this, obtain ⟨q, hq, hq₁, hq₂⟩ := exists_rat_pow_btwn_rat hn hq₁₂ this, refine ⟨q, hq, (le_max_left _ _).trans_lt $ hx₁.trans _, hy₂.trans' _⟩; assumption_mod_cast, end end real section tactics /-! ## Tactic extensions for real powers -/ namespace norm_num open tactic theorem rpow_pos (a b : ℝ) (b' : ℕ) (c : ℝ) (hb : (b':ℝ) = b) (h : a ^ b' = c) : a ^ b = c := by rw [← h, ← hb, real.rpow_nat_cast] theorem rpow_neg (a b : ℝ) (b' : ℕ) (c c' : ℝ) (a0 : 0 ≤ a) (hb : (b':ℝ) = b) (h : a ^ b' = c) (hc : c⁻¹ = c') : a ^ -b = c' := by rw [← hc, ← h, ← hb, real.rpow_neg a0, real.rpow_nat_cast] /-- Evaluate `real.rpow a b` where `a` is a rational numeral and `b` is an integer. (This cannot go via the generalized version `prove_rpow'` because `rpow_pos` has a side condition; we do not attempt to evaluate `a ^ b` where `a` and `b` are both negative because it comes out to some garbage.) -/ meta def prove_rpow (a b : expr) : tactic (expr × expr) := do na ← a.to_rat, ic ← mk_instance_cache `(ℝ), match match_sign b with | sum.inl b := do (ic, a0) ← guard (na ≥ 0) >> prove_nonneg ic a, nc ← mk_instance_cache `(ℕ), (ic, nc, b', hb) ← prove_nat_uncast ic nc b, (ic, c, h) ← prove_pow a na ic b', cr ← c.to_rat, (ic, c', hc) ← prove_inv ic c cr, pure (c', (expr.const ``rpow_neg []).mk_app [a, b, b', c, c', a0, hb, h, hc]) | sum.inr ff := pure (`(1:ℝ), expr.const ``real.rpow_zero [] a) | sum.inr tt := do nc ← mk_instance_cache `(ℕ), (ic, nc, b', hb) ← prove_nat_uncast ic nc b, (ic, c, h) ← prove_pow a na ic b', pure (c, (expr.const ``rpow_pos []).mk_app [a, b, b', c, hb, h]) end /-- Evaluates expressions of the form `rpow a b` and `a ^ b` in the special case where `b` is an integer and `a` is a positive rational (so it's really just a rational power). -/ @[norm_num] meta def eval_rpow : expr → tactic (expr × expr) | `(@has_pow.pow _ _ real.has_pow %%a %%b) := b.to_int >> prove_rpow a b | `(real.rpow %%a %%b) := b.to_int >> prove_rpow a b | _ := tactic.failed end norm_num namespace tactic namespace positivity /-- Auxiliary definition for the `positivity` tactic to handle real powers of reals. -/ meta def prove_rpow (a b : expr) : tactic strictness := do strictness_a ← core a, match strictness_a with | nonnegative p := nonnegative <$> mk_app ``real.rpow_nonneg_of_nonneg [p, b] | positive p := positive <$> mk_app ``real.rpow_pos_of_pos [p, b] | _ := failed end end positivity open positivity /-- Extension for the `positivity` tactic: exponentiation by a real number is nonnegative when the base is nonnegative and positive when the base is positive. -/ @[positivity] meta def positivity_rpow : expr → tactic strictness | `(@has_pow.pow _ _ real.has_pow %%a %%b) := prove_rpow a b | `(real.rpow %%a %%b) := prove_rpow a b | _ := failed end tactic end tactics
46d46c235dab3b6b6e693b6c8e0f4174d96cfd47
7da5ceac20aaab989eeb795a4be9639982e7b35a
/src/group_theory/suzuki.lean
6a4e78563180b49669185401970e91ef3916deb6
[ "MIT" ]
permissive
formalabstracts/formalabstracts
46c2f1b3a172e62ca6ffeb46fbbdf1705718af49
b0173da1af45421239d44492eeecd54bf65ee0f6
refs/heads/master
1,606,896,370,374
1,572,988,776,000
1,572,988,776,000
96,763,004
165
28
null
1,555,709,319,000
1,499,680,948,000
Lean
UTF-8
Lean
false
false
2,256
lean
import ..data.dvector .presentation .monster open category_theory (mk_ob) local notation `⟪`:50 a `⟫`:50 := free_group.of a local notation h :: t := dvector.cons h t local notation `[` l:(foldr `, ` (h t, dvector.cons h t) dvector.nil `]`) := l /- From the corresponding entry in the atlas (p. 128) Suz . 2 is a quotient of the group given by a certain Coxeter-type presentation -/ open coxeter_vertices namespace suzuki /-- We represent the diagram a--b--c-8-d--e--f--g--h as an annotation of the unlabelled coxeter Y-diagram of shape [2,5], so that c is the torso and d belongs to the second arm.-/ noncomputable def suzuki_diagram_graph : annotated_graph := annotate (annotated_graph_of_graph $ coxeter_edges [2,5]) (torso, arm (by to_dfin 1) (by to_dfin 0)) 8 noncomputable instance suzuki_diagram_decidable_rel : decidable_rel suzuki_diagram_graph.edge := λ _ _, classical.prop_decidable _ /-- The group generated by the Coxeter-Dynkin diagram a--b--c-8-d--e--f--g--h -/ noncomputable def suzuki_diagram_group : Group := coxeter_group $ matrix_of_annotated_graph suzuki_diagram_graph private def a : suzuki_diagram_group := generated_of $ arm (by to_dfin 0) (by to_dfin 1) private def b : suzuki_diagram_group := generated_of $ arm (by to_dfin 0) (by to_dfin 0) private def c : suzuki_diagram_group := generated_of $ torso private def d : suzuki_diagram_group := generated_of $ arm (by to_dfin 1) (by to_dfin 0) private def e : suzuki_diagram_group := generated_of $ arm (by to_dfin 1) (by to_dfin 1) private def f : suzuki_diagram_group := generated_of $ arm (by to_dfin 1) (by to_dfin 2) private def g : suzuki_diagram_group := generated_of $ arm (by to_dfin 1) (by to_dfin 3) private def h : suzuki_diagram_group := generated_of $ arm (by to_dfin 1) (by to_dfin 4) /-- Suz.2 is the above group quotiented by additional relations -/ noncomputable def Suz_2 : Group := suzuki_diagram_group/⟪{(c*d)^4*a⁻¹,(b*c*d*e)^8,(b*c*d*c*d*e*f*g*h)^13}⟫ /- Since Suz is simple, and we have an exact sequence 1 → Suz → Suz_2 → C_2 → 1, we can reconstruct it from Suz_2 by taking the derived subgroup of Suz_2 -/ noncomputable def Suz : Group := category_theory.mk_ob $ derived_subgroup Suz_2 end suzuki
b7465bc23135eea9f1e538ab3d4e423d41daf607
367134ba5a65885e863bdc4507601606690974c1
/src/data/multiset/lattice.lean
113a5abaf9a50b7a00cc90f87de7c1004f4f7671
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
3,609
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import data.multiset.finset_ops import data.multiset.fold /-! # Lattice operations on multisets -/ namespace multiset variables {α : Type*} /-! ### sup -/ section sup variables [semilattice_sup_bot α] /-- Supremum of a multiset: `sup {a, b, c} = a ⊔ b ⊔ c` -/ def sup (s : multiset α) : α := s.fold (⊔) ⊥ @[simp] lemma sup_zero : (0 : multiset α).sup = ⊥ := fold_zero _ _ @[simp] lemma sup_cons (a : α) (s : multiset α) : (a ::ₘ s).sup = a ⊔ s.sup := fold_cons_left _ _ _ _ @[simp] lemma sup_singleton {a : α} : (a ::ₘ 0).sup = a := by simp @[simp] lemma sup_add (s₁ s₂ : multiset α) : (s₁ + s₂).sup = s₁.sup ⊔ s₂.sup := eq.trans (by simp [sup]) (fold_add _ _ _ _ _) lemma sup_le {s : multiset α} {a : α} : s.sup ≤ a ↔ (∀b ∈ s, b ≤ a) := multiset.induction_on s (by simp) (by simp [or_imp_distrib, forall_and_distrib] {contextual := tt}) lemma le_sup {s : multiset α} {a : α} (h : a ∈ s) : a ≤ s.sup := sup_le.1 (le_refl _) _ h lemma sup_mono {s₁ s₂ : multiset α} (h : s₁ ⊆ s₂) : s₁.sup ≤ s₂.sup := sup_le.2 $ assume b hb, le_sup (h hb) variables [decidable_eq α] @[simp] lemma sup_erase_dup (s : multiset α) : (erase_dup s).sup = s.sup := fold_erase_dup_idem _ _ _ @[simp] lemma sup_ndunion (s₁ s₂ : multiset α) : (ndunion s₁ s₂).sup = s₁.sup ⊔ s₂.sup := by rw [← sup_erase_dup, erase_dup_ext.2, sup_erase_dup, sup_add]; simp @[simp] lemma sup_union (s₁ s₂ : multiset α) : (s₁ ∪ s₂).sup = s₁.sup ⊔ s₂.sup := by rw [← sup_erase_dup, erase_dup_ext.2, sup_erase_dup, sup_add]; simp @[simp] lemma sup_ndinsert (a : α) (s : multiset α) : (ndinsert a s).sup = a ⊔ s.sup := by rw [← sup_erase_dup, erase_dup_ext.2, sup_erase_dup, sup_cons]; simp end sup /-! ### inf -/ section inf variables [semilattice_inf_top α] /-- Infimum of a multiset: `inf {a, b, c} = a ⊓ b ⊓ c` -/ def inf (s : multiset α) : α := s.fold (⊓) ⊤ @[simp] lemma inf_zero : (0 : multiset α).inf = ⊤ := fold_zero _ _ @[simp] lemma inf_cons (a : α) (s : multiset α) : (a ::ₘ s).inf = a ⊓ s.inf := fold_cons_left _ _ _ _ @[simp] lemma inf_singleton {a : α} : (a ::ₘ 0).inf = a := by simp @[simp] lemma inf_add (s₁ s₂ : multiset α) : (s₁ + s₂).inf = s₁.inf ⊓ s₂.inf := eq.trans (by simp [inf]) (fold_add _ _ _ _ _) lemma le_inf {s : multiset α} {a : α} : a ≤ s.inf ↔ (∀b ∈ s, a ≤ b) := multiset.induction_on s (by simp) (by simp [or_imp_distrib, forall_and_distrib] {contextual := tt}) lemma inf_le {s : multiset α} {a : α} (h : a ∈ s) : s.inf ≤ a := le_inf.1 (le_refl _) _ h lemma inf_mono {s₁ s₂ : multiset α} (h : s₁ ⊆ s₂) : s₂.inf ≤ s₁.inf := le_inf.2 $ assume b hb, inf_le (h hb) variables [decidable_eq α] @[simp] lemma inf_erase_dup (s : multiset α) : (erase_dup s).inf = s.inf := fold_erase_dup_idem _ _ _ @[simp] lemma inf_ndunion (s₁ s₂ : multiset α) : (ndunion s₁ s₂).inf = s₁.inf ⊓ s₂.inf := by rw [← inf_erase_dup, erase_dup_ext.2, inf_erase_dup, inf_add]; simp @[simp] lemma inf_union (s₁ s₂ : multiset α) : (s₁ ∪ s₂).inf = s₁.inf ⊓ s₂.inf := by rw [← inf_erase_dup, erase_dup_ext.2, inf_erase_dup, inf_add]; simp @[simp] lemma inf_ndinsert (a : α) (s : multiset α) : (ndinsert a s).inf = a ⊓ s.inf := by rw [← inf_erase_dup, erase_dup_ext.2, inf_erase_dup, inf_cons]; simp end inf end multiset
37f50d83c82d1e8fc12f6f1752ac91c677346c79
a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7
/src/algebra/ordered_group.lean
912ceb8eaf3dfeab7a4f76e95ea1829c98fabb11
[ "Apache-2.0" ]
permissive
kmill/mathlib
ea5a007b67ae4e9e18dd50d31d8aa60f650425ee
1a419a9fea7b959317eddd556e1bb9639f4dcc05
refs/heads/master
1,668,578,197,719
1,593,629,163,000
1,593,629,163,000
276,482,939
0
0
null
1,593,637,960,000
1,593,637,959,000
null
UTF-8
Lean
false
false
65,976
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl -/ import algebra.group.units import algebra.group.with_one import algebra.group.type_tags import order.bounded_lattice set_option old_structure_cmd true set_option default_priority 100 -- see Note [default priority] /-! # Ordered monoids and groups This file develops the basics of ordered monoids and groups. ## Implementation details Unfortunately, the number of `'` appended to lemmas in this file may differ between the multiplicative and the additive version of a lemma. The reason is that we did not want to change existing names in the library. -/ universe u variable {α : Type u} /-- An ordered commutative monoid is a commutative monoid with a partial order such that * `a ≤ b → c * a ≤ c * b` (multiplication is monotone) * `a * b < a * c → b < c`. -/ @[protect_proj, ancestor comm_monoid partial_order] class ordered_comm_monoid (α : Type*) extends comm_monoid α, partial_order α := (mul_le_mul_left : ∀ a b : α, a ≤ b → ∀ c : α, c * a ≤ c * b) (lt_of_mul_lt_mul_left : ∀ a b c : α, a * b < a * c → b < c) /-- An ordered (additive) commutative monoid is a commutative monoid with a partial order such that * `a ≤ b → c + a ≤ c + b` (addition is monotone) * `a + b < a + c → b < c`. -/ @[protect_proj, ancestor add_comm_monoid partial_order] class ordered_add_comm_monoid (α : Type*) extends add_comm_monoid α, partial_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) (lt_of_add_lt_add_left : ∀ a b c : α, a + b < a + c → b < c) attribute [to_additive ordered_add_comm_monoid] ordered_comm_monoid section ordered_comm_monoid variables [ordered_comm_monoid α] {a b c d : α} @[to_additive] lemma mul_le_mul_left' (h : a ≤ b) : c * a ≤ c * b := ordered_comm_monoid.mul_le_mul_left a b h c @[to_additive] lemma mul_le_mul_right' (h : a ≤ b) : a * c ≤ b * c := mul_comm c a ▸ mul_comm c b ▸ mul_le_mul_left' h @[to_additive] lemma lt_of_mul_lt_mul_left' : a * b < a * c → b < c := ordered_comm_monoid.lt_of_mul_lt_mul_left a b c @[to_additive add_le_add] lemma mul_le_mul' (h₁ : a ≤ b) (h₂ : c ≤ d) : a * c ≤ b * d := le_trans (mul_le_mul_right' h₁) (mul_le_mul_left' h₂) @[to_additive le_add_of_nonneg_right'] lemma le_mul_of_one_le_right'' (h : 1 ≤ b) : a ≤ a * b := have a * 1 ≤ a * b, from mul_le_mul_left' h, by rwa mul_one at this @[to_additive le_add_of_nonneg_left'] lemma le_mul_of_one_le_left'' (h : 1 ≤ b) : a ≤ b * a := have 1 * a ≤ b * a, from mul_le_mul_right' h, by rwa one_mul at this @[to_additive] lemma lt_of_mul_lt_mul_right' (h : a * b < c * b) : a < c := lt_of_mul_lt_mul_left' (show b * a < b * c, begin rw [mul_comm b a, mul_comm b c], assumption end) -- here we start using properties of one. @[to_additive] lemma le_mul_of_one_le_of_le' (ha : 1 ≤ a) (hbc : b ≤ c) : b ≤ a * c := one_mul b ▸ mul_le_mul' ha hbc @[to_additive] lemma le_mul_of_le_of_one_le' (hbc : b ≤ c) (ha : 1 ≤ a) : b ≤ c * a := mul_one b ▸ mul_le_mul' hbc ha @[to_additive add_nonneg'] lemma one_le_mul' (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b := le_mul_of_one_le_of_le' ha hb @[to_additive] lemma mul_one_lt_of_one_lt_of_one_le' (ha : 1 < a) (hb : 1 ≤ b) : 1 < a * b := lt_of_lt_of_le ha $ le_mul_of_one_le_right'' hb @[to_additive] lemma mul_one_lt' (ha : 1 < a) (hb : 1 < b) : 1 < a * b := mul_one_lt_of_one_lt_of_one_le' ha $ le_of_lt hb @[to_additive] lemma mul_one_lt_of_one_le_of_one_lt' (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := lt_of_lt_of_le hb $ le_mul_of_one_le_left'' ha @[to_additive] lemma mul_le_one' (ha : a ≤ 1) (hb : b ≤ 1) : a * b ≤ 1 := one_mul (1:α) ▸ (mul_le_mul' ha hb) @[to_additive] lemma mul_le_of_le_one_of_le' (ha : a ≤ 1) (hbc : b ≤ c) : a * b ≤ c := one_mul c ▸ mul_le_mul' ha hbc @[to_additive] lemma mul_le_of_le_of_le_one' (hbc : b ≤ c) (ha : a ≤ 1) : b * a ≤ c := mul_one c ▸ mul_le_mul' hbc ha @[to_additive] lemma mul_lt_one_of_lt_one_of_le_one' (ha : a < 1) (hb : b ≤ 1) : a * b < 1 := lt_of_le_of_lt (mul_le_of_le_of_le_one' (le_refl _) hb) ha @[to_additive] lemma mul_lt_one_of_le_one_of_lt_one' (ha : a ≤ 1) (hb : b < 1) : a * b < 1 := lt_of_le_of_lt (mul_le_of_le_one_of_le' ha (le_refl _)) hb @[to_additive] lemma mul_lt_one' (ha : a < 1) (hb : b < 1) : a * b < 1 := mul_lt_one_of_le_one_of_lt_one' (le_of_lt ha) hb @[to_additive] lemma lt_mul_of_one_le_of_lt' (ha : 1 ≤ a) (hbc : b < c) : b < a * c := lt_of_lt_of_le hbc $ le_mul_of_one_le_left'' ha @[to_additive] lemma lt_mul_of_lt_of_one_le' (hbc : b < c) (ha : 1 ≤ a) : b < c * a := lt_of_lt_of_le hbc $ le_mul_of_one_le_right'' ha @[to_additive] lemma lt_mul_of_one_lt_of_lt' (ha : 1 < a) (hbc : b < c) : b < a * c := lt_mul_of_one_le_of_lt' (le_of_lt ha) hbc @[to_additive] lemma lt_mul_of_lt_of_one_lt' (hbc : b < c) (ha : 1 < a) : b < c * a := lt_mul_of_lt_of_one_le' hbc (le_of_lt ha) @[to_additive] lemma mul_lt_of_le_one_of_lt' (ha : a ≤ 1) (hbc : b < c) : a * b < c := lt_of_le_of_lt (mul_le_of_le_one_of_le' ha (le_refl _)) hbc @[to_additive] lemma mul_lt_of_lt_of_le_one' (hbc : b < c) (ha : a ≤ 1) : b * a < c := lt_of_le_of_lt (mul_le_of_le_of_le_one' (le_refl _) ha) hbc @[to_additive] lemma mul_lt_of_lt_one_of_lt' (ha : a < 1) (hbc : b < c) : a * b < c := mul_lt_of_le_one_of_lt' (le_of_lt ha) hbc @[to_additive] lemma mul_lt_of_lt_of_lt_one' (hbc : b < c) (ha : a < 1) : b * a < c := mul_lt_of_lt_of_le_one' hbc (le_of_lt ha) @[to_additive] lemma mul_eq_one_iff' (ha : 1 ≤ a) (hb : 1 ≤ b) : a * b = 1 ↔ a = 1 ∧ b = 1 := iff.intro (assume hab : a * b = 1, have a ≤ 1, from hab ▸ le_mul_of_le_of_one_le' (le_refl _) hb, have a = 1, from le_antisymm this ha, have b ≤ 1, from hab ▸ le_mul_of_one_le_of_le' ha (le_refl _), have b = 1, from le_antisymm this hb, and.intro ‹a = 1› ‹b = 1›) (assume ⟨ha', hb'⟩, by rw [ha', hb', mul_one]) @[to_additive] lemma one_le_mul_of_one_le_of_one_le (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b := have h1 : a * 1 ≤ a * b, from mul_le_mul_left' hb, have h2 : a ≤ a * b, by rwa mul_one a at h1, le_trans ha h2 @[to_additive] lemma mul_le_one_of_le_one_of_le_one (ha : a ≤ 1) (hb : b ≤ 1) : a * b ≤ 1 := have h1 : a * b ≤ a * 1, from mul_le_mul_left' hb, have h2 : a * b ≤ a, by rwa mul_one a at h1, le_trans h2 ha section mono variables {β : Type*} [preorder β] {f g : β → α} @[to_additive monotone.add] lemma monotone.mul' (hf : monotone f) (hg : monotone g) : monotone (λ x, f x * g x) := λ x y h, mul_le_mul' (hf h) (hg h) @[to_additive monotone.add_const] lemma monotone.mul_const' (hf : monotone f) (a : α) : monotone (λ x, f x * a) := hf.mul' monotone_const @[to_additive monotone.const_add] lemma monotone.const_mul' (hf : monotone f) (a : α) : monotone (λ x, a * f x) := monotone_const.mul' hf end mono end ordered_comm_monoid lemma bit0_pos [ordered_add_comm_monoid α] {a : α} (h : 0 < a) : 0 < bit0 a := add_pos' h h namespace units @[to_additive] instance [monoid α] [preorder α] : preorder (units α) := preorder.lift (coe : units α → α) @[simp, to_additive, norm_cast] theorem coe_le_coe [monoid α] [preorder α] {a b : units α} : (a : α) ≤ b ↔ a ≤ b := iff.rfl @[simp, to_additive, norm_cast] theorem coe_lt_coe [monoid α] [preorder α] {a b : units α} : (a : α) < b ↔ a < b := iff.rfl @[to_additive] instance [monoid α] [partial_order α] : partial_order (units α) := partial_order.lift coe units.ext @[to_additive] instance [monoid α] [linear_order α] : linear_order (units α) := linear_order.lift coe units.ext @[to_additive] instance [monoid α] [decidable_linear_order α] : decidable_linear_order (units α) := decidable_linear_order.lift coe units.ext @[simp, to_additive, norm_cast] theorem max_coe [monoid α] [decidable_linear_order α] {a b : units α} : (↑(max a b) : α) = max a b := by by_cases a ≤ b; simp [max, h] @[simp, to_additive, norm_cast] theorem min_coe [monoid α] [decidable_linear_order α] {a b : units α} : (↑(min a b) : α) = min a b := by by_cases a ≤ b; simp [min, h] end units namespace with_zero instance [preorder α] : preorder (with_zero α) := with_bot.preorder instance [partial_order α] : partial_order (with_zero α) := with_bot.partial_order instance [partial_order α] : order_bot (with_zero α) := with_bot.order_bot instance [lattice α] : lattice (with_zero α) := with_bot.lattice instance [linear_order α] : linear_order (with_zero α) := with_bot.linear_order instance [decidable_linear_order α] : decidable_linear_order (with_zero α) := with_bot.decidable_linear_order /-- If `0` is the least element in `α`, then `with_zero α` is an `ordered_add_comm_monoid`. -/ def ordered_add_comm_monoid [ordered_add_comm_monoid α] (zero_le : ∀ a : α, 0 ≤ a) : ordered_add_comm_monoid (with_zero α) := begin suffices, refine { add_le_add_left := this, ..with_zero.partial_order, ..with_zero.add_comm_monoid, .. }, { intros a b c h, have h' := lt_iff_le_not_le.1 h, rw lt_iff_le_not_le at ⊢, refine ⟨λ b h₂, _, λ h₂, h'.2 $ this _ _ h₂ _⟩, cases h₂, cases c with c, { cases h'.2 (this _ _ bot_le a) }, { refine ⟨_, rfl, _⟩, cases a with a, { exact with_bot.some_le_some.1 h'.1 }, { exact le_of_lt (lt_of_add_lt_add_left' $ with_bot.some_lt_some.1 h), } } }, { intros a b h c ca h₂, cases b with b, { rw le_antisymm h bot_le at h₂, exact ⟨_, h₂, le_refl _⟩ }, cases a with a, { change c + 0 = some ca at h₂, simp at h₂, simp [h₂], exact ⟨_, rfl, by simpa using add_le_add_left' (zero_le b)⟩ }, { simp at h, cases c with c; change some _ = _ at h₂; simp [-add_comm] at h₂; subst ca; refine ⟨_, rfl, _⟩, { exact h }, { exact add_le_add_left' h } } } end end with_zero namespace with_top section has_one variables [has_one α] @[to_additive] instance : has_one (with_top α) := ⟨(1 : α)⟩ @[simp, to_additive] lemma coe_one : ((1 : α) : with_top α) = 1 := rfl @[simp, to_additive] lemma coe_eq_one {a : α} : (a : with_top α) = 1 ↔ a = 1 := coe_eq_coe @[simp, to_additive] theorem one_eq_coe {a : α} : 1 = (a : with_top α) ↔ a = 1 := by rw [eq_comm, coe_eq_one] attribute [norm_cast] coe_one coe_eq_one coe_zero coe_eq_zero one_eq_coe zero_eq_coe @[simp, to_additive] theorem top_ne_one : ⊤ ≠ (1 : with_top α) . @[simp, to_additive] theorem one_ne_top : (1 : with_top α) ≠ ⊤ . end has_one instance [has_add α] : has_add (with_top α) := ⟨λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a + b))⟩ instance [add_semigroup α] : add_semigroup (with_top α) := { add := (+), ..@additive.add_semigroup _ $ @with_zero.semigroup (multiplicative α) _ } @[norm_cast] lemma coe_add [has_add α] {a b : α} : ((a + b : α) : with_top α) = a + b := rfl @[norm_cast] lemma coe_bit0 [has_add α] {a : α} : ((bit0 a : α) : with_top α) = bit0 a := rfl @[norm_cast] lemma coe_bit1 [has_add α] [has_one α] {a : α} : ((bit1 a : α) : with_top α) = bit1 a := rfl instance [add_comm_semigroup α] : add_comm_semigroup (with_top α) := { ..@additive.add_comm_semigroup _ $ @with_zero.comm_semigroup (multiplicative α) _ } instance [add_monoid α] : add_monoid (with_top α) := { zero := some 0, add := (+), ..@additive.add_monoid _ $ @with_zero.monoid (multiplicative α) _ } instance [add_comm_monoid α] : add_comm_monoid (with_top α) := { zero := 0, add := (+), ..@additive.add_comm_monoid _ $ @with_zero.comm_monoid (multiplicative α) _ } instance [ordered_add_comm_monoid α] : ordered_add_comm_monoid (with_top α) := begin suffices, refine { add_le_add_left := this, ..with_top.partial_order, ..with_top.add_comm_monoid, ..}, { intros a b c h, have h' := h, rw lt_iff_le_not_le at h' ⊢, refine ⟨λ c h₂, _, λ h₂, h'.2 $ this _ _ h₂ _⟩, cases h₂, cases a with a, { exact (not_le_of_lt h).elim le_top }, cases b with b, { exact (not_le_of_lt h).elim le_top }, { exact ⟨_, rfl, le_of_lt (lt_of_add_lt_add_left' $ with_top.some_lt_some.1 h)⟩ } }, { intros a b h c ca h₂, cases c with c, {cases h₂}, cases b with b; cases h₂, cases a with a, {cases le_antisymm h le_top }, simp at h, exact ⟨_, rfl, add_le_add_left' h⟩, } end @[simp] lemma zero_lt_top [ordered_add_comm_monoid α] : (0 : with_top α) < ⊤ := coe_lt_top 0 @[simp, norm_cast] lemma zero_lt_coe [ordered_add_comm_monoid α] (a : α) : (0 : with_top α) < a ↔ 0 < a := coe_lt_coe @[simp] lemma add_top [ordered_add_comm_monoid α] : ∀{a : with_top α}, a + ⊤ = ⊤ | none := rfl | (some a) := rfl @[simp] lemma top_add [ordered_add_comm_monoid α] {a : with_top α} : ⊤ + a = ⊤ := rfl lemma add_eq_top [ordered_add_comm_monoid α] (a b : with_top α) : a + b = ⊤ ↔ a = ⊤ ∨ b = ⊤ := by cases a; cases b; simp [none_eq_top, some_eq_coe, coe_add.symm] lemma add_lt_top [ordered_add_comm_monoid α] (a b : with_top α) : a + b < ⊤ ↔ a < ⊤ ∧ b < ⊤ := begin apply not_iff_not.1, simp [lt_top_iff_ne_top, add_eq_top], finish, apply classical.dec _, apply classical.dec _, end end with_top namespace with_bot instance [has_zero α] : has_zero (with_bot α) := with_top.has_zero instance [has_one α] : has_one (with_bot α) := with_top.has_one instance [add_semigroup α] : add_semigroup (with_bot α) := with_top.add_semigroup instance [add_comm_semigroup α] : add_comm_semigroup (with_bot α) := with_top.add_comm_semigroup instance [add_monoid α] : add_monoid (with_bot α) := with_top.add_monoid instance [add_comm_monoid α] : add_comm_monoid (with_bot α) := with_top.add_comm_monoid instance [ordered_add_comm_monoid α] : ordered_add_comm_monoid (with_bot α) := begin suffices, refine { add_le_add_left := this, ..with_bot.partial_order, ..with_bot.add_comm_monoid, ..}, { intros a b c h, have h' := h, rw lt_iff_le_not_le at h' ⊢, refine ⟨λ b h₂, _, λ h₂, h'.2 $ this _ _ h₂ _⟩, cases h₂, cases a with a, { exact (not_le_of_lt h).elim bot_le }, cases c with c, { exact (not_le_of_lt h).elim bot_le }, { exact ⟨_, rfl, le_of_lt (lt_of_add_lt_add_left' $ with_bot.some_lt_some.1 h)⟩ } }, { intros a b h c ca h₂, cases c with c, {cases h₂}, cases a with a; cases h₂, cases b with b, {cases le_antisymm h bot_le}, simp at h, exact ⟨_, rfl, add_le_add_left' h⟩, } end -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_zero [has_zero α] : ((0 : α) : with_bot α) = 0 := rfl -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_one [has_one α] : ((1 : α) : with_bot α) = 1 := rfl -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_eq_zero {α : Type*} [add_monoid α] {a : α} : (a : with_bot α) = 0 ↔ a = 0 := by norm_cast -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_add [add_semigroup α] (a b : α) : ((a + b : α) : with_bot α) = a + b := by norm_cast -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_bit0 [add_semigroup α] {a : α} : ((bit0 a : α) : with_bot α) = bit0 a := by norm_cast -- `by norm_cast` proves this lemma, so I did not tag it with `norm_cast` lemma coe_bit1 [add_semigroup α] [has_one α] {a : α} : ((bit1 a : α) : with_bot α) = bit1 a := by norm_cast @[simp] lemma bot_add [ordered_add_comm_monoid α] (a : with_bot α) : ⊥ + a = ⊥ := rfl @[simp] lemma add_bot [ordered_add_comm_monoid α] (a : with_bot α) : a + ⊥ = ⊥ := by cases a; refl end with_bot /-- A canonically ordered monoid is an ordered commutative monoid in which the ordering coincides with the divisibility relation, which is to say, `a ≤ b` iff there exists `c` with `b = a + c`. This is satisfied by the natural numbers, for example, but not the integers or other ordered groups. -/ @[protect_proj] class canonically_ordered_add_monoid (α : Type*) extends ordered_add_comm_monoid α, order_bot α := (le_iff_exists_add : ∀a b:α, a ≤ b ↔ ∃c, b = a + c) section canonically_ordered_add_monoid variables [canonically_ordered_add_monoid α] {a b c d : α} lemma le_iff_exists_add : a ≤ b ↔ ∃c, b = a + c := canonically_ordered_add_monoid.le_iff_exists_add a b @[simp] lemma zero_le (a : α) : 0 ≤ a := le_iff_exists_add.mpr ⟨a, by simp⟩ @[simp] lemma bot_eq_zero : (⊥ : α) = 0 := le_antisymm bot_le (zero_le ⊥) @[simp] lemma add_eq_zero_iff : a + b = 0 ↔ a = 0 ∧ b = 0 := add_eq_zero_iff' (zero_le _) (zero_le _) @[simp] lemma le_zero_iff_eq : a ≤ 0 ↔ a = 0 := iff.intro (assume h, le_antisymm h (zero_le a)) (assume h, h ▸ le_refl a) lemma zero_lt_iff_ne_zero : 0 < a ↔ a ≠ 0 := iff.intro ne_of_gt $ assume hne, lt_of_le_of_ne (zero_le _) hne.symm @[nolint ge_or_gt] lemma exists_pos_add_of_lt (h : a < b) : ∃ c > 0, a + c = b := begin obtain ⟨c, hc⟩ := le_iff_exists_add.1 (le_of_lt h), refine ⟨c, zero_lt_iff_ne_zero.2 _, hc.symm⟩, rintro rfl, simpa [hc, lt_irrefl] using h end lemma le_add_left (h : a ≤ c) : a ≤ b + c := calc a = 0 + a : by simp ... ≤ b + c : add_le_add (zero_le _) h lemma le_add_right (h : a ≤ b) : a ≤ b + c := calc a = a + 0 : by simp ... ≤ b + c : add_le_add h (zero_le _) instance with_zero.canonically_ordered_add_monoid : canonically_ordered_add_monoid (with_zero α) := { le_iff_exists_add := λ a b, begin cases a with a, { exact iff_of_true bot_le ⟨b, (zero_add b).symm⟩ }, cases b with b, { exact iff_of_false (mt (le_antisymm bot_le) (by simp)) (λ ⟨c, h⟩, by cases c; cases h) }, { simp [le_iff_exists_add, -add_comm], split; intro h; rcases h with ⟨c, h⟩, { exact ⟨some c, congr_arg some h⟩ }, { cases c; cases h, { exact ⟨_, (add_zero _).symm⟩ }, { exact ⟨_, rfl⟩ } } } end, bot := 0, bot_le := assume a a' h, option.no_confusion h, .. with_zero.ordered_add_comm_monoid zero_le } instance with_top.canonically_ordered_add_monoid : canonically_ordered_add_monoid (with_top α) := { le_iff_exists_add := assume a b, match a, b with | a, none := show a ≤ ⊤ ↔ ∃c, ⊤ = a + c, by simp; refine ⟨⊤, _⟩; cases a; refl | (some a), (some b) := show (a:with_top α) ≤ ↑b ↔ ∃c:with_top α, ↑b = ↑a + c, begin simp [canonically_ordered_add_monoid.le_iff_exists_add, -add_comm], split, { rintro ⟨c, rfl⟩, refine ⟨c, _⟩, norm_cast }, { exact assume h, match b, h with _, ⟨some c, rfl⟩ := ⟨_, rfl⟩ end } end | none, some b := show (⊤ : with_top α) ≤ b ↔ ∃c:with_top α, ↑b = ⊤ + c, by simp end, .. with_top.order_bot, .. with_top.ordered_add_comm_monoid } end canonically_ordered_add_monoid /-- An ordered cancellative additive commutative monoid is an additive commutative monoid with a partial order, in which addition is cancellative and strictly monotone. -/ @[protect_proj, ancestor add_comm_monoid add_left_cancel_semigroup add_right_cancel_semigroup partial_order] class ordered_cancel_add_comm_monoid (α : Type u) extends add_comm_monoid α, add_left_cancel_semigroup α, add_right_cancel_semigroup α, partial_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) (le_of_add_le_add_left : ∀ a b c : α, a + b ≤ a + c → b ≤ c) /-- An ordered cancellative commutative monoid is a commutative monoid with a partial order, in which multiplication is cancellative and strictly monotone. -/ @[protect_proj, ancestor comm_monoid left_cancel_semigroup right_cancel_semigroup partial_order] class ordered_cancel_comm_monoid (α : Type u) extends comm_monoid α, left_cancel_semigroup α, right_cancel_semigroup α, partial_order α := (mul_le_mul_left : ∀ a b : α, a ≤ b → ∀ c : α, c * a ≤ c * b) (le_of_mul_le_mul_left : ∀ a b c : α, a * b ≤ a * c → b ≤ c) attribute [to_additive ordered_cancel_add_comm_monoid] ordered_cancel_comm_monoid section ordered_cancel_comm_monoid variables [ordered_cancel_comm_monoid α] {a b c d : α} @[to_additive] instance ordered_cancel_comm_monoid.to_left_cancel_monoid : left_cancel_monoid α := { ..‹ordered_cancel_comm_monoid α› } @[to_additive add_le_add_left] lemma mul_le_mul_left'' : ∀ {a b : α} (h : a ≤ b) (c : α), c * a ≤ c * b := ordered_cancel_comm_monoid.mul_le_mul_left @[to_additive le_of_add_le_add_left] lemma le_of_mul_le_mul_left' : ∀ {a b c : α}, a * b ≤ a * c → b ≤ c := ordered_cancel_comm_monoid.le_of_mul_le_mul_left @[to_additive add_lt_add_left] lemma mul_lt_mul_left' (h : a < b) (c : α) : c * a < c * b := lt_of_le_not_le (mul_le_mul_left'' (le_of_lt h) _) $ mt le_of_mul_le_mul_left' (not_le_of_gt h) @[to_additive lt_of_add_lt_add_left] lemma lt_of_mul_lt_mul_left'' (h : a * b < a * c) : b < c := lt_of_le_not_le (le_of_mul_le_mul_left' (le_of_lt h)) $ mt (λ h, mul_le_mul_left'' h _) (not_le_of_gt h) @[to_additive] instance ordered_cancel_comm_monoid.to_ordered_comm_monoid : ordered_comm_monoid α := { lt_of_mul_lt_mul_left := @lt_of_mul_lt_mul_left'' _ _, ..‹ordered_cancel_comm_monoid α› } @[to_additive add_le_add_right] lemma mul_le_mul_right'' (h : a ≤ b) (c : α) : a * c ≤ b * c := mul_comm c a ▸ mul_comm c b ▸ mul_le_mul_left' h @[to_additive add_lt_add_right] lemma mul_lt_mul_right' (h : a < b) (c : α) : a * c < b * c := begin rw [mul_comm a c, mul_comm b c], exact (mul_lt_mul_left' h c) end @[to_additive] lemma le_mul_of_one_le_right (h : 1 ≤ b) : a ≤ a * b := have a * 1 ≤ a * b, from mul_le_mul_left' h, by rwa mul_one at this @[to_additive] lemma le_mul_of_one_le_left (h : 1 ≤ b) : a ≤ b * a := have 1 * a ≤ b * a, from mul_le_mul_right' h, by rwa one_mul at this @[to_additive add_lt_add] lemma mul_lt_mul''' (h₁ : a < b) (h₂ : c < d) : a * c < b * d := lt_trans (mul_lt_mul_right' h₁ c) (mul_lt_mul_left' h₂ b) @[to_additive] lemma mul_lt_mul_of_le_of_lt (h₁ : a ≤ b) (h₂ : c < d) : a * c < b * d := lt_of_le_of_lt (mul_le_mul_right' h₁) (mul_lt_mul_left' h₂ b) @[to_additive] lemma mul_lt_mul_of_lt_of_le (h₁ : a < b) (h₂ : c ≤ d) : a * c < b * d := lt_of_lt_of_le (mul_lt_mul_right' h₁ c) (mul_le_mul_left' h₂) @[to_additive] lemma lt_mul_of_one_lt_right (a : α) {b : α} (h : 1 < b) : a < a * b := have a * 1 < a * b, from mul_lt_mul_left' h a, by rwa [mul_one] at this @[to_additive] lemma lt_mul_of_one_lt_left (a : α) {b : α} (h : 1 < b) : a < b * a := have 1 * a < b * a, from mul_lt_mul_right' h a, by rwa [one_mul] at this @[to_additive le_of_add_le_add_right] lemma le_of_mul_le_mul_right' (h : a * b ≤ c * b) : a ≤ c := le_of_mul_le_mul_left' (show b * a ≤ b * c, begin rw [mul_comm b a, mul_comm b c], assumption end) @[to_additive lt_of_add_lt_add_right] lemma lt_of_mul_lt_mul_right'' (h : a * b < c * b) : a < c := lt_of_mul_lt_mul_left'' (show b * a < b * c, begin rw [mul_comm b a, mul_comm b c], assumption end) -- here we start using properties of one. @[to_additive add_nonneg] lemma one_le_mul (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b := one_mul (1:α) ▸ (mul_le_mul' ha hb) @[to_additive] lemma mul_one_lt (ha : 1 < a) (hb : 1 < b) : 1 < a * b := one_mul (1:α) ▸ (mul_lt_mul''' ha hb) @[to_additive] lemma mul_one_lt_of_one_lt_of_one_le (ha : 1 < a) (hb : 1 ≤ b) : 1 < a * b := one_mul (1:α) ▸ (mul_lt_mul_of_lt_of_le ha hb) @[to_additive] lemma mul_one_lt_of_one_le_of_one_lt (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := one_mul (1:α) ▸ (mul_lt_mul_of_le_of_lt ha hb) @[to_additive add_nonpos] lemma mul_le_one'' (ha : a ≤ 1) (hb : b ≤ 1) : a * b ≤ 1 := one_mul (1:α) ▸ (mul_le_mul' ha hb) @[to_additive] lemma mul_lt_one (ha : a < 1) (hb : b < 1) : a * b < 1 := one_mul (1:α) ▸ (mul_lt_mul''' ha hb) @[to_additive] lemma mul_lt_one_of_lt_one_of_le_one (ha : a < 1) (hb : b ≤ 1) : a * b < 1 := one_mul (1:α) ▸ (mul_lt_mul_of_lt_of_le ha hb) @[to_additive] lemma mul_lt_one_of_le_one_of_lt_one (ha : a ≤ 1) (hb : b < 1) : a * b < 1 := one_mul (1:α) ▸ (mul_lt_mul_of_le_of_lt ha hb) @[to_additive] lemma mul_eq_one_iff_eq_one_and_eq_one_of_one_le_of_one_le (ha : 1 ≤ a) (hb : 1 ≤ b) : a * b = 1 ↔ a = 1 ∧ b = 1 := iff.intro (assume hab : a * b = 1, have ha' : a ≤ 1, from calc a = a * 1 : by rw mul_one ... ≤ a * b : mul_le_mul_left' hb ... = 1 : hab, have haz : a = 1, from le_antisymm ha' ha, have hb' : b ≤ 1, from calc b = 1 * b : by rw one_mul ... ≤ a * b : by exact mul_le_mul_right' ha ... = 1 : hab, have hbz : b = 1, from le_antisymm hb' hb, and.intro haz hbz) (assume ⟨ha', hb'⟩, by rw [ha', hb', mul_one]) @[to_additive] lemma le_mul_of_one_le_of_le (ha : 1 ≤ a) (hbc : b ≤ c) : b ≤ a * c := one_mul b ▸ mul_le_mul' ha hbc @[to_additive] lemma le_mul_of_le_of_one_le (hbc : b ≤ c) (ha : 1 ≤ a) : b ≤ c * a := mul_one b ▸ mul_le_mul' hbc ha @[to_additive] lemma lt_mul_of_one_lt_of_le (ha : 1 < a) (hbc : b ≤ c) : b < a * c := one_mul b ▸ mul_lt_mul_of_lt_of_le ha hbc @[to_additive] lemma lt_mul_of_le_of_one_lt (hbc : b ≤ c) (ha : 1 < a) : b < c * a := mul_one b ▸ mul_lt_mul_of_le_of_lt hbc ha @[to_additive] lemma mul_le_of_le_one_of_le (ha : a ≤ 1) (hbc : b ≤ c) : a * b ≤ c := one_mul c ▸ mul_le_mul' ha hbc @[to_additive] lemma mul_le_of_le_of_le_one (hbc : b ≤ c) (ha : a ≤ 1) : b * a ≤ c := mul_one c ▸ mul_le_mul' hbc ha @[to_additive] lemma mul_lt_of_lt_one_of_le (ha : a < 1) (hbc : b ≤ c) : a * b < c := one_mul c ▸ mul_lt_mul_of_lt_of_le ha hbc @[to_additive] lemma mul_lt_of_le_of_lt_one (hbc : b ≤ c) (ha : a < 1) : b * a < c := mul_one c ▸ mul_lt_mul_of_le_of_lt hbc ha @[to_additive] lemma lt_mul_of_one_le_of_lt (ha : 1 ≤ a) (hbc : b < c) : b < a * c := one_mul b ▸ mul_lt_mul_of_le_of_lt ha hbc @[to_additive] lemma lt_mul_of_lt_of_one_le (hbc : b < c) (ha : 1 ≤ a) : b < c * a := mul_one b ▸ mul_lt_mul_of_lt_of_le hbc ha @[to_additive] lemma lt_mul_of_one_lt_of_lt (ha : 1 < a) (hbc : b < c) : b < a * c := one_mul b ▸ mul_lt_mul''' ha hbc @[to_additive] lemma lt_mul_of_lt_of_one_lt (hbc : b < c) (ha : 1 < a) : b < c * a := mul_one b ▸ mul_lt_mul''' hbc ha @[to_additive] lemma mul_lt_of_le_one_of_lt (ha : a ≤ 1) (hbc : b < c) : a * b < c := one_mul c ▸ mul_lt_mul_of_le_of_lt ha hbc @[to_additive] lemma mul_lt_of_lt_of_le_one (hbc : b < c) (ha : a ≤ 1) : b * a < c := mul_one c ▸ mul_lt_mul_of_lt_of_le hbc ha @[to_additive] lemma mul_lt_of_lt_one_of_lt (ha : a < 1) (hbc : b < c) : a * b < c := one_mul c ▸ mul_lt_mul''' ha hbc @[to_additive] lemma mul_lt_of_lt_of_lt_one (hbc : b < c) (ha : a < 1) : b * a < c := mul_one c ▸ mul_lt_mul''' hbc ha @[simp, to_additive] lemma mul_le_mul_iff_left (a : α) {b c : α} : a * b ≤ a * c ↔ b ≤ c := ⟨le_of_mul_le_mul_left', λ h, mul_le_mul_left' h⟩ @[simp, to_additive] lemma mul_le_mul_iff_right (c : α) : a * c ≤ b * c ↔ a ≤ b := mul_comm c a ▸ mul_comm c b ▸ mul_le_mul_iff_left c @[simp, to_additive] lemma mul_lt_mul_iff_left (a : α) {b c : α} : a * b < a * c ↔ b < c := ⟨lt_of_mul_lt_mul_left'', λ h, mul_lt_mul_left' h _⟩ @[simp, to_additive] lemma mul_lt_mul_iff_right (c : α) : a * c < b * c ↔ a < b := mul_comm c a ▸ mul_comm c b ▸ mul_lt_mul_iff_left c @[simp, to_additive le_add_iff_nonneg_right] lemma le_mul_iff_one_le_right' (a : α) {b : α} : a ≤ a * b ↔ 1 ≤ b := have a * 1 ≤ a * b ↔ 1 ≤ b, from mul_le_mul_iff_left a, by rwa mul_one at this @[simp, to_additive le_add_iff_nonneg_left] lemma le_mul_iff_one_le_left' (a : α) {b : α} : a ≤ b * a ↔ 1 ≤ b := by rw [mul_comm, le_mul_iff_one_le_right'] @[simp, to_additive lt_add_iff_pos_right] lemma lt_mul_iff_one_lt_right' (a : α) {b : α} : a < a * b ↔ 1 < b := have a * 1 < a * b ↔ 1 < b, from mul_lt_mul_iff_left a, by rwa mul_one at this @[simp, to_additive lt_add_iff_pos_left] lemma lt_mul_iff_one_lt_left' (a : α) {b : α} : a < b * a ↔ 1 < b := by rw [mul_comm, lt_mul_iff_one_lt_right'] @[simp, to_additive add_le_iff_nonpos_left] lemma mul_le_iff_le_one_left' : a * b ≤ b ↔ a ≤ 1 := by { convert mul_le_mul_iff_right b, rw [one_mul] } @[simp, to_additive add_le_iff_nonpos_right] lemma mul_le_iff_le_one_right' : a * b ≤ a ↔ b ≤ 1 := by { convert mul_le_mul_iff_left a, rw [mul_one] } @[simp, to_additive add_lt_iff_neg_right] lemma mul_lt_iff_lt_one_right' : a * b < b ↔ a < 1 := by { convert mul_lt_mul_iff_right b, rw [one_mul] } @[simp, to_additive add_lt_iff_neg_left] lemma mul_lt_iff_lt_one_left' : a * b < a ↔ b < 1 := by { convert mul_lt_mul_iff_left a, rw [mul_one] } @[to_additive] lemma mul_eq_one_iff_eq_one_of_one_le (ha : 1 ≤ a) (hb : 1 ≤ b) : a * b = 1 ↔ a = 1 ∧ b = 1 := ⟨λ hab : a * b = 1, by split; apply le_antisymm; try {assumption}; rw ← hab; simp [ha, hb], λ ⟨ha', hb'⟩, by rw [ha', hb', mul_one]⟩ section mono variables {β : Type*} [preorder β] {f g : β → α} @[to_additive monotone.add_strict_mono] lemma monotone.mul_strict_mono' (hf : monotone f) (hg : strict_mono g) : strict_mono (λ x, f x * g x) := λ x y h, mul_lt_mul_of_le_of_lt (hf $ le_of_lt h) (hg h) @[to_additive strict_mono.add_monotone] lemma strict_mono.mul_monotone' (hf : strict_mono f) (hg : monotone g) : strict_mono (λ x, f x * g x) := λ x y h, mul_lt_mul_of_lt_of_le (hf h) (hg $ le_of_lt h) @[to_additive strict_mono.add_const] lemma strict_mono.mul_const' (hf : strict_mono f) (c : α) : strict_mono (λ x, f x * c) := hf.mul_monotone' monotone_const @[to_additive strict_mono.const_add] lemma strict_mono.const_mul' (hf : strict_mono f) (c : α) : strict_mono (λ x, c * f x) := monotone_const.mul_strict_mono' hf end mono end ordered_cancel_comm_monoid section ordered_cancel_add_comm_monoid variable [ordered_cancel_add_comm_monoid α] lemma with_top.add_lt_add_iff_left : ∀{a b c : with_top α}, a < ⊤ → (a + c < a + b ↔ c < b) | none := assume b c h, (lt_irrefl ⊤ h).elim | (some a) := begin assume b c h, cases b; cases c; simp [with_top.none_eq_top, with_top.some_eq_coe, with_top.coe_lt_top, with_top.coe_lt_coe], { norm_cast, exact with_top.coe_lt_top _ }, { norm_cast, exact add_lt_add_iff_left _ } end lemma with_top.add_lt_add_iff_right {a b c : with_top α} : a < ⊤ → (c + a < b + a ↔ c < b) := by simpa [add_comm] using @with_top.add_lt_add_iff_left _ _ a b c end ordered_cancel_add_comm_monoid /-- An ordered additive commutative group is an additive commutative group with a partial order in which addition is strictly monotone. -/ @[protect_proj, ancestor add_comm_group partial_order] class ordered_add_comm_group (α : Type u) extends add_comm_group α, partial_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) /-- An ordered commutative group is an commutative group with a partial order in which multiplication is strictly monotone. -/ @[protect_proj, ancestor comm_group partial_order] class ordered_comm_group (α : Type u) extends comm_group α, partial_order α := (mul_le_mul_left : ∀ a b : α, a ≤ b → ∀ c : α, c * a ≤ c * b) attribute [to_additive ordered_add_comm_group] ordered_comm_group /--The units of an ordered commutative monoid form an ordered commutative group. -/ @[to_additive] instance units.ordered_comm_group [ordered_comm_monoid α] : ordered_comm_group (units α) := { mul_le_mul_left := λ a b h c, mul_le_mul_left' h, .. units.partial_order, .. (infer_instance : comm_group (units α)) } section ordered_comm_group variables [ordered_comm_group α] {a b c d : α} @[to_additive ordered_add_comm_group.add_lt_add_left] lemma ordered_comm_group.mul_lt_mul_left' (a b : α) (h : a < b) (c : α) : c * a < c * b := begin rw lt_iff_le_not_le at h ⊢, split, { apply ordered_comm_group.mul_le_mul_left _ _ h.1 }, { intro w, replace w : c⁻¹ * (c * b) ≤ c⁻¹ * (c * a) := ordered_comm_group.mul_le_mul_left _ _ w _, simp only [mul_one, mul_comm, mul_left_inv, mul_left_comm] at w, exact h.2 w }, end @[to_additive ordered_add_comm_group.le_of_add_le_add_left] lemma ordered_comm_group.le_of_mul_le_mul_left (h : a * b ≤ a * c) : b ≤ c := have a⁻¹ * (a * b) ≤ a⁻¹ * (a * c), from ordered_comm_group.mul_le_mul_left _ _ h _, begin simp [inv_mul_cancel_left] at this, assumption end @[to_additive] lemma ordered_comm_group.lt_of_mul_lt_mul_left (h : a * b < a * c) : b < c := have a⁻¹ * (a * b) < a⁻¹ * (a * c), from ordered_comm_group.mul_lt_mul_left' _ _ h _, begin simp [inv_mul_cancel_left] at this, assumption end @[to_additive] instance ordered_comm_group.to_ordered_cancel_comm_monoid (α : Type u) [s : ordered_comm_group α] : ordered_cancel_comm_monoid α := { mul_left_cancel := @mul_left_cancel α _, mul_right_cancel := @mul_right_cancel α _, le_of_mul_le_mul_left := @ordered_comm_group.le_of_mul_le_mul_left α _, ..s } @[to_additive neg_le_neg] lemma inv_le_inv' (h : a ≤ b) : b⁻¹ ≤ a⁻¹ := have 1 ≤ a⁻¹ * b, from mul_left_inv a ▸ mul_le_mul_left' h, have 1 * b⁻¹ ≤ a⁻¹ * b * b⁻¹, from mul_le_mul_right' this, by rwa [mul_inv_cancel_right, one_mul] at this @[to_additive] lemma le_of_inv_le_inv (h : b⁻¹ ≤ a⁻¹) : a ≤ b := suffices (a⁻¹)⁻¹ ≤ (b⁻¹)⁻¹, from begin simp [inv_inv] at this, assumption end, inv_le_inv' h @[to_additive] lemma one_le_of_inv_le_one (h : a⁻¹ ≤ 1) : 1 ≤ a := have a⁻¹ ≤ 1⁻¹, by rwa one_inv, le_of_inv_le_inv this @[to_additive] lemma inv_le_one_of_one_le (h : 1 ≤ a) : a⁻¹ ≤ 1 := have a⁻¹ ≤ 1⁻¹, from inv_le_inv' h, by rwa one_inv at this @[to_additive nonpos_of_neg_nonneg] lemma le_one_of_one_le_inv (h : 1 ≤ a⁻¹) : a ≤ 1 := have 1⁻¹ ≤ a⁻¹, by rwa one_inv, le_of_inv_le_inv this @[to_additive neg_nonneg_of_nonpos] lemma one_le_inv_of_le_one (h : a ≤ 1) : 1 ≤ a⁻¹ := have 1⁻¹ ≤ a⁻¹, from inv_le_inv' h, by rwa one_inv at this @[to_additive neg_lt_neg] lemma inv_lt_inv' (h : a < b) : b⁻¹ < a⁻¹ := have 1 < a⁻¹ * b, from mul_left_inv a ▸ mul_lt_mul_left' h (a⁻¹), have 1 * b⁻¹ < a⁻¹ * b * b⁻¹, from mul_lt_mul_right' this (b⁻¹), by rwa [mul_inv_cancel_right, one_mul] at this @[to_additive] lemma lt_of_inv_lt_inv (h : b⁻¹ < a⁻¹) : a < b := inv_inv a ▸ inv_inv b ▸ inv_lt_inv' h @[to_additive] lemma one_lt_of_inv_inv (h : a⁻¹ < 1) : 1 < a := have a⁻¹ < 1⁻¹, by rwa one_inv, lt_of_inv_lt_inv this @[to_additive] lemma inv_inv_of_one_lt (h : 1 < a) : a⁻¹ < 1 := have a⁻¹ < 1⁻¹, from inv_lt_inv' h, by rwa one_inv at this @[to_additive neg_of_neg_pos] lemma inv_of_one_lt_inv (h : 1 < a⁻¹) : a < 1 := have 1⁻¹ < a⁻¹, by rwa one_inv, lt_of_inv_lt_inv this @[to_additive neg_pos_of_neg] lemma one_lt_inv_of_inv (h : a < 1) : 1 < a⁻¹ := have 1⁻¹ < a⁻¹, from inv_lt_inv' h, by rwa one_inv at this @[to_additive] lemma le_inv_of_le_inv (h : a ≤ b⁻¹) : b ≤ a⁻¹ := begin have h := inv_le_inv' h, rwa inv_inv at h end @[to_additive] lemma inv_le_of_inv_le (h : a⁻¹ ≤ b) : b⁻¹ ≤ a := begin have h := inv_le_inv' h, rwa inv_inv at h end @[to_additive] lemma lt_inv_of_lt_inv (h : a < b⁻¹) : b < a⁻¹ := begin have h := inv_lt_inv' h, rwa inv_inv at h end @[to_additive] lemma inv_lt_of_inv_lt (h : a⁻¹ < b) : b⁻¹ < a := begin have h := inv_lt_inv' h, rwa inv_inv at h end @[to_additive] lemma mul_le_of_le_inv_mul (h : b ≤ a⁻¹ * c) : a * b ≤ c := begin have h := mul_le_mul_left' h, rwa mul_inv_cancel_left at h end @[to_additive] lemma le_inv_mul_of_mul_le (h : a * b ≤ c) : b ≤ a⁻¹ * c := begin have h := mul_le_mul_left' h, rwa inv_mul_cancel_left at h end @[to_additive] lemma le_mul_of_inv_mul_le (h : b⁻¹ * a ≤ c) : a ≤ b * c := begin have h := mul_le_mul_left' h, rwa mul_inv_cancel_left at h end @[to_additive] lemma inv_mul_le_of_le_mul (h : a ≤ b * c) : b⁻¹ * a ≤ c := begin have h := mul_le_mul_left' h, rwa inv_mul_cancel_left at h end @[to_additive] lemma le_mul_of_inv_mul_le_left (h : b⁻¹ * a ≤ c) : a ≤ b * c := le_mul_of_inv_mul_le h @[to_additive] lemma inv_mul_le_left_of_le_mul (h : a ≤ b * c) : b⁻¹ * a ≤ c := inv_mul_le_of_le_mul h @[to_additive] lemma le_mul_of_inv_mul_le_right (h : c⁻¹ * a ≤ b) : a ≤ b * c := by { rw mul_comm, exact le_mul_of_inv_mul_le h } @[to_additive] lemma inv_mul_le_right_of_le_mul (h : a ≤ b * c) : c⁻¹ * a ≤ b := by { rw mul_comm at h, apply inv_mul_le_left_of_le_mul h } @[to_additive] lemma mul_lt_of_lt_inv_mul (h : b < a⁻¹ * c) : a * b < c := begin have h := mul_lt_mul_left' h a, rwa mul_inv_cancel_left at h end @[to_additive] lemma lt_inv_mul_of_mul_lt (h : a * b < c) : b < a⁻¹ * c := begin have h := mul_lt_mul_left' h (a⁻¹), rwa inv_mul_cancel_left at h end @[to_additive] lemma lt_mul_of_inv_mul_lt (h : b⁻¹ * a < c) : a < b * c := begin have h := mul_lt_mul_left' h b, rwa mul_inv_cancel_left at h end @[to_additive] lemma inv_mul_lt_of_lt_mul (h : a < b * c) : b⁻¹ * a < c := begin have h := mul_lt_mul_left' h (b⁻¹), rwa inv_mul_cancel_left at h end @[to_additive] lemma lt_mul_of_inv_mul_lt_left (h : b⁻¹ * a < c) : a < b * c := lt_mul_of_inv_mul_lt h @[to_additive] lemma inv_mul_lt_left_of_lt_mul (h : a < b * c) : b⁻¹ * a < c := inv_mul_lt_of_lt_mul h @[to_additive] lemma lt_mul_of_inv_mul_lt_right (h : c⁻¹ * a < b) : a < b * c := by { rw mul_comm, exact lt_mul_of_inv_mul_lt h } @[to_additive] lemma inv_mul_lt_right_of_lt_mul (h : a < b * c) : c⁻¹ * a < b := by { rw mul_comm at h, exact inv_mul_lt_of_lt_mul h } @[to_additive] lemma mul_le_mul_three {a b c d e f : α} (h₁ : a ≤ d) (h₂ : b ≤ e) (h₃ : c ≤ f) : a * b * c ≤ d * e * f := begin apply le_trans, apply mul_le_mul', apply mul_le_mul', assumption', apply le_refl end @[simp, to_additive] lemma inv_lt_one_iff_one_lt : a⁻¹ < 1 ↔ 1 < a := ⟨ one_lt_of_inv_inv, inv_inv_of_one_lt ⟩ @[simp, to_additive] lemma inv_le_inv_iff : a⁻¹ ≤ b⁻¹ ↔ b ≤ a := have a * b * a⁻¹ ≤ a * b * b⁻¹ ↔ a⁻¹ ≤ b⁻¹, from mul_le_mul_iff_left _, by { rw [mul_inv_cancel_right, mul_comm a, mul_inv_cancel_right] at this, rw [this] } @[to_additive neg_le] lemma inv_le' : a⁻¹ ≤ b ↔ b⁻¹ ≤ a := have a⁻¹ ≤ (b⁻¹)⁻¹ ↔ b⁻¹ ≤ a, from inv_le_inv_iff, by rwa inv_inv at this @[to_additive le_neg] lemma le_inv' : a ≤ b⁻¹ ↔ b ≤ a⁻¹ := have (a⁻¹)⁻¹ ≤ b⁻¹ ↔ b ≤ a⁻¹, from inv_le_inv_iff, by rwa inv_inv at this @[to_additive neg_le_iff_add_nonneg] lemma inv_le_iff_one_le_mul : a⁻¹ ≤ b ↔ 1 ≤ a * b := (mul_le_mul_iff_left a).symm.trans $ by rw mul_inv_self @[to_additive] lemma le_inv_iff_mul_le_one : a ≤ b⁻¹ ↔ a * b ≤ 1 := (mul_le_mul_iff_right b).symm.trans $ by rw inv_mul_self @[simp, to_additive neg_nonpos] lemma inv_le_one' : a⁻¹ ≤ 1 ↔ 1 ≤ a := have a⁻¹ ≤ 1⁻¹ ↔ 1 ≤ a, from inv_le_inv_iff, by rwa one_inv at this @[simp, to_additive neg_nonneg] lemma one_le_inv' : 1 ≤ a⁻¹ ↔ a ≤ 1 := have 1⁻¹ ≤ a⁻¹ ↔ a ≤ 1, from inv_le_inv_iff, by rwa one_inv at this @[to_additive] lemma inv_le_self (h : 1 ≤ a) : a⁻¹ ≤ a := le_trans (inv_le_one'.2 h) h @[to_additive] lemma self_le_inv (h : a ≤ 1) : a ≤ a⁻¹ := le_trans h (one_le_inv'.2 h) @[simp, to_additive] lemma inv_lt_inv_iff : a⁻¹ < b⁻¹ ↔ b < a := have a * b * a⁻¹ < a * b * b⁻¹ ↔ a⁻¹ < b⁻¹, from mul_lt_mul_iff_left _, by { rw [mul_inv_cancel_right, mul_comm a, mul_inv_cancel_right] at this, rw [this] } @[to_additive neg_lt_zero] lemma inv_lt_one' : a⁻¹ < 1 ↔ 1 < a := have a⁻¹ < 1⁻¹ ↔ 1 < a, from inv_lt_inv_iff, by rwa one_inv at this @[to_additive neg_pos] lemma one_lt_inv' : 1 < a⁻¹ ↔ a < 1 := have 1⁻¹ < a⁻¹ ↔ a < 1, from inv_lt_inv_iff, by rwa one_inv at this @[to_additive neg_lt] lemma inv_lt' : a⁻¹ < b ↔ b⁻¹ < a := have a⁻¹ < (b⁻¹)⁻¹ ↔ b⁻¹ < a, from inv_lt_inv_iff, by rwa inv_inv at this @[to_additive lt_neg] lemma lt_inv' : a < b⁻¹ ↔ b < a⁻¹ := have (a⁻¹)⁻¹ < b⁻¹ ↔ b < a⁻¹, from inv_lt_inv_iff, by rwa inv_inv at this @[to_additive] lemma le_inv_mul_iff_mul_le : b ≤ a⁻¹ * c ↔ a * b ≤ c := have a⁻¹ * (a * b) ≤ a⁻¹ * c ↔ a * b ≤ c, from mul_le_mul_iff_left _, by rwa inv_mul_cancel_left at this @[simp, to_additive] lemma inv_mul_le_iff_le_mul : b⁻¹ * a ≤ c ↔ a ≤ b * c := have b⁻¹ * a ≤ b⁻¹ * (b * c) ↔ a ≤ b * c, from mul_le_mul_iff_left _, by rwa inv_mul_cancel_left at this @[to_additive] lemma mul_inv_le_iff_le_mul : a * c⁻¹ ≤ b ↔ a ≤ b * c := by rw [mul_comm a, mul_comm b, inv_mul_le_iff_le_mul] @[simp, to_additive] lemma mul_inv_le_iff_le_mul' : a * b⁻¹ ≤ c ↔ a ≤ b * c := by rw [← inv_mul_le_iff_le_mul, mul_comm] @[to_additive] lemma inv_mul_le_iff_le_mul' : c⁻¹ * a ≤ b ↔ a ≤ b * c := by rw [inv_mul_le_iff_le_mul, mul_comm] @[simp, to_additive] lemma lt_inv_mul_iff_mul_lt : b < a⁻¹ * c ↔ a * b < c := have a⁻¹ * (a * b) < a⁻¹ * c ↔ a * b < c, from mul_lt_mul_iff_left _, by rwa inv_mul_cancel_left at this @[simp, to_additive] lemma inv_mul_lt_iff_lt_mul : b⁻¹ * a < c ↔ a < b * c := have b⁻¹ * a < b⁻¹ * (b * c) ↔ a < b * c, from mul_lt_mul_iff_left _, by rwa inv_mul_cancel_left at this @[to_additive] lemma inv_mul_lt_iff_lt_mul_right : c⁻¹ * a < b ↔ a < b * c := by rw [inv_mul_lt_iff_lt_mul, mul_comm] @[to_additive sub_le_sub_iff] lemma div_le_div_iff' (a b c d : α) : a * b⁻¹ ≤ c * d⁻¹ ↔ a * d ≤ c * b := begin split ; intro h, have := mul_le_mul_right'' (mul_le_mul_right'' h b) d, rwa [inv_mul_cancel_right, mul_assoc _ _ b, mul_comm _ b, ← mul_assoc, inv_mul_cancel_right] at this, have := mul_le_mul_right'' (mul_le_mul_right'' h d⁻¹) b⁻¹, rwa [mul_inv_cancel_right, _root_.mul_assoc, _root_.mul_comm d⁻¹ b⁻¹, ← mul_assoc, mul_inv_cancel_right] at this, end end ordered_comm_group section ordered_add_comm_group variables [ordered_add_comm_group α] {a b c d : α} lemma sub_nonneg_of_le (h : b ≤ a) : 0 ≤ a - b := begin have h := add_le_add_right h (-b), rwa add_right_neg at h end lemma le_of_sub_nonneg (h : 0 ≤ a - b) : b ≤ a := begin have h := add_le_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma sub_nonpos_of_le (h : a ≤ b) : a - b ≤ 0 := begin have h := add_le_add_right h (-b), rwa add_right_neg at h end lemma le_of_sub_nonpos (h : a - b ≤ 0) : a ≤ b := begin have h := add_le_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma sub_pos_of_lt (h : b < a) : 0 < a - b := begin have h := add_lt_add_right h (-b), rwa add_right_neg at h end lemma lt_of_sub_pos (h : 0 < a - b) : b < a := begin have h := add_lt_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma sub_neg_of_lt (h : a < b) : a - b < 0 := begin have h := add_lt_add_right h (-b), rwa add_right_neg at h end lemma lt_of_sub_neg (h : a - b < 0) : a < b := begin have h := add_lt_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma add_le_of_le_sub_left (h : b ≤ c - a) : a + b ≤ c := begin have h := add_le_add_left h a, rwa [← add_sub_assoc, add_comm a c, add_sub_cancel] at h end lemma le_sub_left_of_add_le (h : a + b ≤ c) : b ≤ c - a := begin have h := add_le_add_right h (-a), rwa [add_comm a b, add_neg_cancel_right] at h end lemma add_le_of_le_sub_right (h : a ≤ c - b) : a + b ≤ c := begin have h := add_le_add_right h b, rwa sub_add_cancel at h end lemma le_sub_right_of_add_le (h : a + b ≤ c) : a ≤ c - b := begin have h := add_le_add_right h (-b), rwa add_neg_cancel_right at h end lemma le_add_of_sub_left_le (h : a - b ≤ c) : a ≤ b + c := begin have h := add_le_add_right h b, rwa [sub_add_cancel, add_comm] at h end lemma sub_left_le_of_le_add (h : a ≤ b + c) : a - b ≤ c := begin have h := add_le_add_right h (-b), rwa [add_comm b c, add_neg_cancel_right] at h end lemma le_add_of_sub_right_le (h : a - c ≤ b) : a ≤ b + c := begin have h := add_le_add_right h c, rwa sub_add_cancel at h end lemma sub_right_le_of_le_add (h : a ≤ b + c) : a - c ≤ b := begin have h := add_le_add_right h (-c), rwa add_neg_cancel_right at h end lemma le_add_of_neg_le_sub_left (h : -a ≤ b - c) : c ≤ a + b := le_add_of_neg_add_le_left (add_le_of_le_sub_right h) lemma neg_le_sub_left_of_le_add (h : c ≤ a + b) : -a ≤ b - c := begin have h := le_neg_add_of_add_le (sub_left_le_of_le_add h), rwa add_comm at h end lemma le_add_of_neg_le_sub_right (h : -b ≤ a - c) : c ≤ a + b := le_add_of_sub_right_le (add_le_of_le_sub_left h) lemma neg_le_sub_right_of_le_add (h : c ≤ a + b) : -b ≤ a - c := le_sub_left_of_add_le (sub_right_le_of_le_add h) lemma sub_le_of_sub_le (h : a - b ≤ c) : a - c ≤ b := sub_left_le_of_le_add (le_add_of_sub_right_le h) lemma sub_le_sub_left (h : a ≤ b) (c : α) : c - b ≤ c - a := add_le_add_left (neg_le_neg h) c lemma sub_le_sub_right (h : a ≤ b) (c : α) : a - c ≤ b - c := add_le_add_right h (-c) lemma sub_le_sub (hab : a ≤ b) (hcd : c ≤ d) : a - d ≤ b - c := add_le_add hab (neg_le_neg hcd) lemma add_lt_of_lt_sub_left (h : b < c - a) : a + b < c := begin have h := add_lt_add_left h a, rwa [← add_sub_assoc, add_comm a c, add_sub_cancel] at h end lemma lt_sub_left_of_add_lt (h : a + b < c) : b < c - a := begin have h := add_lt_add_right h (-a), rwa [add_comm a b, add_neg_cancel_right] at h end lemma add_lt_of_lt_sub_right (h : a < c - b) : a + b < c := begin have h := add_lt_add_right h b, rwa sub_add_cancel at h end lemma lt_sub_right_of_add_lt (h : a + b < c) : a < c - b := begin have h := add_lt_add_right h (-b), rwa add_neg_cancel_right at h end lemma lt_add_of_sub_left_lt (h : a - b < c) : a < b + c := begin have h := add_lt_add_right h b, rwa [sub_add_cancel, add_comm] at h end lemma sub_left_lt_of_lt_add (h : a < b + c) : a - b < c := begin have h := add_lt_add_right h (-b), rwa [add_comm b c, add_neg_cancel_right] at h end lemma lt_add_of_sub_right_lt (h : a - c < b) : a < b + c := begin have h := add_lt_add_right h c, rwa sub_add_cancel at h end lemma sub_right_lt_of_lt_add (h : a < b + c) : a - c < b := begin have h := add_lt_add_right h (-c), rwa add_neg_cancel_right at h end lemma lt_add_of_neg_lt_sub_left (h : -a < b - c) : c < a + b := lt_add_of_neg_add_lt_left (add_lt_of_lt_sub_right h) lemma neg_lt_sub_left_of_lt_add (h : c < a + b) : -a < b - c := begin have h := lt_neg_add_of_add_lt (sub_left_lt_of_lt_add h), rwa add_comm at h end lemma lt_add_of_neg_lt_sub_right (h : -b < a - c) : c < a + b := lt_add_of_sub_right_lt (add_lt_of_lt_sub_left h) lemma neg_lt_sub_right_of_lt_add (h : c < a + b) : -b < a - c := lt_sub_left_of_add_lt (sub_right_lt_of_lt_add h) lemma sub_lt_of_sub_lt (h : a - b < c) : a - c < b := sub_left_lt_of_lt_add (lt_add_of_sub_right_lt h) lemma sub_lt_sub_left (h : a < b) (c : α) : c - b < c - a := add_lt_add_left (neg_lt_neg h) c lemma sub_lt_sub_right (h : a < b) (c : α) : a - c < b - c := add_lt_add_right h (-c) lemma sub_lt_sub (hab : a < b) (hcd : c < d) : a - d < b - c := add_lt_add hab (neg_lt_neg hcd) lemma sub_lt_sub_of_le_of_lt (hab : a ≤ b) (hcd : c < d) : a - d < b - c := add_lt_add_of_le_of_lt hab (neg_lt_neg hcd) lemma sub_lt_sub_of_lt_of_le (hab : a < b) (hcd : c ≤ d) : a - d < b - c := add_lt_add_of_lt_of_le hab (neg_le_neg hcd) lemma sub_le_self (a : α) {b : α} (h : 0 ≤ b) : a - b ≤ a := calc a - b = a + -b : rfl ... ≤ a + 0 : add_le_add_left (neg_nonpos_of_nonneg h) _ ... = a : by rw add_zero lemma sub_lt_self (a : α) {b : α} (h : 0 < b) : a - b < a := calc a - b = a + -b : rfl ... < a + 0 : add_lt_add_left (neg_neg_of_pos h) _ ... = a : by rw add_zero @[simp] lemma sub_le_sub_iff_left (a : α) {b c : α} : a - b ≤ a - c ↔ c ≤ b := (add_le_add_iff_left _).trans neg_le_neg_iff @[simp] lemma sub_le_sub_iff_right (c : α) : a - c ≤ b - c ↔ a ≤ b := add_le_add_iff_right _ @[simp] lemma sub_lt_sub_iff_left (a : α) {b c : α} : a - b < a - c ↔ c < b := (add_lt_add_iff_left _).trans neg_lt_neg_iff @[simp] lemma sub_lt_sub_iff_right (c : α) : a - c < b - c ↔ a < b := add_lt_add_iff_right _ @[simp] lemma sub_nonneg : 0 ≤ a - b ↔ b ≤ a := have a - a ≤ a - b ↔ b ≤ a, from sub_le_sub_iff_left a, by rwa sub_self at this @[simp] lemma sub_nonpos : a - b ≤ 0 ↔ a ≤ b := have a - b ≤ b - b ↔ a ≤ b, from sub_le_sub_iff_right b, by rwa sub_self at this @[simp] lemma sub_pos : 0 < a - b ↔ b < a := have a - a < a - b ↔ b < a, from sub_lt_sub_iff_left a, by rwa sub_self at this @[simp] lemma sub_lt_zero : a - b < 0 ↔ a < b := have a - b < b - b ↔ a < b, from sub_lt_sub_iff_right b, by rwa sub_self at this lemma le_sub_iff_add_le' : b ≤ c - a ↔ a + b ≤ c := by rw [sub_eq_add_neg, add_comm, le_neg_add_iff_add_le] lemma le_sub_iff_add_le : a ≤ c - b ↔ a + b ≤ c := by rw [le_sub_iff_add_le', add_comm] lemma sub_le_iff_le_add' : a - b ≤ c ↔ a ≤ b + c := by rw [sub_eq_add_neg, add_comm, neg_add_le_iff_le_add] lemma sub_le_iff_le_add : a - c ≤ b ↔ a ≤ b + c := by rw [sub_le_iff_le_add', add_comm] @[simp] lemma neg_le_sub_iff_le_add : -b ≤ a - c ↔ c ≤ a + b := le_sub_iff_add_le.trans neg_add_le_iff_le_add' lemma neg_le_sub_iff_le_add' : -a ≤ b - c ↔ c ≤ a + b := by rw [neg_le_sub_iff_le_add, add_comm] lemma sub_le : a - b ≤ c ↔ a - c ≤ b := sub_le_iff_le_add'.trans sub_le_iff_le_add.symm theorem le_sub : a ≤ b - c ↔ c ≤ b - a := le_sub_iff_add_le'.trans le_sub_iff_add_le.symm lemma lt_sub_iff_add_lt' : b < c - a ↔ a + b < c := by rw [sub_eq_add_neg, add_comm, lt_neg_add_iff_add_lt] lemma lt_sub_iff_add_lt : a < c - b ↔ a + b < c := by rw [lt_sub_iff_add_lt', add_comm] lemma sub_lt_iff_lt_add' : a - b < c ↔ a < b + c := by rw [sub_eq_add_neg, add_comm, neg_add_lt_iff_lt_add] lemma sub_lt_iff_lt_add : a - c < b ↔ a < b + c := by rw [sub_lt_iff_lt_add', add_comm] @[simp] lemma neg_lt_sub_iff_lt_add : -b < a - c ↔ c < a + b := lt_sub_iff_add_lt.trans neg_add_lt_iff_lt_add_right lemma neg_lt_sub_iff_lt_add' : -a < b - c ↔ c < a + b := by rw [neg_lt_sub_iff_lt_add, add_comm] lemma sub_lt : a - b < c ↔ a - c < b := sub_lt_iff_lt_add'.trans sub_lt_iff_lt_add.symm theorem lt_sub : a < b - c ↔ c < b - a := lt_sub_iff_add_lt'.trans lt_sub_iff_add_lt.symm lemma sub_le_self_iff (a : α) {b : α} : a - b ≤ a ↔ 0 ≤ b := sub_le_iff_le_add'.trans (le_add_iff_nonneg_left _) lemma sub_lt_self_iff (a : α) {b : α} : a - b < a ↔ 0 < b := sub_lt_iff_lt_add'.trans (lt_add_iff_pos_left _) end ordered_add_comm_group /- TODO: The `add_lt_add_left` field of `ordered_add_comm_group` is redundant, and it is no longer in core so we can remove it now. This alternative constructor is a workaround until someone fixes this. -/ /-- Alternative constructor for ordered commutative groups, that avoids the field `mul_lt_mul_left`. -/ @[to_additive "Alternative constructor for ordered commutative groups, that avoids the field `mul_lt_mul_left`."] def ordered_comm_group.mk' {α : Type u} [comm_group α] [partial_order α] (mul_le_mul_left : ∀ a b : α, a ≤ b → ∀ c : α, c * a ≤ c * b) : ordered_comm_group α := { mul_le_mul_left := mul_le_mul_left, ..(by apply_instance : comm_group α), ..(by apply_instance : partial_order α) } /-- A decidable linearly ordered cancellative additive commutative monoid is an additive commutative monoid with a decidable linear order in which addition is cancellative and strictly monotone. -/ @[protect_proj] class decidable_linear_ordered_cancel_add_comm_monoid (α : Type u) extends ordered_cancel_add_comm_monoid α, decidable_linear_order α section decidable_linear_ordered_cancel_add_comm_monoid variables [decidable_linear_ordered_cancel_add_comm_monoid α] lemma min_add_add_left (a b c : α) : min (a + b) (a + c) = a + min b c := eq.symm (eq_min (show a + min b c ≤ a + b, from add_le_add_left (min_le_left _ _) _) (show a + min b c ≤ a + c, from add_le_add_left (min_le_right _ _) _) (assume d, assume : d ≤ a + b, assume : d ≤ a + c, decidable.by_cases (assume : b ≤ c, by rwa [min_eq_left this]) (assume : ¬ b ≤ c, by rwa [min_eq_right (le_of_lt (lt_of_not_ge this))]))) lemma min_add_add_right (a b c : α) : min (a + c) (b + c) = min a b + c := begin rw [add_comm a c, add_comm b c, add_comm _ c], apply min_add_add_left end lemma max_add_add_left (a b c : α) : max (a + b) (a + c) = a + max b c := eq.symm (eq_max (add_le_add_left (le_max_left _ _) _) (add_le_add_left (le_max_right _ _) _) (assume d, assume : a + b ≤ d, assume : a + c ≤ d, decidable.by_cases (assume : b ≤ c, by rwa [max_eq_right this]) (assume : ¬ b ≤ c, by rwa [max_eq_left (le_of_lt (lt_of_not_ge this))]))) lemma max_add_add_right (a b c : α) : max (a + c) (b + c) = max a b + c := begin rw [add_comm a c, add_comm b c, add_comm _ c], apply max_add_add_left end end decidable_linear_ordered_cancel_add_comm_monoid /-- A decidable linearly ordered additive commutative group is an additive commutative group with a decidable linear order in which addition is strictly monotone. -/ @[protect_proj] class decidable_linear_ordered_add_comm_group (α : Type u) extends add_comm_group α, decidable_linear_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) instance decidable_linear_ordered_comm_group.to_ordered_add_comm_group (α : Type u) [s : decidable_linear_ordered_add_comm_group α] : ordered_add_comm_group α := { add := s.add, ..s } section decidable_linear_ordered_add_comm_group variables [decidable_linear_ordered_add_comm_group α] @[priority 100] -- see Note [lower instance priority] instance decidable_linear_ordered_add_comm_group.to_decidable_linear_ordered_cancel_add_comm_monoid : decidable_linear_ordered_cancel_add_comm_monoid α := { le_of_add_le_add_left := λ x y z, le_of_add_le_add_left, add_left_cancel := λ x y z, add_left_cancel, add_right_cancel := λ x y z, add_right_cancel, ..‹decidable_linear_ordered_add_comm_group α› } lemma decidable_linear_ordered_add_comm_group.add_lt_add_left (a b : α) (h : a < b) (c : α) : c + a < c + b := ordered_add_comm_group.add_lt_add_left a b h c lemma max_neg_neg (a b : α) : max (-a) (-b) = - min a b := eq.symm (eq_max (show -a ≤ -(min a b), from neg_le_neg $ min_le_left a b) (show -b ≤ -(min a b), from neg_le_neg $ min_le_right a b) (assume d, assume H₁ : -a ≤ d, assume H₂ : -b ≤ d, have H : -d ≤ min a b, from le_min (neg_le_of_neg_le H₁) (neg_le_of_neg_le H₂), show -(min a b) ≤ d, from neg_le_of_neg_le H)) lemma min_eq_neg_max_neg_neg (a b : α) : min a b = - max (-a) (-b) := by rw [max_neg_neg, neg_neg] lemma min_neg_neg (a b : α) : min (-a) (-b) = - max a b := by rw [min_eq_neg_max_neg_neg, neg_neg, neg_neg] lemma max_eq_neg_min_neg_neg (a b : α) : max a b = - min (-a) (-b) := by rw [min_neg_neg, neg_neg] /-- `abs a` is the absolute value of `a`. -/ def abs (a : α) : α := max a (-a) lemma abs_of_nonneg {a : α} (h : 0 ≤ a) : abs a = a := have h' : -a ≤ a, from le_trans (neg_nonpos_of_nonneg h) h, max_eq_left h' lemma abs_of_pos {a : α} (h : 0 < a) : abs a = a := abs_of_nonneg (le_of_lt h) lemma abs_of_nonpos {a : α} (h : a ≤ 0) : abs a = -a := have h' : a ≤ -a, from le_trans h (neg_nonneg_of_nonpos h), max_eq_right h' lemma abs_of_neg {a : α} (h : a < 0) : abs a = -a := abs_of_nonpos (le_of_lt h) lemma abs_zero : abs 0 = (0:α) := abs_of_nonneg (le_refl _) lemma abs_neg (a : α) : abs (-a) = abs a := begin unfold abs, rw [max_comm, neg_neg] end lemma abs_pos_of_pos {a : α} (h : 0 < a) : 0 < abs a := by rwa (abs_of_pos h) lemma abs_pos_of_neg {a : α} (h : a < 0) : 0 < abs a := abs_neg a ▸ abs_pos_of_pos (neg_pos_of_neg h) lemma abs_sub (a b : α) : abs (a - b) = abs (b - a) := by rw [← neg_sub, abs_neg] lemma ne_zero_of_abs_ne_zero {a : α} (h : abs a ≠ 0) : a ≠ 0 := assume ha, h (eq.symm ha ▸ abs_zero) /- these assume a linear order -/ lemma eq_zero_of_neg_eq {a : α} (h : -a = a) : a = 0 := match lt_trichotomy a 0 with | or.inl h₁ := have 0 < a, from h ▸ neg_pos_of_neg h₁, absurd h₁ (lt_asymm this) | or.inr (or.inl h₁) := h₁ | or.inr (or.inr h₁) := have a < 0, from h ▸ neg_neg_of_pos h₁, absurd h₁ (lt_asymm this) end lemma abs_nonneg (a : α) : 0 ≤ abs a := or.elim (le_total 0 a) (assume h : 0 ≤ a, by rwa (abs_of_nonneg h)) (assume h : a ≤ 0, calc 0 ≤ -a : neg_nonneg_of_nonpos h ... = abs a : eq.symm (abs_of_nonpos h)) lemma abs_abs (a : α) : abs (abs a) = abs a := abs_of_nonneg $ abs_nonneg a lemma le_abs_self (a : α) : a ≤ abs a := or.elim (le_total 0 a) (assume h : 0 ≤ a, begin rw [abs_of_nonneg h] end) (assume h : a ≤ 0, le_trans h $ abs_nonneg a) lemma neg_le_abs_self (a : α) : -a ≤ abs a := abs_neg a ▸ le_abs_self (-a) lemma eq_zero_of_abs_eq_zero {a : α} (h : abs a = 0) : a = 0 := have h₁ : a ≤ 0, from h ▸ le_abs_self a, have h₂ : -a ≤ 0, from h ▸ abs_neg a ▸ le_abs_self (-a), le_antisymm h₁ (nonneg_of_neg_nonpos h₂) lemma eq_of_abs_sub_eq_zero {a b : α} (h : abs (a - b) = 0) : a = b := have a - b = 0, from eq_zero_of_abs_eq_zero h, show a = b, from eq_of_sub_eq_zero this lemma abs_pos_of_ne_zero {a : α} (h : a ≠ 0) : 0 < abs a := or.elim (lt_or_gt_of_ne h) abs_pos_of_neg abs_pos_of_pos lemma abs_by_cases (P : α → Prop) {a : α} (h1 : P a) (h2 : P (-a)) : P (abs a) := or.elim (le_total 0 a) (assume h : 0 ≤ a, eq.symm (abs_of_nonneg h) ▸ h1) (assume h : a ≤ 0, eq.symm (abs_of_nonpos h) ▸ h2) lemma abs_le_of_le_of_neg_le {a b : α} (h1 : a ≤ b) (h2 : -a ≤ b) : abs a ≤ b := abs_by_cases (λ x : α, x ≤ b) h1 h2 lemma abs_lt_of_lt_of_neg_lt {a b : α} (h1 : a < b) (h2 : -a < b) : abs a < b := abs_by_cases (λ x : α, x < b) h1 h2 private lemma aux1 {a b : α} (h1 : 0 ≤ a + b) (h2 : 0 ≤ a) : abs (a + b) ≤ abs a + abs b := decidable.by_cases (assume h3 : 0 ≤ b, calc abs (a + b) ≤ abs (a + b) : by apply le_refl ... = a + b : by rw (abs_of_nonneg h1) ... = abs a + b : by rw (abs_of_nonneg h2) ... = abs a + abs b : by rw (abs_of_nonneg h3)) (assume h3 : ¬ 0 ≤ b, have h4 : b ≤ 0, from le_of_lt (lt_of_not_ge h3), calc abs (a + b) = a + b : by rw (abs_of_nonneg h1) ... = abs a + b : by rw (abs_of_nonneg h2) ... ≤ abs a + 0 : add_le_add_left h4 _ ... ≤ abs a + -b : add_le_add_left (neg_nonneg_of_nonpos h4) _ ... = abs a + abs b : by rw (abs_of_nonpos h4)) private lemma aux2 {a b : α} (h1 : 0 ≤ a + b) : abs (a + b) ≤ abs a + abs b := or.elim (le_total b 0) (assume h2 : b ≤ 0, have h3 : ¬ a < 0, from assume h4 : a < 0, have h5 : a + b < 0, begin have aux := add_lt_add_of_lt_of_le h4 h2, rwa [add_zero] at aux end, not_lt_of_ge h1 h5, aux1 h1 (le_of_not_gt h3)) (assume h2 : 0 ≤ b, begin have h3 : abs (b + a) ≤ abs b + abs a, begin rw add_comm at h1, exact aux1 h1 h2 end, rw [add_comm, add_comm (abs a)], exact h3 end) lemma abs_add_le_abs_add_abs (a b : α) : abs (a + b) ≤ abs a + abs b := or.elim (le_total 0 (a + b)) (assume h2 : 0 ≤ a + b, aux2 h2) (assume h2 : a + b ≤ 0, have h3 : -a + -b = -(a + b), by rw neg_add, have h4 : 0 ≤ -(a + b), from neg_nonneg_of_nonpos h2, have h5 : 0 ≤ -a + -b, begin rw [← h3] at h4, exact h4 end, calc abs (a + b) = abs (-a + -b) : by rw [← abs_neg, neg_add] ... ≤ abs (-a) + abs (-b) : aux2 h5 ... = abs a + abs b : by rw [abs_neg, abs_neg]) lemma abs_sub_abs_le_abs_sub (a b : α) : abs a - abs b ≤ abs (a - b) := have h1 : abs a - abs b + abs b ≤ abs (a - b) + abs b, from calc abs a - abs b + abs b = abs a : by rw sub_add_cancel ... = abs (a - b + b) : by rw sub_add_cancel ... ≤ abs (a - b) + abs b : by apply abs_add_le_abs_add_abs, le_of_add_le_add_right h1 lemma abs_sub_le (a b c : α) : abs (a - c) ≤ abs (a - b) + abs (b - c) := calc abs (a - c) = abs (a - b + (b - c)) : by rw [sub_eq_add_neg, sub_eq_add_neg, sub_eq_add_neg, add_assoc, neg_add_cancel_left] ... ≤ abs (a - b) + abs (b - c) : by apply abs_add_le_abs_add_abs lemma abs_add_three (a b c : α) : abs (a + b + c) ≤ abs a + abs b + abs c := begin apply le_trans, apply abs_add_le_abs_add_abs, apply le_trans, apply add_le_add_right, apply abs_add_le_abs_add_abs, apply le_refl end lemma dist_bdd_within_interval {a b lb ub : α} (hal : lb ≤ a) (hau : a ≤ ub) (hbl : lb ≤ b) (hbu : b ≤ ub) : abs (a - b) ≤ ub - lb := begin cases (decidable.em (b ≤ a)) with hba hba, rw (abs_of_nonneg (sub_nonneg_of_le hba)), apply sub_le_sub, apply hau, apply hbl, rw [abs_of_neg (sub_neg_of_lt (lt_of_not_ge hba)), neg_sub], apply sub_le_sub, apply hbu, apply hal end lemma decidable_linear_ordered_add_comm_group.eq_of_abs_sub_nonpos {a b : α} (h : abs (a - b) ≤ 0) : a = b := eq_of_abs_sub_eq_zero (le_antisymm h (abs_nonneg (a - b))) end decidable_linear_ordered_add_comm_group set_option old_structure_cmd true section prio set_option default_priority 100 -- see Note [default priority] /-- This is not so much a new structure as a construction mechanism for ordered groups, by specifying only the "positive cone" of the group. -/ class nonneg_add_comm_group (α : Type*) extends add_comm_group α := (nonneg : α → Prop) (pos : α → Prop := λ a, nonneg a ∧ ¬ nonneg (neg a)) (pos_iff : ∀ a, pos a ↔ nonneg a ∧ ¬ nonneg (-a) . order_laws_tac) (zero_nonneg : nonneg 0) (add_nonneg : ∀ {a b}, nonneg a → nonneg b → nonneg (a + b)) (nonneg_antisymm : ∀ {a}, nonneg a → nonneg (-a) → a = 0) end prio namespace nonneg_add_comm_group variable [s : nonneg_add_comm_group α] include s @[reducible, priority 100] -- see Note [lower instance priority] instance to_ordered_add_comm_group : ordered_add_comm_group α := { le := λ a b, nonneg (b - a), lt := λ a b, pos (b - a), lt_iff_le_not_le := λ a b, by simp; rw [pos_iff]; simp, le_refl := λ a, by simp [zero_nonneg], le_trans := λ a b c nab nbc, by simp [-sub_eq_add_neg]; rw ← sub_add_sub_cancel; exact add_nonneg nbc nab, le_antisymm := λ a b nab nba, eq_of_sub_eq_zero $ nonneg_antisymm nba (by rw neg_sub; exact nab), add_le_add_left := λ a b nab c, by simpa [(≤), preorder.le] using nab, ..s } theorem nonneg_def {a : α} : nonneg a ↔ 0 ≤ a := show _ ↔ nonneg _, by simp theorem pos_def {a : α} : pos a ↔ 0 < a := show _ ↔ pos _, by simp theorem not_zero_pos : ¬ pos (0 : α) := mt pos_def.1 (lt_irrefl _) theorem zero_lt_iff_nonneg_nonneg {a : α} : 0 < a ↔ nonneg a ∧ ¬ nonneg (-a) := pos_def.symm.trans (pos_iff _) theorem nonneg_total_iff : (∀ a : α, nonneg a ∨ nonneg (-a)) ↔ (∀ a b : α, a ≤ b ∨ b ≤ a) := ⟨λ h a b, by have := h (b - a); rwa [neg_sub] at this, λ h a, by rw [nonneg_def, nonneg_def, neg_nonneg]; apply h⟩ /-- A `nonneg_add_comm_group` is a `decidable_linear_ordered_add_comm_group` if `nonneg` is total and decidable. -/ def to_decidable_linear_ordered_add_comm_group [decidable_pred (@nonneg α _)] (nonneg_total : ∀ a : α, nonneg a ∨ nonneg (-a)) : decidable_linear_ordered_add_comm_group α := { le := (≤), lt := (<), lt_iff_le_not_le := @lt_iff_le_not_le _ _, le_refl := @le_refl _ _, le_trans := @le_trans _ _, le_antisymm := @le_antisymm _ _, le_total := nonneg_total_iff.1 nonneg_total, decidable_le := by apply_instance, decidable_lt := by apply_instance, ..@nonneg_add_comm_group.to_ordered_add_comm_group _ s } end nonneg_add_comm_group namespace order_dual instance [ordered_add_comm_monoid α] : ordered_add_comm_monoid (order_dual α) := { add_le_add_left := λ a b h c, @add_le_add_left' α _ b a c h, lt_of_add_lt_add_left := λ a b c h, @lt_of_add_lt_add_left' α _ a c b h, ..order_dual.partial_order α, ..show add_comm_monoid α, by apply_instance } instance [ordered_cancel_add_comm_monoid α] : ordered_cancel_add_comm_monoid (order_dual α) := { le_of_add_le_add_left := λ a b c : α, le_of_add_le_add_left, add_left_cancel := @add_left_cancel α _, add_right_cancel := @add_right_cancel α _, ..order_dual.ordered_add_comm_monoid } instance [ordered_add_comm_group α] : ordered_add_comm_group (order_dual α) := { add_left_neg := λ a : α, add_left_neg a, ..order_dual.ordered_add_comm_monoid, ..show add_comm_group α, by apply_instance } end order_dual
df8ef690ac354a8846af956616249e59c6cd0215
bb31430994044506fa42fd667e2d556327e18dfe
/src/group_theory/perm/cycle/basic.lean
caa1530b9dcef9ee9d60f576dee0fb901989d2a8
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
68,347
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Yaël Dillies -/ import data.finset.noncomm_prod import data.fintype.perm import data.int.modeq import group_theory.perm.sign import logic.equiv.fintype /-! # Cyclic permutations This file develops the theory of cycles in permutations. ## Main definitions In the following, `f : equiv.perm β`. * `equiv.perm.same_cycle`: `f.same_cycle x y` when `x` and `y` are in the same cycle of `f`. * `equiv.perm.is_cycle`: `f` is a cycle if any two nonfixed points of `f` are related by repeated applications of `f`, and `f` is not the identity. * `equiv.perm.is_cycle_on`: `f` is a cycle on a set `s` when any two points of `s` are related by repeated applications of `f`. The following two definitions require that `β` is a `fintype`: * `equiv.perm.cycle_of`: `f.cycle_of x` is the cycle of `f` that `x` belongs to. * `equiv.perm.cycle_factors`: `f.cycle_factors` is a list of disjoint cyclic permutations that multiply to `f`. ## Main results * This file contains several closure results: - `closure_is_cycle` : The symmetric group is generated by cycles - `closure_cycle_adjacent_swap` : The symmetric group is generated by a cycle and an adjacent transposition - `closure_cycle_coprime_swap` : The symmetric group is generated by a cycle and a coprime transposition - `closure_prime_cycle_swap` : The symmetric group is generated by a prime cycle and a transposition ## Notes `equiv.perm.is_cycle` and `equiv.perm.is_cycle_on` are different in three ways: * `is_cycle` is about the entire type while `is_cycle_on` is restricted to a set. * `is_cycle` forbids the identity while `is_cycle_on` allows it (if `s` is a subsingleton). * `is_cycle_on` forbids fixed points on `s` (if `s` is nontrivial), while `is_cycle` allows them. -/ open equiv function finset variables {α β : Type*} namespace equiv.perm /-! ### `same_cycle` -/ section same_cycle variables {f g : perm α} {p : α → Prop} {x y z : α} /-- The equivalence relation indicating that two points are in the same cycle of a permutation. -/ def same_cycle (f : perm α) (x y : α) : Prop := ∃ i : ℤ, (f ^ i) x = y @[refl] lemma same_cycle.refl (f : perm α) (x : α) : same_cycle f x x := ⟨0, rfl⟩ lemma same_cycle.rfl : same_cycle f x x := same_cycle.refl _ _ protected lemma _root_.eq.same_cycle (h : x = y) (f : perm α) : f.same_cycle x y := by rw h @[symm] lemma same_cycle.symm : same_cycle f x y → same_cycle f y x := λ ⟨i, hi⟩, ⟨-i, by rw [zpow_neg, ← hi, inv_apply_self]⟩ lemma same_cycle_comm : same_cycle f x y ↔ same_cycle f y x := ⟨same_cycle.symm, same_cycle.symm⟩ @[trans] lemma same_cycle.trans : same_cycle f x y → same_cycle f y z → same_cycle f x z := λ ⟨i, hi⟩ ⟨j, hj⟩, ⟨j + i, by rw [zpow_add, mul_apply, hi, hj]⟩ @[simp] lemma same_cycle_one : same_cycle 1 x y ↔ x = y := by simp [same_cycle] @[simp] lemma same_cycle_inv : same_cycle f⁻¹ x y ↔ same_cycle f x y := (equiv.neg _).exists_congr_left.trans $ by simp [same_cycle] alias same_cycle_inv ↔ same_cycle.of_inv same_cycle.inv @[simp] lemma same_cycle_conj : same_cycle (g * f * g⁻¹) x y ↔ same_cycle f (g⁻¹ x) (g⁻¹ y) := exists_congr $ λ i, by simp [conj_zpow, eq_inv_iff_eq] lemma same_cycle.conj : same_cycle f x y → same_cycle (g * f * g⁻¹) (g x) (g y) := by simp [same_cycle_conj] lemma same_cycle.apply_eq_self_iff : same_cycle f x y → (f x = x ↔ f y = y) := λ ⟨i, hi⟩, by rw [← hi, ← mul_apply, ← zpow_one_add, add_comm, zpow_add_one, mul_apply, (f ^ i).injective.eq_iff] lemma same_cycle.eq_of_left (h : same_cycle f x y) (hx : is_fixed_pt f x) : x = y := let ⟨n, hn⟩ := h in (hx.perm_zpow _).eq.symm.trans hn lemma same_cycle.eq_of_right (h : same_cycle f x y) (hy : is_fixed_pt f y) : x = y := h.eq_of_left $ h.apply_eq_self_iff.2 hy @[simp] lemma same_cycle_apply_left : same_cycle f (f x) y ↔ same_cycle f x y := (equiv.add_right 1).exists_congr_left.trans $ by simp [zpow_sub, same_cycle] @[simp] lemma same_cycle_apply_right : same_cycle f x (f y) ↔ same_cycle f x y := by rw [same_cycle_comm, same_cycle_apply_left, same_cycle_comm] @[simp] lemma same_cycle_inv_apply_left : same_cycle f (f⁻¹ x) y ↔ same_cycle f x y := by rw [←same_cycle_apply_left, apply_inv_self] @[simp] lemma same_cycle_inv_apply_right : same_cycle f x (f⁻¹ y) ↔ same_cycle f x y := by rw [←same_cycle_apply_right, apply_inv_self] @[simp] lemma same_cycle_zpow_left {n : ℤ} : same_cycle f ((f ^ n) x) y ↔ same_cycle f x y := (equiv.add_right (n : ℤ)).exists_congr_left.trans $ by simp [same_cycle, zpow_add] @[simp] lemma same_cycle_zpow_right {n : ℤ} : same_cycle f x ((f ^ n) y) ↔ same_cycle f x y := by rw [same_cycle_comm, same_cycle_zpow_left, same_cycle_comm] @[simp] lemma same_cycle_pow_left {n : ℕ} : same_cycle f ((f ^ n) x) y ↔ same_cycle f x y := by rw [←zpow_coe_nat, same_cycle_zpow_left] @[simp] lemma same_cycle_pow_right {n : ℕ} : same_cycle f x ((f ^ n) y) ↔ same_cycle f x y := by rw [←zpow_coe_nat, same_cycle_zpow_right] alias same_cycle_apply_left ↔ same_cycle.of_apply_left same_cycle.apply_left alias same_cycle_apply_right ↔ same_cycle.of_apply_right same_cycle.apply_right alias same_cycle_inv_apply_left ↔ same_cycle.of_inv_apply_left same_cycle.inv_apply_left alias same_cycle_inv_apply_right ↔ same_cycle.of_inv_apply_right same_cycle.inv_apply_right alias same_cycle_pow_left ↔ same_cycle.of_pow_left same_cycle.pow_left alias same_cycle_pow_right ↔ same_cycle.of_pow_right same_cycle.pow_right alias same_cycle_zpow_left ↔ same_cycle.of_zpow_left same_cycle.zpow_left alias same_cycle_zpow_right ↔ same_cycle.of_zpow_right same_cycle.zpow_right lemma same_cycle.of_pow {n : ℕ} : same_cycle (f ^ n) x y → same_cycle f x y := λ ⟨m, h⟩, ⟨n * m, by simp [zpow_mul, h]⟩ lemma same_cycle.of_zpow {n : ℤ} : same_cycle (f ^ n) x y → same_cycle f x y := λ ⟨m, h⟩, ⟨n * m, by simp [zpow_mul, h]⟩ @[simp] lemma same_cycle_subtype_perm {h} {x y : {x // p x}} : (f.subtype_perm h).same_cycle x y ↔ f.same_cycle x y := exists_congr $ λ n, by simp [subtype.ext_iff] alias same_cycle_subtype_perm ↔ _ same_cycle.subtype_perm @[simp] lemma same_cycle_extend_domain {p : β → Prop} [decidable_pred p] {f : α ≃ subtype p} : same_cycle (g.extend_domain f) (f x) (f y) ↔ g.same_cycle x y := exists_congr $ λ n, by rw [←extend_domain_zpow, extend_domain_apply_image, subtype.coe_inj, f.injective.eq_iff] alias same_cycle_extend_domain ↔ _ same_cycle.extend_domain lemma same_cycle.exists_pow_eq' [finite α] : same_cycle f x y → ∃ i < order_of f, (f ^ i) x = y := begin classical, rintro ⟨k, rfl⟩, use (k % order_of f).nat_abs, have h₀ := int.coe_nat_pos.mpr (order_of_pos f), have h₁ := int.mod_nonneg k h₀.ne', rw [←zpow_coe_nat, int.nat_abs_of_nonneg h₁, ←zpow_eq_mod_order_of], refine ⟨_, rfl⟩, rw [←int.coe_nat_lt, int.nat_abs_of_nonneg h₁], exact int.mod_lt_of_pos _ h₀, end lemma same_cycle.exists_pow_eq'' [finite α] (h : same_cycle f x y) : ∃ (i : ℕ) (hpos : 0 < i) (h : i ≤ order_of f), (f ^ i) x = y := begin classical, obtain ⟨_ | i, hi, rfl⟩ := h.exists_pow_eq', { refine ⟨order_of f, order_of_pos f, le_rfl, _⟩, rw [pow_order_of_eq_one, pow_zero] }, { exact ⟨i.succ, i.zero_lt_succ, hi.le, rfl⟩ } end instance [fintype α] [decidable_eq α] (f : perm α) : decidable_rel (same_cycle f) := λ x y, decidable_of_iff (∃ n ∈ list.range (fintype.card (perm α)), (f ^ n) x = y) ⟨λ ⟨n, _, hn⟩, ⟨n, hn⟩, λ ⟨i, hi⟩, ⟨(i % order_of f).nat_abs, list.mem_range.2 (int.coe_nat_lt.1 $ by { rw int.nat_abs_of_nonneg (int.mod_nonneg _ $ int.coe_nat_ne_zero.2 (order_of_pos _).ne'), { refine (int.mod_lt _ $ int.coe_nat_ne_zero_iff_pos.2 $ order_of_pos _).trans_le _, simp [order_of_le_card_univ] }, apply_instance }), by { rw [← zpow_coe_nat, int.nat_abs_of_nonneg (int.mod_nonneg _ $ int.coe_nat_ne_zero_iff_pos.2 $ order_of_pos _), ← zpow_eq_mod_order_of, hi], apply_instance }⟩⟩ end same_cycle /-! ### `is_cycle` -/ section is_cycle variables {f g : perm α} {x y : α} /-- A cycle is a non identity permutation where any two nonfixed points of the permutation are related by repeated application of the permutation. -/ def is_cycle (f : perm α) : Prop := ∃ x, f x ≠ x ∧ ∀ ⦃y⦄, f y ≠ y → same_cycle f x y lemma is_cycle.ne_one (h : is_cycle f) : f ≠ 1 := λ hf, by simpa [hf, is_cycle] using h @[simp] lemma not_is_cycle_one : ¬ (1 : perm α).is_cycle := λ H, H.ne_one rfl protected lemma is_cycle.same_cycle (hf : is_cycle f) (hx : f x ≠ x) (hy : f y ≠ y) : same_cycle f x y := let ⟨g, hg⟩ := hf in let ⟨a, ha⟩ := hg.2 hx in let ⟨b, hb⟩ := hg.2 hy in ⟨b - a, by rw [←ha, ←mul_apply, ←zpow_add, sub_add_cancel, hb]⟩ lemma is_cycle.exists_zpow_eq : is_cycle f → f x ≠ x → f y ≠ y → ∃ i : ℤ, (f ^ i) x = y := is_cycle.same_cycle lemma is_cycle.inv (hf : is_cycle f) : is_cycle f⁻¹ := hf.imp $ λ x ⟨hx, h⟩, ⟨inv_eq_iff_eq.not.2 hx.symm, λ y hy, (h $ inv_eq_iff_eq.not.2 hy.symm).inv⟩ @[simp] lemma is_cycle_inv : is_cycle f⁻¹ ↔ is_cycle f := ⟨λ h, by { convert h.inv, rw inv_inv }, is_cycle.inv⟩ lemma is_cycle.conj : is_cycle f → is_cycle (g * f * g⁻¹) := begin rintro ⟨x, hx, h⟩, refine ⟨g x, by simp [coe_mul, inv_apply_self, hx], λ y hy, _⟩, rw ←apply_inv_self g y, exact (h $ eq_inv_iff_eq.not.2 hy).conj, end protected lemma is_cycle.extend_domain {p : β → Prop} [decidable_pred p] (f : α ≃ subtype p) : is_cycle g → is_cycle (g.extend_domain f) := begin rintro ⟨a, ha, ha'⟩, refine ⟨f a, _, λ b hb, _⟩, { rw extend_domain_apply_image, exact subtype.coe_injective.ne (f.injective.ne ha) }, have h : b = f (f.symm ⟨b, of_not_not $ hb ∘ extend_domain_apply_not_subtype _ _⟩), { rw [apply_symm_apply, subtype.coe_mk] }, rw h at ⊢ hb, simp only [extend_domain_apply_image, subtype.coe_injective.ne_iff, f.injective.ne_iff] at hb, exact (ha' hb).extend_domain, end lemma is_cycle_iff_same_cycle (hx : f x ≠ x) : is_cycle f ↔ ∀ {y}, same_cycle f x y ↔ f y ≠ y := ⟨λ hf y, ⟨λ ⟨i, hi⟩ hy, hx $ by { rw [← zpow_apply_eq_self_of_apply_eq_self hy i, (f ^ i).injective.eq_iff] at hi, rw [hi, hy] }, hf.exists_zpow_eq hx⟩, λ h, ⟨x, hx, λ y hy, h.2 hy⟩⟩ section finite variables [finite α] lemma is_cycle.exists_pow_eq (hf : is_cycle f) (hx : f x ≠ x) (hy : f y ≠ y) : ∃ i : ℕ, (f ^ i) x = y := let ⟨n, hn⟩ := hf.exists_zpow_eq hx hy in by classical; exact ⟨(n % order_of f).to_nat, by { have := n.mod_nonneg (int.coe_nat_ne_zero.mpr (ne_of_gt (order_of_pos f))), rwa [← zpow_coe_nat, int.to_nat_of_nonneg this, ← zpow_eq_mod_order_of] }⟩ end finite variables [decidable_eq α] lemma is_cycle_swap (hxy : x ≠ y) : is_cycle (swap x y) := ⟨y, by rwa swap_apply_right, λ a (ha : ite (a = x) y (ite (a = y) x a) ≠ a), if hya : y = a then ⟨0, hya⟩ else ⟨1, by { rw [zpow_one, swap_apply_def], split_ifs at *; cc }⟩⟩ protected lemma is_swap.is_cycle : is_swap f → is_cycle f := by { rintro ⟨x, y, hxy, rfl⟩, exact is_cycle_swap hxy } variables [fintype α] lemma is_cycle.two_le_card_support (h : is_cycle f) : 2 ≤ f.support.card := two_le_card_support_of_ne_one h.ne_one lemma is_cycle.exists_pow_eq_one [finite β] {f : perm β} (hf : is_cycle f) : ∃ (k : ℕ) (hk : 1 < k), f ^ k = 1 := begin classical, have : is_of_fin_order f := exists_pow_eq_one f, rw is_of_fin_order_iff_pow_eq_one at this, obtain ⟨x, hx, hx'⟩ := hf, obtain ⟨_ | _ | k, hk, hk'⟩ := this, { exact absurd hk (lt_asymm hk) }, { rw pow_one at hk', simpa [hk'] using hx }, { exact ⟨k + 2, by simp, hk'⟩ } end /-- The subgroup generated by a cycle is in bijection with its support -/ noncomputable def is_cycle.zpowers_equiv_support {σ : perm α} (hσ : is_cycle σ) : (↑(subgroup.zpowers σ) : set (perm α)) ≃ (↑(σ.support) : set α) := equiv.of_bijective (λ τ, ⟨τ (classical.some hσ), begin obtain ⟨τ, n, rfl⟩ := τ, rw [finset.mem_coe, coe_fn_coe_base', subtype.coe_mk, zpow_apply_mem_support, mem_support], exact (classical.some_spec hσ).1, end⟩) begin split, { rintros ⟨a, m, rfl⟩ ⟨b, n, rfl⟩ h, ext y, by_cases hy : σ y = y, { simp_rw [subtype.coe_mk, zpow_apply_eq_self_of_apply_eq_self hy] }, { obtain ⟨i, rfl⟩ := (classical.some_spec hσ).2 hy, rw [subtype.coe_mk, subtype.coe_mk, zpow_apply_comm σ m i, zpow_apply_comm σ n i], exact congr_arg _ (subtype.ext_iff.mp h) } }, by { rintros ⟨y, hy⟩, rw [finset.mem_coe, mem_support] at hy, obtain ⟨n, rfl⟩ := (classical.some_spec hσ).2 hy, exact ⟨⟨σ ^ n, n, rfl⟩, rfl⟩ }, end @[simp] lemma is_cycle.zpowers_equiv_support_apply {σ : perm α} (hσ : is_cycle σ) {n : ℕ} : hσ.zpowers_equiv_support ⟨σ ^ n, n, rfl⟩ = ⟨(σ ^ n) (classical.some hσ), pow_apply_mem_support.2 (mem_support.2 (classical.some_spec hσ).1)⟩ := rfl @[simp] lemma is_cycle.zpowers_equiv_support_symm_apply {σ : perm α} (hσ : is_cycle σ) (n : ℕ) : hσ.zpowers_equiv_support.symm ⟨(σ ^ n) (classical.some hσ), pow_apply_mem_support.2 (mem_support.2 (classical.some_spec hσ).1)⟩ = ⟨σ ^ n, n, rfl⟩ := (equiv.symm_apply_eq _).2 hσ.zpowers_equiv_support_apply protected lemma is_cycle.order_of (hf : is_cycle f) : order_of f = f.support.card := begin rw [order_eq_card_zpowers, ←fintype.card_coe], convert fintype.card_congr (is_cycle.zpowers_equiv_support hf), end lemma is_cycle_swap_mul_aux₁ {α : Type*} [decidable_eq α] : ∀ (n : ℕ) {b x : α} {f : perm α} (hb : (swap x (f x) * f) b ≠ b) (h : (f ^ n) (f x) = b), ∃ i : ℤ, ((swap x (f x) * f) ^ i) (f x) = b | 0 := λ b x f hb h, ⟨0, h⟩ | (n+1 : ℕ) := λ b x f hb h, if hfbx : f x = b then ⟨0, hfbx⟩ else have f b ≠ b ∧ b ≠ x, from ne_and_ne_of_swap_mul_apply_ne_self hb, have hb' : (swap x (f x) * f) (f⁻¹ b) ≠ f⁻¹ b, by { rw [mul_apply, apply_inv_self, swap_apply_of_ne_of_ne this.2 (ne.symm hfbx), ne.def, ← f.injective.eq_iff, apply_inv_self], exact this.1 }, let ⟨i, hi⟩ := is_cycle_swap_mul_aux₁ n hb' (f.injective $ by { rw [apply_inv_self], rwa [pow_succ, mul_apply] at h }) in ⟨i + 1, by rw [add_comm, zpow_add, mul_apply, hi, zpow_one, mul_apply, apply_inv_self, swap_apply_of_ne_of_ne (ne_and_ne_of_swap_mul_apply_ne_self hb).2 (ne.symm hfbx)]⟩ lemma is_cycle_swap_mul_aux₂ {α : Type*} [decidable_eq α] : ∀ (n : ℤ) {b x : α} {f : perm α} (hb : (swap x (f x) * f) b ≠ b) (h : (f ^ n) (f x) = b), ∃ i : ℤ, ((swap x (f x) * f) ^ i) (f x) = b | (n : ℕ) := λ b x f, is_cycle_swap_mul_aux₁ n | -[1+ n] := λ b x f hb h, if hfbx' : f x = b then ⟨0, hfbx'⟩ else have f b ≠ b ∧ b ≠ x := ne_and_ne_of_swap_mul_apply_ne_self hb, have hb : (swap x (f⁻¹ x) * f⁻¹) (f⁻¹ b) ≠ f⁻¹ b, by { rw [mul_apply, swap_apply_def], split_ifs; simp only [inv_eq_iff_eq, perm.mul_apply, zpow_neg_succ_of_nat, ne.def, perm.apply_inv_self] at *; cc }, let ⟨i, hi⟩ := is_cycle_swap_mul_aux₁ n hb (show (f⁻¹ ^ n) (f⁻¹ x) = f⁻¹ b, by rw [← zpow_coe_nat, ← h, ← mul_apply, ← mul_apply, ← mul_apply, zpow_neg_succ_of_nat, ← inv_pow, pow_succ', mul_assoc, mul_assoc, inv_mul_self, mul_one, zpow_coe_nat, ← pow_succ', ← pow_succ]) in have h : (swap x (f⁻¹ x) * f⁻¹) (f x) = f⁻¹ x, by rw [mul_apply, inv_apply_self, swap_apply_left], ⟨-i, by rw [← add_sub_cancel i 1, neg_sub, sub_eq_add_neg, zpow_add, zpow_one, zpow_neg, ← inv_zpow, mul_inv_rev, swap_inv, mul_swap_eq_swap_mul, inv_apply_self, swap_comm _ x, zpow_add, zpow_one, mul_apply, mul_apply (_ ^ i), h, hi, mul_apply, apply_inv_self, swap_apply_of_ne_of_ne this.2 (ne.symm hfbx')]⟩ lemma is_cycle.eq_swap_of_apply_apply_eq_self {α : Type*} [decidable_eq α] {f : perm α} (hf : is_cycle f) {x : α} (hfx : f x ≠ x) (hffx : f (f x) = x) : f = swap x (f x) := equiv.ext $ λ y, let ⟨z, hz⟩ := hf in let ⟨i, hi⟩ := hz.2 hfx in if hyx : y = x then by simp [hyx] else if hfyx : y = f x then by simp [hfyx, hffx] else begin rw [swap_apply_of_ne_of_ne hyx hfyx], refine by_contradiction (λ hy, _), cases hz.2 hy with j hj, rw [← sub_add_cancel j i, zpow_add, mul_apply, hi] at hj, cases zpow_apply_eq_of_apply_apply_eq_self hffx (j - i) with hji hji, { rw [← hj, hji] at hyx, cc }, { rw [← hj, hji] at hfyx, cc } end lemma is_cycle.swap_mul {α : Type*} [decidable_eq α] {f : perm α} (hf : is_cycle f) {x : α} (hx : f x ≠ x) (hffx : f (f x) ≠ x) : is_cycle (swap x (f x) * f) := ⟨f x, by { simp [swap_apply_def, mul_apply, if_neg hffx, f.injective.eq_iff, if_neg hx, hx], }, λ y hy, let ⟨i, hi⟩ := hf.exists_zpow_eq hx (ne_and_ne_of_swap_mul_apply_ne_self hy).1 in have hi : (f ^ (i - 1)) (f x) = y, from calc (f ^ (i - 1)) (f x) = (f ^ (i - 1) * f ^ (1 : ℤ)) x : by rw [zpow_one, mul_apply] ... = y : by rwa [← zpow_add, sub_add_cancel], is_cycle_swap_mul_aux₂ (i - 1) hy hi⟩ lemma is_cycle.sign : ∀ {f : perm α} (hf : is_cycle f), sign f = -(-1) ^ f.support.card | f := λ hf, let ⟨x, hx⟩ := hf in calc sign f = sign (swap x (f x) * (swap x (f x) * f)) : by rw [← mul_assoc, mul_def, mul_def, swap_swap, trans_refl] ... = -(-1) ^ f.support.card : if h1 : f (f x) = x then have h : swap x (f x) * f = 1, begin rw hf.eq_swap_of_apply_apply_eq_self hx.1 h1, simp only [perm.mul_def, perm.one_def, swap_apply_left, swap_swap] end, by { rw [sign_mul, sign_swap hx.1.symm, h, sign_one, hf.eq_swap_of_apply_apply_eq_self hx.1 h1, card_support_swap hx.1.symm], refl } else have h : card (support (swap x (f x) * f)) + 1 = card (support f), by rw [← insert_erase (mem_support.2 hx.1), support_swap_mul_eq _ _ h1, card_insert_of_not_mem (not_mem_erase _ _), sdiff_singleton_eq_erase], have wf : card (support (swap x (f x) * f)) < card (support f), from card_support_swap_mul hx.1, by { rw [sign_mul, sign_swap hx.1.symm, (hf.swap_mul hx.1 h1).sign, ← h], simp only [pow_add, mul_one, neg_neg, one_mul, mul_neg, eq_self_iff_true, pow_one, neg_mul_neg] } using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ f, f.support.card)⟩]} lemma is_cycle.of_pow {n : ℕ} (h1 : is_cycle (f ^ n)) (h2 : f.support ⊆ (f ^ n).support) : is_cycle f := begin have key : ∀ x : α, (f ^ n) x ≠ x ↔ f x ≠ x, { simp_rw [←mem_support, ←finset.ext_iff], exact (support_pow_le _ n).antisymm h2 }, obtain ⟨x, hx1, hx2⟩ := h1, refine ⟨x, (key x).mp hx1, λ y hy, _⟩, cases (hx2 ((key y).mpr hy)) with i _, exact ⟨n * i, by rwa zpow_mul⟩ end -- The lemma `support_zpow_le` is relevant. It means that `h2` is equivalent to -- `σ.support = (σ ^ n).support`, as well as to `σ.support.card ≤ (σ ^ n).support.card`. lemma is_cycle.of_zpow {n : ℤ} (h1 : is_cycle (f ^ n)) (h2 : f.support ⊆ (f ^ n).support) : is_cycle f := begin cases n, { exact h1.of_pow h2 }, { simp only [le_eq_subset, zpow_neg_succ_of_nat, perm.support_inv] at h1 h2, simpa using h1.inv.of_pow h2 } end lemma nodup_of_pairwise_disjoint_cycles {l : list (perm β)} (h1 : ∀ f ∈ l, is_cycle f) (h2 : l.pairwise disjoint) : l.nodup := nodup_of_pairwise_disjoint (λ h, (h1 1 h).ne_one rfl) h2 /-- Unlike `support_congr`, which assumes that `∀ (x ∈ g.support), f x = g x)`, here we have the weaker assumption that `∀ (x ∈ f.support), f x = g x`. -/ lemma is_cycle.support_congr (hf : is_cycle f) (hg : is_cycle g) (h : f.support ⊆ g.support) (h' : ∀ x ∈ f.support, f x = g x) : f = g := begin have : f.support = g.support, { refine le_antisymm h _, intros z hz, obtain ⟨x, hx, hf'⟩ := id hf, have hx' : g x ≠ x, { rwa [←h' x (mem_support.mpr hx)] }, obtain ⟨m, hm⟩ := hg.exists_pow_eq hx' (mem_support.mp hz), have h'' : ∀ (x ∈ f.support ∩ g.support), f x = g x, { intros x hx, exact h' x (mem_of_mem_inter_left hx) }, rwa [←hm, ←pow_eq_on_of_mem_support h'' _ x (mem_inter_of_mem (mem_support.mpr hx) (mem_support.mpr hx')), pow_apply_mem_support, mem_support] }, refine support_congr h _, simpa [←this] using h' end /-- If two cyclic permutations agree on all terms in their intersection, and that intersection is not empty, then the two cyclic permutations must be equal. -/ lemma is_cycle.eq_on_support_inter_nonempty_congr (hf : is_cycle f) (hg : is_cycle g) (h : ∀ x ∈ f.support ∩ g.support, f x = g x) (hx : f x = g x) (hx' : x ∈ f.support) : f = g := begin have hx'' : x ∈ g.support, { rwa [mem_support, ←hx, ←mem_support] }, have : f.support ⊆ g.support, { intros y hy, obtain ⟨k, rfl⟩ := hf.exists_pow_eq (mem_support.mp hx') (mem_support.mp hy), rwa [pow_eq_on_of_mem_support h _ _ (mem_inter_of_mem hx' hx''), pow_apply_mem_support] }, rw (inter_eq_left_iff_subset _ _).mpr this at h, exact hf.support_congr hg this h end lemma is_cycle.support_pow_eq_iff (hf : is_cycle f) {n : ℕ} : support (f ^ n) = support f ↔ ¬ order_of f ∣ n := begin rw order_of_dvd_iff_pow_eq_one, split, { intros h H, refine hf.ne_one _, rw [←support_eq_empty_iff, ←h, H, support_one] }, { intro H, apply le_antisymm (support_pow_le _ n) _, intros x hx, contrapose! H, ext z, by_cases hz : f z = z, { rw [pow_apply_eq_self_of_apply_eq_self hz, one_apply] }, { obtain ⟨k, rfl⟩ := hf.exists_pow_eq hz (mem_support.mp hx), apply (f ^ k).injective, rw [←mul_apply, (commute.pow_pow_self _ _ _).eq, mul_apply], simpa using H } } end lemma is_cycle.support_pow_of_pos_of_lt_order_of (hf : is_cycle f) {n : ℕ} (npos : 0 < n) (hn : n < order_of f) : (f ^ n).support = f.support := hf.support_pow_eq_iff.2 $ nat.not_dvd_of_pos_of_lt npos hn lemma is_cycle.pow_iff [finite β] {f : perm β} (hf : is_cycle f) {n : ℕ} : is_cycle (f ^ n) ↔ n.coprime (order_of f) := begin classical, casesI nonempty_fintype β, split, { intro h, have hr : support (f ^ n) = support f, { rw hf.support_pow_eq_iff, rintro ⟨k, rfl⟩, refine h.ne_one _, simp [pow_mul, pow_order_of_eq_one] }, have : order_of (f ^ n) = order_of f, { rw [h.order_of, hr, hf.order_of] }, rw [order_of_pow, nat.div_eq_self] at this, cases this, { exact absurd this (order_of_pos _).ne' }, { rwa [nat.coprime_iff_gcd_eq_one, nat.gcd_comm] } }, { intro h, obtain ⟨m, hm⟩ := exists_pow_eq_self_of_coprime h, have hf' : is_cycle ((f ^ n) ^ m) := by rwa hm, refine hf'.of_pow (λ x hx, _), rw [hm], exact support_pow_le _ n hx } end -- TODO: Define a `set`-valued support to get rid of the `finite β` assumption lemma is_cycle.pow_eq_one_iff [finite β] {f : perm β} (hf : is_cycle f) {n : ℕ} : f ^ n = 1 ↔ ∃ x, f x ≠ x ∧ (f ^ n) x = x := begin classical, casesI nonempty_fintype β, split, { intro h, obtain ⟨x, hx, -⟩ := id hf, exact ⟨x, hx, by simp [h]⟩ }, { rintro ⟨x, hx, hx'⟩, by_cases h : support (f ^ n) = support f, { rw [← mem_support, ← h, mem_support] at hx, contradiction }, { rw [hf.support_pow_eq_iff, not_not] at h, obtain ⟨k, rfl⟩ := h, rw [pow_mul, pow_order_of_eq_one, one_pow] } } end -- TODO: Define a `set`-valued support to get rid of the `finite β` assumption lemma is_cycle.pow_eq_one_iff' [finite β] {f : perm β} (hf : is_cycle f) {n : ℕ} {x : β} (hx : f x ≠ x) : f ^ n = 1 ↔ (f ^ n) x = x := ⟨λ h, fun_like.congr_fun h x, λ h, hf.pow_eq_one_iff.2 ⟨x, hx, h⟩⟩ -- TODO: Define a `set`-valued support to get rid of the `finite β` assumption lemma is_cycle.pow_eq_one_iff'' [finite β] {f : perm β} (hf : is_cycle f) {n : ℕ} : f ^ n = 1 ↔ ∀ x, f x ≠ x → (f ^ n) x = x := ⟨λ h x hx, (hf.pow_eq_one_iff' hx).1 h, λ h, let ⟨x, hx, _⟩ := id hf in (hf.pow_eq_one_iff' hx).2 (h _ hx)⟩ -- TODO: Define a `set`-valued support to get rid of the `finite β` assumption lemma is_cycle.pow_eq_pow_iff [finite β] {f : perm β} (hf : is_cycle f) {a b : ℕ} : f ^ a = f ^ b ↔ ∃ x, f x ≠ x ∧ (f ^ a) x = (f ^ b) x := begin classical, casesI nonempty_fintype β, split, { intro h, obtain ⟨x, hx, -⟩ := id hf, exact ⟨x, hx, by simp [h]⟩ }, { rintro ⟨x, hx, hx'⟩, wlog hab : a ≤ b, suffices : f ^ (b - a) = 1, { rw [pow_sub _ hab, mul_inv_eq_one] at this, rw this }, rw hf.pow_eq_one_iff, by_cases hfa : (f ^ a) x ∈ f.support, { refine ⟨(f ^ a) x, mem_support.mp hfa, _⟩, simp only [pow_sub _ hab, equiv.perm.coe_mul, function.comp_app, inv_apply_self, ← hx'] }, { have h := @equiv.perm.zpow_apply_comm _ f 1 a x, simp only [zpow_one, zpow_coe_nat] at h, rw [not_mem_support, h, function.injective.eq_iff (f ^ a).injective] at hfa, contradiction }} end lemma is_cycle.is_cycle_pow_pos_of_lt_prime_order [finite β] {f : perm β} (hf : is_cycle f) (hf' : (order_of f).prime) (n : ℕ) (hn : 0 < n) (hn' : n < order_of f) : is_cycle (f ^ n) := begin classical, casesI nonempty_fintype β, have : n.coprime (order_of f), { refine nat.coprime.symm _, rw nat.prime.coprime_iff_not_dvd hf', exact nat.not_dvd_of_pos_of_lt hn hn' }, obtain ⟨m, hm⟩ := exists_pow_eq_self_of_coprime this, have hf'' := hf, rw ←hm at hf'', refine hf''.of_pow _, rw [hm], exact support_pow_le f n end end is_cycle /-! ### `is_cycle_on` -/ section is_cycle_on variables {f g : perm α} {s t : set α} {a b x y : α} /-- A permutation is a cycle on `s` when any two points of `s` are related by repeated application of the permutation. Note that this means the identity is a cycle of subsingleton sets. -/ def is_cycle_on (f : perm α) (s : set α) : Prop := set.bij_on f s s ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → f.same_cycle x y @[simp] lemma is_cycle_on_empty : f.is_cycle_on ∅ := by simp [is_cycle_on] @[simp] lemma is_cycle_on_one : (1 : perm α).is_cycle_on s ↔ s.subsingleton := by simp [is_cycle_on, set.bij_on_id, set.subsingleton] alias is_cycle_on_one ↔ is_cycle_on.subsingleton _root_.set.subsingleton.is_cycle_on_one @[simp] lemma is_cycle_on_singleton : f.is_cycle_on {a} ↔ f a = a := by simp [is_cycle_on, same_cycle.rfl] lemma is_cycle_on_of_subsingleton [subsingleton α] (f : perm α) (s : set α) : f.is_cycle_on s := ⟨s.bij_on_of_subsingleton _, λ x _ y _, (subsingleton.elim x y).same_cycle _⟩ @[simp] lemma is_cycle_on_inv : f⁻¹.is_cycle_on s ↔ f.is_cycle_on s := by simp [is_cycle_on, set.bij_on_perm_inv] alias is_cycle_on_inv ↔ is_cycle_on.of_inv is_cycle_on.inv lemma is_cycle_on.conj (h : f.is_cycle_on s) : (g * f * g⁻¹).is_cycle_on ((g : perm α) '' s) := ⟨(g.bij_on_image.comp h.1).comp g.bij_on_symm_image, λ x hx y hy, by { rw ←preimage_inv at hx hy, convert (h.2 hx hy).conj; rw apply_inv_self }⟩ lemma is_cycle_on_swap [decidable_eq α] (hab : a ≠ b) : (swap a b).is_cycle_on {a, b} := ⟨bij_on_swap (by simp) (by simp), λ x hx y hy, begin rw [set.mem_insert_iff, set.mem_singleton_iff] at hx hy, obtain rfl | rfl := hx; obtain rfl | rfl := hy, { exact ⟨0, by rw [zpow_zero, coe_one, id.def]⟩ }, { exact ⟨1, by rw [zpow_one, swap_apply_left]⟩ }, { exact ⟨1, by rw [zpow_one, swap_apply_right]⟩ }, { exact ⟨0, by rw [zpow_zero, coe_one, id.def]⟩ } end⟩ protected lemma is_cycle_on.apply_ne (hf : f.is_cycle_on s) (hs : s.nontrivial) (ha : a ∈ s) : f a ≠ a := begin obtain ⟨b, hb, hba⟩ := hs.exists_ne a, obtain ⟨n, rfl⟩ := hf.2 ha hb, exact λ h, hba (is_fixed_pt.perm_zpow h n), end protected lemma is_cycle.is_cycle_on (hf : f.is_cycle) : f.is_cycle_on {x | f x ≠ x} := ⟨f.bij_on $ λ x, f.apply_eq_iff_eq.not, λ a ha b, hf.same_cycle ha⟩ /-- This lemma demonstrates the relation between `equiv.perm.is_cycle` and `equiv.perm.is_cycle_on` in non-degenerate cases. -/ lemma is_cycle_iff_exists_is_cycle_on : f.is_cycle ↔ ∃ s : set α, s.nontrivial ∧ f.is_cycle_on s ∧ ∀ ⦃x⦄, ¬ is_fixed_pt f x → x ∈ s := begin refine ⟨λ hf, ⟨{x | f x ≠ x}, _, hf.is_cycle_on, λ _, id⟩, _⟩, { obtain ⟨a, ha⟩ := hf, exact ⟨f a, f.injective.ne ha.1, a, ha.1, ha.1⟩ }, { rintro ⟨s, hs, hf, hsf⟩, obtain ⟨a, ha⟩ := hs.nonempty, exact ⟨a, hf.apply_ne hs ha, λ b hb, hf.2 ha $ hsf hb⟩ } end lemma is_cycle_on.apply_mem_iff (hf : f.is_cycle_on s) : f x ∈ s ↔ x ∈ s := ⟨λ hx, by { convert hf.1.perm_inv.1 hx, rw inv_apply_self }, λ hx, hf.1.maps_to hx⟩ /-- Note that the identity satisfies `is_cycle_on` for any subsingleton set, but not `is_cycle`. -/ lemma is_cycle_on.is_cycle_subtype_perm (hf : f.is_cycle_on s) (hs : s.nontrivial) : (f.subtype_perm $ λ _, hf.apply_mem_iff.symm : perm s).is_cycle := begin obtain ⟨a, ha⟩ := hs.nonempty, exact ⟨⟨a, ha⟩, ne_of_apply_ne (coe : s → α) (hf.apply_ne hs ha), λ b hb, (hf.2 (⟨a, ha⟩ : s).prop b.prop).subtype_perm⟩, end /-- Note that the identity is a cycle on any subsingleton set, but not a cycle. -/ protected lemma is_cycle_on.subtype_perm (hf : f.is_cycle_on s) : (f.subtype_perm $ λ _, hf.apply_mem_iff.symm : perm s).is_cycle_on set.univ := begin obtain hs | hs := s.subsingleton_or_nontrivial, { haveI := hs.coe_sort, exact is_cycle_on_of_subsingleton _ _ }, convert (hf.is_cycle_subtype_perm hs).is_cycle_on, rw [eq_comm, set.eq_univ_iff_forall], exact λ x, ne_of_apply_ne (coe : s → α) (hf.apply_ne hs x.prop), end -- TODO: Theory of order of an element under an action lemma is_cycle_on.pow_apply_eq {s : finset α} (hf : f.is_cycle_on s) (ha : a ∈ s) {n : ℕ} : (f ^ n) a = a ↔ s.card ∣ n := begin obtain rfl | hs := finset.eq_singleton_or_nontrivial ha, { rw [coe_singleton, is_cycle_on_singleton] at hf, simpa using is_fixed_pt.iterate hf n }, classical, have h : ∀ x ∈ s.attach, ¬ f ↑x = ↑x := λ x hx, hf.apply_ne hs x.prop, have := (hf.is_cycle_subtype_perm hs).order_of, simp only [filter_true_of_mem h, support_subtype_perm, card_attach] at this, rw [←this, order_of_dvd_iff_pow_eq_one, (hf.is_cycle_subtype_perm hs).pow_eq_one_iff' (ne_of_apply_ne (coe : s → α) $ hf.apply_ne hs (⟨a, ha⟩ : s).prop)], simp only [subtype.coe_mk, subtype_perm_pow, subtype_perm_apply], end lemma is_cycle_on.zpow_apply_eq {s : finset α} (hf : f.is_cycle_on s) (ha : a ∈ s) : ∀ {n : ℤ}, (f ^ n) a = a ↔ (s.card : ℤ) ∣ n | (int.of_nat n) := (hf.pow_apply_eq ha).trans int.coe_nat_dvd.symm | (int.neg_succ_of_nat n) := by { rw [zpow_neg_succ_of_nat, ←inv_pow], exact (hf.inv.pow_apply_eq ha).trans ((dvd_neg _ _).trans int.coe_nat_dvd).symm } lemma is_cycle_on.pow_apply_eq_pow_apply {s : finset α} (hf : f.is_cycle_on s) (ha : a ∈ s) {m n : ℕ} : (f ^ m) a = (f ^ n) a ↔ m ≡ n [MOD s.card] := begin rw [nat.modeq_iff_dvd, ←hf.zpow_apply_eq ha], simp [sub_eq_neg_add, zpow_add, eq_inv_iff_eq, eq_comm], end lemma is_cycle_on.zpow_apply_eq_zpow_apply {s : finset α} (hf : f.is_cycle_on s) (ha : a ∈ s) {m n : ℤ} : (f ^ m) a = (f ^ n) a ↔ m ≡ n [ZMOD s.card] := begin rw [int.modeq_iff_dvd, ←hf.zpow_apply_eq ha], simp [sub_eq_neg_add, zpow_add, eq_inv_iff_eq, eq_comm], end lemma is_cycle_on.pow_card_apply {s : finset α} (hf : f.is_cycle_on s) (ha : a ∈ s) : (f ^ s.card) a = a := (hf.pow_apply_eq ha).2 dvd_rfl lemma is_cycle_on.exists_pow_eq {s : finset α} (hf : f.is_cycle_on s) (ha : a ∈ s) (hb : b ∈ s) : ∃ n < s.card, (f ^ n) a = b := begin classical, obtain ⟨n, rfl⟩ := hf.2 ha hb, obtain ⟨k, hk⟩ := (int.mod_modeq n s.card).symm.dvd, refine ⟨n.nat_mod s.card, int.nat_mod_lt (nonempty.card_pos ⟨a, ha⟩).ne', _⟩, rw [←zpow_coe_nat, int.nat_mod, int.to_nat_of_nonneg (int.mod_nonneg _ $ nat.cast_ne_zero.2 (nonempty.card_pos ⟨a, ha⟩).ne'), sub_eq_iff_eq_add'.1 hk, zpow_add, zpow_mul], simp only [zpow_coe_nat, coe_mul, embedding_like.apply_eq_iff_eq], exact is_fixed_pt.perm_zpow (hf.pow_card_apply ha) _, end lemma is_cycle_on.exists_pow_eq' (hs : s.finite) (hf : f.is_cycle_on s) (ha : a ∈ s) (hb : b ∈ s) : ∃ n : ℕ, (f ^ n) a = b := by { lift s to finset α using id hs, obtain ⟨n, -, hn⟩ := hf.exists_pow_eq ha hb, exact ⟨n, hn⟩ } lemma is_cycle_on.range_pow (hs : s.finite) (h : f.is_cycle_on s) (ha : a ∈ s) : set.range (λ n, (f ^ n) a : ℕ → α) = s := set.subset.antisymm (set.range_subset_iff.2 $ λ n, h.1.maps_to.perm_pow _ ha) $ λ x, h.exists_pow_eq' hs ha lemma is_cycle_on.range_zpow (h : f.is_cycle_on s) (ha : a ∈ s) : set.range (λ n, (f ^ n) a : ℤ → α) = s := set.subset.antisymm (set.range_subset_iff.2 $ λ n, (h.1.perm_zpow _).maps_to ha) $ h.2 ha lemma is_cycle_on.of_pow {n : ℕ} (hf : (f ^ n).is_cycle_on s) (h : set.bij_on f s s) : f.is_cycle_on s := ⟨h, λ x hx y hy, (hf.2 hx hy).of_pow⟩ lemma is_cycle_on.of_zpow {n : ℤ} (hf : (f ^ n).is_cycle_on s) (h : set.bij_on f s s) : f.is_cycle_on s := ⟨h, λ x hx y hy, (hf.2 hx hy).of_zpow⟩ lemma is_cycle_on.extend_domain {p : β → Prop} [decidable_pred p] (f : α ≃ subtype p) (h : g.is_cycle_on s) : (g.extend_domain f).is_cycle_on (coe ∘ f '' s) := ⟨h.1.extend_domain, by { rintro _ ⟨a, ha, rfl⟩ _ ⟨b, hb, rfl⟩, exact (h.2 ha hb).extend_domain }⟩ end is_cycle_on /-! ### `cycle_of` -/ section cycle_of variables [decidable_eq α] [fintype α] {f g : perm α} {x y : α} /-- `f.cycle_of x` is the cycle of the permutation `f` to which `x` belongs. -/ def cycle_of (f : perm α) (x : α) : perm α := of_subtype (subtype_perm f (λ _, same_cycle_apply_right.symm) : perm {y // same_cycle f x y}) lemma cycle_of_apply (f : perm α) (x y : α) : cycle_of f x y = if same_cycle f x y then f y else y := begin dsimp only [cycle_of], split_ifs, { apply of_subtype_apply_of_mem, exact h, }, { apply of_subtype_apply_of_not_mem, exact h }, end lemma cycle_of_inv (f : perm α) (x : α) : (cycle_of f x)⁻¹ = cycle_of f⁻¹ x := equiv.ext $ λ y, begin rw [inv_eq_iff_eq, cycle_of_apply, cycle_of_apply], split_ifs; simp [*, same_cycle_inv, same_cycle_inv_apply_right] at * end @[simp] lemma cycle_of_pow_apply_self (f : perm α) (x : α) : ∀ n : ℕ, (cycle_of f x ^ n) x = (f ^ n) x | 0 := rfl | (n+1) := by { rw [pow_succ, mul_apply, cycle_of_apply, cycle_of_pow_apply_self, if_pos, pow_succ, mul_apply], exact ⟨n, rfl⟩ } @[simp] lemma cycle_of_zpow_apply_self (f : perm α) (x : α) : ∀ n : ℤ, (cycle_of f x ^ n) x = (f ^ n) x | (n : ℕ) := cycle_of_pow_apply_self f x n | -[1+ n] := by rw [zpow_neg_succ_of_nat, ← inv_pow, cycle_of_inv, zpow_neg_succ_of_nat, ← inv_pow, cycle_of_pow_apply_self] lemma same_cycle.cycle_of_apply : same_cycle f x y → cycle_of f x y = f y := of_subtype_apply_of_mem _ lemma cycle_of_apply_of_not_same_cycle : ¬ same_cycle f x y → cycle_of f x y = y := of_subtype_apply_of_not_mem _ lemma same_cycle.cycle_of_eq (h : same_cycle f x y) : cycle_of f x = cycle_of f y := begin ext z, rw cycle_of_apply, split_ifs with hz hz, { exact (h.symm.trans hz).cycle_of_apply.symm }, { exact (cycle_of_apply_of_not_same_cycle (mt h.trans hz)).symm } end @[simp] lemma cycle_of_apply_apply_zpow_self (f : perm α) (x : α) (k : ℤ) : cycle_of f x ((f ^ k) x) = (f ^ (k + 1)) x := begin rw same_cycle.cycle_of_apply, { rw [add_comm, zpow_add, zpow_one, mul_apply] }, { exact ⟨k, rfl⟩ } end @[simp] lemma cycle_of_apply_apply_pow_self (f : perm α) (x : α) (k : ℕ) : cycle_of f x ((f ^ k) x) = (f ^ (k + 1)) x := by convert cycle_of_apply_apply_zpow_self f x k using 1 @[simp] lemma cycle_of_apply_apply_self (f : perm α) (x : α) : cycle_of f x (f x) = f (f x) := by convert cycle_of_apply_apply_pow_self f x 1 using 1 @[simp] lemma cycle_of_apply_self (f : perm α) (x : α) : cycle_of f x x = f x := same_cycle.rfl.cycle_of_apply lemma is_cycle.cycle_of_eq (hf : is_cycle f) (hx : f x ≠ x) : cycle_of f x = f := equiv.ext $ λ y, if h : same_cycle f x y then by rw [h.cycle_of_apply] else by rw [cycle_of_apply_of_not_same_cycle h, not_not.1 (mt ((is_cycle_iff_same_cycle hx).1 hf).2 h)] @[simp] lemma cycle_of_eq_one_iff (f : perm α) : cycle_of f x = 1 ↔ f x = x := begin simp_rw [ext_iff, cycle_of_apply, one_apply], refine ⟨λ h, (if_pos (same_cycle.refl f x)).symm.trans (h x), λ h y, _⟩, by_cases hy : f y = y, { rw [hy, if_t_t] }, { exact if_neg (mt same_cycle.apply_eq_self_iff (by tauto)) }, end @[simp] lemma cycle_of_self_apply (f : perm α) (x : α) : cycle_of f (f x) = cycle_of f x := (same_cycle_apply_right.2 same_cycle.rfl).symm.cycle_of_eq @[simp] lemma cycle_of_self_apply_pow (f : perm α) (n : ℕ) (x : α) : cycle_of f ((f ^ n) x) = cycle_of f x := same_cycle.rfl.pow_left.cycle_of_eq @[simp] lemma cycle_of_self_apply_zpow (f : perm α) (n : ℤ) (x : α) : cycle_of f ((f ^ n) x) = cycle_of f x := same_cycle.rfl.zpow_left.cycle_of_eq protected lemma is_cycle.cycle_of (hf : is_cycle f) : cycle_of f x = if f x = x then 1 else f := begin by_cases hx : f x = x, { rwa [if_pos hx, cycle_of_eq_one_iff] }, { rwa [if_neg hx, hf.cycle_of_eq] }, end lemma cycle_of_one (x : α) : cycle_of 1 x = 1 := (cycle_of_eq_one_iff 1).mpr rfl lemma is_cycle_cycle_of (f : perm α) (hx : f x ≠ x) : is_cycle (cycle_of f x) := have cycle_of f x x ≠ x, by rwa [same_cycle.rfl.cycle_of_apply], (is_cycle_iff_same_cycle this).2 $ λ y, ⟨λ h, mt h.apply_eq_self_iff.2 this, λ h, if hxy : same_cycle f x y then let ⟨i, hi⟩ := hxy in ⟨i, by rw [cycle_of_zpow_apply_self, hi]⟩ else by { rw [cycle_of_apply_of_not_same_cycle hxy] at h, exact (h rfl).elim }⟩ @[simp] lemma two_le_card_support_cycle_of_iff : 2 ≤ card (cycle_of f x).support ↔ f x ≠ x := begin refine ⟨λ h, _, λ h, by simpa using (is_cycle_cycle_of _ h).two_le_card_support⟩, contrapose! h, rw ←cycle_of_eq_one_iff at h, simp [h] end @[simp] lemma card_support_cycle_of_pos_iff : 0 < card (cycle_of f x).support ↔ f x ≠ x := begin rw [←two_le_card_support_cycle_of_iff, ←nat.succ_le_iff], exact ⟨λ h, or.resolve_left h.eq_or_lt (card_support_ne_one _).symm, zero_lt_two.trans_le⟩ end lemma pow_apply_eq_pow_mod_order_of_cycle_of_apply (f : perm α) (n : ℕ) (x : α) : (f ^ n) x = (f ^ (n % order_of (cycle_of f x))) x := by rw [←cycle_of_pow_apply_self f, ←cycle_of_pow_apply_self f, pow_eq_mod_order_of] lemma cycle_of_mul_of_apply_right_eq_self (h : _root_.commute f g) (x : α) (hx : g x = x) : (f * g).cycle_of x = f.cycle_of x := begin ext y, by_cases hxy : (f * g).same_cycle x y, { obtain ⟨z, rfl⟩ := hxy, rw cycle_of_apply_apply_zpow_self, simp [h.mul_zpow, zpow_apply_eq_self_of_apply_eq_self hx] }, { rw [cycle_of_apply_of_not_same_cycle hxy, cycle_of_apply_of_not_same_cycle], contrapose! hxy, obtain ⟨z, rfl⟩ := hxy, refine ⟨z, _⟩, simp [h.mul_zpow, zpow_apply_eq_self_of_apply_eq_self hx] } end lemma disjoint.cycle_of_mul_distrib (h : f.disjoint g) (x : α) : (f * g).cycle_of x = (f.cycle_of x * g.cycle_of x) := begin cases (disjoint_iff_eq_or_eq.mp h) x with hfx hgx, { simp [h.commute.eq, cycle_of_mul_of_apply_right_eq_self h.symm.commute, hfx] }, { simp [cycle_of_mul_of_apply_right_eq_self h.commute, hgx] } end lemma support_cycle_of_eq_nil_iff : (f.cycle_of x).support = ∅ ↔ x ∉ f.support := by simp lemma support_cycle_of_le (f : perm α) (x : α) : support (f.cycle_of x) ≤ support f := begin intros y hy, rw [mem_support, cycle_of_apply] at hy, split_ifs at hy, { exact mem_support.mpr hy }, { exact absurd rfl hy } end lemma mem_support_cycle_of_iff : y ∈ support (f.cycle_of x) ↔ same_cycle f x y ∧ x ∈ support f := begin by_cases hx : f x = x, { rw (cycle_of_eq_one_iff _).mpr hx, simp [hx] }, { rw [mem_support, cycle_of_apply], split_ifs with hy, { simp only [hx, hy, iff_true, ne.def, not_false_iff, and_self, mem_support], rcases hy with ⟨k, rfl⟩, rw ←not_mem_support, simpa using hx }, { simpa [hx] using hy } } end lemma mem_support_cycle_of_iff' (hx : f x ≠ x) : y ∈ support (f.cycle_of x) ↔ same_cycle f x y := by rw [mem_support_cycle_of_iff, and_iff_left (mem_support.2 hx)] lemma same_cycle.mem_support_iff (h : same_cycle f x y) : x ∈ support f ↔ y ∈ support f := ⟨λ hx, support_cycle_of_le f x (mem_support_cycle_of_iff.mpr ⟨h, hx⟩), λ hy, support_cycle_of_le f y (mem_support_cycle_of_iff.mpr ⟨h.symm, hy⟩)⟩ lemma pow_mod_card_support_cycle_of_self_apply (f : perm α) (n : ℕ) (x : α) : (f ^ (n % (f.cycle_of x).support.card)) x = (f ^ n) x := begin by_cases hx : f x = x, { rw [pow_apply_eq_self_of_apply_eq_self hx, pow_apply_eq_self_of_apply_eq_self hx] }, { rw [←cycle_of_pow_apply_self, ←cycle_of_pow_apply_self f, ←(is_cycle_cycle_of f hx).order_of, ←pow_eq_mod_order_of] } end /-- `x` is in the support of `f` iff `equiv.perm.cycle_of f x` is a cycle. -/ lemma is_cycle_cycle_of_iff (f : perm α) : is_cycle (cycle_of f x) ↔ f x ≠ x := begin refine ⟨λ hx, _, f.is_cycle_cycle_of⟩, rw [ne.def, ←cycle_of_eq_one_iff f], exact hx.ne_one, end lemma is_cycle_on_support_cycle_of (f : perm α) (x : α) : f.is_cycle_on (f.cycle_of x).support := ⟨f.bij_on $ by simp [mem_support_cycle_of_iff], λ a ha b hb, by { rw [mem_coe, mem_support_cycle_of_iff] at ha hb, exact ha.1.symm.trans hb.1 }⟩ lemma same_cycle.exists_pow_eq_of_mem_support (h : same_cycle f x y) (hx : x ∈ f.support) : ∃ (i : ℕ) (hi' : i < (f.cycle_of x).support.card), (f ^ i) x = y := begin rw mem_support at hx, refine (f.is_cycle_on_support_cycle_of _).exists_pow_eq _ _; rwa mem_support_cycle_of_iff' hx, end lemma same_cycle.exists_pow_eq (f : perm α) (h : same_cycle f x y) : ∃ (i : ℕ) (hi : 0 < i) (hi' : i ≤ (f.cycle_of x).support.card + 1), (f ^ i) x = y := begin by_cases hx : x ∈ f.support, { obtain ⟨k, hk, hk'⟩ := h.exists_pow_eq_of_mem_support hx, cases k, { refine ⟨(f.cycle_of x).support.card, _, self_le_add_right _ _, _⟩, { refine zero_lt_one.trans (one_lt_card_support_of_ne_one _), simpa using hx }, { simp only [perm.coe_one, id.def, pow_zero] at hk', subst hk', rw [←(is_cycle_cycle_of _ $ mem_support.1 hx).order_of, ←cycle_of_pow_apply_self, pow_order_of_eq_one, one_apply] } }, { exact ⟨k + 1, by simp, nat.le_succ_of_le hk.le, hk'⟩ } }, { refine ⟨1, zero_lt_one, by simp, _⟩, obtain ⟨k, rfl⟩ := h, rw [not_mem_support] at hx, rw [pow_apply_eq_self_of_apply_eq_self hx, zpow_apply_eq_self_of_apply_eq_self hx] } end end cycle_of /-! ### `cycle_factors` -/ variables [decidable_eq α] /-- Given a list `l : list α` and a permutation `f : perm α` whose nonfixed points are all in `l`, recursively factors `f` into cycles. -/ def cycle_factors_aux [fintype α] : Π (l : list α) (f : perm α), (∀ {x}, f x ≠ x → x ∈ l) → {l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint} | [] f h := ⟨[], by { simp only [imp_false, list.pairwise.nil, list.not_mem_nil, forall_const, and_true, forall_prop_of_false, not_not, not_false_iff, list.prod_nil] at *, ext, simp * }⟩ | (x::l) f h := if hx : f x = x then cycle_factors_aux l f (λ y hy, list.mem_of_ne_of_mem (λ h, hy (by rwa h)) (h hy)) else let ⟨m, hm₁, hm₂, hm₃⟩ := cycle_factors_aux l ((cycle_of f x)⁻¹ * f) (λ y hy, list.mem_of_ne_of_mem (λ h : y = x, by { rw [h, mul_apply, ne.def, inv_eq_iff_eq, cycle_of_apply_self] at hy, exact hy rfl }) (h (λ h : f y = y, by { rw [mul_apply, h, ne.def, inv_eq_iff_eq, cycle_of_apply] at hy, split_ifs at hy; cc }))) in ⟨(cycle_of f x) :: m, by { rw [list.prod_cons, hm₁], simp }, λ g hg, ((list.mem_cons_iff _ _ _).1 hg).elim (λ hg, hg.symm ▸ is_cycle_cycle_of _ hx) (hm₂ g), list.pairwise_cons.2 ⟨λ g hg y, or_iff_not_imp_left.2 (λ hfy, have hxy : same_cycle f x y := not_not.1 (mt cycle_of_apply_of_not_same_cycle hfy), have hgm : g :: m.erase g ~ m := list.cons_perm_iff_perm_erase.2 ⟨hg, list.perm.refl _⟩, have ∀ h ∈ m.erase g, disjoint g h, from (list.pairwise_cons.1 ((hgm.pairwise_iff (λ a b (h : disjoint a b), h.symm)).2 hm₃)).1, classical.by_cases id $ λ hgy : g y ≠ y, (disjoint_prod_right _ this y).resolve_right $ have hsc : same_cycle f⁻¹ x (f y), by rwa [same_cycle_inv, same_cycle_apply_right], by { rw [disjoint_prod_perm hm₃ hgm.symm, list.prod_cons, ← eq_inv_mul_iff_mul_eq] at hm₁, rwa [hm₁, mul_apply, mul_apply, cycle_of_inv, hsc.cycle_of_apply, inv_apply_self, inv_eq_iff_eq, eq_comm] }), hm₃⟩⟩ lemma mem_list_cycles_iff {α : Type*} [finite α] {l : list (perm α)} (h1 : ∀ σ : perm α, σ ∈ l → σ.is_cycle) (h2 : l.pairwise disjoint) {σ : perm α} : σ ∈ l ↔ σ.is_cycle ∧ ∀ (a : α) (h4 : σ a ≠ a), σ a = l.prod a := begin suffices : σ.is_cycle → (σ ∈ l ↔ ∀ (a : α) (h4 : σ a ≠ a), σ a = l.prod a), { exact ⟨λ hσ, ⟨h1 σ hσ, (this (h1 σ hσ)).mp hσ⟩, λ hσ, (this hσ.1).mpr hσ.2⟩ }, intro h3, classical, casesI nonempty_fintype α, split, { intros h a ha, exact eq_on_support_mem_disjoint h h2 _ (mem_support.mpr ha) }, { intros h, have hσl : σ.support ⊆ l.prod.support, { intros x hx, rw mem_support at hx, rwa [mem_support, ←h _ hx] }, obtain ⟨a, ha, -⟩ := id h3, rw ←mem_support at ha, obtain ⟨τ, hτ, hτa⟩ := exists_mem_support_of_mem_support_prod (hσl ha), have hτl : ∀ (x ∈ τ.support), τ x = l.prod x := eq_on_support_mem_disjoint hτ h2, have key : ∀ (x ∈ σ.support ∩ τ.support), σ x = τ x, { intros x hx, rw [h x (mem_support.mp (mem_of_mem_inter_left hx)), hτl x (mem_of_mem_inter_right hx)] }, convert hτ, refine h3.eq_on_support_inter_nonempty_congr (h1 _ hτ) key _ ha, exact key a (mem_inter_of_mem ha hτa) } end lemma list_cycles_perm_list_cycles {α : Type*} [finite α] {l₁ l₂ : list (perm α)} (h₀ : l₁.prod = l₂.prod) (h₁l₁ : ∀ σ : perm α, σ ∈ l₁ → σ.is_cycle) (h₁l₂ : ∀ σ : perm α, σ ∈ l₂ → σ.is_cycle) (h₂l₁ : l₁.pairwise disjoint) (h₂l₂ : l₂.pairwise disjoint) : l₁ ~ l₂ := begin classical, refine (list.perm_ext (nodup_of_pairwise_disjoint_cycles h₁l₁ h₂l₁) (nodup_of_pairwise_disjoint_cycles h₁l₂ h₂l₂)).mpr (λ σ, _), by_cases hσ : σ.is_cycle, { obtain ⟨a, ha⟩ := not_forall.mp (mt ext hσ.ne_one), rw [mem_list_cycles_iff h₁l₁ h₂l₁, mem_list_cycles_iff h₁l₂ h₂l₂, h₀] }, { exact iff_of_false (mt (h₁l₁ σ) hσ) (mt (h₁l₂ σ) hσ) } end /-- Factors a permutation `f` into a list of disjoint cyclic permutations that multiply to `f`. -/ def cycle_factors [fintype α] [linear_order α] (f : perm α) : {l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint} := cycle_factors_aux (univ.sort (≤)) f (λ _ _, (mem_sort _).2 (mem_univ _)) /-- Factors a permutation `f` into a list of disjoint cyclic permutations that multiply to `f`, without a linear order. -/ def trunc_cycle_factors [fintype α] (f : perm α) : trunc {l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint} := quotient.rec_on_subsingleton (@univ α _).1 (λ l h, trunc.mk (cycle_factors_aux l f h)) (show ∀ x, f x ≠ x → x ∈ (@univ α _).1, from λ _ _, mem_univ _) section cycle_factors_finset variables [fintype α] (f : perm α) /-- Factors a permutation `f` into a `finset` of disjoint cyclic permutations that multiply to `f`. -/ def cycle_factors_finset : finset (perm α) := (trunc_cycle_factors f).lift (λ (l : {l : list (perm α) // l.prod = f ∧ (∀ g ∈ l, is_cycle g) ∧ l.pairwise disjoint}), l.val.to_finset) (λ ⟨l, hl⟩ ⟨l', hl'⟩, list.to_finset_eq_of_perm _ _ (list_cycles_perm_list_cycles (hl'.left.symm ▸ hl.left) hl.right.left (hl'.right.left) hl.right.right hl'.right.right)) lemma cycle_factors_finset_eq_list_to_finset {σ : perm α} {l : list (perm α)} (hn : l.nodup) : σ.cycle_factors_finset = l.to_finset ↔ (∀ f : perm α, f ∈ l → f.is_cycle) ∧ l.pairwise disjoint ∧ l.prod = σ := begin obtain ⟨⟨l', hp', hc', hd'⟩, hl⟩ := trunc.exists_rep σ.trunc_cycle_factors, have ht : cycle_factors_finset σ = l'.to_finset, { rw [cycle_factors_finset, ←hl, trunc.lift_mk] }, rw ht, split, { intro h, have hn' : l'.nodup := nodup_of_pairwise_disjoint_cycles hc' hd', have hperm : l ~ l' := list.perm_of_nodup_nodup_to_finset_eq hn hn' h.symm, refine ⟨_, _, _⟩, { exact λ _ h, hc' _ (hperm.subset h) }, { rwa list.perm.pairwise_iff disjoint.symmetric hperm }, { rw [←hp', hperm.symm.prod_eq'], refine hd'.imp _, exact λ _ _, disjoint.commute } }, { rintro ⟨hc, hd, hp⟩, refine list.to_finset_eq_of_perm _ _ _, refine list_cycles_perm_list_cycles _ hc' hc hd' hd, rw [hp, hp'] } end lemma cycle_factors_finset_eq_finset {σ : perm α} {s : finset (perm α)} : σ.cycle_factors_finset = s ↔ (∀ f : perm α, f ∈ s → f.is_cycle) ∧ ∃ h : (s : set (perm α)).pairwise disjoint, s.noncomm_prod id (h.mono' $ λ _ _, disjoint.commute) = σ := begin obtain ⟨l, hl, rfl⟩ := s.exists_list_nodup_eq, simp [cycle_factors_finset_eq_list_to_finset, hl], end lemma cycle_factors_finset_pairwise_disjoint : (cycle_factors_finset f : set (perm α)).pairwise disjoint := (cycle_factors_finset_eq_finset.mp rfl).2.some lemma cycle_factors_finset_mem_commute : (cycle_factors_finset f : set (perm α)).pairwise commute := (cycle_factors_finset_pairwise_disjoint _).mono' $ λ _ _, disjoint.commute /-- The product of cycle factors is equal to the original `f : perm α`. -/ lemma cycle_factors_finset_noncomm_prod (comm : (cycle_factors_finset f : set (perm α)).pairwise commute := cycle_factors_finset_mem_commute f) : f.cycle_factors_finset.noncomm_prod id comm = f := (cycle_factors_finset_eq_finset.mp rfl).2.some_spec lemma mem_cycle_factors_finset_iff {f p : perm α} : p ∈ cycle_factors_finset f ↔ p.is_cycle ∧ ∀ (a ∈ p.support), p a = f a := begin obtain ⟨l, hl, hl'⟩ := f.cycle_factors_finset.exists_list_nodup_eq, rw ←hl', rw [eq_comm, cycle_factors_finset_eq_list_to_finset hl] at hl', simpa [list.mem_to_finset, ne.def, ←hl'.right.right] using mem_list_cycles_iff hl'.left hl'.right.left end lemma cycle_of_mem_cycle_factors_finset_iff {f : perm α} {x : α} : cycle_of f x ∈ cycle_factors_finset f ↔ x ∈ f.support := begin rw mem_cycle_factors_finset_iff, split, { rintro ⟨hc, h⟩, contrapose! hc, rw [not_mem_support, ←cycle_of_eq_one_iff] at hc, simp [hc] }, { intros hx, refine ⟨is_cycle_cycle_of _ (mem_support.mp hx), _⟩, intros y hy, rw mem_support at hy, rw cycle_of_apply, split_ifs with H, { refl }, { rw cycle_of_apply_of_not_same_cycle H at hy, contradiction } } end lemma mem_cycle_factors_finset_support_le {p f : perm α} (h : p ∈ cycle_factors_finset f) : p.support ≤ f.support := begin rw mem_cycle_factors_finset_iff at h, intros x hx, rwa [mem_support, ←h.right x hx, ←mem_support] end lemma cycle_factors_finset_eq_empty_iff {f : perm α} : cycle_factors_finset f = ∅ ↔ f = 1 := by simpa [cycle_factors_finset_eq_finset] using eq_comm @[simp] lemma cycle_factors_finset_one : cycle_factors_finset (1 : perm α) = ∅ := by simp [cycle_factors_finset_eq_empty_iff] @[simp] lemma cycle_factors_finset_eq_singleton_self_iff {f : perm α} : f.cycle_factors_finset = {f} ↔ f.is_cycle := by simp [cycle_factors_finset_eq_finset] lemma is_cycle.cycle_factors_finset_eq_singleton {f : perm α} (hf : is_cycle f) : f.cycle_factors_finset = {f} := cycle_factors_finset_eq_singleton_self_iff.mpr hf lemma cycle_factors_finset_eq_singleton_iff {f g : perm α} : f.cycle_factors_finset = {g} ↔ f.is_cycle ∧ f = g := begin suffices : f = g → (g.is_cycle ↔ f.is_cycle), { simpa [cycle_factors_finset_eq_finset, eq_comm] }, rintro rfl, exact iff.rfl end /-- Two permutations `f g : perm α` have the same cycle factors iff they are the same. -/ lemma cycle_factors_finset_injective : function.injective (@cycle_factors_finset α _ _) := begin intros f g h, rw ←cycle_factors_finset_noncomm_prod f, simpa [h] using cycle_factors_finset_noncomm_prod g end lemma disjoint.disjoint_cycle_factors_finset {f g : perm α} (h : disjoint f g) : _root_.disjoint (cycle_factors_finset f) (cycle_factors_finset g) := begin rw [disjoint_iff_disjoint_support] at h, rw finset.disjoint_left, intros x hx hy, simp only [mem_cycle_factors_finset_iff, mem_support] at hx hy, obtain ⟨⟨⟨a, ha, -⟩, hf⟩, -, hg⟩ := ⟨hx, hy⟩, refine h.le_bot (_ : a ∈ f.support ∩ g.support), simp [ha, ←hf a ha, ←hg a ha] end lemma disjoint.cycle_factors_finset_mul_eq_union {f g : perm α} (h : disjoint f g) : cycle_factors_finset (f * g) = cycle_factors_finset f ∪ cycle_factors_finset g := begin rw cycle_factors_finset_eq_finset, refine ⟨_, _, _⟩, { simp [or_imp_distrib, mem_cycle_factors_finset_iff, forall_swap] }, { rw [coe_union, set.pairwise_union_of_symmetric disjoint.symmetric], exact ⟨cycle_factors_finset_pairwise_disjoint _, cycle_factors_finset_pairwise_disjoint _, λ x hx y hy hxy, h.mono (mem_cycle_factors_finset_support_le hx) (mem_cycle_factors_finset_support_le hy)⟩ }, { rw noncomm_prod_union_of_disjoint h.disjoint_cycle_factors_finset, rw [cycle_factors_finset_noncomm_prod, cycle_factors_finset_noncomm_prod] } end lemma disjoint_mul_inv_of_mem_cycle_factors_finset {f g : perm α} (h : f ∈ cycle_factors_finset g) : disjoint (g * f⁻¹) f := begin rw mem_cycle_factors_finset_iff at h, intro x, by_cases hx : f x = x, { exact or.inr hx }, { refine or.inl _, rw [mul_apply, ←h.right, apply_inv_self], rwa [←support_inv, apply_mem_support, support_inv, mem_support] } end /-- If c is a cycle, a ∈ c.support and c is a cycle of f, then `c = f.cycle_of a` -/ lemma cycle_is_cycle_of {f c : equiv.perm α} {a : α} (ha : a ∈ c.support) (hc : c ∈ f.cycle_factors_finset) : c = f.cycle_of a := begin suffices : f.cycle_of a = c.cycle_of a, { rw this, apply symm, exact equiv.perm.is_cycle.cycle_of_eq ((equiv.perm.mem_cycle_factors_finset_iff.mp hc).left) (equiv.perm.mem_support.mp ha), }, let hfc := (equiv.perm.disjoint_mul_inv_of_mem_cycle_factors_finset hc).symm, let hfc2 := (perm.disjoint.commute hfc), rw ← equiv.perm.cycle_of_mul_of_apply_right_eq_self hfc2, simp only [hfc2.eq, inv_mul_cancel_right], -- a est dans le support de c, donc pas dans celui de g c⁻¹ exact equiv.perm.not_mem_support.mp (finset.disjoint_left.mp (equiv.perm.disjoint.disjoint_support hfc) ha), end end cycle_factors_finset @[elab_as_eliminator] lemma cycle_induction_on [finite β] (P : perm β → Prop) (σ : perm β) (base_one : P 1) (base_cycles : ∀ σ : perm β, σ.is_cycle → P σ) (induction_disjoint : ∀ σ τ : perm β, disjoint σ τ → is_cycle σ → P σ → P τ → P (σ * τ)) : P σ := begin casesI nonempty_fintype β, suffices : ∀ l : list (perm β), (∀ τ : perm β, τ ∈ l → τ.is_cycle) → l.pairwise disjoint → P l.prod, { classical, let x := σ.trunc_cycle_factors.out, exact (congr_arg P x.2.1).mp (this x.1 x.2.2.1 x.2.2.2) }, intro l, induction l with σ l ih, { exact λ _ _, base_one }, { intros h1 h2, rw list.prod_cons, exact induction_disjoint σ l.prod (disjoint_prod_right _ (list.pairwise_cons.mp h2).1) (h1 _ (list.mem_cons_self _ _)) (base_cycles σ (h1 σ (l.mem_cons_self σ))) (ih (λ τ hτ, h1 τ (list.mem_cons_of_mem σ hτ)) h2.of_cons) } end lemma cycle_factors_finset_mul_inv_mem_eq_sdiff [fintype α] {f g : perm α} (h : f ∈ cycle_factors_finset g) : cycle_factors_finset (g * f⁻¹) = (cycle_factors_finset g) \ {f} := begin revert f, apply cycle_induction_on _ g, { simp }, { intros σ hσ f hf, simp only [cycle_factors_finset_eq_singleton_self_iff.mpr hσ, mem_singleton] at hf ⊢, simp [hf] }, { intros σ τ hd hc hσ hτ f, simp_rw [hd.cycle_factors_finset_mul_eq_union, mem_union], -- if only `wlog` could work here... rintro (hf | hf), { rw [hd.commute.eq, union_comm, union_sdiff_distrib, sdiff_singleton_eq_erase, erase_eq_of_not_mem, mul_assoc, disjoint.cycle_factors_finset_mul_eq_union, hσ hf], { rw mem_cycle_factors_finset_iff at hf, intro x, cases hd.symm x with hx hx, { exact or.inl hx }, { refine or.inr _, by_cases hfx : f x = x, { rw ←hfx, simpa [hx] using hfx.symm }, { rw mul_apply, rw ←hf.right _ (mem_support.mpr hfx) at hx, contradiction } } }, { exact λ H, hd.disjoint_cycle_factors_finset.le_bot (mem_inter_of_mem hf H) } }, { rw [union_sdiff_distrib, sdiff_singleton_eq_erase, erase_eq_of_not_mem, mul_assoc, disjoint.cycle_factors_finset_mul_eq_union, hτ hf], { rw mem_cycle_factors_finset_iff at hf, intro x, cases hd x with hx hx, { exact or.inl hx }, { refine or.inr _, by_cases hfx : f x = x, { rw ←hfx, simpa [hx] using hfx.symm }, { rw mul_apply, rw ←hf.right _ (mem_support.mpr hfx) at hx, contradiction } } }, { exact λ H, hd.disjoint_cycle_factors_finset.le_bot (mem_inter_of_mem H hf) } } } end section generation variables [finite β] open subgroup lemma closure_is_cycle : closure {σ : perm β | is_cycle σ} = ⊤ := begin classical, casesI nonempty_fintype β, exact top_le_iff.mp (le_trans (ge_of_eq closure_is_swap) (closure_mono (λ _, is_swap.is_cycle))), end variables [fintype α] lemma closure_cycle_adjacent_swap {σ : perm α} (h1 : is_cycle σ) (h2 : σ.support = ⊤) (x : α) : closure ({σ, swap x (σ x)} : set (perm α)) = ⊤ := begin let H := closure ({σ, swap x (σ x)} : set (perm α)), have h3 : σ ∈ H := subset_closure (set.mem_insert σ _), have h4 : swap x (σ x) ∈ H := subset_closure (set.mem_insert_of_mem _ (set.mem_singleton _)), have step1 : ∀ (n : ℕ), swap ((σ ^ n) x) ((σ^(n+1)) x) ∈ H, { intro n, induction n with n ih, { exact subset_closure (set.mem_insert_of_mem _ (set.mem_singleton _)) }, { convert H.mul_mem (H.mul_mem h3 ih) (H.inv_mem h3), simp_rw [mul_swap_eq_swap_mul, mul_inv_cancel_right, pow_succ], refl } }, have step2 : ∀ (n : ℕ), swap x ((σ ^ n) x) ∈ H, { intro n, induction n with n ih, { convert H.one_mem, exact swap_self x }, { by_cases h5 : x = (σ ^ n) x, { rw [pow_succ, mul_apply, ←h5], exact h4 }, by_cases h6 : x = (σ^(n+1)) x, { rw [←h6, swap_self], exact H.one_mem }, rw [swap_comm, ←swap_mul_swap_mul_swap h5 h6], exact H.mul_mem (H.mul_mem (step1 n) ih) (step1 n) } }, have step3 : ∀ (y : α), swap x y ∈ H, { intro y, have hx : x ∈ (⊤ : finset α) := finset.mem_univ x, rw [←h2, mem_support] at hx, have hy : y ∈ (⊤ : finset α) := finset.mem_univ y, rw [←h2, mem_support] at hy, cases is_cycle.exists_pow_eq h1 hx hy with n hn, rw ← hn, exact step2 n }, have step4 : ∀ (y z : α), swap y z ∈ H, { intros y z, by_cases h5 : z = x, { rw [h5, swap_comm], exact step3 y }, by_cases h6 : z = y, { rw [h6, swap_self], exact H.one_mem }, rw [←swap_mul_swap_mul_swap h5 h6, swap_comm z x], exact H.mul_mem (H.mul_mem (step3 y) (step3 z)) (step3 y) }, rw [eq_top_iff, ←closure_is_swap, closure_le], rintros τ ⟨y, z, h5, h6⟩, rw h6, exact step4 y z, end lemma closure_cycle_coprime_swap {n : ℕ} {σ : perm α} (h0 : nat.coprime n (fintype.card α)) (h1 : is_cycle σ) (h2 : σ.support = finset.univ) (x : α) : closure ({σ, swap x ((σ ^ n) x)} : set (perm α)) = ⊤ := begin rw [←finset.card_univ, ←h2, ←h1.order_of] at h0, cases exists_pow_eq_self_of_coprime h0 with m hm, have h2' : (σ ^ n).support = ⊤ := eq.trans (support_pow_coprime h0) h2, have h1' : is_cycle ((σ ^ n) ^ (m : ℤ)) := by rwa ← hm at h1, replace h1' : is_cycle (σ ^ n) := h1'.of_pow (le_trans (support_pow_le σ n) (ge_of_eq (congr_arg support hm))), rw [eq_top_iff, ←closure_cycle_adjacent_swap h1' h2' x, closure_le, set.insert_subset], exact ⟨subgroup.pow_mem (closure _) (subset_closure (set.mem_insert σ _)) n, set.singleton_subset_iff.mpr (subset_closure (set.mem_insert_of_mem _ (set.mem_singleton _)))⟩, end lemma closure_prime_cycle_swap {σ τ : perm α} (h0 : (fintype.card α).prime) (h1 : is_cycle σ) (h2 : σ.support = finset.univ) (h3 : is_swap τ) : closure ({σ, τ} : set (perm α)) = ⊤ := begin obtain ⟨x, y, h4, h5⟩ := h3, obtain ⟨i, hi⟩ := h1.exists_pow_eq (mem_support.mp ((finset.ext_iff.mp h2 x).mpr (finset.mem_univ x))) (mem_support.mp ((finset.ext_iff.mp h2 y).mpr (finset.mem_univ y))), rw [h5, ←hi], refine closure_cycle_coprime_swap (nat.coprime.symm (h0.coprime_iff_not_dvd.mpr (λ h, h4 _))) h1 h2 x, cases h with m hm, rwa [hm, pow_mul, ←finset.card_univ, ←h2, ←h1.order_of, pow_order_of_eq_one, one_pow, one_apply] at hi, end end generation section variables [fintype α] {σ τ : perm α} noncomputable theory lemma is_conj_of_support_equiv (f : {x // x ∈ (σ.support : set α)} ≃ {x // x ∈ (τ.support : set α)}) (hf : ∀ (x : α) (hx : x ∈ (σ.support : set α)), (f ⟨σ x, apply_mem_support.2 hx⟩ : α) = τ ↑(f ⟨x,hx⟩)) : is_conj σ τ := begin refine is_conj_iff.2 ⟨equiv.extend_subtype f, _⟩, rw mul_inv_eq_iff_eq_mul, ext, simp only [perm.mul_apply], by_cases hx : x ∈ σ.support, { rw [equiv.extend_subtype_apply_of_mem, equiv.extend_subtype_apply_of_mem], { exact hf x (finset.mem_coe.2 hx) } }, { rwa [not_not.1 ((not_congr mem_support).1 (equiv.extend_subtype_not_mem f _ _)), not_not.1 ((not_congr mem_support).mp hx)] } end theorem is_cycle.is_conj (hσ : is_cycle σ) (hτ : is_cycle τ) (h : σ.support.card = τ.support.card) : is_conj σ τ := begin refine is_conj_of_support_equiv (hσ.zpowers_equiv_support.symm.trans $ (zpowers_equiv_zpowers $ by rw [hσ.order_of, h, hτ.order_of]).trans hτ.zpowers_equiv_support) _, intros x hx, simp only [perm.mul_apply, equiv.trans_apply, equiv.sum_congr_apply], obtain ⟨n, rfl⟩ := hσ.exists_pow_eq (classical.some_spec hσ).1 (mem_support.1 hx), apply eq.trans _ (congr rfl (congr rfl (congr rfl (congr rfl (hσ.zpowers_equiv_support_symm_apply n).symm)))), apply (congr rfl (congr rfl (congr rfl (hσ.zpowers_equiv_support_symm_apply (n + 1))))).trans _, simp only [ne.def, is_cycle.zpowers_equiv_support_apply, subtype.coe_mk, zpowers_equiv_zpowers_apply], rw [pow_succ, perm.mul_apply], end theorem is_cycle.is_conj_iff (hσ : is_cycle σ) (hτ : is_cycle τ) : is_conj σ τ ↔ σ.support.card = τ.support.card := ⟨begin intro h, obtain ⟨π, rfl⟩ := is_conj_iff.1 h, apply finset.card_congr (λ a ha, π a) (λ _ ha, _) (λ _ _ _ _ ab, π.injective ab) (λ b hb, _), { simp [mem_support.1 ha] }, { refine ⟨π⁻¹ b, ⟨_, π.apply_inv_self b⟩⟩, contrapose! hb, rw [mem_support, not_not] at hb, rw [mem_support, not_not, perm.mul_apply, perm.mul_apply, hb, perm.apply_inv_self] } end, hσ.is_conj hτ⟩ @[simp] lemma support_conj : (σ * τ * σ⁻¹).support = τ.support.map σ.to_embedding := begin ext, simp only [mem_map_equiv, perm.coe_mul, comp_app, ne.def, perm.mem_support, equiv.eq_symm_apply], refl, end lemma card_support_conj : (σ * τ * σ⁻¹).support.card = τ.support.card := by simp end theorem disjoint.is_conj_mul {α : Type*} [finite α] {σ τ π ρ : perm α} (hc1 : is_conj σ π) (hc2 : is_conj τ ρ) (hd1 : disjoint σ τ) (hd2 : disjoint π ρ) : is_conj (σ * τ) (π * ρ) := begin classical, casesI nonempty_fintype α, obtain ⟨f, rfl⟩ := is_conj_iff.1 hc1, obtain ⟨g, rfl⟩ := is_conj_iff.1 hc2, have hd1' := coe_inj.2 hd1.support_mul, have hd2' := coe_inj.2 hd2.support_mul, rw [coe_union] at *, have hd1'' := disjoint_coe.2 (disjoint_iff_disjoint_support.1 hd1), have hd2'' := disjoint_coe.2 (disjoint_iff_disjoint_support.1 hd2), refine is_conj_of_support_equiv _ _, { refine ((equiv.set.of_eq hd1').trans (equiv.set.union hd1''.le_bot)).trans ((equiv.sum_congr (subtype_equiv f (λ a, _)) (subtype_equiv g (λ a, _))).trans ((equiv.set.of_eq hd2').trans (equiv.set.union hd2''.le_bot)).symm); { simp only [set.mem_image, to_embedding_apply, exists_eq_right, support_conj, coe_map, apply_eq_iff_eq] } }, { intros x hx, simp only [trans_apply, symm_trans_apply, set.of_eq_apply, set.of_eq_symm_apply, equiv.sum_congr_apply], rw [hd1', set.mem_union] at hx, cases hx with hxσ hxτ, { rw [mem_coe, mem_support] at hxσ, rw [set.union_apply_left hd1''.le_bot _, set.union_apply_left hd1''.le_bot _], simp only [subtype_equiv_apply, perm.coe_mul, sum.map_inl, comp_app, set.union_symm_apply_left, subtype.coe_mk, apply_eq_iff_eq], { have h := (hd2 (f x)).resolve_left _, { rw [mul_apply, mul_apply] at h, rw [h, inv_apply_self, (hd1 x).resolve_left hxσ] }, { rwa [mul_apply, mul_apply, inv_apply_self, apply_eq_iff_eq] } }, { rwa [subtype.coe_mk, subtype.coe_mk, mem_coe, mem_support] }, { rwa [subtype.coe_mk, subtype.coe_mk, perm.mul_apply, (hd1 x).resolve_left hxσ, mem_coe, apply_mem_support, mem_support] } }, { rw [mem_coe, ← apply_mem_support, mem_support] at hxτ, rw [set.union_apply_right hd1''.le_bot _, set.union_apply_right hd1''.le_bot _], simp only [subtype_equiv_apply, perm.coe_mul, sum.map_inr, comp_app, set.union_symm_apply_right, subtype.coe_mk, apply_eq_iff_eq], { have h := (hd2 (g (τ x))).resolve_right _, { rw [mul_apply, mul_apply] at h, rw [inv_apply_self, h, (hd1 (τ x)).resolve_right hxτ] }, { rwa [mul_apply, mul_apply, inv_apply_self, apply_eq_iff_eq] } }, { rwa [subtype.coe_mk, subtype.coe_mk, mem_coe, ← apply_mem_support, mem_support] }, { rwa [subtype.coe_mk, subtype.coe_mk, perm.mul_apply, (hd1 (τ x)).resolve_right hxτ, mem_coe, mem_support] } } } end section fixed_points /-! ### Fixed points -/ lemma fixed_point_card_lt_of_ne_one [fintype α] {σ : perm α} (h : σ ≠ 1) : (filter (λ x, σ x = x) univ).card < fintype.card α - 1 := begin rw [lt_tsub_iff_left, ← lt_tsub_iff_right, ← finset.card_compl, finset.compl_filter], exact one_lt_card_support_of_ne_one h end end fixed_points end equiv.perm
0d00fd7ca744a4d3c094d25b6413dcdf67e136a6
43390109ab88557e6090f3245c47479c123ee500
/src/M1F/problem_bank/0505/Q0505.lean
50e517141aee803828eee845730d924da6583943
[ "Apache-2.0" ]
permissive
Ja1941/xena-UROP-2018
41f0956519f94d56b8bf6834a8d39473f4923200
b111fb87f343cf79eca3b886f99ee15c1dd9884b
refs/heads/master
1,662,355,955,139
1,590,577,325,000
1,590,577,325,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
201
lean
import algebra.group_power tactic.norm_num algebra.big_operators theorem Q5 : (¬ (∃ a b c : ℕ, 6*a+9*b+20*c = 43)) ∧ ∀ m, m ≥ 44 → ∃ a b c : ℕ, 6*a+9*b+20*c = m := sorry
2c9c2173ff23dffbe146fbdb0eb52bbcde8b601c
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/basics/unnamed_1388.lean
a24924ce73600204197e3d3077ba55734c858f6d
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
271
lean
import data.real.basic tactic variables a b : ℝ -- BEGIN example : 2*a*b ≤ a^2 + b^2 := begin have h : 0 ≤ a^2 - 2*a*b + b^2, calc a^2 - 2*a*b + b^2 = (a - b)^2 : by ring ... ≥ 0 : by apply pow_two_nonneg, linarith end -- END
b8c289add948e2d77580a3d219790bcbab6ab85d
a7602958ab456501ff85db8cf5553f7bcab201d7
/Notes/Logic_and_Proof/Chapter4/4.40.lean
f690c40fea2826cc718b6512254fda6523695cf2
[]
no_license
enlauren/math-logic
081e2e737c8afb28dbb337968df95ead47321ba0
086b6935543d1841f1db92d0e49add1124054c37
refs/heads/master
1,594,506,621,950
1,558,634,976,000
1,558,634,976,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
445
lean
-- 4.40 Forward Reasoning -- Lean provides the "have" command for forward reasoning. variables A B C : Prop variable h1: A -> B variable h2: B -> C -- Traditionally we might prove A -> C, as: example: A -> C := assume h, -- Implicitly, |h| is a proof of |A|. show C, from h2 (h1 h) -- However we can use "have", like this: example: A -> C := assume h: A, have h3: B, from h1 h, -- Build up a proof of B now (forward) now. show C, from h2 h3
dba6e8c670922790751e2da97f24c2b89073f74c
09b3e1beaeff2641ac75019c9f735d79d508071d
/Mathlib/Set.lean
913835940a256bd7971905b962d2b842a1aeab50
[ "Apache-2.0" ]
permissive
abentkamp/mathlib4
b819079cc46426b3c5c77413504b07541afacc19
f8294a67548f8f3d1f5913677b070a2ef5bcf120
refs/heads/master
1,685,309,252,764
1,623,232,534,000
1,623,232,534,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,339
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Mathlib.Mem /-! # Sets This file sets up the theory of sets whose elements have a given type. ## Main definitions Given a type `X` and a predicate `p : X → Prop`: * `Set X` : the type of sets whose elements have type `X` * `{a : X | p a} : Set X` : the set of all elements of `X` satisfying `p` * `{a | p a} : Set X` : a more concise notation for `{a : X | p a}` * `{a ∈ S | p a} : Set X` : given `S : Set X`, the subset of `S` consisting of its elements satisfying `p`. ## Implementation issues As in Lean 3, `Set X := X → Prop` I didn't call this file Data.Set.Basic because it contains core Lean 3 stuff which happens before mathlib3's data.set.basic . This file is a port of the core Lean 3 file `lib/lean/library/init/data/set.lean`. -/ universes u v def Set (α : Type u) := α → Prop def setOf {α : Type u} (p : α → Prop) : Set α := p class Subset (α : Type u) where subset : α → α → Prop infix:50 " ⊆ " => Subset.subset class Union (α : Type u) where union : α → α → α infixl:65 " ∪ " => Union.union class Inter (α : Type u) where inter : α → α → α infixl:70 " ∩ " => Inter.inter class Sdiff (α : Type u) where sdiff : α → α → α infix:70 " \\ " => Sdiff.sdiff namespace Set protected def mem (a : α) (s : Set α) := s a instance : Mem α (Set α) := ⟨Set.mem⟩ protected def subset (s₁ s₂ : Set α) := ∀ {a}, a ∈ s₁ → a ∈ s₂ instance : Subset (Set α) := ⟨Set.subset⟩ instance : EmptyCollection (Set α) := ⟨λ a => false⟩ declare_syntax_cat binderterm -- notation for `a` or `a : A` or `a ∈ S` syntax ident : binderterm syntax ident " : " term : binderterm syntax ident " ∈ " term : binderterm -- Notation for sets syntax "{ " binderterm " | " term " }" : term macro_rules -- {a : A | p a} | `({ $x:ident : $t | $p }) => `(setOf (λ ($x:ident : $t) => $p)) -- {a | p a} | `({ $x:ident | $p }) => `(setOf (λ ($x:ident) => $p)) -- {a ∈ s | p a} := {a | a ∈ s ∧ p a} | `({ $x:ident ∈ $s | $p }) => `(setOf (λ $x => $x ∈ $s ∧ $p)) syntax "∀" binderterm "," term : term syntax "∃" binderterm "," term : term macro_rules -- ∀ x ∈ s, p := ∀ x, x ∈ s → p | `(∀ $x:ident ∈ $s, $p) => `(∀ $x:ident, $x ∈ $s → $p) -- ∃ x ∈ s, p := ∃ x, x ∈ s ∧ p | `(∃ $x:ident ∈ $s, $p) => `(∃ $x:ident, $x ∈ $s ∧ $p) def univ : Set α := {a | True } protected def insert (a : α) (s : Set α) : Set α := {b | b = a ∨ b ∈ s} protected def singleton (a : α) : Set α := {b | b = a} protected def union (s₁ s₂ : Set α) : Set α := {a | a ∈ s₁ ∨ a ∈ s₂} instance : Union (Set α) := ⟨Set.union⟩ protected def inter (s₁ s₂ : Set α) : Set α := {a | a ∈ s₁ ∧ a ∈ s₂} instance : Inter (Set α) := ⟨Set.inter⟩ def compl (s : Set α) : Set α := {a | a ∉ s} protected def diff (s t : Set α) : Set α := {a ∈ s | a ∉ t} instance : Sdiff (Set α) := ⟨Set.diff⟩ def powerset (s : Set α) : Set (Set α) := {t | t ⊆ s} prefix:100 "𝒫" => powerset @[reducible] def sUnion (s : Set (Set α)) : Set α := {t | ∃ a ∈ s, t ∈ a} prefix:110 "⋃₀" => sUnion def image (f : α → β) (s : Set α) : Set β := {b | ∃ a, a ∈ s ∧ f a = b} instance : Functor Set := { map := @Set.image } instance : LawfulFunctor Set where id_map s := funext $ λ b => propext ⟨λ ⟨_, sb, rfl⟩ => sb, λ sb => ⟨_, sb, rfl⟩⟩ comp_map g h s := funext $ λ c => propext ⟨λ ⟨a, ⟨h₁, h₂⟩⟩ => ⟨g a, ⟨⟨a, ⟨h₁, rfl⟩⟩, h₂⟩⟩, λ ⟨b, ⟨⟨a, ⟨h₁, h₂⟩⟩, h₃⟩⟩ => ⟨a, ⟨h₁, show h (g a) = c from h₂ ▸ h₃⟩⟩⟩ map_const := rfl syntax (priority := high) "{" term,+ "}" : term macro_rules | `({$x}) => `(Set.singleton $x) | `({$x, $xs:term,*}) => `(Set.insert $x {$xs,*}) @[appUnexpander Set.singleton] def singletonUnexpander : Lean.PrettyPrinter.Unexpander | `(Set.singleton $a) => `({ $a }) | _ => throw () @[appUnexpander Set.insert] def insertUnexpander : Lean.PrettyPrinter.Unexpander | `(Set.insert $a { $ts,* }) => `({$a, $ts,*}) | _ => throw () end Set
82871588c561028e34f5a6f29e10a61840cc1522
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/measure_theory/measure/outer_measure.lean
39a686268f7f6646a422d0a55f98fdccc36e048a
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
61,699
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import analysis.specific_limits import measure_theory.pi_system import data.matrix.notation import topology.algebra.infinite_sum /-! # Outer Measures An outer measure is a function `μ : set α → ℝ≥0∞`, from the powerset of a type to the extended nonnegative real numbers that satisfies the following conditions: 1. `μ ∅ = 0`; 2. `μ` is monotone; 3. `μ` is countably subadditive. This means that the outer measure of a countable union is at most the sum of the outer measure on the individual sets. Note that we do not need `α` to be measurable to define an outer measure. The outer measures on a type `α` form a complete lattice. Given an arbitrary function `m : set α → ℝ≥0∞` that sends `∅` to `0` we can define an outer measure on `α` that on `s` is defined to be the infimum of `∑ᵢ, m (sᵢ)` for all collections of sets `sᵢ` that cover `s`. This is the unique maximal outer measure that is at most the given function. We also define this for functions `m` defined on a subset of `set α`, by treating the function as having value `∞` outside its domain. Given an outer measure `m`, the Carathéodory-measurable sets are the sets `s` such that for all sets `t` we have `m t = m (t ∩ s) + m (t \ s)`. This forms a measurable space. ## Main definitions and statements * `outer_measure.bounded_by` is the greatest outer measure that is at most the given function. If you know that the given functions sends `∅` to `0`, then `outer_measure.of_function` is a special case. * `caratheodory` is the Carathéodory-measurable space of an outer measure. * `Inf_eq_of_function_Inf_gen` is a characterization of the infimum of outer measures. * `induced_outer_measure` is the measure induced by a function on a subset of `set α` ## References * <https://en.wikipedia.org/wiki/Outer_measure> * <https://en.wikipedia.org/wiki/Carath%C3%A9odory%27s_criterion> ## Tags outer measure, Carathéodory-measurable, Carathéodory's criterion -/ noncomputable theory open set finset function filter encodable open_locale classical big_operators nnreal topological_space ennreal namespace measure_theory /-- An outer measure is a countably subadditive monotone function that sends `∅` to `0`. -/ structure outer_measure (α : Type*) := (measure_of : set α → ℝ≥0∞) (empty : measure_of ∅ = 0) (mono : ∀{s₁ s₂}, s₁ ⊆ s₂ → measure_of s₁ ≤ measure_of s₂) (Union_nat : ∀(s:ℕ → set α), measure_of (⋃i, s i) ≤ ∑'i, measure_of (s i)) namespace outer_measure section basic variables {α : Type*} {β : Type*} {ms : set (outer_measure α)} {m : outer_measure α} instance : has_coe_to_fun (outer_measure α) := ⟨_, λ m, m.measure_of⟩ @[simp] lemma measure_of_eq_coe (m : outer_measure α) : m.measure_of = m := rfl @[simp] theorem empty' (m : outer_measure α) : m ∅ = 0 := m.empty theorem mono' (m : outer_measure α) {s₁ s₂} (h : s₁ ⊆ s₂) : m s₁ ≤ m s₂ := m.mono h protected theorem Union (m : outer_measure α) {β} [encodable β] (s : β → set α) : m (⋃i, s i) ≤ ∑'i, m (s i) := rel_supr_tsum m m.empty (≤) m.Union_nat s lemma Union_null (m : outer_measure α) {β} [encodable β] {s : β → set α} (h : ∀ i, m (s i) = 0) : m (⋃i, s i) = 0 := by simpa [h] using m.Union s protected lemma Union_finset (m : outer_measure α) (s : β → set α) (t : finset β) : m (⋃i ∈ t, s i) ≤ ∑ i in t, m (s i) := rel_supr_sum m m.empty (≤) m.Union_nat s t protected lemma union (m : outer_measure α) (s₁ s₂ : set α) : m (s₁ ∪ s₂) ≤ m s₁ + m s₂ := rel_sup_add m m.empty (≤) m.Union_nat s₁ s₂ /-- If `s : ι → set α` is a sequence of sets, `S = ⋃ n, s n`, and `m (S \ s n)` tends to zero along some nontrivial filter (usually `at_top` on `α = ℕ`), then `m S = ⨆ n, m (s n)`. -/ lemma Union_of_tendsto_zero {ι} (m : outer_measure α) {s : ι → set α} (l : filter ι) [ne_bot l] (h0 : tendsto (λ k, m ((⋃ n, s n) \ s k)) l (𝓝 0)) : m (⋃ n, s n) = ⨆ n, m (s n) := begin set S := ⋃ n, s n, set M := ⨆ n, m (s n), have hsS : ∀ {k}, s k ⊆ S, from λ k, subset_Union _ _, refine le_antisymm _ (supr_le $ λ n, m.mono hsS), have A : ∀ k, m S ≤ M + m (S \ s k), from λ k, calc m S = m (s k ∪ S \ s k) : by rw [union_diff_self, union_eq_self_of_subset_left hsS] ... ≤ m (s k) + m (S \ s k) : m.union _ _ ... ≤ M + m (S \ s k) : add_le_add_right (le_supr _ k) _, have B : tendsto (λ k, M + m (S \ s k)) l (𝓝 (M + 0)), from tendsto_const_nhds.add h0, rw add_zero at B, exact ge_of_tendsto' B A end /-- If `s : ℕ → set α` is a monotone sequence of sets such that `∑' k, m (s (k + 1) \ s k) ≠ ∞`, then `m (⋃ n, s n) = ⨆ n, m (s n)`. -/ lemma Union_nat_of_monotone_of_tsum_ne_top (m : outer_measure α) {s : ℕ → set α} (h_mono : ∀ n, s n ⊆ s (n + 1)) (h0 : ∑' k, m (s (k + 1) \ s k) ≠ ∞) : m (⋃ n, s n) = ⨆ n, m (s n) := begin refine m.Union_of_tendsto_zero at_top _, refine tendsto_nhds_bot_mono' (ennreal.tendsto_sum_nat_add _ h0) (λ n, _), refine (m.mono _).trans (m.Union _), /- Current goal: `(⋃ k, s k) \ s n ⊆ ⋃ k, s (k + n + 1) \ s (k + n)` -/ have h' : monotone s := @monotone_nat_of_le_succ (set α) _ _ h_mono, simp only [diff_subset_iff, Union_subset_iff], intros i x hx, rcases nat.find_x ⟨i, hx⟩ with ⟨j, hj, hlt⟩, clear hx i, cases le_or_lt j n with hjn hnj, { exact or.inl (h' hjn hj) }, have : j - (n + 1) + n + 1 = j, by rw [add_assoc, nat.sub_add_cancel hnj], refine or.inr (mem_Union.2 ⟨j - (n + 1), _, hlt _ _⟩), { rwa this }, { rw [← nat.succ_le_iff, nat.succ_eq_add_one, this] } end lemma le_inter_add_diff {m : outer_measure α} {t : set α} (s : set α) : m t ≤ m (t ∩ s) + m (t \ s) := by { convert m.union _ _, rw inter_union_diff t s } lemma diff_null (m : outer_measure α) (s : set α) {t : set α} (ht : m t = 0) : m (s \ t) = m s := begin refine le_antisymm (m.mono $ diff_subset _ _) _, calc m s ≤ m (s ∩ t) + m (s \ t) : le_inter_add_diff _ ... ≤ m t + m (s \ t) : add_le_add_right (m.mono $ inter_subset_right _ _) _ ... = m (s \ t) : by rw [ht, zero_add] end lemma union_null (m : outer_measure α) {s₁ s₂ : set α} (h₁ : m s₁ = 0) (h₂ : m s₂ = 0) : m (s₁ ∪ s₂) = 0 := by simpa [h₁, h₂] using m.union s₁ s₂ lemma coe_fn_injective : injective (λ (μ : outer_measure α) (s : set α), μ s) := λ μ₁ μ₂ h, by { cases μ₁, cases μ₂, congr, exact h } @[ext] lemma ext {μ₁ μ₂ : outer_measure α} (h : ∀ s, μ₁ s = μ₂ s) : μ₁ = μ₂ := coe_fn_injective $ funext h /-- A version of `measure_theory.outer_measure.ext` that assumes `μ₁ s = μ₂ s` on all *nonempty* sets `s`, and gets `μ₁ ∅ = μ₂ ∅` from `measure_theory.outer_measure.empty'`. -/ lemma ext_nonempty {μ₁ μ₂ : outer_measure α} (h : ∀ s : set α, s.nonempty → μ₁ s = μ₂ s) : μ₁ = μ₂ := ext $ λ s, s.eq_empty_or_nonempty.elim (λ he, by rw [he, empty', empty']) (h s) instance : has_zero (outer_measure α) := ⟨{ measure_of := λ_, 0, empty := rfl, mono := assume _ _ _, le_refl 0, Union_nat := assume s, zero_le _ }⟩ @[simp] theorem coe_zero : ⇑(0 : outer_measure α) = 0 := rfl instance : inhabited (outer_measure α) := ⟨0⟩ instance : has_add (outer_measure α) := ⟨λm₁ m₂, { measure_of := λs, m₁ s + m₂ s, empty := show m₁ ∅ + m₂ ∅ = 0, by simp [outer_measure.empty], mono := assume s₁ s₂ h, add_le_add (m₁.mono h) (m₂.mono h), Union_nat := assume s, calc m₁ (⋃i, s i) + m₂ (⋃i, s i) ≤ (∑'i, m₁ (s i)) + (∑'i, m₂ (s i)) : add_le_add (m₁.Union_nat s) (m₂.Union_nat s) ... = _ : ennreal.tsum_add.symm}⟩ @[simp] theorem coe_add (m₁ m₂ : outer_measure α) : ⇑(m₁ + m₂) = m₁ + m₂ := rfl theorem add_apply (m₁ m₂ : outer_measure α) (s : set α) : (m₁ + m₂) s = m₁ s + m₂ s := rfl instance add_comm_monoid : add_comm_monoid (outer_measure α) := { zero := 0, add := (+), .. injective.add_comm_monoid (show outer_measure α → set α → ℝ≥0∞, from coe_fn) coe_fn_injective rfl (λ _ _, rfl) } instance : has_scalar ℝ≥0∞ (outer_measure α) := ⟨λ c m, { measure_of := λ s, c * m s, empty := by simp, mono := λ s t h, ennreal.mul_left_mono $ m.mono h, Union_nat := λ s, by { rw [ennreal.tsum_mul_left], exact ennreal.mul_left_mono (m.Union _) } }⟩ @[simp] lemma coe_smul (c : ℝ≥0∞) (m : outer_measure α) : ⇑(c • m) = c • m := rfl lemma smul_apply (c : ℝ≥0∞) (m : outer_measure α) (s : set α) : (c • m) s = c * m s := rfl instance : module ℝ≥0∞ (outer_measure α) := { smul := (•), .. injective.module ℝ≥0∞ ⟨show outer_measure α → set α → ℝ≥0∞, from coe_fn, coe_zero, coe_add⟩ coe_fn_injective coe_smul } instance : has_bot (outer_measure α) := ⟨0⟩ instance outer_measure.order_bot : order_bot (outer_measure α) := { le := λm₁ m₂, ∀s, m₁ s ≤ m₂ s, bot := 0, le_refl := assume a s, le_refl _, le_trans := assume a b c hab hbc s, le_trans (hab s) (hbc s), le_antisymm := assume a b hab hba, ext $ assume s, le_antisymm (hab s) (hba s), bot_le := assume a s, zero_le _ } section supremum instance : has_Sup (outer_measure α) := ⟨λms, { measure_of := λs, ⨆ m ∈ ms, (m : outer_measure α) s, empty := nonpos_iff_eq_zero.1 $ bsupr_le $ λ m h, le_of_eq m.empty, mono := assume s₁ s₂ hs, bsupr_le_bsupr $ assume m hm, m.mono hs, Union_nat := assume f, bsupr_le $ assume m hm, calc m (⋃i, f i) ≤ ∑' (i : ℕ), m (f i) : m.Union_nat _ ... ≤ ∑'i, (⨆ m ∈ ms, (m : outer_measure α) (f i)) : ennreal.tsum_le_tsum $ assume i, le_bsupr m hm }⟩ instance : complete_lattice (outer_measure α) := { .. outer_measure.order_bot, .. complete_lattice_of_Sup (outer_measure α) (λ ms, ⟨λ m hm s, le_bsupr m hm, λ m hm s, bsupr_le (λ m' hm', hm hm' s)⟩) } @[simp] theorem Sup_apply (ms : set (outer_measure α)) (s : set α) : (Sup ms) s = ⨆ m ∈ ms, (m : outer_measure α) s := rfl @[simp] theorem supr_apply {ι} (f : ι → outer_measure α) (s : set α) : (⨆ i : ι, f i) s = ⨆ i, f i s := by rw [supr, Sup_apply, supr_range, supr] @[norm_cast] theorem coe_supr {ι} (f : ι → outer_measure α) : ⇑(⨆ i, f i) = ⨆ i, f i := funext $ λ s, by rw [supr_apply, _root_.supr_apply] @[simp] theorem sup_apply (m₁ m₂ : outer_measure α) (s : set α) : (m₁ ⊔ m₂) s = m₁ s ⊔ m₂ s := by have := supr_apply (λ b, cond b m₁ m₂) s; rwa [supr_bool_eq, supr_bool_eq] at this theorem smul_supr {ι} (f : ι → outer_measure α) (c : ℝ≥0∞) : c • (⨆ i, f i) = ⨆ i, c • f i := ext $ λ s, by simp only [smul_apply, supr_apply, ennreal.mul_supr] end supremum @[mono] lemma mono'' {m₁ m₂ : outer_measure α} {s₁ s₂ : set α} (hm : m₁ ≤ m₂) (hs : s₁ ⊆ s₂) : m₁ s₁ ≤ m₂ s₂ := (hm s₁).trans (m₂.mono hs) /-- The pushforward of `m` along `f`. The outer measure on `s` is defined to be `m (f ⁻¹' s)`. -/ def map {β} (f : α → β) : outer_measure α →ₗ[ℝ≥0∞] outer_measure β := { to_fun := λ m, { measure_of := λs, m (f ⁻¹' s), empty := m.empty, mono := λ s t h, m.mono (preimage_mono h), Union_nat := λ s, by rw [preimage_Union]; exact m.Union_nat (λ i, f ⁻¹' s i) }, map_add' := λ m₁ m₂, coe_fn_injective rfl, map_smul' := λ c m, coe_fn_injective rfl } @[simp] theorem map_apply {β} (f : α → β) (m : outer_measure α) (s : set β) : map f m s = m (f ⁻¹' s) := rfl @[simp] theorem map_id (m : outer_measure α) : map id m = m := ext $ λ s, rfl @[simp] theorem map_map {β γ} (f : α → β) (g : β → γ) (m : outer_measure α) : map g (map f m) = map (g ∘ f) m := ext $ λ s, rfl @[mono] theorem map_mono {β} (f : α → β) : monotone (map f) := λ m m' h s, h _ @[simp] theorem map_sup {β} (f : α → β) (m m' : outer_measure α) : map f (m ⊔ m') = map f m ⊔ map f m' := ext $ λ s, by simp only [map_apply, sup_apply] @[simp] theorem map_supr {β ι} (f : α → β) (m : ι → outer_measure α) : map f (⨆ i, m i) = ⨆ i, map f (m i) := ext $ λ s, by simp only [map_apply, supr_apply] instance : functor outer_measure := {map := λ α β f, map f} instance : is_lawful_functor outer_measure := { id_map := λ α, map_id, comp_map := λ α β γ f g m, (map_map f g m).symm } /-- The dirac outer measure. -/ def dirac (a : α) : outer_measure α := { measure_of := λs, indicator s (λ _, 1) a, empty := by simp, mono := λ s t h, indicator_le_indicator_of_subset h (λ _, zero_le _) a, Union_nat := λ s, if hs : a ∈ ⋃ n, s n then let ⟨i, hi⟩ := mem_Union.1 hs in calc indicator (⋃ n, s n) (λ _, (1 : ℝ≥0∞)) a = 1 : indicator_of_mem hs _ ... = indicator (s i) (λ _, 1) a : (indicator_of_mem hi _).symm ... ≤ ∑' n, indicator (s n) (λ _, 1) a : ennreal.le_tsum _ else by simp only [indicator_of_not_mem hs, zero_le]} @[simp] theorem dirac_apply (a : α) (s : set α) : dirac a s = indicator s (λ _, 1) a := rfl /-- The sum of an (arbitrary) collection of outer measures. -/ def sum {ι} (f : ι → outer_measure α) : outer_measure α := { measure_of := λs, ∑' i, f i s, empty := by simp, mono := λ s t h, ennreal.tsum_le_tsum (λ i, (f i).mono' h), Union_nat := λ s, by rw ennreal.tsum_comm; exact ennreal.tsum_le_tsum (λ i, (f i).Union_nat _) } @[simp] theorem sum_apply {ι} (f : ι → outer_measure α) (s : set α) : sum f s = ∑' i, f i s := rfl theorem smul_dirac_apply (a : ℝ≥0∞) (b : α) (s : set α) : (a • dirac b) s = indicator s (λ _, a) b := by simp only [smul_apply, dirac_apply, ← indicator_mul_right _ (λ _, a), mul_one] /-- Pullback of an `outer_measure`: `comap f μ s = μ (f '' s)`. -/ def comap {β} (f : α → β) : outer_measure β →ₗ[ℝ≥0∞] outer_measure α := { to_fun := λ m, { measure_of := λ s, m (f '' s), empty := by simp, mono := λ s t h, m.mono $ image_subset f h, Union_nat := λ s, by { rw [image_Union], apply m.Union_nat } }, map_add' := λ m₁ m₂, rfl, map_smul' := λ c m, rfl } @[simp] lemma comap_apply {β} (f : α → β) (m : outer_measure β) (s : set α) : comap f m s = m (f '' s) := rfl @[mono] lemma comap_mono {β} (f : α → β) : monotone (comap f) := λ m m' h s, h _ @[simp] theorem comap_supr {β ι} (f : α → β) (m : ι → outer_measure β) : comap f (⨆ i, m i) = ⨆ i, comap f (m i) := ext $ λ s, by simp only [comap_apply, supr_apply] /-- Restrict an `outer_measure` to a set. -/ def restrict (s : set α) : outer_measure α →ₗ[ℝ≥0∞] outer_measure α := (map coe).comp (comap (coe : s → α)) @[simp] lemma restrict_apply (s t : set α) (m : outer_measure α) : restrict s m t = m (t ∩ s) := by simp [restrict] @[mono] lemma restrict_mono {s t : set α} (h : s ⊆ t) {m m' : outer_measure α} (hm : m ≤ m') : restrict s m ≤ restrict t m' := λ u, by { simp only [restrict_apply], exact (hm _).trans (m'.mono $ inter_subset_inter_right _ h) } @[simp] lemma restrict_univ (m : outer_measure α) : restrict univ m = m := ext $ λ s, by simp @[simp] lemma restrict_empty (m : outer_measure α) : restrict ∅ m = 0 := ext $ λ s, by simp @[simp] lemma restrict_supr {ι} (s : set α) (m : ι → outer_measure α) : restrict s (⨆ i, m i) = ⨆ i, restrict s (m i) := by simp [restrict] lemma map_comap {β} (f : α → β) (m : outer_measure β) : map f (comap f m) = restrict (range f) m := ext $ λ s, congr_arg m $ by simp only [image_preimage_eq_inter_range, subtype.range_coe] lemma map_comap_le {β} (f : α → β) (m : outer_measure β) : map f (comap f m) ≤ m := λ s, m.mono $ image_preimage_subset _ _ lemma restrict_le_self (m : outer_measure α) (s : set α) : restrict s m ≤ m := map_comap_le _ _ @[simp] lemma map_le_restrict_range {β} {ma : outer_measure α} {mb : outer_measure β} {f : α → β} : map f ma ≤ restrict (range f) mb ↔ map f ma ≤ mb := ⟨λ h, h.trans (restrict_le_self _ _), λ h s, by simpa using h (s ∩ range f)⟩ lemma map_comap_of_surjective {β} {f : α → β} (hf : surjective f) (m : outer_measure β) : map f (comap f m) = m := ext $ λ s, by rw [map_apply, comap_apply, hf.image_preimage] lemma le_comap_map {β} (f : α → β) (m : outer_measure α) : m ≤ comap f (map f m) := λ s, m.mono $ subset_preimage_image _ _ lemma comap_map {β} {f : α → β} (hf : injective f) (m : outer_measure α) : comap f (map f m) = m := ext $ λ s, by rw [comap_apply, map_apply, hf.preimage_image] @[simp] theorem top_apply {s : set α} (h : s.nonempty) : (⊤ : outer_measure α) s = ∞ := let ⟨a, as⟩ := h in top_unique $ le_trans (by simp [smul_dirac_apply, as]) (le_bsupr (∞ • dirac a) trivial) theorem top_apply' (s : set α) : (⊤ : outer_measure α) s = ⨅ (h : s = ∅), 0 := s.eq_empty_or_nonempty.elim (λ h, by simp [h]) (λ h, by simp [h, h.ne_empty]) @[simp] theorem comap_top (f : α → β) : comap f ⊤ = ⊤ := ext_nonempty $ λ s hs, by rw [comap_apply, top_apply hs, top_apply (hs.image _)] theorem map_top (f : α → β) : map f ⊤ = restrict (range f) ⊤ := ext $ λ s, by rw [map_apply, restrict_apply, ← image_preimage_eq_inter_range, top_apply', top_apply', set.image_eq_empty] theorem map_top_of_surjective (f : α → β) (hf : surjective f) : map f ⊤ = ⊤ := by rw [map_top, hf.range_eq, restrict_univ] end basic section of_function set_option eqn_compiler.zeta true variables {α : Type*} (m : set α → ℝ≥0∞) (m_empty : m ∅ = 0) include m_empty /-- Given any function `m` assigning measures to sets satisying `m ∅ = 0`, there is a unique maximal outer measure `μ` satisfying `μ s ≤ m s` for all `s : set α`. -/ protected def of_function : outer_measure α := let μ := λs, ⨅{f : ℕ → set α} (h : s ⊆ ⋃i, f i), ∑'i, m (f i) in { measure_of := μ, empty := le_antisymm (infi_le_of_le (λ_, ∅) $ infi_le_of_le (empty_subset _) $ by simp [m_empty]) (zero_le _), mono := assume s₁ s₂ hs, infi_le_infi $ assume f, infi_le_infi2 $ assume hb, ⟨subset.trans hs hb, le_refl _⟩, Union_nat := assume s, ennreal.le_of_forall_pos_le_add $ begin assume ε hε (hb : ∑'i, μ (s i) < ∞), rcases ennreal.exists_pos_sum_of_encodable (ennreal.coe_pos.2 hε).ne' ℕ with ⟨ε', hε', hl⟩, refine le_trans _ (add_le_add_left (le_of_lt hl) _), rw ← ennreal.tsum_add, choose f hf using show ∀i, ∃f:ℕ → set α, s i ⊆ (⋃i, f i) ∧ ∑'i, m (f i) < μ (s i) + ε' i, { intro, have : μ (s i) < μ (s i) + ε' i := ennreal.lt_add_right (ne_top_of_le_ne_top hb.ne $ ennreal.le_tsum _) (by simpa using (hε' i).ne'), simpa [μ, infi_lt_iff] }, refine le_trans _ (ennreal.tsum_le_tsum $ λ i, le_of_lt (hf i).2), rw [← ennreal.tsum_prod, ← equiv.nat_prod_nat_equiv_nat.symm.tsum_eq], swap, {apply_instance}, refine infi_le_of_le _ (infi_le _ _), exact Union_subset (λ i, subset.trans (hf i).1 $ Union_subset $ λ j, subset.trans (by simp) $ subset_Union _ $ equiv.nat_prod_nat_equiv_nat (i, j)), end } lemma of_function_apply (s : set α) : outer_measure.of_function m m_empty s = (⨅ (t : ℕ → set α) (h : s ⊆ Union t), ∑' n, m (t n)) := rfl variables {m m_empty} theorem of_function_le (s : set α) : outer_measure.of_function m m_empty s ≤ m s := let f : ℕ → set α := λi, nat.cases_on i s (λ _, ∅) in infi_le_of_le f $ infi_le_of_le (subset_Union f 0) $ le_of_eq $ tsum_eq_single 0 $ by rintro (_|i); simp [f, m_empty] theorem of_function_eq (s : set α) (m_mono : ∀ ⦃t : set α⦄, s ⊆ t → m s ≤ m t) (m_subadd : ∀ (s : ℕ → set α), m (⋃i, s i) ≤ ∑'i, m (s i)) : outer_measure.of_function m m_empty s = m s := le_antisymm (of_function_le s) $ le_infi $ λ f, le_infi $ λ hf, le_trans (m_mono hf) (m_subadd f) theorem le_of_function {μ : outer_measure α} : μ ≤ outer_measure.of_function m m_empty ↔ ∀ s, μ s ≤ m s := ⟨λ H s, le_trans (H s) (of_function_le s), λ H s, le_infi $ λ f, le_infi $ λ hs, le_trans (μ.mono hs) $ le_trans (μ.Union f) $ ennreal.tsum_le_tsum $ λ i, H _⟩ lemma is_greatest_of_function : is_greatest {μ : outer_measure α | ∀ s, μ s ≤ m s} (outer_measure.of_function m m_empty) := ⟨λ s, of_function_le _, λ μ, le_of_function.2⟩ lemma of_function_eq_Sup : outer_measure.of_function m m_empty = Sup {μ | ∀ s, μ s ≤ m s} := (@is_greatest_of_function α m m_empty).is_lub.Sup_eq.symm /-- If `m u = ∞` for any set `u` that has nonempty intersection both with `s` and `t`, then `μ (s ∪ t) = μ s + μ t`, where `μ = measure_theory.outer_measure.of_function m m_empty`. E.g., if `α` is an (e)metric space and `m u = ∞` on any set of diameter `≥ r`, then this lemma implies that `μ (s ∪ t) = μ s + μ t` on any two sets such that `r ≤ edist x y` for all `x ∈ s` and `y ∈ t`. -/ lemma of_function_union_of_top_of_nonempty_inter {s t : set α} (h : ∀ u, (s ∩ u).nonempty → (t ∩ u).nonempty → m u = ∞) : outer_measure.of_function m m_empty (s ∪ t) = outer_measure.of_function m m_empty s + outer_measure.of_function m m_empty t := begin refine le_antisymm (outer_measure.union _ _ _) (le_infi $ λ f, le_infi $ λ hf, _), set μ := outer_measure.of_function m m_empty, rcases em (∃ i, (s ∩ f i).nonempty ∧ (t ∩ f i).nonempty) with ⟨i, hs, ht⟩|he, { calc μ s + μ t ≤ ∞ : le_top ... = m (f i) : (h (f i) hs ht).symm ... ≤ ∑' i, m (f i) : ennreal.le_tsum i }, set I := λ s, {i : ℕ | (s ∩ f i).nonempty}, have hd : disjoint (I s) (I t), from λ i hi, he ⟨i, hi⟩, have hI : ∀ u ⊆ s ∪ t, μ u ≤ ∑' i : I u, μ (f i), from λ u hu, calc μ u ≤ μ (⋃ i : I u, f i) : μ.mono (λ x hx, let ⟨i, hi⟩ := mem_Union.1 (hf (hu hx)) in mem_Union.2 ⟨⟨i, ⟨x, hx, hi⟩⟩, hi⟩) ... ≤ ∑' i : I u, μ (f i) : μ.Union _, calc μ s + μ t ≤ (∑' i : I s, μ (f i)) + (∑' i : I t, μ (f i)) : add_le_add (hI _ $ subset_union_left _ _) (hI _ $ subset_union_right _ _) ... = ∑' i : I s ∪ I t, μ (f i) : (@tsum_union_disjoint _ _ _ _ _ (λ i, μ (f i)) _ _ _ hd ennreal.summable ennreal.summable).symm ... ≤ ∑' i, μ (f i) : tsum_le_tsum_of_inj coe subtype.coe_injective (λ _ _, zero_le _) (λ _, le_rfl) ennreal.summable ennreal.summable ... ≤ ∑' i, m (f i) : ennreal.tsum_le_tsum (λ i, of_function_le _) end lemma comap_of_function {β} (f : β → α) (h : monotone m ∨ surjective f) : comap f (outer_measure.of_function m m_empty) = outer_measure.of_function (λ s, m (f '' s)) (by rwa set.image_empty) := begin refine le_antisymm (le_of_function.2 $ λ s, _) (λ s, _), { rw comap_apply, apply of_function_le }, { rw [comap_apply, of_function_apply, of_function_apply], refine infi_le_infi2 (λ t, ⟨λ k, f ⁻¹' (t k), _⟩), refine infi_le_infi2 (λ ht, _), rw [set.image_subset_iff, preimage_Union] at ht, refine ⟨ht, ennreal.tsum_le_tsum $ λ n, _⟩, cases h, exacts [h (image_preimage_subset _ _), (congr_arg m (h.image_preimage (t n))).le] } end lemma map_of_function_le {β} (f : α → β) : map f (outer_measure.of_function m m_empty) ≤ outer_measure.of_function (λ s, m (f ⁻¹' s)) m_empty := le_of_function.2 $ λ s, by { rw map_apply, apply of_function_le } lemma map_of_function {β} {f : α → β} (hf : injective f) : map f (outer_measure.of_function m m_empty) = outer_measure.of_function (λ s, m (f ⁻¹' s)) m_empty := begin refine (map_of_function_le _).antisymm (λ s, _), simp only [of_function_apply, map_apply, le_infi_iff], intros t ht, refine infi_le_of_le (λ n, (range f)ᶜ ∪ f '' (t n)) (infi_le_of_le _ _), { rw [← union_Union, ← inter_subset, ← image_preimage_eq_inter_range, ← image_Union], exact image_subset _ ht }, { refine ennreal.tsum_le_tsum (λ n, le_of_eq _), simp [hf.preimage_image] } end lemma restrict_of_function (s : set α) (hm : monotone m) : restrict s (outer_measure.of_function m m_empty) = outer_measure.of_function (λ t, m (t ∩ s)) (by rwa set.empty_inter) := by simp only [restrict, linear_map.comp_apply, comap_of_function _ (or.inl hm), map_of_function subtype.coe_injective, subtype.image_preimage_coe] lemma smul_of_function {c : ℝ≥0∞} (hc : c ≠ ∞) : c • outer_measure.of_function m m_empty = outer_measure.of_function (c • m) (by simp [m_empty]) := begin ext1 s, haveI : nonempty {t : ℕ → set α // s ⊆ ⋃ i, t i} := ⟨⟨λ _, s, subset_Union (λ _, s) 0⟩⟩, simp only [smul_apply, of_function_apply, ennreal.tsum_mul_left, pi.smul_apply, smul_eq_mul, infi_subtype', ennreal.infi_mul_left (λ h, (hc h).elim)], end end of_function section bounded_by variables {α : Type*} (m : set α → ℝ≥0∞) /-- Given any function `m` assigning measures to sets, there is a unique maximal outer measure `μ` satisfying `μ s ≤ m s` for all `s : set α`. This is the same as `outer_measure.of_function`, except that it doesn't require `m ∅ = 0`. -/ def bounded_by : outer_measure α := outer_measure.of_function (λ s, ⨆ (h : s.nonempty), m s) (by simp [empty_not_nonempty]) variables {m} theorem bounded_by_le (s : set α) : bounded_by m s ≤ m s := (of_function_le _).trans supr_const_le theorem bounded_by_eq_of_function (m_empty : m ∅ = 0) (s : set α) : bounded_by m s = outer_measure.of_function m m_empty s := begin have : (λ s : set α, ⨆ (h : s.nonempty), m s) = m, { ext1 t, cases t.eq_empty_or_nonempty with h h; simp [h, empty_not_nonempty, m_empty] }, simp [bounded_by, this] end theorem bounded_by_apply (s : set α) : bounded_by m s = ⨅ (t : ℕ → set α) (h : s ⊆ Union t), ∑' n, ⨆ (h : (t n).nonempty), m (t n) := by simp [bounded_by, of_function_apply] theorem bounded_by_eq (s : set α) (m_empty : m ∅ = 0) (m_mono : ∀ ⦃t : set α⦄, s ⊆ t → m s ≤ m t) (m_subadd : ∀ (s : ℕ → set α), m (⋃i, s i) ≤ ∑'i, m (s i)) : bounded_by m s = m s := by rw [bounded_by_eq_of_function m_empty, of_function_eq s m_mono m_subadd] @[simp] theorem bounded_by_eq_self (m : outer_measure α) : bounded_by m = m := ext $ λ s, bounded_by_eq _ m.empty' (λ t ht, m.mono' ht) m.Union theorem le_bounded_by {μ : outer_measure α} : μ ≤ bounded_by m ↔ ∀ s, μ s ≤ m s := begin rw [bounded_by, le_of_function, forall_congr], intro s, cases s.eq_empty_or_nonempty with h h; simp [h, empty_not_nonempty] end theorem le_bounded_by' {μ : outer_measure α} : μ ≤ bounded_by m ↔ ∀ s : set α, s.nonempty → μ s ≤ m s := by { rw [le_bounded_by, forall_congr], intro s, cases s.eq_empty_or_nonempty with h h; simp [h] } lemma smul_bounded_by {c : ℝ≥0∞} (hc : c ≠ ∞) : c • bounded_by m = bounded_by (c • m) := begin simp only [bounded_by, smul_of_function hc], congr' 1 with s : 1, rcases s.eq_empty_or_nonempty with rfl|hs; simp * end lemma comap_bounded_by {β} (f : β → α) (h : monotone (λ s : {s : set α // s.nonempty}, m s) ∨ surjective f) : comap f (bounded_by m) = bounded_by (λ s, m (f '' s)) := begin refine (comap_of_function _ _).trans _, { refine h.imp (λ H s t hst, supr_le $ λ hs, _) id, have ht : t.nonempty := hs.mono hst, exact (@H ⟨s, hs⟩ ⟨t, ht⟩ hst).trans (le_supr (λ h : t.nonempty, m t) ht) }, { dunfold bounded_by, congr' with s : 1, rw nonempty_image_iff } end /-- If `m u = ∞` for any set `u` that has nonempty intersection both with `s` and `t`, then `μ (s ∪ t) = μ s + μ t`, where `μ = measure_theory.outer_measure.bounded_by m`. E.g., if `α` is an (e)metric space and `m u = ∞` on any set of diameter `≥ r`, then this lemma implies that `μ (s ∪ t) = μ s + μ t` on any two sets such that `r ≤ edist x y` for all `x ∈ s` and `y ∈ t`. -/ lemma bounded_by_union_of_top_of_nonempty_inter {s t : set α} (h : ∀ u, (s ∩ u).nonempty → (t ∩ u).nonempty → m u = ∞) : bounded_by m (s ∪ t) = bounded_by m s + bounded_by m t := of_function_union_of_top_of_nonempty_inter $ λ u hs ht, top_unique $ (h u hs ht).ge.trans $ le_supr (λ h, m u) (hs.mono $ inter_subset_right s u) end bounded_by section caratheodory_measurable universe u parameters {α : Type u} (m : outer_measure α) include m local attribute [simp] set.inter_comm set.inter_left_comm set.inter_assoc variables {s s₁ s₂ : set α} /-- A set `s` is Carathéodory-measurable for an outer measure `m` if for all sets `t` we have `m t = m (t ∩ s) + m (t \ s)`. -/ def is_caratheodory (s : set α) : Prop := ∀t, m t = m (t ∩ s) + m (t \ s) lemma is_caratheodory_iff_le' {s : set α} : is_caratheodory s ↔ ∀t, m (t ∩ s) + m (t \ s) ≤ m t := forall_congr $ λ t, le_antisymm_iff.trans $ and_iff_right $ le_inter_add_diff _ @[simp] lemma is_caratheodory_empty : is_caratheodory ∅ := by simp [is_caratheodory, m.empty, diff_empty] lemma is_caratheodory_compl : is_caratheodory s₁ → is_caratheodory s₁ᶜ := by simp [is_caratheodory, diff_eq, add_comm] @[simp] lemma is_caratheodory_compl_iff : is_caratheodory sᶜ ↔ is_caratheodory s := ⟨λ h, by simpa using is_caratheodory_compl m h, is_caratheodory_compl⟩ lemma is_caratheodory_union (h₁ : is_caratheodory s₁) (h₂ : is_caratheodory s₂) : is_caratheodory (s₁ ∪ s₂) := λ t, begin rw [h₁ t, h₂ (t ∩ s₁), h₂ (t \ s₁), h₁ (t ∩ (s₁ ∪ s₂)), inter_diff_assoc _ _ s₁, set.inter_assoc _ _ s₁, inter_eq_self_of_subset_right (set.subset_union_left _ _), union_diff_left, h₂ (t ∩ s₁)], simp [diff_eq, add_assoc] end lemma measure_inter_union (h : s₁ ∩ s₂ ⊆ ∅) (h₁ : is_caratheodory s₁) {t : set α} : m (t ∩ (s₁ ∪ s₂)) = m (t ∩ s₁) + m (t ∩ s₂) := by rw [h₁, set.inter_assoc, set.union_inter_cancel_left, inter_diff_assoc, union_diff_cancel_left h] lemma is_caratheodory_Union_lt {s : ℕ → set α} : ∀{n:ℕ}, (∀i<n, is_caratheodory (s i)) → is_caratheodory (⋃i<n, s i) | 0 h := by simp [nat.not_lt_zero] | (n + 1) h := by rw bUnion_lt_succ; exact is_caratheodory_union m (is_caratheodory_Union_lt $ assume i hi, h i $ lt_of_lt_of_le hi $ nat.le_succ _) (h n (le_refl (n + 1))) lemma is_caratheodory_inter (h₁ : is_caratheodory s₁) (h₂ : is_caratheodory s₂) : is_caratheodory (s₁ ∩ s₂) := by { rw [← is_caratheodory_compl_iff, compl_inter], exact is_caratheodory_union _ (is_caratheodory_compl _ h₁) (is_caratheodory_compl _ h₂) } lemma is_caratheodory_sum {s : ℕ → set α} (h : ∀i, is_caratheodory (s i)) (hd : pairwise (disjoint on s)) {t : set α} : ∀ {n}, ∑ i in finset.range n, m (t ∩ s i) = m (t ∩ ⋃i<n, s i) | 0 := by simp [nat.not_lt_zero, m.empty] | (nat.succ n) := begin rw [bUnion_lt_succ, finset.sum_range_succ, set.union_comm, is_caratheodory_sum, m.measure_inter_union _ (h n), add_comm], intro a, simpa using λ (h₁ : a ∈ s n) i (hi : i < n) h₂, hd _ _ (ne_of_gt hi) ⟨h₁, h₂⟩ end lemma is_caratheodory_Union_nat {s : ℕ → set α} (h : ∀i, is_caratheodory (s i)) (hd : pairwise (disjoint on s)) : is_caratheodory (⋃i, s i) := is_caratheodory_iff_le'.2 $ λ t, begin have hp : m (t ∩ ⋃i, s i) ≤ (⨆n, m (t ∩ ⋃i<n, s i)), { convert m.Union (λ i, t ∩ s i), { rw inter_Union }, { simp [ennreal.tsum_eq_supr_nat, is_caratheodory_sum m h hd] } }, refine le_trans (add_le_add_right hp _) _, rw ennreal.supr_add, refine supr_le (λ n, le_trans (add_le_add_left _ _) (ge_of_eq (is_caratheodory_Union_lt m (λ i _, h i) _))), refine m.mono (diff_subset_diff_right _), exact bUnion_subset (λ i _, subset_Union _ i), end lemma f_Union {s : ℕ → set α} (h : ∀i, is_caratheodory (s i)) (hd : pairwise (disjoint on s)) : m (⋃i, s i) = ∑'i, m (s i) := begin refine le_antisymm (m.Union_nat s) _, rw ennreal.tsum_eq_supr_nat, refine supr_le (λ n, _), have := @is_caratheodory_sum _ m _ h hd univ n, simp at this, simp [this], exact m.mono (bUnion_subset (λ i _, subset_Union _ i)), end /-- The Carathéodory-measurable sets for an outer measure `m` form a Dynkin system. -/ def caratheodory_dynkin : measurable_space.dynkin_system α := { has := is_caratheodory, has_empty := is_caratheodory_empty, has_compl := assume s, is_caratheodory_compl, has_Union_nat := assume f hf hn, is_caratheodory_Union_nat hn hf } /-- Given an outer measure `μ`, the Carathéodory-measurable space is defined such that `s` is measurable if `∀t, μ t = μ (t ∩ s) + μ (t \ s)`. -/ protected def caratheodory : measurable_space α := caratheodory_dynkin.to_measurable_space $ assume s₁ s₂, is_caratheodory_inter lemma is_caratheodory_iff {s : set α} : caratheodory.measurable_set' s ↔ ∀t, m t = m (t ∩ s) + m (t \ s) := iff.rfl lemma is_caratheodory_iff_le {s : set α} : caratheodory.measurable_set' s ↔ ∀t, m (t ∩ s) + m (t \ s) ≤ m t := is_caratheodory_iff_le' protected lemma Union_eq_of_caratheodory {s : ℕ → set α} (h : ∀i, caratheodory.measurable_set' (s i)) (hd : pairwise (disjoint on s)) : m (⋃i, s i) = ∑'i, m (s i) := f_Union h hd end caratheodory_measurable variables {α : Type*} lemma of_function_caratheodory {m : set α → ℝ≥0∞} {s : set α} {h₀ : m ∅ = 0} (hs : ∀t, m (t ∩ s) + m (t \ s) ≤ m t) : (outer_measure.of_function m h₀).caratheodory.measurable_set' s := begin apply (is_caratheodory_iff_le _).mpr, refine λ t, le_infi (λ f, le_infi $ λ hf, _), refine le_trans (add_le_add (infi_le_of_le (λi, f i ∩ s) $ infi_le _ _) (infi_le_of_le (λi, f i \ s) $ infi_le _ _)) _, { rw ← Union_inter, exact inter_subset_inter_left _ hf }, { rw ← Union_diff, exact diff_subset_diff_left hf }, { rw ← ennreal.tsum_add, exact ennreal.tsum_le_tsum (λ i, hs _) } end lemma bounded_by_caratheodory {m : set α → ℝ≥0∞} {s : set α} (hs : ∀t, m (t ∩ s) + m (t \ s) ≤ m t) : (bounded_by m).caratheodory.measurable_set' s := begin apply of_function_caratheodory, intro t, cases t.eq_empty_or_nonempty with h h, { simp [h, empty_not_nonempty] }, { convert le_trans _ (hs t), { simp [h] }, exact add_le_add supr_const_le supr_const_le } end @[simp] theorem zero_caratheodory : (0 : outer_measure α).caratheodory = ⊤ := top_unique $ λ s _ t, (add_zero _).symm theorem top_caratheodory : (⊤ : outer_measure α).caratheodory = ⊤ := top_unique $ assume s hs, (is_caratheodory_iff_le _).2 $ assume t, t.eq_empty_or_nonempty.elim (λ ht, by simp [ht]) (λ ht, by simp only [ht, top_apply, le_top]) theorem le_add_caratheodory (m₁ m₂ : outer_measure α) : m₁.caratheodory ⊓ m₂.caratheodory ≤ (m₁ + m₂ : outer_measure α).caratheodory := λ s ⟨hs₁, hs₂⟩ t, by simp [hs₁ t, hs₂ t, add_left_comm, add_assoc] theorem le_sum_caratheodory {ι} (m : ι → outer_measure α) : (⨅ i, (m i).caratheodory) ≤ (sum m).caratheodory := λ s h t, by simp [λ i, measurable_space.measurable_set_infi.1 h i t, ennreal.tsum_add] theorem le_smul_caratheodory (a : ℝ≥0∞) (m : outer_measure α) : m.caratheodory ≤ (a • m).caratheodory := λ s h t, by simp [h t, mul_add] @[simp] theorem dirac_caratheodory (a : α) : (dirac a).caratheodory = ⊤ := top_unique $ λ s _ t, begin by_cases ht : a ∈ t, swap, by simp [ht], by_cases hs : a ∈ s; simp* end section Inf_gen /-- Given a set of outer measures, we define a new function that on a set `s` is defined to be the infimum of `μ(s)` for the outer measures `μ` in the collection. We ensure that this function is defined to be `0` on `∅`, even if the collection of outer measures is empty. The outer measure generated by this function is the infimum of the given outer measures. -/ def Inf_gen (m : set (outer_measure α)) (s : set α) : ℝ≥0∞ := ⨅ (μ : outer_measure α) (h : μ ∈ m), μ s lemma Inf_gen_def (m : set (outer_measure α)) (t : set α) : Inf_gen m t = (⨅ (μ : outer_measure α) (h : μ ∈ m), μ t) := rfl lemma Inf_eq_bounded_by_Inf_gen (m : set (outer_measure α)) : Inf m = outer_measure.bounded_by (Inf_gen m) := begin refine le_antisymm _ _, { refine (le_bounded_by.2 $ λ s, _), refine le_binfi _, intros μ hμ, refine (show Inf m ≤ μ, from Inf_le hμ) s }, { refine le_Inf _, intros μ hμ t, refine le_trans (bounded_by_le t) (binfi_le μ hμ) } end lemma supr_Inf_gen_nonempty {m : set (outer_measure α)} (h : m.nonempty) (t : set α) : (⨆ (h : t.nonempty), Inf_gen m t) = (⨅ (μ : outer_measure α) (h : μ ∈ m), μ t) := begin rcases t.eq_empty_or_nonempty with rfl|ht, { rcases h with ⟨μ, hμ⟩, rw [eq_false_intro empty_not_nonempty, supr_false, eq_comm], simp_rw [empty'], apply bot_unique, refine infi_le_of_le μ (infi_le _ hμ) }, { simp [ht, Inf_gen_def] } end /-- The value of the Infimum of a nonempty set of outer measures on a set is not simply the minimum value of a measure on that set: it is the infimum sum of measures of countable set of sets that covers that set, where a different measure can be used for each set in the cover. -/ lemma Inf_apply {m : set (outer_measure α)} {s : set α} (h : m.nonempty) : Inf m s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t), ∑' n, ⨅ (μ : outer_measure α) (h3 : μ ∈ m), μ (t n) := by simp_rw [Inf_eq_bounded_by_Inf_gen, bounded_by_apply, supr_Inf_gen_nonempty h] /-- The value of the Infimum of a set of outer measures on a nonempty set is not simply the minimum value of a measure on that set: it is the infimum sum of measures of countable set of sets that covers that set, where a different measure can be used for each set in the cover. -/ lemma Inf_apply' {m : set (outer_measure α)} {s : set α} (h : s.nonempty) : Inf m s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t), ∑' n, ⨅ (μ : outer_measure α) (h3 : μ ∈ m), μ (t n) := m.eq_empty_or_nonempty.elim (λ hm, by simp [hm, h]) Inf_apply /-- The value of the Infimum of a nonempty family of outer measures on a set is not simply the minimum value of a measure on that set: it is the infimum sum of measures of countable set of sets that covers that set, where a different measure can be used for each set in the cover. -/ lemma infi_apply {ι} [nonempty ι] (m : ι → outer_measure α) (s : set α) : (⨅ i, m i) s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t), ∑' n, ⨅ i, m i (t n) := by { rw [infi, Inf_apply (range_nonempty m)], simp only [infi_range] } /-- The value of the Infimum of a family of outer measures on a nonempty set is not simply the minimum value of a measure on that set: it is the infimum sum of measures of countable set of sets that covers that set, where a different measure can be used for each set in the cover. -/ lemma infi_apply' {ι} (m : ι → outer_measure α) {s : set α} (hs : s.nonempty) : (⨅ i, m i) s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t), ∑' n, ⨅ i, m i (t n) := by { rw [infi, Inf_apply' hs], simp only [infi_range] } /-- The value of the Infimum of a nonempty family of outer measures on a set is not simply the minimum value of a measure on that set: it is the infimum sum of measures of countable set of sets that covers that set, where a different measure can be used for each set in the cover. -/ lemma binfi_apply {ι} {I : set ι} (hI : I.nonempty) (m : ι → outer_measure α) (s : set α) : (⨅ i ∈ I, m i) s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t), ∑' n, ⨅ i ∈ I, m i (t n) := by { haveI := hI.to_subtype, simp only [← infi_subtype'', infi_apply] } /-- The value of the Infimum of a nonempty family of outer measures on a set is not simply the minimum value of a measure on that set: it is the infimum sum of measures of countable set of sets that covers that set, where a different measure can be used for each set in the cover. -/ lemma binfi_apply' {ι} (I : set ι) (m : ι → outer_measure α) {s : set α} (hs : s.nonempty) : (⨅ i ∈ I, m i) s = ⨅ (t : ℕ → set α) (h2 : s ⊆ Union t), ∑' n, ⨅ i ∈ I, m i (t n) := by { simp only [← infi_subtype'', infi_apply' _ hs] } lemma map_infi_le {ι β} (f : α → β) (m : ι → outer_measure α) : map f (⨅ i, m i) ≤ ⨅ i, map f (m i) := (map_mono f).map_infi_le lemma comap_infi {ι β} (f : α → β) (m : ι → outer_measure β) : comap f (⨅ i, m i) = ⨅ i, comap f (m i) := begin refine ext_nonempty (λ s hs, _), refine ((comap_mono f).map_infi_le s).antisymm _, simp only [comap_apply, infi_apply' _ hs, infi_apply' _ (hs.image _), le_infi_iff, set.image_subset_iff, preimage_Union], refine λ t ht, infi_le_of_le _ (infi_le_of_le ht $ ennreal.tsum_le_tsum $ λ k, _), exact infi_le_infi (λ i, (m i).mono (image_preimage_subset _ _)) end lemma map_infi {ι β} {f : α → β} (hf : injective f) (m : ι → outer_measure α) : map f (⨅ i, m i) = restrict (range f) (⨅ i, map f (m i)) := begin refine eq.trans _ (map_comap _ _), simp only [comap_infi, comap_map hf] end lemma map_infi_comap {ι β} [nonempty ι] {f : α → β} (m : ι → outer_measure β) : map f (⨅ i, comap f (m i)) = ⨅ i, map f (comap f (m i)) := begin refine (map_infi_le _ _).antisymm (λ s, _), simp only [map_apply, comap_apply, infi_apply, le_infi_iff], refine λ t ht, infi_le_of_le (λ n, f '' (t n) ∪ (range f)ᶜ) (infi_le_of_le _ _), { rw [← Union_union, set.union_comm, ← inter_subset, ← image_Union, ← image_preimage_eq_inter_range], exact image_subset _ ht }, { refine ennreal.tsum_le_tsum (λ n, infi_le_infi (λ i, (m i).mono _)), simp } end lemma map_binfi_comap {ι β} {I : set ι} (hI : I.nonempty) {f : α → β} (m : ι → outer_measure β) : map f (⨅ i ∈ I, comap f (m i)) = ⨅ i ∈ I, map f (comap f (m i)) := by { haveI := hI.to_subtype, rw [← infi_subtype'', ← infi_subtype''], exact map_infi_comap _ } lemma restrict_infi_restrict {ι} (s : set α) (m : ι → outer_measure α) : restrict s (⨅ i, restrict s (m i)) = restrict s (⨅ i, m i) := calc restrict s (⨅ i, restrict s (m i)) = restrict (range (coe : s → α)) (⨅ i, restrict s (m i)) : by rw [subtype.range_coe] ... = map (coe : s → α) (⨅ i, comap coe (m i)) : (map_infi subtype.coe_injective _).symm ... = restrict s (⨅ i, m i) : congr_arg (map coe) (comap_infi _ _).symm lemma restrict_infi {ι} [nonempty ι] (s : set α) (m : ι → outer_measure α) : restrict s (⨅ i, m i) = ⨅ i, restrict s (m i) := (congr_arg (map coe) (comap_infi _ _)).trans (map_infi_comap _) lemma restrict_binfi {ι} {I : set ι} (hI : I.nonempty) (s : set α) (m : ι → outer_measure α) : restrict s (⨅ i ∈ I, m i) = ⨅ i ∈ I, restrict s (m i) := by { haveI := hI.to_subtype, rw [← infi_subtype'', ← infi_subtype''], exact restrict_infi _ _ } /-- This proves that Inf and restrict commute for outer measures, so long as the set of outer measures is nonempty. -/ lemma restrict_Inf_eq_Inf_restrict (m : set (outer_measure α)) {s : set α} (hm : m.nonempty) : restrict s (Inf m) = Inf ((restrict s) '' m) := by simp only [Inf_eq_infi, restrict_binfi, hm, infi_image] end Inf_gen end outer_measure open outer_measure /-! ### Induced Outer Measure We can extend a function defined on a subset of `set α` to an outer measure. The underlying function is called `extend`, and the measure it induces is called `induced_outer_measure`. Some lemmas below are proven twice, once in the general case, and one where the function `m` is only defined on measurable sets (i.e. when `P = measurable_set`). In the latter cases, we can remove some hypotheses in the statement. The general version has the same name, but with a prime at the end. -/ section extend variables {α : Type*} {P : α → Prop} variables (m : Π (s : α), P s → ℝ≥0∞) /-- We can trivially extend a function defined on a subclass of objects (with codomain `ℝ≥0∞`) to all objects by defining it to be `∞` on the objects not in the class. -/ def extend (s : α) : ℝ≥0∞ := ⨅ h : P s, m s h lemma extend_eq {s : α} (h : P s) : extend m s = m s h := by simp [extend, h] lemma extend_eq_top {s : α} (h : ¬P s) : extend m s = ∞ := by simp [extend, h] lemma le_extend {s : α} (h : P s) : m s h ≤ extend m s := by { simp only [extend, le_infi_iff], intro, refl' } -- TODO: why this is a bad `congr` lemma? lemma extend_congr {β : Type*} {Pb : β → Prop} {mb : Π s : β, Pb s → ℝ≥0∞} {sa : α} {sb : β} (hP : P sa ↔ Pb sb) (hm : ∀ (ha : P sa) (hb : Pb sb), m sa ha = mb sb hb) : extend m sa = extend mb sb := infi_congr_Prop hP (λ h, hm _ _) end extend section extend_set variables {α : Type*} {P : set α → Prop} variables {m : Π (s : set α), P s → ℝ≥0∞} variables (P0 : P ∅) (m0 : m ∅ P0 = 0) variables (PU : ∀{{f : ℕ → set α}} (hm : ∀i, P (f i)), P (⋃i, f i)) variables (mU : ∀ {{f : ℕ → set α}} (hm : ∀i, P (f i)), pairwise (disjoint on f) → m (⋃i, f i) (PU hm) = ∑'i, m (f i) (hm i)) variables (msU : ∀ {{f : ℕ → set α}} (hm : ∀i, P (f i)), m (⋃i, f i) (PU hm) ≤ ∑'i, m (f i) (hm i)) variables (m_mono : ∀⦃s₁ s₂ : set α⦄ (hs₁ : P s₁) (hs₂ : P s₂), s₁ ⊆ s₂ → m s₁ hs₁ ≤ m s₂ hs₂) lemma extend_empty : extend m ∅ = 0 := (extend_eq _ P0).trans m0 lemma extend_Union_nat {f : ℕ → set α} (hm : ∀i, P (f i)) (mU : m (⋃i, f i) (PU hm) = ∑'i, m (f i) (hm i)) : extend m (⋃i, f i) = ∑'i, extend m (f i) := (extend_eq _ _).trans $ mU.trans $ by { congr' with i, rw extend_eq } section subadditive include PU msU lemma extend_Union_le_tsum_nat' (s : ℕ → set α) : extend m (⋃i, s i) ≤ ∑'i, extend m (s i) := begin by_cases h : ∀i, P (s i), { rw [extend_eq _ (PU h), congr_arg tsum _], { apply msU h }, funext i, apply extend_eq _ (h i) }, { cases not_forall.1 h with i hi, exact le_trans (le_infi $ λ h, hi.elim h) (ennreal.le_tsum i) } end end subadditive section mono include m_mono lemma extend_mono' ⦃s₁ s₂ : set α⦄ (h₁ : P s₁) (hs : s₁ ⊆ s₂) : extend m s₁ ≤ extend m s₂ := by { refine le_infi _, intro h₂, rw [extend_eq m h₁], exact m_mono h₁ h₂ hs } end mono section unions include P0 m0 PU mU lemma extend_Union {β} [encodable β] {f : β → set α} (hd : pairwise (disjoint on f)) (hm : ∀i, P (f i)) : extend m (⋃i, f i) = ∑'i, extend m (f i) := begin rw [← encodable.Union_decode₂, ← tsum_Union_decode₂], { exact extend_Union_nat PU (λ n, encodable.Union_decode₂_cases P0 hm) (mU _ (encodable.Union_decode₂_disjoint_on hd)) }, { exact extend_empty P0 m0 } end lemma extend_union {s₁ s₂ : set α} (hd : disjoint s₁ s₂) (h₁ : P s₁) (h₂ : P s₂) : extend m (s₁ ∪ s₂) = extend m s₁ + extend m s₂ := begin rw [union_eq_Union, extend_Union P0 m0 PU mU (pairwise_disjoint_on_bool.2 hd) (bool.forall_bool.2 ⟨h₂, h₁⟩), tsum_fintype], simp end end unions variable (m) /-- Given an arbitrary function on a subset of sets, we can define the outer measure corresponding to it (this is the unique maximal outer measure that is at most `m` on the domain of `m`). -/ def induced_outer_measure : outer_measure α := outer_measure.of_function (extend m) (extend_empty P0 m0) variables {m P0 m0} lemma le_induced_outer_measure {μ : outer_measure α} : μ ≤ induced_outer_measure m P0 m0 ↔ ∀ s (hs : P s), μ s ≤ m s hs := le_of_function.trans $ forall_congr $ λ s, le_infi_iff /-- If `P u` is `false` for any set `u` that has nonempty intersection both with `s` and `t`, then `μ (s ∪ t) = μ s + μ t`, where `μ = induced_outer_measure m P0 m0`. E.g., if `α` is an (e)metric space and `P u = diam u < r`, then this lemma implies that `μ (s ∪ t) = μ s + μ t` on any two sets such that `r ≤ edist x y` for all `x ∈ s` and `y ∈ t`. -/ lemma induced_outer_measure_union_of_false_of_nonempty_inter {s t : set α} (h : ∀ u, (s ∩ u).nonempty → (t ∩ u).nonempty → ¬P u) : induced_outer_measure m P0 m0 (s ∪ t) = induced_outer_measure m P0 m0 s + induced_outer_measure m P0 m0 t := of_function_union_of_top_of_nonempty_inter $ λ u hsu htu, @infi_of_empty _ _ _ ⟨h u hsu htu⟩ _ include msU m_mono lemma induced_outer_measure_eq_extend' {s : set α} (hs : P s) : induced_outer_measure m P0 m0 s = extend m s := of_function_eq s (λ t, extend_mono' m_mono hs) (extend_Union_le_tsum_nat' PU msU) lemma induced_outer_measure_eq' {s : set α} (hs : P s) : induced_outer_measure m P0 m0 s = m s hs := (induced_outer_measure_eq_extend' PU msU m_mono hs).trans $ extend_eq _ _ lemma induced_outer_measure_eq_infi (s : set α) : induced_outer_measure m P0 m0 s = ⨅ (t : set α) (ht : P t) (h : s ⊆ t), m t ht := begin apply le_antisymm, { simp only [le_infi_iff], intros t ht, simp only [le_infi_iff], intro hs, refine le_trans (mono' _ hs) _, exact le_of_eq (induced_outer_measure_eq' _ msU m_mono _) }, { refine le_infi _, intro f, refine le_infi _, intro hf, refine le_trans _ (extend_Union_le_tsum_nat' _ msU _), refine le_infi _, intro h2f, refine infi_le_of_le _ (infi_le_of_le h2f $ infi_le _ hf) } end lemma induced_outer_measure_preimage (f : α ≃ α) (Pm : ∀ (s : set α), P (f ⁻¹' s) ↔ P s) (mm : ∀ (s : set α) (hs : P s), m (f ⁻¹' s) ((Pm _).mpr hs) = m s hs) {A : set α} : induced_outer_measure m P0 m0 (f ⁻¹' A) = induced_outer_measure m P0 m0 A := begin simp only [induced_outer_measure_eq_infi _ msU m_mono], symmetry, refine infi_congr (preimage f) f.injective.preimage_surjective _, intro s, refine infi_congr_Prop (Pm s) _, intro hs, refine infi_congr_Prop f.surjective.preimage_subset_preimage_iff _, intro h2s, exact mm s hs end lemma induced_outer_measure_exists_set {s : set α} (hs : induced_outer_measure m P0 m0 s ≠ ∞) {ε : ℝ≥0∞} (hε : ε ≠ 0) : ∃ (t : set α) (ht : P t), s ⊆ t ∧ induced_outer_measure m P0 m0 t ≤ induced_outer_measure m P0 m0 s + ε := begin have := ennreal.lt_add_right hs hε, conv at this {to_lhs, rw induced_outer_measure_eq_infi _ msU m_mono }, simp only [infi_lt_iff] at this, rcases this with ⟨t, h1t, h2t, h3t⟩, exact ⟨t, h1t, h2t, le_trans (le_of_eq $ induced_outer_measure_eq' _ msU m_mono h1t) (le_of_lt h3t)⟩ end /-- To test whether `s` is Carathéodory-measurable we only need to check the sets `t` for which `P t` holds. See `of_function_caratheodory` for another way to show the Carathéodory-measurability of `s`. -/ lemma induced_outer_measure_caratheodory (s : set α) : (induced_outer_measure m P0 m0).caratheodory.measurable_set' s ↔ ∀ (t : set α), P t → induced_outer_measure m P0 m0 (t ∩ s) + induced_outer_measure m P0 m0 (t \ s) ≤ induced_outer_measure m P0 m0 t := begin rw is_caratheodory_iff_le, split, { intros h t ht, exact h t }, { intros h u, conv_rhs { rw induced_outer_measure_eq_infi _ msU m_mono }, refine le_infi _, intro t, refine le_infi _, intro ht, refine le_infi _, intro h2t, refine le_trans _ (le_trans (h t ht) $ le_of_eq $ induced_outer_measure_eq' _ msU m_mono ht), refine add_le_add (mono' _ $ set.inter_subset_inter_left _ h2t) (mono' _ $ diff_subset_diff_left h2t) } end end extend_set /-! If `P` is `measurable_set` for some measurable space, then we can remove some hypotheses of the above lemmas. -/ section measurable_space variables {α : Type*} [measurable_space α] variables {m : Π (s : set α), measurable_set s → ℝ≥0∞} variables (m0 : m ∅ measurable_set.empty = 0) variable (mU : ∀ {{f : ℕ → set α}} (hm : ∀i, measurable_set (f i)), pairwise (disjoint on f) → m (⋃i, f i) (measurable_set.Union hm) = ∑'i, m (f i) (hm i)) include m0 mU lemma extend_mono {s₁ s₂ : set α} (h₁ : measurable_set s₁) (hs : s₁ ⊆ s₂) : extend m s₁ ≤ extend m s₂ := begin refine le_infi _, intro h₂, have := extend_union measurable_set.empty m0 measurable_set.Union mU disjoint_diff h₁ (h₂.diff h₁), rw union_diff_cancel hs at this, rw ← extend_eq m, exact le_iff_exists_add.2 ⟨_, this⟩, end lemma extend_Union_le_tsum_nat : ∀ (s : ℕ → set α), extend m (⋃i, s i) ≤ ∑'i, extend m (s i) := begin refine extend_Union_le_tsum_nat' measurable_set.Union _, intros f h, simp [Union_disjointed.symm] {single_pass := tt}, rw [mU (measurable_set.disjointed h) (disjoint_disjointed _)], refine ennreal.tsum_le_tsum (λ i, _), rw [← extend_eq m, ← extend_eq m], exact extend_mono m0 mU (measurable_set.disjointed h _) (disjointed_le f _), end lemma induced_outer_measure_eq_extend {s : set α} (hs : measurable_set s) : induced_outer_measure m measurable_set.empty m0 s = extend m s := of_function_eq s (λ t, extend_mono m0 mU hs) (extend_Union_le_tsum_nat m0 mU) lemma induced_outer_measure_eq {s : set α} (hs : measurable_set s) : induced_outer_measure m measurable_set.empty m0 s = m s hs := (induced_outer_measure_eq_extend m0 mU hs).trans $ extend_eq _ _ end measurable_space namespace outer_measure variables {α : Type*} [measurable_space α] (m : outer_measure α) /-- Given an outer measure `m` we can forget its value on non-measurable sets, and then consider `m.trim`, the unique maximal outer measure less than that function. -/ def trim : outer_measure α := induced_outer_measure (λ s _, m s) measurable_set.empty m.empty theorem le_trim : m ≤ m.trim := le_of_function.mpr $ λ s, le_infi $ λ _, le_refl _ theorem trim_eq {s : set α} (hs : measurable_set s) : m.trim s = m s := induced_outer_measure_eq' measurable_set.Union (λ f hf, m.Union_nat f) (λ _ _ _ _ h, m.mono h) hs theorem trim_congr {m₁ m₂ : outer_measure α} (H : ∀ {s : set α}, measurable_set s → m₁ s = m₂ s) : m₁.trim = m₂.trim := by { unfold trim, congr, funext s hs, exact H hs } @[mono] theorem trim_mono : monotone (trim : outer_measure α → outer_measure α) := λ m₁ m₂ H s, binfi_le_binfi $ λ f hs, ennreal.tsum_le_tsum $ λ b, infi_le_infi $ λ hf, H _ theorem le_trim_iff {m₁ m₂ : outer_measure α} : m₁ ≤ m₂.trim ↔ ∀ s, measurable_set s → m₁ s ≤ m₂ s := le_of_function.trans $ forall_congr $ λ s, le_infi_iff theorem trim_le_trim_iff {m₁ m₂ : outer_measure α} : m₁.trim ≤ m₂.trim ↔ ∀ s, measurable_set s → m₁ s ≤ m₂ s := le_trim_iff.trans $ forall_congr $ λ s, forall_congr $ λ hs, by rw [trim_eq _ hs] theorem trim_eq_trim_iff {m₁ m₂ : outer_measure α} : m₁.trim = m₂.trim ↔ ∀ s, measurable_set s → m₁ s = m₂ s := by simp only [le_antisymm_iff, trim_le_trim_iff, forall_and_distrib] theorem trim_eq_infi (s : set α) : m.trim s = ⨅ t (st : s ⊆ t) (ht : measurable_set t), m t := by { simp only [infi_comm] {single_pass := tt}, exact induced_outer_measure_eq_infi measurable_set.Union (λ f _, m.Union_nat f) (λ _ _ _ _ h, m.mono h) s } theorem trim_eq_infi' (s : set α) : m.trim s = ⨅ t : {t // s ⊆ t ∧ measurable_set t}, m t := by simp [infi_subtype, infi_and, trim_eq_infi] theorem trim_trim (m : outer_measure α) : m.trim.trim = m.trim := trim_eq_trim_iff.2 $ λ s, m.trim_eq @[simp] theorem trim_zero : (0 : outer_measure α).trim = 0 := ext $ λ s, le_antisymm (le_trans ((trim 0).mono (subset_univ s)) $ le_of_eq $ trim_eq _ measurable_set.univ) (zero_le _) theorem trim_sum_ge {ι} (m : ι → outer_measure α) : sum (λ i, (m i).trim) ≤ (sum m).trim := λ s, by simp [trim_eq_infi]; exact λ t st ht, ennreal.tsum_le_tsum (λ i, infi_le_of_le t $ infi_le_of_le st $ infi_le _ ht) lemma exists_measurable_superset_eq_trim (m : outer_measure α) (s : set α) : ∃ t, s ⊆ t ∧ measurable_set t ∧ m t = m.trim s := begin simp only [trim_eq_infi], set ms := ⨅ (t : set α) (st : s ⊆ t) (ht : measurable_set t), m t, by_cases hs : ms = ∞, { simp only [hs], simp only [infi_eq_top] at hs, exact ⟨univ, subset_univ s, measurable_set.univ, hs _ (subset_univ s) measurable_set.univ⟩ }, { have : ∀ r > ms, ∃ t, s ⊆ t ∧ measurable_set t ∧ m t < r, { intros r hs, simpa [infi_lt_iff] using hs }, have : ∀ n : ℕ, ∃ t, s ⊆ t ∧ measurable_set t ∧ m t < ms + n⁻¹, { assume n, refine this _ (ennreal.lt_add_right hs _), simp }, choose t hsub hm hm', refine ⟨⋂ n, t n, subset_Inter hsub, measurable_set.Inter hm, _⟩, have : tendsto (λ n : ℕ, ms + n⁻¹) at_top (𝓝 (ms + 0)), from tendsto_const_nhds.add ennreal.tendsto_inv_nat_nhds_zero, rw add_zero at this, refine le_antisymm (ge_of_tendsto' this $ λ n, _) _, { exact le_trans (m.mono' $ Inter_subset t n) (hm' n).le }, { refine infi_le_of_le (⋂ n, t n) _, refine infi_le_of_le (subset_Inter hsub) _, refine infi_le _ (measurable_set.Inter hm) } } end lemma exists_measurable_superset_of_trim_eq_zero {m : outer_measure α} {s : set α} (h : m.trim s = 0) : ∃t, s ⊆ t ∧ measurable_set t ∧ m t = 0 := begin rcases exists_measurable_superset_eq_trim m s with ⟨t, hst, ht, hm⟩, exact ⟨t, hst, ht, h ▸ hm⟩ end /-- If `μ i` is a countable family of outer measures, then for every set `s` there exists a measurable set `t ⊇ s` such that `μ i t = (μ i).trim s` for all `i`. -/ lemma exists_measurable_superset_forall_eq_trim {ι} [encodable ι] (μ : ι → outer_measure α) (s : set α) : ∃ t, s ⊆ t ∧ measurable_set t ∧ ∀ i, μ i t = (μ i).trim s := begin choose t hst ht hμt using λ i, (μ i).exists_measurable_superset_eq_trim s, replace hst := subset_Inter hst, replace ht := measurable_set.Inter ht, refine ⟨⋂ i, t i, hst, ht, λ i, le_antisymm _ _⟩, exacts [hμt i ▸ (μ i).mono (Inter_subset _ _), (mono' _ hst).trans_eq ((μ i).trim_eq ht)] end /-- If `m₁ s = op (m₂ s) (m₃ s)` for all `s`, then the same is true for `m₁.trim`, `m₂.trim`, and `m₃ s`. -/ theorem trim_binop {m₁ m₂ m₃ : outer_measure α} {op : ℝ≥0∞ → ℝ≥0∞ → ℝ≥0∞} (h : ∀ s, m₁ s = op (m₂ s) (m₃ s)) (s : set α) : m₁.trim s = op (m₂.trim s) (m₃.trim s) := begin rcases exists_measurable_superset_forall_eq_trim (![m₁, m₂, m₃]) s with ⟨t, hst, ht, htm⟩, simp only [fin.forall_fin_succ, matrix.cons_val_zero, matrix.cons_val_succ] at htm, rw [← htm.1, ← htm.2.1, ← htm.2.2.1, h] end /-- If `m₁ s = op (m₂ s)` for all `s`, then the same is true for `m₁.trim` and `m₂.trim`. -/ theorem trim_op {m₁ m₂ : outer_measure α} {op : ℝ≥0∞ → ℝ≥0∞} (h : ∀ s, m₁ s = op (m₂ s)) (s : set α) : m₁.trim s = op (m₂.trim s) := @trim_binop α _ m₁ m₂ 0 (λ a b, op a) h s /-- `trim` is additive. -/ theorem trim_add (m₁ m₂ : outer_measure α) : (m₁ + m₂).trim = m₁.trim + m₂.trim := ext $ trim_binop (add_apply m₁ m₂) /-- `trim` respects scalar multiplication. -/ theorem trim_smul (c : ℝ≥0∞) (m : outer_measure α) : (c • m).trim = c • m.trim := ext $ trim_op (smul_apply c m) /-- `trim` sends the supremum of two outer measures to the supremum of the trimmed measures. -/ theorem trim_sup (m₁ m₂ : outer_measure α) : (m₁ ⊔ m₂).trim = m₁.trim ⊔ m₂.trim := ext $ λ s, (trim_binop (sup_apply m₁ m₂) s).trans (sup_apply _ _ _).symm /-- `trim` sends the supremum of a countable family of outer measures to the supremum of the trimmed measures. -/ lemma trim_supr {ι} [encodable ι] (μ : ι → outer_measure α) : trim (⨆ i, μ i) = ⨆ i, trim (μ i) := begin ext1 s, rcases exists_measurable_superset_forall_eq_trim (λ o, option.elim o (supr μ) μ) s with ⟨t, hst, ht, hμt⟩, simp only [option.forall, option.elim] at hμt, simp only [supr_apply, ← hμt.1, ← hμt.2] end /-- The trimmed property of a measure μ states that `μ.to_outer_measure.trim = μ.to_outer_measure`. This theorem shows that a restricted trimmed outer measure is a trimmed outer measure. -/ lemma restrict_trim {μ : outer_measure α} {s : set α} (hs : measurable_set s) : (restrict s μ).trim = restrict s μ.trim := begin refine le_antisymm (λ t, _) (le_trim_iff.2 $ λ t ht, _), { rw restrict_apply, rcases μ.exists_measurable_superset_eq_trim (t ∩ s) with ⟨t', htt', ht', hμt'⟩, rw [← hμt'], rw inter_subset at htt', refine (mono' _ htt').trans _, rw [trim_eq _ (hs.compl.union ht'), restrict_apply, union_inter_distrib_right, compl_inter_self, set.empty_union], exact μ.mono' (inter_subset_left _ _) }, { rw [restrict_apply, trim_eq _ (ht.inter hs), restrict_apply], exact le_rfl } end end outer_measure end measure_theory
38f3c44c8860074ab7c607c518343619335a3ed6
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/preadditive/functor_category.lean
1d6dc357ec7db6be3fcbec82b228b04b5e323de4
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
2,666
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import category_theory.preadditive.default /-! # Preadditive structure on functor categories If `C` and `D` are categories and `D` is preadditive, then `C ⥤ D` is also preadditive. -/ open_locale big_operators namespace category_theory open category_theory.limits preadditive variables {C D : Type*} [category C] [category D] [preadditive D] instance functor_category_preadditive : preadditive (C ⥤ D) := { hom_group := λ F G, { add := λ α β, { app := λ X, α.app X + β.app X, naturality' := by { intros, rw [comp_add, add_comp, α.naturality, β.naturality] } }, zero := { app := λ X, 0, naturality' := by { intros, rw [zero_comp, comp_zero] } }, neg := λ α, { app := λ X, -α.app X, naturality' := by { intros, rw [comp_neg, neg_comp, α.naturality] } }, sub := λ α β, { app := λ X, α.app X - β.app X, naturality' := by { intros, rw [comp_sub, sub_comp, α.naturality, β.naturality] } }, add_assoc := by { intros, ext, apply add_assoc }, zero_add := by { intros, ext, apply zero_add }, add_zero := by { intros, ext, apply add_zero }, sub_eq_add_neg := by { intros, ext, apply sub_eq_add_neg }, add_left_neg := by { intros, ext, apply add_left_neg }, add_comm := by { intros, ext, apply add_comm } }, add_comp' := by { intros, ext, apply add_comp }, comp_add' := by { intros, ext, apply comp_add } } namespace nat_trans variables {F G : C ⥤ D} /-- Application of a natural transformation at a fixed object, as group homomorphism -/ @[simps] def app_hom (X : C) : (F ⟶ G) →+ (F.obj X ⟶ G.obj X) := { to_fun := λ α, α.app X, map_zero' := rfl, map_add' := λ _ _, rfl } @[simp] lemma app_zero (X : C) : (0 : F ⟶ G).app X = 0 := rfl @[simp] lemma app_add (X : C) (α β : F ⟶ G) : (α + β).app X = α.app X + β.app X := rfl @[simp] lemma app_sub (X : C) (α β : F ⟶ G) : (α - β).app X = α.app X - β.app X := rfl @[simp] lemma app_neg (X : C) (α : F ⟶ G) : (-α).app X = -α.app X := rfl @[simp] lemma app_nsmul (X : C) (α : F ⟶ G) (n : ℕ) : (n • α).app X = n • α.app X := (app_hom X).map_nsmul α n @[simp] lemma app_zsmul (X : C) (α : F ⟶ G) (n : ℤ) : (n • α).app X = n • α.app X := (app_hom X).map_zsmul α n @[simp] lemma app_sum {ι : Type*} (s : finset ι) (X : C) (α : ι → (F ⟶ G)) : (∑ i in s, α i).app X = ∑ i in s, ((α i).app X) := by { rw [← app_hom_apply, add_monoid_hom.map_sum], refl } end nat_trans end category_theory
eab9ffc244d016fccdb2c152045ac8c3ef660fa9
5749d8999a76f3a8fddceca1f6941981e33aaa96
/src/order/bounded_lattice.lean
7216fd293c430a86db018d9c15ff9832edbb36a7
[ "Apache-2.0" ]
permissive
jdsalchow/mathlib
13ab43ef0d0515a17e550b16d09bd14b76125276
497e692b946d93906900bb33a51fd243e7649406
refs/heads/master
1,585,819,143,348
1,580,072,892,000
1,580,072,892,000
154,287,128
0
0
Apache-2.0
1,540,281,610,000
1,540,281,609,000
null
UTF-8
Lean
false
false
29,628
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Defines bounded lattice type class hierarchy. Includes the Prop and fun instances. -/ import order.lattice data.option.basic tactic.pi_instances set_option old_structure_cmd true universes u v namespace lattice variable {α : Type u} /-- Typeclass for the `⊤` (`\top`) notation -/ class has_top (α : Type u) := (top : α) /-- Typeclass for the `⊥` (`\bot`) notation -/ class has_bot (α : Type u) := (bot : α) notation `⊤` := has_top.top _ notation `⊥` := has_bot.bot _ attribute [pattern] lattice.has_bot.bot lattice.has_top.top section prio set_option default_priority 100 -- see Note [default priority] /-- An `order_top` is a partial order with a maximal element. (We could state this on preorders, but then it wouldn't be unique so distinguishing one would seem odd.) -/ class order_top (α : Type u) extends has_top α, partial_order α := (le_top : ∀ a : α, a ≤ ⊤) end prio section order_top variables [order_top α] {a b : α} @[simp] theorem le_top : a ≤ ⊤ := order_top.le_top a theorem top_unique (h : ⊤ ≤ a) : a = ⊤ := le_antisymm le_top h -- TODO: delete in favor of the next? theorem eq_top_iff : a = ⊤ ↔ ⊤ ≤ a := ⟨assume eq, eq.symm ▸ le_refl ⊤, top_unique⟩ @[simp] theorem top_le_iff : ⊤ ≤ a ↔ a = ⊤ := ⟨top_unique, λ h, h.symm ▸ le_refl ⊤⟩ @[simp] theorem not_top_lt : ¬ ⊤ < a := assume h, lt_irrefl a (lt_of_le_of_lt le_top h) theorem eq_top_mono (h : a ≤ b) (h₂ : a = ⊤) : b = ⊤ := top_le_iff.1 $ h₂ ▸ h lemma lt_top_iff_ne_top : a < ⊤ ↔ a ≠ ⊤ := begin haveI := classical.dec_eq α, haveI : decidable (⊤ ≤ a) := decidable_of_iff' _ top_le_iff, by simp [-top_le_iff, lt_iff_le_not_le, not_iff_not.2 (@top_le_iff _ _ a)] end lemma ne_top_of_lt (h : a < b) : a ≠ ⊤ := lt_top_iff_ne_top.1 $ lt_of_lt_of_le h le_top end order_top theorem order_top.ext_top {α} {A B : order_top α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : (by haveI := A; exact ⊤ : α) = ⊤ := top_unique $ by rw ← H; apply le_top theorem order_top.ext {α} {A B : order_top α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := begin haveI this := partial_order.ext H, have tt := order_top.ext_top H, cases A; cases B; injection this; congr' end section prio set_option default_priority 100 -- see Note [default priority] /-- An `order_bot` is a partial order with a minimal element. (We could state this on preorders, but then it wouldn't be unique so distinguishing one would seem odd.) -/ class order_bot (α : Type u) extends has_bot α, partial_order α := (bot_le : ∀ a : α, ⊥ ≤ a) end prio section order_bot variables [order_bot α] {a b : α} @[simp] theorem bot_le : ⊥ ≤ a := order_bot.bot_le a theorem bot_unique (h : a ≤ ⊥) : a = ⊥ := le_antisymm h bot_le -- TODO: delete? theorem eq_bot_iff : a = ⊥ ↔ a ≤ ⊥ := ⟨assume eq, eq.symm ▸ le_refl ⊥, bot_unique⟩ @[simp] theorem le_bot_iff : a ≤ ⊥ ↔ a = ⊥ := ⟨bot_unique, assume h, h.symm ▸ le_refl ⊥⟩ @[simp] theorem not_lt_bot : ¬ a < ⊥ := assume h, lt_irrefl a (lt_of_lt_of_le h bot_le) theorem ne_bot_of_le_ne_bot {a b : α} (hb : b ≠ ⊥) (hab : b ≤ a) : a ≠ ⊥ := assume ha, hb $ bot_unique $ ha ▸ hab theorem eq_bot_mono (h : a ≤ b) (h₂ : b = ⊥) : a = ⊥ := le_bot_iff.1 $ h₂ ▸ h lemma bot_lt_iff_ne_bot : ⊥ < a ↔ a ≠ ⊥ := begin haveI := classical.dec_eq α, haveI : decidable (a ≤ ⊥) := decidable_of_iff' _ le_bot_iff, simp [-le_bot_iff, lt_iff_le_not_le, not_iff_not.2 (@le_bot_iff _ _ a)] end lemma ne_bot_of_gt (h : a < b) : b ≠ ⊥ := bot_lt_iff_ne_bot.1 $ lt_of_le_of_lt bot_le h end order_bot theorem order_bot.ext_bot {α} {A B : order_bot α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : (by haveI := A; exact ⊥ : α) = ⊥ := bot_unique $ by rw ← H; apply bot_le theorem order_bot.ext {α} {A B : order_bot α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := begin haveI this := partial_order.ext H, have tt := order_bot.ext_bot H, cases A; cases B; injection this; congr' end section prio set_option default_priority 100 -- see Note [default priority] /-- A `semilattice_sup_top` is a semilattice with top and join. -/ class semilattice_sup_top (α : Type u) extends order_top α, semilattice_sup α end prio section semilattice_sup_top variables [semilattice_sup_top α] {a : α} @[simp] theorem top_sup_eq : ⊤ ⊔ a = ⊤ := sup_of_le_left le_top @[simp] theorem sup_top_eq : a ⊔ ⊤ = ⊤ := sup_of_le_right le_top end semilattice_sup_top section prio set_option default_priority 100 -- see Note [default priority] /-- A `semilattice_sup_bot` is a semilattice with bottom and join. -/ class semilattice_sup_bot (α : Type u) extends order_bot α, semilattice_sup α end prio section semilattice_sup_bot variables [semilattice_sup_bot α] {a b : α} @[simp] theorem bot_sup_eq : ⊥ ⊔ a = a := sup_of_le_right bot_le @[simp] theorem sup_bot_eq : a ⊔ ⊥ = a := sup_of_le_left bot_le @[simp] theorem sup_eq_bot_iff : a ⊔ b = ⊥ ↔ (a = ⊥ ∧ b = ⊥) := by rw [eq_bot_iff, sup_le_iff]; simp end semilattice_sup_bot instance nat.semilattice_sup_bot : semilattice_sup_bot ℕ := { bot := 0, bot_le := nat.zero_le, .. nat.distrib_lattice } private def bot_aux (s : set ℕ) [decidable_pred s] [h : nonempty s] : s := have ∃ x, x ∈ s, from nonempty.elim h (λ x, ⟨x.1, x.2⟩), ⟨nat.find this, nat.find_spec this⟩ instance nat.subtype.semilattice_sup_bot (s : set ℕ) [decidable_pred s] [h : nonempty s] : semilattice_sup_bot s := { bot := bot_aux s, bot_le := λ x, nat.find_min' _ x.2, ..subtype.linear_order s, ..lattice.lattice_of_decidable_linear_order } section prio set_option default_priority 100 -- see Note [default priority] /-- A `semilattice_inf_top` is a semilattice with top and meet. -/ class semilattice_inf_top (α : Type u) extends order_top α, semilattice_inf α end prio section semilattice_inf_top variables [semilattice_inf_top α] {a b : α} @[simp] theorem top_inf_eq : ⊤ ⊓ a = a := inf_of_le_right le_top @[simp] theorem inf_top_eq : a ⊓ ⊤ = a := inf_of_le_left le_top @[simp] theorem inf_eq_top_iff : a ⊓ b = ⊤ ↔ (a = ⊤ ∧ b = ⊤) := by rw [eq_top_iff, le_inf_iff]; simp end semilattice_inf_top section prio set_option default_priority 100 -- see Note [default priority] /-- A `semilattice_inf_bot` is a semilattice with bottom and meet. -/ class semilattice_inf_bot (α : Type u) extends order_bot α, semilattice_inf α end prio section semilattice_inf_bot variables [semilattice_inf_bot α] {a : α} @[simp] theorem bot_inf_eq : ⊥ ⊓ a = ⊥ := inf_of_le_left bot_le @[simp] theorem inf_bot_eq : a ⊓ ⊥ = ⊥ := inf_of_le_right bot_le end semilattice_inf_bot /- Bounded lattices -/ section prio set_option default_priority 100 -- see Note [default priority] /-- A bounded lattice is a lattice with a top and bottom element, denoted `⊤` and `⊥` respectively. This allows for the interpretation of all finite suprema and infima, taking `inf ∅ = ⊤` and `sup ∅ = ⊥`. -/ class bounded_lattice (α : Type u) extends lattice α, order_top α, order_bot α end prio @[priority 100] -- see Note [lower instance priority] instance semilattice_inf_top_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_inf_top α := { le_top := assume x, @le_top α _ x, ..bl } @[priority 100] -- see Note [lower instance priority] instance semilattice_inf_bot_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_inf_bot α := { bot_le := assume x, @bot_le α _ x, ..bl } @[priority 100] -- see Note [lower instance priority] instance semilattice_sup_top_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_sup_top α := { le_top := assume x, @le_top α _ x, ..bl } @[priority 100] -- see Note [lower instance priority] instance semilattice_sup_bot_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_sup_bot α := { bot_le := assume x, @bot_le α _ x, ..bl } theorem bounded_lattice.ext {α} {A B : bounded_lattice α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := begin haveI H1 : @bounded_lattice.to_lattice α A = @bounded_lattice.to_lattice α B := lattice.ext H, haveI H2 := order_bot.ext H, haveI H3 : @bounded_lattice.to_order_top α A = @bounded_lattice.to_order_top α B := order_top.ext H, have tt := order_bot.ext_bot H, cases A; cases B; injection H1; injection H2; injection H3; congr' end section prio set_option default_priority 100 -- see Note [default priority] /-- A bounded distributive lattice is exactly what it sounds like. -/ class bounded_distrib_lattice α extends distrib_lattice α, bounded_lattice α end prio lemma inf_eq_bot_iff_le_compl {α : Type u} [bounded_distrib_lattice α] {a b c : α} (h₁ : b ⊔ c = ⊤) (h₂ : b ⊓ c = ⊥) : a ⊓ b = ⊥ ↔ a ≤ c := ⟨assume : a ⊓ b = ⊥, calc a ≤ a ⊓ (b ⊔ c) : by simp [h₁] ... = (a ⊓ b) ⊔ (a ⊓ c) : by simp [inf_sup_left] ... ≤ c : by simp [this, inf_le_right], assume : a ≤ c, bot_unique $ calc a ⊓ b ≤ b ⊓ c : by rw [inf_comm]; exact inf_le_inf (le_refl _) this ... = ⊥ : h₂⟩ /- Prop instance -/ instance bounded_lattice_Prop : bounded_lattice Prop := { lattice.bounded_lattice . le := λa b, a → b, le_refl := assume _, id, le_trans := assume a b c f g, g ∘ f, le_antisymm := assume a b Hab Hba, propext ⟨Hab, Hba⟩, sup := or, le_sup_left := @or.inl, le_sup_right := @or.inr, sup_le := assume a b c, or.rec, inf := and, inf_le_left := @and.left, inf_le_right := @and.right, le_inf := assume a b c Hab Hac Ha, and.intro (Hab Ha) (Hac Ha), top := true, le_top := assume a Ha, true.intro, bot := false, bot_le := @false.elim } section logic variable [preorder α] theorem monotone_and {p q : α → Prop} (m_p : monotone p) (m_q : monotone q) : monotone (λx, p x ∧ q x) := assume a b h, and.imp (m_p h) (m_q h) -- Note: by finish [monotone] doesn't work theorem monotone_or {p q : α → Prop} (m_p : monotone p) (m_q : monotone q) : monotone (λx, p x ∨ q x) := assume a b h, or.imp (m_p h) (m_q h) end logic /- Function lattices -/ /- TODO: * build up the lattice hierarchy for `fun`-functor piecewise. semilattic_*, bounded_lattice, lattice ... * can this be generalized to the dependent function space? -/ instance pi.bounded_lattice {α : Type u} {β : Type v} [bounded_lattice β] : bounded_lattice (α → β) := by pi_instance end lattice def with_bot (α : Type*) := option α namespace with_bot variable {α : Type u} open lattice meta instance {α} [has_to_format α] : has_to_format (with_bot α) := { to_format := λ x, match x with | none := "⊥" | (some x) := to_fmt x end } instance : has_coe_t α (with_bot α) := ⟨some⟩ instance has_bot : has_bot (with_bot α) := ⟨none⟩ lemma none_eq_bot : (none : with_bot α) = (⊥ : with_bot α) := rfl lemma some_eq_coe (a : α) : (some a : with_bot α) = (↑a : with_bot α) := rfl theorem coe_eq_coe {a b : α} : (a : with_bot α) = b ↔ a = b := by rw [← option.some.inj_eq a b]; refl @[priority 10] instance has_lt [has_lt α] : has_lt (with_bot α) := { lt := λ o₁ o₂ : option α, ∃ b ∈ o₂, ∀ a ∈ o₁, a < b } @[simp] theorem some_lt_some [has_lt α] {a b : α} : @has_lt.lt (with_bot α) _ (some a) (some b) ↔ a < b := by simp [(<)] instance [preorder α] : preorder (with_bot α) := { le := λ o₁ o₂ : option α, ∀ a ∈ o₁, ∃ b ∈ o₂, a ≤ b, lt := (<), lt_iff_le_not_le := by intros; cases a; cases b; simp [lt_iff_le_not_le]; simp [(<)]; split; refl, le_refl := λ o a ha, ⟨a, ha, le_refl _⟩, le_trans := λ o₁ o₂ o₃ h₁ h₂ a ha, let ⟨b, hb, ab⟩ := h₁ a ha, ⟨c, hc, bc⟩ := h₂ b hb in ⟨c, hc, le_trans ab bc⟩ } instance partial_order [partial_order α] : partial_order (with_bot α) := { le_antisymm := λ o₁ o₂ h₁ h₂, begin cases o₁ with a, { cases o₂ with b, {refl}, rcases h₂ b rfl with ⟨_, ⟨⟩, _⟩ }, { rcases h₁ a rfl with ⟨b, ⟨⟩, h₁'⟩, rcases h₂ b rfl with ⟨_, ⟨⟩, h₂'⟩, rw le_antisymm h₁' h₂' } end, .. with_bot.preorder } instance order_bot [partial_order α] : order_bot (with_bot α) := { bot_le := λ a a' h, option.no_confusion h, ..with_bot.partial_order, ..with_bot.has_bot } @[simp] theorem coe_le_coe [partial_order α] {a b : α} : (a : with_bot α) ≤ b ↔ a ≤ b := ⟨λ h, by rcases h a rfl with ⟨_, ⟨⟩, h⟩; exact h, λ h a' e, option.some_inj.1 e ▸ ⟨b, rfl, h⟩⟩ @[simp] theorem some_le_some [partial_order α] {a b : α} : @has_le.le (with_bot α) _ (some a) (some b) ↔ a ≤ b := coe_le_coe theorem coe_le [partial_order α] {a b : α} : ∀ {o : option α}, b ∈ o → ((a : with_bot α) ≤ o ↔ a ≤ b) | _ rfl := coe_le_coe lemma coe_lt_coe [partial_order α] {a b : α} : (a : with_bot α) < b ↔ a < b := some_lt_some lemma bot_lt_some [partial_order α] (a : α) : (⊥ : with_bot α) < some a := lt_of_le_of_ne bot_le (λ h, option.no_confusion h) lemma bot_lt_coe [partial_order α] (a : α) : (⊥ : with_bot α) < a := bot_lt_some a instance linear_order [linear_order α] : linear_order (with_bot α) := { le_total := λ o₁ o₂, begin cases o₁ with a, {exact or.inl bot_le}, cases o₂ with b, {exact or.inr bot_le}, simp [le_total] end, ..with_bot.partial_order } instance decidable_lt [has_lt α] [@decidable_rel α (<)] : @decidable_rel (with_bot α) (<) | none (some x) := is_true $ by existsi [x,rfl]; rintros _ ⟨⟩ | (some x) (some y) := if h : x < y then is_true $ by simp * else is_false $ by simp * | x none := is_false $ by rintro ⟨a,⟨⟨⟩⟩⟩ instance decidable_linear_order [decidable_linear_order α] : decidable_linear_order (with_bot α) := { decidable_le := λ a b, begin cases a with a, { exact is_true bot_le }, cases b with b, { exact is_false (mt (le_antisymm bot_le) (by simp)) }, { exact decidable_of_iff _ some_le_some } end, ..with_bot.linear_order } instance semilattice_sup [semilattice_sup α] : semilattice_sup_bot (with_bot α) := { sup := option.lift_or_get (⊔), le_sup_left := λ o₁ o₂ a ha, by cases ha; cases o₂; simp [option.lift_or_get], le_sup_right := λ o₁ o₂ a ha, by cases ha; cases o₁; simp [option.lift_or_get], sup_le := λ o₁ o₂ o₃ h₁ h₂ a ha, begin cases o₁ with b; cases o₂ with c; cases ha, { exact h₂ a rfl }, { exact h₁ a rfl }, { rcases h₁ b rfl with ⟨d, ⟨⟩, h₁'⟩, simp at h₂, exact ⟨d, rfl, sup_le h₁' h₂⟩ } end, ..with_bot.order_bot } instance semilattice_inf [semilattice_inf α] : semilattice_inf_bot (with_bot α) := { inf := λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a ⊓ b)), inf_le_left := λ o₁ o₂ a ha, begin simp at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩, exact ⟨_, rfl, inf_le_left⟩ end, inf_le_right := λ o₁ o₂ a ha, begin simp at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩, exact ⟨_, rfl, inf_le_right⟩ end, le_inf := λ o₁ o₂ o₃ h₁ h₂ a ha, begin cases ha, rcases h₁ a rfl with ⟨b, ⟨⟩, ab⟩, rcases h₂ a rfl with ⟨c, ⟨⟩, ac⟩, exact ⟨_, rfl, le_inf ab ac⟩ end, ..with_bot.order_bot } instance lattice [lattice α] : lattice (with_bot α) := { ..with_bot.semilattice_sup, ..with_bot.semilattice_inf } theorem lattice_eq_DLO [decidable_linear_order α] : lattice.lattice_of_decidable_linear_order = @with_bot.lattice α _ := lattice.ext $ λ x y, iff.rfl theorem sup_eq_max [decidable_linear_order α] (x y : with_bot α) : x ⊔ y = max x y := by rw [← sup_eq_max, lattice_eq_DLO] theorem inf_eq_min [decidable_linear_order α] (x y : with_bot α) : x ⊓ y = min x y := by rw [← inf_eq_min, lattice_eq_DLO] instance order_top [order_top α] : order_top (with_bot α) := { top := some ⊤, le_top := λ o a ha, by cases ha; exact ⟨_, rfl, le_top⟩, ..with_bot.partial_order } instance bounded_lattice [bounded_lattice α] : bounded_lattice (with_bot α) := { ..with_bot.lattice, ..with_bot.order_top, ..with_bot.order_bot } lemma well_founded_lt [partial_order α] (h : well_founded ((<) : α → α → Prop)) : well_founded ((<) : with_bot α → with_bot α → Prop) := have acc_bot : acc ((<) : with_bot α → with_bot α → Prop) ⊥ := acc.intro _ (λ a ha, (not_le_of_gt ha bot_le).elim), ⟨λ a, option.rec_on a acc_bot (λ a, acc.intro _ (λ b, option.rec_on b (λ _, acc_bot) (λ b, well_founded.induction h b (show ∀ b : α, (∀ c, c < b → (c : with_bot α) < a → acc ((<) : with_bot α → with_bot α → Prop) c) → (b : with_bot α) < a → acc ((<) : with_bot α → with_bot α → Prop) b, from λ b ih hba, acc.intro _ (λ c, option.rec_on c (λ _, acc_bot) (λ c hc, ih _ (some_lt_some.1 hc) (lt_trans hc hba)))))))⟩ instance densely_ordered [partial_order α] [densely_ordered α] [no_bot_order α] : densely_ordered (with_bot α) := ⟨ assume a b, match a, b with | a, none := assume h : a < ⊥, (not_lt_bot h).elim | none, some b := assume h, let ⟨a, ha⟩ := no_bot b in ⟨a, bot_lt_coe a, coe_lt_coe.2 ha⟩ | some a, some b := assume h, let ⟨a, ha₁, ha₂⟩ := dense (coe_lt_coe.1 h) in ⟨a, coe_lt_coe.2 ha₁, coe_lt_coe.2 ha₂⟩ end⟩ end with_bot --TODO(Mario): Construct using order dual on with_bot def with_top (α : Type*) := option α namespace with_top variable {α : Type u} open lattice meta instance {α} [has_to_format α] : has_to_format (with_top α) := { to_format := λ x, match x with | none := "⊤" | (some x) := to_fmt x end } instance : has_coe_t α (with_top α) := ⟨some⟩ instance has_top : has_top (with_top α) := ⟨none⟩ lemma none_eq_top : (none : with_top α) = (⊤ : with_top α) := rfl lemma some_eq_coe (a : α) : (some a : with_top α) = (↑a : with_top α) := rfl theorem coe_eq_coe {a b : α} : (a : with_top α) = b ↔ a = b := by rw [← option.some.inj_eq a b]; refl @[simp] theorem top_ne_coe {a : α} : ⊤ ≠ (a : with_top α) . @[simp] theorem coe_ne_top {a : α} : (a : with_top α) ≠ ⊤ . @[priority 10] instance has_lt [has_lt α] : has_lt (with_top α) := { lt := λ o₁ o₂ : option α, ∃ b ∈ o₁, ∀ a ∈ o₂, b < a } @[priority 10] instance has_le [has_le α] : has_le (with_top α) := { le := λ o₁ o₂ : option α, ∀ a ∈ o₂, ∃ b ∈ o₁, b ≤ a } @[simp] theorem some_lt_some [has_lt α] {a b : α} : @has_lt.lt (with_top α) _ (some a) (some b) ↔ a < b := by simp [(<)] @[simp] theorem some_le_some [has_le α] {a b : α} : @has_le.le (with_top α) _ (some a) (some b) ↔ a ≤ b := by simp [(≤)] @[simp] theorem none_le [has_le α] {a : with_top α} : @has_le.le (with_top α) _ a none := by simp [(≤)] @[simp] theorem none_lt_some [has_lt α] {a : α} : @has_lt.lt (with_top α) _ (some a) none := by simp [(<)]; existsi a; refl instance [preorder α] : preorder (with_top α) := { le := λ o₁ o₂ : option α, ∀ a ∈ o₂, ∃ b ∈ o₁, b ≤ a, lt := (<), lt_iff_le_not_le := by { intros; cases a; cases b; simp [lt_iff_le_not_le]; simp [(<),(≤)] }, le_refl := λ o a ha, ⟨a, ha, le_refl _⟩, le_trans := λ o₁ o₂ o₃ h₁ h₂ c hc, let ⟨b, hb, bc⟩ := h₂ c hc, ⟨a, ha, ab⟩ := h₁ b hb in ⟨a, ha, le_trans ab bc⟩, } instance partial_order [partial_order α] : partial_order (with_top α) := { le_antisymm := λ o₁ o₂ h₁ h₂, begin cases o₂ with b, { cases o₁ with a, {refl}, rcases h₂ a rfl with ⟨_, ⟨⟩, _⟩ }, { rcases h₁ b rfl with ⟨a, ⟨⟩, h₁'⟩, rcases h₂ a rfl with ⟨_, ⟨⟩, h₂'⟩, rw le_antisymm h₁' h₂' } end, .. with_top.preorder } instance order_top [partial_order α] : order_top (with_top α) := { le_top := λ a a' h, option.no_confusion h, ..with_top.partial_order, .. with_top.has_top } @[simp] theorem coe_le_coe [partial_order α] {a b : α} : (a : with_top α) ≤ b ↔ a ≤ b := ⟨λ h, by rcases h b rfl with ⟨_, ⟨⟩, h⟩; exact h, λ h a' e, option.some_inj.1 e ▸ ⟨a, rfl, h⟩⟩ theorem le_coe [partial_order α] {a b : α} : ∀ {o : option α}, a ∈ o → (@has_le.le (with_top α) _ o b ↔ a ≤ b) | _ rfl := coe_le_coe theorem le_coe_iff [partial_order α] (b : α) : ∀(x : with_top α), x ≤ b ↔ (∃a:α, x = a ∧ a ≤ b) | (some a) := by simp [some_eq_coe, coe_eq_coe] | none := by simp [none_eq_top] theorem coe_le_iff [partial_order α] (a : α) : ∀(x : with_top α), ↑a ≤ x ↔ (∀b:α, x = ↑b → a ≤ b) | (some b) := by simp [some_eq_coe, coe_eq_coe] | none := by simp [none_eq_top] theorem lt_iff_exists_coe [partial_order α] : ∀(a b : with_top α), a < b ↔ (∃p:α, a = p ∧ ↑p < b) | (some a) b := by simp [some_eq_coe, coe_eq_coe] | none b := by simp [none_eq_top] lemma coe_lt_coe [partial_order α] {a b : α} : (a : with_top α) < b ↔ a < b := some_lt_some lemma coe_lt_top [partial_order α] (a : α) : (a : with_top α) < ⊤ := lt_of_le_of_ne le_top (λ h, option.no_confusion h) lemma not_top_le_coe [partial_order α] (a : α) : ¬ (⊤:with_top α) ≤ ↑a := assume h, (lt_irrefl ⊤ (lt_of_le_of_lt h (coe_lt_top a))).elim instance linear_order [linear_order α] : linear_order (with_top α) := { le_total := λ o₁ o₂, begin cases o₁ with a, {exact or.inr le_top}, cases o₂ with b, {exact or.inl le_top}, simp [le_total] end, ..with_top.partial_order } instance decidable_linear_order [decidable_linear_order α] : decidable_linear_order (with_top α) := { decidable_le := λ a b, begin cases b with b, { exact is_true le_top }, cases a with a, { exact is_false (mt (le_antisymm le_top) (by simp)) }, { exact decidable_of_iff _ some_le_some } end, ..with_top.linear_order } instance semilattice_inf [semilattice_inf α] : semilattice_inf_top (with_top α) := { inf := option.lift_or_get (⊓), inf_le_left := λ o₁ o₂ a ha, by cases ha; cases o₂; simp [option.lift_or_get], inf_le_right := λ o₁ o₂ a ha, by cases ha; cases o₁; simp [option.lift_or_get], le_inf := λ o₁ o₂ o₃ h₁ h₂ a ha, begin cases o₂ with b; cases o₃ with c; cases ha, { exact h₂ a rfl }, { exact h₁ a rfl }, { rcases h₁ b rfl with ⟨d, ⟨⟩, h₁'⟩, simp at h₂, exact ⟨d, rfl, le_inf h₁' h₂⟩ } end, ..with_top.order_top } lemma coe_inf [semilattice_inf α] (a b : α) : ((a ⊓ b : α) : with_top α) = a ⊓ b := rfl instance semilattice_sup [semilattice_sup α] : semilattice_sup_top (with_top α) := { sup := λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a ⊔ b)), le_sup_left := λ o₁ o₂ a ha, begin simp at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩, exact ⟨_, rfl, le_sup_left⟩ end, le_sup_right := λ o₁ o₂ a ha, begin simp at ha, rcases ha with ⟨b, rfl, c, rfl, rfl⟩, exact ⟨_, rfl, le_sup_right⟩ end, sup_le := λ o₁ o₂ o₃ h₁ h₂ a ha, begin cases ha, rcases h₁ a rfl with ⟨b, ⟨⟩, ab⟩, rcases h₂ a rfl with ⟨c, ⟨⟩, ac⟩, exact ⟨_, rfl, sup_le ab ac⟩ end, ..with_top.order_top } lemma coe_sup [semilattice_sup α] (a b : α) : ((a ⊔ b : α) : with_top α) = a ⊔ b := rfl instance lattice [lattice α] : lattice (with_top α) := { ..with_top.semilattice_sup, ..with_top.semilattice_inf } theorem lattice_eq_DLO [decidable_linear_order α] : lattice.lattice_of_decidable_linear_order = @with_top.lattice α _ := lattice.ext $ λ x y, iff.rfl theorem sup_eq_max [decidable_linear_order α] (x y : with_top α) : x ⊔ y = max x y := by rw [← sup_eq_max, lattice_eq_DLO] theorem inf_eq_min [decidable_linear_order α] (x y : with_top α) : x ⊓ y = min x y := by rw [← inf_eq_min, lattice_eq_DLO] instance order_bot [order_bot α] : order_bot (with_top α) := { bot := some ⊥, bot_le := λ o a ha, by cases ha; exact ⟨_, rfl, bot_le⟩, ..with_top.partial_order } instance bounded_lattice [bounded_lattice α] : bounded_lattice (with_top α) := { ..with_top.lattice, ..with_top.order_top, ..with_top.order_bot } lemma well_founded_lt {α : Type*} [partial_order α] (h : well_founded ((<) : α → α → Prop)) : well_founded ((<) : with_top α → with_top α → Prop) := have acc_some : ∀ a : α, acc ((<) : with_top α → with_top α → Prop) (some a) := λ a, acc.intro _ (well_founded.induction h a (show ∀ b, (∀ c, c < b → ∀ d : with_top α, d < some c → acc (<) d) → ∀ y : with_top α, y < some b → acc (<) y, from λ b ih c, option.rec_on c (λ hc, (not_lt_of_ge lattice.le_top hc).elim) (λ c hc, acc.intro _ (ih _ (some_lt_some.1 hc))))), ⟨λ a, option.rec_on a (acc.intro _ (λ y, option.rec_on y (λ h, (lt_irrefl _ h).elim) (λ _ _, acc_some _))) acc_some⟩ instance densely_ordered [partial_order α] [densely_ordered α] [no_top_order α] : densely_ordered (with_top α) := ⟨ assume a b, match a, b with | none, a := assume h : ⊤ < a, (not_top_lt h).elim | some a, none := assume h, let ⟨b, hb⟩ := no_top a in ⟨b, coe_lt_coe.2 hb, coe_lt_top b⟩ | some a, some b := assume h, let ⟨a, ha₁, ha₂⟩ := dense (coe_lt_coe.1 h) in ⟨a, coe_lt_coe.2 ha₁, coe_lt_coe.2 ha₂⟩ end⟩ lemma dense_coe [partial_order α] [densely_ordered α] [no_top_order α] {a b : with_top α} (h : a < b) : ∃ x : α, a < ↑x ∧ ↑x < b := let ⟨y, hy⟩ := dense h, ⟨x, hx⟩ := (lt_iff_exists_coe _ _).1 hy.2 in ⟨x, hx.1 ▸ hy⟩ end with_top namespace order_dual open lattice variable (α : Type*) instance [has_bot α] : has_top (order_dual α) := ⟨(⊥ : α)⟩ instance [has_top α] : has_bot (order_dual α) := ⟨(⊤ : α)⟩ instance [order_bot α] : order_top (order_dual α) := { le_top := @bot_le α _, .. order_dual.partial_order α, .. order_dual.lattice.has_top α } instance [order_top α] : order_bot (order_dual α) := { bot_le := @le_top α _, .. order_dual.partial_order α, .. order_dual.lattice.has_bot α } instance [semilattice_inf_bot α] : semilattice_sup_top (order_dual α) := { .. order_dual.lattice.semilattice_sup α, .. order_dual.lattice.order_top α } instance [semilattice_inf_top α] : semilattice_sup_bot (order_dual α) := { .. order_dual.lattice.semilattice_sup α, .. order_dual.lattice.order_bot α } instance [semilattice_sup_bot α] : semilattice_inf_top (order_dual α) := { .. order_dual.lattice.semilattice_inf α, .. order_dual.lattice.order_top α } instance [semilattice_sup_top α] : semilattice_inf_bot (order_dual α) := { .. order_dual.lattice.semilattice_inf α, .. order_dual.lattice.order_bot α } instance [bounded_lattice α] : bounded_lattice (order_dual α) := { .. order_dual.lattice.lattice α, .. order_dual.lattice.order_top α, .. order_dual.lattice.order_bot α } instance [bounded_distrib_lattice α] : bounded_distrib_lattice (order_dual α) := { .. order_dual.lattice.bounded_lattice α, .. order_dual.lattice.distrib_lattice α } end order_dual namespace prod open lattice variables (α : Type u) (β : Type v) instance [has_top α] [has_top β] : has_top (α × β) := ⟨⟨⊤, ⊤⟩⟩ instance [has_bot α] [has_bot β] : has_bot (α × β) := ⟨⟨⊥, ⊥⟩⟩ instance [order_top α] [order_top β] : order_top (α × β) := { le_top := assume a, ⟨le_top, le_top⟩, .. prod.partial_order α β, .. prod.lattice.has_top α β } instance [order_bot α] [order_bot β] : order_bot (α × β) := { bot_le := assume a, ⟨bot_le, bot_le⟩, .. prod.partial_order α β, .. prod.lattice.has_bot α β } instance [semilattice_sup_top α] [semilattice_sup_top β] : semilattice_sup_top (α × β) := { .. prod.lattice.semilattice_sup α β, .. prod.lattice.order_top α β } instance [semilattice_inf_top α] [semilattice_inf_top β] : semilattice_inf_top (α × β) := { .. prod.lattice.semilattice_inf α β, .. prod.lattice.order_top α β } instance [semilattice_sup_bot α] [semilattice_sup_bot β] : semilattice_sup_bot (α × β) := { .. prod.lattice.semilattice_sup α β, .. prod.lattice.order_bot α β } instance [semilattice_inf_bot α] [semilattice_inf_bot β] : semilattice_inf_bot (α × β) := { .. prod.lattice.semilattice_inf α β, .. prod.lattice.order_bot α β } instance [bounded_lattice α] [bounded_lattice β] : bounded_lattice (α × β) := { .. prod.lattice.lattice α β, .. prod.lattice.order_top α β, .. prod.lattice.order_bot α β } instance [bounded_distrib_lattice α] [bounded_distrib_lattice β] : bounded_distrib_lattice (α × β) := { .. prod.lattice.bounded_lattice α β, .. prod.lattice.distrib_lattice α β } end prod
e34e9ab6f1128c0add6c31f93cde9f1d1eaf4ac3
137c667471a40116a7afd7261f030b30180468c2
/src/algebra/big_operators/finprod.lean
be06afa68be1f30ab01b72ac228cc831ae343bf6
[ "Apache-2.0" ]
permissive
bragadeesh153/mathlib
46bf814cfb1eecb34b5d1549b9117dc60f657792
b577bb2cd1f96eb47031878256856020b76f73cd
refs/heads/master
1,687,435,188,334
1,626,384,207,000
1,626,384,207,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
36,792
lean
/- Copyright (c) 2020 Kexing Ying and Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying, Kevin Buzzard, Yury Kudryashov -/ import data.set.finite import data.set.disjointed import algebra.big_operators import algebra.indicator_function /-! # Finite products and sums over types and sets We define products and sums over types and subsets of types, with no finiteness hypotheses. All infinite products and sums are defined to be junk values (i.e. one or zero). This approach is sometimes easier to use than `finset.sum`, when issues arise with `finset` and `fintype` being data. ## Main definitions We use the following variables: * `α`, `β` - types with no structure; * `s`, `t` - sets * `M`, `N` - additive or multiplicative commutative monoids * `f`, `g` - functions Definitions in this file: * `finsum f : M` : the sum of `f x` as `x` ranges over the support of `f`, if it's finite. Zero otherwise. * `finprod f : M` : the product of `f x` as `x` ranges over the multiplicative support of `f`, if it's finite. One otherwise. ## Notation * `∑ᶠ i, f i` and `∑ᶠ i : α, f i` for `finsum f` * `∏ᶠ i, f i` and `∏ᶠ i : α, f i` for `finprod f` This notation works for functions `f : p → M`, where `p : Prop`, so the following works: * `∑ᶠ i ∈ s, f i`, where `f : α → M`, `s : set α` : sum over the set `s`; * `∑ᶠ n < 5, f n`, where `f : ℕ → M` : same as `f 0 + f 1 + f 2 + f 3 + f 4`; * `∏ᶠ (n >= -2) (hn : n < 3), f n`, where `f : ℤ → M` : same as `f (-2) * f (-1) * f 0 * f 1 * f 2`. ## Implementation notes `finsum` and `finprod` is "yet another way of doing finite sums and products in Lean". However experiments in the wild (e.g. with matroids) indicate that it is a helpful approach in settings where the user is not interested in computability and wants to do reasoning without running into typeclass diamonds caused by the constructive finiteness used in definitions such as `finset` and `fintype`. By sticking solely to `set.finite` we avoid these problems. We are aware that there are other solutions but for beginner mathematicians this approach is easier in practice. Another application is the construction of a partition of unity from a collection of “bump” function. In this case the finite set depends on the point and it's convenient to have a definition that does not mention the set explicitly. The first arguments in all definitions and lemmas is the codomain of the function of the big operator. This is necessary for the heuristic in `@[to_additive]`. See the documentation of `to_additive.attr` for more information. ## Todo We did not add `is_finite (X : Type) : Prop`, because it is simply `nonempty (fintype X)`. There is work on `fincard` in the pipeline, which returns the cardinality of `X` if it is finite, and 0 otherwise. ## Tags finsum, finprod, finite sum, finite product -/ open function set /-! ### Definition and relation to `finset.sum` and `finset.prod` -/ section sort variables {M N : Type*} {α β ι : Sort*} [comm_monoid M] [comm_monoid N] open_locale big_operators section /- Note: we use classical logic only for these definitions, to ensure that we do not write lemmas with `classical.dec` in their statement. -/ open_locale classical /-- Sum of `f x` as `x` ranges over the elements of the support of `f`, if it's finite. Zero otherwise. -/ @[irreducible] noncomputable def finsum {M α} [add_comm_monoid M] (f : α → M) : M := if h : finite (support (f ∘ plift.down)) then ∑ i in h.to_finset, f i.down else 0 /-- Product of `f x` as `x` ranges over the elements of the multiplicative support of `f`, if it's finite. One otherwise. -/ @[irreducible, to_additive] noncomputable def finprod (f : α → M) : M := if h : finite (mul_support (f ∘ plift.down)) then ∏ i in h.to_finset, f i.down else 1 end localized "notation `∑ᶠ` binders `, ` r:(scoped:67 f, finsum f) := r" in big_operators localized "notation `∏ᶠ` binders `, ` r:(scoped:67 f, finprod f) := r" in big_operators @[to_additive] lemma finprod_eq_prod_plift_of_mul_support_to_finset_subset {f : α → M} (hf : finite (mul_support (f ∘ plift.down))) {s : finset (plift α)} (hs : hf.to_finset ⊆ s) : ∏ᶠ i, f i = ∏ i in s, f i.down := begin rw [finprod, dif_pos], refine finset.prod_subset hs (λ x hx hxf, _), rwa [hf.mem_to_finset, nmem_mul_support] at hxf end @[to_additive] lemma finprod_eq_prod_plift_of_mul_support_subset {f : α → M} {s : finset (plift α)} (hs : mul_support (f ∘ plift.down) ⊆ s) : ∏ᶠ i, f i = ∏ i in s, f i.down := finprod_eq_prod_plift_of_mul_support_to_finset_subset (s.finite_to_set.subset hs) $ λ x hx, by { rw finite.mem_to_finset at hx, exact hs hx } @[simp, to_additive] lemma finprod_one : ∏ᶠ i : α, (1 : M) = 1 := begin have : mul_support (λ x : plift α, (λ _, 1 : α → M) x.down) ⊆ (∅ : finset (plift α)), from λ x h, h rfl, rw [finprod_eq_prod_plift_of_mul_support_subset this, finset.prod_empty] end @[to_additive] lemma finprod_of_is_empty [is_empty α] (f : α → M) : ∏ᶠ i, f i = 1 := by { rw ← finprod_one, congr } @[simp, to_additive] lemma finprod_false (f : false → M) : ∏ᶠ i, f i = 1 := finprod_of_is_empty _ @[to_additive] lemma finprod_eq_single (f : α → M) (a : α) (ha : ∀ x ≠ a, f x = 1) : ∏ᶠ x, f x = f a := begin have : mul_support (f ∘ plift.down) ⊆ ({plift.up a} : finset (plift α)), { intro x, contrapose, simpa [plift.eq_up_iff_down_eq] using ha x.down }, rw [finprod_eq_prod_plift_of_mul_support_subset this, finset.prod_singleton], end @[to_additive] lemma finprod_unique [unique α] (f : α → M) : ∏ᶠ i, f i = f (default α) := finprod_eq_single f (default α) $ λ x hx, (hx $ unique.eq_default _).elim @[simp, to_additive] lemma finprod_true (f : true → M) : ∏ᶠ i, f i = f trivial := @finprod_unique M true _ ⟨⟨trivial⟩, λ _, rfl⟩ f @[to_additive] lemma finprod_eq_dif {p : Prop} [decidable p] (f : p → M) : ∏ᶠ i, f i = if h : p then f h else 1 := begin split_ifs, { haveI : unique p := ⟨⟨h⟩, λ _, rfl⟩, exact finprod_unique f }, { haveI : is_empty p := ⟨h⟩, exact finprod_of_is_empty f } end @[to_additive] lemma finprod_eq_if {p : Prop} [decidable p] {x : M} : ∏ᶠ i : p, x = if p then x else 1 := finprod_eq_dif (λ _, x) @[to_additive] lemma finprod_congr {f g : α → M} (h : ∀ x, f x = g x) : finprod f = finprod g := congr_arg _ $ funext h @[congr, to_additive] lemma finprod_congr_Prop {p q : Prop} {f : p → M} {g : q → M} (hpq : p = q) (hfg : ∀ h : q, f (hpq.mpr h) = g h) : finprod f = finprod g := by { subst q, exact finprod_congr hfg } attribute [congr] finsum_congr_Prop /-- To prove a property of a finite product, it suffices to prove that the property is multiplicative and holds on multipliers. -/ @[to_additive] lemma finprod_induction {f : α → M} (p : M → Prop) (hp₀ : p 1) (hp₁ : ∀ x y, p x → p y → p (x * y)) (hp₂ : ∀ i, p (f i)) : p (∏ᶠ i, f i) := begin rw finprod, split_ifs, exacts [finset.prod_induction _ _ hp₁ hp₀ (λ i hi, hp₂ _), hp₀] end /-- To prove a property of a finite sum, it suffices to prove that the property is additive and holds on summands. -/ add_decl_doc finsum_induction lemma finprod_nonneg {R : Type*} [ordered_comm_semiring R] {f : α → R} (hf : ∀ x, 0 ≤ f x) : 0 ≤ ∏ᶠ x, f x := finprod_induction (λ x, 0 ≤ x) zero_le_one (λ x y, mul_nonneg) hf @[to_additive finsum_nonneg] lemma one_le_finprod' {M : Type*} [ordered_comm_monoid M] {f : α → M} (hf : ∀ i, 1 ≤ f i) : 1 ≤ ∏ᶠ i, f i := finprod_induction _ le_rfl (λ _ _, one_le_mul) hf @[to_additive] lemma monoid_hom.map_finprod_plift (f : M →* N) (g : α → M) (h : finite (mul_support $ g ∘ plift.down)) : f (∏ᶠ x, g x) = ∏ᶠ x, f (g x) := begin rw [finprod_eq_prod_plift_of_mul_support_subset h.coe_to_finset.ge, finprod_eq_prod_plift_of_mul_support_subset, f.map_prod], rw [h.coe_to_finset], exact mul_support_comp_subset f.map_one (g ∘ plift.down) end @[to_additive] lemma monoid_hom.map_finprod_Prop {p : Prop} (f : M →* N) (g : p → M) : f (∏ᶠ x, g x) = ∏ᶠ x, f (g x) := f.map_finprod_plift g (finite.of_fintype _) end sort section type variables {α β ι M N : Type*} [comm_monoid M] [comm_monoid N] open_locale big_operators @[to_additive] lemma finprod_eq_mul_indicator_apply (s : set α) (f : α → M) (a : α) : ∏ᶠ (h : a ∈ s), f a = mul_indicator s f a := by convert finprod_eq_if @[simp, to_additive] lemma finprod_mem_mul_support (f : α → M) (a : α) : ∏ᶠ (h : f a ≠ 1), f a = f a := by rw [← mem_mul_support, finprod_eq_mul_indicator_apply, mul_indicator_mul_support] @[to_additive] lemma finprod_mem_def (s : set α) (f : α → M) : ∏ᶠ a ∈ s, f a = ∏ᶠ a, mul_indicator s f a := finprod_congr $ finprod_eq_mul_indicator_apply s f @[to_additive] lemma finprod_eq_prod_of_mul_support_subset (f : α → M) {s : finset α} (h : mul_support f ⊆ s) : ∏ᶠ i, f i = ∏ i in s, f i := begin have A : mul_support (f ∘ plift.down) = equiv.plift.symm '' mul_support f, { rw mul_support_comp_eq_preimage, exact (equiv.plift.symm.image_eq_preimage _).symm }, have : mul_support (f ∘ plift.down) ⊆ s.map equiv.plift.symm.to_embedding, { rw [A, finset.coe_map], exact image_subset _ h }, rw [finprod_eq_prod_plift_of_mul_support_subset this], simp end @[to_additive] lemma finprod_eq_prod_of_mul_support_to_finset_subset (f : α → M) (hf : finite (mul_support f)) {s : finset α} (h : hf.to_finset ⊆ s) : ∏ᶠ i, f i = ∏ i in s, f i := finprod_eq_prod_of_mul_support_subset _ $ λ x hx, h $ hf.mem_to_finset.2 hx @[to_additive] lemma finprod_def (f : α → M) [decidable (mul_support f).finite] : ∏ᶠ i : α, f i = if h : (mul_support f).finite then ∏ i in h.to_finset, f i else 1 := begin split_ifs, { exact finprod_eq_prod_of_mul_support_to_finset_subset _ h (finset.subset.refl _) }, { rw [finprod, dif_neg], rw [mul_support_comp_eq_preimage], exact mt (λ hf, hf.of_preimage equiv.plift.surjective) h} end @[to_additive] lemma finprod_of_infinite_mul_support {f : α → M} (hf : (mul_support f).infinite) : ∏ᶠ i, f i = 1 := by { classical, rw [finprod_def, dif_neg hf] } @[to_additive] lemma finprod_eq_prod (f : α → M) (hf : (mul_support f).finite) : ∏ᶠ i : α, f i = ∏ i in hf.to_finset, f i := by { classical, rw [finprod_def, dif_pos hf] } @[to_additive] lemma finprod_eq_prod_of_fintype [fintype α] (f : α → M) : ∏ᶠ i : α, f i = ∏ i, f i := finprod_eq_prod_of_mul_support_to_finset_subset _ (finite.of_fintype _) $ finset.subset_univ _ @[to_additive] lemma finprod_cond_eq_prod_of_cond_iff (f : α → M) {p : α → Prop} {t : finset α} (h : ∀ {x}, f x ≠ 1 → (p x ↔ x ∈ t)) : ∏ᶠ i (hi : p i), f i = ∏ i in t, f i := begin set s := {x | p x}, have : mul_support (s.mul_indicator f) ⊆ t, { rw [set.mul_support_mul_indicator], intros x hx, exact (h hx.2).1 hx.1 }, erw [finprod_mem_def, finprod_eq_prod_of_mul_support_subset _ this], refine finset.prod_congr rfl (λ x hx, mul_indicator_apply_eq_self.2 $ λ hxs, _), contrapose! hxs, exact (h hxs).2 hx end @[to_additive] lemma finprod_mem_eq_prod_of_inter_mul_support_eq (f : α → M) {s : set α} {t : finset α} (h : s ∩ mul_support f = t ∩ mul_support f) : ∏ᶠ i ∈ s, f i = ∏ i in t, f i := finprod_cond_eq_prod_of_cond_iff _ $ by simpa [set.ext_iff] using h @[to_additive] lemma finprod_mem_eq_prod_of_subset (f : α → M) {s : set α} {t : finset α} (h₁ : s ∩ mul_support f ⊆ t) (h₂ : ↑t ⊆ s) : ∏ᶠ i ∈ s, f i = ∏ i in t, f i := finprod_cond_eq_prod_of_cond_iff _ $ λ x hx, ⟨λ h, h₁ ⟨h, hx⟩, λ h, h₂ h⟩ @[to_additive] lemma finprod_mem_eq_prod (f : α → M) {s : set α} (hf : (s ∩ mul_support f).finite) : ∏ᶠ i ∈ s, f i = ∏ i in hf.to_finset, f i := finprod_mem_eq_prod_of_inter_mul_support_eq _ $ by simp [inter_assoc] @[to_additive] lemma finprod_mem_eq_prod_filter (f : α → M) (s : set α) [decidable_pred (∈ s)] (hf : (mul_support f).finite) : ∏ᶠ i ∈ s, f i = ∏ i in finset.filter (∈ s) hf.to_finset, f i := finprod_mem_eq_prod_of_inter_mul_support_eq _ $ by simp [inter_comm, inter_left_comm] @[to_additive] lemma finprod_mem_eq_to_finset_prod (f : α → M) (s : set α) [fintype s] : ∏ᶠ i ∈ s, f i = ∏ i in s.to_finset, f i := finprod_mem_eq_prod_of_inter_mul_support_eq _ $ by rw [coe_to_finset] @[to_additive] lemma finprod_mem_eq_finite_to_finset_prod (f : α → M) {s : set α} (hs : s.finite) : ∏ᶠ i ∈ s, f i = ∏ i in hs.to_finset, f i := finprod_mem_eq_prod_of_inter_mul_support_eq _ $ by rw [hs.coe_to_finset] @[to_additive] lemma finprod_mem_finset_eq_prod (f : α → M) (s : finset α) : ∏ᶠ i ∈ s, f i = ∏ i in s, f i := finprod_mem_eq_prod_of_inter_mul_support_eq _ rfl @[to_additive] lemma finprod_mem_coe_finset (f : α → M) (s : finset α) : ∏ᶠ i ∈ (s : set α), f i = ∏ i in s, f i := finprod_mem_eq_prod_of_inter_mul_support_eq _ rfl @[to_additive] lemma finprod_mem_eq_one_of_infinite {f : α → M} {s : set α} (hs : (s ∩ mul_support f).infinite) : ∏ᶠ i ∈ s, f i = 1 := begin rw finprod_mem_def, apply finprod_of_infinite_mul_support, rwa [← mul_support_mul_indicator] at hs end @[to_additive] lemma finprod_mem_inter_mul_support (f : α → M) (s : set α) : ∏ᶠ i ∈ (s ∩ mul_support f), f i = ∏ᶠ i ∈ s, f i := by rw [finprod_mem_def, finprod_mem_def, mul_indicator_inter_mul_support] @[to_additive] lemma finprod_mem_inter_mul_support_eq (f : α → M) (s t : set α) (h : s ∩ mul_support f = t ∩ mul_support f) : ∏ᶠ i ∈ s, f i = ∏ᶠ i ∈ t, f i := by rw [← finprod_mem_inter_mul_support, h, finprod_mem_inter_mul_support] @[to_additive] lemma finprod_mem_inter_mul_support_eq' (f : α → M) (s t : set α) (h : ∀ x ∈ mul_support f, x ∈ s ↔ x ∈ t) : ∏ᶠ i ∈ s, f i = ∏ᶠ i ∈ t, f i := begin apply finprod_mem_inter_mul_support_eq, ext x, exact and_congr_left (h x) end @[to_additive] lemma finprod_mem_univ (f : α → M) : ∏ᶠ i ∈ @set.univ α, f i = ∏ᶠ i : α, f i := finprod_congr $ λ i, finprod_true _ variables {f g : α → M} {a b : α} {s t : set α} @[to_additive] lemma finprod_mem_congr (h₀ : s = t) (h₁ : ∀ x ∈ t, f x = g x) : ∏ᶠ i ∈ s, f i = ∏ᶠ i ∈ t, g i := h₀.symm ▸ (finprod_congr $ λ i, finprod_congr_Prop rfl (h₁ i)) /-! ### Distributivity w.r.t. addition, subtraction, and (scalar) multiplication -/ /-- If the multiplicative supports of `f` and `g` are finite, then the product of `f i * g i` equals the product of `f i` multiplied by the product over `g i`. -/ @[to_additive] lemma finprod_mul_distrib (hf : (mul_support f).finite) (hg : (mul_support g).finite) : ∏ᶠ i, (f i * g i) = (∏ᶠ i, f i) * ∏ᶠ i, g i := begin classical, rw [finprod_eq_prod_of_mul_support_to_finset_subset _ hf (finset.subset_union_left _ _), finprod_eq_prod_of_mul_support_to_finset_subset _ hg (finset.subset_union_right _ _), ← finset.prod_mul_distrib], refine finprod_eq_prod_of_mul_support_subset _ _, simp [mul_support_mul] end /-- A more general version of `finprod_mem_mul_distrib` that requires `s ∩ mul_support f` and `s ∩ mul_support g` instead of `s` to be finite. -/ @[to_additive] lemma finprod_mem_mul_distrib' (hf : (s ∩ mul_support f).finite) (hg : (s ∩ mul_support g).finite) : ∏ᶠ i ∈ s, (f i * g i) = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ s, g i := begin rw [← mul_support_mul_indicator] at hf hg, simp only [finprod_mem_def, mul_indicator_mul, finprod_mul_distrib hf hg] end /-- The product of constant one over any set equals one. -/ @[to_additive] lemma finprod_mem_one (s : set α) : ∏ᶠ i ∈ s, (1 : M) = 1 := by simp /-- If a function `f` equals one on a set `s`, then the product of `f i` over `i ∈ s` equals one. -/ @[to_additive] lemma finprod_mem_of_eq_on_one (hf : eq_on f 1 s) : ∏ᶠ i ∈ s, f i = 1 := by { rw ← finprod_mem_one s, exact finprod_mem_congr rfl hf } /-- If the product of `f i` over `i ∈ s` is not equal to one, then there is some `x ∈ s` such that `f x ≠ 1`. -/ @[to_additive] lemma exists_ne_one_of_finprod_mem_ne_one (h : ∏ᶠ i ∈ s, f i ≠ 1) : ∃ x ∈ s, f x ≠ 1 := begin by_contra h', push_neg at h', exact h (finprod_mem_of_eq_on_one h') end /-- Given a finite set `s`, the product of `f i * g i` over `i ∈ s` equals the product of `f i` over `i ∈ s` times the product of `g i` over `i ∈ s`. -/ @[to_additive] lemma finprod_mem_mul_distrib (hs : s.finite) : ∏ᶠ i ∈ s, (f i * g i) = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ s, g i := finprod_mem_mul_distrib' (hs.inter_of_left _) (hs.inter_of_left _) @[to_additive] lemma monoid_hom.map_finprod {f : α → M} (g : M →* N) (hf : (mul_support f).finite) : g (∏ᶠ i, f i) = ∏ᶠ i, g (f i) := g.map_finprod_plift f $ hf.preimage $ equiv.plift.injective.inj_on _ /-- A more general version of `monoid_hom.map_finprod_mem` that requires `s ∩ mul_support f` and instead of `s` to be finite. -/ @[to_additive] lemma monoid_hom.map_finprod_mem' {f : α → M} (g : M →* N) (h₀ : (s ∩ mul_support f).finite) : g (∏ᶠ j ∈ s, f j) = ∏ᶠ i ∈ s, (g (f i)) := begin rw [g.map_finprod], { simp only [g.map_finprod_Prop] }, { simpa only [finprod_eq_mul_indicator_apply, mul_support_mul_indicator] } end /-- Given a monoid homomorphism `g : M →* N`, and a function `f : α → M`, the value of `g` at the product of `f i` over `i ∈ s` equals the product of `(g ∘ f) i` over `s`. -/ @[to_additive] lemma monoid_hom.map_finprod_mem (f : α → M) (g : M →* N) (hs : s.finite) : g (∏ᶠ j ∈ s, f j) = ∏ᶠ i ∈ s, g (f i) := g.map_finprod_mem' (hs.inter_of_left _) /-! ### `∏ᶠ x ∈ s, f x` and set operations -/ /-- The product of any function over an empty set is one. -/ @[to_additive] lemma finprod_mem_empty : ∏ᶠ i ∈ (∅ : set α), f i = 1 := by simp /-- A set `s` is not empty if the product of some function over `s` is not equal to one. -/ @[to_additive] lemma nonempty_of_finprod_mem_ne_one (h : ∏ᶠ i ∈ s, f i ≠ 1) : s.nonempty := ne_empty_iff_nonempty.1 $ λ h', h $ h'.symm ▸ finprod_mem_empty /-- Given finite sets `s` and `t`, the product of `f i` over `i ∈ s ∪ t` times the product of `f i` over `i ∈ s ∩ t` equals the product of `f i` over `i ∈ s` times the product of `f i` over `i ∈ t`. -/ @[to_additive] lemma finprod_mem_union_inter (hs : s.finite) (ht : t.finite) : (∏ᶠ i ∈ s ∪ t, f i) * ∏ᶠ i ∈ s ∩ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := begin unfreezingI { lift s to finset α using hs, lift t to finset α using ht }, classical, rw [← finset.coe_union, ← finset.coe_inter], simp only [finprod_mem_coe_finset, finset.prod_union_inter] end /-- A more general version of `finprod_mem_union_inter` that requires `s ∩ mul_support f` and `t ∩ mul_support f` instead of `s` and `t` to be finite. -/ @[to_additive] lemma finprod_mem_union_inter' (hs : (s ∩ mul_support f).finite) (ht : (t ∩ mul_support f).finite) : (∏ᶠ i ∈ s ∪ t, f i) * ∏ᶠ i ∈ s ∩ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := begin rw [← finprod_mem_inter_mul_support f s, ← finprod_mem_inter_mul_support f t, ← finprod_mem_union_inter hs ht, ← union_inter_distrib_right, finprod_mem_inter_mul_support, ← finprod_mem_inter_mul_support f (s ∩ t)], congr' 2, rw [inter_left_comm, inter_assoc, inter_assoc, inter_self, inter_left_comm] end /-- A more general version of `finprod_mem_union` that requires `s ∩ mul_support f` and `t ∩ mul_support f` instead of `s` and `t` to be finite. -/ @[to_additive] lemma finprod_mem_union' (hst : disjoint s t) (hs : (s ∩ mul_support f).finite) (ht : (t ∩ mul_support f).finite) : ∏ᶠ i ∈ s ∪ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := by rw [← finprod_mem_union_inter' hs ht, disjoint_iff_inter_eq_empty.1 hst, finprod_mem_empty, mul_one] /-- Given two finite disjoint sets `s` and `t`, the product of `f i` over `i ∈ s ∪ t` equals the product of `f i` over `i ∈ s` times the product of `f i` over `i ∈ t`. -/ @[to_additive] lemma finprod_mem_union (hst : disjoint s t) (hs : s.finite) (ht : t.finite) : ∏ᶠ i ∈ s ∪ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := finprod_mem_union' hst (hs.inter_of_left _) (ht.inter_of_left _) /-- A more general version of `finprod_mem_union'` that requires `s ∩ mul_support f` and `t ∩ mul_support f` instead of `s` and `t` to be disjoint -/ @[to_additive] lemma finprod_mem_union'' (hst : disjoint (s ∩ mul_support f) (t ∩ mul_support f)) (hs : (s ∩ mul_support f).finite) (ht : (t ∩ mul_support f).finite) : ∏ᶠ i ∈ s ∪ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := by rw [← finprod_mem_inter_mul_support f s, ← finprod_mem_inter_mul_support f t, ← finprod_mem_union hst hs ht, ← union_inter_distrib_right, finprod_mem_inter_mul_support] /-- The product of `f i` over `i ∈ {a}` equals `f a`. -/ @[to_additive] lemma finprod_mem_singleton : ∏ᶠ i ∈ ({a} : set α), f i = f a := by rw [← finset.coe_singleton, finprod_mem_coe_finset, finset.prod_singleton] @[simp, to_additive] lemma finprod_cond_eq_left : ∏ᶠ i = a, f i = f a := finprod_mem_singleton @[simp, to_additive] lemma finprod_cond_eq_right : ∏ᶠ i (hi : a = i), f i = f a := by simp [@eq_comm _ a] /-- A more general version of `finprod_mem_insert` that requires `s ∩ mul_support f` instead of `s` to be finite. -/ @[to_additive] lemma finprod_mem_insert' (f : α → M) (h : a ∉ s) (hs : (s ∩ mul_support f).finite) : ∏ᶠ i ∈ insert a s, f i = f a * ∏ᶠ i ∈ s, f i := begin rw [insert_eq, finprod_mem_union' _ _ hs, finprod_mem_singleton], { rwa disjoint_singleton_left }, { exact (finite_singleton a).inter_of_left _ } end /-- Given a finite set `s` and an element `a ∉ s`, the product of `f i` over `i ∈ insert a s` equals `f a` times the product of `f i` over `i ∈ s`. -/ @[to_additive] lemma finprod_mem_insert (f : α → M) (h : a ∉ s) (hs : s.finite) : ∏ᶠ i ∈ insert a s, f i = f a * ∏ᶠ i ∈ s, f i := finprod_mem_insert' f h $ hs.inter_of_left _ /-- If `f a = 1` for all `a ∉ s`, then the product of `f i` over `i ∈ insert a s` equals the product of `f i` over `i ∈ s`. -/ @[to_additive] lemma finprod_mem_insert_of_eq_one_if_not_mem (h : a ∉ s → f a = 1) : ∏ᶠ i ∈ (insert a s), f i = ∏ᶠ i ∈ s, f i := begin refine finprod_mem_inter_mul_support_eq' _ _ _ (λ x hx, ⟨_, or.inr⟩), rintro (rfl|hxs), exacts [not_imp_comm.1 h hx, hxs] end /-- If `f a = 1`, then the product of `f i` over `i ∈ insert a s` equals the product of `f i` over `i ∈ s`. -/ @[to_additive] lemma finprod_mem_insert_one (h : f a = 1) : ∏ᶠ i ∈ (insert a s), f i = ∏ᶠ i ∈ s, f i := finprod_mem_insert_of_eq_one_if_not_mem (λ _, h) /-- The product of `f i` over `i ∈ {a, b}`, `a ≠ b`, is equal to `f a * f b`. -/ @[to_additive] lemma finprod_mem_pair (h : a ≠ b) : ∏ᶠ i ∈ ({a, b} : set α), f i = f a * f b := by { rw [finprod_mem_insert, finprod_mem_singleton], exacts [h, finite_singleton b] } /-- The product of `f y` over `y ∈ g '' s` equals the product of `f (g i)` over `s` provided that `g` is injective on `s ∩ mul_support (f ∘ g)`. -/ @[to_additive] lemma finprod_mem_image' {s : set β} {g : β → α} (hg : set.inj_on g (s ∩ mul_support (f ∘ g))) : ∏ᶠ i ∈ (g '' s), f i = ∏ᶠ j ∈ s, f (g j) := begin classical, by_cases hs : finite (s ∩ mul_support (f ∘ g)), { have hg : ∀ (x ∈ hs.to_finset) (y ∈ hs.to_finset), g x = g y → x = y, by simpa only [hs.mem_to_finset], rw [finprod_mem_eq_prod _ hs, ← finset.prod_image hg], refine finprod_mem_eq_prod_of_inter_mul_support_eq f _, rw [finset.coe_image, hs.coe_to_finset, ← image_inter_mul_support_eq, inter_assoc, inter_self] }, { rw [finprod_mem_eq_one_of_infinite hs, finprod_mem_eq_one_of_infinite], rwa [image_inter_mul_support_eq, infinite_image_iff hg] } end /-- The product of `f y` over `y ∈ g '' s` equals the product of `f (g i)` over `s` provided that `g` is injective on `s`. -/ @[to_additive] lemma finprod_mem_image {β} {s : set β} {g : β → α} (hg : set.inj_on g s) : ∏ᶠ i ∈ (g '' s), f i = ∏ᶠ j ∈ s, f (g j) := finprod_mem_image' $ hg.mono $ inter_subset_left _ _ /-- The product of `f y` over `y ∈ set.range g` equals the product of `f (g i)` over all `i` provided that `g` is injective on `mul_support (f ∘ g)`. -/ @[to_additive] lemma finprod_mem_range' {g : β → α} (hg : set.inj_on g (mul_support (f ∘ g))) : ∏ᶠ i ∈ range g, f i = ∏ᶠ j, f (g j) := begin rw [← image_univ, finprod_mem_image', finprod_mem_univ], rwa univ_inter end /-- The product of `f y` over `y ∈ set.range g` equals the product of `f (g i)` over all `i` provided that `g` is injective. -/ @[to_additive] lemma finprod_mem_range {g : β → α} (hg : injective g) : ∏ᶠ i ∈ range g, f i = ∏ᶠ j, f (g j) := finprod_mem_range' (hg.inj_on _) /-- The product of `f i` over `s : set α` is equal to the product of `g j` over `t : set β` if there exists a function `e : α → β` such that `e` is bijective from `s` to `t` and for all `x` in `s` we have `f x = g (e x)`. -/ @[to_additive] lemma finprod_mem_eq_of_bij_on {s : set α} {t : set β} {f : α → M} {g : β → M} (e : α → β) (he₀ : set.bij_on e s t) (he₁ : ∀ x ∈ s, f x = g (e x)) : ∏ᶠ i ∈ s, f i = ∏ᶠ j ∈ t, g j := begin rw [← set.bij_on.image_eq he₀, finprod_mem_image he₀.2.1], exact finprod_mem_congr rfl he₁ end @[to_additive] lemma finprod_set_coe_eq_finprod_mem (s : set α) : ∏ᶠ j : s, f j = ∏ᶠ i ∈ s, f i := begin rw [← finprod_mem_range, subtype.range_coe], exact subtype.coe_injective end @[to_additive] lemma finprod_subtype_eq_finprod_cond (p : α → Prop) : ∏ᶠ j : subtype p, f j = ∏ᶠ i (hi : p i), f i := finprod_set_coe_eq_finprod_mem {i | p i} @[to_additive] lemma finprod_mem_inter_mul_diff' (t : set α) (h : (s ∩ mul_support f).finite) : (∏ᶠ i ∈ s ∩ t, f i) * ∏ᶠ i ∈ s \ t, f i = ∏ᶠ i ∈ s, f i := begin rw [← finprod_mem_union', inter_union_diff], exacts [λ x hx, hx.2.2 hx.1.2, h.subset (λ x hx, ⟨hx.1.1, hx.2⟩), h.subset (λ x hx, ⟨hx.1.1, hx.2⟩)], end @[to_additive] lemma finprod_mem_inter_mul_diff (t : set α) (h : s.finite) : (∏ᶠ i ∈ s ∩ t, f i) * ∏ᶠ i ∈ s \ t, f i = ∏ᶠ i ∈ s, f i := finprod_mem_inter_mul_diff' _ $ h.inter_of_left _ /-- A more general version of `finprod_mem_mul_diff` that requires `t ∩ mul_support f` instead of `t` to be finite. -/ @[to_additive] lemma finprod_mem_mul_diff' (hst : s ⊆ t) (ht : (t ∩ mul_support f).finite) : (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t \ s, f i = ∏ᶠ i ∈ t, f i := by rw [← finprod_mem_inter_mul_diff' _ ht, inter_eq_self_of_subset_right hst] /-- Given a finite set `t` and a subset `s` of `t`, the product of `f i` over `i ∈ s` times the product of `f i` over `t \ s` equals the product of `f i` over `i ∈ t`. -/ @[to_additive] lemma finprod_mem_mul_diff (hst : s ⊆ t) (ht : t.finite) : (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t \ s, f i = ∏ᶠ i ∈ t, f i := finprod_mem_mul_diff' hst (ht.inter_of_left _) /-- Given a family of pairwise disjoint finite sets `t i` indexed by a finite type, the product of `f a` over the union `⋃ i, t i` is equal to the product over all indexes `i` of the products of `f a` over `a ∈ t i`. -/ @[to_additive] lemma finprod_mem_Union [fintype ι] {t : ι → set α} (h : pairwise (disjoint on t)) (ht : ∀ i, (t i).finite) : ∏ᶠ a ∈ (⋃ i : ι, t i), f a = ∏ᶠ i, (∏ᶠ a ∈ t i, f a) := begin unfreezingI { lift t to ι → finset α using ht }, classical, rw [← bUnion_univ, ← finset.coe_univ, ← finset.coe_bUnion, finprod_mem_coe_finset, finset.prod_bUnion], { simp only [finprod_mem_coe_finset, finprod_eq_prod_of_fintype] }, { exact λ x _ y _ hxy, finset.disjoint_iff_disjoint_coe.2 (h x y hxy) } end /-- Given a family of sets `t : ι → set α`, a finite set `I` in the index type such that all sets `t i`, `i ∈ I`, are finite, if all `t i`, `i ∈ I`, are pairwise disjoint, then the product of `f a` over `a ∈ ⋃ i ∈ I, t i` is equal to the product over `i ∈ I` of the products of `f a` over `a ∈ t i`. -/ @[to_additive] lemma finprod_mem_bUnion {I : set ι} {t : ι → set α} (h : pairwise_on I (disjoint on t)) (hI : I.finite) (ht : ∀ i ∈ I, (t i).finite) : ∏ᶠ a ∈ ⋃ x ∈ I, t x, f a = ∏ᶠ i ∈ I, ∏ᶠ j ∈ t i, f j := begin haveI := hI.fintype, rw [← Union_subtype, finprod_mem_Union, ← finprod_set_coe_eq_finprod_mem], exacts [λ x y hxy, h x x.2 y y.2 (subtype.coe_injective.ne hxy), λ b, ht b b.2] end /-- If `t` is a finite set of pairwise disjoint finite sets, then the product of `f a` over `a ∈ ⋃₀ t` is the product over `s ∈ t` of the products of `f a` over `a ∈ s`. -/ @[to_additive] lemma finprod_mem_sUnion {t : set (set α)} (h : pairwise_on t disjoint) (ht₀ : t.finite) (ht₁ : ∀ x ∈ t, set.finite x): ∏ᶠ a ∈ ⋃₀ t, f a = ∏ᶠ s ∈ t, ∏ᶠ a ∈ s, f a := by rw [set.sUnion_eq_bUnion, finprod_mem_bUnion h ht₀ ht₁] /-- If `s : set α` and `t : set β` are finite sets, then the product over `s` commutes with the product over `t`. -/ @[to_additive] lemma finprod_mem_comm {s : set α} {t : set β} (f : α → β → M) (hs : s.finite) (ht : t.finite) : ∏ᶠ i ∈ s, ∏ᶠ j ∈ t, f i j = ∏ᶠ j ∈ t, ∏ᶠ i ∈ s, f i j := begin unfreezingI { lift s to finset α using hs, lift t to finset β using ht }, simp only [finprod_mem_coe_finset], exact finset.prod_comm end /-- To prove a property of a finite product, it suffices to prove that the property is multiplicative and holds on multipliers. -/ @[to_additive] lemma finprod_mem_induction (p : M → Prop) (hp₀ : p 1) (hp₁ : ∀ x y, p x → p y → p (x * y)) (hp₂ : ∀ x ∈ s, p $ f x) : p (∏ᶠ i ∈ s, f i) := finprod_induction _ hp₀ hp₁ $ λ x, finprod_induction _ hp₀ hp₁ $ hp₂ x lemma finprod_cond_nonneg {R : Type*} [ordered_comm_semiring R] {p : α → Prop} {f : α → R} (hf : ∀ x, p x → 0 ≤ f x) : 0 ≤ ∏ᶠ x (h : p x), f x := finprod_nonneg $ λ x, finprod_nonneg $ hf x @[to_additive] lemma single_le_finprod {M : Type*} [ordered_comm_monoid M] (i : α) {f : α → M} (hf : finite (mul_support f)) (h : ∀ j, 1 ≤ f j) : f i ≤ ∏ᶠ j, f j := by classical; calc f i ≤ ∏ j in insert i hf.to_finset, f j : finset.single_le_prod' (λ j hj, h j) (finset.mem_insert_self _ _) ... = ∏ᶠ j, f j : (finprod_eq_prod_of_mul_support_to_finset_subset _ hf (finset.subset_insert _ _)).symm lemma finprod_eq_zero {M₀ : Type*} [comm_monoid_with_zero M₀] (f : α → M₀) (x : α) (hx : f x = 0) (hf : finite (mul_support f)) : ∏ᶠ x, f x = 0 := begin nontriviality, rw [finprod_eq_prod f hf], refine finset.prod_eq_zero (hf.mem_to_finset.2 _) hx, simp [hx] end @[to_additive] lemma finprod_prod_comm (s : finset β) (f : α → β → M) (h : ∀ b ∈ s, (mul_support (λ a, f a b)).finite) : ∏ᶠ a : α, ∏ b in s, f a b = ∏ b in s, ∏ᶠ a : α, f a b := begin have hU : mul_support (λ a, ∏ b in s, f a b) ⊆ (s.finite_to_set.bUnion (λ b hb, h b (finset.mem_coe.1 hb))).to_finset, { rw finite.coe_to_finset, intros x hx, simp only [exists_prop, mem_Union, ne.def, mem_mul_support, finset.mem_coe], contrapose! hx, rw [mem_mul_support, not_not, finset.prod_congr rfl hx, finset.prod_const_one] }, rw [finprod_eq_prod_of_mul_support_subset _ hU, finset.prod_comm], refine finset.prod_congr rfl (λ b hb, (finprod_eq_prod_of_mul_support_subset _ _).symm), intros a ha, simp only [finite.coe_to_finset, mem_Union], exact ⟨b, hb, ha⟩ end @[to_additive] lemma prod_finprod_comm (s : finset α) (f : α → β → M) (h : ∀ a ∈ s, (mul_support (f a)).finite) : ∏ a in s, ∏ᶠ b : β, f a b = ∏ᶠ b : β, ∏ a in s, f a b := (finprod_prod_comm s (λ b a, f a b) h).symm lemma mul_finsum {R : Type*} [semiring R] (f : α → R) (r : R) (h : (function.support f).finite) : r * ∑ᶠ a : α, f a = ∑ᶠ a : α, r * f a := (add_monoid_hom.mul_left r).map_finsum h lemma finsum_mul {R : Type*} [semiring R] (f : α → R) (r : R) (h : (function.support f).finite) : (∑ᶠ a : α, f a) * r = ∑ᶠ a : α, f a * r := (add_monoid_hom.mul_right r).map_finsum h @[to_additive] lemma finset.mul_support_of_fiberwise_prod_subset_image [decidable_eq β] (s : finset α) (f : α → M) (g : α → β) : mul_support (λ b, (s.filter (λ a, g a = b)).prod f) ⊆ s.image g := begin simp only [finset.coe_image, set.mem_image, finset.mem_coe, function.support_subset_iff], intros b h, suffices : (s.filter (λ (a : α), g a = b)).nonempty, { simpa only [s.fiber_nonempty_iff_mem_image g b, finset.mem_image, exists_prop], }, exact finset.nonempty_of_prod_ne_one h, end /-- Note that `b ∈ (s.filter (λ ab, prod.fst ab = a)).image prod.snd` iff `(a, b) ∈ s` so we can simplify the right hand side of this lemma. However the form stated here is more useful for iterating this lemma, e.g., if we have `f : α × β × γ → M`. -/ @[to_additive] lemma finprod_mem_finset_product' [decidable_eq α] [decidable_eq β] (s : finset (α × β)) (f : α × β → M) : ∏ᶠ ab (h : ab ∈ s), f ab = ∏ᶠ a b (h : b ∈ (s.filter (λ ab, prod.fst ab = a)).image prod.snd), f (a, b) := begin have : ∀ a, ∏ (i : β) in (s.filter (λ ab, prod.fst ab = a)).image prod.snd, f (a, i) = (finset.filter (λ ab, prod.fst ab = a) s).prod f, { intros a, apply finset.prod_bij (λ b _, (a, b)); finish, }, rw finprod_mem_finset_eq_prod, simp_rw [finprod_mem_finset_eq_prod, this], rw [finprod_eq_prod_of_mul_support_subset _ (s.mul_support_of_fiberwise_prod_subset_image f prod.fst), ← finset.prod_fiberwise_of_maps_to _ f], finish, end /-- See also `finprod_mem_finset_product'`. -/ @[to_additive] lemma finprod_mem_finset_product (s : finset (α × β)) (f : α × β → M) : ∏ᶠ ab (h : ab ∈ s), f ab = ∏ᶠ a b (h : (a, b) ∈ s), f (a, b) := by { classical, rw finprod_mem_finset_product', simp, } @[to_additive] lemma finprod_mem_finset_product₃ {γ : Type*} (s : finset (α × β × γ)) (f : α × β × γ → M) : ∏ᶠ abc (h : abc ∈ s), f abc = ∏ᶠ a b c (h : (a, b, c) ∈ s), f (a, b, c) := by { classical, rw finprod_mem_finset_product', simp_rw finprod_mem_finset_product', simp, } @[to_additive] lemma finprod_curry (f : α × β → M) (hf : (mul_support f).finite) : ∏ᶠ ab, f ab = ∏ᶠ a b, f (a, b) := begin have h₁ : ∀ a, ∏ᶠ (h : a ∈ hf.to_finset), f a = f a, { simp, }, have h₂ : ∏ᶠ a, f a = ∏ᶠ a (h : a ∈ hf.to_finset), f a, { simp, }, simp_rw [h₂, finprod_mem_finset_product, h₁], end @[to_additive] lemma finprod_curry₃ {γ : Type*} (f : α × β × γ → M) (h : (mul_support f).finite) : ∏ᶠ abc, f abc = ∏ᶠ a b c, f (a, b, c) := by { rw finprod_curry f h, congr, ext a, rw finprod_curry, simp [h], } @[to_additive] lemma finprod_dmem {s : set α} [decidable_pred (∈ s)] (f : (Π (a : α), a ∈ s → M)) : ∏ᶠ (a : α) (h : a ∈ s), f a h = ∏ᶠ (a : α) (h : a ∈ s), if h' : a ∈ s then f a h' else 1 := finprod_congr (λ a, finprod_congr (λ ha, (dif_pos ha).symm)) @[to_additive] lemma finprod_emb_domain' {f : α → β} (hf : function.injective f) [decidable_pred (∈ set.range f)] (g : α → M) : ∏ᶠ (b : β), (if h : b ∈ set.range f then g (classical.some h) else 1) = ∏ᶠ (a : α), g a := begin simp_rw [← finprod_eq_dif], rw [finprod_dmem, finprod_mem_range hf, finprod_congr (λ a, _)], rw [dif_pos (set.mem_range_self a), hf (classical.some_spec (set.mem_range_self a))] end @[to_additive] lemma finprod_emb_domain (f : α ↪ β) [decidable_pred (∈ set.range f)] (g : α → M) : ∏ᶠ (b : β), (if h : b ∈ set.range f then g (classical.some h) else 1) = ∏ᶠ (a : α), g a := finprod_emb_domain' f.injective g end type
6a52e34cfe75ff70717234e8728eda7f51686949
ff5230333a701471f46c57e8c115a073ebaaa448
/tests/lean/run/injection_ginductive.lean
38f2f8edc5ce64b79d4b915c8a85dcf1986c9625
[ "Apache-2.0" ]
permissive
stanford-cs242/lean
f81721d2b5d00bc175f2e58c57b710d465e6c858
7bd861261f4a37326dcf8d7a17f1f1f330e4548c
refs/heads/master
1,600,957,431,849
1,576,465,093,000
1,576,465,093,000
225,779,423
0
3
Apache-2.0
1,575,433,936,000
1,575,433,935,000
null
UTF-8
Lean
false
false
402
lean
inductive term | const (n : string) : term | app (fn : string) (args : list term) : term constant p : Prop → Prop axiom p_ax : ∀ x, p x = x example (f₁ f₂ : string) (as₁ as₂ : list term) (h : term.app f₁ as₁ = term.app f₂ as₂) : p (as₁ = as₂) := begin injection h, trace_state, guard_hyp h_1 := f₁ = f₂, guard_hyp h_2 := as₁ = as₂, rw p_ax, assumption end
2ef019b015e7fa38133483d083d10b212909f0da
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/src/Lean/Elab/MacroRules.lean
30110f74d68065a62ef90725d325407b48504099
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach" ]
permissive
tobiasgrosser/lean4
ce0fd9cca0feba1100656679bf41f0bffdbabb71
ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f
refs/heads/master
1,673,103,412,948
1,664,930,501,000
1,664,930,501,000
186,870,185
0
0
Apache-2.0
1,665,129,237,000
1,557,939,901,000
Lean
UTF-8
Lean
false
false
3,340
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Elab.Syntax import Lean.Elab.AuxDef namespace Lean.Elab.Command open Lean.Syntax open Lean.Parser.Term hiding macroArg open Lean.Parser.Command /-- Remark: `k` is the user provided kind with the current namespace included. Recall that syntax node kinds contain the current namespace. -/ def elabMacroRulesAux (doc? : Option (TSyntax ``docComment)) (attrs? : Option (TSepArray ``attrInstance ",")) (attrKind : TSyntax ``attrKind) (tk : Syntax) (k : SyntaxNodeKind) (alts : Array (TSyntax ``matchAlt)) : CommandElabM Syntax := do let alts ← alts.mapM fun (alt : TSyntax ``matchAlt) => match alt with | `(matchAltExpr| | $pats,* => $rhs) => do let pat := pats.elemsAndSeps[0]! if !pat.isQuot then throwUnsupportedSyntax let quoted := getQuotContent pat let k' := quoted.getKind if checkRuleKind k' k then pure alt else if k' == choiceKind then match quoted.getArgs.find? fun quotAlt => checkRuleKind quotAlt.getKind k with | none => throwErrorAt alt "invalid macro_rules alternative, expected syntax node kind '{k}'" | some quoted => let pat := pat.setArg 1 quoted let pats := pats.elemsAndSeps.set! 0 pat `(matchAltExpr| | $(⟨pats⟩),* => $rhs) else throwErrorAt alt "invalid macro_rules alternative, unexpected syntax node kind '{k'}'" | _ => throwUnsupportedSyntax let attr ← `(attrInstance| $attrKind macro $(Lean.mkIdent k)) let attrs := match attrs? with | some attrs => attrs.getElems.push attr | none => #[attr] `($[$doc?:docComment]? @[$attrs,*] aux_def macroRules $(mkIdentFrom tk k (canonical := true)) : Macro := fun $alts:matchAlt* | _ => no_error_if_unused% throw Lean.Macro.Exception.unsupportedSyntax) @[builtinCommandElab «macro_rules»] def elabMacroRules : CommandElab := adaptExpander fun stx => match stx with | `($[$doc?:docComment]? $[@[$attrs?,*]]? $attrKind:attrKind macro_rules%$tk $alts:matchAlt*) => -- exclude command prefix from synthetic position used for e.g. jumping to the macro definition withRef (mkNullNode #[tk, mkNullNode alts]) do expandNoKindMacroRulesAux alts "macro_rules" fun kind? alts => `($[$doc?:docComment]? $[@[$attrs?,*]]? $attrKind:attrKind macro_rules $[(kind := $(mkIdent <$> kind?))]? $alts:matchAlt*) | `($[$doc?:docComment]? $[@[$attrs?,*]]? $attrKind:attrKind macro_rules%$tk (kind := $kind) | $x:ident => $rhs) => withRef (mkNullNode #[tk, rhs]) do let attr ← `(attrInstance| $attrKind:attrKind macro $kind) let attrs := match attrs? with | some attrs => attrs.getElems.push attr | none => #[attr] `($[$doc?:docComment]? @[$attrs,*] aux_def $(mkIdentFrom tk kind.getId (canonical := true)) $kind : Macro := fun $x:ident => $rhs) | `($[$doc?:docComment]? $[@[$attrs?,*]]? $attrKind:attrKind macro_rules%$tk (kind := $kind) $alts:matchAlt*) => withRef (mkNullNode #[tk, mkNullNode alts]) do elabMacroRulesAux doc? attrs? attrKind tk (← resolveSyntaxKind kind.getId) alts | _ => throwUnsupportedSyntax end Lean.Elab.Command
515c6eadc56af01d156e89c178dbe11294681b12
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/run/ctorAutoParams.lean
5207e4c089fa9add68d16d0e64c2ce21306918c9
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach" ]
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
1,526
lean
structure State -- TODO structure Expr -- TODO def eval : State → Expr → Bool := fun _ _ => true inductive Command where | skip | cond : Expr → Command → Command → Command | while : Expr → Command → Command | seq : Command → Command → Command open Command infix:10 ";;" => Command.seq inductive Bigstep : Command × State → State → Nat → Prop where | skip : Bigstep (skip, σ) σ 1 | seq : Bigstep (c₁, σ₁) σ₂ t₁ → Bigstep (c₂, σ₂) σ₃ t₂ → Bigstep (c₁ ;; c₂, σ₁) σ₃ (t₁ + t₂ + 1) | ifTrue : eval σ₁ b = true → Bigstep (c₁, σ₁) σ₂ t → Bigstep (cond b c₁ c₂, σ₁) σ₂ (t + 1) | ifFalse : eval σ₁ b = false → Bigstep (c₂, σ₁) σ₂ t → Bigstep (cond b c₁ c₂, σ₁) σ₂ (t + 1) #check @Bigstep.seq namespace WithoutAutoImplicit set_option autoBoundImplicitLocal false inductive Bigstep : Command × State → State → Nat → Prop where | skip {σ} : Bigstep (skip, σ) σ 1 | seq {c₁ c₂ σ₁ σ₂ σ₃ t₁ t₂} : Bigstep (c₁, σ₁) σ₂ t₁ → Bigstep (c₂, σ₂) σ₃ t₂ → Bigstep (c₁ ;; c₂, σ₁) σ₃ (t₁ + t₂ + 1) | ifTrue {b c₁ c₂ σ₁ σ₂ t} : eval σ₁ b = true → Bigstep (c₁, σ₁) σ₂ t → Bigstep (cond b c₁ c₂, σ₁) σ₂ (t + 1) | ifFalse {b c₁ c₂ σ₁ σ₂ t} : eval σ₁ b = false → Bigstep (c₂, σ₁) σ₂ t → Bigstep (cond b c₁ c₂, σ₁) σ₂ (t + 1) end WithoutAutoImplicit
f3babac97c6b8859207899ce2aa8c4f4ac435768
ea5678cc400c34ff95b661fa26d15024e27ea8cd
/presburger.lean
65f38f563ff8c364734eb31d318196c4af1764da
[]
no_license
ChrisHughes24/leanstuff
dca0b5349c3ed893e8792ffbd98cbcadaff20411
9efa85f72efaccd1d540385952a6acc18fce8687
refs/heads/master
1,654,883,241,759
1,652,873,885,000
1,652,873,885,000
134,599,537
1
0
null
null
null
null
UTF-8
Lean
false
false
382
lean
import data.set.basic inductive presburger_equation : Type | inductive presburger : Π n, (set (fin n → ℕ)) → Prop | empty (n : ℕ) : presburger n ∅ | union (n : ℕ) (s t : set (fin n → ℕ)) : presburger n s → presburger n t → presburger n (s ∪ t) | inter (n : ℕ) (s t : set (fin n → ℕ)) : presburger n s → presburger n t → presburger n (s ∪ t)
8983422e6d337ef7c502508088f7b88e90c3f591
367134ba5a65885e863bdc4507601606690974c1
/src/combinatorics/hall.lean
c71809733c1d4b93859945f009931fab6e53f4b2
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
14,769
lean
/- Copyright (c) 2021 Alena Gusakov, Bhavik Mehta, Kyle Miller. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Alena Gusakov, Bhavik Mehta, Kyle Miller -/ import data.fintype.basic import data.rel import data.set.finite /-! # Hall's Marriage Theorem Given a list of finite subsets $X_1,X_2,\dots,X_n$ of some given set $S$, Hall in [Hall1935] gave a necessary and sufficient condition for there to be a list of distinct elements $x_1,x_2,\dots,x_n$ with $x_i\in X_i$ for each $i$: it is when for each $k$, the union of every $k$ of these subsets has at least $k$ elements. This file proves this for an indexed family `t : ι → finset α` of finite sets, with `[fintype ι]`, along with some variants of the statement. The list of distinct representatives is given by an injective function `f : ι → α` such that `∀ i, f i ∈ t i`. A description of this formalization is in [Gusakov2021]. ## Main statements * `finset.all_card_le_bUnion_card_iff_exists_injective` is in terms of `t : ι → finset α`. * `fintype.all_card_le_rel_image_card_iff_exists_injective` is in terms of a relation `r : α → β → Prop` such that `rel.image r {a}` is a finite set for all `a : α`. * `fintype.all_card_le_filter_rel_iff_exists_injective` is in terms of a relation `r : α → β → Prop` on finite types, with the Hall condition given in terms of `finset.univ.filter`. ## Todo * The theorem is still true even if `ι` is not a finite type. The infinite case follows from a compactness argument. * The statement of the theorem in terms of bipartite graphs is in preparation. ## Tags Hall's Marriage Theorem, indexed families -/ open finset universes u v namespace hall_marriage_theorem variables {ι : Type u} {α : Type v} [fintype ι] theorem hall_hard_inductive_zero (t : ι → finset α) (hn : fintype.card ι = 0) : ∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x := begin rw fintype.card_eq_zero_iff at hn, exact ⟨λ x, (hn x).elim, by tauto⟩, end variables {t : ι → finset α} [decidable_eq α] lemma hall_cond_of_erase {x : ι} (a : α) (ha : ∀ (s : finset ι), s.nonempty → s ≠ univ → s.card < (s.bUnion t).card) (s' : finset {x' : ι | x' ≠ x}) : s'.card ≤ (s'.bUnion (λ x', (t x').erase a)).card := begin haveI := classical.dec_eq ι, specialize ha (s'.image coe), rw [nonempty.image_iff, finset.card_image_of_injective s' subtype.coe_injective] at ha, by_cases he : s'.nonempty, { have ha' : s'.card < (s'.bUnion (λ x, t x)).card, { specialize ha he (λ h, by { have h' := mem_univ x, rw ←h at h', simpa using h' }), convert ha using 2, ext x, simp only [mem_image, mem_bUnion, exists_prop, set_coe.exists, exists_and_distrib_right, exists_eq_right, subtype.coe_mk], }, rw ←erase_bUnion, by_cases hb : a ∈ s'.bUnion (λ x, t x), { rw card_erase_of_mem hb, exact nat.le_pred_of_lt ha' }, { rw erase_eq_of_not_mem hb, exact nat.le_of_lt ha' }, }, { rw [nonempty_iff_ne_empty, not_not] at he, subst s', simp }, end /-- First case of the inductive step: assuming that `∀ (s : finset ι), s.nonempty → s ≠ univ → s.card < (s.bUnion t).card` and that the statement of Hall's Marriage Theorem is true for all `ι'` of cardinality ≤ `n`, then it is true for `ι` of cardinality `n + 1`. -/ lemma hall_hard_inductive_step_A {n : ℕ} (hn : fintype.card ι = n + 1) (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) (ih : ∀ {ι' : Type u} [fintype ι'] (t' : ι' → finset α), by exactI fintype.card ι' ≤ n → (∀ (s' : finset ι'), s'.card ≤ (s'.bUnion t').card) → ∃ (f : ι' → α), function.injective f ∧ ∀ x, f x ∈ t' x) (ha : ∀ (s : finset ι), s.nonempty → s ≠ univ → s.card < (s.bUnion t).card) : ∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x := begin haveI : nonempty ι := fintype.card_pos_iff.mp (hn.symm ▸ nat.succ_pos _), haveI := classical.dec_eq ι, /- Choose an arbitrary element `x : ι` and `y : t x`. -/ let x := classical.arbitrary ι, have tx_ne : (t x).nonempty, { rw ←finset.card_pos, apply nat.lt_of_lt_of_le nat.one_pos, convert ht {x}, rw finset.singleton_bUnion, }, rcases classical.indefinite_description _ tx_ne with ⟨y, hy⟩, /- Restrict to everything except `x` and `y`. -/ let ι' := {x' : ι | x' ≠ x}, let t' : ι' → finset α := λ x', (t x').erase y, have card_ι' : fintype.card ι' = n, { convert congr_arg (λ m, m - 1) hn, convert set.card_ne_eq _, }, rcases ih t' card_ι'.le (hall_cond_of_erase y ha) with ⟨f', hfinj, hfr⟩, /- Extend the resulting function. -/ refine ⟨λ z, if h : z = x then y else f' ⟨z, h⟩, _, _⟩, { rintro z₁ z₂, have key : ∀ {x}, y ≠ f' x, { intros x h, specialize hfr x, rw ←h at hfr, simpa using hfr, }, by_cases h₁ : z₁ = x; by_cases h₂ : z₂ = x; simp [h₁, h₂, hfinj.eq_iff, key, key.symm], }, { intro z, split_ifs with hz, { rwa hz }, { specialize hfr ⟨z, hz⟩, rw mem_erase at hfr, exact hfr.2, }, }, end lemma hall_cond_of_restrict {ι : Type u} {t : ι → finset α} {s : finset ι} (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) (s' : finset (s : set ι)) : s'.card ≤ (s'.bUnion (λ a', t a')).card := begin haveI := classical.dec_eq ι, convert ht (s'.image coe) using 1, { rw card_image_of_injective _ subtype.coe_injective, }, { apply congr_arg, ext y, simp, }, end lemma hall_cond_of_compl {ι : Type u} {t : ι → finset α} {s : finset ι} (hus : s.card = (s.bUnion t).card) (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) (s' : finset (sᶜ : set ι)) : s'.card ≤ (s'.bUnion (λ x', t x' \ s.bUnion t)).card := begin haveI := classical.dec_eq ι, have : s'.card = (s ∪ s'.image coe).card - s.card, { rw [card_disjoint_union, nat.add_sub_cancel_left, card_image_of_injective _ subtype.coe_injective], simp only [disjoint_left, not_exists, mem_image, exists_prop, set_coe.exists, exists_and_distrib_right, exists_eq_right, subtype.coe_mk], intros x hx hc h, exact (hc hx).elim }, rw [this, hus], apply (nat.sub_le_sub_right (ht _) _).trans _, rw ← card_sdiff, { have : (s ∪ s'.image subtype.val).bUnion t \ s.bUnion t ⊆ s'.bUnion (λ x', t x' \ s.bUnion t), { intros t, simp only [mem_bUnion, mem_sdiff, not_exists, mem_image, and_imp, mem_union, exists_and_distrib_right, exists_imp_distrib], rintro x (hx | ⟨x', hx', rfl⟩) rat hs, { exact (hs x hx rat).elim }, { exact ⟨⟨x', hx', rat⟩, hs⟩, } }, exact (card_le_of_subset this).trans le_rfl, }, { apply bUnion_subset_bUnion_of_subset_left, apply subset_union_left } end /-- Second case of the inductive step: assuming that `∃ (s : finset ι), s ≠ univ → s.card = (s.bUnion t).card` and that the statement of Hall's Marriage Theorem is true for all `ι'` of cardinality ≤ `n`, then it is true for `ι` of cardinality `n + 1`. -/ lemma hall_hard_inductive_step_B {n : ℕ} (hn : fintype.card ι = n + 1) (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) (ih : ∀ {ι' : Type u} [fintype ι'] (t' : ι' → finset α), by exactI fintype.card ι' ≤ n → (∀ (s' : finset ι'), s'.card ≤ (s'.bUnion t').card) → ∃ (f : ι' → α), function.injective f ∧ ∀ x, f x ∈ t' x) (s : finset ι) (hs : s.nonempty) (hns : s ≠ univ) (hus : s.card = (s.bUnion t).card) : ∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x := begin haveI := classical.dec_eq ι, /- Restrict to `s` -/ let ι' := (s : set ι), let t' : ι' → finset α := λ x', t x', rw nat.add_one at hn, have card_ι'_le : fintype.card ι' ≤ n, { apply nat.le_of_lt_succ, rw ←hn, convert (card_lt_iff_ne_univ _).mpr hns, convert fintype.card_coe _ }, rcases ih t' card_ι'_le (hall_cond_of_restrict ht) with ⟨f', hf', hsf'⟩, /- Restrict to `sᶜ` in the domain and `(s.bUnion t)ᶜ` in the codomain. -/ let ι'' := (s : set ι)ᶜ, let t'' : ι'' → finset α := λ a'', t a'' \ s.bUnion t, have card_ι''_le : fintype.card ι'' ≤ n, { apply nat.le_of_lt_succ, rw ←hn, convert (card_compl_lt_iff_nonempty _).mpr hs, convert fintype.card_coe _, rw coe_compl, }, rcases ih t'' card_ι''_le (hall_cond_of_compl hus ht) with ⟨f'', hf'', hsf''⟩, /- Put them together -/ have f'_mem_bUnion : ∀ {x'} (hx' : x' ∈ s), f' ⟨x', hx'⟩ ∈ s.bUnion t, { intros x' hx', rw mem_bUnion, exact ⟨x', hx', hsf' _⟩, }, have f''_not_mem_bUnion : ∀ {x''} (hx'' : ¬ x'' ∈ s), ¬ f'' ⟨x'', hx''⟩ ∈ s.bUnion t, { intros x'' hx'', have h := hsf'' ⟨x'', hx''⟩, rw mem_sdiff at h, exact h.2, }, have im_disj : ∀ {x' x'' : ι} {hx' : x' ∈ s} {hx'' : ¬x'' ∈ s}, f' ⟨x', hx'⟩ ≠ f'' ⟨x'', hx''⟩, { intros _ _ hx' hx'' h, apply f''_not_mem_bUnion hx'', rw ←h, apply f'_mem_bUnion, }, refine ⟨λ x, if h : x ∈ s then f' ⟨x, h⟩ else f'' ⟨x, h⟩, _, _⟩, { exact hf'.dite _ hf'' @im_disj }, { intro x, split_ifs, { exact hsf' ⟨x, h⟩ }, { exact sdiff_subset _ _ (hsf'' ⟨x, h⟩) } } end /-- If `ι` has cardinality `n + 1` and the statement of Hall's Marriage Theorem is true for all `ι'` of cardinality ≤ `n`, then it is true for `ι`. -/ theorem hall_hard_inductive_step {n : ℕ} (hn : fintype.card ι = n + 1) (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) (ih : ∀ {ι' : Type u} [fintype ι'] (t' : ι' → finset α), by exactI fintype.card ι' ≤ n → (∀ (s' : finset ι'), s'.card ≤ (s'.bUnion t').card) → ∃ (f : ι' → α), function.injective f ∧ ∀ x, f x ∈ t' x) : ∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x := begin by_cases h : ∀ (s : finset ι), s.nonempty → s ≠ univ → s.card < (s.bUnion t).card, { exact hall_hard_inductive_step_A hn ht @ih h, }, { push_neg at h, rcases h with ⟨s, sne, snu, sle⟩, have seq := nat.le_antisymm (ht _) sle, exact hall_hard_inductive_step_B hn ht @ih s sne snu seq, }, end /-- Here we combine the base case and the inductive step into a full strong induction proof, thus completing the proof of the second direction. -/ theorem hall_hard_inductive {n : ℕ} (hn : fintype.card ι = n) (ht : ∀ (s : finset ι), s.card ≤ (s.bUnion t).card) : ∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x := begin tactic.unfreeze_local_instances, revert ι, refine nat.strong_induction_on n (λ n' ih, _), intros _ _ t hn ht, rcases n' with (_|_), { exact hall_hard_inductive_zero t hn }, { apply hall_hard_inductive_step hn ht, introsI ι' _ _ hι', exact ih (fintype.card ι') (nat.lt_succ_of_le hι') rfl, }, end end hall_marriage_theorem /-- This the version of Hall's Marriage Theorem in terms of indexed families of finite sets `t : ι → finset α`. It states that there is a set of distinct representatives if and only if every union of `k` of the sets has at least `k` elements. Recall that `s.bUnion t` is the union of all the sets `t i` for `i ∈ s`. -/ theorem finset.all_card_le_bUnion_card_iff_exists_injective {ι α : Type*} [fintype ι] [decidable_eq α] (t : ι → finset α) : (∀ (s : finset ι), s.card ≤ (s.bUnion t).card) ↔ (∃ (f : ι → α), function.injective f ∧ ∀ x, f x ∈ t x) := begin split, { exact hall_marriage_theorem.hall_hard_inductive rfl }, { rintro ⟨f, hf₁, hf₂⟩ s, rw ←card_image_of_injective s hf₁, apply card_le_of_subset, intro _, rw [mem_image, mem_bUnion], rintros ⟨x, hx, rfl⟩, exact ⟨x, hx, hf₂ x⟩, }, end /-- Given a relation such that the image of every singleton set is finite, then the image of every finite set is finite. -/ instance {α β : Type*} [decidable_eq β] (r : α → β → Prop) [∀ (a : α), fintype (rel.image r {a})] (A : finset α) : fintype (rel.image r A) := begin have h : rel.image r A = (A.bUnion (λ a, (rel.image r {a}).to_finset) : set β), { ext, simp [rel.image], }, rw [h], apply finset_coe.fintype, end /-- This is a version of Hall's Marriage Theorem in terms of a relation between types `α` and `β` such that `α` is finite and the image of each `x : α` is finite (it suffices for `β` to be finite). There is an injective function `α → β` respecting the relation iff every subset of `k` terms of `α` is related to at least `k` terms of `β`. If `[fintype β]`, then `[∀ (a : α), fintype (rel.image r {a})]` is automatically implied. -/ theorem fintype.all_card_le_rel_image_card_iff_exists_injective {α β : Type*} [fintype α] [decidable_eq β] (r : α → β → Prop) [∀ (a : α), fintype (rel.image r {a})] : (∀ (A : finset α), A.card ≤ fintype.card (rel.image r A)) ↔ (∃ (f : α → β), function.injective f ∧ ∀ x, r x (f x)) := begin let r' := λ a, (rel.image r {a}).to_finset, have h : ∀ (A : finset α), fintype.card (rel.image r A) = (A.bUnion r').card, { intro A, rw ←set.to_finset_card, apply congr_arg, ext b, simp [rel.image], }, have h' : ∀ (f : α → β) x, r x (f x) ↔ f x ∈ r' x, { simp [rel.image], }, simp only [h, h'], apply finset.all_card_le_bUnion_card_iff_exists_injective, end /-- This is a version of Hall's Marriage Theorem in terms of a relation between finite types. There is an injective function `α → β` respecting the relation iff every subset of `k` terms of `α` is related to at least `k` terms of `β`. It is like `fintype.all_card_le_rel_image_card_iff_exists_injective` but uses `finset.filter` rather than `rel.image`. -/ theorem fintype.all_card_le_filter_rel_iff_exists_injective {α β : Type*} [fintype α] [fintype β] (r : α → β → Prop) [∀ a, decidable_pred (r a)] : (∀ (A : finset α), A.card ≤ (univ.filter (λ (b : β), ∃ a ∈ A, r a b)).card) ↔ (∃ (f : α → β), function.injective f ∧ ∀ x, r x (f x)) := begin haveI := classical.dec_eq β, let r' := λ a, univ.filter (λ b, r a b), have h : ∀ (A : finset α), (univ.filter (λ (b : β), ∃ a ∈ A, r a b)) = (A.bUnion r'), { intro A, ext b, simp, }, have h' : ∀ (f : α → β) x, r x (f x) ↔ f x ∈ r' x, { simp, }, simp_rw [h, h'], apply finset.all_card_le_bUnion_card_iff_exists_injective, end
cb6bcaf7a8bdb0185c3533944a2b8be7f2be3f86
f7c63613a4933f66368ef2802a50c417a46cddfc
/tests/lean/derive.lean
ff8c8dd59e11ea335d027b7e94c1c0dfbcfceae5
[ "Apache-2.0" ]
permissive
avigad/lean
28cef71cd8c4eae53f342c87f81ca78d7cc75874
5410178203ab5ae854b5804586ace074ffd63aae
refs/heads/master
1,608,801,379,340
1,504,894,459,000
1,505,174,163,000
22,361,408
0
0
null
null
null
null
UTF-8
Lean
false
false
193
lean
@[derive [decidable_eq, inhabited, has_sizeof]] inductive foo (α β : Type*) | bar : ℕ → foo | baz : foo → α → foo #check foo.decidable_eq #check foo.inhabited #check foo.has_sizeof
9dd58f15a8a878281295c2b99117a9f76a774925
618003631150032a5676f229d13a079ac875ff77
/src/data/monoid_algebra.lean
5f29eacb63bb392d50b664a0b2e36bb6f1254736
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
23,780
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury G. Kudryashov, Scott Morrison -/ import data.finsupp import ring_theory.algebra /-! # Monoid algebras When the domain of a `finsupp` has a multiplicative or additive structure, we can define a convolution product. To mathematicians this structure is known as the "monoid algebra", i.e. the finite formal linear combinations over a given semiring of elements of the monoid. The "group ring" ℤ[G] or the "group algebra" k[G] are typical uses. In this file we define `monoid_algebra k G := G →₀ k`, and `add_monoid_algebra k G` in the same way, and then define the convolution product on these. When the domain is additive, this is used to define polynomials: ``` polynomial α := add_monoid_algebra ℕ α mv_polynominal σ α := add_monoid_algebra (σ →₀ ℕ) α ``` When the domain is multiplicative, e.g. a group, this will be used to define the group ring. ## Implementation note Unfortunately because additive and multiplicative structures both appear in both cases, it doesn't appear to be possible to make much use of `to_additive`, and we just settle for saying everything twice. Similarly, I attempted to just define `add_monoid_algebra k G := monoid_algebra k (multiplicative G)`, but the definitional equality `multiplicative G = G` leaks through everywhere, and seems impossible to use. -/ noncomputable theory open_locale classical open finset finsupp universes u₁ u₂ u₃ variables (k : Type u₁) (G : Type u₂) section variables [semiring k] /-- The monoid algebra over a semiring `k` generated by the monoid `G`. It is the type of finite formal `k`-linear combinations of terms of `G`, endowed with the convolution product. -/ @[derive [inhabited, add_comm_monoid]] def monoid_algebra : Type (max u₁ u₂) := G →₀ k end namespace monoid_algebra variables {k G} local attribute [reducible] monoid_algebra section variables [semiring k] [monoid G] /-- The product of `f g : monoid_algebra k G` is the finitely supported function whose value at `a` is the sum of `f x * g y` over all pairs `x, y` such that `x * y = a`. (Think of the group ring of a group.) -/ instance : has_mul (monoid_algebra k G) := ⟨λf g, f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ * a₂) (b₁ * b₂)⟩ lemma mul_def {f g : monoid_algebra k G} : f * g = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ * a₂) (b₁ * b₂)) := rfl lemma mul_apply (f g : monoid_algebra k G) (x : G) : (f * g) x = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, if a₁ * a₂ = x then b₁ * b₂ else 0) := begin rw [mul_def], simp only [finsupp.sum_apply, single_apply], end lemma mul_apply_antidiagonal (f g : monoid_algebra k G) (x : G) (s : finset (G × G)) (hs : ∀ {p : G × G}, p ∈ s ↔ p.1 * p.2 = x) : (f * g) x = s.sum (λ p, f p.1 * g p.2) := let F : G × G → k := λ p, if p.1 * p.2 = x then f p.1 * g p.2 else 0 in calc (f * g) x = (f.support.sum $ λ a₁, g.support.sum $ λ a₂, F (a₁, a₂)) : mul_apply f g x ... = (f.support.product g.support).sum F : finset.sum_product.symm ... = ((f.support.product g.support).filter (λ p : G × G, p.1 * p.2 = x)).sum (λ p, f p.1 * g p.2) : (finset.sum_filter _ _).symm ... = (s.filter (λ p : G × G, p.1 ∈ f.support ∧ p.2 ∈ g.support)).sum (λ p, f p.1 * g p.2) : sum_congr (by { ext, simp [hs, and_comm] }) (λ _ _, rfl) ... = s.sum (λ p, f p.1 * g p.2) : sum_subset (filter_subset _) $ λ p hps hp, begin simp only [mem_filter, mem_support_iff, not_and, not_not] at hp ⊢, by_cases h1 : f p.1 = 0, { rw [h1, zero_mul] }, { rw [hp hps h1, mul_zero] } end end section variables [semiring k] [monoid G] lemma support_mul (a b : monoid_algebra k G) : (a * b).support ⊆ a.support.bind (λa₁, b.support.bind $ λa₂, {a₁ * a₂}) := subset.trans support_sum $ bind_mono $ assume a₁ _, subset.trans support_sum $ bind_mono $ assume a₂ _, support_single_subset /-- The unit of the multiplication is `single 1 1`, i.e. the function that is `1` at `1` and zero elsewhere. -/ instance : has_one (monoid_algebra k G) := ⟨single 1 1⟩ lemma one_def : (1 : monoid_algebra k G) = single 1 1 := rfl -- TODO: the simplifier unfolds 0 in the instance proof! private lemma zero_mul (f : monoid_algebra k G) : 0 * f = 0 := by simp only [mul_def, sum_zero_index] private lemma mul_zero (f : monoid_algebra k G) : f * 0 = 0 := by simp only [mul_def, sum_zero_index, sum_zero] private lemma left_distrib (a b c : monoid_algebra k G) : a * (b + c) = a * b + a * c := by simp only [mul_def, sum_add_index, mul_add, _root_.mul_zero, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_add] private lemma right_distrib (a b c : monoid_algebra k G) : (a + b) * c = a * c + b * c := by simp only [mul_def, sum_add_index, add_mul, _root_.mul_zero, _root_.zero_mul, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_zero, sum_add] instance : semiring (monoid_algebra k G) := { one := 1, mul := (*), one_mul := assume f, by simp only [mul_def, one_def, sum_single_index, _root_.zero_mul, single_zero, sum_zero, zero_add, one_mul, sum_single], mul_one := assume f, by simp only [mul_def, one_def, sum_single_index, _root_.mul_zero, single_zero, sum_zero, add_zero, mul_one, sum_single], zero_mul := zero_mul, mul_zero := mul_zero, mul_assoc := assume f g h, by simp only [mul_def, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, add_mul, mul_add, add_assoc, mul_assoc, _root_.zero_mul, _root_.mul_zero, sum_zero, sum_add], left_distrib := left_distrib, right_distrib := right_distrib, .. finsupp.add_comm_monoid } @[simp] lemma single_mul_single {a₁ a₂ : G} {b₁ b₂ : k} : (single a₁ b₁ : monoid_algebra k G) * single a₂ b₂ = single (a₁ * a₂) (b₁ * b₂) := (sum_single_index (by simp only [_root_.zero_mul, single_zero, sum_zero])).trans (sum_single_index (by rw [_root_.mul_zero, single_zero])) @[simp] lemma single_pow {a : G} {b : k} : ∀ n : ℕ, (single a b : monoid_algebra k G)^n = single (a^n) (b ^ n) | 0 := rfl | (n+1) := by simp only [pow_succ, single_pow n, single_mul_single] section variables (k G) /-- Embedding of a monoid into its monoid algebra. -/ def of : G →* monoid_algebra k G := { to_fun := λ a, single a 1, map_one' := rfl, map_mul' := λ a b, by rw [single_mul_single, one_mul] } end @[simp] lemma of_apply (a : G) : of k G a = single a 1 := rfl lemma mul_single_apply_aux (f : monoid_algebra k G) {r : k} {x y z : G} (H : ∀ a, a * x = z ↔ a = y) : (f * single x r) z = f y * r := have A : ∀ a₁ b₁, (single x r).sum (λ a₂ b₂, ite (a₁ * a₂ = z) (b₁ * b₂) 0) = ite (a₁ * x = z) (b₁ * r) 0, from λ a₁ b₁, sum_single_index $ by simp, calc (f * single x r) z = sum f (λ a b, if (a = y) then (b * r) else 0) : -- different `decidable` instances make it not trivial by { simp only [mul_apply, A, H], congr, funext, split_ifs; refl } ... = if y ∈ f.support then f y * r else 0 : f.support.sum_ite_eq' _ _ ... = f y * r : by split_ifs with h; simp at h; simp [h] lemma mul_single_one_apply (f : monoid_algebra k G) (r : k) (x : G) : (f * single 1 r) x = f x * r := f.mul_single_apply_aux $ λ a, by rw [mul_one] lemma single_mul_apply_aux (f : monoid_algebra k G) {r : k} {x y z : G} (H : ∀ a, x * a = y ↔ a = z) : (single x r * f) y = r * f z := have f.sum (λ a b, ite (x * a = y) (0 * b) 0) = 0, by simp, calc (single x r * f) y = sum f (λ a b, ite (x * a = y) (r * b) 0) : (mul_apply _ _ _).trans $ sum_single_index this ... = f.sum (λ a b, ite (a = z) (r * b) 0) : by { simp only [H], congr, ext; split_ifs; refl } ... = if z ∈ f.support then (r * f z) else 0 : f.support.sum_ite_eq' _ _ ... = _ : by split_ifs with h; simp at h; simp [h] lemma single_one_mul_apply (f : monoid_algebra k G) (r : k) (x : G) : (single 1 r * f) x = r * f x := f.single_mul_apply_aux $ λ a, by rw [one_mul] end instance [comm_semiring k] [comm_monoid G] : comm_semiring (monoid_algebra k G) := { mul_comm := assume f g, begin simp only [mul_def, finsupp.sum, mul_comm], rw [finset.sum_comm], simp only [mul_comm] end, .. monoid_algebra.semiring } instance [ring k] : has_neg (monoid_algebra k G) := by apply_instance instance [ring k] [monoid G] : ring (monoid_algebra k G) := { neg := has_neg.neg, add_left_neg := add_left_neg, .. monoid_algebra.semiring } instance [comm_ring k] [comm_monoid G] : comm_ring (monoid_algebra k G) := { mul_comm := mul_comm, .. monoid_algebra.ring} instance [semiring k] : has_scalar k (monoid_algebra k G) := finsupp.has_scalar instance [semiring k] : semimodule k (monoid_algebra k G) := finsupp.semimodule G k instance [ring k] : module k (monoid_algebra k G) := finsupp.module G k instance [comm_semiring k] [monoid G] : algebra k (monoid_algebra k G) := { to_fun := single 1, map_one' := rfl, map_mul' := λ x y, by rw [single_mul_single, one_mul], map_zero' := single_zero, map_add' := λ x y, single_add, smul_def' := λ r a, ext (λ _, smul_apply.trans (single_one_mul_apply _ _ _).symm), commutes' := λ r f, ext $ λ _, by rw [single_one_mul_apply, mul_single_one_apply, mul_comm] } @[simp] lemma coe_algebra_map [comm_semiring k] [monoid G] : (algebra_map k (monoid_algebra k G) : k → monoid_algebra k G) = single 1 := rfl lemma single_eq_algebra_map_mul_of [comm_semiring k] [monoid G] (a : G) (b : k) : single a b = (algebra_map k (monoid_algebra k G) : k → monoid_algebra k G) b * of k G a := by simp instance [group G] [semiring k] : distrib_mul_action G (monoid_algebra k G) := finsupp.comap_distrib_mul_action_self section lift variables (k G) [comm_semiring k] [monoid G] (R : Type u₃) [semiring R] [algebra k R] /-- Any monoid homomorphism `G →* R` can be lifted to an algebra homomorphism `monoid_algebra k G →ₐ[k] R`. -/ def lift : (G →* R) ≃ (monoid_algebra k G →ₐ[k] R) := { inv_fun := λ f, (f : monoid_algebra k G →* R).comp (of k G), to_fun := λ F, { to_fun := λ f, f.sum (λ a b, b • F a), map_one' := by { rw [one_def, sum_single_index, one_smul, F.map_one], apply zero_smul }, map_mul' := begin intros f g, rw [mul_def, finsupp.sum_mul, finsupp.sum_sum_index]; try { intros, simp only [zero_smul, add_smul], done }, refine finset.sum_congr rfl (λ a ha, _), simp only [], rw [finsupp.mul_sum, finsupp.sum_sum_index]; try { intros, simp only [zero_smul, add_smul], done }, refine finset.sum_congr rfl (λ a' ha', _), simp only [], rw [sum_single_index, F.map_mul, algebra.mul_smul_comm, algebra.smul_mul_assoc, smul_smul, mul_comm], apply zero_smul end, map_zero' := sum_zero_index, map_add' := λ f g, by rw [sum_add_index]; intros; simp only [zero_smul, add_smul], commutes' := λ r, by rw [coe_algebra_map, sum_single_index, F.map_one, algebra.smul_def, mul_one]; apply zero_smul }, left_inv := λ f, begin ext x, simp [sum_single_index] end, right_inv := λ F, begin ext f, conv_rhs { rw ← f.sum_single }, simp [← F.map_smul, finsupp.sum, ← F.map_sum] end } variables {k G R} lemma lift_apply (F : G →* R) (f : monoid_algebra k G) : lift k G R F f = f.sum (λ a b, b • F a) := rfl @[simp] lemma lift_symm_apply (F : monoid_algebra k G →ₐ[k] R) (x : G) : (lift k G R).symm F x = F (single x 1) := rfl lemma lift_of (F : G →* R) (x) : lift k G R F (of k G x) = F x := by rw [of_apply, ← lift_symm_apply, equiv.symm_apply_apply] @[simp] lemma lift_single (F : G →* R) (a b) : lift k G R F (single a b) = b • F a := by rw [single_eq_algebra_map_mul_of, ← algebra.smul_def, alg_hom.map_smul, lift_of] lemma lift_unique' (F : monoid_algebra k G →ₐ[k] R) : F = lift k G R ((F : monoid_algebra k G →* R).comp (of k G)) := ((lift k G R).apply_symm_apply F).symm /-- Decomposition of a `k`-algebra homomorphism from `monoid_algebra k G` by its values on `F (single a 1)`. -/ lemma lift_unique (F : monoid_algebra k G →ₐ[k] R) (f : monoid_algebra k G) : F f = f.sum (λ a b, b • F (single a 1)) := by conv_lhs { rw lift_unique' F, simp [lift_apply] } /-- A `k`-algebra homomorphism from `monoid_algebra k G` is uniquely defined by its values on the functions `single a 1`. -/ lemma alg_hom_ext ⦃φ₁ φ₂ : monoid_algebra k G →ₐ[k] R⦄ (h : ∀ x, φ₁ (single x 1) = φ₂ (single x 1)) : φ₁ = φ₂ := (lift k G R).symm.injective $ monoid_hom.ext h end lift -- TODO we should prove here that G and k commute; -- presumably a `linear_mul_action` typeclass is in order universe ui variable {ι : Type ui} lemma prod_single [comm_semiring k] [comm_monoid G] {s : finset ι} {a : ι → G} {b : ι → k} : s.prod (λi, single (a i) (b i)) = single (s.prod a) (s.prod b) := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, single_mul_single, prod_insert has, prod_insert has] section -- We now prove some additional statements that hold for group algebras. variables [semiring k] [group G] @[simp] lemma mul_single_apply (f : monoid_algebra k G) (r : k) (x y : G) : (f * single x r) y = f (y * x⁻¹) * r := f.mul_single_apply_aux $ λ a, eq_mul_inv_iff_mul_eq.symm @[simp] lemma single_mul_apply (r : k) (x : G) (f : monoid_algebra k G) (y : G) : (single x r * f) y = r * f (x⁻¹ * y) := f.single_mul_apply_aux $ λ z, eq_inv_mul_iff_mul_eq.symm lemma mul_apply_left (f g : monoid_algebra k G) (x : G) : (f * g) x = (f.sum $ λ a b, b * (g (a⁻¹ * x))) := calc (f * g) x = sum f (λ a b, (single a (f a) * g) x) : by rw [← finsupp.sum_apply, ← finsupp.sum_mul, f.sum_single] ... = _ : by simp only [single_mul_apply, finsupp.sum] -- If we'd assumed `comm_semiring`, we could deduce this from `mul_apply_left`. lemma mul_apply_right (f g : monoid_algebra k G) (x : G) : (f * g) x = (g.sum $ λa b, (f (x * a⁻¹)) * b) := calc (f * g) x = sum g (λ a b, (f * single a (g a)) x) : by rw [← finsupp.sum_apply, ← finsupp.mul_sum, g.sum_single] ... = _ : by simp only [mul_single_apply, finsupp.sum] end end monoid_algebra section variables [semiring k] /-- The monoid algebra over a semiring `k` generated by the additive monoid `G`. It is the type of finite formal `k`-linear combinations of terms of `G`, endowed with the convolution product. -/ @[derive [inhabited, add_comm_monoid]] def add_monoid_algebra := G →₀ k end namespace add_monoid_algebra variables {k G} local attribute [reducible] add_monoid_algebra section variables [semiring k] [add_monoid G] /-- The product of `f g : add_monoid_algebra k G` is the finitely supported function whose value at `a` is the sum of `f x * g y` over all pairs `x, y` such that `x + y = a`. (Think of the product of multivariate polynomials where `α` is the additive monoid of monomial exponents.) -/ instance : has_mul (add_monoid_algebra k G) := ⟨λf g, f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)⟩ lemma mul_def {f g : add_monoid_algebra k G} : f * g = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)) := rfl lemma mul_apply (f g : add_monoid_algebra k G) (x : G) : (f * g) x = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, if a₁ + a₂ = x then b₁ * b₂ else 0) := begin rw [mul_def], simp only [finsupp.sum_apply, single_apply], end lemma support_mul (a b : add_monoid_algebra k G) : (a * b).support ⊆ a.support.bind (λa₁, b.support.bind $ λa₂, {a₁ + a₂}) := subset.trans support_sum $ bind_mono $ assume a₁ _, subset.trans support_sum $ bind_mono $ assume a₂ _, support_single_subset /-- The unit of the multiplication is `single 1 1`, i.e. the function that is `1` at `0` and zero elsewhere. -/ instance : has_one (add_monoid_algebra k G) := ⟨single 0 1⟩ lemma one_def : (1 : add_monoid_algebra k G) = single 0 1 := rfl -- TODO: the simplifier unfolds 0 in the instance proof! private lemma zero_mul (f : add_monoid_algebra k G) : 0 * f = 0 := by simp only [mul_def, sum_zero_index] private lemma mul_zero (f : add_monoid_algebra k G) : f * 0 = 0 := by simp only [mul_def, sum_zero_index, sum_zero] private lemma left_distrib (a b c : add_monoid_algebra k G) : a * (b + c) = a * b + a * c := by simp only [mul_def, sum_add_index, mul_add, _root_.mul_zero, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_add] private lemma right_distrib (a b c : add_monoid_algebra k G) : (a + b) * c = a * c + b * c := by simp only [mul_def, sum_add_index, add_mul, _root_.mul_zero, _root_.zero_mul, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_zero, sum_add] instance : semiring (add_monoid_algebra k G) := { one := 1, mul := (*), one_mul := assume f, by simp only [mul_def, one_def, sum_single_index, _root_.zero_mul, single_zero, sum_zero, zero_add, one_mul, sum_single], mul_one := assume f, by simp only [mul_def, one_def, sum_single_index, _root_.mul_zero, single_zero, sum_zero, add_zero, mul_one, sum_single], zero_mul := zero_mul, mul_zero := mul_zero, mul_assoc := assume f g h, by simp only [mul_def, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, add_mul, mul_add, add_assoc, mul_assoc, _root_.zero_mul, _root_.mul_zero, sum_zero, sum_add], left_distrib := left_distrib, right_distrib := right_distrib, .. finsupp.add_comm_monoid } lemma single_mul_single {a₁ a₂ : G} {b₁ b₂ : k} : (single a₁ b₁ : add_monoid_algebra k G) * single a₂ b₂ = single (a₁ + a₂) (b₁ * b₂) := (sum_single_index (by simp only [_root_.zero_mul, single_zero, sum_zero])).trans (sum_single_index (by rw [_root_.mul_zero, single_zero])) section variables (k G) /-- Embedding of a monoid into its monoid algebra. -/ def of : multiplicative G →* add_monoid_algebra k G := { to_fun := λ a, single a 1, map_one' := rfl, map_mul' := λ a b, by { rw [single_mul_single, one_mul], refl } } end @[simp] lemma of_apply (a : G) : of k G a = single a 1 := rfl lemma mul_single_apply_aux (f : add_monoid_algebra k G) (r : k) (x y z : G) (H : ∀ a, a + x = z ↔ a = y) : (f * single x r) z = f y * r := have A : ∀ a₁ b₁, (single x r).sum (λ a₂ b₂, ite (a₁ + a₂ = z) (b₁ * b₂) 0) = ite (a₁ + x = z) (b₁ * r) 0, from λ a₁ b₁, sum_single_index $ by simp, calc (f * single x r) z = sum f (λ a b, if (a = y) then (b * r) else 0) : -- different `decidable` instances make it not trivial by { simp only [mul_apply, A, H], congr, funext, split_ifs; refl } ... = if y ∈ f.support then f y * r else 0 : f.support.sum_ite_eq' _ _ ... = f y * r : by split_ifs with h; simp at h; simp [h] lemma mul_single_zero_apply (f : add_monoid_algebra k G) (r : k) (x : G) : (f * single 0 r) x = f x * r := f.mul_single_apply_aux r _ _ _ $ λ a, by rw [add_zero] lemma single_mul_apply_aux (f : add_monoid_algebra k G) (r : k) (x y z : G) (H : ∀ a, x + a = y ↔ a = z) : (single x r * f) y = r * f z := have f.sum (λ a b, ite (x + a = y) (0 * b) 0) = 0, by simp, calc (single x r * f) y = sum f (λ a b, ite (x + a = y) (r * b) 0) : (mul_apply _ _ _).trans $ sum_single_index this ... = f.sum (λ a b, ite (a = z) (r * b) 0) : by { simp only [H], congr, ext; split_ifs; refl } ... = if z ∈ f.support then (r * f z) else 0 : f.support.sum_ite_eq' _ _ ... = _ : by split_ifs with h; simp at h; simp [h] lemma single_zero_mul_apply (f : add_monoid_algebra k G) (r : k) (x : G) : (single 0 r * f) x = r * f x := f.single_mul_apply_aux r _ _ _ $ λ a, by rw [zero_add] end instance [comm_semiring k] [add_comm_monoid G] : comm_semiring (add_monoid_algebra k G) := { mul_comm := assume f g, begin simp only [mul_def, finsupp.sum, mul_comm], rw [finset.sum_comm], simp only [add_comm] end, .. add_monoid_algebra.semiring } instance [ring k] : has_neg (add_monoid_algebra k G) := by apply_instance instance [ring k] [add_monoid G] : ring (add_monoid_algebra k G) := { neg := has_neg.neg, add_left_neg := add_left_neg, .. add_monoid_algebra.semiring } instance [comm_ring k] [add_comm_monoid G] : comm_ring (add_monoid_algebra k G) := { mul_comm := mul_comm, .. add_monoid_algebra.ring} instance [semiring k] : has_scalar k (add_monoid_algebra k G) := finsupp.has_scalar instance [semiring k] : semimodule k (add_monoid_algebra k G) := finsupp.semimodule G k instance [ring k] : module k (add_monoid_algebra k G) := finsupp.module G k instance [comm_semiring k] [add_monoid G] : algebra k (add_monoid_algebra k G) := { to_fun := single 0, map_one' := rfl, map_mul' := λ x y, by rw [single_mul_single, zero_add], map_zero' := single_zero, map_add' := λ x y, single_add, smul_def' := λ r a, by { ext x, exact smul_apply.trans (single_zero_mul_apply _ _ _).symm }, commutes' := λ r f, show single 0 r * f = f * single 0 r, by ext; rw [single_zero_mul_apply, mul_single_zero_apply, mul_comm] } @[simp] lemma coe_algebra_map [comm_semiring k] [add_monoid G] : (algebra_map k (add_monoid_algebra k G) : k → add_monoid_algebra k G) = single 0 := rfl /-- Any monoid homomorphism `multiplicative G →* R` can be lifted to an algebra homomorphism `add_monoid_algebra k G →ₐ[k] R`. -/ def lift [comm_semiring k] [add_monoid G] {R : Type u₃} [semiring R] [algebra k R] : (multiplicative G →* R) ≃ (add_monoid_algebra k G →ₐ[k] R) := { inv_fun := λ f, ((f : add_monoid_algebra k G →+* R) : add_monoid_algebra k G →* R).comp (of k G), to_fun := λ F, { to_fun := λ f, f.sum (λ a b, b • F a), map_one' := by { rw [one_def, sum_single_index, one_smul], erw [F.map_one], apply zero_smul }, map_mul' := begin intros f g, rw [mul_def, finsupp.sum_mul, finsupp.sum_sum_index]; try { intros, simp only [zero_smul, add_smul], done }, refine finset.sum_congr rfl (λ a ha, _), simp only [], rw [finsupp.mul_sum, finsupp.sum_sum_index]; try { intros, simp only [zero_smul, add_smul], done }, refine finset.sum_congr rfl (λ a' ha', _), simp only [], rw [sum_single_index], erw [F.map_mul], rw [algebra.mul_smul_comm, algebra.smul_mul_assoc, smul_smul, mul_comm], apply zero_smul end, map_zero' := sum_zero_index, map_add' := λ f g, by rw [sum_add_index]; intros; simp only [zero_smul, add_smul], commutes' := λ r, begin rw [coe_algebra_map, sum_single_index], erw [F.map_one], rw [algebra.smul_def, mul_one], apply zero_smul end, }, left_inv := λ f, begin ext x, simp [sum_single_index] end, right_inv := λ F, begin ext f, conv_rhs { rw ← f.sum_single }, simp [← F.map_smul, finsupp.sum, ← F.map_sum] end } -- It is hard to state the equivalent of `distrib_mul_action G (monoid_algebra k G)` -- because we've never discussed actions of additive groups. universe ui variable {ι : Type ui} lemma prod_single [comm_semiring k] [add_comm_monoid G] {s : finset ι} {a : ι → G} {b : ι → k} : s.prod (λi, single (a i) (b i)) = single (s.sum a) (s.prod b) := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, single_mul_single, sum_insert has, prod_insert has] end add_monoid_algebra
8a77d7f0476bc2732d2fc499ef39ad1bd2b6349f
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Elab/DeclarationRange.lean
8d11137fd89fcfd31b9885e829c060020fb0811d
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach" ]
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
2,514
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.DeclarationRange import Lean.Elab.Log import Lean.Data.Lsp.Utf16 namespace Lean.Elab def getDeclarationRange [Monad m] [MonadFileMap m] (stx : Syntax) : m DeclarationRange := do let fileMap ← getFileMap let pos := stx.getPos?.getD 0 let endPos := stx.getTailPos?.getD pos |> fileMap.toPosition let pos := pos |> fileMap.toPosition return { pos := pos charUtf16 := fileMap.leanPosToLspPos pos |>.character endPos := endPos endCharUtf16 := fileMap.leanPosToLspPos endPos |>.character } /-- For most builtin declarations, the selection range is just its name, which is stored in the second position. Example: ``` "def " >> declId >> optDeclSig >> declVal ``` If the declaration name is absent, we use the keyword instead. This function converts the given `Syntax` into one that represents its "selection range". -/ def getDeclarationSelectionRef (stx : Syntax) : Syntax := if stx.isOfKind ``Lean.Parser.Command.instance then -- must skip `attrKind` and `optPrio` for `instance` if !stx[3].isNone then stx[3][0] else stx[1] else if stx[1][0].isIdent then stx[1][0] -- `declId` else if stx[1].isIdent then stx[1] -- raw `ident` else stx[0] /-- Store the `range` and `selectionRange` for `declName` where `stx` is the whole syntax object decribing `declName`. This method is for the builtin declarations only. User-defined commands should use `Lean.addDeclarationRanges` to store this information for their commands. -/ def addDeclarationRanges [Monad m] [MonadEnv m] [MonadFileMap m] (declName : Name) (stx : Syntax) : m Unit := do if stx.getKind == ``Parser.Command.«example» then return () else Lean.addDeclarationRanges declName { range := (← getDeclarationRange stx) selectionRange := (← getDeclarationRange (getDeclarationSelectionRef stx)) } /-- Auxiliary method for recording ranges for auxiliary declarations (e.g., fields, nested declarations, etc. -/ def addAuxDeclarationRanges [Monad m] [MonadEnv m] [MonadFileMap m] (declName : Name) (stx : Syntax) (header : Syntax) : m Unit := do Lean.addDeclarationRanges declName { range := (← getDeclarationRange stx) selectionRange := (← getDeclarationRange header) } end Lean.Elab
c49d873ac62f231b7c3e69d3b9cc2eaf3785f6df
9bf90df35bb15a2f76571e35c48192142a328c40
/src/ch6.lean
90d25cbf38a059fb1242695270f75267350ca459
[]
no_license
ehaskell1/set_theory
ed0726520e84990d5f3180bafa0a3674ed31fb5e
e6c829c4dd953d98c9cba08f9f79784cd91794fb
refs/heads/master
1,693,282,405,362
1,636,928,916,000
1,636,928,916,000
428,055,746
0
0
null
null
null
null
UTF-8
Lean
false
false
30,069
lean
import ch4 universe u namespace Set local attribute [irreducible] mem reserve infix ` ≈ `:50 reserve infix ` ≉ `:50 reserve infix ` ≼ `:50 structure correspondence (A B f : Set) : Prop := (onto : f.onto_fun A B) (oto : f.one_to_one) lemma id_corr {A : Set} : A.correspondence A A.id := ⟨id_onto, id_oto⟩ lemma empty_corr_empty : correspondence ∅ ∅ ∅ := ⟨empty_onto, empty_oto⟩ def equinumerous (A B : Set) : Prop := ∃ f : Set, A.correspondence B f infix ≈ := equinumerous infix ≉ := (λ A B : Set, ¬(A ≈ B)) lemma empty_of_equin_empty {A : Set} (Ae : A ≈ ∅) : A = ∅ := begin rcases Ae with ⟨f, fonto, foto⟩, rw [←fonto.right.left, eq_empty], intros x xf, rw mem_dom at xf, rcases xf with ⟨y, xyf⟩, apply mem_empty y, rw ←fonto.right.right, rw mem_ran, exact ⟨_, xyf⟩, end def nat_prod_nat_equin_nat : prod nat.{u} nat.{u} ≈ nat.{u} := begin let f : Set := pair_sep_eq (prod ω ω) ω (λ z, pred.fun_value (two ^ z.fst * (two * z.snd).succ)), refine ⟨f, ⟨pair_sep_eq_is_fun, pair_sep_eq_dom_eq _, pair_sep_eq_ran_eq _⟩, pair_sep_eq_oto _⟩, { intros z hz, rw mem_prod at hz, rcases hz with ⟨m, hm, n, hn, he⟩, subst he, simp only [fst_congr, snd_congr], rw ←pred_onto_fun.right.right, apply fun_value_def'' pred_onto_fun.left, rw pred_onto_fun.right.left, exact mul_into_nat (exp_into_nat two_nat hm) (nat_induct.succ_closed (mul_into_nat two_nat hn)), }, { intros k hk, have hmod2 := mod2_fun_value_mem hk, simp [bool] at hmod2, cases hmod2, obtain ⟨n, hn, he⟩ := mod2_spec_zero hk hmod2, subst he, use pair ∅ n, split, rw pair_mem_prod, exact ⟨zero_nat, hn⟩, dsimp, rw [fst_congr, snd_congr, exp_base two_nat, one_mul (nat_induct.succ_closed hk), pred_succ_eq_self hk], obtain ⟨q, hq, he⟩ := mod2_spec_one' hk hmod2, obtain ⟨n, hn, l, hl, hlo, he'⟩ := nonzero_nat_factor_two (nat_induct.succ_closed hq) succ_neq_empty, obtain ⟨m, hm, he''⟩ := mod2_spec_one hl hlo, rw [he', two_pow_lemma hn hl, he''] at he, subst he, use n.succ.pair m, dsimp, rw [pair_mem_prod, fst_congr, snd_congr], exact ⟨⟨nat_induct.succ_closed hn, hm⟩, rfl⟩, }, { have hnez : ∀ {n : Set.{u}}, n ∈ nat.{u} → ∀ {m : Set}, m ∈ nat.{u} → two.{u} ^ n * (two * m).succ ≠ ∅, intros n hn m hm he, apply mem_empty ∅, nth_rewrite 1 ←he, rw ←zero_mul (nat_induct.succ_closed (mul_into_nat two_nat hm)), rw ←mul_lt_mul_of_lt zero_nat (exp_into_nat two_nat hn) (nat_induct.succ_closed (mul_into_nat two_nat hm)) succ_neq_empty, exact nonzero_exp_positive two_nat zero_ne_two.symm hn, have hnmnat : ∀ {n : Set.{u}}, n ∈ nat.{u} → ∀ {m : Set}, m ∈ nat.{u} → two.{u} ^ n * (two * m).succ ∈ nat.{u}, intros n hn m hm, exact mul_into_nat (exp_into_nat two_nat hn) (nat_induct.succ_closed (mul_into_nat two_nat hm)), have hcases : ∀ {n : Set.{u}}, n ∈ nat.{u} → ∀ {m : Set}, m ∈ nat.{u} → ∀ {n' : Set.{u}}, n' ∈ nat.{u} → ∀ {m' : Set}, m' ∈ nat.{u} → two ^ n * (two * m).succ = two.{u} ^ n' * (two * m').succ → n ∈ n' → false, intros n hn m hm n' hn' m' hm' he hnn', rw lt_iff hn hn' at hnn', rcases hnn' with ⟨k, hk, hke⟩, rw ←hke at he, rw exp_add two_nat hn (nat_induct.succ_closed hk) at he, have two_exp_n := exp_into_nat two_nat hn, have two_mul_m' := nat_induct.succ_closed (mul_into_nat two_nat hm'), rw ←mul_assoc two_exp_n (exp_into_nat two_nat (nat_induct.succ_closed hk)) two_mul_m' at he, have two_exp_n_nz : (two.{u} ^ n) ≠ ∅, intro h, apply mem_empty ∅, nth_rewrite 1 ←h, refine nonzero_exp_positive two_nat zero_ne_two.symm hn, replace he := cancel_mul_left (nat_induct.succ_closed (mul_into_nat two_nat hm)) (hnmnat (nat_induct.succ_closed hk) hm') two_exp_n two_exp_n_nz he, rw exp_ind two_nat hk at he, have two_exp_k := exp_into_nat two_nat hk, rw [mul_comm two_exp_k two_nat, ←mul_assoc two_nat two_exp_k two_mul_m', mul_comm two_nat (mul_into_nat two_exp_k two_mul_m')] at he, have two_mul_m := mul_into_nat two_nat hm, rw succ_eq_add_one two_mul_m at he, have tdo : two.divides one, apply mod2_of_sum_mod2_of_mod2 two_mul_m one_nat, rw he, refine ⟨_, mul_into_nat two_exp_k two_mul_m', rfl⟩, refine ⟨_, hm, _⟩, rw mul_comm two_nat hm, exact not_two_divides_one tdo, simp only [mem_prod], rintros z ⟨n, hn, m, hm, hz⟩ z' ⟨n', hn', m', hm', hz'⟩ he, subst hz, subst hz', simp only [fst_congr, snd_congr] at he, replace he := pred_oto (hnmnat hn hm) (hnez hn hm) (hnmnat hn' hm') (hnez hn' hm') he, have hnn' : n = n', apply classical.by_contradiction, intro hnn', replace hnn' := nat_order_conn hn hn' hnn', cases hnn', exact hcases hn hm hn' hm' he hnn', exact hcases hn' hm' hn hm he.symm hnn', rw ←hnn' at he, have two_exp_n_nz : two.{u} ^ n ≠ ∅, intro h, apply mem_empty ∅, nth_rewrite 1 ←h, refine nonzero_exp_positive two_nat zero_ne_two.symm hn, replace he := cancel_mul_left (nat_induct.succ_closed (mul_into_nat two_nat hm)) (nat_induct.succ_closed (mul_into_nat two_nat hm')) (exp_into_nat two_nat hn) two_exp_n_nz he, replace he := succ_inj (mul_into_nat two_nat hm) (mul_into_nat two_nat hm') he, replace he := cancel_mul_left hm hm' two_nat zero_ne_two.symm he, congr, exact hnn', exact he, }, end local attribute [instance] classical.prop_decidable def char_fun (B A : Set) : Set := pair_sep (λ b c, c = if b ∈ A then one else ∅) B two lemma char_fun_into_fun {B A : Set} : (B.char_fun A).into_fun B two := begin refine ⟨pair_sep_eq_is_fun, pair_sep_eq_dom_eq _, pair_sep_eq_ran_sub⟩, intros b hb, simp only [hb, if_true, two, succ, mem_union, one, mem_singleton], finish, end lemma char_fun_value {B A x : Set} (hx : x ∈ B) : (B.char_fun A).fun_value x = if x ∈ A then one else ∅ := begin symmetry, apply fun_value_def (is_function_of_into char_fun_into_fun), simp only [char_fun, pair_mem_pair_sep], refine ⟨hx, _, rfl⟩, by_cases h : x ∈ A, { simp only [h, if_true, two, succ, mem_union, mem_singleton], finish, }, { simp only [h, if_false, two, succ, mem_union, one, mem_singleton], finish, }, end lemma char_fun_value' {B A x : Set} (hx : x ∈ B) : (B.char_fun A).fun_value x = one ↔ x ∈ A := begin rw char_fun_value hx, by_cases h : x ∈ A, { simp only [h, if_true, eq_self_iff_true], }, { simp only [h, if_false, iff_false], intro he, simp only [one, succ] at he, apply mem_empty ∅, nth_rewrite 1 he, rw [mem_union, mem_singleton], finish, }, end def powerset_corr (B : Set) : Set := pair_sep (λ A f, f = B.char_fun A) B.powerset (B.into_funs two) lemma powerset_corr_onto {B : Set.{u}} : B.powerset_corr.onto_fun B.powerset (B.into_funs two) := begin refine ⟨pair_sep_eq_is_fun, pair_sep_eq_dom_eq _, pair_sep_eq_ran_eq _⟩, { intros A hA, rw mem_into_funs, exact char_fun_into_fun, }, { intros f hf, let A := {b ∈ B | f.fun_value b = one}, refine ⟨A, _, _⟩, { simp only [mem_powerset], intro a, rw mem_sep, finish, }, rw mem_into_funs at hf, apply fun_ext (is_function_of_into hf) (is_function_of_into char_fun_into_fun), { rw [dom_eq_of_into hf, dom_eq_of_into char_fun_into_fun], }, { intros x hx, apply fun_value_def (is_function_of_into char_fun_into_fun), simp only [char_fun, pair_mem_pair_sep], refine ⟨_, _, _⟩, { rw ←dom_eq_of_into hf, assumption, }, { apply ran_sub_of_into hf, exact fun_value_def'' (is_function_of_into hf) hx, }, by_cases h : x ∈ A, { simp only [h, if_true], rw [mem_sep] at h, finish, }, { simp only [h, if_false], have hor : (f.fun_value x) ∈ two.{u}, apply ran_sub_of_into hf, exact fun_value_def'' (is_function_of_into hf) hx, rw dom_eq_of_into hf at hx, simp only [two, succ, mem_union, mem_singleton] at hor, conv at hor { find (f.fun_value x ∈ one.{u}) { rw one }, }, simp only [succ, mem_union, mem_singleton, iff_false_intro (mem_empty (f.fun_value x)), or_false] at hor, rw mem_sep at h, cases hor, { exfalso, apply h, finish, }, { finish, }, }, }, }, end lemma powerset_corr_fun_value {B A : Set} (hB : A ⊆ B) : B.powerset_corr.fun_value A = B.char_fun A := begin symmetry, apply fun_value_def pair_sep_eq_is_fun, simp only [pair_mem_pair_sep, mem_powerset, mem_into_funs], exact ⟨hB, char_fun_into_fun, rfl⟩, end lemma powerset_corr_oto {B : Set} : B.powerset_corr.one_to_one := begin apply one_to_one_of (is_function_of_into (into_of_onto powerset_corr_onto)), intros A hA A' hA' hne he, apply hne, simp only [dom_eq_of_into (into_of_onto powerset_corr_onto), mem_powerset] at hA hA', rw [powerset_corr_fun_value hA, powerset_corr_fun_value hA'] at he, apply ext, intro x, split, { intro hx, rw ←char_fun_value' (hA hx), rw ←he, rw char_fun_value' (hA hx), assumption, }, { intro hx, rw ←char_fun_value' (hA' hx), rw he, rw char_fun_value' (hA' hx), assumption, }, end lemma powerset_corr_corr {A : Set} : A.powerset.correspondence (A.into_funs two) A.powerset_corr := ⟨powerset_corr_onto, powerset_corr_oto⟩ -- example on page 131 theorem powerset_equinumerous_into_funs {A : Set} : A.powerset.equinumerous (A.into_funs two) := ⟨_, powerset_corr_corr⟩ -- Theorem 6A part a theorem equin_refl {A : Set} : A.equinumerous A := ⟨_, id_onto, id_oto⟩ lemma corr_symm {A B f : Set} (h : correspondence A B f) : correspondence B A f.inv := begin split, split, rw T3F_a, exact h.oto, rw [T3E_a, T3E_b], exact ⟨h.onto.right.right, h.onto.right.left⟩, rw ←T3F_b h.onto.left.left, exact h.onto.left, end -- Theorem 6A part b theorem equin_symm {A B : Set} (heq : A.equinumerous B) : B.equinumerous A := begin rcases heq with ⟨F, hF⟩, exact ⟨F.inv, corr_symm hF⟩, end lemma corr_trans {A B C f g : Set} (hAB : correspondence A B f) (hBC : correspondence B C g) : correspondence A C (g.comp f) := begin have hfun : (g.comp f).is_function := T3H_a hBC.onto.left hAB.onto.left, have hdom : (g.comp f).dom = f.dom, rw T3H_b hBC.onto.left hAB.onto.left, apply ext, intro x, rw [mem_sep, hBC.onto.right.left, hAB.onto.right.left, and_iff_left_iff_imp, ←hAB.onto.right.left, ←hAB.onto.right.right], exact fun_value_def'' hAB.onto.left, refine ⟨⟨hfun, _, _⟩, _⟩, { rw hdom, exact hAB.onto.right.left, }, { apply ext, intro z, simp only [mem_ran_iff hfun, ←hBC.onto.right.right, mem_ran_iff hBC.onto.left, hBC.onto.right.left, ←hAB.onto.right.right, mem_ran_iff hAB.onto.left, hdom], split, { rintro ⟨x, hx, he⟩, refine ⟨f.fun_value x, ⟨_, hx, rfl⟩, _⟩, rw ←T3H_c hBC.onto.left hAB.onto.left, assumption, rw hdom, assumption, }, { rintro ⟨y, ⟨x, hx, he⟩, he'⟩, refine ⟨x, hx, _⟩, rw T3H_c hBC.onto.left hAB.onto.left, rw ←he, assumption, rw hdom, assumption, }, }, { apply one_to_one_of hfun, intros x hx x' hx' hne he, apply hne, rw [T3H_c hBC.onto.left hAB.onto.left hx, T3H_c hBC.onto.left hAB.onto.left hx'] at he, apply from_one_to_one hAB.onto.left hAB.oto, rw ←hdom, assumption, rw ←hdom, assumption, apply from_one_to_one hBC.onto.left hBC.oto, { rw [hBC.onto.right.left, ←hAB.onto.right.right], apply fun_value_def'' hAB.onto.left, rw ←hdom, assumption, }, { rw [hBC.onto.right.left, ←hAB.onto.right.right], apply fun_value_def'' hAB.onto.left, rw ←hdom, assumption, }, assumption, }, end -- Theorem 6A part c theorem equin_trans {A B C : Set} (hAB : A.equinumerous B) (hBC : B.equinumerous C) : A.equinumerous C := begin rcases hAB with ⟨F, hF⟩, rcases hBC with ⟨G, hG⟩, exact ⟨G.comp F, corr_trans hF hG⟩, end -- Prove that omega.times omega is equinumerous to omega -- Porve that the set of nats is equinumerous to the set of rational numbers -- Prove Theorem 6A part a (omega is not equinumerous to the set of real numbers) -- Theorem 6B part b theorem not_equin_powerset {A : Set} : ¬ A.equinumerous A.powerset := begin rintro ⟨g, gonto, goto⟩, let B := { x ∈ A | x ∉ g.fun_value x }, have hB : B ∈ g.ran, rw [gonto.right.right, mem_powerset], intro x, rw [mem_sep], intro h, finish, rw [mem_ran_iff gonto.left, gonto.right.left] at hB, rcases hB with ⟨x, hx, he⟩, have h : x ∈ B ↔ x ∉ g.fun_value x, simp only [mem_sep, hx, true_and], rw he at h, exact (iff_not_self _).mp h, end def is_finite (A : Set) : Prop := ∃ n : Set, n ∈ ω ∧ A.equinumerous n lemma inhab_of_inf {X : Set} (Xinf : ¬ X.is_finite) : X.inhab := begin apply classical.by_contradiction, intro Xi, apply Xinf, refine ⟨_, zero_nat, _⟩, rw inhab at Xi, push_neg at Xi, rw (eq_empty _).mpr Xi, exact equin_refl, end lemma finite_of_equin_finite {A : Set} (hA : A.is_finite) {B : Set} (hAB : A ≈ B) : B.is_finite := begin rcases hA with ⟨n, hn, hAn⟩, exact ⟨_, hn, equin_trans (equin_symm hAB) hAn⟩, end theorem pigeonhole : ∀ {n : Set}, n ∈ ω → ∀ {A : Set}, A ⊂ n → ¬ n.equinumerous A := begin suffices h : ∀ {n : Set}, n ∈ ω → ∀ {f : Set}, f.into_fun n n → f.one_to_one → f.ran = n, rintros n hn A hA ⟨f, fonto, foto⟩, simp at hA, have h : A = n, rw ←fonto.right.right, have hinto : f.into_fun n n, refine ⟨fonto.left, fonto.right.left, _⟩, rw fonto.right.right, exact hA.left, exact h hn hinto foto, exact hA.right h, apply @induction (λ n, ∀ {f : Set}, f.into_fun n n → f.one_to_one → f.ran = n), { intros f hinto foto, simp only [←mem_into_funs, ex2, mem_singleton] at hinto, rw hinto, apply ext, intro y, simp only [mem_ran, iff_false_intro (mem_empty _), exists_false], }, { intros k hk hi, have hcaseI : ∀ {f : Set}, f.into_fun k.succ k.succ → f.one_to_one → (∀ {n : Set}, n ∈ k → f.fun_value n ∈ k) → f.ran = k.succ, intros f hinto hoto hc, have hf : {k.pair (f.fun_value k)} ∪ f.restrict k = f, have hd : k ∈ f.dom, rw hinto.right.left, exact self_mem_succ, rw ←restrict_singleton_eq hinto.left hd, apply restrict_combine hinto.left.left, rw [hinto.right.left, succ], have hd : k ⊆ f.dom, rw [hinto.right.left], exact self_sub_succ, have hf' : (f.restrict k).ran = k, have hr : (f.restrict k).ran ⊆ k, intro n, rw [mem_ran_iff (restrict_is_function hinto.left), restrict_dom hd], rintro ⟨x, hx, he⟩, rw he, rw restrict_fun_value hinto.left hd hx, exact hc hx, rw eq_iff_subset_and_subset, refine ⟨hr, _⟩, have frinto : (f.restrict k).into_fun k k, refine ⟨restrict_is_function hinto.left, restrict_dom hd, hr⟩, have froto: (f.restrict k).one_to_one := restrict_one_to_one hinto.left hoto hd, rw hi frinto froto, exact subset_self, have hf'' : ({k.pair (f.fun_value k)} : Set).ran = {k}, rw ran_singleton, rw singleton_eq, have ho : f.fun_value k ∈ k ∨ f.fun_value k = k, rw [←le_iff, ←mem_succ_iff_le], apply hinto.right.right, apply fun_value_def'' hinto.left, rw hinto.right.left, exact self_mem_succ, cases ho, { exfalso, nth_rewrite 1 ←hf' at ho, rw [mem_ran_iff, restrict_dom hd] at ho, rcases ho with ⟨n, hn, he⟩, rw restrict_fun_value hinto.left hd hn at he, have h : k = n, apply from_one_to_one hinto.left hoto, { rw hinto.right.left, exact self_mem_succ, }, { rw hinto.right.left, exact lt_trans ((mem_nat_iff hk).mp hn).left hk (nat_induct.succ_closed hk) hn self_mem_succ, }, { exact he, }, rw ←h at hn, exact nat_not_mem_self hk hn, exact restrict_is_function hinto.left, }, { assumption, }, rw [←hf, ran_union, hf', hf'', succ], intros f hinto hoto, by_cases hc : ∃ n : Set, n ∈ k ∧ f.fun_value n = k, { rcases hc with ⟨p, hp, he⟩, let f' : Set := {p.pair (f.fun_value k), k.pair k} ∪ (f \ {p.pair k, k.pair (f.fun_value k)}), have hps : p ∈ k.succ := self_sub_succ hp, have hr : ∀ {x : Set}, x ∈ k.succ → f.fun_value x ∈ k.succ, intros x hx, apply hinto.right.right, rw ←hinto.right.left at hx, exact fun_value_def'' hinto.left hx, have f'into : f'.into_fun k.succ k.succ, rw fun_def_equiv, refine ⟨λ z hz, _, λ x hx, _⟩, { simp only [mem_union, mem_insert, mem_singleton, mem_diff] at hz, simp only [mem_prod], rcases hz with (hz|hz)|⟨hm,hz⟩, { exact ⟨_, hps, _, hr self_mem_succ, hz⟩, }, { exact ⟨_, self_mem_succ, _, self_mem_succ, hz⟩, }, { rw fun_def_equiv at hinto, replace hinto := hinto.left hm, rw mem_prod at hinto, assumption, }, }, { rw fun_def_equiv at hinto, simp only [mem_union, mem_insert, mem_singleton, mem_diff], by_cases hep : x = p, { rw hep, refine ⟨_, or.inl (or.inl rfl), _⟩, rintros y ((hz|hz)|⟨hm,hz⟩), { exact (pair_inj hz).right, }, { rw [←(pair_inj hz).left, (pair_inj hz).right, he], }, { rw ←fun_def_equiv at hinto, exfalso, apply hz, left, rw ←he, rw fun_value_def hinto.left hm, }, }, { rw [mem_succ_iff_le, le_iff] at hx, cases hx, { rw ←fun_def_equiv at hinto, refine ⟨f.fun_value x, or.inr ⟨fun_value_def' hinto.left _, _⟩, _⟩, { rw hinto.right.left, exact self_sub_succ hx, }, { rintro (hpe|hpe), { exact hep (pair_inj hpe).left, }, { rw (pair_inj hpe).left at hx, exact nat_not_mem_self hk hx, }, }, { rintros y ((hz|hz)|⟨hm,hz⟩), { exfalso, exact hep (pair_inj hz).left, }, { exfalso, rw (pair_inj hz).left at hx, exact nat_not_mem_self hk hx, }, { exact fun_value_def hinto.left hm, }, }, }, { rw hx, refine ⟨_, or.inl (or.inr rfl), _⟩, rintros y ((hz|hz)|⟨hm,hz⟩), { exfalso, rw (pair_inj hz).left at hx, exact hep hx, }, { exact (pair_inj hz).right, }, { exfalso, apply hz, right, rw ←fun_def_equiv at hinto, rw fun_value_def hinto.left hm, }, }, }, }, have hfp : f'.fun_value p = f.fun_value k, symmetry, apply fun_value_def f'into.left, simp only [mem_union, mem_insert, mem_singleton], finish, have hfk : f'.fun_value k = k, symmetry, apply fun_value_def f'into.left, simp only [mem_union, mem_insert, mem_singleton], finish, have hfo : ∀ {n : Set}, n ≠ p → n ∈ k → f'.fun_value n = f.fun_value n, intros l hlp hlk, symmetry, apply fun_value_def f'into.left, simp only [mem_union, mem_insert, mem_singleton, mem_diff], refine or.inr ⟨fun_value_def' hinto.left _, _⟩, { rw hinto.right.left, exact self_sub_succ hlk, }, { rintro (hor|hor), { exact hlp (pair_inj hor).left, }, { apply nat_not_mem_self hk, suffices hlke : l = k, nth_rewrite 0 ←hlke, exact hlk, apply from_one_to_one hinto.left hoto, { rw hinto.right.left, exact self_sub_succ hlk, }, { rw hinto.right.left, exact self_mem_succ, }, exact (pair_inj hor).right, }, }, have hor : ∀ {n : Set}, n ∈ k.succ → n = p ∨ n = k ∨ n ≠ p ∧ n ∈ k, intros m hmk, by_cases ho : m = p, { exact or.inl ho, }, { rw [mem_succ_iff_le, le_iff] at hmk, cases hmk, { exact or.inr (or.inr ⟨ho, hmk⟩), }, { exact or.inr (or.inl hmk), }, }, have f'oto : f'.one_to_one, apply one_to_one_of f'into.left, simp only [f'into.right.left], intros m hm n hn hne hmn, apply hne, obtain (horm|horm|horm) := hor hm; obtain (horn|horn|horn) := hor hn, { rw [horm, horn], }, { exfalso, apply nat_not_mem_self hk, suffices hkp : k = p, nth_rewrite 0 hkp, exact hp, apply from_one_to_one hinto.left hoto, { rw hinto.right.left, exact self_mem_succ, }, { rw [hinto.right.left, ←horm], exact hm, }, rw [←hfp, ←horm, hmn, horn, hfk, ←he, horm], }, { exfalso, apply nat_not_mem_self hk, suffices hkn : k = n, nth_rewrite 0 hkn, exact horn.right, apply from_one_to_one hinto.left hoto, { rw hinto.right.left, exact self_mem_succ, }, { rw hinto.right.left, exact hn, }, rw [←hfp, ←horm, hmn, hfo horn.left horn.right], }, { exfalso, apply nat_not_mem_self hk, suffices hkp : k = p, nth_rewrite 0 hkp, exact hp, apply from_one_to_one hinto.left hoto, { rw hinto.right.left, exact self_mem_succ, }, { rw [hinto.right.left, ←horn], exact hn, }, rw [←hfp, ←horn, ←hmn, horm, hfk, ←he, horn], }, { rw [horm, horn], }, { exfalso, apply horn.left, apply from_one_to_one hinto.left hoto, { rw hinto.right.left, exact hn, }, { rw hinto.right.left, exact self_sub_succ hp, }, rw [he, ←hfk, ←horm, hmn, hfo horn.left horn.right], }, { exfalso, apply nat_not_mem_self hk, suffices hkn : k = m, nth_rewrite 0 hkn, exact horm.right, apply from_one_to_one hinto.left hoto, { rw hinto.right.left, exact self_mem_succ, }, { rw hinto.right.left, exact hm, }, rw [←hfp, ←horn, ←hmn, hfo horm.left horm.right], }, { exfalso, apply horm.left, apply from_one_to_one hinto.left hoto, { rw hinto.right.left, exact hm, }, { rw hinto.right.left, exact self_sub_succ hp, }, rw [he, ←hfk, ←horn, ←hmn, hfo horm.left horm.right], }, { apply from_one_to_one hinto.left hoto, { rw hinto.right.left, exact hm, }, { rw hinto.right.left, exact hn, }, rw [←hfo horm.left horm.right, ←hfo horn.left horn.right, hmn], }, have f'cl : ∀ {n : Set}, n ∈ k → f'.fun_value n ∈ k, intros n hnk, by_cases hnp : n = p, { rw [hnp, hfp], specialize hr self_mem_succ, rw [mem_succ_iff_le, le_iff] at hr, cases hr, { exact hr, }, { exfalso, nth_rewrite 1 ←he at hr, suffices hkp : k = p, rw ←hkp at hp, exact nat_not_mem_self hk hp, apply from_one_to_one hinto.left hoto, { rw hinto.right.left, exact self_mem_succ, }, { rw hinto.right.left, exact self_sub_succ hp, }, exact hr, }, }, { rw hfo hnp hnk, specialize hr (self_sub_succ hnk), rw [mem_succ_iff_le, le_iff] at hr, cases hr, { exact hr, }, { exfalso, rw ←he at hr, apply hnp, apply from_one_to_one hinto.left hoto, { rw hinto.right.left, exact self_sub_succ hnk, }, { rw hinto.right.left, exact hps, }, exact hr, }, }, have freq : f'.ran = f.ran, rw eq_iff_subset_and_subset, split, { intro y, simp only [mem_ran_iff hinto.left, mem_ran_iff f'into.left, hinto.right.left, f'into.right.left], rintro ⟨n, hn, hm⟩, obtain (hnp|hnk|⟨hnp,hnk⟩) := hor hn, { rw [hnp, hfp] at hm, exact ⟨_, self_mem_succ, hm⟩, }, { rw [hnk, hfk, ←he] at hm, exact ⟨_, self_sub_succ hp, hm⟩, }, { rw hfo hnp hnk at hm, exact ⟨_, hn, hm⟩, }, }, { rw hcaseI f'into f'oto @f'cl, exact hinto.right.right, }, rw ←freq, exact hcaseI f'into f'oto @f'cl, }, { replace hc : ∀ {n : Set}, n ∈ k → f.fun_value n ∈ k, intros n hn, have ho : f.fun_value n ∈ k ∨ f.fun_value n = k, rw [←le_iff, ←mem_succ_iff_le], apply hinto.right.right, apply fun_value_def'' hinto.left, rw hinto.right.left, rw [mem_succ_iff_le, le_iff], finish, cases ho, { assumption, }, { exfalso, exact hc ⟨_, hn, ho⟩, }, exact hcaseI hinto hoto @hc, }, }, end -- Corollary 6C theorem pigeonhole' : ¬ ∃ A : Set, A.is_finite ∧ ∃ B : Set, B ⊂ A ∧ A.equinumerous B := begin rintro ⟨A, ⟨n, hn, g, hfin⟩, B, hBA, f, heq⟩, let H : Set := g.comp (f.comp g.inv), have Hoto : H.one_to_one, refine comp_one_to_one hfin.oto (comp_one_to_one heq.oto _), rw ←T3F_b hfin.onto.left.left, exact hfin.onto.left, have Hdom : H.dom = n, have h : g.inv.ran = f.dom, rw [T3E_b, heq.onto.right.left, hfin.onto.right.left], have hl : g.inv.ran ⊆ f.dom, rw h, exact subset_self, have hr : f.dom ⊆ g.inv.ran, rw h, exact subset_self, have h' : (f.comp g.inv).ran ⊆ g.dom, rw [ran_comp hr, heq.onto.right.right, hfin.onto.right.left], exact hBA.left, rw [dom_comp h', dom_comp hl, T3E_a, hfin.onto.right.right], have Hran : H.ran ⊆ n, rw ←hfin.onto.right.right, exact ran_comp_sub, have Hfun : H.is_function, apply T3H_a hfin.onto.left, apply T3H_a heq.onto.left, rw T3F_a, exact hfin.oto, have hr : H.ran ⊂ n, refine ⟨Hran, λ he, _⟩, have hex : ∃ x, x ∈ A ∧ x ∉ B, apply classical.by_contradiction, intro h, apply hBA.right, rw eq_iff_subset_and_subset, refine ⟨hBA.left, λ x hx, _⟩, apply classical.by_contradiction, intro hnx, apply h, exact ⟨_, hx, hnx⟩, rcases hex with ⟨a, hA, hB⟩, have h : g.fun_value a ∉ H.ran, intro h, apply hB, rw mem_ran_iff Hfun at h, rcases h with ⟨x, hn, ho⟩, rw T3H_c hfin.onto.left (T3H_a heq.onto.left (T3F_a.mpr hfin.oto)) _ at ho, have h : (f.comp g.inv).ran = B, rw ←heq.onto.right.right, apply ran_comp, rw [T3E_b, hfin.onto.right.left, heq.onto.right.left], exact subset_self, have hax : a = (f.comp g.inv).fun_value x, apply from_one_to_one hfin.onto.left hfin.oto, { rw hfin.onto.right.left, exact hA, }, { rw hfin.onto.right.left, have h : (f.comp g.inv).ran = B, rw ←heq.onto.right.right, apply ran_comp, rw [T3E_b, hfin.onto.right.left, heq.onto.right.left], exact subset_self, apply hBA.left, rw ←h, apply fun_value_def'' (T3H_a heq.onto.left (T3F_a.mpr hfin.oto)), rw dom_comp, rw [T3E_a, hfin.onto.right.right], rw ←Hdom, exact hn, rw [T3E_b, hfin.onto.right.left, heq.onto.right.left], exact subset_self, }, exact ho, rw [hax, ←h], apply fun_value_def'' (T3H_a heq.onto.left (T3F_a.mpr hfin.oto)), rw dom_comp, rw [T3E_a, hfin.onto.right.right], rw ←Hdom, exact hn, rw [T3E_b, hfin.onto.right.left, heq.onto.right.left], exact subset_self, exact hn, apply h, rw he, rw ←hfin.onto.right.right, apply fun_value_def'' hfin.onto.left, rw hfin.onto.right.left, exact hA, apply pigeonhole hn hr, exact ⟨H, ⟨Hfun, Hdom, rfl⟩, Hoto⟩, end -- Corollary 6D part a theorem pigeonhole'' {A B : Set} (hBA : B ⊂ A) (heq : A.equinumerous B) : ¬ A.is_finite := begin intro hf, apply pigeonhole', exact ⟨A, hf, B, hBA, heq⟩, end -- Corollary 6D part b theorem nat_infinite : ¬ (ω : Set).is_finite := begin have h : ∅ ∉ succ_fun.ran := succ_not_onto, have hr : succ_fun.ran ⊂ ω, refine ⟨succ_fun_into_fun.right.right, _⟩, intro he, apply h, rw he, exact nat_induct.zero, exact pigeonhole'' hr ⟨succ_fun, ⟨succ_fun_into_fun.left, succ_fun_into_fun.right.left, rfl⟩, succ_one_to_one⟩, end -- Corollary 6D part c theorem exists_unique_equiv_nat_of_finite {A : Set} (hf : A.is_finite) : ∃! n : Set, n ∈ ω ∧ A.equinumerous n := begin apply exists_unique_of_exists_of_unique hf, rintros n m ⟨hn, hAn⟩ ⟨hm, hAm⟩, have hnmeq : n.equinumerous m := equin_trans (equin_symm hAn) hAm, apply classical.by_contradiction, intro hne, cases nat_order_lin.conn hn hm hne with hnm hnm; rw [nat_order, pair_mem_pair_sep] at hnm; rcases hnm with ⟨-, -, hnm⟩, { rw ←nat_ssub_iff_mem hn hm at hnm, exact pigeonhole'' hnm (equin_symm hnmeq) ⟨_, hm, equin_refl⟩, }, { rw ←nat_ssub_iff_mem hm hn at hnm, exact pigeonhole'' hnm hnmeq ⟨_, hn, equin_refl⟩, }, end -- problem 20 theorem exists_desc_chain_of_no_least {A : Set} (hA : A ≠ ∅) {R : Set} (hR : R.is_rel) (h : ∀ x ∈ A, ∃ y : Set, y ∈ A ∧ y.pair x ∈ R) : ∃ f : Set, f.into_fun ω A ∧ ∀ ⦃n : Set⦄, n ∈ ω → (f.fun_value n.succ).pair (f.fun_value n) ∈ R := begin let S : Set := pair_sep (λ x y : Set, y.pair x ∈ R) A A, have domS : S.dom = A, rw eq_iff_subset_and_subset, split, exact pair_sep_dom_sub, intros x hx, obtain ⟨y, hy, hyx⟩ := h _ hx, rw mem_dom, use y, rw pair_mem_pair_sep, exact ⟨hx, hy, hyx⟩, obtain ⟨g, gfun, gsub, gdom⟩ := @ax_ch_1 S pair_sep_is_rel, have ginto : g.into_fun A A, refine ⟨gfun, _, _⟩, rw [gdom, domS], intro y, rw mem_ran, rintro ⟨x, hxy⟩, replace hxy := gsub hxy, rw pair_mem_pair_sep at hxy, exact hxy.right.left, obtain ⟨a, ha⟩ := inhabited_of_ne_empty hA, let f : Set := A.rec_fun a g, refine ⟨f, rec_fun_into_fun ha ginto, λ n hn, _⟩, have he := (recursion_thm ha ginto).right _ hn, rw he, suffices h : (f.fun_value n).pair (g.fun_value ((A.rec_fun a g).fun_value n)) ∈ S, rw pair_mem_pair_sep at h, exact h.right.right, apply gsub, apply fun_value_def' ginto.left, rw [gdom, domS], apply @rec_fun_ran A a g, apply fun_value_def'' (rec_fun_is_fun ha), rw rec_fun_dom_eq_nat ha ginto, exact hn, end def dominated (A B : Set) : Prop := ∃ f : Set, f.into_fun A B ∧ f.one_to_one infix ≼ := dominated lemma dominated_iff {A B : Set} : A ≼ B ↔ ∃ C : Set, C ⊆ B ∧ A.equinumerous C := begin split, { rintro ⟨f, finto, foto⟩, exact ⟨_, finto.right.right, _, onto_ran_of_into finto, foto⟩, }, { rintro ⟨C, hCB, f, fonto, foto⟩, exact ⟨_, into_of_onto_ran_sub hCB fonto, foto⟩, }, end end Set
f72825a8f4bc731ac7316d3af610de36a1866df1
46125763b4dbf50619e8846a1371029346f4c3db
/src/topology/metric_space/emetric_space.lean
80d5c1b8c27544c46b49b9eda46aabc5f936ef86
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
37,774
lean
/- Copyright (c) 2015, 2017 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis, Johannes Hölzl, Mario Carneiro, Sébastien Gouëzel -/ import data.real.nnreal data.real.ennreal import topology.uniform_space.separation topology.uniform_space.uniform_embedding topology.uniform_space.pi import topology.bases /-! # Extended metric spaces This file is devoted to the definition and study of `emetric_spaces`, i.e., metric spaces in which the distance is allowed to take the value ∞. This extended distance is called `edist`, and takes values in `ennreal`. Many definitions and theorems expected on emetric spaces are already introduced on uniform spaces and topological spaces. For example: open and closed sets, compactness, completeness, continuity and uniform continuity The class `emetric_space` therefore extends `uniform_space` (and `topological_space`). -/ open lattice set filter classical noncomputable theory open_locale uniformity topological_space universes u v w variables {α : Type u} {β : Type v} {γ : Type w} /-- Characterizing uniformities associated to a (generalized) distance function `D` in terms of the elements of the uniformity. -/ theorem uniformity_dist_of_mem_uniformity [linear_order β] {U : filter (α × α)} (z : β) (D : α → α → β) (H : ∀ s, s ∈ U ↔ ∃ε>z, ∀{a b:α}, D a b < ε → (a, b) ∈ s) : U = ⨅ ε>z, principal {p:α×α | D p.1 p.2 < ε} := le_antisymm (le_infi $ λ ε, le_infi $ λ ε0, le_principal_iff.2 $ (H _).2 ⟨ε, ε0, λ a b, id⟩) (λ r ur, let ⟨ε, ε0, h⟩ := (H _).1 ur in mem_infi_sets ε $ mem_infi_sets ε0 $ mem_principal_sets.2 $ λ ⟨a, b⟩, h) class has_edist (α : Type*) := (edist : α → α → ennreal) export has_edist (edist) /- Design note: one could define an `emetric_space` just by giving `edist`, and then derive an instance of `uniform_space` by taking the natural uniform structure associated to the distance. This creates diamonds problem for products, as the uniform structure on the product of two emetric spaces could be obtained first by obtaining two uniform spaces and then taking their products, or by taking the product of the emetric spaces and then the associated uniform structure. The two uniform structure we have just described are equal, but not defeq, which creates a lot of problem. The idea is to add, in the very definition of an `emetric_space`, a uniform structure with a uniformity which equal to the one given by the distance, but maybe not defeq. And the instance from `emetric_space` to `uniform_space` uses this uniformity. In this way, when we create the product of emetric spaces, we put in the product the uniformity corresponding to the product of the uniformities. There is one more proof obligation, that this product uniformity is equal to the uniformity corresponding to the product metric. But the diamond problem disappears. The same trick is used in the definition of a metric space, where one stores as well a uniform structure and an edistance. -/ /-- Creating a uniform space from an extended distance. -/ def uniform_space_of_edist (edist : α → α → ennreal) (edist_self : ∀ x : α, edist x x = 0) (edist_comm : ∀ x y : α, edist x y = edist y x) (edist_triangle : ∀ x y z : α, edist x z ≤ edist x y + edist y z) : uniform_space α := uniform_space.of_core { uniformity := (⨅ ε>0, principal {p:α×α | edist p.1 p.2 < ε}), refl := le_infi $ assume ε, le_infi $ by simp [set.subset_def, id_rel, edist_self, (>)] {contextual := tt}, comp := le_infi $ assume ε, le_infi $ assume h, have (2 : ennreal) = (2 : ℕ) := by simp, have A : 0 < ε / 2 := ennreal.div_pos_iff.2 ⟨ne_of_gt h, this ▸ ennreal.nat_ne_top 2⟩, lift'_le (mem_infi_sets (ε / 2) $ mem_infi_sets A (subset.refl _)) $ have ∀ (a b c : α), edist a c < ε / 2 → edist c b < ε / 2 → edist a b < ε, from assume a b c hac hcb, calc edist a b ≤ edist a c + edist c b : edist_triangle _ _ _ ... < ε / 2 + ε / 2 : ennreal.add_lt_add hac hcb ... = ε : by rw [ennreal.add_halves], by simpa [comp_rel], symm := tendsto_infi.2 $ assume ε, tendsto_infi.2 $ assume h, tendsto_infi' ε $ tendsto_infi' h $ tendsto_principal_principal.2 $ by simp [edist_comm] } section prio set_option default_priority 100 -- see Note [default priority] /-- Extended metric spaces, with an extended distance `edist` possibly taking the value ∞ Each emetric space induces a canonical `uniform_space` and hence a canonical `topological_space`. This is enforced in the type class definition, by extending the `uniform_space` structure. When instantiating an `emetric_space` structure, the uniformity fields are not necessary, they will be filled in by default. There is a default value for the uniformity, that can be substituted in cases of interest, for instance when instantiating an `emetric_space` structure on a product. Continuity of `edist` is finally proving in `topology.instances.ennreal` -/ class emetric_space (α : Type u) extends has_edist α : Type u := (edist_self : ∀ x : α, edist x x = 0) (eq_of_edist_eq_zero : ∀ {x y : α}, edist x y = 0 → x = y) (edist_comm : ∀ x y : α, edist x y = edist y x) (edist_triangle : ∀ x y z : α, edist x z ≤ edist x y + edist y z) (to_uniform_space : uniform_space α := uniform_space_of_edist edist edist_self edist_comm edist_triangle) (uniformity_edist : 𝓤 α = ⨅ ε>0, principal {p:α×α | edist p.1 p.2 < ε} . control_laws_tac) end prio /- emetric spaces are less common than metric spaces. Therefore, we work in a dedicated namespace, while notions associated to metric spaces are mostly in the root namespace. -/ variables [emetric_space α] @[priority 100] -- see Note [lower instance priority] instance emetric_space.to_uniform_space' : uniform_space α := emetric_space.to_uniform_space α export emetric_space (edist_self eq_of_edist_eq_zero edist_comm edist_triangle) attribute [simp] edist_self /-- Characterize the equality of points by the vanishing of their extended distance -/ @[simp] theorem edist_eq_zero {x y : α} : edist x y = 0 ↔ x = y := iff.intro eq_of_edist_eq_zero (assume : x = y, this ▸ edist_self _) @[simp] theorem zero_eq_edist {x y : α} : 0 = edist x y ↔ x = y := iff.intro (assume h, eq_of_edist_eq_zero (h.symm)) (assume : x = y, this ▸ (edist_self _).symm) theorem edist_le_zero {x y : α} : (edist x y ≤ 0) ↔ x = y := le_zero_iff_eq.trans edist_eq_zero /-- Triangle inequality for the extended distance -/ theorem edist_triangle_left (x y z : α) : edist x y ≤ edist z x + edist z y := by rw edist_comm z; apply edist_triangle theorem edist_triangle_right (x y z : α) : edist x y ≤ edist x z + edist y z := by rw edist_comm y; apply edist_triangle lemma edist_triangle4 (x y z t : α) : edist x t ≤ edist x y + edist y z + edist z t := calc edist x t ≤ edist x z + edist z t : edist_triangle x z t ... ≤ (edist x y + edist y z) + edist z t : add_le_add_right' (edist_triangle x y z) /-- The triangle (polygon) inequality for sequences of points; `finset.Ico` version. -/ lemma edist_le_Ico_sum_edist (f : ℕ → α) {m n} (h : m ≤ n) : edist (f m) (f n) ≤ (finset.Ico m n).sum (λ i, edist (f i) (f (i + 1))) := begin revert n, refine nat.le_induction _ _, { simp only [finset.sum_empty, finset.Ico.self_eq_empty, edist_self], -- TODO: Why doesn't Lean close this goal automatically? `apply le_refl` fails too. exact le_refl (0:ennreal) }, { assume n hn hrec, calc edist (f m) (f (n+1)) ≤ edist (f m) (f n) + edist (f n) (f (n+1)) : edist_triangle _ _ _ ... ≤ (finset.Ico m n).sum _ + _ : add_le_add' hrec (le_refl _) ... = (finset.Ico m (n+1)).sum _ : by rw [finset.Ico.succ_top hn, finset.sum_insert, add_comm]; simp } end /-- The triangle (polygon) inequality for sequences of points; `finset.range` version. -/ lemma edist_le_range_sum_edist (f : ℕ → α) (n : ℕ) : edist (f 0) (f n) ≤ (finset.range n).sum (λ i, edist (f i) (f (i + 1))) := finset.Ico.zero_bot n ▸ edist_le_Ico_sum_edist f (nat.zero_le n) /-- A version of `edist_le_Ico_sum_edist` with each intermediate distance replaced with an upper estimate. -/ lemma edist_le_Ico_sum_of_edist_le {f : ℕ → α} {m n} (hmn : m ≤ n) {d : ℕ → ennreal} (hd : ∀ {k}, m ≤ k → k < n → edist (f k) (f (k + 1)) ≤ d k) : edist (f m) (f n) ≤ (finset.Ico m n).sum d := le_trans (edist_le_Ico_sum_edist f hmn) $ finset.sum_le_sum $ λ k hk, hd (finset.Ico.mem.1 hk).1 (finset.Ico.mem.1 hk).2 /-- A version of `edist_le_range_sum_edist` with each intermediate distance replaced with an upper estimate. -/ lemma edist_le_range_sum_of_edist_le {f : ℕ → α} (n : ℕ) {d : ℕ → ennreal} (hd : ∀ {k}, k < n → edist (f k) (f (k + 1)) ≤ d k) : edist (f 0) (f n) ≤ (finset.range n).sum d := finset.Ico.zero_bot n ▸ edist_le_Ico_sum_of_edist_le (zero_le n) (λ _ _, hd) /-- Two points coincide if their distance is `< ε` for all positive ε -/ theorem eq_of_forall_edist_le {x y : α} (h : ∀ε, ε > 0 → edist x y ≤ ε) : x = y := eq_of_edist_eq_zero (eq_of_le_of_forall_le_of_dense bot_le h) /-- Reformulation of the uniform structure in terms of the extended distance -/ theorem uniformity_edist : 𝓤 α = ⨅ ε>0, principal {p:α×α | edist p.1 p.2 < ε} := emetric_space.uniformity_edist α theorem uniformity_basis_edist : (𝓤 α).has_basis (λ ε : ennreal, 0 < ε) (λ ε, {p:α×α | edist p.1 p.2 < ε}) := (@uniformity_edist α _).symm ▸ has_basis_binfi_principal (λ r hr p hp, ⟨min r p, lt_min hr hp, λ x hx, lt_of_lt_of_le hx (min_le_left _ _), λ x hx, lt_of_lt_of_le hx (min_le_right _ _)⟩) ⟨1, ennreal.zero_lt_one⟩ /-- Characterization of the elements of the uniformity in terms of the extended distance -/ theorem mem_uniformity_edist {s : set (α×α)} : s ∈ 𝓤 α ↔ (∃ε>0, ∀{a b:α}, edist a b < ε → (a, b) ∈ s) := uniformity_basis_edist.mem_uniformity_iff /-- Given `f : β → ennreal`, if `f` sends `{i | p i}` to a set of positive numbers accumulating to zero, then `f i`-neighborhoods of the diagonal form a basis of `𝓤 α`. For specific bases see `uniformity_basis_edist`, `uniformity_basis_edist'`, `uniformity_basis_edist_nnreal`, and `uniformity_basis_edist_inv_nat`. -/ protected theorem emetric.mk_uniformity_basis {β : Type*} {p : β → Prop} {f : β → ennreal} (hf₀ : ∀ x, p x → 0 < f x) (hf : ∀ ε, 0 < ε → ∃ x (hx : p x), f x ≤ ε) : (𝓤 α).has_basis p (λ x, {p:α×α | edist p.1 p.2 < f x}) := begin refine λ s, uniformity_basis_edist.mem_iff.trans _, split, { rintros ⟨ε, ε₀, hε⟩, rcases hf ε ε₀ with ⟨i, hi, H⟩, exact ⟨i, hi, λ x hx, hε $ lt_of_lt_of_le hx H⟩ }, { exact λ ⟨i, hi, H⟩, ⟨f i, hf₀ i hi, H⟩ } end /-- Given `f : β → ennreal`, if `f` sends `{i | p i}` to a set of positive numbers accumulating to zero, then closed `f i`-neighborhoods of the diagonal form a basis of `𝓤 α`. For specific bases see `uniformity_basis_edist_le` and `uniformity_basis_edist_le'`. -/ protected theorem emetric.mk_uniformity_basis_le {β : Type*} {p : β → Prop} {f : β → ennreal} (hf₀ : ∀ x, p x → 0 < f x) (hf : ∀ ε, 0 < ε → ∃ x (hx : p x), f x ≤ ε) : (𝓤 α).has_basis p (λ x, {p:α×α | edist p.1 p.2 ≤ f x}) := begin refine λ s, uniformity_basis_edist.mem_iff.trans _, split, { rintros ⟨ε, ε₀, hε⟩, rcases dense ε₀ with ⟨ε', hε'⟩, rcases hf ε' hε'.1 with ⟨i, hi, H⟩, exact ⟨i, hi, λ x hx, hε $ lt_of_le_of_lt (le_trans hx H) hε'.2⟩ }, { exact λ ⟨i, hi, H⟩, ⟨f i, hf₀ i hi, λ x hx, H (le_of_lt hx)⟩ } end theorem uniformity_basis_edist_le : (𝓤 α).has_basis (λ ε : ennreal, 0 < ε) (λ ε, {p:α×α | edist p.1 p.2 ≤ ε}) := emetric.mk_uniformity_basis_le (λ _, id) (λ ε ε₀, ⟨ε, ε₀, le_refl ε⟩) theorem uniformity_basis_edist' (ε' : ennreal) (hε' : 0 < ε') : (𝓤 α).has_basis (λ ε : ennreal, ε ∈ Ioo 0 ε') (λ ε, {p:α×α | edist p.1 p.2 < ε}) := emetric.mk_uniformity_basis (λ _, and.left) (λ ε ε₀, let ⟨δ, hδ⟩ := dense hε' in ⟨min ε δ, ⟨lt_min ε₀ hδ.1, lt_of_le_of_lt (min_le_right _ _) hδ.2⟩, min_le_left _ _⟩) theorem uniformity_basis_edist_le' (ε' : ennreal) (hε' : 0 < ε') : (𝓤 α).has_basis (λ ε : ennreal, ε ∈ Ioo 0 ε') (λ ε, {p:α×α | edist p.1 p.2 ≤ ε}) := emetric.mk_uniformity_basis_le (λ _, and.left) (λ ε ε₀, let ⟨δ, hδ⟩ := dense hε' in ⟨min ε δ, ⟨lt_min ε₀ hδ.1, lt_of_le_of_lt (min_le_right _ _) hδ.2⟩, min_le_left _ _⟩) theorem uniformity_basis_edist_nnreal : (𝓤 α).has_basis (λ ε : nnreal, 0 < ε) (λ ε, {p:α×α | edist p.1 p.2 < ε}) := emetric.mk_uniformity_basis (λ _, ennreal.coe_pos.2) (λ ε ε₀, let ⟨δ, hδ⟩ := with_top.dense_coe ε₀ in ⟨δ, ennreal.coe_pos.1 hδ.1, le_of_lt hδ.2⟩) theorem uniformity_basis_edist_inv_nat : (𝓤 α).has_basis (λ _, true) (λ n:ℕ, {p:α×α | edist p.1 p.2 < (↑n)⁻¹}) := emetric.mk_uniformity_basis (λ n _, ennreal.inv_pos.2 $ ennreal.nat_ne_top n) (λ ε ε₀, let ⟨n, hn⟩ := ennreal.exists_inv_nat_lt (ne_of_gt ε₀) in ⟨n, trivial, le_of_lt hn⟩) /-- Fixed size neighborhoods of the diagonal belong to the uniform structure -/ theorem edist_mem_uniformity {ε:ennreal} (ε0 : 0 < ε) : {p:α×α | edist p.1 p.2 < ε} ∈ 𝓤 α := mem_uniformity_edist.2 ⟨ε, ε0, λ a b, id⟩ namespace emetric theorem uniformity_has_countable_basis : has_countable_basis (𝓤 α) := has_countable_basis_of_seq _ _ uniformity_basis_edist_inv_nat.eq_infi /-- ε-δ characterization of uniform continuity on emetric spaces -/ theorem uniform_continuous_iff [emetric_space β] {f : α → β} : uniform_continuous f ↔ ∀ ε > 0, ∃ δ > 0, ∀{a b:α}, edist a b < δ → edist (f a) (f b) < ε := uniformity_basis_edist.uniform_continuous_iff uniformity_basis_edist /-- ε-δ characterization of uniform embeddings on emetric spaces -/ theorem uniform_embedding_iff [emetric_space β] {f : α → β} : uniform_embedding f ↔ function.injective f ∧ uniform_continuous f ∧ ∀ δ > 0, ∃ ε > 0, ∀ {a b : α}, edist (f a) (f b) < ε → edist a b < δ := uniform_embedding_def'.trans $ and_congr iff.rfl $ and_congr iff.rfl ⟨λ H δ δ0, let ⟨t, tu, ht⟩ := H _ (edist_mem_uniformity δ0), ⟨ε, ε0, hε⟩ := mem_uniformity_edist.1 tu in ⟨ε, ε0, λ a b h, ht _ _ (hε h)⟩, λ H s su, let ⟨δ, δ0, hδ⟩ := mem_uniformity_edist.1 su, ⟨ε, ε0, hε⟩ := H _ δ0 in ⟨_, edist_mem_uniformity ε0, λ a b h, hδ (hε h)⟩⟩ /-- A map between emetric spaces is a uniform embedding if and only if the edistance between `f x` and `f y` is controlled in terms of the distance between `x` and `y` and conversely. -/ theorem uniform_embedding_iff' [emetric_space β] {f : α → β} : uniform_embedding f ↔ (∀ ε > 0, ∃ δ > 0, ∀ {a b : α}, edist a b < δ → edist (f a) (f b) < ε) ∧ (∀ δ > 0, ∃ ε > 0, ∀ {a b : α}, edist (f a) (f b) < ε → edist a b < δ) := begin split, { assume h, exact ⟨uniform_continuous_iff.1 (uniform_embedding_iff.1 h).2.1, (uniform_embedding_iff.1 h).2.2⟩ }, { rintros ⟨h₁, h₂⟩, refine uniform_embedding_iff.2 ⟨_, uniform_continuous_iff.2 h₁, h₂⟩, assume x y hxy, have : edist x y ≤ 0, { refine le_of_forall_lt' (λδ δpos, _), rcases h₂ δ δpos with ⟨ε, εpos, hε⟩, have : edist (f x) (f y) < ε, by simpa [hxy], exact hε this }, simpa using this } end /-- ε-δ characterization of Cauchy sequences on emetric spaces -/ protected lemma cauchy_iff {f : filter α} : cauchy f ↔ f ≠ ⊥ ∧ ∀ ε > 0, ∃ t ∈ f, ∀ x y ∈ t, edist x y < ε := uniformity_basis_edist.cauchy_iff /-- A very useful criterion to show that a space is complete is to show that all sequences which satisfy a bound of the form `edist (u n) (u m) < B N` for all `n m ≥ N` are converging. This is often applied for `B N = 2^{-N}`, i.e., with a very fast convergence to `0`, which makes it possible to use arguments of converging series, while this is impossible to do in general for arbitrary Cauchy sequences. -/ theorem complete_of_convergent_controlled_sequences (B : ℕ → ennreal) (hB : ∀n, 0 < B n) (H : ∀u : ℕ → α, (∀N n m : ℕ, N ≤ n → N ≤ m → edist (u n) (u m) < B N) → ∃x, tendsto u at_top (𝓝 x)) : complete_space α := uniform_space.complete_of_convergent_controlled_sequences uniformity_has_countable_basis (λ n, {p:α×α | edist p.1 p.2 < B n}) (λ n, edist_mem_uniformity $ hB n) H /-- A sequentially complete emetric space is complete. -/ theorem complete_of_cauchy_seq_tendsto : (∀ u : ℕ → α, cauchy_seq u → ∃a, tendsto u at_top (𝓝 a)) → complete_space α := uniform_space.complete_of_cauchy_seq_tendsto uniformity_has_countable_basis end emetric open emetric /-- An emetric space is separated -/ @[priority 100] -- see Note [lower instance priority] instance to_separated : separated α := separated_def.2 $ λ x y h, eq_of_forall_edist_le $ λ ε ε0, le_of_lt (h _ (edist_mem_uniformity ε0)) /-- Auxiliary function to replace the uniformity on an emetric space with a uniformity which is equal to the original one, but maybe not defeq. This is useful if one wants to construct an emetric space with a specified uniformity. -/ def emetric_space.replace_uniformity {α} [U : uniform_space α] (m : emetric_space α) (H : @uniformity _ U = @uniformity _ (emetric_space.to_uniform_space α)) : emetric_space α := { edist := @edist _ m.to_has_edist, edist_self := edist_self, eq_of_edist_eq_zero := @eq_of_edist_eq_zero _ _, edist_comm := edist_comm, edist_triangle := edist_triangle, to_uniform_space := U, uniformity_edist := H.trans (@emetric_space.uniformity_edist α _) } /-- The extended metric induced by an injective function taking values in an emetric space. -/ def emetric_space.induced {α β} (f : α → β) (hf : function.injective f) (m : emetric_space β) : emetric_space α := { edist := λ x y, edist (f x) (f y), edist_self := λ x, edist_self _, eq_of_edist_eq_zero := λ x y h, hf (edist_eq_zero.1 h), edist_comm := λ x y, edist_comm _ _, edist_triangle := λ x y z, edist_triangle _ _ _, to_uniform_space := uniform_space.comap f m.to_uniform_space, uniformity_edist := begin apply @uniformity_dist_of_mem_uniformity _ _ _ _ _ (λ x y, edist (f x) (f y)), refine λ s, mem_comap_sets.trans _, split; intro H, { rcases H with ⟨r, ru, rs⟩, rcases mem_uniformity_edist.1 ru with ⟨ε, ε0, hε⟩, refine ⟨ε, ε0, λ a b h, rs (hε _)⟩, exact h }, { rcases H with ⟨ε, ε0, hε⟩, exact ⟨_, edist_mem_uniformity ε0, λ ⟨a, b⟩, hε⟩ } end } /-- Emetric space instance on subsets of emetric spaces -/ instance {α : Type*} {p : α → Prop} [t : emetric_space α] : emetric_space (subtype p) := t.induced subtype.val (λ x y, subtype.eq) /-- The extended distance on a subset of an emetric space is the restriction of the original distance, by definition -/ theorem subtype.edist_eq {p : α → Prop} (x y : subtype p) : edist x y = edist x.1 y.1 := rfl /-- The product of two emetric spaces, with the max distance, is an extended metric spaces. We make sure that the uniform structure thus constructed is the one corresponding to the product of uniform spaces, to avoid diamond problems. -/ instance prod.emetric_space_max [emetric_space β] : emetric_space (α × β) := { edist := λ x y, max (edist x.1 y.1) (edist x.2 y.2), edist_self := λ x, by simp, eq_of_edist_eq_zero := λ x y h, begin cases max_le_iff.1 (le_of_eq h) with h₁ h₂, have A : x.fst = y.fst := edist_le_zero.1 h₁, have B : x.snd = y.snd := edist_le_zero.1 h₂, exact prod.ext_iff.2 ⟨A, B⟩ end, edist_comm := λ x y, by simp [edist_comm], edist_triangle := λ x y z, max_le (le_trans (edist_triangle _ _ _) (add_le_add' (le_max_left _ _) (le_max_left _ _))) (le_trans (edist_triangle _ _ _) (add_le_add' (le_max_right _ _) (le_max_right _ _))), uniformity_edist := begin refine uniformity_prod.trans _, simp [emetric_space.uniformity_edist, comap_infi], rw ← infi_inf_eq, congr, funext, rw ← infi_inf_eq, congr, funext, simp [inf_principal, ext_iff, max_lt_iff] end, to_uniform_space := prod.uniform_space } lemma prod.edist_eq [emetric_space β] (x y : α × β) : edist x y = max (edist x.1 y.1) (edist x.2 y.2) := rfl section pi open finset variables {π : β → Type*} [fintype β] /-- The product of a finite number of emetric spaces, with the max distance, is still an emetric space. This construction would also work for infinite products, but it would not give rise to the product topology. Hence, we only formalize it in the good situation of finitely many spaces. -/ instance emetric_space_pi [∀b, emetric_space (π b)] : emetric_space (Πb, π b) := { edist := λ f g, finset.sup univ (λb, edist (f b) (g b)), edist_self := assume f, bot_unique $ finset.sup_le $ by simp, edist_comm := assume f g, by unfold edist; congr; funext a; exact edist_comm _ _, edist_triangle := assume f g h, begin simp only [finset.sup_le_iff], assume b hb, exact le_trans (edist_triangle _ (g b) _) (add_le_add' (le_sup hb) (le_sup hb)) end, eq_of_edist_eq_zero := assume f g eq0, begin have eq1 : sup univ (λ (b : β), edist (f b) (g b)) ≤ 0 := le_of_eq eq0, simp only [finset.sup_le_iff] at eq1, exact (funext $ assume b, edist_le_zero.1 $ eq1 b $ mem_univ b), end, to_uniform_space := Pi.uniform_space _, uniformity_edist := begin simp only [Pi.uniformity, emetric_space.uniformity_edist, comap_infi, gt_iff_lt, preimage_set_of_eq, comap_principal], rw infi_comm, congr, funext ε, rw infi_comm, congr, funext εpos, change 0 < ε at εpos, simp [ext_iff, εpos] end } end pi namespace emetric variables {x y z : α} {ε ε₁ ε₂ : ennreal} {s : set α} /-- `emetric.ball x ε` is the set of all points `y` with `edist y x < ε` -/ def ball (x : α) (ε : ennreal) : set α := {y | edist y x < ε} @[simp] theorem mem_ball : y ∈ ball x ε ↔ edist y x < ε := iff.rfl theorem mem_ball' : y ∈ ball x ε ↔ edist x y < ε := by rw edist_comm; refl /-- `emetric.closed_ball x ε` is the set of all points `y` with `edist y x ≤ ε` -/ def closed_ball (x : α) (ε : ennreal) := {y | edist y x ≤ ε} @[simp] theorem mem_closed_ball : y ∈ closed_ball x ε ↔ edist y x ≤ ε := iff.rfl theorem ball_subset_closed_ball : ball x ε ⊆ closed_ball x ε := assume y, by simp; intros h; apply le_of_lt h theorem pos_of_mem_ball (hy : y ∈ ball x ε) : 0 < ε := lt_of_le_of_lt (zero_le _) hy theorem mem_ball_self (h : 0 < ε) : x ∈ ball x ε := show edist x x < ε, by rw edist_self; assumption theorem mem_closed_ball_self : x ∈ closed_ball x ε := show edist x x ≤ ε, by rw edist_self; exact bot_le theorem mem_ball_comm : x ∈ ball y ε ↔ y ∈ ball x ε := by simp [edist_comm] theorem ball_subset_ball (h : ε₁ ≤ ε₂) : ball x ε₁ ⊆ ball x ε₂ := λ y (yx : _ < ε₁), lt_of_lt_of_le yx h theorem closed_ball_subset_closed_ball (h : ε₁ ≤ ε₂) : closed_ball x ε₁ ⊆ closed_ball x ε₂ := λ y (yx : _ ≤ ε₁), le_trans yx h theorem ball_disjoint (h : ε₁ + ε₂ ≤ edist x y) : ball x ε₁ ∩ ball y ε₂ = ∅ := eq_empty_iff_forall_not_mem.2 $ λ z ⟨h₁, h₂⟩, not_lt_of_le (edist_triangle_left x y z) (lt_of_lt_of_le (ennreal.add_lt_add h₁ h₂) h) theorem ball_subset (h : edist x y + ε₁ ≤ ε₂) (h' : edist x y < ⊤) : ball x ε₁ ⊆ ball y ε₂ := λ z zx, calc edist z y ≤ edist z x + edist x y : edist_triangle _ _ _ ... = edist x y + edist z x : add_comm _ _ ... < edist x y + ε₁ : (ennreal.add_lt_add_iff_left h').2 zx ... ≤ ε₂ : h theorem exists_ball_subset_ball (h : y ∈ ball x ε) : ∃ ε' > 0, ball y ε' ⊆ ball x ε := begin have : 0 < ε - edist y x := by simpa using h, refine ⟨ε - edist y x, this, ball_subset _ _⟩, { rw ennreal.add_sub_cancel_of_le (le_of_lt h), apply le_refl _}, { have : edist y x ≠ ⊤ := lattice.ne_top_of_lt h, apply lt_top_iff_ne_top.2 this } end theorem ball_eq_empty_iff : ball x ε = ∅ ↔ ε = 0 := eq_empty_iff_forall_not_mem.trans ⟨λh, le_bot_iff.1 (le_of_not_gt (λ ε0, h _ (mem_ball_self ε0))), λε0 y h, not_lt_of_le (le_of_eq ε0) (pos_of_mem_ball h)⟩ theorem nhds_basis_eball : (𝓝 x).has_basis (λ ε:ennreal, 0 < ε) (ball x) := nhds_basis_uniformity uniformity_basis_edist theorem nhds_eq : 𝓝 x = (⨅ε>0, principal (ball x ε)) := nhds_basis_eball.eq_binfi theorem mem_nhds_iff : s ∈ 𝓝 x ↔ ∃ε>0, ball x ε ⊆ s := nhds_basis_eball s theorem is_open_iff : is_open s ↔ ∀x∈s, ∃ε>0, ball x ε ⊆ s := by simp [is_open_iff_nhds, mem_nhds_iff] theorem is_open_ball : is_open (ball x ε) := is_open_iff.2 $ λ y, exists_ball_subset_ball theorem is_closed_ball_top : is_closed (ball x ⊤) := is_open_iff.2 $ λ y hy, ⟨⊤, ennreal.coe_lt_top, subset_compl_iff_disjoint.2 $ ball_disjoint $ by { rw ennreal.top_add, exact le_of_not_lt hy }⟩ theorem ball_mem_nhds (x : α) {ε : ennreal} (ε0 : 0 < ε) : ball x ε ∈ 𝓝 x := mem_nhds_sets is_open_ball (mem_ball_self ε0) /-- ε-characterization of the closure in emetric spaces -/ @[nolint ge_or_gt] -- see Note [nolint_ge] theorem mem_closure_iff : x ∈ closure s ↔ ∀ε>0, ∃y ∈ s, edist x y < ε := (mem_closure_iff_nhds_basis nhds_basis_eball).trans $ by simp only [mem_ball, edist_comm x] theorem tendsto_nhds {f : filter β} {u : β → α} {a : α} : tendsto u f (𝓝 a) ↔ ∀ ε > 0, ∀ᶠ x in f, edist (u x) a < ε := nhds_basis_eball.tendsto_right_iff theorem tendsto_at_top [nonempty β] [semilattice_sup β] (u : β → α) {a : α} : tendsto u at_top (𝓝 a) ↔ ∀ε>0, ∃N, ∀n≥N, edist (u n) a < ε := (at_top_basis.tendsto_iff nhds_basis_eball).trans $ by simp only [exists_prop, true_and, mem_Ici, mem_ball] /-- In an emetric space, Cauchy sequences are characterized by the fact that, eventually, the edistance between its elements is arbitrarily small -/ theorem cauchy_seq_iff [nonempty β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ ∀ε>0, ∃N, ∀m n≥N, edist (u m) (u n) < ε := uniformity_basis_edist.cauchy_seq_iff /-- A variation around the emetric characterization of Cauchy sequences -/ theorem cauchy_seq_iff' [nonempty β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ ∀ε>(0 : ennreal), ∃N, ∀n≥N, edist (u n) (u N) < ε := uniformity_basis_edist.cauchy_seq_iff' /-- A variation of the emetric characterization of Cauchy sequences that deals with `nnreal` upper bounds. -/ theorem cauchy_seq_iff_nnreal [nonempty β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ ∀ ε : nnreal, 0 < ε → ∃ N, ∀ n, N ≤ n → edist (u n) (u N) < ε := uniformity_basis_edist_nnreal.cauchy_seq_iff' theorem totally_bounded_iff {s : set α} : totally_bounded s ↔ ∀ ε > 0, ∃t : set α, finite t ∧ s ⊆ ⋃y∈t, ball y ε := ⟨λ H ε ε0, H _ (edist_mem_uniformity ε0), λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_edist.1 ru, ⟨t, ft, h⟩ := H ε ε0 in ⟨t, ft, subset.trans h $ Union_subset_Union $ λ y, Union_subset_Union $ λ yt z, hε⟩⟩ theorem totally_bounded_iff' {s : set α} : totally_bounded s ↔ ∀ ε > 0, ∃t⊆s, finite t ∧ s ⊆ ⋃y∈t, ball y ε := ⟨λ H ε ε0, (totally_bounded_iff_subset.1 H) _ (edist_mem_uniformity ε0), λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_edist.1 ru, ⟨t, _, ft, h⟩ := H ε ε0 in ⟨t, ft, subset.trans h $ Union_subset_Union $ λ y, Union_subset_Union $ λ yt z, hε⟩⟩ section compact /-- A compact set in an emetric space is separable, i.e., it is the closure of a countable set -/ lemma countable_closure_of_compact {α : Type u} [emetric_space α] {s : set α} (hs : compact s) : ∃ t ⊆ s, (countable t ∧ s = closure t) := begin have A : ∀ (e:ennreal), e > 0 → ∃ t ⊆ s, (finite t ∧ s ⊆ (⋃x∈t, ball x e)) := totally_bounded_iff'.1 (compact_iff_totally_bounded_complete.1 hs).1, -- assume e, finite_cover_balls_of_compact hs, have B : ∀ (e:ennreal), ∃ t ⊆ s, finite t ∧ (e > 0 → s ⊆ (⋃x∈t, ball x e)), { intro e, cases le_or_gt e 0 with h, { exact ⟨∅, by finish⟩ }, { rcases A e h with ⟨s, ⟨finite_s, closure_s⟩⟩, existsi s, finish }}, /-The desired countable set is obtained by taking for each `n` the centers of a finite cover by balls of radius `1/n`, and then the union over `n`. -/ choose T T_in_s finite_T using B, let t := ⋃n:ℕ, T n⁻¹, have T₁ : t ⊆ s := begin apply Union_subset, assume n, apply T_in_s end, have T₂ : countable t := by finish [countable_Union, countable_finite], have T₃ : s ⊆ closure t, { intros x x_in_s, apply mem_closure_iff.2, intros ε εpos, rcases ennreal.exists_inv_nat_lt (bot_lt_iff_ne_bot.1 εpos) with ⟨n, hn⟩, have inv_n_pos : (0 : ennreal) < (n : ℕ)⁻¹ := by simp [ennreal.bot_lt_iff_ne_bot], have C : x ∈ (⋃y∈ T (n : ℕ)⁻¹, ball y (n : ℕ)⁻¹) := mem_of_mem_of_subset x_in_s ((finite_T (n : ℕ)⁻¹).2 inv_n_pos), rcases mem_Union.1 C with ⟨y, _, ⟨y_in_T, rfl⟩, Dxy⟩, simp at Dxy, -- Dxy : edist x y < 1 / ↑n have : y ∈ t := mem_of_mem_of_subset y_in_T (by apply subset_Union (λ (n:ℕ), T (n : ℕ)⁻¹)), have : edist x y < ε := lt_trans Dxy hn, exact ⟨y, ‹y ∈ t›, ‹edist x y < ε›⟩ }, have T₄ : closure t ⊆ s := calc closure t ⊆ closure s : closure_mono T₁ ... = s : closure_eq_of_is_closed (closed_of_compact _ hs), exact ⟨t, ⟨T₁, T₂, subset.antisymm T₃ T₄⟩⟩ end end compact section first_countable @[priority 100] -- see Note [lower instance priority] instance (α : Type u) [emetric_space α] : topological_space.first_countable_topology α := uniform_space.first_countable_topology uniformity_has_countable_basis end first_countable section second_countable open topological_space /-- A separable emetric space is second countable: one obtains a countable basis by taking the balls centered at points in a dense subset, and with rational radii. We do not register this as an instance, as there is already an instance going in the other direction from second countable spaces to separable spaces, and we want to avoid loops. -/ lemma second_countable_of_separable (α : Type u) [emetric_space α] [separable_space α] : second_countable_topology α := let ⟨S, ⟨S_countable, S_dense⟩⟩ := separable_space.exists_countable_closure_eq_univ α in ⟨⟨⋃x ∈ S, ⋃ (n : nat), {ball x (n⁻¹)}, ⟨show countable ⋃x ∈ S, ⋃ (n : nat), {ball x (n⁻¹)}, { apply countable_bUnion S_countable, intros a aS, apply countable_Union, simp }, show uniform_space.to_topological_space α = generate_from (⋃x ∈ S, ⋃ (n : nat), {ball x (n⁻¹)}), { have A : ∀ (u : set α), (u ∈ ⋃x ∈ S, ⋃ (n : nat), ({ball x ((n : ennreal)⁻¹)} : set (set α))) → is_open u, { simp only [and_imp, exists_prop, set.mem_Union, set.mem_singleton_iff, exists_imp_distrib], intros u x hx i u_ball, rw [u_ball], exact is_open_ball }, have B : is_topological_basis (⋃x ∈ S, ⋃ (n : nat), ({ball x (n⁻¹)} : set (set α))), { refine is_topological_basis_of_open_of_nhds A (λa u au open_u, _), rcases is_open_iff.1 open_u a au with ⟨ε, εpos, εball⟩, have : ε / 2 > 0 := ennreal.half_pos εpos, /- The ball `ball a ε` is included in `u`. We need to find one of our balls `ball x (n⁻¹)` containing `a` and contained in `ball a ε`. For this, we take `n` larger than `2/ε`, and then `x` in `S` at distance at most `n⁻¹` of `a` -/ rcases ennreal.exists_inv_nat_lt (bot_lt_iff_ne_bot.1 (ennreal.half_pos εpos)) with ⟨n, εn⟩, have : (0 : ennreal) < n⁻¹ := by simp [ennreal.bot_lt_iff_ne_bot], have : (a : α) ∈ closure (S : set α) := by rw [S_dense]; simp, rcases mem_closure_iff.1 this _ ‹(0 : ennreal) < n⁻¹› with ⟨x, xS, xdist⟩, existsi ball x (↑n)⁻¹, have I : ball x (n⁻¹) ⊆ ball a ε := λy ydist, calc edist y a = edist a y : edist_comm _ _ ... ≤ edist a x + edist y x : edist_triangle_right _ _ _ ... < n⁻¹ + n⁻¹ : ennreal.add_lt_add xdist ydist ... < ε/2 + ε/2 : ennreal.add_lt_add εn εn ... = ε : ennreal.add_halves _, simp only [emetric.mem_ball, exists_prop, set.mem_Union, set.mem_singleton_iff], exact ⟨⟨x, ⟨xS, ⟨n, rfl⟩⟩⟩, ⟨by simpa, subset.trans I εball⟩⟩ }, exact B.2.2 }⟩⟩⟩ end second_countable section diam /-- The diameter of a set in an emetric space, named `emetric.diam` -/ def diam (s : set α) := ⨆ (x ∈ s) (y ∈ s), edist x y lemma diam_le_iff_forall_edist_le {d : ennreal} : diam s ≤ d ↔ ∀ (x ∈ s) (y ∈ s), edist x y ≤ d := by simp only [diam, supr_le_iff] /-- If two points belong to some set, their edistance is bounded by the diameter of the set -/ lemma edist_le_diam_of_mem (hx : x ∈ s) (hy : y ∈ s) : edist x y ≤ diam s := diam_le_iff_forall_edist_le.1 (le_refl _) x hx y hy /-- If the distance between any two points in a set is bounded by some constant, this constant bounds the diameter. -/ lemma diam_le_of_forall_edist_le {d : ennreal} (h : ∀ (x ∈ s) (y ∈ s), edist x y ≤ d) : diam s ≤ d := diam_le_iff_forall_edist_le.2 h /-- The diameter of a subsingleton vanishes. -/ lemma diam_subsingleton (hs : s.subsingleton) : diam s = 0 := le_zero_iff_eq.1 $ diam_le_of_forall_edist_le $ λ x hx y hy, (hs hx hy).symm ▸ edist_self y ▸ le_refl _ /-- The diameter of the empty set vanishes -/ @[simp] lemma diam_empty : diam (∅ : set α) = 0 := diam_subsingleton subsingleton_empty /-- The diameter of a singleton vanishes -/ @[simp] lemma diam_singleton : diam ({x} : set α) = 0 := diam_subsingleton subsingleton_singleton lemma diam_eq_zero_iff : diam s = 0 ↔ s.subsingleton := ⟨λ h x hx y hy, edist_le_zero.1 $ h ▸ edist_le_diam_of_mem hx hy, diam_subsingleton⟩ lemma diam_pos_iff : 0 < diam s ↔ ∃ (x ∈ s) (y ∈ s), x ≠ y := begin have := not_congr (@diam_eq_zero_iff _ _ s), dunfold set.subsingleton at this, push_neg at this, simpa only [zero_lt_iff_ne_zero, exists_prop] using this end lemma diam_insert : diam (insert x s) = max (diam s) (⨆ y ∈ s, edist y x) := eq_of_forall_ge_iff $ λ d, by simp only [diam_le_iff_forall_edist_le, ball_insert_iff, max_le_iff, edist_self, zero_le, true_and, supr_le_iff, forall_and_distrib, edist_comm x, and_self, (and_assoc _ _).symm, max_comm (diam s)] lemma diam_pair : diam ({x, y} : set α) = edist x y := by simp only [supr_singleton, diam_insert, diam_singleton, ennreal.max_zero_left] lemma diam_triple : diam ({x, y, z} : set α) = max (edist x y) (max (edist y z) (edist x z)) := by simp only [diam_insert, supr_insert, supr_singleton, diam_singleton, ennreal.max_zero_left, ennreal.sup_eq_max] /-- The diameter is monotonous with respect to inclusion -/ lemma diam_mono {s t : set α} (h : s ⊆ t) : diam s ≤ diam t := diam_le_of_forall_edist_le $ λ x hx y hy, edist_le_diam_of_mem (h hx) (h hy) /-- The diameter of a union is controlled by the diameter of the sets, and the edistance between two points in the sets. -/ lemma diam_union {t : set α} (xs : x ∈ s) (yt : y ∈ t) : diam (s ∪ t) ≤ diam s + edist x y + diam t := begin have A : ∀a ∈ s, ∀b ∈ t, edist a b ≤ diam s + edist x y + diam t := λa ha b hb, calc edist a b ≤ edist a x + edist x y + edist y b : edist_triangle4 _ _ _ _ ... ≤ diam s + edist x y + diam t : add_le_add' (add_le_add' (edist_le_diam_of_mem ha xs) (le_refl _)) (edist_le_diam_of_mem yt hb), refine diam_le_of_forall_edist_le (λa ha b hb, _), cases (mem_union _ _ _).1 ha with h'a h'a; cases (mem_union _ _ _).1 hb with h'b h'b, { calc edist a b ≤ diam s : edist_le_diam_of_mem h'a h'b ... ≤ diam s + (edist x y + diam t) : le_add_right (le_refl _) ... = diam s + edist x y + diam t : by simp only [add_comm, eq_self_iff_true, add_left_comm] }, { exact A a h'a b h'b }, { have Z := A b h'b a h'a, rwa [edist_comm] at Z }, { calc edist a b ≤ diam t : edist_le_diam_of_mem h'a h'b ... ≤ (diam s + edist x y) + diam t : le_add_left (le_refl _) } end lemma diam_union' {t : set α} (h : (s ∩ t).nonempty) : diam (s ∪ t) ≤ diam s + diam t := let ⟨x, ⟨xs, xt⟩⟩ := h in by simpa using diam_union xs xt lemma diam_closed_ball {r : ennreal} : diam (closed_ball x r) ≤ 2 * r := diam_le_of_forall_edist_le $ λa ha b hb, calc edist a b ≤ edist a x + edist b x : edist_triangle_right _ _ _ ... ≤ r + r : add_le_add' ha hb ... = 2 * r : by simp [mul_two, mul_comm] lemma diam_ball {r : ennreal} : diam (ball x r) ≤ 2 * r := le_trans (diam_mono ball_subset_closed_ball) diam_closed_ball end diam end emetric --namespace
2962b949260469d69c5e4a761f3eebaed4f23514
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/ring_theory/ideal/basic.lean
f7235863644b8f2303998f6e9ff91567fb4d6033
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
33,836
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Chris Hughes, Mario Carneiro -/ import algebra.associated import linear_algebra.basic import order.zorn import order.atoms import order.compactly_generated /-! # Ideals over a ring This file defines `ideal R`, the type of ideals over a commutative ring `R`. ## Implementation notes `ideal R` is implemented using `submodule R R`, where `•` is interpreted as `*`. ## TODO Support one-sided ideals, and ideals over non-commutative rings. See `algebra.ring_quot` for quotients of non-commutative rings. -/ universes u v w variables {α : Type u} {β : Type v} open set function open_locale classical big_operators /-- An ideal in a commutative semiring `R` is an additive submonoid `s` such that `a * b ∈ s` whenever `b ∈ s`. If `R` is a ring, then `s` is an additive subgroup. -/ @[reducible] def ideal (R : Type u) [comm_semiring R] := submodule R R section comm_semiring namespace ideal variables [comm_semiring α] (I : ideal α) {a b : α} protected lemma zero_mem : (0 : α) ∈ I := I.zero_mem protected lemma add_mem : a ∈ I → b ∈ I → a + b ∈ I := I.add_mem variables (a) lemma mul_mem_left : b ∈ I → a * b ∈ I := I.smul_mem a variables {a} variables (b) lemma mul_mem_right (h : a ∈ I) : a * b ∈ I := mul_comm b a ▸ I.mul_mem_left b h variables {b} end ideal variables {a b : α} -- A separate namespace definition is needed because the variables were historically in a different -- order. namespace ideal variables [comm_semiring α] (I : ideal α) @[ext] lemma ext {I J : ideal α} (h : ∀ x, x ∈ I ↔ x ∈ J) : I = J := submodule.ext h theorem eq_top_of_unit_mem (x y : α) (hx : x ∈ I) (h : y * x = 1) : I = ⊤ := eq_top_iff.2 $ λ z _, calc z = z * (y * x) : by simp [h] ... = (z * y) * x : eq.symm $ mul_assoc z y x ... ∈ I : I.mul_mem_left _ hx theorem eq_top_of_is_unit_mem {x} (hx : x ∈ I) (h : is_unit x) : I = ⊤ := let ⟨y, hy⟩ := is_unit_iff_exists_inv'.1 h in eq_top_of_unit_mem I x y hx hy theorem eq_top_iff_one : I = ⊤ ↔ (1:α) ∈ I := ⟨by rintro rfl; trivial, λ h, eq_top_of_unit_mem _ _ 1 h (by simp)⟩ theorem ne_top_iff_one : I ≠ ⊤ ↔ (1:α) ∉ I := not_congr I.eq_top_iff_one @[simp] theorem unit_mul_mem_iff_mem {x y : α} (hy : is_unit y) : y * x ∈ I ↔ x ∈ I := begin refine ⟨λ h, _, λ h, I.mul_mem_left y h⟩, obtain ⟨y', hy'⟩ := is_unit_iff_exists_inv.1 hy, have := I.mul_mem_left y' h, rwa [← mul_assoc, mul_comm y' y, hy', one_mul] at this, end @[simp] theorem mul_unit_mem_iff_mem {x y : α} (hy : is_unit y) : x * y ∈ I ↔ x ∈ I := mul_comm y x ▸ unit_mul_mem_iff_mem I hy /-- The ideal generated by a subset of a ring -/ def span (s : set α) : ideal α := submodule.span α s lemma subset_span {s : set α} : s ⊆ span s := submodule.subset_span lemma span_le {s : set α} {I} : span s ≤ I ↔ s ⊆ I := submodule.span_le lemma span_mono {s t : set α} : s ⊆ t → span s ≤ span t := submodule.span_mono @[simp] lemma span_eq : span (I : set α) = I := submodule.span_eq _ @[simp] lemma span_singleton_one : span (1 : set α) = ⊤ := (eq_top_iff_one _).2 $ subset_span $ mem_singleton _ lemma mem_span_insert {s : set α} {x y} : x ∈ span (insert y s) ↔ ∃ a (z ∈ span s), x = a * y + z := submodule.mem_span_insert lemma mem_span_singleton' {x y : α} : x ∈ span ({y} : set α) ↔ ∃ a, a * y = x := submodule.mem_span_singleton lemma mem_span_singleton {x y : α} : x ∈ span ({y} : set α) ↔ y ∣ x := mem_span_singleton'.trans $ exists_congr $ λ _, by rw [eq_comm, mul_comm] lemma span_singleton_le_span_singleton {x y : α} : span ({x} : set α) ≤ span ({y} : set α) ↔ y ∣ x := span_le.trans $ singleton_subset_iff.trans mem_span_singleton lemma span_singleton_eq_span_singleton {α : Type u} [integral_domain α] {x y : α} : span ({x} : set α) = span ({y} : set α) ↔ associated x y := begin rw [←dvd_dvd_iff_associated, le_antisymm_iff, and_comm], apply and_congr; rw span_singleton_le_span_singleton, end lemma span_eq_bot {s : set α} : span s = ⊥ ↔ ∀ x ∈ s, (x:α) = 0 := submodule.span_eq_bot @[simp] lemma span_singleton_eq_bot {x} : span ({x} : set α) = ⊥ ↔ x = 0 := submodule.span_singleton_eq_bot @[simp] lemma span_zero : span (0 : set α) = ⊥ := by rw [←set.singleton_zero, span_singleton_eq_bot] lemma span_singleton_eq_top {x} : span ({x} : set α) = ⊤ ↔ is_unit x := by rw [is_unit_iff_dvd_one, ← span_singleton_le_span_singleton, singleton_one, span_singleton_one, eq_top_iff] lemma span_singleton_mul_right_unit {a : α} (h2 : is_unit a) (x : α) : span ({x * a} : set α) = span {x} := begin apply le_antisymm, { rw span_singleton_le_span_singleton, use a}, { rw span_singleton_le_span_singleton, rw is_unit.mul_right_dvd h2} end lemma span_singleton_mul_left_unit {a : α} (h2 : is_unit a) (x : α) : span ({a * x} : set α) = span {x} := by rw [mul_comm, span_singleton_mul_right_unit h2] /-- The ideal generated by an arbitrary binary relation. -/ def of_rel (r : α → α → Prop) : ideal α := submodule.span α { x | ∃ (a b) (h : r a b), x + b = a } /-- An ideal `P` of a ring `R` is prime if `P ≠ R` and `xy ∈ P → x ∈ P ∨ y ∈ P` -/ class is_prime (I : ideal α) : Prop := (ne_top' : I ≠ ⊤) (mem_or_mem' : ∀ {x y : α}, x * y ∈ I → x ∈ I ∨ y ∈ I) theorem is_prime_iff {I : ideal α} : is_prime I ↔ I ≠ ⊤ ∧ ∀ {x y : α}, x * y ∈ I → x ∈ I ∨ y ∈ I := ⟨λ h, ⟨h.1, h.2⟩, λ h, ⟨h.1, h.2⟩⟩ theorem is_prime.ne_top {I : ideal α} (hI : I.is_prime) : I ≠ ⊤ := hI.1 theorem is_prime.mem_or_mem {I : ideal α} (hI : I.is_prime) : ∀ {x y : α}, x * y ∈ I → x ∈ I ∨ y ∈ I := hI.2 theorem is_prime.mem_or_mem_of_mul_eq_zero {I : ideal α} (hI : I.is_prime) {x y : α} (h : x * y = 0) : x ∈ I ∨ y ∈ I := hI.mem_or_mem (h.symm ▸ I.zero_mem) theorem is_prime.mem_of_pow_mem {I : ideal α} (hI : I.is_prime) {r : α} (n : ℕ) (H : r^n ∈ I) : r ∈ I := begin induction n with n ih, { exact (mt (eq_top_iff_one _).2 hI.1).elim H }, exact or.cases_on (hI.mem_or_mem H) id ih end lemma not_is_prime_iff {I : ideal α} : ¬ I.is_prime ↔ I = ⊤ ∨ ∃ (x ∉ I) (y ∉ I), x * y ∈ I := begin simp_rw [ideal.is_prime_iff, not_and_distrib, ne.def, not_not, not_forall, not_or_distrib], exact or_congr iff.rfl ⟨λ ⟨x, y, hxy, hx, hy⟩, ⟨x, hx, y, hy, hxy⟩, λ ⟨x, hx, y, hy, hxy⟩, ⟨x, y, hxy, hx, hy⟩⟩ end theorem zero_ne_one_of_proper {I : ideal α} (h : I ≠ ⊤) : (0:α) ≠ 1 := λ hz, I.ne_top_iff_one.1 h $ hz ▸ I.zero_mem theorem span_singleton_prime {p : α} (hp : p ≠ 0) : is_prime (span ({p} : set α)) ↔ prime p := by simp [is_prime_iff, prime, span_singleton_eq_top, hp, mem_span_singleton] lemma bot_prime {R : Type*} [integral_domain R] : (⊥ : ideal R).is_prime := ⟨λ h, one_ne_zero (by rwa [ideal.eq_top_iff_one, submodule.mem_bot] at h), λ x y h, mul_eq_zero.mp (by simpa only [submodule.mem_bot] using h)⟩ /-- An ideal is maximal if it is maximal in the collection of proper ideals. -/ class is_maximal (I : ideal α) : Prop := (out : is_coatom I) theorem is_maximal_def {I : ideal α} : I.is_maximal ↔ is_coatom I := ⟨λ h, h.1, λ h, ⟨h⟩⟩ theorem is_maximal.ne_top {I : ideal α} (h : I.is_maximal) : I ≠ ⊤ := (is_maximal_def.1 h).1 theorem is_maximal_iff {I : ideal α} : I.is_maximal ↔ (1:α) ∉ I ∧ ∀ (J : ideal α) x, I ≤ J → x ∉ I → x ∈ J → (1:α) ∈ J := is_maximal_def.trans $ and_congr I.ne_top_iff_one $ forall_congr $ λ J, by rw [lt_iff_le_not_le]; exact ⟨λ H x h hx₁ hx₂, J.eq_top_iff_one.1 $ H ⟨h, not_subset.2 ⟨_, hx₂, hx₁⟩⟩, λ H ⟨h₁, h₂⟩, let ⟨x, xJ, xI⟩ := not_subset.1 h₂ in J.eq_top_iff_one.2 $ H x h₁ xI xJ⟩ theorem is_maximal.eq_of_le {I J : ideal α} (hI : I.is_maximal) (hJ : J ≠ ⊤) (IJ : I ≤ J) : I = J := eq_iff_le_not_lt.2 ⟨IJ, λ h, hJ (hI.1.2 _ h)⟩ theorem is_maximal.is_prime {I : ideal α} (H : I.is_maximal) : I.is_prime := ⟨H.1.1, λ x y hxy, or_iff_not_imp_left.2 $ λ hx, begin let J : ideal α := submodule.span α (insert x ↑I), have IJ : I ≤ J := (set.subset.trans (subset_insert _ _) subset_span), have xJ : x ∈ J := ideal.subset_span (set.mem_insert x I), cases is_maximal_iff.1 H with _ oJ, specialize oJ J x IJ hx xJ, rcases submodule.mem_span_insert.mp oJ with ⟨a, b, h, oe⟩, obtain (F : y * 1 = y * (a • x + b)) := congr_arg (λ g : α, y * g) oe, rw [← mul_one y, F, mul_add, mul_comm, smul_eq_mul, mul_assoc], refine submodule.add_mem I (I.mul_mem_left a hxy) (submodule.smul_mem I y _), rwa submodule.span_eq at h, end⟩ @[priority 100] -- see Note [lower instance priority] instance is_maximal.is_prime' (I : ideal α) : ∀ [H : I.is_maximal], I.is_prime := is_maximal.is_prime instance : is_coatomic (ideal α) := begin apply complete_lattice.coatomic_of_top_compact, rw ←span_singleton_one, exact submodule.singleton_span_is_compact_element 1, end /-- Krull's theorem: if `I` is an ideal that is not the whole ring, then it is included in some maximal ideal. -/ theorem exists_le_maximal (I : ideal α) (hI : I ≠ ⊤) : ∃ M : ideal α, M.is_maximal ∧ I ≤ M := let ⟨m, hm⟩ := (eq_top_or_exists_le_coatom I).resolve_left hI in ⟨m, ⟨⟨hm.1⟩, hm.2⟩⟩ variables (α) /-- Krull's theorem: a nontrivial ring has a maximal ideal. -/ theorem exists_maximal [nontrivial α] : ∃ M : ideal α, M.is_maximal := let ⟨I, ⟨hI, _⟩⟩ := exists_le_maximal (⊥ : ideal α) bot_ne_top in ⟨I, hI⟩ variables {α} instance [nontrivial α] : nontrivial (ideal α) := begin rcases @exists_maximal α _ _ with ⟨M, hM, _⟩, exact nontrivial_of_ne M ⊤ hM end /-- If P is not properly contained in any maximal ideal then it is not properly contained in any proper ideal -/ lemma maximal_of_no_maximal {R : Type u} [comm_semiring R] {P : ideal R} (hmax : ∀ m : ideal R, P < m → ¬is_maximal m) (J : ideal R) (hPJ : P < J) : J = ⊤ := begin by_contradiction hnonmax, rcases exists_le_maximal J hnonmax with ⟨M, hM1, hM2⟩, exact hmax M (lt_of_lt_of_le hPJ hM2) hM1, end theorem mem_span_pair {x y z : α} : z ∈ span ({x, y} : set α) ↔ ∃ a b, a * x + b * y = z := by simp [mem_span_insert, mem_span_singleton', @eq_comm _ _ z] lemma span_singleton_lt_span_singleton [integral_domain β] {x y : β} : span ({x} : set β) < span ({y} : set β) ↔ dvd_not_unit y x := by rw [lt_iff_le_not_le, span_singleton_le_span_singleton, span_singleton_le_span_singleton, dvd_and_not_dvd_iff] lemma factors_decreasing [integral_domain β] (b₁ b₂ : β) (h₁ : b₁ ≠ 0) (h₂ : ¬ is_unit b₂) : span ({b₁ * b₂} : set β) < span {b₁} := lt_of_le_not_le (ideal.span_le.2 $ singleton_subset_iff.2 $ ideal.mem_span_singleton.2 ⟨b₂, rfl⟩) $ λ h, h₂ $ is_unit_of_dvd_one _ $ (mul_dvd_mul_iff_left h₁).1 $ by rwa [mul_one, ← ideal.span_singleton_le_span_singleton] theorem is_maximal.exists_inv {I : ideal α} (hI : I.is_maximal) {x} (hx : x ∉ I) : ∃ y, ∃ i ∈ I, y * x + i = 1 := begin cases is_maximal_iff.1 hI with H₁ H₂, rcases mem_span_insert.1 (H₂ (span (insert x I)) x (set.subset.trans (subset_insert _ _) subset_span) hx (subset_span (mem_insert _ _))) with ⟨y, z, hz, hy⟩, refine ⟨y, z, _, hy.symm⟩, rwa ← span_eq I, end end ideal end comm_semiring namespace ideal variables [comm_ring α] (I : ideal α) {a b : α} lemma neg_mem_iff : -a ∈ I ↔ a ∈ I := I.neg_mem_iff lemma add_mem_iff_left : b ∈ I → (a + b ∈ I ↔ a ∈ I) := I.add_mem_iff_left lemma add_mem_iff_right : a ∈ I → (a + b ∈ I ↔ b ∈ I) := I.add_mem_iff_right protected lemma sub_mem : a ∈ I → b ∈ I → a - b ∈ I := I.sub_mem lemma mem_span_insert' {s : set α} {x y} : x ∈ span (insert y s) ↔ ∃a, x + a * y ∈ span s := submodule.mem_span_insert' /-- The quotient `R/I` of a ring `R` by an ideal `I`. -/ def quotient (I : ideal α) := I.quotient namespace quotient variables {I} {x y : α} instance (I : ideal α) : has_one I.quotient := ⟨submodule.quotient.mk 1⟩ instance (I : ideal α) : has_mul I.quotient := ⟨λ a b, quotient.lift_on₂' a b (λ a b, submodule.quotient.mk (a * b)) $ λ a₁ a₂ b₁ b₂ h₁ h₂, quot.sound $ begin have F := I.add_mem (I.mul_mem_left a₂ h₁) (I.mul_mem_right b₁ h₂), have : a₁ * a₂ - b₁ * b₂ = a₂ * (a₁ - b₁) + (a₂ - b₂) * b₁, { rw [mul_sub, sub_mul, sub_add_sub_cancel, mul_comm, mul_comm b₁] }, rw ← this at F, change _ ∈ _, convert F, end⟩ instance (I : ideal α) : comm_ring I.quotient := { mul := (*), one := 1, mul_assoc := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg submodule.quotient.mk (mul_assoc a b c), mul_comm := λ a b, quotient.induction_on₂' a b $ λ a b, congr_arg submodule.quotient.mk (mul_comm a b), one_mul := λ a, quotient.induction_on' a $ λ a, congr_arg submodule.quotient.mk (one_mul a), mul_one := λ a, quotient.induction_on' a $ λ a, congr_arg submodule.quotient.mk (mul_one a), left_distrib := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg submodule.quotient.mk (left_distrib a b c), right_distrib := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg submodule.quotient.mk (right_distrib a b c), ..submodule.quotient.add_comm_group I } /-- The ring homomorphism from a ring `R` to a quotient ring `R/I`. -/ def mk (I : ideal α) : α →+* I.quotient := ⟨λ a, submodule.quotient.mk a, rfl, λ _ _, rfl, rfl, λ _ _, rfl⟩ instance : inhabited (quotient I) := ⟨mk I 37⟩ protected theorem eq : mk I x = mk I y ↔ x - y ∈ I := submodule.quotient.eq I @[simp] theorem mk_eq_mk (x : α) : (submodule.quotient.mk x : quotient I) = mk I x := rfl lemma eq_zero_iff_mem {I : ideal α} : mk I a = 0 ↔ a ∈ I := by conv {to_rhs, rw ← sub_zero a }; exact quotient.eq' theorem zero_eq_one_iff {I : ideal α} : (0 : I.quotient) = 1 ↔ I = ⊤ := eq_comm.trans $ eq_zero_iff_mem.trans (eq_top_iff_one _).symm theorem zero_ne_one_iff {I : ideal α} : (0 : I.quotient) ≠ 1 ↔ I ≠ ⊤ := not_congr zero_eq_one_iff protected theorem nontrivial {I : ideal α} (hI : I ≠ ⊤) : nontrivial I.quotient := ⟨⟨0, 1, zero_ne_one_iff.2 hI⟩⟩ lemma mk_surjective : function.surjective (mk I) := λ y, quotient.induction_on' y (λ x, exists.intro x rfl) instance (I : ideal α) [hI : I.is_prime] : integral_domain I.quotient := { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b, quotient.induction_on₂' a b $ λ a b hab, (hI.mem_or_mem (eq_zero_iff_mem.1 hab)).elim (or.inl ∘ eq_zero_iff_mem.2) (or.inr ∘ eq_zero_iff_mem.2), .. quotient.comm_ring I, .. quotient.nontrivial hI.1 } lemma is_integral_domain_iff_prime (I : ideal α) : is_integral_domain I.quotient ↔ I.is_prime := ⟨ λ ⟨h1, h2, h3⟩, ⟨zero_ne_one_iff.1 $ @zero_ne_one _ _ ⟨h1⟩, λ x y h, by { simp only [←eq_zero_iff_mem, (mk I).map_mul] at ⊢ h, exact h3 _ _ h}⟩, λ h, by exactI integral_domain.to_is_integral_domain I.quotient⟩ lemma exists_inv {I : ideal α} [hI : I.is_maximal] : ∀ {a : I.quotient}, a ≠ 0 → ∃ b : I.quotient, a * b = 1 := begin rintro ⟨a⟩ h, rcases hI.exists_inv (mt eq_zero_iff_mem.2 h) with ⟨b, c, hc, abc⟩, rw [mul_comm] at abc, refine ⟨mk _ b, quot.sound _⟩, --quot.sound hb rw ← eq_sub_iff_add_eq' at abc, rw [abc, ← neg_mem_iff, neg_sub] at hc, convert hc, end /-- quotient by maximal ideal is a field. def rather than instance, since users will have computable inverses in some applications -/ protected noncomputable def field (I : ideal α) [hI : I.is_maximal] : field I.quotient := { inv := λ a, if ha : a = 0 then 0 else classical.some (exists_inv ha), mul_inv_cancel := λ a (ha : a ≠ 0), show a * dite _ _ _ = _, by rw dif_neg ha; exact classical.some_spec (exists_inv ha), inv_zero := dif_pos rfl, ..quotient.integral_domain I } /-- If the quotient by an ideal is a field, then the ideal is maximal. -/ theorem maximal_of_is_field (I : ideal α) (hqf : is_field I.quotient) : I.is_maximal := begin apply ideal.is_maximal_iff.2, split, { intro h, rcases hqf.exists_pair_ne with ⟨⟨x⟩, ⟨y⟩, hxy⟩, exact hxy (ideal.quotient.eq.2 (mul_one (x - y) ▸ I.mul_mem_left _ h)) }, { intros J x hIJ hxnI hxJ, rcases hqf.mul_inv_cancel (mt ideal.quotient.eq_zero_iff_mem.1 hxnI) with ⟨⟨y⟩, hy⟩, rw [← zero_add (1 : α), ← sub_self (x * y), sub_add], refine J.sub_mem (J.mul_mem_right _ hxJ) (hIJ (ideal.quotient.eq.1 hy)) } end /-- The quotient of a ring by an ideal is a field iff the ideal is maximal. -/ theorem maximal_ideal_iff_is_field_quotient (I : ideal α) : I.is_maximal ↔ is_field I.quotient := ⟨λ h, @field.to_is_field I.quotient (@ideal.quotient.field _ _ I h), λ h, maximal_of_is_field I h⟩ variable [comm_ring β] /-- Given a ring homomorphism `f : α →+* β` sending all elements of an ideal to zero, lift it to the quotient by this ideal. -/ def lift (S : ideal α) (f : α →+* β) (H : ∀ (a : α), a ∈ S → f a = 0) : quotient S →+* β := { to_fun := λ x, quotient.lift_on' x f $ λ (a b) (h : _ ∈ _), eq_of_sub_eq_zero $ by rw [← f.map_sub, H _ h], map_one' := f.map_one, map_zero' := f.map_zero, map_add' := λ a₁ a₂, quotient.induction_on₂' a₁ a₂ f.map_add, map_mul' := λ a₁ a₂, quotient.induction_on₂' a₁ a₂ f.map_mul } @[simp] lemma lift_mk (S : ideal α) (f : α →+* β) (H : ∀ (a : α), a ∈ S → f a = 0) : lift S f H (mk S a) = f a := rfl end quotient section lattice variables {R : Type u} [comm_semiring R] lemma mem_sup_left {S T : ideal R} : ∀ {x : R}, x ∈ S → x ∈ S ⊔ T := show S ≤ S ⊔ T, from le_sup_left lemma mem_sup_right {S T : ideal R} : ∀ {x : R}, x ∈ T → x ∈ S ⊔ T := show T ≤ S ⊔ T, from le_sup_right lemma mem_supr_of_mem {ι : Type*} {S : ι → ideal R} (i : ι) : ∀ {x : R}, x ∈ S i → x ∈ supr S := show S i ≤ supr S, from le_supr _ _ lemma mem_Sup_of_mem {S : set (ideal R)} {s : ideal R} (hs : s ∈ S) : ∀ {x : R}, x ∈ s → x ∈ Sup S := show s ≤ Sup S, from le_Sup hs theorem mem_Inf {s : set (ideal R)} {x : R} : x ∈ Inf s ↔ ∀ ⦃I⦄, I ∈ s → x ∈ I := ⟨λ hx I his, hx I ⟨I, infi_pos his⟩, λ H I ⟨J, hij⟩, hij ▸ λ S ⟨hj, hS⟩, hS ▸ H hj⟩ @[simp] lemma mem_inf {I J : ideal R} {x : R} : x ∈ I ⊓ J ↔ x ∈ I ∧ x ∈ J := iff.rfl @[simp] lemma mem_infi {ι : Type*} {I : ι → ideal R} {x : R} : x ∈ infi I ↔ ∀ i, x ∈ I i := submodule.mem_infi _ @[simp] lemma mem_bot {x : R} : x ∈ (⊥ : ideal R) ↔ x = 0 := submodule.mem_bot _ end lattice /-- All ideals in a field are trivial. -/ lemma eq_bot_or_top {K : Type u} [field K] (I : ideal K) : I = ⊥ ∨ I = ⊤ := begin rw or_iff_not_imp_right, change _ ≠ _ → _, rw ideal.ne_top_iff_one, intro h1, rw eq_bot_iff, intros r hr, by_cases H : r = 0, {simpa}, simpa [H, h1] using I.mul_mem_left r⁻¹ hr, end lemma eq_bot_of_prime {K : Type u} [field K] (I : ideal K) [h : I.is_prime] : I = ⊥ := or_iff_not_imp_right.mp I.eq_bot_or_top h.1 lemma bot_is_maximal {K : Type u} [field K] : is_maximal (⊥ : ideal K) := ⟨⟨λ h, absurd ((eq_top_iff_one (⊤ : ideal K)).mp rfl) (by rw ← h; simp), λ I hI, or_iff_not_imp_left.mp (eq_bot_or_top I) (ne_of_gt hI)⟩⟩ section pi variables (ι : Type v) /-- `I^n` as an ideal of `R^n`. -/ def pi : ideal (ι → α) := { carrier := { x | ∀ i, x i ∈ I }, zero_mem' := λ i, I.zero_mem, add_mem' := λ a b ha hb i, I.add_mem (ha i) (hb i), smul_mem' := λ a b hb i, I.mul_mem_left (a i) (hb i) } lemma mem_pi (x : ι → α) : x ∈ I.pi ι ↔ ∀ i, x i ∈ I := iff.rfl /-- `R^n/I^n` is a `R/I`-module. -/ instance module_pi : module (I.quotient) (I.pi ι).quotient := begin refine { smul := λ c m, quotient.lift_on₂' c m (λ r m, submodule.quotient.mk $ r • m) _, .. }, { intros c₁ m₁ c₂ m₂ hc hm, change c₁ - c₂ ∈ I at hc, change m₁ - m₂ ∈ (I.pi ι) at hm, apply ideal.quotient.eq.2, have : c₁ • (m₂ - m₁) ∈ I.pi ι, { rw ideal.mem_pi, intro i, simp only [smul_eq_mul, pi.smul_apply, pi.sub_apply], apply ideal.mul_mem_left, rw ←ideal.neg_mem_iff, simpa only [neg_sub] using hm i }, rw [←ideal.add_mem_iff_left (I.pi ι) this, sub_eq_add_neg, add_comm, ←add_assoc, ←smul_add, sub_add_cancel, ←sub_eq_add_neg, ←sub_smul, ideal.mem_pi], exact λ i, I.mul_mem_right _ hc }, all_goals { rintro ⟨a⟩ ⟨b⟩ ⟨c⟩ <|> rintro ⟨a⟩, simp only [(•), submodule.quotient.quot_mk_eq_mk, ideal.quotient.mk_eq_mk], change ideal.quotient.mk _ _ = ideal.quotient.mk _ _, congr' with i, simp [mul_assoc, mul_add, add_mul] } end /-- `R^n/I^n` is isomorphic to `(R/I)^n` as an `R/I`-module. -/ noncomputable def pi_quot_equiv : (I.pi ι).quotient ≃ₗ[I.quotient] (ι → I.quotient) := { to_fun := λ x, quotient.lift_on' x (λ f i, ideal.quotient.mk I (f i)) $ λ a b hab, funext (λ i, ideal.quotient.eq.2 (hab i)), map_add' := by { rintros ⟨_⟩ ⟨_⟩, refl }, map_smul' := by { rintros ⟨_⟩ ⟨_⟩, refl }, inv_fun := λ x, ideal.quotient.mk (I.pi ι) $ λ i, quotient.out' (x i), left_inv := begin rintro ⟨x⟩, exact ideal.quotient.eq.2 (λ i, ideal.quotient.eq.1 (quotient.out_eq' _)) end, right_inv := begin intro x, ext i, obtain ⟨r, hr⟩ := @quot.exists_rep _ _ (x i), simp_rw ←hr, convert quotient.out_eq' _ end } /-- If `f : R^n → R^m` is an `R`-linear map and `I ⊆ R` is an ideal, then the image of `I^n` is contained in `I^m`. -/ lemma map_pi {ι} [fintype ι] {ι' : Type w} (x : ι → α) (hi : ∀ i, x i ∈ I) (f : (ι → α) →ₗ[α] (ι' → α)) (i : ι') : f x i ∈ I := begin rw pi_eq_sum_univ x, simp only [finset.sum_apply, smul_eq_mul, linear_map.map_sum, pi.smul_apply, linear_map.map_smul], exact I.sum_mem (λ j hj, I.mul_mem_right _ (hi j)) end end pi end ideal namespace ring variables {R : Type*} [comm_ring R] lemma not_is_field_of_subsingleton {R : Type*} [ring R] [subsingleton R] : ¬ is_field R := λ ⟨⟨x, y, hxy⟩, _, _⟩, hxy (subsingleton.elim x y) lemma exists_not_is_unit_of_not_is_field [nontrivial R] (hf : ¬ is_field R) : ∃ x ≠ (0 : R), ¬ is_unit x := begin have : ¬ _ := λ h, hf ⟨exists_pair_ne R, mul_comm, h⟩, simp_rw is_unit_iff_exists_inv, push_neg at ⊢ this, obtain ⟨x, hx, not_unit⟩ := this, exact ⟨x, hx, not_unit⟩ end lemma not_is_field_iff_exists_ideal_bot_lt_and_lt_top [nontrivial R] : ¬ is_field R ↔ ∃ I : ideal R, ⊥ < I ∧ I < ⊤ := begin split, { intro h, obtain ⟨x, nz, nu⟩ := exists_not_is_unit_of_not_is_field h, use ideal.span {x}, rw [bot_lt_iff_ne_bot, lt_top_iff_ne_top], exact ⟨mt ideal.span_singleton_eq_bot.mp nz, mt ideal.span_singleton_eq_top.mp nu⟩ }, { rintros ⟨I, bot_lt, lt_top⟩ hf, obtain ⟨x, mem, ne_zero⟩ := set_like.exists_of_lt bot_lt, rw submodule.mem_bot at ne_zero, obtain ⟨y, hy⟩ := hf.mul_inv_cancel ne_zero, rw [lt_top_iff_ne_top, ne.def, ideal.eq_top_iff_one, ← hy] at lt_top, exact lt_top (I.mul_mem_right _ mem), } end lemma not_is_field_iff_exists_prime [nontrivial R] : ¬ is_field R ↔ ∃ p : ideal R, p ≠ ⊥ ∧ p.is_prime := not_is_field_iff_exists_ideal_bot_lt_and_lt_top.trans ⟨λ ⟨I, bot_lt, lt_top⟩, let ⟨p, hp, le_p⟩ := I.exists_le_maximal (lt_top_iff_ne_top.mp lt_top) in ⟨p, bot_lt_iff_ne_bot.mp (lt_of_lt_of_le bot_lt le_p), hp.is_prime⟩, λ ⟨p, ne_bot, prime⟩, ⟨p, bot_lt_iff_ne_bot.mpr ne_bot, lt_top_iff_ne_top.mpr prime.1⟩⟩ /-- When a ring is not a field, the maximal ideals are nontrivial. -/ lemma ne_bot_of_is_maximal_of_not_is_field [nontrivial R] {M : ideal R} (max : M.is_maximal) (not_field : ¬ is_field R) : M ≠ ⊥ := begin rintros h, rw h at max, rcases max with ⟨⟨h1, h2⟩⟩, obtain ⟨I, hIbot, hItop⟩ := not_is_field_iff_exists_ideal_bot_lt_and_lt_top.mp not_field, exact ne_of_lt hItop (h2 I hIbot), end end ring namespace ideal /-- Maximal ideals in a non-field are nontrivial. -/ variables {R : Type u} [comm_ring R] [nontrivial R] lemma bot_lt_of_maximal (M : ideal R) [hm : M.is_maximal] (non_field : ¬ is_field R) : ⊥ < M := begin rcases (ring.not_is_field_iff_exists_ideal_bot_lt_and_lt_top.1 non_field) with ⟨I, Ibot, Itop⟩, split, finish, intro mle, apply @irrefl _ (<) _ (⊤ : ideal R), have : M = ⊥ := eq_bot_iff.mpr mle, rw this at *, rwa hm.1.2 I Ibot at Itop, end end ideal variables {a b : α} /-- The set of non-invertible elements of a monoid. -/ def nonunits (α : Type u) [monoid α] : set α := { a | ¬is_unit a } @[simp] theorem mem_nonunits_iff [comm_monoid α] : a ∈ nonunits α ↔ ¬ is_unit a := iff.rfl theorem mul_mem_nonunits_right [comm_monoid α] : b ∈ nonunits α → a * b ∈ nonunits α := mt is_unit_of_mul_is_unit_right theorem mul_mem_nonunits_left [comm_monoid α] : a ∈ nonunits α → a * b ∈ nonunits α := mt is_unit_of_mul_is_unit_left theorem zero_mem_nonunits [semiring α] : 0 ∈ nonunits α ↔ (0:α) ≠ 1 := not_congr is_unit_zero_iff @[simp] theorem one_not_mem_nonunits [monoid α] : (1:α) ∉ nonunits α := not_not_intro is_unit_one theorem coe_subset_nonunits [comm_semiring α] {I : ideal α} (h : I ≠ ⊤) : (I : set α) ⊆ nonunits α := λ x hx hu, h $ I.eq_top_of_is_unit_mem hx hu lemma exists_max_ideal_of_mem_nonunits [comm_semiring α] (h : a ∈ nonunits α) : ∃ I : ideal α, I.is_maximal ∧ a ∈ I := begin have : ideal.span ({a} : set α) ≠ ⊤, { intro H, rw ideal.span_singleton_eq_top at H, contradiction }, rcases ideal.exists_le_maximal _ this with ⟨I, Imax, H⟩, use [I, Imax], apply H, apply ideal.subset_span, exact set.mem_singleton a end /-- A commutative ring is local if it has a unique maximal ideal. Note that `local_ring` is a predicate. -/ class local_ring (α : Type u) [comm_ring α] extends nontrivial α : Prop := (is_local : ∀ (a : α), (is_unit a) ∨ (is_unit (1 - a))) namespace local_ring variables [comm_ring α] [local_ring α] lemma is_unit_or_is_unit_one_sub_self (a : α) : (is_unit a) ∨ (is_unit (1 - a)) := is_local a lemma is_unit_of_mem_nonunits_one_sub_self (a : α) (h : (1 - a) ∈ nonunits α) : is_unit a := or_iff_not_imp_right.1 (is_local a) h lemma is_unit_one_sub_self_of_mem_nonunits (a : α) (h : a ∈ nonunits α) : is_unit (1 - a) := or_iff_not_imp_left.1 (is_local a) h lemma nonunits_add {x y} (hx : x ∈ nonunits α) (hy : y ∈ nonunits α) : x + y ∈ nonunits α := begin rintros ⟨u, hu⟩, apply hy, suffices : is_unit ((↑u⁻¹ : α) * y), { rcases this with ⟨s, hs⟩, use u * s, convert congr_arg (λ z, (u : α) * z) hs, rw ← mul_assoc, simp }, rw show (↑u⁻¹ * y) = (1 - ↑u⁻¹ * x), { rw eq_sub_iff_add_eq, replace hu := congr_arg (λ z, (↑u⁻¹ : α) * z) hu.symm, simpa [mul_add, add_comm] using hu }, apply is_unit_one_sub_self_of_mem_nonunits, exact mul_mem_nonunits_right hx end variable (α) /-- The ideal of elements that are not units. -/ def maximal_ideal : ideal α := { carrier := nonunits α, zero_mem' := zero_mem_nonunits.2 $ zero_ne_one, add_mem' := λ x y hx hy, nonunits_add hx hy, smul_mem' := λ a x, mul_mem_nonunits_right } instance maximal_ideal.is_maximal : (maximal_ideal α).is_maximal := begin rw ideal.is_maximal_iff, split, { intro h, apply h, exact is_unit_one }, { intros I x hI hx H, erw not_not at hx, rcases hx with ⟨u,rfl⟩, simpa using I.mul_mem_left ↑u⁻¹ H } end lemma maximal_ideal_unique : ∃! I : ideal α, I.is_maximal := ⟨maximal_ideal α, maximal_ideal.is_maximal α, λ I hI, hI.eq_of_le (maximal_ideal.is_maximal α).1.1 $ λ x hx, hI.1.1 ∘ I.eq_top_of_is_unit_mem hx⟩ variable {α} lemma eq_maximal_ideal {I : ideal α} (hI : I.is_maximal) : I = maximal_ideal α := unique_of_exists_unique (maximal_ideal_unique α) hI $ maximal_ideal.is_maximal α lemma le_maximal_ideal {J : ideal α} (hJ : J ≠ ⊤) : J ≤ maximal_ideal α := begin rcases ideal.exists_le_maximal J hJ with ⟨M, hM1, hM2⟩, rwa ←eq_maximal_ideal hM1 end @[simp] lemma mem_maximal_ideal (x) : x ∈ maximal_ideal α ↔ x ∈ nonunits α := iff.rfl end local_ring lemma local_of_nonunits_ideal [comm_ring α] (hnze : (0:α) ≠ 1) (h : ∀ x y ∈ nonunits α, x + y ∈ nonunits α) : local_ring α := { exists_pair_ne := ⟨0, 1, hnze⟩, is_local := λ x, or_iff_not_imp_left.mpr $ λ hx, begin by_contra H, apply h _ _ hx H, simp [-sub_eq_add_neg, add_sub_cancel'_right] end } lemma local_of_unique_max_ideal [comm_ring α] (h : ∃! I : ideal α, I.is_maximal) : local_ring α := local_of_nonunits_ideal (let ⟨I, Imax, _⟩ := h in (λ (H : 0 = 1), Imax.1.1 $ I.eq_top_iff_one.2 $ H ▸ I.zero_mem)) $ λ x y hx hy H, let ⟨I, Imax, Iuniq⟩ := h in let ⟨Ix, Ixmax, Hx⟩ := exists_max_ideal_of_mem_nonunits hx in let ⟨Iy, Iymax, Hy⟩ := exists_max_ideal_of_mem_nonunits hy in have xmemI : x ∈ I, from ((Iuniq Ix Ixmax) ▸ Hx), have ymemI : y ∈ I, from ((Iuniq Iy Iymax) ▸ Hy), Imax.1.1 $ I.eq_top_of_is_unit_mem (I.add_mem xmemI ymemI) H lemma local_of_unique_nonzero_prime (R : Type u) [comm_ring R] (h : ∃! P : ideal R, P ≠ ⊥ ∧ ideal.is_prime P) : local_ring R := local_of_unique_max_ideal begin rcases h with ⟨P, ⟨hPnonzero, hPnot_top, _⟩, hPunique⟩, refine ⟨P, ⟨⟨hPnot_top, _⟩⟩, λ M hM, hPunique _ ⟨_, ideal.is_maximal.is_prime hM⟩⟩, { refine ideal.maximal_of_no_maximal (λ M hPM hM, ne_of_lt hPM _), exact (hPunique _ ⟨ne_bot_of_gt hPM, ideal.is_maximal.is_prime hM⟩).symm }, { rintro rfl, exact hPnot_top (hM.1.2 P (bot_lt_iff_ne_bot.2 hPnonzero)) }, end lemma local_of_surjective {A B : Type*} [comm_ring A] [local_ring A] [comm_ring B] [nontrivial B] (f : A →+* B) (hf : function.surjective f) : local_ring B := { is_local := begin intros b, obtain ⟨a, rfl⟩ := hf b, apply (local_ring.is_unit_or_is_unit_one_sub_self a).imp f.is_unit_map _, rw [← f.map_one, ← f.map_sub], apply f.is_unit_map, end, .. ‹nontrivial B› } /-- A local ring homomorphism is a homomorphism between local rings such that the image of the maximal ideal of the source is contained within the maximal ideal of the target. -/ class is_local_ring_hom [semiring α] [semiring β] (f : α →+* β) : Prop := (map_nonunit : ∀ a, is_unit (f a) → is_unit a) instance is_local_ring_hom_id (A : Type*) [semiring A] : is_local_ring_hom (ring_hom.id A) := { map_nonunit := λ a, id } @[simp] lemma is_unit_map_iff {A B : Type*} [semiring A] [semiring B] (f : A →+* B) [is_local_ring_hom f] (a) : is_unit (f a) ↔ is_unit a := ⟨is_local_ring_hom.map_nonunit a, f.is_unit_map⟩ instance is_local_ring_hom_comp {A B C : Type*} [semiring A] [semiring B] [semiring C] (g : B →+* C) (f : A →+* B) [is_local_ring_hom g] [is_local_ring_hom f] : is_local_ring_hom (g.comp f) := { map_nonunit := λ a, is_local_ring_hom.map_nonunit a ∘ is_local_ring_hom.map_nonunit (f a) } @[simp] lemma is_unit_of_map_unit [semiring α] [semiring β] (f : α →+* β) [is_local_ring_hom f] (a) (h : is_unit (f a)) : is_unit a := is_local_ring_hom.map_nonunit a h theorem of_irreducible_map [semiring α] [semiring β] (f : α →+* β) [h : is_local_ring_hom f] {x : α} (hfx : irreducible (f x)) : irreducible x := ⟨λ h, hfx.not_unit $ is_unit.map f.to_monoid_hom h, λ p q hx, let ⟨H⟩ := h in or.imp (H p) (H q) $ hfx.is_unit_or_is_unit $ f.map_mul p q ▸ congr_arg f hx⟩ section open local_ring variables [comm_ring α] [local_ring α] [comm_ring β] [local_ring β] variables (f : α →+* β) [is_local_ring_hom f] lemma map_nonunit (a) (h : a ∈ maximal_ideal α) : f a ∈ maximal_ideal β := λ H, h $ is_unit_of_map_unit f a H end namespace local_ring variables [comm_ring α] [local_ring α] [comm_ring β] [local_ring β] variable (α) /-- The residue field of a local ring is the quotient of the ring by its maximal ideal. -/ def residue_field := (maximal_ideal α).quotient noncomputable instance residue_field.field : field (residue_field α) := ideal.quotient.field (maximal_ideal α) noncomputable instance : inhabited (residue_field α) := ⟨37⟩ /-- The quotient map from a local ring to its residue field. -/ def residue : α →+* (residue_field α) := ideal.quotient.mk _ namespace residue_field variables {α β} /-- The map on residue fields induced by a local homomorphism between local rings -/ noncomputable def map (f : α →+* β) [is_local_ring_hom f] : residue_field α →+* residue_field β := ideal.quotient.lift (maximal_ideal α) ((ideal.quotient.mk _).comp f) $ λ a ha, begin erw ideal.quotient.eq_zero_iff_mem, exact map_nonunit f a ha end end residue_field end local_ring namespace field variables [field α] @[priority 100] -- see Note [lower instance priority] instance : local_ring α := { is_local := λ a, if h : a = 0 then or.inr (by rw [h, sub_zero]; exact is_unit_one) else or.inl $ is_unit.mk0 a h } end field
8a1254cabb43d15a603515f5141e33363c039155
5d166a16ae129621cb54ca9dde86c275d7d2b483
/library/init/meta/decl_cmds.lean
92717a9e1ca93ff6d4c7e4838117d1e5720acc28
[ "Apache-2.0" ]
permissive
jcarlson23/lean
b00098763291397e0ac76b37a2dd96bc013bd247
8de88701247f54d325edd46c0eed57aeacb64baf
refs/heads/master
1,611,571,813,719
1,497,020,963,000
1,497,021,515,000
93,882,536
1
0
null
1,497,029,896,000
1,497,029,896,000
null
UTF-8
Lean
false
false
2,024
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.tactic init.meta.rb_map open tactic private meta def apply_replacement (replacements : name_map name) (e : expr) : expr := e.replace (λ e d, match e with | expr.const n ls := match replacements.find n with | some new_n := some (expr.const new_n ls) | none := none end | _ := none end) /- Given a set of constant renamings `replacements` and a declaration name `src_decl_name`, create a new declaration called `new_decl_name` s.t. its type is the type of `src_decl_name` after applying the given constant replacement. Remark: the new type must be definitionally equal to the type of `src_decl_name`. Example: Assume the environment contains def f : nat -> nat := ... def g : nat -> nat := f lemma f_lemma : forall a, f a > 0 := ... Moreover, assume we have a mapping M containing `f -> `g Then, the command run_command copy_decl_updating_type M `f_lemma `g_lemma creates the declaration lemma g_lemma : forall a, g a > 0 := ... -/ meta def copy_decl_updating_type (replacements : name_map name) (src_decl_name : name) (new_decl_name : name) : command := do env ← get_env, decl ← env.get src_decl_name, let decl := decl.update_name $ new_decl_name, let decl := decl.update_type $ apply_replacement replacements decl.type, let decl := decl.update_value $ expr.const src_decl_name (decl.univ_params.map level.param), add_decl decl meta def copy_decl_using (replacements : name_map name) (src_decl_name : name) (new_decl_name : name) : command := do env ← get_env, decl ← env.get src_decl_name, let decl := decl.update_name $ new_decl_name, let decl := decl.update_type $ apply_replacement replacements decl.type, let decl := decl.map_value $ apply_replacement replacements, add_decl decl
df2e97067b763db3ee3ba789e4849e7281441e37
798dd332c1ad790518589a09bc82459fb12e5156
/algebra/big_operators.lean
0a9c49296ba56ede2000dc016aa03f62b01f823a
[ "Apache-2.0" ]
permissive
tobiasgrosser/mathlib
b040b7eb42d5942206149371cf92c61404de3c31
120635628368ec261e031cefc6d30e0304088b03
refs/heads/master
1,644,803,442,937
1,536,663,752,000
1,536,663,907,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
19,123
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Some big operators for lists and finite sets. -/ import data.list.basic data.list.perm data.finset algebra.group algebra.ordered_group algebra.group_power universes u v w variables {α : Type u} {β : Type v} {γ : Type w} namespace finset variables {s s₁ s₂ : finset α} {a : α} {f g : α → β} /-- `prod s f` is the product of `f x` as `x` ranges over the elements of the finite set `s`. -/ @[to_additive finset.sum] protected def prod [comm_monoid β] (s : finset α) (f : α → β) : β := (s.1.map f).prod attribute [to_additive finset.sum.equations._eqn_1] finset.prod.equations._eqn_1 @[to_additive finset.sum_eq_fold] theorem prod_eq_fold [comm_monoid β] (s : finset α) (f : α → β) : s.prod f = s.fold (*) 1 f := rfl section comm_monoid variables [comm_monoid β] @[simp, to_additive finset.sum_empty] lemma prod_empty {α : Type u} {f : α → β} : (∅:finset α).prod f = 1 := rfl @[simp, to_additive finset.sum_insert] lemma prod_insert [decidable_eq α] : a ∉ s → (insert a s).prod f = f a * s.prod f := fold_insert @[simp, to_additive finset.sum_singleton] lemma prod_singleton : (singleton a).prod f = f a := eq.trans fold_singleton (by simp) @[simp] lemma prod_const_one : s.prod (λx, (1 : β)) = 1 := by simp [finset.prod] @[simp] lemma sum_const_zero {β} {s : finset α} [add_comm_monoid β] : s.sum (λx, (0 : β)) = 0 := @prod_const_one _ (multiplicative β) _ _ attribute [to_additive finset.sum_const_zero] prod_const_one @[simp, to_additive finset.sum_image] lemma prod_image [decidable_eq α] [decidable_eq γ] {s : finset γ} {g : γ → α} : (∀x∈s, ∀y∈s, g x = g y → x = y) → (s.image g).prod f = s.prod (λx, f (g x)) := fold_image @[congr, to_additive finset.sum_congr] lemma prod_congr (h : s₁ = s₂) : (∀x∈s₂, f x = g x) → s₁.prod f = s₂.prod g := by rw [h]; exact fold_congr attribute [congr] finset.sum_congr @[to_additive finset.sum_union_inter] lemma prod_union_inter [decidable_eq α] : (s₁ ∪ s₂).prod f * (s₁ ∩ s₂).prod f = s₁.prod f * s₂.prod f := fold_union_inter @[to_additive finset.sum_union] lemma prod_union [decidable_eq α] (h : s₁ ∩ s₂ = ∅) : (s₁ ∪ s₂).prod f = s₁.prod f * s₂.prod f := by rw [←prod_union_inter, h]; simp @[to_additive finset.sum_sdiff] lemma prod_sdiff [decidable_eq α] (h : s₁ ⊆ s₂) : (s₂ \ s₁).prod f * s₁.prod f = s₂.prod f := by rw [←prod_union (sdiff_inter_self _ _), sdiff_union_of_subset h] @[to_additive finset.sum_bind] lemma prod_bind [decidable_eq α] {s : finset γ} {t : γ → finset α} : (∀x∈s, ∀y∈s, x ≠ y → t x ∩ t y = ∅) → (s.bind t).prod f = s.prod (λx, (t x).prod f) := by haveI := classical.dec_eq γ; exact finset.induction_on s (by simp) (assume x s hxs ih hd, have hd' : ∀x∈s, ∀y∈s, x ≠ y → t x ∩ t y = ∅, from assume _ hx _ hy, hd _ (mem_insert_of_mem hx) _ (mem_insert_of_mem hy), have t x ∩ finset.bind s t = ∅, from ext.2 $ assume a, by simp [mem_bind]; from assume h₁ y hys hy₂, have ha : a ∈ t x ∩ t y, by simp [*], have t x ∩ t y = ∅, from hd _ (mem_insert_self _ _) _ (mem_insert_of_mem hys) $ assume h, hxs $ h.symm ▸ hys, by rwa [this] at ha, by simp [hxs, prod_union this, ih hd'] {contextual := tt}) @[to_additive finset.sum_product] lemma prod_product {s : finset γ} {t : finset α} {f : γ×α → β} : (s.product t).prod f = s.prod (λx, t.prod $ λy, f (x, y)) := begin haveI := classical.dec_eq α, haveI := classical.dec_eq γ, rw [product_eq_bind, prod_bind (λ x hx y hy h, ext.2 _)], {simp [prod_image]}, simp [mem_image], intros, intro, refine h _, cc end @[to_additive finset.sum_sigma] lemma prod_sigma {σ : α → Type*} {s : finset α} {t : Πa, finset (σ a)} {f : sigma σ → β} : (s.sigma t).prod f = s.prod (λa, (t a).prod $ λs, f ⟨a, s⟩) := by haveI := classical.dec_eq α; haveI := (λ a, classical.dec_eq (σ a)); exact have ∀a₁ a₂:α, ∀s₁ : finset (σ a₁), ∀s₂ : finset (σ a₂), a₁ ≠ a₂ → s₁.image (sigma.mk a₁) ∩ s₂.image (sigma.mk a₂) = ∅, from assume b₁ b₂ s₁ s₂ h, ext.2 $ assume ⟨b₃, c₃⟩, by simp [mem_image, sigma.mk.inj_iff, heq_iff_eq] {contextual := tt}; cc, calc (s.sigma t).prod f = (s.bind (λa, (t a).image (λs, sigma.mk a s))).prod f : by rw sigma_eq_bind ... = s.prod (λa, ((t a).image (λs, sigma.mk a s)).prod f) : prod_bind $ assume a₁ ha a₂ ha₂ h, this a₁ a₂ (t a₁) (t a₂) h ... = (s.prod $ λa, (t a).prod $ λs, f ⟨a, s⟩) : by simp [prod_image, sigma.mk.inj_iff, heq_iff_eq] @[to_additive finset.sum_add_distrib] lemma prod_mul_distrib : s.prod (λx, f x * g x) = s.prod f * s.prod g := eq.trans (by simp; refl) fold_op_distrib @[to_additive finset.sum_comm] lemma prod_comm [decidable_eq γ] {s : finset γ} {t : finset α} {f : γ → α → β} : s.prod (λx, t.prod $ f x) = t.prod (λy, s.prod $ λx, f x y) := finset.induction_on s (by simp) (by simp [prod_mul_distrib] {contextual := tt}) @[to_additive finset.sum_hom] lemma prod_hom [comm_monoid γ] (g : β → γ) (h₁ : g 1 = 1) (h₂ : ∀x y, g (x * y) = g x * g y) : s.prod (λx, g (f x)) = g (s.prod f) := eq.trans (by rw [h₁]; refl) (fold_hom h₂) lemma sum_nat_cast [add_comm_monoid β] [has_one β] (s : finset α) (f : α → ℕ) : ↑(s.sum f) = s.sum (λa, f a : α → β) := (sum_hom _ nat.cast_zero nat.cast_add).symm @[to_additive finset.sum_subset] lemma prod_subset (h : s₁ ⊆ s₂) (hf : ∀x∈s₂, x ∉ s₁ → f x = 1) : s₁.prod f = s₂.prod f := by haveI := classical.dec_eq α; exact have (s₂ \ s₁).prod f = (s₂ \ s₁).prod (λx, 1), from prod_congr rfl begin simp [hf] {contextual := tt} end, by rw [←prod_sdiff h]; simp [this] @[to_additive finset.sum_eq_single] lemma prod_eq_single {s : finset α} {f : α → β} (a : α) (h₀ : ∀b∈s, b ≠ a → f b = 1) (h₁ : a ∉ s → f a = 1) : s.prod f = f a := by haveI := classical.dec_eq α; from classical.by_cases (assume : a ∈ s, calc s.prod f = ({a} : finset α).prod f : begin refine (prod_subset _ _).symm, { simp [finset.subset_iff, this] }, { simpa using h₀ } end ... = f a : by simp) (assume : a ∉ s, have ∀b, b ∈ s → f b = 1, from assume b hb, h₀ b hb $ assume eq, this $ eq ▸ hb, calc s.prod f = (∅ : finset α).prod f : (prod_subset (empty_subset s) $ by simpa).symm ... = f a : (h₁ ‹a ∉ s›).symm) @[to_additive finset.sum_attach] lemma prod_attach {f : α → β} : s.attach.prod (λx, f x.val) = s.prod f := by haveI := classical.dec_eq α; exact calc s.attach.prod (λx, f x.val) = ((s.attach).image subtype.val).prod f : by rw [prod_image]; exact assume x _ y _, subtype.eq ... = _ : by rw [attach_image_val] @[to_additive finset.sum_bij] lemma prod_bij {s : finset α} {t : finset γ} {f : α → β} {g : γ → β} (i : Πa∈s, γ) (hi : ∀a ha, i a ha ∈ t) (h : ∀a ha, f a = g (i a ha)) (i_inj : ∀a₁ a₂ ha₁ ha₂, i a₁ ha₁ = i a₂ ha₂ → a₁ = a₂) (i_surj : ∀b∈t, ∃a ha, b = i a ha) : s.prod f = t.prod g := by haveI := classical.prop_decidable; exact calc s.prod f = s.attach.prod (λx, f x.val) : prod_attach.symm ... = s.attach.prod (λx, g (i x.1 x.2)) : prod_congr rfl $ assume x hx, h _ _ ... = (s.attach.image $ λx:{x // x ∈ s}, i x.1 x.2).prod g : (prod_image $ assume (a₁:{x // x ∈ s}) _ a₂ _ eq, subtype.eq $ i_inj a₁.1 a₂.1 a₁.2 a₂.2 eq).symm ... = _ : prod_subset (by simp [subset_iff]; intros b a h eq; subst eq; exact hi _ _) (assume b hb, not_imp_comm.mp $ assume hb₂, let ⟨a, ha, eq⟩ := i_surj b hb in by simp [eq]; exact ⟨_, _, rfl⟩) @[to_additive finset.sum_bij_ne_zero] lemma prod_bij_ne_one {s : finset α} {t : finset γ} {f : α → β} {g : γ → β} (i : Πa∈s, f a ≠ 1 → γ) (hi₁ : ∀a h₁ h₂, i a h₁ h₂ ∈ t) (hi₂ : ∀a₁ a₂ h₁₁ h₁₂ h₂₁ h₂₂, i a₁ h₁₁ h₁₂ = i a₂ h₂₁ h₂₂ → a₁ = a₂) (hi₃ : ∀b∈t, g b ≠ 1 → ∃a h₁ h₂, b = i a h₁ h₂) (h : ∀a h₁ h₂, f a = g (i a h₁ h₂)) : s.prod f = t.prod g := by haveI := classical.prop_decidable; exact calc s.prod f = (s.filter $ λx, f x ≠ 1).prod f : (prod_subset (filter_subset _) $ by simp {contextual:=tt}).symm ... = (t.filter $ λx, g x ≠ 1).prod g : prod_bij (assume a ha, i a (mem_filter.mp ha).1 (mem_filter.mp ha).2) (assume a ha, (mem_filter.mp ha).elim $ λh₁ h₂, mem_filter.mpr ⟨hi₁ a h₁ h₂, λ hg, h₂ (hg ▸ h a h₁ h₂)⟩) (assume a ha, (mem_filter.mp ha).elim $ h a) (assume a₁ a₂ ha₁ ha₂, (mem_filter.mp ha₁).elim $ λha₁₁ ha₁₂, (mem_filter.mp ha₂).elim $ λha₂₁ ha₂₂, hi₂ a₁ a₂ _ _ _ _) (assume b hb, (mem_filter.mp hb).elim $ λh₁ h₂, let ⟨a, ha₁, ha₂, eq⟩ := hi₃ b h₁ h₂ in ⟨a, mem_filter.mpr ⟨ha₁, ha₂⟩, eq⟩) ... = t.prod g : (prod_subset (filter_subset _) $ by simp {contextual:=tt}) @[to_additive finset.exists_ne_zero_of_sum_ne_zero] lemma exists_ne_one_of_prod_ne_one : s.prod f ≠ 1 → ∃a∈s, f a ≠ 1 := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (assume a s has ih h, classical.by_cases (assume ha : f a = 1, have s.prod f ≠ 1, by simpa [ha, has] using h, let ⟨a, ha, hfa⟩ := ih this in ⟨a, mem_insert_of_mem ha, hfa⟩) (assume hna : f a ≠ 1, ⟨a, mem_insert_self _ _, hna⟩)) @[to_additive finset.sum_range_succ] lemma prod_range_succ (f : ℕ → β) (n : ℕ) : (range (nat.succ n)).prod f = f n * (range n).prod f := by simp lemma prod_range_succ' (f : ℕ → β) : ∀ n : ℕ, (range (nat.succ n)).prod f = (range n).prod (f ∘ nat.succ) * f 0 | 0 := by simp | (n + 1) := by rw [prod_range_succ (λ m, f (nat.succ m)), mul_assoc, ← prod_range_succ']; exact prod_range_succ _ _ @[simp] lemma prod_const [decidable_eq α] (b : β) : s.prod (λ a, b) = b ^ s.card := finset.induction_on s rfl (by simp [pow_add, mul_comm] {contextual := tt}) end comm_monoid @[simp] lemma sum_const [add_comm_monoid β] [decidable_eq α] (b : β) : s.sum (λ a, b) = add_monoid.smul s.card b := @prod_const _ (multiplicative β) _ _ _ _ attribute [to_additive finset.sum_const] prod_const lemma sum_range_succ' [add_comm_monoid β] (f : ℕ → β) : ∀ n : ℕ, (range (nat.succ n)).sum f = (range n).sum (f ∘ nat.succ) + f 0 := @prod_range_succ' (multiplicative β) _ _ attribute [to_additive finset.sum_range_succ'] prod_range_succ' section comm_group variables [comm_group β] @[simp, to_additive finset.sum_neg_distrib] lemma prod_inv_distrib : s.prod (λx, (f x)⁻¹) = (s.prod f)⁻¹ := prod_hom has_inv.inv one_inv mul_inv end comm_group @[simp] theorem card_sigma {σ : α → Type*} (s : finset α) (t : Π a, finset (σ a)) : card (s.sigma t) = s.sum (λ a, card (t a)) := multiset.card_sigma _ _ end finset namespace finset variables {s s₁ s₂ : finset α} {f g : α → β} {b : β} {a : α} @[simp] lemma sum_sub_distrib [add_comm_group β] : s.sum (λx, f x - g x) = s.sum f - s.sum g := by simp [sum_add_distrib] section ordered_cancel_comm_monoid variables [decidable_eq α] [ordered_cancel_comm_monoid β] lemma sum_le_sum : (∀x∈s, f x ≤ g x) → s.sum f ≤ s.sum g := finset.induction_on s (by simp) $ assume a s ha ih h, have f a + s.sum f ≤ g a + s.sum g, from add_le_add (h _ (mem_insert_self _ _)) (ih $ assume x hx, h _ $ mem_insert_of_mem hx), by simp [*] lemma zero_le_sum (h : ∀x∈s, 0 ≤ f x) : 0 ≤ s.sum f := le_trans (by simp) (sum_le_sum h) lemma sum_le_zero (h : ∀x∈s, f x ≤ 0) : s.sum f ≤ 0 := le_trans (sum_le_sum h) (by simp) end ordered_cancel_comm_monoid section semiring variables [semiring β] lemma sum_mul : s.sum f * b = s.sum (λx, f x * b) := (sum_hom (λx, x * b) (zero_mul b) (assume a c, add_mul a c b)).symm lemma mul_sum : b * s.sum f = s.sum (λx, b * f x) := (sum_hom (λx, b * x) (mul_zero b) (assume a c, mul_add b a c)).symm end semiring section comm_semiring variables [decidable_eq α] [comm_semiring β] lemma prod_eq_zero (ha : a ∈ s) (h : f a = 0) : s.prod f = 0 := calc s.prod f = (insert a (erase s a)).prod f : by simp [ha, insert_erase] ... = 0 : by simp [h] lemma prod_sum {δ : α → Type*} [∀a, decidable_eq (δ a)] {s : finset α} {t : Πa, finset (δ a)} {f : Πa, δ a → β} : s.prod (λa, (t a).sum (λb, f a b)) = (s.pi t).sum (λp, s.attach.prod (λx, f x.1 (p x.1 x.2))) := begin induction s using finset.induction with a s ha ih, { simp }, { have h₁ : ∀x ∈ t a, ∀y∈t a, ∀h : x ≠ y, image (pi.cons s a x) (pi s t) ∩ image (pi.cons s a y) (pi s t) = ∅, { assume x hx y hy h, apply eq_empty_of_forall_not_mem, simp, assume p₁ p₂ hp eq p₃ hp₃ eq', subst eq', have : pi.cons s a x p₂ a (mem_insert_self _ _) = pi.cons s a y p₃ a (mem_insert_self _ _), { rw [eq] }, rw [pi.cons_same, pi.cons_same] at this, exact h this }, have h₂ : ∀b:δ a, ∀p₁∈pi s t, ∀p₂∈pi s t, pi.cons s a b p₁ = pi.cons s a b p₂ → p₁ = p₂, from assume b p₁ h₁ p₂ h₂ eq, injective_pi_cons ha eq, have h₃ : ∀(v:{x // x ∈ s}) (b:δ a) (h : v.1 ∈ insert a s) (p : Πa, a ∈ s → δ a), pi.cons s a b p v.1 h = p v.1 v.2, from assume v b h p, pi.cons_ne $ assume eq, ha $ eq.symm ▸ v.2, simp [ha, ih, sum_bind h₁, sum_image (h₂ _), sum_mul, h₃], simp [mul_sum] } end end comm_semiring section integral_domain /- add integral_semi_domain to support nat and ennreal -/ variables [decidable_eq α] [integral_domain β] lemma prod_eq_zero_iff : s.prod f = 0 ↔ (∃a∈s, f a = 0) := finset.induction_on s (by simp) begin intros a s, rw [bex_def, bex_def, exists_mem_insert], simp [mul_eq_zero_iff_eq_zero_or_eq_zero] {contextual := tt} end end integral_domain section ordered_comm_monoid variables [decidable_eq α] [ordered_comm_monoid β] lemma sum_le_sum' : (∀x∈s, f x ≤ g x) → s.sum f ≤ s.sum g := finset.induction_on s (by simp; refl) $ assume a s ha ih h, have f a + s.sum f ≤ g a + s.sum g, from add_le_add' (h _ (mem_insert_self _ _)) (ih $ assume x hx, h _ $ mem_insert_of_mem hx), by simp [*] lemma zero_le_sum' (h : ∀x∈s, 0 ≤ f x) : 0 ≤ s.sum f := le_trans (by simp) (sum_le_sum' h) lemma sum_le_zero' (h : ∀x∈s, f x ≤ 0) : s.sum f ≤ 0 := le_trans (sum_le_sum' h) (by simp) lemma sum_le_sum_of_subset_of_nonneg (h : s₁ ⊆ s₂) (hf : ∀x∈s₂, x ∉ s₁ → 0 ≤ f x) : s₁.sum f ≤ s₂.sum f := calc s₁.sum f ≤ (s₂ \ s₁).sum f + s₁.sum f : le_add_of_nonneg_left' $ zero_le_sum' $ by simp [hf] {contextual := tt} ... = (s₂ \ s₁ ∪ s₁).sum f : (sum_union (sdiff_inter_self _ _)).symm ... = s₂.sum f : by rw [sdiff_union_of_subset h] lemma sum_eq_zero_iff_of_nonneg : (∀x∈s, 0 ≤ f x) → (s.sum f = 0 ↔ ∀x∈s, f x = 0) := finset.induction_on s (by simp) $ by simp [or_imp_distrib, forall_and_distrib, zero_le_sum' , add_eq_zero_iff_eq_zero_and_eq_zero_of_nonneg_of_nonneg'] {contextual := tt} lemma single_le_sum (hf : ∀x∈s, 0 ≤ f x) {a} (h : a ∈ s) : f a ≤ s.sum f := by simpa using show (singleton a).sum f ≤ s.sum f, from sum_le_sum_of_subset_of_nonneg (λ x e, (mem_singleton.1 e).symm ▸ h) (λ x h _, hf x h) end ordered_comm_monoid section canonically_ordered_monoid variables [decidable_eq α] [canonically_ordered_monoid β] [@decidable_rel β (≤)] lemma sum_le_sum_of_subset (h : s₁ ⊆ s₂) : s₁.sum f ≤ s₂.sum f := sum_le_sum_of_subset_of_nonneg h $ assume x h₁ h₂, zero_le _ lemma sum_le_sum_of_ne_zero (h : ∀x∈s₁, f x ≠ 0 → x ∈ s₂) : s₁.sum f ≤ s₂.sum f := calc s₁.sum f = (s₁.filter (λx, f x = 0)).sum f + (s₁.filter (λx, f x ≠ 0)).sum f : by rw [←sum_union, filter_union_filter_neg_eq]; apply filter_inter_filter_neg_eq ... ≤ s₂.sum f : add_le_of_nonpos_of_le' (sum_le_zero' $ by simp {contextual:=tt}) (sum_le_sum_of_subset $ by simp [subset_iff, *] {contextual:=tt}) end canonically_ordered_monoid section discrete_linear_ordered_field variables [discrete_linear_ordered_field α] [decidable_eq β] lemma abs_sum_le_sum_abs {f : β → α} {s : finset β} : abs (s.sum f) ≤ s.sum (λa, abs (f a)) := finset.induction_on s (by simp [abs_zero]) $ assume a s has ih, calc abs (sum (insert a s) f) ≤ abs (f a) + abs (sum s f) : by simp [has]; exact abs_add_le_abs_add_abs _ _ ... ≤ abs (f a) + s.sum (λa, abs (f a)) : add_le_add (le_refl _) ih ... ≤ sum (insert a s) (λ (a : β), abs (f a)) : by simp [has] end discrete_linear_ordered_field @[simp] lemma card_pi [decidable_eq α] {δ : α → Type*} (s : finset α) (t : Π a, finset (δ a)) : (s.pi t).card = s.prod (λ a, card (t a)) := multiset.card_pi _ _ end finset section group open list variables [group α] [group β] theorem is_group_hom.prod (f : α → β) [is_group_hom f] (l : list α) : f (prod l) = prod (map f l) := by induction l; simp [*, is_group_hom.mul f, is_group_hom.one f] theorem is_group_anti_hom.prod (f : α → β) [is_group_anti_hom f] (l : list α) : f (prod l) = prod (map f (reverse l)) := by induction l; simp [*, is_group_anti_hom.mul f, is_group_anti_hom.one f] theorem inv_prod : ∀ l : list α, (prod l)⁻¹ = prod (map (λ x, x⁻¹) (reverse l)) := λ l, @is_group_anti_hom.prod _ _ _ _ _ inv_is_group_anti_hom l -- TODO there is probably a cleaner proof of this end group namespace multiset variables [decidable_eq α] @[simp] lemma to_finset_sum_count_eq (s : multiset α) : s.to_finset.sum (λa, s.count a) = s.card := multiset.induction_on s (by simp) (assume a s ih, calc (to_finset (a :: s)).sum (λx, count x (a :: s)) = (to_finset (a :: s)).sum (λx, (if x = a then 1 else 0) + count x s) : by congr; funext x; split_ifs; simp [h, nat.one_add] ... = card (a :: s) : begin by_cases a ∈ s.to_finset, { have : (to_finset s).sum (λx, ite (x = a) 1 0) = ({a}:finset α).sum (λx, ite (x = a) 1 0), { apply (finset.sum_subset _ _).symm, { simp [finset.subset_iff, *] at * }, { simp [if_neg] {contextual := tt} } }, simp [h, ih, this, finset.sum_add_distrib] }, { have : a ∉ s, by simp * at *, have : (to_finset s).sum (λx, ite (x = a) 1 0) = (to_finset s).sum (λx, 0), from finset.sum_congr rfl begin assume a ha, split_ifs; simp [*] at * end, simp [*, finset.sum_add_distrib], } end) end multiset
37aefd874c2286c9e40873110979575f05c5f2f7
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/uniform_space/completion.lean
ae7133be2cc5694d9f45a976904a08fdc2f1ff55
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
24,487
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl -/ import topology.uniform_space.abstract_completion /-! # Hausdorff completions of uniform spaces The goal is to construct a left-adjoint to the inclusion of complete Hausdorff uniform spaces into all uniform spaces. Any uniform space `α` gets a completion `completion α` and a morphism (ie. uniformly continuous map) `coe : α → completion α` which solves the universal mapping problem of factorizing morphisms from `α` to any complete Hausdorff uniform space `β`. It means any uniformly continuous `f : α → β` gives rise to a unique morphism `completion.extension f : completion α → β` such that `f = completion.extension f ∘ coe`. Actually `completion.extension f` is defined for all maps from `α` to `β` but it has the desired properties only if `f` is uniformly continuous. Beware that `coe` is not injective if `α` is not Hausdorff. But its image is always dense. The adjoint functor acting on morphisms is then constructed by the usual abstract nonsense. For every uniform spaces `α` and `β`, it turns `f : α → β` into a morphism `completion.map f : completion α → completion β` such that `coe ∘ f = (completion.map f) ∘ coe` provided `f` is uniformly continuous. This construction is compatible with composition. In this file we introduce the following concepts: * `Cauchy α` the uniform completion of the uniform space `α` (using Cauchy filters). These are not minimal filters. * `completion α := quotient (separation_setoid (Cauchy α))` the Hausdorff completion. ## References This formalization is mostly based on N. Bourbaki: General Topology I. M. James: Topologies and Uniformities From a slightly different perspective in order to reuse material in topology.uniform_space.basic. -/ noncomputable theory open filter set universes u v w x open_locale uniformity classical topological_space filter /-- Space of Cauchy filters This is essentially the completion of a uniform space. The embeddings are the neighbourhood filters. This space is not minimal, the separated uniform space (i.e. quotiented on the intersection of all entourages) is necessary for this. -/ def Cauchy (α : Type u) [uniform_space α] : Type u := { f : filter α // cauchy f } namespace Cauchy section parameters {α : Type u} [uniform_space α] variables {β : Type v} {γ : Type w} variables [uniform_space β] [uniform_space γ] /-- The pairs of Cauchy filters generated by a set. -/ def gen (s : set (α × α)) : set (Cauchy α × Cauchy α) := {p | s ∈ p.1.val ×ᶠ p.2.val } lemma monotone_gen : monotone gen := monotone_set_of $ assume p, @filter.monotone_mem _ (p.1.val ×ᶠ p.2.val) private lemma symm_gen : map prod.swap ((𝓤 α).lift' gen) ≤ (𝓤 α).lift' gen := calc map prod.swap ((𝓤 α).lift' gen) = (𝓤 α).lift' (λs:set (α×α), {p | s ∈ p.2.val ×ᶠ p.1.val }) : begin delta gen, simp [map_lift'_eq, monotone_set_of, filter.monotone_mem, function.comp, image_swap_eq_preimage_swap, -subtype.val_eq_coe] end ... ≤ (𝓤 α).lift' gen : uniformity_lift_le_swap (monotone_principal.comp (monotone_set_of $ assume p, @filter.monotone_mem _ (p.2.val ×ᶠ p.1.val))) begin have h := λ(p:Cauchy α×Cauchy α), @filter.prod_comm _ _ (p.2.val) (p.1.val), simp [function.comp, h, -subtype.val_eq_coe, mem_map'], exact le_rfl, end private lemma comp_rel_gen_gen_subset_gen_comp_rel {s t : set (α×α)} : comp_rel (gen s) (gen t) ⊆ (gen (comp_rel s t) : set (Cauchy α × Cauchy α)) := assume ⟨f, g⟩ ⟨h, h₁, h₂⟩, let ⟨t₁, (ht₁ : t₁ ∈ f.val), t₂, (ht₂ : t₂ ∈ h.val), (h₁ : t₁ ×ˢ t₂ ⊆ s)⟩ := mem_prod_iff.mp h₁ in let ⟨t₃, (ht₃ : t₃ ∈ h.val), t₄, (ht₄ : t₄ ∈ g.val), (h₂ : t₃ ×ˢ t₄ ⊆ t)⟩ := mem_prod_iff.mp h₂ in have t₂ ∩ t₃ ∈ h.val, from inter_mem ht₂ ht₃, let ⟨x, xt₂, xt₃⟩ := h.property.left.nonempty_of_mem this in (f.val ×ᶠ g.val).sets_of_superset (prod_mem_prod ht₁ ht₄) (assume ⟨a, b⟩ ⟨(ha : a ∈ t₁), (hb : b ∈ t₄)⟩, ⟨x, h₁ (show (a, x) ∈ t₁ ×ˢ t₂, from ⟨ha, xt₂⟩), h₂ (show (x, b) ∈ t₃ ×ˢ t₄, from ⟨xt₃, hb⟩)⟩) private lemma comp_gen : ((𝓤 α).lift' gen).lift' (λs, comp_rel s s) ≤ (𝓤 α).lift' gen := calc ((𝓤 α).lift' gen).lift' (λs, comp_rel s s) = (𝓤 α).lift' (λs, comp_rel (gen s) (gen s)) : begin rw [lift'_lift'_assoc], exact monotone_gen, exact (monotone_comp_rel monotone_id monotone_id) end ... ≤ (𝓤 α).lift' (λs, gen $ comp_rel s s) : lift'_mono' $ assume s hs, comp_rel_gen_gen_subset_gen_comp_rel ... = ((𝓤 α).lift' $ λs:set(α×α), comp_rel s s).lift' gen : begin rw [lift'_lift'_assoc], exact (monotone_comp_rel monotone_id monotone_id), exact monotone_gen end ... ≤ (𝓤 α).lift' gen : lift'_mono comp_le_uniformity le_rfl instance : uniform_space (Cauchy α) := uniform_space.of_core { uniformity := (𝓤 α).lift' gen, refl := principal_le_lift'.2 $ λ s hs ⟨a, b⟩ (a_eq_b : a = b), a_eq_b ▸ a.property.right hs, symm := symm_gen, comp := comp_gen } theorem mem_uniformity {s : set (Cauchy α × Cauchy α)} : s ∈ 𝓤 (Cauchy α) ↔ ∃ t ∈ 𝓤 α, gen t ⊆ s := mem_lift'_sets monotone_gen theorem mem_uniformity' {s : set (Cauchy α × Cauchy α)} : s ∈ 𝓤 (Cauchy α) ↔ ∃ t ∈ 𝓤 α, ∀ f g : Cauchy α, t ∈ f.1 ×ᶠ g.1 → (f, g) ∈ s := mem_uniformity.trans $ bex_congr $ λ t h, prod.forall /-- Embedding of `α` into its completion `Cauchy α` -/ def pure_cauchy (a : α) : Cauchy α := ⟨pure a, cauchy_pure⟩ lemma uniform_inducing_pure_cauchy : uniform_inducing (pure_cauchy : α → Cauchy α) := ⟨have (preimage (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ∘ gen) = id, from funext $ assume s, set.ext $ assume ⟨a₁, a₂⟩, by simp [preimage, gen, pure_cauchy, prod_principal_principal], calc comap (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ((𝓤 α).lift' gen) = (𝓤 α).lift' (preimage (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ∘ gen) : comap_lift'_eq ... = 𝓤 α : by simp [this]⟩ lemma uniform_embedding_pure_cauchy : uniform_embedding (pure_cauchy : α → Cauchy α) := { inj := assume a₁ a₂ h, pure_injective $ subtype.ext_iff_val.1 h, ..uniform_inducing_pure_cauchy } lemma dense_range_pure_cauchy : dense_range pure_cauchy := assume f, have h_ex : ∀ s ∈ 𝓤 (Cauchy α), ∃y:α, (f, pure_cauchy y) ∈ s, from assume s hs, let ⟨t'', ht''₁, (ht''₂ : gen t'' ⊆ s)⟩ := (mem_lift'_sets monotone_gen).mp hs in let ⟨t', ht'₁, ht'₂⟩ := comp_mem_uniformity_sets ht''₁ in have t' ∈ f.val ×ᶠ f.val, from f.property.right ht'₁, let ⟨t, ht, (h : t ×ˢ t ⊆ t')⟩ := mem_prod_same_iff.mp this in let ⟨x, (hx : x ∈ t)⟩ := f.property.left.nonempty_of_mem ht in have t'' ∈ f.val ×ᶠ pure x, from mem_prod_iff.mpr ⟨t, ht, {y:α | (x, y) ∈ t'}, h $ mk_mem_prod hx hx, assume ⟨a, b⟩ ⟨(h₁ : a ∈ t), (h₂ : (x, b) ∈ t')⟩, ht'₂ $ prod_mk_mem_comp_rel (@h (a, x) ⟨h₁, hx⟩) h₂⟩, ⟨x, ht''₂ $ by dsimp [gen]; exact this⟩, begin simp only [closure_eq_cluster_pts, cluster_pt, nhds_eq_uniformity, lift'_inf_principal_eq, set.inter_comm _ (range pure_cauchy), mem_set_of_eq], exact (lift'_ne_bot_iff $ monotone_const.inter monotone_preimage).mpr (assume s hs, let ⟨y, hy⟩ := h_ex s hs in have pure_cauchy y ∈ range pure_cauchy ∩ {y : Cauchy α | (f, y) ∈ s}, from ⟨mem_range_self y, hy⟩, ⟨_, this⟩) end lemma dense_inducing_pure_cauchy : dense_inducing pure_cauchy := uniform_inducing_pure_cauchy.dense_inducing dense_range_pure_cauchy lemma dense_embedding_pure_cauchy : dense_embedding pure_cauchy := uniform_embedding_pure_cauchy.dense_embedding dense_range_pure_cauchy lemma nonempty_Cauchy_iff : nonempty (Cauchy α) ↔ nonempty α := begin split ; rintro ⟨c⟩, { have := eq_univ_iff_forall.1 dense_embedding_pure_cauchy.to_dense_inducing.closure_range c, obtain ⟨_, ⟨_, a, _⟩⟩ := mem_closure_iff.1 this _ is_open_univ trivial, exact ⟨a⟩ }, { exact ⟨pure_cauchy c⟩ } end section set_option eqn_compiler.zeta true instance : complete_space (Cauchy α) := complete_space_extension uniform_inducing_pure_cauchy dense_range_pure_cauchy $ assume f hf, let f' : Cauchy α := ⟨f, hf⟩ in have map pure_cauchy f ≤ (𝓤 $ Cauchy α).lift' (preimage (prod.mk f')), from le_lift'.2 $ assume s hs, let ⟨t, ht₁, (ht₂ : gen t ⊆ s)⟩ := (mem_lift'_sets monotone_gen).mp hs in let ⟨t', ht', (h : t' ×ˢ t' ⊆ t)⟩ := mem_prod_same_iff.mp (hf.right ht₁) in have t' ⊆ { y : α | (f', pure_cauchy y) ∈ gen t }, from assume x hx, (f ×ᶠ pure x).sets_of_superset (prod_mem_prod ht' hx) h, f.sets_of_superset ht' $ subset.trans this (preimage_mono ht₂), ⟨f', by simp [nhds_eq_uniformity]; assumption⟩ end instance [inhabited α] : inhabited (Cauchy α) := ⟨pure_cauchy default⟩ instance [h : nonempty α] : nonempty (Cauchy α) := h.rec_on $ assume a, nonempty.intro $ Cauchy.pure_cauchy a section extend /-- Extend a uniformly continuous function `α → β` to a function `Cauchy α → β`. Outputs junk when `f` is not uniformly continuous. -/ def extend (f : α → β) : Cauchy α → β := if uniform_continuous f then dense_inducing_pure_cauchy.extend f else λ x, f (nonempty_Cauchy_iff.1 ⟨x⟩).some section separated_space variables [separated_space β] lemma extend_pure_cauchy {f : α → β} (hf : uniform_continuous f) (a : α) : extend f (pure_cauchy a) = f a := begin rw [extend, if_pos hf], exact uniformly_extend_of_ind uniform_inducing_pure_cauchy dense_range_pure_cauchy hf _ end end separated_space variables [_root_.complete_space β] lemma uniform_continuous_extend {f : α → β} : uniform_continuous (extend f) := begin by_cases hf : uniform_continuous f, { rw [extend, if_pos hf], exact uniform_continuous_uniformly_extend uniform_inducing_pure_cauchy dense_range_pure_cauchy hf }, { rw [extend, if_neg hf], exact uniform_continuous_of_const (assume a b, by congr) } end end extend end theorem Cauchy_eq {α : Type*} [inhabited α] [uniform_space α] [complete_space α] [separated_space α] {f g : Cauchy α} : Lim f.1 = Lim g.1 ↔ (f, g) ∈ separation_rel (Cauchy α) := begin split, { intros e s hs, rcases Cauchy.mem_uniformity'.1 hs with ⟨t, tu, ts⟩, apply ts, rcases comp_mem_uniformity_sets tu with ⟨d, du, dt⟩, refine mem_prod_iff.2 ⟨_, f.2.le_nhds_Lim (mem_nhds_right (Lim f.1) du), _, g.2.le_nhds_Lim (mem_nhds_left (Lim g.1) du), λ x h, _⟩, cases x with a b, cases h with h₁ h₂, rw ← e at h₂, exact dt ⟨_, h₁, h₂⟩ }, { intros H, refine separated_def.1 (by apply_instance) _ _ (λ t tu, _), rcases mem_uniformity_is_closed tu with ⟨d, du, dc, dt⟩, refine H {p | (Lim p.1.1, Lim p.2.1) ∈ t} (Cauchy.mem_uniformity'.2 ⟨d, du, λ f g h, _⟩), rcases mem_prod_iff.1 h with ⟨x, xf, y, yg, h⟩, have limc : ∀ (f : Cauchy α) (x ∈ f.1), Lim f.1 ∈ closure x, { intros f x xf, rw closure_eq_cluster_pts, exact f.2.1.mono (le_inf f.2.le_nhds_Lim (le_principal_iff.2 xf)) }, have := dc.closure_subset_iff.2 h, rw closure_prod_eq at this, refine dt (this ⟨_, _⟩); dsimp; apply limc; assumption } end section local attribute [instance] uniform_space.separation_setoid lemma separated_pure_cauchy_injective {α : Type*} [uniform_space α] [s : separated_space α] : function.injective (λa:α, ⟦pure_cauchy a⟧) | a b h := separated_def.1 s _ _ $ assume s hs, let ⟨t, ht, hts⟩ := by rw [← (@uniform_embedding_pure_cauchy α _).comap_uniformity, filter.mem_comap] at hs; exact hs in have (pure_cauchy a, pure_cauchy b) ∈ t, from quotient.exact h t ht, @hts (a, b) this end end Cauchy local attribute [instance] uniform_space.separation_setoid open Cauchy set namespace uniform_space variables (α : Type*) [uniform_space α] variables {β : Type*} [uniform_space β] variables {γ : Type*} [uniform_space γ] instance complete_space_separation [h : complete_space α] : complete_space (quotient (separation_setoid α)) := ⟨assume f, assume hf : cauchy f, have cauchy (f.comap (λx, ⟦x⟧)), from hf.comap' comap_quotient_le_uniformity $ hf.left.comap_of_surj (surjective_quotient_mk _), let ⟨x, (hx : f.comap (λx, ⟦x⟧) ≤ 𝓝 x)⟩ := complete_space.complete this in ⟨⟦x⟧, (comap_le_comap_iff $ by simp).1 (hx.trans $ map_le_iff_le_comap.1 continuous_quotient_mk.continuous_at)⟩⟩ /-- Hausdorff completion of `α` -/ def completion := quotient (separation_setoid $ Cauchy α) namespace completion instance [inhabited α] : inhabited (completion α) := quotient.inhabited (separation_setoid (Cauchy α)) @[priority 50] instance : uniform_space (completion α) := separation_setoid.uniform_space instance : complete_space (completion α) := uniform_space.complete_space_separation (Cauchy α) instance : separated_space (completion α) := uniform_space.separated_separation instance : t3_space (completion α) := separated_t3 /-- Automatic coercion from `α` to its completion. Not always injective. -/ instance : has_coe_t α (completion α) := ⟨quotient.mk ∘ pure_cauchy⟩ -- note [use has_coe_t] protected lemma coe_eq : (coe : α → completion α) = quotient.mk ∘ pure_cauchy := rfl lemma comap_coe_eq_uniformity : (𝓤 _).comap (λ(p:α×α), ((p.1 : completion α), (p.2 : completion α))) = 𝓤 α := begin have : (λx:α×α, ((x.1 : completion α), (x.2 : completion α))) = (λx:(Cauchy α)×(Cauchy α), (⟦x.1⟧, ⟦x.2⟧)) ∘ (λx:α×α, (pure_cauchy x.1, pure_cauchy x.2)), { ext ⟨a, b⟩; simp; refl }, rw [this, ← filter.comap_comap], change filter.comap _ (filter.comap _ (𝓤 $ quotient $ separation_setoid $ Cauchy α)) = 𝓤 α, rw [comap_quotient_eq_uniformity, uniform_embedding_pure_cauchy.comap_uniformity] end lemma uniform_inducing_coe : uniform_inducing (coe : α → completion α) := ⟨comap_coe_eq_uniformity α⟩ variables {α} lemma dense_range_coe : dense_range (coe : α → completion α) := dense_range_pure_cauchy.quotient variables (α) /-- The Haudorff completion as an abstract completion. -/ def cpkg {α : Type*} [uniform_space α] : abstract_completion α := { space := completion α, coe := coe, uniform_struct := by apply_instance, complete := by apply_instance, separation := by apply_instance, uniform_inducing := completion.uniform_inducing_coe α, dense := completion.dense_range_coe } instance abstract_completion.inhabited : inhabited (abstract_completion α) := ⟨cpkg⟩ local attribute [instance] abstract_completion.uniform_struct abstract_completion.complete abstract_completion.separation lemma nonempty_completion_iff : nonempty (completion α) ↔ nonempty α := cpkg.dense.nonempty_iff.symm lemma uniform_continuous_coe : uniform_continuous (coe : α → completion α) := cpkg.uniform_continuous_coe lemma continuous_coe : continuous (coe : α → completion α) := cpkg.continuous_coe lemma uniform_embedding_coe [separated_space α] : uniform_embedding (coe : α → completion α) := { comap_uniformity := comap_coe_eq_uniformity α, inj := separated_pure_cauchy_injective } lemma coe_injective [separated_space α] : function.injective (coe : α → completion α) := uniform_embedding.inj (uniform_embedding_coe _) variable {α} lemma dense_inducing_coe : dense_inducing (coe : α → completion α) := { dense := dense_range_coe, ..(uniform_inducing_coe α).inducing } /-- The uniform bijection between a complete space and its uniform completion. -/ def uniform_completion.complete_equiv_self [complete_space α] [separated_space α]: completion α ≃ᵤ α := abstract_completion.compare_equiv completion.cpkg abstract_completion.of_complete open topological_space instance separable_space_completion [separable_space α] : separable_space (completion α) := completion.dense_inducing_coe.separable_space lemma dense_embedding_coe [separated_space α]: dense_embedding (coe : α → completion α) := { inj := separated_pure_cauchy_injective, ..dense_inducing_coe } lemma dense_range_coe₂ : dense_range (λx:α × β, ((x.1 : completion α), (x.2 : completion β))) := dense_range_coe.prod_map dense_range_coe lemma dense_range_coe₃ : dense_range (λx:α × (β × γ), ((x.1 : completion α), ((x.2.1 : completion β), (x.2.2 : completion γ)))) := dense_range_coe.prod_map dense_range_coe₂ @[elab_as_eliminator] lemma induction_on {p : completion α → Prop} (a : completion α) (hp : is_closed {a | p a}) (ih : ∀a:α, p a) : p a := is_closed_property dense_range_coe hp ih a @[elab_as_eliminator] lemma induction_on₂ {p : completion α → completion β → Prop} (a : completion α) (b : completion β) (hp : is_closed {x : completion α × completion β | p x.1 x.2}) (ih : ∀(a:α) (b:β), p a b) : p a b := have ∀x : completion α × completion β, p x.1 x.2, from is_closed_property dense_range_coe₂ hp $ assume ⟨a, b⟩, ih a b, this (a, b) @[elab_as_eliminator] lemma induction_on₃ {p : completion α → completion β → completion γ → Prop} (a : completion α) (b : completion β) (c : completion γ) (hp : is_closed {x : completion α × completion β × completion γ | p x.1 x.2.1 x.2.2}) (ih : ∀(a:α) (b:β) (c:γ), p a b c) : p a b c := have ∀x : completion α × completion β × completion γ, p x.1 x.2.1 x.2.2, from is_closed_property dense_range_coe₃ hp $ assume ⟨a, b, c⟩, ih a b c, this (a, b, c) lemma ext {Y : Type*} [topological_space Y] [t2_space Y] {f g : completion α → Y} (hf : continuous f) (hg : continuous g) (h : ∀a:α, f a = g a) : f = g := cpkg.funext hf hg h lemma ext' {Y : Type*} [topological_space Y] [t2_space Y] {f g : completion α → Y} (hf : continuous f) (hg : continuous g) (h : ∀a:α, f a = g a) (a : completion α) : f a = g a := congr_fun (ext hf hg h) a section extension variables {f : α → β} /-- "Extension" to the completion. It is defined for any map `f` but returns an arbitrary constant value if `f` is not uniformly continuous -/ protected def extension (f : α → β) : completion α → β := cpkg.extend f section complete_space variables [complete_space β] lemma uniform_continuous_extension : uniform_continuous (completion.extension f) := cpkg.uniform_continuous_extend lemma continuous_extension : continuous (completion.extension f) := cpkg.continuous_extend end complete_space @[simp] lemma extension_coe [separated_space β] (hf : uniform_continuous f) (a : α) : (completion.extension f) a = f a := cpkg.extend_coe hf a variables [separated_space β] [complete_space β] lemma extension_unique (hf : uniform_continuous f) {g : completion α → β} (hg : uniform_continuous g) (h : ∀ a : α, f a = g (a : completion α)) : completion.extension f = g := cpkg.extend_unique hf hg h @[simp] lemma extension_comp_coe {f : completion α → β} (hf : uniform_continuous f) : completion.extension (f ∘ coe) = f := cpkg.extend_comp_coe hf end extension section map variables {f : α → β} /-- Completion functor acting on morphisms -/ protected def map (f : α → β) : completion α → completion β := cpkg.map cpkg f lemma uniform_continuous_map : uniform_continuous (completion.map f) := cpkg.uniform_continuous_map cpkg f lemma continuous_map : continuous (completion.map f) := cpkg.continuous_map cpkg f @[simp] lemma map_coe (hf : uniform_continuous f) (a : α) : (completion.map f) a = f a := cpkg.map_coe cpkg hf a lemma map_unique {f : α → β} {g : completion α → completion β} (hg : uniform_continuous g) (h : ∀a:α, ↑(f a) = g a) : completion.map f = g := cpkg.map_unique cpkg hg h @[simp] lemma map_id : completion.map (@id α) = id := cpkg.map_id lemma extension_map [complete_space γ] [separated_space γ] {f : β → γ} {g : α → β} (hf : uniform_continuous f) (hg : uniform_continuous g) : completion.extension f ∘ completion.map g = completion.extension (f ∘ g) := completion.ext (continuous_extension.comp continuous_map) continuous_extension $ by intro a; simp only [hg, hf, hf.comp hg, (∘), map_coe, extension_coe] lemma map_comp {g : β → γ} {f : α → β} (hg : uniform_continuous g) (hf : uniform_continuous f) : completion.map g ∘ completion.map f = completion.map (g ∘ f) := extension_map ((uniform_continuous_coe _).comp hg) hf end map /- In this section we construct isomorphisms between the completion of a uniform space and the completion of its separation quotient -/ section separation_quotient_completion /-- The isomorphism between the completion of a uniform space and the completion of its separation quotient. -/ def completion_separation_quotient_equiv (α : Type u) [uniform_space α] : completion (separation_quotient α) ≃ completion α := begin refine ⟨completion.extension (separation_quotient.lift (coe : α → completion α)), completion.map quotient.mk, _, _⟩, { assume a, refine induction_on a (is_closed_eq (continuous_map.comp continuous_extension) continuous_id) _, rintros ⟨a⟩, show completion.map quotient.mk (completion.extension (separation_quotient.lift coe) ↑⟦a⟧) = ↑⟦a⟧, rw [extension_coe (separation_quotient.uniform_continuous_lift _), separation_quotient.lift_mk (uniform_continuous_coe α), completion.map_coe uniform_continuous_quotient_mk] ; apply_instance }, { assume a, refine completion.induction_on a (is_closed_eq (continuous_extension.comp continuous_map) continuous_id) (λ a, _), rw [map_coe uniform_continuous_quotient_mk, extension_coe (separation_quotient.uniform_continuous_lift _), separation_quotient.lift_mk (uniform_continuous_coe α) _] ; apply_instance } end lemma uniform_continuous_completion_separation_quotient_equiv : uniform_continuous ⇑(completion_separation_quotient_equiv α) := uniform_continuous_extension lemma uniform_continuous_completion_separation_quotient_equiv_symm : uniform_continuous ⇑(completion_separation_quotient_equiv α).symm := uniform_continuous_map end separation_quotient_completion section extension₂ variables (f : α → β → γ) open function /-- Extend a two variable map to the Hausdorff completions. -/ protected def extension₂ (f : α → β → γ) : completion α → completion β → γ := cpkg.extend₂ cpkg f section separated_space variables [separated_space γ] {f} @[simp] lemma extension₂_coe_coe (hf : uniform_continuous₂ f) (a : α) (b : β) : completion.extension₂ f a b = f a b := cpkg.extension₂_coe_coe cpkg hf a b end separated_space variables [complete_space γ] (f) lemma uniform_continuous_extension₂ : uniform_continuous₂ (completion.extension₂ f) := cpkg.uniform_continuous_extension₂ cpkg f end extension₂ section map₂ open function /-- Lift a two variable map to the Hausdorff completions. -/ protected def map₂ (f : α → β → γ) : completion α → completion β → completion γ := cpkg.map₂ cpkg cpkg f lemma uniform_continuous_map₂ (f : α → β → γ) : uniform_continuous₂ (completion.map₂ f) := cpkg.uniform_continuous_map₂ cpkg cpkg f lemma continuous_map₂ {δ} [topological_space δ] {f : α → β → γ} {a : δ → completion α} {b : δ → completion β} (ha : continuous a) (hb : continuous b) : continuous (λd:δ, completion.map₂ f (a d) (b d)) := cpkg.continuous_map₂ cpkg cpkg ha hb lemma map₂_coe_coe (a : α) (b : β) (f : α → β → γ) (hf : uniform_continuous₂ f) : completion.map₂ f (a : completion α) (b : completion β) = f a b := cpkg.map₂_coe_coe cpkg cpkg a b f hf end map₂ end completion end uniform_space
afab7cdc86298a4848ed4cb125765fa3f22af3ef
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/analysis/normed_space/is_R_or_C.lean
b8d83b44849ba4c7ec6df50042334470222433f3
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
4,146
lean
/- Copyright (c) 2021 Kalle Kytölä. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kalle Kytölä -/ import data.complex.is_R_or_C import analysis.normed_space.operator_norm import analysis.normed_space.pointwise /-! # Normed spaces over R or C This file is about results on normed spaces over the fields `ℝ` and `ℂ`. ## Main definitions None. ## Main theorems * `continuous_linear_map.op_norm_bound_of_ball_bound`: A bound on the norms of values of a linear map in a ball yields a bound on the operator norm. ## Notes This file exists mainly to avoid importing `is_R_or_C` in the main normed space theory files. -/ open metric @[simp, is_R_or_C_simps] lemma is_R_or_C.norm_coe_norm {𝕜 : Type*} [is_R_or_C 𝕜] {E : Type*} [normed_group E] {z : E} : ∥(∥z∥ : 𝕜)∥ = ∥z∥ := by { unfold_coes, simp only [norm_algebra_map_eq, ring_hom.to_fun_eq_coe, norm_norm], } variables {𝕜 : Type*} [is_R_or_C 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] /-- Lemma to normalize a vector in a normed space `E` over either `ℂ` or `ℝ` to unit length. -/ @[simp] lemma norm_smul_inv_norm {x : E} (hx : x ≠ 0) : ∥(∥x∥⁻¹ : 𝕜) • x∥ = 1 := begin have : ∥x∥ ≠ 0 := by simp [hx], field_simp [norm_smul] end /-- Lemma to normalize a vector in a normed space `E` over either `ℂ` or `ℝ` to length `r`. -/ lemma norm_smul_inv_norm' {r : ℝ} (r_nonneg : 0 ≤ r) {x : E} (hx : x ≠ 0) : ∥(r * ∥x∥⁻¹ : 𝕜) • x∥ = r := begin have : ∥x∥ ≠ 0 := by simp [hx], field_simp [norm_smul, is_R_or_C.norm_eq_abs, r_nonneg] with is_R_or_C_simps end lemma linear_map.bound_of_sphere_bound {r : ℝ} (r_pos : 0 < r) (c : ℝ) (f : E →ₗ[𝕜] 𝕜) (h : ∀ z ∈ sphere (0 : E) r, ∥f z∥ ≤ c) (z : E) : ∥f z∥ ≤ c / r * ∥z∥ := begin by_cases z_zero : z = 0, { rw z_zero, simp only [linear_map.map_zero, norm_zero, mul_zero], }, set z₁ := (r * ∥z∥⁻¹ : 𝕜) • z with hz₁, have norm_f_z₁ : ∥f z₁∥ ≤ c, { apply h, rw mem_sphere_zero_iff_norm, exact norm_smul_inv_norm' r_pos.le z_zero }, have r_ne_zero : (r : 𝕜) ≠ 0 := (algebra_map ℝ 𝕜).map_ne_zero.mpr r_pos.ne.symm, have eq : f z = ∥z∥ / r * (f z₁), { rw [hz₁, linear_map.map_smul, smul_eq_mul], rw [← mul_assoc, ← mul_assoc, div_mul_cancel _ r_ne_zero, mul_inv_cancel, one_mul], simp only [z_zero, is_R_or_C.of_real_eq_zero, norm_eq_zero, ne.def, not_false_iff], }, rw [eq, normed_field.norm_mul, normed_field.norm_div, is_R_or_C.norm_coe_norm, is_R_or_C.norm_of_nonneg r_pos.le, div_mul_eq_mul_div, div_mul_eq_mul_div, mul_comm], apply div_le_div _ _ r_pos rfl.ge, { exact mul_nonneg ((norm_nonneg _).trans norm_f_z₁) (norm_nonneg z), }, apply mul_le_mul norm_f_z₁ rfl.le (norm_nonneg z) ((norm_nonneg _).trans norm_f_z₁), end /-- `linear_map.bound_of_ball_bound` is a version of this over arbitrary nondiscrete normed fields. It produces a less precise bound so we keep both versions. -/ lemma linear_map.bound_of_ball_bound' {r : ℝ} (r_pos : 0 < r) (c : ℝ) (f : E →ₗ[𝕜] 𝕜) (h : ∀ z ∈ closed_ball (0 : E) r, ∥f z∥ ≤ c) (z : E) : ∥f z∥ ≤ c / r * ∥z∥ := f.bound_of_sphere_bound r_pos c (λ z hz, h z hz.le) z lemma continuous_linear_map.op_norm_bound_of_ball_bound {r : ℝ} (r_pos : 0 < r) (c : ℝ) (f : E →L[𝕜] 𝕜) (h : ∀ z ∈ closed_ball (0 : E) r, ∥f z∥ ≤ c) : ∥f∥ ≤ c / r := begin apply continuous_linear_map.op_norm_le_bound, { apply div_nonneg _ r_pos.le, exact (norm_nonneg _).trans (h 0 (by simp only [norm_zero, mem_closed_ball, dist_zero_left, r_pos.le])), }, apply linear_map.bound_of_ball_bound' r_pos, exact λ z hz, h z hz, end variables (𝕜) include 𝕜 lemma normed_space.sphere_nonempty_is_R_or_C [nontrivial E] {r : ℝ} (hr : 0 ≤ r) : nonempty (sphere (0:E) r) := begin letI : normed_space ℝ E := normed_space.restrict_scalars ℝ 𝕜 E, exact (sphere (0:E) r).nonempty_coe_sort.mpr (normed_space.sphere_nonempty.mpr hr), end
f57e2f4c0b12c18767dd4a585df3bd96b48b9f37
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/tropical/basic.lean
419b1fc1af7c0a85f3bed5ee507797d7aaf6d694
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
16,243
lean
/- Copyright (c) 2021 Yakov Pechersky. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yakov Pechersky -/ import algebra.group_power.order import algebra.smul_with_zero /-! # Tropical algebraic structures This file defines algebraic structures of the (min-)tropical numbers, up to the tropical semiring. Some basic lemmas about conversion from the base type `R` to `tropical R` are provided, as well as the expected implementations of tropical addition and tropical multiplication. ## Main declarations * `tropical R`: The type synonym of the tropical interpretation of `R`. If `[linear_order R]`, then addition on `R` is via `min`. * `semiring (tropical R)`: A `linear_ordered_add_comm_monoid_with_top R` induces a `semiring (tropical R)`. If one solely has `[linear_ordered_add_comm_monoid R]`, then the "tropicalization of `R`" would be `tropical (with_top R)`. ## Implementation notes The tropical structure relies on `has_top` and `min`. For the max-tropical numbers, use `order_dual R`. Inspiration was drawn from the implementation of `additive`/`multiplicative`/`opposite`, where a type synonym is created with some barebones API, and quickly made irreducible. Algebraic structures are provided with as few typeclass assumptions as possible, even though most references rely on `semiring (tropical R)` for building up the whole theory. ## References followed * https://arxiv.org/pdf/math/0408099.pdf * https://www.mathenjeans.fr/sites/default/files/sujets/tropical_geometry_-_casagrande.pdf -/ universes u v variables (R : Type u) /-- The tropicalization of a type `R`. -/ def tropical : Type u := R variables {R} namespace tropical /-- Reinterpret `x : R` as an element of `tropical R`. See `tropical.trop_equiv` for the equivalence. -/ @[pp_nodot] def trop : R → tropical R := id /-- Reinterpret `x : tropical R` as an element of `R`. See `tropical.trop_equiv` for the equivalence. -/ @[pp_nodot] def untrop : tropical R → R := id lemma trop_injective : function.injective (trop : R → tropical R) := λ _ _, id lemma untrop_injective : function.injective (untrop : tropical R → R) := λ _ _, id @[simp] lemma trop_inj_iff (x y : R) : trop x = trop y ↔ x = y := iff.rfl @[simp] lemma untrop_inj_iff (x y : tropical R) : untrop x = untrop y ↔ x = y := iff.rfl @[simp] lemma trop_untrop (x : tropical R) : trop (untrop x) = x := rfl @[simp] lemma untrop_trop (x : R) : untrop (trop x) = x := rfl lemma left_inverse_trop : function.left_inverse (trop : R → tropical R) untrop := trop_untrop lemma right_inverse_trop : function.right_inverse (trop : R → tropical R) untrop := trop_untrop attribute [irreducible] tropical /-- Reinterpret `x : R` as an element of `tropical R`. See `tropical.trop_order_iso` for the order-preserving equivalence. -/ def trop_equiv : R ≃ tropical R := { to_fun := trop, inv_fun := untrop, left_inv := untrop_trop, right_inv := trop_untrop } @[simp] lemma trop_equiv_coe_fn : (trop_equiv : R → tropical R) = trop := rfl @[simp] lemma trop_equiv_symm_coe_fn : (trop_equiv.symm : tropical R → R) = untrop := rfl lemma trop_eq_iff_eq_untrop {x : R} {y} : trop x = y ↔ x = untrop y := trop_equiv.apply_eq_iff_eq_symm_apply lemma untrop_eq_iff_eq_trop {x} {y : R} : untrop x = y ↔ x = trop y := trop_equiv.symm.apply_eq_iff_eq_symm_apply lemma injective_trop : function.injective (trop : R → tropical R) := trop_equiv.injective lemma injective_untrop : function.injective (untrop : tropical R → R) := trop_equiv.symm.injective lemma surjective_trop : function.surjective (trop : R → tropical R) := trop_equiv.surjective lemma surjective_untrop : function.surjective (untrop : tropical R → R) := trop_equiv.symm.surjective instance [inhabited R] : inhabited (tropical R) := ⟨trop default⟩ /-- Recursing on a `x' : tropical R` is the same as recursing on an `x : R` reinterpreted as a term of `tropical R` via `trop x`. -/ @[simp] def trop_rec {F : Π (X : tropical R), Sort v} (h : Π X, F (trop X)) : Π X, F X := λ X, h (untrop X) instance [decidable_eq R] : decidable_eq (tropical R) := λ x y, decidable_of_iff _ injective_untrop.eq_iff section order instance [has_le R] : has_le (tropical R) := { le := λ x y, untrop x ≤ untrop y } @[simp] lemma untrop_le_iff [has_le R] {x y : tropical R} : untrop x ≤ untrop y ↔ x ≤ y := iff.rfl instance decidable_le [has_le R] [decidable_rel ((≤) : R → R → Prop)] : decidable_rel ((≤) : tropical R → tropical R → Prop) := λ x y, ‹decidable_rel (≤)› (untrop x) (untrop y) instance [has_lt R] : has_lt (tropical R) := { lt := λ x y, untrop x < untrop y } @[simp] lemma untrop_lt_iff [has_lt R] {x y : tropical R} : untrop x < untrop y ↔ x < y := iff.rfl instance decidable_lt [has_lt R] [decidable_rel ((<) : R → R → Prop)] : decidable_rel ((<) : tropical R → tropical R → Prop) := λ x y, ‹decidable_rel (<)› (untrop x) (untrop y) instance [preorder R] : preorder (tropical R) := { le_refl := λ _, le_rfl, le_trans := λ _ _ _ h h', le_trans h h', lt_iff_le_not_le := λ _ _, lt_iff_le_not_le, ..tropical.has_le, ..tropical.has_lt } /-- Reinterpret `x : R` as an element of `tropical R`, preserving the order. -/ def trop_order_iso [preorder R] : R ≃o tropical R := { map_rel_iff' := λ _ _, untrop_le_iff, ..trop_equiv } @[simp] lemma trop_order_iso_coe_fn [preorder R] : (trop_order_iso : R → tropical R) = trop := rfl @[simp] lemma trop_order_iso_symm_coe_fn [preorder R] : (trop_order_iso.symm : tropical R → R) = untrop := rfl lemma trop_monotone [preorder R] : monotone (trop : R → tropical R) := λ _ _, id lemma untrop_monotone [preorder R] : monotone (untrop : tropical R → R) := λ _ _, id instance [partial_order R] : partial_order (tropical R) := { le_antisymm := λ _ _ h h', untrop_injective (le_antisymm h h'), ..tropical.preorder } instance [has_top R] : has_zero (tropical R) := ⟨trop ⊤⟩ instance [has_top R] : has_top (tropical R) := ⟨0⟩ @[simp] lemma untrop_zero [has_top R] : untrop (0 : tropical R) = ⊤ := rfl @[simp] lemma trop_top [has_top R] : trop (⊤ : R) = 0 := rfl @[simp] lemma trop_coe_ne_zero (x : R) : trop (x : with_top R) ≠ 0 . @[simp] lemma zero_ne_trop_coe (x : R) : (0 : tropical (with_top R)) ≠ trop x . @[simp] lemma le_zero [has_le R] [order_top R] (x : tropical R) : x ≤ 0 := le_top instance [has_le R] [order_top R] : order_top (tropical R) := { le_top := λ _, le_top, ..tropical.has_top } variable [linear_order R] /-- Tropical addition is the minimum of two underlying elements of `R`. -/ instance : has_add (tropical R) := ⟨λ x y, trop (min (untrop x) (untrop y))⟩ instance : add_comm_semigroup (tropical R) := { add := (+), add_assoc := λ _ _ _, untrop_injective (min_assoc _ _ _), add_comm := λ _ _, untrop_injective (min_comm _ _) } @[simp] lemma untrop_add (x y : tropical R) : untrop (x + y) = min (untrop x) (untrop y) := rfl @[simp] lemma trop_min (x y : R) : trop (min x y) = trop x + trop y := rfl @[simp] lemma trop_inf (x y : R) : trop (x ⊓ y) = trop x + trop y := rfl lemma trop_add_def (x y : tropical R) : x + y = trop (min (untrop x) (untrop y)) := rfl instance : linear_order (tropical R) := { le_total := λ a b, le_total (untrop a) (untrop b), decidable_le := tropical.decidable_le, decidable_lt := tropical.decidable_lt, decidable_eq := tropical.decidable_eq, max := λ a b, trop (max (untrop a) (untrop b)), max_def := begin ext x y, rw [max_default, max_def, apply_ite trop, trop_untrop, trop_untrop, if_congr untrop_le_iff rfl rfl], end, min := (+), min_def := begin ext x y, rw [trop_add_def, min_default, min_def, apply_ite trop, trop_untrop, trop_untrop, if_congr untrop_le_iff rfl rfl], end, ..tropical.partial_order } @[simp] lemma untrop_sup (x y : tropical R) : untrop (x ⊔ y) = untrop x ⊔ untrop y := rfl @[simp] lemma untrop_max (x y : tropical R) : untrop (max x y) = max (untrop x) (untrop y) := rfl @[simp] lemma min_eq_add : (min : tropical R → tropical R → tropical R) = (+) := rfl @[simp] lemma inf_eq_add : ((⊓) : tropical R → tropical R → tropical R) = (+) := rfl lemma trop_max_def (x y : tropical R) : max x y = trop (max (untrop x) (untrop y)) := rfl lemma trop_sup_def (x y : tropical R) : x ⊔ y = trop (untrop x ⊔ untrop y) := rfl @[simp] lemma add_eq_left ⦃x y : tropical R⦄ (h : x ≤ y) : x + y = x := untrop_injective (by simpa using h) @[simp] lemma add_eq_right ⦃x y : tropical R⦄ (h : y ≤ x) : x + y = y := untrop_injective (by simpa using h) lemma add_eq_left_iff {x y : tropical R} : x + y = x ↔ x ≤ y := by rw [trop_add_def, trop_eq_iff_eq_untrop, ←untrop_le_iff, min_eq_left_iff] lemma add_eq_right_iff {x y : tropical R} : x + y = y ↔ y ≤ x := by rw [trop_add_def, trop_eq_iff_eq_untrop, ←untrop_le_iff, min_eq_right_iff] @[simp] lemma add_self (x : tropical R) : x + x = x := untrop_injective (min_eq_right le_rfl) @[simp] lemma bit0 (x : tropical R) : bit0 x = x := add_self x lemma add_eq_iff {x y z : tropical R} : x + y = z ↔ x = z ∧ x ≤ y ∨ y = z ∧ y ≤ x := by { rw [trop_add_def, trop_eq_iff_eq_untrop], simp [min_eq_iff] } @[simp] lemma add_eq_zero_iff {a b : tropical (with_top R)} : a + b = 0 ↔ a = 0 ∧ b = 0 := begin rw add_eq_iff, split, { rintro (⟨rfl, h⟩|⟨rfl, h⟩), { exact ⟨rfl, le_antisymm (le_zero _) h⟩ }, { exact ⟨le_antisymm (le_zero _) h, rfl⟩ } }, { rintro ⟨rfl, rfl⟩, simp } end instance [order_top R] : add_comm_monoid (tropical R) := { zero_add := λ _, untrop_injective (min_top_left _), add_zero := λ _, untrop_injective (min_top_right _), ..tropical.has_zero, ..tropical.add_comm_semigroup } end order section monoid /-- Tropical multiplication is the addition in the underlying `R`. -/ instance [has_add R] : has_mul (tropical R) := ⟨λ x y, trop (untrop x + untrop y)⟩ @[simp] lemma trop_add [has_add R] (x y : R) : trop (x + y) = trop x * trop y := rfl @[simp] lemma untrop_mul [has_add R] (x y : tropical R) : untrop (x * y) = untrop x + untrop y := rfl lemma trop_mul_def [has_add R] (x y : tropical R) : x * y = trop (untrop x + untrop y) := rfl instance [has_zero R] : has_one (tropical R) := ⟨trop 0⟩ @[simp] lemma trop_zero [has_zero R] : trop (0 : R) = 1 := rfl @[simp] lemma untrop_one [has_zero R] : untrop (1 : tropical R) = 0 := rfl instance [linear_order R] [order_top R] [has_zero R] : add_monoid_with_one (tropical R) := { nat_cast := λ n, if n = 0 then 0 else 1, nat_cast_zero := rfl, nat_cast_succ := λ n, (untrop_inj_iff _ _).1 (by cases n; simp [nat.cast]), .. tropical.has_one, .. tropical.add_comm_monoid } instance [has_zero R] : nontrivial (tropical (with_top R)) := ⟨⟨0, 1, trop_injective.ne with_top.top_ne_coe⟩⟩ instance [has_neg R] : has_inv (tropical R) := ⟨λ x, trop (- untrop x)⟩ @[simp] lemma untrop_inv [has_neg R] (x : tropical R) : untrop x⁻¹ = - untrop x := rfl instance [has_sub R] : has_div (tropical R) := ⟨λ x y, trop (untrop x - untrop y)⟩ @[simp] lemma untrop_div [has_sub R] (x y : tropical R) : untrop (x / y) = untrop x - untrop y := rfl instance [add_semigroup R] : semigroup (tropical R) := { mul := (*), mul_assoc := λ _ _ _, untrop_injective (add_assoc _ _ _) } instance [add_comm_semigroup R] : comm_semigroup (tropical R) := { mul_comm := λ _ _, untrop_injective (add_comm _ _), ..tropical.semigroup } instance {α : Type*} [has_smul α R] : has_pow (tropical R) α := { pow := λ x n, trop $ n • untrop x } @[simp] lemma untrop_pow {α : Type*} [has_smul α R] (x : tropical R) (n : α) : untrop (x ^ n) = n • untrop x := rfl @[simp] lemma trop_smul {α : Type*} [has_smul α R] (x : R) (n : α) : trop (n • x) = trop x ^ n := rfl instance [add_zero_class R] : mul_one_class (tropical R) := { one := 1, mul := (*), one_mul := λ _, untrop_injective $ zero_add _, mul_one := λ _, untrop_injective $ add_zero _ } instance [add_monoid R] : monoid (tropical R) := { npow := λ n x, x ^ n, npow_zero' := λ _, untrop_injective $ zero_smul _ _, npow_succ' := λ _ _, untrop_injective $ succ_nsmul _ _, ..tropical.mul_one_class, ..tropical.semigroup } @[simp] lemma trop_nsmul [add_monoid R] (x : R) (n : ℕ) : trop (n • x) = trop x ^ n := rfl instance [add_comm_monoid R] : comm_monoid (tropical R) := { ..tropical.monoid, ..tropical.comm_semigroup } instance [add_group R] : group (tropical R) := { inv := has_inv.inv, mul_left_inv := λ _, untrop_injective $ add_left_neg _, zpow := λ n x, trop $ n • untrop x, zpow_zero' := λ _, untrop_injective $ zero_zsmul _, zpow_succ' := λ _ _, untrop_injective $ add_group.zsmul_succ' _ _, zpow_neg' := λ _ _, untrop_injective $ add_group.zsmul_neg' _ _, ..tropical.monoid } instance [add_comm_group R] : comm_group (tropical R) := { mul_comm := λ _ _, untrop_injective (add_comm _ _), ..tropical.group } @[simp] lemma untrop_zpow [add_group R] (x : tropical R) (n : ℤ) : untrop (x ^ n) = n • untrop x := rfl @[simp] lemma trop_zsmul [add_group R] (x : R) (n : ℤ) : trop (n • x) = trop x ^ n := rfl end monoid section distrib instance covariant_mul [has_le R] [has_add R] [covariant_class R R (+) (≤)] : covariant_class (tropical R) (tropical R) (*) (≤) := ⟨λ x y z h, add_le_add_left h _⟩ instance covariant_swap_mul [has_le R] [has_add R] [covariant_class R R (function.swap (+)) (≤)] : covariant_class (tropical R) (tropical R) (function.swap (*)) (≤) := ⟨λ x y z h, add_le_add_right h _⟩ instance covariant_add [linear_order R] : covariant_class (tropical R) (tropical R) (+) (≤) := ⟨λ x y z h, begin cases le_total x y with hx hy, { rw [add_eq_left hx, add_eq_left (hx.trans h)] }, { rw [add_eq_right hy], cases le_total x z with hx hx, { rwa [add_eq_left hx] }, { rwa [add_eq_right hx] } } end⟩ instance covariant_mul_lt [has_lt R] [has_add R] [covariant_class R R (+) (<)] : covariant_class (tropical R) (tropical R) (*) (<) := ⟨λ x y z h, add_lt_add_left h _⟩ instance covariant_swap_mul_lt [preorder R] [has_add R] [covariant_class R R (function.swap (+)) (<)] : covariant_class (tropical R) (tropical R) (function.swap (*)) (<) := ⟨λ x y z h, add_lt_add_right h _⟩ instance [linear_order R] [has_add R] [covariant_class R R (+) (≤)] [covariant_class R R (function.swap (+)) (≤)] : distrib (tropical R) := { mul := (*), add := (+), left_distrib := λ _ _ _, untrop_injective (min_add_add_left _ _ _).symm, right_distrib := λ _ _ _, untrop_injective (min_add_add_right _ _ _).symm } @[simp] lemma add_pow [linear_order R] [add_monoid R] [covariant_class R R (+) (≤)] [covariant_class R R (function.swap (+)) (≤)] (x y : tropical R) (n : ℕ) : (x + y) ^ n = x ^ n + y ^ n := begin cases le_total x y with h h, { rw [add_eq_left h, add_eq_left (pow_le_pow_of_le_left' h _)] }, { rw [add_eq_right h, add_eq_right (pow_le_pow_of_le_left' h _)] } end end distrib section semiring variable [linear_ordered_add_comm_monoid_with_top R] instance : comm_semiring (tropical R) := { zero_mul := λ _, untrop_injective (top_add _), mul_zero := λ _, untrop_injective (add_top _), ..tropical.add_monoid_with_one, ..tropical.distrib, ..tropical.add_comm_monoid, ..tropical.comm_monoid } @[simp] lemma succ_nsmul {R} [linear_order R] [order_top R] (x : tropical R) (n : ℕ) : (n + 1) • x = x := begin induction n with n IH, { simp }, { rw [add_nsmul, IH, one_nsmul, add_self] } end -- TODO: find/create the right classes to make this hold (for enat, ennreal, etc) -- Requires `zero_eq_bot` to be true -- lemma add_eq_zero_iff {a b : tropical R} : -- a + b = 1 ↔ a = 1 ∨ b = 1 := sorry @[simp] lemma mul_eq_zero_iff {R : Type*} [linear_ordered_add_comm_monoid R] {a b : tropical (with_top R)} : a * b = 0 ↔ a = 0 ∨ b = 0 := by simp [←untrop_inj_iff, with_top.add_eq_top] instance {R : Type*} [linear_ordered_add_comm_monoid R] : no_zero_divisors (tropical (with_top R)) := ⟨λ _ _, mul_eq_zero_iff.mp⟩ end semiring end tropical
45a6f9dfb26dd5afd8ea48d451ae2ec42115d21f
649957717d58c43b5d8d200da34bf374293fe739
/src/topology/algebra/TopCommRing/basic.lean
4b778b5b479e096050806c805f4e7ec322231b67
[ "Apache-2.0" ]
permissive
Vtec234/mathlib
b50c7b21edea438df7497e5ed6a45f61527f0370
fb1848bbbfce46152f58e219dc0712f3289d2b20
refs/heads/master
1,592,463,095,113
1,562,737,749,000
1,562,737,749,000
196,202,858
0
0
Apache-2.0
1,562,762,338,000
1,562,762,337,000
null
UTF-8
Lean
false
false
2,785
lean
-- Copyright (c) 2019 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison import algebra.CommRing.basic import topology.Top.basic import topology.instances.complex universes u open category_theory structure TopCommRing := (α : Type u) [is_comm_ring : comm_ring α] [is_topological_space : topological_space α] [is_topological_ring : topological_ring α] namespace TopCommRing instance : has_coe_to_sort TopCommRing := { S := Type u, coe := TopCommRing.α } attribute [instance] is_comm_ring is_topological_space is_topological_ring instance TopCommRing_category : category TopCommRing := { hom := λ R S, {f : R → S // is_ring_hom f ∧ continuous f }, id := λ R, ⟨id, by obviously⟩, comp := λ R S T f g, ⟨g.val ∘ f.val, begin -- TODO automate cases f, cases g, cases f_property, cases g_property, split, dsimp, resetI, apply_instance, dsimp, apply continuous.comp ; assumption end⟩ }. def of (X : Type u) [comm_ring X] [topological_space X] [topological_ring X] : TopCommRing := ⟨X⟩ noncomputable example : TopCommRing := TopCommRing.of ℚ noncomputable example : TopCommRing := TopCommRing.of ℝ noncomputable example : TopCommRing := TopCommRing.of ℂ /-- The forgetful functor to CommRing. -/ def forget_to_CommRing : TopCommRing ⥤ CommRing := { obj := λ R, { α := R }, map := λ R S f, ⟨ f.1, f.2.left ⟩ } instance forget_to_CommRing_faithful : faithful (forget_to_CommRing) := by tidy instance forget_to_CommRing_topological_space (R : TopCommRing) : topological_space (forget_to_CommRing.obj R) := R.is_topological_space /-- The forgetful functor to Top. -/ def forget_to_Top : TopCommRing ⥤ Top := { obj := λ R, { α := R }, map := λ R S f, ⟨ f.1, f.2.right ⟩ } instance forget_to_Top_faithful : faithful (forget_to_Top) := {} instance forget_to_Top_comm_ring (R : TopCommRing) : comm_ring (forget_to_Top.obj R) := R.is_comm_ring instance forget_to_Top_topological_ring (R : TopCommRing) : topological_ring (forget_to_Top.obj R) := R.is_topological_ring def forget : TopCommRing ⥤ Type u := { obj := λ R, R, map := λ R S f, f.1 } instance forget_faithful : faithful forget := {} instance forget_topological_space (R : TopCommRing) : topological_space (forget.obj R) := R.is_topological_space instance forget_comm_ring (R : TopCommRing) : comm_ring (forget.obj R) := R.is_comm_ring instance forget_topological_ring (R : TopCommRing) : topological_ring (forget.obj R) := R.is_topological_ring def forget_to_Type_via_Top : forget_to_Top ⋙ Top.forget ≅ forget := iso.refl _ def forget_to_Type_via_CommRing : forget_to_CommRing ⋙ CommRing.forget ≅ forget := iso.refl _ end TopCommRing
111a6566f00c61606f53844dcc52cc872e5fd746
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/linear_algebra/clifford_algebra/basic.lean
13979cce70b16bbe77fa58e7c4e7fcea022fc0f2
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,491
lean
/- Copyright (c) 2020 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser, Utensil Song -/ import algebra.ring_quot import linear_algebra.tensor_algebra import linear_algebra.exterior_algebra import linear_algebra.quadratic_form /-! # Clifford Algebras We construct the Clifford algebra of a module `M` over a commutative ring `R`, equipped with a quadratic_form `Q`. ## Notation The Clifford algebra of the `R`-module `M` equipped with a quadratic_form `Q` is denoted as `clifford_algebra Q`. Given a linear morphism `f : M → A` from a semimodule `M` to another `R`-algebra `A`, such that `cond : ∀ m, f m * f m = algebra_map _ _ (Q m)`, there is a (unique) lift of `f` to an `R`-algebra morphism, which is denoted `clifford_algebra.lift Q f cond`. The canonical linear map `M → clifford_algebra Q` is denoted `clifford_algebra.ι Q`. ## Theorems The main theorems proved ensure that `clifford_algebra Q` satisfies the universal property of the Clifford algebra. 1. `ι_comp_lift` is the fact that the composition of `ι Q` with `lift Q f cond` agrees with `f`. 2. `lift_unique` ensures the uniqueness of `lift Q f cond` with respect to 1. Additionally, when `Q = 0` an `alg_equiv` to the `exterior_algebra` is provided as `as_exterior`. ## Implementation details The Clifford algebra of `M` is constructed as a quotient of the tensor algebra, as follows. 1. We define a relation `clifford_algebra.rel Q` on `tensor_algebra R M`. This is the smallest relation which identifies squares of elements of `M` with `Q m`. 2. The Clifford algebra is the quotient of the tensor algebra by this relation. This file is almost identical to `linear_algebra/exterior_algebra.lean`. -/ variables {R : Type*} [comm_ring R] variables {M : Type*} [add_comm_group M] [module R M] variables (Q : quadratic_form R M) variable {n : ℕ} namespace clifford_algebra open tensor_algebra /-- `rel` relates each `ι m * ι m`, for `m : M`, with `Q m`. The Clifford algebra of `M` is defined as the quotient modulo this relation. -/ inductive rel : tensor_algebra R M → tensor_algebra R M → Prop | of (m : M) : rel (ι R m * ι R m) (algebra_map R _ (Q m)) end clifford_algebra /-- The Clifford algebra of an `R`-module `M` equipped with a quadratic_form `Q`. -/ @[derive [inhabited, ring, algebra R]] def clifford_algebra := ring_quot (clifford_algebra.rel Q) namespace clifford_algebra /-- The canonical linear map `M →ₗ[R] clifford_algebra Q`. -/ def ι : M →ₗ[R] clifford_algebra Q := (ring_quot.mk_alg_hom R _).to_linear_map.comp (tensor_algebra.ι R) /-- As well as being linear, `ι Q` squares to the quadratic form -/ @[simp] theorem ι_square_scalar (m : M) : ι Q m * ι Q m = algebra_map R _ (Q m) := begin erw [←alg_hom.map_mul, ring_quot.mk_alg_hom_rel R (rel.of m), alg_hom.commutes], refl, end variables {Q} {A : Type*} [semiring A] [algebra R A] @[simp] theorem comp_ι_square_scalar (g : clifford_algebra Q →ₐ[R] A) (m : M) : g (ι Q m) * g (ι Q m) = algebra_map _ _ (Q m) := by rw [←alg_hom.map_mul, ι_square_scalar, alg_hom.commutes] variables (Q) /-- Given a linear map `f : M →ₗ[R] A` into an `R`-algebra `A`, which satisfies the condition: `cond : ∀ m : M, f m * f m = Q(m)`, this is the canonical lift of `f` to a morphism of `R`-algebras from `clifford_algebra Q` to `A`. -/ @[simps symm_apply] def lift : {f : M →ₗ[R] A // ∀ m, f m * f m = algebra_map _ _ (Q m)} ≃ (clifford_algebra Q →ₐ[R] A) := { to_fun := λ f, ring_quot.lift_alg_hom R ⟨tensor_algebra.lift R (f : M →ₗ[R] A), (λ x y (h : rel Q x y), by { induction h, rw [alg_hom.commutes, alg_hom.map_mul, tensor_algebra.lift_ι_apply, f.prop], })⟩, inv_fun := λ F, ⟨F.to_linear_map.comp (ι Q), λ m, by rw [ linear_map.comp_apply, alg_hom.to_linear_map_apply, comp_ι_square_scalar]⟩, left_inv := λ f, by { ext, simp [ι] }, right_inv := λ F, by { ext, simp [ι] } } variables {Q} @[simp] theorem ι_comp_lift (f : M →ₗ[R] A) (cond : ∀ m, f m * f m = algebra_map _ _ (Q m)) : (lift Q ⟨f, cond⟩).to_linear_map.comp (ι Q) = f := (subtype.mk_eq_mk.mp $ (lift Q).symm_apply_apply ⟨f, cond⟩) @[simp] theorem lift_ι_apply (f : M →ₗ[R] A) (cond : ∀ m, f m * f m = algebra_map _ _ (Q m)) (x) : lift Q ⟨f, cond⟩ (ι Q x) = f x := (linear_map.ext_iff.mp $ ι_comp_lift f cond) x @[simp] theorem lift_unique (f : M →ₗ[R] A) (cond : ∀ m : M, f m * f m = algebra_map _ _ (Q m)) (g : clifford_algebra Q →ₐ[R] A) : g.to_linear_map.comp (ι Q) = f ↔ g = lift Q ⟨f, cond⟩ := begin convert (lift Q).symm_apply_eq, rw lift_symm_apply, simp only, end attribute [irreducible] clifford_algebra ι lift @[simp] theorem lift_comp_ι (g : clifford_algebra Q →ₐ[R] A) : lift Q ⟨g.to_linear_map.comp (ι Q), comp_ι_square_scalar _⟩ = g := begin convert (lift Q).apply_symm_apply g, rw lift_symm_apply, refl, end /-- See note [partially-applied ext lemmas]. -/ @[ext] theorem hom_ext {A : Type*} [semiring A] [algebra R A] {f g : clifford_algebra Q →ₐ[R] A} : f.to_linear_map.comp (ι Q) = g.to_linear_map.comp (ι Q) → f = g := begin intro h, apply (lift Q).symm.injective, rw [lift_symm_apply, lift_symm_apply], simp only [h], end /-- If `C` holds for the `algebra_map` of `r : R` into `clifford_algebra Q`, the `ι` of `x : M`, and is preserved under addition and muliplication, then it holds for all of `clifford_algebra Q`. -/ -- This proof closely follows `tensor_algebra.induction` @[elab_as_eliminator] lemma induction {C : clifford_algebra Q → Prop} (h_grade0 : ∀ r, C (algebra_map R (clifford_algebra Q) r)) (h_grade1 : ∀ x, C (ι Q x)) (h_mul : ∀ a b, C a → C b → C (a * b)) (h_add : ∀ a b, C a → C b → C (a + b)) (a : clifford_algebra Q) : C a := begin -- the arguments are enough to construct a subalgebra, and a mapping into it from M let s : subalgebra R (clifford_algebra Q) := { carrier := C, mul_mem' := h_mul, add_mem' := h_add, algebra_map_mem' := h_grade0, }, let of : { f : M →ₗ[R] s // ∀ m, f m * f m = algebra_map _ _ (Q m) } := ⟨(ι Q).cod_restrict s.to_submodule h_grade1, λ m, subtype.eq $ ι_square_scalar Q m ⟩, -- the mapping through the subalgebra is the identity have of_id : alg_hom.id R (clifford_algebra Q) = s.val.comp (lift Q of), { ext, simp [of], }, -- finding a proof is finding an element of the subalgebra convert subtype.prop (lift Q of a), exact alg_hom.congr_fun of_id a, end /-- A Clifford algebra with a zero quadratic form is isomorphic to an `exterior_algebra` -/ def as_exterior : clifford_algebra (0 : quadratic_form R M) ≃ₐ[R] exterior_algebra R M := alg_equiv.of_alg_hom (clifford_algebra.lift 0 ⟨(exterior_algebra.ι R), by simp⟩) (exterior_algebra.lift R ⟨(ι (0 : quadratic_form R M)), by simp⟩) (by { ext, simp, }) (by { ext, simp, }) end clifford_algebra namespace tensor_algebra variables {Q} /-- The canonical image of the `tensor_algebra` in the `clifford_algebra`, which maps `tensor_algebra.ι R x` to `clifford_algebra.ι Q x`. -/ def to_clifford : tensor_algebra R M →ₐ[R] clifford_algebra Q := tensor_algebra.lift R (clifford_algebra.ι Q) @[simp] lemma to_clifford_ι (m : M) : (tensor_algebra.ι R m).to_clifford = clifford_algebra.ι Q m := by simp [to_clifford] end tensor_algebra
eed46eb49308e5f547d90abc2753acddc0f704ab
57c233acf9386e610d99ed20ef139c5f97504ba3
/test/induction.lean
53587615dd6cc740f6a32f1b5c55b88a09e16d1e
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
37,951
lean
import tactic.induction import tactic.linarith universes u v w -------------------------------------------------------------------------------- -- Setup: Some Inductive Types -------------------------------------------------------------------------------- inductive le : ℕ → ℕ → Type | zero {n} : le 0 n | succ {n m} : le n m → le (n + 1) (m + 1) inductive lt : ℕ → ℕ → Type | zero {n} : lt 0 (n + 1) | succ {n m} : lt n m → lt (n + 1) (m + 1) inductive Fin : ℕ → Type | zero {n} : Fin (n + 1) | succ {n} : Fin n → Fin (n + 1) inductive List (α : Sort*) : Sort* | nil {} : List | cons {} (x : α) (xs : List) : List namespace List def append {α} : List α → List α → List α | nil ys := ys | (cons x xs) ys := cons x (append xs ys) end List inductive Vec (α : Sort u) : ℕ → Sort (max 1 u) | nil : Vec 0 | cons {n} : α → Vec n → Vec (n + 1) namespace Vec inductive eq {α} : ∀ n m, Vec α n → Vec α m → Prop | nil : eq 0 0 nil nil | cons {n m} {xs : Vec α n} {ys : Vec α m} {x y : α} : x = y → eq n m xs ys → eq (n + 1) (m + 1) (cons x xs) (cons y ys) end Vec inductive Two : Type | zero | one inductive ℕ' : Type | intro : ℕ → ℕ' -------------------------------------------------------------------------------- -- Unit Tests -------------------------------------------------------------------------------- example (k) : 0 + k = k := begin induction' k, { refl }, { simp } end example {k} (fk : Fin k) : Fin (k + 1) := begin induction' fk, { exact Fin.zero }, { exact Fin.succ ih } end example {α} (l : List α) : l.append List.nil = l := begin induction' l, { refl }, { dsimp only [List.append], exact (congr_arg _ ih) } end example {k l} (h : lt k l) : le k l := begin induction' h, { exact le.zero }, { exact le.succ ih } end example {k l} : lt k l → le k l := begin induction' k; induction' l; intro hlt, { cases' hlt }, { exact le.zero }, { cases' hlt }, { cases' hlt, exact le.succ (@ih m hlt), } end example {α n m} {xs : Vec α n} {ys : Vec α m} (h : Vec.eq n m xs ys) : n = m := begin induction' h, case nil { refl }, case cons { exact congr_arg nat.succ ih, } end -- A simple induction with complex index arguments. example {k} (h : lt (k + 1) k) : false := begin induction' h, { exact ih } end -- A more complex induction with complex index arguments. Note the dependencies -- between index arguments. example {α : Sort u} {x y n m} {xs : Vec α n} {ys : Vec α m} : Vec.eq (n + 1) (m + 1) (Vec.cons x xs) (Vec.cons y ys) → Vec.eq n m xs ys := begin intro h, induction' h, exact h_1 end -- It also works with cases'. example {α : Sort u} {x y n m} {xs : Vec α n} {ys : Vec α m} : Vec.eq (n + 1) (m + 1) (Vec.cons x xs) (Vec.cons y ys) → Vec.eq n m xs ys := begin intro h, cases' h, exact h_1 end -- This example requires elimination of cyclic generalised index equations. example (n : ℕ) (h : n = n + 3) : false := begin success_if_fail { cases h }, induction' h end -- It also works with cases'. example (n : ℕ) (h : n = n + 3) : false := begin success_if_fail { cases h }, cases' h end -- This example used to fail because it involves a nested inductive as a complex -- index. inductive rose₁ : Type | tip : rose₁ | node : list rose₁ → rose₁ example (rs) (h : rose₁.tip = rose₁.node rs) : false := begin cases' h end -- This example checks whether we can deal with infinitely branching inductive -- types. inductive inf_tree (α : Type) : Type | tip : inf_tree | node (a : α) (f : ∀ (n : ℕ), inf_tree) : inf_tree namespace inf_tree inductive all {α} (P : α → Prop) : inf_tree α → Prop | tip : all tip | node {a} {f : ℕ → inf_tree α} : P a → (∀ n, all (f n)) → all (node a f) example {α} (t : inf_tree α) : all (λ _, true) t := begin induction' t, { exact all.tip }, { exact all.node trivial ih } end end inf_tree -- This example tests type-based naming. example (k : ℕ') (i : ℕ') : ℕ := begin induction' k, induction' i, exact (n + m) end -- For constructor arguments that are propositions, the default name is "h". -- For non-propositions, it is "x". inductive nat_or_positive | nat : ℕ' → nat_or_positive | positive (n : ℕ) : n > 0 → nat_or_positive example (n : nat_or_positive) : unit := begin cases' n, case nat { guard_hyp x : ℕ', exact () }, case positive { guard_hyp n : ℕ, guard_hyp h : n > 0, exact () } end -- By default, induction' generates the most general possible induction -- hypothesis. example {n m : ℕ} : n + m = m + n := begin induction' m, case zero { simp }, case succ : k IH { guard_hyp k : ℕ, guard_hyp n : ℕ, guard_hyp IH : ∀ {n}, n + k = k + n, ac_refl } end -- Here's an example where this is more useful. example {n m : ℕ} (h : n + n = m + m) : n = m := begin induction' n with n ih, case zero { cases' m, { refl }, { cases' h } }, case succ { cases' m, { cases' h }, { rw @ih m, simp only [nat.succ_eq_add_one] at h, replace h : n + n + 2 = m + m + 2 := by linarith, injections } } end -- If we don't want a hypothesis to be generalised, we can say so with a -- "fixing" clause. example {n m : ℕ} : n + m = m + n := begin induction' m fixing n, case zero { simp }, case succ : k IH { guard_hyp k : ℕ, guard_hyp n : ℕ, guard_hyp IH : n + k = k + n, ac_refl } end -- We can also fix all hypotheses. This gives us the behaviour of stock -- `induction`. Hypotheses which depend on the major premise (or its index -- arguments) still get generalised. example {n m k : ℕ} (h : n + m = k) : n + m = k := begin induction' n fixing *, case zero { simp [*] }, case succ : n IH { guard_hyp n : ℕ, guard_hyp m : ℕ, guard_hyp k : ℕ, guard_hyp h : n.succ + m = k, guard_hyp IH : n + m = k → n + m = k, -- Neither m nor k were generalised. exact h } end -- We can also generalise only certain hypotheses using a `generalizing` -- clause. This gives us the behaviour of stock `induction ... generalizing`. -- Hypotheses which depend on the major premise get generalised even if they are -- not mentioned in the `generalizing` clause. example {n m k : ℕ} (h : n + m = k) : n + m = k := begin induction' n generalizing k, case zero { simp [*] }, case succ : n IH { guard_hyp n : ℕ, guard_hyp m : ℕ, guard_hyp k : ℕ, guard_hyp h : n.succ + m = k, guard_hyp IH : ∀ {k}, n + m = k → n + m = k, -- k was generalised, but m was not. exact h } end -- Sometimes generalising a hypothesis H does not give us a more general -- induction hypothesis. In such cases, induction' should not generalise H. The -- following example is not realistic, but situations like this can occur in -- practice; see accufact_1_eq_fact below. example (n m k : ℕ) : m + k = k + m := begin induction' m, case zero { simp }, case succ { guard_hyp ih : ∀ k, m + k = k + m, -- k was generalised because this makes the IH more general. -- n was not generalised -- if it had been, the IH would be -- -- ∀ n k, m + k = k + m -- -- with one useless additional argument. ac_refl } end -- This example checks that constructor arguments don't 'steal' the names of -- generalised hypotheses. example (n : list ℕ) (n : ℕ) : list ℕ := begin -- this performs induction on (n : ℕ) induction' n, { exact n }, { guard_hyp n : list ℕ, guard_hyp n_1 : ℕ, -- n is the list, which was automatically generalized and keeps its name. -- n_1 is the recursive argument of `nat.succ`. It would be called `n` if -- there wasn't already an `n` in the context. exact (n_1 :: n) } end -- This example tests whether `induction'` gets confused when there are -- additional cases around. example (k : ℕ) (t : Two) : 0 + k = k := begin cases t, induction' k, { refl }, { simp }, induction' k, { refl }, { simp } end -- The type of the induction premise can be a complex expression so long as it -- normalises to an inductive (possibly applied to params/indexes). example (n) : 0 + n = n := begin let T := ℕ, change T at n, induction' n; simp end -- Fail if the type of the induction premise is not an inductive type example {α} (x : α) (f : α → α) : unit := begin success_if_fail { induction' x }, success_if_fail { induction' f }, exact () end -- The following example used to trigger a bug where eliminate would generate -- hypotheses with duplicate names. structure fraction : Type := (num : ℤ) (denom : ℤ) (denom_ne_zero : denom ≠ 0) lemma fraction.ext (a b : fraction) (hnum : fraction.num a = fraction.num b) (hdenom : fraction.denom a = fraction.denom b) : a = b := begin cases' a, cases' b, guard_hyp num : ℤ, guard_hyp denom : ℤ, guard_hyp num_1 : ℤ, guard_hyp denom_1 : ℤ, rw fraction.mk.inj_eq, exact and.intro hnum hdenom end -- A "with" clause can be used to give the names of constructor arguments (as -- for `cases`, `induction` etc). example (x : ℕ × ℕ) (y : Vec ℕ 2) (z : List ℕ) : unit := begin cases' x with i j k l, guard_hyp i : ℕ, guard_hyp j : ℕ, clear i j, cases' y with i j k l, -- Note that i is 'skipped' because it is used to name the (n : ℕ) -- argument of `cons`, but that argument is cleared by index unification. I -- find this a little strange, but `cases` also behaves like this. guard_hyp j : ℕ, guard_hyp k : Vec ℕ 1, clear j k, cases' z with i j k l, case nil { exact () }, case cons { guard_hyp i : ℕ, guard_hyp j : List ℕ, exact () } end -- "with" also works with induction'. example (x : List ℕ) : unit := begin induction' x with i j k l, case nil { exact () }, case cons { guard_hyp i : ℕ, guard_hyp j : List ℕ, guard_hyp k : unit, exact () } end -- An underscore in a "with" clause means "use the auto-generated name for this -- argument". example (x : List ℕ) : unit := begin induction' x with _ j _ l, case nil { exact () }, case cons { guard_hyp x : ℕ, guard_hyp j : List ℕ, guard_hyp ih : unit, exact () } end namespace with_tests inductive test | intro (n) (f : fin n) (m) (g : fin m) -- A hyphen in a "with" clause means "clear this hypothesis and its reverse -- dependencies". example (h : test) : unit := begin cases' h with - F M G, guard_hyp M : ℕ, guard_hyp G : fin M, success_if_fail { guard_hyp n }, success_if_fail { guard_hyp F }, exact () end -- Names given in a "with" clause are used verbatim, even if this results in -- shadowing. example (x : ℕ) (h : test) : unit := begin cases' h with x y y -, /- Expected goal: x x : ℕ, y : fin x, y : ℕ ⊢ unit It's hard to give a good test case here because we would need a variant of `guard_hyp` that is sensitive to shadowing. But we can at least check that the hyps don't have the names they would get if we avoided shadowing. -/ success_if_fail { guard_hyp x_1 }, success_if_fail { guard_hyp y_1 }, exact () end end with_tests -- induction' and cases' can be used to perform induction/case analysis on -- arbitrary expressions (not just hypotheses). A very synthetic example: example {α} : α ∨ ¬ α := begin cases' classical.em α with a nota, { exact (or.inl a) }, { exact (or.inr nota) } end -- Cases'/induction' can add an equation witnessing the case split it -- performed. Again, a highly synthetic example: example {α} (xs : list α) : xs.reverse.length = 0 ∨ ∃ m, xs.reverse.length = m + 1 := begin cases' eq : xs.length, case zero { left, rw list.length_reverse, exact eq }, case succ : l { right, rw list.length_reverse, use l, exact eq } end -- Index equation simplification can deal with equations that aren't in normal -- form. example {α} (x : α) (xs) : list.take 1 (x :: xs) ≠ [] := begin intro contra, cases' contra end -- Index generalisation should leave early occurrences of complex index terms -- alone. This means that given the major premise `e : E (f y) y` where `y` is a -- complex term, index generalisation should give us -- -- e : E (f y) i, -- -- *not* -- -- e : E (f i) i. -- -- Otherwise we get problems with examples like this: inductive ℕ₂ : Type | zero | succ (n : ℕ₂) : ℕ₂ namespace ℕ₂ def plus : ℕ₂ → ℕ₂ → ℕ₂ | zero y := y | (succ x) y := succ (plus x y) example (x : ℕ₂) (h : plus zero x = zero) : x = zero := begin cases' h, guard_target zero = plus zero zero, refl -- If index generalisation blindly replaced all occurrences of zero, we would -- get -- -- index = zero → plus index x = index → x = index -- -- and after applying the recursor -- -- plus index x = zero → x = plus index x -- -- This leaves the goal provable, but very confusing. end -- TODO Here's a test case (due to Floris van Doorn) where index generalisation -- is over-eager: it replaces the complex index `zero` everywhere in the goal, -- which makes the goal type-incorrect. `cases` does not exhibit this problem. example (x : ℕ₂) (h : plus zero x = zero) : (⟨x, h⟩ : ∃ x, plus zero x = zero) = ⟨zero, rfl⟩ := begin success_if_fail { cases' h }, cases h, refl end end ℕ₂ -- For whatever reason, the eliminator for `false` has an explicit argument -- where all other eliminators have an implicit one. `eliminate_hyp` has to -- work around this to ensure that we can eliminate a `false` hyp. example {α} (h : false) : α := begin cases' h end -- Index equation simplification also works with nested datatypes. inductive rose (α : Type) : Type | leaf : rose | node (val : α) (children : list rose) : rose namespace rose inductive nonempty {α} : rose α → Prop | node (v c cs) : nonempty (node v (c :: cs)) lemma nonempty_node_elim {α} {v : α} {cs} (h : nonempty (node v cs)) : ¬ cs.empty := begin induction' h, finish end end rose -- The following test cases, provided by Patrick Massot, test interactions with -- several 'advanced' Lean features. namespace topological_space_tests class topological_space (X : Type) := (is_open : set X → Prop) (univ_mem : is_open set.univ) (union : ∀ (B : set (set X)) (h : ∀ b ∈ B, is_open b), is_open (⋃₀ B)) (inter : ∀ (A B : set X) (hA : is_open A) (hB : is_open B), is_open (A ∩ B)) open topological_space (is_open) inductive generated_open (X : Type) (g : set (set X)) : set X → Prop | generator : ∀ A ∈ g, generated_open A | inter : ∀ A B, generated_open A → generated_open B → generated_open (A ∩ B) | union : ∀ (B : set (set X)), (∀ b ∈ B, generated_open b) → generated_open (⋃₀ B) | univ : generated_open set.univ def generate_from (X : Type) (g : set (set X)) : topological_space X := { is_open := generated_open X g, univ_mem := generated_open.univ, inter := generated_open.inter, union := generated_open.union } inductive generated_filter {X : Type*} (g : set (set X)) : set X → Prop | generator {A} : A ∈ g → generated_filter A | inter {A B} : generated_filter A → generated_filter B → generated_filter (A ∩ B) | subset {A B} : generated_filter A → A ⊆ B → generated_filter B | univ : generated_filter set.univ def neighbourhood {X : Type} [topological_space X] (x : X) (V : set X) : Prop := ∃ (U : set X), is_open U ∧ x ∈ U ∧ U ⊆ V axiom nhd_inter {X : Type*} [topological_space X] {x : X} {U V : set X} (hU : neighbourhood x U) (hV : neighbourhood x V) : neighbourhood x (U ∩ V) axiom nhd_superset {X : Type*} [topological_space X] {x : X} {U V : set X} (hU : neighbourhood x U) (hUV : U ⊆ V) : neighbourhood x V axiom nhd_univ {X : Type*} [topological_space X] {x : X} : neighbourhood x set.univ -- This example fails if auto-generalisation refuses to revert before -- frozen local instances. example {X : Type} [T : topological_space X] {s : set (set X)} (h : T = generate_from X s) {U x} : generated_filter {V | V ∈ s ∧ x ∈ V} U → neighbourhood x U := begin rw h, intro U_in, induction' U_in fixing T h with U hU U V U_gen V_gen hU hV U V U_gen hUV hU, { exact ⟨U, generated_open.generator U hU.1, hU.2, set.subset.refl U⟩ }, { exact @nhd_inter _ (generate_from X s) _ _ _ hU hV }, { exact @nhd_superset _ (generate_from X s) _ _ _ hU hUV }, { apply nhd_univ } end -- This example fails if auto-generalisation tries to generalise `let` -- hypotheses. example {X : Type} [T : topological_space X] {s : set (set X)} (h : T = generate_from X s) {U x} : generated_filter {V | V ∈ s ∧ x ∈ V} U → neighbourhood x U := begin rw h, letI := generate_from X s, intro U_in, induction' U_in fixing T h with U hU U V U_gen V_gen hU hV U V U_gen hUV hU, { exact ⟨U, generated_open.generator U hU.1, hU.2, set.subset.refl U⟩ }, { exact nhd_inter hU hV }, { exact nhd_superset hU hUV }, { apply nhd_univ } end -- This example fails if infinitely branching inductive types like -- `generated_open` are not handled properly. In particular, it tests the -- interaction of infinitely branching types with complex indices. example {X : Type*} [T : topological_space X] {s : set (set X)} (h : T = generate_from X s) {U : set X} {x : X} : neighbourhood x U → generated_filter {V | V ∈ s ∧ x ∈ V} U := begin rw h, letI := generate_from X s, clear h, rintros ⟨V, V_op, x_in, hUV⟩, apply generated_filter.subset _ hUV, clear hUV, induction' V_op fixing _inst T s U, { apply generated_filter.generator, split ; assumption }, { cases x_in, apply generated_filter.inter ; tauto }, { rw set.mem_sUnion at x_in, rcases x_in with ⟨W, hW, hxW⟩, exact generated_filter.subset (ih W hW hxW) (set.subset_sUnion_of_mem hW)}, { apply generated_filter.univ } end end topological_space_tests -------------------------------------------------------------------------------- -- Logical Verification Use Cases -------------------------------------------------------------------------------- -- The following examples were provided by Jasmin Blanchette. They are taken -- from his course 'Logical Verification'. /- Head induction for transitive closure -/ inductive star {α : Sort*} (r : α → α → Prop) (a : α) : α → Prop | refl {} : star a | tail {b c} : star b → r b c → star c namespace star variables {α : Sort*} {r : α → α → Prop} {a b c d : α} lemma head (hab : r a b) (hbc : star r b c) : star r a c := begin induction' hbc fixing hab, case refl { exact refl.tail hab }, case tail : c d hbc hcd hac { exact hac.tail hcd } end -- In this example, induction' must apply the dependent recursor for star; the -- nondependent one doesn't apply. lemma head_induction_on {b} {P : ∀a : α, star r a b → Prop} {a} (h : star r a b) (refl : P b refl) (head : ∀{a c} (h' : r a c) (h : star r c b), P c h → P a (h.head h')) : P a h := begin induction' h, case refl { exact refl }, case tail : b c hab hbc ih { apply ih, { exact head hbc _ refl, }, { intros _ _ hab _, exact head hab _} } end end star /- Factorial -/ def accufact : ℕ → ℕ → ℕ | a 0 := a | a (n + 1) := accufact ((n + 1) * a) n lemma accufact_1_eq_fact (n : ℕ) : accufact 1 n = nat.factorial n := have accufact_eq_fact_mul : ∀m a, accufact a m = nat.factorial m * a := begin intros m a, induction' m, case zero { simp [nat.factorial, accufact] }, case succ { simp [nat.factorial, accufact, ih, nat.succ_eq_add_one], cc } end, by simp [accufact_eq_fact_mul n 1] /- Substitution -/ namespace expressions inductive exp : Type | Var : string → exp | Num : ℤ → exp | Plus : exp → exp → exp export exp def subst (ρ : string → exp) : exp → exp | (Var y) := ρ y | (Num i) := Num i | (Plus e₁ e₂) := Plus (subst e₁) (subst e₂) lemma subst_Var (e : exp) : subst (λx, Var x) e = e := begin induction' e, case Var { guard_hyp s : string, rw [subst] }, case Num { guard_hyp n : ℤ, rw [subst] }, case Plus { guard_hyp e : exp, guard_hyp e_1 : exp, guard_hyp ih_e, guard_hyp ih_e_1, rw [subst], rw ih_e, rw ih_e_1 } end end expressions /- Less-than -/ namespace less_than inductive lt : nat → nat → Type | zero_succ (n : nat) : lt 0 (1 + n) | succ_succ {n m : nat} : lt n m → lt (1 + n) (1 + m) inductive lte : nat → nat → Type | zero (n : nat) : lte 0 n | succ {n m : nat} : lte n m → lte (1 + n) (1 + m) lemma lt_lte {n m} : lt n m → lte n m := begin intro lt_n_m, induction' lt_n_m, case zero_succ : i { constructor }, case succ_succ : i j lt_i_j ih { constructor, apply ih } end end less_than /- Sortedness -/ inductive sorted : list ℕ → Prop | nil : sorted [] | single {x : ℕ} : sorted [x] | two_or_more {x y : ℕ} {zs : list ℕ} (hle : x ≤ y) (hsorted : sorted (y :: zs)) : sorted (x :: y :: zs) /- In this example it's important that cases' *doesn't* normalise the values of indexes when simplifying index equations. -/ lemma not_sorted_17_13 : ¬ sorted [17, 13] := begin intro h, cases' h, guard_hyp hle : 17 ≤ 13, linarith end /- Palindromes -/ namespace palindrome inductive palindrome {α : Type} : list α → Prop | nil : palindrome [] | single (x : α) : palindrome [x] | sandwich (x : α) (xs : list α) (hpal : palindrome xs) : palindrome ([x] ++ xs ++ [x]) axiom reverse_append_sandwich {α : Type} (x : α) (ys : list α) : list.reverse ([x] ++ ys ++ [x]) = [x] ++ list.reverse ys ++ [x] lemma rev_palindrome {α : Type} (xs : list α) (hpal : palindrome xs) : palindrome (list.reverse xs) := begin induction' hpal, case nil { exact palindrome.nil }, case single { exact palindrome.single _ }, case sandwich { rw reverse_append_sandwich, apply palindrome.sandwich, apply ih } end end palindrome /- Transitive Closure -/ namespace transitive_closure inductive tc {α : Type} (r : α → α → Prop) : α → α → Prop | base (x y : α) (hr : r x y) : tc x y | step (x y z : α) (hr : r x y) (ht : tc y z) : tc x z /- The transitive closure is a nice example with lots of variables to keep track of. We start with a lemma where the variable names do not collide with those appearing in the definition of the inductive predicate. -/ lemma tc_pets₁ {α : Type} (r : α → α → Prop) (c : α) : ∀a b, tc r a b → r b c → tc r a c := begin intros a b htab hrbc, induction' htab fixing c, case base : _ _ hrab { exact tc.step _ _ _ hrab (tc.base _ _ hrbc) }, case step : _ x _ hrax { exact tc.step _ _ _ hrax (ih hrbc) } end /- The same proof, but this time the variable names clash. Also, this time we let `induction'` generalize `z`. -/ lemma tc_pets₂ {α : Type} (r : α → α → Prop) (z : α) : ∀x y, tc r x y → r y z → tc r x z := begin intros x y htxy hryz, induction' htxy, case base : _ _ hrxy { exact tc.step _ _ _ hrxy (tc.base _ _ hryz) }, case step : _ x' y hrxx' htx'y ih { exact tc.step _ _ _ hrxx' (ih _ hryz) } end /- Another proof along the same lines. -/ lemma tc_trans {α : Type} (r : α → α → Prop) (c : α) : ∀a b : α, tc r a b → tc r b c → tc r a c := begin intros a b htab htbc, induction' htab, case base { exact tc.step _ _ _ hr htbc }, case step { exact tc.step _ _ _ hr (ih _ htbc) } end /- ... and with clashing variable names: -/ lemma tc_trans' {α : Type} (r : α → α → Prop) {x y z} : tc r x y → tc r y z → tc r x z := begin intros h₁ h₂, induction' h₁, case base { exact tc.step _ _ _ hr h₂ }, case step { exact tc.step _ _ _ hr (ih h₂) } end end transitive_closure /- Evenness -/ inductive Even : ℕ → Prop | zero : Even 0 | add_two : ∀k : ℕ, Even k → Even (k + 2) lemma not_even_2_mul_add_1 (n : ℕ) : ¬ Even (2 * n + 1) := begin intro h, induction' h, -- No case tag since there's only one goal. I don't really like this, but -- this is the behaviour of induction/cases. { apply ih (n - 1), cases' n, case zero { linarith }, case succ { simp [nat.succ_eq_add_one] at *, linarith } } end /- Big-Step Semantics -/ namespace semantics def state := string → ℕ def state.update (name : string) (val : ℕ) (s : state) : state := λname', if name' = name then val else s name' notation s `{` name ` ↦ ` val `}` := state.update name val s inductive stmt : Type | skip : stmt | assign : string → (state → ℕ) → stmt | seq : stmt → stmt → stmt | ite : (state → Prop) → stmt → stmt → stmt | while : (state → Prop) → stmt → stmt export stmt infixr ` ;; ` : 90 := stmt.seq /- Our first version is partly uncurried, like in the Logical Verification course, and also like in Concrete Semantics. This makes the binary infix notation possible. -/ inductive big_step : stmt × state → state → Prop | skip {s} : big_step (skip, s) s | assign {x a s} : big_step (assign x a, s) (s{x ↦ a s}) | seq {S T s t u} (hS : big_step (S, s) t) (hT : big_step (T, t) u) : big_step (seq S T, s) u | ite_true {b : state → Prop} {S T s t} (hcond : b s) (hbody : big_step (S, s) t) : big_step (ite b S T, s) t | ite_false {b : state → Prop} {S T s t} (hcond : ¬ b s) (hbody : big_step (T, s) t) : big_step (ite b S T, s) t | while_true {b : state → Prop} {S s t u} (hcond : b s) (hbody : big_step (S, s) t) (hrest : big_step (while b S, t) u) : big_step (while b S, s) u | while_false {b : state → Prop} {S s} (hcond : ¬ b s) : big_step (while b S, s) s infix ` ⟹ `:110 := big_step open big_step lemma not_big_step_while_true {S s t} : ¬ (while (λ_, true) S, s) ⟹ t := begin intro hw, induction' hw, case while_true { exact ih_hw_1 }, case while_false { exact hcond trivial } end /- The same with a curried version of the predicate. It should make no difference whether a predicate is curried or uncurried. -/ inductive curried_big_step : stmt → state → state → Prop | skip {s} : curried_big_step skip s s | assign {x a s} : curried_big_step (assign x a) s (s{x ↦ a s}) | seq {S T s t u} (hS : curried_big_step S s t) (hT : curried_big_step T t u) : curried_big_step (seq S T) s u | ite_true {b : state → Prop} {S T s t} (hcond : b s) (hbody : curried_big_step S s t) : curried_big_step (ite b S T) s t | ite_false {b : state → Prop} {S T s t} (hcond : ¬ b s) (hbody : curried_big_step T s t) : curried_big_step (ite b S T) s t | while_true {b : state → Prop} {S s t u} (hcond : b s) (hbody : curried_big_step S s t) (hrest : curried_big_step (while b S) t u) : curried_big_step (while b S) s u | while_false {b : state → Prop} {S s} (hcond : ¬ b s) : curried_big_step (while b S) s s lemma not_curried_big_step_while_true {S s t} : ¬ curried_big_step (while (λ_, true) S) s t := begin intro hw, induction' hw, case while_true { exact ih_hw_1, }, case while_false { exact hcond trivial } end end semantics /- Small-Step Semantics -/ namespace semantics inductive small_step : stmt × state → stmt × state → Prop | assign {x a s} : small_step (assign x a, s) (skip, s{x ↦ a s}) | seq_step {S S' T s s'} (hS : small_step (S, s) (S', s')) : small_step (seq S T, s) (seq S' T, s') | seq_skip {T s} : small_step (seq skip T, s) (T, s) | ite_true {b : state → Prop} {S T s} (hcond : b s) : small_step (ite b S T, s) (S, s) | ite_false {b : state → Prop} {S T s} (hcond : ¬ b s) : small_step (ite b S T, s) (T, s) | while {b : state → Prop} {S s} : small_step (while b S, s) (ite b (seq S (while b S)) skip, s) lemma small_step_if_equal_states {S T s t s' t'} (hstep : small_step (S, s) (T, t)) (hs : s' = s) (ht : t' = t) : small_step (S, s') (T, t') := begin induction' hstep, { rw [hs, ht], exact small_step.assign, }, { apply small_step.seq_step, exact ih hs ht, }, { rw [hs, ht], exact small_step.seq_skip, }, { rw [hs, ht], exact small_step.ite_true hcond, }, { rw [hs, ht], exact small_step.ite_false hcond, }, { rw [hs, ht], exact small_step.while, } end infixr ` ⇒ ` := small_step infixr ` ⇒* ` : 100 := star small_step /- More lemmas about big-step and small-step semantics. These are taken from the Logical Verification course materials. They provide lots of good test cases for cases'/induction'. -/ namespace star variables {α : Sort*} {r : α → α → Prop} {a b c d : α} attribute [refl] star.refl @[trans] lemma trans (hab : star r a b) (hbc : star r b c) : star r a c := begin induction' hbc, case refl { assumption }, case tail : c d hbc hcd hac { exact (star.tail (hac hab)) hcd } end lemma single (hab : r a b) : star r a b := star.refl.tail hab lemma trans_induction_on {α : Sort*} {r : α → α → Prop} {p : ∀{a b : α}, star r a b → Prop} {a b : α} (h : star r a b) (ih₁ : ∀a, @p a a star.refl) (ih₂ : ∀{a b} (h : r a b), p (single h)) (ih₃ : ∀{a b c} (h₁ : star r a b) (h₂ : star r b c), p h₁ → p h₂ → p (trans h₁ h₂)) : p h := begin induction' h, case refl { exact ih₁ a }, case tail : b c hab hbc ih { exact ih₃ hab (single hbc) (ih ih₁ @ih₂ @ih₃) (ih₂ hbc) } end lemma lift {β : Sort*} {s : β → β → Prop} (f : α → β) (h : ∀a b, r a b → s (f a) (f b)) (hab : star r a b) : star s (f a) (f b) := begin apply trans_induction_on hab, exact (λ a, star.refl), exact (λ a b, star.single ∘ h _ _), exact (λ a b c _ _, star.trans) end end star lemma big_step_deterministic {S s l r} (hl : (S, s) ⟹ l) (hr : (S, s) ⟹ r) : l = r := begin induction' hl, case skip : t { cases' hr, refl }, case assign : x a s { cases' hr, refl }, case seq : S T s t l hS hT ihS ihT { cases' hr with _ _ _ _ _ _ _ t' _ hS' hT', cases' ihS hS', cases' ihT hT', refl }, case ite_true : b S T s t hb hS ih { cases' hr, { apply ih, assumption }, { apply ih, cc } }, case ite_false : b S T s t hb hT ih { cases' hr, { apply ih, cc }, { apply ih, assumption } }, case while_true : b S s t u hb hS hw ihS ihw { cases' hr, { cases' ihS hr, cases' ihw hr_1, refl }, { cc } }, { cases' hr, { cc }, { refl } } end @[simp] lemma big_step_skip_iff {s t} : (stmt.skip, s) ⟹ t ↔ t = s := begin apply iff.intro, { intro h, cases' h, refl }, { intro h, rw h, exact big_step.skip } end @[simp] lemma big_step_assign_iff {x a s t} : (stmt.assign x a, s) ⟹ t ↔ t = s{x ↦ a s} := begin apply iff.intro, { intro h, cases' h, refl }, { intro h, rw h, exact big_step.assign } end @[simp] lemma big_step_seq_iff {S T s t} : (S ;; T, s) ⟹ t ↔ (∃u, (S, s) ⟹ u ∧ (T, u) ⟹ t) := begin apply iff.intro, { intro h, cases' h, apply exists.intro, apply and.intro; assumption }, { intro h, cases' h, cases' h, apply big_step.seq; assumption } end @[simp] lemma big_step_ite_iff {b S T s t} : (stmt.ite b S T, s) ⟹ t ↔ (b s ∧ (S, s) ⟹ t) ∨ (¬ b s ∧ (T, s) ⟹ t) := begin apply iff.intro, { intro h, cases' h, { apply or.intro_left, cc }, { apply or.intro_right, cc } }, { intro h, cases' h; cases' h, { apply big_step.ite_true; assumption }, { apply big_step.ite_false; assumption } } end lemma big_step_while_iff {b S s u} : (stmt.while b S, s) ⟹ u ↔ (∃t, b s ∧ (S, s) ⟹ t ∧ (stmt.while b S, t) ⟹ u) ∨ (¬ b s ∧ u = s) := begin apply iff.intro, { intro h, cases' h, { apply or.intro_left, apply exists.intro t, cc }, { apply or.intro_right, cc } }, { intro h, cases' h, case or.inl { cases' h with t h, cases' h with hb h, cases' h with hS hwhile, exact big_step.while_true hb hS hwhile }, case or.inr { cases' h with hb hus, rw hus, exact big_step.while_false hb } } end lemma big_step_while_true_iff {b : state → Prop} {S s u} (hcond : b s) : (stmt.while b S, s) ⟹ u ↔ (∃t, (S, s) ⟹ t ∧ (stmt.while b S, t) ⟹ u) := by rw big_step_while_iff; simp [hcond] @[simp] lemma big_step_while_false_iff {b : state → Prop} {S s t} (hcond : ¬ b s) : (stmt.while b S, s) ⟹ t ↔ t = s := by rw big_step_while_iff; simp [hcond] lemma small_step_final (S s) : (¬ ∃T t, (S, s) ⇒ (T, t)) ↔ S = stmt.skip := begin induction' S, case skip { simp, intros T t hstep, cases' hstep }, case assign : x a { simp, apply exists.intro stmt.skip, apply exists.intro (s{x ↦ a s}), exact small_step.assign }, case seq : S T ihS ihT { simp, cases' classical.em (S = stmt.skip), case inl { rw h, apply exists.intro T, apply exists.intro s, exact small_step.seq_skip }, case inr { simp [h, not_forall, not_not] at ihS, cases' ihS s with S' hS', cases' hS' with s' hs', apply exists.intro (S' ;; T), apply exists.intro s', exact small_step.seq_step hs' } }, case ite : b S T ihS ihT { simp, cases' classical.em (b s), case inl { apply exists.intro S, apply exists.intro s, exact small_step.ite_true h }, case inr { apply exists.intro T, apply exists.intro s, exact small_step.ite_false h } }, case while : b S ih { simp, apply exists.intro (stmt.ite b (S ;; stmt.while b S) stmt.skip), apply exists.intro s, exact small_step.while } end lemma small_step_deterministic {S s Ll Rr} (hl : (S, s) ⇒ Ll) (hr : (S, s) ⇒ Rr) : Ll = Rr := begin induction' hl, case assign : x a s { cases' hr, refl }, case seq_step : S S₁ T s s₁ hS₁ ih { cases' hr, case seq_step : S S₂ _ _ s₂ hS₂ { have hSs₁₂ := ih hS₂, cc }, case seq_skip { cases' hS₁ } }, case seq_skip : T s { cases' hr, { cases' hr }, { refl } }, case ite_true : b S T s hcond { cases' hr, case ite_true { refl }, case ite_false { cc } }, case ite_false : b S T s hcond { cases' hr, case ite_true { cc }, case ite_false { refl } }, case while : b S s { cases' hr, refl } end lemma small_step_skip {S s t} : ¬ ((stmt.skip, s) ⇒ (S, t)) := by intro h; cases' h @[simp] lemma small_step_seq_iff {S T s Ut} : (S ;; T, s) ⇒ Ut ↔ (∃S' t, (S, s) ⇒ (S', t) ∧ Ut = (S' ;; T, t)) ∨ (S = stmt.skip ∧ Ut = (T, s)) := begin apply iff.intro, { intro h, cases' h, { apply or.intro_left, apply exists.intro S', apply exists.intro s', cc }, { apply or.intro_right, cc } }, { intro h, cases' h, { cases' h, cases' h, cases' h, rw right, apply small_step.seq_step, assumption }, { cases' h, rw left, rw right, apply small_step.seq_skip } } end @[simp] lemma small_step_ite_iff {b S T s Us} : (stmt.ite b S T, s) ⇒ Us ↔ (b s ∧ Us = (S, s)) ∨ (¬ b s ∧ Us = (T, s)) := begin apply iff.intro, { intro h, cases' h, { apply or.intro_left, cc }, { apply or.intro_right, cc } }, { intro h, cases' h, { cases' h, rw right, apply small_step.ite_true, assumption }, { cases' h, rw right, apply small_step.ite_false, assumption } } end lemma star_small_step_seq {S T s u} (h : (S, s) ⇒* (stmt.skip, u)) : (S ;; T, s) ⇒* (stmt.skip ;; T, u) := begin apply star.lift (λSs, (prod.fst Ss ;; T, prod.snd Ss)) _ h, intros Ss Ss' h, cases' Ss, cases' Ss', apply small_step.seq_step, assumption end lemma star_small_step_of_big_step {S s t} (h : (S, s) ⟹ t) : (S, s) ⇒* (stmt.skip, t) := begin induction' h, case skip { refl }, case assign { exact star.single small_step.assign }, case seq : S T s t u hS hT ihS ihT { transitivity, exact star_small_step_seq ihS, apply star.head small_step.seq_skip ihT }, case ite_true : b S T s t hs hst ih { exact star.head (small_step.ite_true hs) ih }, case ite_false : b S T s t hs hst ih { exact star.head (small_step.ite_false hs) ih }, case while_true : b S s t u hb hS hw ihS ihw { exact (star.head small_step.while (star.head (small_step.ite_true hb) (star.trans (star_small_step_seq ihS) (star.head small_step.seq_skip ihw)))) }, case while_false : b S s hb { exact star.tail (star.single small_step.while) (small_step.ite_false hb) } end lemma big_step_of_small_step_of_big_step {S₀ S₁ s₀ s₁ s₂} (h₁ : (S₀, s₀) ⇒ (S₁, s₁)) : (S₁, s₁) ⟹ s₂ → (S₀, s₀) ⟹ s₂ := begin induction' h₁; simp [*, big_step_while_true_iff, or_imp_distrib] {contextual := tt}, case seq_step { intros u hS' hT, apply exists.intro u, exact and.intro (ih hS') hT }, end lemma big_step_of_star_small_step {S s t} : (S, s) ⇒* (stmt.skip, t) → (S, s) ⟹ t := begin generalize hSs : (S, s) = Ss, intro h, induction h using star.head_induction_on with _ S's' h h' ih generalizing S s; cases' hSs, { exact big_step.skip }, { cases' S's' with S' s', apply big_step_of_small_step_of_big_step h, apply ih, refl } end end semantics
e4daf9ec67bdd03d8e4939ff2c3c8015b7259a09
037dba89703a79cd4a4aec5e959818147f97635d
/src/2020/relations/partition_challenge_official_solution.lean
23a05395498064c1e16cb4a459ca87086089712e
[]
no_license
ImperialCollegeLondon/M40001_lean
3a6a09298da395ab51bc220a535035d45bbe919b
62a76fa92654c855af2b2fc2bef8e60acd16ccec
refs/heads/master
1,666,750,403,259
1,665,771,117,000
1,665,771,117,000
209,141,835
115
12
null
1,640,270,596,000
1,568,749,174,000
Lean
UTF-8
Lean
false
false
9,980
lean
import tactic /-! # The partition challenge! Prove that equivalence relations on α are the same as partitions of α. Three sections: 1) partitions 2) equivalence classes 3) the challenge Say `α` is a type, and `R` is a binary relation on `α`. The following things are already in Lean: reflexive R := ∀ (x : α), R x x symmetric R := ∀ ⦃x y : α⦄, R x y → R y x transitive R := ∀ ⦃x y z : α⦄, R x y → R y z → R x z equivalence R := reflexive R ∧ symmetric R ∧ transitive R In the file below, we will define partitions of `α` and "build some interface" (i.e. prove some propositions). We will define equivalence classes and do the same thing. Finally, we will prove that there's a bijection between equivalence relations on `α` and partitions of `α`. -/ /- # 1) Partitions We define a partition, and prove some easy lemmas. -/ /- ## Definition of a partition Let `α` be a type. A *partition* on `α` is defined to be the following data: 1) A set C of subsets of α, called "blocks". 2) A hypothesis (i.e. a proof!) that all the blocks are non-empty. 3) A hypothesis that every term of type α is in one of the blocks. 4) A hypothesis that two blocks with non-empty intersection are equal. -/ /-- The structure of a partition on a Type α. -/ @[ext] structure partition (α : Type) := (C : set (set α)) (Hnonempty : ∀ X ∈ C, (X : set α).nonempty) (Hcover : ∀ a, ∃ X ∈ C, a ∈ X) (Hdisjoint : ∀ X Y ∈ C, (X ∩ Y : set α).nonempty → X = Y) -- docstrings /-- The set of blocks. -/ add_decl_doc partition.C /-- Every element of a block is nonempty. -/ add_decl_doc partition.Hnonempty /-- The blocks cover the type they partition -/ add_decl_doc partition.Hcover /-- Two blocks which share an element are equal -/ add_decl_doc partition.Hdisjoint /- ## Basic interface for partitions -/ 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 α} /-- If X and Y are blocks, and a is in X and Y, then X = Y. -/ theorem eq_of_mem (hX : X ∈ P.C) (hY : Y ∈ P.C) {a : α} (haX : a ∈ X) (haY : a ∈ Y) : X = Y := -- Proof: follows immediately from the disjointness hypothesis. P.Hdisjoint _ hX _ hY ⟨a, haX, haY⟩ /-- If a is in two blocks X and Y, and if b is in X, then b is in Y (as X=Y) -/ theorem mem_of_mem (hX : X ∈ P.C) (hY : Y ∈ P.C) {a b : α} (haX : a ∈ X) (haY : a ∈ Y) (hbX : b ∈ X) : b ∈ Y := begin -- *great* place to teach convert here convert hbX, apply eq_of_mem hY hX haY haX, end theorem mem_block (a : α) : ∃ X : set α, X ∈ P.C ∧ a ∈ X := begin -- new tactic rcases P.Hcover a with ⟨X, hX, haX⟩, -- obtain ⟨X, hX, haX⟩ := P.Hcover a, use X, split, assumption, exact haX, end end partition /- # 2) Equivalence classes. We define equivalence classes and prove a few basic results about them. -/ section equivalence_classes /-! ## Definition of equivalence classes -/ -- Notation and variables for the equivalence class section: -- let α be a type, and let R be an equivalence relation on R. variables {α : Type} (R : α → α → Prop) /-- The equivalence class of `x` is the set of `y` related to `x`. -/ def cl (x : α) := {y : α | R y x} /-! ## Basic lemmas about equivalence classes -/ /-- Useful for rewriting -- `y` is in the equivalence class of `x` iff `y` is related to `x`. True by definition. -/ theorem cl_def {x y : α} : x ∈ cl R y ↔ R x y := iff.rfl variables {R} (hR : equivalence R) include hR /-- x is in cl(x) -/ lemma mem_cl_self (x : α) : x ∈ cl R x := begin rcases hR with ⟨hrefl, hsymm, htrans⟩, -- rw on ↔ rw cl_def, -- understand how to use hrefl apply hrefl, end lemma cl_sub_cl_of_mem_cl {x y : α} : x ∈ cl R y → cl R x ⊆ cl R y := begin rcases hR with ⟨hrefl, hsymm, htrans⟩, intro h, rw cl_def at h, -- new thing -- understand def of subset rw set.subset_def, intro z, intro hzx, rw cl_def at hzx ⊢, unfold transitive at htrans, apply htrans, exact hzx, exact h, end lemma cl_eq_cl_of_mem_cl {x y : α} : x ∈ cl R y → cl R x = cl R y := begin intro hxy, -- new function, find with library_search apply set.subset.antisymm, apply cl_sub_cl_of_mem_cl hR, assumption, apply cl_sub_cl_of_mem_cl hR, rcases hR with ⟨hrefl, hsymm, htrans⟩, apply hsymm, assumption, end end equivalence_classes -- section /-! # 3) The challenge! Let `α` be a type (i.e. a collection of stucff). There is a bijection between equivalence relations on `α` and partitions of `α`. We prove this by writing down constructions in each direction and proving that the constructions are two-sided inverses of one another. -/ open partition example (α : Type) : {R : α → α → Prop // equivalence R} ≃ partition α := -- We define functions in both directions and prove that one is a two-sided -- inverse of the other { -- Here is the first function (construction), from equivalence -- relations to partitions. -- 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.1 x}, -- I claim that C is a partition. We need to check the three -- hypotheses for a partition (`Hnonempty`, `Hcover` and `Hdisjoint`), -- so we need to supply three proofs. Hnonempty := begin cases R with R hR, -- If X is an equivalence class then X is nonempty. show ∀ (X : set α), (∃ (a : α), X = cl R a) → X.nonempty, rintros _ ⟨a, rfl⟩, -- unfold set.nonempty, use a, rw cl_def, rcases hR with ⟨hrefl, hsymm, htrans⟩, apply hrefl, end, Hcover := begin cases R with R hR, -- The equivalence classes cover α show ∀ (a : α), ∃ (X : set α) (H : ∃ (b : α), X = cl R b), a ∈ X, intro a, use cl R a, split, use a, apply hR.1, end, Hdisjoint := begin cases R with R hR, -- If two equivalence classes overlap, they are equal. show ∀ (X : set α), (∃ (a : α), X = cl R a) → ∀ (Y : set α), (∃ (b : α), Y = cl _ b) → (X ∩ Y).nonempty → X = Y, rintros X ⟨a, rfl⟩ Y ⟨b, rfl⟩ ⟨c, hca, hcb⟩, apply cl_eq_cl_of_mem_cl hR, apply hR.2.2, apply hR.2.1, exact hca, exact hcb, end }, -- Conversely, say P is an partition. inv_fun := λ P, -- Let's define a binary relation by x ~ y iff every block containing a, -- also contains b. Because only one block contains a, this will work, -- and it turns out to be a nice way of thinking about it. ⟨λ a b, ∀ X ∈ P.C, a ∈ X → b ∈ X, begin -- I claim this is an equivalence relation. split, { -- It's reflexive show ∀ (a : α) (X : set α), X ∈ P.C → a ∈ X → a ∈ X, intros a X hXC haX, assumption, }, split, { -- it's symmetric show ∀ (a b : α), (∀ (X : set α), X ∈ P.C → a ∈ X → b ∈ X) → ∀ (X : set α), X ∈ P.C → b ∈ X → a ∈ X, intros a b h X hX hbX, -- need to know something about how to use partitions. obtain ⟨Y, hY, haY⟩ := P.Hcover a, specialize h Y hY haY, exact mem_of_mem hY hX h hbX haY, }, { -- it's transitive unfold transitive, show ∀ (a b c : α), (∀ (X : set α), X ∈ P.C → a ∈ X → b ∈ X) → (∀ (X : set α), X ∈ P.C → b ∈ X → c ∈ X) → ∀ (X : set α), X ∈ P.C → a ∈ X → c ∈ X, intros a b c hbX hcX X hX haX, apply hcX, assumption, apply hbX, assumption, assumption, } 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⟩, -- Tidying up the mess... suffices : (λ (a b : α), ∀ (c : α), a ∈ cl R c → b ∈ cl R c) = R, simpa, -- ... you have to prove two binary relations are equal. ext a b, -- so you have to prove an if and only if. show (∀ (c : α), a ∈ cl R c → b ∈ cl R c) ↔ R a b, split, { intros hab, apply hR.2.1, apply hab, apply hR.1, }, { intros hab c hac, apply hR.2.2, apply hR.2.1, exact hab, exact hac, } 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 -- Let P be a partition intro P, -- It suffices to prove that a subset X is in the original partition -- if and only if it's in the one made from the equivalence relation. ext X, show (∃ (a : α), X = cl _ a) ↔ X ∈ P.C, split, { rintro ⟨a, rfl⟩, obtain ⟨X, hX, haX⟩ := P.Hcover a, convert hX, ext b, rw cl_def, dsimp, split, { intro haY, obtain ⟨Y, hY, hbY⟩ := P.Hcover b, specialize haY Y hY hbY, convert hbY, exact eq_of_mem hX hY haX haY, }, { intros hbX Y hY hbY, apply mem_of_mem hX hY hbX hbY haX, } }, { intro hX, rcases P.Hnonempty X hX with ⟨a, ha⟩, use a, ext b, split, { intro hbX, rw cl_def, dsimp, intros Y hY hbY, exact mem_of_mem hX hY hbX hbY ha, }, { rw cl_def, dsimp, intro haY, obtain ⟨Y, hY, hbY⟩ := P.Hcover b, specialize haY Y hY hbY, exact mem_of_mem hY hX haY ha hbY, } } end } /- -- get these files with leanproject get ImperialCollegeLondon/M40001_lean -/
94720852ed41d58d50908416d2e365ace5f3cf48
82e44445c70db0f03e30d7be725775f122d72f3e
/src/group_theory/coset.lean
c9629e74782b101418ec8f86341a314a691fd9f2
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
16,693
lean
/- Copyright (c) 2018 Mitchell Rowett. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mitchell Rowett, Scott Morrison -/ import group_theory.subgroup /-! # Cosets This file develops the basic theory of left and right cosets. ## Main definitions * `left_coset a s`: the left coset `a * s` for an element `a : α` and a subset `s ⊆ α`, for an `add_group` this is `left_add_coset a s`. * `right_coset s a`: the right coset `s * a` for an element `a : α` and a subset `s ⊆ α`, for an `add_group` this is `right_add_coset s a`. * `quotient_group.quotient s`: the quotient type representing the left cosets with respect to a subgroup `s`, for an `add_group` this is `quotient_add_group.quotient s`. * `quotient_group.mk`: the canonical map from `α` to `α/s` for a subgroup `s` of `α`, for an `add_group` this is `quotient_add_group.mk`. * `subgroup.left_coset_equiv_subgroup`: the natural bijection between a left coset and the subgroup, for an `add_group` this is `add_subgroup.left_coset_equiv_add_subgroup`. ## Notation * `a *l s`: for `left_coset a s`. * `a +l s`: for `left_add_coset a s`. * `s *r a`: for `right_coset s a`. * `s +r a`: for `right_add_coset s a`. ## TODO Add `to_additive` to `preimage_mk_equiv_subgroup_times_set`. -/ open set function variable {α : Type*} /-- The left coset `a * s` for an element `a : α` and a subset `s : set α` -/ @[to_additive left_add_coset "The left coset `a+s` for an element `a : α` and a subset `s : set α`"] def left_coset [has_mul α] (a : α) (s : set α) : set α := (λ x, a * x) '' s /-- The right coset `s * a` for an element `a : α` and a subset `s : set α` -/ @[to_additive right_add_coset "The right coset `s+a` for an element `a : α` and a subset `s : set α`"] def right_coset [has_mul α] (s : set α) (a : α) : set α := (λ x, x * a) '' s localized "infix ` *l `:70 := left_coset" in coset localized "infix ` +l `:70 := left_add_coset" in coset localized "infix ` *r `:70 := right_coset" in coset localized "infix ` +r `:70 := right_add_coset" in coset section coset_mul variable [has_mul α] @[to_additive mem_left_add_coset] lemma mem_left_coset {s : set α} {x : α} (a : α) (hxS : x ∈ s) : a * x ∈ a *l s := mem_image_of_mem (λ b : α, a * b) hxS @[to_additive mem_right_add_coset] lemma mem_right_coset {s : set α} {x : α} (a : α) (hxS : x ∈ s) : x * a ∈ s *r a := mem_image_of_mem (λ b : α, b * a) hxS /-- Equality of two left cosets `a * s` and `b * s`. -/ @[to_additive left_add_coset_equivalence "Equality of two left cosets `a + s` and `b + s`."] def left_coset_equivalence (s : set α) (a b : α) := a *l s = b *l s @[to_additive left_add_coset_equivalence_rel] lemma left_coset_equivalence_rel (s : set α) : equivalence (left_coset_equivalence s) := mk_equivalence (left_coset_equivalence s) (λ a, rfl) (λ a b, eq.symm) (λ a b c, eq.trans) /-- Equality of two right cosets `s * a` and `s * b`. -/ @[to_additive right_add_coset_equivalence "Equality of two right cosets `s + a` and `s + b`."] def right_coset_equivalence (s : set α) (a b : α) := s *r a = s *r b @[to_additive right_add_coset_equivalence_rel] lemma right_coset_equivalence_rel (s : set α) : equivalence (right_coset_equivalence s) := mk_equivalence (right_coset_equivalence s) (λ a, rfl) (λ a b, eq.symm) (λ a b c, eq.trans) end coset_mul section coset_semigroup variable [semigroup α] @[simp] lemma left_coset_assoc (s : set α) (a b : α) : a *l (b *l s) = (a * b) *l s := by simp [left_coset, right_coset, (image_comp _ _ _).symm, function.comp, mul_assoc] attribute [to_additive left_add_coset_assoc] left_coset_assoc @[simp] lemma right_coset_assoc (s : set α) (a b : α) : s *r a *r b = s *r (a * b) := by simp [left_coset, right_coset, (image_comp _ _ _).symm, function.comp, mul_assoc] attribute [to_additive right_add_coset_assoc] right_coset_assoc @[to_additive left_add_coset_right_add_coset] lemma left_coset_right_coset (s : set α) (a b : α) : a *l s *r b = a *l (s *r b) := by simp [left_coset, right_coset, (image_comp _ _ _).symm, function.comp, mul_assoc] end coset_semigroup section coset_monoid variables [monoid α] (s : set α) @[simp] lemma one_left_coset : 1 *l s = s := set.ext $ by simp [left_coset] attribute [to_additive zero_left_add_coset] one_left_coset @[simp] lemma right_coset_one : s *r 1 = s := set.ext $ by simp [right_coset] attribute [to_additive right_add_coset_zero] right_coset_one end coset_monoid section coset_submonoid open submonoid variables [monoid α] (s : submonoid α) @[to_additive mem_own_left_add_coset] lemma mem_own_left_coset (a : α) : a ∈ a *l s := suffices a * 1 ∈ a *l s, by simpa, mem_left_coset a (one_mem s) @[to_additive mem_own_right_add_coset] lemma mem_own_right_coset (a : α) : a ∈ (s : set α) *r a := suffices 1 * a ∈ (s : set α) *r a, by simpa, mem_right_coset a (one_mem s) @[to_additive mem_left_add_coset_left_add_coset] lemma mem_left_coset_left_coset {a : α} (ha : a *l s = s) : a ∈ s := by rw [←set_like.mem_coe, ←ha]; exact mem_own_left_coset s a @[to_additive mem_right_add_coset_right_add_coset] lemma mem_right_coset_right_coset {a : α} (ha : (s : set α) *r a = s) : a ∈ s := by rw [←set_like.mem_coe, ←ha]; exact mem_own_right_coset s a end coset_submonoid section coset_group variables [group α] {s : set α} {x : α} @[to_additive mem_left_add_coset_iff] lemma mem_left_coset_iff (a : α) : x ∈ a *l s ↔ a⁻¹ * x ∈ s := iff.intro (assume ⟨b, hb, eq⟩, by simp [eq.symm, hb]) (assume h, ⟨a⁻¹ * x, h, by simp⟩) @[to_additive mem_right_add_coset_iff] lemma mem_right_coset_iff (a : α) : x ∈ s *r a ↔ x * a⁻¹ ∈ s := iff.intro (assume ⟨b, hb, eq⟩, by simp [eq.symm, hb]) (assume h, ⟨x * a⁻¹, h, by simp⟩) end coset_group section coset_subgroup open subgroup variables [group α] (s : subgroup α) @[to_additive left_add_coset_mem_left_add_coset] lemma left_coset_mem_left_coset {a : α} (ha : a ∈ s) : a *l s = s := set.ext $ by simp [mem_left_coset_iff, mul_mem_cancel_left s (s.inv_mem ha)] @[to_additive right_add_coset_mem_right_add_coset] lemma right_coset_mem_right_coset {a : α} (ha : a ∈ s) : (s : set α) *r a = s := set.ext $ assume b, by simp [mem_right_coset_iff, mul_mem_cancel_right s (s.inv_mem ha)] @[to_additive eq_add_cosets_of_normal] theorem eq_cosets_of_normal (N : s.normal) (g : α) : g *l s = s *r g := set.ext $ assume a, by simp [mem_left_coset_iff, mem_right_coset_iff]; rw [N.mem_comm_iff] @[to_additive normal_of_eq_add_cosets] theorem normal_of_eq_cosets (h : ∀ g : α, g *l s = s *r g) : s.normal := ⟨assume a ha g, show g * a * g⁻¹ ∈ (s : set α), by rw [← mem_right_coset_iff, ← h]; exact mem_left_coset g ha⟩ @[to_additive normal_iff_eq_add_cosets] theorem normal_iff_eq_cosets : s.normal ↔ ∀ g : α, g *l s = s *r g := ⟨@eq_cosets_of_normal _ _ s, normal_of_eq_cosets s⟩ @[to_additive left_add_coset_eq_iff] lemma left_coset_eq_iff {x y : α} : left_coset x s = left_coset y s ↔ x⁻¹ * y ∈ s := begin rw set.ext_iff, simp_rw [mem_left_coset_iff, set_like.mem_coe], split, { intro h, apply (h y).mpr, rw mul_left_inv, exact s.one_mem }, { intros h z, rw ←mul_inv_cancel_right x⁻¹ y, rw mul_assoc, exact s.mul_mem_cancel_left h }, end @[to_additive right_add_coset_eq_iff] lemma right_coset_eq_iff {x y : α} : right_coset ↑s x = right_coset s y ↔ y * x⁻¹ ∈ s := begin rw set.ext_iff, simp_rw [mem_right_coset_iff, set_like.mem_coe], split, { intro h, apply (h y).mpr, rw mul_right_inv, exact s.one_mem }, { intros h z, rw ←inv_mul_cancel_left y x⁻¹, rw ←mul_assoc, exact s.mul_mem_cancel_right h }, end end coset_subgroup run_cmd to_additive.map_namespace `quotient_group `quotient_add_group namespace quotient_group variables [group α] (s : subgroup α) /-- The equivalence relation corresponding to the partition of a group by left cosets of a subgroup.-/ @[to_additive "The equivalence relation corresponding to the partition of a group by left cosets of a subgroup."] def left_rel : setoid α := ⟨λ x y, x⁻¹ * y ∈ s, by { simp_rw ←left_coset_eq_iff, exact left_coset_equivalence_rel s }⟩ lemma left_rel_r_eq_left_coset_equivalence : @setoid.r _ (quotient_group.left_rel s) = left_coset_equivalence s := by { ext, exact (left_coset_eq_iff s).symm } @[to_additive] instance left_rel_decidable [decidable_pred (∈ s)] : decidable_rel (left_rel s).r := λ x y, ‹decidable_pred (∈ s)› _ /-- `quotient s` is the quotient type representing the left cosets of `s`. If `s` is a normal subgroup, `quotient s` is a group -/ def quotient : Type* := quotient (left_rel s) /-- The equivalence relation corresponding to the partition of a group by right cosets of a subgroup. -/ @[to_additive "The equivalence relation corresponding to the partition of a group by right cosets of a subgroup."] def right_rel : setoid α := ⟨λ x y, y * x⁻¹ ∈ s, by { simp_rw ←right_coset_eq_iff, exact right_coset_equivalence_rel s }⟩ lemma right_rel_r_eq_right_coset_equivalence : @setoid.r _ (quotient_group.right_rel s) = right_coset_equivalence s := by { ext, exact (right_coset_eq_iff s).symm } @[to_additive] instance right_rel_decidable [decidable_pred (∈ s)] : decidable_rel (right_rel s).r := λ x y, ‹decidable_pred (∈ s)› _ end quotient_group namespace quotient_add_group /-- `quotient s` is the quotient type representing the left cosets of `s`. If `s` is a normal subgroup, `quotient s` is a group -/ def quotient [add_group α] (s : add_subgroup α) : Type* := quotient (left_rel s) end quotient_add_group attribute [to_additive quotient_add_group.quotient] quotient_group.quotient namespace quotient_group variables [group α] {s : subgroup α} @[to_additive] instance fintype [fintype α] (s : subgroup α) [decidable_rel (left_rel s).r] : fintype (quotient_group.quotient s) := quotient.fintype (left_rel s) /-- The canonical map from a group `α` to the quotient `α/s`. -/ @[to_additive "The canonical map from an `add_group` `α` to the quotient `α/s`."] abbreviation mk (a : α) : quotient s := quotient.mk' a @[elab_as_eliminator, to_additive] lemma induction_on {C : quotient s → Prop} (x : quotient s) (H : ∀ z, C (quotient_group.mk z)) : C x := quotient.induction_on' x H @[to_additive] instance : has_coe_t α (quotient s) := ⟨mk⟩ -- note [use has_coe_t] @[elab_as_eliminator, to_additive] lemma induction_on' {C : quotient s → Prop} (x : quotient s) (H : ∀ z : α, C z) : C x := quotient.induction_on' x H @[to_additive] instance (s : subgroup α) : inhabited (quotient s) := ⟨((1 : α) : quotient s)⟩ @[to_additive quotient_add_group.eq] protected lemma eq {a b : α} : (a : quotient s) = b ↔ a⁻¹ * b ∈ s := quotient.eq' @[to_additive] lemma eq_class_eq_left_coset (s : subgroup α) (g : α) : {x : α | (x : quotient s) = g} = left_coset g s := set.ext $ λ z, by rw [mem_left_coset_iff, set.mem_set_of_eq, eq_comm, quotient_group.eq, set_like.mem_coe] @[to_additive] lemma preimage_image_coe (N : subgroup α) (s : set α) : coe ⁻¹' ((coe : α → quotient N) '' s) = ⋃ x : N, (λ y : α, y * x) ⁻¹' s := begin ext x, simp only [quotient_group.eq, set_like.exists, exists_prop, set.mem_preimage, set.mem_Union, set.mem_image, subgroup.coe_mk, ← eq_inv_mul_iff_mul_eq], exact ⟨λ ⟨y, hs, hN⟩, ⟨_, N.inv_mem hN, by simpa using hs⟩, λ ⟨z, hz, hxz⟩, ⟨x*z, hxz, by simpa using hz⟩⟩, end end quotient_group namespace subgroup open quotient_group variables [group α] {s : subgroup α} /-- The natural bijection between a left coset `g * s` and `s`. -/ @[to_additive "The natural bijection between the cosets `g + s` and `s`."] def left_coset_equiv_subgroup (g : α) : left_coset g s ≃ s := ⟨λ x, ⟨g⁻¹ * x.1, (mem_left_coset_iff _).1 x.2⟩, λ x, ⟨g * x.1, x.1, x.2, rfl⟩, λ ⟨x, hx⟩, subtype.eq $ by simp, λ ⟨g, hg⟩, subtype.eq $ by simp⟩ /-- The natural bijection between a right coset `s * g` and `s`. -/ @[to_additive "The natural bijection between the cosets `s + g` and `s`."] def right_coset_equiv_subgroup (g : α) : right_coset ↑s g ≃ s := ⟨λ x, ⟨x.1 * g⁻¹, (mem_right_coset_iff _).1 x.2⟩, λ x, ⟨x.1 * g, x.1, x.2, rfl⟩, λ ⟨x, hx⟩, subtype.eq $ by simp, λ ⟨g, hg⟩, subtype.eq $ by simp⟩ /-- A (non-canonical) bijection between a group `α` and the product `(α/s) × s` -/ @[to_additive "A (non-canonical) bijection between an add_group `α` and the product `(α/s) × s`"] noncomputable def group_equiv_quotient_times_subgroup : α ≃ quotient s × s := calc α ≃ Σ L : quotient s, {x : α // (x : quotient s) = L} : (equiv.sigma_preimage_equiv quotient_group.mk).symm ... ≃ Σ L : quotient s, left_coset (quotient.out' L) s : equiv.sigma_congr_right (λ L, begin rw ← eq_class_eq_left_coset, show _root_.subtype (λ x : α, quotient.mk' x = L) ≃ _root_.subtype (λ x : α, quotient.mk' x = quotient.mk' _), simp [-quotient.eq'], end) ... ≃ Σ L : quotient s, s : equiv.sigma_congr_right (λ L, left_coset_equiv_subgroup _) ... ≃ quotient s × s : equiv.sigma_equiv_prod _ _ lemma card_eq_card_quotient_mul_card_subgroup [fintype α] (s : subgroup α) [fintype s] [decidable_pred (λ a, a ∈ s)] : fintype.card α = fintype.card (quotient s) * fintype.card s := by rw ← fintype.card_prod; exact fintype.card_congr (subgroup.group_equiv_quotient_times_subgroup) /-- **Order of a Subgroup** -/ lemma card_subgroup_dvd_card [fintype α] (s : subgroup α) [fintype s] : fintype.card s ∣ fintype.card α := by haveI := classical.prop_decidable; simp [card_eq_card_quotient_mul_card_subgroup s] lemma card_quotient_dvd_card [fintype α] (s : subgroup α) [decidable_pred (λ a, a ∈ s)] [fintype s] : fintype.card (quotient s) ∣ fintype.card α := by simp [card_eq_card_quotient_mul_card_subgroup s] open fintype variables {H : Type*} [group H] lemma card_dvd_of_injective [fintype α] [fintype H] (f : α →* H) (hf : function.injective f) : card α ∣ card H := by classical; calc card α = card (f.range : subgroup H) : card_congr (equiv.of_injective f hf) ...∣ card H : card_subgroup_dvd_card _ lemma card_dvd_of_le {H K : subgroup α} [fintype H] [fintype K] (hHK : H ≤ K) : card H ∣ card K := card_dvd_of_injective (inclusion hHK) (inclusion_injective hHK) lemma card_comap_dvd_of_injective (K : subgroup H) [fintype K] (f : α →* H) [fintype (K.comap f)] (hf : function.injective f) : fintype.card (K.comap f) ∣ fintype.card K := by haveI : fintype ((K.comap f).map f) := fintype.of_equiv _ (equiv_map_of_injective _ _ hf).to_equiv; calc fintype.card (K.comap f) = fintype.card ((K.comap f).map f) : fintype.card_congr (equiv_map_of_injective _ _ hf).to_equiv ... ∣ fintype.card K : card_dvd_of_le (map_comap_le _ _) end subgroup namespace quotient_group variables [group α] -- FIXME -- why is there no `to_additive`? /-- If `s` is a subgroup of the group `α`, and `t` is a subset of `α/s`, then there is a (typically non-canonical) bijection between the preimage of `t` in `α` and the product `s × t`. -/ noncomputable def preimage_mk_equiv_subgroup_times_set (s : subgroup α) (t : set (quotient s)) : quotient_group.mk ⁻¹' t ≃ s × t := have h : ∀ {x : quotient s} {a : α}, x ∈ t → a ∈ s → (quotient.mk' (quotient.out' x * a) : quotient s) = quotient.mk' (quotient.out' x) := λ x a hx ha, quotient.sound' (show (quotient.out' x * a)⁻¹ * quotient.out' x ∈ s, from (s.inv_mem_iff).1 $ by rwa [mul_inv_rev, inv_inv, ← mul_assoc, inv_mul_self, one_mul]), { to_fun := λ ⟨a, ha⟩, ⟨⟨(quotient.out' (quotient.mk' a))⁻¹ * a, @quotient.exact' _ (left_rel s) _ _ $ (quotient.out_eq' _)⟩, ⟨quotient.mk' a, ha⟩⟩, inv_fun := λ ⟨⟨a, ha⟩, ⟨x, hx⟩⟩, ⟨quotient.out' x * a, show quotient.mk' _ ∈ t, by simp [h hx ha, hx]⟩, left_inv := λ ⟨a, ha⟩, subtype.eq $ show _ * _ = a, by simp, right_inv := λ ⟨⟨a, ha⟩, ⟨x, hx⟩⟩, show (_, _) = _, by simp [h hx ha] } end quotient_group /-- We use the class `has_coe_t` instead of `has_coe` if the first argument is a variable, or if the second argument is a variable not occurring in the first. Using `has_coe` would cause looping of type-class inference. See <https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/remove.20all.20instances.20with.20variable.20domain> -/ library_note "use has_coe_t"
e3fe301c8cf37abdc3d7c75efecd40aa7e581c7e
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/data/set/map.lean
f9e99a0244777395d8779f7d45aa168ff1dc456b
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
6,179
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad, Andrew Zipperer Functions between subsets of finite types, bundled with the domain and range. -/ import data.set.function open eq.ops namespace set record map {X Y : Type} (a : set X) (b : set Y) := (func : X → Y) (mapsto : maps_to func a b) attribute map.func [coercion] namespace map variables {X Y Z: Type} variables {a : set X} {b : set Y} {c : set Z} /- the equivalence relation -/ protected definition equiv [reducible] (f1 f2 : map a b) : Prop := eq_on f1 f2 a namespace equiv_notation infix `~` := map.equiv end equiv_notation open equiv_notation protected theorem equiv.refl (f : map a b) : f ~ f := take x, assume H, rfl protected theorem equiv.symm {f₁ f₂ : map a b} : f₁ ~ f₂ → f₂ ~ f₁ := assume H : f₁ ~ f₂, take x, assume Ha : x ∈ a, eq.symm (H Ha) protected theorem equiv.trans {f₁ f₂ f₃ : map a b} : f₁ ~ f₂ → f₂ ~ f₃ → f₁ ~ f₃ := assume H₁ : f₁ ~ f₂, assume H₂ : f₂ ~ f₃, take x, assume Ha : x ∈ a, eq.trans (H₁ Ha) (H₂ Ha) protected theorem equiv.is_equivalence {X Y : Type} (a : set X) (b : set Y) : equivalence (@map.equiv X Y a b) := mk_equivalence (@map.equiv X Y a b) (@equiv.refl X Y a b) (@equiv.symm X Y a b) (@equiv.trans X Y a b) /- compose -/ protected definition comp (g : map b c) (f : map a b) : map a c := map.mk (#function g ∘ f) (maps_to_comp (mapsto g) (mapsto f)) notation g ∘ f := map.comp g f /- range -/ protected definition range (f : map a b) : set Y := image f a theorem range_eq_range_of_equiv {f1 f2 : map a b} (H : f1 ~ f2) : map.range f1 = map.range f2 := image_eq_image_of_eq_on H /- injective -/ protected definition injective (f : map a b) : Prop := inj_on f a theorem injective_of_equiv {f1 f2 : map a b} (H1 : f1 ~ f2) (H2 : map.injective f1) : map.injective f2 := inj_on_of_eq_on H1 H2 theorem injective_comp {g : map b c} {f : map a b} (Hg : map.injective g) (Hf: map.injective f) : map.injective (g ∘ f) := inj_on_comp (mapsto f) Hg Hf /- surjective -/ protected definition surjective (f : map a b) : Prop := surj_on f a b theorem surjective_of_equiv {f1 f2 : map a b} (H1 : f1 ~ f2) (H2 : map.surjective f1) : map.surjective f2 := surj_on_of_eq_on H1 H2 theorem surjective_comp {g : map b c} {f : map a b} (Hg : map.surjective g) (Hf: map.surjective f) : map.surjective (g ∘ f) := surj_on_comp Hg Hf theorem image_eq_of_surjective {f : map a b} (H : map.surjective f) : f ' a = b := image_eq_of_maps_to_of_surj_on (map.mapsto f) H /- bijective -/ protected definition bijective (f : map a b) : Prop := map.injective f ∧ map.surjective f theorem bijective_of_equiv {f1 f2 : map a b} (H1 : f1 ~ f2) (H2 : map.bijective f1) : map.bijective f2 := and.intro (injective_of_equiv H1 (and.left H2)) (surjective_of_equiv H1 (and.right H2)) theorem bijective_comp {g : map b c} {f : map a b} (Hg : map.bijective g) (Hf: map.bijective f) : map.bijective (g ∘ f) := obtain Hg₁ Hg₂, from Hg, obtain Hf₁ Hf₂, from Hf, and.intro (injective_comp Hg₁ Hf₁) (surjective_comp Hg₂ Hf₂) theorem image_eq_of_bijective {f : map a b} (H : map.bijective f) : f ' a = b := image_eq_of_surjective (proof and.right H qed) /- left inverse -/ -- g is a left inverse to f protected definition left_inverse (g : map b a) (f : map a b) : Prop := left_inv_on g f a theorem left_inverse_of_equiv_left {g1 g2 : map b a} {f : map a b} (eqg : g1 ~ g2) (H : map.left_inverse g1 f) : map.left_inverse g2 f := left_inv_on_of_eq_on_left (mapsto f) eqg H theorem left_inverse_of_equiv_right {g : map b a} {f1 f2 : map a b} (eqf : f1 ~ f2) (H : map.left_inverse g f1) : map.left_inverse g f2 := left_inv_on_of_eq_on_right eqf H theorem injective_of_left_inverse {g : map b a} {f : map a b} (H : map.left_inverse g f) : map.injective f := inj_on_of_left_inv_on H theorem left_inverse_comp {f' : map b a} {g' : map c b} {g : map b c} {f : map a b} (Hf : map.left_inverse f' f) (Hg : map.left_inverse g' g) : map.left_inverse (f' ∘ g') (g ∘ f) := begin unfold map.left_inverse, exact left_inv_on_comp (mapsto f) Hf Hg end /- right inverse -/ -- g is a right inverse to f protected definition right_inverse (g : map b a) (f : map a b) : Prop := map.left_inverse f g theorem right_inverse_of_equiv_left {g1 g2 : map b a} {f : map a b} (eqg : g1 ~ g2) (H : map.right_inverse g1 f) : map.right_inverse g2 f := map.left_inverse_of_equiv_right eqg H theorem right_inverse_of_equiv_right {g : map b a} {f1 f2 : map a b} (eqf : f1 ~ f2) (H : map.right_inverse g f1) : map.right_inverse g f2 := map.left_inverse_of_equiv_left eqf H theorem right_inverse_of_injective_of_left_inverse {f : map a b} {g : map b a} (injf : map.injective f) (lfg : map.left_inverse f g) : map.right_inverse f g := right_inv_on_of_inj_on_of_left_inv_on (mapsto f) (mapsto g) injf lfg theorem surjective_of_right_inverse {g : map b a} {f : map a b} (H : map.right_inverse g f) : map.surjective f := surj_on_of_right_inv_on (mapsto g) H theorem left_inverse_of_surjective_of_right_inverse {f : map a b} {g : map b a} (surjf : map.surjective f) (rfg : map.right_inverse f g) : map.left_inverse f g := left_inv_on_of_surj_on_right_inv_on surjf rfg theorem right_inverse_comp {f' : map b a} {g' : map c b} {g : map b c} {f : map a b} (Hf : map.right_inverse f' f) (Hg : map.right_inverse g' g) : map.right_inverse (f' ∘ g') (g ∘ f) := map.left_inverse_comp Hg Hf theorem equiv_of_map.left_inverse_of_right_inverse {g1 g2 : map b a} {f : map a b} (H1 : map.left_inverse g1 f) (H2 : map.right_inverse g2 f) : g1 ~ g2 := eq_on_of_left_inv_of_right_inv (mapsto g2) H1 H2 /- inverse -/ -- g is an inverse to f protected definition is_inverse (g : map b a) (f : map a b) : Prop := map.left_inverse g f ∧ map.right_inverse g f theorem bijective_of_is_inverse {g : map b a} {f : map a b} (H : map.is_inverse g f) : map.bijective f := and.intro (injective_of_left_inverse (and.left H)) (surjective_of_right_inverse (and.right H)) end map end set
2f7674e7d8f60f0f7f6d3cae09cc719b6ba3edb7
6e9cd8d58e550c481a3b45806bd34a3514c6b3e0
/src/data/rat/basic.lean
09b8644a28420307433425d529381ba4d83e689d
[ "Apache-2.0" ]
permissive
sflicht/mathlib
220fd16e463928110e7b0a50bbed7b731979407f
1b2048d7195314a7e34e06770948ee00f0ac3545
refs/heads/master
1,665,934,056,043
1,591,373,803,000
1,591,373,803,000
269,815,267
0
0
Apache-2.0
1,591,402,068,000
1,591,402,067,000
null
UTF-8
Lean
false
false
25,329
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import data.int.sqrt import data.equiv.encodable import algebra.group import algebra.euclidean_domain import algebra.ordered_field /-! # Basics for the Rational Numbers ## Summary We define a rational number `q` as a structure `{ num, denom, pos, cop }`, where - `num` is the numerator of `q`, - `denom` is the denominator of `q`, - `pos` is a proof that `denom > 0`, and - `cop` is a proof `num` and `denom` are coprime. We then define the expected (discrete) field structure on `ℚ` and prove basic lemmas about it. Moreoever, we provide the expected casts from `ℕ` and `ℤ` into `ℚ`, i.e. `(↑n : ℚ) = n / 1`. ## Main Definitions - `rat` is the structure encoding `ℚ`. - `rat.mk n d` constructs a rational number `q = n / d` from `n d : ℤ`. ## Notations - `/.` is infix notation for `rat.mk`. ## Tags rat, rationals, field, ℚ, numerator, denominator, num, denom -/ /-- `rat`, or `ℚ`, is the type of rational numbers. It is defined as the set of pairs ⟨n, d⟩ of integers such that `d` is positive and `n` and `d` are coprime. This representation is preferred to the quotient because without periodic reduction, the numerator and denominator can grow exponentially (for example, adding 1/2 to itself repeatedly). -/ structure rat := mk' :: (num : ℤ) (denom : ℕ) (pos : 0 < denom) (cop : num.nat_abs.coprime denom) notation `ℚ` := rat namespace rat protected def repr : ℚ → string | ⟨n, d, _, _⟩ := if d = 1 then _root_.repr n else _root_.repr n ++ "/" ++ _root_.repr d instance : has_repr ℚ := ⟨rat.repr⟩ instance : has_to_string ℚ := ⟨rat.repr⟩ meta instance : has_to_format ℚ := ⟨coe ∘ rat.repr⟩ instance : encodable ℚ := encodable.of_equiv (Σ n : ℤ, {d : ℕ // 0 < d ∧ n.nat_abs.coprime d}) ⟨λ ⟨a, b, c, d⟩, ⟨a, b, c, d⟩, λ⟨a, b, c, d⟩, ⟨a, b, c, d⟩, λ ⟨a, b, c, d⟩, rfl, λ⟨a, b, c, d⟩, rfl⟩ /-- Embed an integer as a rational number -/ def of_int (n : ℤ) : ℚ := ⟨n, 1, nat.one_pos, nat.coprime_one_right _⟩ instance : has_zero ℚ := ⟨of_int 0⟩ instance : has_one ℚ := ⟨of_int 1⟩ instance : inhabited ℚ := ⟨0⟩ /-- Form the quotient `n / d` where `n:ℤ` and `d:ℕ+` (not necessarily coprime) -/ def mk_pnat (n : ℤ) : ℕ+ → ℚ | ⟨d, dpos⟩ := let n' := n.nat_abs, g := n'.gcd d in ⟨n / g, d / g, begin apply (nat.le_div_iff_mul_le _ _ (nat.gcd_pos_of_pos_right _ dpos)).2, simp, exact nat.le_of_dvd dpos (nat.gcd_dvd_right _ _) end, begin have : int.nat_abs (n / ↑g) = n' / g, { cases int.nat_abs_eq n with e e; rw e, { refl }, rw [int.neg_div_of_dvd, int.nat_abs_neg], { refl }, exact int.coe_nat_dvd.2 (nat.gcd_dvd_left _ _) }, rw this, exact nat.coprime_div_gcd_div_gcd (nat.gcd_pos_of_pos_right _ dpos) end⟩ /-- Form the quotient `n / d` where `n:ℤ` and `d:ℕ`. In the case `d = 0`, we define `n / 0 = 0` by convention. -/ def mk_nat (n : ℤ) (d : ℕ) : ℚ := if d0 : d = 0 then 0 else mk_pnat n ⟨d, nat.pos_of_ne_zero d0⟩ /-- Form the quotient `n / d` where `n d : ℤ`. -/ def mk : ℤ → ℤ → ℚ | n (d : ℕ) := mk_nat n d | n -[1+ d] := mk_pnat (-n) d.succ_pnat localized "infix ` /. `:70 := rat.mk" in rat theorem mk_pnat_eq (n d h) : mk_pnat n ⟨d, h⟩ = n /. d := by change n /. d with dite _ _ _; simp [ne_of_gt h] theorem mk_nat_eq (n d) : mk_nat n d = n /. d := rfl @[simp] theorem mk_zero (n) : n /. 0 = 0 := rfl @[simp] theorem zero_mk_pnat (n) : mk_pnat 0 n = 0 := by cases n; simp [mk_pnat]; change int.nat_abs 0 with 0; simp *; refl @[simp] theorem zero_mk_nat (n) : mk_nat 0 n = 0 := by by_cases n = 0; simp [*, mk_nat] @[simp] theorem zero_mk (n) : 0 /. n = 0 := by cases n; simp [mk] private lemma gcd_abs_dvd_left {a b} : (nat.gcd (int.nat_abs a) b : ℤ) ∣ a := int.dvd_nat_abs.1 $ int.coe_nat_dvd.2 $ nat.gcd_dvd_left (int.nat_abs a) b @[simp] theorem mk_eq_zero {a b : ℤ} (b0 : b ≠ 0) : a /. b = 0 ↔ a = 0 := begin constructor; intro h; [skip, {subst a, simp}], have : ∀ {a b}, mk_pnat a b = 0 → a = 0, { intros a b e, cases b with b h, injection e with e, apply int.eq_mul_of_div_eq_right gcd_abs_dvd_left e }, cases b with b; simp [mk, mk_nat] at h, { simp [mt (congr_arg int.of_nat) b0] at h, exact this h }, { apply neg_inj, simp [this h] } end theorem mk_eq : ∀ {a b c d : ℤ} (hb : b ≠ 0) (hd : d ≠ 0), a /. b = c /. d ↔ a * d = c * b := suffices ∀ a b c d hb hd, mk_pnat a ⟨b, hb⟩ = mk_pnat c ⟨d, hd⟩ ↔ a * d = c * b, begin intros, cases b with b b; simp [mk, mk_nat, nat.succ_pnat], simp [mt (congr_arg int.of_nat) hb], all_goals { cases d with d d; simp [mk, mk_nat, nat.succ_pnat], simp [mt (congr_arg int.of_nat) hd], all_goals { rw this, try {refl} } }, { change a * ↑(d.succ) = -c * ↑b ↔ a * -(d.succ) = c * b, constructor; intro h; apply neg_inj; simpa [left_distrib, neg_add_eq_iff_eq_add, eq_neg_iff_add_eq_zero, neg_eq_iff_add_eq_zero] using h }, { change -a * ↑d = c * b.succ ↔ a * d = c * -b.succ, constructor; intro h; apply neg_inj; simpa [left_distrib, eq_comm] using h }, { change -a * d.succ = -c * b.succ ↔ a * -d.succ = c * -b.succ, simp [left_distrib, sub_eq_add_neg], cc } end, begin intros, simp [mk_pnat], constructor; intro h, { cases h with ha hb, have ha, { have dv := @gcd_abs_dvd_left, have := int.eq_mul_of_div_eq_right dv ha, rw ← int.mul_div_assoc _ dv at this, exact int.eq_mul_of_div_eq_left (dvd_mul_of_dvd_right dv _) this.symm }, have hb, { have dv := λ {a b}, nat.gcd_dvd_right (int.nat_abs a) b, have := nat.eq_mul_of_div_eq_right dv hb, rw ← nat.mul_div_assoc _ dv at this, exact nat.eq_mul_of_div_eq_left (dvd_mul_of_dvd_right dv _) this.symm }, have m0 : (a.nat_abs.gcd b * c.nat_abs.gcd d : ℤ) ≠ 0, { refine int.coe_nat_ne_zero.2 (ne_of_gt _), apply mul_pos; apply nat.gcd_pos_of_pos_right; assumption }, apply eq_of_mul_eq_mul_right m0, simpa [mul_comm, mul_left_comm] using congr (congr_arg (*) ha.symm) (congr_arg coe hb) }, { suffices : ∀ a c, a * d = c * b → a / a.gcd b = c / c.gcd d ∧ b / a.gcd b = d / c.gcd d, { cases this a.nat_abs c.nat_abs (by simpa [int.nat_abs_mul] using congr_arg int.nat_abs h) with h₁ h₂, have hs := congr_arg int.sign h, simp [int.sign_eq_one_of_pos (int.coe_nat_lt.2 hb), int.sign_eq_one_of_pos (int.coe_nat_lt.2 hd)] at hs, conv in a { rw ← int.sign_mul_nat_abs a }, conv in c { rw ← int.sign_mul_nat_abs c }, rw [int.mul_div_assoc, int.mul_div_assoc], exact ⟨congr (congr_arg (*) hs) (congr_arg coe h₁), h₂⟩, all_goals { exact int.coe_nat_dvd.2 (nat.gcd_dvd_left _ _) } }, intros a c h, suffices bd : b / a.gcd b = d / c.gcd d, { refine ⟨_, bd⟩, apply nat.eq_of_mul_eq_mul_left hb, rw [← nat.mul_div_assoc _ (nat.gcd_dvd_left _ _), mul_comm, nat.mul_div_assoc _ (nat.gcd_dvd_right _ _), bd, ← nat.mul_div_assoc _ (nat.gcd_dvd_right _ _), h, mul_comm, nat.mul_div_assoc _ (nat.gcd_dvd_left _ _)] }, suffices : ∀ {a c : ℕ} (b>0) (d>0), a * d = c * b → b / a.gcd b ≤ d / c.gcd d, { exact le_antisymm (this _ hb _ hd h) (this _ hd _ hb h.symm) }, intros a c b hb d hd h, have gb0 := nat.gcd_pos_of_pos_right a hb, have gd0 := nat.gcd_pos_of_pos_right c hd, apply nat.le_of_dvd, apply (nat.le_div_iff_mul_le _ _ gd0).2, simp, apply nat.le_of_dvd hd (nat.gcd_dvd_right _ _), apply (nat.coprime_div_gcd_div_gcd gb0).symm.dvd_of_dvd_mul_left, refine ⟨c / c.gcd d, _⟩, rw [← nat.mul_div_assoc _ (nat.gcd_dvd_left _ _), ← nat.mul_div_assoc _ (nat.gcd_dvd_right _ _)], apply congr_arg (/ c.gcd d), rw [mul_comm, ← nat.mul_div_assoc _ (nat.gcd_dvd_left _ _), mul_comm, h, nat.mul_div_assoc _ (nat.gcd_dvd_right _ _), mul_comm] } end @[simp] theorem div_mk_div_cancel_left {a b c : ℤ} (c0 : c ≠ 0) : (a * c) /. (b * c) = a /. b := begin by_cases b0 : b = 0, { subst b0, simp }, apply (mk_eq (mul_ne_zero b0 c0) b0).2, simp [mul_comm, mul_assoc] end @[simp] theorem num_denom : ∀ {a : ℚ}, a.num /. a.denom = a | ⟨n, d, h, (c:_=1)⟩ := show mk_nat n d = _, by simp [mk_nat, ne_of_gt h, mk_pnat, c] theorem num_denom' {n d h c} : (⟨n, d, h, c⟩ : ℚ) = n /. d := num_denom.symm theorem of_int_eq_mk (z : ℤ) : of_int z = z /. 1 := num_denom' @[elab_as_eliminator] def {u} num_denom_cases_on {C : ℚ → Sort u} : ∀ (a : ℚ) (H : ∀ n d, 0 < d → (int.nat_abs n).coprime d → C (n /. d)), C a | ⟨n, d, h, c⟩ H := by rw num_denom'; exact H n d h c @[elab_as_eliminator] def {u} num_denom_cases_on' {C : ℚ → Sort u} (a : ℚ) (H : ∀ (n:ℤ) (d:ℕ), d ≠ 0 → C (n /. d)) : C a := num_denom_cases_on a $ λ n d h c, H n d $ ne_of_gt h theorem num_dvd (a) {b : ℤ} (b0 : b ≠ 0) : (a /. b).num ∣ a := begin cases e : a /. b with n d h c, rw [rat.num_denom', rat.mk_eq b0 (ne_of_gt (int.coe_nat_pos.2 h))] at e, refine (int.nat_abs_dvd.1 $ int.dvd_nat_abs.1 $ int.coe_nat_dvd.2 $ c.dvd_of_dvd_mul_right _), have := congr_arg int.nat_abs e, simp [int.nat_abs_mul, int.nat_abs_of_nat] at this, simp [this] end theorem denom_dvd (a b : ℤ) : ((a /. b).denom : ℤ) ∣ b := begin by_cases b0 : b = 0, {simp [b0]}, cases e : a /. b with n d h c, rw [num_denom', mk_eq b0 (ne_of_gt (int.coe_nat_pos.2 h))] at e, refine (int.dvd_nat_abs.1 $ int.coe_nat_dvd.2 $ c.symm.dvd_of_dvd_mul_left _), rw [← int.nat_abs_mul, ← int.coe_nat_dvd, int.dvd_nat_abs, ← e], simp end protected def add : ℚ → ℚ → ℚ | ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ := mk_pnat (n₁ * d₂ + n₂ * d₁) ⟨d₁ * d₂, mul_pos h₁ h₂⟩ instance : has_add ℚ := ⟨rat.add⟩ theorem lift_binop_eq (f : ℚ → ℚ → ℚ) (f₁ : ℤ → ℤ → ℤ → ℤ → ℤ) (f₂ : ℤ → ℤ → ℤ → ℤ → ℤ) (fv : ∀ {n₁ d₁ h₁ c₁ n₂ d₂ h₂ c₂}, f ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ = f₁ n₁ d₁ n₂ d₂ /. f₂ n₁ d₁ n₂ d₂) (f0 : ∀ {n₁ d₁ n₂ d₂} (d₁0 : d₁ ≠ 0) (d₂0 : d₂ ≠ 0), f₂ n₁ d₁ n₂ d₂ ≠ 0) (a b c d : ℤ) (b0 : b ≠ 0) (d0 : d ≠ 0) (H : ∀ {n₁ d₁ n₂ d₂} (h₁ : a * d₁ = n₁ * b) (h₂ : c * d₂ = n₂ * d), f₁ n₁ d₁ n₂ d₂ * f₂ a b c d = f₁ a b c d * f₂ n₁ d₁ n₂ d₂) : f (a /. b) (c /. d) = f₁ a b c d /. f₂ a b c d := begin generalize ha : a /. b = x, cases x with n₁ d₁ h₁ c₁, rw num_denom' at ha, generalize hc : c /. d = x, cases x with n₂ d₂ h₂ c₂, rw num_denom' at hc, rw fv, have d₁0 := ne_of_gt (int.coe_nat_lt.2 h₁), have d₂0 := ne_of_gt (int.coe_nat_lt.2 h₂), exact (mk_eq (f0 d₁0 d₂0) (f0 b0 d0)).2 (H ((mk_eq b0 d₁0).1 ha) ((mk_eq d0 d₂0).1 hc)) end @[simp] theorem add_def {a b c d : ℤ} (b0 : b ≠ 0) (d0 : d ≠ 0) : a /. b + c /. d = (a * d + c * b) /. (b * d) := begin apply lift_binop_eq rat.add; intros; try {assumption}, { apply mk_pnat_eq }, { apply mul_ne_zero d₁0 d₂0 }, calc (n₁ * d₂ + n₂ * d₁) * (b * d) = (n₁ * b) * d₂ * d + (n₂ * d) * (d₁ * b) : by simp [mul_add, mul_comm, mul_left_comm] ... = (a * d₁) * d₂ * d + (c * d₂) * (d₁ * b) : by rw [h₁, h₂] ... = (a * d + c * b) * (d₁ * d₂) : by simp [mul_add, mul_comm, mul_left_comm] end protected def neg : ℚ → ℚ | ⟨n, d, h, c⟩ := ⟨-n, d, h, by simp [c]⟩ instance : has_neg ℚ := ⟨rat.neg⟩ @[simp] theorem neg_def {a b : ℤ} : -(a /. b) = -a /. b := begin by_cases b0 : b = 0, { subst b0, simp, refl }, generalize ha : a /. b = x, cases x with n₁ d₁ h₁ c₁, rw num_denom' at ha, show rat.mk' _ _ _ _ = _, rw num_denom', have d0 := ne_of_gt (int.coe_nat_lt.2 h₁), apply (mk_eq d0 b0).2, have h₁ := (mk_eq b0 d0).1 ha, simp only [neg_mul_eq_neg_mul_symm, congr_arg has_neg.neg h₁] end protected def mul : ℚ → ℚ → ℚ | ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ := mk_pnat (n₁ * n₂) ⟨d₁ * d₂, mul_pos h₁ h₂⟩ instance : has_mul ℚ := ⟨rat.mul⟩ @[simp] theorem mul_def {a b c d : ℤ} (b0 : b ≠ 0) (d0 : d ≠ 0) : (a /. b) * (c /. d) = (a * c) /. (b * d) := begin apply lift_binop_eq rat.mul; intros; try {assumption}, { apply mk_pnat_eq }, { apply mul_ne_zero d₁0 d₂0 }, cc end protected def inv : ℚ → ℚ | ⟨(n+1:ℕ), d, h, c⟩ := ⟨d, n+1, n.succ_pos, c.symm⟩ | ⟨0, d, h, c⟩ := 0 | ⟨-[1+ n], d, h, c⟩ := ⟨-d, n+1, n.succ_pos, nat.coprime.symm $ by simp; exact c⟩ instance : has_inv ℚ := ⟨rat.inv⟩ @[simp] theorem inv_def {a b : ℤ} : (a /. b)⁻¹ = b /. a := begin by_cases a0 : a = 0, { subst a0, simp, refl }, by_cases b0 : b = 0, { subst b0, simp, refl }, generalize ha : a /. b = x, cases x with n d h c, rw num_denom' at ha, refine eq.trans (_ : rat.inv ⟨n, d, h, c⟩ = d /. n) _, { cases n with n; [cases n with n, skip], { refl }, { change int.of_nat n.succ with (n+1:ℕ), unfold rat.inv, rw num_denom' }, { unfold rat.inv, rw num_denom', refl } }, have n0 : n ≠ 0, { refine mt (λ (n0 : n = 0), _) a0, subst n0, simp at ha, exact (mk_eq_zero b0).1 ha }, have d0 := ne_of_gt (int.coe_nat_lt.2 h), have ha := (mk_eq b0 d0).1 ha, apply (mk_eq n0 a0).2, cc end variables (a b c : ℚ) protected theorem add_zero : a + 0 = a := num_denom_cases_on' a $ λ n d h, by rw [← zero_mk d]; simp [h, -zero_mk] protected theorem zero_add : 0 + a = a := num_denom_cases_on' a $ λ n d h, by rw [← zero_mk d]; simp [h, -zero_mk] protected theorem add_comm : a + b = b + a := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, by simp [h₁, h₂]; cc protected theorem add_assoc : a + b + c = a + (b + c) := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, num_denom_cases_on' c $ λ n₃ d₃ h₃, by simp [h₁, h₂, h₃, mul_ne_zero, mul_add, mul_comm, mul_left_comm, add_left_comm, add_assoc] protected theorem add_left_neg : -a + a = 0 := num_denom_cases_on' a $ λ n d h, by simp [h] protected theorem mul_one : a * 1 = a := num_denom_cases_on' a $ λ n d h, by change (1:ℚ) with 1 /. 1; simp [h] protected theorem one_mul : 1 * a = a := num_denom_cases_on' a $ λ n d h, by change (1:ℚ) with 1 /. 1; simp [h] protected theorem mul_comm : a * b = b * a := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, by simp [h₁, h₂, mul_comm] protected theorem mul_assoc : a * b * c = a * (b * c) := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, num_denom_cases_on' c $ λ n₃ d₃ h₃, by simp [h₁, h₂, h₃, mul_ne_zero, mul_comm, mul_left_comm] protected theorem add_mul : (a + b) * c = a * c + b * c := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, num_denom_cases_on' c $ λ n₃ d₃ h₃, by simp [h₁, h₂, h₃, mul_ne_zero]; refine (div_mk_div_cancel_left (int.coe_nat_ne_zero.2 h₃)).symm.trans _; simp [mul_add, mul_comm, mul_assoc, mul_left_comm] protected theorem mul_add : a * (b + c) = a * b + a * c := by rw [rat.mul_comm, rat.add_mul, rat.mul_comm, rat.mul_comm c a] protected theorem zero_ne_one : 0 ≠ (1:ℚ) := mt (λ (h : 0 = 1 /. 1), (mk_eq_zero one_ne_zero).1 h.symm) one_ne_zero protected theorem mul_inv_cancel : a ≠ 0 → a * a⁻¹ = 1 := num_denom_cases_on' a $ λ n d h a0, have n0 : n ≠ 0, from mt (by intro e; subst e; simp) a0, by simp [h, n0, mul_comm]; exact eq.trans (by simp) (@div_mk_div_cancel_left 1 1 _ n0) protected theorem inv_mul_cancel (h : a ≠ 0) : a⁻¹ * a = 1 := eq.trans (rat.mul_comm _ _) (rat.mul_inv_cancel _ h) instance : decidable_eq ℚ := by tactic.mk_dec_eq_instance instance : field ℚ := { zero := 0, add := rat.add, neg := rat.neg, one := 1, mul := rat.mul, inv := rat.inv, zero_add := rat.zero_add, add_zero := rat.add_zero, add_comm := rat.add_comm, add_assoc := rat.add_assoc, add_left_neg := rat.add_left_neg, mul_one := rat.mul_one, one_mul := rat.one_mul, mul_comm := rat.mul_comm, mul_assoc := rat.mul_assoc, left_distrib := rat.mul_add, right_distrib := rat.add_mul, zero_ne_one := rat.zero_ne_one, mul_inv_cancel := rat.mul_inv_cancel, inv_zero := rfl } /- Extra instances to short-circuit type class resolution -/ instance : division_ring ℚ := by apply_instance instance : integral_domain ℚ := by apply_instance -- TODO(Mario): this instance slows down data.real.basic --instance : domain ℚ := by apply_instance instance : nonzero ℚ := by apply_instance instance : comm_ring ℚ := by apply_instance --instance : ring ℚ := by apply_instance instance : comm_semiring ℚ := by apply_instance instance : semiring ℚ := by apply_instance instance : add_comm_group ℚ := by apply_instance instance : add_group ℚ := by apply_instance instance : add_comm_monoid ℚ := by apply_instance instance : add_monoid ℚ := by apply_instance instance : add_left_cancel_semigroup ℚ := by apply_instance instance : add_right_cancel_semigroup ℚ := by apply_instance instance : add_comm_semigroup ℚ := by apply_instance instance : add_semigroup ℚ := by apply_instance instance : comm_monoid ℚ := by apply_instance instance : monoid ℚ := by apply_instance instance : comm_semigroup ℚ := by apply_instance instance : semigroup ℚ := by apply_instance theorem sub_def {a b c d : ℤ} (b0 : b ≠ 0) (d0 : d ≠ 0) : a /. b - c /. d = (a * d - c * b) /. (b * d) := by simp [b0, d0, sub_eq_add_neg] @[simp] lemma denom_neg_eq_denom : ∀ q : ℚ, (-q).denom = q.denom | ⟨_, d, _, _⟩ := rfl @[simp] lemma num_neg_eq_neg_num : ∀ q : ℚ, (-q).num = -(q.num) | ⟨n, _, _, _⟩ := rfl @[simp] lemma num_zero : rat.num 0 = 0 := rfl lemma zero_of_num_zero {q : ℚ} (hq : q.num = 0) : q = 0 := have q = q.num /. q.denom, from num_denom.symm, by simpa [hq] lemma zero_iff_num_zero {q : ℚ} : q = 0 ↔ q.num = 0 := ⟨λ _, by simp *, zero_of_num_zero⟩ lemma num_ne_zero_of_ne_zero {q : ℚ} (h : q ≠ 0) : q.num ≠ 0 := assume : q.num = 0, h $ zero_of_num_zero this @[simp] lemma num_one : (1 : ℚ).num = 1 := rfl @[simp] lemma denom_one : (1 : ℚ).denom = 1 := rfl lemma denom_ne_zero (q : ℚ) : q.denom ≠ 0 := ne_of_gt q.pos lemma eq_iff_mul_eq_mul {p q : ℚ} : p = q ↔ p.num * q.denom = q.num * p.denom := begin conv_lhs { rw [←(@num_denom p), ←(@num_denom q)] }, apply rat.mk_eq, { exact_mod_cast p.denom_ne_zero }, { exact_mod_cast q.denom_ne_zero } end lemma mk_num_ne_zero_of_ne_zero {q : ℚ} {n d : ℤ} (hq : q ≠ 0) (hqnd : q = n /. d) : n ≠ 0 := assume : n = 0, hq $ by simpa [this] using hqnd lemma mk_denom_ne_zero_of_ne_zero {q : ℚ} {n d : ℤ} (hq : q ≠ 0) (hqnd : q = n /. d) : d ≠ 0 := assume : d = 0, hq $ by simpa [this] using hqnd lemma mk_ne_zero_of_ne_zero {n d : ℤ} (h : n ≠ 0) (hd : d ≠ 0) : n /. d ≠ 0 := assume : n /. d = 0, h $ (mk_eq_zero hd).1 this lemma mul_num_denom (q r : ℚ) : q * r = (q.num * r.num) /. ↑(q.denom * r.denom) := have hq' : (↑q.denom : ℤ) ≠ 0, by have := denom_ne_zero q; simpa, have hr' : (↑r.denom : ℤ) ≠ 0, by have := denom_ne_zero r; simpa, suffices (q.num /. ↑q.denom) * (r.num /. ↑r.denom) = (q.num * r.num) /. ↑(q.denom * r.denom), by simpa using this, by simp [mul_def hq' hr', -num_denom] lemma div_num_denom (q r : ℚ) : q / r = (q.num * r.denom) /. (q.denom * r.num) := if hr : r.num = 0 then have hr' : r = 0, from zero_of_num_zero hr, by simp * else calc q / r = q * r⁻¹ : div_eq_mul_inv ... = (q.num /. q.denom) * (r.num /. r.denom)⁻¹ : by simp ... = (q.num /. q.denom) * (r.denom /. r.num) : by rw inv_def ... = (q.num * r.denom) /. (q.denom * r.num) : mul_def (by simpa using denom_ne_zero q) hr lemma num_denom_mk {q : ℚ} {n d : ℤ} (hn : n ≠ 0) (hd : d ≠ 0) (qdf : q = n /. d) : ∃ c : ℤ, n = c * q.num ∧ d = c * q.denom := have hq : q ≠ 0, from assume : q = 0, hn $ (rat.mk_eq_zero hd).1 (by cc), have q.num /. q.denom = n /. d, by rwa [num_denom], have q.num * d = n * ↑(q.denom), from (rat.mk_eq (by simp [rat.denom_ne_zero]) hd).1 this, begin existsi n / q.num, have hqdn : q.num ∣ n, begin rw qdf, apply rat.num_dvd, assumption end, split, { rw int.div_mul_cancel hqdn }, { apply int.eq_mul_div_of_mul_eq_mul_of_dvd_left, { apply rat.num_ne_zero_of_ne_zero hq }, repeat { assumption } } end theorem mk_pnat_num (n : ℤ) (d : ℕ+) : (mk_pnat n d).num = n / nat.gcd n.nat_abs d := by cases d; refl theorem mk_pnat_denom (n : ℤ) (d : ℕ+) : (mk_pnat n d).denom = d / nat.gcd n.nat_abs d := by cases d; refl theorem mul_num (q₁ q₂ : ℚ) : (q₁ * q₂).num = (q₁.num * q₂.num) / nat.gcd (q₁.num * q₂.num).nat_abs (q₁.denom * q₂.denom) := by cases q₁; cases q₂; refl theorem mul_denom (q₁ q₂ : ℚ) : (q₁ * q₂).denom = (q₁.denom * q₂.denom) / nat.gcd (q₁.num * q₂.num).nat_abs (q₁.denom * q₂.denom) := by cases q₁; cases q₂; refl theorem mul_self_num (q : ℚ) : (q * q).num = q.num * q.num := by rw [mul_num, int.nat_abs_mul, nat.coprime.gcd_eq_one, int.coe_nat_one, int.div_one]; exact (q.cop.mul_right q.cop).mul (q.cop.mul_right q.cop) theorem mul_self_denom (q : ℚ) : (q * q).denom = q.denom * q.denom := by rw [rat.mul_denom, int.nat_abs_mul, nat.coprime.gcd_eq_one, nat.div_one]; exact (q.cop.mul_right q.cop).mul (q.cop.mul_right q.cop) lemma add_num_denom (q r : ℚ) : q + r = ((q.num * r.denom + q.denom * r.num : ℤ)) /. (↑q.denom * ↑r.denom : ℤ) := have hqd : (q.denom : ℤ) ≠ 0, from int.coe_nat_ne_zero_iff_pos.2 q.3, have hrd : (r.denom : ℤ) ≠ 0, from int.coe_nat_ne_zero_iff_pos.2 r.3, by conv_lhs { rw [←@num_denom q, ←@num_denom r, rat.add_def hqd hrd] }; simp [mul_comm] section casts theorem coe_int_eq_mk : ∀ (z : ℤ), ↑z = z /. 1 | (n : ℕ) := show (n:ℚ) = n /. 1, by induction n with n IH n; simp [*, show (1:ℚ) = 1 /. 1, from rfl] | -[1+ n] := show (-(n + 1) : ℚ) = -[1+ n] /. 1, begin induction n with n IH, {refl}, show -(n + 1 + 1 : ℚ) = -[1+ n.succ] /. 1, rw [neg_add, IH], simpa [show -1 = (-1) /. 1, from rfl] end theorem mk_eq_div (n d : ℤ) : n /. d = ((n : ℚ) / d) := begin by_cases d0 : d = 0, {simp [d0, div_zero]}, simp [division_def, coe_int_eq_mk, mul_def one_ne_zero d0] end theorem coe_int_eq_of_int (z : ℤ) : ↑z = of_int z := (coe_int_eq_mk z).trans (of_int_eq_mk z).symm @[simp, norm_cast] theorem coe_int_num (n : ℤ) : (n : ℚ).num = n := by rw coe_int_eq_of_int; refl @[simp, norm_cast] theorem coe_int_denom (n : ℤ) : (n : ℚ).denom = 1 := by rw coe_int_eq_of_int; refl lemma coe_int_num_of_denom_eq_one {q : ℚ} (hq : q.denom = 1) : ↑(q.num) = q := by { conv_rhs { rw [←(@num_denom q), hq] }, rw [coe_int_eq_mk], refl } instance : can_lift ℚ ℤ := ⟨coe, λ q, q.denom = 1, λ q hq, ⟨q.num, coe_int_num_of_denom_eq_one hq⟩⟩ theorem coe_nat_eq_mk (n : ℕ) : ↑n = n /. 1 := by rw [← int.cast_coe_nat, coe_int_eq_mk] @[simp, norm_cast] theorem coe_nat_num (n : ℕ) : (n : ℚ).num = n := by rw [← int.cast_coe_nat, coe_int_num] @[simp, norm_cast] theorem coe_nat_denom (n : ℕ) : (n : ℚ).denom = 1 := by rw [← int.cast_coe_nat, coe_int_denom] @[simp, norm_cast] lemma coe_int_inj (m n : ℤ) : (m : ℚ) = n ↔ m = n := ⟨λ h, by simpa using congr_arg num h, congr_arg _⟩ end casts lemma inv_def' {q : ℚ} : q⁻¹ = (q.denom : ℚ) / q.num := by { conv_lhs { rw ←(@num_denom q) }, cases q, simp [div_num_denom] } @[simp] lemma mul_own_denom_eq_num {q : ℚ} : q * q.denom = q.num := begin suffices : mk (q.num) ↑(q.denom) * mk ↑(q.denom) 1 = mk (q.num) 1, by { conv { for q [1] { rw ←(@num_denom q) }}, rwa [coe_int_eq_mk, coe_nat_eq_mk] }, have : (q.denom : ℤ) ≠ 0, from ne_of_gt (by exact_mod_cast q.pos), rw [(rat.mul_def this one_ne_zero), (mul_comm (q.denom : ℤ) 1), (div_mk_div_cancel_left this)] end lemma denom_div_cast_eq_one_iff (m n : ℤ) (hn : n ≠ 0) : ((m : ℚ) / n).denom = 1 ↔ n ∣ m := begin replace hn : (n:ℚ) ≠ 0, by rwa [ne.def, ← int.cast_zero, coe_int_inj], split, { intro h, lift ((m : ℚ) / n) to ℤ using h with k hk, use k, rwa [eq_div_iff_mul_eq _ _ hn, ← int.cast_mul, mul_comm, eq_comm, coe_int_inj] at hk }, { rintros ⟨d, rfl⟩, rw [int.cast_mul, mul_comm, mul_div_cancel _ hn, rat.coe_int_denom] } end end rat
eda1e9d4bcdc46a04f52e58651a8cb9292772f4c
a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7
/src/field_theory/subfield.lean
59ef6bf636323a5ffcdaf0d87f89323771bb0c8f
[ "Apache-2.0" ]
permissive
kmill/mathlib
ea5a007b67ae4e9e18dd50d31d8aa60f650425ee
1a419a9fea7b959317eddd556e1bb9639f4dcc05
refs/heads/master
1,668,578,197,719
1,593,629,163,000
1,593,629,163,000
276,482,939
0
0
null
1,593,637,960,000
1,593,637,959,000
null
UTF-8
Lean
false
false
4,952
lean
/- Copyright (c) 2018 Andreas Swerdlow. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andreas Swerdlow -/ import ring_theory.subring variables {F : Type*} [field F] (S : set F) section prio set_option default_priority 100 -- see Note [default priority] class is_subfield extends is_subring S : Prop := (inv_mem : ∀ {x : F}, x ∈ S → x⁻¹ ∈ S) end prio instance is_subfield.field [is_subfield S] : field S := { inv := λ x, ⟨x⁻¹, is_subfield.inv_mem x.2⟩, zero_ne_one := λ h, zero_ne_one (subtype.ext_iff_val.1 h), mul_inv_cancel := λ a ha, subtype.ext_iff_val.2 (mul_inv_cancel (λ h, ha $ subtype.ext_iff_val.2 h)), inv_zero := subtype.ext_iff_val.2 inv_zero, ..show comm_ring S, by apply_instance } instance univ.is_subfield : is_subfield (@set.univ F) := { inv_mem := by intros; trivial } /- note: in the next two declarations, if we let type-class inference figure out the instance `ring_hom.is_subring_preimage` then that instance only applies when particular instances of `is_add_subgroup _` and `is_submonoid _` are chosen (which are not the default ones). If we specify it explicitly, then it doesn't complain. -/ instance preimage.is_subfield {K : Type*} [field K] (f : F →+* K) (s : set K) [is_subfield s] : is_subfield (f ⁻¹' s) := { inv_mem := λ a (ha : f a ∈ s), show f a⁻¹ ∈ s, by { rw [f.map_inv], exact is_subfield.inv_mem ha }, ..f.is_subring_preimage s } instance image.is_subfield {K : Type*} [field K] (f : F →+* K) (s : set F) [is_subfield s] : is_subfield (f '' s) := { inv_mem := λ a ⟨x, xmem, ha⟩, ⟨x⁻¹, is_subfield.inv_mem xmem, ha ▸ f.map_inv⟩, ..f.is_subring_image s } instance range.is_subfield {K : Type*} [field K] (f : F →+* K) : is_subfield (set.range f) := by { rw ← set.image_univ, apply_instance } namespace field /-- `field.closure s` is the minimal subfield that includes `s`. -/ def closure : set F := { x | ∃ y ∈ ring.closure S, ∃ z ∈ ring.closure S, y / z = x } variables {S} theorem ring_closure_subset : ring.closure S ⊆ closure S := λ x hx, ⟨x, hx, 1, is_submonoid.one_mem, div_one x⟩ instance closure.is_submonoid : is_submonoid (closure S) := { mul_mem := by rintros _ _ ⟨p, hp, q, hq, hq0, rfl⟩ ⟨r, hr, s, hs, hs0, rfl⟩; exact ⟨p * r, is_submonoid.mul_mem hp hr, q * s, is_submonoid.mul_mem hq hs, (div_mul_div _ _ _ _).symm⟩, one_mem := ring_closure_subset $ is_submonoid.one_mem } instance closure.is_subfield : is_subfield (closure S) := have h0 : (0:F) ∈ closure S, from ring_closure_subset $ is_add_submonoid.zero_mem, { add_mem := begin intros a b ha hb, rcases (id ha) with ⟨p, hp, q, hq, rfl⟩, rcases (id hb) with ⟨r, hr, s, hs, rfl⟩, classical, by_cases hq0 : q = 0, by simp [hb, hq0], by_cases hs0 : s = 0, by simp [ha, hs0], exact ⟨p * s + q * r, is_add_submonoid.add_mem (is_submonoid.mul_mem hp hs) (is_submonoid.mul_mem hq hr), q * s, is_submonoid.mul_mem hq hs, (div_add_div p r hq0 hs0).symm⟩ end, zero_mem := h0, neg_mem := begin rintros _ ⟨p, hp, q, hq, rfl⟩, exact ⟨-p, is_add_subgroup.neg_mem hp, q, hq, neg_div q p⟩ end, inv_mem := begin rintros _ ⟨p, hp, q, hq, rfl⟩, classical, by_cases hp0 : p = 0, by simp [hp0, h0], exact ⟨q, hq, p, hp, inv_div.symm⟩ end } theorem mem_closure {a : F} (ha : a ∈ S) : a ∈ closure S := ring_closure_subset $ ring.mem_closure ha theorem subset_closure : S ⊆ closure S := λ _, mem_closure theorem closure_subset {T : set F} [is_subfield T] (H : S ⊆ T) : closure S ⊆ T := by rintros _ ⟨p, hp, q, hq, hq0, rfl⟩; exact is_submonoid.mul_mem (ring.closure_subset H hp) (is_subfield.inv_mem $ ring.closure_subset H hq) theorem closure_subset_iff (s t : set F) [is_subfield t] : closure s ⊆ t ↔ s ⊆ t := ⟨set.subset.trans subset_closure, closure_subset⟩ theorem closure_mono {s t : set F} (H : s ⊆ t) : closure s ⊆ closure t := closure_subset $ set.subset.trans H subset_closure end field lemma is_subfield_Union_of_directed {ι : Type*} [hι : nonempty ι] (s : ι → set F) [∀ i, is_subfield (s i)] (directed : ∀ i j, ∃ k, s i ⊆ s k ∧ s j ⊆ s k) : is_subfield (⋃i, s i) := { inv_mem := λ x hx, let ⟨i, hi⟩ := set.mem_Union.1 hx in set.mem_Union.2 ⟨i, is_subfield.inv_mem hi⟩, to_is_subring := is_subring_Union_of_directed s directed } instance is_subfield.inter (S₁ S₂ : set F) [is_subfield S₁] [is_subfield S₂] : is_subfield (S₁ ∩ S₂) := { inv_mem := λ x hx, ⟨is_subfield.inv_mem hx.1, is_subfield.inv_mem hx.2⟩ } instance is_subfield.Inter {ι : Sort*} (S : ι → set F) [h : ∀ y : ι, is_subfield (S y)] : is_subfield (set.Inter S) := { inv_mem := λ x hx, set.mem_Inter.2 $ λ y, is_subfield.inv_mem $ set.mem_Inter.1 hx y }
108491ef67e42cbdb7ba90a42c1124b4a2bb8f79
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/list/sections.lean
eea26f06244f2b736e750b22873c92456aa0e314
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
1,458
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.list.forall2 /-! # List sections This file proves some stuff about `list.sections` (definition in `data.list.defs`). A section of a list of lists `[l₁, ..., lₙ]` is a list whose `i`-th element comes from the `i`-th list. -/ open nat function namespace list variables {α β : Type*} theorem mem_sections {L : list (list α)} {f} : f ∈ sections L ↔ forall₂ (∈) f L := begin refine ⟨λ h, _, λ h, _⟩, { induction L generalizing f, {cases mem_singleton.1 h, exact forall₂.nil}, simp only [sections, bind_eq_bind, mem_bind, mem_map] at h, rcases h with ⟨_, _, _, _, rfl⟩, simp only [*, forall₂_cons, true_and] }, { induction h with a l f L al fL fs, {exact or.inl rfl}, simp only [sections, bind_eq_bind, mem_bind, mem_map], exact ⟨_, fs, _, al, rfl, rfl⟩ } end theorem mem_sections_length {L : list (list α)} {f} (h : f ∈ sections L) : length f = length L := forall₂_length_eq (mem_sections.1 h) lemma rel_sections {r : α → β → Prop} : (forall₂ (forall₂ r) ⇒ forall₂ (forall₂ r)) sections sections | _ _ forall₂.nil := forall₂.cons forall₂.nil forall₂.nil | _ _ (forall₂.cons h₀ h₁) := rel_bind (rel_sections h₁) (assume _ _ hl, rel_map (assume _ _ ha, forall₂.cons ha hl) h₀) end list
459abce35d7ad767a11f92d3cb9591d8561ee18b
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/geometry/manifold/smooth_manifold_with_corners.lean
ec421d963876f04e5a0967acab0062dfd6144a10
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
33,096
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.calculus.times_cont_diff import geometry.manifold.charted_space /-! # Smooth manifolds (possibly with boundary or corners) A smooth manifold is a manifold modelled on a normed vector space, or a subset like a half-space (to get manifolds with boundaries) for which the changes of coordinates are smooth maps. We define a model with corners as a map `I : H → E` embedding nicely the topological space `H` in the vector space `E` (or more precisely as a structure containing all the relevant properties). Given such a model with corners `I` on `(E, H)`, we define the groupoid of local homeomorphisms of `H` which are smooth when read in `E` (for any regularity `n : with_top ℕ`). With this groupoid at hand and the general machinery of charted spaces, we thus get the notion of `C^n` manifold with respect to any model with corners `I` on `(E, H)`. We also introduce a specific type class for `C^∞` manifolds as these are the most commonly used. ## Main definitions * `model_with_corners 𝕜 E H` : a structure containing informations on the way a space `H` embeds in a model vector space E over the field `𝕜`. This is all that is needed to define a smooth manifold with model space `H`, and model vector space `E`. * `model_with_corners_self 𝕜 E` : trivial model with corners structure on the space `E` embedded in itself by the identity. * `times_cont_diff_groupoid n I` : when `I` is a model with corners on `(𝕜, E, H)`, this is the groupoid of local homeos of `H` which are of class `C^n` over the normed field `𝕜`, when read in `E`. * `smooth_manifold_with_corners I M` : a type class saying that the charted space `M`, modelled on the space `H`, has `C^∞` changes of coordinates with respect to the model with corners `I` on `(𝕜, E, H)`. This type class is just a shortcut for `has_groupoid M (times_cont_diff_groupoid ∞ I)`. * `ext_chart_at I x`: in a smooth manifold with corners with the model `I` on `(E, H)`, the charts take values in `H`, but often we may want to use their `E`-valued version, obtained by composing the charts with `I`. Since the target is in general not open, we can not register them as local homeomorphisms, but we register them as local equivs. `ext_chart_at I x` is the canonical such local equiv around `x`. As specific examples of models with corners, we define (in the file `real_instances.lean`) * `model_with_corners_self ℝ (euclidean_space (fin n))` for the model space used to define `n`-dimensional real manifolds without boundary (with notation `𝓡 n` in the locale `manifold`) * `model_with_corners ℝ (euclidean_space (fin n)) (euclidean_half_space n)` for the model space used to define `n`-dimensional real manifolds with boundary (with notation `𝓡∂ n` in the locale `manifold`) * `model_with_corners ℝ (euclidean_space (fin n)) (euclidean_quadrant n)` for the model space used to define `n`-dimensional real manifolds with corners With these definitions at hand, to invoke an `n`-dimensional real manifold without boundary, one could use `variables {n : ℕ} {M : Type*} [topological_space M] [charted_space (euclidean_space (fin n)) M] [smooth_manifold_with_corners (𝓡 n) M]`. However, this is not the recommended way: a theorem proved using this assumption would not apply for instance to the tangent space of such a manifold, which is modelled on `(euclidean_space (fin n)) × (euclidean_space (fin n))` and not on `euclidean_space (fin (2 * n))`! In the same way, it would not apply to product manifolds, modelled on `(euclidean_space (fin n)) × (euclidean_space (fin m))`. The right invocation does not focus on one specific construction, but on all constructions sharing the right properties, like `variables {E : Type*} [normed_group E] [normed_space ℝ E] [finite_dimensional ℝ E] {I : model_with_corners ℝ E E} [I.boundaryless] {M : Type*} [topological_space M] [charted_space E M] [smooth_manifold_with_corners I M]` Here, `I.boundaryless` is a typeclass property ensuring that there is no boundary (this is for instance the case for `model_with_corners_self`, or products of these). Note that one could consider as a natural assumption to only use the trivial model with corners `model_with_corners_self ℝ E`, but again in product manifolds the natural model with corners will not be this one but the product one (and they are not defeq as `(λp : E × F, (p.1, p.2))` is not defeq to the identity). So, it is important to use the above incantation to maximize the applicability of theorems. ## Implementation notes We want to talk about manifolds modelled on a vector space, but also on manifolds with boundary, modelled on a half space (or even manifolds with corners). For the latter examples, we still want to define smooth functions, tangent bundles, and so on. As smooth functions are well defined on vector spaces or subsets of these, one could take for model space a subtype of a vector space. With the drawback that the whole vector space itself (which is the most basic example) is not directly a subtype of itself: the inclusion of `univ : set E` in `set E` would show up in the definition, instead of `id`. A good abstraction covering both cases it to have a vector space `E` (with basic example the Euclidean space), a model space `H` (with basic example the upper half space), and an embedding of `H` into `E` (which can be the identity for `H = E`, or `subtype.val` for manifolds with corners). We say that the pair `(E, H)` with their embedding is a model with corners, and we encompass all the relevant properties (in particular the fact that the image of `H` in `E` should have unique differentials) in the definition of `model_with_corners`. We concentrate on `C^∞` manifolds: all the definitions work equally well for `C^n` manifolds, but later on it is a pain to carry all over the smoothness parameter, especially when one wants to deal with `C^k` functions as there would be additional conditions `k ≤ n` everywhere. Since one deals almost all the time with `C^∞` (or analytic) manifolds, this seems to be a reasonable choice that one could revisit later if needed. `C^k` manifolds are still available, but they should be called using `has_groupoid M (times_cont_diff_groupoid k I)` where `I` is the model with corners. I have considered using the model with corners `I` as a typeclass argument, possibly `out_param`, to get lighter notations later on, but it did not turn out right, as on `E × F` there are two natural model with corners, the trivial (identity) one, and the product one, and they are not defeq and one needs to indicate to Lean which one we want to use. This means that when talking on objects on manifolds one will most often need to specify the model with corners one is using. For instance, the tangent bundle will be `tangent_bundle I M` and the derivative will be `mfderiv I I' f`, instead of the more natural notations `tangent_bundle 𝕜 M` and `mfderiv 𝕜 f` (the field has to be explicit anyway, as some manifolds could be considered both as real and complex manifolds). -/ noncomputable theory universes u v w u' v' w' open set localized "notation `∞` := (⊤ : with_top ℕ)" in manifold section model_with_corners /-! ### Models with corners. -/ /-- A structure containing informations on the way a space `H` embeds in a model vector space `E` over the field `𝕜`. This is all what is needed to define a smooth manifold with model space `H`, and model vector space `E`. -/ @[nolint has_inhabited_instance] structure model_with_corners (𝕜 : Type*) [nondiscrete_normed_field 𝕜] (E : Type*) [normed_group E] [normed_space 𝕜 E] (H : Type*) [topological_space H] extends local_equiv H E := (source_eq : source = univ) (unique_diff' : unique_diff_on 𝕜 (range to_fun)) (continuous_to_fun : continuous to_fun . tactic.interactive.continuity') (continuous_inv_fun : continuous inv_fun . tactic.interactive.continuity') attribute [simp, mfld_simps] model_with_corners.source_eq /-- A vector space is a model with corners. -/ def model_with_corners_self (𝕜 : Type*) [nondiscrete_normed_field 𝕜] (E : Type*) [normed_group E] [normed_space 𝕜 E] : model_with_corners 𝕜 E E := { to_fun := id, inv_fun := id, source := univ, target := univ, source_eq := rfl, map_source' := λ_ _, mem_univ _, map_target' := λ_ _, mem_univ _, left_inv' := λ_ _, rfl, right_inv' := λ_ _, rfl, unique_diff' := by { rw range_id, exact unique_diff_on_univ }, continuous_to_fun := continuous_id, continuous_inv_fun := continuous_id } localized "notation `𝓘(` 𝕜 `, ` E `)` := model_with_corners_self 𝕜 E" in manifold localized "notation `𝓘(` 𝕜 `)` := model_with_corners_self 𝕜 𝕜" in manifold section variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) instance : has_coe_to_fun (model_with_corners 𝕜 E H) := ⟨_, λ e, e.to_fun⟩ /-- The inverse to a model with corners, only registered as a local equiv. -/ protected def model_with_corners.symm : local_equiv E H := I.to_local_equiv.symm /- Register a few lemmas to make sure that `simp` puts expressions in normal form -/ @[simp, mfld_simps] lemma model_with_corners.to_local_equiv_coe : (I.to_local_equiv : H → E) = I := rfl @[simp, mfld_simps] lemma model_with_corners.mk_coe (e : local_equiv H E) (a b c d) : ((model_with_corners.mk e a b c d : model_with_corners 𝕜 E H) : H → E) = (e : H → E) := rfl @[simp, mfld_simps] lemma model_with_corners.to_local_equiv_coe_symm : (I.to_local_equiv.symm : E → H) = I.symm := rfl @[simp, mfld_simps] lemma model_with_corners.mk_coe_symm (e : local_equiv H E) (a b c d) : ((model_with_corners.mk e a b c d : model_with_corners 𝕜 E H).symm : E → H) = (e.symm : E → H) := rfl lemma model_with_corners.unique_diff : unique_diff_on 𝕜 (range I) := I.unique_diff' protected lemma model_with_corners.continuous : continuous I := I.continuous_to_fun lemma model_with_corners.continuous_symm : continuous I.symm := I.continuous_inv_fun section variables (𝕜 E) /-- In the trivial model with corners, the associated local equiv is the identity. -/ @[simp, mfld_simps] lemma model_with_corners_self_local_equiv : (model_with_corners_self 𝕜 E).to_local_equiv = local_equiv.refl E := rfl @[simp, mfld_simps] lemma model_with_corners_self_coe : (model_with_corners_self 𝕜 E : E → E) = id := rfl @[simp, mfld_simps] lemma model_with_corners_self_coe_symm : ((model_with_corners_self 𝕜 E).symm : E → E) = id := rfl end @[simp, mfld_simps] lemma model_with_corners.target : I.target = range (I : H → E) := by { rw [← image_univ, ← I.source_eq], exact (I.to_local_equiv.image_source_eq_target).symm } @[simp, mfld_simps] lemma model_with_corners.left_inv (x : H) : I.symm (I x) = x := by { convert I.left_inv' _, simp } @[simp, mfld_simps] lemma model_with_corners.left_inv' : I.symm ∘ I = id := by { ext x, exact model_with_corners.left_inv _ _ } @[simp, mfld_simps] lemma model_with_corners.right_inv {x : E} (hx : x ∈ range I) : I (I.symm x) = x := by { apply I.right_inv', simp [hx] } lemma model_with_corners.image (s : set H) : I '' s = I.symm ⁻¹' s ∩ range I := begin ext x, simp only [mem_image, mem_inter_eq, mem_range, mem_preimage], split, { rintros ⟨y, ⟨ys, hy⟩⟩, rw ← hy, simp only [ys, true_and, model_with_corners.left_inv], exact ⟨y, rfl⟩ }, { rintros ⟨xs, ⟨y, yx⟩⟩, rw ← yx at xs, simp only [model_with_corners.left_inv] at xs, exact ⟨y, ⟨xs, yx⟩⟩ } end lemma model_with_corners.unique_diff_preimage {s : set H} (hs : is_open s) : unique_diff_on 𝕜 (I.symm ⁻¹' s ∩ range I) := by { rw inter_comm, exact I.unique_diff.inter (I.continuous_inv_fun _ hs) } lemma model_with_corners.unique_diff_preimage_source {β : Type*} [topological_space β] {e : local_homeomorph H β} : unique_diff_on 𝕜 (I.symm ⁻¹' (e.source) ∩ range I) := I.unique_diff_preimage e.open_source lemma model_with_corners.unique_diff_at_image {x : H} : unique_diff_within_at 𝕜 (range I) (I x) := I.unique_diff _ (mem_range_self _) end section model_with_corners_prod /-- Given two model_with_corners `I` on `(E, H)` and `I'` on `(E', H')`, we define the model with corners `I.prod I'` on `(E × E', H × H')`. This appears in particular for the manifold structure on the tangent bundle to a manifold modelled on `(E, H)`: it will be modelled on `(E × E, H × E)`. -/ def model_with_corners.prod {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H] (I : model_with_corners 𝕜 E H) {E' : Type v'} [normed_group E'] [normed_space 𝕜 E'] {H' : Type w'} [topological_space H'] (I' : model_with_corners 𝕜 E' H') : model_with_corners 𝕜 (E × E') (model_prod H H') := { to_fun := λ p, (I p.1, I' p.2), inv_fun := λ p, (I.symm p.1, I'.symm p.2), source := (univ : set (H × H')), target := set.prod (range I) (range I'), map_source' := λ ⟨x, x'⟩ _, by simp [-mem_range, mem_range_self], map_target' := λ ⟨x, x'⟩ _, mem_univ _, left_inv' := λ ⟨x, x'⟩ _, by simp, right_inv' := λ ⟨x, x'⟩ ⟨hx, hx'⟩, by simp [hx, hx'], source_eq := rfl, unique_diff' := begin have : range (λ(p : model_prod H H'), (I p.1, I' p.2)) = set.prod (range I) (range I'), by { dsimp [model_prod], rw ← prod_range_range_eq }, rw this, exact unique_diff_on.prod I.unique_diff I'.unique_diff, end, continuous_to_fun := (continuous.comp I.continuous_to_fun continuous_fst).prod_mk (continuous.comp I'.continuous_to_fun continuous_snd), continuous_inv_fun := (continuous.comp I.continuous_inv_fun continuous_fst).prod_mk (continuous.comp I'.continuous_inv_fun continuous_snd) } /-- Special case of product model with corners, which is trivial on the second factor. This shows up as the model to tangent bundles. -/ @[reducible] def model_with_corners.tangent {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H] (I : model_with_corners 𝕜 E H) : model_with_corners 𝕜 (E × E) (model_prod H E) := I.prod (model_with_corners_self 𝕜 E) variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {F : Type*} [normed_group F] [normed_space 𝕜 F] {F' : Type*} [normed_group F'] [normed_space 𝕜 F'] {H : Type*} [topological_space H] {H' : Type*} [topological_space H'] {G : Type*} [topological_space G] {G' : Type*} [topological_space G'] {I : model_with_corners 𝕜 E H} {J : model_with_corners 𝕜 F G} @[simp, mfld_simps] lemma model_with_corners_prod_to_local_equiv : (I.prod J).to_local_equiv = (I.to_local_equiv).prod (J.to_local_equiv) := begin ext1 x, { refl, }, { intro x, refl, }, { simp only [set.univ_prod_univ, model_with_corners.source_eq, local_equiv.prod_source], } end @[simp, mfld_simps] lemma model_with_corners_prod_coe (I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H') : (I.prod I' : _ × _ → _ × _) = prod.map I I' := rfl @[simp, mfld_simps] lemma model_with_corners_prod_coe_symm (I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H') : ((I.prod I').symm : _ × _ → _ × _) = prod.map I.symm I'.symm := rfl end model_with_corners_prod section boundaryless /-- Property ensuring that the model with corners `I` defines manifolds without boundary. -/ class model_with_corners.boundaryless {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) : Prop := (range_eq_univ : range I = univ) /-- The trivial model with corners has no boundary -/ instance model_with_corners_self_boundaryless (𝕜 : Type*) [nondiscrete_normed_field 𝕜] (E : Type*) [normed_group E] [normed_space 𝕜 E] : (model_with_corners_self 𝕜 E).boundaryless := ⟨by simp⟩ /-- If two model with corners are boundaryless, their product also is -/ instance model_with_corners.range_eq_univ_prod {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H] (I : model_with_corners 𝕜 E H) [I.boundaryless] {E' : Type v'} [normed_group E'] [normed_space 𝕜 E'] {H' : Type w'} [topological_space H'] (I' : model_with_corners 𝕜 E' H') [I'.boundaryless] : (I.prod I').boundaryless := begin split, dsimp [model_with_corners.prod, model_prod], rw [← prod_range_range_eq, model_with_corners.boundaryless.range_eq_univ, model_with_corners.boundaryless.range_eq_univ, univ_prod_univ] end end boundaryless section times_cont_diff_groupoid /-! ### Smooth functions on models with corners -/ variables {m n : with_top ℕ} {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] variable (n) /-- Given a model with corners `(E, H)`, we define the groupoid of `C^n` transformations of `H` as the maps that are `C^n` when read in `E` through `I`. -/ def times_cont_diff_groupoid : structure_groupoid H := pregroupoid.groupoid { property := λf s, times_cont_diff_on 𝕜 n (I ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I), comp := λf g u v hf hg hu hv huv, begin have : I ∘ (g ∘ f) ∘ I.symm = (I ∘ g ∘ I.symm) ∘ (I ∘ f ∘ I.symm), by { ext x, simp }, rw this, apply times_cont_diff_on.comp hg _, { rintros x ⟨hx1, hx2⟩, simp only with mfld_simps at ⊢ hx1, exact hx1.2 }, { refine hf.mono _, rintros x ⟨hx1, hx2⟩, exact ⟨hx1.1, hx2⟩ } end, id_mem := begin apply times_cont_diff_on.congr (times_cont_diff_id.times_cont_diff_on), rintros x ⟨hx1, hx2⟩, rcases mem_range.1 hx2 with ⟨y, hy⟩, rw ← hy, simp only with mfld_simps, end, locality := λf u hu H, begin apply times_cont_diff_on_of_locally_times_cont_diff_on, rintros y ⟨hy1, hy2⟩, rcases mem_range.1 hy2 with ⟨x, hx⟩, rw ← hx at ⊢ hy1, simp only with mfld_simps at ⊢ hy1, rcases H x hy1 with ⟨v, v_open, xv, hv⟩, have : ((I.symm ⁻¹' (u ∩ v)) ∩ (range I)) = ((I.symm ⁻¹' u) ∩ (range I) ∩ I.symm ⁻¹' v), { rw [preimage_inter, inter_assoc, inter_assoc], congr' 1, rw inter_comm }, rw this at hv, exact ⟨I.symm ⁻¹' v, I.continuous_symm _ v_open, by simpa, hv⟩ end, congr := λf g u hu fg hf, begin apply hf.congr, rintros y ⟨hy1, hy2⟩, rcases mem_range.1 hy2 with ⟨x, hx⟩, rw ← hx at ⊢ hy1, simp only with mfld_simps at ⊢ hy1, rw fg _ hy1 end } variable {n} /-- Inclusion of the groupoid of `C^n` local diffeos in the groupoid of `C^m` local diffeos when `m ≤ n` -/ lemma times_cont_diff_groupoid_le (h : m ≤ n) : times_cont_diff_groupoid n I ≤ times_cont_diff_groupoid m I := begin rw [times_cont_diff_groupoid, times_cont_diff_groupoid], apply groupoid_of_pregroupoid_le, assume f s hfs, exact times_cont_diff_on.of_le hfs h end /-- The groupoid of `0`-times continuously differentiable maps is just the groupoid of all local homeomorphisms -/ lemma times_cont_diff_groupoid_zero_eq : times_cont_diff_groupoid 0 I = continuous_groupoid H := begin apply le_antisymm le_top, assume u hu, -- we have to check that every local homeomorphism belongs to `times_cont_diff_groupoid 0 I`, -- by unfolding its definition change u ∈ times_cont_diff_groupoid 0 I, rw [times_cont_diff_groupoid, mem_groupoid_of_pregroupoid], simp only [times_cont_diff_on_zero], split, { apply continuous_on.comp (@continuous.continuous_on _ _ _ _ _ univ I.continuous) _ (subset_univ _), apply continuous_on.comp u.continuous_to_fun I.continuous_symm.continuous_on (inter_subset_left _ _) }, { apply continuous_on.comp (@continuous.continuous_on _ _ _ _ _ univ I.continuous) _ (subset_univ _), apply continuous_on.comp u.continuous_inv_fun I.continuous_inv_fun.continuous_on (inter_subset_left _ _) }, end variable (n) /-- An identity local homeomorphism belongs to the `C^n` groupoid. -/ lemma of_set_mem_times_cont_diff_groupoid {s : set H} (hs : is_open s) : local_homeomorph.of_set s hs ∈ times_cont_diff_groupoid n I := begin rw [times_cont_diff_groupoid, mem_groupoid_of_pregroupoid], suffices h : times_cont_diff_on 𝕜 n (I ∘ I.symm) (I.symm ⁻¹' s ∩ range I), by simp [h], have : times_cont_diff_on 𝕜 n id (univ : set E) := times_cont_diff_id.times_cont_diff_on, exact this.congr_mono (λ x hx, by simp [hx.2]) (subset_univ _) end /-- The composition of a local homeomorphism from `H` to `M` and its inverse belongs to the `C^n` groupoid. -/ lemma symm_trans_mem_times_cont_diff_groupoid (e : local_homeomorph M H) : e.symm.trans e ∈ times_cont_diff_groupoid n I := begin have : e.symm.trans e ≈ local_homeomorph.of_set e.target e.open_target := local_homeomorph.trans_symm_self _, exact structure_groupoid.eq_on_source _ (of_set_mem_times_cont_diff_groupoid n I e.open_target) this end variables {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] /-- The product of two smooth local homeomorphisms is smooth. -/ lemma times_cont_diff_groupoid_prod {I : model_with_corners 𝕜 E H} {I' : model_with_corners 𝕜 E' H'} {e : local_homeomorph H H} {e' : local_homeomorph H' H'} (he : e ∈ times_cont_diff_groupoid ⊤ I) (he' : e' ∈ times_cont_diff_groupoid ⊤ I') : e.prod e' ∈ times_cont_diff_groupoid ⊤ (I.prod I') := begin cases he with he he_symm, cases he' with he' he'_symm, simp only at he he_symm he' he'_symm, split; simp only [local_equiv.prod_source, local_homeomorph.prod_to_local_equiv], { have h3 := times_cont_diff_on.prod_map he he', rw [← model_with_corners.image I _, ← model_with_corners.image I' _, set.prod_image_image_eq] at h3, rw ← model_with_corners.image (I.prod I') _, exact h3, }, { have h3 := times_cont_diff_on.prod_map he_symm he'_symm, rw [← model_with_corners.image I _, ← model_with_corners.image I' _, set.prod_image_image_eq] at h3, rw ← model_with_corners.image (I.prod I') _, exact h3, } end /-- The `C^n` groupoid is closed under restriction. -/ instance : closed_under_restriction (times_cont_diff_groupoid n I) := (closed_under_restriction_iff_id_le _).mpr begin apply structure_groupoid.le_iff.mpr, rintros e ⟨s, hs, hes⟩, apply (times_cont_diff_groupoid n I).eq_on_source' _ _ _ hes, exact of_set_mem_times_cont_diff_groupoid n I hs, end end times_cont_diff_groupoid end model_with_corners /-! ### Smooth manifolds with corners -/ /-- Typeclass defining smooth manifolds with corners with respect to a model with corners, over a field `𝕜` and with infinite smoothness to simplify typeclass search and statements later on. -/ class smooth_manifold_with_corners {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) (M : Type*) [topological_space M] [charted_space H M] extends has_groupoid M (times_cont_diff_groupoid ∞ I) : Prop /-- For any model with corners, the model space is a smooth manifold -/ instance model_space_smooth {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H} : smooth_manifold_with_corners I H := {} namespace smooth_manifold_with_corners /- We restate in the namespace `smooth_manifolds_with_corners` some lemmas that hold for general charted space with a structure groupoid, avoiding the need to specify the groupoid `times_cont_diff_groupoid ∞ I` explicitly. -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) (M : Type*) [topological_space M] [charted_space H M] /-- The maximal atlas of `M` for the smooth manifold with corners structure corresponding to the model with corners `I`. -/ def maximal_atlas := (times_cont_diff_groupoid ∞ I).maximal_atlas M variable {M} lemma compatible [smooth_manifold_with_corners I M] {e e' : local_homeomorph M H} (he : e ∈ atlas H M) (he' : e' ∈ atlas H M) : e.symm.trans e' ∈ times_cont_diff_groupoid ∞ I := has_groupoid.compatible _ he he' lemma mem_maximal_atlas_of_mem_atlas [smooth_manifold_with_corners I M] {e : local_homeomorph M H} (he : e ∈ atlas H M) : e ∈ maximal_atlas I M := structure_groupoid.mem_maximal_atlas_of_mem_atlas _ he lemma chart_mem_maximal_atlas [smooth_manifold_with_corners I M] (x : M) : chart_at H x ∈ maximal_atlas I M := structure_groupoid.chart_mem_maximal_atlas _ x variable {I} lemma compatible_of_mem_maximal_atlas {e e' : local_homeomorph M H} (he : e ∈ maximal_atlas I M) (he' : e' ∈ maximal_atlas I M) : e.symm.trans e' ∈ times_cont_diff_groupoid ∞ I := structure_groupoid.compatible_of_mem_maximal_atlas he he' /-- The product of two smooth manifolds with corners is naturally a smooth manifold with corners. -/ instance prod_smooth_manifold_with_corners {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H} {H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'} (M : Type*) [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] (M' : Type*) [topological_space M'] [charted_space H' M'] [smooth_manifold_with_corners I' M'] : smooth_manifold_with_corners (I.prod I') (M×M') := { compatible := begin rintros f g ⟨f1, hf1, f2, hf2, hf⟩ ⟨g1, hg1, g2, hg2, hg⟩, rw [hf, hg, local_homeomorph.prod_symm, local_homeomorph.prod_trans], have h1 := has_groupoid.compatible (times_cont_diff_groupoid ⊤ I) hf1 hg1, have h2 := has_groupoid.compatible (times_cont_diff_groupoid ⊤ I') hf2 hg2, exact times_cont_diff_groupoid_prod h1 h2, end } end smooth_manifold_with_corners section extended_charts open_locale topological_space variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] (x : M) {s t : set M} /-! ### Extended charts In a smooth manifold with corners, the model space is the space `H`. However, we will also need to use extended charts taking values in the model vector space `E`. These extended charts are not `local_homeomorph` as the target is not open in `E` in general, but we can still register them as `local_equiv`. -/ /-- The preferred extended chart on a manifold with corners around a point `x`, from a neighborhood of `x` to the model vector space. -/ @[simp, mfld_simps] def ext_chart_at (x : M) : local_equiv M E := (chart_at H x).to_local_equiv.trans I.to_local_equiv lemma ext_chart_at_source : (ext_chart_at I x).source = (chart_at H x).source := by rw [ext_chart_at, local_equiv.trans_source, I.source_eq, preimage_univ, inter_univ] lemma ext_chart_at_open_source : is_open (ext_chart_at I x).source := by { rw ext_chart_at_source, exact (chart_at H x).open_source } lemma mem_ext_chart_source : x ∈ (ext_chart_at I x).source := by simp only with mfld_simps lemma ext_chart_at_to_inv : (ext_chart_at I x).symm ((ext_chart_at I x) x) = x := by simp only with mfld_simps lemma ext_chart_at_source_mem_nhds : (ext_chart_at I x).source ∈ 𝓝 x := mem_nhds_sets (ext_chart_at_open_source I x) (mem_ext_chart_source I x) lemma ext_chart_at_continuous_on : continuous_on (ext_chart_at I x) (ext_chart_at I x).source := begin refine continuous_on.comp I.continuous.continuous_on _ subset_preimage_univ, rw ext_chart_at_source, exact (chart_at H x).continuous_on end lemma ext_chart_at_continuous_at : continuous_at (ext_chart_at I x) x := (ext_chart_at_continuous_on I x x (mem_ext_chart_source I x)).continuous_at (ext_chart_at_source_mem_nhds I x) lemma ext_chart_at_continuous_on_symm : continuous_on (ext_chart_at I x).symm (ext_chart_at I x).target := begin apply continuous_on.comp (chart_at H x).continuous_on_symm I.continuous_symm.continuous_on, simp [ext_chart_at, local_equiv.trans_target] end lemma ext_chart_at_target_mem_nhds_within : (ext_chart_at I x).target ∈ 𝓝[range I] ((ext_chart_at I x) x) := begin rw [ext_chart_at, local_equiv.trans_target], simp only [function.comp_app, local_equiv.coe_trans, model_with_corners.target], refine inter_mem_nhds_within _ (mem_nhds_sets (I.continuous_symm _ (chart_at H x).open_target) _), simp only with mfld_simps end lemma ext_chart_at_coe (p : M) : (ext_chart_at I x) p = I ((chart_at H x : M → H) p) := rfl lemma ext_chart_at_coe_symm (p : E) : (ext_chart_at I x).symm p = ((chart_at H x).symm : H → M) (I.symm p) := rfl lemma nhds_within_ext_chart_target_eq : 𝓝[(ext_chart_at I x).target] ((ext_chart_at I x) x) = 𝓝[range I] ((ext_chart_at I x) x) := begin apply le_antisymm, { apply nhds_within_mono, simp only with mfld_simps}, { apply nhds_within_le_of_mem (ext_chart_at_target_mem_nhds_within _ _) } end lemma ext_chart_continuous_at_symm' {x' : M} (h : x' ∈ (ext_chart_at I x).source) : continuous_at (ext_chart_at I x).symm ((ext_chart_at I x) x') := begin apply continuous_at.comp, { rw ext_chart_at_source at h, simp only with mfld_simps, exact ((chart_at H x).continuous_on_symm _ ((chart_at H x).map_source h)).continuous_at (mem_nhds_sets (chart_at H x).open_target ((chart_at H x).map_source h)) }, { exact I.continuous_symm.continuous_at } end lemma ext_chart_continuous_at_symm : continuous_at (ext_chart_at I x).symm ((ext_chart_at I x) x) := ext_chart_continuous_at_symm' I x (mem_ext_chart_source I x) /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of a point in the source is a neighborhood of the preimage, within a set. -/ lemma ext_chart_preimage_mem_nhds_within' {x' : M} (h : x' ∈ (ext_chart_at I x).source) (ht : t ∈ 𝓝[s] x') : (ext_chart_at I x).symm ⁻¹' t ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x') := begin apply (ext_chart_continuous_at_symm' I x h).continuous_within_at.tendsto_nhds_within_image, rw (ext_chart_at I x).left_inv h, apply nhds_within_mono _ _ ht, have : (ext_chart_at I x).symm '' ((ext_chart_at I x).symm ⁻¹' s) ⊆ s := image_preimage_subset _ _, exact subset.trans (image_subset _ (inter_subset_left _ _)) this end /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of the base point is a neighborhood of the preimage, within a set. -/ lemma ext_chart_preimage_mem_nhds_within (ht : t ∈ 𝓝[s] x) : (ext_chart_at I x).symm ⁻¹' t ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) := ext_chart_preimage_mem_nhds_within' I x (mem_ext_chart_source I x) ht /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of a point is a neighborhood of the preimage. -/ lemma ext_chart_preimage_mem_nhds (ht : t ∈ 𝓝 x) : (ext_chart_at I x).symm ⁻¹' t ∈ 𝓝 ((ext_chart_at I x) x) := begin apply (ext_chart_continuous_at_symm I x).preimage_mem_nhds, rwa (ext_chart_at I x).left_inv (mem_ext_chart_source _ _) end /-- Technical lemma to rewrite suitably the preimage of an intersection under an extended chart, to bring it into a convenient form to apply derivative lemmas. -/ lemma ext_chart_preimage_inter_eq : ((ext_chart_at I x).symm ⁻¹' (s ∩ t) ∩ range I) = ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ∩ ((ext_chart_at I x).symm ⁻¹' t) := by mfld_set_tac end extended_charts /-- In the case of the manifold structure on a vector space, the extended charts are just the identity.-/ lemma ext_chart_model_space_eq_id (𝕜 : Type*) [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] (x : E) : ext_chart_at (model_with_corners_self 𝕜 E) x = local_equiv.refl E := by simp only with mfld_simps
d7978b92acbcb97134fb330707da7a38f314819a
75c54c8946bb4203e0aaf196f918424a17b0de99
/old/reflection.lean
06e7509c5ec3562ed901433f330270cc2bb53366
[ "Apache-2.0" ]
permissive
urkud/flypitch
261e2a45f1038130178575406df8aea78255ba77
2250f5eda14b6ef9fc3e4e1f4a9ac4005634de5c
refs/heads/master
1,653,266,469,246
1,577,819,679,000
1,577,819,679,000
259,862,235
1
0
Apache-2.0
1,588,147,244,000
1,588,147,244,000
null
UTF-8
Lean
false
false
4,027
lean
/- Copyright (c) 2019 The Flypitch Project. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jesse Han, Floris van Doorn -/ import fol tactic.abel tactic.ring completeness abel open fol local notation h :: t := dvector.cons h t local notation `[]` := dvector.nil local notation `[` l:(foldr `, ` (h t, dvector.cons h t) dvector.nil `]`) := l namespace reflection section /- rephrasing of soundness -/ def by_reflection {L} (T : Theory L) {S : Structure L} [h_nonempty : nonempty S] {hS : S ⊨ T} (p : Prop) (ψ : sentence L) (h_p : realize_sentence S ψ ↔ p) : T ⊢' ψ → p := by {intro P, cases P, apply h_p.mp, apply soundness, repeat{assumption}} open abel /- every abelian group satisfies ∀ x ∀ y, x + (y + y) = y + (y + x) -/ def T_ab_proves_x_y_y : T_ab ⊢' ∀' ∀' (&1 +' (&0 +' &0) ≃ &0 +' (&0 +' &1)) := begin apply (completeness _ _).mpr, intros M h_nonempty hM, intros x y, unfold T_ab at hM, have hM' : M ⊨ a_comm ∧ M ⊨ a_inv ∧ M ⊨ a_zero_left ∧ M ⊨ a_zero_right ∧ M ⊨ a_assoc, by {simp at hM, assumption}, rcases hM' with ⟨uno,dos,tres,cuatro,cinco⟩, have this1 := cinco x y y, have this2 := uno y x, have this3 := uno (realize_bounded_term ([x,y]) (&0 +' &1) []) y, dsimp at *, conv {to_lhs, change realize_bounded_term ([y, y, x]) (&2 +' (&1 +' &0)) [], rw[<-this1]}, conv {to_lhs, change realize_bounded_term ([y, (realize_bounded_term ([x, y]) (&0 +' &1) [])]) (&1 +' &0) [], rw[this3, <-(this2)]}, refl end example : ∀ x y : ℤ, x + (y + y) = y + (y + x) := begin apply @by_reflection L_abel T_ab ℤ' (by apply_instance) (ℤ'_is_abelian_group) _ _ _ _, exact ∀' ∀' (&1 +' (&0 +' &0) ≃ &0 +' (&0 +' &1)), {refl}, exact T_ab_proves_x_y_y end example : ∀ x y : ℤ, x + (y + y) = y + (y + x) := by {intros, rw[add_comm,add_assoc]} example : ∀ x y : ℤ, x + (y + y) = y + (y + x) := by {intros, ring} example : ∀ x y : ℤ, x + (y + y) = y + (y + x) := by {simp} example : ∀ x y : ℤ, x + (y + y) = y + (y + x) := by {tidy} def L_abel_structure_of_add_comm_group (α : Type) [add_comm_group α] : Structure L_abel := begin refine ⟨α, λn f, _, λn r, by {cases r}⟩, {induction f, intro xs, exact 0, intro xs, exact (xs.nth 0 dec_trivial) + (xs.nth 1 dec_trivial)} end variables (α : Type) [add_comm_group α] local notation `α'` := L_abel_structure_of_add_comm_group α @[simp]lemma α'_α : ↥(α') = α := by refl @[reducible]instance has_zero_α' : has_zero α' := ⟨(0 : α)⟩ @[reducible]instance has_add_α' : has_add α' := ⟨λx y, (x + y : α)⟩ @[reducible]instance nonempty_α' : nonempty α' := ⟨0⟩ @[reducible]instance has_neg_α' : has_neg α' := ⟨λ x, (-x : α)⟩ @[simp]lemma zero_is_zero : @realize_bounded_term L_abel α' _ [] _ zero [] = (0 : α) := by refl @[simp]lemma plus_is_plus_l : ∀ x y : α', realize_bounded_term ([x,y]) (&0 +' &1) [] = x + y := by {intros, refl} @[simp]lemma plus_is_plus_r : ∀ x y : α', realize_bounded_term ([x,y]) (&1 +' &0) [] = y + x := by {intros, refl} example {α} [add_comm_group α] {x y : α} : x + y = y + x := by simp theorem add_comm_group_is_abelian_group {α : Type} [add_comm_group α] : T_ab ⊆ Th(L_abel_structure_of_add_comm_group α) := begin intros a H, repeat{cases H}, {intros x y, change x + y = y + x, simp}, {intro x, simp only [realize_bounded_formula_ex, realize_bounded_formula,realize_bounded_formula_and], use -x, change (x + -x = 0 ∧ -x + x = 0), simp}, {intro x, change 0 + x = x, simp}, {intro x, change x + 0 = x, simp}, {intros x y z, change x + y + z = x + (y + z), simp} end example : ∀ x y : α, x + (y + y) = y + (y + x) := begin refine by_reflection (T_ab) (∀ x y, x + (y + y) = y + (y + x)) (∀' ∀' (&1 +' (&0 +' &0) ≃ &0 +' (&0 +' &1))) _ _, from L_abel_structure_of_add_comm_group α, from ⟨0⟩, from add_comm_group_is_abelian_group, by refl, from T_ab_proves_x_y_y end end end reflection
2c3a5c561bb816e8f74d22ae27a4dbde4fd1db46
49ffcd4736fa3bdcc1cdbb546d4c855d67c0f28a
/library/init/data/fin/ops.lean
0aff7950bd57b7b8f8fe8236673ad12c2e331020
[ "Apache-2.0" ]
permissive
black13/lean
979e24d09e17b2fdf8ec74aac160583000086bc8
1a80ea9c8e28902cadbfb612896bcd45ba4ce697
refs/heads/master
1,626,839,620,164
1,509,113,016,000
1,509,122,889,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,833
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import init.data.nat init.data.fin.basic namespace fin open nat variable {n : nat} protected def succ : fin n → fin (succ n) | ⟨a, h⟩ := ⟨nat.succ a, succ_lt_succ h⟩ def of_nat {n : nat} (a : nat) : fin (succ n) := ⟨a % succ n, nat.mod_lt _ (nat.zero_lt_succ _)⟩ private lemma mlt {n b : nat} : ∀ {a}, n > a → b % n < n | 0 h := nat.mod_lt _ h | (a+1) h := have n > 0, from lt.trans (nat.zero_lt_succ _) h, nat.mod_lt _ this protected def add : fin n → fin n → fin n | ⟨a, h⟩ ⟨b, _⟩ := ⟨(a + b) % n, mlt h⟩ protected def mul : fin n → fin n → fin n | ⟨a, h⟩ ⟨b, _⟩ := ⟨(a * b) % n, mlt h⟩ private lemma sublt {a b n : nat} (h : a < n) : a - b < n := lt_of_le_of_lt (nat.sub_le a b) h protected def sub : fin n → fin n → fin n | ⟨a, h⟩ ⟨b, _⟩ := ⟨a - b, sublt h⟩ private lemma modlt {a b n : nat} (h₁ : a < n) (h₂ : b < n) : a % b < n := begin cases b with b, {simp [mod_zero], assumption}, {have h : a % (succ b) < succ b, apply nat.mod_lt _ (nat.zero_lt_succ _), exact lt.trans h h₂} end protected def mod : fin n → fin n → fin n | ⟨a, h₁⟩ ⟨b, h₂⟩ := ⟨a % b, modlt h₁ h₂⟩ private lemma divlt {a b n : nat} (h : a < n) : a / b < n := lt_of_le_of_lt (nat.div_le_self a b) h protected def div : fin n → fin n → fin n | ⟨a, h⟩ ⟨b, _⟩ := ⟨a / b, divlt h⟩ protected def lt : fin n → fin n → Prop | ⟨a, _⟩ ⟨b, _⟩ := a < b protected def le : fin n → fin n → Prop | ⟨a, _⟩ ⟨b, _⟩ := a ≤ b instance : has_zero (fin (succ n)) := ⟨⟨0, succ_pos n⟩⟩ instance : has_one (fin (succ n)) := ⟨of_nat 1⟩ instance : has_add (fin n) := ⟨fin.add⟩ instance : has_sub (fin n) := ⟨fin.sub⟩ instance : has_mul (fin n) := ⟨fin.mul⟩ instance : has_mod (fin n) := ⟨fin.mod⟩ instance : has_div (fin n) := ⟨fin.div⟩ instance : has_lt (fin n) := ⟨fin.lt⟩ instance : has_le (fin n) := ⟨fin.le⟩ instance decidable_lt : ∀ (a b : fin n), decidable (a < b) | ⟨a, _⟩ ⟨b, _⟩ := by apply nat.decidable_lt instance decidable_le : ∀ (a b : fin n), decidable (a ≤ b) | ⟨a, _⟩ ⟨b, _⟩ := by apply nat.decidable_le lemma of_nat_zero : @of_nat n 0 = 0 := rfl lemma add_def (a b : fin n) : (a + b).val = (a.val + b.val) % n := show (fin.add a b).val = (a.val + b.val) % n, from by cases a; cases b; simp [fin.add] lemma mul_def (a b : fin n) : (a * b).val = (a.val * b.val) % n := show (fin.mul a b).val = (a.val * b.val) % n, from by cases a; cases b; simp [fin.mul] lemma sub_def (a b : fin n) : (a - b).val = a.val - b.val := show (fin.sub a b).val = a.val - b.val, from by cases a; cases b; simp [fin.sub] lemma mod_def (a b : fin n) : (a % b).val = a.val % b.val := show (fin.mod a b).val = a.val % b.val, from by cases a; cases b; simp [fin.mod] lemma div_def (a b : fin n) : (a / b).val = a.val / b.val := show (fin.div a b).val = a.val / b.val, from by cases a; cases b; simp [fin.div] lemma lt_def (a b : fin n) : (a < b) = (a.val < b.val) := show (fin.lt a b) = (a.val < b.val), from by cases a; cases b; simp [fin.lt] lemma le_def (a b : fin n) : (a ≤ b) = (a.val ≤ b.val) := show (fin.le a b) = (a.val ≤ b.val), from by cases a; cases b; simp [fin.le] lemma val_zero : (0 : fin (succ n)).val = 0 := rfl def pred {n : nat} : ∀ i : fin (succ n), i ≠ 0 → fin n | ⟨a, h₁⟩ h₂ := ⟨a.pred, begin have this : a ≠ 0, { have aux₁ := vne_of_ne h₂, dsimp at aux₁, rw val_zero at aux₁, exact aux₁ }, exact nat.pred_lt_pred this h₁ end⟩ end fin
4486d9e26289560e9a7bf1d827834dcefd2fed39
ba4794a0deca1d2aaa68914cd285d77880907b5c
/src/game/world10/level7.lean
e6b768bdec6b5fb221f0b1e740748cfd9bd5c90e
[ "Apache-2.0" ]
permissive
ChrisHughes24/natural_number_game
c7c00aa1f6a95004286fd456ed13cf6e113159ce
9d09925424da9f6275e6cfe427c8bcf12bb0944f
refs/heads/master
1,600,715,773,528
1,573,910,462,000
1,573,910,462,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
493
lean
import game.world10.level6 -- hide namespace mynat -- hide /- # Inequality world ## Level 7: `le_zero` We proved `add_right_eq_zero` back in advanced addition world. Note that you can do things like `have h2 := add_right_eq_zero _ _ h1` if `h1 : a + c = 0`. -/ /- Lemma For all naturals $a$, if $a\le 0$ then $a = 0$. -/ lemma le_zero (a : mynat) (h : a ≤ 0) : a = 0 := begin [less_leaky] cases h with c hc, symmetry at hc, exact add_right_eq_zero _ _ hc, end end mynat -- hide