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
e00b665915c3d64c2ab209570a33f857bdb3063a
7afc29faca4febb6e5005c20aa4aa5c3df5cf35c
/src/single.lean
6b5f0e809d79cda2c0788025e8255647ced380c4
[ "MIT" ]
permissive
Piwry/Proof-of-Surreal
ad2883027e275050b43a578c5513ae3fe350515b
6b92baf2382ac23dd0d700f5c958aa910ad4b754
refs/heads/master
1,670,521,185,736
1,599,657,591,000
1,599,657,591,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
597
lean
import data.list algebra.ring tactic.omega defs lemma single_complete: almost_complete [●] := begin apply almost_complete.cmp_rule, existsi ([]), intros t h, exfalso, apply h, apply grow_list.head_grow, apply grow.single_grow end lemma single_grow : ∀ t : bintree, (t ↣ ●) → t = ● := begin intros t H1, destruct t, begin intros, assumption end, repeat { intros a H2, rewrite H2 at H1, cases H1, }, -- this proves the first two goals begin intros a a1 H2, rewrite H2 at H1, cases H1, end -- this proves the last two goals end
d884fcabfa7da40a8007b3839b1e2bd08b661c2b
c777c32c8e484e195053731103c5e52af26a25d1
/counterexamples/phillips.lean
d5221ec1e43839edd2f2776fc56526f8f77e925f
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
29,001
lean
/- Copyright (c) 2021 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.normed_space.hahn_banach.extension import measure_theory.measure.lebesgue /-! # A counterexample on Pettis integrability There are several theories of integration for functions taking values in Banach spaces. Bochner integration, requiring approximation by simple functions, is the analogue of the one-dimensional theory. It is very well behaved, but only works for functions with second-countable range. For functions `f` taking values in a larger Banach space `B`, one can define the Dunford integral as follows. Assume that, for all continuous linear functional `φ`, the function `φ ∘ f` is measurable (we say that `f` is weakly measurable, or scalarly measurable) and integrable. Then `φ ↦ ∫ φ ∘ f` is continuous (by the closed graph theorem), and therefore defines an element of the bidual `B**`. This is the Dunford integral of `f`. This Dunford integral is not usable in practice as it does not belong to the right space. Let us say that a function is Pettis integrable if its Dunford integral belongs to the canonical image of `B` in `B**`. In this case, we define the Pettis integral as the Dunford integral inside `B`. This integral is very general, but not really usable to do analysis. This file illustrates this, by giving an example of a function with nice properties but which is *not* Pettis-integrable. This function: - is defined from `[0, 1]` to a complete Banach space; - is weakly measurable; - has norm everywhere bounded by `1` (in particular, its norm is integrable); - and yet it is not Pettis-integrable with respect to Lebesgue measure. This construction is due to [Ralph S. Phillips, *Integration in a convex linear topological space*][phillips1940], in Example 10.8. It requires the continuum hypothesis. The example is the following. Under the continuum hypothesis, one can find a subset of `ℝ²` which, along each vertical line, only misses a countable set of points, while it is countable along each horizontal line. This is due to Sierpinski, and formalized in `sierpinski_pathological_family`. (In fact, Sierpinski proves that the existence of such a set is equivalent to the continuum hypothesis). Let `B` be the set of all bounded functions on `ℝ` (we are really talking about everywhere defined functions here). Define `f : ℝ → B` by taking `f x` to be the characteristic function of the vertical slice at position `x` of Sierpinski's set. It is our counterexample. To show that it is weakly measurable, we should consider `φ ∘ f` where `φ` is an arbitrary continuous linear form on `B`. There is no reasonable classification of such linear forms (they can be very wild). But if one restricts such a linear form to characteristic functions, one gets a finitely additive signed "measure". Such a "measure" can be decomposed into a discrete part (supported on a countable set) and a continuous part (giving zero mass to countable sets). For all but countably many points, `f x` will not intersect the discrete support of `φ` thanks to the definition of the Sierpinski set. This implies that `φ ∘ f` is constant outside of a countable set, and equal to the total mass of the continuous part of `φ` there. In particular, it is measurable (and its integral is the total mass of the continuous part of `φ`). Assume that `f` has a Pettis integral `g`. For all continuous linear form `φ`, then `φ g` should be the total mass of the continuous part of `φ`. Taking for `φ` the evaluation at the point `x` (which has no continuous part), one gets `g x = 0`. Take then for `φ` the Lebesgue integral on `[0, 1]` (or rather an arbitrary extension of Lebesgue integration to all bounded functions, thanks to Hahn-Banach). Then `φ g` should be the total mass of the continuous part of `φ`, which is `1`. This contradicts the fact that `g = 0`, and concludes the proof that `f` has no Pettis integral. ## Implementation notes The space of all bounded functions is defined as the space of all bounded continuous functions on a discrete copy of the original type, as mathlib only contains the space of all bounded continuous functions (which is the useful one). -/ universe u variables {α : Type u} open set bounded_continuous_function measure_theory open cardinal (aleph) open_locale cardinal bounded_continuous_function noncomputable theory /-- A copy of a type, endowed with the discrete topology -/ def discrete_copy (α : Type u) : Type u := α instance : topological_space (discrete_copy α) := ⊥ instance : discrete_topology (discrete_copy α) := ⟨rfl⟩ instance [inhabited α] : inhabited (discrete_copy α) := ⟨show α, from default⟩ namespace phillips_1940 /-! ### Extending the integral Thanks to Hahn-Banach, one can define a (non-canonical) continuous linear functional on the space of all bounded functions, coinciding with the integral on the integrable ones. -/ /-- The subspace of integrable functions in the space of all bounded functions on a type. This is a technical device, used to apply Hahn-Banach theorem to construct an extension of the integral to all bounded functions. -/ def bounded_integrable_functions [measurable_space α] (μ : measure α) : subspace ℝ (discrete_copy α →ᵇ ℝ) := { carrier := {f | integrable f μ}, zero_mem' := integrable_zero _ _ _, add_mem' := λ f g hf hg, integrable.add hf hg, smul_mem' := λ c f hf, integrable.smul c hf } /-- The integral, as a continuous linear map on the subspace of integrable functions in the space of all bounded functions on a type. This is a technical device, that we will extend through Hahn-Banach. -/ def bounded_integrable_functions_integral_clm [measurable_space α] (μ : measure α) [is_finite_measure μ] : bounded_integrable_functions μ →L[ℝ] ℝ := linear_map.mk_continuous { to_fun := λ f, ∫ x, f x ∂μ, map_add' := λ f g, integral_add f.2 g.2, map_smul' := λ c f, integral_smul _ _ } (μ univ).to_real begin assume f, rw mul_comm, apply norm_integral_le_of_norm_le_const, apply filter.eventually_of_forall, assume x, exact bounded_continuous_function.norm_coe_le_norm f x, end /-- Given a measure, there exists a continuous linear form on the space of all bounded functions (not necessarily measurable) that coincides with the integral on bounded measurable functions. -/ lemma exists_linear_extension_to_bounded_functions [measurable_space α] (μ : measure α) [is_finite_measure μ] : ∃ φ : (discrete_copy α →ᵇ ℝ) →L[ℝ] ℝ, ∀ (f : discrete_copy α →ᵇ ℝ), integrable f μ → φ f = ∫ x, f x ∂μ := begin rcases exists_extension_norm_eq _ (bounded_integrable_functions_integral_clm μ) with ⟨φ, hφ⟩, exact ⟨φ, λ f hf, hφ.1 ⟨f, hf⟩⟩, end /-- An arbitrary extension of the integral to all bounded functions, as a continuous linear map. It is not at all canonical, and constructed using Hahn-Banach. -/ def _root_.measure_theory.measure.extension_to_bounded_functions [measurable_space α] (μ : measure α) [is_finite_measure μ] : (discrete_copy α →ᵇ ℝ) →L[ℝ] ℝ := (exists_linear_extension_to_bounded_functions μ).some lemma extension_to_bounded_functions_apply [measurable_space α] (μ : measure α) [is_finite_measure μ] (f : discrete_copy α →ᵇ ℝ) (hf : integrable f μ) : μ.extension_to_bounded_functions f = ∫ x, f x ∂μ := (exists_linear_extension_to_bounded_functions μ).some_spec f hf /-! ### Additive measures on the space of all sets We define bounded finitely additive signed measures on the space of all subsets of a type `α`, and show that such an object can be split into a discrete part and a continuous part. -/ /-- A bounded signed finitely additive measure defined on *all* subsets of a type. -/ structure bounded_additive_measure (α : Type u) := (to_fun : set α → ℝ) (additive' : ∀ s t, disjoint s t → to_fun (s ∪ t) = to_fun s + to_fun t) (exists_bound : ∃ (C : ℝ), ∀ s, |to_fun s| ≤ C) instance : inhabited (bounded_additive_measure α) := ⟨{ to_fun := λ s, 0, additive' := λ s t hst, by simp, exists_bound := ⟨0, λ s, by simp⟩ }⟩ instance : has_coe_to_fun (bounded_additive_measure α) (λ _, set α → ℝ) := ⟨λ f, f.to_fun⟩ namespace bounded_additive_measure /-- A constant bounding the mass of any set for `f`. -/ def C (f : bounded_additive_measure α) := f.exists_bound.some lemma additive (f : bounded_additive_measure α) (s t : set α) (h : disjoint s t) : f (s ∪ t) = f s + f t := f.additive' s t h lemma abs_le_bound (f : bounded_additive_measure α) (s : set α) : |f s| ≤ f.C := f.exists_bound.some_spec s lemma le_bound (f : bounded_additive_measure α) (s : set α) : f s ≤ f.C := le_trans (le_abs_self _) (f.abs_le_bound s) @[simp] lemma empty (f : bounded_additive_measure α) : f ∅ = 0 := begin have : (∅ : set α) = ∅ ∪ ∅, by simp only [empty_union], apply_fun f at this, rwa [f.additive _ _ (empty_disjoint _), self_eq_add_left] at this, end instance : has_neg (bounded_additive_measure α) := ⟨λ f, { to_fun := λ s, - f s, additive' := λ s t hst, by simp only [f.additive s t hst, add_comm, neg_add_rev], exists_bound := ⟨f.C, λ s, by simp [f.abs_le_bound]⟩ }⟩ @[simp] lemma neg_apply (f : bounded_additive_measure α) (s : set α) : (-f) s = - (f s) := rfl /-- Restricting a bounded additive measure to a subset still gives a bounded additive measure. -/ def restrict (f : bounded_additive_measure α) (t : set α) : bounded_additive_measure α := { to_fun := λ s, f (t ∩ s), additive' := λ s s' h, begin rw [← f.additive (t ∩ s) (t ∩ s'), inter_union_distrib_left], exact h.mono (inter_subset_right _ _) (inter_subset_right _ _), end, exists_bound := ⟨f.C, λ s, f.abs_le_bound _⟩ } @[simp] lemma restrict_apply (f : bounded_additive_measure α) (s t : set α) : f.restrict s t = f (s ∩ t) := rfl /-- There is a maximal countable set of positive measure, in the sense that any countable set not intersecting it has nonpositive measure. Auxiliary lemma to prove `exists_discrete_support`. -/ lemma exists_discrete_support_nonpos (f : bounded_additive_measure α) : ∃ (s : set α), s.countable ∧ (∀ t : set α, t.countable → f (t \ s) ≤ 0) := begin /- The idea of the proof is to construct the desired set inductively, adding at each step a countable set with close to maximal measure among those points that have not already been chosen. Doing this countably many steps will be enough. Indeed, otherwise, a remaining set would have positive measure `ε`. This means that at each step the set we have added also had a large measure, say at least `ε / 2`. After `n` steps, the set we have constructed has therefore measure at least `n * ε / 2`. This is a contradiction since the measures have to remain uniformly bounded. We argue from the start by contradiction, as this means that our inductive construction will never be stuck, so we won't have to consider this case separately. In this proof, we use explicit coercions `↑s` for `s : A` as otherwise the system tries to find a `has_coe_to_fun` instance on `↥A`, which is too costly. -/ by_contra' h, -- We will formulate things in terms of the type of countable subsets of `α`, as this is more -- convenient to formalize the inductive construction. let A : set (set α) := {t | t.countable}, let empty : A := ⟨∅, countable_empty⟩, haveI : nonempty A := ⟨empty⟩, -- given a countable set `s`, one can find a set `t` in its complement with measure close to -- maximal. have : ∀ (s : A), ∃ (t : A), (∀ (u : A), f (↑u \ ↑s) ≤ 2 * f (↑t \ ↑s)), { assume s, have B : bdd_above (range (λ (u : A), f (↑u \ ↑s))), { refine ⟨f.C, λ x hx, _⟩, rcases hx with ⟨u, hu⟩, rw ← hu, exact f.le_bound _ }, let S := supr (λ (t : A), f (↑t \ ↑s)), have S_pos : 0 < S, { rcases h s.1 s.2 with ⟨t, t_count, ht⟩, apply ht.trans_le, let t' : A := ⟨t, t_count⟩, change f (↑t' \ ↑s) ≤ S, exact le_csupr B t' }, rcases exists_lt_of_lt_csupr (half_lt_self S_pos) with ⟨t, ht⟩, refine ⟨t, λ u, _⟩, calc f (↑u \ ↑s) ≤ S : le_csupr B _ ... = 2 * (S / 2) : by ring ... ≤ 2 * f (↑t \ ↑s) : mul_le_mul_of_nonneg_left ht.le (by norm_num) }, choose! F hF using this, -- iterate the above construction, by adding at each step a set with measure close to maximal in -- the complement of already chosen points. This is the set `s n` at step `n`. let G : A → A := λ u, ⟨(↑u : set α) ∪ ↑(F u), u.2.union (F u).2⟩, let s : ℕ → A := λ n, G^[n] empty, -- We will get a contradiction from the fact that there is a countable set `u` with positive -- measure in the complement of `⋃ n, s n`. rcases h (⋃ n, ↑(s n)) (countable_Union (λ n, (s n).2)) with ⟨t, t_count, ht⟩, let u : A := ⟨t \ ⋃ n, ↑(s n), t_count.mono (diff_subset _ _)⟩, set ε := f (↑u) with hε, have ε_pos : 0 < ε := ht, have I1 : ∀ n, ε / 2 ≤ f (↑(s (n+1)) \ ↑(s n)), { assume n, rw [div_le_iff' (show (0 : ℝ) < 2, by norm_num), hε], convert hF (s n) u using 3, { dsimp [u], ext x, simp only [not_exists, mem_Union, mem_diff], tauto }, { simp only [s, function.iterate_succ', subtype.coe_mk, union_diff_left] } }, have I2 : ∀ (n : ℕ), (n : ℝ) * (ε / 2) ≤ f (↑(s n)), { assume n, induction n with n IH, { simp only [s, bounded_additive_measure.empty, id.def, nat.cast_zero, zero_mul, function.iterate_zero, subtype.coe_mk], }, { have : (↑(s (n+1)) : set α) = (↑(s (n+1)) \ ↑(s n)) ∪ ↑(s n), by simp only [s, function.iterate_succ', union_comm, union_diff_self, subtype.coe_mk, union_diff_left], rw [nat.succ_eq_add_one, this, f.additive], swap, { exact disjoint_sdiff_self_left }, calc ((n + 1 : ℕ) : ℝ) * (ε / 2) = ε / 2 + n * (ε / 2) : by simp only [nat.cast_succ]; ring ... ≤ f (↑(s (n + 1 : ℕ)) \ ↑(s n)) + f (↑(s n)) : add_le_add (I1 n) IH } }, rcases exists_nat_gt (f.C / (ε / 2)) with ⟨n, hn⟩, have : (n : ℝ) ≤ f.C / (ε / 2), by { rw le_div_iff (half_pos ε_pos), exact (I2 n).trans (f.le_bound _) }, exact lt_irrefl _ (this.trans_lt hn) end lemma exists_discrete_support (f : bounded_additive_measure α) : ∃ s : set α, s.countable ∧ (∀ t : set α, t.countable → f (t \ s) = 0) := begin rcases f.exists_discrete_support_nonpos with ⟨s₁, s₁_count, h₁⟩, rcases (-f).exists_discrete_support_nonpos with ⟨s₂, s₂_count, h₂⟩, refine ⟨s₁ ∪ s₂, s₁_count.union s₂_count, λ t ht, le_antisymm _ _⟩, { have : t \ (s₁ ∪ s₂) = (t \ (s₁ ∪ s₂)) \ s₁, by rw [diff_diff, union_comm, union_assoc, union_self], rw this, exact h₁ _ (ht.mono (diff_subset _ _)) }, { have : t \ (s₁ ∪ s₂) = (t \ (s₁ ∪ s₂)) \ s₂, by rw [diff_diff, union_assoc, union_self], rw this, simp only [neg_nonpos, neg_apply] at h₂, exact h₂ _ (ht.mono (diff_subset _ _)) }, end /-- A countable set outside of which the measure gives zero mass to countable sets. We are not claiming this set is unique, but we make an arbitrary choice of such a set. -/ def discrete_support (f : bounded_additive_measure α) : set α := (exists_discrete_support f).some lemma countable_discrete_support (f : bounded_additive_measure α) : f.discrete_support.countable := (exists_discrete_support f).some_spec.1 lemma apply_countable (f : bounded_additive_measure α) (t : set α) (ht : t.countable) : f (t \ f.discrete_support) = 0 := (exists_discrete_support f).some_spec.2 t ht /-- The discrete part of a bounded additive measure, obtained by restricting the measure to its countable support. -/ def discrete_part (f : bounded_additive_measure α) : bounded_additive_measure α := f.restrict f.discrete_support /-- The continuous part of a bounded additive measure, giving zero measure to every countable set. -/ def continuous_part (f : bounded_additive_measure α) : bounded_additive_measure α := f.restrict (univ \ f.discrete_support) lemma eq_add_parts (f : bounded_additive_measure α) (s : set α) : f s = f.discrete_part s + f.continuous_part s := begin simp only [discrete_part, continuous_part, restrict_apply], rw [← f.additive, ← inter_distrib_right], { simp only [union_univ, union_diff_self, univ_inter] }, { have : disjoint f.discrete_support (univ \ f.discrete_support) := disjoint_sdiff_self_right, exact this.mono (inter_subset_left _ _) (inter_subset_left _ _) } end lemma discrete_part_apply (f : bounded_additive_measure α) (s : set α) : f.discrete_part s = f (f.discrete_support ∩ s) := rfl lemma continuous_part_apply_eq_zero_of_countable (f : bounded_additive_measure α) (s : set α) (hs : s.countable) : f.continuous_part s = 0 := begin simp [continuous_part], convert f.apply_countable s hs using 2, ext x, simp [and_comm] end lemma continuous_part_apply_diff (f : bounded_additive_measure α) (s t : set α) (hs : s.countable) : f.continuous_part (t \ s) = f.continuous_part t := begin conv_rhs { rw ← diff_union_inter t s }, rw [additive, self_eq_add_right], { exact continuous_part_apply_eq_zero_of_countable _ _ (hs.mono (inter_subset_right _ _)) }, { exact disjoint.mono_right (inter_subset_right _ _) disjoint_sdiff_self_left }, end end bounded_additive_measure open bounded_additive_measure section /-! ### Relationship between continuous functionals and finitely additive measures. -/ lemma norm_indicator_le_one (s : set α) (x : α) : ‖(indicator s (1 : α → ℝ)) x‖ ≤ 1 := by { simp only [indicator, pi.one_apply], split_ifs; norm_num } /-- A functional in the dual space of bounded functions gives rise to a bounded additive measure, by applying the functional to the indicator functions. -/ def _root_.continuous_linear_map.to_bounded_additive_measure [topological_space α] [discrete_topology α] (f : (α →ᵇ ℝ) →L[ℝ] ℝ) : bounded_additive_measure α := { to_fun := λ s, f (of_normed_add_comm_group_discrete (indicator s 1) 1 (norm_indicator_le_one s)), additive' := λ s t hst, begin have : of_normed_add_comm_group_discrete (indicator (s ∪ t) 1) 1 (norm_indicator_le_one _) = of_normed_add_comm_group_discrete (indicator s 1) 1 (norm_indicator_le_one s) + of_normed_add_comm_group_discrete (indicator t 1) 1 (norm_indicator_le_one t), by { ext x, simp [indicator_union_of_disjoint hst], }, rw [this, f.map_add], end, exists_bound := ⟨‖f‖, λ s, begin have I : ‖of_normed_add_comm_group_discrete (indicator s 1) 1 (norm_indicator_le_one s)‖ ≤ 1, by apply norm_of_normed_add_comm_group_le _ zero_le_one, apply le_trans (f.le_op_norm _), simpa using mul_le_mul_of_nonneg_left I (norm_nonneg f), end⟩ } @[simp] lemma continuous_part_eval_clm_eq_zero [topological_space α] [discrete_topology α] (s : set α) (x : α) : (eval_clm ℝ x).to_bounded_additive_measure.continuous_part s = 0 := let f := (eval_clm ℝ x).to_bounded_additive_measure in calc f.continuous_part s = f.continuous_part (s \ {x}) : (continuous_part_apply_diff _ _ _ (countable_singleton x)).symm ... = f ((univ \ f.discrete_support) ∩ (s \ {x})) : rfl ... = indicator ((univ \ f.discrete_support) ∩ (s \ {x})) 1 x : rfl ... = 0 : by simp lemma to_functions_to_measure [measurable_space α] (μ : measure α) [is_finite_measure μ] (s : set α) (hs : measurable_set s) : μ.extension_to_bounded_functions.to_bounded_additive_measure s = (μ s).to_real := begin change μ.extension_to_bounded_functions (of_normed_add_comm_group_discrete (indicator s 1) 1 (norm_indicator_le_one s)) = (μ s).to_real, rw extension_to_bounded_functions_apply, { change ∫ x, s.indicator (λ y, (1 : ℝ)) x ∂μ = _, simp [integral_indicator hs] }, { change integrable (indicator s 1) μ, have : integrable (λ x, (1 : ℝ)) μ := integrable_const (1 : ℝ), apply this.mono' (measurable.indicator (@measurable_const _ _ _ _ (1 : ℝ)) hs).ae_strongly_measurable, apply filter.eventually_of_forall, exact norm_indicator_le_one _ } end lemma to_functions_to_measure_continuous_part [measurable_space α] [measurable_singleton_class α] (μ : measure α) [is_finite_measure μ] [has_no_atoms μ] (s : set α) (hs : measurable_set s) : μ.extension_to_bounded_functions.to_bounded_additive_measure.continuous_part s = (μ s).to_real := begin let f := μ.extension_to_bounded_functions.to_bounded_additive_measure, change f ((univ \ f.discrete_support) ∩ s) = (μ s).to_real, rw to_functions_to_measure, swap, { exact measurable_set.inter (measurable_set.univ.diff (countable.measurable_set f.countable_discrete_support)) hs }, congr' 1, rw [inter_comm, ← inter_diff_assoc, inter_univ], exact measure_diff_null (f.countable_discrete_support.measure_zero _) end end /-! ### A set in `ℝ²` large along verticals, small along horizontals We construct a subset of `ℝ²`, given as a family of sets, which is large along verticals (i.e., it only misses a countable set along each vertical) but small along horizontals (it is countable along horizontals). Such a set can not be measurable as it would contradict Fubini theorem. We need the continuum hypothesis to construct it. -/ theorem sierpinski_pathological_family (Hcont : #ℝ = aleph 1) : ∃ (f : ℝ → set ℝ), (∀ x, (univ \ f x).countable) ∧ (∀ y, {x : ℝ | y ∈ f x}.countable) := begin rcases cardinal.ord_eq ℝ with ⟨r, hr, H⟩, resetI, refine ⟨λ x, {y | r x y}, λ x, _, λ y, _⟩, { have : univ \ {y | r x y} = {y | r y x} ∪ {x}, { ext y, simp only [true_and, mem_univ, mem_set_of_eq, mem_insert_iff, union_singleton, mem_diff], rcases trichotomous_of r x y with h|rfl|h, { simp only [h, not_or_distrib, false_iff, not_true], split, { rintros rfl, exact irrefl_of r y h }, { exact asymm h } }, { simp only [true_or, eq_self_iff_true, iff_true], exact irrefl x }, { simp only [h, iff_true, or_true], exact asymm h } }, rw this, apply countable.union _ (countable_singleton _), rw [cardinal.countable_iff_lt_aleph_one, ← Hcont], exact cardinal.card_typein_lt r x H }, { rw [cardinal.countable_iff_lt_aleph_one, ← Hcont], exact cardinal.card_typein_lt r y H } end /-- A family of sets in `ℝ` which only miss countably many points, but such that any point is contained in only countably many of them. -/ def spf (Hcont : #ℝ = aleph 1) (x : ℝ) : set ℝ := (sierpinski_pathological_family Hcont).some x lemma countable_compl_spf (Hcont : #ℝ = aleph 1) (x : ℝ) : (univ \ spf Hcont x).countable := (sierpinski_pathological_family Hcont).some_spec.1 x lemma countable_spf_mem (Hcont : #ℝ = aleph 1) (y : ℝ) : {x | y ∈ spf Hcont x}.countable := (sierpinski_pathological_family Hcont).some_spec.2 y /-! ### A counterexample for the Pettis integral We construct a function `f` from `[0,1]` to a complete Banach space `B`, which is weakly measurable (i.e., for any continuous linear form `φ` on `B` the function `φ ∘ f` is measurable), bounded in norm (i.e., for all `x`, one has `‖f x‖ ≤ 1`), and still `f` has no Pettis integral. This construction, due to Phillips, requires the continuum hypothesis. We will take for `B` the space of all bounded functions on `ℝ`, with the supremum norm (no measure here, we are really talking of everywhere defined functions). And `f x` will be the characteristic function of a set which is large (it has countable complement), as in the Sierpinski pathological family. -/ /-- A family of bounded functions `f_x` from `ℝ` (seen with the discrete topology) to `ℝ` (in fact taking values in `{0, 1}`), indexed by a real parameter `x`, corresponding to the characteristic functions of the different fibers of the Sierpinski pathological family -/ def f (Hcont : #ℝ = aleph 1) (x : ℝ) : (discrete_copy ℝ →ᵇ ℝ) := of_normed_add_comm_group_discrete (indicator (spf Hcont x) 1) 1 (norm_indicator_le_one _) lemma apply_f_eq_continuous_part (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) (x : ℝ) (hx : φ.to_bounded_additive_measure.discrete_support ∩ spf Hcont x = ∅) : φ (f Hcont x) = φ.to_bounded_additive_measure.continuous_part univ := begin set ψ := φ.to_bounded_additive_measure with hψ, have : φ (f Hcont x) = ψ (spf Hcont x) := rfl, have U : univ = spf Hcont x ∪ (univ \ spf Hcont x), by simp only [union_univ, union_diff_self], rw [this, eq_add_parts, discrete_part_apply, hx, ψ.empty, zero_add, U, ψ.continuous_part.additive _ _ disjoint_sdiff_self_right, ψ.continuous_part_apply_eq_zero_of_countable _ (countable_compl_spf Hcont x), add_zero], end lemma countable_ne (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) : {x | φ.to_bounded_additive_measure.continuous_part univ ≠ φ (f Hcont x)}.countable := begin have A : {x | φ.to_bounded_additive_measure.continuous_part univ ≠ φ (f Hcont x)} ⊆ {x | φ.to_bounded_additive_measure.discrete_support ∩ spf Hcont x ≠ ∅}, { assume x hx, contrapose! hx, simp only [not_not, mem_set_of_eq] at hx, simp [apply_f_eq_continuous_part Hcont φ x hx], }, have B : {x | φ.to_bounded_additive_measure.discrete_support ∩ spf Hcont x ≠ ∅} ⊆ ⋃ y ∈ φ.to_bounded_additive_measure.discrete_support, {x | y ∈ spf Hcont x}, { assume x hx, dsimp at hx, rw [← ne.def, ←nonempty_iff_ne_empty] at hx, simp only [exists_prop, mem_Union, mem_set_of_eq], exact hx }, apply countable.mono (subset.trans A B), exact countable.bUnion (countable_discrete_support _) (λ a ha, countable_spf_mem Hcont a), end lemma comp_ae_eq_const (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) : ∀ᵐ x ∂(volume.restrict (Icc (0 : ℝ) 1)), φ.to_bounded_additive_measure.continuous_part univ = φ (f Hcont x) := begin apply ae_restrict_of_ae, refine measure_mono_null _ ((countable_ne Hcont φ).measure_zero _), assume x, simp only [imp_self, mem_set_of_eq, mem_compl_iff], end lemma integrable_comp (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) : integrable_on (λ x, φ (f Hcont x)) (Icc 0 1) := begin have : integrable_on (λ x, φ.to_bounded_additive_measure.continuous_part univ) (Icc (0 : ℝ) 1) volume, by simp [integrable_on_const], apply integrable.congr this (comp_ae_eq_const Hcont φ), end lemma integral_comp (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) : ∫ x in Icc 0 1, φ (f Hcont x) = φ.to_bounded_additive_measure.continuous_part univ := begin rw ← integral_congr_ae (comp_ae_eq_const Hcont φ), simp, end /-! The next few statements show that the function `f Hcont : ℝ → (discrete_copy ℝ →ᵇ ℝ)` takes its values in a complete space, is scalarly measurable, is everywhere bounded by `1`, and still has no Pettis integral. -/ example : complete_space (discrete_copy ℝ →ᵇ ℝ) := by apply_instance /-- The function `f Hcont : ℝ → (discrete_copy ℝ →ᵇ ℝ)` is scalarly measurable. -/ lemma measurable_comp (Hcont : #ℝ = aleph 1) (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ) : measurable (λ x, φ (f Hcont x)) := begin have : measurable (λ x, φ.to_bounded_additive_measure.continuous_part univ) := measurable_const, refine this.measurable_of_countable_ne _, exact countable_ne Hcont φ, end /-- The function `f Hcont : ℝ → (discrete_copy ℝ →ᵇ ℝ)` is uniformly bounded by `1` in norm. -/ lemma norm_bound (Hcont : #ℝ = aleph 1) (x : ℝ) : ‖f Hcont x‖ ≤ 1 := norm_of_normed_add_comm_group_le _ zero_le_one _ /-- The function `f Hcont : ℝ → (discrete_copy ℝ →ᵇ ℝ)` has no Pettis integral. -/ theorem no_pettis_integral (Hcont : #ℝ = aleph 1) : ¬ ∃ (g : discrete_copy ℝ →ᵇ ℝ), ∀ (φ : (discrete_copy ℝ →ᵇ ℝ) →L[ℝ] ℝ), ∫ x in Icc 0 1, φ (f Hcont x) = φ g := begin rintros ⟨g, h⟩, simp only [integral_comp] at h, have : g = 0, { ext x, have : g x = eval_clm ℝ x g := rfl, rw [this, ← h], simp }, simp only [this, continuous_linear_map.map_zero] at h, specialize h (volume.restrict (Icc (0 : ℝ) 1)).extension_to_bounded_functions, simp_rw [to_functions_to_measure_continuous_part _ _ measurable_set.univ] at h, simpa using h, end end phillips_1940
0874027c417d67a48b6fed9aa74e3b6ca21e3d85
7490bf5d40d31857a58062614642bb5a41c36154
/hw1.lean
f329aa239de35ea375e96c8d2c8bde304b6bb9aa
[]
no_license
reesegrayallen/Lean-Discrete-Mathematics
9f1d6fe1c814cc9264ce868a67adcf5a82566e22
00c875284613ea12e0a729f519738aab8599456b
refs/heads/main
1,674,181,372,629
1,606,801,004,000
1,606,801,004,000
317,387,970
0
0
null
null
null
null
UTF-8
Lean
false
false
3,147
lean
/- Homework #1 Reese Allen, rga2uz 20Sp CS 2102-001 -/ -- namespace for identifiers in this file -- namespace ends below, see end of file namespace hidden /- An abstract data type combines a data type and a set of functions that operate on values of that type. As we're implementing a Boolean algebra ADT, we define a bool data type and a set of standard Boolean operators (functions). -/ /- ********* DATA TYPE ********* -/ inductive dm_bool : Type | dm_tt : dm_bool | dm_ff : dm_bool -- Data type has it's own namespace open dm_bool -- Some examples of definitions using this type def b1 := dm_tt def b2 := dm_ff /- ********* FUNCTIONS ********* -/ -- a unary function, a.k.a, operator /- Truth table for "not" ARG RES ------|------ dm_tt | dm_ff ------|------ dm_ff | dm_tt ------|------ -/ def dm_not : dm_bool → dm_bool := λ (b : dm_bool), match b with | dm_tt := dm_ff | dm_ff := dm_tt end -- binary functions, a.k.a., operators /- Truth table for "and" b1 b2 res --------- tt tt tt tt ff ff ff tt ff ff ff ff -/ def dm_and : dm_bool → dm_bool → dm_bool := λ (b1 b2 : dm_bool), match b1, b2 with | dm_tt, dm_tt := dm_tt | dm_tt, dm_ff := dm_ff | dm_ff, dm_tt := dm_ff | dm_ff, dm_ff := dm_ff end #reduce dm_and dm_tt dm_tt -- A shorter though less clear definition def dm_and' : dm_bool → dm_bool → dm_bool := λ (b1 b2 : dm_bool), match b1 with | dm_tt := b2 | _ := dm_ff end -- The definition of "or" is similar def dm_or : dm_bool → dm_bool → dm_bool := λ (b1 b2 : dm_bool), match b1 with | dm_ff := b2 | _ := dm_tt end /- Truth table for "identity function" ARG RES ------|------ dm_tt | dm_tt ------|------ dm_ff | dm_ff ------|------ -/ def dm_id : dm_bool → dm_bool := λ (b : dm_bool), match b with | dm_tt := dm_tt | dm_ff := dm_ff end /- Truth table for "xor" (exclusive-or) ; true only when inputs differ b1 b2 res --------- tt tt ff tt ff tt ff tt tt ff ff ff -/ def dm_xor : dm_bool → dm_bool → dm_bool := λ (b1 b2 : dm_bool), match b1, b2 with | dm_tt, dm_ff := dm_tt | dm_ff, dm_tt := dm_tt | _ , _ := dm_ff end /- Truth table for "nand" ; essentially the opposite of and b1 b2 res --------- tt tt ff tt ff tt ff tt tt ff ff tt -/ def dm_nand : dm_bool → dm_bool → dm_bool := λ (b1 b2 : dm_bool), match b1, b2 with | dm_tt, dm_tt := dm_ff | _ , _ := dm_tt end /- Truth table for "implies" b1 b2 res --------- tt tt tt tt ff ff ff tt tt ff ff tt -/ def dm_implies : dm_bool → dm_bool → dm_bool := λ (b1 b2 : dm_bool), match b1, b2 with | dm_tt, dm_ff := dm_ff | _ , _ := dm_tt end /- Truth table for "equivalent" b1 b2 res --------- tt tt tt tt ff ff ff tt ff ff ff tt -/ def dm_equiv : dm_bool → dm_bool → dm_bool := λ (b1 b2 : dm_bool), match b1, b2 with | dm_tt, dm_tt := dm_tt | dm_ff, dm_ff := dm_tt | _ , _ := dm_ff end end hidden
d844e57363ed365b1fa7ebcdf4c9f6ace9abde3e
351a46035517d2a1985619b8cabdf263754d343a
/src/ch5.lean
e65ad034cb77ed2664d774b36781cfaea832b5ce
[]
no_license
kaychaks/logic_proof
accc212517b613caca92c10db77e6aaf6b7ccfbc
90f3bf0acbabf558ba2f82dee968255d8bfe2de1
refs/heads/master
1,587,001,734,509
1,548,235,051,000
1,548,235,051,000
165,186,786
0
0
null
null
null
null
UTF-8
Lean
false
false
1,286
lean
variables A B : Prop open classical example : ¬ A → false → A := sorry example : ¬ A ∨ ¬ B → ¬ (A ∧ B) := assume h, show ¬ (A ∧ B), from assume h1 : A ∧ B, show false, from or.elim h (assume na : ¬ A, show false, from na h1.left) (assume nb : ¬ B, show false, from nb h1.right) -- Prove ¬ (A ∧ B) → ¬ A ∨ ¬ B by replacing the sorry's below -- by proofs. lemma step1 (h₁ : ¬ (A ∧ B)) (h₂ : A) : ¬ A ∨ ¬ B := have ¬ B, from assume b : B, show false, from h₁ (and.intro h₂ b), show ¬ A ∨ ¬ B, from or.inr this lemma step2 (h₁ : ¬ (A ∧ B)) (h₂ : ¬ (¬ A ∨ ¬ B)) : false := have ¬ A, from assume : A, have ¬ A ∨ ¬ B, from step1 h₁ ‹A›, show false, from h₂ this, show false, from h₂ (or.inl this) theorem step3 (h : ¬ (A ∧ B)) : ¬ A ∨ ¬ B := by_contradiction (assume h' : ¬ (¬ A ∨ ¬ B), show false, from step2 h h') example (h : ¬ B → ¬ A) : A → B := assume a, by_contradiction (assume h1 : ¬ B, show false, from h h1 a) example (h : A → B) : ¬ A ∨ B := by_contradiction (assume h1 : ¬ (¬ A ∨ B), show false, from have a : ¬ A, from assume aa : A, show false, from h1 (or.inr (h aa)), h1 (or.inl a))
b4820d6ae8261bb7f004c81a6ad4e975c57af29b
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/tactic/fin_cases.lean
a38ed37cc72abbcf743f8282d395c962153d11da
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
4,298
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Scott Morrison Case bashing: * on `x ∈ A`, for `A : finset α` or `A : list α`, or * on `x : A`, with `[fintype A]`. -/ import data.fintype import tactic.norm_num namespace tactic open lean.parser open interactive interactive.types expr open conv.interactive /-- Checks that the expression looks like `x ∈ A` for `A : finset α`, `multiset α` or `A : list α`, and returns the type α. -/ meta def guard_mem_fin (e : expr) : tactic expr := do t ← infer_type e, α ← mk_mvar, to_expr ``(_ ∈ (_ : finset %%α)) tt ff >>= unify t <|> to_expr ``(_ ∈ (_ : multiset %%α)) tt ff >>= unify t <|> to_expr ``(_ ∈ (_ : list %%α)) tt ff >>= unify t, instantiate_mvars α meta def expr_list_to_list_expr : Π (e : expr), tactic (list expr) | `(list.cons %%h %%t) := list.cons h <$> expr_list_to_list_expr t | `([]) := return [] | _ := failed meta def fin_cases_at_aux : Π (with_list : list expr) (e : expr), tactic unit | with_list e := (do result ← cases_core e, match result with -- We have a goal with an equation `s`, and a second goal with a smaller `e : x ∈ _`. | [(_, [s], _), (_, [e], _)] := do let sn := local_pp_name s, ng ← num_goals, -- tidy up the new value match with_list.nth 0 with -- If an explicit value was specified via the `with` keyword, use that. | (some h) := tactic.interactive.conv (some sn) none (to_rhs >> conv.interactive.change (to_pexpr h)) -- Otherwise, call `norm_num`. We let `norm_num` unfold `max` and `min` -- because it's helpful for the `interval_cases` tactic. | _ := try $ tactic.interactive.norm_num [simp_arg_type.expr ``(max), simp_arg_type.expr ``(min)] (loc.ns [some sn]) end, s ← get_local sn, try `[subst %%s], ng' ← num_goals, when (ng = ng') (rotate_left 1), fin_cases_at_aux with_list.tail e -- No cases; we're done. | [] := skip | _ := failed end) meta def fin_cases_at : Π (with_list : option pexpr) (e : expr), tactic unit | with_list e := do ty ← try_core $ guard_mem_fin e, match ty with | none := -- Deal with `x : A`, where `[fintype A]` is available: (do ty ← infer_type e, i ← to_expr ``(fintype %%ty) >>= mk_instance <|> fail "Failed to find `fintype` instance.", t ← to_expr ``(%%e ∈ @fintype.elems %%ty %%i), v ← to_expr ``(@fintype.complete %%ty %%i %%e), h ← assertv `h t v, fin_cases_at with_list h) | (some ty) := -- Deal with `x ∈ A` hypotheses: (do with_list ← match with_list with | (some e) := do e ← to_expr ``(%%e : list %%ty), expr_list_to_list_expr e | none := return [] end, fin_cases_at_aux with_list e) end namespace interactive private meta def hyp := tk "*" *> return none <|> some <$> ident local postfix `?`:9001 := optional /-- `fin_cases h` performs case analysis on a hypothesis of the form `h : A`, where `[fintype A]` is available, or `h ∈ A`, where `A : finset X`, `A : multiset X` or `A : list X`. `fin_cases *` performs case analysis on all suitable hypotheses. As an example, in ``` example (f : ℕ → Prop) (p : fin 3) (h0 : f 0) (h1 : f 1) (h2 : f 2) : f p.val := begin fin_cases *; simp, all_goals { assumption } end ``` after `fin_cases p; simp`, there are three goals, `f 0`, `f 1`, and `f 2`. -/ meta def fin_cases : parse hyp → parse (tk "with" *> texpr)? → tactic unit | none none := focus1 $ do ctx ← local_context, ctx.mfirst (fin_cases_at none) <|> fail "No hypothesis of the forms `x ∈ A`, where `A : finset X`, `A : list X`, or `A : multiset X`, or `x : A`, with `[fintype A]`." | none (some _) := fail "Specify a single hypothesis when using a `with` argument." | (some n) with_list := do h ← get_local n, focus1 $ fin_cases_at with_list h end interactive add_tactic_doc { name := "fin_cases", category := doc_category.tactic, decl_names := [`tactic.interactive.fin_cases], tags := ["case bashing"] } end tactic
d10de9db0cb6df2f6e92fadb55d6caf5cf9ca4d0
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/p_series.lean
0467dc5f398fecb5cbe34f788e1f674e309bf754
[]
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
5,969
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.special_functions.pow import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Convergence of `p`-series In this file we prove that the series `∑' k in ℕ, 1 / k ^ p` converges if and only if `p > 1`. The proof is based on the [Cauchy condensation test](https://en.wikipedia.org/wiki/Cauchy_condensation_test): `∑ k, f k` converges if and only if so does `∑ k, 2 ^ k f (2 ^ k)`. We prove this test in `nnreal.summable_condensed_iff` and `summable_condensed_iff_of_nonneg`, then use it to prove `summable_one_div_rpow`. After this transformation, a `p`-series turns into a geometric series. ## TODO It should be easy to generalize arguments to Schlömilch's generalization of the Cauchy condensation test once we need it. ## Tags p-series, Cauchy condensation test -/ /-! ### Cauchy condensation test In this section we prove the Cauchy condensation test: for `f : ℕ → ℝ≥0` or `f : ℕ → ℝ`, `∑ k, f k` converges if and only if so does `∑ k, 2 ^ k f (2 ^ k)`. Instead of giving a monolithic proof, we split it into a series of lemmas with explicit estimates of partial sums of each series in terms of the partial sums of the other series. -/ namespace finset theorem le_sum_condensed' {M : Type u_1} [ordered_add_comm_monoid M] {f : ℕ → M} (hf : ∀ {m n : ℕ}, 0 < m → m ≤ n → f n ≤ f m) (n : ℕ) : (finset.sum (Ico 1 (bit0 1 ^ n)) fun (k : ℕ) => f k) ≤ finset.sum (range n) fun (k : ℕ) => bit0 1 ^ k •ℕ f (bit0 1 ^ k) := sorry theorem le_sum_condensed {M : Type u_1} [ordered_add_comm_monoid M] {f : ℕ → M} (hf : ∀ {m n : ℕ}, 0 < m → m ≤ n → f n ≤ f m) (n : ℕ) : (finset.sum (range (bit0 1 ^ n)) fun (k : ℕ) => f k) ≤ f 0 + finset.sum (range n) fun (k : ℕ) => bit0 1 ^ k •ℕ f (bit0 1 ^ k) := sorry theorem sum_condensed_le' {M : Type u_1} [ordered_add_comm_monoid M] {f : ℕ → M} (hf : ∀ {m n : ℕ}, 1 < m → m ≤ n → f n ≤ f m) (n : ℕ) : (finset.sum (range n) fun (k : ℕ) => bit0 1 ^ k •ℕ f (bit0 1 ^ (k + 1))) ≤ finset.sum (Ico (bit0 1) (bit0 1 ^ n + 1)) fun (k : ℕ) => f k := sorry theorem sum_condensed_le {M : Type u_1} [ordered_add_comm_monoid M] {f : ℕ → M} (hf : ∀ {m n : ℕ}, 1 < m → m ≤ n → f n ≤ f m) (n : ℕ) : (finset.sum (range (n + 1)) fun (k : ℕ) => bit0 1 ^ k •ℕ f (bit0 1 ^ k)) ≤ f 1 + bit0 1 •ℕ finset.sum (Ico (bit0 1) (bit0 1 ^ n + 1)) fun (k : ℕ) => f k := sorry end finset namespace ennreal theorem le_tsum_condensed {f : ℕ → ennreal} (hf : ∀ {m n : ℕ}, 0 < m → m ≤ n → f n ≤ f m) : (tsum fun (k : ℕ) => f k) ≤ f 0 + tsum fun (k : ℕ) => bit0 1 ^ k * f (bit0 1 ^ k) := sorry theorem tsum_condensed_le {f : ℕ → ennreal} (hf : ∀ {m n : ℕ}, 1 < m → m ≤ n → f n ≤ f m) : (tsum fun (k : ℕ) => bit0 1 ^ k * f (bit0 1 ^ k)) ≤ f 1 + bit0 1 * tsum fun (k : ℕ) => f k := sorry end ennreal namespace nnreal /-- Cauchy condensation test for a series of `nnreal` version. -/ theorem summable_condensed_iff {f : ℕ → nnreal} (hf : ∀ {m n : ℕ}, 0 < m → m ≤ n → f n ≤ f m) : (summable fun (k : ℕ) => bit0 1 ^ k * f (bit0 1 ^ k)) ↔ summable f := sorry end nnreal /-- Cauchy condensation test for series of nonnegative real numbers. -/ theorem summable_condensed_iff_of_nonneg {f : ℕ → ℝ} (h_nonneg : ∀ (n : ℕ), 0 ≤ f n) (h_mono : ∀ {m n : ℕ}, 0 < m → m ≤ n → f n ≤ f m) : (summable fun (k : ℕ) => bit0 1 ^ k * f (bit0 1 ^ k)) ↔ summable f := sorry /-! ### Convergence of the `p`-series In this section we prove that for a real number `p`, the series `∑' n : ℕ, 1 / (n ^ p)` converges if and only if `1 < p`. There are many different proofs of this fact. The proof in this file uses the Cauchy condensation test we formalized above. This test implies that `∑ n, 1 / (n ^ p)` converges if and only if `∑ n, 2 ^ n / ((2 ^ n) ^ p)` converges, and the latter series is a geometric series with common ratio `2 ^ {1 - p}`. -/ /-- Test for congergence of the `p`-series: the real-valued series `∑' n : ℕ, (n ^ p)⁻¹` converges if and only if `1 < p`. -/ @[simp] theorem real.summable_nat_rpow_inv {p : ℝ} : (summable fun (n : ℕ) => ↑n ^ p⁻¹) ↔ 1 < p := sorry /-- Test for congergence of the `p`-series: the real-valued series `∑' n : ℕ, 1 / n ^ p` converges if and only if `1 < p`. -/ theorem real.summable_one_div_nat_rpow {p : ℝ} : (summable fun (n : ℕ) => 1 / ↑n ^ p) ↔ 1 < p := sorry /-- Test for congergence of the `p`-series: the real-valued series `∑' n : ℕ, (n ^ p)⁻¹` converges if and only if `1 < p`. -/ @[simp] theorem real.summable_nat_pow_inv {p : ℕ} : (summable fun (n : ℕ) => ↑n ^ p⁻¹) ↔ 1 < p := sorry /-- Test for congergence of the `p`-series: the real-valued series `∑' n : ℕ, 1 / n ^ p` converges if and only if `1 < p`. -/ theorem real.summable_one_div_nat_pow {p : ℕ} : (summable fun (n : ℕ) => 1 / ↑n ^ p) ↔ 1 < p := sorry /-- Harmonic series is not unconditionally summable. -/ theorem real.not_summable_nat_cast_inv : ¬summable fun (n : ℕ) => ↑n⁻¹ := sorry /-- Harmonic series is not unconditionally summable. -/ theorem real.not_summable_one_div_nat_cast : ¬summable fun (n : ℕ) => 1 / ↑n := sorry /-- Harmonic series diverges. -/ theorem real.tendsto_sum_range_one_div_nat_succ_at_top : filter.tendsto (fun (n : ℕ) => finset.sum (finset.range n) fun (i : ℕ) => 1 / (↑i + 1)) filter.at_top filter.at_top := sorry @[simp] theorem nnreal.summable_one_rpow_inv {p : ℝ} : (summable fun (n : ℕ) => ↑n ^ p⁻¹) ↔ 1 < p := sorry theorem nnreal.summable_one_div_rpow {p : ℝ} : (summable fun (n : ℕ) => 1 / ↑n ^ p) ↔ 1 < p := sorry
a3fb338d751d5328bce4762a45e725a33aa57958
367134ba5a65885e863bdc4507601606690974c1
/src/tactic/lint/simp.lean
cb14925a683e97325726fba1e6a1721565cb662f
[ "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,340
lean
/- Copyright (c) 2020 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import tactic.lint.basic /-! # Linter for simplification lemmas This files defines several linters that prevent common mistakes when declaring simp lemmas: * `simp_nf` checks that the left-hand side of a simp lemma is not simplified by a different lemma. * `simp_var_head` checks that the head symbol of the left-hand side is not a variable. * `simp_comm` checks that commutativity lemmas are not marked as simplification lemmas. -/ open tactic expr /-- `simp_lhs_rhs ty` returns the left-hand and right-hand side of a simp lemma with type `ty`. -/ private meta def simp_lhs_rhs : expr → tactic (expr × expr) | ty := do ty ← head_beta ty, -- We only detect a fixed set of simp relations here. -- This is somewhat justified since for a custom simp relation R, -- the simp lemma `R a b` is implicitly converted to `R a b ↔ true` as well. match ty with | `(¬ %%lhs) := pure (lhs, `(false)) | `(%%lhs = %%rhs) := pure (lhs, rhs) | `(%%lhs ↔ %%rhs) := pure (lhs, rhs) | (expr.pi n bi a b) := do l ← mk_local' n bi a, simp_lhs_rhs (b.instantiate_var l) | ty := pure (ty, `(true)) end /-- `simp_lhs ty` returns the left-hand side of a simp lemma with type `ty`. -/ private meta def simp_lhs (ty : expr): tactic expr := prod.fst <$> simp_lhs_rhs ty /-- `simp_is_conditional_core ty` returns `none` if `ty` is a conditional simp lemma, and `some lhs` otherwise. -/ private meta def simp_is_conditional_core : expr → tactic (option expr) | ty := do ty ← head_beta ty, match ty with | `(¬ %%lhs) := pure lhs | `(%%lhs = _) := pure lhs | `(%%lhs ↔ _) := pure lhs | (expr.pi n bi a b) := do l ← mk_local' n bi a, some lhs ← simp_is_conditional_core (b.instantiate_var l) | pure none, if bi ≠ binder_info.inst_implicit ∧ ¬ (lhs.abstract_local l.local_uniq_name).has_var then pure none else pure lhs | ty := pure ty end /-- `simp_is_conditional ty` returns true iff the simp lemma with type `ty` is conditional. -/ private meta def simp_is_conditional (ty : expr) : tactic bool := option.is_none <$> simp_is_conditional_core ty private meta def heuristic_simp_lemma_extraction (prf : expr) : tactic (list name) := prf.list_constant.to_list.mfilter is_simp_lemma /-- Checks whether two expressions are equal for the simplifier. That is, they are reducibly-definitional equal, and they have the same head symbol. -/ meta def is_simp_eq (a b : expr) : tactic bool := if a.get_app_fn.const_name ≠ b.get_app_fn.const_name then pure ff else succeeds $ is_def_eq a b transparency.reducible /-- Reports declarations that are simp lemmas whose left-hand side is not in simp-normal form. -/ meta def simp_nf_linter (timeout := 200000) (d : declaration) : tactic (option string) := do tt ← is_simp_lemma d.to_name | pure none, -- Sometimes, a definition is tagged @[simp] to add the equational lemmas to the simp set. -- In this case, ignore the declaration if it is not a valid simp lemma by itself. tt ← is_valid_simp_lemma_cnst d.to_name | pure none, [] ← get_eqn_lemmas_for ff d.to_name | pure none, try_for timeout $ retrieve $ do g ← mk_meta_var d.type, set_goals [g], unfreezing intros, (lhs, rhs) ← target >>= simp_lhs_rhs, sls ← simp_lemmas.mk_default, let sls' := sls.erase [d.to_name], (lhs', prf1, ns1) ← decorate_error "simplify fails on left-hand side:" $ simplify sls [] lhs {fail_if_unchanged := ff}, prf1_lems ← heuristic_simp_lemma_extraction prf1, if d.to_name ∈ prf1_lems then pure none else do is_cond ← simp_is_conditional d.type, (rhs', prf2, ns2) ← decorate_error "simplify fails on right-hand side:" $ simplify sls [] rhs {fail_if_unchanged := ff}, lhs'_eq_rhs' ← is_simp_eq lhs' rhs', lhs_in_nf ← is_simp_eq lhs' lhs, if lhs'_eq_rhs' then do used_lemmas ← heuristic_simp_lemma_extraction (prf1 prf2), pure $ pure $ "simp can prove this:\n" ++ " by simp only " ++ to_string used_lemmas ++ "\n" ++ "One of the lemmas above could be a duplicate.\n" ++ "If that's not the case try reordering lemmas or adding @[priority].\n" else if ¬ lhs_in_nf then do lhs ← pp lhs, lhs' ← pp lhs', pure $ format.to_string $ to_fmt "Left-hand side simplifies from" ++ lhs.group.indent 2 ++ format.line ++ "to" ++ lhs'.group.indent 2 ++ format.line ++ "using " ++ (to_fmt prf1_lems).group.indent 2 ++ format.line ++ "Try to change the left-hand side to the simplified term!\n" else if ¬ is_cond ∧ lhs = lhs' then do pure "Left-hand side does not simplify.\nYou need to debug this yourself using `set_option trace.simplify.rewrite true`" else pure none /-- This note gives you some tips to debug any errors that the simp-normal form linter raises. The reason that a lemma was considered faulty is because its left-hand side is not in simp-normal form. These lemmas are hence never used by the simplifier. This linter gives you a list of other simp lemmas: look at them! Here are some tips depending on the error raised by the linter: 1. 'the left-hand side reduces to XYZ': you should probably use XYZ as the left-hand side. 2. 'simp can prove this': This typically means that lemma is a duplicate, or is shadowed by another lemma: 2a. Always put more general lemmas after specific ones: ``` @[simp] lemma zero_add_zero : 0 + 0 = 0 := rfl @[simp] lemma add_zero : x + 0 = x := rfl ``` And not the other way around! The simplifier always picks the last matching lemma. 2b. You can also use `@[priority]` instead of moving simp-lemmas around in the file. Tip: the default priority is 1000. Use `@[priority 1100]` instead of moving a lemma down, and `@[priority 900]` instead of moving a lemma up. 2c. Conditional simp lemmas are tried last. If they are shadowed just remove the `simp` attribute. 2d. If two lemmas are duplicates, the linter will complain about the first one. Try to fix the second one instead! (You can find it among the other simp lemmas the linter prints out!) 3. 'try_for tactic failed, timeout': This typically means that there is a loop of simp lemmas. Try to apply squeeze_simp to the right-hand side (removing this lemma from the simp set) to see what lemmas might be causing the loop. Another trick is to `set_option trace.simplify.rewrite true` and then apply `try_for 10000 { simp }` to the right-hand side. You will see a periodic sequence of lemma applications in the trace message. -/ library_note "simp-normal form" /-- A linter for simp lemmas whose lhs is not in simp-normal form, and which hence never fire. -/ @[linter] meta def linter.simp_nf : linter := { test := simp_nf_linter, auto_decls := tt, no_errors_found := "All left-hand sides of simp lemmas are in simp-normal form", errors_found := "SOME SIMP LEMMAS ARE NOT IN SIMP-NORMAL FORM. see note [simp-normal form] for tips how to debug this. https://leanprover-community.github.io/mathlib_docs/notes.html#simp-normal%20form " } private meta def simp_var_head (d : declaration) : tactic (option string) := do tt ← is_simp_lemma d.to_name | pure none, -- Sometimes, a definition is tagged @[simp] to add the equational lemmas to the simp set. -- In this case, ignore the declaration if it is not a valid simp lemma by itself. tt ← is_valid_simp_lemma_cnst d.to_name | pure none, lhs ← simp_lhs d.type, head_sym@(expr.local_const _ _ _ _) ← pure lhs.get_app_fn | pure none, head_sym ← pp head_sym, pure $ format.to_string $ "Left-hand side has variable as head symbol: " ++ head_sym /-- A linter for simp lemmas whose lhs has a variable as head symbol, and which hence never fire. -/ @[linter] meta def linter.simp_var_head : linter := { test := simp_var_head, auto_decls := tt, no_errors_found := "No left-hand sides of a simp lemma has a variable as head symbol", errors_found := "LEFT-HAND SIDE HAS VARIABLE AS HEAD SYMBOL.\n" ++ "Some simp lemmas have a variable as head symbol of the left-hand side" } private meta def simp_comm (d : declaration) : tactic (option string) := do tt ← is_simp_lemma d.to_name | pure none, -- Sometimes, a definition is tagged @[simp] to add the equational lemmas to the simp set. -- In this case, ignore the declaration if it is not a valid simp lemma by itself. tt ← is_valid_simp_lemma_cnst d.to_name | pure none, (lhs, rhs) ← simp_lhs_rhs d.type, if lhs.get_app_fn.const_name ≠ rhs.get_app_fn.const_name then pure none else do (lhs', rhs') ← (prod.snd <$> open_pis_metas d.type) >>= simp_lhs_rhs, tt ← succeeds $ unify rhs lhs' transparency.reducible | pure none, tt ← succeeds $ is_def_eq rhs lhs' transparency.reducible | pure none, -- ensure that the second application makes progress: ff ← succeeds $ unify lhs' rhs' transparency.reducible | pure none, pure $ "should not be marked simp" /-- A linter for commutativity lemmas that are marked simp. -/ @[linter] meta def linter.simp_comm : linter := { test := simp_comm, auto_decls := tt, no_errors_found := "No commutativity lemma is marked simp", errors_found := "COMMUTATIVITY LEMMA IS SIMP.\n" ++ "Some commutativity lemmas are simp lemmas" }
8f2ea77f2ba7945ff069fc1bd174873acb9b5f2f
18a72c9e704298dc782e2cc8d85d0a37e783a3f4
/Bigop.lean
75cbbf338c6fccccfd3e7347423a9600c223d72d
[]
no_license
Kha/macro-supplement
dfb6298f8aafc700a2aea1820ace70e3d0f6ab43
1d98fe918cc70433a489cae1dab5b2b2f889829d
refs/heads/master
1,677,039,200,024
1,675,790,384,000
1,675,790,384,000
236,180,481
7
1
null
1,650,524,499,000
1,579,962,356,000
Lean
UTF-8
Lean
false
false
4,208
lean
namespace Bigop section Preliminaries def Seq (α : Type) := List α def BigBody (β α) := α × (β → β → β) × Bool × β def applyBig (body : BigBody β α) (x : β) : β := let (_, op, b, v) := body if b then op v x else x def reducebig (idx : β) (r : Seq α) (body : α → BigBody β α) : β := r.foldr (applyBig ∘ body) idx def bigop := @reducebig def iota : Nat → Nat → List Nat | m, 0 => [] | m, n+1 => m :: iota (m+1) n def index_iota (m n : Nat) := iota m (n - m) class Enumerable (α : Type) where elems : List α instance : Enumerable Bool where elems := [false, true] instance [Enumerable α] [Enumerable β]: Enumerable (α × β) where elems := Enumerable.elems.bind fun a => Enumerable.elems.map fun b => (a, b) def finElemsAux (n : Nat) : (i : Nat) → i < n → List (Fin n) | 0, h => [⟨0, h⟩] | i+1, h => ⟨i+1, h⟩ :: finElemsAux n i (Nat.lt_of_succ_lt h) def finElems : (n : Nat) → List (Fin n) | 0 => [] | (n+1) => finElemsAux (n+1) n (Nat.lt_succ_self n) instance : Enumerable (Fin n) where elems := (finElems n).reverse instance : OfNat (Fin (Nat.succ n)) m where ofNat := Fin.ofNat m end Preliminaries -- Declare a new syntax category for "indexing" big operators -- declare_syntax_cat declare_syntax_cat index syntax ident "<-" term : index syntax ident "<-" term "|" term : index -- end syntax term:51 "≤" ident "<" term : index syntax term:51 "≤" ident "<" term "|" term : index -- Primitive notation for big operators syntax "\\big[" term:max "/" term "]_(" index ")" term : term -- We define how to expand `\big` with the different kinds of index macro_rules | `(\big[$op/$idx]_($i:ident <- $r) $F) => `(bigop $idx $r (fun $i => ($i, $op, true, $F))) | `(\big[$op/$idx]_($i:ident <- $r | $p) $F) => `(bigop $idx $r (fun $i => ($i, $op, $p, $F))) | `(\big[$op/$idx]_($lower:term ≤ $i:ident < $upper) $F) => `(bigop $idx (index_iota $lower $upper) (fun $i => ($i, $op, true, $F))) | `(\big[$op/$idx]_($lower:term ≤ $i:ident < $upper | $p) $F) => `(bigop $idx (index_iota $lower $upper) (fun $i => ($i, $op, $p, $F))) -- Sum macro "Σ" "(" idx:index ")" F:term : term => `(\big[Add.add/0]_($idx) $F) -- end -- We can already use `Σ` with the different kinds of index. #check Σ (i <- [0, 2, 4] | i != 2) i #check Σ (10 ≤ i < 20 | i != 5) i+1 #check Σ (10 ≤ i < 20) i+1 -- Define `Π` -- Prod macro "Π" "(" idx:index ")" F:term : term => `(\big[Mul.mul/1]_($idx) $F) -- end -- The examples above now also work for `Π` #check Π (i <- [0, 2, 4] | i != 2) i #check Π (10 ≤ i < 20 | i != 5) i+1 #check Π (10 ≤ i < 20) i+1 -- We can extend our grammar for the syntax category `index`. syntax ident "|" term : index syntax ident ":" term : index syntax ident ":" term "|" term : index -- Add new rules macro_rules | `(\big[$op/$idx]_($i:ident : $type) $F) => `(bigop $idx (Enumerable.elems (α := $type)) (fun $i => ($i, $op, true, $F))) | `(\big[$op/$idx]_($i:ident : $type | $p) $F) => `(bigop $idx (Enumerable.elems (α := $type)) (fun $i => ($i, $op, $p, $F))) | `(\big[$op/$idx]_($i:ident | $p) $F) => `(bigop $idx Enumerable.elems (fun $i => ($i, $op, $p, $F))) -- The new syntax is immediately available for all big operators that we have defined def myPred (x : Fin 10) : Bool := true #check Σ (i : Fin 10) i+1 #check Σ (i : Fin 10 | i != 2) i+1 #check Σ (i | myPred i) i+i #check Π (i : Fin 10) i+1 #check Π (i : Fin 10 | i != 2) i+1 #check Π (i | myPred i) i+i -- We can easily create alternative syntax for any big operator. macro "Σ" idx:index "," F:term : term => `(Σ ($idx) $F) #check Σ 10 ≤ i < 20, i+1 -- Finally, we create a command for automating the generation of big operators. syntax "def_bigop" str term:max term:max : command -- Antiquotations can be nested as in `$$F`, which expands to `$F`, i.e. the value of -- `F` is inserted only in the second expansion, the expansion of the new macro `$head`. macro_rules | `(def_bigop $head $op $unit) => `(macro $head:strLit "(" idx:index ")" F:term : term => `(\big[$op/$unit]_($$idx) $$F)) def_bigop "SUM" Nat.add 0 #check SUM (i <- [0, 1, 2]) i+1 end Bigop
5eb0b8467c30469715d51a6eba1b78914f760162
6065973b1fa7bbacba932011c9e2f32bf7bdd6c1
/src/analysis/normed_space/basic.lean
cf5f423b3eb41ed9b411655a48cc6505231dd96b
[ "Apache-2.0" ]
permissive
khmacdonald/mathlib
90a0fa2222369fa69ed2fbfb841b74d2bdfd66cb
3669cb35c578441812ad30fd967d21a94b6f387e
refs/heads/master
1,675,863,801,090
1,609,761,876,000
1,609,761,876,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
55,852
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.instances.nnreal import topology.algebra.module import topology.metric_space.antilipschitz /-! # Normed spaces -/ variables {α : Type*} {β : Type*} {γ : Type*} {ι : Type*} noncomputable theory open filter metric open_locale topological_space big_operators nnreal /-- Auxiliary class, endowing a type `α` with a function `norm : α → ℝ`. This class is designed to be extended in more interesting classes specifying the properties of the norm. -/ class has_norm (α : Type*) := (norm : α → ℝ) export has_norm (norm) notation `∥`:1024 e:1 `∥`:1 := norm e /-- A normed group is an additive group endowed with a norm for which `dist x y = ∥x - y∥` defines a metric space structure. -/ class normed_group (α : Type*) extends has_norm α, add_comm_group α, metric_space α := (dist_eq : ∀ x y, dist x y = norm (x - y)) /-- Construct a normed group from a translation invariant distance -/ def normed_group.of_add_dist [has_norm α] [add_comm_group α] [metric_space α] (H1 : ∀ x:α, ∥x∥ = dist x 0) (H2 : ∀ x y z : α, dist x y ≤ dist (x + z) (y + z)) : normed_group α := { dist_eq := λ x y, begin rw H1, apply le_antisymm, { rw [sub_eq_add_neg, ← add_right_neg y], apply H2 }, { have := H2 (x-y) 0 y, rwa [sub_add_cancel, zero_add] at this } end } /-- Construct a normed group from a translation invariant distance -/ def normed_group.of_add_dist' [has_norm α] [add_comm_group α] [metric_space α] (H1 : ∀ x:α, ∥x∥ = dist x 0) (H2 : ∀ x y z : α, dist (x + z) (y + z) ≤ dist x y) : normed_group α := { dist_eq := λ x y, begin rw H1, apply le_antisymm, { have := H2 (x-y) 0 y, rwa [sub_add_cancel, zero_add] at this }, { rw [sub_eq_add_neg, ← add_right_neg y], apply H2 } end } /-- A normed group can be built from a norm that satisfies algebraic properties. This is formalised in this structure. -/ structure normed_group.core (α : Type*) [add_comm_group α] [has_norm α] : Prop := (norm_eq_zero_iff : ∀ x : α, ∥x∥ = 0 ↔ x = 0) (triangle : ∀ x y : α, ∥x + y∥ ≤ ∥x∥ + ∥y∥) (norm_neg : ∀ x : α, ∥-x∥ = ∥x∥) /-- Constructing a normed group from core properties of a norm, i.e., registering the distance and the metric space structure from the norm properties. -/ noncomputable def normed_group.of_core (α : Type*) [add_comm_group α] [has_norm α] (C : normed_group.core α) : normed_group α := { dist := λ x y, ∥x - y∥, dist_eq := assume x y, by refl, dist_self := assume x, (C.norm_eq_zero_iff (x - x)).mpr (show x - x = 0, by simp), eq_of_dist_eq_zero := assume x y h, show (x = y), from sub_eq_zero.mp $ (C.norm_eq_zero_iff (x - y)).mp h, dist_triangle := assume x y z, calc ∥x - z∥ = ∥x - y + (y - z)∥ : by rw sub_add_sub_cancel ... ≤ ∥x - y∥ + ∥y - z∥ : C.triangle _ _, dist_comm := assume x y, calc ∥x - y∥ = ∥ -(y - x)∥ : by simp ... = ∥y - x∥ : by { rw [C.norm_neg] } } section normed_group variables [normed_group α] [normed_group β] lemma dist_eq_norm (g h : α) : dist g h = ∥g - h∥ := normed_group.dist_eq _ _ @[simp] lemma dist_zero_right (g : α) : dist g 0 = ∥g∥ := by rw [dist_eq_norm, sub_zero] lemma tendsto_norm_cocompact_at_top [proper_space α] : tendsto norm (cocompact α) at_top := by simpa only [dist_zero_right] using tendsto_dist_right_cocompact_at_top (0:α) lemma norm_sub_rev (g h : α) : ∥g - h∥ = ∥h - g∥ := by simpa only [dist_eq_norm] using dist_comm g h @[simp] lemma norm_neg (g : α) : ∥-g∥ = ∥g∥ := by simpa using norm_sub_rev 0 g @[simp] lemma dist_add_left (g h₁ h₂ : α) : dist (g + h₁) (g + h₂) = dist h₁ h₂ := by simp [dist_eq_norm] @[simp] lemma dist_add_right (g₁ g₂ h : α) : dist (g₁ + h) (g₂ + h) = dist g₁ g₂ := by simp [dist_eq_norm] @[simp] lemma dist_neg_neg (g h : α) : dist (-g) (-h) = dist g h := by simp only [dist_eq_norm, neg_sub_neg, norm_sub_rev] @[simp] lemma dist_sub_left (g h₁ h₂ : α) : dist (g - h₁) (g - h₂) = dist h₁ h₂ := by simp only [sub_eq_add_neg, dist_add_left, dist_neg_neg] @[simp] lemma dist_sub_right (g₁ g₂ h : α) : dist (g₁ - h) (g₂ - h) = dist g₁ g₂ := by simpa only [sub_eq_add_neg] using dist_add_right _ _ _ /-- Triangle inequality for the norm. -/ lemma norm_add_le (g h : α) : ∥g + h∥ ≤ ∥g∥ + ∥h∥ := by simpa [dist_eq_norm] using dist_triangle g 0 (-h) lemma norm_add_le_of_le {g₁ g₂ : α} {n₁ n₂ : ℝ} (H₁ : ∥g₁∥ ≤ n₁) (H₂ : ∥g₂∥ ≤ n₂) : ∥g₁ + g₂∥ ≤ n₁ + n₂ := le_trans (norm_add_le g₁ g₂) (add_le_add H₁ H₂) lemma dist_add_add_le (g₁ g₂ h₁ h₂ : α) : dist (g₁ + g₂) (h₁ + h₂) ≤ dist g₁ h₁ + dist g₂ h₂ := by simpa only [dist_add_left, dist_add_right] using dist_triangle (g₁ + g₂) (h₁ + g₂) (h₁ + h₂) lemma dist_add_add_le_of_le {g₁ g₂ h₁ h₂ : α} {d₁ d₂ : ℝ} (H₁ : dist g₁ h₁ ≤ d₁) (H₂ : dist g₂ h₂ ≤ d₂) : dist (g₁ + g₂) (h₁ + h₂) ≤ d₁ + d₂ := le_trans (dist_add_add_le g₁ g₂ h₁ h₂) (add_le_add H₁ H₂) lemma dist_sub_sub_le (g₁ g₂ h₁ h₂ : α) : dist (g₁ - g₂) (h₁ - h₂) ≤ dist g₁ h₁ + dist g₂ h₂ := by simpa only [sub_eq_add_neg, dist_neg_neg] using dist_add_add_le g₁ (-g₂) h₁ (-h₂) lemma dist_sub_sub_le_of_le {g₁ g₂ h₁ h₂ : α} {d₁ d₂ : ℝ} (H₁ : dist g₁ h₁ ≤ d₁) (H₂ : dist g₂ h₂ ≤ d₂) : dist (g₁ - g₂) (h₁ - h₂) ≤ d₁ + d₂ := le_trans (dist_sub_sub_le g₁ g₂ h₁ h₂) (add_le_add H₁ H₂) lemma abs_dist_sub_le_dist_add_add (g₁ g₂ h₁ h₂ : α) : abs (dist g₁ h₁ - dist g₂ h₂) ≤ dist (g₁ + g₂) (h₁ + h₂) := by simpa only [dist_add_left, dist_add_right, dist_comm h₂] using abs_dist_sub_le (g₁ + g₂) (h₁ + h₂) (h₁ + g₂) @[simp] lemma norm_nonneg (g : α) : 0 ≤ ∥g∥ := by { rw[←dist_zero_right], exact dist_nonneg } @[simp] lemma norm_eq_zero {g : α} : ∥g∥ = 0 ↔ g = 0 := dist_zero_right g ▸ dist_eq_zero @[simp] lemma norm_zero : ∥(0:α)∥ = 0 := norm_eq_zero.2 rfl @[nontriviality] lemma norm_of_subsingleton [subsingleton α] (x : α) : ∥x∥ = 0 := by rw [subsingleton.elim x 0, norm_zero] lemma norm_sum_le {β} : ∀(s : finset β) (f : β → α), ∥∑ a in s, f a∥ ≤ ∑ a in s, ∥ f a ∥ := finset.le_sum_of_subadditive norm norm_zero norm_add_le lemma norm_sum_le_of_le {β} (s : finset β) {f : β → α} {n : β → ℝ} (h : ∀ b ∈ s, ∥f b∥ ≤ n b) : ∥∑ b in s, f b∥ ≤ ∑ b in s, n b := le_trans (norm_sum_le s f) (finset.sum_le_sum h) @[simp] lemma norm_pos_iff {g : α} : 0 < ∥ g ∥ ↔ g ≠ 0 := dist_zero_right g ▸ dist_pos @[simp] lemma norm_le_zero_iff {g : α} : ∥g∥ ≤ 0 ↔ g = 0 := by { rw[←dist_zero_right], exact dist_le_zero } lemma norm_sub_le (g h : α) : ∥g - h∥ ≤ ∥g∥ + ∥h∥ := by simpa [dist_eq_norm] using dist_triangle g 0 h lemma norm_sub_le_of_le {g₁ g₂ : α} {n₁ n₂ : ℝ} (H₁ : ∥g₁∥ ≤ n₁) (H₂ : ∥g₂∥ ≤ n₂) : ∥g₁ - g₂∥ ≤ n₁ + n₂ := le_trans (norm_sub_le g₁ g₂) (add_le_add H₁ H₂) lemma dist_le_norm_add_norm (g h : α) : dist g h ≤ ∥g∥ + ∥h∥ := by { rw dist_eq_norm, apply norm_sub_le } lemma abs_norm_sub_norm_le (g h : α) : abs(∥g∥ - ∥h∥) ≤ ∥g - h∥ := by simpa [dist_eq_norm] using abs_dist_sub_le g h 0 lemma norm_sub_norm_le (g h : α) : ∥g∥ - ∥h∥ ≤ ∥g - h∥ := le_trans (le_abs_self _) (abs_norm_sub_norm_le g h) lemma dist_norm_norm_le (g h : α) : dist ∥g∥ ∥h∥ ≤ ∥g - h∥ := abs_norm_sub_norm_le g h lemma eq_of_norm_sub_eq_zero {u v : α} (h : ∥u - v∥ = 0) : u = v := begin apply eq_of_dist_eq_zero, rwa dist_eq_norm end lemma norm_le_insert (u v : α) : ∥v∥ ≤ ∥u∥ + ∥u - v∥ := calc ∥v∥ = ∥u - (u - v)∥ : by abel ... ≤ ∥u∥ + ∥u - v∥ : norm_sub_le u _ lemma ball_0_eq (ε : ℝ) : ball (0:α) ε = {x | ∥x∥ < ε} := set.ext $ assume a, by simp lemma mem_ball_iff_norm {g h : α} {r : ℝ} : h ∈ ball g r ↔ ∥h - g∥ < r := by rw [mem_ball, dist_eq_norm] lemma mem_ball_iff_norm' {g h : α} {r : ℝ} : h ∈ ball g r ↔ ∥g - h∥ < r := by rw [mem_ball', dist_eq_norm] lemma mem_closed_ball_iff_norm {g h : α} {r : ℝ} : h ∈ closed_ball g r ↔ ∥h - g∥ ≤ r := by rw [mem_closed_ball, dist_eq_norm] lemma mem_closed_ball_iff_norm' {g h : α} {r : ℝ} : h ∈ closed_ball g r ↔ ∥g - h∥ ≤ r := by rw [mem_closed_ball', dist_eq_norm] lemma norm_le_of_mem_closed_ball {g h : α} {r : ℝ} (H : h ∈ closed_ball g r) : ∥h∥ ≤ ∥g∥ + r := calc ∥h∥ = ∥g + (h - g)∥ : by rw [add_sub_cancel'_right] ... ≤ ∥g∥ + ∥h - g∥ : norm_add_le _ _ ... ≤ ∥g∥ + r : by { apply add_le_add_left, rw ← dist_eq_norm, exact H } lemma norm_lt_of_mem_ball {g h : α} {r : ℝ} (H : h ∈ ball g r) : ∥h∥ < ∥g∥ + r := calc ∥h∥ = ∥g + (h - g)∥ : by rw [add_sub_cancel'_right] ... ≤ ∥g∥ + ∥h - g∥ : norm_add_le _ _ ... < ∥g∥ + r : by { apply add_lt_add_left, rw ← dist_eq_norm, exact H } @[simp] lemma mem_sphere_iff_norm (v w : α) (r : ℝ) : w ∈ sphere v r ↔ ∥w - v∥ = r := by simp [dist_eq_norm] @[simp] lemma mem_sphere_zero_iff_norm {w : α} {r : ℝ} : w ∈ sphere (0:α) r ↔ ∥w∥ = r := by simp [dist_eq_norm] @[simp] lemma norm_eq_of_mem_sphere {r : ℝ} (x : sphere (0:α) r) : ∥(x:α)∥ = r := mem_sphere_zero_iff_norm.mp x.2 lemma nonzero_of_mem_sphere {r : ℝ} (hr : 0 < r) (x : sphere (0:α) r) : (x:α) ≠ 0 := by rwa [← norm_pos_iff, norm_eq_of_mem_sphere] lemma nonzero_of_mem_unit_sphere (x : sphere (0:α) 1) : (x:α) ≠ 0 := by { apply nonzero_of_mem_sphere, norm_num } /-- We equip the sphere, in a normed group, with a formal operation of negation, namely the antipodal map. -/ instance {r : ℝ} : has_neg (sphere (0:α) r) := { neg := λ w, ⟨-↑w, by simp⟩ } @[simp] lemma coe_neg_sphere {r : ℝ} (v : sphere (0:α) r) : (((-v) : sphere _ _) : α) = - (v:α) := rfl theorem normed_group.tendsto_nhds_zero {f : γ → α} {l : filter γ} : tendsto f l (𝓝 0) ↔ ∀ ε > 0, ∀ᶠ x in l, ∥ f x ∥ < ε := metric.tendsto_nhds.trans $ by simp only [dist_zero_right] lemma normed_group.tendsto_nhds_nhds {f : α → β} {x : α} {y : β} : tendsto f (𝓝 x) (𝓝 y) ↔ ∀ ε > 0, ∃ δ > 0, ∀ x', ∥x' - x∥ < δ → ∥f x' - y∥ < ε := by simp_rw [metric.tendsto_nhds_nhds, dist_eq_norm] /-- A homomorphism `f` of normed groups is Lipschitz, if there exists a constant `C` such that for all `x`, one has `∥f x∥ ≤ C * ∥x∥`. The analogous condition for a linear map of normed spaces is in `normed_space.operator_norm`. -/ lemma add_monoid_hom.lipschitz_of_bound (f :α →+ β) (C : ℝ) (h : ∀x, ∥f x∥ ≤ C * ∥x∥) : lipschitz_with (nnreal.of_real C) f := lipschitz_with.of_dist_le' $ λ x y, by simpa only [dist_eq_norm, f.map_sub] using h (x - y) lemma lipschitz_on_with_iff_norm_sub_le {f : α → β} {C : ℝ≥0} {s : set α} : lipschitz_on_with C f s ↔ ∀ (x ∈ s) (y ∈ s), ∥f x - f y∥ ≤ C * ∥x - y∥ := by simp only [lipschitz_on_with_iff_dist_le_mul, dist_eq_norm] lemma lipschitz_on_with.norm_sub_le {f : α → β} {C : ℝ≥0} {s : set α} (h : lipschitz_on_with C f s) {x y : α} (x_in : x ∈ s) (y_in : y ∈ s) : ∥f x - f y∥ ≤ C * ∥x - y∥ := lipschitz_on_with_iff_norm_sub_le.mp h x x_in y y_in /-- A homomorphism `f` of normed groups is continuous, if there exists a constant `C` such that for all `x`, one has `∥f x∥ ≤ C * ∥x∥`. The analogous condition for a linear map of normed spaces is in `normed_space.operator_norm`. -/ lemma add_monoid_hom.continuous_of_bound (f :α →+ β) (C : ℝ) (h : ∀x, ∥f x∥ ≤ C * ∥x∥) : continuous f := (f.lipschitz_of_bound C h).continuous section nnnorm /-- Version of the norm taking values in nonnegative reals. -/ def nnnorm (a : α) : ℝ≥0 := ⟨norm a, norm_nonneg a⟩ @[simp, norm_cast] lemma coe_nnnorm (a : α) : (nnnorm a : ℝ) = norm a := rfl lemma nndist_eq_nnnorm (a b : α) : nndist a b = nnnorm (a - b) := nnreal.eq $ dist_eq_norm _ _ @[simp] lemma nnnorm_eq_zero {a : α} : nnnorm a = 0 ↔ a = 0 := by simp only [nnreal.eq_iff.symm, nnreal.coe_zero, coe_nnnorm, norm_eq_zero] @[simp] lemma nnnorm_zero : nnnorm (0 : α) = 0 := nnreal.eq norm_zero lemma nnnorm_add_le (g h : α) : nnnorm (g + h) ≤ nnnorm g + nnnorm h := nnreal.coe_le_coe.2 $ norm_add_le g h @[simp] lemma nnnorm_neg (g : α) : nnnorm (-g) = nnnorm g := nnreal.eq $ norm_neg g lemma nndist_nnnorm_nnnorm_le (g h : α) : nndist (nnnorm g) (nnnorm h) ≤ nnnorm (g - h) := nnreal.coe_le_coe.2 $ dist_norm_norm_le g h lemma of_real_norm_eq_coe_nnnorm (x : β) : ennreal.of_real ∥x∥ = (nnnorm x : ennreal) := ennreal.of_real_eq_coe_nnreal _ lemma edist_eq_coe_nnnorm_sub (x y : β) : edist x y = (nnnorm (x - y) : ennreal) := by rw [edist_dist, dist_eq_norm, of_real_norm_eq_coe_nnnorm] lemma edist_eq_coe_nnnorm (x : β) : edist x 0 = (nnnorm x : ennreal) := by rw [edist_eq_coe_nnnorm_sub, _root_.sub_zero] lemma mem_emetric_ball_0_iff {x : β} {r : ennreal} : x ∈ emetric.ball (0 : β) r ↔ ↑(nnnorm x) < r := by rw [emetric.mem_ball, edist_eq_coe_nnnorm] lemma nndist_add_add_le (g₁ g₂ h₁ h₂ : α) : nndist (g₁ + g₂) (h₁ + h₂) ≤ nndist g₁ h₁ + nndist g₂ h₂ := nnreal.coe_le_coe.2 $ dist_add_add_le g₁ g₂ h₁ h₂ lemma edist_add_add_le (g₁ g₂ h₁ h₂ : α) : edist (g₁ + g₂) (h₁ + h₂) ≤ edist g₁ h₁ + edist g₂ h₂ := by { simp only [edist_nndist], norm_cast, apply nndist_add_add_le } lemma nnnorm_sum_le {β} : ∀(s : finset β) (f : β → α), nnnorm (∑ a in s, f a) ≤ ∑ a in s, nnnorm (f a) := finset.le_sum_of_subadditive nnnorm nnnorm_zero nnnorm_add_le end nnnorm lemma lipschitz_with.neg {α : Type*} [emetric_space α] {K : ℝ≥0} {f : α → β} (hf : lipschitz_with K f) : lipschitz_with K (λ x, -f x) := λ x y, by simpa only [edist_dist, dist_neg_neg] using hf x y lemma lipschitz_with.add {α : Type*} [emetric_space α] {Kf : ℝ≥0} {f : α → β} (hf : lipschitz_with Kf f) {Kg : ℝ≥0} {g : α → β} (hg : lipschitz_with Kg g) : lipschitz_with (Kf + Kg) (λ x, f x + g x) := λ x y, calc edist (f x + g x) (f y + g y) ≤ edist (f x) (f y) + edist (g x) (g y) : edist_add_add_le _ _ _ _ ... ≤ Kf * edist x y + Kg * edist x y : add_le_add (hf x y) (hg x y) ... = (Kf + Kg) * edist x y : (add_mul _ _ _).symm lemma lipschitz_with.sub {α : Type*} [emetric_space α] {Kf : ℝ≥0} {f : α → β} (hf : lipschitz_with Kf f) {Kg : ℝ≥0} {g : α → β} (hg : lipschitz_with Kg g) : lipschitz_with (Kf + Kg) (λ x, f x - g x) := by simpa only [sub_eq_add_neg] using hf.add hg.neg lemma antilipschitz_with.add_lipschitz_with {α : Type*} [metric_space α] {Kf : ℝ≥0} {f : α → β} (hf : antilipschitz_with Kf f) {Kg : ℝ≥0} {g : α → β} (hg : lipschitz_with Kg g) (hK : Kg < Kf⁻¹) : antilipschitz_with (Kf⁻¹ - Kg)⁻¹ (λ x, f x + g x) := begin refine antilipschitz_with.of_le_mul_dist (λ x y, _), rw [nnreal.coe_inv, ← div_eq_inv_mul], rw le_div_iff (nnreal.coe_pos.2 $ nnreal.sub_pos.2 hK), rw [mul_comm, nnreal.coe_sub (le_of_lt hK), sub_mul], calc ↑Kf⁻¹ * dist x y - Kg * dist x y ≤ dist (f x) (f y) - dist (g x) (g y) : sub_le_sub (hf.mul_le_dist x y) (hg.dist_le_mul x y) ... ≤ _ : le_trans (le_abs_self _) (abs_dist_sub_le_dist_add_add _ _ _ _) end /-- A submodule of a normed group is also a normed group, with the restriction of the norm. As all instances can be inferred from the submodule `s`, they are put as implicit instead of typeclasses. -/ instance submodule.normed_group {𝕜 : Type*} {_ : ring 𝕜} {E : Type*} [normed_group E] {_ : module 𝕜 E} (s : submodule 𝕜 E) : normed_group s := { norm := λx, norm (x : E), dist_eq := λx y, dist_eq_norm (x : E) (y : E) } /-- normed group instance on the product of two normed groups, using the sup norm. -/ instance prod.normed_group : normed_group (α × β) := { norm := λx, max ∥x.1∥ ∥x.2∥, dist_eq := assume (x y : α × β), show max (dist x.1 y.1) (dist x.2 y.2) = (max ∥(x - y).1∥ ∥(x - y).2∥), by simp [dist_eq_norm] } lemma prod.norm_def (x : α × β) : ∥x∥ = (max ∥x.1∥ ∥x.2∥) := rfl lemma prod.nnnorm_def (x : α × β) : nnnorm x = max (nnnorm x.1) (nnnorm x.2) := by { have := x.norm_def, simp only [← coe_nnnorm] at this, exact_mod_cast this } lemma norm_fst_le (x : α × β) : ∥x.1∥ ≤ ∥x∥ := le_max_left _ _ lemma norm_snd_le (x : α × β) : ∥x.2∥ ≤ ∥x∥ := le_max_right _ _ lemma norm_prod_le_iff {x : α × β} {r : ℝ} : ∥x∥ ≤ r ↔ ∥x.1∥ ≤ r ∧ ∥x.2∥ ≤ r := max_le_iff /-- normed group instance on the product of finitely many normed groups, using the sup norm. -/ instance pi.normed_group {π : ι → Type*} [fintype ι] [∀i, normed_group (π i)] : normed_group (Πi, π i) := { norm := λf, ((finset.sup finset.univ (λ b, nnnorm (f b)) : ℝ≥0) : ℝ), dist_eq := assume x y, congr_arg (coe : ℝ≥0 → ℝ) $ congr_arg (finset.sup finset.univ) $ funext $ assume a, show nndist (x a) (y a) = nnnorm (x a - y a), from nndist_eq_nnnorm _ _ } /-- The norm of an element in a product space is `≤ r` if and only if the norm of each component is. -/ lemma pi_norm_le_iff {π : ι → Type*} [fintype ι] [∀i, normed_group (π i)] {r : ℝ} (hr : 0 ≤ r) {x : Πi, π i} : ∥x∥ ≤ r ↔ ∀i, ∥x i∥ ≤ r := by { simp only [(dist_zero_right _).symm, dist_pi_le_iff hr], refl } lemma norm_le_pi_norm {π : ι → Type*} [fintype ι] [∀i, normed_group (π i)] (x : Πi, π i) (i : ι) : ∥x i∥ ≤ ∥x∥ := (pi_norm_le_iff (norm_nonneg x)).1 (le_refl _) i lemma tendsto_iff_norm_tendsto_zero {f : ι → β} {a : filter ι} {b : β} : tendsto f a (𝓝 b) ↔ tendsto (λ e, ∥f e - b∥) a (𝓝 0) := by { convert tendsto_iff_dist_tendsto_zero, simp [dist_eq_norm] } lemma tendsto_zero_iff_norm_tendsto_zero {f : γ → β} {a : filter γ} : tendsto f a (𝓝 0) ↔ tendsto (λ e, ∥f e∥) a (𝓝 0) := by simp [tendsto_iff_norm_tendsto_zero] /-- Special case of the sandwich theorem: if the norm of `f` is eventually bounded by a real function `g` which tends to `0`, then `f` tends to `0`. In this pair of lemmas (`squeeze_zero_norm'` and `squeeze_zero_norm`), following a convention of similar lemmas in `topology.metric_space.basic` and `topology.algebra.ordered`, the `'` version is phrased using "eventually" and the non-`'` version is phrased absolutely. -/ lemma squeeze_zero_norm' {f : γ → α} {g : γ → ℝ} {t₀ : filter γ} (h : ∀ᶠ n in t₀, ∥f n∥ ≤ g n) (h' : tendsto g t₀ (𝓝 0)) : tendsto f t₀ (𝓝 0) := tendsto_zero_iff_norm_tendsto_zero.mpr (squeeze_zero' (eventually_of_forall (λ n, norm_nonneg _)) h h') /-- Special case of the sandwich theorem: if the norm of `f` is bounded by a real function `g` which tends to `0`, then `f` tends to `0`. -/ lemma squeeze_zero_norm {f : γ → α} {g : γ → ℝ} {t₀ : filter γ} (h : ∀ (n:γ), ∥f n∥ ≤ g n) (h' : tendsto g t₀ (𝓝 0)) : tendsto f t₀ (𝓝 0) := squeeze_zero_norm' (eventually_of_forall h) h' lemma tendsto_norm_sub_self (x : α) : tendsto (λ g : α, ∥g - x∥) (𝓝 x) (𝓝 0) := by simpa [dist_eq_norm] using tendsto_id.dist (tendsto_const_nhds : tendsto (λ g, (x:α)) (𝓝 x) _) lemma tendsto_norm {x : α} : tendsto (λg : α, ∥g∥) (𝓝 x) (𝓝 ∥x∥) := by simpa using tendsto_id.dist (tendsto_const_nhds : tendsto (λ g, (0:α)) _ _) lemma tendsto_norm_zero : tendsto (λg : α, ∥g∥) (𝓝 0) (𝓝 0) := by simpa using tendsto_norm_sub_self (0:α) lemma continuous_norm : continuous (λg:α, ∥g∥) := by simpa using continuous_id.dist (continuous_const : continuous (λ g, (0:α))) lemma continuous_nnnorm : continuous (nnnorm : α → ℝ≥0) := continuous_subtype_mk _ continuous_norm lemma tendsto_norm_nhds_within_zero : tendsto (norm : α → ℝ) (𝓝[{0}ᶜ] 0) (𝓝[set.Ioi 0] 0) := (continuous_norm.tendsto' (0 : α) 0 norm_zero).inf $ tendsto_principal_principal.2 $ λ x, norm_pos_iff.2 section variables {l : filter γ} {f : γ → α} {a : α} lemma filter.tendsto.norm {a : α} (h : tendsto f l (𝓝 a)) : tendsto (λ x, ∥f x∥) l (𝓝 ∥a∥) := tendsto_norm.comp h lemma filter.tendsto.nnnorm (h : tendsto f l (𝓝 a)) : tendsto (λ x, nnnorm (f x)) l (𝓝 (nnnorm a)) := tendsto.comp continuous_nnnorm.continuous_at h end section variables [topological_space γ] {f : γ → α} {s : set γ} {a : γ} {b : α} lemma continuous.norm (h : continuous f) : continuous (λ x, ∥f x∥) := continuous_norm.comp h lemma continuous.nnnorm (h : continuous f) : continuous (λ x, nnnorm (f x)) := continuous_nnnorm.comp h lemma continuous_at.norm (h : continuous_at f a) : continuous_at (λ x, ∥f x∥) a := h.norm lemma continuous_at.nnnorm (h : continuous_at f a) : continuous_at (λ x, nnnorm (f x)) a := h.nnnorm lemma continuous_within_at.norm (h : continuous_within_at f s a) : continuous_within_at (λ x, ∥f x∥) s a := h.norm lemma continuous_within_at.nnnorm (h : continuous_within_at f s a) : continuous_within_at (λ x, nnnorm (f x)) s a := h.nnnorm lemma continuous_on.norm (h : continuous_on f s) : continuous_on (λ x, ∥f x∥) s := λ x hx, (h x hx).norm lemma continuous_on.nnnorm (h : continuous_on f s) : continuous_on (λ x, nnnorm (f x)) s := λ x hx, (h x hx).nnnorm end /-- If `∥y∥→∞`, then we can assume `y≠x` for any fixed `x`. -/ lemma eventually_ne_of_tendsto_norm_at_top {l : filter γ} {f : γ → α} (h : tendsto (λ y, ∥f y∥) l at_top) (x : α) : ∀ᶠ y in l, f y ≠ x := begin have : ∀ᶠ y in l, 1 + ∥x∥ ≤ ∥f y∥ := h (mem_at_top (1 + ∥x∥)), refine this.mono (λ y hy hxy, _), subst x, exact not_le_of_lt zero_lt_one (add_le_iff_nonpos_left.1 hy) end /-- A normed group is a uniform additive group, i.e., addition and subtraction are uniformly continuous. -/ @[priority 100] -- see Note [lower instance priority] instance normed_uniform_group : uniform_add_group α := ⟨(lipschitz_with.prod_fst.sub lipschitz_with.prod_snd).uniform_continuous⟩ @[priority 100] -- see Note [lower instance priority] instance normed_top_monoid : has_continuous_add α := by apply_instance -- short-circuit type class inference @[priority 100] -- see Note [lower instance priority] instance normed_top_group : topological_add_group α := by apply_instance -- short-circuit type class inference end normed_group section normed_ring /-- A normed ring is a ring endowed with a norm which satisfies the inequality `∥x y∥ ≤ ∥x∥ ∥y∥`. -/ class normed_ring (α : Type*) extends has_norm α, ring α, metric_space α := (dist_eq : ∀ x y, dist x y = norm (x - y)) (norm_mul : ∀ a b, norm (a * b) ≤ norm a * norm b) /-- A normed commutative ring is a commutative ring endowed with a norm which satisfies the inequality `∥x y∥ ≤ ∥x∥ ∥y∥`. -/ class normed_comm_ring (α : Type*) extends normed_ring α := (mul_comm : ∀ x y : α, x * y = y * x) /-- A mixin class with the axiom `∥1∥ = 1`. Many `normed_ring`s and all `normed_field`s satisfy this axiom. -/ class norm_one_class (α : Type*) [has_norm α] [has_one α] : Prop := (norm_one : ∥(1:α)∥ = 1) export norm_one_class (norm_one) attribute [simp] norm_one @[simp] lemma nnnorm_one [normed_group α] [has_one α] [norm_one_class α] : nnnorm (1:α) = 1 := nnreal.eq norm_one @[priority 100] -- see Note [lower instance priority] instance normed_comm_ring.to_comm_ring [β : normed_comm_ring α] : comm_ring α := { ..β } @[priority 100] -- see Note [lower instance priority] instance normed_ring.to_normed_group [β : normed_ring α] : normed_group α := { ..β } instance prod.norm_one_class [normed_group α] [has_one α] [norm_one_class α] [normed_group β] [has_one β] [norm_one_class β] : norm_one_class (α × β) := ⟨by simp [prod.norm_def]⟩ variables [normed_ring α] lemma norm_mul_le (a b : α) : (∥a*b∥) ≤ (∥a∥) * (∥b∥) := normed_ring.norm_mul _ _ lemma list.norm_prod_le' : ∀ {l : list α}, l ≠ [] → ∥l.prod∥ ≤ (l.map norm).prod | [] h := (h rfl).elim | [a] _ := by simp | (a :: b :: l) _ := begin rw [list.map_cons, list.prod_cons, @list.prod_cons _ _ _ ∥a∥], refine le_trans (norm_mul_le _ _) (mul_le_mul_of_nonneg_left _ (norm_nonneg _)), exact list.norm_prod_le' (list.cons_ne_nil b l) end lemma list.norm_prod_le [norm_one_class α] : ∀ l : list α, ∥l.prod∥ ≤ (l.map norm).prod | [] := by simp | (a::l) := list.norm_prod_le' (list.cons_ne_nil a l) lemma finset.norm_prod_le' {α : Type*} [normed_comm_ring α] (s : finset ι) (hs : s.nonempty) (f : ι → α) : ∥∏ i in s, f i∥ ≤ ∏ i in s, ∥f i∥ := begin rcases s with ⟨⟨l⟩, hl⟩, have : l.map f ≠ [], by simpa using hs, simpa using list.norm_prod_le' this end lemma finset.norm_prod_le {α : Type*} [normed_comm_ring α] [norm_one_class α] (s : finset ι) (f : ι → α) : ∥∏ i in s, f i∥ ≤ ∏ i in s, ∥f i∥ := begin rcases s with ⟨⟨l⟩, hl⟩, simpa using (l.map f).norm_prod_le end /-- If `α` is a normed ring, then `∥a^n∥≤ ∥a∥^n` for `n > 0`. See also `norm_pow_le`. -/ lemma norm_pow_le' (a : α) : ∀ {n : ℕ}, 0 < n → ∥a^n∥ ≤ ∥a∥^n | 1 h := by simp | (n+2) h := le_trans (norm_mul_le a (a^(n+1))) (mul_le_mul (le_refl _) (norm_pow_le' (nat.succ_pos _)) (norm_nonneg _) (norm_nonneg _)) /-- If `α` is a normed ring with `∥1∥=1`, then `∥a^n∥≤ ∥a∥^n`. See also `norm_pow_le'`. -/ lemma norm_pow_le [norm_one_class α] (a : α) : ∀ (n : ℕ), ∥a^n∥ ≤ ∥a∥^n | 0 := by simp | (n+1) := norm_pow_le' a n.zero_lt_succ lemma eventually_norm_pow_le (a : α) : ∀ᶠ (n:ℕ) in at_top, ∥a ^ n∥ ≤ ∥a∥ ^ n := eventually_at_top.mpr ⟨1, λ b h, norm_pow_le' a (nat.succ_le_iff.mp h)⟩ lemma units.norm_pos [nontrivial α] (x : units α) : 0 < ∥(x:α)∥ := norm_pos_iff.mpr (units.ne_zero x) /-- In a normed ring, the left-multiplication `add_monoid_hom` is bounded. -/ lemma mul_left_bound (x : α) : ∀ (y:α), ∥add_monoid_hom.mul_left x y∥ ≤ ∥x∥ * ∥y∥ := norm_mul_le x /-- In a normed ring, the right-multiplication `add_monoid_hom` is bounded. -/ lemma mul_right_bound (x : α) : ∀ (y:α), ∥add_monoid_hom.mul_right x y∥ ≤ ∥x∥ * ∥y∥ := λ y, by {rw mul_comm, convert norm_mul_le y x} /-- Normed ring structure on the product of two normed rings, using the sup norm. -/ instance prod.normed_ring [normed_ring β] : normed_ring (α × β) := { norm_mul := assume x y, calc ∥x * y∥ = ∥(x.1*y.1, x.2*y.2)∥ : rfl ... = (max ∥x.1*y.1∥ ∥x.2*y.2∥) : rfl ... ≤ (max (∥x.1∥*∥y.1∥) (∥x.2∥*∥y.2∥)) : max_le_max (norm_mul_le (x.1) (y.1)) (norm_mul_le (x.2) (y.2)) ... = (max (∥x.1∥*∥y.1∥) (∥y.2∥*∥x.2∥)) : by simp[mul_comm] ... ≤ (max (∥x.1∥) (∥x.2∥)) * (max (∥y.2∥) (∥y.1∥)) : by { apply max_mul_mul_le_max_mul_max; simp [norm_nonneg] } ... = (max (∥x.1∥) (∥x.2∥)) * (max (∥y.1∥) (∥y.2∥)) : by simp[max_comm] ... = (∥x∥*∥y∥) : rfl, ..prod.normed_group } end normed_ring @[priority 100] -- see Note [lower instance priority] instance normed_ring_top_monoid [normed_ring α] : has_continuous_mul α := ⟨ continuous_iff_continuous_at.2 $ λ x, tendsto_iff_norm_tendsto_zero.2 $ begin have : ∀ e : α × α, ∥e.1 * e.2 - x.1 * x.2∥ ≤ ∥e.1∥ * ∥e.2 - x.2∥ + ∥e.1 - x.1∥ * ∥x.2∥, { intro e, calc ∥e.1 * e.2 - x.1 * x.2∥ ≤ ∥e.1 * (e.2 - x.2) + (e.1 - x.1) * x.2∥ : by rw [mul_sub, sub_mul, sub_add_sub_cancel] ... ≤ ∥e.1∥ * ∥e.2 - x.2∥ + ∥e.1 - x.1∥ * ∥x.2∥ : norm_add_le_of_le (norm_mul_le _ _) (norm_mul_le _ _) }, refine squeeze_zero (λ e, norm_nonneg _) this _, convert ((continuous_fst.tendsto x).norm.mul ((continuous_snd.tendsto x).sub tendsto_const_nhds).norm).add (((continuous_fst.tendsto x).sub tendsto_const_nhds).norm.mul _), show tendsto _ _ _, from tendsto_const_nhds, simp end ⟩ /-- A normed ring is a topological ring. -/ @[priority 100] -- see Note [lower instance priority] instance normed_top_ring [normed_ring α] : topological_ring α := ⟨ continuous_iff_continuous_at.2 $ λ x, tendsto_iff_norm_tendsto_zero.2 $ have ∀ e : α, -e - -x = -(e - x), by intro; simp, by simp only [this, norm_neg]; apply tendsto_norm_sub_self ⟩ /-- A normed field is a field with a norm satisfying ∥x y∥ = ∥x∥ ∥y∥. -/ class normed_field (α : Type*) extends has_norm α, field α, metric_space α := (dist_eq : ∀ x y, dist x y = norm (x - y)) (norm_mul' : ∀ a b, norm (a * b) = norm a * norm b) /-- A nondiscrete normed field is a normed field in which there is an element of norm different from `0` and `1`. This makes it possible to bring any element arbitrarily close to `0` by multiplication by the powers of any element, and thus to relate algebra and topology. -/ class nondiscrete_normed_field (α : Type*) extends normed_field α := (non_trivial : ∃x:α, 1<∥x∥) namespace normed_field section normed_field variables [normed_field α] @[simp] lemma norm_mul (a b : α) : ∥a * b∥ = ∥a∥ * ∥b∥ := normed_field.norm_mul' a b @[priority 100] -- see Note [lower instance priority] instance to_normed_comm_ring : normed_comm_ring α := { norm_mul := λ a b, (norm_mul a b).le, ..‹normed_field α› } @[priority 900] instance to_norm_one_class : norm_one_class α := ⟨mul_left_cancel' (mt norm_eq_zero.1 (@one_ne_zero α _ _)) $ by rw [← norm_mul, mul_one, mul_one]⟩ @[simp] lemma nnnorm_mul (a b : α) : nnnorm (a * b) = nnnorm a * nnnorm b := nnreal.eq $ norm_mul a b /-- `norm` as a `monoid_hom`. -/ @[simps] def norm_hom : monoid_with_zero_hom α ℝ := ⟨norm, norm_zero, norm_one, norm_mul⟩ /-- `nnnorm` as a `monoid_hom`. -/ @[simps] def nnnorm_hom : monoid_with_zero_hom α ℝ≥0 := ⟨nnnorm, nnnorm_zero, nnnorm_one, nnnorm_mul⟩ @[simp] lemma norm_pow (a : α) : ∀ (n : ℕ), ∥a ^ n∥ = ∥a∥ ^ n := norm_hom.to_monoid_hom.map_pow a @[simp] lemma nnnorm_pow (a : α) (n : ℕ) : nnnorm (a ^ n) = nnnorm a ^ n := nnnorm_hom.to_monoid_hom.map_pow a n @[simp] lemma norm_prod (s : finset β) (f : β → α) : ∥∏ b in s, f b∥ = ∏ b in s, ∥f b∥ := (norm_hom.to_monoid_hom : α →* ℝ).map_prod f s @[simp] lemma nnnorm_prod (s : finset β) (f : β → α) : nnnorm (∏ b in s, f b) = ∏ b in s, nnnorm (f b) := (nnnorm_hom.to_monoid_hom : α →* ℝ≥0).map_prod f s @[simp] lemma norm_div (a b : α) : ∥a / b∥ = ∥a∥ / ∥b∥ := (norm_hom : monoid_with_zero_hom α ℝ).map_div a b @[simp] lemma nnnorm_div (a b : α) : nnnorm (a / b) = nnnorm a / nnnorm b := (nnnorm_hom : monoid_with_zero_hom α ℝ≥0).map_div a b @[simp] lemma norm_inv (a : α) : ∥a⁻¹∥ = ∥a∥⁻¹ := (norm_hom : monoid_with_zero_hom α ℝ).map_inv' a @[simp] lemma nnnorm_inv (a : α) : nnnorm (a⁻¹) = (nnnorm a)⁻¹ := nnreal.eq $ by simp @[simp] lemma norm_fpow : ∀ (a : α) (n : ℤ), ∥a^n∥ = ∥a∥^n := (norm_hom : monoid_with_zero_hom α ℝ).map_fpow @[simp] lemma nnnorm_fpow : ∀ (a : α) (n : ℤ), nnnorm (a^n) = (nnnorm a)^n := (nnnorm_hom : monoid_with_zero_hom α ℝ≥0).map_fpow @[priority 100] -- see Note [lower instance priority] instance : has_continuous_inv' α := begin refine ⟨λ r r0, tendsto_iff_norm_tendsto_zero.2 _⟩, have r0' : 0 < ∥r∥ := norm_pos_iff.2 r0, rcases exists_between r0' with ⟨ε, ε0, εr⟩, have : ∀ᶠ e in 𝓝 r, ∥e⁻¹ - r⁻¹∥ ≤ ∥r - e∥ / ∥r∥ / ε, { filter_upwards [(is_open_lt continuous_const continuous_norm).eventually_mem εr], intros e he, have e0 : e ≠ 0 := norm_pos_iff.1 (ε0.trans he), calc ∥e⁻¹ - r⁻¹∥ = ∥r - e∥ / ∥r∥ / ∥e∥ : by field_simp [mul_comm] ... ≤ ∥r - e∥ / ∥r∥ / ε : div_le_div_of_le_left (div_nonneg (norm_nonneg _) (norm_nonneg _)) ε0 he.le }, refine squeeze_zero' (eventually_of_forall $ λ _, norm_nonneg _) this _, refine (continuous_const.sub continuous_id).norm.div_const.div_const.tendsto' _ _ _, simp end end normed_field variables (α) [nondiscrete_normed_field α] lemma exists_one_lt_norm : ∃x : α, 1 < ∥x∥ := ‹nondiscrete_normed_field α›.non_trivial lemma exists_norm_lt_one : ∃x : α, 0 < ∥x∥ ∧ ∥x∥ < 1 := begin rcases exists_one_lt_norm α with ⟨y, hy⟩, refine ⟨y⁻¹, _, _⟩, { simp only [inv_eq_zero, ne.def, norm_pos_iff], rintro rfl, rw norm_zero at hy, exact lt_asymm zero_lt_one hy }, { simp [inv_lt_one hy] } end lemma exists_lt_norm (r : ℝ) : ∃ x : α, r < ∥x∥ := let ⟨w, hw⟩ := exists_one_lt_norm α in let ⟨n, hn⟩ := pow_unbounded_of_one_lt r hw in ⟨w^n, by rwa norm_pow⟩ lemma exists_norm_lt {r : ℝ} (hr : 0 < r) : ∃ x : α, 0 < ∥x∥ ∧ ∥x∥ < r := let ⟨w, hw⟩ := exists_one_lt_norm α in let ⟨n, hle, hlt⟩ := exists_int_pow_near' hr hw in ⟨w^n, by { rw norm_fpow; exact fpow_pos_of_pos (lt_trans zero_lt_one hw) _}, by rwa norm_fpow⟩ variable {α} @[instance] lemma punctured_nhds_ne_bot (x : α) : ne_bot (𝓝[{x}ᶜ] x) := begin rw [← mem_closure_iff_nhds_within_ne_bot, metric.mem_closure_iff], rintros ε ε0, rcases normed_field.exists_norm_lt α ε0 with ⟨b, hb0, hbε⟩, refine ⟨x + b, mt (set.mem_singleton_iff.trans add_right_eq_self).1 $ norm_pos_iff.1 hb0, _⟩, rwa [dist_comm, dist_eq_norm, add_sub_cancel'], end @[instance] lemma nhds_within_is_unit_ne_bot : ne_bot (𝓝[{x : α | is_unit x}] 0) := by simpa only [is_unit_iff_ne_zero] using punctured_nhds_ne_bot (0:α) end normed_field instance : normed_field ℝ := { norm := λ x, abs x, dist_eq := assume x y, rfl, norm_mul' := abs_mul } instance : nondiscrete_normed_field ℝ := { non_trivial := ⟨2, by { unfold norm, rw abs_of_nonneg; norm_num }⟩ } namespace real lemma norm_eq_abs (r : ℝ) : ∥r∥ = abs r := rfl lemma norm_of_nonneg {x : ℝ} (hx : 0 ≤ x) : ∥x∥ = x := abs_of_nonneg hx @[simp] lemma norm_coe_nat (n : ℕ) : ∥(n : ℝ)∥ = n := abs_of_nonneg n.cast_nonneg @[simp] lemma nnnorm_coe_nat (n : ℕ) : nnnorm (n : ℝ) = n := nnreal.eq $ by simp @[simp] lemma norm_two : ∥(2:ℝ)∥ = 2 := abs_of_pos (@zero_lt_two ℝ _ _) @[simp] lemma nnnorm_two : nnnorm (2:ℝ) = 2 := nnreal.eq $ by simp open_locale nnreal @[simp] lemma nnreal.norm_eq (x : ℝ≥0) : ∥(x : ℝ)∥ = x := by rw [real.norm_eq_abs, x.abs_eq] lemma nnnorm_coe_eq_self {x : ℝ≥0} : nnnorm (x : ℝ) = x := by { ext, exact norm_of_nonneg (zero_le x) } lemma nnnorm_of_nonneg {x : ℝ} (hx : 0 ≤ x) : nnnorm x = ⟨x, hx⟩ := @nnnorm_coe_eq_self ⟨x, hx⟩ lemma ennnorm_eq_of_real {x : ℝ} (hx : 0 ≤ x) : (nnnorm x : ennreal) = ennreal.of_real x := by { rw [← of_real_norm_eq_coe_nnnorm, norm_of_nonneg hx] } end real @[simp] lemma norm_norm [normed_group α] (x : α) : ∥∥x∥∥ = ∥x∥ := by rw [real.norm_of_nonneg (norm_nonneg _)] @[simp] lemma nnnorm_norm [normed_group α] (a : α) : nnnorm ∥a∥ = nnnorm a := by simp only [nnnorm, norm_norm] instance : normed_comm_ring ℤ := { norm := λ n, ∥(n : ℝ)∥, norm_mul := λ m n, le_of_eq $ by simp only [norm, int.cast_mul, abs_mul], dist_eq := λ m n, by simp only [int.dist_eq, norm, int.cast_sub], mul_comm := mul_comm } @[norm_cast] lemma int.norm_cast_real (m : ℤ) : ∥(m : ℝ)∥ = ∥m∥ := rfl instance : norm_one_class ℤ := ⟨by simp [← int.norm_cast_real]⟩ instance : normed_field ℚ := { norm := λ r, ∥(r : ℝ)∥, norm_mul' := λ r₁ r₂, by simp only [norm, rat.cast_mul, abs_mul], dist_eq := λ r₁ r₂, by simp only [rat.dist_eq, norm, rat.cast_sub] } instance : nondiscrete_normed_field ℚ := { non_trivial := ⟨2, by { unfold norm, rw abs_of_nonneg; norm_num }⟩ } @[norm_cast, simp] lemma rat.norm_cast_real (r : ℚ) : ∥(r : ℝ)∥ = ∥r∥ := rfl @[norm_cast, simp] lemma int.norm_cast_rat (m : ℤ) : ∥(m : ℚ)∥ = ∥m∥ := by rw [← rat.norm_cast_real, ← int.norm_cast_real]; congr' 1; norm_cast section normed_space section prio set_option extends_priority 920 -- Here, we set a rather high priority for the instance `[normed_space α β] : semimodule α β` -- to take precedence over `semiring.to_semimodule` as this leads to instance paths with better -- unification properties. -- see Note[vector space definition] for why we extend `semimodule`. /-- A normed space over a normed field is a vector space endowed with a norm which satisfies the equality `∥c • x∥ = ∥c∥ ∥x∥`. We require only `∥c • x∥ ≤ ∥c∥ ∥x∥` in the definition, then prove `∥c • x∥ = ∥c∥ ∥x∥` in `norm_smul`. -/ class normed_space (α : Type*) (β : Type*) [normed_field α] [normed_group β] extends semimodule α β := (norm_smul_le : ∀ (a:α) (b:β), ∥a • b∥ ≤ ∥a∥ * ∥b∥) end prio variables [normed_field α] [normed_group β] instance normed_field.to_normed_space : normed_space α α := { norm_smul_le := λ a b, le_of_eq (normed_field.norm_mul a b) } lemma norm_smul [normed_space α β] (s : α) (x : β) : ∥s • x∥ = ∥s∥ * ∥x∥ := begin classical, by_cases h : s = 0, { simp [h] }, { refine le_antisymm (normed_space.norm_smul_le s x) _, calc ∥s∥ * ∥x∥ = ∥s∥ * ∥s⁻¹ • s • x∥ : by rw [inv_smul_smul' h] ... ≤ ∥s∥ * (∥s⁻¹∥ * ∥s • x∥) : _ ... = ∥s • x∥ : _, exact mul_le_mul_of_nonneg_left (normed_space.norm_smul_le _ _) (norm_nonneg _), rw [normed_field.norm_inv, ← mul_assoc, mul_inv_cancel, one_mul], rwa [ne.def, norm_eq_zero] } end @[simp] lemma abs_norm_eq_norm (z : β) : abs ∥z∥ = ∥z∥ := (abs_eq (norm_nonneg z)).mpr (or.inl rfl) lemma dist_smul [normed_space α β] (s : α) (x y : β) : dist (s • x) (s • y) = ∥s∥ * dist x y := by simp only [dist_eq_norm, (norm_smul _ _).symm, smul_sub] lemma nnnorm_smul [normed_space α β] (s : α) (x : β) : nnnorm (s • x) = nnnorm s * nnnorm x := nnreal.eq $ norm_smul s x lemma nndist_smul [normed_space α β] (s : α) (x y : β) : nndist (s • x) (s • y) = nnnorm s * nndist x y := nnreal.eq $ dist_smul s x y lemma norm_smul_of_nonneg [normed_space ℝ β] {t : ℝ} (ht : 0 ≤ t) (x : β) : ∥t • x∥ = t * ∥x∥ := by rw [norm_smul, real.norm_eq_abs, abs_of_nonneg ht] variables {E : Type*} {F : Type*} [normed_group E] [normed_space α E] [normed_group F] [normed_space α F] @[priority 100] -- see Note [lower instance priority] instance normed_space.topological_vector_space : topological_vector_space α E := begin refine { continuous_smul := continuous_iff_continuous_at.2 $ λ p, tendsto_iff_norm_tendsto_zero.2 _ }, refine squeeze_zero (λ _, norm_nonneg _) _ _, { exact λ q, ∥q.1 - p.1∥ * ∥q.2∥ + ∥p.1∥ * ∥q.2 - p.2∥ }, { intro q, rw [← sub_add_sub_cancel, ← norm_smul, ← norm_smul, smul_sub, sub_smul], exact norm_add_le _ _ }, { conv { congr, skip, skip, congr, rw [← zero_add (0:ℝ)], congr, rw [← zero_mul ∥p.2∥], skip, rw [← mul_zero ∥p.1∥] }, exact ((tendsto_iff_norm_tendsto_zero.1 (continuous_fst.tendsto p)).mul (continuous_snd.tendsto p).norm).add (tendsto_const_nhds.mul (tendsto_iff_norm_tendsto_zero.1 (continuous_snd.tendsto p))) } end theorem closure_ball [normed_space ℝ E] (x : E) {r : ℝ} (hr : 0 < r) : closure (ball x r) = closed_ball x r := begin refine set.subset.antisymm closure_ball_subset_closed_ball (λ y hy, _), have : continuous_within_at (λ c : ℝ, c • (y - x) + x) (set.Ico 0 1) 1 := ((continuous_id.smul continuous_const).add continuous_const).continuous_within_at, convert this.mem_closure _ _, { rw [one_smul, sub_add_cancel] }, { simp [closure_Ico (@zero_lt_one ℝ _ _), zero_le_one] }, { rintros c ⟨hc0, hc1⟩, rw [set.mem_preimage, mem_ball, dist_eq_norm, add_sub_cancel, norm_smul, real.norm_eq_abs, abs_of_nonneg hc0, mul_comm, ← mul_one r], rw [mem_closed_ball, dist_eq_norm] at hy, apply mul_lt_mul'; assumption } end theorem frontier_ball [normed_space ℝ E] (x : E) {r : ℝ} (hr : 0 < r) : frontier (ball x r) = sphere x r := begin rw [frontier, closure_ball x hr, is_open_ball.interior_eq], ext x, exact (@eq_iff_le_not_lt ℝ _ _ _).symm end theorem interior_closed_ball [normed_space ℝ E] (x : E) {r : ℝ} (hr : 0 < r) : interior (closed_ball x r) = ball x r := begin refine set.subset.antisymm _ ball_subset_interior_closed_ball, intros y hy, rcases le_iff_lt_or_eq.1 (mem_closed_ball.1 $ interior_subset hy) with hr|rfl, { exact hr }, set f : ℝ → E := λ c : ℝ, c • (y - x) + x, suffices : f ⁻¹' closed_ball x (dist y x) ⊆ set.Icc (-1) 1, { have hfc : continuous f := (continuous_id.smul continuous_const).add continuous_const, have hf1 : (1:ℝ) ∈ f ⁻¹' (interior (closed_ball x $ dist y x)), by simpa [f], have h1 : (1:ℝ) ∈ interior (set.Icc (-1:ℝ) 1) := interior_mono this (preimage_interior_subset_interior_preimage hfc hf1), contrapose h1, simp }, intros c hc, rw [set.mem_Icc, ← abs_le, ← real.norm_eq_abs, ← mul_le_mul_right hr], simpa [f, dist_eq_norm, norm_smul] using hc end theorem interior_closed_ball' [normed_space ℝ E] [nontrivial E] (x : E) (r : ℝ) : interior (closed_ball x r) = ball x r := begin rcases lt_trichotomy r 0 with hr|rfl|hr, { simp [closed_ball_eq_empty_iff_neg.2 hr, ball_eq_empty_iff_nonpos.2 (le_of_lt hr)] }, { suffices : x ∉ interior {x}, { rw [ball_zero, closed_ball_zero, ← set.subset_empty_iff], intros y hy, obtain rfl : y = x := set.mem_singleton_iff.1 (interior_subset hy), exact this hy }, rw [← set.mem_compl_iff, ← closure_compl], rcases exists_ne (0 : E) with ⟨z, hz⟩, suffices : (λ c : ℝ, x + c • z) 0 ∈ closure ({x}ᶜ : set E), by simpa only [zero_smul, add_zero] using this, have : (0:ℝ) ∈ closure (set.Ioi (0:ℝ)), by simp [closure_Ioi], refine (continuous_const.add (continuous_id.smul continuous_const)).continuous_within_at.mem_closure this _, intros c hc, simp [smul_eq_zero, hz, ne_of_gt hc] }, { exact interior_closed_ball x hr } end theorem frontier_closed_ball [normed_space ℝ E] (x : E) {r : ℝ} (hr : 0 < r) : frontier (closed_ball x r) = sphere x r := by rw [frontier, closure_closed_ball, interior_closed_ball x hr, closed_ball_diff_ball] theorem frontier_closed_ball' [normed_space ℝ E] [nontrivial E] (x : E) (r : ℝ) : frontier (closed_ball x r) = sphere x r := by rw [frontier, closure_closed_ball, interior_closed_ball' x r, closed_ball_diff_ball] variables (α) lemma ne_neg_of_mem_sphere [char_zero α] {r : ℝ} (hr : 0 < r) (x : sphere (0:E) r) : x ≠ - x := λ h, nonzero_of_mem_sphere hr x (eq_zero_of_eq_neg α (by { conv_lhs {rw h}, simp })) lemma ne_neg_of_mem_unit_sphere [char_zero α] (x : sphere (0:E) 1) : x ≠ - x := ne_neg_of_mem_sphere α (by norm_num) x variables {α} open normed_field /-- If there is a scalar `c` with `∥c∥>1`, then any element can be moved by scalar multiplication to any shell of width `∥c∥`. Also recap information on the norm of the rescaling element that shows up in applications. -/ lemma rescale_to_shell {c : α} (hc : 1 < ∥c∥) {ε : ℝ} (εpos : 0 < ε) {x : E} (hx : x ≠ 0) : ∃d:α, d ≠ 0 ∧ ∥d • x∥ < ε ∧ (ε/∥c∥ ≤ ∥d • x∥) ∧ (∥d∥⁻¹ ≤ ε⁻¹ * ∥c∥ * ∥x∥) := begin have xεpos : 0 < ∥x∥/ε := div_pos (norm_pos_iff.2 hx) εpos, rcases exists_int_pow_near xεpos hc with ⟨n, hn⟩, have cpos : 0 < ∥c∥ := lt_trans (zero_lt_one : (0 :ℝ) < 1) hc, have cnpos : 0 < ∥c^(n+1)∥ := by { rw norm_fpow, exact lt_trans xεpos hn.2 }, refine ⟨(c^(n+1))⁻¹, _, _, _, _⟩, show (c ^ (n + 1))⁻¹ ≠ 0, by rwa [ne.def, inv_eq_zero, ← ne.def, ← norm_pos_iff], show ∥(c ^ (n + 1))⁻¹ • x∥ < ε, { rw [norm_smul, norm_inv, ← div_eq_inv_mul, div_lt_iff cnpos, mul_comm, norm_fpow], exact (div_lt_iff εpos).1 (hn.2) }, show ε / ∥c∥ ≤ ∥(c ^ (n + 1))⁻¹ • x∥, { rw [div_le_iff cpos, norm_smul, norm_inv, norm_fpow, fpow_add (ne_of_gt cpos), fpow_one, mul_inv_rev', mul_comm, ← mul_assoc, ← mul_assoc, mul_inv_cancel (ne_of_gt cpos), one_mul, ← div_eq_inv_mul, le_div_iff (fpow_pos_of_pos cpos _), mul_comm], exact (le_div_iff εpos).1 hn.1 }, show ∥(c ^ (n + 1))⁻¹∥⁻¹ ≤ ε⁻¹ * ∥c∥ * ∥x∥, { have : ε⁻¹ * ∥c∥ * ∥x∥ = ε⁻¹ * ∥x∥ * ∥c∥, by ring, rw [norm_inv, inv_inv', norm_fpow, fpow_add (ne_of_gt cpos), fpow_one, this, ← div_eq_inv_mul], exact mul_le_mul_of_nonneg_right hn.1 (norm_nonneg _) } end /-- The product of two normed spaces is a normed space, with the sup norm. -/ instance : normed_space α (E × F) := { norm_smul_le := λ s x, le_of_eq $ by simp [prod.norm_def, norm_smul, mul_max_of_nonneg], -- TODO: without the next two lines Lean unfolds `≤` to `real.le` add_smul := λ r x y, prod.ext (add_smul _ _ _) (add_smul _ _ _), smul_add := λ r x y, prod.ext (smul_add _ _ _) (smul_add _ _ _), ..prod.normed_group, ..prod.semimodule } /-- The product of finitely many normed spaces is a normed space, with the sup norm. -/ instance pi.normed_space {E : ι → Type*} [fintype ι] [∀i, normed_group (E i)] [∀i, normed_space α (E i)] : normed_space α (Πi, E i) := { norm_smul_le := λ a f, le_of_eq $ show (↑(finset.sup finset.univ (λ (b : ι), nnnorm (a • f b))) : ℝ) = nnnorm a * ↑(finset.sup finset.univ (λ (b : ι), nnnorm (f b))), by simp only [(nnreal.coe_mul _ _).symm, nnreal.mul_finset_sup, nnnorm_smul] } /-- A subspace of a normed space is also a normed space, with the restriction of the norm. -/ instance submodule.normed_space {𝕜 : Type*} [normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] (s : submodule 𝕜 E) : normed_space 𝕜 s := { norm_smul_le := λc x, le_of_eq $ norm_smul c (x : E) } end normed_space section normed_algebra /-- A normed algebra `𝕜'` over `𝕜` is an algebra endowed with a norm for which the embedding of `𝕜` in `𝕜'` is an isometry. -/ class normed_algebra (𝕜 : Type*) (𝕜' : Type*) [normed_field 𝕜] [normed_ring 𝕜'] extends algebra 𝕜 𝕜' := (norm_algebra_map_eq : ∀x:𝕜, ∥algebra_map 𝕜 𝕜' x∥ = ∥x∥) @[simp] lemma norm_algebra_map_eq {𝕜 : Type*} (𝕜' : Type*) [normed_field 𝕜] [normed_ring 𝕜'] [h : normed_algebra 𝕜 𝕜'] (x : 𝕜) : ∥algebra_map 𝕜 𝕜' x∥ = ∥x∥ := normed_algebra.norm_algebra_map_eq _ variables (𝕜 : Type*) [normed_field 𝕜] variables (𝕜' : Type*) [normed_ring 𝕜'] @[priority 100] instance normed_algebra.to_normed_space [h : normed_algebra 𝕜 𝕜'] : normed_space 𝕜 𝕜' := { norm_smul_le := λ s x, calc ∥s • x∥ = ∥((algebra_map 𝕜 𝕜') s) * x∥ : by { rw h.smul_def', refl } ... ≤ ∥algebra_map 𝕜 𝕜' s∥ * ∥x∥ : normed_ring.norm_mul _ _ ... = ∥s∥ * ∥x∥ : by rw norm_algebra_map_eq, ..h } instance normed_algebra.id : normed_algebra 𝕜 𝕜 := { norm_algebra_map_eq := by simp, .. algebra.id 𝕜} variables {𝕜'} [normed_algebra 𝕜 𝕜'] include 𝕜 @[simp] lemma normed_algebra.norm_one : ∥(1:𝕜')∥ = 1 := by simpa using (norm_algebra_map_eq 𝕜' (1:𝕜)) lemma normed_algebra.norm_one_class : norm_one_class 𝕜' := ⟨normed_algebra.norm_one 𝕜⟩ lemma normed_algebra.zero_ne_one : (0:𝕜') ≠ 1 := begin refine (norm_pos_iff.mp _).symm, rw @normed_algebra.norm_one 𝕜, norm_num, end lemma normed_algebra.nontrivial : nontrivial 𝕜' := ⟨⟨0, 1, normed_algebra.zero_ne_one 𝕜⟩⟩ end normed_algebra section restrict_scalars variables (𝕜 : Type*) (𝕜' : Type*) [normed_field 𝕜] [normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] (E : Type*) [normed_group E] [normed_space 𝕜' E] /-- Warning: This declaration should be used judiciously. Please consider using `is_scalar_tower` instead. `𝕜`-normed space structure induced by a `𝕜'`-normed space structure when `𝕜'` is a normed algebra over `𝕜`. Not registered as an instance as `𝕜'` can not be inferred. The type synonym `semimodule.restrict_scalars 𝕜 𝕜' E` will be endowed with this instance by default. -/ def normed_space.restrict_scalars : normed_space 𝕜 E := { norm_smul_le := λc x, le_of_eq $ begin change ∥(algebra_map 𝕜 𝕜' c) • x∥ = ∥c∥ * ∥x∥, simp [norm_smul] end, ..restrict_scalars.semimodule 𝕜 𝕜' E } instance {𝕜 : Type*} {𝕜' : Type*} {E : Type*} [I : normed_group E] : normed_group (restrict_scalars 𝕜 𝕜' E) := I instance semimodule.restrict_scalars.normed_space_orig {𝕜 : Type*} {𝕜' : Type*} {E : Type*} [normed_field 𝕜'] [normed_group E] [I : normed_space 𝕜' E] : normed_space 𝕜' (restrict_scalars 𝕜 𝕜' E) := I instance : normed_space 𝕜 (restrict_scalars 𝕜 𝕜' E) := (normed_space.restrict_scalars 𝕜 𝕜' E : normed_space 𝕜 E) end restrict_scalars section summable open_locale classical open finset filter variables [normed_group α] [normed_group β] lemma cauchy_seq_finset_iff_vanishing_norm {f : ι → α} : cauchy_seq (λ s : finset ι, ∑ i in s, f i) ↔ ∀ε > (0 : ℝ), ∃s:finset ι, ∀t, disjoint t s → ∥ ∑ i in t, f i ∥ < ε := begin rw [cauchy_seq_finset_iff_vanishing, nhds_basis_ball.forall_iff], { simp only [ball_0_eq, set.mem_set_of_eq] }, { rintros s t hst ⟨s', hs'⟩, exact ⟨s', λ t' ht', hst $ hs' _ ht'⟩ } end lemma summable_iff_vanishing_norm [complete_space α] {f : ι → α} : summable f ↔ ∀ε > (0 : ℝ), ∃s:finset ι, ∀t, disjoint t s → ∥ ∑ i in t, f i ∥ < ε := by rw [summable_iff_cauchy_seq_finset, cauchy_seq_finset_iff_vanishing_norm] lemma cauchy_seq_finset_of_norm_bounded {f : ι → α} (g : ι → ℝ) (hg : summable g) (h : ∀i, ∥f i∥ ≤ g i) : cauchy_seq (λ s : finset ι, ∑ i in s, f i) := cauchy_seq_finset_iff_vanishing_norm.2 $ assume ε hε, let ⟨s, hs⟩ := summable_iff_vanishing_norm.1 hg ε hε in ⟨s, assume t ht, have ∥∑ i in t, g i∥ < ε := hs t ht, have nn : 0 ≤ ∑ i in t, g i := finset.sum_nonneg (assume a _, le_trans (norm_nonneg _) (h a)), lt_of_le_of_lt (norm_sum_le_of_le t (λ i _, h i)) $ by rwa [real.norm_eq_abs, abs_of_nonneg nn] at this⟩ lemma cauchy_seq_finset_of_summable_norm {f : ι → α} (hf : summable (λa, ∥f a∥)) : cauchy_seq (λ s : finset ι, ∑ a in s, f a) := cauchy_seq_finset_of_norm_bounded _ hf (assume i, le_refl _) /-- If a function `f` is summable in norm, and along some sequence of finsets exhausting the space its sum is converging to a limit `a`, then this holds along all finsets, i.e., `f` is summable with sum `a`. -/ lemma has_sum_of_subseq_of_summable {f : ι → α} (hf : summable (λa, ∥f a∥)) {s : γ → finset ι} {p : filter γ} [ne_bot p] (hs : tendsto s p at_top) {a : α} (ha : tendsto (λ b, ∑ i in s b, f i) p (𝓝 a)) : has_sum f a := tendsto_nhds_of_cauchy_seq_of_subseq (cauchy_seq_finset_of_summable_norm hf) hs ha /-- If `∑' i, ∥f i∥` is summable, then `∥(∑' i, f i)∥ ≤ (∑' i, ∥f i∥)`. Note that we do not assume that `∑' i, f i` is summable, and it might not be the case if `α` is not a complete space. -/ lemma norm_tsum_le_tsum_norm {f : ι → α} (hf : summable (λi, ∥f i∥)) : ∥(∑'i, f i)∥ ≤ (∑' i, ∥f i∥) := begin by_cases h : summable f, { have h₁ : tendsto (λs:finset ι, ∥∑ i in s, f i∥) at_top (𝓝 ∥(∑' i, f i)∥) := (continuous_norm.tendsto _).comp h.has_sum, have h₂ : tendsto (λs:finset ι, ∑ i in s, ∥f i∥) at_top (𝓝 (∑' i, ∥f i∥)) := hf.has_sum, exact le_of_tendsto_of_tendsto' h₁ h₂ (assume s, norm_sum_le _ _) }, { rw tsum_eq_zero_of_not_summable h, simp [tsum_nonneg] } end lemma has_sum_iff_tendsto_nat_of_summable_norm {f : ℕ → α} {a : α} (hf : summable (λi, ∥f i∥)) : has_sum f a ↔ tendsto (λn:ℕ, ∑ i in range n, f i) at_top (𝓝 a) := ⟨λ h, h.tendsto_sum_nat, λ h, has_sum_of_subseq_of_summable hf tendsto_finset_range h⟩ /-- The direct comparison test for series: if the norm of `f` is bounded by a real function `g` which is summable, then `f` is summable. -/ lemma summable_of_norm_bounded [complete_space α] {f : ι → α} (g : ι → ℝ) (hg : summable g) (h : ∀i, ∥f i∥ ≤ g i) : summable f := by { rw summable_iff_cauchy_seq_finset, exact cauchy_seq_finset_of_norm_bounded g hg h } /-- Quantitative result associated to the direct comparison test for series: If `∑' i, g i` is summable, and for all `i`, `∥f i∥ ≤ g i`, then `∥(∑' i, f i)∥ ≤ (∑' i, g i)`. Note that we do not assume that `∑' i, f i` is summable, and it might not be the case if `α` is not a complete space. -/ lemma tsum_of_norm_bounded {f : ι → α} {g : ι → ℝ} {a : ℝ} (hg : has_sum g a) (h : ∀i, ∥f i∥ ≤ g i) : ∥(∑' (i:ι), f i)∥ ≤ a := begin have h' : summable (λ (i : ι), ∥f i∥), { let f' : ι → ℝ := λ i, ∥f i∥, have h'' : ∀ i, ∥f' i∥ ≤ g i, { intros i, convert h i, simp }, simpa [f'] using summable_of_norm_bounded g hg.summable h'' }, have h1 : ∥(∑' (i:ι), f i)∥ ≤ ∑' (i:ι), ∥f i∥ := by simpa using norm_tsum_le_tsum_norm h', have h2 := tsum_le_tsum h h' hg.summable, have h3 : a = ∑' (i:ι), g i := (has_sum.tsum_eq hg).symm, linarith end variable [complete_space α] /-- Variant of the direct comparison test for series: if the norm of `f` is eventually bounded by a real function `g` which is summable, then `f` is summable. -/ lemma summable_of_norm_bounded_eventually {f : ι → α} (g : ι → ℝ) (hg : summable g) (h : ∀ᶠ i in cofinite, ∥f i∥ ≤ g i) : summable f := begin replace h := mem_cofinite.1 h, refine h.summable_compl_iff.mp _, refine summable_of_norm_bounded _ (h.summable_compl_iff.mpr hg) _, rintros ⟨a, h'⟩, simpa using h' end lemma summable_of_nnnorm_bounded {f : ι → α} (g : ι → ℝ≥0) (hg : summable g) (h : ∀i, nnnorm (f i) ≤ g i) : summable f := summable_of_norm_bounded (λ i, (g i : ℝ)) (nnreal.summable_coe.2 hg) (λ i, by exact_mod_cast h i) lemma summable_of_summable_norm {f : ι → α} (hf : summable (λa, ∥f a∥)) : summable f := summable_of_norm_bounded _ hf (assume i, le_refl _) lemma summable_of_summable_nnnorm {f : ι → α} (hf : summable (λa, nnnorm (f a))) : summable f := summable_of_nnnorm_bounded _ hf (assume i, le_refl _) end summable
dc48a8a78e5820a0fb499a4e2d27a4b47e2e610f
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/analysis/calculus/mean_value.lean
dc6ec5ad5c8439c9deb4f3c28c01398af1752726
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
60,509
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, Yury Kudryashov -/ import analysis.calculus.local_extr import analysis.convex.topology import data.complex.is_R_or_C /-! # The mean value inequality and equalities In this file we prove the following facts: * `convex.norm_image_sub_le_of_norm_deriv_le` : if `f` is differentiable on a convex set `s` and the norm of its derivative is bounded by `C`, then `f` is Lipschitz continuous on `s` with constant `C`; also a variant in which what is bounded by `C` is the norm of the difference of the derivative from a fixed linear map. This lemma and its versions are formulated using `is_R_or_C`, so they work both for real and complex derivatives. * `image_le_of*`, `image_norm_le_of_*` : several similar lemmas deducing `f x ≤ B x` or `∥f x∥ ≤ B x` from upper estimates on `f'` or `∥f'∥`, respectively. These lemmas differ by their assumptions: * `of_liminf_*` lemmas assume that limit inferior of some ratio is less than `B' x`; * `of_deriv_right_*`, `of_norm_deriv_right_*` lemmas assume that the right derivative or its norm is less than `B' x`; * `of_*_lt_*` lemmas assume a strict inequality whenever `f x = B x` or `∥f x∥ = B x`; * `of_*_le_*` lemmas assume a non-strict inequality everywhere on `[a, b)`; * name of a lemma ends with `'` if (1) it assumes that `B` is continuous on `[a, b]` and has a right derivative at every point of `[a, b)`, and (2) the lemma has a counterpart assuming that `B` is differentiable everywhere on `ℝ` * `norm_image_sub_le_*_segment` : if derivative of `f` on `[a, b]` is bounded above by a constant `C`, then `∥f x - f a∥ ≤ C * ∥x - a∥`; several versions deal with right derivative and derivative within `[a, b]` (`has_deriv_within_at` or `deriv_within`). * `convex.is_const_of_fderiv_within_eq_zero` : if a function has derivative `0` on a convex set `s`, then it is a constant on `s`. * `exists_ratio_has_deriv_at_eq_ratio_slope` and `exists_ratio_deriv_eq_ratio_slope` : Cauchy's Mean Value Theorem. * `exists_has_deriv_at_eq_slope` and `exists_deriv_eq_slope` : Lagrange's Mean Value Theorem. * `domain_mvt` : Lagrange's Mean Value Theorem, applied to a segment in a convex domain. * `convex.image_sub_lt_mul_sub_of_deriv_lt`, `convex.mul_sub_lt_image_sub_of_lt_deriv`, `convex.image_sub_le_mul_sub_of_deriv_le`, `convex.mul_sub_le_image_sub_of_le_deriv`, if `∀ x, C (</≤/>/≥) (f' x)`, then `C * (y - x) (</≤/>/≥) (f y - f x)` whenever `x < y`. * `convex.mono_of_deriv_nonneg`, `convex.antimono_of_deriv_nonpos`, `convex.strict_mono_of_deriv_pos`, `convex.strict_antimono_of_deriv_neg` : if the derivative of a function is non-negative/non-positive/positive/negative, then the function is monotone/monotonically decreasing/strictly monotone/strictly monotonically decreasing. * `convex_on_of_deriv_mono`, `convex_on_of_deriv2_nonneg` : if the derivative of a function is increasing or its second derivative is nonnegative, then the original function is convex. * `strict_fderiv_of_cont_diff` : a C^1 function over the reals is strictly differentiable. (This is a corollary of the mean value inequality.) -/ variables {E : Type*} [normed_group E] [normed_space ℝ E] {F : Type*} [normed_group F] [normed_space ℝ F] open metric set asymptotics continuous_linear_map filter open_locale classical topological_space nnreal /-! ### One-dimensional fencing inequalities -/ /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_liminf_slope_right_lt_deriv_boundary' {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) -- `hf'` actually says `liminf (z - x)⁻¹ * (f z - f x) ≤ f' x` (hf' : ∀ x ∈ Ico a b, ∀ r, f' x < r → ∃ᶠ z in 𝓝[Ioi x] x, (z - x)⁻¹ * (f z - f x) < r) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := begin change Icc a b ⊆ {x | f x ≤ B x}, set s := {x | f x ≤ B x} ∩ Icc a b, have A : continuous_on (λ x, (f x, B x)) (Icc a b), from hf.prod hB, have : is_closed s, { simp only [s, inter_comm], exact A.preimage_closed_of_closed is_closed_Icc order_closed_topology.is_closed_le' }, apply this.Icc_subset_of_forall_exists_gt ha, rintros x ⟨hxB : f x ≤ B x, xab⟩ y hy, cases hxB.lt_or_eq with hxB hxB, { -- If `f x < B x`, then all we need is continuity of both sides refine nonempty_of_mem_sets (inter_mem_sets _ (Ioc_mem_nhds_within_Ioi ⟨le_rfl, hy⟩)), have : ∀ᶠ x in 𝓝[Icc a b] x, f x < B x, from A x (Ico_subset_Icc_self xab) (is_open.mem_nhds (is_open_lt continuous_fst continuous_snd) hxB), have : ∀ᶠ x in 𝓝[Ioi x] x, f x < B x, from nhds_within_le_of_mem (Icc_mem_nhds_within_Ioi xab) this, exact this.mono (λ y, le_of_lt) }, { rcases exists_between (bound x xab hxB) with ⟨r, hfr, hrB⟩, specialize hf' x xab r hfr, have HB : ∀ᶠ z in 𝓝[Ioi x] x, r < (z - x)⁻¹ * (B z - B x), from (has_deriv_within_at_iff_tendsto_slope' $ lt_irrefl x).1 (hB' x xab).Ioi_of_Ici (Ioi_mem_nhds hrB), obtain ⟨z, ⟨hfz, hzB⟩, hz⟩ : ∃ z, ((z - x)⁻¹ * (f z - f x) < r ∧ r < (z - x)⁻¹ * (B z - B x)) ∧ z ∈ Ioc x y, from ((hf'.and_eventually HB).and_eventually (Ioc_mem_nhds_within_Ioi ⟨le_rfl, hy⟩)).exists, refine ⟨z, _, hz⟩, have := (hfz.trans hzB).le, rwa [mul_le_mul_left (inv_pos.2 $ sub_pos.2 hz.1), hxB, sub_le_sub_iff_right] at this } end /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_liminf_slope_right_lt_deriv_boundary {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) -- `hf'` actually says `liminf (z - x)⁻¹ * (f z - f x) ≤ f' x` (hf' : ∀ x ∈ Ico a b, ∀ r, f' x < r → ∃ᶠ z in 𝓝[Ioi x] x, (z - x)⁻¹ * (f z - f x) < r) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : ∀ x, has_deriv_at B (B' x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' hf hf' ha (λ x hx, (hB x).continuous_at.continuous_within_at) (λ x hx, (hB x).has_deriv_within_at) bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by `B'`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_liminf_slope_right_le_deriv_boundary {f : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x) -- `bound` actually says `liminf (z - x)⁻¹ * (f z - f x) ≤ B' x` (bound : ∀ x ∈ Ico a b, ∀ r, B' x < r → ∃ᶠ z in 𝓝[Ioi x] x, (z - x)⁻¹ * (f z - f x) < r) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := begin have Hr : ∀ x ∈ Icc a b, ∀ r > 0, f x ≤ B x + r * (x - a), { intros x hx r hr, apply image_le_of_liminf_slope_right_lt_deriv_boundary' hf bound, { rwa [sub_self, mul_zero, add_zero] }, { exact hB.add (continuous_on_const.mul (continuous_id.continuous_on.sub continuous_on_const)) }, { assume x hx, exact (hB' x hx).add (((has_deriv_within_at_id x (Ici x)).sub_const a).const_mul r) }, { assume x hx _, rw [mul_one], exact (lt_add_iff_pos_right _).2 hr }, exact hx }, assume x hx, have : continuous_within_at (λ r, B x + r * (x - a)) (Ioi 0) 0, from continuous_within_at_const.add (continuous_within_at_id.mul continuous_within_at_const), convert continuous_within_at_const.closure_le _ this (Hr x hx); simp end /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_deriv_right_lt_deriv_boundary' {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' hf (λ x hx r hr, (hf' x hx).liminf_right_slope_le hr) ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_deriv_right_lt_deriv_boundary {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : ∀ x, has_deriv_at B (B' x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_deriv_right_lt_deriv_boundary' hf hf' ha (λ x hx, (hB x).continuous_at.continuous_within_at) (λ x hx, (hB x).has_deriv_within_at) bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x ≤ B' x` on `[a, b)`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_deriv_right_le_deriv_boundary {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x) (bound : ∀ x ∈ Ico a b, f' x ≤ B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_liminf_slope_right_le_deriv_boundary hf ha hB hB' $ assume x hx r hr, (hf' x hx).liminf_right_slope_le (lt_of_le_of_lt (bound x hx) hr) /-! ### Vector-valued functions `f : ℝ → E` -/ section variables {f : ℝ → E} {a b : ℝ} /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `B` has right derivative at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(∥f z∥ - ∥f x∥) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `∥f x∥ = B x`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. -/ lemma image_norm_le_of_liminf_right_slope_norm_lt_deriv_boundary {E : Type*} [normed_group E] {f : ℝ → E} {f' : ℝ → ℝ} (hf : continuous_on f (Icc a b)) -- `hf'` actually says `liminf ∥z - x∥⁻¹ * (∥f z∥ - ∥f x∥) ≤ f' x` (hf' : ∀ x ∈ Ico a b, ∀ r, f' x < r → ∃ᶠ z in 𝓝[Ioi x] x, (z - x)⁻¹ * (∥f z∥ - ∥f x∥) < r) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x) (bound : ∀ x ∈ Ico a b, ∥f x∥ = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' (continuous_norm.comp_continuous_on hf) hf' ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` and `B` have right derivatives `f'` and `B'` respectively at every point of `[a, b)`; * the norm of `f'` is strictly less than `B'` whenever `∥f x∥ = B x`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_lt_deriv_boundary' {f' : ℝ → E} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x) (bound : ∀ x ∈ Ico a b, ∥f x∥ = B x → ∥f' x∥ < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_norm_le_of_liminf_right_slope_norm_lt_deriv_boundary hf (λ x hx r hr, (hf' x hx).liminf_right_slope_norm_le hr) ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` has right derivative `f'` at every point of `[a, b)`; * `B` has derivative `B'` everywhere on `ℝ`; * the norm of `f'` is strictly less than `B'` whenever `∥f x∥ = B x`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_lt_deriv_boundary {f' : ℝ → E} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : ∀ x, has_deriv_at B (B' x) x) (bound : ∀ x ∈ Ico a b, ∥f x∥ = B x → ∥f' x∥ < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_norm_le_of_norm_deriv_right_lt_deriv_boundary' hf hf' ha (λ x hx, (hB x).continuous_at.continuous_within_at) (λ x hx, (hB x).has_deriv_within_at) bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` and `B` have right derivatives `f'` and `B'` respectively at every point of `[a, b)`; * we have `∥f' x∥ ≤ B x` everywhere on `[a, b)`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_le_deriv_boundary' {f' : ℝ → E} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ici x) x) (bound : ∀ x ∈ Ico a b, ∥f' x∥ ≤ B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_le_of_liminf_slope_right_le_deriv_boundary (continuous_norm.comp_continuous_on hf) ha hB hB' $ (λ x hx r hr, (hf' x hx).liminf_right_slope_norm_le (lt_of_le_of_lt (bound x hx) hr)) /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` has right derivative `f'` at every point of `[a, b)`; * `B` has derivative `B'` everywhere on `ℝ`; * we have `∥f' x∥ ≤ B x` everywhere on `[a, b)`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_le_deriv_boundary {f' : ℝ → E} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : ∀ x, has_deriv_at B (B' x) x) (bound : ∀ x ∈ Ico a b, ∥f' x∥ ≤ B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_norm_le_of_norm_deriv_right_le_deriv_boundary' hf hf' ha (λ x hx, (hB x).continuous_at.continuous_within_at) (λ x hx, (hB x).has_deriv_within_at) bound /-- A function on `[a, b]` with the norm of the right derivative bounded by `C` satisfies `∥f x - f a∥ ≤ C * (x - a)`. -/ theorem norm_image_sub_le_of_norm_deriv_right_le_segment {f' : ℝ → E} {C : ℝ} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) (bound : ∀x ∈ Ico a b, ∥f' x∥ ≤ C) : ∀ x ∈ Icc a b, ∥f x - f a∥ ≤ C * (x - a) := begin let g := λ x, f x - f a, have hg : continuous_on g (Icc a b), from hf.sub continuous_on_const, have hg' : ∀ x ∈ Ico a b, has_deriv_within_at g (f' x) (Ici x) x, { assume x hx, simpa using (hf' x hx).sub (has_deriv_within_at_const _ _ _) }, let B := λ x, C * (x - a), have hB : ∀ x, has_deriv_at B C x, { assume x, simpa using (has_deriv_at_const x C).mul ((has_deriv_at_id x).sub (has_deriv_at_const x a)) }, convert image_norm_le_of_norm_deriv_right_le_deriv_boundary hg hg' _ hB bound, simp only [g, B], rw [sub_self, norm_zero, sub_self, mul_zero] end /-- A function on `[a, b]` with the norm of the derivative within `[a, b]` bounded by `C` satisfies `∥f x - f a∥ ≤ C * (x - a)`, `has_deriv_within_at` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment' {f' : ℝ → E} {C : ℝ} (hf : ∀ x ∈ Icc a b, has_deriv_within_at f (f' x) (Icc a b) x) (bound : ∀x ∈ Ico a b, ∥f' x∥ ≤ C) : ∀ x ∈ Icc a b, ∥f x - f a∥ ≤ C * (x - a) := begin refine norm_image_sub_le_of_norm_deriv_right_le_segment (λ x hx, (hf x hx).continuous_within_at) (λ x hx, _) bound, exact (hf x $ Ico_subset_Icc_self hx).nhds_within (Icc_mem_nhds_within_Ici hx) end /-- A function on `[a, b]` with the norm of the derivative within `[a, b]` bounded by `C` satisfies `∥f x - f a∥ ≤ C * (x - a)`, `deriv_within` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment {C : ℝ} (hf : differentiable_on ℝ f (Icc a b)) (bound : ∀x ∈ Ico a b, ∥deriv_within f (Icc a b) x∥ ≤ C) : ∀ x ∈ Icc a b, ∥f x - f a∥ ≤ C * (x - a) := begin refine norm_image_sub_le_of_norm_deriv_le_segment' _ bound, exact λ x hx, (hf x hx).has_deriv_within_at end /-- A function on `[0, 1]` with the norm of the derivative within `[0, 1]` bounded by `C` satisfies `∥f 1 - f 0∥ ≤ C`, `has_deriv_within_at` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment_01' {f' : ℝ → E} {C : ℝ} (hf : ∀ x ∈ Icc (0:ℝ) 1, has_deriv_within_at f (f' x) (Icc (0:ℝ) 1) x) (bound : ∀x ∈ Ico (0:ℝ) 1, ∥f' x∥ ≤ C) : ∥f 1 - f 0∥ ≤ C := by simpa only [sub_zero, mul_one] using norm_image_sub_le_of_norm_deriv_le_segment' hf bound 1 (right_mem_Icc.2 zero_le_one) /-- A function on `[0, 1]` with the norm of the derivative within `[0, 1]` bounded by `C` satisfies `∥f 1 - f 0∥ ≤ C`, `deriv_within` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment_01 {C : ℝ} (hf : differentiable_on ℝ f (Icc (0:ℝ) 1)) (bound : ∀x ∈ Ico (0:ℝ) 1, ∥deriv_within f (Icc (0:ℝ) 1) x∥ ≤ C) : ∥f 1 - f 0∥ ≤ C := by simpa only [sub_zero, mul_one] using norm_image_sub_le_of_norm_deriv_le_segment hf bound 1 (right_mem_Icc.2 zero_le_one) theorem constant_of_has_deriv_right_zero (hcont : continuous_on f (Icc a b)) (hderiv : ∀ x ∈ Ico a b, has_deriv_within_at f 0 (Ici x) x) : ∀ x ∈ Icc a b, f x = f a := by simpa only [zero_mul, norm_le_zero_iff, sub_eq_zero] using λ x hx, norm_image_sub_le_of_norm_deriv_right_le_segment hcont hderiv (λ y hy, by rw norm_le_zero_iff) x hx theorem constant_of_deriv_within_zero (hdiff : differentiable_on ℝ f (Icc a b)) (hderiv : ∀ x ∈ Ico a b, deriv_within f (Icc a b) x = 0) : ∀ x ∈ Icc a b, f x = f a := begin have H : ∀ x ∈ Ico a b, ∥deriv_within f (Icc a b) x∥ ≤ 0 := by simpa only [norm_le_zero_iff] using λ x hx, hderiv x hx, simpa only [zero_mul, norm_le_zero_iff, sub_eq_zero] using λ x hx, norm_image_sub_le_of_norm_deriv_le_segment hdiff H x hx, end variables {f' g : ℝ → E} /-- If two continuous functions on `[a, b]` have the same right derivative and are equal at `a`, then they are equal everywhere on `[a, b]`. -/ theorem eq_of_has_deriv_right_eq (derivf : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ici x) x) (derivg : ∀ x ∈ Ico a b, has_deriv_within_at g (f' x) (Ici x) x) (fcont : continuous_on f (Icc a b)) (gcont : continuous_on g (Icc a b)) (hi : f a = g a) : ∀ y ∈ Icc a b, f y = g y := begin simp only [← @sub_eq_zero _ _ (f _)] at hi ⊢, exact hi ▸ constant_of_has_deriv_right_zero (fcont.sub gcont) (λ y hy, by simpa only [sub_self] using (derivf y hy).sub (derivg y hy)), end /-- If two differentiable functions on `[a, b]` have the same derivative within `[a, b]` everywhere on `[a, b)` and are equal at `a`, then they are equal everywhere on `[a, b]`. -/ theorem eq_of_deriv_within_eq (fdiff : differentiable_on ℝ f (Icc a b)) (gdiff : differentiable_on ℝ g (Icc a b)) (hderiv : eq_on (deriv_within f (Icc a b)) (deriv_within g (Icc a b)) (Ico a b)) (hi : f a = g a) : ∀ y ∈ Icc a b, f y = g y := begin have A : ∀ y ∈ Ico a b, has_deriv_within_at f (deriv_within f (Icc a b) y) (Ici y) y := λ y hy, (fdiff y (mem_Icc_of_Ico hy)).has_deriv_within_at.nhds_within (Icc_mem_nhds_within_Ici hy), have B : ∀ y ∈ Ico a b, has_deriv_within_at g (deriv_within g (Icc a b) y) (Ici y) y := λ y hy, (gdiff y (mem_Icc_of_Ico hy)).has_deriv_within_at.nhds_within (Icc_mem_nhds_within_Ici hy), exact eq_of_has_deriv_right_eq A (λ y hy, (hderiv hy).symm ▸ B y hy) fdiff.continuous_on gdiff.continuous_on hi end end /-! ### Vector-valued functions `f : E → G` Theorems in this section work both for real and complex differentiable functions. We use assumptions `[is_R_or_C 𝕜] [normed_space 𝕜 E] [normed_space 𝕜 G]` to achieve this result. For the domain `E` we also assume `[normed_space ℝ E] [is_scalar_tower ℝ 𝕜 E]` to have a notion of a `convex` set. In both interesting cases `𝕜 = ℝ` and `𝕜 = ℂ` the assumption `[is_scalar_tower ℝ 𝕜 E]` is satisfied automatically. -/ section variables {𝕜 G : Type*} [is_R_or_C 𝕜] [normed_space 𝕜 E] [is_scalar_tower ℝ 𝕜 E] [normed_group G] [normed_space 𝕜 G] {f : E → G} {C : ℝ} {s : set E} {x y : E} {f' : E → E →L[𝕜] G} {φ : E →L[𝕜] G} /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `has_fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_has_fderiv_within_le (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := begin letI : normed_space ℝ G := restrict_scalars.normed_space ℝ 𝕜 G, letI : is_scalar_tower ℝ 𝕜 G := restrict_scalars.is_scalar_tower _ _ _, /- By composition with `t ↦ x + t • (y-x)`, we reduce to a statement for functions defined on `[0,1]`, for which it is proved in `norm_image_sub_le_of_norm_deriv_le_segment`. We just have to check the differentiability of the composition and bounds on its derivative, which is straightforward but tedious for lack of automation. -/ have C0 : 0 ≤ C := le_trans (norm_nonneg _) (bound x xs), set g : ℝ → E := λ t, x + t • (y - x), have Dg : ∀ t, has_deriv_at g (y-x) t, { assume t, simpa only [one_smul] using ((has_deriv_at_id t).smul_const (y - x)).const_add x }, have segm : Icc 0 1 ⊆ g ⁻¹' s, { rw [← image_subset_iff, ← segment_eq_image'], apply hs.segment_subset xs ys }, have : f x = f (g 0), by { simp only [g], rw [zero_smul, add_zero] }, rw this, have : f y = f (g 1), by { simp only [g], rw [one_smul, add_sub_cancel'_right] }, rw this, have D2: ∀ t ∈ Icc (0:ℝ) 1, has_deriv_within_at (f ∘ g) (f' (g t) (y - x)) (Icc 0 1) t, { intros t ht, have : has_fderiv_within_at f ((f' (g t)).restrict_scalars ℝ) s (g t), from hf (g t) (segm ht), exact this.comp_has_deriv_within_at _ (Dg t).has_deriv_within_at segm }, apply norm_image_sub_le_of_norm_deriv_le_segment_01' D2, refine λ t ht, le_of_op_norm_le _ _ _, exact bound (g t) (segm $ Ico_subset_Icc_self ht) end /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `has_fderiv_within` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_has_fderiv_within_le (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x∥ ≤ C) (hs : convex s) : lipschitz_on_with (nnreal.of_real C) f s := begin rw lipschitz_on_with_iff_norm_sub_le, intros x x_in y y_in, convert hs.norm_image_sub_le_of_norm_has_fderiv_within_le hf bound y_in x_in, exact nnreal.coe_of_real C ((norm_nonneg $ f' x).trans $ bound x x_in) end /-- The mean value theorem on a convex set: if the derivative of a function within this set is bounded by `C`, then the function is `C`-Lipschitz. Version with `fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_within_le (hf : differentiable_on 𝕜 f s) (bound : ∀x∈s, ∥fderiv_within 𝕜 f s x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_within_at) bound xs ys /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `fderiv_within` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_fderiv_within_le (hf : differentiable_on 𝕜 f s) (bound : ∀x∈s, ∥fderiv_within 𝕜 f s x∥ ≤ C) (hs : convex s) : lipschitz_on_with (nnreal.of_real C) f s:= hs.lipschitz_on_with_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_within_at) bound /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `fderiv`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_le (hf : ∀ x ∈ s, differentiable_at 𝕜 f x) (bound : ∀x∈s, ∥fderiv 𝕜 f x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_at.has_fderiv_within_at) bound xs ys /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `fderiv` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_fderiv_le (hf : ∀ x ∈ s, differentiable_at 𝕜 f x) (bound : ∀x∈s, ∥fderiv 𝕜 f x∥ ≤ C) (hs : convex s) : lipschitz_on_with (nnreal.of_real C) f s := hs.lipschitz_on_with_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_at.has_fderiv_within_at) bound /-- Variant of the mean value inequality on a convex set, using a bound on the difference between the derivative and a fixed linear map, rather than a bound on the derivative itself. Version with `has_fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_has_fderiv_within_le' (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x - φ∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x - φ (y - x)∥ ≤ C * ∥y - x∥ := begin /- We subtract `φ` to define a new function `g` for which `g' = 0`, for which the previous theorem applies, `convex.norm_image_sub_le_of_norm_has_fderiv_within_le`. Then, we just need to glue together the pieces, expressing back `f` in terms of `g`. -/ let g := λy, f y - φ y, have hg : ∀ x ∈ s, has_fderiv_within_at g (f' x - φ) s x := λ x xs, (hf x xs).sub φ.has_fderiv_within_at, calc ∥f y - f x - φ (y - x)∥ = ∥f y - f x - (φ y - φ x)∥ : by simp ... = ∥(f y - φ y) - (f x - φ x)∥ : by abel ... = ∥g y - g x∥ : by simp ... ≤ C * ∥y - x∥ : convex.norm_image_sub_le_of_norm_has_fderiv_within_le hg bound hs xs ys, end /-- Variant of the mean value inequality on a convex set. Version with `fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_within_le' (hf : differentiable_on 𝕜 f s) (bound : ∀x∈s, ∥fderiv_within 𝕜 f s x - φ∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x - φ (y - x)∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_fderiv_within_le' (λ x hx, (hf x hx).has_fderiv_within_at) bound xs ys /-- Variant of the mean value inequality on a convex set. Version with `fderiv`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_le' (hf : ∀ x ∈ s, differentiable_at 𝕜 f x) (bound : ∀x∈s, ∥fderiv 𝕜 f x - φ∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x - φ (y - x)∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_fderiv_within_le' (λ x hx, (hf x hx).has_fderiv_at.has_fderiv_within_at) bound xs ys /-- If a function has zero Fréchet derivative at every point of a convex set, then it is a constant on this set. -/ theorem convex.is_const_of_fderiv_within_eq_zero (hs : convex s) (hf : differentiable_on 𝕜 f s) (hf' : ∀ x ∈ s, fderiv_within 𝕜 f s x = 0) (hx : x ∈ s) (hy : y ∈ s) : f x = f y := have bound : ∀ x ∈ s, ∥fderiv_within 𝕜 f s x∥ ≤ 0, from λ x hx, by simp only [hf' x hx, norm_zero], by simpa only [(dist_eq_norm _ _).symm, zero_mul, dist_le_zero, eq_comm] using hs.norm_image_sub_le_of_norm_fderiv_within_le hf bound hx hy theorem is_const_of_fderiv_eq_zero (hf : differentiable 𝕜 f) (hf' : ∀ x, fderiv 𝕜 f x = 0) (x y : E) : f x = f y := convex_univ.is_const_of_fderiv_within_eq_zero hf.differentiable_on (λ x _, by rw fderiv_within_univ; exact hf' x) trivial trivial end /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `has_deriv_within`. -/ theorem convex.norm_image_sub_le_of_norm_has_deriv_within_le {f f' : ℝ → F} {C : ℝ} {s : set ℝ} {x y : ℝ} (hf : ∀ x ∈ s, has_deriv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := convex.norm_image_sub_le_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_within_at) (λ x hx, le_trans (by simp) (bound x hx)) hs xs ys /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `has_deriv_within` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_has_deriv_within_le {f f' : ℝ → F} {C : ℝ} {s : set ℝ} (hs : convex s) (hf : ∀ x ∈ s, has_deriv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x∥ ≤ C) : lipschitz_on_with (nnreal.of_real C) f s := convex.lipschitz_on_with_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_within_at) (λ x hx, le_trans (by simp) (bound x hx)) hs /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function within this set is bounded by `C`, then the function is `C`-Lipschitz. Version with `deriv_within` -/ theorem convex.norm_image_sub_le_of_norm_deriv_within_le {f : ℝ → F} {C : ℝ} {s : set ℝ} {x y : ℝ} (hf : differentiable_on ℝ f s) (bound : ∀x∈s, ∥deriv_within f s x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_deriv_within_le (λ x hx, (hf x hx).has_deriv_within_at) bound xs ys /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `deriv_within` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_deriv_within_le {f : ℝ → F} {C : ℝ} {s : set ℝ} (hs : convex s) (hf : differentiable_on ℝ f s) (bound : ∀x∈s, ∥deriv_within f s x∥ ≤ C) : lipschitz_on_with (nnreal.of_real C) f s := hs.lipschitz_on_with_of_norm_has_deriv_within_le (λ x hx, (hf x hx).has_deriv_within_at) bound /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `deriv`. -/ theorem convex.norm_image_sub_le_of_norm_deriv_le {f : ℝ → F} {C : ℝ} {s : set ℝ} {x y : ℝ} (hf : ∀ x ∈ s, differentiable_at ℝ f x) (bound : ∀x∈s, ∥deriv f x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_deriv_within_le (λ x hx, (hf x hx).has_deriv_at.has_deriv_within_at) bound xs ys /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `deriv` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_deriv_le {f : ℝ → F} {C : ℝ} {s : set ℝ} (hf : ∀ x ∈ s, differentiable_at ℝ f x) (bound : ∀x∈s, ∥deriv f x∥ ≤ C) (hs : convex s) : lipschitz_on_with (nnreal.of_real C) f s := hs.lipschitz_on_with_of_norm_has_deriv_within_le (λ x hx, (hf x hx).has_deriv_at.has_deriv_within_at) bound /-! ### Functions `[a, b] → ℝ`. -/ section interval -- Declare all variables here to make sure they come in a correct order variables (f f' : ℝ → ℝ) {a b : ℝ} (hab : a < b) (hfc : continuous_on f (Icc a b)) (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hfd : differentiable_on ℝ f (Ioo a b)) (g g' : ℝ → ℝ) (hgc : continuous_on g (Icc a b)) (hgg' : ∀ x ∈ Ioo a b, has_deriv_at g (g' x) x) (hgd : differentiable_on ℝ g (Ioo a b)) include hab hfc hff' hgc hgg' /-- Cauchy's Mean Value Theorem, `has_deriv_at` version. -/ lemma exists_ratio_has_deriv_at_eq_ratio_slope : ∃ c ∈ Ioo a b, (g b - g a) * f' c = (f b - f a) * g' c := begin let h := λ x, (g b - g a) * f x - (f b - f a) * g x, have hI : h a = h b, { simp only [h], ring }, let h' := λ x, (g b - g a) * f' x - (f b - f a) * g' x, have hhh' : ∀ x ∈ Ioo a b, has_deriv_at h (h' x) x, from λ x hx, ((hff' x hx).const_mul (g b - g a)).sub ((hgg' x hx).const_mul (f b - f a)), have hhc : continuous_on h (Icc a b), from (continuous_on_const.mul hfc).sub (continuous_on_const.mul hgc), rcases exists_has_deriv_at_eq_zero h h' hab hhc hI hhh' with ⟨c, cmem, hc⟩, exact ⟨c, cmem, sub_eq_zero.1 hc⟩ end omit hfc hgc /-- Cauchy's Mean Value Theorem, extended `has_deriv_at` version. -/ lemma exists_ratio_has_deriv_at_eq_ratio_slope' {lfa lga lfb lgb : ℝ} (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hgg' : ∀ x ∈ Ioo a b, has_deriv_at g (g' x) x) (hfa : tendsto f (𝓝[Ioi a] a) (𝓝 lfa)) (hga : tendsto g (𝓝[Ioi a] a) (𝓝 lga)) (hfb : tendsto f (𝓝[Iio b] b) (𝓝 lfb)) (hgb : tendsto g (𝓝[Iio b] b) (𝓝 lgb)) : ∃ c ∈ Ioo a b, (lgb - lga) * (f' c) = (lfb - lfa) * (g' c) := begin let h := λ x, (lgb - lga) * f x - (lfb - lfa) * g x, have hha : tendsto h (𝓝[Ioi a] a) (𝓝 $ lgb * lfa - lfb * lga), { have : tendsto h (𝓝[Ioi a] a)(𝓝 $ (lgb - lga) * lfa - (lfb - lfa) * lga) := (tendsto_const_nhds.mul hfa).sub (tendsto_const_nhds.mul hga), convert this using 2, ring }, have hhb : tendsto h (𝓝[Iio b] b) (𝓝 $ lgb * lfa - lfb * lga), { have : tendsto h (𝓝[Iio b] b)(𝓝 $ (lgb - lga) * lfb - (lfb - lfa) * lgb) := (tendsto_const_nhds.mul hfb).sub (tendsto_const_nhds.mul hgb), convert this using 2, ring }, let h' := λ x, (lgb - lga) * f' x - (lfb - lfa) * g' x, have hhh' : ∀ x ∈ Ioo a b, has_deriv_at h (h' x) x, { intros x hx, exact ((hff' x hx).const_mul _ ).sub (((hgg' x hx)).const_mul _) }, rcases exists_has_deriv_at_eq_zero' hab hha hhb hhh' with ⟨c, cmem, hc⟩, exact ⟨c, cmem, sub_eq_zero.1 hc⟩ end include hfc omit hgg' /-- Lagrange's Mean Value Theorem, `has_deriv_at` version -/ lemma exists_has_deriv_at_eq_slope : ∃ c ∈ Ioo a b, f' c = (f b - f a) / (b - a) := begin rcases exists_ratio_has_deriv_at_eq_ratio_slope f f' hab hfc hff' id 1 continuous_id.continuous_on (λ x hx, has_deriv_at_id x) with ⟨c, cmem, hc⟩, use [c, cmem], simp only [_root_.id, pi.one_apply, mul_one] at hc, rw [← hc, mul_div_cancel_left], exact ne_of_gt (sub_pos.2 hab) end omit hff' /-- Cauchy's Mean Value Theorem, `deriv` version. -/ lemma exists_ratio_deriv_eq_ratio_slope : ∃ c ∈ Ioo a b, (g b - g a) * (deriv f c) = (f b - f a) * (deriv g c) := exists_ratio_has_deriv_at_eq_ratio_slope f (deriv f) hab hfc (λ x hx, ((hfd x hx).differentiable_at $ is_open.mem_nhds is_open_Ioo hx).has_deriv_at) g (deriv g) hgc $ λ x hx, ((hgd x hx).differentiable_at $ is_open.mem_nhds is_open_Ioo hx).has_deriv_at omit hfc /-- Cauchy's Mean Value Theorem, extended `deriv` version. -/ lemma exists_ratio_deriv_eq_ratio_slope' {lfa lga lfb lgb : ℝ} (hdf : differentiable_on ℝ f $ Ioo a b) (hdg : differentiable_on ℝ g $ Ioo a b) (hfa : tendsto f (𝓝[Ioi a] a) (𝓝 lfa)) (hga : tendsto g (𝓝[Ioi a] a) (𝓝 lga)) (hfb : tendsto f (𝓝[Iio b] b) (𝓝 lfb)) (hgb : tendsto g (𝓝[Iio b] b) (𝓝 lgb)) : ∃ c ∈ Ioo a b, (lgb - lga) * (deriv f c) = (lfb - lfa) * (deriv g c) := exists_ratio_has_deriv_at_eq_ratio_slope' _ _ hab _ _ (λ x hx, ((hdf x hx).differentiable_at $ Ioo_mem_nhds hx.1 hx.2).has_deriv_at) (λ x hx, ((hdg x hx).differentiable_at $ Ioo_mem_nhds hx.1 hx.2).has_deriv_at) hfa hga hfb hgb /-- Lagrange's Mean Value Theorem, `deriv` version. -/ lemma exists_deriv_eq_slope : ∃ c ∈ Ioo a b, deriv f c = (f b - f a) / (b - a) := exists_has_deriv_at_eq_slope f (deriv f) hab hfc (λ x hx, ((hfd x hx).differentiable_at $ is_open.mem_nhds is_open_Ioo hx).has_deriv_at) end interval /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `C < f'`, then `f` grows faster than `C * x` on `D`, i.e., `C * (y - x) < f y - f x` whenever `x, y ∈ D`, `x < y`. -/ theorem convex.mul_sub_lt_image_sub_of_lt_deriv {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (hf'_gt : ∀ x ∈ interior D, C < deriv f x) : ∀ x y ∈ D, x < y → C * (y - x) < f y - f x := begin assume x y hx hy hxy, have hxyD : Icc x y ⊆ D, from hD.ord_connected.out hx hy, have hxyD' : Ioo x y ⊆ interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩, obtain ⟨a, a_mem, ha⟩ : ∃ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x), from exists_deriv_eq_slope f hxy (hf.mono hxyD) (hf'.mono hxyD'), have : C < (f y - f x) / (y - x), by { rw [← ha], exact hf'_gt _ (hxyD' a_mem) }, exact (lt_div_iff (sub_pos.2 hxy)).1 this end /-- Let `f : ℝ → ℝ` be a differentiable function. If `C < f'`, then `f` grows faster than `C * x`, i.e., `C * (y - x) < f y - f x` whenever `x < y`. -/ theorem mul_sub_lt_image_sub_of_lt_deriv {f : ℝ → ℝ} (hf : differentiable ℝ f) {C} (hf'_gt : ∀ x, C < deriv f x) ⦃x y⦄ (hxy : x < y) : C * (y - x) < f y - f x := convex_univ.mul_sub_lt_image_sub_of_lt_deriv hf.continuous.continuous_on hf.differentiable_on (λ x _, hf'_gt x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `C ≤ f'`, then `f` grows at least as fast as `C * x` on `D`, i.e., `C * (y - x) ≤ f y - f x` whenever `x, y ∈ D`, `x ≤ y`. -/ theorem convex.mul_sub_le_image_sub_of_le_deriv {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (hf'_ge : ∀ x ∈ interior D, C ≤ deriv f x) : ∀ x y ∈ D, x ≤ y → C * (y - x) ≤ f y - f x := begin assume x y hx hy hxy, cases eq_or_lt_of_le hxy with hxy' hxy', by rw [hxy', sub_self, sub_self, mul_zero], have hxyD : Icc x y ⊆ D, from hD.ord_connected.out hx hy, have hxyD' : Ioo x y ⊆ interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩, obtain ⟨a, a_mem, ha⟩ : ∃ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x), from exists_deriv_eq_slope f hxy' (hf.mono hxyD) (hf'.mono hxyD'), have : C ≤ (f y - f x) / (y - x), by { rw [← ha], exact hf'_ge _ (hxyD' a_mem) }, exact (le_div_iff (sub_pos.2 hxy')).1 this end /-- Let `f : ℝ → ℝ` be a differentiable function. If `C ≤ f'`, then `f` grows at least as fast as `C * x`, i.e., `C * (y - x) ≤ f y - f x` whenever `x ≤ y`. -/ theorem mul_sub_le_image_sub_of_le_deriv {f : ℝ → ℝ} (hf : differentiable ℝ f) {C} (hf'_ge : ∀ x, C ≤ deriv f x) ⦃x y⦄ (hxy : x ≤ y) : C * (y - x) ≤ f y - f x := convex_univ.mul_sub_le_image_sub_of_le_deriv hf.continuous.continuous_on hf.differentiable_on (λ x _, hf'_ge x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f' < C`, then `f` grows slower than `C * x` on `D`, i.e., `f y - f x < C * (y - x)` whenever `x, y ∈ D`, `x < y`. -/ theorem convex.image_sub_lt_mul_sub_of_deriv_lt {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (lt_hf' : ∀ x ∈ interior D, deriv f x < C) : ∀ x y ∈ D, x < y → f y - f x < C * (y - x) := begin assume x y hx hy hxy, have hf'_gt : ∀ x ∈ interior D, -C < deriv (λ y, -f y) x, { assume x hx, rw [deriv.neg, neg_lt_neg_iff], exact lt_hf' x hx }, simpa [-neg_lt_neg_iff] using neg_lt_neg (hD.mul_sub_lt_image_sub_of_lt_deriv hf.neg hf'.neg hf'_gt x y hx hy hxy) end /-- Let `f : ℝ → ℝ` be a differentiable function. If `f' < C`, then `f` grows slower than `C * x` on `D`, i.e., `f y - f x < C * (y - x)` whenever `x < y`. -/ theorem image_sub_lt_mul_sub_of_deriv_lt {f : ℝ → ℝ} (hf : differentiable ℝ f) {C} (lt_hf' : ∀ x, deriv f x < C) ⦃x y⦄ (hxy : x < y) : f y - f x < C * (y - x) := convex_univ.image_sub_lt_mul_sub_of_deriv_lt hf.continuous.continuous_on hf.differentiable_on (λ x _, lt_hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f' ≤ C`, then `f` grows at most as fast as `C * x` on `D`, i.e., `f y - f x ≤ C * (y - x)` whenever `x, y ∈ D`, `x ≤ y`. -/ theorem convex.image_sub_le_mul_sub_of_deriv_le {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (le_hf' : ∀ x ∈ interior D, deriv f x ≤ C) : ∀ x y ∈ D, x ≤ y → f y - f x ≤ C * (y - x) := begin assume x y hx hy hxy, have hf'_ge : ∀ x ∈ interior D, -C ≤ deriv (λ y, -f y) x, { assume x hx, rw [deriv.neg, neg_le_neg_iff], exact le_hf' x hx }, simpa [-neg_le_neg_iff] using neg_le_neg (hD.mul_sub_le_image_sub_of_le_deriv hf.neg hf'.neg hf'_ge x y hx hy hxy) end /-- Let `f : ℝ → ℝ` be a differentiable function. If `f' ≤ C`, then `f` grows at most as fast as `C * x`, i.e., `f y - f x ≤ C * (y - x)` whenever `x ≤ y`. -/ theorem image_sub_le_mul_sub_of_deriv_le {f : ℝ → ℝ} (hf : differentiable ℝ f) {C} (le_hf' : ∀ x, deriv f x ≤ C) ⦃x y⦄ (hxy : x ≤ y) : f y - f x ≤ C * (y - x) := convex_univ.image_sub_le_mul_sub_of_deriv_le hf.continuous.continuous_on hf.differentiable_on (λ x _, le_hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is positive, then `f` is a strictly monotonically increasing function on `D`. -/ theorem convex.strict_mono_of_deriv_pos {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_pos : ∀ x ∈ interior D, 0 < deriv f x) : ∀ x y ∈ D, x < y → f x < f y := by simpa only [zero_mul, sub_pos] using hD.mul_sub_lt_image_sub_of_lt_deriv hf hf' hf'_pos /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is positive, then `f` is a strictly monotonically increasing function. -/ theorem strict_mono_of_deriv_pos {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf'_pos : ∀ x, 0 < deriv f x) : strict_mono f := λ x y hxy, convex_univ.strict_mono_of_deriv_pos hf.continuous.continuous_on hf.differentiable_on (λ x _, hf'_pos x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is nonnegative, then `f` is a monotonically increasing function on `D`. -/ theorem convex.mono_of_deriv_nonneg {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_nonneg : ∀ x ∈ interior D, 0 ≤ deriv f x) : ∀ x y ∈ D, x ≤ y → f x ≤ f y := by simpa only [zero_mul, sub_nonneg] using hD.mul_sub_le_image_sub_of_le_deriv hf hf' hf'_nonneg /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is nonnegative, then `f` is a monotonically increasing function. -/ theorem mono_of_deriv_nonneg {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf' : ∀ x, 0 ≤ deriv f x) : monotone f := λ x y hxy, convex_univ.mono_of_deriv_nonneg hf.continuous.continuous_on hf.differentiable_on (λ x _, hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is negative, then `f` is a strictly monotonically decreasing function on `D`. -/ theorem convex.strict_antimono_of_deriv_neg {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_neg : ∀ x ∈ interior D, deriv f x < 0) : ∀ x y ∈ D, x < y → f y < f x := by simpa only [zero_mul, sub_lt_zero] using hD.image_sub_lt_mul_sub_of_deriv_lt hf hf' hf'_neg /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is negative, then `f` is a strictly monotonically decreasing function. -/ theorem strict_antimono_of_deriv_neg {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf' : ∀ x, deriv f x < 0) : ∀ ⦃x y⦄, x < y → f y < f x := λ x y hxy, convex_univ.strict_antimono_of_deriv_neg hf.continuous.continuous_on hf.differentiable_on (λ x _, hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is nonpositive, then `f` is a monotonically decreasing function on `D`. -/ theorem convex.antimono_of_deriv_nonpos {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_nonpos : ∀ x ∈ interior D, deriv f x ≤ 0) : ∀ x y ∈ D, x ≤ y → f y ≤ f x := by simpa only [zero_mul, sub_nonpos] using hD.image_sub_le_mul_sub_of_deriv_le hf hf' hf'_nonpos /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is nonpositive, then `f` is a monotonically decreasing function. -/ theorem antimono_of_deriv_nonpos {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf' : ∀ x, deriv f x ≤ 0) : ∀ ⦃x y⦄, x ≤ y → f y ≤ f x := λ x y hxy, convex_univ.antimono_of_deriv_nonpos hf.continuous.continuous_on hf.differentiable_on (λ x _, hf' x) x y trivial trivial hxy /-- If a function `f` is continuous on a convex set `D ⊆ ℝ`, is differentiable on its interior, and `f'` is monotone on the interior, then `f` is convex on `D`. -/ theorem convex_on_of_deriv_mono {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_mono : ∀ x y ∈ interior D, x ≤ y → deriv f x ≤ deriv f y) : convex_on D f := convex_on_real_of_slope_mono_adjacent hD begin intros x y z hx hz hxy hyz, -- First we prove some trivial inclusions have hxzD : Icc x z ⊆ D, from hD.ord_connected.out hx hz, have hxyD : Icc x y ⊆ D, from subset.trans (Icc_subset_Icc_right $ le_of_lt hyz) hxzD, have hxyD' : Ioo x y ⊆ interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩, have hyzD : Icc y z ⊆ D, from subset.trans (Icc_subset_Icc_left $ le_of_lt hxy) hxzD, have hyzD' : Ioo y z ⊆ interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hyzD⟩, -- Then we apply MVT to both `[x, y]` and `[y, z]` obtain ⟨a, ⟨hxa, hay⟩, ha⟩ : ∃ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x), from exists_deriv_eq_slope f hxy (hf.mono hxyD) (hf'.mono hxyD'), obtain ⟨b, ⟨hyb, hbz⟩, hb⟩ : ∃ b ∈ Ioo y z, deriv f b = (f z - f y) / (z - y), from exists_deriv_eq_slope f hyz (hf.mono hyzD) (hf'.mono hyzD'), rw [← ha, ← hb], exact hf'_mono a b (hxyD' ⟨hxa, hay⟩) (hyzD' ⟨hyb, hbz⟩) (le_of_lt $ lt_trans hay hyb) end /-- If a function `f` is continuous on a convex set `D ⊆ ℝ`, is differentiable on its interior, and `f'` is antimonotone on the interior, then `f` is concave on `D`. -/ theorem concave_on_of_deriv_antimono {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_mono : ∀ x y ∈ interior D, x ≤ y → deriv f y ≤ deriv f x) : concave_on D f := begin have : ∀ x y ∈ interior D, x ≤ y → deriv (-f) x ≤ deriv (-f) y, { intros x y hx hy hxy, convert neg_le_neg (hf'_mono x y hx hy hxy); convert deriv.neg }, exact (neg_convex_on_iff D f).mp (convex_on_of_deriv_mono hD hf.neg hf'.neg this), end /-- If a function `f` is differentiable and `f'` is monotone on `ℝ` then `f` is convex. -/ theorem convex_on_univ_of_deriv_mono {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf'_mono : monotone (deriv f)) : convex_on univ f := convex_on_of_deriv_mono convex_univ hf.continuous.continuous_on hf.differentiable_on (λ x y _ _ h, hf'_mono h) /-- If a function `f` is differentiable and `f'` is antimonotone on `ℝ` then `f` is concave. -/ theorem concave_on_univ_of_deriv_antimono {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf'_antimono : ∀⦃a b⦄, a ≤ b → (deriv f) b ≤ (deriv f) a) : concave_on univ f := concave_on_of_deriv_antimono convex_univ hf.continuous.continuous_on hf.differentiable_on (λ x y _ _ h, hf'_antimono h) /-- If a function `f` is continuous on a convex set `D ⊆ ℝ`, is twice differentiable on its interior, and `f''` is nonnegative on the interior, then `f` is convex on `D`. -/ theorem convex_on_of_deriv2_nonneg {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'' : differentiable_on ℝ (deriv f) (interior D)) (hf''_nonneg : ∀ x ∈ interior D, 0 ≤ (deriv^[2] f x)) : convex_on D f := convex_on_of_deriv_mono hD hf hf' $ assume x y hx hy hxy, hD.interior.mono_of_deriv_nonneg hf''.continuous_on (by rwa [interior_interior]) (by rwa [interior_interior]) _ _ hx hy hxy /-- If a function `f` is continuous on a convex set `D ⊆ ℝ`, is twice differentiable on its interior, and `f''` is nonpositive on the interior, then `f` is concave on `D`. -/ theorem concave_on_of_deriv2_nonpos {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'' : differentiable_on ℝ (deriv f) (interior D)) (hf''_nonpos : ∀ x ∈ interior D, (deriv^[2] f x) ≤ 0) : concave_on D f := concave_on_of_deriv_antimono hD hf hf' $ assume x y hx hy hxy, hD.interior.antimono_of_deriv_nonpos hf''.continuous_on (by rwa [interior_interior]) (by rwa [interior_interior]) _ _ hx hy hxy /-- If a function `f` is twice differentiable on a open convex set `D ⊆ ℝ` and `f''` is nonnegative on `D`, then `f` is convex on `D`. -/ theorem convex_on_open_of_deriv2_nonneg {D : set ℝ} (hD : convex D) (hD₂ : is_open D) {f : ℝ → ℝ} (hf' : differentiable_on ℝ f D) (hf'' : differentiable_on ℝ (deriv f) D) (hf''_nonneg : ∀ x ∈ D, 0 ≤ (deriv^[2] f x)) : convex_on D f := convex_on_of_deriv2_nonneg hD hf'.continuous_on (by simpa [hD₂.interior_eq] using hf') (by simpa [hD₂.interior_eq] using hf'') (by simpa [hD₂.interior_eq] using hf''_nonneg) /-- If a function `f` is twice differentiable on a open convex set `D ⊆ ℝ` and `f''` is nonpositive on `D`, then `f` is concave on `D`. -/ theorem concave_on_open_of_deriv2_nonpos {D : set ℝ} (hD : convex D) (hD₂ : is_open D) {f : ℝ → ℝ} (hf' : differentiable_on ℝ f D) (hf'' : differentiable_on ℝ (deriv f) D) (hf''_nonpos : ∀ x ∈ D, (deriv^[2] f x) ≤ 0) : concave_on D f := concave_on_of_deriv2_nonpos hD hf'.continuous_on (by simpa [hD₂.interior_eq] using hf') (by simpa [hD₂.interior_eq] using hf'') (by simpa [hD₂.interior_eq] using hf''_nonpos) /-- If a function `f` is twice differentiable on `ℝ`, and `f''` is nonnegative on `ℝ`, then `f` is convex on `ℝ`. -/ theorem convex_on_univ_of_deriv2_nonneg {f : ℝ → ℝ} (hf' : differentiable ℝ f) (hf'' : differentiable ℝ (deriv f)) (hf''_nonneg : ∀ x, 0 ≤ (deriv^[2] f x)) : convex_on univ f := convex_on_open_of_deriv2_nonneg convex_univ is_open_univ hf'.differentiable_on hf''.differentiable_on (λ x _, hf''_nonneg x) /-- If a function `f` is twice differentiable on `ℝ`, and `f''` is nonpositive on `ℝ`, then `f` is concave on `ℝ`. -/ theorem concave_on_univ_of_deriv2_nonpos {f : ℝ → ℝ} (hf' : differentiable ℝ f) (hf'' : differentiable ℝ (deriv f)) (hf''_nonpos : ∀ x, (deriv^[2] f x) ≤ 0) : concave_on univ f := concave_on_open_of_deriv2_nonpos convex_univ is_open_univ hf'.differentiable_on hf''.differentiable_on (λ x _, hf''_nonpos x) /-! ### Functions `f : E → ℝ` -/ /-- Lagrange's Mean Value Theorem, applied to convex domains. -/ theorem domain_mvt {f : E → ℝ} {s : set E} {x y : E} {f' : E → (E →L[ℝ] ℝ)} (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∃ z ∈ segment x y, f y - f x = f' z (y - x) := begin have hIccIoo := @Ioo_subset_Icc_self ℝ _ 0 1, -- parametrize segment set g : ℝ → E := λ t, x + t • (y - x), have hseg : ∀ t ∈ Icc (0:ℝ) 1, g t ∈ segment x y, { rw segment_eq_image', simp only [mem_image, and_imp, add_right_inj], intros t ht, exact ⟨t, ht, rfl⟩ }, have hseg' : Icc 0 1 ⊆ g ⁻¹' s, { rw ← image_subset_iff, unfold image, change ∀ _, _, intros z Hz, rw mem_set_of_eq at Hz, rcases Hz with ⟨t, Ht, hgt⟩, rw ← hgt, exact hs.segment_subset xs ys (hseg t Ht) }, -- derivative of pullback of f under parametrization have hfg: ∀ t ∈ Icc (0:ℝ) 1, has_deriv_within_at (f ∘ g) ((f' (g t) : E → ℝ) (y-x)) (Icc (0:ℝ) 1) t, { intros t Ht, have hg : has_deriv_at g (y-x) t, { have := ((has_deriv_at_id t).smul_const (y - x)).const_add x, rwa one_smul at this }, exact (hf (g t) $ hseg' Ht).comp_has_deriv_within_at _ hg.has_deriv_within_at hseg' }, -- apply 1-variable mean value theorem to pullback have hMVT : ∃ (t ∈ Ioo (0:ℝ) 1), ((f' (g t) : E → ℝ) (y-x)) = (f (g 1) - f (g 0)) / (1 - 0), { refine exists_has_deriv_at_eq_slope (f ∘ g) _ (by norm_num) _ _, { unfold continuous_on, exact λ t Ht, (hfg t Ht).continuous_within_at }, { refine λ t Ht, (hfg t $ hIccIoo Ht).has_deriv_at _, refine _root_.mem_nhds_iff.mpr _, use (Ioo (0:ℝ) 1), refine ⟨hIccIoo, _, Ht⟩, simp [real.Ioo_eq_ball, is_open_ball] } }, -- reinterpret on domain rcases hMVT with ⟨t, Ht, hMVT'⟩, use g t, refine ⟨hseg t $ hIccIoo Ht, _⟩, simp [g, hMVT'], end section is_R_or_C /-! ### Vector-valued functions `f : E → F`. Strict differentiability. A `C^1` function is strictly differentiable, when the field is `ℝ` or `ℂ`. This follows from the mean value inequality on balls, which is a particular case of the above results after restricting the scalars to `ℝ`. Note that it does not make sense to talk of a convex set over `ℂ`, but balls make sense and are enough. Many formulations of the mean value inequality could be generalized to balls over `ℝ` or `ℂ`. For now, we only include the ones that we need. -/ variables {𝕜 : Type*} [is_R_or_C 𝕜] {G : Type*} [normed_group G] [normed_space 𝕜 G] {H : Type*} [normed_group H] [normed_space 𝕜 H] {f : G → H} {f' : G → G →L[𝕜] H} {x : G} /-- Over the reals or the complexes, a continuously differentiable function is strictly differentiable. -/ lemma has_strict_fderiv_at_of_has_fderiv_at_of_continuous_at (hder : ∀ᶠ y in 𝓝 x, has_fderiv_at f (f' y) y) (hcont : continuous_at f' x) : has_strict_fderiv_at f (f' x) x := begin -- turn little-o definition of strict_fderiv into an epsilon-delta statement refine is_o_iff.mpr (λ c hc, metric.eventually_nhds_iff_ball.mpr _), -- the correct ε is the modulus of continuity of f' rcases metric.mem_nhds_iff.mp (inter_mem_sets hder (hcont $ ball_mem_nhds _ hc)) with ⟨ε, ε0, hε⟩, refine ⟨ε, ε0, _⟩, -- simplify formulas involving the product E × E rintros ⟨a, b⟩ h, rw [← ball_prod_same, prod_mk_mem_set_prod_eq] at h, -- exploit the choice of ε as the modulus of continuity of f' have hf' : ∀ x' ∈ ball x ε, ∥f' x' - f' x∥ ≤ c, { intros x' H', rw ← dist_eq_norm, exact le_of_lt (hε H').2 }, -- apply mean value theorem letI : normed_space ℝ G := restrict_scalars.normed_space ℝ 𝕜 G, letI : is_scalar_tower ℝ 𝕜 G := restrict_scalars.is_scalar_tower _ _ _, refine (convex_ball _ _).norm_image_sub_le_of_norm_has_fderiv_within_le' _ hf' h.2 h.1, exact λ y hy, (hε hy).1.has_fderiv_within_at end /-- Over the reals or the complexes, a continuously differentiable function is strictly differentiable. -/ lemma has_strict_deriv_at_of_has_deriv_at_of_continuous_at {f f' : 𝕜 → G} {x : 𝕜} (hder : ∀ᶠ y in 𝓝 x, has_deriv_at f (f' y) y) (hcont : continuous_at f' x) : has_strict_deriv_at f (f' x) x := has_strict_fderiv_at_of_has_fderiv_at_of_continuous_at (hder.mono (λ y hy, hy.has_fderiv_at)) $ (smul_rightL 𝕜 _ _ 1).continuous.continuous_at.comp hcont end is_R_or_C
cfc5f6121ad18fea0fd0df457363657bbf822df3
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/data/set/disjointed.lean
0b2213dad172ebfad0c92f33e828bcbbf3bf8264
[ "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
4,390
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 Disjointed sets -/ import data.set.lattice import tactic.wlog open set classical open_locale classical universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {s t u : set α} /-- A relation `p` holds pairwise if `p i j` for all `i ≠ j`. -/ def pairwise {α : Type*} (p : α → α → Prop) := ∀i j, i ≠ j → p i j theorem set.pairwise_on_univ {r : α → α → Prop} : (univ : set α).pairwise_on r ↔ pairwise r := by simp only [pairwise_on, pairwise, mem_univ, forall_const] theorem set.pairwise_on.on_injective {s : set α} {r : α → α → Prop} (hs : pairwise_on s r) {f : β → α} (hf : function.injective f) (hfs : ∀ x, f x ∈ s) : pairwise (r on f) := λ i j hij, hs _ (hfs i) _ (hfs j) (hf.ne hij) theorem pairwise_on_bool {r} (hr : symmetric r) {a b : α} : pairwise (r on (λ c, cond c a b)) ↔ r a b := by simp [pairwise, bool.forall_bool, function.on_fun]; exact and_iff_right_of_imp (@hr _ _) theorem pairwise_disjoint_on_bool [semilattice_inf_bot α] {a b : α} : pairwise (disjoint on (λ c, cond c a b)) ↔ disjoint a b := pairwise_on_bool $ λ _ _, disjoint.symm theorem pairwise.pairwise_on {p : α → α → Prop} (h : pairwise p) (s : set α) : s.pairwise_on p := λ x hx y hy, h x y theorem pairwise_disjoint_fiber (f : α → β) : pairwise (disjoint on (λ y : β, f ⁻¹' {y})) := set.pairwise_on_univ.1 $ pairwise_on_disjoint_fiber f univ namespace set /-- If `f : ℕ → set α` is a sequence of sets, then `disjointed f` is the sequence formed with each set subtracted from the later ones in the sequence, to form a disjoint sequence. -/ def disjointed (f : ℕ → set α) (n : ℕ) : set α := f n ∩ (⋂i<n, (f i)ᶜ) lemma disjoint_disjointed {f : ℕ → set α} : pairwise (disjoint on disjointed f) := λ i j h, begin wlog h' : i ≤ j; [skip, {revert a, exact (this h.symm).symm}], rintro a ⟨⟨h₁, _⟩, h₂, h₃⟩, simp at h₃, exact h₃ _ (lt_of_le_of_ne h' h) h₁ end lemma disjoint_disjointed' {f : ℕ → set α} : ∀ i j, i ≠ j → (disjointed f i) ∩ (disjointed f j) = ∅ := λ i j hij, disjoint_iff.1 $ disjoint_disjointed i j hij lemma disjointed_subset {f : ℕ → set α} {n : ℕ} : disjointed f n ⊆ f n := inter_subset_left _ _ lemma Union_lt_succ {f : ℕ → set α} {n} : (⋃i < nat.succ n, f i) = f n ∪ (⋃i < n, f i) := ext $ λ a, by simp [nat.lt_succ_iff_lt_or_eq, or_and_distrib_right, exists_or_distrib, or_comm] lemma Inter_lt_succ {f : ℕ → set α} {n} : (⋂i < nat.succ n, f i) = f n ∩ (⋂i < n, f i) := ext $ λ a, by simp [nat.lt_succ_iff_lt_or_eq, or_imp_distrib, forall_and_distrib, and_comm] lemma Union_disjointed {f : ℕ → set α} : (⋃n, disjointed f n) = (⋃n, f n) := subset.antisymm (Union_subset_Union $ assume i, inter_subset_left _ _) $ assume x h, have ∃n, x ∈ f n, from mem_Union.mp h, have hn : ∀ (i : ℕ), i < nat.find this → x ∉ f i, from assume i, nat.find_min this, mem_Union.mpr ⟨nat.find this, nat.find_spec this, by simp; assumption⟩ lemma disjointed_induct {f : ℕ → set α} {n : ℕ} {p : set α → Prop} (h₁ : p (f n)) (h₂ : ∀t i, p t → p (t \ f i)) : p (disjointed f n) := begin rw disjointed, generalize_hyp : f n = t at h₁ ⊢, induction n, case nat.zero { simp [nat.not_lt_zero, h₁] }, case nat.succ : n ih { rw [Inter_lt_succ, inter_comm ((f n)ᶜ), ← inter_assoc], exact h₂ _ n ih } end lemma disjointed_of_mono {f : ℕ → set α} {n : ℕ} (hf : monotone f) : disjointed f (n + 1) = f (n + 1) \ f n := have (⋂i (h : i < n + 1), (f i)ᶜ) = (f n)ᶜ, from le_antisymm (infi_le_of_le n $ infi_le_of_le (nat.lt_succ_self _) $ subset.refl _) (le_infi $ assume i, le_infi $ assume hi, compl_le_compl $ hf $ nat.le_of_succ_le_succ hi), by simp [disjointed, this, diff_eq] lemma Union_disjointed_of_mono {f : ℕ → set α} (hf : monotone f) : ∀ n : ℕ, (⋃i<n.succ, disjointed f i) = f n | 0 := by rw [Union_lt_succ]; simp [disjointed, nat.not_lt_zero] | (n+1) := by rw [Union_lt_succ, disjointed_of_mono hf, Union_disjointed_of_mono n, diff_union_self, union_eq_self_of_subset_right (hf (nat.le_succ _))] end set
12f3dee776865814309286ca595f20502dcab333
5f83eb0c32f15aeed5993a3ad5ededb6f31fe7aa
/lean/src/bv/basic.lean
19a2ae4055a400fbb82cfcbf259330096efcf7b4
[]
no_license
uw-unsat/jitterbug
45b54979b156c0f5330012313052f8594abd6f14
78d1e75ad506498b585fbac66985ff9d9d05952d
refs/heads/master
1,689,066,921,433
1,687,061,448,000
1,688,415,161,000
244,440,882
46
5
null
null
null
null
UTF-8
Lean
false
false
6,640
lean
import data.fin.tuple.basic import data.fintype.pi import tactic.omega.main /-! # Fixed-size bitvectors This file defines fixed-sized bitvectors following the SMT-LIB standard. ## References * http://smtlib.cs.uiowa.edu/Theories/FixedSizeBitVectors.smt2 * http://smtlib.cs.uiowa.edu/Logics/QF_BV.smt2 -/ /-- The type of bitvectors. -/ def bv (n : ℕ) := fin n → bool namespace bv section fin variable {n : ℕ} def nil : bv 0 := fin.elim0 def cons (b : bool) (v : bv n) : bv (n + 1) := fin.cons b v def snoc (v : bv n) (b : bool) : bv (n + 1) := fin.snoc v b def init (v : bv (n + 1)) : bv n := fin.init v def tail (v : bv (n + 1)) : bv n := fin.tail v @[reducible] def lsb (v : bv (n + 1)) : bool := v 0 def msb (v : bv (n + 1)) : bool := v (fin.last n) instance : decidable_eq (bv n) := fintype.decidable_pi_fintype instance : fintype (bv n) := pi.fintype instance : inhabited (bv n) := pi.inhabited _ end fin section list variable {n : ℕ} def to_list {n : ℕ} (v : bv n) : list bool := list.of_fn v instance bv_to_list : has_coe (bv n) (list bool) := ⟨to_list⟩ end list section nat variable {n : ℕ} def of_nat : Π {n : ℕ}, ℕ → bv n | 0 _ := nil | (_ + 1) a := cons a.bodd (of_nat a.div2) def to_nat : Π {n : ℕ}, bv n → ℕ | 0 _ := 0 | (_ + 1) v := nat.bit v.lsb v.tail.to_nat instance bv_to_nat : has_coe (bv n) ℕ := ⟨to_nat⟩ def of_int (a : ℤ) : bv n := bv.of_nat (a % (2^n)).to_nat def to_int (v : bv n) : ℤ := if (v : ℕ) < 2^(n - 1) then (v : ℕ) else (v : ℕ) - 2^n end nat section literals variable {n : ℕ} protected def zero : bv n := λ _, ff instance : has_zero (bv n) := ⟨bv.zero⟩ protected def umax : bv n := λ _, tt protected def one : Π {n : ℕ}, bv n | 0 := nil | (n + 1) := cons tt 0 instance : has_one (bv n) := ⟨bv.one⟩ protected def smin : Π {n : ℕ}, bv n | 0 := nil | (n + 1) := bv.zero.snoc tt protected def smax : Π {n : ℕ}, bv n | 0 := nil | (n + 1) := bv.umax.snoc ff end literals section concatenation def concat {n₁ n₂ : ℕ} (v₁ : bv n₁) (v₂ : bv n₂) : bv (n₁ + n₂) := λ ⟨i, h₁⟩, if h₂ : i < n₂ then v₂ ⟨i, h₂⟩ else v₁ ⟨i - n₂, by omega⟩ def zero_extend {n : ℕ} (i : ℕ) (v : bv n) : bv (i + n) := concat 0 v def sign_extend {n : ℕ} (i : ℕ) (v : bv (n + 1)) : bv (i + (n + 1)) := concat (λ _, v.msb) v end concatenation section extraction def extract {n : ℕ} (i j : ℕ) (h₁ : i < n) (h₂ : j ≤ i) (v : bv n) : bv (i - j + 1) := λ ⟨x, h⟩, v ⟨j + x, by omega⟩ def drop {n₁ : ℕ} (n₂ : ℕ) (v : bv (n₁ + n₂)) : bv n₁ := λ ⟨x, h⟩, v ⟨n₂ + x, by omega⟩ def take {n₁ : ℕ} (n₂ : ℕ) (v : bv (n₁ + n₂)) : bv n₂ := λ ⟨x, h⟩, v ⟨x, by omega⟩ end extraction section bitwise variable {n : ℕ} def map (f : bool → bool) (v : bv n) : bv n := λ i, f (v i) def map₂ (f : bool → bool → bool) (v₁ v₂ : bv n) : bv n := λ i, f (v₁ i) (v₂ i) protected def not : bv n → bv n := map bnot protected def and : bv n → bv n → bv n := map₂ band protected def or : bv n → bv n → bv n := map₂ bor protected def xor : bv n → bv n → bv n := map₂ bxor end bitwise section arithmetic variable {n : ℕ} protected def neg (v : bv n) : bv n := of_nat (2^n - (v : ℕ)) instance : has_neg (bv n) := ⟨bv.neg⟩ protected def add (v₁ v₂ : bv n) : bv n := of_nat ((v₁ : ℕ) + (v₂ : ℕ)) instance : has_add (bv n) := ⟨bv.add⟩ protected def sub (v₁ v₂ : bv n) : bv n := v₁ + (-v₂) instance : has_sub (bv n) := ⟨bv.sub⟩ protected def mul (v₁ v₂ : bv n) : bv n := of_nat ((v₁ : ℕ) * (v₂ : ℕ)) instance : has_mul (bv n) := ⟨bv.mul⟩ protected def udiv (v₁ v₂ : bv n) : bv n := if (v₂ : ℕ) = 0 then bv.umax else of_nat ((v₁ : ℕ) / (v₂ : ℕ)) instance : has_div (bv n) := ⟨bv.udiv⟩ protected def urem (v₁ v₂ : bv n) : bv n := if (v₂ : ℕ) = 0 then v₁ else of_nat ((v₁ : ℕ) % (v₂ : ℕ)) instance : has_mod (bv n) := ⟨bv.urem⟩ end arithmetic section shift variable {n : ℕ} def shl (v₁ v₂ : bv n) : bv n := of_nat ((v₁ : ℕ) * 2^(v₂ : ℕ)) def lshr (v₁ v₂ : bv n) : bv n := of_nat ((v₁ : ℕ) / 2^(v₂ : ℕ)) def ashr (v₁ v₂ : bv (n + 1)) : bv (n + 1) := match v₁.msb with | ff := v₁.lshr v₂ | tt := (v₁.not.lshr v₂).not end end shift section comparison variable {n : ℕ} protected def ult (v₁ v₂ : bv n) : Prop := (v₁ : ℕ) < (v₂ : ℕ) instance : has_lt (bv n) := ⟨bv.ult⟩ protected def ule (v₁ v₂ : bv n) : Prop := v₁ < v₂ ∨ v₁ = v₂ instance : has_le (bv n) := ⟨bv.ule⟩ @[reducible] protected def ugt (v₁ v₂ : bv n) : Prop := v₂ < v₁ @[reducible] protected def uge (v₁ v₂ : bv n) : Prop := v₂ < v₁ ∨ v₁ = v₂ instance decidable_ult : @decidable_rel (bv n) bv.ult := λ _ _, by dsimp [bv.ult]; apply_instance instance decidable_ule : @decidable_rel (bv n) bv.ule := λ _ _, by dsimp [bv.ule]; apply_instance protected def slt (v₁ v₂ : bv (n + 1)) : Prop := (v₁.msb = tt ∧ v₂.msb = ff) ∨ (v₁.msb = v₂.msb ∧ v₁ < v₂) protected def sle (v₁ v₂ : bv (n + 1)) : Prop := (v₁.msb = tt ∧ v₂.msb = ff) ∨ (v₁.msb = v₂.msb ∧ v₁ ≤ v₂) @[reducible] protected def sgt (v₁ v₂ : bv (n + 1)) : Prop := v₂.slt v₁ @[reducible] protected def sge (v₁ v₂ : bv (n + 1)) : Prop := v₂.sle v₁ instance decidable_slt : @decidable_rel (bv (n + 1)) bv.slt := λ _ _, by dsimp [bv.slt]; apply_instance instance decidable_sle : @decidable_rel (bv (n + 1)) bv.sle := λ _ _, by dsimp [bv.sle]; apply_instance end comparison private def bin_repr : Π {n : ℕ}, bv n → string | 0 _ := "" | (n + 1) v := bin_repr v.tail ++ cond v.lsb "1" "0" private def hex_repr : Π {n : ℕ} (h : 4 ∣ n), bv n → string | 0 _ _ := "" | 1 h _ := absurd h dec_trivial | 2 h _ := absurd h dec_trivial | 3 h _ := absurd h dec_trivial | (n + 4) h v := hex_repr (nat.dvd_add_self_right.mp h) (v.drop 4) ++ [(v.take 4 : ℕ).digit_char].as_string private def repr {n : ℕ} (v : bv n) : string := if h : 4 ∣ n then "#x" ++ hex_repr h v else "#b" ++ bin_repr v instance {n : ℕ} : has_repr (bv n) := ⟨repr⟩ end bv
0e6d7c355e206f1912e2fc822e368c1c706ead8a
4727251e0cd73359b15b664c3170e5d754078599
/src/number_theory/class_number/admissible_absolute_value.lean
9ba9b4345a66b9504bad8c192c809756a9e2c85f
[ "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,776
lean
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import data.fin.tuple import data.real.basic import combinatorics.pigeonhole import algebra.order.euclidean_absolute_value /-! # Admissible absolute values This file defines a structure `absolute_value.is_admissible` which we use to show the class number of the ring of integers of a global field is finite. ## Main definitions * `absolute_value.is_admissible abv` states the absolute value `abv : R → ℤ` respects the Euclidean domain structure on `R`, and that a large enough set of elements of `R^n` contains a pair of elements whose remainders are pointwise close together. ## Main results * `absolute_value.abs_is_admissible` shows the "standard" absolute value on `ℤ`, mapping negative `x` to `-x`, is admissible. * `polynomial.card_pow_degree_is_admissible` shows `card_pow_degree`, mapping `p : polynomial 𝔽_q` to `q ^ degree p`, is admissible -/ local infix ` ≺ `:50 := euclidean_domain.r namespace absolute_value variables {R : Type*} [euclidean_domain R] variables (abv : absolute_value R ℤ) /-- An absolute value `R → ℤ` is admissible if it respects the Euclidean domain structure and a large enough set of elements in `R^n` will contain a pair of elements whose remainders are pointwise close together. -/ structure is_admissible extends is_euclidean abv := (card : ℝ → ℕ) (exists_partition' : ∀ (n : ℕ) {ε : ℝ} (hε : 0 < ε) {b : R} (hb : b ≠ 0) (A : fin n → R), ∃ (t : fin n → fin (card ε)), ∀ i₀ i₁, t i₀ = t i₁ → (abv (A i₁ % b - A i₀ % b) : ℝ) < abv b • ε) attribute [protected] is_admissible.card namespace is_admissible variables {abv} /-- For all `ε > 0` and finite families `A`, we can partition the remainders of `A` mod `b` into `abv.card ε` sets, such that all elements in each part of remainders are close together. -/ lemma exists_partition {ι : Type*} [fintype ι] {ε : ℝ} (hε : 0 < ε) {b : R} (hb : b ≠ 0) (A : ι → R) (h : abv.is_admissible) : ∃ (t : ι → fin (h.card ε)), ∀ i₀ i₁, t i₀ = t i₁ → (abv (A i₁ % b - A i₀ % b) : ℝ) < abv b • ε := begin let e := fintype.equiv_fin ι, obtain ⟨t, ht⟩ := h.exists_partition' (fintype.card ι) hε hb (A ∘ e.symm), refine ⟨t ∘ e, λ i₀ i₁ h, _⟩, convert ht (e i₀) (e i₁) h; simp only [e.symm_apply_apply] end /-- Any large enough family of vectors in `R^n` has a pair of elements whose remainders are close together, pointwise. -/ lemma exists_approx_aux (n : ℕ) (h : abv.is_admissible) : ∀ {ε : ℝ} (hε : 0 < ε) {b : R} (hb : b ≠ 0) (A : fin (h.card ε ^ n).succ → (fin n → R)), ∃ (i₀ i₁), (i₀ ≠ i₁) ∧ ∀ k, (abv (A i₁ k % b - A i₀ k % b) : ℝ) < abv b • ε := begin haveI := classical.dec_eq R, induction n with n ih, { intros ε hε b hb A, refine ⟨0, 1, _, _⟩, { simp }, rintros ⟨i, ⟨⟩⟩ }, intros ε hε b hb A, set M := h.card ε with hM, -- By the "nicer" pigeonhole principle, we can find a collection `s` -- of more than `M^n` remainders where the first components lie close together: obtain ⟨s, s_inj, hs⟩ : ∃ s : fin (M ^ n).succ → fin (M ^ n.succ).succ, function.injective s ∧ ∀ i₀ i₁, (abv (A (s i₁) 0 % b - A (s i₀) 0 % b) : ℝ) < abv b • ε, { -- We can partition the `A`s into `M` subsets where -- the first components lie close together: obtain ⟨t, ht⟩ : ∃ (t : fin (M ^ n.succ).succ → fin M), ∀ i₀ i₁, t i₀ = t i₁ → (abv (A i₁ 0 % b - A i₀ 0 % b) : ℝ) < abv b • ε := h.exists_partition hε hb (λ x, A x 0), -- Since the `M` subsets contain more than `M * M^n` elements total, -- there must be a subset that contains more than `M^n` elements. obtain ⟨s, hs⟩ := @fintype.exists_lt_card_fiber_of_mul_lt_card _ _ _ _ _ t (M ^ n) (by simpa only [fintype.card_fin, pow_succ] using nat.lt_succ_self (M ^ n.succ) ), refine ⟨λ i, (finset.univ.filter (λ x, t x = s)).to_list.nth_le i _, _, λ i₀ i₁, ht _ _ _⟩, { refine i.2.trans_le _, rwa finset.length_to_list }, { intros i j h, ext, exact list.nodup_iff_nth_le_inj.mp (finset.nodup_to_list _) _ _ _ _ h }, have : ∀ i h, (finset.univ.filter (λ x, t x = s)).to_list.nth_le i h ∈ finset.univ.filter (λ x, t x = s), { intros i h, exact (finset.mem_to_list _).mp (list.nth_le_mem _ _ _) }, obtain ⟨_, h₀⟩ := finset.mem_filter.mp (this i₀ _), obtain ⟨_, h₁⟩ := finset.mem_filter.mp (this i₁ _), exact h₀.trans h₁.symm }, -- Since `s` is large enough, there are two elements of `A ∘ s` -- where the second components lie close together. obtain ⟨k₀, k₁, hk, h⟩ := ih hε hb (λ x, fin.tail (A (s x))), refine ⟨s k₀, s k₁, λ h, hk (s_inj h), λ i, fin.cases _ (λ i, _) i⟩, { exact hs k₀ k₁ }, { exact h i }, end /-- Any large enough family of vectors in `R^ι` has a pair of elements whose remainders are close together, pointwise. -/ lemma exists_approx {ι : Type*} [fintype ι] {ε : ℝ} (hε : 0 < ε) {b : R} (hb : b ≠ 0) (h : abv.is_admissible) (A : fin (h.card ε ^ fintype.card ι).succ → ι → R) : ∃ (i₀ i₁), (i₀ ≠ i₁) ∧ ∀ k, (abv (A i₁ k % b - A i₀ k % b) : ℝ) < abv b • ε := begin let e := fintype.equiv_fin ι, obtain ⟨i₀, i₁, ne, h⟩ := h.exists_approx_aux (fintype.card ι) hε hb (λ x y, A x (e.symm y)), refine ⟨i₀, i₁, ne, λ k, _⟩, convert h (e k); simp only [e.symm_apply_apply] end end is_admissible end absolute_value
f89af5731f86901cccdbdadaab60525f6da8239f
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/skeletal_auto.lean
f3f93c1032c4d6cce2fc2d22c5ed1c55b58d5347
[]
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
8,704
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.isomorphism_classes import Mathlib.category_theory.thin import Mathlib.PostPort universes v₁ u₁ v₂ u₂ l u₃ v₃ namespace Mathlib /-! # Skeleton of a category Define skeletal categories as categories in which any two isomorphic objects are equal. Construct the skeleton of a thin category as a partial ordering, and (noncomputably) show it is a skeleton of the original category. The advantage of this special case being handled separately is that lemmas and definitions about orderings can be used directly, for example for the subobject lattice (TODO). In addition, some of the commutative diagrams about the functors commute definitionally on the nose which is convenient in practice. (TODO): Construct the skeleton of a general category using choice, and show it is a skeleton. -/ namespace category_theory /-- A category is skeletal if isomorphic objects are equal. -/ def skeletal (C : Type u₁) [category C] := ∀ {X Y : C}, is_isomorphic X Y → X = Y /-- `is_skeleton_of C D F` says that `F : D ⥤ C` exhibits `D` as a skeletal full subcategory of `C`, in particular `F` is a (strong) equivalence and `D` is skeletal. -/ structure is_skeleton_of (C : Type u₁) [category C] (D : Type u₂) [category D] (F : D ⥤ C) where skel : skeletal D eqv : is_equivalence F /-- If `C` is thin and skeletal, then any naturally isomorphic functors to `C` are equal. -/ theorem functor.eq_of_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {F₁ : D ⥤ C} {F₂ : D ⥤ C} [∀ (X Y : C), subsingleton (X ⟶ Y)] (hC : skeletal C) (hF : F₁ ≅ F₂) : F₁ = F₂ := sorry /-- If `C` is thin and skeletal, `D ⥤ C` is skeletal. `category_theory.functor_thin` shows it is thin also. -/ theorem functor_skeletal {C : Type u₁} [category C] {D : Type u₂} [category D] [∀ (X Y : C), subsingleton (X ⟶ Y)] (hC : skeletal C) : skeletal (D ⥤ C) := fun (F₁ F₂ : D ⥤ C) (h : is_isomorphic F₁ F₂) => nonempty.elim h (functor.eq_of_iso hC) /-- Construct the skeleton category by taking the quotient of objects. This construction gives a preorder with nice definitional properties, but is only really appropriate for thin categories. -/ def thin_skeleton (C : Type u₁) [category C] := quotient (is_isomorphic_setoid C) protected instance inhabited_thin_skeleton (C : Type u₁) [category C] [Inhabited C] : Inhabited (thin_skeleton C) := { default := quotient.mk Inhabited.default } protected instance thin_skeleton.preorder (C : Type u₁) [category C] : preorder (thin_skeleton C) := preorder.mk (quotient.lift₂ (fun (X Y : C) => Nonempty (X ⟶ Y)) sorry) (fun (a b : thin_skeleton C) => quotient.lift₂ (fun (X Y : C) => Nonempty (X ⟶ Y)) sorry a b ∧ ¬quotient.lift₂ (fun (X Y : C) => Nonempty (X ⟶ Y)) sorry b a) sorry sorry /-- The functor from a category to its thin skeleton. -/ @[simp] theorem to_thin_skeleton_obj (C : Type u₁) [category C] (a : C) : functor.obj (to_thin_skeleton C) a = quotient.mk a := Eq.refl (functor.obj (to_thin_skeleton C) a) /-! The constructions here are intended to be used when the category `C` is thin, even though some of the statements can be shown without this assumption. -/ namespace thin_skeleton /-- The thin skeleton is thin. -/ protected instance thin (C : Type u₁) [category C] {X : thin_skeleton C} {Y : thin_skeleton C} : subsingleton (X ⟶ Y) := subsingleton.intro fun (a b : X ⟶ Y) => ulift.cases_on a fun (a : plift (X ≤ Y)) => plift.cases_on a fun (f₁ : X ≤ Y) => ulift.cases_on b fun (b : plift (X ≤ Y)) => plift.cases_on b fun (f₂ : X ≤ Y) => Eq.refl (ulift.up (plift.up f₁)) /-- A functor `C ⥤ D` computably lowers to a functor `thin_skeleton C ⥤ thin_skeleton D`. -/ def map {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) : thin_skeleton C ⥤ thin_skeleton D := functor.mk (quotient.map (functor.obj F) sorry) fun (X Y : thin_skeleton C) => quotient.rec_on_subsingleton₂ X Y fun (x y : C) (k : quotient.mk x ⟶ quotient.mk y) => hom_of_le sorry theorem comp_to_thin_skeleton {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) : F ⋙ to_thin_skeleton D = to_thin_skeleton C ⋙ map F := rfl /-- Given a natural transformation `F₁ ⟶ F₂`, induce a natural transformation `map F₁ ⟶ map F₂`.-/ def map_nat_trans {C : Type u₁} [category C] {D : Type u₂} [category D] {F₁ : C ⥤ D} {F₂ : C ⥤ D} (k : F₁ ⟶ F₂) : map F₁ ⟶ map F₂ := nat_trans.mk fun (X : thin_skeleton C) => quotient.rec_on_subsingleton X fun (x : C) => ulift.up (plift.up sorry) -- TODO: state the lemmas about what happens when you compose with `to_thin_skeleton` /-- A functor `C ⥤ D ⥤ E` computably lowers to a functor `thin_skeleton C ⥤ thin_skeleton D ⥤ thin_skeleton E` -/ @[simp] theorem map₂_obj_obj {C : Type u₁} [category C] {D : Type u₂} [category D] {E : Type u₃} [category E] (F : C ⥤ D ⥤ E) (x : thin_skeleton C) (y : thin_skeleton D) : functor.obj (functor.obj (map₂ F) x) y = quotient.map₂ (fun (X : C) (Y : D) => functor.obj (functor.obj F X) Y) (map₂._proof_1 F) x y := Eq.refl (functor.obj (functor.obj (map₂ F) x) y) protected instance to_thin_skeleton_faithful (C : Type u₁) [category C] [∀ (X Y : C), subsingleton (X ⟶ Y)] : faithful (to_thin_skeleton C) := faithful.mk /-- Use `quotient.out` to create a functor out of the thin skeleton. -/ @[simp] theorem from_thin_skeleton_map (C : Type u₁) [category C] [∀ (X Y : C), subsingleton (X ⟶ Y)] (x : thin_skeleton C) (y : thin_skeleton C) : ∀ (ᾰ : x ⟶ y), functor.map (from_thin_skeleton C) ᾰ = quotient.rec_on_subsingleton₂ x y (fun (X Y : C) (f : quotient.mk X ⟶ quotient.mk Y) => iso.hom (nonempty.some (from_thin_skeleton._proof_2 C X)) ≫ nonempty.some (from_thin_skeleton._proof_3 C X Y f) ≫ iso.inv (nonempty.some (from_thin_skeleton._proof_4 C Y))) ᾰ := fun (ᾰ : x ⟶ y) => Eq.refl (functor.map (from_thin_skeleton C) ᾰ) protected instance from_thin_skeleton_equivalence (C : Type u₁) [category C] [∀ (X Y : C), subsingleton (X ⟶ Y)] : is_equivalence (from_thin_skeleton C) := is_equivalence.mk' (to_thin_skeleton C) (nat_iso.of_components (fun (x : thin_skeleton C) => quotient.rec_on_subsingleton x fun (X : C) => eq_to_iso sorry) sorry) (nat_iso.of_components (fun (X : C) => nonempty.some sorry) sorry) theorem equiv_of_both_ways {C : Type u₁} [category C] [∀ (X Y : C), subsingleton (X ⟶ Y)] {X : C} {Y : C} (f : X ⟶ Y) (g : Y ⟶ X) : X ≈ Y := Nonempty.intro (iso_of_both_ways f g) protected instance thin_skeleton_partial_order {C : Type u₁} [category C] [∀ (X Y : C), subsingleton (X ⟶ Y)] : partial_order (thin_skeleton C) := partial_order.mk preorder.le preorder.lt sorry sorry sorry theorem skeletal {C : Type u₁} [category C] [∀ (X Y : C), subsingleton (X ⟶ Y)] : skeletal (thin_skeleton C) := sorry theorem map_comp_eq {C : Type u₁} [category C] {D : Type u₂} [category D] {E : Type u₃} [category E] [∀ (X Y : C), subsingleton (X ⟶ Y)] (F : E ⥤ D) (G : D ⥤ C) : map (F ⋙ G) = map F ⋙ map G := sorry theorem map_id_eq {C : Type u₁} [category C] [∀ (X Y : C), subsingleton (X ⟶ Y)] : map 𝟭 = 𝟭 := sorry theorem map_iso_eq {C : Type u₁} [category C] {D : Type u₂} [category D] [∀ (X Y : C), subsingleton (X ⟶ Y)] {F₁ : D ⥤ C} {F₂ : D ⥤ C} (h : F₁ ≅ F₂) : map F₁ = map F₂ := functor.eq_of_iso skeletal (iso.mk (map_nat_trans (iso.hom h)) (map_nat_trans (iso.inv h))) /-- `from_thin_skeleton C` exhibits the thin skeleton as a skeleton. -/ def thin_skeleton_is_skeleton {C : Type u₁} [category C] [∀ (X Y : C), subsingleton (X ⟶ Y)] : is_skeleton_of C (thin_skeleton C) (from_thin_skeleton C) := is_skeleton_of.mk sorry (thin_skeleton.from_thin_skeleton_equivalence C) protected instance is_skeleton_of_inhabited {C : Type u₁} [category C] [∀ (X Y : C), subsingleton (X ⟶ Y)] : Inhabited (is_skeleton_of C (thin_skeleton C) (from_thin_skeleton C)) := { default := thin_skeleton_is_skeleton } end Mathlib
cbd7af5b965cf508ae4a6e9ef4bc4455abf1fe4f
65b579fba1b0b66add04cccd4529add645eff597
/lambda/with_primitives/src/types.lean
65294fd950bb0198124958133078e6000db800c0
[]
no_license
teodorov/sf_lean
ba637ca8ecc538aece4d02c8442d03ef713485db
cd4832d6bee9c606014c977951f6aebc4c8d611b
refs/heads/master
1,632,890,232,054
1,543,005,745,000
1,543,005,745,000
108,566,115
1
0
null
null
null
null
UTF-8
Lean
false
false
407
lean
namespace lambda_types inductive Constant | int : int → Constant | nat : nat → Constant | bool : bool → Constant inductive AST | var : string → AST | literal : Constant → AST | primitive : string → AST → AST → AST | abstraction : string → AST → AST | application : AST → AST → AST | conditional : AST → AST → AST → AST | letrec (id : string) (x : AST) : AST end lambda_types
196809aa4b7febef9c7c8c39f2c62d689150d946
9ad8d18fbe5f120c22b5e035bc240f711d2cbd7e
/src/combinatorics/card_sign.lean
b6966f704199d30cb20d3d3ff24ffbc9fb01b2ca
[]
no_license
agusakov/lean_lib
c0e9cc29fc7d2518004e224376adeb5e69b5cc1a
f88d162da2f990b87c4d34f5f46bbca2bbc5948e
refs/heads/master
1,642,141,461,087
1,557,395,798,000
1,557,395,798,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,952
lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Neil Strickland If J is a finite set then the sum of (-1)^{|K|} over subsets K ⊆ J is usually zero, except that the sum is one if J is empty. We will prove two versions of this, one where J is a finset in an arbitrary type I, and one where J is the universal set in a fintype I. A key point is as follows: if we take J' = (insert a J) = J ∪ {a}, then the subsets of J can be split into those that contain a and those that do not, and these two families are in bijection. Equivalently, we have a bijection P(J') ≃ P(J) × bool (where bool is a set with two elements). We construct this bijection as `finset.insert.powerset_filter`. -/ import data.fintype algebra.big_operators import algebra.prod_equiv namespace finset open finset universe u variables {I : Type u} [decidable_eq I] def card_sign (K : finset I) : ℤ := (-1) ^ K.card def card_sign_sum (J : finset I) := J.powerset.sum (card_sign : finset I → ℤ) section insert variables {i : I} {J : finset I} (hiJ : i ∉ J) include hiJ lemma insert.card_sign : card_sign (insert i J) = - card_sign J := by {dsimp[card_sign], rw[card_insert_of_not_mem hiJ,pow_add,pow_one], rw[mul_neg_eq_neg_mul_symm,mul_one], } lemma subset_iff_subset_insert (K : finset I) : K ⊆ J ↔ K ⊆ (insert i J) ∧ i ∉ K := begin split, {exact λ hKJ,⟨subset.trans hKJ (subset_insert i J),λ i_in_K,hiJ (hKJ i_in_K)⟩}, {rintros ⟨hKiJ,hi⟩ a ha, rcases mem_insert.mp (hKiJ ha) with a_eq_i | a_in_J, {exact (hi (a_eq_i ▸ ha)).elim},{exact a_in_J} } end lemma insert.powerset_filter : (insert i J).powerset.filter (λ K, i ∉ K) = J.powerset := by { ext K,rw[mem_filter,mem_powerset,mem_powerset,subset_iff_subset_insert hiJ], } def insert.powerset_equiv : { K // K ∈ (insert i J).powerset} ≃ ({K // K ∈ J.powerset} × bool) := { to_fun := λ K, ⟨⟨K.val ∩ J,mem_powerset.mpr (inter_subset_right K.val J)⟩, if i ∈ K.val then tt else ff⟩, inv_fun := λ Kb, cond Kb.2 ⟨insert i Kb.1.val, mem_powerset.mpr (insert_subset_insert i (mem_powerset.mp Kb.1.property))⟩ ⟨Kb.1.val, mem_powerset.mpr (subset.trans (mem_powerset.mp Kb.1.property) (subset_insert i J))⟩, left_inv := λ ⟨K,hK⟩, begin let hK' := mem_powerset.mp hK, apply subtype.eq,ext a,simp only [],split_ifs, {rw[cond,mem_insert,mem_inter], split, {rintro (a_eq_i | h2),exact a_eq_i.symm.subst h,exact h2.left}, {intro a_in_K, rcases mem_insert.mp (hK' a_in_K) with a_eq_i | a_in_J, {exact or.inl a_eq_i}, {exact or.inr ⟨a_in_K,a_in_J⟩} } },{ rw[cond,mem_inter], split, {exact λ h,h.1}, {intro a_in_K, exact ⟨a_in_K,((subset_iff_subset_insert hiJ K).mpr ⟨hK',h⟩) a_in_K⟩, } } end, right_inv := λ ⟨⟨K,hK⟩,b⟩, begin let hK' := mem_powerset.mp hK, have hiK : i ∉ K := λ h, hiJ (hK' h), cases b; simp only[cond], {rw[if_neg hiK],congr,ext a,rw[mem_inter], exact ⟨λ h,h.1,λ h,⟨h,hK' h⟩⟩, },{ rw[if_pos (mem_insert_self i K)],congr,ext a,rw[mem_inter,mem_insert], split, {rintro ⟨a_eq_i | a_in_K,a_in_J⟩, {exact (hiJ (a_eq_i ▸ a_in_J)).elim,}, {exact a_in_K} },{ intro a_in_K, exact ⟨or.inr a_in_K,hK' a_in_K⟩ } } end } lemma card_sign_sum_insert : card_sign_sum (insert i J) = 0 := begin let e := (insert.powerset_equiv hiJ).symm, rw[card_sign_sum,sum_eq_univ_sum], rw[← univ_sum_equiv e (λ K,card_sign K.val)], let g : {K // K ∈ J.powerset} → bool → ℤ := λ K b, cond b (- (card_sign K.val)) (card_sign K.val), have eg : ∀ K, (@univ bool _).sum (g K) = 0 := λ K, by {rw[sum_over_bool],dsimp[g],rw[add_neg_self]}, have : (λ (K : {K // K ∈ powerset (insert i J)}), (card_sign K.val)) ∘ e.to_fun = λ Kb, g Kb.1 Kb.2 := by {ext Kb,rcases Kb with ⟨⟨K,hK⟩,ff | tt⟩, {refl}, {have : i ∉ K := λ h, hiJ ((mem_powerset.mp hK) h), exact insert.card_sign this} }, rw[this,sum_univ_product,sum_congr rfl (λ K _,eg K),sum_const_zero], end end insert lemma card_sign_sum_eq (J : finset I) : card_sign_sum J = if J = ∅ then 1 else 0 := begin split_ifs with h, {rw[h],refl}, {rcases (exists_mem_of_ne_empty h) with ⟨a,a_in_J⟩, rw[← insert_erase a_in_J,card_sign_sum_insert (not_mem_erase a J)], } end end finset namespace fintype open finset lemma card_sign_sum_eq (I : Type*) [decidable_eq I] [fintype I] : (@univ I _).powerset.sum card_sign = if (card I = 0) then 1 else 0 := begin change (@univ I _).card_sign_sum = if (card I = 0) then 1 else 0, let h := @finset.card_eq_zero I univ, rw[finset.card_sign_sum_eq,← card_univ], by_cases hu : (@univ I _).card = 0, rw[if_pos hu,if_pos (h.mp hu)], rw[if_neg hu,if_neg (hu ∘ h.mpr)], end end fintype
b42d3ce05f2d37c5db9a946011d0f5979cf066c7
9dc8cecdf3c4634764a18254e94d43da07142918
/src/category_theory/enriched/basic.lean
8644f3bafb15a89be5deb81a06d7ad184e771d6f
[ "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
17,221
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 category_theory.monoidal.types import category_theory.monoidal.center /-! # Enriched categories We set up the basic theory of `V`-enriched categories, for `V` an arbitrary monoidal category. We do not assume here that `V` is a concrete category, so there does not need to be a "honest" underlying category! Use `X ⟶[V] Y` to obtain the `V` object of morphisms from `X` to `Y`. This file contains the definitions of `V`-enriched categories and `V`-functors. We don't yet define the `V`-object of natural transformations between a pair of `V`-functors (this requires limits in `V`), but we do provide a presheaf isomorphic to the Yoneda embedding of this object. We verify that when `V = Type v`, all these notion reduce to the usual ones. -/ universes w v u₁ u₂ u₃ noncomputable theory namespace category_theory open opposite open monoidal_category variables (V : Type v) [category.{w} V] [monoidal_category V] /-- A `V`-category is a category enriched in a monoidal category `V`. Note that we do not assume that `V` is a concrete category, so there may not be an "honest" underlying category at all! -/ class enriched_category (C : Type u₁) := (hom : C → C → V) (notation X ` ⟶[] ` Y:10 := hom X Y) (id : Π X, 𝟙_ V ⟶ (X ⟶[] X)) (comp : Π X Y Z, (X ⟶[] Y) ⊗ (Y ⟶[] Z) ⟶ (X ⟶[] Z)) (id_comp : Π X Y, (λ_ (X ⟶[] Y)).inv ≫ (id X ⊗ 𝟙 _) ≫ comp X X Y = 𝟙 _ . obviously) (comp_id : Π X Y, (ρ_ (X ⟶[] Y)).inv ≫ (𝟙 _ ⊗ id Y) ≫ comp X Y Y = 𝟙 _ . obviously) (assoc : Π W X Y Z, (α_ _ _ _).inv ≫ (comp W X Y ⊗ 𝟙 _) ≫ comp W Y Z = (𝟙 _ ⊗ comp X Y Z) ≫ comp W X Z . obviously) notation (name := enriched_category.hom) X ` ⟶[`V`] ` Y:10 := (enriched_category.hom X Y : V) variables (V) {C : Type u₁} [enriched_category V C] /-- The `𝟙_ V`-shaped generalized element giving the identity in a `V`-enriched category. -/ def e_id (X : C) : 𝟙_ V ⟶ (X ⟶[V] X) := enriched_category.id X /-- The composition `V`-morphism for a `V`-enriched category. -/ def e_comp (X Y Z : C) : (X ⟶[V] Y) ⊗ (Y ⟶[V] Z) ⟶ (X ⟶[V] Z) := enriched_category.comp X Y Z -- We don't just use `restate_axiom` here; that would leave `V` as an implicit argument. @[simp, reassoc] lemma e_id_comp (X Y : C) : (λ_ (X ⟶[V] Y)).inv ≫ (e_id V X ⊗ 𝟙 _) ≫ e_comp V X X Y = 𝟙 (X ⟶[V] Y) := enriched_category.id_comp X Y @[simp, reassoc] lemma e_comp_id (X Y : C) : (ρ_ (X ⟶[V] Y)).inv ≫ (𝟙 _ ⊗ e_id V Y) ≫ e_comp V X Y Y = 𝟙 (X ⟶[V] Y) := enriched_category.comp_id X Y @[simp, reassoc] lemma e_assoc (W X Y Z : C) : (α_ _ _ _).inv ≫ (e_comp V W X Y ⊗ 𝟙 _) ≫ e_comp V W Y Z = (𝟙 _ ⊗ e_comp V X Y Z) ≫ e_comp V W X Z := enriched_category.assoc W X Y Z section variables {V} {W : Type v} [category.{w} W] [monoidal_category W] /-- A type synonym for `C`, which should come equipped with a `V`-enriched category structure. In a moment we will equip this with the `W`-enriched category structure obtained by applying the functor `F : lax_monoidal_functor V W` to each hom object. -/ @[nolint has_nonempty_instance unused_arguments] def transport_enrichment (F : lax_monoidal_functor V W) (C : Type u₁) := C instance (F : lax_monoidal_functor V W) : enriched_category W (transport_enrichment F C) := { hom := λ (X Y : C), F.obj (X ⟶[V] Y), id := λ (X : C), F.ε ≫ F.map (e_id V X), comp := λ (X Y Z : C), F.μ _ _ ≫ F.map (e_comp V X Y Z), id_comp := λ X Y, begin rw [comp_tensor_id, category.assoc, ←F.to_functor.map_id, F.μ_natural_assoc, F.to_functor.map_id, F.left_unitality_inv_assoc, ←F.to_functor.map_comp, ←F.to_functor.map_comp, e_id_comp, F.to_functor.map_id], end, comp_id := λ X Y, begin rw [id_tensor_comp, category.assoc, ←F.to_functor.map_id, F.μ_natural_assoc, F.to_functor.map_id, F.right_unitality_inv_assoc, ←F.to_functor.map_comp, ←F.to_functor.map_comp, e_comp_id, F.to_functor.map_id], end, assoc := λ P Q R S, begin rw [comp_tensor_id, category.assoc, ←F.to_functor.map_id, F.μ_natural_assoc, F.to_functor.map_id, ←F.associativity_inv_assoc, ←F.to_functor.map_comp, ←F.to_functor.map_comp, e_assoc, id_tensor_comp, category.assoc, ←F.to_functor.map_id, F.μ_natural_assoc, F.to_functor.map_comp], end, } end /-- Construct an honest category from a `Type v`-enriched category. -/ def category_of_enriched_category_Type (C : Type u₁) [𝒞 : enriched_category (Type v) C] : category.{v} C := { hom := 𝒞.hom, id := λ X, e_id (Type v) X punit.star, comp := λ X Y Z f g, e_comp (Type v) X Y Z ⟨f, g⟩, id_comp' := λ X Y f, congr_fun (e_id_comp (Type v) X Y) f, comp_id' := λ X Y f, congr_fun (e_comp_id (Type v) X Y) f, assoc' := λ W X Y Z f g h, (congr_fun (e_assoc (Type v) W X Y Z) ⟨f, g, h⟩ : _), } /-- Construct a `Type v`-enriched category from an honest category. -/ def enriched_category_Type_of_category (C : Type u₁) [𝒞 : category.{v} C] : enriched_category (Type v) C := { hom := 𝒞.hom, id := λ X p, 𝟙 X, comp := λ X Y Z p, p.1 ≫ p.2, id_comp := λ X Y, by { ext, simp, }, comp_id := λ X Y, by { ext, simp, }, assoc := λ W X Y Z, by { ext ⟨f, g, h⟩, simp, }, } /-- We verify that an enriched category in `Type u` is just the same thing as an honest category. -/ def enriched_category_Type_equiv_category (C : Type u₁) : (enriched_category (Type v) C) ≃ category.{v} C := { to_fun := λ 𝒞, by exactI category_of_enriched_category_Type C, inv_fun := λ 𝒞, by exactI enriched_category_Type_of_category C, left_inv := λ 𝒞, begin cases 𝒞, dsimp [enriched_category_Type_of_category], congr, { ext X ⟨⟩, refl, }, { ext X Y Z ⟨f, g⟩, refl, } end, right_inv := λ 𝒞, by { rcases 𝒞 with ⟨⟨⟨⟩⟩⟩, dsimp, congr, }, }. section variables {W : Type (v+1)} [category.{v} W] [monoidal_category W] [enriched_category W C] /-- A type synonym for `C`, which should come equipped with a `V`-enriched category structure. In a moment we will equip this with the (honest) category structure so that `X ⟶ Y` is `(𝟙_ W) ⟶ (X ⟶[W] Y)`. We obtain this category by transporting the enrichment in `V` along the lax monoidal functor `coyoneda_tensor_unit`, then using the equivalence of `Type`-enriched categories with honest categories. This is sometimes called the "underlying" category of an enriched category, although some care is needed as the functor `coyoneda_tensor_unit`, which always exists, does not necessarily coincide with "the forgetful functor" from `V` to `Type`, if such exists. When `V` is any of `Type`, `Top`, `AddCommGroup`, or `Module R`, `coyoneda_tensor_unit` is just the usual forgetful functor, however. For `V = Algebra R`, the usual forgetful functor is coyoneda of `polynomial R`, not of `R`. (Perhaps we should have a typeclass for this situation: `concrete_monoidal`?) -/ @[nolint has_nonempty_instance unused_arguments] def forget_enrichment (W : Type (v+1)) [category.{v} W] [monoidal_category W] (C : Type u₁) [enriched_category W C] := C variables (W) /-- Typecheck an object of `C` as an object of `forget_enrichment W C`. -/ def forget_enrichment.of (X : C) : forget_enrichment W C := X /-- Typecheck an object of `forget_enrichment W C` as an object of `C`. -/ def forget_enrichment.to (X : forget_enrichment W C) : C := X @[simp] lemma forget_enrichment.to_of (X : C) : forget_enrichment.to W (forget_enrichment.of W X) = X := rfl @[simp] lemma forget_enrichment.of_to (X : forget_enrichment W C) : forget_enrichment.of W (forget_enrichment.to W X) = X := rfl instance category_forget_enrichment : category (forget_enrichment W C) := begin let I : enriched_category (Type v) (transport_enrichment (coyoneda_tensor_unit W) C) := infer_instance, exact enriched_category_Type_equiv_category C I, end /-- We verify that the morphism types in `forget_enrichment W C` are `(𝟙_ W) ⟶ (X ⟶[W] Y)`. -/ example (X Y : forget_enrichment W C) : (X ⟶ Y) = ((𝟙_ W) ⟶ (forget_enrichment.to W X ⟶[W] forget_enrichment.to W Y)) := rfl /-- Typecheck a `(𝟙_ W)`-shaped `W`-morphism as a morphism in `forget_enrichment W C`. -/ def forget_enrichment.hom_of {X Y : C} (f : (𝟙_ W) ⟶ (X ⟶[W] Y)) : forget_enrichment.of W X ⟶ forget_enrichment.of W Y := f /-- Typecheck a morphism in `forget_enrichment W C` as a `(𝟙_ W)`-shaped `W`-morphism. -/ def forget_enrichment.hom_to {X Y : forget_enrichment W C} (f : X ⟶ Y) : (𝟙_ W) ⟶ (forget_enrichment.to W X ⟶[W] forget_enrichment.to W Y) := f @[simp] lemma forget_enrichment.hom_to_hom_of {X Y : C} (f : (𝟙_ W) ⟶ (X ⟶[W] Y)) : forget_enrichment.hom_to W (forget_enrichment.hom_of W f) = f := rfl @[simp] lemma forget_enrichment.hom_of_hom_to {X Y : forget_enrichment W C} (f : X ⟶ Y) : forget_enrichment.hom_of W (forget_enrichment.hom_to W f) = f := rfl /-- The identity in the "underlying" category of an enriched category. -/ @[simp] lemma forget_enrichment_id (X : forget_enrichment W C) : forget_enrichment.hom_to W (𝟙 X) = (e_id W (forget_enrichment.to W X : C)) := category.id_comp _ @[simp] lemma forget_enrichment_id' (X : C) : forget_enrichment.hom_of W (e_id W X) = (𝟙 (forget_enrichment.of W X : C)) := (forget_enrichment_id W (forget_enrichment.of W X)).symm /-- Composition in the "underlying" category of an enriched category. -/ @[simp] lemma forget_enrichment_comp {X Y Z : forget_enrichment W C} (f : X ⟶ Y) (g : Y ⟶ Z) : forget_enrichment.hom_to W (f ≫ g) = (((λ_ (𝟙_ W)).inv ≫ (forget_enrichment.hom_to W f ⊗ forget_enrichment.hom_to W g)) ≫ e_comp W _ _ _) := rfl end /-- A `V`-functor `F` between `V`-enriched categories has a `V`-morphism from `X ⟶[V] Y` to `F.obj X ⟶[V] F.obj Y`, satisfying the usual axioms. -/ structure enriched_functor (C : Type u₁) [enriched_category V C] (D : Type u₂) [enriched_category V D] := (obj : C → D) (map : Π X Y : C, (X ⟶[V] Y) ⟶ (obj X ⟶[V] obj Y)) (map_id' : ∀ X : C, e_id V X ≫ map X X = e_id V (obj X) . obviously) (map_comp' : ∀ X Y Z : C, e_comp V X Y Z ≫ map X Z = (map X Y ⊗ map Y Z) ≫ e_comp V (obj X) (obj Y) (obj Z) . obviously) restate_axiom enriched_functor.map_id' restate_axiom enriched_functor.map_comp' attribute [simp, reassoc] enriched_functor.map_id attribute [simp, reassoc] enriched_functor.map_comp /-- The identity enriched functor. -/ @[simps] def enriched_functor.id (C : Type u₁) [enriched_category V C] : enriched_functor V C C := { obj := λ X, X, map := λ X Y, 𝟙 _, } instance : inhabited (enriched_functor V C C) := ⟨enriched_functor.id V C⟩ /-- Composition of enriched functors. -/ @[simps] def enriched_functor.comp {C : Type u₁} {D : Type u₂} {E : Type u₃} [enriched_category V C] [enriched_category V D] [enriched_category V E] (F : enriched_functor V C D) (G : enriched_functor V D E) : enriched_functor V C E := { obj := λ X, G.obj (F.obj X), map := λ X Y, F.map _ _ ≫ G.map _ _, } section variables {W : Type (v+1)} [category.{v} W] [monoidal_category W] /-- An enriched functor induces an honest functor of the underlying categories, by mapping the `(𝟙_ W)`-shaped morphisms. -/ def enriched_functor.forget {C : Type u₁} {D : Type u₂} [enriched_category W C] [enriched_category W D] (F : enriched_functor W C D) : (forget_enrichment W C) ⥤ (forget_enrichment W D) := { obj := λ X, forget_enrichment.of W (F.obj (forget_enrichment.to W X)), map := λ X Y f, forget_enrichment.hom_of W (forget_enrichment.hom_to W f ≫ F.map (forget_enrichment.to W X) (forget_enrichment.to W Y)), map_comp' := λ X Y Z f g, begin dsimp, apply_fun forget_enrichment.hom_to W, { simp only [iso.cancel_iso_inv_left, category.assoc, tensor_comp, forget_enrichment.hom_to_hom_of, enriched_functor.map_comp, forget_enrichment_comp], refl, }, { intros f g w, apply_fun forget_enrichment.hom_of W at w, simpa using w, }, end, } end section variables {V} variables {D : Type u₂} [enriched_category V D] /-! We now turn to natural transformations between `V`-functors. The mostly commonly encountered definition of an enriched natural transformation is a collection of morphisms ``` (𝟙_ W) ⟶ (F.obj X ⟶[V] G.obj X) ``` satisfying an appropriate analogue of the naturality square. (c.f. https://ncatlab.org/nlab/show/enriched+natural+transformation) This is the same thing as a natural transformation `F.forget ⟶ G.forget`. We formalize this as `enriched_nat_trans F G`, which is a `Type`. However, there's also something much nicer: with appropriate additional hypotheses, there is a `V`-object `enriched_nat_trans_obj F G` which contains more information, and from which one can recover `enriched_nat_trans F G ≃ (𝟙_ V) ⟶ enriched_nat_trans_obj F G`. Using these as the hom-objects, we can build a `V`-enriched category with objects the `V`-functors. For `enriched_nat_trans_obj` to exist, it suffices to have `V` braided and complete. Before assuming `V` is complete, we assume it is braided and define a presheaf `enriched_nat_trans_yoneda F G` which is isomorphic to the Yoneda embedding of `enriched_nat_trans_obj F G` whether or not that object actually exists. This presheaf has components `(enriched_nat_trans_yoneda F G).obj A` what we call the `A`-graded enriched natural transformations, which are collections of morphisms ``` A ⟶ (F.obj X ⟶[V] G.obj X) ``` satisfying a similar analogue of the naturality square, this time incorporating a half-braiding on `A`. (We actually define `enriched_nat_trans F G` as the special case `A := 𝟙_ V` with the trivial half-braiding, and when defining `enriched_nat_trans_yoneda F G` we use the half-braidings coming from the ambient braiding on `V`.) -/ /-- The type of `A`-graded natural transformations between `V`-functors `F` and `G`. This is the type of morphisms in `V` from `A` to the `V`-object of natural transformations. -/ @[ext, nolint has_nonempty_instance] structure graded_nat_trans (A : center V) (F G : enriched_functor V C D) := (app : Π (X : C), A.1 ⟶ (F.obj X ⟶[V] G.obj X)) (naturality : ∀ (X Y : C), (A.2.β (X ⟶[V] Y)).hom ≫ (F.map X Y ⊗ app Y) ≫ e_comp V _ _ _ = (app X ⊗ G.map X Y) ≫ e_comp V _ _ _) variables [braided_category V] open braided_category /-- A presheaf isomorphic to the Yoneda embedding of the `V`-object of natural transformations from `F` to `G`. -/ @[simps] def enriched_nat_trans_yoneda (F G : enriched_functor V C D) : Vᵒᵖ ⥤ (Type (max u₁ w)) := { obj := λ A, graded_nat_trans ((center.of_braided V).obj (unop A)) F G, map := λ A A' f σ, { app := λ X, f.unop ≫ σ.app X, naturality := λ X Y, begin have p := σ.naturality X Y, dsimp at p ⊢, rw [←id_tensor_comp_tensor_id (f.unop ≫ σ.app Y) _, id_tensor_comp, category.assoc, category.assoc, ←braiding_naturality_assoc, id_tensor_comp_tensor_id_assoc, p, ←tensor_comp_assoc,category.id_comp], end }, } -- TODO assuming `[has_limits C]` construct the actual object of natural transformations -- and show that the functor category is `V`-enriched. end section local attribute [instance] category_of_enriched_category_Type /-- We verify that an enriched functor between `Type v` enriched categories is just the same thing as an honest functor. -/ @[simps] def enriched_functor_Type_equiv_functor {C : Type u₁} [𝒞 : enriched_category (Type v) C] {D : Type u₂} [𝒟 : enriched_category (Type v) D] : enriched_functor (Type v) C D ≃ (C ⥤ D) := { to_fun := λ F, { obj := λ X, F.obj X, map := λ X Y f, F.map X Y f, map_id' := λ X, congr_fun (F.map_id X) punit.star, map_comp' := λ X Y Z f g, congr_fun (F.map_comp X Y Z) ⟨f, g⟩, }, inv_fun := λ F, { obj := λ X, F.obj X, map := λ X Y f, F.map f, map_id' := λ X, by { ext ⟨⟩, exact F.map_id X, }, map_comp' := λ X Y Z, by { ext ⟨f, g⟩, exact F.map_comp f g, }, }, left_inv := λ F, by { cases F, simp, }, right_inv := λ F, by { cases F, simp, }, } /-- We verify that the presheaf representing natural transformations between `Type v`-enriched functors is actually represented by the usual type of natural transformations! -/ def enriched_nat_trans_yoneda_Type_iso_yoneda_nat_trans {C : Type v} [enriched_category (Type v) C] {D : Type v} [enriched_category (Type v) D] (F G : enriched_functor (Type v) C D) : enriched_nat_trans_yoneda F G ≅ yoneda.obj ((enriched_functor_Type_equiv_functor F) ⟶ (enriched_functor_Type_equiv_functor G)) := nat_iso.of_components (λ α, { hom := λ σ x, { app := λ X, σ.app X x, naturality' := λ X Y f, congr_fun (σ.naturality X Y) ⟨x, f⟩, }, inv := λ σ, { app := λ X x, (σ x).app X, naturality := λ X Y, by { ext ⟨x, f⟩, exact ((σ x).naturality f), }, }}) (by tidy) end end category_theory
04453ccecd8b95d8f00f61332db3db6206e0d3dd
fef48cac17c73db8662678da38fd75888db97560
/src/point7.lean
f2c3f8a34e025b0736b2cb7526b6ebd0b90c74af
[]
no_license
kbuzzard/lean-squares-in-fibonacci
6c0d924f799d6751e19798bb2530ee602ec7087e
8cea20e5ce88ab7d17b020932d84d316532a84a8
refs/heads/master
1,584,524,504,815
1,582,387,156,000
1,582,387,156,000
134,576,655
3
1
null
1,541,538,497,000
1,527,083,406,000
Lean
UTF-8
Lean
false
false
1,594
lean
import definitions import point4 theorem fib_add (m n : ℕ) : fib (m + n + 1) = fib m * fib n + fib (m + 1) * fib (n + 1) := nat.rec_on_two n (by unfold fib; rw [mul_zero, zero_add, mul_one]; refl) (by unfold fib; rw [zero_add, mul_one, mul_one]; refl) $ λ n ih1 ih2, calc fib (m + n + 1) + fib (m + nat.succ n + 1) = fib m * (fib n + fib (n+1)) + fib (m+1) * (fib (n+1) + (fib n + fib (n+1))) : by rw [ih1, ih2, fib, mul_add, mul_add, mul_add, mul_add, nat.succ_eq_add_one]; ac_refl theorem fib_dvd_mul (m n : ℕ) : fib m ∣ fib (m * n) := nat.cases_on m (by rw [zero_mul]; refl) $ λ m, nat.rec_on n (dvd_zero _) $ λ n ih, show fib (nat.succ m) ∣ fib (nat.succ m * n + m + 1), by rw [fib_add]; apply dvd_add; [apply dvd_mul_of_dvd_left ih, apply dvd_mul_left] theorem fib_gcd_mod (m n : ℕ) : nat.gcd (fib m) (fib n) = nat.gcd (fib (m%n)) (fib n) := nat.mod_rec n m $ λ m, show nat.gcd (fib m) (fib n) = nat.gcd (fib (m + n)) (fib n), from nat.cases_on n rfl $ λ n, by rw [nat.add_succ, nat.succ_eq_add_one, fib_add]; symmetry; rw [nat.gcd_comm, nat.gcd_rec, nat.add_mul_mod_self_right, ← nat.gcd_rec]; rw [nat.gcd_comm, nat.coprime.gcd_mul_right_cancel]; symmetry; apply fib_succ_coprime theorem fib_gcd (m : ℕ) : ∀ n, nat.gcd (fib m) (fib n) = fib (nat.gcd m n) := nat.strong_induction_on m $ λ m, nat.cases_on m (λ _ _, by rw [fib, nat.gcd_zero_left, nat.gcd_zero_left]) $ λ m ih n, have H : n % nat.succ m < nat.succ m, from nat.mod_lt _ $ nat.zero_lt_succ m, by rw [nat.gcd_succ, ← ih _ H, ← fib_gcd_mod, nat.gcd_comm]
ec1aa9267f98acfe2ca1d1f257d21345b101ae5b
78269ad0b3c342b20786f60690708b6e328132b0
/src/library_dev/data/list/perm.lean
a2a5789f33b8bf85fefa4903b0c27f6ed10961f9
[]
no_license
dselsam/library_dev
e74f46010fee9c7b66eaa704654cad0fcd2eefca
1b4e34e7fb067ea5211714d6d3ecef5132fc8218
refs/heads/master
1,610,372,841,675
1,497,014,421,000
1,497,014,421,000
86,526,137
0
0
null
1,490,752,133,000
1,490,752,132,000
null
UTF-8
Lean
false
false
39,663
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad List permutations. -/ import .basic .comb .set -- TODO(Jeremy): Here is a common idiom: after simplifying, we have a goal 1 + t = nat.succ t -- and need to say rw [add_comm, reflexivity]. Can we get the simplifier to finish this off? namespace list universe variables uu vv variables {α : Type uu} {β : Type vv} inductive perm : list α → list α → Prop | nil : perm [] [] | skip : Π (x : α) {l₁ l₂ : list α}, perm l₁ l₂ → perm (x::l₁) (x::l₂) | swap : Π (x y : α) (l : list α), perm (y::x::l) (x::y::l) | trans : Π {l₁ l₂ l₃ : list α}, perm l₁ l₂ → perm l₂ l₃ → perm l₁ l₃ namespace perm infix ~ := perm @[refl] protected theorem refl : ∀ (l : list α), l ~ l | [] := nil | (x::xs) := skip x (refl xs) @[symm] protected theorem symm {l₁ l₂ : list α} (p : l₁ ~ l₂) : l₂ ~ l₁ := perm.rec_on p nil (λ x l₁ l₂ p₁ r₁, skip x r₁) (λ x y l, swap y x l) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₂ r₁) attribute [trans] perm.trans theorem eqv (α : Type) : equivalence (@perm α) := mk_equivalence (@perm α) (@perm.refl α) (@perm.symm α) (@perm.trans α) attribute [instance] protected definition is_setoid (α : Type) : setoid (list α) := setoid.mk (@perm α) (perm.eqv α) theorem mem_of_perm {a : α} {l₁ l₂ : list α} (p : l₁ ~ l₂) : a ∈ l₁ → a ∈ l₂ := perm.rec_on p (λ h, h) (λ x l₁ l₂ p₁ r₁ i, or.elim i (λ ax, by simp [ax]) (λ al₁, or.inr (r₁ al₁))) (λ x y l ayxl, or.elim ayxl (λ ay, by simp [ay]) (λ axl, or.elim axl (λ ax, by simp [ax]) (λ al, or.inr (or.inr al)))) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂ ainl₁, r₂ (r₁ ainl₁)) theorem not_mem_of_perm {a : α} {l₁ l₂ : list α} : l₁ ~ l₂ → a ∉ l₁ → a ∉ l₂ := assume p nainl₁ ainl₂, nainl₁ (mem_of_perm p.symm ainl₂) theorem mem_iff_mem_of_perm {a : α} {l₁ l₂ : list α} (h : l₁ ~ l₂) : a ∈ l₁ ↔ a ∈ l₂ := iff.intro (mem_of_perm h) (mem_of_perm h.symm) theorem perm_app_left {l₁ l₂ : list α} (t₁ : list α) (p : l₁ ~ l₂) : (l₁++t₁) ~ (l₂++t₁) := perm.rec_on p (perm.refl ([] ++ t₁)) (λ x l₁ l₂ p₁ r₁, skip x r₁) (λ x y l, swap x y _) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) theorem perm_app_right {t₁ t₂ : list α} : ∀ (l : list α), t₁ ~ t₂ → (l++t₁) ~ (l++t₂) | [] p := p | (x::xs) p := skip x (perm_app_right xs p) theorem perm_app {l₁ l₂ t₁ t₂ : list α} : l₁ ~ l₂ → t₁ ~ t₂ → (l₁++t₁) ~ (l₂++t₂) := assume p₁ p₂, trans (perm_app_left t₁ p₁) (perm_app_right l₂ p₂) --theorem perm_app_cons (a : α) {h₁ h₂ t₁ t₂ : list α} : -- h₁ ~ h₂ → t₁ ~ t₂ → (h₁ ++ (a::t₁)) ~ (h₂ ++ (a::t₂)) := --assume p₁ p₂, perm_app p₁ (skip a p₂) theorem perm_cons_app (a : α) : ∀ (l : list α), (a::l) ~ (l ++ [a]) | [] := perm.refl _ | (x::xs) := trans (swap x a xs) $ skip x (perm_cons_app xs) @[simp] theorem perm_cons_app_simp (a : α) (l : list α) : (l ++ [a]) ~ (a::l) := perm.symm (perm_cons_app a l) @[simp] theorem perm_app_comm : ∀ {l₁ l₂ : list α}, (l₁++l₂) ~ (l₂++l₁) | [] l₂ := by simp | (a::t) l₂ := calc a::(t++l₂) ~ a::(l₂++t) : skip a perm_app_comm ... ~ l₂++t++[a] : perm_cons_app _ _ ... = l₂++(t++[a]) : by rw append.assoc ... ~ l₂++(a::t) : perm_app_right l₂ (perm.symm (perm_cons_app a t)) theorem length_eq_length_of_perm {l₁ l₂ : list α} (p : l₁ ~ l₂) : length l₁ = length l₂ := perm.rec_on p rfl (λ x l₁ l₂ p r, by simp[r]) (λ x y l, by simp) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, eq.trans r₁ r₂) theorem eq_nil_of_perm_nil {l₁ : list α} (p : ([] : list α) ~ l₁) : l₁ = ([] : list α) := eq_nil_of_length_eq_zero (length_eq_length_of_perm p).symm theorem not_perm_nil_cons (x : α) (l : list α) : ¬ [] ~ (x::l) := assume p, by note h := eq_nil_of_perm_nil p; contradiction theorem eq_singleton_of_perm {a b : α} (p : [a] ~ [b]) : a = b := have a ∈ [b], from mem_of_perm p (by simp), by simp at this; simph theorem eq_singleton_of_perm_inv {a : α} {l : list α} (p : [a] ~ l) : l = [a] := match l, length_eq_length_of_perm p, p with | [a'], rfl, p := by simp [eq_singleton_of_perm p] end theorem perm_rev : ∀ (l : list α), l ~ (reverse l) | [] := nil | (x::xs) := calc x::xs ~ x::reverse xs : skip x (perm_rev xs) ... ~ reverse xs ++ [x] : perm_cons_app _ _ ... = reverse (x::xs) : by rw [reverse_cons, concat_eq_append] @[simp] theorem perm_rev_simp (l : list α) : (reverse l) ~ l := perm.symm (perm_rev l) theorem perm_middle (a : α) (l₁ l₂ : list α) : (a::l₁)++l₂ ~ l₁++(a::l₂) := have a::l₁++l₂ ~ l₁++[a]++l₂, from perm_app_left l₂ (perm_cons_app a l₁), by simp at this; exact this attribute [simp] theorem perm_middle_simp (a : α) (l₁ l₂ : list α) : l₁++(a::l₂) ~ (a::l₁)++l₂ := perm.symm $ perm_middle a l₁ l₂ theorem perm_cons_app_cons {l l₁ l₂ : list α} (a : α) (p : l ~ l₁++l₂) : a::l ~ l₁++(a::l₂) := trans (skip a p) $ perm_middle a l₁ l₂ open decidable theorem perm_erase [decidable_eq α] {a : α} : ∀ {l : list α}, a ∈ l → l ~ a:: l.erase a | [] h := false.elim h | (x::t) h := if ax : x = a then by rw [ax, erase_cons_head] else by rw [erase_cons_tail _ ax]; exact have aint : a ∈ t, from mem_of_ne_of_mem (take h, ax h.symm) h, trans (skip _ $ perm_erase aint) (swap _ _ _) @[elab_as_eliminator] theorem perm_induction_on {P : list α → list α → Prop} {l₁ l₂ : list α} (p : l₁ ~ l₂) (h₁ : P [] []) (h₂ : ∀ x l₁ l₂, l₁ ~ l₂ → P l₁ l₂ → P (x::l₁) (x::l₂)) (h₃ : ∀ x y l₁ l₂, l₁ ~ l₂ → P l₁ l₂ → P (y::x::l₁) (x::y::l₂)) (h₄ : ∀ l₁ l₂ l₃, l₁ ~ l₂ → l₂ ~ l₃ → P l₁ l₂ → P l₂ l₃ → P l₁ l₃) : P l₁ l₂ := have P_refl : ∀ l, P l l, from take l, list.rec_on l h₁ (λ x xs ih, h₂ x xs xs (perm.refl xs) ih), perm.rec_on p h₁ h₂ (λ x y l, h₃ x y l l (perm.refl l) (P_refl l)) h₄ theorem xswap {l₁ l₂ : list α} (x y : α) : l₁ ~ l₂ → x::y::l₁ ~ y::x::l₂ := assume p, calc x::y::l₁ ~ y::x::l₁ : swap y x l₁ ... ~ y::x::l₂ : skip y (skip x p) @[congr] theorem perm_map (f : α → β) {l₁ l₂ : list α} : l₁ ~ l₂ → map f l₁ ~ map f l₂ := assume p, perm_induction_on p nil (λ x l₁ l₂ p r, skip (f x) r) (λ x y l₁ l₂ p r, xswap (f y) (f x) r) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) /- TODO(Jeremy) In the next section, the decidability proof works, but gave the following error: equation compiler failed to generate bytecode for auxiliary declaration 'list.perm.decidable_perm_aux._main' nested exception message: code generation failed, inductive predicate 'eq' is not supported So I will comment it out and give another decidability proof below. -/ /- /- permutation is decidable if α has decidable equality -/ section dec open decidable variable [Ha : decidable_eq α] include Ha def decidable_perm_aux : ∀ (n : nat) (l₁ l₂ : list α), length l₁ = n → length l₂ = n → decidable (l₁ ~ l₂) | 0 l₁ l₂ H₁ H₂ := have l₁n : l₁ = [], from eq_nil_of_length_eq_zero H₁, have l₂n : l₂ = [], from eq_nil_of_length_eq_zero H₂, begin rw [l₁n, l₂n], exact (is_true perm.nil) end | (n+1) (x::t₁) l₂ H₁ H₂ := by_cases (assume xinl₂ : x ∈ l₂, -- TODO(Jeremy): it seems the equation editor abstracts t₂, and loses the definition, so -- I had to expand it manually -- let t₂ : list α := erase x l₂ in have len_t₁ : length t₁ = n, begin simp at H₁, assert H₁' : nat.succ (length t₁) = nat.succ n, exact H₁, injection H₁' with e, exact e end, have length (erase x l₂) = nat.pred (length l₂), from length_erase_of_mem xinl₂, have length (erase x l₂) = n, begin rw [this, H₂], reflexivity end, match decidable_perm_aux n t₁ (erase x l₂) len_t₁ this with | is_true p := is_true (calc x::t₁ ~ x::erase x l₂ : skip x p ... ~ l₂ : perm.symm (perm_erase xinl₂)) | is_false np := is_false (λ p : x::t₁ ~ l₂, have erase x (x::t₁) ~ erase x l₂, from erase_perm_erase_of_perm x p, have t₁ ~ erase x l₂, begin rw [erase_cons_head] at this, exact this end, absurd this np) end) (assume nxinl₂ : x ∉ l₂, is_false (λ p : x::t₁ ~ l₂, absurd (mem_of_perm p (mem_cons_self _ _)) nxinl₂)) attribute [instance] definition decidable_perm : ∀ (l₁ l₂ : list α), decidable (l₁ ~ l₂) := λ l₁ l₂, by_cases (assume eql : length l₁ = length l₂, decidable_perm_aux (length l₂) l₁ l₂ eql rfl) (assume neql : length l₁ ≠ length l₂, is_false (λ p : l₁ ~ l₂, absurd (length_eq_length_of_perm p) neql)) end dec -/ section count variable [decα : decidable_eq α] include decα theorem count_eq_count_of_perm {l₁ l₂ : list α} : l₁ ~ l₂ → ∀ a, count a l₁ = count a l₂ := suppose l₁ ~ l₂, perm.rec_on this (λ a, rfl) (λ x l₁ l₂ p h a, begin simp [count_cons', h a] end) (λ x y l a, begin simp [count_cons'] end) (λ l₁ l₂ l₃ p₁ p₂ h₁ h₂ a, eq.trans (h₁ a) (h₂ a)) theorem perm_of_forall_count_eq : ∀ {l₁ l₂ : list α}, (∀ a, count a l₁ = count a l₂) → l₁ ~ l₂ | [] := take l₂, assume h : ∀ a, count a [] = count a l₂, have ∀ a, a ∉ l₂, from take a, not_mem_of_count_eq_zero (by simp [(h a).symm]), have l₂ = [], from eq_nil_of_forall_not_mem this, show [] ~ l₂, by rw this | (b :: l) := take l₂, assume h : ∀ a, count a (b :: l) = count a l₂, have b ∈ l₂, from mem_of_count_pos (begin rw [-(h b)], simp, apply nat.succ_pos end), have l₂ ~ b :: l₂.erase b, from perm_erase this, have ∀ a, count a l = count a (l₂.erase b), from take a, if h' : a = b then nat.succ_inj (calc count a l + 1 = count a (b :: l) : begin simp [h'], rw add_comm end ... = count a l₂ : by rw h ... = count a (b :: l₂.erase b) : count_eq_count_of_perm (by assumption) a ... = count a (l₂.erase b) + 1 : begin simp [h'], rw add_comm end) else calc count a l = count a (b :: l) : by simp [h'] ... = count a l₂ : by rw h ... = count a (b :: l₂.erase b) : count_eq_count_of_perm (by assumption) a ... = count a (l₂.erase b) : by simp [h'], have l ~ l₂.erase b, from perm_of_forall_count_eq this, calc b :: l ~ b :: l₂.erase b : skip b this ... ~ l₂ : perm.symm (by assumption) theorem perm_iff_forall_count_eq_count (l₁ l₂ : list α) : l₁ ~ l₂ ↔ ∀ a, count a l₁ = count a l₂ := iff.intro count_eq_count_of_perm perm_of_forall_count_eq -- This next theorem shows that perm is equivalent to a decidable (and efficiently checkable) -- property. theorem perm_iff_forall_mem_count_eq_count (l₁ l₂ : list α) : l₁ ~ l₂ ↔ ∀ a ∈ erase_dup (l₁ ∪ l₂), count a l₁ = count a l₂ := iff.intro (assume h : l₁ ~ l₂, take a, assume ha, count_eq_count_of_perm h a) (assume h, have ∀ a, count a l₁ = count a l₂, from take a, if hl₁ : a ∈ l₁ then have a ∈ erase_dup (l₁ ∪ l₂), from mem_erase_dup (mem_union_left hl₁ l₂), h a this else if hl₂ : a ∈ l₂ then have a ∈ erase_dup (l₁ ∪ l₂), from mem_erase_dup (mem_union_right l₁ hl₂), h a this else by simp [hl₁, hl₂], perm_of_forall_count_eq this) instance : ∀ (l₁ l₂ : list α), decidable (l₁ ~ l₂) := take l₁ l₂, decidable_of_decidable_of_iff (decidable_forall_mem _) (perm_iff_forall_mem_count_eq_count l₁ l₂).symm end count -- Auxiliary theorem for performing cases-analysis on l₂. -- We use it to prove perm_inv_core. private theorem discr {P : Prop} {a b : α} {l₁ l₂ l₃ : list α} : a::l₁ = l₂++(b::l₃) → (l₂ = [] → a = b → l₁ = l₃ → P) → (∀ t, l₂ = a::t → l₁ = t++(b::l₃) → P) → P := match l₂ with | [] := λ e h₁ h₂, begin simp at e, injection e with e₁ e₂, exact h₁ rfl e₁ e₂ end | h::t := λ e h₁ h₂, begin simp at e, injection e with e₁ e₂, rw e₁ at h₂, exact h₂ t rfl e₂ end end -- Auxiliary theorem for performing cases-analysis on l₂. -- We use it to prove perm_inv_core. private theorem discr₂ {P : Prop} {a b c : α} {l₁ l₂ l₃ : list α} (e : a::b::l₁ = l₂++(c::l₃)) (H₁ : l₂ = [] → l₃ = b::l₁ → a = c → P) (H₂ : l₂ = [a] → b = c → l₁ = l₃ → P) (H₃ : ∀ t, l₂ = a::b::t → l₁ = t++(c::l₃) → P) : P := discr e (λh₁ h₂ h₃, H₁ h₁ h₃.symm h₂) $ λt h₁ h₂, discr h₂ (λh₃ h₄, match t, h₃, h₁ with ._, rfl, h₁ := H₂ h₁ h₄ end) (λt' h₃ h₄, match t, h₃, h₁ with ._, rfl, h₁ := H₃ t' h₁ h₄ end) /- quasiequal a l l' means that l' is exactly l, with a added once somewhere -/ section qeq inductive qeq (a : α) : list α → list α → Prop | qhead : ∀ l, qeq l (a::l) | qcons : ∀ (b : α) {l l' : list α}, qeq l l' → qeq (b::l) (b::l') open qeq notation l' `≈`:50 a `|` l:50 := qeq a l l' lemma perm_of_qeq {a : α} {l₁ l₂ : list α} : l₁≈a|l₂ → l₁~a::l₂ := assume q, qeq.rec_on q (λ h, perm.refl (a :: h)) (λ b t₁ t₂ q₁ r₁, calc b::t₂ ~ b::a::t₁ : skip b r₁ ... ~ a::b::t₁ : swap a b t₁) theorem qeq_app : ∀ (l₁ : list α) (a : α) (l₂ : list α), l₁ ++ (a :: l₂) ≈ a | l₁ ++ l₂ | ([] : list α) b l₂ := qhead b l₂ | (a::ains) b l₂ := qcons a (qeq_app ains b l₂) theorem mem_head_of_qeq {a : α} : ∀ {l₁ l₂ : list α}, l₁ ≈ a | l₂ → a ∈ l₁ | ._ ._ (qhead .(a) l) := mem_cons_self a l | ._ ._ (@qcons .(α) .(a) b l l' q) := mem_cons_of_mem b (mem_head_of_qeq q) theorem mem_tail_of_qeq {a : α} : ∀ {l₁ l₂ : list α}, l₁ ≈ a | l₂ → ∀ {b}, b ∈ l₂ → b ∈ l₁ | ._ ._ (qhead .(a) l) b bl := mem_cons_of_mem a bl | ._ ._ (@qcons .(α) .(a) c l l' q) b bcl := or.elim (eq_or_mem_of_mem_cons bcl) (take bc : b = c, begin rw bc, apply mem_cons_self end) (take bl : b ∈ l, have bl' : b ∈ l', from mem_tail_of_qeq q bl, mem_cons_of_mem c bl') theorem mem_cons_of_qeq {a : α} : ∀ {l₁ l₂ : list α}, l₁≈a|l₂ → ∀ {b}, b ∈ l₁ → b ∈ a::l₂ | ._ ._ (qhead ._ l) b bal := bal | ._ ._ (@qcons ._ ._ c l l' q) b (bcl' : b ∈ c :: l') := show b ∈ a :: c :: l, from or.elim (eq_or_mem_of_mem_cons bcl') (take bc : b = c, begin rw bc, apply mem_cons_of_mem, apply mem_cons_self end) (take bl' : b ∈ l', have b ∈ a :: l, from mem_cons_of_qeq q bl', or.elim (eq_or_mem_of_mem_cons this) (take ba : b = a, begin rw ba, apply mem_cons_self end) (take bl : b ∈ l, mem_cons_of_mem a (mem_cons_of_mem c bl))) theorem length_eq_of_qeq {a : α} {l₁ l₂ : list α} : l₁ ≈ a | l₂ → length l₁ = nat.succ (length l₂) := begin intro q, induction q with l b l l' q ih, simp[nat.one_add], simph end theorem qeq_of_mem {a : α} {l : list α} : a ∈ l → (∃ l', l ≈ a | l') := list.rec_on l (λ h : a ∈ ([] : list α), absurd h (not_mem_nil a)) (λ b bs r ainbbs, or.elim (eq_or_mem_of_mem_cons ainbbs) (λ aeqb : a = b, have ∃ l, b::bs ≈ b | l, from exists.intro bs (qhead b bs), begin rw aeqb, exact this end) (λ ainbs : a ∈ bs, have ∃ l', bs ≈ a|l', from r ainbs, exists.elim this (take (l' : list α) (q : bs ≈ a|l'), have b::bs ≈ a | b::l', from qcons b q, exists.intro (b::l') this))) theorem qeq_split {a : α} : ∀ {l l' : list α}, l'≈a|l → ∃ l₁ l₂, l = l₁ ++ l₂ ∧ l' = l₁ ++ (a::l₂) | ._ ._ (qhead ._ l) := ⟨[], l, by simp⟩ | ._ ._ (@qcons ._ ._ c l l' q) := match (qeq_split q) with | ⟨l₁, l₂, h₁, h₂⟩ := ⟨c :: l₁, l₂, by simp [h₁, h₂]⟩ end --theorem subset_of_mem_of_subset_of_qeq {a : α} {l : list α} {u v : list α} : -- a ∉ l → a::l ⊆ v → v≈a|u → l ⊆ u := --λ (nainl : a ∉ l) (s : a::l ⊆ v) (q : v≈a|u) (b : α) (binl : b ∈ l), -- have b ∈ v, from s (or.inr binl), -- have b ∈ a::u, from mem_cons_of_qeq q this, -- or.elim (eq_or_mem_of_mem_cons this) -- (suppose b = a, begin subst b, contradiction end) -- (suppose b ∈ u, this) --end qeq theorem perm_inv_core {l₁ l₂ : list α} (p' : l₁ ~ l₂) : ∀ {a s₁ s₂}, l₁≈a|s₁ → l₂≈a|s₂ → s₁ ~ s₂ := perm_induction_on p' (λ a s₁ s₂ e₁ e₂, match e₁ with end) (λ x t₁ t₂ p (r : ∀{a s₁ s₂}, t₁≈a|s₁ → t₂≈a|s₂ → s₁ ~ s₂) a s₁ s₂ e₁ e₂, match s₁, s₂, qeq_split e₁, qeq_split e₂ with ._, ._, ⟨s₁₁, s₁₂, rfl, C₁⟩, ⟨s₂₁, s₂₂, rfl, C₂⟩ := discr C₁ (λe₁ xa, match s₁₁, x, e₁, xa, C₂ with ._, ._, rfl, rfl, C₂ := discr C₂ (λe₁ _ e₂ e₃, match s₁₂, s₂₁, s₂₂, e₁, e₂, e₃ with | ._, ._, ._, rfl, rfl, rfl := p end) (λt₃ e₁ e₂ e₃, match s₂₁, s₁₂, t₂, e₁, e₂, e₃, p with | ._, ._, ._, rfl, rfl, rfl, p := trans p (perm_middle _ _ _).symm end) end) (λt₃ e₁ e₂, match s₁₁, t₁, e₁, e₂, C₂, p, @r with ._, ._, rfl, rfl, C₂, p, r := discr C₂ (λe₁ xa e₂, match x, s₂₁, s₂₂, xa, e₁, e₂ with | ._, ._, ._, rfl, rfl, rfl := trans (perm_middle _ _ _) p end) (λt₃ e₁ e₂, match s₂₁, t₂, e₁, e₂, @r with | ._, ._, rfl, rfl, r := skip x (r (qeq_app _ _ _) (qeq_app _ _ _)) end) end) end) (λ x y t₁ t₂ p (r : ∀{a s₁ s₂}, t₁≈a|s₁ → t₂≈a|s₂ → s₁ ~ s₂) a s₁ s₂ e₁ e₂, match s₁, s₂, qeq_split e₁, qeq_split e₂ with ._, ._, ⟨s₁₁, s₁₂, rfl, C₁⟩, ⟨s₂₁, s₂₂, rfl, C₂⟩ := discr₂ C₁ (λe₁ e₂ ya, match s₁₁, s₁₂, y, e₁, e₂, ya, C₂ with ._, ._, ._, rfl, rfl, rfl, C₂ := discr₂ C₂ (λe₁ e₂ xa, match s₂₁, s₂₂, x, e₁, e₂, xa with | ._, ._, ._, rfl, rfl, rfl := skip a p end) (λe₁ _ e₂, match s₂₁, s₂₂, e₁, e₂ with | ._, ._, rfl, rfl := skip x p end) (λt₃ e₁ e₂, match s₂₁, t₂, e₁, e₂, p with | ._, ._, rfl, rfl, p := skip x (trans p (perm_middle _ _ _).symm) end) end) (λe₁ xa e₂, match s₁₁, s₁₂, x, e₁, e₂, xa, C₂ with ._, ._, ._, rfl, rfl, rfl, C₂ := discr₂ C₂ (λe₁ e₂ _, match s₂₁, s₂₂, e₁, e₂ with | ._, ._, rfl, rfl := skip y p end) (λe₁ ya e₂, match s₂₁, s₂₂, y, e₁, e₂, ya with | ._, ._, ._, rfl, rfl, rfl := skip a p end) (λt₃ e₁ e₂, match s₂₁, t₂, e₁, e₂, p with | ._, ._, rfl, rfl, p := trans (skip y $ trans p (perm_middle _ _ _).symm) (swap _ _ _) end) end) (λt₃ e₁ e₂, match s₁₁, t₁, e₁, e₂, C₂, p, @r with ._, ._, rfl, rfl, C₂, p, r := discr₂ C₂ (λe₁ e₂ xa, match s₂₁, s₂₂, x, e₁, e₂, xa with | ._, ._, ._, rfl, rfl, rfl := skip y (trans (perm_middle _ _ _) p) end) (λe₁ ya e₂, match s₂₁, s₂₂, y, e₁, e₂, ya with | ._, ._, ._, rfl, rfl, rfl := trans (swap _ _ _) (skip x $ trans (perm_middle _ _ _) p) end) (λt₄ e₁ e₂, match s₂₁, t₂, e₁, e₂, @r with | ._, ._, rfl, rfl, r := trans (swap _ _ _) (skip x $ skip y $ r (qeq_app _ _ _) (qeq_app _ _ _)) end) end) end) (λ t₁ t₂ t₃ p₁ p₂ (r₁ : ∀{a s₁ s₂}, t₁ ≈ a|s₁ → t₂≈a|s₂ → s₁ ~ s₂) (r₂ : ∀{a s₁ s₂}, t₂ ≈ a|s₁ → t₃≈a|s₂ → s₁ ~ s₂) a s₁ s₂ e₁ e₂, let ⟨t₂', e₂'⟩ := qeq_of_mem $ mem_of_perm p₁ $ mem_head_of_qeq e₁ in trans (r₁ e₁ e₂') (r₂ e₂' e₂)) end qeq theorem perm_cons_inv {a : α} {l₁ l₂ : list α} (p : a::l₁ ~ a::l₂) : l₁ ~ l₂ := perm_inv_core p (qeq.qhead a l₁) (qeq.qhead a l₂) theorem perm_app_inv_left {l₁ l₂ : list α} : ∀ {l}, l++l₁ ~ l++l₂ → l₁ ~ l₂ | [] p := p | (a::l) p := perm_app_inv_left (perm_cons_inv p) theorem perm_app_inv_right {l₁ l₂ l : list α} (p : l₁++l ~ l₂++l) : l₁ ~ l₂ := perm_app_inv_left $ trans perm_app_comm $ trans p perm_app_comm theorem perm_app_inv {a : α} {l₁ l₂ l₃ l₄ : list α} (p : l₁++(a::l₂) ~ l₃++(a::l₄)) : l₁++l₂ ~ l₃++l₄ := perm_cons_inv $ trans (perm_middle _ _ _) $ trans p $ (perm_middle _ _ _).symm theorem foldl_eq_of_perm {f : β → α → β} {l₁ l₂ : list α} (rcomm : right_commutative f) (p : l₁ ~ l₂) : ∀ b, foldl f b l₁ = foldl f b l₂ := perm_induction_on p (λ b, rfl) (λ x t₁ t₂ p r b, r (f b x)) (λ x y t₁ t₂ p r b, by simp; rw rcomm; exact r (f (f b x) y)) (λ t₁ t₂ t₃ p₁ p₂ r₁ r₂ b, eq.trans (r₁ b) (r₂ b)) theorem foldr_eq_of_perm {f : α → β → β} {l₁ l₂ : list α} (lcomm : left_commutative f) (p : l₁ ~ l₂) : ∀ b, foldr f b l₁ = foldr f b l₂ := perm_induction_on p (λ b, rfl) (λ x t₁ t₂ p r b, by simp; rw [r b]) (λ x y t₁ t₂ p r b, by simp; rw [lcomm, r b]) (λ t₁ t₂ t₃ p₁ p₂ r₁ r₂ a, eq.trans (r₁ a) (r₂ a)) -- attribute [congr] theorem erase_perm_erase_of_perm [decidable_eq α] (a : α) {l₁ l₂ : list α} (p : l₁ ~ l₂) : l₁.erase a ~ l₂.erase a := if h₁ : a ∈ l₁ then have h₂ : a ∈ l₂, from mem_of_perm p h₁, perm_cons_inv $ trans (perm_erase h₁).symm $ trans p (perm_erase h₂) else have h₂ : a ∉ l₂, from not_mem_of_perm p h₁, by rw [erase_of_not_mem h₁, erase_of_not_mem h₂]; exact p -- attribute [congr] theorem perm_erase_dup_of_perm [H : decidable_eq α] {l₁ l₂ : list α} : l₁ ~ l₂ → erase_dup l₁ ~ erase_dup l₂ := assume p, perm_induction_on p nil (λ x t₁ t₂ p r, by_cases (λ xint₁ : x ∈ t₁, have xint₂ : x ∈ t₂, from mem_of_mem_erase_dup (mem_of_perm r (mem_erase_dup xint₁)), begin rw [erase_dup_cons_of_mem xint₁, erase_dup_cons_of_mem xint₂], exact r end) (λ nxint₁ : x ∉ t₁, have nxint₂ : x ∉ t₂, from assume xint₂ : x ∈ t₂, absurd (mem_of_mem_erase_dup (mem_of_perm (perm.symm r) (mem_erase_dup xint₂))) nxint₁, begin rw [erase_dup_cons_of_not_mem nxint₂, erase_dup_cons_of_not_mem nxint₁], exact (skip x r) end)) (λ y x t₁ t₂ p r, by_cases (λ xinyt₁ : x ∈ y::t₁, by_cases (λ yint₁ : y ∈ t₁, have yint₂ : y ∈ t₂, from mem_of_mem_erase_dup (mem_of_perm r (mem_erase_dup yint₁)), have yinxt₂ : y ∈ x::t₂, from or.inr (yint₂), or.elim (eq_or_mem_of_mem_cons xinyt₁) (λ xeqy : x = y, have xint₂ : x ∈ t₂, begin rw [-xeqy] at yint₂, exact yint₂ end, begin rw [erase_dup_cons_of_mem xinyt₁, erase_dup_cons_of_mem yinxt₂, erase_dup_cons_of_mem yint₁, erase_dup_cons_of_mem xint₂], exact r end) (λ xint₁ : x ∈ t₁, have xint₂ : x ∈ t₂, from mem_of_mem_erase_dup (mem_of_perm r (mem_erase_dup xint₁)), begin rw [erase_dup_cons_of_mem xinyt₁, erase_dup_cons_of_mem yinxt₂, erase_dup_cons_of_mem yint₁, erase_dup_cons_of_mem xint₂], exact r end)) (λ nyint₁ : y ∉ t₁, have nyint₂ : y ∉ t₂, from assume yint₂ : y ∈ t₂, absurd (mem_of_mem_erase_dup (mem_of_perm (perm.symm r) (mem_erase_dup yint₂))) nyint₁, by_cases (λ xeqy : x = y, have nxint₂ : x ∉ t₂, begin rw [-xeqy] at nyint₂, exact nyint₂ end, have yinxt₂ : y ∈ x::t₂, begin rw [xeqy], apply mem_cons_self end, begin rw [erase_dup_cons_of_mem xinyt₁, erase_dup_cons_of_mem yinxt₂, erase_dup_cons_of_not_mem nyint₁, erase_dup_cons_of_not_mem nxint₂, xeqy], exact skip y r end) (λ xney : x ≠ y, have x ∈ t₁, from or_resolve_right xinyt₁ xney, have x ∈ t₂, from mem_of_mem_erase_dup (mem_of_perm r (mem_erase_dup this)), have y ∉ x::t₂, from suppose y ∈ x::t₂, or.elim (eq_or_mem_of_mem_cons this) (λ h, absurd h (ne.symm xney)) (λ h, absurd h nyint₂), begin rw [erase_dup_cons_of_mem xinyt₁, erase_dup_cons_of_not_mem ‹y ∉ x::t₂›, erase_dup_cons_of_not_mem nyint₁, erase_dup_cons_of_mem ‹x ∈ t₂›], exact skip y r end))) (λ nxinyt₁ : x ∉ y::t₁, have xney : x ≠ y, from ne_of_not_mem_cons nxinyt₁, have nxint₁ : x ∉ t₁, from not_mem_of_not_mem_cons nxinyt₁, have nxint₂ : x ∉ t₂, from assume xint₂ : x ∈ t₂, absurd (mem_of_mem_erase_dup (mem_of_perm (perm.symm r) (mem_erase_dup xint₂))) nxint₁, by_cases (λ yint₁ : y ∈ t₁, have yinxt₂ : y ∈ x::t₂, from or.inr (mem_of_mem_erase_dup (mem_of_perm r (mem_erase_dup yint₁))), begin rw [erase_dup_cons_of_not_mem nxinyt₁, erase_dup_cons_of_mem yinxt₂, erase_dup_cons_of_mem yint₁, erase_dup_cons_of_not_mem nxint₂], exact skip x r end) (λ nyint₁ : y ∉ t₁, have nyinxt₂ : y ∉ x::t₂, from assume yinxt₂ : y ∈ x::t₂, or.elim (eq_or_mem_of_mem_cons yinxt₂) (λ h, absurd h (ne.symm xney)) (λ h, absurd (mem_of_mem_erase_dup (mem_of_perm (r.symm) (mem_erase_dup h))) nyint₁), begin rw [erase_dup_cons_of_not_mem nxinyt₁, erase_dup_cons_of_not_mem nyinxt₂, erase_dup_cons_of_not_mem nyint₁, erase_dup_cons_of_not_mem nxint₂], exact xswap x y r end))) (λ t₁ t₂ t₃ p₁ p₂ r₁ r₂, trans r₁ r₂) section perm_union variable [decidable_eq α] theorem perm_union_left {l₁ l₂ : list α} (t₁ : list α) : l₁ ~ l₂ → (l₁ ∪ t₁) ~ (l₂ ∪ t₁) := begin generalize l₂ l₂, clear l₂, generalize l₁ l₁, clear l₁, induction t₁ with a t ih, { intros l₁ l₂ h, exact h }, exact take l₁ l₂, assume h : l₁ ~ l₂, if ha₁ : a ∈ l₁ then have ha₂ : a ∈ l₂, from mem_of_perm h ha₁, begin simp [ha₁, ha₂], apply ih l₁ l₂ h end else have ha₂ : a ∉ l₂, from assume otherwise, ha₁ (mem_of_perm h.symm otherwise), begin simp [ha₁, ha₂], apply ih, apply perm_app_left, exact h end end lemma perm_insert_insert (x y : α) (l : list α) : insert x (insert y l) ~ insert y (insert x l) := if yl : y ∈ l then if xl : x ∈ l then by simp [xl, yl] else by simp [xl, yl] else if xl : x ∈ l then by simp [xl, yl] else if xy : x = y then by simp [xy, xl, yl] else have h₁ : x ∉ l ++ [y], begin intro h, simp at h, cases h, repeat { contradiction } end, have h₂ : y ∉ l ++ [x], begin intro h, simp at h, cases h with h₃, exact xy h₃.symm, contradiction end, begin simp [xl, yl, h₁, h₂], apply perm_app_right, apply perm.swap end theorem perm_union_right (l : list α) {t₁ t₂ : list α} : t₁ ~ t₂ → (l ∪ t₁) ~ (l ∪ t₂) := begin intro h, generalize l l, clear l, exact perm.rec_on h (λ l, perm.refl l) (take x t₁ t₂, assume ht : t₁ ~ t₂, assume ih, take l, ih _) (take x y t l, begin simp, apply perm_union_left, apply perm_insert_insert end) (λ l₁ l₂ l₃ p₁ p₂ h₁ h₂ l, perm.trans (h₁ l) (h₂ l)) end -- attribute [congr] theorem perm_union {l₁ l₂ t₁ t₂ : list α} : l₁ ~ l₂ → t₁ ~ t₂ → (l₁ ∪ t₁) ~ (l₂ ∪ t₂) := assume p₁ p₂, trans (perm_union_left t₁ p₁) (perm_union_right l₂ p₂) end perm_union section perm_insert variable [H : decidable_eq α] include H -- attribute [congr] theorem perm_insert (a : α) {l₁ l₂ : list α} : l₁ ~ l₂ → (insert a l₁) ~ (insert a l₂) := assume p, if al₁ : a ∈ l₁ then have al₂ : a ∈ l₂, from mem_of_perm p al₁, begin simp [al₁, al₂], exact p end else have al₂ : a ∉ l₂, from assume otherwise, al₁ (mem_of_perm p.symm otherwise), begin simp [al₁, al₂], exact perm_app_left _ p end end perm_insert section perm_inter variable [decidable_eq α] theorem perm_inter_left {l₁ l₂ : list α} (t₁ : list α) : l₁ ~ l₂ → (l₁ ∩ t₁) ~ (l₂ ∩ t₁) := assume p, perm.rec_on p (perm.refl _) (λ x l₁ l₂ p₁ r₁, by_cases (λ xint₁ : x ∈ t₁, begin rw [inter_cons_of_mem _ xint₁, inter_cons_of_mem _ xint₁], exact (skip x r₁) end) (λ nxint₁ : x ∉ t₁, begin rw [inter_cons_of_not_mem _ nxint₁, inter_cons_of_not_mem _ nxint₁], exact r₁ end)) (λ x y l, by_cases (λ yint : y ∈ t₁, by_cases (λ xint : x ∈ t₁, begin rw [inter_cons_of_mem _ xint, inter_cons_of_mem _ yint, inter_cons_of_mem _ yint, inter_cons_of_mem _ xint], apply swap end) (λ nxint : x ∉ t₁, begin rw [inter_cons_of_mem _ yint, inter_cons_of_not_mem _ nxint, inter_cons_of_not_mem _ nxint, inter_cons_of_mem _ yint] end)) (λ nyint : y ∉ t₁, by_cases (λ xint : x ∈ t₁, by rw [inter_cons_of_mem _ xint, inter_cons_of_not_mem _ nyint, inter_cons_of_not_mem _ nyint, inter_cons_of_mem _ xint]) (λ nxint : x ∉ t₁, by rw [inter_cons_of_not_mem _ nxint, inter_cons_of_not_mem _ nyint, inter_cons_of_not_mem _ nyint, inter_cons_of_not_mem _ nxint]))) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) theorem perm_inter_right (l : list α) {t₁ t₂ : list α} : t₁ ~ t₂ → (l ∩ t₁) ~ (l ∩ t₂) := list.rec_on l (λ p, by simp [inter_nil]) (λ x xs r p, by_cases (λ xint₁ : x ∈ t₁, have xint₂ : x ∈ t₂, from mem_of_perm p xint₁, begin rw [inter_cons_of_mem _ xint₁, inter_cons_of_mem _ xint₂], exact (skip _ (r p)) end) (λ nxint₁ : x ∉ t₁, have nxint₂ : x ∉ t₂, from not_mem_of_perm p nxint₁, begin rw [inter_cons_of_not_mem _ nxint₁, inter_cons_of_not_mem _ nxint₂], exact (r p) end)) -- attribute [congr] theorem perm_inter {l₁ l₂ t₁ t₂ : list α} : l₁ ~ l₂ → t₁ ~ t₂ → (l₁ ∩ t₁) ~ (l₂ ∩ t₂) := assume p₁ p₂, trans (perm_inter_left t₁ p₁) (perm_inter_right l₂ p₂) end perm_inter /- extensionality -/ section ext theorem perm_ext : ∀ {l₁ l₂ : list α}, nodup l₁ → nodup l₂ → (∀a, a ∈ l₁ ↔ a ∈ l₂) → l₁ ~ l₂ | [] [] d₁ d₂ e := perm.nil | [] (a₂::t₂) d₁ d₂ e := absurd (iff.mpr (e a₂) (mem_cons_self _ _)) (not_mem_nil a₂) | (a₁::t₁) [] d₁ d₂ e := absurd (iff.mp (e a₁) (mem_cons_self _ _)) (not_mem_nil a₁) | (a₁::t₁) (a₂::t₂) d₁ d₂ e := have a₁ ∈ a₂::t₂, from iff.mp (e a₁) (mem_cons_self _ _), have ∃ s₁ s₂, a₂::t₂ = s₁++(a₁::s₂), from mem_split this, -- obtain (s₁ s₂ : list α) (t₂_eq : a₂::t₂ = s₁++(a₁::s₂)), from this, match this with | ⟨ s₁, s₂, (t₂_eq : a₂::t₂ = s₁++(a₁::s₂)) ⟩ := have dt₂' : nodup (a₁::(s₁++s₂)), from nodup_head (begin rw [t₂_eq] at d₂, exact d₂ end), have eqv : ∀a, a ∈ t₁ ↔ a ∈ s₁++s₂, from take a, iff.intro (suppose a ∈ t₁, have a ∈ a₂::t₂, from iff.mp (e a) (mem_cons_of_mem _ this), have a ∈ s₁++(a₁::s₂), begin rw [t₂_eq] at this, exact this end, or.elim (mem_or_mem_of_mem_append this) (suppose a ∈ s₁, mem_append_left s₂ this) (suppose a ∈ a₁::s₂, or.elim (eq_or_mem_of_mem_cons this) (suppose a = a₁, have a₁ ∉ t₁, from not_mem_of_nodup_cons d₁, begin subst a, contradiction end) (suppose a ∈ s₂, mem_append_right s₁ this))) (suppose a ∈ s₁ ++ s₂, or.elim (mem_or_mem_of_mem_append this) (suppose a ∈ s₁, have a ∈ a₂::t₂, from begin rw [t₂_eq], exact (mem_append_left _ this) end, have a ∈ a₁::t₁, from iff.mpr (e a) this, or.elim (eq_or_mem_of_mem_cons this) (suppose a = a₁, have a₁ ∉ s₁++s₂, from not_mem_of_nodup_cons dt₂', have a₁ ∉ s₁, from not_mem_of_not_mem_append_left this, begin subst a, contradiction end) (suppose a ∈ t₁, this)) (suppose a ∈ s₂, have a ∈ a₂::t₂, from begin rw [t₂_eq], exact (mem_append_right _ (mem_cons_of_mem _ this)) end, have a ∈ a₁::t₁, from iff.mpr (e a) this, or.elim (eq_or_mem_of_mem_cons this) (suppose a = a₁, have a₁ ∉ s₁++s₂, from not_mem_of_nodup_cons dt₂', have a₁ ∉ s₂, from not_mem_of_not_mem_append_right this, begin subst a, contradiction end) (suppose a ∈ t₁, this))), have ds₁s₂ : nodup (s₁++s₂), from nodup_of_nodup_cons dt₂', have nodup t₁, from nodup_of_nodup_cons d₁, calc a₁::t₁ ~ a₁::(s₁++s₂) : skip a₁ (perm_ext this ds₁s₂ eqv) ... ~ s₁++(a₁::s₂) : perm_middle _ _ _ ... = a₂::t₂ : by rw t₂_eq end end ext theorem nodup_of_perm_of_nodup {l₁ l₂ : list α} : l₁ ~ l₂ → nodup l₁ → nodup l₂ := assume h, perm.rec_on h (λ h, h) (λ a l₁ l₂ p ih nd, have nodup l₁, from nodup_of_nodup_cons nd, have nodup l₂, from ih this, have a ∉ l₁, from not_mem_of_nodup_cons nd, have a ∉ l₂, from suppose a ∈ l₂, absurd (mem_of_perm (perm.symm p) this) ‹a ∉ l₁›, nodup_cons ‹a ∉ l₂› ‹nodup l₂›) (λ x y l₁ nd, have nodup (x::l₁), from nodup_of_nodup_cons nd, have nodup l₁, from nodup_of_nodup_cons this, have x ∉ l₁, from not_mem_of_nodup_cons ‹nodup (x::l₁)›, have y ∉ x::l₁, from not_mem_of_nodup_cons nd, have x ≠ y, from suppose x = y, begin subst x, apply absurd (mem_cons_self _ _), apply ‹y ∉ y::l₁› end, -- this line used to be "exact absurd (mem_cons_self _ _) ‹y ∉ y::l₁›, but it's now a syntax error have y ∉ l₁, from not_mem_of_not_mem_cons ‹y ∉ x::l₁›, have x ∉ y::l₁, from not_mem_cons_of_ne_of_not_mem ‹x ≠ y› ‹x ∉ l₁›, have nodup (y::l₁), from nodup_cons ‹y ∉ l₁› ‹nodup l₁›, show nodup (x::y::l₁), from nodup_cons ‹x ∉ y::l₁› ‹nodup (y::l₁)›) (λ l₁ l₂ l₃ p₁ p₂ ih₁ ih₂ nd, ih₂ (ih₁ nd)) /- product -/ section product theorem perm_product_left {l₁ l₂ : list α} (t₁ : list β) : l₁ ~ l₂ → (product l₁ t₁) ~ (product l₂ t₁) := assume p : l₁ ~ l₂, perm.rec_on p (perm.refl _) (λ x l₁ l₂ p r, perm_app (perm.refl (map _ t₁)) r) (λ x y l, let m₁ := map (λ b, (x, b)) t₁ in let m₂ := map (λ b, (y, b)) t₁ in let c := product l t₁ in calc m₂ ++ (m₁ ++ c) = (m₂ ++ m₁) ++ c : by rw append.assoc ... ~ (m₁ ++ m₂) ++ c : perm_app perm_app_comm (perm.refl _) ... = m₁ ++ (m₂ ++ c) : by rw append.assoc) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) theorem perm_product_right (l : list α) {t₁ t₂ : list β} : t₁ ~ t₂ → (product l t₁) ~ (product l t₂) := list.rec_on l (λ p, by simp [nil_product]) (λ (a : α) (t : list α) (r : t₁ ~ t₂ → product t t₁ ~ product t t₂) (p : t₁ ~ t₂), perm_app (perm_map (λ b : β, (a, b)) p) (r p)) attribute [congr] theorem perm_product {l₁ l₂ : list α} {t₁ t₂ : list β} : l₁ ~ l₂ → t₁ ~ t₂ → (product l₁ t₁) ~ (product l₂ t₂) := assume p₁ p₂, trans (perm_product_left t₁ p₁) (perm_product_right l₂ p₂) end product /- filter -/ -- attribute [congr] theorem perm_filter {l₁ l₂ : list α} {p : α → Prop} [decidable_pred p] : l₁ ~ l₂ → (filter p l₁) ~ (filter p l₂) := assume u, perm.rec_on u perm.nil (take x l₁' l₂', assume u' : l₁' ~ l₂', assume u'' : filter p l₁' ~ filter p l₂', decidable.by_cases (suppose p x, begin rw [filter_cons_of_pos _ this, filter_cons_of_pos _ this], apply perm.skip, apply u'' end) (suppose ¬ p x, begin rw [filter_cons_of_neg _ this, filter_cons_of_neg _ this], apply u'' end)) (take x y l, decidable.by_cases (assume H1 : p x, decidable.by_cases (assume H2 : p y, begin rw [filter_cons_of_pos _ H1, filter_cons_of_pos _ H2, filter_cons_of_pos _ H2, filter_cons_of_pos _ H1], apply perm.swap end) (assume H2 : ¬ p y, by rw [filter_cons_of_pos _ H1, filter_cons_of_neg _ H2, filter_cons_of_neg _ H2, filter_cons_of_pos _ H1])) (assume H1 : ¬ p x, decidable.by_cases (assume H2 : p y, by rw [filter_cons_of_neg _ H1, filter_cons_of_pos _ H2, filter_cons_of_pos _ H2, filter_cons_of_neg _ H1]) (assume H2 : ¬ p y, by rw [filter_cons_of_neg _ H1, filter_cons_of_neg _ H2, filter_cons_of_neg _ H2, filter_cons_of_neg _ H1]))) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) end perm end list
001cf9aed3e9d208fbb380bc7006cdb6b9f71726
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/group/ulift.lean
2e65b7c46f8b0f33df9b68218f3d50d086b155ba
[ "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,669
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 data.int.cast.defs import algebra.hom.equiv.basic import algebra.group_with_zero.inj_surj /-! # `ulift` instances for groups and monoids This file defines instances for group, monoid, semigroup and related structures on `ulift` types. (Recall `ulift α` is just a "copy" of a type `α` in a higher universe.) We use `tactic.pi_instance_derive_field`, even though it wasn't intended for this purpose, which seems to work fine. We also provide `ulift.mul_equiv : ulift R ≃* R` (and its additive analogue). -/ universes u v variables {α : Type u} {β : Type*} {x y : ulift.{v} α} namespace ulift @[to_additive] instance has_one [has_one α] : has_one (ulift α) := ⟨⟨1⟩⟩ @[simp, to_additive] lemma one_down [has_one α] : (1 : ulift α).down = 1 := rfl @[to_additive] instance has_mul [has_mul α] : has_mul (ulift α) := ⟨λ f g, ⟨f.down * g.down⟩⟩ @[simp, to_additive] lemma mul_down [has_mul α] : (x * y).down = x.down * y.down := rfl @[to_additive] instance has_div [has_div α] : has_div (ulift α) := ⟨λ f g, ⟨f.down / g.down⟩⟩ @[simp, to_additive] lemma div_down [has_div α] : (x / y).down = x.down / y.down := rfl @[to_additive] instance has_inv [has_inv α] : has_inv (ulift α) := ⟨λ f, ⟨f.down⁻¹⟩⟩ @[simp, to_additive] lemma inv_down [has_inv α] : x⁻¹.down = (x.down)⁻¹ := rfl @[to_additive] instance has_smul [has_smul α β] : has_smul α (ulift β) := ⟨λ n x, up (n • x.down)⟩ @[simp, to_additive] lemma smul_down [has_smul α β] (a : α) (b : ulift.{v} β) : (a • b).down = a • b.down := rfl @[to_additive has_smul, to_additive_reorder 1] instance has_pow [has_pow α β] : has_pow (ulift α) β := ⟨λ x n, up (x.down ^ n)⟩ @[simp, to_additive smul_down, to_additive_reorder 1] lemma pow_down [has_pow α β] (a : ulift.{v} α) (b : β) : (a ^ b).down = a.down ^ b := rfl /-- The multiplicative equivalence between `ulift α` and `α`. -/ @[to_additive "The additive equivalence between `ulift α` and `α`."] def _root_.mul_equiv.ulift [has_mul α] : ulift α ≃* α := { map_mul' := λ x y, rfl, .. equiv.ulift } @[to_additive] instance semigroup [semigroup α] : semigroup (ulift α) := mul_equiv.ulift.injective.semigroup _ $ λ x y, rfl @[to_additive] instance comm_semigroup [comm_semigroup α] : comm_semigroup (ulift α) := equiv.ulift.injective.comm_semigroup _ $ λ x y, rfl @[to_additive] instance mul_one_class [mul_one_class α] : mul_one_class (ulift α) := equiv.ulift.injective.mul_one_class _ rfl $ λ x y, rfl instance mul_zero_one_class [mul_zero_one_class α] : mul_zero_one_class (ulift α) := equiv.ulift.injective.mul_zero_one_class _ rfl rfl $ λ x y, rfl @[to_additive] instance monoid [monoid α] : monoid (ulift α) := equiv.ulift.injective.monoid _ rfl (λ _ _, rfl) (λ _ _, rfl) instance add_monoid_with_one [add_monoid_with_one α] : add_monoid_with_one (ulift α) := { nat_cast := λ n, ⟨n⟩, nat_cast_zero := congr_arg ulift.up nat.cast_zero, nat_cast_succ := λ n, congr_arg ulift.up (nat.cast_succ _), .. ulift.has_one, .. ulift.add_monoid } @[simp] lemma nat_cast_down [add_monoid_with_one α] (n : ℕ) : (n : ulift α).down = n := rfl @[to_additive] instance comm_monoid [comm_monoid α] : comm_monoid (ulift α) := equiv.ulift.injective.comm_monoid _ rfl (λ _ _, rfl) (λ _ _, rfl) instance monoid_with_zero [monoid_with_zero α] : monoid_with_zero (ulift α) := equiv.ulift.injective.monoid_with_zero _ rfl rfl (λ _ _, rfl) (λ _ _, rfl) instance comm_monoid_with_zero [comm_monoid_with_zero α] : comm_monoid_with_zero (ulift α) := equiv.ulift.injective.comm_monoid_with_zero _ rfl rfl (λ _ _, rfl) (λ _ _, rfl) @[to_additive] instance div_inv_monoid [div_inv_monoid α] : div_inv_monoid (ulift α) := equiv.ulift.injective.div_inv_monoid _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) @[to_additive] instance group [group α] : group (ulift α) := equiv.ulift.injective.group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) instance add_group_with_one [add_group_with_one α] : add_group_with_one (ulift α) := { int_cast := λ n, ⟨n⟩, int_cast_of_nat := λ n, congr_arg ulift.up (int.cast_of_nat _), int_cast_neg_succ_of_nat := λ n, congr_arg ulift.up (int.cast_neg_succ_of_nat _), .. ulift.add_monoid_with_one, .. ulift.add_group } @[simp] lemma int_cast_down [add_group_with_one α] (n : ℤ) : (n : ulift α).down = n := rfl @[to_additive] instance comm_group [comm_group α] : comm_group (ulift α) := equiv.ulift.injective.comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) instance group_with_zero [group_with_zero α] : group_with_zero (ulift α) := equiv.ulift.injective.group_with_zero _ rfl rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) instance comm_group_with_zero [comm_group_with_zero α] : comm_group_with_zero (ulift α) := equiv.ulift.injective.comm_group_with_zero _ rfl rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) @[to_additive add_left_cancel_semigroup] instance left_cancel_semigroup [left_cancel_semigroup α] : left_cancel_semigroup (ulift α) := equiv.ulift.injective.left_cancel_semigroup _ (λ _ _, rfl) @[to_additive add_right_cancel_semigroup] instance right_cancel_semigroup [right_cancel_semigroup α] : right_cancel_semigroup (ulift α) := equiv.ulift.injective.right_cancel_semigroup _ (λ _ _, rfl) @[to_additive add_left_cancel_monoid] instance left_cancel_monoid [left_cancel_monoid α] : left_cancel_monoid (ulift α) := equiv.ulift.injective.left_cancel_monoid _ rfl (λ _ _, rfl) (λ _ _, rfl) @[to_additive add_right_cancel_monoid] instance right_cancel_monoid [right_cancel_monoid α] : right_cancel_monoid (ulift α) := equiv.ulift.injective.right_cancel_monoid _ rfl (λ _ _, rfl) (λ _ _, rfl) @[to_additive add_cancel_monoid] instance cancel_monoid [cancel_monoid α] : cancel_monoid (ulift α) := equiv.ulift.injective.cancel_monoid _ rfl (λ _ _, rfl) (λ _ _, rfl) @[to_additive add_cancel_monoid] instance cancel_comm_monoid [cancel_comm_monoid α] : cancel_comm_monoid (ulift α) := equiv.ulift.injective.cancel_comm_monoid _ rfl (λ _ _, rfl) (λ _ _, rfl) instance nontrivial [nontrivial α] : nontrivial (ulift α) := equiv.ulift.symm.injective.nontrivial -- TODO we don't do `ordered_cancel_comm_monoid` or `ordered_comm_group` -- We'd need to add instances for `ulift` in `order.basic`. end ulift
15468b707a69e7f699539b65525baa01d4fea586
0845ae2ca02071debcfd4ac24be871236c01784f
/library/init/data/array/binsearch.lean
5f1111a3d199d76fe9e43833e0b3afcadb9422a9
[ "Apache-2.0" ]
permissive
GaloisInc/lean4
74c267eb0e900bfaa23df8de86039483ecbd60b7
228ddd5fdcd98dd4e9c009f425284e86917938aa
refs/heads/master
1,643,131,356,301
1,562,715,572,000
1,562,715,572,000
192,390,898
0
0
null
1,560,792,750,000
1,560,792,749,000
null
UTF-8
Lean
false
false
1,255
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 -/ prelude import init.data.array.basic universes u v namespace Array -- TODO: remove the [Inhabited α] parameters as soon as we have the tactic framework for automating proof generation and using Array.fget -- TODO: remove `partial` using well-founded recursion @[specialize] partial def binSearchAux {α : Type u} {β : Type v} [Inhabited α] [Inhabited β] (lt : α → α → Bool) (found : Option α → β) (as : Array α) (k : α) : Nat → Nat → β | lo hi := if lo <= hi then let m := (lo + hi)/2; let a := as.get m; if lt a k then binSearchAux (m+1) hi else if lt k a then if m == 0 then found none else binSearchAux lo (m-1) else found (some a) else found none @[inline] def binSearch {α : Type} [Inhabited α] (as : Array α) (k : α) (lt : α → α → Bool) (lo := 0) (hi := as.size - 1) : Option α := binSearchAux lt id as k lo hi @[inline] def binSearchContains {α : Type} [Inhabited α] (as : Array α) (k : α) (lt : α → α → Bool) (lo := 0) (hi := as.size - 1) : Bool := binSearchAux lt Option.isSome as k lo hi end Array
f7a52b0d77daa872b7b3db9aa6b7b7451b238d4a
f7c25cf719a8c52d384031424ca44d6dd755e06c
/src/abelian_inj_iff_div.lean
79d1d772559484baed6f26950911e6dd9f7aef4d
[]
no_license
uzulim/lean_injective_divisible
601e0a357f9b3908a1b3d803ef8a22c4087ea282
9b2b2aa0741af6a2fbbb6cf6897a937b1b83f2dc
refs/heads/master
1,690,088,115,021
1,615,478,572,000
1,615,478,572,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,970
lean
import group_theory.subgroup import algebra import tactic import logic.function.basic import order.zorn import data.set.disjointed open function -- Injective abelian group class is_inj_abgp (G : Type) [add_comm_group G] : Prop := (inj_extd : ∀ (H K : Type) [add_comm_group H] [add_comm_group K] (i : H →+ K) (h_subHK : injective i), (∀f : H →+ G, ∃f_extd : K →+ G, (∀h : H, f_extd (i h) = f h))) -- Divisible abelian group class is_div_abgp (G : Type) [add_comm_group G] : Prop := (nat_div : ∀ (n : ℤ) (g : G) (n_nonzero : n ≠ 0), ∃ (h : G), n •ℤ h = g) -- •ℤ is the ℤ-scalar mult. -- Injective abelian group is divisible. theorem inj_is_div_abgp (G : Type) [add_comm_group G] : is_inj_abgp G → is_div_abgp G := begin /- Outline: Construct a morphism f: ℤ → G given by g-mult. Construct a morphism i: ℤ → ℤ given by n-mult. Apply these to the injectivity hypothesis Let the image of 1 be g0 Since n*1 = n, n*g0 = g. -/ -- Setup intro G_is_inj, refine ⟨_⟩, -- Elaborate the goal intros n g n_nonzero, -- Define two functions f and i set f: ℤ →+ G := {to_fun := λ n, n •ℤ g, map_zero' := by simp, map_add' := by exact add_gsmul g} with fdef, set i: ℤ →+ ℤ := {to_fun := λ m, m •ℤ n, map_zero' := by simp, map_add' := by exact add_gsmul n} with idef, -- i is injective (Clean this section) have i_inj : injective i := begin rw function.injective, intros a b h_iaib, rw idef at h_iaib, simp at h_iaib, cases h_iaib, exact h_iaib, tauto, end, -- Feed our earlier constructions into the assumption that G is injective cases G_is_inj, specialize G_is_inj ℤ ℤ i i_inj f, cases G_is_inj with f_extd G_is_inj_h, specialize G_is_inj_h 1, -- Start with f 1 = g and change this to n •ℤ ⇑f_extd 1 = g have h_f1_is_g : f 1 = g, by apply one_gsmul g, rw h_f1_is_g at G_is_inj_h, rw (by simp : i 1 = n) at G_is_inj_h, rw ← (by simp : n •ℤ 1 = n) at G_is_inj_h, rw ← (by exact (add_monoid_hom.map_gsmul f_extd 1 n).symm : n •ℤ (f_extd 1) = f_extd (n •ℤ 1)) at G_is_inj_h, -- Use f_extd 1 and finish use (f_extd 1), exact G_is_inj_h, end /- Takes the data of H ⊆ K and f: H → G and asks for an intermediate H ⊆ H1 ⊆ K such that f_extd: H1 → G is a lift of f: H → G. -/ structure extd_abhom {H K G : Type} [add_comm_group H] [add_comm_group K] [add_comm_group G] (i0 : H →+ K) (i0_inj: injective i0) (f : H →+ G) := (H1: Type) [h_H1: add_comm_group H1] (i1 : H →+ H1) (i2 : H1 →+ K) (i1_inj: injective i1) (i2_inj: injective i2) (h_comm_HH1K : i2 ∘ i1 = i0) -- commuting inclusions (f_extd : H1 →+ G) (h_lift: f_extd ∘ i1 = f) -- f_extd is a lift of f -- Declare that H1 in an extd_abhom is an add_comm_group. instance {H K G : Type} [add_comm_group H] [add_comm_group K] [add_comm_group G] (i0 : H →+ K) (i0_inj: injective i0) (f : H →+ G) {f_ex : extd_abhom i0 i0_inj f} : add_comm_group f_ex.H1 := f_ex.h_H1 /- Partial order on extd_abhom Each time we need to sandwich between H,K and fix the target G. -/ def ord_extd_abhom {H K G : Type} [add_comm_group H] [add_comm_group K] [add_comm_group G] {i0 : H →+ K} {i0_inj: injective i0} {f : H →+ G} : (extd_abhom i0 i0_inj f) → (extd_abhom i0 i0_inj f) → Prop := begin intros hom1 hom2, exact (∃j : hom1.H1 →+ hom2.H1, ∃j_inj : injective j, -- hom.H1 ⊆ hom2.H1 (hom2.i2 ∘ j = hom1.i2) ∧ -- comm. of inclusion (j ∘ hom1.i1 = hom2.i1) ∧ -- comm. of inclusion (hom2.f_extd ∘ j = hom1.f_extd)), -- comm. of lifts end def ord_extd_abhom_trans {H K G : Type} [add_comm_group H] [add_comm_group K] [add_comm_group G] (i0 : H →+ K) (i0_inj: injective i0) (f : H →+ G) {f1 f2 f3 : extd_abhom i0 i0_inj f} : ord_extd_abhom f1 f2 → ord_extd_abhom f2 f3 → ord_extd_abhom f1 f3 := begin -- intros f1 f2 f3, intros ex_f1f2 ex_f2f3, -- Notations set H1 := f1.H1, set H2 := f2.H1, set H3 := f3.H1, let i11 := f1.i1, let i11_inj := f1.i1_inj, let i21 := f2.i1, let i21_inj := f2.i1_inj, let i31 := f3.i1, let i31_inj := f3.i1_inj, let i12 := f1.i2, let i12_inj := f1.i2_inj, let i22 := f2.i2, let i22_inj := f2.i2_inj, let i32 := f3.i2, let i32_inj := f3.i2_inj, rcases ex_f1f2 with ⟨j1, j1_inj, i12_comm, i12_comm2, ex12_comm⟩, rcases ex_f2f3 with ⟨j2, j2_inj, i23_comm, i23_comm2, ex23_comm⟩, -- j3 is to be used. let j3 := j2.comp j1, -- composition in add_comm_group morphisms have j3_inj : injective j3 := by tauto, use j3, use j3_inj, split, -- Show that i32 ∘ j3 = i12 calc (f3.i2) ∘ j3 = (i32 ∘ j2) ∘ j1 : by refl ... = i22 ∘ j1 : by tauto ... = i12 : by tauto, -- by exact i12_comm, split, -- Show that j3 ∘ i11 = i31 calc j3 ∘ (f1.i1) = (j2 ∘ j1) ∘ i11 : by refl ... = j2 ∘ i21 : by tauto ... = i31 : by tauto, -- by exact i23_comm2, -- Show that extension property is transitive calc f3.f_extd ∘ j3 = (f3.f_extd ∘ j2) ∘ j1 : by refl ... = f2.f_extd ∘ j1 : by tauto ... = f1.f_extd : by tauto, -- by exact ex12_comm, end -- Every chain of extd_abhom's have an upper bound. def ord_extd_abhom_upper {H K G : Type} [add_comm_group H] [add_comm_group K] [add_comm_group G] {i0 : H →+ K} {i0_inj: injective i0} {f : H →+ G} : ∀(chain : set (extd_abhom i0 i0_inj f)), zorn.chain ord_extd_abhom chain → (∃ (ub : extd_abhom i0 i0_inj f), ∀ (a : extd_abhom i0 i0_inj f), a ∈ chain → ord_extd_abhom a ub) := begin intros chain is_chain, rw zorn.chain at is_chain, sorry, end lemma exists_notin_of_ne_top {G : Type} [add_comm_group G] {H : add_subgroup G} (h : H ≠ ⊤) : ∃ x : G, x ∉ H := begin revert h, contrapose!, intro h, rw eq_top_iff, intros y hy, apply h, end lemma extd_mor_from_add_subgps {G : Type} [add_group G] (H1 H2 : add_subgroup G) (f1 : H1 →+ G) (f2 : H2 →+ G) (h : ∀ (x1 : H1) (x2 : H2), (x1 : G) = (x2 : G) → f1 x1 = f2 x2) : ∃ f : ↥(H1 ⊔ H2) →+ G, f.comp (add_subgroup.inclusion le_sup_left) = f1 ∧ f.comp (add_subgroup.inclusion le_sup_right) = f2 := begin sorry, end -- Divisble abelian group is injective. theorem div_is_inj_abgp (G : Type) [add_comm_group G] : is_div_abgp G → is_inj_abgp G := begin /- Outline: Find maximally extendible H0 such that H ⊆ H0 ⊆ K This is done using Zorn's lemma. Proof by contradiction; if H0 = K, we are done. So suppose otherwise. If H0 ⊆ K and not the same, then ∃ k ∈ K, k ∉ H0 Either (ℤ ⬝ k) ∩ H = {0} or not If empty intersection, map anywhere If nonempty intersection, n ⬝ k ∈ H, nonzero. Let g = f (n ⬝ k) find g0 such that n ⬝ g0 = g by div_abgp G Define an extension f1 : (H ∨ ⟨k⟩) → G by mapping k to g0 Show well-definedness (?) Contradiction to maximality. -/ intro divG, refine ⟨_⟩, intros H K H_is_abgp K_is_abgp i0 i0_inj f, -- For any H ⊆ K, f: H → G, resetI, -- telling that H, K are groups, from local context to type class -- Zorn's Lemma have exist_max_extn : ( ∃ (m : extd_abhom i0 i0_inj f), ∀ (a : extd_abhom i0 i0_inj f), ord_extd_abhom m a → ord_extd_abhom a m ) := zorn.exists_maximal_of_chains_bounded ord_extd_abhom_upper (λ _ _ _ , ord_extd_abhom_trans i0 i0_inj f), cases exist_max_extn with max_extn h_max_extn, -- Notations let max_H1 := max_extn.H1, let max_i1 := max_extn.i1, let max_i2 := max_extn.i2, let max_f := max_extn.f_extd, have max_h_H1 := max_extn.h_H1, have max_i1_inj := max_extn.i1_inj, have max_i2_inj := max_extn.i2_inj, have max_f_lift := max_extn.h_lift, let max_H1_sub := max_i2.range, -- Proof by contradiction on the maximal extension taking up everything have max_H1_is_K : max_H1_sub = ⊤, by_contra not_top, have not_top_2 : max_H1_sub ≠ ⊤ := by tauto, have not_top_has_elt := exists_notin_of_ne_top not_top_2, cases not_top_has_elt with k k_notin_top, clear not_top, clear not_top_2, let k_gp : add_subgroup K := add_subgroup.closure {k}, let max_H1_bigger : add_subgroup K := max_H1_sub ⊔ k_gp, by_cases h : (∃n ∈ ℕ, n •ℕ k ∈ max_H1_sub), /- (f1 : H1 →+ G) (f2 : H2 →+ G) (h : ∀ (x1 : H1) (x2 : H2), (x1 : G) = (x2 : G) → f1 x1 = f2 x2) : ∃ f : ↥(H1 ⊔ H2) →+ G, f.comp (add_subgroup.inclusion le_sup_left) = f1 ∧ f.comp (add_subgroup.inclusion le_sup_right) = f2 := -/ -- Assuming that n.k is in max_H1 for some n > 0 -- have mult_k_into_maxH1 : (∃n : ℕ, ∃h : max_H1_sub, n •ℕ k = h), sorry, end -- An abelian group is injective iff divisible. theorem inj_iff_div_abgp (G : Type) [add_comm_group G] : is_inj_abgp G ↔ is_div_abgp G := begin split, exact inj_is_div_abgp G, exact div_is_inj_abgp G, end
fe647f2df3986743816b57b3215ce99f82dec06e
b00eb947a9c4141624aa8919e94ce6dcd249ed70
/src/Lean/Parser/Command.lean
90bee9c1d60a4dab83a8eed4ee5b51b2704e2b2c
[ "Apache-2.0" ]
permissive
gebner/lean4-old
a4129a041af2d4d12afb3a8d4deedabde727719b
ee51cdfaf63ee313c914d83264f91f414a0e3b6e
refs/heads/master
1,683,628,606,745
1,622,651,300,000
1,622,654,405,000
142,608,821
1
0
null
null
null
null
UTF-8
Lean
false
false
9,745
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 -/ import Lean.Parser.Term import Lean.Parser.Do namespace Lean namespace Parser /-- Syntax quotation for terms and (lists of) commands. We prefer terms, so ambiguous quotations like `($x $y) will be parsed as an application, not two commands. Use `($x:command $y:command) instead. Multiple command will be put in a `null node, but a single command will not (so that you can directly match against a quotation in a command kind's elaborator). -/ -- TODO: use two separate quotation parsers with parser priorities instead @[builtinTermParser] def Term.quot := leading_parser "`(" >> incQuotDepth (termParser <|> many1Unbox commandParser) >> ")" @[builtinTermParser] def Term.precheckedQuot := leading_parser "`" >> Term.quot namespace Command def namedPrio := leading_parser (atomic ("(" >> nonReservedSymbol "priority") >> " := " >> priorityParser >> ")") def optNamedPrio := optional namedPrio def «private» := leading_parser "private " def «protected» := leading_parser "protected " def visibility := «private» <|> «protected» def «noncomputable» := leading_parser "noncomputable " def «unsafe» := leading_parser "unsafe " def «partial» := leading_parser "partial " def declModifiers (inline : Bool) := leading_parser optional docComment >> optional (Term.«attributes» >> if inline then skip else ppDedent ppLine) >> optional visibility >> optional «noncomputable» >> optional «unsafe» >> optional «partial» def declId := leading_parser ident >> optional (".{" >> sepBy1 ident ", " >> "}") def declSig := leading_parser many (ppSpace >> (Term.simpleBinderWithoutType <|> Term.bracketedBinder)) >> Term.typeSpec def optDeclSig := leading_parser many (ppSpace >> (Term.simpleBinderWithoutType <|> Term.bracketedBinder)) >> Term.optType def declValSimple := leading_parser " :=\n" >> termParser >> optional Term.whereDecls def declValEqns := leading_parser Term.matchAltsWhereDecls def declVal := declValSimple <|> declValEqns <|> Term.whereDecls def «abbrev» := leading_parser "abbrev " >> declId >> optDeclSig >> declVal def «def» := leading_parser "def " >> declId >> optDeclSig >> declVal def «theorem» := leading_parser "theorem " >> declId >> declSig >> declVal def «constant» := leading_parser "constant " >> declId >> declSig >> optional declValSimple def «instance» := leading_parser Term.attrKind >> "instance " >> optNamedPrio >> optional declId >> declSig >> declVal def «axiom» := leading_parser "axiom " >> declId >> declSig def «example» := leading_parser "example " >> declSig >> declVal def inferMod := leading_parser atomic (symbol "{" >> "}") def ctor := leading_parser "\n| " >> declModifiers true >> ident >> optional inferMod >> optDeclSig def optDeriving := leading_parser optional (atomic ("deriving " >> notSymbol "instance") >> sepBy1 ident ", ") def «inductive» := leading_parser "inductive " >> declId >> optDeclSig >> optional (symbol ":=" <|> "where") >> many ctor >> optDeriving def classInductive := leading_parser atomic (group (symbol "class " >> "inductive ")) >> declId >> optDeclSig >> optional (symbol ":=" <|> "where") >> many ctor >> optDeriving def structExplicitBinder := leading_parser atomic (declModifiers true >> "(") >> many1 ident >> optional inferMod >> optDeclSig >> optional Term.binderDefault >> ")" def structImplicitBinder := leading_parser atomic (declModifiers true >> "{") >> many1 ident >> optional inferMod >> declSig >> "}" def structInstBinder := leading_parser atomic (declModifiers true >> "[") >> many1 ident >> optional inferMod >> declSig >> "]" def structSimpleBinder := leading_parser atomic (declModifiers true >> ident) >> optional inferMod >> optDeclSig >> optional Term.binderDefault def structFields := leading_parser manyIndent (ppLine >> checkColGe >>(structExplicitBinder <|> structImplicitBinder <|> structInstBinder <|> structSimpleBinder)) def structCtor := leading_parser atomic (declModifiers true >> ident >> optional inferMod >> " :: ") def structureTk := leading_parser "structure " def classTk := leading_parser "class " def «extends» := leading_parser " extends " >> sepBy1 termParser ", " def «structure» := leading_parser (structureTk <|> classTk) >> declId >> many Term.bracketedBinder >> optional «extends» >> Term.optType >> optional ((symbol " := " <|> " where ") >> optional structCtor >> structFields) >> optDeriving @[builtinCommandParser] def declaration := leading_parser declModifiers false >> («abbrev» <|> «def» <|> «theorem» <|> «constant» <|> «instance» <|> «axiom» <|> «example» <|> «inductive» <|> classInductive <|> «structure») @[builtinCommandParser] def «deriving» := leading_parser "deriving " >> "instance " >> sepBy1 ident ", " >> " for " >> sepBy1 ident ", " @[builtinCommandParser] def «section» := leading_parser "section " >> optional ident @[builtinCommandParser] def «namespace» := leading_parser "namespace " >> ident @[builtinCommandParser] def «end» := leading_parser "end " >> optional ident @[builtinCommandParser] def «variable» := leading_parser "variable" >> many1 Term.bracketedBinder @[builtinCommandParser] def «universe» := leading_parser "universe " >> ident @[builtinCommandParser] def «universes» := leading_parser "universes " >> many1 ident @[builtinCommandParser] def check := leading_parser "#check " >> termParser @[builtinCommandParser] def check_failure := leading_parser "#check_failure " >> termParser -- Like `#check`, but succeeds only if term does not type check @[builtinCommandParser] def reduce := leading_parser "#reduce " >> termParser @[builtinCommandParser] def eval := leading_parser "#eval " >> termParser @[builtinCommandParser] def synth := leading_parser "#synth " >> termParser @[builtinCommandParser] def exit := leading_parser "#exit" @[builtinCommandParser] def print := leading_parser "#print " >> (ident <|> strLit) @[builtinCommandParser] def printAxioms := leading_parser "#print " >> nonReservedSymbol "axioms " >> ident @[builtinCommandParser] def «resolve_name» := leading_parser "#resolve_name " >> ident @[builtinCommandParser] def «init_quot» := leading_parser "init_quot" def optionValue := nonReservedSymbol "true" <|> nonReservedSymbol "false" <|> strLit <|> numLit @[builtinCommandParser] def «set_option» := leading_parser "set_option " >> ident >> ppSpace >> optionValue def eraseAttr := leading_parser "-" >> ident @[builtinCommandParser] def «attribute» := leading_parser "attribute " >> "[" >> sepBy1 (eraseAttr <|> Term.attrInstance) ", " >> "] " >> many1 ident @[builtinCommandParser] def «export» := leading_parser "export " >> ident >> "(" >> many1 ident >> ")" def openHiding := leading_parser atomic (ident >> "hiding") >> many1 ident def openRenamingItem := leading_parser ident >> unicodeSymbol "→" "->" >> ident def openRenaming := leading_parser atomic (ident >> "renaming") >> sepBy1 openRenamingItem ", " def openOnly := leading_parser atomic (ident >> "(") >> many1 ident >> ")" def openSimple := leading_parser many1 ident def openDecl := openHiding <|> openRenaming <|> openOnly <|> openSimple @[builtinCommandParser] def «open» := leading_parser "open " >> openDecl @[builtinCommandParser] def «mutual» := leading_parser "mutual " >> many1 (ppLine >> notSymbol "end" >> commandParser) >> ppDedent (ppLine >> "end") @[builtinCommandParser] def «initialize» := leading_parser "initialize " >> optional (atomic (ident >> Term.typeSpec >> Term.leftArrow)) >> Term.doSeq @[builtinCommandParser] def «builtin_initialize» := leading_parser "builtin_initialize " >> optional (atomic (ident >> Term.typeSpec >> Term.leftArrow)) >> Term.doSeq @[builtinCommandParser] def «in» := trailing_parser " in " >> commandParser /- This is an auxiliary command for generation constructor injectivity theorems for inductive types defined at `Prelude.lean`. It is meant for bootstrapping purposes only. -/ @[builtinCommandParser] def genInjectiveTheorems := leading_parser "gen_injective_theorems% " >> ident @[runBuiltinParserAttributeHooks] abbrev declModifiersF := declModifiers false @[runBuiltinParserAttributeHooks] abbrev declModifiersT := declModifiers true builtin_initialize register_parser_alias "declModifiers" declModifiersF register_parser_alias "nestedDeclModifiers" declModifiersT register_parser_alias "declId" declId register_parser_alias "declSig" declSig register_parser_alias "declVal" declVal register_parser_alias "optDeclSig" optDeclSig register_parser_alias "openDecl" openDecl end Command namespace Term @[builtinTermParser] def «open» := leading_parser:leadPrec "open " >> Command.openDecl >> " in " >> termParser @[builtinTermParser] def «set_option» := leading_parser:leadPrec "set_option " >> ident >> ppSpace >> Command.optionValue >> " in " >> termParser end Term namespace Tactic @[builtinTacticParser] def «open» := leading_parser:leadPrec "open " >> Command.openDecl >> " in " >> tacticSeq @[builtinTacticParser] def «set_option» := leading_parser:leadPrec "set_option " >> ident >> ppSpace >> Command.optionValue >> " in " >> tacticSeq end Tactic end Parser end Lean
ed5fb018d9ace30a5b8684299f0aa0cd62e71d7d
432d948a4d3d242fdfb44b81c9e1b1baacd58617
/src/group_theory/perm/sign.lean
e6c68be1980453dd05e3882177e488bd2595dc7f
[ "Apache-2.0" ]
permissive
JLimperg/aesop3
306cc6570c556568897ed2e508c8869667252e8a
a4a116f650cc7403428e72bd2e2c4cda300fe03f
refs/heads/master
1,682,884,916,368
1,620,320,033,000
1,620,320,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
43,305
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.fintype.basic import data.finset.sort import data.nat.parity import group_theory.perm.basic import group_theory.order_of_element import tactic.norm_swap import group_theory.quotient_group /-! # Sign of a permutation The main definition of this file is `equiv.perm.sign`, associating a `units ℤ` sign with a permutation. This file also contains miscellaneous lemmas about `equiv.perm` and `equiv.swap`, building on top of those in `data/equiv/basic` and `data/equiv/perm`. -/ universes u v open equiv function fintype finset open_locale big_operators variables {α : Type u} {β : Type v} namespace equiv.perm /-- `mod_swap i j` contains permutations up to swapping `i` and `j`. We use this to partition permutations in `matrix.det_zero_of_row_eq`, such that each partition sums up to `0`. -/ def mod_swap [decidable_eq α] (i j : α) : setoid (perm α) := ⟨λ σ τ, σ = τ ∨ σ = swap i j * τ, λ σ, or.inl (refl σ), λ σ τ h, or.cases_on h (λ h, or.inl h.symm) (λ h, or.inr (by rw [h, swap_mul_self_mul])), λ σ τ υ hστ hτυ, by cases hστ; cases hτυ; try {rw [hστ, hτυ, swap_mul_self_mul]}; finish⟩ instance {α : Type*} [fintype α] [decidable_eq α] (i j : α) : decidable_rel (mod_swap i j).r := λ σ τ, or.decidable lemma perm_inv_on_of_perm_on_finset {s : finset α} {f : perm α} (h : ∀ x ∈ s, f x ∈ s) {y : α} (hy : y ∈ s) : f⁻¹ y ∈ s := begin have h0 : ∀ y ∈ s, ∃ x (hx : x ∈ s), y = (λ i (hi : i ∈ s), f i) x hx := finset.surj_on_of_inj_on_of_card_le (λ x hx, (λ i hi, f i) x hx) (λ a ha, h a ha) (λ a₁ a₂ ha₁ ha₂ heq, (equiv.apply_eq_iff_eq f).mp heq) rfl.ge, obtain ⟨y2, hy2, heq⟩ := h0 y hy, convert hy2, rw heq, simp only [inv_apply_self] end lemma perm_inv_maps_to_of_maps_to (f : perm α) {s : set α} [fintype s] (h : set.maps_to f s s) : set.maps_to (f⁻¹ : _) s s := λ x hx, set.mem_to_finset.mp $ perm_inv_on_of_perm_on_finset (λ a ha, set.mem_to_finset.mpr (h (set.mem_to_finset.mp ha))) (set.mem_to_finset.mpr hx) @[simp] lemma perm_inv_maps_to_iff_maps_to {f : perm α} {s : set α} [fintype s] : set.maps_to (f⁻¹ : _) s s ↔ set.maps_to f s s := ⟨perm_inv_maps_to_of_maps_to f⁻¹, perm_inv_maps_to_of_maps_to f⟩ lemma perm_inv_on_of_perm_on_fintype {f : perm α} {p : α → Prop} [fintype {x // p x}] (h : ∀ x, p x → p (f x)) {x : α} (hx : p x) : p (f⁻¹ x) := begin letI : fintype ↥(show set α, from p) := ‹fintype {x // p x}›, exact perm_inv_maps_to_of_maps_to f h hx end /-- If the permutation `f` maps `{x // p x}` into itself, then this returns the permutation on `{x // p x}` induced by `f`. Note that the `h` hypothesis is weaker than for `equiv.perm.subtype_perm`. -/ abbreviation subtype_perm_of_fintype (f : perm α) {p : α → Prop} [fintype {x // p x}] (h : ∀ x, p x → p (f x)) : perm {x // p x} := f.subtype_perm (λ x, ⟨h x, λ h₂, f.inv_apply_self x ▸ perm_inv_on_of_perm_on_fintype h h₂⟩) @[simp] lemma subtype_perm_of_fintype_apply (f : perm α) {p : α → Prop} [fintype {x // p x}] (h : ∀ x, p x → p (f x)) (x : {x // p x}) : subtype_perm_of_fintype f h x = ⟨f x, h x x.2⟩ := rfl @[simp] lemma subtype_perm_of_fintype_one (p : α → Prop) [fintype {x // p x}] (h : ∀ x, p x → p ((1 : perm α) x)) : @subtype_perm_of_fintype α 1 p _ h = 1 := equiv.ext $ λ ⟨_, _⟩, rfl lemma perm_maps_to_inl_iff_maps_to_inr {m n : Type*} [fintype m] [fintype n] (σ : equiv.perm (m ⊕ n)) : set.maps_to σ (set.range sum.inl) (set.range sum.inl) ↔ set.maps_to σ (set.range sum.inr) (set.range sum.inr) := begin split; id { intros h, classical, rw ←perm_inv_maps_to_iff_maps_to at h, intro x, cases hx : σ x with l r, }, { rintros ⟨a, rfl⟩, obtain ⟨y, hy⟩ := h ⟨l, rfl⟩, rw [←hx, σ.inv_apply_self] at hy, exact absurd hy sum.inl_ne_inr}, { rintros ⟨a, ha⟩, exact ⟨r, rfl⟩, }, { rintros ⟨a, ha⟩, exact ⟨l, rfl⟩, }, { rintros ⟨a, rfl⟩, obtain ⟨y, hy⟩ := h ⟨r, rfl⟩, rw [←hx, σ.inv_apply_self] at hy, exact absurd hy sum.inr_ne_inl}, end lemma mem_sum_congr_hom_range_of_perm_maps_to_inl {m n : Type*} [fintype m] [fintype n] {σ : perm (m ⊕ n)} (h : set.maps_to σ (set.range sum.inl) (set.range sum.inl)) : σ ∈ (sum_congr_hom m n).range := begin classical, have h1 : ∀ (x : m ⊕ n), (∃ (a : m), sum.inl a = x) → (∃ (a : m), sum.inl a = σ x), { rintros x ⟨a, ha⟩, apply h, rw ← ha, exact ⟨a, rfl⟩ }, have h3 : ∀ (x : m ⊕ n), (∃ (b : n), sum.inr b = x) → (∃ (b : n), sum.inr b = σ x), { rintros x ⟨b, hb⟩, apply (perm_maps_to_inl_iff_maps_to_inr σ).mp h, rw ← hb, exact ⟨b, rfl⟩ }, let σ₁' := subtype_perm_of_fintype σ h1, let σ₂' := subtype_perm_of_fintype σ h3, let σ₁ := perm_congr (equiv.of_injective (@sum.inl m n) sum.inl_injective).symm σ₁', let σ₂ := perm_congr (equiv.of_injective (@sum.inr m n) sum.inr_injective).symm σ₂', rw [monoid_hom.mem_range, prod.exists], use [σ₁, σ₂], rw [perm.sum_congr_hom_apply], ext, cases x with a b, { rw [equiv.sum_congr_apply, sum.map_inl, perm_congr_apply, equiv.symm_symm, apply_of_injective_symm (@sum.inl m n)], erw subtype_perm_apply, rw [of_injective_apply, subtype.coe_mk, subtype.coe_mk] }, { rw [equiv.sum_congr_apply, sum.map_inr, perm_congr_apply, equiv.symm_symm, apply_of_injective_symm (@sum.inr m n)], erw subtype_perm_apply, rw [of_injective_apply, subtype.coe_mk, subtype.coe_mk] } end /-- Two permutations `f` and `g` are `disjoint` if their supports are disjoint, i.e., every element is fixed either by `f`, or by `g`. -/ def disjoint (f g : perm α) := ∀ x, f x = x ∨ g x = x @[symm] lemma disjoint.symm {f g : perm α} : disjoint f g → disjoint g f := by simp only [disjoint, or.comm, imp_self] lemma disjoint_comm {f g : perm α} : disjoint f g ↔ disjoint g f := ⟨disjoint.symm, disjoint.symm⟩ lemma disjoint.mul_comm {f g : perm α} (h : disjoint f g) : f * g = g * f := equiv.ext $ λ x, (h x).elim (λ hf, (h (g x)).elim (λ hg, by simp [mul_apply, hf, hg]) (λ hg, by simp [mul_apply, hf, g.injective hg])) (λ hg, (h (f x)).elim (λ hf, by simp [mul_apply, f.injective hf, hg]) (λ hf, by simp [mul_apply, hf, hg])) @[simp] lemma disjoint_one_left (f : perm α) : disjoint 1 f := λ _, or.inl rfl @[simp] lemma disjoint_one_right (f : perm α) : disjoint f 1 := λ _, or.inr rfl lemma disjoint.mul_left {f g h : perm α} (H1 : disjoint f h) (H2 : disjoint g h) : disjoint (f * g) h := λ x, by cases H1 x; cases H2 x; simp * lemma disjoint.mul_right {f g h : perm α} (H1 : disjoint f g) (H2 : disjoint f h) : disjoint f (g * h) := by { rw disjoint_comm, exact H1.symm.mul_left H2.symm } lemma disjoint_prod_right {f : perm α} (l : list (perm α)) (h : ∀ g ∈ l, disjoint f g) : disjoint f l.prod := begin induction l with g l ih, { exact disjoint_one_right _ }, { rw list.prod_cons, exact (h _ (list.mem_cons_self _ _)).mul_right (ih (λ g hg, h g (list.mem_cons_of_mem _ hg))) } end lemma disjoint_prod_perm {l₁ l₂ : list (perm α)} (hl : l₁.pairwise disjoint) (hp : l₁ ~ l₂) : l₁.prod = l₂.prod := hp.prod_eq' $ hl.imp $ λ f g, disjoint.mul_comm lemma nodup_of_pairwise_disjoint {l : list (perm α)} (h1 : (1 : perm α) ∉ l) (h2 : l.pairwise disjoint) : l.nodup := begin refine list.pairwise.imp_of_mem _ h2, rintros σ - h_mem - h_disjoint rfl, suffices : σ = 1, { rw this at h_mem, exact h1 h_mem }, exact ext (λ a, (or_self _).mp (h_disjoint a)), end lemma pow_apply_eq_self_of_apply_eq_self {f : perm α} {x : α} (hfx : f x = x) : ∀ n : ℕ, (f ^ n) x = x | 0 := rfl | (n+1) := by rw [pow_succ', mul_apply, hfx, pow_apply_eq_self_of_apply_eq_self] lemma gpow_apply_eq_self_of_apply_eq_self {f : perm α} {x : α} (hfx : f x = x) : ∀ n : ℤ, (f ^ n) x = x | (n : ℕ) := pow_apply_eq_self_of_apply_eq_self hfx n | -[1+ n] := by rw [gpow_neg_succ_of_nat, inv_eq_iff_eq, pow_apply_eq_self_of_apply_eq_self hfx] lemma pow_apply_eq_of_apply_apply_eq_self {f : perm α} {x : α} (hffx : f (f x) = x) : ∀ n : ℕ, (f ^ n) x = x ∨ (f ^ n) x = f x | 0 := or.inl rfl | (n+1) := (pow_apply_eq_of_apply_apply_eq_self n).elim (λ h, or.inr (by rw [pow_succ, mul_apply, h])) (λ h, or.inl (by rw [pow_succ, mul_apply, h, hffx])) lemma gpow_apply_eq_of_apply_apply_eq_self {f : perm α} {x : α} (hffx : f (f x) = x) : ∀ i : ℤ, (f ^ i) x = x ∨ (f ^ i) x = f x | (n : ℕ) := pow_apply_eq_of_apply_apply_eq_self hffx n | -[1+ n] := by { rw [gpow_neg_succ_of_nat, inv_eq_iff_eq, ← f.injective.eq_iff, ← mul_apply, ← pow_succ, eq_comm, inv_eq_iff_eq, ← mul_apply, ← pow_succ', @eq_comm _ x, or.comm], exact pow_apply_eq_of_apply_apply_eq_self hffx _ } lemma disjoint.mul_apply_eq_iff {σ τ : perm α} (hστ : disjoint σ τ) {a : α} : (σ * τ) a = a ↔ σ a = a ∧ τ a = a := begin refine ⟨λ h, _, λ h, by rw [mul_apply, h.2, h.1]⟩, cases hστ a with hσ hτ, { exact ⟨hσ, σ.injective (h.trans hσ.symm)⟩ }, { exact ⟨(congr_arg σ hτ).symm.trans h, hτ⟩ }, end lemma disjoint.mul_eq_one_iff {σ τ : perm α} (hστ : disjoint σ τ) : σ * τ = 1 ↔ σ = 1 ∧ τ = 1 := by simp_rw [ext_iff, one_apply, hστ.mul_apply_eq_iff, forall_and_distrib] lemma disjoint.gpow_disjoint_gpow {σ τ : perm α} (hστ : disjoint σ τ) (m n : ℤ) : disjoint (σ ^ m) (τ ^ n) := λ x, or.imp (λ h, gpow_apply_eq_self_of_apply_eq_self h m) (λ h, gpow_apply_eq_self_of_apply_eq_self h n) (hστ x) lemma disjoint.pow_disjoint_pow {σ τ : perm α} (hστ : disjoint σ τ) (m n : ℕ) : disjoint (σ ^ m) (τ ^ n) := hστ.gpow_disjoint_gpow m n lemma disjoint.order_of {σ τ : perm α} (hστ : disjoint σ τ) : order_of (σ * τ) = nat.lcm (order_of σ) (order_of τ) := begin have h : ∀ n : ℕ, (σ * τ) ^ n = 1 ↔ σ ^ n = 1 ∧ τ ^ n = 1 := λ n, by rw [commute.mul_pow hστ.mul_comm, disjoint.mul_eq_one_iff (hστ.pow_disjoint_pow n n)], exact nat.dvd_antisymm (commute.order_of_mul_dvd_lcm hστ.mul_comm) (nat.lcm_dvd (order_of_dvd_of_pow_eq_one ((h (order_of (σ * τ))).mp (pow_order_of_eq_one (σ * τ))).1) (order_of_dvd_of_pow_eq_one ((h (order_of (σ * τ))).mp (pow_order_of_eq_one (σ * τ))).2)), end variable [decidable_eq α] section fintype variable [fintype α] /-- The `finset` of nonfixed points of a permutation. -/ def support (f : perm α) : finset α := univ.filter (λ x, f x ≠ x) @[simp] lemma mem_support {f : perm α} {x : α} : x ∈ f.support ↔ f x ≠ x := by rw [support, mem_filter, and_iff_right (mem_univ x)] @[simp] lemma support_eq_empty_iff {σ : perm α} : σ.support = ∅ ↔ σ = 1 := by simp_rw [finset.ext_iff, mem_support, finset.not_mem_empty, iff_false, not_not, equiv.perm.ext_iff, one_apply] @[simp] lemma support_one : (1 : perm α).support = ∅ := by rw support_eq_empty_iff lemma support_mul_le (f g : perm α) : (f * g).support ≤ f.support ⊔ g.support := λ x, begin rw [sup_eq_union, mem_union, mem_support, mem_support, mem_support, mul_apply, ←not_and_distrib, not_imp_not], rintro ⟨hf, hg⟩, rw [hg, hf] end lemma exists_mem_support_of_mem_support_prod {l : list (perm α)} {x : α} (hx : x ∈ l.prod.support) : ∃ f : perm α, f ∈ l ∧ x ∈ f.support := begin contrapose! hx, simp_rw [mem_support, not_not] at hx ⊢, induction l with f l ih generalizing hx, { refl }, { rw [list.prod_cons, mul_apply, ih (λ g hg, hx g (or.inr hg)), hx f (or.inl rfl)] }, end lemma support_pow_le (σ : perm α) (n : ℤ) : (σ ^ n).support ≤ σ.support := λ x h1, mem_support.mpr (λ h2, mem_support.mp h1 (gpow_apply_eq_self_of_apply_eq_self h2 n)) lemma support_pow_coprime {σ : perm α} {n : ℕ} (h : nat.coprime n (order_of σ)) : (σ ^ n).support = σ.support := begin obtain ⟨m, hm⟩ := exists_pow_eq_self_of_coprime h, exact le_antisymm (support_pow_le σ n) (le_trans (ge_of_eq (congr_arg support hm)) (support_pow_le (σ ^ n) m)), end @[simp] lemma support_inv (σ : perm α) : support (σ⁻¹) = σ.support := by simp_rw [finset.ext_iff, mem_support, not_iff_not, (inv_eq_iff_eq).trans eq_comm, iff_self, imp_true_iff] @[simp] lemma apply_mem_support {f : perm α} {x : α} : f x ∈ f.support ↔ x ∈ f.support := by rw [mem_support, mem_support, ne.def, ne.def, not_iff_not, apply_eq_iff_eq] @[simp] lemma pow_apply_mem_support {f : perm α} {n : ℕ} {x : α} : (f ^ n) x ∈ f.support ↔ x ∈ f.support := begin induction n with n ih, { refl }, rw [pow_succ, perm.mul_apply, apply_mem_support, ih] end @[simp] lemma gpow_apply_mem_support {f : perm α} {n : ℤ} {x : α} : (f ^ n) x ∈ f.support ↔ x ∈ f.support := begin cases n, { rw [int.of_nat_eq_coe, gpow_coe_nat, pow_apply_mem_support] }, { rw [gpow_neg_succ_of_nat, ← support_inv, ← inv_pow, pow_apply_mem_support] } end end fintype /-- `f.is_swap` indicates that the permutation `f` is a transposition of two elements. -/ def is_swap (f : perm α) : Prop := ∃ x y, x ≠ y ∧ f = swap x y lemma is_swap.of_subtype_is_swap {p : α → Prop} [decidable_pred p] {f : perm (subtype p)} (h : f.is_swap) : (of_subtype f).is_swap := let ⟨⟨x, hx⟩, ⟨y, hy⟩, hxy⟩ := h in ⟨x, y, by { simp only [ne.def] at hxy, exact hxy.1 }, equiv.ext $ λ z, begin rw [hxy.2, of_subtype], simp only [swap_apply_def, coe_fn_mk, swap_inv, subtype.mk_eq_mk, monoid_hom.coe_mk], split_ifs; rw subtype.coe_mk <|> cc, end⟩ lemma ne_and_ne_of_swap_mul_apply_ne_self {f : perm α} {x y : α} (hy : (swap x (f x) * f) y ≠ y) : f y ≠ y ∧ y ≠ x := begin simp only [swap_apply_def, mul_apply, f.injective.eq_iff] at *, by_cases h : f y = x, { split; intro; simp only [*, if_true, eq_self_iff_true, not_true, ne.def] at * }, { split_ifs at hy; cc } end section fintype variables [fintype α] lemma support_swap_mul_eq {f : perm α} {x : α} (hffx : f (f x) ≠ x) : (swap x (f x) * f).support = f.support.erase x := have hfx : f x ≠ x, from λ hfx, by simpa [hfx] using hffx, finset.ext $ λ y, ⟨λ hy, have hy' : (swap x (f x) * f) y ≠ y, from mem_support.1 hy, mem_erase.2 ⟨λ hyx, by simp [hyx, mul_apply, *] at *, mem_support.2 $ λ hfy, by simp only [mul_apply, swap_apply_def, hfy] at hy'; split_ifs at hy'; simp only [*, eq_self_iff_true, not_true, ne.def, apply_eq_iff_eq] at *⟩, λ hy, by simp only [mem_erase, mem_support, swap_apply_def, mul_apply] at *; intro; split_ifs at *; simp only [*, eq_self_iff_true, not_true, ne.def] at *⟩ @[simp] lemma card_support_eq_zero {f : perm α} : f.support.card = 0 ↔ f = 1 := by rw [finset.card_eq_zero, support_eq_empty_iff] lemma one_lt_card_support_of_ne_one {f : perm α} (h : f ≠ 1) : 1 < f.support.card := begin simp_rw [one_lt_card_iff, mem_support, ←not_or_distrib], contrapose! h, ext a, specialize h (f a) a, rwa [apply_eq_iff_eq, or_self, or_self] at h, end lemma card_support_ne_one (f : perm α) : f.support.card ≠ 1 := begin by_cases h : f = 1, { exact ne_of_eq_of_ne (card_support_eq_zero.mpr h) zero_ne_one }, { exact ne_of_gt (one_lt_card_support_of_ne_one h) }, end @[simp] lemma card_support_le_one {f : perm α} : f.support.card ≤ 1 ↔ f = 1 := by rw [le_iff_lt_or_eq, nat.lt_succ_iff, nat.le_zero_iff, card_support_eq_zero, or_iff_not_imp_right, imp_iff_right f.card_support_ne_one] lemma two_le_card_support_of_ne_one {f : perm α} (h : f ≠ 1) : 2 ≤ f.support.card := one_lt_card_support_of_ne_one h lemma card_support_swap_mul {f : perm α} {x : α} (hx : f x ≠ x) : (swap x (f x) * f).support.card < f.support.card := finset.card_lt_card ⟨λ z hz, mem_support.2 (ne_and_ne_of_swap_mul_apply_ne_self (mem_support.1 hz)).1, λ h, absurd (h (mem_support.2 hx)) (mt mem_support.1 (by simp))⟩ end fintype /-- Given a list `l : list α` and a permutation `f : perm α` such that the nonfixed points of `f` are in `l`, recursively factors `f` as a product of transpositions. -/ def swap_factors_aux : Π (l : list α) (f : perm α), (∀ {x}, f x ≠ x → x ∈ l) → {l : list (perm α) // l.prod = f ∧ ∀ g ∈ l, is_swap g} | [] := λ f h, ⟨[], equiv.ext $ λ x, by { rw [list.prod_nil], exact (not_not.1 (mt h (list.not_mem_nil _))).symm }, by simp⟩ | (x :: l) := λ f h, if hfx : x = f x then swap_factors_aux l f (λ y hy, list.mem_of_ne_of_mem (λ h : y = x, by simpa [h, hfx.symm] using hy) (h hy)) else let m := swap_factors_aux l (swap x (f x) * f) (λ y hy, have f y ≠ y ∧ y ≠ x, from ne_and_ne_of_swap_mul_apply_ne_self hy, list.mem_of_ne_of_mem this.2 (h this.1)) in ⟨swap x (f x) :: m.1, by rw [list.prod_cons, m.2.1, ← mul_assoc, mul_def (swap x (f x)), swap_swap, ← one_def, one_mul], λ g hg, ((list.mem_cons_iff _ _ _).1 hg).elim (λ h, ⟨x, f x, hfx, h⟩) (m.2.2 _)⟩ /-- `swap_factors` represents a permutation as a product of a list of transpositions. The representation is non unique and depends on the linear order structure. For types without linear order `trunc_swap_factors` can be used. -/ def swap_factors [fintype α] [linear_order α] (f : perm α) : {l : list (perm α) // l.prod = f ∧ ∀ g ∈ l, is_swap g} := swap_factors_aux ((@univ α _).sort (≤)) f (λ _ _, (mem_sort _).2 (mem_univ _)) /-- This computably represents the fact that any permutation can be represented as the product of a list of transpositions. -/ def trunc_swap_factors [fintype α] (f : perm α) : trunc {l : list (perm α) // l.prod = f ∧ ∀ g ∈ l, is_swap g} := quotient.rec_on_subsingleton (@univ α _).1 (λ l h, trunc.mk (swap_factors_aux l f h)) (show ∀ x, f x ≠ x → x ∈ (@univ α _).1, from λ _ _, mem_univ _) /-- An induction principle for permutations. If `P` holds for the identity permutation, and is preserved under composition with a non-trivial swap, then `P` holds for all permutations. -/ @[elab_as_eliminator] lemma swap_induction_on [fintype α] {P : perm α → Prop} (f : perm α) : P 1 → (∀ f x y, x ≠ y → P f → P (swap x y * f)) → P f := begin cases (trunc_swap_factors f).out with l hl, induction l with g l ih generalizing f, { simp only [hl.left.symm, list.prod_nil, forall_true_iff] {contextual := tt} }, { assume h1 hmul_swap, rcases hl.2 g (by simp) with ⟨x, y, hxy⟩, rw [← hl.1, list.prod_cons, hxy.2], exact hmul_swap _ _ _ hxy.1 (ih _ ⟨rfl, λ v hv, hl.2 _ (list.mem_cons_of_mem _ hv)⟩ h1 hmul_swap) } end lemma closure_is_swap [fintype α] : subgroup.closure {σ : perm α | is_swap σ} = ⊤ := begin refine eq_top_iff.mpr (λ x hx, _), obtain ⟨h1, h2⟩ := subtype.mem (trunc_swap_factors x).out, rw ← h1, exact subgroup.list_prod_mem _ (λ y hy, subgroup.subset_closure (h2 y hy)), end /-- Like `swap_induction_on`, but with the composition on the right of `f`. An induction principle for permutations. If `P` holds for the identity permutation, and is preserved under composition with a non-trivial swap, then `P` holds for all permutations. -/ @[elab_as_eliminator] lemma swap_induction_on' [fintype α] {P : perm α → Prop} (f : perm α) : P 1 → (∀ f x y, x ≠ y → P f → P (f * swap x y)) → P f := λ h1 IH, inv_inv f ▸ swap_induction_on f⁻¹ h1 (λ f, IH f⁻¹) lemma is_conj_swap {w x y z : α} (hwx : w ≠ x) (hyz : y ≠ z) : is_conj (swap w x) (swap y z) := is_conj_iff.2 (have h : ∀ {y z : α}, y ≠ z → w ≠ z → (swap w y * swap x z) * swap w x * (swap w y * swap x z)⁻¹ = swap y z := λ y z hyz hwz, by rw [mul_inv_rev, swap_inv, swap_inv, mul_assoc (swap w y), mul_assoc (swap w y), ← mul_assoc _ (swap x z), swap_mul_swap_mul_swap hwx hwz, ← mul_assoc, swap_mul_swap_mul_swap hwz.symm hyz.symm], if hwz : w = z then have hwy : w ≠ y, by cc, ⟨swap w z * swap x y, by rw [swap_comm y z, h hyz.symm hwy]⟩ else ⟨swap w y * swap x z, h hyz hwz⟩) /-- set of all pairs (⟨a, b⟩ : Σ a : fin n, fin n) such that b < a -/ def fin_pairs_lt (n : ℕ) : finset (Σ a : fin n, fin n) := (univ : finset (fin n)).sigma (λ a, (range a).attach_fin (λ m hm, (mem_range.1 hm).trans a.2)) lemma mem_fin_pairs_lt {n : ℕ} {a : Σ a : fin n, fin n} : a ∈ fin_pairs_lt n ↔ a.2 < a.1 := by simp only [fin_pairs_lt, fin.lt_iff_coe_lt_coe, true_and, mem_attach_fin, mem_range, mem_univ, mem_sigma] /-- `sign_aux σ` is the sign of a permutation on `fin n`, defined as the parity of the number of pairs `(x₁, x₂)` such that `x₂ < x₁` but `σ x₁ ≤ σ x₂` -/ def sign_aux {n : ℕ} (a : perm (fin n)) : units ℤ := ∏ x in fin_pairs_lt n, if a x.1 ≤ a x.2 then -1 else 1 @[simp] lemma sign_aux_one (n : ℕ) : sign_aux (1 : perm (fin n)) = 1 := begin unfold sign_aux, conv { to_rhs, rw ← @finset.prod_const_one _ (units ℤ) (fin_pairs_lt n) }, exact finset.prod_congr rfl (λ a ha, if_neg (mem_fin_pairs_lt.1 ha).not_le) end /-- `sign_bij_aux f ⟨a, b⟩` returns the pair consisting of `f a` and `f b` in decreasing order. -/ def sign_bij_aux {n : ℕ} (f : perm (fin n)) (a : Σ a : fin n, fin n) : Σ a : fin n, fin n := if hxa : f a.2 < f a.1 then ⟨f a.1, f a.2⟩ else ⟨f a.2, f a.1⟩ lemma sign_bij_aux_inj {n : ℕ} {f : perm (fin n)} : ∀ a b : Σ a : fin n, fin n, a ∈ fin_pairs_lt n → b ∈ fin_pairs_lt n → sign_bij_aux f a = sign_bij_aux f b → a = b := λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ha hb h, begin unfold sign_bij_aux at h, rw mem_fin_pairs_lt at *, have : ¬b₁ < b₂ := hb.le.not_lt, split_ifs at h; simp only [*, (equiv.injective f).eq_iff, eq_self_iff_true, and_self, heq_iff_eq] at *, end lemma sign_bij_aux_surj {n : ℕ} {f : perm (fin n)} : ∀ a ∈ fin_pairs_lt n, ∃ b ∈ fin_pairs_lt n, a = sign_bij_aux f b := λ ⟨a₁, a₂⟩ ha, if hxa : f⁻¹ a₂ < f⁻¹ a₁ then ⟨⟨f⁻¹ a₁, f⁻¹ a₂⟩, mem_fin_pairs_lt.2 hxa, by { dsimp [sign_bij_aux], rw [apply_inv_self, apply_inv_self, if_pos (mem_fin_pairs_lt.1 ha)] }⟩ else ⟨⟨f⁻¹ a₂, f⁻¹ a₁⟩, mem_fin_pairs_lt.2 $ (le_of_not_gt hxa).lt_of_ne $ λ h, by simpa [mem_fin_pairs_lt, (f⁻¹).injective h, lt_irrefl] using ha, by { dsimp [sign_bij_aux], rw [apply_inv_self, apply_inv_self, if_neg (mem_fin_pairs_lt.1 ha).le.not_lt] }⟩ lemma sign_bij_aux_mem {n : ℕ} {f : perm (fin n)} : ∀ a : Σ a : fin n, fin n, a ∈ fin_pairs_lt n → sign_bij_aux f a ∈ fin_pairs_lt n := λ ⟨a₁, a₂⟩ ha, begin unfold sign_bij_aux, split_ifs with h, { exact mem_fin_pairs_lt.2 h }, { exact mem_fin_pairs_lt.2 ((le_of_not_gt h).lt_of_ne (λ h, (mem_fin_pairs_lt.1 ha).ne (f.injective h.symm))) } end @[simp] lemma sign_aux_inv {n : ℕ} (f : perm (fin n)) : sign_aux f⁻¹ = sign_aux f := prod_bij (λ a ha, sign_bij_aux f⁻¹ a) sign_bij_aux_mem (λ ⟨a, b⟩ hab, if h : f⁻¹ b < f⁻¹ a then by rw [sign_bij_aux, dif_pos h, if_neg h.not_le, apply_inv_self, apply_inv_self, if_neg (mem_fin_pairs_lt.1 hab).not_le] else by rw [sign_bij_aux, if_pos (le_of_not_gt h), dif_neg h, apply_inv_self, apply_inv_self, if_pos (mem_fin_pairs_lt.1 hab).le]) sign_bij_aux_inj sign_bij_aux_surj lemma sign_aux_mul {n : ℕ} (f g : perm (fin n)) : sign_aux (f * g) = sign_aux f * sign_aux g := begin rw ← sign_aux_inv g, unfold sign_aux, rw ← prod_mul_distrib, refine prod_bij (λ a ha, sign_bij_aux g a) sign_bij_aux_mem _ sign_bij_aux_inj sign_bij_aux_surj, rintros ⟨a, b⟩ hab, rw [sign_bij_aux, mul_apply, mul_apply], rw mem_fin_pairs_lt at hab, by_cases h : g b < g a, { rw dif_pos h, simp only [not_le_of_gt hab, mul_one, perm.inv_apply_self, if_false] }, { rw [dif_neg h, inv_apply_self, inv_apply_self, if_pos hab.le], by_cases h₁ : f (g b) ≤ f (g a), { have : f (g b) ≠ f (g a), { rw [ne.def, f.injective.eq_iff, g.injective.eq_iff], exact ne_of_lt hab }, rw [if_pos h₁, if_neg (h₁.lt_of_ne this).not_le], refl }, { rw [if_neg h₁, if_pos (lt_of_not_ge h₁).le], refl } } end private lemma sign_aux_swap_zero_one' (n : ℕ) : sign_aux (swap (0 : fin (n + 2)) 1) = -1 := show _ = ∏ x : Σ a : fin (n + 2), fin (n + 2) in {(⟨1, 0⟩ : Σ a : fin (n + 2), fin (n + 2))}, if (equiv.swap 0 1) x.1 ≤ swap 0 1 x.2 then (-1 : units ℤ) else 1, begin refine eq.symm (prod_subset (λ ⟨x₁, x₂⟩, by simp [mem_fin_pairs_lt, fin.one_pos] {contextual := tt}) (λ a ha₁ ha₂, _)), rcases a with ⟨a₁, a₂⟩, replace ha₁ : a₂ < a₁ := mem_fin_pairs_lt.1 ha₁, dsimp only, rcases a₁.zero_le.eq_or_lt with rfl|H, { exact absurd a₂.zero_le ha₁.not_le }, rcases a₂.zero_le.eq_or_lt with rfl|H', { simp only [and_true, eq_self_iff_true, heq_iff_eq, mem_singleton] at ha₂, have : 1 < a₁ := lt_of_le_of_ne (nat.succ_le_of_lt ha₁) (ne.symm ha₂), norm_num [swap_apply_of_ne_of_ne (ne_of_gt H) ha₂, this.not_le] }, { have le : 1 ≤ a₂ := nat.succ_le_of_lt H', have lt : 1 < a₁ := le.trans_lt ha₁, rcases le.eq_or_lt with rfl|lt', { norm_num [swap_apply_of_ne_of_ne (ne_of_gt H) (ne_of_gt lt), H.not_le] }, { norm_num [swap_apply_of_ne_of_ne (ne_of_gt H) (ne_of_gt lt), swap_apply_of_ne_of_ne (ne_of_gt H') (ne_of_gt lt'), ha₁.not_le] } } end private lemma sign_aux_swap_zero_one {n : ℕ} (hn : 2 ≤ n) : sign_aux (swap (⟨0, lt_of_lt_of_le dec_trivial hn⟩ : fin n) ⟨1, lt_of_lt_of_le dec_trivial hn⟩) = -1 := begin rcases n with _|_|n, { norm_num at hn }, { norm_num at hn }, { exact sign_aux_swap_zero_one' n } end lemma sign_aux_swap : ∀ {n : ℕ} {x y : fin n} (hxy : x ≠ y), sign_aux (swap x y) = -1 | 0 := dec_trivial | 1 := dec_trivial | (n+2) := λ x y hxy, have h2n : 2 ≤ n + 2 := dec_trivial, by { rw [← is_conj_iff_eq, ← sign_aux_swap_zero_one h2n], exact (monoid_hom.mk' sign_aux sign_aux_mul).map_is_conj (is_conj_swap hxy dec_trivial) } /-- When the list `l : list α` contains all nonfixed points of the permutation `f : perm α`, `sign_aux2 l f` recursively calculates the sign of `f`. -/ def sign_aux2 : list α → perm α → units ℤ | [] f := 1 | (x::l) f := if x = f x then sign_aux2 l f else -sign_aux2 l (swap x (f x) * f) lemma sign_aux_eq_sign_aux2 {n : ℕ} : ∀ (l : list α) (f : perm α) (e : α ≃ fin n) (h : ∀ x, f x ≠ x → x ∈ l), sign_aux ((e.symm.trans f).trans e) = sign_aux2 l f | [] f e h := have f = 1, from equiv.ext $ λ y, not_not.1 (mt (h y) (list.not_mem_nil _)), by rw [this, one_def, equiv.trans_refl, equiv.symm_trans, ← one_def, sign_aux_one, sign_aux2] | (x::l) f e h := begin rw sign_aux2, by_cases hfx : x = f x, { rw if_pos hfx, exact sign_aux_eq_sign_aux2 l f _ (λ y (hy : f y ≠ y), list.mem_of_ne_of_mem (λ h : y = x, by simpa [h, hfx.symm] using hy) (h y hy) ) }, { have hy : ∀ y : α, (swap x (f x) * f) y ≠ y → y ∈ l, from λ y hy, have f y ≠ y ∧ y ≠ x, from ne_and_ne_of_swap_mul_apply_ne_self hy, list.mem_of_ne_of_mem this.2 (h _ this.1), have : (e.symm.trans (swap x (f x) * f)).trans e = (swap (e x) (e (f x))) * (e.symm.trans f).trans e, by ext; simp [← equiv.symm_trans_swap_trans, mul_def], have hefx : e x ≠ e (f x), from mt e.injective.eq_iff.1 hfx, rw [if_neg hfx, ← sign_aux_eq_sign_aux2 _ _ e hy, this, sign_aux_mul, sign_aux_swap hefx], simp only [units.neg_neg, one_mul, units.neg_mul]} end /-- When the multiset `s : multiset α` contains all nonfixed points of the permutation `f : perm α`, `sign_aux2 f _` recursively calculates the sign of `f`. -/ def sign_aux3 [fintype α] (f : perm α) {s : multiset α} : (∀ x, x ∈ s) → units ℤ := quotient.hrec_on s (λ l h, sign_aux2 l f) (trunc.induction_on (fintype.trunc_equiv_fin α) (λ e l₁ l₂ h, function.hfunext (show (∀ x, x ∈ l₁) = ∀ x, x ∈ l₂, by simp only [h.mem_iff]) (λ h₁ h₂ _, by rw [← sign_aux_eq_sign_aux2 _ _ e (λ _ _, h₁ _), ← sign_aux_eq_sign_aux2 _ _ e (λ _ _, h₂ _)]))) lemma sign_aux3_mul_and_swap [fintype α] (f g : perm α) (s : multiset α) (hs : ∀ x, x ∈ s) : sign_aux3 (f * g) hs = sign_aux3 f hs * sign_aux3 g hs ∧ ∀ x y, x ≠ y → sign_aux3 (swap x y) hs = -1 := let ⟨l, hl⟩ := quotient.exists_rep s in let e := equiv_fin α in begin clear _let_match, subst hl, show sign_aux2 l (f * g) = sign_aux2 l f * sign_aux2 l g ∧ ∀ x y, x ≠ y → sign_aux2 l (swap x y) = -1, have hfg : (e.symm.trans (f * g)).trans e = (e.symm.trans f).trans e * (e.symm.trans g).trans e, from equiv.ext (λ h, by simp [mul_apply]), split, { rw [← sign_aux_eq_sign_aux2 _ _ e (λ _ _, hs _), ← sign_aux_eq_sign_aux2 _ _ e (λ _ _, hs _), ← sign_aux_eq_sign_aux2 _ _ e (λ _ _, hs _), hfg, sign_aux_mul] }, { assume x y hxy, have hexy : e x ≠ e y, from mt e.injective.eq_iff.1 hxy, rw [← sign_aux_eq_sign_aux2 _ _ e (λ _ _, hs _), symm_trans_swap_trans, sign_aux_swap hexy] } end /-- `sign` of a permutation returns the signature or parity of a permutation, `1` for even permutations, `-1` for odd permutations. It is the unique surjective group homomorphism from `perm α` to the group with two elements.-/ def sign [fintype α] : perm α →* units ℤ := monoid_hom.mk' (λ f, sign_aux3 f mem_univ) (λ f g, (sign_aux3_mul_and_swap f g _ mem_univ).1) section sign variable [fintype α] @[simp] lemma sign_mul (f g : perm α) : sign (f * g) = sign f * sign g := monoid_hom.map_mul sign f g @[simp] lemma sign_trans (f g : perm α) : sign (f.trans g) = sign g * sign f := by rw [←mul_def, sign_mul] @[simp] lemma sign_one : (sign (1 : perm α)) = 1 := monoid_hom.map_one sign @[simp] lemma sign_refl : sign (equiv.refl α) = 1 := monoid_hom.map_one sign @[simp] lemma sign_inv (f : perm α) : sign f⁻¹ = sign f := by rw [monoid_hom.map_inv sign f, int.units_inv_eq_self] @[simp] lemma sign_symm (e : perm α) : sign e.symm = sign e := sign_inv e lemma sign_swap {x y : α} (h : x ≠ y) : sign (swap x y) = -1 := (sign_aux3_mul_and_swap 1 1 _ mem_univ).2 x y h @[simp] lemma sign_swap' {x y : α} : (swap x y).sign = if x = y then 1 else -1 := if H : x = y then by simp [H, swap_self] else by simp [sign_swap H, H] lemma is_swap.sign_eq {f : perm α} (h : f.is_swap) : sign f = -1 := let ⟨x, y, hxy⟩ := h in hxy.2.symm ▸ sign_swap hxy.1 lemma sign_aux3_symm_trans_trans [decidable_eq β] [fintype β] (f : perm α) (e : α ≃ β) {s : multiset α} {t : multiset β} (hs : ∀ x, x ∈ s) (ht : ∀ x, x ∈ t) : sign_aux3 ((e.symm.trans f).trans e) ht = sign_aux3 f hs := quotient.induction_on₂ t s (λ l₁ l₂ h₁ h₂, show sign_aux2 _ _ = sign_aux2 _ _, from let n := equiv_fin β in by { rw [← sign_aux_eq_sign_aux2 _ _ n (λ _ _, h₁ _), ← sign_aux_eq_sign_aux2 _ _ (e.trans n) (λ _ _, h₂ _)], exact congr_arg sign_aux (equiv.ext (λ x, by simp only [equiv.coe_trans, apply_eq_iff_eq, symm_trans_apply])) }) ht hs @[simp] lemma sign_symm_trans_trans [decidable_eq β] [fintype β] (f : perm α) (e : α ≃ β) : sign ((e.symm.trans f).trans e) = sign f := sign_aux3_symm_trans_trans f e mem_univ mem_univ @[simp] lemma sign_trans_trans_symm [decidable_eq β] [fintype β] (f : perm β) (e : α ≃ β) : sign ((e.trans f).trans e.symm) = sign f := sign_symm_trans_trans f e.symm lemma sign_prod_list_swap {l : list (perm α)} (hl : ∀ g ∈ l, is_swap g) : sign l.prod = (-1) ^ l.length := have h₁ : l.map sign = list.repeat (-1) l.length := list.eq_repeat.2 ⟨by simp, λ u hu, let ⟨g, hg⟩ := list.mem_map.1 hu in hg.2 ▸ (hl _ hg.1).sign_eq⟩, by rw [← list.prod_repeat, ← h₁, list.prod_hom _ (@sign α _ _)] variable (α) lemma sign_surjective [nontrivial α] : function.surjective (sign : perm α → units ℤ) := λ a, (int.units_eq_one_or a).elim (λ h, ⟨1, by simp [h]⟩) (λ h, let ⟨x, y, hxy⟩ := exists_pair_ne α in ⟨swap x y, by rw [sign_swap hxy, h]⟩ ) variable {α} lemma eq_sign_of_surjective_hom {s : perm α →* units ℤ} (hs : surjective s) : s = sign := have ∀ {f}, is_swap f → s f = -1 := λ f ⟨x, y, hxy, hxy'⟩, hxy'.symm ▸ by_contradiction (λ h, have ∀ f, is_swap f → s f = 1 := λ f ⟨a, b, hab, hab'⟩, by { rw [← is_conj_iff_eq, ← or.resolve_right (int.units_eq_one_or _) h, hab'], exact (monoid_hom.of s).map_is_conj (is_conj_swap hab hxy) }, let ⟨g, hg⟩ := hs (-1) in let ⟨l, hl⟩ := (trunc_swap_factors g).out in have ∀ a ∈ l.map s, a = (1 : units ℤ) := λ a ha, let ⟨g, hg⟩ := list.mem_map.1 ha in hg.2 ▸ this _ (hl.2 _ hg.1), have s l.prod = 1, by rw [← l.prod_hom s, list.eq_repeat'.2 this, list.prod_repeat, one_pow], by { rw [hl.1, hg] at this, exact absurd this dec_trivial }), monoid_hom.ext $ λ f, let ⟨l, hl₁, hl₂⟩ := (trunc_swap_factors f).out in have hsl : ∀ a ∈ l.map s, a = (-1 : units ℤ) := λ a ha, let ⟨g, hg⟩ := list.mem_map.1 ha in hg.2 ▸ this (hl₂ _ hg.1), by rw [← hl₁, ← l.prod_hom s, list.eq_repeat'.2 hsl, list.length_map, list.prod_repeat, sign_prod_list_swap hl₂] lemma sign_subtype_perm (f : perm α) {p : α → Prop} [decidable_pred p] (h₁ : ∀ x, p x ↔ p (f x)) (h₂ : ∀ x, f x ≠ x → p x) : sign (subtype_perm f h₁) = sign f := let l := (trunc_swap_factors (subtype_perm f h₁)).out in have hl' : ∀ g' ∈ l.1.map of_subtype, is_swap g' := λ g' hg', let ⟨g, hg⟩ := list.mem_map.1 hg' in hg.2 ▸ (l.2.2 _ hg.1).of_subtype_is_swap, have hl'₂ : (l.1.map of_subtype).prod = f, by rw [l.1.prod_hom of_subtype, l.2.1, of_subtype_subtype_perm _ h₂], by { conv { congr, rw ← l.2.1, skip, rw ← hl'₂ }, rw [sign_prod_list_swap l.2.2, sign_prod_list_swap hl', list.length_map] } @[simp] lemma sign_of_subtype {p : α → Prop} [decidable_pred p] (f : perm (subtype p)) : sign (of_subtype f) = sign f := have ∀ x, of_subtype f x ≠ x → p x, from λ x, not_imp_comm.1 (of_subtype_apply_of_not_mem f), by conv {to_rhs, rw [← subtype_perm_of_subtype f, sign_subtype_perm _ _ this]} lemma sign_eq_sign_of_equiv [decidable_eq β] [fintype β] (f : perm α) (g : perm β) (e : α ≃ β) (h : ∀ x, e (f x) = g (e x)) : sign f = sign g := have hg : g = (e.symm.trans f).trans e, from equiv.ext $ by simp [h], by rw [hg, sign_symm_trans_trans] lemma sign_bij [decidable_eq β] [fintype β] {f : perm α} {g : perm β} (i : Π x : α, f x ≠ x → β) (h : ∀ x hx hx', i (f x) hx' = g (i x hx)) (hi : ∀ x₁ x₂ hx₁ hx₂, i x₁ hx₁ = i x₂ hx₂ → x₁ = x₂) (hg : ∀ y, g y ≠ y → ∃ x hx, i x hx = y) : sign f = sign g := calc sign f = sign (@subtype_perm _ f (λ x, f x ≠ x) (by simp)) : (sign_subtype_perm _ _ (λ _, id)).symm ... = sign (@subtype_perm _ g (λ x, g x ≠ x) (by simp)) : sign_eq_sign_of_equiv _ _ (equiv.of_bijective (λ x : {x // f x ≠ x}, (⟨i x.1 x.2, have f (f x) ≠ f x, from mt (λ h, f.injective h) x.2, by { rw [← h _ x.2 this], exact mt (hi _ _ this x.2) x.2 }⟩ : {y // g y ≠ y})) ⟨λ ⟨x, hx⟩ ⟨y, hy⟩ h, subtype.eq (hi _ _ _ _ (subtype.mk.inj h)), λ ⟨y, hy⟩, let ⟨x, hfx, hx⟩ := hg y hy in ⟨⟨x, hfx⟩, subtype.eq hx⟩⟩) (λ ⟨x, _⟩, subtype.eq (h x _ _)) ... = sign g : sign_subtype_perm _ _ (λ _, id) @[simp] lemma support_swap {x y : α} (hxy : x ≠ y) : (swap x y).support = {x, y} := finset.ext $ λ a, by { simp only [swap_apply_def, mem_insert, ne.def, mem_support, mem_singleton], split_ifs; cc } lemma card_support_swap {x y : α} (hxy : x ≠ y) : (swap x y).support.card = 2 := show (swap x y).support.card = finset.card ⟨x ::ₘ y ::ₘ 0, by simp [hxy]⟩, from congr_arg card $ by simp [support_swap hxy, *, finset.ext_iff] @[simp] lemma card_support_eq_two {f : perm α} : f.support.card = 2 ↔ is_swap f := begin split; intro h, { obtain ⟨x, t, hmem, hins, ht⟩ := card_eq_succ.1 h, obtain ⟨y, rfl⟩ := card_eq_one.1 ht, rw mem_singleton at hmem, refine ⟨x, y, hmem, _⟩, ext a, have key : ∀ b, f b ≠ b ↔ _ := λ b, by rw [←mem_support, ←hins, mem_insert, mem_singleton], by_cases ha : f a = a, { have ha' := not_or_distrib.mp (mt (key a).mpr (not_not.mpr ha)), rw [ha, swap_apply_of_ne_of_ne ha'.1 ha'.2] }, { have ha' := (key (f a)).mp (mt f.apply_eq_iff_eq.mp ha), obtain rfl | rfl := ((key a).mp ha), { rw [or.resolve_left ha' ha, swap_apply_left] }, { rw [or.resolve_right ha' ha, swap_apply_right] } } }, { obtain ⟨x, y, hxy, rfl⟩ := h, exact card_support_swap hxy } end /-- If we apply `prod_extend_right a (σ a)` for all `a : α` in turn, we get `prod_congr_right σ`. -/ lemma prod_prod_extend_right {α : Type*} [decidable_eq α] (σ : α → perm β) {l : list α} (hl : l.nodup) (mem_l : ∀ a, a ∈ l) : (l.map (λ a, prod_extend_right a (σ a))).prod = prod_congr_right σ := begin ext ⟨a, b⟩ : 1, -- We'll use induction on the list of elements, -- but we have to keep track of whether we already passed `a` in the list. suffices : (a ∈ l ∧ (l.map (λ a, prod_extend_right a (σ a))).prod (a, b) = (a, σ a b)) ∨ (a ∉ l ∧ (l.map (λ a, prod_extend_right a (σ a))).prod (a, b) = (a, b)), { obtain ⟨_, prod_eq⟩ := or.resolve_right this (not_and.mpr (λ h _, h (mem_l a))), rw [prod_eq, prod_congr_right_apply] }, clear mem_l, induction l with a' l ih, { refine or.inr ⟨list.not_mem_nil _, _⟩, rw [list.map_nil, list.prod_nil, one_apply] }, rw [list.map_cons, list.prod_cons, mul_apply], rcases ih (list.nodup_cons.mp hl).2 with ⟨mem_l, prod_eq⟩ | ⟨not_mem_l, prod_eq⟩; rw prod_eq, { refine or.inl ⟨list.mem_cons_of_mem _ mem_l, _⟩, rw prod_extend_right_apply_ne _ (λ (h : a = a'), (list.nodup_cons.mp hl).1 (h ▸ mem_l)) }, by_cases ha' : a = a', { rw ← ha' at *, refine or.inl ⟨l.mem_cons_self a, _⟩, rw prod_extend_right_apply_eq }, { refine or.inr ⟨λ h, not_or ha' not_mem_l ((list.mem_cons_iff _ _ _).mp h), _⟩, rw prod_extend_right_apply_ne _ ha' }, end section congr variables [decidable_eq β] [fintype β] @[simp] lemma sign_prod_extend_right (a : α) (σ : perm β) : (prod_extend_right a σ).sign = σ.sign := sign_bij (λ (ab : α × β) _, ab.snd) (λ ⟨a', b⟩ hab hab', by simp [eq_of_prod_extend_right_ne hab]) (λ ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ hab₁ hab₂ h, by simpa [eq_of_prod_extend_right_ne hab₁, eq_of_prod_extend_right_ne hab₂] using h) (λ y hy, ⟨(a, y), by simpa, by simp⟩) lemma sign_prod_congr_right (σ : α → perm β) : sign (prod_congr_right σ) = ∏ k, (σ k).sign := begin obtain ⟨l, hl, mem_l⟩ := fintype.exists_univ_list α, have l_to_finset : l.to_finset = finset.univ, { apply eq_top_iff.mpr, intros b _, exact list.mem_to_finset.mpr (mem_l b) }, rw [← prod_prod_extend_right σ hl mem_l, sign.map_list_prod, list.map_map, ← l_to_finset, list.prod_to_finset _ hl], simp_rw ← λ a, sign_prod_extend_right a (σ a) end lemma sign_prod_congr_left (σ : α → perm β) : sign (prod_congr_left σ) = ∏ k, (σ k).sign := begin refine (sign_eq_sign_of_equiv _ _ (prod_comm β α) _).trans (sign_prod_congr_right σ), rintro ⟨b, α⟩, refl end @[simp] lemma sign_perm_congr (e : α ≃ β) (p : perm α) : (e.perm_congr p).sign = p.sign := sign_eq_sign_of_equiv _ _ e.symm (by simp) @[simp] lemma sign_sum_congr (σa : perm α) (σb : perm β) : (sum_congr σa σb).sign = σa.sign * σb.sign := begin suffices : (sum_congr σa (1 : perm β)).sign = σa.sign ∧ (sum_congr (1 : perm α) σb).sign = σb.sign, { rw [←this.1, ←this.2, ←sign_mul, sum_congr_mul, one_mul, mul_one], }, split, { apply σa.swap_induction_on _ (λ σa' a₁ a₂ ha ih, _), { simp }, { rw [←one_mul (1 : perm β), ←sum_congr_mul, sign_mul, sign_mul, ih, sum_congr_swap_one, sign_swap ha, sign_swap (sum.inl_injective.ne_iff.mpr ha)], }, }, { apply σb.swap_induction_on _ (λ σb' b₁ b₂ hb ih, _), { simp }, { rw [←one_mul (1 : perm α), ←sum_congr_mul, sign_mul, sign_mul, ih, sum_congr_one_swap, sign_swap hb, sign_swap (sum.inr_injective.ne_iff.mpr hb)], }, } end @[simp] lemma sign_subtype_congr {p : α → Prop} [decidable_pred p] (ep : perm {a // p a}) (en : perm {a // ¬ p a}) : (ep.subtype_congr en).sign = ep.sign * en.sign := by simp [subtype_congr] @[simp] lemma sign_extend_domain (e : perm α) {p : β → Prop} [decidable_pred p] (f : α ≃ subtype p) : equiv.perm.sign (e.extend_domain f) = equiv.perm.sign e := by simp [equiv.perm.extend_domain] end congr end sign section disjoint variables [fintype α] {f g : perm α} lemma disjoint_iff_disjoint_support : disjoint f g ↔ _root_.disjoint f.support g.support := begin change (∀ x, f x = x ∨ g x = x) ↔ (∀ x, x ∉ (f.support ∩ g.support)), simp_rw [finset.mem_inter, not_and_distrib, mem_support, not_not], end lemma disjoint.disjoint_support (h : disjoint f g) : _root_.disjoint f.support g.support := disjoint_iff_disjoint_support.1 h lemma disjoint.support_mul (h : disjoint f g) : (f * g).support = f.support ∪ g.support := begin refine le_antisymm (support_mul_le _ _) (λ a, _), rw [mem_union, mem_support, mem_support, mem_support, mul_apply, ←not_and_distrib, not_imp_not], exact (h a).elim (λ hf h, ⟨hf, f.apply_eq_iff_eq.mp (h.trans hf.symm)⟩) (λ hg h, ⟨(congr_arg f hg).symm.trans h, hg⟩), end lemma disjoint.card_support_mul (h : disjoint f g) : (f * g).support.card = f.support.card + g.support.card := (congr_arg card h.support_mul).trans (finset.card_disjoint_union h.disjoint_support) lemma disjoint_prod_list_of_disjoint [fintype β] {f : perm β} {l : list (perm β)} (h : ∀ g, g ∈ l → disjoint f g) : disjoint f l.prod := begin classical, intro x, by_cases hx : l.prod x = x, { exact or.inr hx }, { obtain ⟨f, fl, fx⟩ := exists_mem_support_of_mem_support_prod (mem_support.mpr hx), exact (h f fl x).imp_right (λ fx', ((mem_support.mp fx) fx').elim) } end lemma card_support_prod_list_of_pairwise_disjoint {l : list (perm α)} (h : l.pairwise disjoint) : l.prod.support.card = (l.map (finset.card ∘ support)).sum := begin induction l with a t ih, { exact card_support_eq_zero.mpr rfl, }, { obtain ⟨ha, ht⟩ := list.pairwise_cons.1 h, rw [list.prod_cons, list.map_cons, list.sum_cons, ←ih ht], exact (disjoint_prod_list_of_disjoint ha).card_support_mul } end end disjoint end equiv.perm
e0ac520f1d08658c3fa4f8bdb64de92062a35313
0030aab32adb6325aa93cc2051c443945934d083
/src/temp.lean
e1dbf68abaa35f74e5e4bb9215bbc85c07e52d56
[]
no_license
JasonKYi/sylvester
914e1abe9c82a3ab4edec15ef58faa8f31921d8f
dfa7a2f323cf9db5fca0a3b8f852b3b9a058bc56
refs/heads/main
1,682,286,953,450
1,620,149,451,000
1,620,149,451,000
362,582,788
0
0
null
null
null
null
UTF-8
Lean
false
false
10,485
lean
/- Copyright (c) 2021 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import linear_algebra.quadratic_form import analysis.special_functions.pow /-! # Classification of quadratic forms This file contains theorem regarding the classification of quadratic forms. ## Main statements * `quadratic_form.equivalent_sum_squares`: A nondegenerate quadratic form over ℂ is equivalent to the quadratic form corresponding to the sum of squares. * `quadratic_form.equivalent_one_neg_one_weighted_sum_squared`: A nondegenerate quadratic form over ℝ is equivalent to the quadratic form corresponding to a weighted sum of squares with the weights being ±1. This is known as Sylvester's law of inertia. ## Notation In this file we let `R` be a ring, `R₁` a commutative ring and `M`, `M₁` be modules over `R`, while we also let `M` be a module over `R`. We also define the finite type `ι` and let`v` be a `ι`-indexed vector with type `ι → M`. ## TODO - Hasse invariant of a quadratic form - Classification of quadratic forms over Q_p ## Tags quadratic form, classification of quadratic form -/ variables {R : Type*} [ring R] {R₁ : Type*} [comm_ring R₁] variables {M : Type*} [add_comm_group M] [module R M] [module R₁ M] variables {M₁ : Type*} [add_comm_group M₁] [module R M₁] variables {ι : Type*} [fintype ι] {v : ι → M} namespace quadratic_form open_locale big_operators open finset bilin_form /-- A quadratic form composed with a `linear_equiv` is isometric to itself. -/ def isometry_of_comp_linear_equiv (Q : quadratic_form R M) (f : M₁ ≃ₗ[R] M) : Q.isometry (Q.comp (f : M₁ →ₗ[R] M)) := { map_app' := begin intro, simp only [comp_apply, linear_equiv.coe_coe, linear_equiv.to_fun_eq_coe, linear_equiv.apply_symm_apply, f.apply_symm_apply], end, .. f.symm } /-- Given a quadratic form `Q` and a basis, `of_is_basis` is the basis representation of `Q`. -/ noncomputable def of_is_basis (Q : quadratic_form R M) (hv₁ : is_basis R v) : quadratic_form R (ι → R) := Q.comp hv₁.equiv_fun.symm @[simp] lemma isometry_of_is_basis_apply (Q : quadratic_form R M) (hv₁ : is_basis R v) (w : ι → R) : Q.of_is_basis hv₁ w = Q (∑ i : ι, w i • v i) := by { rw ← hv₁.equiv_fun_symm_apply, refl } /-- A quadratic form is isometric to its bases representations. -/ noncomputable def isometry_of_is_basis (Q : quadratic_form R M) (hv₁ : is_basis R v) : isometry Q (Q.of_is_basis hv₁) := isometry_of_comp_linear_equiv Q hv₁.equiv_fun.symm lemma isometry_of_is_Ortho_apply [invertible (2 : R₁)] (Q : quadratic_form R₁ M) (hv₁ : is_basis R₁ v) (hv₂ : (associated Q).is_Ortho v) (w : ι → R₁) : Q.of_is_basis hv₁ w = ∑ i : ι, associated Q (v i) (v i) * w i * w i := begin rw [isometry_of_is_basis_apply, ← @associated_eq_self_apply R₁, sum_left], refine sum_congr rfl (λ j hj, _), rw [sum_right, sum_eq_single j], { rw [smul_left, smul_right], ring }, { intros i _ hij, rw [smul_left, smul_right, show (associated_hom R₁) Q (v j) (v i) = 0, by exact hv₂ i j hij, mul_zero, mul_zero] }, { contradiction } end /-- The weighted sum of squares with respect some weight as a quadratic form. -/ def weighted_sum_squares (w : ι → R₁) : quadratic_form R₁ (ι → R₁) := ∑ i : ι, w i • proj i i @[simp] lemma weighted_sum_squares_apply (w v : ι → R₁) : weighted_sum_squares w v = ∑ i : ι, w i * (v i * v i) := quadratic_form.sum_apply _ _ _ variables {V : Type*} {K : Type*} [field K] [invertible (2 : K)] variables [add_comm_group V] [module K V] [finite_dimensional K V] lemma equivalent_weighted_sum_squares_of_nondegenerate' (Q : quadratic_form K V) (hQ : (associated Q).nondegenerate) : ∃ w : fin (finite_dimensional.finrank K V) → K, (∀ i, w i ≠ 0) ∧ equivalent Q (weighted_sum_squares w) := begin obtain ⟨v, hv₁, hv₂, hv₃⟩ := exists_orthogonal_basis' hQ associated_is_sym, refine ⟨λ i, associated Q (v i) (v i), hv₃, _⟩, refine nonempty.intro _, convert Q.isometry_of_is_basis hv₂, ext w, rw [isometry_of_is_Ortho_apply Q hv₂ hv₁, weighted_sum_squares_apply], refine finset.sum_congr rfl _, intros, rw ← mul_assoc, end lemma equivalent_weighted_sum_squares_of_nondegenerate (Q : quadratic_form K V) (hQ : (associated Q).nondegenerate) : ∃ w : fin (finite_dimensional.finrank K V) → K, equivalent Q (weighted_sum_squares w) := let ⟨w, _, hw₂⟩ := Q.equivalent_weighted_sum_squares_of_nondegenerate' hQ in ⟨w, hw₂⟩ section complex /-- The isometry between a weighted sum of squares on the complex numbers and the sum of squares, i.e. `weighted_sum_squares` with weight `λ i : ι, 1`. -/ noncomputable def isometry_sum_squares [decidable_eq ι] (w : ι → ℂ) (hw : ∀ i : ι, w i ≠ 0) : isometry (weighted_sum_squares w) (weighted_sum_squares (λ _, 1 : ι → ℂ)) := begin have hw' : ∀ i : ι, (w i) ^ - (1 / 2 : ℂ) ≠ 0, { intros i hi, exact hw i ((complex.cpow_eq_zero_iff _ _).1 hi).1 }, convert (weighted_sum_squares w).isometry_of_is_basis (is_basis.smul_of_invertible (pi.is_basis_fun ℂ ι) (λ i, invertible_of_nonzero (hw' i))), ext1 v, rw [isometry_of_is_basis_apply, weighted_sum_squares_apply, weighted_sum_squares_apply], refine sum_congr rfl (λ j hj, _), have hsum : (∑ (i : ι), v i • w i ^ - (1 / 2 : ℂ) • (linear_map.std_basis ℂ (λ (i : ι), ℂ) i) 1) j = v j • w j ^ - (1 / 2 : ℂ), { rw [finset.sum_apply, sum_eq_single j, linear_map.std_basis_apply, pi.smul_apply, pi.smul_apply, function.update_same, smul_eq_mul, smul_eq_mul, smul_eq_mul, mul_one], intros i _ hij, rw [linear_map.std_basis_apply, pi.smul_apply, pi.smul_apply, function.update_noteq hij.symm, pi.zero_apply, smul_eq_mul, smul_eq_mul, mul_zero, mul_zero], intro hj', exact false.elim (hj' hj) }, rw [hsum, smul_eq_mul], suffices : 1 * v j * v j = w j ^ - (1 / 2 : ℂ) * w j ^ - (1 / 2 : ℂ) * w j * v j * v j, { rw [← mul_assoc, this], ring }, rw [← complex.cpow_add _ _ (hw j), show - (1 / 2 : ℂ) + - (1 / 2) = -1, by ring, complex.cpow_neg_one, inv_mul_cancel (hw j)], end . /-- A nondegenerate quadratic form on the complex numbers is equivalent to the sum of squares, i.e. `weighted_sum_squares` with weight `λ i : ι, 1`. -/ theorem equivalent_sum_squares {M : Type*} [add_comm_group M] [module ℂ M] [finite_dimensional ℂ M] (Q : quadratic_form ℂ M) (hQ : (associated Q).nondegenerate) : equivalent Q (weighted_sum_squares (λ _, 1 : fin (finite_dimensional.finrank ℂ M) → ℂ)) := let ⟨w, hw₁, hw₂⟩ := Q.equivalent_weighted_sum_squares_of_nondegenerate' hQ in nonempty.intro $ (classical.choice hw₂).trans (isometry_sum_squares w hw₁) end complex section real /-- The sign function that maps negative real numbers to -1 and nonnegative numbers to 1. -/ noncomputable def sign (r : ℝ) : ℝ := if r < 0 then -1 else 1 lemma sign_apply_eq (r : ℝ) : sign r = -1 ∨ sign r = 1 := begin by_cases r < 0, { exact or.intro_left _ (if_pos h) }, { exact or.intro_right _ (if_neg h) } end lemma sign_mul_nonneg (r : ℝ) : 0 ≤ sign r * r := begin by_cases r < 0, { rw [sign, if_pos h], exact mul_nonneg_of_nonpos_of_nonpos (by norm_num) (le_of_lt h) }, { rw [sign, if_neg h, one_mul], exact not_lt.1 h } end lemma sign_mul_ne_zero_pos (r : ℝ) (hr : r ≠ 0) : 0 < sign r * r := begin refine lt_of_le_of_ne (sign_mul_nonneg r) (λ h, _), rw zero_eq_mul at h, cases sign_apply_eq r with hneg hpos; cases h; { linarith <|> exact hr h } end lemma sign_inv_eq_self (r : ℝ) : (sign r)⁻¹ = sign r := begin cases sign_apply_eq r with h h, { rw h, norm_num }, { rw h, exact inv_one } end /-- The isometry between a weighted sum of squares with weights `u` on the complex numbers and the weighted sum of squares with weights `sign ∘ u`. -/ noncomputable def isometry_sign_weighted_sum_squares' [decidable_eq ι] (u : ι → ℝ) (hu : ∀ i : ι, u i ≠ 0) : isometry (weighted_sum_squares u) (weighted_sum_squares (sign ∘ u)) := begin have hu' : ∀ i : ι, 0 ≠ (sign (u i) * u i) ^ - (1 / 2 : ℝ), { intro i, exact ne_of_lt (real.rpow_pos_of_pos (sign_mul_ne_zero_pos _ (hu i)) _) }, convert ((weighted_sum_squares u).isometry_of_is_basis (is_basis.smul_of_invertible (pi.is_basis_fun ℝ ι) (λ i, invertible_of_nonzero (hu' i).symm))), ext1 v, rw [isometry_of_is_basis_apply, weighted_sum_squares_apply, weighted_sum_squares_apply], refine sum_congr rfl (λ j hj, _), have hsum : (∑ (i : ι), v i • (sign (u i) * u i) ^ - (1 / 2 : ℝ) • (linear_map.std_basis ℝ (λ (i : ι), ℝ) i) 1) j = v j • (sign (u j) * u j) ^ - (1 / 2 : ℝ), { rw [finset.sum_apply, sum_eq_single j, linear_map.std_basis_apply, pi.smul_apply, pi.smul_apply, function.update_same, smul_eq_mul, smul_eq_mul, smul_eq_mul, mul_one], intros i _ hij, rw [linear_map.std_basis_apply, pi.smul_apply, pi.smul_apply, function.update_noteq hij.symm, pi.zero_apply, smul_eq_mul, smul_eq_mul, mul_zero, mul_zero], intro hj', exact false.elim (hj' hj) }, rw [hsum, smul_eq_mul], suffices : (sign ∘ u) j * v j * v j = (sign (u j) * u j) ^ - (1 / 2 : ℝ) * (sign (u j) * u j) ^ - (1 / 2 : ℝ) * u j * v j * v j, { rw [← mul_assoc, this], ring }, rw [← real.rpow_add (sign_mul_ne_zero_pos _ (hu j)), show - (1 / 2 : ℝ) + - (1 / 2) = -1, by ring, real.rpow_neg_one, mul_inv', sign_inv_eq_self, mul_assoc (sign (u j)) (u j)⁻¹, inv_mul_cancel (hu j), mul_one], end /-- Sylvester's law of inertia: A nondegenerate real quadratic form is equivalent to a weighted sum of squares with the weights being ±1. -/ theorem equivalent_one_neg_one_weighted_sum_squared {M : Type*} [add_comm_group M] [module ℝ M] [finite_dimensional ℝ M] (Q : quadratic_form ℝ M) (hQ : (associated Q).nondegenerate) : ∃ w : fin (finite_dimensional.finrank ℝ M) → ℝ, (∀ i, w i = -1 ∨ w i = 1) ∧ equivalent Q (weighted_sum_squares w) := let ⟨w, hw₁, hw₂⟩ := Q.equivalent_weighted_sum_squares_of_nondegenerate' hQ in ⟨sign ∘ w, λ i, sign_apply_eq (w i), hw₂.trans (nonempty.intro $ isometry_sign_weighted_sum_squares' w hw₁)⟩ end real end quadratic_form
55761c51abdcceedba169a3d0fd4c9e1fbe7e190
618003631150032a5676f229d13a079ac875ff77
/src/data/pnat/xgcd.lean
c5c3aacc21178a5bb38f4681a2c761af85494072
[ "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
13,398
lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Neil Strickland This file sets up a version of the Euclidean algorithm that works only with natural numbers. Given a, b > 0, it computes the unique system (w, x, y, z, d) such that the following identities hold: w * z = x * y + 1 a = (w + x) d b = (y + z) d These equations force w, z, d > 0. They also imply that the integers a' = w + x = a / d and b' = y + z = b / d are coprime, and that d is the gcd of a and b. This story is closely related to the structure of SL₂(ℕ) (as a free monoid on two generators) and the theory of continued fractions. -/ import tactic.ring import tactic.abel namespace pnat open nat pnat /-- A term of xgcd_type is a system of six naturals. They should be thought of as representing the matrix [[w, x], [y, z]] = [[wp + 1, x], [y, zp + 1]] together with the vector [a, b] = [ap + 1, bp + 1]. -/ @[derive inhabited] structure xgcd_type := (wp x y zp ap bp : ℕ) namespace xgcd_type variable (u : xgcd_type) instance : has_sizeof xgcd_type := ⟨λ u, u.bp⟩ /-- The has_repr instance converts terms to strings in a way that reflects the matrix/vector interpretation as above. -/ instance : has_repr xgcd_type := ⟨λ u, "[[[" ++ (repr (u.wp + 1)) ++ ", " ++ (repr u.x) ++ "], [" ++ (repr u.y) ++ ", " ++ (repr (u.zp + 1)) ++ "]], [" ++ (repr (u.ap + 1)) ++ ", " ++ (repr (u.bp + 1)) ++ "]]"⟩ def mk' (w : ℕ+) (x : ℕ) (y : ℕ) (z : ℕ+) (a : ℕ+) (b : ℕ+) : xgcd_type := mk w.val.pred x y z.val.pred a.val.pred b.val.pred def w : ℕ+ := succ_pnat u.wp def z : ℕ+ := succ_pnat u.zp def a : ℕ+ := succ_pnat u.ap def b : ℕ+ := succ_pnat u.bp def r : ℕ := (u.ap + 1) % (u.bp + 1) def q : ℕ := (u.ap + 1) / (u.bp + 1) def qp : ℕ := u.q - 1 /-- The map v gives the product of the matrix [[w, x], [y, z]] = [[wp + 1, x], [y, zp + 1]] and the vector [a, b] = [ap + 1, bp + 1]. The map vp gives [sp, tp] such that v = [sp + 1, tp + 1]. -/ def vp : ℕ × ℕ := ⟨ u.wp + u.x + u.ap + u.wp * u.ap + u.x * u.bp, u.y + u.zp + u.bp + u.y * u.ap + u.zp * u.bp ⟩ def v : ℕ × ℕ := ⟨u.w * u.a + u.x * u.b, u.y * u.a + u.z * u.b⟩ def succ₂ (t : ℕ × ℕ) : ℕ × ℕ := ⟨t.1.succ, t.2.succ⟩ theorem v_eq_succ_vp : u.v = succ₂ u.vp := by { ext; dsimp [v, vp, w, z, a, b, succ₂]; repeat { rw [nat.succ_eq_add_one] }; ring } /-- is_special holds if the matrix has determinant one. -/ def is_special : Prop := u.wp + u.zp + u.wp * u.zp = u.x * u.y def is_special' : Prop := u.w * u.z = succ_pnat (u.x * u.y) theorem is_special_iff : u.is_special ↔ u.is_special' := begin dsimp [is_special, is_special'], split; intro h, { apply eq, dsimp [w, z, succ_pnat], rw [← h], repeat { rw [nat.succ_eq_add_one] }, ring }, { apply nat.succ_inj, replace h := congr_arg (coe : ℕ+ → ℕ) h, rw [mul_coe, w, z] at h, repeat { rw [succ_pnat_coe, nat.succ_eq_add_one] at h }, repeat { rw [nat.succ_eq_add_one] }, rw [← h], ring } end /-- is_reduced holds if the two entries in the vector are the same. The reduction algorithm will produce a system with this property, whose product vector is the same as for the original system. -/ def is_reduced : Prop := u.ap = u.bp def is_reduced' : Prop := u.a = u.b theorem is_reduced_iff : u.is_reduced ↔ u.is_reduced' := ⟨ congr_arg succ_pnat, succ_pnat_inj ⟩ def flip : xgcd_type := { wp := u.zp, x := u.y, y := u.x, zp := u.wp, ap := u.bp, bp := u.ap } @[simp] theorem flip_w : (flip u).w = u.z := rfl @[simp] theorem flip_x : (flip u).x = u.y := rfl @[simp] theorem flip_y : (flip u).y = u.x := rfl @[simp] theorem flip_z : (flip u).z = u.w := rfl @[simp] theorem flip_a : (flip u).a = u.b := rfl @[simp] theorem flip_b : (flip u).b = u.a := rfl theorem flip_is_reduced : (flip u).is_reduced ↔ u.is_reduced := by { dsimp [is_reduced, flip], split; intro h; exact h.symm } theorem flip_is_special : (flip u).is_special ↔ u.is_special := by { dsimp [is_special, flip], rw[mul_comm u.x, mul_comm u.zp, add_comm u.zp] } theorem flip_v : (flip u).v = (u.v).swap := by { dsimp [v], ext, { simp only [], ring }, { simp only [], ring } } /-- Properties of division with remainder for a / b. -/ theorem rq_eq : u.r + (u.bp + 1) * u.q = u.ap + 1 := nat.mod_add_div (u.ap + 1) (u.bp + 1) theorem qp_eq (hr : u.r = 0) : u.q = u.qp + 1 := begin by_cases hq : u.q = 0, { let h := u.rq_eq, rw [hr, hq, mul_zero, add_zero] at h, cases h }, { exact (nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero hq)).symm } end /-- The following function provides the starting point for our algorithm. We will apply an iterative reduction process to it, which will produce a system satisfying is_reduced. The gcd can be read off from this final system. -/ def start (a b : ℕ+) : xgcd_type := ⟨0, 0, 0, 0, a - 1, b - 1⟩ theorem start_is_special (a b : ℕ+) : (start a b).is_special := by { dsimp [start, is_special], refl } theorem start_v (a b : ℕ+) : (start a b).v = ⟨a, b⟩ := begin dsimp [start, v, xgcd_type.a, xgcd_type.b, w, z], rw [one_mul, one_mul, zero_mul, zero_mul, zero_add, add_zero], rw [← nat.pred_eq_sub_one, ← nat.pred_eq_sub_one], rw [nat.succ_pred_eq_of_pos a.pos, nat.succ_pred_eq_of_pos b.pos] end def finish : xgcd_type := xgcd_type.mk u.wp ((u.wp + 1) * u.qp + u.x) u.y (u.y * u.qp + u.zp) u.bp u.bp theorem finish_is_reduced : u.finish.is_reduced := by { dsimp [is_reduced], refl } theorem finish_is_special (hs : u.is_special) : u.finish.is_special := begin dsimp [is_special, finish] at hs ⊢, rw [add_mul _ _ u.y, add_comm _ (u.x * u.y), ← hs], ring end theorem finish_v (hr : u.r = 0) : u.finish.v = u.v := begin let ha : u.r + u.b * u.q = u.a := u.rq_eq, rw [hr, zero_add] at ha, ext, { change (u.wp + 1) * u.b + ((u.wp + 1) * u.qp + u.x) * u.b = u.w * u.a + u.x * u.b, have : u.wp + 1 = u.w := rfl, rw [this, ← ha, u.qp_eq hr], ring }, { change u.y * u.b + (u.y * u.qp + u.z) * u.b = u.y * u.a + u.z * u.b, rw [← ha, u.qp_eq hr], ring } end /-- This is the main reduction step, which is used when u.r ≠ 0, or equivalently b does not divide a. -/ def step : xgcd_type := xgcd_type.mk (u.y * u.q + u.zp) u.y ((u.wp + 1) * u.q + u.x) u.wp u.bp (u.r - 1) /-- We will apply the above step recursively. The following result is used to ensure that the process terminates. -/ theorem step_wf (hr : u.r ≠ 0) : sizeof u.step < sizeof u := begin change u.r - 1 < u.bp, have h₀ : (u.r - 1) + 1 = u.r := nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero hr), have h₁ : u.r < u.bp + 1 := nat.mod_lt (u.ap + 1) u.bp.succ_pos, rw[← h₀] at h₁, exact lt_of_succ_lt_succ h₁, end theorem step_is_special (hs : u.is_special) : u.step.is_special := begin dsimp [is_special, step] at hs ⊢, rw [mul_add, mul_comm u.y u.x, ← hs], ring end /-- The reduction step does not change the product vector. -/ theorem step_v (hr : u.r ≠ 0) : u.step.v = (u.v).swap := begin let ha : u.r + u.b * u.q = u.a := u.rq_eq, let hr : (u.r - 1) + 1 = u.r := (add_comm _ 1).trans (nat.add_sub_of_le (nat.pos_of_ne_zero hr)), ext, { change ((u.y * u.q + u.z) * u.b + u.y * (u.r - 1 + 1) : ℕ) = u.y * u.a + u.z * u.b, rw [← ha, hr], ring }, { change ((u.w * u.q + u.x) * u.b + u.w * (u.r - 1 + 1) : ℕ) = u.w * u.a + u.x * u.b, rw [← ha, hr], ring } end /-- We can now define the full reduction function, which applies step as long as possible, and then applies finish. Note that the "have" statement puts a fact in the local context, and the equation compiler uses this fact to help construct the full definition in terms of well-founded recursion. The same fact needs to be introduced in all the inductive proofs of properties given below. -/ def reduce : xgcd_type → xgcd_type | u := dite (u.r = 0) (λ h, u.finish) (λ h, have sizeof u.step < sizeof u, from u.step_wf h, flip (reduce u.step)) theorem reduce_a {u : xgcd_type} (h : u.r = 0) : u.reduce = u.finish := by { rw [reduce], simp only [], rw [if_pos h] } theorem reduce_b {u : xgcd_type} (h : u.r ≠ 0) : u.reduce = u.step.reduce.flip := by { rw [reduce], simp only [], rw [if_neg h, step] } theorem reduce_reduced : ∀ (u : xgcd_type), u.reduce.is_reduced | u := dite (u.r = 0) (λ h, by { rw [reduce_a h], exact u.finish_is_reduced }) (λ h, have sizeof u.step < sizeof u, from u.step_wf h, by { rw [reduce_b h, flip_is_reduced], apply reduce_reduced }) theorem reduce_reduced' (u : xgcd_type) : u.reduce.is_reduced' := (is_reduced_iff _).mp u.reduce_reduced theorem reduce_special : ∀ (u : xgcd_type), u.is_special → u.reduce.is_special | u := dite (u.r = 0) (λ h hs, by { rw [reduce_a h], exact u.finish_is_special hs }) (λ h hs, have sizeof u.step < sizeof u, from u.step_wf h, by { rw [reduce_b h], let u' := u.step.reduce, have : u'.is_special := reduce_special u.step (u.step_is_special hs), exact (flip_is_special _).mpr (reduce_special _ (u.step_is_special hs)) }) theorem reduce_special' (u : xgcd_type) (hs : u.is_special) : u.reduce.is_special' := (is_special_iff _).mp (u.reduce_special hs) theorem reduce_v : ∀ (u : xgcd_type), u.reduce.v = u.v | u := dite (u.r = 0) (λ h, by {rw[reduce_a h, finish_v u h]}) (λ h, have sizeof u.step < sizeof u, from u.step_wf h, by { rw[reduce_b h, flip_v, reduce_v (step u), step_v u h, prod.swap_swap] }) end xgcd_type section gcd variables (a b : ℕ+) def xgcd: xgcd_type := (xgcd_type.start a b).reduce def gcd_d : ℕ+ := (xgcd a b).a def gcd_w : ℕ+ := (xgcd a b).w def gcd_x : ℕ := (xgcd a b).x def gcd_y : ℕ := (xgcd a b).y def gcd_z : ℕ+ := (xgcd a b).z def gcd_a' : ℕ+ := succ_pnat ((xgcd a b).wp + (xgcd a b).x) def gcd_b' : ℕ+ := succ_pnat ((xgcd a b).y + (xgcd a b).zp) theorem gcd_a'_coe : ((gcd_a' a b) : ℕ) = (gcd_w a b) + (gcd_x a b) := by { dsimp [gcd_a', gcd_w, xgcd_type.w], rw [nat.succ_eq_add_one, nat.succ_eq_add_one], ring } theorem gcd_b'_coe : ((gcd_b' a b) : ℕ) = (gcd_y a b) + (gcd_z a b) := by { dsimp [gcd_b', gcd_z, xgcd_type.z], rw [nat.succ_eq_add_one, nat.succ_eq_add_one], ring } theorem gcd_props : let d := gcd_d a b, w := gcd_w a b, x := gcd_x a b, y := gcd_y a b, z := gcd_z a b, a' := gcd_a' a b, b' := gcd_b' a b in (w * z = succ_pnat (x * y) ∧ (a = a' * d) ∧ (b = b' * d) ∧ z * a' = succ_pnat (x * b') ∧ w * b' = succ_pnat (y * a') ∧ (z * a : ℕ) = x * b + d ∧ (w * b : ℕ) = y * a + d ) := begin intros, let u := (xgcd_type.start a b), let ur := u.reduce, have ha : d = ur.a := rfl, have hb : d = ur.b := u.reduce_reduced', have ha' : (a' : ℕ) = w + x := gcd_a'_coe a b, have hb' : (b' : ℕ) = y + z := gcd_b'_coe a b, have hdet : w * z = succ_pnat (x * y) := u.reduce_special' rfl, split, exact hdet, have hdet' : ((w * z) : ℕ) = x * y + 1 := by { rw [← mul_coe, hdet, succ_pnat_coe] }, have huv : u.v = ⟨a, b⟩ := (xgcd_type.start_v a b), let hv : prod.mk (w * d + x * ur.b : ℕ) (y * d + z * ur.b : ℕ) = ⟨a, b⟩ := u.reduce_v.trans (xgcd_type.start_v a b), rw [← hb, ← add_mul, ← add_mul, ← ha', ← hb'] at hv, have ha'' : (a : ℕ) = a' * d := (congr_arg prod.fst hv).symm, have hb'' : (b : ℕ) = b' * d := (congr_arg prod.snd hv).symm, split, exact eq ha'', split, exact eq hb'', have hza' : (z * a' : ℕ) = x * b' + 1, by { rw [ha', hb', mul_add, mul_add, mul_comm (z : ℕ), hdet'], ring }, have hwb' : (w * b' : ℕ) = y * a' + 1, by { rw [ha', hb', mul_add, mul_add, hdet'], ring }, split, { apply eq, rw [succ_pnat_coe, nat.succ_eq_add_one, mul_coe, hza'] }, split, { apply eq, rw [succ_pnat_coe, nat.succ_eq_add_one, mul_coe, hwb'] }, rw [ha'', hb''], repeat { rw [← mul_assoc] }, rw [hza', hwb'], split; ring, end theorem gcd_eq : gcd_d a b = gcd a b := begin rcases gcd_props a b with ⟨h₀, h₁, h₂, h₃, h₄, h₅, h₆⟩, apply dvd_antisymm, { apply dvd_gcd, exact dvd_intro (gcd_a' a b) (h₁.trans (mul_comm _ _)).symm, exact dvd_intro (gcd_b' a b) (h₂.trans (mul_comm _ _)).symm}, { have h₇ := calc ((gcd a b) : ℕ) ∣ a : nat.gcd_dvd_left a b ... ∣ (gcd_z a b) * a : dvd_mul_left _ _, have h₈ := calc ((gcd a b) : ℕ) ∣ b : nat.gcd_dvd_right a b ... ∣ (gcd_x a b) * b : dvd_mul_left _ _, rw[h₅] at h₇, exact (nat.dvd_add_iff_right h₈).mpr h₇} end theorem gcd_det_eq : (gcd_w a b) * (gcd_z a b) = succ_pnat ((gcd_x a b) * (gcd_y a b)) := (gcd_props a b).1 theorem gcd_a_eq : a = (gcd_a' a b) * (gcd a b) := (gcd_eq a b) ▸ (gcd_props a b).2.1 theorem gcd_b_eq : b = (gcd_b' a b) * (gcd a b) := (gcd_eq a b) ▸ (gcd_props a b).2.2.1 theorem gcd_rel_left' : (gcd_z a b) * (gcd_a' a b) = succ_pnat ((gcd_x a b) * (gcd_b' a b)) := (gcd_props a b).2.2.2.1 theorem gcd_rel_right' : (gcd_w a b) * (gcd_b' a b) = succ_pnat ((gcd_y a b) * (gcd_a' a b)) := (gcd_props a b).2.2.2.2.1 theorem gcd_rel_left : ((gcd_z a b) * a : ℕ) = (gcd_x a b) * b + (gcd a b) := (gcd_eq a b) ▸ (gcd_props a b).2.2.2.2.2.1 theorem gcd_rel_right : ((gcd_w a b) * b : ℕ) = (gcd_y a b) * a + (gcd a b) := (gcd_eq a b) ▸ (gcd_props a b).2.2.2.2.2.2 end gcd end pnat
9059bba46354eafa1a49b34086d3b2d57fcff6b7
5ee26964f602030578ef0159d46145dd2e357ba5
/src/for_mathlib/with_zero.lean
602eb21ff81a7e8f565af401d2d325945b76c7bb
[ "Apache-2.0" ]
permissive
fpvandoorn/lean-perfectoid-spaces
569b4006fdfe491ca8b58dd817bb56138ada761f
06cec51438b168837fc6e9268945735037fd1db6
refs/heads/master
1,590,154,571,918
1,557,685,392,000
1,557,685,392,000
186,363,547
0
0
Apache-2.0
1,557,730,933,000
1,557,730,933,000
null
UTF-8
Lean
false
false
4,953
lean
import data.equiv.basic import group_theory.subgroup universes u v namespace with_zero variables {α : Type u} {β : Type v} @[simp] theorem zero_le [preorder α] {x : with_zero α} : 0 ≤ x := begin intros y hy, cases hy end @[simp] theorem none_le [preorder α] {x : with_zero α} : @has_le.le (with_zero α) _ none x := zero_le @[simp] theorem none_lt_some [preorder α] {a : α} : (0 : with_zero α) < some a := begin use a, use rfl, intros y hy, cases hy, end @[simp] theorem zero_lt_some [preorder α] {a : α} : @has_lt.lt (with_zero α) _ 0 (some a : with_zero α) := none_lt_some @[simp] theorem not_some_le_zero [preorder α] {x : α} : ¬ @has_le.le (with_zero α) _ (some x) 0 := λ h, by rcases h x rfl with ⟨y, hy, h⟩; cases hy @[simp] theorem not_some_le_none [preorder α] {x : α} : ¬ @has_le.le (with_zero α) _ (some x) none := not_some_le_zero @[simp] theorem not_some_eq_zero [preorder α] {x : α} : ¬ (some x : with_zero α) = (0 : with_zero α) := by apply option.no_confusion @[simp] theorem not_some_eq_none [preorder α] {x : α} : ¬ (some x : with_zero α) = none := by apply option.no_confusion theorem some_le_some_of_le [preorder α] {x y : α} (h : x ≤ y) : (x : with_zero α) ≤ y := λ a ha, ⟨y, rfl, by cases ha; assumption⟩ @[simp] theorem some_le_some' [preorder α] {x y : α} : (x : with_zero α) ≤ (y : with_zero α) ↔ x ≤ y := ⟨λ h, by rcases (h x rfl) with ⟨z, ⟨h2⟩, h3⟩; exact h3, some_le_some_of_le⟩ @[simp] theorem some_le_some [preorder α] {x y : α} : @has_le.le (with_zero α) _ (some x) (some y) ↔ x ≤ y := some_le_some' @[simp] theorem le_zero_iff_eq_zero [partial_order α] {x : with_zero α} : x ≤ 0 ↔ x = 0 := by cases x; simp; try {refl}; {intro h, exact option.no_confusion h} def map (f : α → β) : with_zero α → with_zero β := option.map f @[simp] lemma map_zero {f : α → β} : map f 0 = 0 := option.map_none' @[simp] lemma map_none {f : α → β} : map f none = 0 := option.map_none' @[simp] lemma map_some {f : α → β} {a : α} : map f (some a) = some (f a) := option.map_some' lemma map_id {α : Type*} : map (id : α → α) = id := option.map_id lemma map_comp {α β γ : Type*} (f : α → β) (g : β → γ) (r : with_zero α) : with_zero.map (g ∘ f) r = (with_zero.map g) ((with_zero.map f) r) := by cases r; refl lemma map_eq_zero_iff {f : α → β} {a : with_zero α} : map f a = 0 ↔ a = 0 := begin split; intro h, { cases a, {refl}, rw map_some at h, revert h, exact dec_trivial }, { rw h, exact map_zero } end theorem injective_map {f : α → β} (H : function.injective f) : function.injective (map f) := option.injective_map H theorem map_monotone [preorder α] [preorder β] {f : α → β} (H : monotone f) : monotone (map f) := begin intros x y, cases x; cases y, {simp}, {simp}, {simp}, {simpa using @H _ _} end theorem map_strict_mono [linear_order α] [partial_order β] {f : α → β} (H : ∀ a b, a < b → f a < f b) : strict_mono (map f) := begin intros x y, cases x; cases y, {simp}, {simp}, {simp}, {simpa using H _ _ } end theorem map_le [preorder α] [preorder β] {f : α → β} (H : ∀ a b : α, a ≤ b ↔ f a ≤ f b) (x y : with_zero α) : x ≤ y ↔ map f x ≤ map f y := begin cases x; cases y, { convert iff.refl true; rw eq_true; show none ≤ _; simp}, { convert iff.refl true; simp}, { convert iff.refl false; simp}, { simp [H x y]} end lemma coe_min (x y : α) [decidable_linear_order α]: ((min x y : α) : with_zero α) = min (x: with_zero α) (y : with_zero α) := begin by_cases h: x ≤ y, { simp [min_eq_left, h] }, { replace h : y ≤ x := le_of_not_le h, simp [min_eq_right, h] } end section group variables [group α] lemma mul_left_cancel : ∀ {x : with_zero α} (h : x ≠ 0) {y z : with_zero α} , x * y = x * z → y = z | 0 h := false.elim $ h rfl | (a : α) h := λ y z h2, begin have h3 : (a⁻¹ : with_zero α) * (a * y) = a⁻¹ * (a * z) := by rw h2, rwa [←mul_assoc, ←mul_assoc, with_zero.mul_left_inv _ h, one_mul, one_mul] at h3, end lemma mul_right_cancel : ∀ {x : with_zero α} (h : x ≠ 0) {y z : with_zero α} , y * x = z * x → y = z | 0 h := false.elim $ h rfl | (a : α) h := λ y z h2, begin have h3 : (y * a) * a⁻¹ = (z * a) * a⁻¹ := by rw h2, rwa [mul_assoc, mul_assoc, with_zero.mul_right_inv _ h, mul_one, mul_one] at h3, end lemma mul_inv_eq_of_eq_mul : ∀ {x : with_zero α} (h : x ≠ 0) {y z : with_zero α} , y = z * x → y * x⁻¹ = z | 0 h := false.elim $ h rfl | (x : α) h := λ y z h2, begin apply with_zero.mul_right_cancel h, rwa [mul_assoc, with_zero.mul_left_inv _ h, mul_one] end lemma eq_mul_inv_of_mul_eq {x : with_zero α} (h : x ≠ 0) {y z : with_zero α} (h2 : z * x = y) : z = y * x⁻¹ := eq.symm $ mul_inv_eq_of_eq_mul h h2.symm end group end with_zero
ae200b0c71d84dbe18037c32df32d653632ff1eb
957a80ea22c5abb4f4670b250d55534d9db99108
/library/init/algebra/order.lean
9c5598237a3cfb00be5cb5d0641e9bc756eb795a
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
8,385
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.logic init.classical init.meta.name /- Make sure instances defined in this file have lower priority than the ones defined for concrete structures -/ set_option default_priority 100 set_option old_structure_cmd true universe u variables {α : Type u} set_option auto_param.check_exists false class preorder (α : Type u) extends has_le α, has_lt α := (le_refl : ∀ a : α, a ≤ a) (le_trans : ∀ a b c : α, a ≤ b → b ≤ c → a ≤ c) (lt := λ a b, a ≤ b ∧ ¬ b ≤ a) (lt_iff_le_not_le : ∀ a b : α, a < b ↔ (a ≤ b ∧ ¬ b ≤ a) . order_laws_tac) class partial_order (α : Type u) extends preorder α := (le_antisymm : ∀ a b : α, a ≤ b → b ≤ a → a = b) class linear_order (α : Type u) extends partial_order α := (le_total : ∀ a b : α, a ≤ b ∨ b ≤ a) @[refl] lemma le_refl [preorder α] : ∀ a : α, a ≤ a := preorder.le_refl @[trans] lemma le_trans [preorder α] : ∀ {a b c : α}, a ≤ b → b ≤ c → a ≤ c := preorder.le_trans lemma lt_iff_le_not_le [preorder α] : ∀ {a b : α}, a < b ↔ (a ≤ b ∧ ¬ b ≤ a) := preorder.lt_iff_le_not_le _ lemma lt_of_le_not_le [preorder α] : ∀ {a b : α}, a ≤ b → ¬ b ≤ a → a < b | a b hab hba := lt_iff_le_not_le.mpr ⟨hab, hba⟩ lemma le_not_le_of_lt [preorder α] : ∀ {a b : α}, a < b → a ≤ b ∧ ¬ b ≤ a | a b hab := lt_iff_le_not_le.mp hab lemma le_antisymm [partial_order α] : ∀ {a b : α}, a ≤ b → b ≤ a → a = b := partial_order.le_antisymm lemma le_of_eq [preorder α] {a b : α} : a = b → a ≤ b := λ h, h ▸ le_refl a lemma le_antisymm_iff [partial_order α] {a b : α} : a = b ↔ a ≤ b ∧ b ≤ a := ⟨λe, ⟨le_of_eq e, le_of_eq e.symm⟩, λ⟨h1, h2⟩, le_antisymm h1 h2⟩ @[trans] lemma ge_trans [preorder α] : ∀ {a b c : α}, a ≥ b → b ≥ c → a ≥ c := λ a b c h₁ h₂, le_trans h₂ h₁ lemma le_total [linear_order α] : ∀ a b : α, a ≤ b ∨ b ≤ a := linear_order.le_total lemma le_of_not_ge [linear_order α] {a b : α} : ¬ a ≥ b → a ≤ b := or.resolve_left (le_total b a) lemma le_of_not_le [linear_order α] {a b : α} : ¬ a ≤ b → b ≤ a := or.resolve_left (le_total a b) lemma lt_irrefl [preorder α] : ∀ a : α, ¬ a < a | a haa := match le_not_le_of_lt haa with | ⟨h1, h2⟩ := false.rec _ (h2 h1) end lemma gt_irrefl [preorder α] : ∀ a : α, ¬ a > a := lt_irrefl @[trans] lemma lt_trans [preorder α] : ∀ {a b c : α}, a < b → b < c → a < c | a b c hab hbc := match le_not_le_of_lt hab, le_not_le_of_lt hbc with | ⟨hab, hba⟩, ⟨hbc, hcb⟩ := lt_of_le_not_le (le_trans hab hbc) (λ hca, hcb (le_trans hca hab)) end def lt.trans := @lt_trans @[trans] lemma gt_trans [preorder α] : ∀ {a b c : α}, a > b → b > c → a > c := λ a b c h₁ h₂, lt_trans h₂ h₁ def gt.trans := @gt_trans lemma ne_of_lt [preorder α] {a b : α} (h : a < b) : a ≠ b := λ he, absurd h (he ▸ lt_irrefl a) lemma ne_of_gt [preorder α] {a b : α} (h : a > b) : a ≠ b := λ he, absurd h (he ▸ lt_irrefl a) lemma lt_asymm [preorder α] {a b : α} (h : a < b) : ¬ b < a := λ h1 : b < a, lt_irrefl a (lt_trans h h1) lemma not_lt_of_gt [linear_order α] {a b : α} (h : a > b) : ¬ a < b := lt_asymm h lemma le_of_lt [preorder α] : ∀ {a b : α}, a < b → a ≤ b | a b hab := (le_not_le_of_lt hab).left @[trans] lemma lt_of_lt_of_le [preorder α] : ∀ {a b c : α}, a < b → b ≤ c → a < c | a b c hab hbc := let ⟨hab, hba⟩ := le_not_le_of_lt hab in lt_of_le_not_le (le_trans hab hbc) $ λ hca, hba (le_trans hbc hca) @[trans] lemma lt_of_le_of_lt [preorder α] : ∀ {a b c : α}, a ≤ b → b < c → a < c | a b c hab hbc := let ⟨hbc, hcb⟩ := le_not_le_of_lt hbc in lt_of_le_not_le (le_trans hab hbc) $ λ hca, hcb (le_trans hca hab) @[trans] lemma gt_of_gt_of_ge [preorder α] {a b c : α} (h₁ : a > b) (h₂ : b ≥ c) : a > c := lt_of_le_of_lt h₂ h₁ @[trans] lemma gt_of_ge_of_gt [preorder α] {a b c : α} (h₁ : a ≥ b) (h₂ : b > c) : a > c := lt_of_lt_of_le h₂ h₁ lemma not_le_of_gt [preorder α] {a b : α} (h : a > b) : ¬ a ≤ b := (le_not_le_of_lt h).right lemma not_lt_of_ge [preorder α] {a b : α} (h : a ≥ b) : ¬ a < b := λ hab, not_le_of_gt hab h lemma lt_or_eq_of_le [partial_order α] : ∀ {a b : α}, a ≤ b → a < b ∨ a = b | a b hab := classical.by_cases (λ hba : b ≤ a, or.inr (le_antisymm hab hba)) (λ hba, or.inl (lt_of_le_not_le hab hba)) lemma le_of_lt_or_eq [preorder α] : ∀ {a b : α}, (a < b ∨ a = b) → a ≤ b | a b (or.inl hab) := le_of_lt hab | a b (or.inr hab) := hab ▸ le_refl _ lemma le_iff_lt_or_eq [partial_order α] : ∀ {a b : α}, a ≤ b ↔ a < b ∨ a = b | a b := ⟨lt_or_eq_of_le, le_of_lt_or_eq⟩ lemma lt_of_le_of_ne [partial_order α] {a b : α} : a ≤ b → a ≠ b → a < b := λ h₁ h₂, or.resolve_right (lt_or_eq_of_le h₁) h₂ lemma lt_trichotomy [linear_order α] (a b : α) : a < b ∨ a = b ∨ b < a := or.elim (le_total a b) (λ h : a ≤ b, or.elim (lt_or_eq_of_le h) (λ h : a < b, or.inl h) (λ h : a = b, or.inr (or.inl h))) (λ h : b ≤ a, or.elim (lt_or_eq_of_le h) (λ h : b < a, or.inr (or.inr h)) (λ h : b = a, or.inr (or.inl h.symm))) lemma le_of_not_gt [linear_order α] {a b : α} (h : ¬ a > b) : a ≤ b := match lt_trichotomy a b with | or.inl hlt := le_of_lt hlt | or.inr (or.inl heq) := heq ▸ le_refl a | or.inr (or.inr hgt) := absurd hgt h end lemma lt_of_not_ge [linear_order α] {a b : α} (h : ¬ a ≥ b) : a < b := match lt_trichotomy a b with | or.inl hlt := hlt | or.inr (or.inl heq) := absurd (heq ▸ le_refl a : a ≥ b) h | or.inr (or.inr hgt) := absurd (le_of_lt hgt) h end lemma lt_or_ge [linear_order α] (a b : α) : a < b ∨ a ≥ b := match lt_trichotomy a b with | or.inl hlt := or.inl hlt | or.inr (or.inl heq) := or.inr (heq ▸ le_refl a) | or.inr (or.inr hgt) := or.inr (le_of_lt hgt) end lemma le_or_gt [linear_order α] (a b : α) : a ≤ b ∨ a > b := or.swap (lt_or_ge b a) lemma lt_or_gt_of_ne [linear_order α] {a b : α} (h : a ≠ b) : a < b ∨ a > b := match lt_trichotomy a b with | or.inl hlt := or.inl hlt | or.inr (or.inl heq) := absurd heq h | or.inr (or.inr hgt) := or.inr hgt end lemma le_of_eq_or_lt [preorder α] {a b : α} (h : a = b ∨ a < b) : a ≤ b := or.elim h le_of_eq le_of_lt lemma ne_iff_lt_or_gt [linear_order α] {a b : α} : a ≠ b ↔ a < b ∨ a > b := ⟨lt_or_gt_of_ne, λo, or.elim o ne_of_lt ne_of_gt⟩ lemma lt_iff_not_ge [linear_order α] (x y : α) : x < y ↔ ¬ x ≥ y := ⟨not_le_of_gt, lt_of_not_ge⟩ instance decidable_lt_of_decidable_le [preorder α] [decidable_rel ((≤) : α → α → Prop)] : decidable_rel ((<) : α → α → Prop) | a b := if hab : a ≤ b then if hba : b ≤ a then is_false $ λ hab', not_le_of_gt hab' hba else is_true $ lt_of_le_not_le hab hba else is_false $ λ hab', hab (le_of_lt hab') instance decidable_eq_of_decidable_le [partial_order α] [decidable_rel ((≤) : α → α → Prop)] : decidable_eq α | a b := if hab : a ≤ b then if hba : b ≤ a then is_true (le_antisymm hab hba) else is_false (λ heq, hba (heq ▸ le_refl _)) else is_false (λ heq, hab (heq ▸ le_refl _)) class decidable_linear_order (α : Type u) extends linear_order α := (decidable_le : decidable_rel (≤)) (decidable_eq : decidable_eq α := @decidable_eq_of_decidable_le _ _ decidable_le) (decidable_lt : decidable_rel ((<) : α → α → Prop) := @decidable_lt_of_decidable_le _ _ decidable_le) instance [decidable_linear_order α] (a b : α) : decidable (a < b) := decidable_linear_order.decidable_lt α a b instance [decidable_linear_order α] (a b : α) : decidable (a ≤ b) := decidable_linear_order.decidable_le α a b instance [decidable_linear_order α] (a b : α) : decidable (a = b) := decidable_linear_order.decidable_eq α a b lemma eq_or_lt_of_not_lt [decidable_linear_order α] {a b : α} (h : ¬ a < b) : a = b ∨ b < a := if h₁ : a = b then or.inl h₁ else or.inr (lt_of_not_ge (λ hge, h (lt_of_le_of_ne hge h₁)))
62ae44e6009d4bf9877548ff0720e9444f4aa676
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/stage0/src/Lean/Util/Recognizers.lean
34b47d5dd09286f46e7bf1bb6f71e4d1faff837b
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,699
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Environment namespace Lean namespace Expr @[inline] def const? (e : Expr) : Option (Name × List Level) := match e with | Expr.const n us _ => some (n, us) | _ => none @[inline] def app1? (e : Expr) (fName : Name) : Option Expr := if e.isAppOfArity fName 1 then some e.appArg! else none @[inline] def app2? (e : Expr) (fName : Name) : Option (Expr × Expr) := if e.isAppOfArity fName 2 then some (e.appFn!.appArg!, e.appArg!) else none @[inline] def app3? (e : Expr) (fName : Name) : Option (Expr × Expr × Expr) := if e.isAppOfArity fName 3 then some (e.appFn!.appFn!.appArg!, e.appFn!.appArg!, e.appArg!) else none @[inline] def app4? (e : Expr) (fName : Name) : Option (Expr × Expr × Expr × Expr) := if e.isAppOfArity fName 4 then some (e.appFn!.appFn!.appFn!.appArg!, e.appFn!.appFn!.appArg!, e.appFn!.appArg!, e.appArg!) else none @[inline] def eq? (p : Expr) : Option (Expr × Expr × Expr) := p.app3? ``Eq @[inline] def ne? (p : Expr) : Option (Expr × Expr × Expr) := p.app3? ``Ne @[inline] def iff? (p : Expr) : Option (Expr × Expr) := p.app2? ``Iff @[inline] def not? (p : Expr) : Option Expr := p.app1? ``Not @[inline] def heq? (p : Expr) : Option (Expr × Expr × Expr × Expr) := p.app4? ``HEq def natAdd? (e : Expr) : Option (Expr × Expr) := e.app2? ``Nat.add @[inline] def arrow? : Expr → Option (Expr × Expr) | Expr.forallE _ α β _ => if β.hasLooseBVars then none else some (α, β) | _ => none def isEq (e : Expr) := e.isAppOfArity ``Eq 3 def isHEq (e : Expr) := e.isAppOfArity ``HEq 4 partial def listLit? (e : Expr) : Option (Expr × List Expr) := let rec loop (e : Expr) (acc : List Expr) := if e.isAppOfArity ``List.nil 1 then some (e.appArg!, acc.reverse) else if e.isAppOfArity ``List.cons 3 then loop e.appArg! (e.appFn!.appArg! :: acc) else none loop e [] def arrayLit? (e : Expr) : Option (Expr × List Expr) := match e.app2? ``List.toArray with | some (_, e) => e.listLit? | none => none /-- Recognize `α × β` -/ def prod? (e : Expr) : Option (Expr × Expr) := e.app2? ``Prod private def getConstructorVal? (env : Environment) (ctorName : Name) : Option ConstructorVal := do match env.find? ctorName with | some (ConstantInfo.ctorInfo v) => v | _ => none def isConstructorApp? (env : Environment) (e : Expr) : Option ConstructorVal := match e with | Expr.lit (Literal.natVal n) _ => if n == 0 then getConstructorVal? env `Nat.zero else getConstructorVal? env `Nat.succ | _ => match e.getAppFn with | Expr.const n _ _ => match getConstructorVal? env n with | some v => if v.numParams + v.numFields == e.getAppNumArgs then some v else none | none => none | _ => none def isConstructorApp (env : Environment) (e : Expr) : Bool := e.isConstructorApp? env |>.isSome def constructorApp? (env : Environment) (e : Expr) : Option (ConstructorVal × Array Expr) := match e with | Expr.lit (Literal.natVal n) _ => if n == 0 then do let v ← getConstructorVal? env `Nat.zero pure (v, #[]) else do let v ← getConstructorVal? env `Nat.succ pure (v, #[mkNatLit (n-1)]) | _ => match e.getAppFn with | Expr.const n _ _ => do let v ← getConstructorVal? env n if v.numParams + v.numFields == e.getAppNumArgs then pure (v, e.getAppArgs) else none | _ => none end Expr end Lean
4ea00e9bf01d182e13a2dfedbdff8bc29a3dff4b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/complex/roots_of_unity.lean
05f70eb5430d5efbda3673b9df70f94e16bc99a9
[]
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,132
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.roots_of_unity import Mathlib.analysis.special_functions.trigonometric import Mathlib.analysis.special_functions.pow import Mathlib.PostPort namespace Mathlib /-! # Complex roots of unity In this file we show that the `n`-th complex roots of unity are exactly the complex numbers `e ^ (2 * real.pi * complex.I * (i / n))` for `i ∈ finset.range n`. ## Main declarations * `complex.mem_roots_of_unity`: the complex `n`-th roots of unity are exactly the complex numbers of the form `e ^ (2 * real.pi * complex.I * (i / n))` for some `i < n`. * `complex.card_roots_of_unity`: the number of `n`-th roots of unity is exactly `n`. -/ namespace complex theorem is_primitive_root_exp_of_coprime (i : ℕ) (n : ℕ) (h0 : n ≠ 0) (hi : nat.coprime i n) : is_primitive_root (exp (bit0 1 * ↑real.pi * I * (↑i / ↑n))) n := sorry theorem is_primitive_root_exp (n : ℕ) (h0 : n ≠ 0) : is_primitive_root (exp (bit0 1 * ↑real.pi * I / ↑n)) n := sorry theorem is_primitive_root_iff (ζ : ℂ) (n : ℕ) (hn : n ≠ 0) : is_primitive_root ζ n ↔ ∃ (i : ℕ), ∃ (H : i < n), ∃ (hi : nat.coprime i n), exp (bit0 1 * ↑real.pi * I * (↑i / ↑n)) = ζ := sorry /-- The complex `n`-th roots of unity are exactly the complex numbers of the form `e ^ (2 * real.pi * complex.I * (i / n))` for some `i < n`. -/ theorem mem_roots_of_unity (n : ℕ+) (x : units ℂ) : x ∈ roots_of_unity n ℂ ↔ ∃ (i : ℕ), ∃ (H : i < ↑n), exp (bit0 1 * ↑real.pi * I * (↑i / ↑n)) = ↑x := sorry theorem card_roots_of_unity (n : ℕ+) : fintype.card ↥(roots_of_unity n ℂ) = ↑n := is_primitive_root.card_roots_of_unity (is_primitive_root_exp (↑n) (pnat.ne_zero n)) theorem card_primitive_roots (k : ℕ) (h : k ≠ 0) : finset.card (primitive_roots k ℂ) = nat.totient k := is_primitive_root.card_primitive_roots (is_primitive_root_exp k h) (nat.pos_of_ne_zero h)
4bd20afe10a23ead582ad3dc725f5419d4ef1957
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/logic/function/basic_auto.lean
ecc863dfa07cdb82f5fccc5378b248f0b44f731a
[]
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
27,366
lean
/- Copyright (c) 2016 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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.logic.basic import Mathlib.data.option.defs import Mathlib.PostPort universes u_1 u_2 u v w u_3 u_4 u_5 u_6 u_7 l namespace Mathlib /-! # Miscellaneous function constructions and lemmas -/ namespace function /-- Evaluate a function at an argument. Useful if you want to talk about the partially applied `function.eval x : (Π x, β x) → β x`. -/ def eval {α : Sort u_1} {β : α → Sort u_2} (x : α) (f : (x : α) → β x) : β x := f x @[simp] theorem eval_apply {α : Sort u_1} {β : α → Sort u_2} (x : α) (f : (x : α) → β x) : eval x f = f x := rfl theorem comp_apply {α : Sort u} {β : Sort v} {φ : Sort w} (f : β → φ) (g : α → β) (a : α) : comp f g a = f (g a) := rfl theorem const_def {α : Sort u_1} {β : Sort u_2} {y : β} : (fun (x : α) => y) = const α y := rfl @[simp] theorem const_apply {α : Sort u_1} {β : Sort u_2} {y : β} {x : α} : const α y x = y := rfl @[simp] theorem const_comp {α : Sort u_1} {β : Sort u_2} {γ : Sort u_3} {f : α → β} {c : γ} : const β c ∘ f = const α c := rfl @[simp] theorem comp_const {α : Sort u_1} {β : Sort u_2} {γ : Sort u_3} {f : β → γ} {b : β} : f ∘ const α b = const α (f b) := rfl theorem id_def {α : Sort u_1} : id = fun (x : α) => x := rfl theorem hfunext {α : Sort u} {α' : Sort u} {β : α → Sort v} {β' : α' → Sort v} {f : (a : α) → β a} {f' : (a : α') → β' a} (hα : α = α') (h : ∀ (a : α) (a' : α'), a == a' → f a == f' a') : f == f' := sorry theorem funext_iff {α : Sort u_1} {β : α → Sort u_2} {f₁ : (x : α) → β x} {f₂ : (x : α) → β x} : f₁ = f₂ ↔ ∀ (a : α), f₁ a = f₂ a := { mp := fun (h : f₁ = f₂) (a : α) => h ▸ rfl, mpr := funext } @[simp] theorem injective.eq_iff {α : Sort u_1} {β : Sort u_2} {f : α → β} (I : injective f) {a : α} {b : α} : f a = f b ↔ a = b := { mp := I, mpr := congr_arg f } theorem injective.eq_iff' {α : Sort u_1} {β : Sort u_2} {f : α → β} (I : injective f) {a : α} {b : α} {c : β} (h : f b = c) : f a = c ↔ a = b := h ▸ injective.eq_iff I theorem injective.ne {α : Sort u_1} {β : Sort u_2} {f : α → β} (hf : injective f) {a₁ : α} {a₂ : α} : a₁ ≠ a₂ → f a₁ ≠ f a₂ := mt fun (h : f a₁ = f a₂) => hf h theorem injective.ne_iff {α : Sort u_1} {β : Sort u_2} {f : α → β} (hf : injective f) {x : α} {y : α} : f x ≠ f y ↔ x ≠ y := { mp := mt (congr_arg f), mpr := injective.ne hf } theorem injective.ne_iff' {α : Sort u_1} {β : Sort u_2} {f : α → β} (hf : injective f) {x : α} {y : α} {z : β} (h : f y = z) : f x ≠ z ↔ x ≠ y := h ▸ injective.ne_iff hf /-- If the co-domain `β` of an injective function `f : α → β` has decidable equality, then the domain `α` also has decidable equality. -/ def injective.decidable_eq {α : Sort u_1} {β : Sort u_2} {f : α → β} [DecidableEq β] (I : injective f) : DecidableEq α := fun (a b : α) => decidable_of_iff (f a = f b) (injective.eq_iff I) theorem injective.of_comp {α : Sort u_1} {β : Sort u_2} {γ : Sort u_3} {f : α → β} {g : γ → α} (I : injective (f ∘ g)) : injective g := fun (x y : γ) (h : g x = g y) => I ((fun (this : f (g x) = f (g y)) => this) (congr_arg f h)) theorem surjective.of_comp {α : Sort u_1} {β : Sort u_2} {γ : Sort u_3} {f : α → β} {g : γ → α} (S : surjective (f ∘ g)) : surjective f := sorry protected instance decidable_eq_pfun (p : Prop) [Decidable p] (α : p → Type u_1) [(hp : p) → DecidableEq (α hp)] : DecidableEq ((hp : p) → α hp) := sorry theorem surjective.forall {α : Sort u_1} {β : Sort u_2} {f : α → β} (hf : surjective f) {p : β → Prop} : (∀ (y : β), p y) ↔ ∀ (x : α), p (f x) := sorry theorem surjective.forall₂ {α : Sort u_1} {β : Sort u_2} {f : α → β} (hf : surjective f) {p : β → β → Prop} : (∀ (y₁ y₂ : β), p y₁ y₂) ↔ ∀ (x₁ x₂ : α), p (f x₁) (f x₂) := iff.trans (surjective.forall hf) (forall_congr fun (x : α) => surjective.forall hf) theorem surjective.forall₃ {α : Sort u_1} {β : Sort u_2} {f : α → β} (hf : surjective f) {p : β → β → β → Prop} : (∀ (y₁ y₂ y₃ : β), p y₁ y₂ y₃) ↔ ∀ (x₁ x₂ x₃ : α), p (f x₁) (f x₂) (f x₃) := iff.trans (surjective.forall hf) (forall_congr fun (x : α) => surjective.forall₂ hf) theorem surjective.exists {α : Sort u_1} {β : Sort u_2} {f : α → β} (hf : surjective f) {p : β → Prop} : (∃ (y : β), p y) ↔ ∃ (x : α), p (f x) := sorry theorem surjective.exists₂ {α : Sort u_1} {β : Sort u_2} {f : α → β} (hf : surjective f) {p : β → β → Prop} : (∃ (y₁ : β), ∃ (y₂ : β), p y₁ y₂) ↔ ∃ (x₁ : α), ∃ (x₂ : α), p (f x₁) (f x₂) := iff.trans (surjective.exists hf) (exists_congr fun (x : α) => surjective.exists hf) theorem surjective.exists₃ {α : Sort u_1} {β : Sort u_2} {f : α → β} (hf : surjective f) {p : β → β → β → Prop} : (∃ (y₁ : β), ∃ (y₂ : β), ∃ (y₃ : β), p y₁ y₂ y₃) ↔ ∃ (x₁ : α), ∃ (x₂ : α), ∃ (x₃ : α), p (f x₁) (f x₂) (f x₃) := iff.trans (surjective.exists hf) (exists_congr fun (x : α) => surjective.exists₂ hf) /-- Cantor's diagonal argument implies that there are no surjective functions from `α` to `set α`. -/ theorem cantor_surjective {α : Type u_1} (f : α → set α) : ¬surjective f := sorry /-- Cantor's diagonal argument implies that there are no injective functions from `set α` to `α`. -/ theorem cantor_injective {α : Type u_1} (f : set α → α) : ¬injective f := sorry /-- `g` is a partial inverse to `f` (an injective but not necessarily surjective function) if `g y = some x` implies `f x = y`, and `g y = none` implies that `y` is not in the range of `f`. -/ def is_partial_inv {α : Type u_1} {β : Sort u_2} (f : α → β) (g : β → Option α) := ∀ (x : α) (y : β), g y = some x ↔ f x = y theorem is_partial_inv_left {α : Type u_1} {β : Sort u_2} {f : α → β} {g : β → Option α} (H : is_partial_inv f g) (x : α) : g (f x) = some x := iff.mpr (H x (f x)) rfl theorem injective_of_partial_inv {α : Type u_1} {β : Sort u_2} {f : α → β} {g : β → Option α} (H : is_partial_inv f g) : injective f := fun (a b : α) (h : f a = f b) => option.some.inj (Eq.trans (Eq.symm (iff.mpr (H a (f b)) h)) (iff.mpr (H b (f b)) rfl)) theorem injective_of_partial_inv_right {α : Type u_1} {β : Sort u_2} {f : α → β} {g : β → Option α} (H : is_partial_inv f g) (x : β) (y : β) (b : α) (h₁ : b ∈ g x) (h₂ : b ∈ g y) : x = y := Eq.trans (Eq.symm (iff.mp (H b x) h₁)) (iff.mp (H b y) h₂) theorem left_inverse.comp_eq_id {α : Sort u_1} {β : Sort u_2} {f : α → β} {g : β → α} (h : left_inverse f g) : f ∘ g = id := funext h theorem left_inverse_iff_comp {α : Sort u_1} {β : Sort u_2} {f : α → β} {g : β → α} : left_inverse f g ↔ f ∘ g = id := { mp := left_inverse.comp_eq_id, mpr := congr_fun } theorem right_inverse.comp_eq_id {α : Sort u_1} {β : Sort u_2} {f : α → β} {g : β → α} (h : right_inverse f g) : g ∘ f = id := funext h theorem right_inverse_iff_comp {α : Sort u_1} {β : Sort u_2} {f : α → β} {g : β → α} : right_inverse f g ↔ g ∘ f = id := { mp := right_inverse.comp_eq_id, mpr := congr_fun } theorem left_inverse.comp {α : Sort u_1} {β : Sort u_2} {γ : Sort u_3} {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : left_inverse f g) (hh : left_inverse h i) : left_inverse (h ∘ f) (g ∘ i) := sorry theorem right_inverse.comp {α : Sort u_1} {β : Sort u_2} {γ : Sort u_3} {f : α → β} {g : β → α} {h : β → γ} {i : γ → β} (hf : right_inverse f g) (hh : right_inverse h i) : right_inverse (h ∘ f) (g ∘ i) := left_inverse.comp hh hf theorem left_inverse.right_inverse {α : Sort u_1} {β : Sort u_2} {f : α → β} {g : β → α} (h : left_inverse g f) : right_inverse f g := h theorem right_inverse.left_inverse {α : Sort u_1} {β : Sort u_2} {f : α → β} {g : β → α} (h : right_inverse g f) : left_inverse f g := h theorem left_inverse.surjective {α : Sort u_1} {β : Sort u_2} {f : α → β} {g : β → α} (h : left_inverse f g) : surjective f := right_inverse.surjective (left_inverse.right_inverse h) theorem right_inverse.injective {α : Sort u_1} {β : Sort u_2} {f : α → β} {g : β → α} (h : right_inverse f g) : injective f := left_inverse.injective (right_inverse.left_inverse h) theorem left_inverse.eq_right_inverse {α : Sort u_1} {β : Sort u_2} {f : α → β} {g₁ : β → α} {g₂ : β → α} (h₁ : left_inverse g₁ f) (h₂ : right_inverse g₂ f) : g₁ = g₂ := sorry /-- We can use choice to construct explicitly a partial inverse for a given injective function `f`. -/ def partial_inv {α : Type u_1} {β : Sort u_2} (f : α → β) (b : β) : Option α := dite (∃ (a : α), f a = b) (fun (h : ∃ (a : α), f a = b) => some (classical.some h)) fun (h : ¬∃ (a : α), f a = b) => none theorem partial_inv_of_injective {α : Type u_1} {β : Sort u_2} {f : α → β} (I : injective f) : is_partial_inv f (partial_inv f) := sorry theorem partial_inv_left {α : Type u_1} {β : Sort u_2} {f : α → β} (I : injective f) (x : α) : partial_inv f (f x) = some x := is_partial_inv_left (partial_inv_of_injective I) /-- Construct the inverse for a function `f` on domain `s`. This function is a right inverse of `f` on `f '' s`. -/ def inv_fun_on {α : Type u} [n : Nonempty α] {β : Sort v} (f : α → β) (s : set α) (b : β) : α := dite (∃ (a : α), a ∈ s ∧ f a = b) (fun (h : ∃ (a : α), a ∈ s ∧ f a = b) => classical.some h) fun (h : ¬∃ (a : α), a ∈ s ∧ f a = b) => Classical.choice n theorem inv_fun_on_pos {α : Type u} [n : Nonempty α] {β : Sort v} {f : α → β} {s : set α} {b : β} (h : ∃ (a : α), ∃ (H : a ∈ s), f a = b) : inv_fun_on f s b ∈ s ∧ f (inv_fun_on f s b) = b := sorry theorem inv_fun_on_mem {α : Type u} [n : Nonempty α] {β : Sort v} {f : α → β} {s : set α} {b : β} (h : ∃ (a : α), ∃ (H : a ∈ s), f a = b) : inv_fun_on f s b ∈ s := and.left (inv_fun_on_pos h) theorem inv_fun_on_eq {α : Type u} [n : Nonempty α] {β : Sort v} {f : α → β} {s : set α} {b : β} (h : ∃ (a : α), ∃ (H : a ∈ s), f a = b) : f (inv_fun_on f s b) = b := and.right (inv_fun_on_pos h) theorem inv_fun_on_eq' {α : Type u} [n : Nonempty α] {β : Sort v} {f : α → β} {s : set α} {a : α} (h : ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → f x = f y → x = y) (ha : a ∈ s) : inv_fun_on f s (f a) = a := (fun (this : ∃ (a' : α), ∃ (H : a' ∈ s), f a' = f a) => h (inv_fun_on (fun (a' : α) => f a') s (f a)) (inv_fun_on_mem this) a ha (inv_fun_on_eq this)) (Exists.intro a (Exists.intro ha rfl)) theorem inv_fun_on_neg {α : Type u} [n : Nonempty α] {β : Sort v} {f : α → β} {s : set α} {b : β} (h : ¬∃ (a : α), ∃ (H : a ∈ s), f a = b) : inv_fun_on f s b = Classical.choice n := sorry /-- The inverse of a function (which is a left inverse if `f` is injective and a right inverse if `f` is surjective). -/ def inv_fun {α : Type u} [n : Nonempty α] {β : Sort v} (f : α → β) : β → α := inv_fun_on f set.univ theorem inv_fun_eq {α : Type u} [n : Nonempty α] {β : Sort v} {f : α → β} {b : β} (h : ∃ (a : α), f a = b) : f (inv_fun f b) = b := sorry theorem inv_fun_neg {α : Type u} [n : Nonempty α] {β : Sort v} {f : α → β} {b : β} (h : ¬∃ (a : α), f a = b) : inv_fun f b = Classical.choice n := sorry theorem inv_fun_eq_of_injective_of_right_inverse {α : Type u} [n : Nonempty α] {β : Sort v} {f : α → β} {g : β → α} (hf : injective f) (hg : right_inverse g f) : inv_fun f = g := funext fun (b : β) => hf (eq.mpr (id (Eq._oldrec (Eq.refl (f (inv_fun f b) = f (g b))) (hg b))) (inv_fun_eq (Exists.intro (g b) (hg b)))) theorem right_inverse_inv_fun {α : Type u} [n : Nonempty α] {β : Sort v} {f : α → β} (hf : surjective f) : right_inverse (inv_fun f) f := fun (b : β) => inv_fun_eq (hf b) theorem left_inverse_inv_fun {α : Type u} [n : Nonempty α] {β : Sort v} {f : α → β} (hf : injective f) : left_inverse (inv_fun f) f := fun (b : α) => (fun (this : f (inv_fun f (f b)) = f b) => hf this) (inv_fun_eq (Exists.intro b rfl)) theorem inv_fun_surjective {α : Type u} [n : Nonempty α] {β : Sort v} {f : α → β} (hf : injective f) : surjective (inv_fun f) := left_inverse.surjective (left_inverse_inv_fun hf) theorem inv_fun_comp {α : Type u} [n : Nonempty α] {β : Sort v} {f : α → β} (hf : injective f) : inv_fun f ∘ f = id := funext (left_inverse_inv_fun hf) theorem injective.has_left_inverse {α : Type u} [i : Nonempty α] {β : Sort v} {f : α → β} (hf : injective f) : has_left_inverse f := Exists.intro (inv_fun f) (left_inverse_inv_fun hf) theorem injective_iff_has_left_inverse {α : Type u} [i : Nonempty α] {β : Sort v} {f : α → β} : injective f ↔ has_left_inverse f := { mp := injective.has_left_inverse, mpr := has_left_inverse.injective } /-- The inverse of a surjective function. (Unlike `inv_fun`, this does not require `α` to be inhabited.) -/ def surj_inv {α : Sort u} {β : Sort v} {f : α → β} (h : surjective f) (b : β) : α := classical.some (h b) theorem surj_inv_eq {α : Sort u} {β : Sort v} {f : α → β} (h : surjective f) (b : β) : f (surj_inv h b) = b := classical.some_spec (h b) theorem right_inverse_surj_inv {α : Sort u} {β : Sort v} {f : α → β} (hf : surjective f) : right_inverse (surj_inv hf) f := surj_inv_eq hf theorem left_inverse_surj_inv {α : Sort u} {β : Sort v} {f : α → β} (hf : bijective f) : left_inverse (surj_inv (and.right hf)) f := right_inverse_of_injective_of_left_inverse (and.left hf) (right_inverse_surj_inv (and.right hf)) theorem surjective.has_right_inverse {α : Sort u} {β : Sort v} {f : α → β} (hf : surjective f) : has_right_inverse f := Exists.intro (surj_inv hf) (right_inverse_surj_inv hf) theorem surjective_iff_has_right_inverse {α : Sort u} {β : Sort v} {f : α → β} : surjective f ↔ has_right_inverse f := { mp := surjective.has_right_inverse, mpr := has_right_inverse.surjective } theorem bijective_iff_has_inverse {α : Sort u} {β : Sort v} {f : α → β} : bijective f ↔ ∃ (g : β → α), left_inverse g f ∧ right_inverse g f := sorry theorem injective_surj_inv {α : Sort u} {β : Sort v} {f : α → β} (h : surjective f) : injective (surj_inv h) := right_inverse.injective (right_inverse_surj_inv h) /-- Replacing the value of a function at a given point by a given value. -/ def update {α : Sort u} {β : α → Sort v} [DecidableEq α] (f : (a : α) → β a) (a' : α) (v : β a') (a : α) : β a := dite (a = a') (fun (h : a = a') => Eq._oldrec v (Eq.symm h)) fun (h : ¬a = a') => f a /-- On non-dependent functions, `function.update` can be expressed as an `ite` -/ theorem update_apply {α : Sort u} [DecidableEq α] {β : Sort u_1} (f : α → β) (a' : α) (b : β) (a : α) : update f a' b a = ite (a = a') b (f a) := sorry @[simp] theorem update_same {α : Sort u} {β : α → Sort v} [DecidableEq α] (a : α) (v : β a) (f : (a : α) → β a) : update f a v a = v := dif_pos rfl theorem update_injective {α : Sort u} {β : α → Sort v} [DecidableEq α] (f : (a : α) → β a) (a' : α) : injective (update f a') := sorry @[simp] theorem update_noteq {α : Sort u} {β : α → Sort v} [DecidableEq α] {a : α} {a' : α} (h : a ≠ a') (v : β a') (f : (a : α) → β a) : update f a' v a = f a := dif_neg h theorem forall_update_iff {α : Sort u} {β : α → Sort v} [DecidableEq α] (f : (a : α) → β a) {a : α} {b : β a} (p : (a : α) → β a → Prop) : (∀ (x : α), p x (update f a b x)) ↔ p a b ∧ ∀ (x : α), x ≠ a → p x (f x) := sorry theorem update_eq_iff {α : Sort u} {β : α → Sort v} [DecidableEq α] {a : α} {b : β a} {f : (a : α) → β a} {g : (a : α) → β a} : update f a b = g ↔ b = g a ∧ ∀ (x : α), x ≠ a → f x = g x := iff.trans funext_iff (forall_update_iff f fun (x : α) (y : β x) => y = g x) theorem eq_update_iff {α : Sort u} {β : α → Sort v} [DecidableEq α] {a : α} {b : β a} {f : (a : α) → β a} {g : (a : α) → β a} : g = update f a b ↔ g a = b ∧ ∀ (x : α), x ≠ a → g x = f x := iff.trans funext_iff (forall_update_iff f fun (x : α) (y : β x) => g x = y) @[simp] theorem update_eq_self {α : Sort u} {β : α → Sort v} [DecidableEq α] (a : α) (f : (a : α) → β a) : update f a (f a) = f := iff.mpr update_eq_iff { left := rfl, right := fun (_x : α) (_x_1 : _x ≠ a) => rfl } theorem update_comp_eq_of_forall_ne' {α : Sort u} {β : α → Sort v} [DecidableEq α] {α' : Sort u_1} (g : (a : α) → β a) {f : α' → α} {i : α} (a : β i) (h : ∀ (x : α'), f x ≠ i) : (fun (j : α') => update g i a (f j)) = fun (j : α') => g (f j) := funext fun (x : α') => update_noteq (h x) a g /-- Non-dependent version of `function.update_comp_eq_of_forall_ne'` -/ theorem update_comp_eq_of_forall_ne {α' : Sort w} [DecidableEq α'] {α : Sort u_1} {β : Sort u_2} (g : α' → β) {f : α → α'} {i : α'} (a : β) (h : ∀ (x : α), f x ≠ i) : update g i a ∘ f = g ∘ f := update_comp_eq_of_forall_ne' g a h theorem update_comp_eq_of_injective' {α : Sort u} {β : α → Sort v} {α' : Sort w} [DecidableEq α] [DecidableEq α'] (g : (a : α) → β a) {f : α' → α} (hf : injective f) (i : α') (a : β (f i)) : (fun (j : α') => update g (f i) a (f j)) = update (fun (i : α') => g (f i)) i a := iff.mpr eq_update_iff { left := update_same (f i) a g, right := fun (j : α') (hj : j ≠ i) => update_noteq (injective.ne hf hj) a g } /-- Non-dependent version of `function.update_comp_eq_of_injective'` -/ theorem update_comp_eq_of_injective {α : Sort u} {α' : Sort w} [DecidableEq α] [DecidableEq α'] {β : Sort u_1} (g : α' → β) {f : α → α'} (hf : injective f) (i : α) (a : β) : update g (f i) a ∘ f = update (g ∘ f) i a := update_comp_eq_of_injective' g hf i a theorem apply_update {ι : Sort u_1} [DecidableEq ι] {α : ι → Sort u_2} {β : ι → Sort u_3} (f : (i : ι) → α i → β i) (g : (i : ι) → α i) (i : ι) (v : α i) (j : ι) : f j (update g i v j) = update (fun (k : ι) => f k (g k)) i (f i v) j := sorry theorem comp_update {α : Sort u} [DecidableEq α] {α' : Sort u_1} {β : Sort u_2} (f : α' → β) (g : α → α') (i : α) (v : α') : f ∘ update g i v = update (f ∘ g) i (f v) := funext (apply_update (fun (x : α) => f) g i v) theorem update_comm {α : Sort u_1} [DecidableEq α] {β : α → Sort u_2} {a : α} {b : α} (h : a ≠ b) (v : β a) (w : β b) (f : (a : α) → β a) : update (update f a v) b w = update (update f b w) a v := sorry @[simp] theorem update_idem {α : Sort u_1} [DecidableEq α] {β : α → Sort u_2} {a : α} (v : β a) (w : β a) (f : (a : α) → β a) : update (update f a v) a w = update f a w := sorry /-- `extend f g e'` extends a function `g : α → γ` along a function `f : α → β` to a function `β → γ`, by using the values of `g` on the range of `f` and the values of an auxiliary function `e' : β → γ` elsewhere. Mostly useful when `f` is injective. -/ def extend {α : Type u_1} {β : Type u_2} {γ : Type u_3} (f : α → β) (g : α → γ) (e' : β → γ) : β → γ := fun (b : β) => dite (∃ (a : α), f a = b) (fun (h : ∃ (a : α), f a = b) => g (classical.some h)) fun (h : ¬∃ (a : α), f a = b) => e' b theorem extend_def {α : Type u_1} {β : Type u_2} {γ : Type u_3} (f : α → β) (g : α → γ) (e' : β → γ) (b : β) : extend f g e' b = dite (∃ (a : α), f a = b) (fun (h : ∃ (a : α), f a = b) => g (classical.some h)) fun (h : ¬∃ (a : α), f a = b) => e' b := rfl @[simp] theorem extend_apply {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β} (hf : injective f) (g : α → γ) (e' : β → γ) (a : α) : extend f g e' (f a) = g a := sorry @[simp] theorem extend_comp {α : Type u_1} {β : Type u_2} {γ : Type u_3} {f : α → β} (hf : injective f) (g : α → γ) (e' : β → γ) : extend f g e' ∘ f = g := funext fun (a : α) => extend_apply hf g e' a theorem uncurry_def {α : Type u_1} {β : Type u_2} {γ : Type u_3} (f : α → β → γ) : uncurry f = fun (p : α × β) => f (prod.fst p) (prod.snd p) := rfl @[simp] theorem uncurry_apply_pair {α : Type u_1} {β : Type u_2} {γ : Type u_3} (f : α → β → γ) (x : α) (y : β) : uncurry f (x, y) = f x y := rfl @[simp] theorem curry_apply {α : Type u_1} {β : Type u_2} {γ : Type u_3} (f : α × β → γ) (x : α) (y : β) : curry f x y = f (x, y) := rfl /-- Compose a binary function `f` with a pair of unary functions `g` and `h`. If both arguments of `f` have the same type and `g = h`, then `bicompl f g g = f on g`. -/ def bicompl {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {ε : Type u_5} (f : γ → δ → ε) (g : α → γ) (h : β → δ) (a : α) (b : β) : ε := f (g a) (h b) /-- Compose an unary function `f` with a binary function `g`. -/ def bicompr {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} (f : γ → δ) (g : α → β → γ) (a : α) (b : β) : δ := f (g a b) -- Suggested local notation: theorem uncurry_bicompr {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} (f : α → β → γ) (g : γ → δ) : uncurry (bicompr g f) = g ∘ uncurry f := rfl theorem uncurry_bicompl {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} {ε : Type u_5} (f : γ → δ → ε) (g : α → γ) (h : β → δ) : uncurry (bicompl f g h) = uncurry f ∘ prod.map g h := rfl /-- Records a way to turn an element of `α` into a function from `β` to `γ`. The most generic use is to recursively uncurry. For instance `f : α → β → γ → δ` will be turned into `↿f : α × β × γ → δ`. One can also add instances for bundled maps. -/ class has_uncurry (α : Type u_5) (β : outParam (Type u_6)) (γ : outParam (Type u_7)) where uncurry : α → β → γ prefix:1024 "↿" => Mathlib.function.has_uncurry.uncurry /-- Uncurrying operator. The most generic use is to recursively uncurry. For instance `f : α → β → γ → δ` will be turned into `↿f : α × β × γ → δ`. One can also add instances for bundled maps.-/ protected instance has_uncurry_base {α : Type u_1} {β : Type u_2} : has_uncurry (α → β) α β := has_uncurry.mk id protected instance has_uncurry_induction {α : Type u_1} {β : Type u_2} {γ : Type u_3} {δ : Type u_4} [has_uncurry β γ δ] : has_uncurry (α → β) (α × γ) δ := has_uncurry.mk fun (f : α → β) (p : α × γ) => has_uncurry.uncurry (f (prod.fst p)) (prod.snd p) /-- A function is involutive, if `f ∘ f = id`. -/ def involutive {α : Sort u_1} (f : α → α) := ∀ (x : α), f (f x) = x theorem involutive_iff_iter_2_eq_id {α : Sort u_1} {f : α → α} : involutive f ↔ nat.iterate f (bit0 1) = id := iff.symm funext_iff namespace involutive @[simp] theorem comp_self {α : Sort u} {f : α → α} (h : involutive f) : f ∘ f = id := funext h protected theorem left_inverse {α : Sort u} {f : α → α} (h : involutive f) : left_inverse f f := h protected theorem right_inverse {α : Sort u} {f : α → α} (h : involutive f) : right_inverse f f := h protected theorem injective {α : Sort u} {f : α → α} (h : involutive f) : injective f := left_inverse.injective (involutive.left_inverse h) protected theorem surjective {α : Sort u} {f : α → α} (h : involutive f) : surjective f := fun (x : α) => Exists.intro (f x) (h x) protected theorem bijective {α : Sort u} {f : α → α} (h : involutive f) : bijective f := { left := involutive.injective h, right := involutive.surjective h } /-- Involuting an `ite` of an involuted value `x : α` negates the `Prop` condition in the `ite`. -/ protected theorem ite_not {α : Sort u} {f : α → α} (h : involutive f) (P : Prop) [Decidable P] (x : α) : f (ite P x (f x)) = ite (¬P) x (f x) := sorry end involutive /-- The property of a binary function `f : α → β → γ` being injective. Mathematically this should be thought of as the corresponding function `α × β → γ` being injective. -/ def injective2 {α : Sort u_1} {β : Sort u_2} {γ : Sort u_3} (f : α → β → γ) := ∀ {a₁ a₂ : α} {b₁ b₂ : β}, f a₁ b₁ = f a₂ b₂ → a₁ = a₂ ∧ b₁ = b₂ namespace injective2 protected theorem left {α : Type u_1} {β : Type u_2} {γ : Type u_3} (f : α → β → γ) (hf : injective2 f) {a₁ : α} {a₂ : α} {b₁ : β} {b₂ : β} (h : f a₁ b₁ = f a₂ b₂) : a₁ = a₂ := and.left (hf h) protected theorem right {α : Type u_1} {β : Type u_2} {γ : Type u_3} (f : α → β → γ) (hf : injective2 f) {a₁ : α} {a₂ : α} {b₁ : β} {b₂ : β} (h : f a₁ b₁ = f a₂ b₂) : b₁ = b₂ := and.right (hf h) theorem eq_iff {α : Type u_1} {β : Type u_2} {γ : Type u_3} (f : α → β → γ) (hf : injective2 f) {a₁ : α} {a₂ : α} {b₁ : β} {b₂ : β} : f a₁ b₁ = f a₂ b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ := sorry end injective2 /-- `sometimes f` evaluates to some value of `f`, if it exists. This function is especially interesting in the case where `α` is a proposition, in which case `f` is necessarily a constant function, so that `sometimes f = f a` for all `a`. -/ def sometimes {α : Sort u_1} {β : Sort u_2} [Nonempty β] (f : α → β) : β := dite (Nonempty α) (fun (h : Nonempty α) => f (Classical.choice h)) fun (h : ¬Nonempty α) => Classical.choice _inst_1 theorem sometimes_eq {p : Prop} {α : Sort u_1} [Nonempty α] (f : p → α) (a : p) : sometimes f = f a := dif_pos (Nonempty.intro a) theorem sometimes_spec {p : Prop} {α : Sort u_1} [Nonempty α] (P : α → Prop) (f : p → α) (a : p) (h : P (f a)) : P (sometimes f) := eq.mpr (id (Eq._oldrec (Eq.refl (P (sometimes f))) (sometimes_eq f a))) h end function /-- `s.piecewise f g` is the function equal to `f` on the set `s`, and to `g` on its complement. -/ def set.piecewise {α : Type u} {β : α → Sort v} (s : set α) (f : (i : α) → β i) (g : (i : α) → β i) [(j : α) → Decidable (j ∈ s)] (i : α) : β i := ite (i ∈ s) (f i) (g i) end Mathlib
d670ce734242b482af2371adb7b2008e254c0301
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/order/atoms.lean
a30462556ce98de6ba46c0680fdc99ed638072f8
[ "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
25,160
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import order.complete_boolean_algebra import order.cover import order.modular_lattice import data.fintype.basic /-! # Atoms, Coatoms, and Simple Lattices This module defines atoms, which are minimal non-`⊥` elements in bounded lattices, simple lattices, which are lattices with only two elements, and related ideas. ## Main definitions ### Atoms and Coatoms * `is_atom a` indicates that the only element below `a` is `⊥`. * `is_coatom a` indicates that the only element above `a` is `⊤`. ### Atomic and Atomistic Lattices * `is_atomic` indicates that every element other than `⊥` is above an atom. * `is_coatomic` indicates that every element other than `⊤` is below a coatom. * `is_atomistic` indicates that every element is the `Sup` of a set of atoms. * `is_coatomistic` indicates that every element is the `Inf` of a set of coatoms. ### Simple Lattices * `is_simple_order` indicates that an order has only two unique elements, `⊥` and `⊤`. * `is_simple_order.bounded_order` * `is_simple_order.distrib_lattice` * Given an instance of `is_simple_order`, we provide the following definitions. These are not made global instances as they contain data : * `is_simple_order.boolean_algebra` * `is_simple_order.complete_lattice` * `is_simple_order.complete_boolean_algebra` ## Main results * `is_atom_dual_iff_is_coatom` and `is_coatom_dual_iff_is_atom` express the (definitional) duality of `is_atom` and `is_coatom`. * `is_simple_order_iff_is_atom_top` and `is_simple_order_iff_is_coatom_bot` express the connection between atoms, coatoms, and simple lattices * `is_compl.is_atom_iff_is_coatom` and `is_compl.is_coatom_if_is_atom`: In a modular bounded lattice, a complement of an atom is a coatom and vice versa. * ``is_atomic_iff_is_coatomic`: A modular complemented lattice is atomic iff it is coatomic. -/ variable {α : Type*} section atoms section is_atom /-- An atom of an `order_bot` is an element with no other element between it and `⊥`, which is not `⊥`. -/ def is_atom [preorder α] [order_bot α] (a : α) : Prop := a ≠ ⊥ ∧ (∀ b, b < a → b = ⊥) variables [partial_order α] [order_bot α] {a b x : α} lemma eq_bot_or_eq_of_le_atom (ha : is_atom a) (hab : b ≤ a) : b = ⊥ ∨ b = a := hab.lt_or_eq.imp_left (ha.2 b) lemma is_atom.Iic (ha : is_atom a) (hax : a ≤ x) : is_atom (⟨a, hax⟩ : set.Iic x) := ⟨λ con, ha.1 (subtype.mk_eq_mk.1 con), λ ⟨b, hb⟩ hba, subtype.mk_eq_mk.2 (ha.2 b hba)⟩ lemma is_atom.of_is_atom_coe_Iic {a : set.Iic x} (ha : is_atom a) : is_atom (a : α) := ⟨λ con, ha.1 (subtype.ext con), λ b hba, subtype.mk_eq_mk.1 (ha.2 ⟨b, hba.le.trans a.prop⟩ hba)⟩ @[simp] lemma bot_covers_iff : ⊥ ⋖ a ↔ is_atom a := ⟨λ h, ⟨h.lt.ne', λ b hba, not_not.1 $ λ hb, h.2 (ne.bot_lt hb) hba⟩, λ h, ⟨h.1.bot_lt, λ b hb hba, hb.ne' $ h.2 _ hba⟩⟩ alias bot_covers_iff ↔ covers.is_atom is_atom.bot_covers end is_atom section is_coatom /-- A coatom of an `order_top` is an element with no other element between it and `⊤`, which is not `⊤`. -/ def is_coatom [preorder α] [order_top α] (a : α) : Prop := a ≠ ⊤ ∧ (∀ b, a < b → b = ⊤) variables [partial_order α] [order_top α] {a b x : α} lemma eq_top_or_eq_of_coatom_le (ha : is_coatom a) (hab : a ≤ b) : b = ⊤ ∨ b = a := hab.lt_or_eq.imp (ha.2 b) eq_comm.2 lemma is_coatom.Ici (ha : is_coatom a) (hax : x ≤ a) : is_coatom (⟨a, hax⟩ : set.Ici x) := ⟨λ con, ha.1 (subtype.mk_eq_mk.1 con), λ ⟨b, hb⟩ hba, subtype.mk_eq_mk.2 (ha.2 b hba)⟩ lemma is_coatom.of_is_coatom_coe_Ici {a : set.Ici x} (ha : is_coatom a) : is_coatom (a : α) := ⟨λ con, ha.1 (subtype.ext con), λ b hba, subtype.mk_eq_mk.1 (ha.2 ⟨b, le_trans a.prop hba.le⟩ hba)⟩ @[simp] lemma covers_top_iff : a ⋖ ⊤ ↔ is_coatom a := ⟨λ h, ⟨h.ne, λ b hab, not_not.1 $ λ hb, h.2 hab $ ne.lt_top hb⟩, λ h, ⟨h.1.lt_top, λ b hab hb, hb.ne $ h.2 _ hab⟩⟩ alias covers_top_iff ↔ covers.is_coatom is_coatom.covers_top end is_coatom section pairwise lemma is_atom.inf_eq_bot_of_ne [semilattice_inf α] [order_bot α] {a b : α} (ha : is_atom a) (hb : is_atom b) (hab : a ≠ b) : a ⊓ b = ⊥ := or.elim (eq_bot_or_eq_of_le_atom ha inf_le_left) id (λ h1, or.elim (eq_bot_or_eq_of_le_atom hb inf_le_right) id (λ h2, false.rec _ (hab (le_antisymm (inf_eq_left.mp h1) (inf_eq_right.mp h2))))) lemma is_atom.disjoint_of_ne [semilattice_inf α] [order_bot α] {a b : α} (ha : is_atom a) (hb : is_atom b) (hab : a ≠ b) : disjoint a b := disjoint_iff.mpr (is_atom.inf_eq_bot_of_ne ha hb hab) lemma is_coatom.sup_eq_top_of_ne [semilattice_sup α] [order_top α] {a b : α} (ha : is_coatom a) (hb : is_coatom b) (hab : a ≠ b) : a ⊔ b = ⊤ := or.elim (eq_top_or_eq_of_coatom_le ha le_sup_left) id (λ h1, or.elim (eq_top_or_eq_of_coatom_le hb le_sup_right) id (λ h2, false.rec _ (hab (le_antisymm (sup_eq_right.mp h2) (sup_eq_left.mp h1))))) end pairwise variables [partial_order α] {a : α} @[simp] lemma is_coatom_dual_iff_is_atom [order_bot α] : is_coatom (order_dual.to_dual a) ↔ is_atom a := iff.rfl @[simp] lemma is_atom_dual_iff_is_coatom [order_top α] : is_atom (order_dual.to_dual a) ↔ is_coatom a := iff.rfl end atoms section atomic variables [partial_order α] (α) /-- A lattice is atomic iff every element other than `⊥` has an atom below it. -/ class is_atomic [order_bot α] : Prop := (eq_bot_or_exists_atom_le : ∀ (b : α), b = ⊥ ∨ ∃ (a : α), is_atom a ∧ a ≤ b) /-- A lattice is coatomic iff every element other than `⊤` has a coatom above it. -/ class is_coatomic [order_top α] : Prop := (eq_top_or_exists_le_coatom : ∀ (b : α), b = ⊤ ∨ ∃ (a : α), is_coatom a ∧ b ≤ a) export is_atomic (eq_bot_or_exists_atom_le) is_coatomic (eq_top_or_exists_le_coatom) variable {α} @[simp] theorem is_coatomic_dual_iff_is_atomic [order_bot α] : is_coatomic (order_dual α) ↔ is_atomic α := ⟨λ h, ⟨λ b, by apply h.eq_top_or_exists_le_coatom⟩, λ h, ⟨λ b, by apply h.eq_bot_or_exists_atom_le⟩⟩ @[simp] theorem is_atomic_dual_iff_is_coatomic [order_top α] : is_atomic (order_dual α) ↔ is_coatomic α := ⟨λ h, ⟨λ b, by apply h.eq_bot_or_exists_atom_le⟩, λ h, ⟨λ b, by apply h.eq_top_or_exists_le_coatom⟩⟩ namespace is_atomic variables [order_bot α] [is_atomic α] instance is_coatomic_dual : is_coatomic (order_dual α) := is_coatomic_dual_iff_is_atomic.2 ‹is_atomic α› instance {x : α} : is_atomic (set.Iic x) := ⟨λ ⟨y, hy⟩, (eq_bot_or_exists_atom_le y).imp subtype.mk_eq_mk.2 (λ ⟨a, ha, hay⟩, ⟨⟨a, hay.trans hy⟩, ha.Iic (hay.trans hy), hay⟩)⟩ end is_atomic namespace is_coatomic variables [order_top α] [is_coatomic α] instance is_coatomic : is_atomic (order_dual α) := is_atomic_dual_iff_is_coatomic.2 ‹is_coatomic α› instance {x : α} : is_coatomic (set.Ici x) := ⟨λ ⟨y, hy⟩, (eq_top_or_exists_le_coatom y).imp subtype.mk_eq_mk.2 (λ ⟨a, ha, hay⟩, ⟨⟨a, le_trans hy hay⟩, ha.Ici (le_trans hy hay), hay⟩)⟩ end is_coatomic theorem is_atomic_iff_forall_is_atomic_Iic [order_bot α] : is_atomic α ↔ ∀ (x : α), is_atomic (set.Iic x) := ⟨@is_atomic.set.Iic.is_atomic _ _ _, λ h, ⟨λ x, ((@eq_bot_or_exists_atom_le _ _ _ (h x)) (⊤ : set.Iic x)).imp subtype.mk_eq_mk.1 (exists_imp_exists' coe (λ ⟨a, ha⟩, and.imp_left (is_atom.of_is_atom_coe_Iic)))⟩⟩ theorem is_coatomic_iff_forall_is_coatomic_Ici [order_top α] : is_coatomic α ↔ ∀ (x : α), is_coatomic (set.Ici x) := is_atomic_dual_iff_is_coatomic.symm.trans $ is_atomic_iff_forall_is_atomic_Iic.trans $ forall_congr (λ x, is_coatomic_dual_iff_is_atomic.symm.trans iff.rfl) end atomic section atomistic variables (α) [complete_lattice α] /-- A lattice is atomistic iff every element is a `Sup` of a set of atoms. -/ class is_atomistic : Prop := (eq_Sup_atoms : ∀ (b : α), ∃ (s : set α), b = Sup s ∧ ∀ a, a ∈ s → is_atom a) /-- A lattice is coatomistic iff every element is an `Inf` of a set of coatoms. -/ class is_coatomistic : Prop := (eq_Inf_coatoms : ∀ (b : α), ∃ (s : set α), b = Inf s ∧ ∀ a, a ∈ s → is_coatom a) export is_atomistic (eq_Sup_atoms) is_coatomistic (eq_Inf_coatoms) variable {α} @[simp] theorem is_coatomistic_dual_iff_is_atomistic : is_coatomistic (order_dual α) ↔ is_atomistic α := ⟨λ h, ⟨λ b, by apply h.eq_Inf_coatoms⟩, λ h, ⟨λ b, by apply h.eq_Sup_atoms⟩⟩ @[simp] theorem is_atomistic_dual_iff_is_coatomistic : is_atomistic (order_dual α) ↔ is_coatomistic α := ⟨λ h, ⟨λ b, by apply h.eq_Sup_atoms⟩, λ h, ⟨λ b, by apply h.eq_Inf_coatoms⟩⟩ namespace is_atomistic instance is_coatomistic_dual [h : is_atomistic α] : is_coatomistic (order_dual α) := is_coatomistic_dual_iff_is_atomistic.2 h variable [is_atomistic α] @[priority 100] instance : is_atomic α := ⟨λ b, by { rcases eq_Sup_atoms b with ⟨s, rfl, hs⟩, cases s.eq_empty_or_nonempty with h h, { simp [h] }, { exact or.intro_right _ ⟨h.some, hs _ h.some_spec, le_Sup h.some_spec⟩ } } ⟩ end is_atomistic section is_atomistic variables [is_atomistic α] @[simp] theorem Sup_atoms_le_eq (b : α) : Sup {a : α | is_atom a ∧ a ≤ b} = b := begin rcases eq_Sup_atoms b with ⟨s, rfl, hs⟩, exact le_antisymm (Sup_le (λ _, and.right)) (Sup_le_Sup (λ a ha, ⟨hs a ha, le_Sup ha⟩)), end @[simp] theorem Sup_atoms_eq_top : Sup {a : α | is_atom a} = ⊤ := begin refine eq.trans (congr rfl (set.ext (λ x, _))) (Sup_atoms_le_eq ⊤), exact (and_iff_left le_top).symm, end theorem le_iff_atom_le_imp {a b : α} : a ≤ b ↔ ∀ c : α, is_atom c → c ≤ a → c ≤ b := ⟨λ ab c hc ca, le_trans ca ab, λ h, begin rw [← Sup_atoms_le_eq a, ← Sup_atoms_le_eq b], exact Sup_le_Sup (λ c hc, ⟨hc.1, h c hc.1 hc.2⟩), end⟩ end is_atomistic namespace is_coatomistic instance is_atomistic_dual [h : is_coatomistic α] : is_atomistic (order_dual α) := is_atomistic_dual_iff_is_coatomistic.2 h variable [is_coatomistic α] @[priority 100] instance : is_coatomic α := ⟨λ b, by { rcases eq_Inf_coatoms b with ⟨s, rfl, hs⟩, cases s.eq_empty_or_nonempty with h h, { simp [h] }, { exact or.intro_right _ ⟨h.some, hs _ h.some_spec, Inf_le h.some_spec⟩ } } ⟩ end is_coatomistic end atomistic /-- An order is simple iff it has exactly two elements, `⊥` and `⊤`. -/ class is_simple_order (α : Type*) [has_le α] [bounded_order α] extends nontrivial α : Prop := (eq_bot_or_eq_top : ∀ (a : α), a = ⊥ ∨ a = ⊤) export is_simple_order (eq_bot_or_eq_top) theorem is_simple_order_iff_is_simple_order_order_dual [has_le α] [bounded_order α] : is_simple_order α ↔ is_simple_order (order_dual α) := begin split; intro i; haveI := i, { exact { exists_pair_ne := @exists_pair_ne α _, eq_bot_or_eq_top := λ a, or.symm (eq_bot_or_eq_top ((order_dual.of_dual a)) : _ ∨ _) } }, { exact { exists_pair_ne := @exists_pair_ne (order_dual α) _, eq_bot_or_eq_top := λ a, or.symm (eq_bot_or_eq_top (order_dual.to_dual a)) } } end lemma is_simple_order.bot_ne_top [has_le α] [bounded_order α] [is_simple_order α] : (⊥ : α) ≠ (⊤ : α) := begin obtain ⟨a, b, h⟩ := exists_pair_ne α, rcases eq_bot_or_eq_top a with rfl|rfl; rcases eq_bot_or_eq_top b with rfl|rfl; simpa <|> simpa using h.symm end section is_simple_order variables [partial_order α] [bounded_order α] [is_simple_order α] instance {α} [has_le α] [bounded_order α] [is_simple_order α] : is_simple_order (order_dual α) := is_simple_order_iff_is_simple_order_order_dual.1 (by apply_instance) /-- A simple `bounded_order` induces a preorder. This is not an instance to prevent loops. -/ protected def is_simple_order.preorder {α} [has_le α] [bounded_order α] [is_simple_order α] : preorder α := { le := (≤), le_refl := λ a, by rcases eq_bot_or_eq_top a with rfl|rfl; simp, le_trans := λ a b c, begin rcases eq_bot_or_eq_top a with rfl|rfl, { simp }, { rcases eq_bot_or_eq_top b with rfl|rfl, { rcases eq_bot_or_eq_top c with rfl|rfl; simp }, { simp } } end } /-- A simple partial ordered `bounded_order` induces a linear order. This is not an instance to prevent loops. -/ protected def is_simple_order.linear_order [decidable_eq α] : linear_order α := { le_total := λ a b, by rcases eq_bot_or_eq_top a with rfl|rfl; simp, decidable_le := λ a b, if ha : a = ⊥ then is_true (ha.le.trans bot_le) else if hb : b = ⊤ then is_true (le_top.trans hb.ge) else is_false (λ H, hb (top_unique (le_trans (top_le_iff.mpr (or.resolve_left (eq_bot_or_eq_top a) ha)) H))), decidable_eq := by assumption, ..(infer_instance : partial_order α) } @[simp] lemma is_atom_top : is_atom (⊤ : α) := ⟨top_ne_bot, λ a ha, or.resolve_right (eq_bot_or_eq_top a) (ne_of_lt ha)⟩ @[simp] lemma is_coatom_bot : is_coatom (⊥ : α) := is_atom_dual_iff_is_coatom.1 is_atom_top lemma bot_covers_top : (⊥ : α) ⋖ ⊤ := is_atom_top.bot_covers end is_simple_order namespace is_simple_order section preorder variables [preorder α] [bounded_order α] [is_simple_order α] {a b : α} (h : a < b) lemma eq_bot_of_lt : a = ⊥ := (is_simple_order.eq_bot_or_eq_top _).resolve_right h.ne_top lemma eq_top_of_lt : b = ⊤ := (is_simple_order.eq_bot_or_eq_top _).resolve_left h.ne_bot alias eq_bot_of_lt ← has_lt.lt.eq_bot alias eq_top_of_lt ← has_lt.lt.eq_top end preorder section bounded_order variables [lattice α] [bounded_order α] [is_simple_order α] /-- A simple partial ordered `bounded_order` induces a lattice. This is not an instance to prevent loops -/ protected def lattice {α} [decidable_eq α] [partial_order α] [bounded_order α] [is_simple_order α] : lattice α := @linear_order.to_lattice α (is_simple_order.linear_order) /-- A lattice that is a `bounded_order` is a distributive lattice. This is not an instance to prevent loops -/ protected def distrib_lattice : distrib_lattice α := { le_sup_inf := λ x y z, by { rcases eq_bot_or_eq_top x with rfl | rfl; simp }, .. (infer_instance : lattice α) } @[priority 100] -- see Note [lower instance priority] instance : is_atomic α := ⟨λ b, (eq_bot_or_eq_top b).imp_right (λ h, ⟨⊤, ⟨is_atom_top, ge_of_eq h⟩⟩)⟩ @[priority 100] -- see Note [lower instance priority] instance : is_coatomic α := is_atomic_dual_iff_is_coatomic.1 is_simple_order.is_atomic end bounded_order /- It is important that in this section `is_simple_order` is the last type-class argument. -/ section decidable_eq variables [decidable_eq α] [partial_order α] [bounded_order α] [is_simple_order α] /-- Every simple lattice is isomorphic to `bool`, regardless of order. -/ @[simps] def equiv_bool {α} [decidable_eq α] [has_le α] [bounded_order α] [is_simple_order α] : α ≃ bool := { to_fun := λ x, x = ⊤, inv_fun := λ x, cond x ⊤ ⊥, left_inv := λ x, by { rcases (eq_bot_or_eq_top x) with rfl | rfl; simp [bot_ne_top] }, right_inv := λ x, by { cases x; simp [bot_ne_top] } } /-- Every simple lattice over a partial order is order-isomorphic to `bool`. -/ def order_iso_bool : α ≃o bool := { map_rel_iff' := λ a b, begin rcases (eq_bot_or_eq_top a) with rfl | rfl, { simp [bot_ne_top] }, { rcases (eq_bot_or_eq_top b) with rfl | rfl, { simp [bot_ne_top.symm, bot_ne_top, bool.ff_lt_tt] }, { simp [bot_ne_top] } } end, ..equiv_bool } /- It is important that `is_simple_order` is the last type-class argument of this instance, so that type-class inference fails quickly if it doesn't apply. -/ @[priority 200] instance {α} [decidable_eq α] [has_le α] [bounded_order α] [is_simple_order α] : fintype α := fintype.of_equiv bool equiv_bool.symm /-- A simple `bounded_order` is also a `boolean_algebra`. -/ protected def boolean_algebra {α} [decidable_eq α] [lattice α] [bounded_order α] [is_simple_order α] : boolean_algebra α := { compl := λ x, if x = ⊥ then ⊤ else ⊥, sdiff := λ x y, if x = ⊤ ∧ y = ⊥ then ⊤ else ⊥, sdiff_eq := λ x y, by rcases eq_bot_or_eq_top x with rfl | rfl; simp [bot_ne_top, has_sdiff.sdiff, compl], inf_compl_le_bot := λ x, begin rcases eq_bot_or_eq_top x with rfl | rfl, { simp }, { simp only [top_inf_eq], split_ifs with h h; simp [h] } end, top_le_sup_compl := λ x, by rcases eq_bot_or_eq_top x with rfl | rfl; simp, sup_inf_sdiff := λ x y, by rcases eq_bot_or_eq_top x with rfl | rfl; rcases eq_bot_or_eq_top y with rfl | rfl; simp [bot_ne_top], inf_inf_sdiff := λ x y, begin rcases eq_bot_or_eq_top x with rfl | rfl, { simpa }, rcases eq_bot_or_eq_top y with rfl | rfl, { simpa }, { simp only [true_and, top_inf_eq, eq_self_iff_true], split_ifs with h h; simpa [h] } end, .. (show bounded_order α, by apply_instance), .. is_simple_order.distrib_lattice } end decidable_eq variables [lattice α] [bounded_order α] [is_simple_order α] open_locale classical /-- A simple `bounded_order` is also complete. -/ protected noncomputable def complete_lattice : complete_lattice α := { Sup := λ s, if ⊤ ∈ s then ⊤ else ⊥, Inf := λ s, if ⊥ ∈ s then ⊥ else ⊤, le_Sup := λ s x h, by { rcases eq_bot_or_eq_top x with rfl | rfl, { exact bot_le }, { rw if_pos h } }, Sup_le := λ s x h, by { rcases eq_bot_or_eq_top x with rfl | rfl, { rw if_neg, intro con, exact bot_ne_top (eq_top_iff.2 (h ⊤ con)) }, { exact le_top } }, Inf_le := λ s x h, by { rcases eq_bot_or_eq_top x with rfl | rfl, { rw if_pos h }, { exact le_top } }, le_Inf := λ s x h, by { rcases eq_bot_or_eq_top x with rfl | rfl, { exact bot_le }, { rw if_neg, intro con, exact top_ne_bot (eq_bot_iff.2 (h ⊥ con)) } }, .. (infer_instance : lattice α), .. (infer_instance : bounded_order α) } /-- A simple `bounded_order` is also a `complete_boolean_algebra`. -/ protected noncomputable def complete_boolean_algebra : complete_boolean_algebra α := { infi_sup_le_sup_Inf := λ x s, by { rcases eq_bot_or_eq_top x with rfl | rfl, { simp only [bot_sup_eq, ← Inf_eq_infi], exact le_rfl }, { simp only [top_sup_eq, le_top] }, }, inf_Sup_le_supr_inf := λ x s, by { rcases eq_bot_or_eq_top x with rfl | rfl, { simp only [bot_inf_eq, bot_le] }, { simp only [top_inf_eq, ← Sup_eq_supr], exact le_rfl } }, .. is_simple_order.complete_lattice, .. is_simple_order.boolean_algebra } end is_simple_order namespace is_simple_order variables [complete_lattice α] [is_simple_order α] set_option default_priority 100 instance : is_atomistic α := ⟨λ b, (eq_bot_or_eq_top b).elim (λ h, ⟨∅, ⟨h.trans Sup_empty.symm, λ a ha, false.elim (set.not_mem_empty _ ha)⟩⟩) (λ h, ⟨{⊤}, h.trans Sup_singleton.symm, λ a ha, (set.mem_singleton_iff.1 ha).symm ▸ is_atom_top⟩)⟩ instance : is_coatomistic α := is_atomistic_dual_iff_is_coatomistic.1 is_simple_order.is_atomistic end is_simple_order namespace fintype namespace is_simple_order variables [partial_order α] [bounded_order α] [is_simple_order α] [decidable_eq α] lemma univ : (finset.univ : finset α) = {⊤, ⊥} := begin change finset.map _ (finset.univ : finset bool) = _, rw fintype.univ_bool, simp only [finset.map_insert, function.embedding.coe_fn_mk, finset.map_singleton], refl, end lemma card : fintype.card α = 2 := (fintype.of_equiv_card _).trans fintype.card_bool end is_simple_order end fintype namespace bool instance : is_simple_order bool := ⟨λ a, begin rw [← finset.mem_singleton, or.comm, ← finset.mem_insert, top_eq_tt, bot_eq_ff, ← fintype.univ_bool], apply finset.mem_univ, end⟩ end bool theorem is_simple_order_iff_is_atom_top [partial_order α] [bounded_order α] : is_simple_order α ↔ is_atom (⊤ : α) := ⟨λ h, @is_atom_top _ _ _ h, λ h, { exists_pair_ne := ⟨⊤, ⊥, h.1⟩, eq_bot_or_eq_top := λ a, ((eq_or_lt_of_le le_top).imp_right (h.2 a)).symm }⟩ theorem is_simple_order_iff_is_coatom_bot [partial_order α] [bounded_order α] : is_simple_order α ↔ is_coatom (⊥ : α) := is_simple_order_iff_is_simple_order_order_dual.trans is_simple_order_iff_is_atom_top namespace set theorem is_simple_order_Iic_iff_is_atom [partial_order α] [bounded_order α] {a : α} : is_simple_order (Iic a) ↔ is_atom a := is_simple_order_iff_is_atom_top.trans $ and_congr (not_congr subtype.mk_eq_mk) ⟨λ h b ab, subtype.mk_eq_mk.1 (h ⟨b, le_of_lt ab⟩ ab), λ h ⟨b, hab⟩ hbotb, subtype.mk_eq_mk.2 (h b (subtype.mk_lt_mk.1 hbotb))⟩ theorem is_simple_order_Ici_iff_is_coatom [partial_order α] [bounded_order α] {a : α} : is_simple_order (Ici a) ↔ is_coatom a := is_simple_order_iff_is_coatom_bot.trans $ and_congr (not_congr subtype.mk_eq_mk) ⟨λ h b ab, subtype.mk_eq_mk.1 (h ⟨b, le_of_lt ab⟩ ab), λ h ⟨b, hab⟩ hbotb, subtype.mk_eq_mk.2 (h b (subtype.mk_lt_mk.1 hbotb))⟩ end set namespace order_iso variables {β : Type*} @[simp] lemma is_atom_iff [partial_order α] [order_bot α] [partial_order β] [order_bot β] (f : α ≃o β) (a : α) : is_atom (f a) ↔ is_atom a := and_congr (not_congr ⟨λ h, f.injective (f.map_bot.symm ▸ h), λ h, f.map_bot ▸ (congr rfl h)⟩) ⟨λ h b hb, f.injective ((h (f b) ((f : α ↪o β).lt_iff_lt.2 hb)).trans f.map_bot.symm), λ h b hb, f.symm.injective begin rw f.symm.map_bot, apply h, rw [← f.symm_apply_apply a], exact (f.symm : β ↪o α).lt_iff_lt.2 hb, end⟩ @[simp] lemma is_coatom_iff [partial_order α] [order_top α] [partial_order β] [order_top β] (f : α ≃o β) (a : α) : is_coatom (f a) ↔ is_coatom a := f.dual.is_atom_iff a lemma is_simple_order_iff [partial_order α] [bounded_order α] [partial_order β] [bounded_order β] (f : α ≃o β) : is_simple_order α ↔ is_simple_order β := by rw [is_simple_order_iff_is_atom_top, is_simple_order_iff_is_atom_top, ← f.is_atom_iff ⊤, f.map_top] lemma is_simple_order [partial_order α] [bounded_order α] [partial_order β] [bounded_order β] [h : is_simple_order β] (f : α ≃o β) : is_simple_order α := f.is_simple_order_iff.mpr h lemma is_atomic_iff [partial_order α] [order_bot α] [partial_order β] [order_bot β] (f : α ≃o β) : is_atomic α ↔ is_atomic β := begin suffices : (∀ b : α, b = ⊥ ∨ ∃ (a : α), is_atom a ∧ a ≤ b) ↔ (∀ b : β, b = ⊥ ∨ ∃ (a : β), is_atom a ∧ a ≤ b), from ⟨λ ⟨p⟩, ⟨this.mp p⟩, λ ⟨p⟩, ⟨this.mpr p⟩⟩, apply f.to_equiv.forall_congr, simp_rw [rel_iso.coe_fn_to_equiv], intro b, apply or_congr, { rw [f.apply_eq_iff_eq_symm_apply, map_bot], }, { split, { exact λ ⟨a, ha⟩, ⟨f a, ⟨(f.is_atom_iff a).mpr ha.1, f.le_iff_le.mpr ha.2⟩⟩, }, { rintros ⟨b, ⟨hb1, hb2⟩⟩, refine ⟨f.symm b, ⟨(f.symm.is_atom_iff b).mpr hb1, _⟩⟩, rwa [←f.le_iff_le, f.apply_symm_apply], }, }, end lemma is_coatomic_iff [partial_order α] [order_top α] [partial_order β] [order_top β] (f : α ≃o β) : is_coatomic α ↔ is_coatomic β := by { rw [←is_atomic_dual_iff_is_coatomic, ←is_atomic_dual_iff_is_coatomic], exact f.dual.is_atomic_iff } end order_iso section is_modular_lattice variables [lattice α] [bounded_order α] [is_modular_lattice α] namespace is_compl variables {a b : α} (hc : is_compl a b) include hc lemma is_atom_iff_is_coatom : is_atom a ↔ is_coatom b := set.is_simple_order_Iic_iff_is_atom.symm.trans $ hc.Iic_order_iso_Ici.is_simple_order_iff.trans set.is_simple_order_Ici_iff_is_coatom lemma is_coatom_iff_is_atom : is_coatom a ↔ is_atom b := hc.symm.is_atom_iff_is_coatom.symm end is_compl variables [is_complemented α] lemma is_coatomic_of_is_atomic_of_is_complemented_of_is_modular [is_atomic α] : is_coatomic α := ⟨λ x, begin rcases exists_is_compl x with ⟨y, xy⟩, apply (eq_bot_or_exists_atom_le y).imp _ _, { rintro rfl, exact eq_top_of_is_compl_bot xy }, { rintro ⟨a, ha, ay⟩, rcases exists_is_compl (xy.symm.Iic_order_iso_Ici ⟨a, ay⟩) with ⟨⟨b, xb⟩, hb⟩, refine ⟨↑(⟨b, xb⟩ : set.Ici x), is_coatom.of_is_coatom_coe_Ici _, xb⟩, rw [← hb.is_atom_iff_is_coatom, order_iso.is_atom_iff], apply ha.Iic } end⟩ lemma is_atomic_of_is_coatomic_of_is_complemented_of_is_modular [is_coatomic α] : is_atomic α := is_coatomic_dual_iff_is_atomic.1 is_coatomic_of_is_atomic_of_is_complemented_of_is_modular theorem is_atomic_iff_is_coatomic : is_atomic α ↔ is_coatomic α := ⟨λ h, @is_coatomic_of_is_atomic_of_is_complemented_of_is_modular _ _ _ _ _ h, λ h, @is_atomic_of_is_coatomic_of_is_complemented_of_is_modular _ _ _ _ _ h⟩ end is_modular_lattice
0ad779e201968eb96ae084315daac6f31c85d9e7
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/02_Dependent_Type_Theory.org.20.lean
f07f6d49f310fc8b53715a237a1f3cddea0eb8c7
[]
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
132
lean
/- page 21 -/ import standard import data.nat open nat -- BEGIN constant n : ℕ check let y := n + n, z := y + y in z * z -- END
60f470fab7cdd8a0ac877b07c6b5c5f34c2a52b8
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/algebra/big_operators/intervals.lean
5794eebc362a5c75f03ff79888a231c5ed842413
[ "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
6,672
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 -/ import algebra.big_operators.basic import data.nat.interval import tactic.linarith /-! # Results about big operators over intervals We prove results about big operators over intervals (mostly the `ℕ`-valued `Ico m n`). -/ universes u v w open_locale big_operators nat namespace finset variables {α : Type u} {β : Type v} {γ : Type w} {s₂ s₁ s : finset α} {a : α} {g f : α → β} [comm_monoid β] lemma sum_Ico_add {δ : Type*} [add_comm_monoid δ] (f : ℕ → δ) (m n k : ℕ) : (∑ l in Ico m n, f (k + l)) = (∑ l in Ico (m + k) (n + k), f l) := image_add_const_Ico m n k ▸ eq.symm $ sum_image $ λ x hx y hy h, nat.add_left_cancel h @[to_additive] lemma prod_Ico_add (f : ℕ → β) (m n k : ℕ) : (∏ l in Ico m n, f (k + l)) = (∏ l in Ico (m + k) (n + k), f l) := @sum_Ico_add (additive β) _ f m n k lemma sum_Ico_succ_top {δ : Type*} [add_comm_monoid δ] {a b : ℕ} (hab : a ≤ b) (f : ℕ → δ) : (∑ k in Ico a (b + 1), f k) = (∑ k in Ico a b, f k) + f b := by rw [nat.Ico_succ_right_eq_insert_Ico hab, sum_insert right_not_mem_Ico, add_comm] @[to_additive] lemma prod_Ico_succ_top {a b : ℕ} (hab : a ≤ b) (f : ℕ → β) : (∏ k in Ico a (b + 1), f k) = (∏ k in Ico a b, f k) * f b := @sum_Ico_succ_top (additive β) _ _ _ hab _ lemma sum_eq_sum_Ico_succ_bot {δ : Type*} [add_comm_monoid δ] {a b : ℕ} (hab : a < b) (f : ℕ → δ) : (∑ k in Ico a b, f k) = f a + (∑ k in Ico (a + 1) b, f k) := have ha : a ∉ Ico (a + 1) b, by simp, by rw [← sum_insert ha, nat.Ico_insert_succ_left hab] @[to_additive] lemma prod_eq_prod_Ico_succ_bot {a b : ℕ} (hab : a < b) (f : ℕ → β) : (∏ k in Ico a b, f k) = f a * (∏ k in Ico (a + 1) b, f k) := @sum_eq_sum_Ico_succ_bot (additive β) _ _ _ hab _ @[to_additive] lemma prod_Ico_consecutive (f : ℕ → β) {m n k : ℕ} (hmn : m ≤ n) (hnk : n ≤ k) : (∏ i in Ico m n, f i) * (∏ i in Ico n k, f i) = (∏ i in Ico m k, f i) := Ico_union_Ico_eq_Ico hmn hnk ▸ eq.symm $ prod_union $ Ico_disjoint_Ico_consecutive m n k @[to_additive] lemma prod_range_mul_prod_Ico (f : ℕ → β) {m n : ℕ} (h : m ≤ n) : (∏ k in range m, f k) * (∏ k in Ico m n, f k) = (∏ k in range n, f k) := m.Ico_zero_eq_range ▸ n.Ico_zero_eq_range ▸ prod_Ico_consecutive f m.zero_le h @[to_additive] lemma prod_Ico_eq_mul_inv {δ : Type*} [comm_group δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) : (∏ k in Ico m n, f k) = (∏ k in range n, f k) * (∏ k in range m, f k)⁻¹ := eq_mul_inv_iff_mul_eq.2 $ by rw [mul_comm]; exact prod_range_mul_prod_Ico f h lemma sum_Ico_eq_sub {δ : Type*} [add_comm_group δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) : (∑ k in Ico m n, f k) = (∑ k in range n, f k) - (∑ k in range m, f k) := by simpa only [sub_eq_add_neg] using sum_Ico_eq_add_neg f h /-- The two ways of summing over `(i,j)` in the range `a<=i<=j<b` are equal. -/ lemma sum_Ico_Ico_comm {M : Type*} [add_comm_monoid M] (a b : ℕ) (f : ℕ → ℕ → M) : ∑ i in finset.Ico a b, ∑ j in finset.Ico i b, f i j = ∑ j in finset.Ico a b, ∑ i in finset.Ico a (j+1), f i j := begin rw [finset.sum_sigma', finset.sum_sigma'], refine finset.sum_bij' (λ (x : Σ (i : ℕ), ℕ) _, (⟨x.2, x.1⟩ : Σ (i : ℕ), ℕ)) _ (λ _ _, rfl) (λ (x : Σ (i : ℕ), ℕ) _, (⟨x.2, x.1⟩ : Σ (i : ℕ), ℕ)) _ (by rintro ⟨⟩ _; refl) (by rintro ⟨⟩ _; refl); simp only [finset.mem_Ico, sigma.forall, finset.mem_sigma]; rintros a b ⟨⟨h₁,h₂⟩, ⟨h₃, h₄⟩⟩; refine ⟨⟨_, _⟩, ⟨_, _⟩⟩; linarith end @[to_additive] lemma prod_Ico_eq_prod_range (f : ℕ → β) (m n : ℕ) : (∏ k in Ico m n, f k) = (∏ k in range (n - m), f (m + k)) := begin by_cases h : m ≤ n, { rw [←nat.Ico_zero_eq_range, prod_Ico_add, zero_add, nat.sub_add_cancel h] }, { replace h : n ≤ m := le_of_not_ge h, rw [Ico_eq_empty_of_le h, nat.sub_eq_zero_of_le h, range_zero, prod_empty, prod_empty] } end lemma prod_Ico_reflect (f : ℕ → β) (k : ℕ) {m n : ℕ} (h : m ≤ n + 1) : ∏ j in Ico k m, f (n - j) = ∏ j in Ico (n + 1 - m) (n + 1 - k), f j := begin have : ∀ i < m, i ≤ n, { intros i hi, exact (add_le_add_iff_right 1).1 (le_trans (nat.lt_iff_add_one_le.1 hi) h) }, cases lt_or_le k m with hkm hkm, { rw [← nat.Ico_image_const_sub_eq_Ico (this _ hkm)], refine (prod_image _).symm, simp only [mem_Ico], rintros i ⟨ki, im⟩ j ⟨kj, jm⟩ Hij, rw [← nat.sub_sub_self (this _ im), Hij, nat.sub_sub_self (this _ jm)] }, { simp [Ico_eq_empty_of_le, nat.sub_le_sub_left, hkm] } end lemma sum_Ico_reflect {δ : Type*} [add_comm_monoid δ] (f : ℕ → δ) (k : ℕ) {m n : ℕ} (h : m ≤ n + 1) : ∑ j in Ico k m, f (n - j) = ∑ j in Ico (n + 1 - m) (n + 1 - k), f j := @prod_Ico_reflect (multiplicative δ) _ f k m n h lemma prod_range_reflect (f : ℕ → β) (n : ℕ) : ∏ j in range n, f (n - 1 - j) = ∏ j in range n, f j := begin cases n, { simp }, { simp only [←nat.Ico_zero_eq_range, nat.succ_sub_succ_eq_sub, nat.sub_zero], rw prod_Ico_reflect _ _ le_rfl, simp } end lemma sum_range_reflect {δ : Type*} [add_comm_monoid δ] (f : ℕ → δ) (n : ℕ) : ∑ j in range n, f (n - 1 - j) = ∑ j in range n, f j := @prod_range_reflect (multiplicative δ) _ f n @[simp] lemma prod_Ico_id_eq_factorial : ∀ n : ℕ, ∏ x in Ico 1 (n + 1), x = n! | 0 := rfl | (n+1) := by rw [prod_Ico_succ_top $ nat.succ_le_succ $ zero_le n, nat.factorial_succ, prod_Ico_id_eq_factorial n, nat.succ_eq_add_one, mul_comm] @[simp] lemma prod_range_add_one_eq_factorial : ∀ n : ℕ, ∏ x in range n, (x+1) = n! | 0 := rfl | (n+1) := by simp [finset.range_succ, prod_range_add_one_eq_factorial n] section gauss_sum /-- Gauss' summation formula -/ lemma sum_range_id_mul_two (n : ℕ) : (∑ i in range n, i) * 2 = n * (n - 1) := calc (∑ i in range n, i) * 2 = (∑ i in range n, i) + (∑ i in range n, (n - 1 - i)) : by rw [sum_range_reflect (λ i, i) n, mul_two] ... = ∑ i in range n, (i + (n - 1 - i)) : sum_add_distrib.symm ... = ∑ i in range n, (n - 1) : sum_congr rfl $ λ i hi, add_sub_cancel_of_le $ nat.le_pred_of_lt $ mem_range.1 hi ... = n * (n - 1) : by rw [sum_const, card_range, nat.nsmul_eq_mul] /-- Gauss' summation formula -/ lemma sum_range_id (n : ℕ) : (∑ i in range n, i) = (n * (n - 1)) / 2 := by rw [← sum_range_id_mul_two n, nat.mul_div_cancel]; exact dec_trivial end gauss_sum end finset
ea3065502999e85551eed2bbaa0a9c40fd300bbf
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/specific_limits/normed.lean
291940cf21cc31c8ef575f12d67f5d1e728d11d2
[ "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
30,330
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker, Sébastien Gouëzel, Yury G. Kudryashov, Dylan MacKenzie, Patrick Massot -/ import algebra.order.field.basic import analysis.asymptotics.asymptotics import analysis.specific_limits.basic /-! # A collection of specific limit computations > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file contains important specific limit computations in (semi-)normed groups/rings/spaces, as as well as such computations in `ℝ` when the natural proof passes through a fact about normed spaces. -/ noncomputable theory open classical set function filter finset metric asymptotics open_locale classical topology nat big_operators uniformity nnreal ennreal variables {α : Type*} {β : Type*} {ι : Type*} lemma tendsto_norm_at_top_at_top : tendsto (norm : ℝ → ℝ) at_top at_top := tendsto_abs_at_top_at_top lemma summable_of_absolute_convergence_real {f : ℕ → ℝ} : (∃r, tendsto (λn, (∑ i in range n, |f i|)) at_top (𝓝 r)) → summable f | ⟨r, hr⟩ := begin refine summable_of_summable_norm ⟨r, (has_sum_iff_tendsto_nat_of_nonneg _ _).2 _⟩, exact assume i, norm_nonneg _, simpa only using hr end /-! ### Powers -/ lemma tendsto_norm_zero' {𝕜 : Type*} [normed_add_comm_group 𝕜] : tendsto (norm : 𝕜 → ℝ) (𝓝[≠] 0) (𝓝[>] 0) := tendsto_norm_zero.inf $ tendsto_principal_principal.2 $ λ x hx, norm_pos_iff.2 hx namespace normed_field lemma tendsto_norm_inverse_nhds_within_0_at_top {𝕜 : Type*} [normed_field 𝕜] : tendsto (λ x:𝕜, ‖x⁻¹‖) (𝓝[≠] 0) at_top := (tendsto_inv_zero_at_top.comp tendsto_norm_zero').congr $ λ x, (norm_inv x).symm lemma tendsto_norm_zpow_nhds_within_0_at_top {𝕜 : Type*} [normed_field 𝕜] {m : ℤ} (hm : m < 0) : tendsto (λ x : 𝕜, ‖x ^ m‖) (𝓝[≠] 0) at_top := begin rcases neg_surjective m with ⟨m, rfl⟩, rw neg_lt_zero at hm, lift m to ℕ using hm.le, rw int.coe_nat_pos at hm, simp only [norm_pow, zpow_neg, zpow_coe_nat, ← inv_pow], exact (tendsto_pow_at_top hm.ne').comp normed_field.tendsto_norm_inverse_nhds_within_0_at_top end /-- The (scalar) product of a sequence that tends to zero with a bounded one also tends to zero. -/ lemma tendsto_zero_smul_of_tendsto_zero_of_bounded {ι 𝕜 𝔸 : Type*} [normed_field 𝕜] [normed_add_comm_group 𝔸] [normed_space 𝕜 𝔸] {l : filter ι} {ε : ι → 𝕜} {f : ι → 𝔸} (hε : tendsto ε l (𝓝 0)) (hf : filter.is_bounded_under (≤) l (norm ∘ f)) : tendsto (ε • f) l (𝓝 0) := begin rw ← is_o_one_iff 𝕜 at hε ⊢, simpa using is_o.smul_is_O hε (hf.is_O_const (one_ne_zero : (1 : 𝕜) ≠ 0)) end @[simp] lemma continuous_at_zpow {𝕜 : Type*} [nontrivially_normed_field 𝕜] {m : ℤ} {x : 𝕜} : continuous_at (λ x, x ^ m) x ↔ x ≠ 0 ∨ 0 ≤ m := begin refine ⟨_, continuous_at_zpow₀ _ _⟩, contrapose!, rintro ⟨rfl, hm⟩ hc, exact not_tendsto_at_top_of_tendsto_nhds (hc.tendsto.mono_left nhds_within_le_nhds).norm (tendsto_norm_zpow_nhds_within_0_at_top hm) end @[simp] lemma continuous_at_inv {𝕜 : Type*} [nontrivially_normed_field 𝕜] {x : 𝕜} : continuous_at has_inv.inv x ↔ x ≠ 0 := by simpa [(zero_lt_one' ℤ).not_le] using @continuous_at_zpow _ _ (-1) x end normed_field lemma is_o_pow_pow_of_lt_left {r₁ r₂ : ℝ} (h₁ : 0 ≤ r₁) (h₂ : r₁ < r₂) : (λ n : ℕ, r₁ ^ n) =o[at_top] (λ n, r₂ ^ n) := have H : 0 < r₂ := h₁.trans_lt h₂, is_o_of_tendsto (λ n hn, false.elim $ H.ne' $ pow_eq_zero hn) $ (tendsto_pow_at_top_nhds_0_of_lt_1 (div_nonneg h₁ (h₁.trans h₂.le)) ((div_lt_one H).2 h₂)).congr (λ n, div_pow _ _ _) lemma is_O_pow_pow_of_le_left {r₁ r₂ : ℝ} (h₁ : 0 ≤ r₁) (h₂ : r₁ ≤ r₂) : (λ n : ℕ, r₁ ^ n) =O[at_top] (λ n, r₂ ^ n) := h₂.eq_or_lt.elim (λ h, h ▸ is_O_refl _ _) (λ h, (is_o_pow_pow_of_lt_left h₁ h).is_O) lemma is_o_pow_pow_of_abs_lt_left {r₁ r₂ : ℝ} (h : |r₁| < |r₂|) : (λ n : ℕ, r₁ ^ n) =o[at_top] (λ n, r₂ ^ n) := begin refine (is_o.of_norm_left _).of_norm_right, exact (is_o_pow_pow_of_lt_left (abs_nonneg r₁) h).congr (pow_abs r₁) (pow_abs r₂) end /-- Various statements equivalent to the fact that `f n` grows exponentially slower than `R ^ n`. * 0: $f n = o(a ^ n)$ for some $-R < a < R$; * 1: $f n = o(a ^ n)$ for some $0 < a < R$; * 2: $f n = O(a ^ n)$ for some $-R < a < R$; * 3: $f n = O(a ^ n)$ for some $0 < a < R$; * 4: there exist `a < R` and `C` such that one of `C` and `R` is positive and $|f n| ≤ Ca^n$ for all `n`; * 5: there exists `0 < a < R` and a positive `C` such that $|f n| ≤ Ca^n$ for all `n`; * 6: there exists `a < R` such that $|f n| ≤ a ^ n$ for sufficiently large `n`; * 7: there exists `0 < a < R` such that $|f n| ≤ a ^ n$ for sufficiently large `n`. NB: For backwards compatibility, if you add more items to the list, please append them at the end of the list. -/ lemma tfae_exists_lt_is_o_pow (f : ℕ → ℝ) (R : ℝ) : tfae [∃ a ∈ Ioo (-R) R, f =o[at_top] pow a, ∃ a ∈ Ioo 0 R, f =o[at_top] (pow a), ∃ a ∈ Ioo (-R) R, f =O[at_top] pow a, ∃ a ∈ Ioo 0 R, f =O[at_top] pow a, ∃ (a < R) C (h₀ : 0 < C ∨ 0 < R), ∀ n, |f n| ≤ C * a ^ n, ∃ (a ∈ Ioo 0 R) (C > 0), ∀ n, |f n| ≤ C * a ^ n, ∃ a < R, ∀ᶠ n in at_top, |f n| ≤ a ^ n, ∃ a ∈ Ioo 0 R, ∀ᶠ n in at_top, |f n| ≤ a ^ n] := begin have A : Ico 0 R ⊆ Ioo (-R) R, from λ x hx, ⟨(neg_lt_zero.2 (hx.1.trans_lt hx.2)).trans_le hx.1, hx.2⟩, have B : Ioo 0 R ⊆ Ioo (-R) R := subset.trans Ioo_subset_Ico_self A, -- First we prove that 1-4 are equivalent using 2 → 3 → 4, 1 → 3, and 2 → 1 tfae_have : 1 → 3, from λ ⟨a, ha, H⟩, ⟨a, ha, H.is_O⟩, tfae_have : 2 → 1, from λ ⟨a, ha, H⟩, ⟨a, B ha, H⟩, tfae_have : 3 → 2, { rintro ⟨a, ha, H⟩, rcases exists_between (abs_lt.2 ha) with ⟨b, hab, hbR⟩, exact ⟨b, ⟨(abs_nonneg a).trans_lt hab, hbR⟩, H.trans_is_o (is_o_pow_pow_of_abs_lt_left (hab.trans_le (le_abs_self b)))⟩ }, tfae_have : 2 → 4, from λ ⟨a, ha, H⟩, ⟨a, ha, H.is_O⟩, tfae_have : 4 → 3, from λ ⟨a, ha, H⟩, ⟨a, B ha, H⟩, -- Add 5 and 6 using 4 → 6 → 5 → 3 tfae_have : 4 → 6, { rintro ⟨a, ha, H⟩, rcases bound_of_is_O_nat_at_top H with ⟨C, hC₀, hC⟩, refine ⟨a, ha, C, hC₀, λ n, _⟩, simpa only [real.norm_eq_abs, abs_pow, abs_of_nonneg ha.1.le] using hC (pow_ne_zero n ha.1.ne') }, tfae_have : 6 → 5, from λ ⟨a, ha, C, H₀, H⟩, ⟨a, ha.2, C, or.inl H₀, H⟩, tfae_have : 5 → 3, { rintro ⟨a, ha, C, h₀, H⟩, rcases sign_cases_of_C_mul_pow_nonneg (λ n, (abs_nonneg _).trans (H n)) with rfl | ⟨hC₀, ha₀⟩, { obtain rfl : f = 0, by { ext n, simpa using H n }, simp only [lt_irrefl, false_or] at h₀, exact ⟨0, ⟨neg_lt_zero.2 h₀, h₀⟩, is_O_zero _ _⟩ }, exact ⟨a, A ⟨ha₀, ha⟩, is_O_of_le' _ (λ n, (H n).trans $ mul_le_mul_of_nonneg_left (le_abs_self _) hC₀.le)⟩ }, -- Add 7 and 8 using 2 → 8 → 7 → 3 tfae_have : 2 → 8, { rintro ⟨a, ha, H⟩, refine ⟨a, ha, (H.def zero_lt_one).mono (λ n hn, _)⟩, rwa [real.norm_eq_abs, real.norm_eq_abs, one_mul, abs_pow, abs_of_pos ha.1] at hn }, tfae_have : 8 → 7, from λ ⟨a, ha, H⟩, ⟨a, ha.2, H⟩, tfae_have : 7 → 3, { rintro ⟨a, ha, H⟩, have : 0 ≤ a, from nonneg_of_eventually_pow_nonneg (H.mono $ λ n, (abs_nonneg _).trans), refine ⟨a, A ⟨this, ha⟩, is_O.of_bound 1 _⟩, simpa only [real.norm_eq_abs, one_mul, abs_pow, abs_of_nonneg this] }, tfae_finish end /-- For any natural `k` and a real `r > 1` we have `n ^ k = o(r ^ n)` as `n → ∞`. -/ lemma is_o_pow_const_const_pow_of_one_lt {R : Type*} [normed_ring R] (k : ℕ) {r : ℝ} (hr : 1 < r) : (λ n, n ^ k : ℕ → R) =o[at_top] (λ n, r ^ n) := begin have : tendsto (λ x : ℝ, x ^ k) (𝓝[>] 1) (𝓝 1), from ((continuous_id.pow k).tendsto' (1 : ℝ) 1 (one_pow _)).mono_left inf_le_left, obtain ⟨r' : ℝ, hr' : r' ^ k < r, h1 : 1 < r'⟩ := ((this.eventually (gt_mem_nhds hr)).and self_mem_nhds_within).exists, have h0 : 0 ≤ r' := zero_le_one.trans h1.le, suffices : (λ n, n ^ k : ℕ → R) =O[at_top] (λ n : ℕ, (r' ^ k) ^ n), from this.trans_is_o (is_o_pow_pow_of_lt_left (pow_nonneg h0 _) hr'), conv in ((r' ^ _) ^ _) { rw [← pow_mul, mul_comm, pow_mul] }, suffices : ∀ n : ℕ, ‖(n : R)‖ ≤ (r' - 1)⁻¹ * ‖(1 : R)‖ * ‖r' ^ n‖, from (is_O_of_le' _ this).pow _, intro n, rw mul_right_comm, refine n.norm_cast_le.trans (mul_le_mul_of_nonneg_right _ (norm_nonneg _)), simpa [div_eq_inv_mul, real.norm_eq_abs, abs_of_nonneg h0] using n.cast_le_pow_div_sub h1 end /-- For a real `r > 1` we have `n = o(r ^ n)` as `n → ∞`. -/ lemma is_o_coe_const_pow_of_one_lt {R : Type*} [normed_ring R] {r : ℝ} (hr : 1 < r) : (coe : ℕ → R) =o[at_top] (λ n, r ^ n) := by simpa only [pow_one] using @is_o_pow_const_const_pow_of_one_lt R _ 1 _ hr /-- If `‖r₁‖ < r₂`, then for any naturak `k` we have `n ^ k r₁ ^ n = o (r₂ ^ n)` as `n → ∞`. -/ lemma is_o_pow_const_mul_const_pow_const_pow_of_norm_lt {R : Type*} [normed_ring R] (k : ℕ) {r₁ : R} {r₂ : ℝ} (h : ‖r₁‖ < r₂) : (λ n, n ^ k * r₁ ^ n : ℕ → R) =o[at_top] (λ n, r₂ ^ n) := begin by_cases h0 : r₁ = 0, { refine (is_o_zero _ _).congr' (mem_at_top_sets.2 $ ⟨1, λ n hn, _⟩) eventually_eq.rfl, simp [zero_pow (zero_lt_one.trans_le hn), h0] }, rw [← ne.def, ← norm_pos_iff] at h0, have A : (λ n, n ^ k : ℕ → R) =o[at_top] (λ n, (r₂ / ‖r₁‖) ^ n), from is_o_pow_const_const_pow_of_one_lt k ((one_lt_div h0).2 h), suffices : (λ n, r₁ ^ n) =O[at_top] (λ n, ‖r₁‖ ^ n), by simpa [div_mul_cancel _ (pow_pos h0 _).ne'] using A.mul_is_O this, exact is_O.of_bound 1 (by simpa using eventually_norm_pow_le r₁) end lemma tendsto_pow_const_div_const_pow_of_one_lt (k : ℕ) {r : ℝ} (hr : 1 < r) : tendsto (λ n, n ^ k / r ^ n : ℕ → ℝ) at_top (𝓝 0) := (is_o_pow_const_const_pow_of_one_lt k hr).tendsto_div_nhds_zero /-- If `|r| < 1`, then `n ^ k r ^ n` tends to zero for any natural `k`. -/ lemma tendsto_pow_const_mul_const_pow_of_abs_lt_one (k : ℕ) {r : ℝ} (hr : |r| < 1) : tendsto (λ n, n ^ k * r ^ n : ℕ → ℝ) at_top (𝓝 0) := begin by_cases h0 : r = 0, { exact tendsto_const_nhds.congr' (mem_at_top_sets.2 ⟨1, λ n hn, by simp [zero_lt_one.trans_le hn, h0]⟩) }, have hr' : 1 < (|r|)⁻¹, from one_lt_inv (abs_pos.2 h0) hr, rw tendsto_zero_iff_norm_tendsto_zero, simpa [div_eq_mul_inv] using tendsto_pow_const_div_const_pow_of_one_lt k hr' end /-- If `0 ≤ r < 1`, then `n ^ k r ^ n` tends to zero for any natural `k`. This is a specialized version of `tendsto_pow_const_mul_const_pow_of_abs_lt_one`, singled out for ease of application. -/ lemma tendsto_pow_const_mul_const_pow_of_lt_one (k : ℕ) {r : ℝ} (hr : 0 ≤ r) (h'r : r < 1) : tendsto (λ n, n ^ k * r ^ n : ℕ → ℝ) at_top (𝓝 0) := tendsto_pow_const_mul_const_pow_of_abs_lt_one k (abs_lt.2 ⟨neg_one_lt_zero.trans_le hr, h'r⟩) /-- If `|r| < 1`, then `n * r ^ n` tends to zero. -/ lemma tendsto_self_mul_const_pow_of_abs_lt_one {r : ℝ} (hr : |r| < 1) : tendsto (λ n, n * r ^ n : ℕ → ℝ) at_top (𝓝 0) := by simpa only [pow_one] using tendsto_pow_const_mul_const_pow_of_abs_lt_one 1 hr /-- If `0 ≤ r < 1`, then `n * r ^ n` tends to zero. This is a specialized version of `tendsto_self_mul_const_pow_of_abs_lt_one`, singled out for ease of application. -/ lemma tendsto_self_mul_const_pow_of_lt_one {r : ℝ} (hr : 0 ≤ r) (h'r : r < 1) : tendsto (λ n, n * r ^ n : ℕ → ℝ) at_top (𝓝 0) := by simpa only [pow_one] using tendsto_pow_const_mul_const_pow_of_lt_one 1 hr h'r /-- In a normed ring, the powers of an element x with `‖x‖ < 1` tend to zero. -/ lemma tendsto_pow_at_top_nhds_0_of_norm_lt_1 {R : Type*} [normed_ring R] {x : R} (h : ‖x‖ < 1) : tendsto (λ (n : ℕ), x ^ n) at_top (𝓝 0) := begin apply squeeze_zero_norm' (eventually_norm_pow_le x), exact tendsto_pow_at_top_nhds_0_of_lt_1 (norm_nonneg _) h, end lemma tendsto_pow_at_top_nhds_0_of_abs_lt_1 {r : ℝ} (h : |r| < 1) : tendsto (λn:ℕ, r^n) at_top (𝓝 0) := tendsto_pow_at_top_nhds_0_of_norm_lt_1 h /-! ### Geometric series-/ section geometric variables {K : Type*} [normed_field K] {ξ : K} lemma has_sum_geometric_of_norm_lt_1 (h : ‖ξ‖ < 1) : has_sum (λn:ℕ, ξ ^ n) (1 - ξ)⁻¹ := begin have xi_ne_one : ξ ≠ 1, by { contrapose! h, simp [h] }, have A : tendsto (λn, (ξ ^ n - 1) * (ξ - 1)⁻¹) at_top (𝓝 ((0 - 1) * (ξ - 1)⁻¹)), from ((tendsto_pow_at_top_nhds_0_of_norm_lt_1 h).sub tendsto_const_nhds).mul tendsto_const_nhds, rw [has_sum_iff_tendsto_nat_of_summable_norm], { simpa [geom_sum_eq, xi_ne_one, neg_inv, div_eq_mul_inv] using A }, { simp [norm_pow, summable_geometric_of_lt_1 (norm_nonneg _) h] } end lemma summable_geometric_of_norm_lt_1 (h : ‖ξ‖ < 1) : summable (λn:ℕ, ξ ^ n) := ⟨_, has_sum_geometric_of_norm_lt_1 h⟩ lemma tsum_geometric_of_norm_lt_1 (h : ‖ξ‖ < 1) : ∑'n:ℕ, ξ ^ n = (1 - ξ)⁻¹ := (has_sum_geometric_of_norm_lt_1 h).tsum_eq lemma has_sum_geometric_of_abs_lt_1 {r : ℝ} (h : |r| < 1) : has_sum (λn:ℕ, r ^ n) (1 - r)⁻¹ := has_sum_geometric_of_norm_lt_1 h lemma summable_geometric_of_abs_lt_1 {r : ℝ} (h : |r| < 1) : summable (λn:ℕ, r ^ n) := summable_geometric_of_norm_lt_1 h lemma tsum_geometric_of_abs_lt_1 {r : ℝ} (h : |r| < 1) : ∑'n:ℕ, r ^ n = (1 - r)⁻¹ := tsum_geometric_of_norm_lt_1 h /-- A geometric series in a normed field is summable iff the norm of the common ratio is less than one. -/ @[simp] lemma summable_geometric_iff_norm_lt_1 : summable (λ n : ℕ, ξ ^ n) ↔ ‖ξ‖ < 1 := begin refine ⟨λ h, _, summable_geometric_of_norm_lt_1⟩, obtain ⟨k : ℕ, hk : dist (ξ ^ k) 0 < 1⟩ := (h.tendsto_cofinite_zero.eventually (ball_mem_nhds _ zero_lt_one)).exists, simp only [norm_pow, dist_zero_right] at hk, rw [← one_pow k] at hk, exact lt_of_pow_lt_pow _ zero_le_one hk end end geometric section mul_geometric lemma summable_norm_pow_mul_geometric_of_norm_lt_1 {R : Type*} [normed_ring R] (k : ℕ) {r : R} (hr : ‖r‖ < 1) : summable (λ n : ℕ, ‖(n ^ k * r ^ n : R)‖) := begin rcases exists_between hr with ⟨r', hrr', h⟩, exact summable_of_is_O_nat (summable_geometric_of_lt_1 ((norm_nonneg _).trans hrr'.le) h) (is_o_pow_const_mul_const_pow_const_pow_of_norm_lt _ hrr').is_O.norm_left end lemma summable_pow_mul_geometric_of_norm_lt_1 {R : Type*} [normed_ring R] [complete_space R] (k : ℕ) {r : R} (hr : ‖r‖ < 1) : summable (λ n, n ^ k * r ^ n : ℕ → R) := summable_of_summable_norm $ summable_norm_pow_mul_geometric_of_norm_lt_1 _ hr /-- If `‖r‖ < 1`, then `∑' n : ℕ, n * r ^ n = r / (1 - r) ^ 2`, `has_sum` version. -/ lemma has_sum_coe_mul_geometric_of_norm_lt_1 {𝕜 : Type*} [normed_field 𝕜] [complete_space 𝕜] {r : 𝕜} (hr : ‖r‖ < 1) : has_sum (λ n, n * r ^ n : ℕ → 𝕜) (r / (1 - r) ^ 2) := begin have A : summable (λ n, n * r ^ n : ℕ → 𝕜), by simpa using summable_pow_mul_geometric_of_norm_lt_1 1 hr, have B : has_sum (pow r : ℕ → 𝕜) (1 - r)⁻¹, from has_sum_geometric_of_norm_lt_1 hr, refine A.has_sum_iff.2 _, have hr' : r ≠ 1, by { rintro rfl, simpa [lt_irrefl] using hr }, set s : 𝕜 := ∑' n : ℕ, n * r ^ n, calc s = (1 - r) * s / (1 - r) : (mul_div_cancel_left _ (sub_ne_zero.2 hr'.symm)).symm ... = (s - r * s) / (1 - r) : by rw [sub_mul, one_mul] ... = ((0 : ℕ) * r ^ 0 + (∑' n : ℕ, (n + 1 : ℕ) * r ^ (n + 1)) - r * s) / (1 - r) : by rw ← tsum_eq_zero_add A ... = (r * (∑' n : ℕ, (n + 1) * r ^ n) - r * s) / (1 - r) : by simp [pow_succ, mul_left_comm _ r, tsum_mul_left] ... = r / (1 - r) ^ 2 : by simp [add_mul, tsum_add A B.summable, mul_add, B.tsum_eq, ← div_eq_mul_inv, sq, div_div] end /-- If `‖r‖ < 1`, then `∑' n : ℕ, n * r ^ n = r / (1 - r) ^ 2`. -/ lemma tsum_coe_mul_geometric_of_norm_lt_1 {𝕜 : Type*} [normed_field 𝕜] [complete_space 𝕜] {r : 𝕜} (hr : ‖r‖ < 1) : (∑' n : ℕ, n * r ^ n : 𝕜) = (r / (1 - r) ^ 2) := (has_sum_coe_mul_geometric_of_norm_lt_1 hr).tsum_eq end mul_geometric section summable_le_geometric variables [seminormed_add_comm_group α] {r C : ℝ} {f : ℕ → α} lemma seminormed_add_comm_group.cauchy_seq_of_le_geometric {C : ℝ} {r : ℝ} (hr : r < 1) {u : ℕ → α} (h : ∀ n, ‖u n - u (n + 1)‖ ≤ C*r^n) : cauchy_seq u := cauchy_seq_of_le_geometric r C hr (by simpa [dist_eq_norm] using h) lemma dist_partial_sum_le_of_le_geometric (hf : ∀n, ‖f n‖ ≤ C * r^n) (n : ℕ) : dist (∑ i in range n, f i) (∑ i in range (n+1), f i) ≤ C * r ^ n := begin rw [sum_range_succ, dist_eq_norm, ← norm_neg, neg_sub, add_sub_cancel'], exact hf n, end /-- If `‖f n‖ ≤ C * r ^ n` for all `n : ℕ` and some `r < 1`, then the partial sums of `f` form a Cauchy sequence. This lemma does not assume `0 ≤ r` or `0 ≤ C`. -/ lemma cauchy_seq_finset_of_geometric_bound (hr : r < 1) (hf : ∀n, ‖f n‖ ≤ C * r^n) : cauchy_seq (λ s : finset (ℕ), ∑ x in s, f x) := cauchy_seq_finset_of_norm_bounded _ (aux_has_sum_of_le_geometric hr (dist_partial_sum_le_of_le_geometric hf)).summable hf /-- If `‖f n‖ ≤ C * r ^ n` for all `n : ℕ` and some `r < 1`, then the partial sums of `f` are within distance `C * r ^ n / (1 - r)` of the sum of the series. This lemma does not assume `0 ≤ r` or `0 ≤ C`. -/ lemma norm_sub_le_of_geometric_bound_of_has_sum (hr : r < 1) (hf : ∀n, ‖f n‖ ≤ C * r^n) {a : α} (ha : has_sum f a) (n : ℕ) : ‖(∑ x in finset.range n, f x) - a‖ ≤ (C * r ^ n) / (1 - r) := begin rw ← dist_eq_norm, apply dist_le_of_le_geometric_of_tendsto r C hr (dist_partial_sum_le_of_le_geometric hf), exact ha.tendsto_sum_nat end @[simp] lemma dist_partial_sum (u : ℕ → α) (n : ℕ) : dist (∑ k in range (n + 1), u k) (∑ k in range n, u k) = ‖u n‖ := by simp [dist_eq_norm, sum_range_succ] @[simp] lemma dist_partial_sum' (u : ℕ → α) (n : ℕ) : dist (∑ k in range n, u k) (∑ k in range (n+1), u k) = ‖u n‖ := by simp [dist_eq_norm', sum_range_succ] lemma cauchy_series_of_le_geometric {C : ℝ} {u : ℕ → α} {r : ℝ} (hr : r < 1) (h : ∀ n, ‖u n‖ ≤ C*r^n) : cauchy_seq (λ n, ∑ k in range n, u k) := cauchy_seq_of_le_geometric r C hr (by simp [h]) lemma normed_add_comm_group.cauchy_series_of_le_geometric' {C : ℝ} {u : ℕ → α} {r : ℝ} (hr : r < 1) (h : ∀ n, ‖u n‖ ≤ C*r^n) : cauchy_seq (λ n, ∑ k in range (n + 1), u k) := (cauchy_series_of_le_geometric hr h).comp_tendsto $ tendsto_add_at_top_nat 1 lemma normed_add_comm_group.cauchy_series_of_le_geometric'' {C : ℝ} {u : ℕ → α} {N : ℕ} {r : ℝ} (hr₀ : 0 < r) (hr₁ : r < 1) (h : ∀ n ≥ N, ‖u n‖ ≤ C*r^n) : cauchy_seq (λ n, ∑ k in range (n + 1), u k) := begin set v : ℕ → α := λ n, if n < N then 0 else u n, have hC : 0 ≤ C, from (zero_le_mul_right $ pow_pos hr₀ N).mp ((norm_nonneg _).trans $ h N $ le_refl N), have : ∀ n ≥ N, u n = v n, { intros n hn, simp [v, hn, if_neg (not_lt.mpr hn)] }, refine cauchy_seq_sum_of_eventually_eq this (normed_add_comm_group.cauchy_series_of_le_geometric' hr₁ _), { exact C }, intro n, dsimp [v], split_ifs with H H, { rw norm_zero, exact mul_nonneg hC (pow_nonneg hr₀.le _) }, { push_neg at H, exact h _ H } end end summable_le_geometric section normed_ring_geometric variables {R : Type*} [normed_ring R] [complete_space R] open normed_space /-- A geometric series in a complete normed ring is summable. Proved above (same name, different namespace) for not-necessarily-complete normed fields. -/ lemma normed_ring.summable_geometric_of_norm_lt_1 (x : R) (h : ‖x‖ < 1) : summable (λ (n:ℕ), x ^ n) := begin have h1 : summable (λ (n:ℕ), ‖x‖ ^ n) := summable_geometric_of_lt_1 (norm_nonneg _) h, refine summable_of_norm_bounded_eventually _ h1 _, rw nat.cofinite_eq_at_top, exact eventually_norm_pow_le x, end /-- Bound for the sum of a geometric series in a normed ring. This formula does not assume that the normed ring satisfies the axiom `‖1‖ = 1`. -/ lemma normed_ring.tsum_geometric_of_norm_lt_1 (x : R) (h : ‖x‖ < 1) : ‖∑' n:ℕ, x ^ n‖ ≤ ‖(1:R)‖ - 1 + (1 - ‖x‖)⁻¹ := begin rw tsum_eq_zero_add (normed_ring.summable_geometric_of_norm_lt_1 x h), simp only [pow_zero], refine le_trans (norm_add_le _ _) _, have : ‖∑' b : ℕ, (λ n, x ^ (n + 1)) b‖ ≤ (1 - ‖x‖)⁻¹ - 1, { refine tsum_of_norm_bounded _ (λ b, norm_pow_le' _ (nat.succ_pos b)), convert (has_sum_nat_add_iff' 1).mpr (has_sum_geometric_of_lt_1 (norm_nonneg x) h), simp }, linarith end lemma geom_series_mul_neg (x : R) (h : ‖x‖ < 1) : (∑' i:ℕ, x ^ i) * (1 - x) = 1 := begin have := ((normed_ring.summable_geometric_of_norm_lt_1 x h).has_sum.mul_right (1 - x)), refine tendsto_nhds_unique this.tendsto_sum_nat _, have : tendsto (λ (n : ℕ), 1 - x ^ n) at_top (𝓝 1), { simpa using tendsto_const_nhds.sub (tendsto_pow_at_top_nhds_0_of_norm_lt_1 h) }, convert ← this, ext n, rw [←geom_sum_mul_neg, finset.sum_mul], end lemma mul_neg_geom_series (x : R) (h : ‖x‖ < 1) : (1 - x) * ∑' i:ℕ, x ^ i = 1 := begin have := (normed_ring.summable_geometric_of_norm_lt_1 x h).has_sum.mul_left (1 - x), refine tendsto_nhds_unique this.tendsto_sum_nat _, have : tendsto (λ (n : ℕ), 1 - x ^ n) at_top (nhds 1), { simpa using tendsto_const_nhds.sub (tendsto_pow_at_top_nhds_0_of_norm_lt_1 h) }, convert ← this, ext n, rw [←mul_neg_geom_sum, finset.mul_sum] end end normed_ring_geometric /-! ### Summability tests based on comparison with geometric series -/ lemma summable_of_ratio_norm_eventually_le {α : Type*} [seminormed_add_comm_group α] [complete_space α] {f : ℕ → α} {r : ℝ} (hr₁ : r < 1) (h : ∀ᶠ n in at_top, ‖f (n+1)‖ ≤ r * ‖f n‖) : summable f := begin by_cases hr₀ : 0 ≤ r, { rw eventually_at_top at h, rcases h with ⟨N, hN⟩, rw ← @summable_nat_add_iff α _ _ _ _ N, refine summable_of_norm_bounded (λ n, ‖f N‖ * r^n) (summable.mul_left _ $ summable_geometric_of_lt_1 hr₀ hr₁) (λ n, _), conv_rhs {rw [mul_comm, ← zero_add N]}, refine le_geom hr₀ n (λ i _, _), convert hN (i + N) (N.le_add_left i) using 3, ac_refl }, { push_neg at hr₀, refine summable_of_norm_bounded_eventually 0 summable_zero _, rw nat.cofinite_eq_at_top, filter_upwards [h] with _ hn, by_contra' h, exact not_lt.mpr (norm_nonneg _) (lt_of_le_of_lt hn $ mul_neg_of_neg_of_pos hr₀ h), }, end lemma summable_of_ratio_test_tendsto_lt_one {α : Type*} [normed_add_comm_group α] [complete_space α] {f : ℕ → α} {l : ℝ} (hl₁ : l < 1) (hf : ∀ᶠ n in at_top, f n ≠ 0) (h : tendsto (λ n, ‖f (n+1)‖/‖f n‖) at_top (𝓝 l)) : summable f := begin rcases exists_between hl₁ with ⟨r, hr₀, hr₁⟩, refine summable_of_ratio_norm_eventually_le hr₁ _, filter_upwards [eventually_le_of_tendsto_lt hr₀ h, hf] with _ _ h₁, rwa ← div_le_iff (norm_pos_iff.mpr h₁), end lemma not_summable_of_ratio_norm_eventually_ge {α : Type*} [seminormed_add_comm_group α] {f : ℕ → α} {r : ℝ} (hr : 1 < r) (hf : ∃ᶠ n in at_top, ‖f n‖ ≠ 0) (h : ∀ᶠ n in at_top, r * ‖f n‖ ≤ ‖f (n+1)‖) : ¬ summable f := begin rw eventually_at_top at h, rcases h with ⟨N₀, hN₀⟩, rw frequently_at_top at hf, rcases hf N₀ with ⟨N, hNN₀ : N₀ ≤ N, hN⟩, rw ← @summable_nat_add_iff α _ _ _ _ N, refine mt summable.tendsto_at_top_zero (λ h', not_tendsto_at_top_of_tendsto_nhds (tendsto_norm_zero.comp h') _), convert tendsto_at_top_of_geom_le _ hr _, { refine lt_of_le_of_ne (norm_nonneg _) _, intro h'', specialize hN₀ N hNN₀, simp only [comp_app, zero_add] at h'', exact hN h''.symm }, { intro i, dsimp only [comp_app], convert (hN₀ (i + N) (hNN₀.trans (N.le_add_left i))) using 3, ac_refl } end lemma not_summable_of_ratio_test_tendsto_gt_one {α : Type*} [seminormed_add_comm_group α] {f : ℕ → α} {l : ℝ} (hl : 1 < l) (h : tendsto (λ n, ‖f (n+1)‖/‖f n‖) at_top (𝓝 l)) : ¬ summable f := begin have key : ∀ᶠ n in at_top, ‖f n‖ ≠ 0, { filter_upwards [eventually_ge_of_tendsto_gt hl h] with _ hn hc, rw [hc, div_zero] at hn, linarith }, rcases exists_between hl with ⟨r, hr₀, hr₁⟩, refine not_summable_of_ratio_norm_eventually_ge hr₀ key.frequently _, filter_upwards [eventually_ge_of_tendsto_gt hr₁ h, key] with _ _ h₁, rwa ← le_div_iff (lt_of_le_of_ne (norm_nonneg _) h₁.symm) end section /-! ### Dirichlet and alternating series tests -/ variables {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] variables {b : ℝ} {f : ℕ → ℝ} {z : ℕ → E} /-- **Dirichlet's Test** for monotone sequences. -/ theorem monotone.cauchy_seq_series_mul_of_tendsto_zero_of_bounded (hfa : monotone f) (hf0 : tendsto f at_top (𝓝 0)) (hgb : ∀ n, ‖∑ i in range n, z i‖ ≤ b) : cauchy_seq (λ n, ∑ i in range (n + 1), (f i) • z i) := begin simp_rw [finset.sum_range_by_parts _ _ (nat.succ _), sub_eq_add_neg, nat.succ_sub_succ_eq_sub, tsub_zero], apply (normed_field.tendsto_zero_smul_of_tendsto_zero_of_bounded hf0 ⟨b, eventually_map.mpr $ eventually_of_forall $ λ n, hgb $ n+1⟩).cauchy_seq.add, refine (cauchy_seq_range_of_norm_bounded _ _ (λ n, _ : ∀ n, _ ≤ b * |f(n+1) - f(n)|)).neg, { simp_rw [abs_of_nonneg (sub_nonneg_of_le (hfa (nat.le_succ _))), ← mul_sum], apply real.uniform_continuous_const_mul.comp_cauchy_seq, simp_rw [sum_range_sub, sub_eq_add_neg], exact (tendsto.cauchy_seq hf0).add_const }, { rw [norm_smul, mul_comm], exact mul_le_mul_of_nonneg_right (hgb _) (abs_nonneg _) }, end /-- **Dirichlet's test** for antitone sequences. -/ theorem antitone.cauchy_seq_series_mul_of_tendsto_zero_of_bounded (hfa : antitone f) (hf0 : tendsto f at_top (𝓝 0)) (hzb : ∀ n, ‖∑ i in range n, z i‖ ≤ b) : cauchy_seq (λ n, ∑ i in range (n+1), (f i) • z i) := begin have hfa': monotone (λ n, -f n) := λ _ _ hab, neg_le_neg $ hfa hab, have hf0': tendsto (λ n, -f n) at_top (𝓝 0) := by { convert hf0.neg, norm_num }, convert (hfa'.cauchy_seq_series_mul_of_tendsto_zero_of_bounded hf0' hzb).neg, funext, simp end lemma norm_sum_neg_one_pow_le (n : ℕ) : ‖∑ i in range n, (-1 : ℝ) ^ i‖ ≤ 1 := by { rw [neg_one_geom_sum], split_ifs; norm_num } /-- The **alternating series test** for monotone sequences. See also `tendsto_alternating_series_of_monotone_tendsto_zero`. -/ theorem monotone.cauchy_seq_alternating_series_of_tendsto_zero (hfa : monotone f) (hf0 : tendsto f at_top (𝓝 0)) : cauchy_seq (λ n, ∑ i in range (n+1), (-1) ^ i * f i) := begin simp_rw [mul_comm], exact hfa.cauchy_seq_series_mul_of_tendsto_zero_of_bounded hf0 norm_sum_neg_one_pow_le end /-- The **alternating series test** for monotone sequences. -/ theorem monotone.tendsto_alternating_series_of_tendsto_zero (hfa : monotone f) (hf0 : tendsto f at_top (𝓝 0)) : ∃ l, tendsto (λ n, ∑ i in range (n+1), (-1) ^ i * f i) at_top (𝓝 l) := cauchy_seq_tendsto_of_complete $ hfa.cauchy_seq_alternating_series_of_tendsto_zero hf0 /-- The **alternating series test** for antitone sequences. See also `tendsto_alternating_series_of_antitone_tendsto_zero`. -/ theorem antitone.cauchy_seq_alternating_series_of_tendsto_zero (hfa : antitone f) (hf0 : tendsto f at_top (𝓝 0)) : cauchy_seq (λ n, ∑ i in range (n+1), (-1) ^ i * f i) := begin simp_rw [mul_comm], exact hfa.cauchy_seq_series_mul_of_tendsto_zero_of_bounded hf0 norm_sum_neg_one_pow_le end /-- The **alternating series test** for antitone sequences. -/ theorem antitone.tendsto_alternating_series_of_tendsto_zero (hfa : antitone f) (hf0 : tendsto f at_top (𝓝 0)) : ∃ l, tendsto (λ n, ∑ i in range (n+1), (-1) ^ i * f i) at_top (𝓝 l) := cauchy_seq_tendsto_of_complete $ hfa.cauchy_seq_alternating_series_of_tendsto_zero hf0 end /-! ### Factorial -/ /-- The series `∑' n, x ^ n / n!` is summable of any `x : ℝ`. See also `exp_series_div_summable` for a version that also works in `ℂ`, and `exp_series_summable'` for a version that works in any normed algebra over `ℝ` or `ℂ`. -/ lemma real.summable_pow_div_factorial (x : ℝ) : summable (λ n, x ^ n / n! : ℕ → ℝ) := begin -- We start with trivial extimates have A : (0 : ℝ) < ⌊‖x‖⌋₊ + 1, from zero_lt_one.trans_le (by simp), have B : ‖x‖ / (⌊‖x‖⌋₊ + 1) < 1, from (div_lt_one A).2 (nat.lt_floor_add_one _), -- Then we apply the ratio test. The estimate works for `n ≥ ⌊‖x‖⌋₊`. suffices : ∀ n ≥ ⌊‖x‖⌋₊, ‖x ^ (n + 1) / (n + 1)!‖ ≤ ‖x‖ / (⌊‖x‖⌋₊ + 1) * ‖x ^ n / ↑n!‖, from summable_of_ratio_norm_eventually_le B (eventually_at_top.2 ⟨⌊‖x‖⌋₊, this⟩), -- Finally, we prove the upper estimate intros n hn, calc ‖x ^ (n + 1) / (n + 1)!‖ = (‖x‖ / (n + 1)) * ‖x ^ n / n!‖ : by rw [pow_succ, nat.factorial_succ, nat.cast_mul, ← div_mul_div_comm, norm_mul, norm_div, real.norm_coe_nat, nat.cast_succ] ... ≤ (‖x‖ / (⌊‖x‖⌋₊ + 1)) * ‖x ^ n / n!‖ : by mono* with [0 ≤ ‖x ^ n / n!‖, 0 ≤ ‖x‖]; apply norm_nonneg end lemma real.tendsto_pow_div_factorial_at_top (x : ℝ) : tendsto (λ n, x ^ n / n! : ℕ → ℝ) at_top (𝓝 0) := (real.summable_pow_div_factorial x).tendsto_at_top_zero
5248330eb51810d1b9b2a449a36c2af00ff5e0a9
efa51dd2edbbbbd6c34bd0ce436415eb405832e7
/20170116_POPL/profile/leanfmt_optimized2.lean
3f8880c2cf2723ec81a4c16c609a5fba06e1fa93
[ "Apache-2.0" ]
permissive
leanprover/presentations
dd031a05bcb12c8855676c77e52ed84246bd889a
3ce2d132d299409f1de269fa8e95afa1333d644e
refs/heads/master
1,688,703,388,796
1,686,838,383,000
1,687,465,742,000
29,750,158
12
9
Apache-2.0
1,540,211,670,000
1,422,042,683,000
Lean
UTF-8
Lean
false
false
2,444
lean
open tactic namespace fmt meta def format.concat : list format -> format | [] := format.nil | (f :: fs) := f ++ format.concat fs @[inline] meta definition mk_local (n : name) (ty : expr) : expr := expr.local_const n n binder_info.default ty @[inline] meta def binder (n : format) (ty : format) : format := "(" ++ n ++ " : " ++ ty ++ ")" meta def let_binding (n : format) (ty : format) (val : format) : format := "x" ++ " : " ++ ty ++ ":=" ++ val @[inline] meta def exp_app (fmt_exp : expr -> format) : list expr → format | [] := format.nil | (e :: []) := "(" ++ fmt_exp e ++ ")" | (e :: es) := "(" ++ fmt_exp e ++ ") " ++ exp_app es meta def exp : expr → format | (expr.elet n ty val body) := "let" ++ let_binding (to_string n) (exp ty) (exp $ expr.instantiate_var body (mk_local n ty)) ++ format.line ++ "in" ++ format.space ++ exp body | (expr.app f arg) := let fn := expr.get_app_fn (expr.app f arg), args := expr.get_app_args (expr.app f arg) in exp fn ++ format.space ++ exp_app exp args | (expr.const c _) := to_string c | (expr.local_const n n' bi ty) := to_string n | (expr.macro _ _ _) := "macro" | (expr.pi n bi sort ty) := "forall" ++ format.space ++ binder (to_string n) (exp sort) ++ "," ++ format.group (format.line ++ format.space ++ exp (expr.instantiate_var ty (mk_local n sort))) | (expr.lam n bi ty body) := "fun" ++ format.space ++ binder (to_string n) (exp ty) ++ "," ++ format.group (format.line ++ exp (expr.instantiate_var body (mk_local n ty))) | (expr.mvar n ty) := "?" ++ to_string n | (expr.sort _) := "Type" | (expr.var i) := "i" meta def decl : declaration → format | (declaration.defn n us ty value _ _) := "def " ++ to_string n ++ " : " ++ exp ty ++ " :=" ++ format.indent (exp value) 2 | (declaration.ax n us ty) := "axiom" ++ format.space ++ to_string n | (declaration.cnst n us body _) := "constant" ++ format.space ++ to_string n | (declaration.thm n us ty body) := "theorem " ++ to_string n end fmt /- Turn on the profiler. -/ set_option profiler true meta def main : tactic unit := do env ← get_env, opts ← get_options, let fs := list.taken 10 $ environment.fold env [] (fun decl decls, fmt.decl decl :: decls) in monad.mapm (fun f, tactic.trace (format.to_string f opts ++ "\n")) fs, return () /- We can then just run the main function to format all declarations in the environment. -/ run_command main
14ec719db99f03285e986aeb0a489a48b89743bd
5883d9218e6f144e20eee6ca1dab8529fa1a97c0
/src/aeq/subst.lean
0646c1789827b4f428f3903ee4a213f25353a35a
[]
no_license
spl/alpha-conversion-is-easy
0d035bc570e52a6345d4890e4d0c9e3f9b8126c1
ed937fe85d8495daffd9412a5524c77b9fcda094
refs/heads/master
1,607,649,280,020
1,517,380,240,000
1,517,380,240,000
52,174,747
4
0
null
1,456,052,226,000
1,456,001,163,000
Lean
UTF-8
Lean
false
false
4,613
lean
/- This file contains the proof that substitution preserves alpha equality. -/ import .id namespace acie ----------------------------------------------------------------- namespace aeq ------------------------------------------------------------------ variables {V : Type} [decidable_eq V] -- Type of variable names variables {a b : V} -- Variable names variables {vs : Type → Type} [vset vs V] -- Type of variable name sets variables {X X₁ X₂ Y Y₁ Y₂ Z : vs V} -- Variable name sets variables {R : X₁ ×ν Y₁} {S : X₂ ×ν Y₂} -- Variable name set relations variables {F : exp.subst X₁ X₂} {G : exp.subst Y₁ Y₂} -- Substitutions -- This is the type of a function that extends a variable name set relation `R` -- to an alpha-equality relation on `S` with the substitutions `F` and `G` -- applied to the left and right sides, respectively. @[reducible] protected def extend (F : exp.subst X₁ X₂) (G : exp.subst Y₁ Y₂) (R : X₁ ×ν Y₁) (S : X₂ ×ν Y₂) := ∀ (x : ν∈ X₁) (y : ν∈ Y₁), ⟪x, y⟫ ∈ν R → F x ≡α⟨S⟩ G y @[reducible] protected def extend.id (F : exp.subst X Y) : aeq.extend F F (vrel.id X) (vrel.id Y) := λ x₁ x₂ x₁_eq_x₂, by induction x₁_eq_x₂; exact aeq.refl (F x₁) -- A lemma needed for the `lam` case in `subst_preservation`. -- Paper: Lemma 5 lemma subst_preservation.update (nx₂ : ν∉ X₂) (ny₂ : ν∉ Y₂) : aeq.extend F G R S → aeq.extend (exp.subst.update a nx₂.1 F) (exp.subst.update b ny₂.1 G) (vrel.update a b R) (vrel.update nx₂.1 ny₂.1 S) := begin intros P x₁ y₁ H, cases H with H H, begin cases H with x₁_eq_a y₁_eq_b, simp [exp.subst.update], rw [dif_pos x₁_eq_a, dif_pos y₁_eq_b], apply var, left, split, reflexivity, reflexivity end, begin cases H with x₁_ne_a₁ H, cases H with y₁_ne_b₁ x₁_R_y₁, simp [exp.subst.update], rw [dif_neg x₁_ne_a₁, dif_neg y₁_ne_b₁], apply map (vset.prop_subset_insert_self _ _) (vset.prop_subset_insert_self _ _), begin intros x₂ y₂ c_S_d, cases x₂ with x₂ px₂, cases y₂ with y₂ py₂, existsi vset.prop_insert nx₂.1 px₂, existsi vset.prop_insert ny₂.1 py₂, right, existsi vset.prop_ne_if_mem_and_not_mem px₂ nx₂.2, existsi vset.prop_ne_if_mem_and_not_mem py₂ ny₂.2, exact c_S_d end, begin exact P (vname.erase x₁ x₁_ne_a₁) (vname.erase y₁ y₁_ne_b₁) x₁_R_y₁ end end end section ------------------------------------------------------------------------ variables {eX₁ : exp X₁} {eY₁ : exp Y₁} -- Expressions -- Substitution preserves alpha equality -- Paper: Proposition 4 theorem subst_preservation (F : exp.subst X₁ X₂) (G : exp.subst Y₁ Y₂) : aeq.extend F G R S → eX₁ ≡α⟨R⟩ eY₁ → exp.subst.apply F eX₁ ≡α⟨S⟩ exp.subst.apply G eY₁ := begin intros P H, induction H with /- var -/ X₁ Y₁ R x y x_R_y /- app -/ X₁ Y₁ R fX eX fY eY af ae rf re /- lam -/ X₁ Y₁ R x y eX eY a r generalizing X₂ Y₂ S F G P, begin /- var -/ exact P x y x_R_y end, begin /- app -/ exact app (rf F G P) (re F G P) end, begin /- lam -/ apply lam, exact r (exp.subst.update x (fresh X₂).1 F) (exp.subst.update y (fresh Y₂).1 G) (subst_preservation.update (fresh X₂) (fresh Y₂) P) end end end /- section -/ -------------------------------------------------------------- section ------------------------------------------------------------------------ variables {eX₁ eX₂ : exp X} -- Expressions -- Substitution preservation with the identity relation. protected theorem subst_preservation.id (F : exp.subst X Y) (G : exp.subst X Y) : aeq.extend F G (vrel.id X) (vrel.id Y) → eX₁ ≡α eX₂ → exp.subst.apply F eX₁ ≡α exp.subst.apply G eX₂ := subst_preservation F G -- Substitution preservation with the identity relation and one substitution. protected theorem subst_preservation.id₁ (F : exp.subst X Y) : eX₁ ≡α eX₂ → exp.subst.apply F eX₁ ≡α exp.subst.apply F eX₂ := aeq.subst_preservation.id F F (aeq.extend.id F) end /- section -/ -------------------------------------------------------------- end /- namespace -/ aeq -------------------------------------------------------- end /- namespace -/ acie -------------------------------------------------------
2402fa7dda9709ae2cc580ab4a5a9a3ab1a96fda
08bd4ba4ca87dba1f09d2c96a26f5d65da81f4b4
/src/Lean/Parser/Attr.lean
bd3042d4885dfc1f247713502bf43bb2a0e8a15b
[ "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", "Apache-2.0", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
gebner/lean4
d51c4922640a52a6f7426536ea669ef18a1d9af5
8cd9ce06843c9d42d6d6dc43d3e81e3b49dfc20f
refs/heads/master
1,685,732,780,391
1,672,962,627,000
1,673,459,398,000
373,307,283
0
0
Apache-2.0
1,691,316,730,000
1,622,669,271,000
Lean
UTF-8
Lean
false
false
2,189
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Parser.Basic import Lean.Parser.Extra namespace Lean.Parser builtin_initialize registerBuiltinParserAttribute `builtin_prio_parser ``Category.prio .both registerBuiltinDynamicParserAttribute `prio_parser `prio builtin_initialize registerBuiltinParserAttribute `builtin_attr_parser ``Category.attr .symbol registerBuiltinDynamicParserAttribute `attr_parser `attr @[inline] def priorityParser (rbp : Nat := 0) : Parser := categoryParser `prio rbp @[inline] def attrParser (rbp : Nat := 0) : Parser := categoryParser `attr rbp attribute [run_builtin_parser_attribute_hooks] priorityParser attrParser namespace Priority @[builtin_prio_parser] def numPrio := checkPrec maxPrec >> numLit attribute [run_builtin_parser_attribute_hooks] numPrio end Priority namespace Attr @[builtin_attr_parser] def simple := leading_parser ident >> optional (priorityParser <|> ident) /- Remark, We can't use `simple` for `class`, `instance`, `export`, and `macro` because they are keywords. -/ @[builtin_attr_parser] def «macro» := leading_parser "macro " >> ident @[builtin_attr_parser] def «export» := leading_parser "export " >> ident /- We don't use `simple` for recursor because the argument is not a priority -/ @[builtin_attr_parser] def recursor := leading_parser nonReservedSymbol "recursor " >> numLit @[builtin_attr_parser] def «class» := leading_parser "class" @[builtin_attr_parser] def «instance» := leading_parser "instance" >> optional priorityParser @[builtin_attr_parser] def default_instance := leading_parser nonReservedSymbol "default_instance " >> optional priorityParser @[builtin_attr_parser] def «specialize» := leading_parser (nonReservedSymbol "specialize") >> many (ident <|> numLit) def externEntry := leading_parser optional ident >> optional (nonReservedSymbol "inline ") >> strLit @[builtin_attr_parser] def extern := leading_parser nonReservedSymbol "extern " >> optional numLit >> many externEntry end Attr end Lean.Parser
6f2b550506028689d6ba0b35073ff70af47ab714
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/jpCasesDiscrM.lean
6bdced3a7b166497ad17dd4e11c46c6ad157d50c
[ "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
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
752
lean
set_option trace.Compiler.saveBase true in def f1 (c : Bool) (a b : Nat) := let k d y z := match d with | true => y + z + z*y | false => z + y match c with | true => k true a b | false => k false b a set_option trace.Compiler.saveBase true in def f2 (c : Bool) (a b : Nat) := let k d y z := match d with | true => y + z + z*y | false => z + y match c with | true => k c a b | false => k c b a inductive C where | c1 | c2 | c3 | c4 set_option trace.Compiler.saveBase true in def f3 (c c' : C) (a b : Nat) := let k y z (d : C) := match d with | C.c1 => y + z + z*y | C.c3 => y*y+a | _ => z + y + y match c with | .c1 => k a b c | .c2 => k b b c | .c3 => k b a c' | .c4 => k a a c'
98583b03cc19afb2e6b6f22b41220b5c85344e81
c8af905dcd8475f414868d303b2eb0e9d3eb32f9
/examples/synthesis-degradation.lean
ae1e6e1aad126f7c7551467bff463b938a9876d5
[ "BSD-3-Clause" ]
permissive
continuouspi/lean-cpi
81480a13842d67ff5f3698643210d8ed5dd08de4
443bf2cb236feadc45a01387099c236ab2b78237
refs/heads/master
1,650,307,316,582
1,587,033,364,000
1,587,033,364,000
207,499,661
1
0
null
null
null
null
UTF-8
Lean
false
false
1,369
lean
import .common open cpi open cpi.species open_locale normalise def k_ambient : ℍ := fin_poly.X "k₀" def k_react : ℍ := fin_poly.X "k₁" def k_degrade : ℍ := fin_poly.X "k₂" def aff : affinity ℍ := ∅ def ω : context := context.extend 0 (context.extend 0 (context.extend 0 context.nil)) def Γ : context := context.extend aff.arity context.nil @[pattern] def A : reference 0 ω := reference.zero 0 @[pattern] def S : reference 0 ω := reference.extend $ reference.zero 0 @[pattern] def R : reference 0 ω := reference.extend ∘ reference.extend $ reference.zero 0 -- A = τ@k₀(A|R) def A_ : choices ℍ ω Γ := τ@k_ambient ⬝' (apply A ∅ |ₛ apply R ∅) -- S = τ@k₁.(S|R) def S_ : choices ℍ ω Γ := τ@k_react ⬝' (apply S ∅ |ₛ apply R ∅) -- R = τ@k₂. 0 def R_ : choices ℍ ω Γ := τ@k_degrade ⬝' nil def ℓ : lookup ℍ ω Γ | _ A := species.rename name.extend A_ | _ S := species.rename name.extend S_ | _ R := species.rename name.extend R_ | (nat.succ n) (reference.extend (reference.extend a)) := by { cases a, cases a_a } def system : process ℂ ℍ ω Γ := 1 ◯ (apply A ∅) |ₚ fin_poly.X "S" ◯ (apply S ∅) |ₚ fin_poly.X "R" ◯ (apply R ∅) #eval process_immediate aff ℓ conc system /- (-1•(R•k₂) + 1•(S•k₁) + 1•(k₀)) • 2([]) ⇒ dR/dt = k₀ + k₁S -k₂R -/
73b01b1763683f9df91db3ea90dd4a22a2d0b76a
6094e25ea0b7699e642463b48e51b2ead6ddc23f
/library/algebra/lattice.lean
c01980753fd1214a85de31237d1d2cd5fc1477a2
[ "Apache-2.0" ]
permissive
gbaz/lean
a7835c4e3006fbbb079e8f8ffe18aacc45adebfb
a501c308be3acaa50a2c0610ce2e0d71becf8032
refs/heads/master
1,611,198,791,433
1,451,339,111,000
1,451,339,111,000
48,713,797
0
0
null
1,451,338,939,000
1,451,338,939,000
null
UTF-8
Lean
false
false
4,067
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad -/ import .order variable {A : Type} /- lattices (we could split this to upper- and lower-semilattices, if needed) -/ structure lattice [class] (A : Type) extends weak_order A := (inf : A → A → A) (sup : A → A → A) (inf_le_left : ∀ a b, le (inf a b) a) (inf_le_right : ∀ a b, le (inf a b) b) (le_inf : ∀a b c, le c a → le c b → le c (inf a b)) (le_sup_left : ∀ a b, le a (sup a b)) (le_sup_right : ∀ a b, le b (sup a b)) (sup_le : ∀ a b c, le a c → le b c → le (sup a b) c) definition inf := @lattice.inf definition sup := @lattice.sup infix ` ⊓ `:70 := inf infix ` ⊔ `:65 := sup section variable [s : lattice A] include s theorem inf_le_left (a b : A) : a ⊓ b ≤ a := !lattice.inf_le_left theorem inf_le_right (a b : A) : a ⊓ b ≤ b := !lattice.inf_le_right theorem le_inf {a b c : A} (H₁ : c ≤ a) (H₂ : c ≤ b) : c ≤ a ⊓ b := !lattice.le_inf H₁ H₂ theorem le_sup_left (a b : A) : a ≤ a ⊔ b := !lattice.le_sup_left theorem le_sup_right (a b : A) : b ≤ a ⊔ b := !lattice.le_sup_right theorem sup_le {a b c : A} (H₁ : a ≤ c) (H₂ : b ≤ c) : a ⊔ b ≤ c := !lattice.sup_le H₁ H₂ /- inf -/ theorem eq_inf {a b c : A} (H₁ : c ≤ a) (H₂ : c ≤ b) (H₃ : ∀{d}, d ≤ a → d ≤ b → d ≤ c) : c = a ⊓ b := le.antisymm (le_inf H₁ H₂) (H₃ !inf_le_left !inf_le_right) theorem inf.comm (a b : A) : a ⊓ b = b ⊓ a := eq_inf !inf_le_right !inf_le_left (λ c H₁ H₂, le_inf H₂ H₁) theorem inf.assoc (a b c : A) : (a ⊓ b) ⊓ c = a ⊓ (b ⊓ c) := begin apply eq_inf, { apply le.trans, apply inf_le_left, apply inf_le_left }, { apply le_inf, apply le.trans, apply inf_le_left, apply inf_le_right, apply inf_le_right }, { intros [d, H₁, H₂], apply le_inf, apply le_inf H₁, apply le.trans H₂, apply inf_le_left, apply le.trans H₂, apply inf_le_right } end theorem inf.left_comm (a b c : A) : a ⊓ (b ⊓ c) = b ⊓ (a ⊓ c) := binary.left_comm (@inf.comm A s) (@inf.assoc A s) a b c theorem inf.right_comm (a b c : A) : (a ⊓ b) ⊓ c = (a ⊓ c) ⊓ b := binary.right_comm (@inf.comm A s) (@inf.assoc A s) a b c theorem inf_self (a : A) : a ⊓ a = a := by apply eq.symm; apply eq_inf (le.refl a) !le.refl; intros; assumption theorem inf_eq_left {a b : A} (H : a ≤ b) : a ⊓ b = a := by apply eq.symm; apply eq_inf !le.refl H; intros; assumption theorem inf_eq_right {a b : A} (H : b ≤ a) : a ⊓ b = b := eq.subst !inf.comm (inf_eq_left H) /- sup -/ theorem eq_sup {a b c : A} (H₁ : a ≤ c) (H₂ : b ≤ c) (H₃ : ∀{d}, a ≤ d → b ≤ d → c ≤ d) : c = a ⊔ b := le.antisymm (H₃ !le_sup_left !le_sup_right) (sup_le H₁ H₂) theorem sup.comm (a b : A) : a ⊔ b = b ⊔ a := eq_sup !le_sup_right !le_sup_left (λ c H₁ H₂, sup_le H₂ H₁) theorem sup.assoc (a b c : A) : (a ⊔ b) ⊔ c = a ⊔ (b ⊔ c) := begin apply eq_sup, { apply le.trans, apply le_sup_left a b, apply le_sup_left }, { apply sup_le, apply le.trans, apply le_sup_right a b, apply le_sup_left, apply le_sup_right }, { intros [d, H₁, H₂], apply sup_le, apply sup_le H₁, apply le.trans !le_sup_left H₂, apply le.trans !le_sup_right H₂} end theorem sup.left_comm (a b c : A) : a ⊔ (b ⊔ c) = b ⊔ (a ⊔ c) := binary.left_comm (@sup.comm A s) (@sup.assoc A s) a b c theorem sup.right_comm (a b c : A) : (a ⊔ b) ⊔ c = (a ⊔ c) ⊔ b := binary.right_comm (@sup.comm A s) (@sup.assoc A s) a b c theorem sup_self (a : A) : a ⊔ a = a := by apply eq.symm; apply eq_sup (le.refl a) !le.refl; intros; assumption theorem sup_eq_left {a b : A} (H : b ≤ a) : a ⊔ b = a := by apply eq.symm; apply eq_sup !le.refl H; intros; assumption theorem sup_eq_right {a b : A} (H : a ≤ b) : a ⊔ b = b := eq.subst !sup.comm (sup_eq_left H) end
943b597e8b30247a64dfda04d68a5c422c07a553
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/logic/equiv/set.lean
ce3c95ec44b439f8fa9e1cb0f1a31db50e2f8d8e
[ "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
25,443
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro -/ import data.set.function import logic.equiv.defs /-! # Equivalences and sets > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we provide lemmas linking equivalences to sets. Some notable definitions are: * `equiv.of_injective`: an injective function is (noncomputably) equivalent to its range. * `equiv.set_congr`: two equal sets are equivalent as types. * `equiv.set.union`: a disjoint union of sets is equivalent to their `sum`. This file is separate from `equiv/basic` such that we do not require the full lattice structure on sets before defining what an equivalence is. -/ open function set universes u v w z variables {α : Sort u} {β : Sort v} {γ : Sort w} namespace equiv @[simp] lemma range_eq_univ {α : Type*} {β : Type*} (e : α ≃ β) : range e = univ := eq_univ_of_forall e.surjective protected lemma image_eq_preimage {α β} (e : α ≃ β) (s : set α) : e '' s = e.symm ⁻¹' s := set.ext $ λ x, mem_image_iff_of_inverse e.left_inv e.right_inv lemma _root_.set.mem_image_equiv {α β} {S : set α} {f : α ≃ β} {x : β} : x ∈ f '' S ↔ f.symm x ∈ S := set.ext_iff.mp (f.image_eq_preimage S) x /-- Alias for `equiv.image_eq_preimage` -/ lemma _root_.set.image_equiv_eq_preimage_symm {α β} (S : set α) (f : α ≃ β) : f '' S = f.symm ⁻¹' S := f.image_eq_preimage S /-- Alias for `equiv.image_eq_preimage` -/ lemma _root_.set.preimage_equiv_eq_image_symm {α β} (S : set α) (f : β ≃ α) : f ⁻¹' S = f.symm '' S := (f.symm.image_eq_preimage S).symm @[simp] protected lemma subset_image {α β} (e : α ≃ β) (s : set α) (t : set β) : e.symm '' t ⊆ s ↔ t ⊆ e '' s := by rw [image_subset_iff, e.image_eq_preimage] @[simp] protected lemma subset_image' {α β} (e : α ≃ β) (s : set α) (t : set β) : s ⊆ e.symm '' t ↔ e '' s ⊆ t := calc s ⊆ e.symm '' t ↔ e.symm.symm '' s ⊆ t : by rw e.symm.subset_image ... ↔ e '' s ⊆ t : by rw e.symm_symm @[simp] lemma symm_image_image {α β} (e : α ≃ β) (s : set α) : e.symm '' (e '' s) = s := e.left_inverse_symm.image_image s lemma eq_image_iff_symm_image_eq {α β} (e : α ≃ β) (s : set α) (t : set β) : t = e '' s ↔ e.symm '' t = s := (e.symm.injective.image_injective.eq_iff' (e.symm_image_image s)).symm @[simp] lemma image_symm_image {α β} (e : α ≃ β) (s : set β) : e '' (e.symm '' s) = s := e.symm.symm_image_image s @[simp] lemma image_preimage {α β} (e : α ≃ β) (s : set β) : e '' (e ⁻¹' s) = s := e.surjective.image_preimage s @[simp] lemma preimage_image {α β} (e : α ≃ β) (s : set α) : e ⁻¹' (e '' s) = s := e.injective.preimage_image s protected lemma image_compl {α β} (f : equiv α β) (s : set α) : f '' sᶜ = (f '' s)ᶜ := image_compl_eq f.bijective @[simp] lemma symm_preimage_preimage {α β} (e : α ≃ β) (s : set β) : e.symm ⁻¹' (e ⁻¹' s) = s := e.right_inverse_symm.preimage_preimage s @[simp] lemma preimage_symm_preimage {α β} (e : α ≃ β) (s : set α) : e ⁻¹' (e.symm ⁻¹' s) = s := e.left_inverse_symm.preimage_preimage s @[simp] lemma preimage_subset {α β} (e : α ≃ β) (s t : set β) : e ⁻¹' s ⊆ e ⁻¹' t ↔ s ⊆ t := e.surjective.preimage_subset_preimage_iff @[simp] lemma image_subset {α β} (e : α ≃ β) (s t : set α) : e '' s ⊆ e '' t ↔ s ⊆ t := image_subset_image_iff e.injective @[simp] lemma image_eq_iff_eq {α β} (e : α ≃ β) (s t : set α) : e '' s = e '' t ↔ s = t := image_eq_image e.injective lemma preimage_eq_iff_eq_image {α β} (e : α ≃ β) (s t) : e ⁻¹' s = t ↔ s = e '' t := preimage_eq_iff_eq_image e.bijective lemma eq_preimage_iff_image_eq {α β} (e : α ≃ β) (s t) : s = e ⁻¹' t ↔ e '' s = t := eq_preimage_iff_image_eq e.bijective @[simp] lemma prod_assoc_preimage {α β γ} {s : set α} {t : set β} {u : set γ} : equiv.prod_assoc α β γ ⁻¹' s ×ˢ (t ×ˢ u) = (s ×ˢ t) ×ˢ u := by { ext, simp [and_assoc] } @[simp] lemma prod_assoc_symm_preimage {α β γ} {s : set α} {t : set β} {u : set γ} : (equiv.prod_assoc α β γ).symm ⁻¹' (s ×ˢ t) ×ˢ u = s ×ˢ (t ×ˢ u) := by { ext, simp [and_assoc] } -- `@[simp]` doesn't like these lemmas, as it uses `set.image_congr'` to turn `equiv.prod_assoc` -- into a lambda expression and then unfold it. lemma prod_assoc_image {α β γ} {s : set α} {t : set β} {u : set γ} : equiv.prod_assoc α β γ '' (s ×ˢ t) ×ˢ u = s ×ˢ (t ×ˢ u) := by simpa only [equiv.image_eq_preimage] using prod_assoc_symm_preimage lemma prod_assoc_symm_image {α β γ} {s : set α} {t : set β} {u : set γ} : (equiv.prod_assoc α β γ).symm '' s ×ˢ (t ×ˢ u) = (s ×ˢ t) ×ˢ u := by simpa only [equiv.image_eq_preimage] using prod_assoc_preimage /-- A set `s` in `α × β` is equivalent to the sigma-type `Σ x, {y | (x, y) ∈ s}`. -/ def set_prod_equiv_sigma {α β : Type*} (s : set (α × β)) : s ≃ Σ x : α, {y | (x, y) ∈ s} := { to_fun := λ x, ⟨x.1.1, x.1.2, by simp⟩, inv_fun := λ x, ⟨(x.1, x.2.1), x.2.2⟩, left_inv := λ ⟨⟨x, y⟩, h⟩, rfl, right_inv := λ ⟨x, y, h⟩, rfl } /-- The subtypes corresponding to equal sets are equivalent. -/ @[simps apply] def set_congr {α : Type*} {s t : set α} (h : s = t) : s ≃ t := subtype_equiv_prop h /-- A set is equivalent to its image under an equivalence. -/ -- We could construct this using `equiv.set.image e s e.injective`, -- but this definition provides an explicit inverse. @[simps] def image {α β : Type*} (e : α ≃ β) (s : set α) : s ≃ e '' s := { to_fun := λ x, ⟨e x.1, by simp⟩, inv_fun := λ y, ⟨e.symm y.1, by { rcases y with ⟨-, ⟨a, ⟨m, rfl⟩⟩⟩, simpa using m, }⟩, left_inv := λ x, by simp, right_inv := λ y, by simp, }. namespace set /-- `univ α` is equivalent to `α`. -/ @[simps apply symm_apply] protected def univ (α) : @univ α ≃ α := ⟨coe, λ a, ⟨a, trivial⟩, λ ⟨a, _⟩, rfl, λ a, rfl⟩ /-- An empty set is equivalent to the `empty` type. -/ protected def empty (α) : (∅ : set α) ≃ empty := equiv_empty _ /-- An empty set is equivalent to a `pempty` type. -/ protected def pempty (α) : (∅ : set α) ≃ pempty := equiv_pempty _ /-- If sets `s` and `t` are separated by a decidable predicate, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union' {α} {s t : set α} (p : α → Prop) [decidable_pred p] (hs : ∀ x ∈ s, p x) (ht : ∀ x ∈ t, ¬ p x) : (s ∪ t : set α) ≃ s ⊕ t := { to_fun := λ x, if hp : p x then sum.inl ⟨_, x.2.resolve_right (λ xt, ht _ xt hp)⟩ else sum.inr ⟨_, x.2.resolve_left (λ xs, hp (hs _ xs))⟩, inv_fun := λ o, match o with | (sum.inl x) := ⟨x, or.inl x.2⟩ | (sum.inr x) := ⟨x, or.inr x.2⟩ end, left_inv := λ ⟨x, h'⟩, by by_cases p x; simp [union'._match_1, h]; congr, right_inv := λ o, begin rcases o with ⟨x, h⟩ | ⟨x, h⟩; dsimp [union'._match_1]; [simp [hs _ h], simp [ht _ h]] end } /-- If sets `s` and `t` are disjoint, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) : (s ∪ t : set α) ≃ s ⊕ t := set.union' (λ x, x ∈ s) (λ _, id) (λ x xt xs, H ⟨xs, xt⟩) lemma union_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) {a : (s ∪ t : set α)} (ha : ↑a ∈ s) : equiv.set.union H a = sum.inl ⟨a, ha⟩ := dif_pos ha lemma union_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) {a : (s ∪ t : set α)} (ha : ↑a ∈ t) : equiv.set.union H a = sum.inr ⟨a, ha⟩ := dif_neg $ λ h, H ⟨h, ha⟩ @[simp] lemma union_symm_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) (a : s) : (equiv.set.union H).symm (sum.inl a) = ⟨a, subset_union_left _ _ a.2⟩ := rfl @[simp] lemma union_symm_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) (a : t) : (equiv.set.union H).symm (sum.inr a) = ⟨a, subset_union_right _ _ a.2⟩ := rfl /-- A singleton set is equivalent to a `punit` type. -/ protected def singleton {α} (a : α) : ({a} : set α) ≃ punit.{u} := ⟨λ _, punit.star, λ _, ⟨a, mem_singleton _⟩, λ ⟨x, h⟩, by { simp at h, subst x }, λ ⟨⟩, rfl⟩ /-- Equal sets are equivalent. TODO: this is the same as `equiv.set_congr`! -/ @[simps apply symm_apply] protected def of_eq {α : Type u} {s t : set α} (h : s = t) : s ≃ t := equiv.set_congr h /-- If `a ∉ s`, then `insert a s` is equivalent to `s ⊕ punit`. -/ protected def insert {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) : (insert a s : set α) ≃ s ⊕ punit.{u+1} := calc (insert a s : set α) ≃ ↥(s ∪ {a}) : equiv.set.of_eq (by simp) ... ≃ s ⊕ ({a} : set α) : equiv.set.union (λ x ⟨hx, hx'⟩, by simp [*] at *) ... ≃ s ⊕ punit.{u+1} : sum_congr (equiv.refl _) (equiv.set.singleton _) @[simp] lemma insert_symm_apply_inl {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) (b : s) : (equiv.set.insert H).symm (sum.inl b) = ⟨b, or.inr b.2⟩ := rfl @[simp] lemma insert_symm_apply_inr {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) (b : punit.{u+1}) : (equiv.set.insert H).symm (sum.inr b) = ⟨a, or.inl rfl⟩ := rfl @[simp] lemma insert_apply_left {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) : equiv.set.insert H ⟨a, or.inl rfl⟩ = sum.inr punit.star := (equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl @[simp] lemma insert_apply_right {α} {s : set.{u} α} [decidable_pred (∈ s)] {a : α} (H : a ∉ s) (b : s) : equiv.set.insert H ⟨b, or.inr b.2⟩ = sum.inl b := (equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl /-- If `s : set α` is a set with decidable membership, then `s ⊕ sᶜ` is equivalent to `α`. -/ protected def sum_compl {α} (s : set α) [decidable_pred (∈ s)] : s ⊕ (sᶜ : set α) ≃ α := calc s ⊕ (sᶜ : set α) ≃ ↥(s ∪ sᶜ) : (equiv.set.union (by simp [set.ext_iff])).symm ... ≃ @univ α : equiv.set.of_eq (by simp) ... ≃ α : equiv.set.univ _ @[simp] lemma sum_compl_apply_inl {α : Type u} (s : set α) [decidable_pred (∈ s)] (x : s) : equiv.set.sum_compl s (sum.inl x) = x := rfl @[simp] lemma sum_compl_apply_inr {α : Type u} (s : set α) [decidable_pred (∈ s)] (x : sᶜ) : equiv.set.sum_compl s (sum.inr x) = x := rfl lemma sum_compl_symm_apply_of_mem {α : Type u} {s : set α} [decidable_pred (∈ s)] {x : α} (hx : x ∈ s) : (equiv.set.sum_compl s).symm x = sum.inl ⟨x, hx⟩ := have ↑(⟨x, or.inl hx⟩ : (s ∪ sᶜ : set α)) ∈ s, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_left _ this } lemma sum_compl_symm_apply_of_not_mem {α : Type u} {s : set α} [decidable_pred (∈ s)] {x : α} (hx : x ∉ s) : (equiv.set.sum_compl s).symm x = sum.inr ⟨x, hx⟩ := have ↑(⟨x, or.inr hx⟩ : (s ∪ sᶜ : set α)) ∈ sᶜ, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_right _ this } @[simp] lemma sum_compl_symm_apply {α : Type*} {s : set α} [decidable_pred (∈ s)] {x : s} : (equiv.set.sum_compl s).symm x = sum.inl x := by cases x with x hx; exact set.sum_compl_symm_apply_of_mem hx @[simp] lemma sum_compl_symm_apply_compl {α : Type*} {s : set α} [decidable_pred (∈ s)] {x : sᶜ} : (equiv.set.sum_compl s).symm x = sum.inr x := by cases x with x hx; exact set.sum_compl_symm_apply_of_not_mem hx /-- `sum_diff_subset s t` is the natural equivalence between `s ⊕ (t \ s)` and `t`, where `s` and `t` are two sets. -/ protected def sum_diff_subset {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] : s ⊕ (t \ s : set α) ≃ t := calc s ⊕ (t \ s : set α) ≃ (s ∪ (t \ s) : set α) : (equiv.set.union (by simp [inter_diff_self])).symm ... ≃ t : equiv.set.of_eq (by { simp [union_diff_self, union_eq_self_of_subset_left h] }) @[simp] lemma sum_diff_subset_apply_inl {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] (x : s) : equiv.set.sum_diff_subset h (sum.inl x) = inclusion h x := rfl @[simp] lemma sum_diff_subset_apply_inr {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] (x : t \ s) : equiv.set.sum_diff_subset h (sum.inr x) = inclusion (diff_subset t s) x := rfl lemma sum_diff_subset_symm_apply_of_mem {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] {x : t} (hx : x.1 ∈ s) : (equiv.set.sum_diff_subset h).symm x = sum.inl ⟨x, hx⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inl], exact subtype.eq rfl, end lemma sum_diff_subset_symm_apply_of_not_mem {α} {s t : set α} (h : s ⊆ t) [decidable_pred (∈ s)] {x : t} (hx : x.1 ∉ s) : (equiv.set.sum_diff_subset h).symm x = sum.inr ⟨x, ⟨x.2, hx⟩⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inr], exact subtype.eq rfl, end /-- If `s` is a set with decidable membership, then the sum of `s ∪ t` and `s ∩ t` is equivalent to `s ⊕ t`. -/ protected def union_sum_inter {α : Type u} (s t : set α) [decidable_pred (∈ s)] : (s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ s ⊕ t := calc (s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ (s ∪ t \ s : set α) ⊕ (s ∩ t : set α) : by rw [union_diff_self] ... ≃ (s ⊕ (t \ s : set α)) ⊕ (s ∩ t : set α) : sum_congr (set.union $ subset_empty_iff.2 (inter_diff_self _ _)) (equiv.refl _) ... ≃ s ⊕ (t \ s : set α) ⊕ (s ∩ t : set α) : sum_assoc _ _ _ ... ≃ s ⊕ (t \ s ∪ s ∩ t : set α) : sum_congr (equiv.refl _) begin refine (set.union' (∉ s) _ _).symm, exacts [λ x hx, hx.2, λ x hx, not_not_intro hx.1] end ... ≃ s ⊕ t : by { rw (_ : t \ s ∪ s ∩ t = t), rw [union_comm, inter_comm, inter_union_diff] } /-- Given an equivalence `e₀` between sets `s : set α` and `t : set β`, the set of equivalences `e : α ≃ β` such that `e ↑x = ↑(e₀ x)` for each `x : s` is equivalent to the set of equivalences between `sᶜ` and `tᶜ`. -/ protected def compl {α : Type u} {β : Type v} {s : set α} {t : set β} [decidable_pred (∈ s)] [decidable_pred (∈ t)] (e₀ : s ≃ t) : {e : α ≃ β // ∀ x : s, e x = e₀ x} ≃ ((sᶜ : set α) ≃ (tᶜ : set β)) := { to_fun := λ e, subtype_equiv e (λ a, not_congr $ iff.symm $ maps_to.mem_iff (maps_to_iff_exists_map_subtype.2 ⟨e₀, e.2⟩) (surj_on.maps_to_compl (surj_on_iff_exists_map_subtype.2 ⟨t, e₀, subset.refl t, e₀.surjective, e.2⟩) e.1.injective)), inv_fun := λ e₁, subtype.mk (calc α ≃ s ⊕ (sᶜ : set α) : (set.sum_compl s).symm ... ≃ t ⊕ (tᶜ : set β) : e₀.sum_congr e₁ ... ≃ β : set.sum_compl t) (λ x, by simp only [sum.map_inl, trans_apply, sum_congr_apply, set.sum_compl_apply_inl, set.sum_compl_symm_apply]), left_inv := λ e, begin ext x, by_cases hx : x ∈ s, { simp only [set.sum_compl_symm_apply_of_mem hx, ←e.prop ⟨x, hx⟩, sum.map_inl, sum_congr_apply, trans_apply, subtype.coe_mk, set.sum_compl_apply_inl] }, { simp only [set.sum_compl_symm_apply_of_not_mem hx, sum.map_inr, subtype_equiv_apply, set.sum_compl_apply_inr, trans_apply, sum_congr_apply, subtype.coe_mk] }, end, right_inv := λ e, equiv.ext $ λ x, by simp only [sum.map_inr, subtype_equiv_apply, set.sum_compl_apply_inr, function.comp_app, sum_congr_apply, equiv.coe_trans, subtype.coe_eta, subtype.coe_mk, set.sum_compl_symm_apply_compl] } /-- The set product of two sets is equivalent to the type product of their coercions to types. -/ protected def prod {α β} (s : set α) (t : set β) : ↥(s ×ˢ t) ≃ s × t := @subtype_prod_equiv_prod α β s t /-- The set `set.pi set.univ s` is equivalent to `Π a, s a`. -/ @[simps] protected def univ_pi {α : Type*} {β : α → Type*} (s : Π a, set (β a)) : pi univ s ≃ Π a, s a := { to_fun := λ f a, ⟨(f : Π a, β a) a, f.2 a (mem_univ a)⟩, inv_fun := λ f, ⟨λ a, f a, λ a ha, (f a).2⟩, left_inv := λ ⟨f, hf⟩, by { ext a, refl }, right_inv := λ f, by { ext a, refl } } /-- If a function `f` is injective on a set `s`, then `s` is equivalent to `f '' s`. -/ protected noncomputable def image_of_inj_on {α β} (f : α → β) (s : set α) (H : inj_on f s) : s ≃ (f '' s) := ⟨λ p, ⟨f p, mem_image_of_mem f p.2⟩, λ p, ⟨classical.some p.2, (classical.some_spec p.2).1⟩, λ ⟨x, h⟩, subtype.eq (H (classical.some_spec (mem_image_of_mem f h)).1 h (classical.some_spec (mem_image_of_mem f h)).2), λ ⟨y, h⟩, subtype.eq (classical.some_spec h).2⟩ /-- If `f` is an injective function, then `s` is equivalent to `f '' s`. -/ @[simps apply] protected noncomputable def image {α β} (f : α → β) (s : set α) (H : injective f) : s ≃ (f '' s) := equiv.set.image_of_inj_on f s (H.inj_on s) @[simp] protected lemma image_symm_apply {α β} (f : α → β) (s : set α) (H : injective f) (x : α) (h : x ∈ s) : (set.image f s H).symm ⟨f x, ⟨x, ⟨h, rfl⟩⟩⟩ = ⟨x, h⟩ := begin apply (set.image f s H).injective, simp [(set.image f s H).apply_symm_apply], end lemma image_symm_preimage {α β} {f : α → β} (hf : injective f) (u s : set α) : (λ x, (set.image f s hf).symm x : f '' s → α) ⁻¹' u = coe ⁻¹' (f '' u) := begin ext ⟨b, a, has, rfl⟩, have : ∀(h : ∃a', a' ∈ s ∧ a' = a), classical.some h = a := λ h, (classical.some_spec h).2, simp [equiv.set.image, equiv.set.image_of_inj_on, hf.eq_iff, this], end /-- If `α` is equivalent to `β`, then `set α` is equivalent to `set β`. -/ @[simps] protected def congr {α β : Type*} (e : α ≃ β) : set α ≃ set β := ⟨λ s, e '' s, λ t, e.symm '' t, symm_image_image e, symm_image_image e.symm⟩ /-- The set `{x ∈ s | t x}` is equivalent to the set of `x : s` such that `t x`. -/ protected def sep {α : Type u} (s : set α) (t : α → Prop) : ({ x ∈ s | t x } : set α) ≃ { x : s | t x } := (equiv.subtype_subtype_equiv_subtype_inter s t).symm /-- The set `𝒫 S := {x | x ⊆ S}` is equivalent to the type `set S`. -/ protected def powerset {α} (S : set α) : 𝒫 S ≃ set S := { to_fun := λ x : 𝒫 S, coe ⁻¹' (x : set α), inv_fun := λ x : set S, ⟨coe '' x, by rintro _ ⟨a : S, _, rfl⟩; exact a.2⟩, left_inv := λ x, by ext y; exact ⟨λ ⟨⟨_, _⟩, h, rfl⟩, h, λ h, ⟨⟨_, x.2 h⟩, h, rfl⟩⟩, right_inv := λ x, by ext; simp } /-- If `s` is a set in `range f`, then its image under `range_splitting f` is in bijection (via `f`) with `s`. -/ @[simps] noncomputable def range_splitting_image_equiv {α β : Type*} (f : α → β) (s : set (range f)) : range_splitting f '' s ≃ s := { to_fun := λ x, ⟨⟨f x, by simp⟩, (by { rcases x with ⟨x, ⟨y, ⟨m, rfl⟩⟩⟩, simpa [apply_range_splitting f] using m, })⟩, inv_fun := λ x, ⟨range_splitting f x, ⟨x, ⟨x.2, rfl⟩⟩⟩, left_inv := λ x, by { rcases x with ⟨x, ⟨y, ⟨m, rfl⟩⟩⟩, simp [apply_range_splitting f] }, right_inv := λ x, by simp [apply_range_splitting f], } end set /-- If `f : α → β` has a left-inverse when `α` is nonempty, then `α` is computably equivalent to the range of `f`. While awkward, the `nonempty α` hypothesis on `f_inv` and `hf` allows this to be used when `α` is empty too. This hypothesis is absent on analogous definitions on stronger `equiv`s like `linear_equiv.of_left_inverse` and `ring_equiv.of_left_inverse` as their typeclass assumptions are already sufficient to ensure non-emptiness. -/ @[simps] def of_left_inverse {α β : Sort*} (f : α → β) (f_inv : nonempty α → β → α) (hf : Π h : nonempty α, left_inverse (f_inv h) f) : α ≃ range f := { to_fun := λ a, ⟨f a, a, rfl⟩, inv_fun := λ b, f_inv (nonempty_of_exists b.2) b, left_inv := λ a, hf ⟨a⟩ a, right_inv := λ ⟨b, a, ha⟩, subtype.eq $ show f (f_inv ⟨a⟩ b) = b, from eq.trans (congr_arg f $ by exact ha ▸ (hf _ a)) ha } /-- If `f : α → β` has a left-inverse, then `α` is computably equivalent to the range of `f`. Note that if `α` is empty, no such `f_inv` exists and so this definition can't be used, unlike the stronger but less convenient `of_left_inverse`. -/ abbreviation of_left_inverse' {α β : Sort*} (f : α → β) (f_inv : β → α) (hf : left_inverse f_inv f) : α ≃ range f := of_left_inverse f (λ _, f_inv) (λ _, hf) /-- If `f : α → β` is an injective function, then domain `α` is equivalent to the range of `f`. -/ @[simps apply] noncomputable def of_injective {α β} (f : α → β) (hf : injective f) : α ≃ range f := equiv.of_left_inverse f (λ h, by exactI function.inv_fun f) (λ h, by exactI function.left_inverse_inv_fun hf) theorem apply_of_injective_symm {α β} {f : α → β} (hf : injective f) (b : range f) : f ((of_injective f hf).symm b) = b := subtype.ext_iff.1 $ (of_injective f hf).apply_symm_apply b @[simp] theorem of_injective_symm_apply {α β} {f : α → β} (hf : injective f) (a : α) : (of_injective f hf).symm ⟨f a, ⟨a, rfl⟩⟩ = a := begin apply (of_injective f hf).injective, simp [apply_of_injective_symm hf], end lemma coe_of_injective_symm {α β} {f : α → β} (hf : injective f) : ((of_injective f hf).symm : range f → α) = range_splitting f := by { ext ⟨y, x, rfl⟩, apply hf, simp [apply_range_splitting f] } @[simp] lemma self_comp_of_injective_symm {α β} {f : α → β} (hf : injective f) : f ∘ ((of_injective f hf).symm) = coe := funext (λ x, apply_of_injective_symm hf x) lemma of_left_inverse_eq_of_injective {α β : Type*} (f : α → β) (f_inv : nonempty α → β → α) (hf : Π h : nonempty α, left_inverse (f_inv h) f) : of_left_inverse f f_inv hf = of_injective f ((em (nonempty α)).elim (λ h, (hf h).injective) (λ h _ _ _, by { haveI : subsingleton α := subsingleton_of_not_nonempty h, simp })) := by { ext, simp } lemma of_left_inverse'_eq_of_injective {α β : Type*} (f : α → β) (f_inv : β → α) (hf : left_inverse f_inv f) : of_left_inverse' f f_inv hf = of_injective f hf.injective := by { ext, simp } protected lemma set_forall_iff {α β} (e : α ≃ β) {p : set α → Prop} : (∀ a, p a) ↔ (∀ a, p (e ⁻¹' a)) := e.injective.preimage_surjective.forall lemma preimage_pi_equiv_pi_subtype_prod_symm_pi {α : Type*} {β : α → Type*} (p : α → Prop) [decidable_pred p] (s : Π i, set (β i)) : (pi_equiv_pi_subtype_prod p β).symm ⁻¹' pi univ s = (pi univ (λ i : {i // p i}, s i)) ×ˢ pi univ (λ i : {i // ¬p i}, s i) := begin ext ⟨f, g⟩, simp only [mem_preimage, mem_univ_pi, prod_mk_mem_set_prod_eq, subtype.forall, ← forall_and_distrib], refine forall_congr (λ i, _), dsimp only [subtype.coe_mk], by_cases hi : p i; simp [hi] end /-- `sigma_fiber_equiv f` for `f : α → β` is the natural equivalence between the type of all preimages of points under `f` and the total space `α`. -/ -- See also `equiv.sigma_fiber_equiv`. @[simps] def sigma_preimage_equiv {α β} (f : α → β) : (Σ b, f ⁻¹' {b}) ≃ α := sigma_fiber_equiv f /-- A family of equivalences between preimages of points gives an equivalence between domains. -/ -- See also `equiv.of_fiber_equiv`. @[simps] def of_preimage_equiv {α β γ} {f : α → γ} {g : β → γ} (e : Π c, (f ⁻¹' {c}) ≃ (g ⁻¹' {c})) : α ≃ β := equiv.of_fiber_equiv e lemma of_preimage_equiv_map {α β γ} {f : α → γ} {g : β → γ} (e : Π c, (f ⁻¹' {c}) ≃ (g ⁻¹' {c})) (a : α) : g (of_preimage_equiv e a) = f a := equiv.of_fiber_equiv_map e a end equiv /-- If a function is a bijection between two sets `s` and `t`, then it induces an equivalence between the types `↥s` and `↥t`. -/ noncomputable def set.bij_on.equiv {α : Type*} {β : Type*} {s : set α} {t : set β} (f : α → β) (h : bij_on f s t) : s ≃ t := equiv.of_bijective _ h.bijective /-- The composition of an updated function with an equiv on a subset can be expressed as an updated function. -/ lemma dite_comp_equiv_update {α : Type*} {β : Sort*} {γ : Sort*} {s : set α} (e : β ≃ s) (v : β → γ) (w : α → γ) (j : β) (x : γ) [decidable_eq β] [decidable_eq α] [∀ j, decidable (j ∈ s)] : (λ (i : α), if h : i ∈ s then (function.update v j x) (e.symm ⟨i, h⟩) else w i) = function.update (λ (i : α), if h : i ∈ s then v (e.symm ⟨i, h⟩) else w i) (e j) x := begin ext i, by_cases h : i ∈ s, { rw [dif_pos h, function.update_apply_equiv_apply, equiv.symm_symm, function.comp, function.update_apply, function.update_apply, dif_pos h], have h_coe : (⟨i, h⟩ : s) = e j ↔ i = e j := subtype.ext_iff.trans (by rw subtype.coe_mk), simp_rw h_coe }, { have : i ≠ e j, by { contrapose! h, have : (e j : α) ∈ s := (e j).2, rwa ← h at this }, simp [h, this] } end
8c8f0a5def222b8926aa5e3106fa734bb1b3a303
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/by_contradiction.lean
7d216cbe511c5f14ff63930ff7d92fef398fc611
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
612
lean
open tactic nat example (a b : nat) : a ≠ b → ¬ a = b := by do intros, by_contradiction `H, trace_state, contradiction #print "-------" example (a b : nat) : ¬¬ a = b → a = b := by do intros, by_contradiction `H, trace_state, contradiction #print "-------" example (p q : Prop) : ¬¬ p → p := by do intros, by_contradiction `H, -- should fail trace_state #print "-------" local attribute [instance] classical.prop_decidable -- Now all propositions are decidable example (p q : Prop) : ¬¬p → p := by do intros, by_contradiction `H, trace_state, contradiction
271a0fd2521ad2e2a51612863ea23d76e9895dc9
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/hom/equiv/basic.lean
cedc374820889b7d562942cba0538fc1a3dee1e1
[ "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
22,107
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 algebra.hom.group import data.fun_like.equiv import logic.equiv.basic import data.pi.algebra /-! # Multiplicative and additive equivs In this file we define two extensions of `equiv` called `add_equiv` and `mul_equiv`, which are datatypes representing isomorphisms of `add_monoid`s/`add_group`s and `monoid`s/`group`s. ## Notations * ``infix ` ≃* `:25 := mul_equiv`` * ``infix ` ≃+ `:25 := add_equiv`` The extended equivs all have coercions to functions, and the coercions are the canonical notation when treating the isomorphisms as maps. ## Implementation notes The fields for `mul_equiv`, `add_equiv` now avoid the unbundled `is_mul_hom` and `is_add_hom`, as these are deprecated. ## Tags equiv, mul_equiv, add_equiv -/ variables {F α β A B M N P Q G H : Type*} /-- Makes a multiplicative inverse from a bijection which preserves multiplication. -/ @[to_additive "Makes an additive inverse from a bijection which preserves addition."] def mul_hom.inverse [has_mul M] [has_mul N] (f : M →ₙ* N) (g : N → M) (h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) : N →ₙ* M := { to_fun := g, map_mul' := λ x y, calc g (x * y) = g (f (g x) * f (g y)) : by rw [h₂ x, h₂ y] ... = g (f (g x * g y)) : by rw f.map_mul ... = g x * g y : h₁ _, } /-- The inverse of a bijective `monoid_hom` is a `monoid_hom`. -/ @[to_additive "The inverse of a bijective `add_monoid_hom` is an `add_monoid_hom`.", simps] def monoid_hom.inverse {A B : Type*} [monoid A] [monoid B] (f : A →* B) (g : B → A) (h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) : B →* A := { to_fun := g, map_one' := by rw [← f.map_one, h₁], .. (f : A →ₙ* B).inverse g h₁ h₂, } set_option old_structure_cmd true /-- add_equiv α β is the type of an equiv α ≃ β which preserves addition. -/ @[ancestor equiv add_hom] structure add_equiv (A B : Type*) [has_add A] [has_add B] extends A ≃ B, add_hom A B /-- `add_equiv_class F A B` states that `F` is a type of addition-preserving morphisms. You should extend this class when you extend `add_equiv`. -/ class add_equiv_class (F A B : Type*) [has_add A] [has_add B] extends equiv_like F A B := (map_add : ∀ (f : F) a b, f (a + b) = f a + f b) /-- The `equiv` underlying an `add_equiv`. -/ add_decl_doc add_equiv.to_equiv /-- The `add_hom` underlying a `add_equiv`. -/ add_decl_doc add_equiv.to_add_hom /-- `mul_equiv α β` is the type of an equiv `α ≃ β` which preserves multiplication. -/ @[ancestor equiv mul_hom, to_additive] structure mul_equiv (M N : Type*) [has_mul M] [has_mul N] extends M ≃ N, M →ₙ* N /-- The `equiv` underlying a `mul_equiv`. -/ add_decl_doc mul_equiv.to_equiv /-- The `mul_hom` underlying a `mul_equiv`. -/ add_decl_doc mul_equiv.to_mul_hom /-- `mul_equiv_class F A B` states that `F` is a type of multiplication-preserving morphisms. You should extend this class when you extend `mul_equiv`. -/ @[to_additive] class mul_equiv_class (F A B : Type*) [has_mul A] [has_mul B] extends equiv_like F A B := (map_mul : ∀ (f : F) a b, f (a * b) = f a * f b) infix ` ≃* `:25 := mul_equiv infix ` ≃+ `:25 := add_equiv namespace mul_equiv_class variables (F) @[priority 100, -- See note [lower instance priority] to_additive] instance [has_mul M] [has_mul N] [h : mul_equiv_class F M N] : mul_hom_class F M N := { coe := (coe : F → M → N), coe_injective' := @fun_like.coe_injective F _ _ _, .. h } @[priority 100, -- See note [lower instance priority] to_additive] instance [mul_one_class M] [mul_one_class N] [mul_equiv_class F M N] : monoid_hom_class F M N := { coe := (coe : F → M → N), map_one := λ e, calc e 1 = e 1 * 1 : (mul_one _).symm ... = e 1 * e (inv e (1 : N) : M) : congr_arg _ (right_inv e 1).symm ... = e (inv e (1 : N)) : by rw [← map_mul, one_mul] ... = 1 : right_inv e 1, .. mul_equiv_class.mul_hom_class F } @[priority 100] -- See note [lower instance priority] instance to_monoid_with_zero_hom_class {α β : Type*} [mul_zero_one_class α] [mul_zero_one_class β] [mul_equiv_class F α β] : monoid_with_zero_hom_class F α β := { map_zero := λ e, calc e 0 = e 0 * e (equiv_like.inv e 0) : by rw [←map_mul, zero_mul] ... = 0 : by { convert mul_zero _, exact equiv_like.right_inv e _ } ..mul_equiv_class.monoid_hom_class _ } variables {F} @[simp, to_additive] lemma map_eq_one_iff {M N} [mul_one_class M] [mul_one_class N] [mul_equiv_class F M N] (h : F) {x : M} : h x = 1 ↔ x = 1 := map_eq_one_iff h (equiv_like.injective h) @[to_additive] lemma map_ne_one_iff {M N} [mul_one_class M] [mul_one_class N] [mul_equiv_class F M N] (h : F) {x : M} : h x ≠ 1 ↔ x ≠ 1 := map_ne_one_iff h (equiv_like.injective h) end mul_equiv_class @[to_additive] instance [has_mul α] [has_mul β] [mul_equiv_class F α β] : has_coe_t F (α ≃* β) := ⟨λ f, { to_fun := f, inv_fun := equiv_like.inv f, left_inv := equiv_like.left_inv f, right_inv := equiv_like.right_inv f, map_mul' := map_mul f }⟩ namespace mul_equiv @[to_additive] instance [has_mul M] [has_mul N] : has_coe_to_fun (M ≃* N) (λ _, M → N) := ⟨mul_equiv.to_fun⟩ @[to_additive] instance [has_mul M] [has_mul N] : mul_equiv_class (M ≃* N) M N := { coe := to_fun, inv := inv_fun, left_inv := left_inv, right_inv := right_inv, coe_injective' := λ f g h₁ h₂, by { cases f, cases g, congr' }, map_mul := map_mul' } variables [has_mul M] [has_mul N] [has_mul P] [has_mul Q] @[simp, to_additive] lemma to_equiv_eq_coe (f : M ≃* N) : f.to_equiv = f := rfl @[simp, to_additive] lemma to_fun_eq_coe {f : M ≃* N} : f.to_fun = f := rfl @[simp, to_additive] lemma coe_to_equiv {f : M ≃* N} : ⇑(f : M ≃ N) = f := rfl @[simp, to_additive] lemma coe_to_mul_hom {f : M ≃* N} : ⇑f.to_mul_hom = f := rfl /-- A multiplicative isomorphism preserves multiplication. -/ @[to_additive "An additive isomorphism preserves addition."] protected lemma map_mul (f : M ≃* N) : ∀ x y, f (x * y) = f x * f y := map_mul f /-- Makes a multiplicative isomorphism from a bijection which preserves multiplication. -/ @[to_additive "Makes an additive isomorphism from a bijection which preserves addition."] def mk' (f : M ≃ N) (h : ∀ x y, f (x * y) = f x * f y) : M ≃* N := ⟨f.1, f.2, f.3, f.4, h⟩ @[to_additive] protected lemma bijective (e : M ≃* N) : function.bijective e := equiv_like.bijective e @[to_additive] protected lemma injective (e : M ≃* N) : function.injective e := equiv_like.injective e @[to_additive] protected lemma surjective (e : M ≃* N) : function.surjective e := equiv_like.surjective e /-- The identity map is a multiplicative isomorphism. -/ @[refl, to_additive "The identity map is an additive isomorphism."] def refl (M : Type*) [has_mul M] : M ≃* M := { map_mul' := λ _ _, rfl, ..equiv.refl _} @[to_additive] instance : inhabited (M ≃* M) := ⟨refl M⟩ /-- The inverse of an isomorphism is an isomorphism. -/ @[symm, to_additive "The inverse of an isomorphism is an isomorphism."] def symm (h : M ≃* N) : N ≃* M := { map_mul' := (h.to_mul_hom.inverse h.to_equiv.symm h.left_inv h.right_inv).map_mul, .. h.to_equiv.symm} @[simp, to_additive] lemma inv_fun_eq_symm {f : M ≃* N} : f.inv_fun = f.symm := rfl /-- See Note [custom simps projection] -/ -- we don't hyperlink the note in the additive version, since that breaks syntax highlighting -- in the whole file. @[to_additive "See Note custom simps projection"] def simps.symm_apply (e : M ≃* N) : N → M := e.symm initialize_simps_projections add_equiv (to_fun → apply, inv_fun → symm_apply) initialize_simps_projections mul_equiv (to_fun → apply, inv_fun → symm_apply) @[simp, to_additive] theorem to_equiv_symm (f : M ≃* N) : f.symm.to_equiv = f.to_equiv.symm := rfl @[simp, to_additive] theorem coe_mk (f : M → N) (g h₁ h₂ h₃) : ⇑(mul_equiv.mk f g h₁ h₂ h₃) = f := rfl @[simp, to_additive] lemma to_equiv_mk (f : M → N) (g : N → M) (h₁ h₂ h₃) : (mk f g h₁ h₂ h₃).to_equiv = ⟨f, g, h₁, h₂⟩ := rfl @[simp, to_additive] lemma symm_symm : ∀ (f : M ≃* N), f.symm.symm = f | ⟨f, g, h₁, h₂, h₃⟩ := rfl @[to_additive] lemma symm_bijective : function.bijective (symm : (M ≃* N) → (N ≃* M)) := equiv.bijective ⟨symm, symm, symm_symm, symm_symm⟩ @[simp, to_additive] theorem symm_mk (f : M → N) (g h₁ h₂ h₃) : (mul_equiv.mk f g h₁ h₂ h₃).symm = { to_fun := g, inv_fun := f, ..(mul_equiv.mk f g h₁ h₂ h₃).symm} := rfl @[simp, to_additive] theorem refl_symm : (refl M).symm = refl M := rfl /-- Transitivity of multiplication-preserving isomorphisms -/ @[trans, to_additive "Transitivity of addition-preserving isomorphisms"] def trans (h1 : M ≃* N) (h2 : N ≃* P) : (M ≃* P) := { map_mul' := λ x y, show h2 (h1 (x * y)) = h2 (h1 x) * h2 (h1 y), by rw [h1.map_mul, h2.map_mul], ..h1.to_equiv.trans h2.to_equiv } /-- `e.symm` is a right inverse of `e`, written as `e (e.symm y) = y`. -/ @[simp, to_additive "`e.symm` is a right inverse of `e`, written as `e (e.symm y) = y`."] lemma apply_symm_apply (e : M ≃* N) (y : N) : e (e.symm y) = y := e.to_equiv.apply_symm_apply y /-- `e.symm` is a left inverse of `e`, written as `e.symm (e y) = y`. -/ @[simp, to_additive "`e.symm` is a left inverse of `e`, written as `e.symm (e y) = y`."] lemma symm_apply_apply (e : M ≃* N) (x : M) : e.symm (e x) = x := e.to_equiv.symm_apply_apply x @[simp, to_additive] theorem symm_comp_self (e : M ≃* N) : e.symm ∘ e = id := funext e.symm_apply_apply @[simp, to_additive] theorem self_comp_symm (e : M ≃* N) : e ∘ e.symm = id := funext e.apply_symm_apply @[simp, to_additive] theorem coe_refl : ⇑(refl M) = id := rfl @[simp, to_additive] theorem refl_apply (m : M) : refl M m = m := rfl @[simp, to_additive] theorem coe_trans (e₁ : M ≃* N) (e₂ : N ≃* P) : ⇑(e₁.trans e₂) = e₂ ∘ e₁ := rfl @[simp, to_additive] theorem trans_apply (e₁ : M ≃* N) (e₂ : N ≃* P) (m : M) : e₁.trans e₂ m = e₂ (e₁ m) := rfl @[simp, to_additive] theorem symm_trans_apply (e₁ : M ≃* N) (e₂ : N ≃* P) (p : P) : (e₁.trans e₂).symm p = e₁.symm (e₂.symm p) := rfl @[simp, to_additive] theorem apply_eq_iff_eq (e : M ≃* N) {x y : M} : e x = e y ↔ x = y := e.injective.eq_iff @[to_additive] lemma apply_eq_iff_symm_apply (e : M ≃* N) {x : M} {y : N} : e x = y ↔ x = e.symm y := e.to_equiv.apply_eq_iff_eq_symm_apply @[to_additive] lemma symm_apply_eq (e : M ≃* N) {x y} : e.symm x = y ↔ x = e y := e.to_equiv.symm_apply_eq @[to_additive] lemma eq_symm_apply (e : M ≃* N) {x y} : y = e.symm x ↔ e y = x := e.to_equiv.eq_symm_apply @[to_additive] lemma eq_comp_symm {α : Type*} (e : M ≃* N) (f : N → α) (g : M → α) : f = g ∘ e.symm ↔ f ∘ e = g := e.to_equiv.eq_comp_symm f g @[to_additive] lemma comp_symm_eq {α : Type*} (e : M ≃* N) (f : N → α) (g : M → α) : g ∘ e.symm = f ↔ g = f ∘ e := e.to_equiv.comp_symm_eq f g @[to_additive] lemma eq_symm_comp {α : Type*} (e : M ≃* N) (f : α → M) (g : α → N) : f = e.symm ∘ g ↔ e ∘ f = g := e.to_equiv.eq_symm_comp f g @[to_additive] lemma symm_comp_eq {α : Type*} (e : M ≃* N) (f : α → M) (g : α → N) : e.symm ∘ g = f ↔ g = e ∘ f := e.to_equiv.symm_comp_eq f g @[simp, to_additive] theorem symm_trans_self (e : M ≃* N) : e.symm.trans e = refl N := fun_like.ext _ _ e.apply_symm_apply @[simp, to_additive] theorem self_trans_symm (e : M ≃* N) : e.trans e.symm = refl M := fun_like.ext _ _ e.symm_apply_apply @[to_additive, simp] lemma coe_monoid_hom_refl {M} [mul_one_class M] : (refl M : M →* M) = monoid_hom.id M := rfl @[to_additive, simp] lemma coe_monoid_hom_trans {M N P} [mul_one_class M] [mul_one_class N] [mul_one_class P] (e₁ : M ≃* N) (e₂ : N ≃* P) : (e₁.trans e₂ : M →* P) = (e₂ : N →* P).comp ↑e₁ := rfl /-- Two multiplicative isomorphisms agree if they are defined by the same underlying function. -/ @[ext, to_additive "Two additive isomorphisms agree if they are defined by the same underlying function."] lemma ext {f g : mul_equiv M N} (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h @[to_additive] lemma ext_iff {f g : mul_equiv M N} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff @[simp, to_additive] lemma mk_coe (e : M ≃* N) (e' h₁ h₂ h₃) : (⟨e, e', h₁, h₂, h₃⟩ : M ≃* N) = e := ext $ λ _, rfl @[simp, to_additive] lemma mk_coe' (e : M ≃* N) (f h₁ h₂ h₃) : (mul_equiv.mk f ⇑e h₁ h₂ h₃ : N ≃* M) = e.symm := symm_bijective.injective $ ext $ λ x, rfl @[to_additive] protected lemma congr_arg {f : mul_equiv M N} {x x' : M} : x = x' → f x = f x' := fun_like.congr_arg f @[to_additive] protected lemma congr_fun {f g : mul_equiv M N} (h : f = g) (x : M) : f x = g x := fun_like.congr_fun h x /-- The `mul_equiv` between two monoids with a unique element. -/ @[to_additive "The `add_equiv` between two add_monoids with a unique element."] def mul_equiv_of_unique {M N} [unique M] [unique N] [has_mul M] [has_mul N] : M ≃* N := { map_mul' := λ _ _, subsingleton.elim _ _, ..equiv.equiv_of_unique M N } /-- There is a unique monoid homomorphism between two monoids with a unique element. -/ @[to_additive "There is a unique additive monoid homomorphism between two additive monoids with a unique element."] instance {M N} [unique M] [unique N] [has_mul M] [has_mul N] : unique (M ≃* N) := { default := mul_equiv_of_unique , uniq := λ _, ext $ λ x, subsingleton.elim _ _} /-! ## Monoids -/ /-- A multiplicative isomorphism of monoids sends `1` to `1` (and is hence a monoid isomorphism). -/ @[to_additive "An additive isomorphism of additive monoids sends `0` to `0` (and is hence an additive monoid isomorphism)."] protected lemma map_one {M N} [mul_one_class M] [mul_one_class N] (h : M ≃* N) : h 1 = 1 := map_one h @[to_additive] protected lemma map_eq_one_iff {M N} [mul_one_class M] [mul_one_class N] (h : M ≃* N) {x : M} : h x = 1 ↔ x = 1 := mul_equiv_class.map_eq_one_iff h @[to_additive] lemma map_ne_one_iff {M N} [mul_one_class M] [mul_one_class N] (h : M ≃* N) {x : M} : h x ≠ 1 ↔ x ≠ 1 := mul_equiv_class.map_ne_one_iff h /-- A bijective `semigroup` homomorphism is an isomorphism -/ @[to_additive "A bijective `add_semigroup` homomorphism is an isomorphism", simps apply] noncomputable def of_bijective {M N F} [has_mul M] [has_mul N] [mul_hom_class F M N] (f : F) (hf : function.bijective f) : M ≃* N := { map_mul' := map_mul f, ..equiv.of_bijective f hf } @[simp] lemma of_bijective_apply_symm_apply {M N} [mul_one_class M] [mul_one_class N] {n : N} (f : M →* N) (hf : function.bijective f) : f ((equiv.of_bijective f hf).symm n) = n := (mul_equiv.of_bijective f hf).apply_symm_apply n /-- Extract the forward direction of a multiplicative equivalence as a multiplication-preserving function. -/ @[to_additive "Extract the forward direction of an additive equivalence as an addition-preserving function."] def to_monoid_hom {M N} [mul_one_class M] [mul_one_class N] (h : M ≃* N) : (M →* N) := { map_one' := h.map_one, .. h } @[simp, to_additive] lemma coe_to_monoid_hom {M N} [mul_one_class M] [mul_one_class N] (e : M ≃* N) : ⇑e.to_monoid_hom = e := rfl @[to_additive] lemma to_monoid_hom_injective {M N} [mul_one_class M] [mul_one_class N] : function.injective (to_monoid_hom : (M ≃* N) → M →* N) := λ f g h, mul_equiv.ext (monoid_hom.ext_iff.1 h) /-- A multiplicative analogue of `equiv.arrow_congr`, where the equivalence between the targets is multiplicative. -/ @[to_additive "An additive analogue of `equiv.arrow_congr`, where the equivalence between the targets is additive.", simps apply] def arrow_congr {M N P Q : Type*} [has_mul P] [has_mul Q] (f : M ≃ N) (g : P ≃* Q) : (M → P) ≃* (N → Q) := { to_fun := λ h n, g (h (f.symm n)), inv_fun := λ k m, g.symm (k (f m)), left_inv := λ h, by { ext, simp, }, right_inv := λ k, by { ext, simp, }, map_mul' := λ h k, by { ext, simp, }, } /-- A multiplicative analogue of `equiv.arrow_congr`, for multiplicative maps from a monoid to a commutative monoid. -/ @[to_additive "An additive analogue of `equiv.arrow_congr`, for additive maps from an additive monoid to a commutative additive monoid.", simps apply] def monoid_hom_congr {M N P Q} [mul_one_class M] [mul_one_class N] [comm_monoid P] [comm_monoid Q] (f : M ≃* N) (g : P ≃* Q) : (M →* P) ≃* (N →* Q) := { to_fun := λ h, g.to_monoid_hom.comp (h.comp f.symm.to_monoid_hom), inv_fun := λ k, g.symm.to_monoid_hom.comp (k.comp f.to_monoid_hom), left_inv := λ h, by { ext, simp, }, right_inv := λ k, by { ext, simp, }, map_mul' := λ h k, by { ext, simp, }, } /-- A family of multiplicative equivalences `Π j, (Ms j ≃* Ns j)` generates a multiplicative equivalence between `Π j, Ms j` and `Π j, Ns j`. This is the `mul_equiv` version of `equiv.Pi_congr_right`, and the dependent version of `mul_equiv.arrow_congr`. -/ @[to_additive add_equiv.Pi_congr_right "A family of additive equivalences `Π j, (Ms j ≃+ Ns j)` generates an additive equivalence between `Π j, Ms j` and `Π j, Ns j`. This is the `add_equiv` version of `equiv.Pi_congr_right`, and the dependent version of `add_equiv.arrow_congr`.", simps apply] def Pi_congr_right {η : Type*} {Ms Ns : η → Type*} [Π j, has_mul (Ms j)] [Π j, has_mul (Ns j)] (es : ∀ j, Ms j ≃* Ns j) : (Π j, Ms j) ≃* (Π j, Ns j) := { to_fun := λ x j, es j (x j), inv_fun := λ x j, (es j).symm (x j), map_mul' := λ x y, funext $ λ j, (es j).map_mul (x j) (y j), .. equiv.Pi_congr_right (λ j, (es j).to_equiv) } @[simp] lemma Pi_congr_right_refl {η : Type*} {Ms : η → Type*} [Π j, has_mul (Ms j)] : Pi_congr_right (λ j, mul_equiv.refl (Ms j)) = mul_equiv.refl _ := rfl @[simp] lemma Pi_congr_right_symm {η : Type*} {Ms Ns : η → Type*} [Π j, has_mul (Ms j)] [Π j, has_mul (Ns j)] (es : ∀ j, Ms j ≃* Ns j) : (Pi_congr_right es).symm = (Pi_congr_right $ λ i, (es i).symm) := rfl @[simp] lemma Pi_congr_right_trans {η : Type*} {Ms Ns Ps : η → Type*} [Π j, has_mul (Ms j)] [Π j, has_mul (Ns j)] [Π j, has_mul (Ps j)] (es : ∀ j, Ms j ≃* Ns j) (fs : ∀ j, Ns j ≃* Ps j) : (Pi_congr_right es).trans (Pi_congr_right fs) = (Pi_congr_right $ λ i, (es i).trans (fs i)) := rfl /-- A family indexed by a nonempty subsingleton type is equivalent to the element at the single index. -/ @[to_additive add_equiv.Pi_subsingleton "A family indexed by a nonempty subsingleton type is equivalent to the element at the single index.", simps] def Pi_subsingleton {ι : Type*} (M : ι → Type*) [Π j, has_mul (M j)] [subsingleton ι] (i : ι) : (Π j, M j) ≃* M i := { map_mul' := λ f1 f2, pi.mul_apply _ _ _, ..equiv.Pi_subsingleton M i } /-! # Groups -/ /-- A multiplicative equivalence of groups preserves inversion. -/ @[to_additive "An additive equivalence of additive groups preserves negation."] protected lemma map_inv [group G] [division_monoid H] (h : G ≃* H) (x : G) : h x⁻¹ = (h x)⁻¹ := map_inv h x /-- A multiplicative equivalence of groups preserves division. -/ @[to_additive "An additive equivalence of additive groups preserves subtractions."] protected lemma map_div [group G] [division_monoid H] (h : G ≃* H) (x y : G) : h (x / y) = h x / h y := map_div h x y end mul_equiv /-- Given a pair of multiplicative homomorphisms `f`, `g` such that `g.comp f = id` and `f.comp g = id`, returns an multiplicative equivalence with `to_fun = f` and `inv_fun = g`. This constructor is useful if the underlying type(s) have specialized `ext` lemmas for multiplicative homomorphisms. -/ @[to_additive /-"Given a pair of additive homomorphisms `f`, `g` such that `g.comp f = id` and `f.comp g = id`, returns an additive equivalence with `to_fun = f` and `inv_fun = g`. This constructor is useful if the underlying type(s) have specialized `ext` lemmas for additive homomorphisms."-/, simps {fully_applied := ff}] def mul_hom.to_mul_equiv [has_mul M] [has_mul N] (f : M →ₙ* N) (g : N →ₙ* M) (h₁ : g.comp f = mul_hom.id _) (h₂ : f.comp g = mul_hom.id _) : M ≃* N := { to_fun := f, inv_fun := g, left_inv := mul_hom.congr_fun h₁, right_inv := mul_hom.congr_fun h₂, map_mul' := f.map_mul } /-- Given a pair of monoid homomorphisms `f`, `g` such that `g.comp f = id` and `f.comp g = id`, returns an multiplicative equivalence with `to_fun = f` and `inv_fun = g`. This constructor is useful if the underlying type(s) have specialized `ext` lemmas for monoid homomorphisms. -/ @[to_additive /-"Given a pair of additive monoid homomorphisms `f`, `g` such that `g.comp f = id` and `f.comp g = id`, returns an additive equivalence with `to_fun = f` and `inv_fun = g`. This constructor is useful if the underlying type(s) have specialized `ext` lemmas for additive monoid homomorphisms."-/, simps {fully_applied := ff}] def monoid_hom.to_mul_equiv [mul_one_class M] [mul_one_class N] (f : M →* N) (g : N →* M) (h₁ : g.comp f = monoid_hom.id _) (h₂ : f.comp g = monoid_hom.id _) : M ≃* N := { to_fun := f, inv_fun := g, left_inv := monoid_hom.congr_fun h₁, right_inv := monoid_hom.congr_fun h₂, map_mul' := f.map_mul } namespace equiv section has_involutive_neg variables (G) [has_involutive_inv G] /-- Inversion on a `group` or `group_with_zero` is a permutation of the underlying type. -/ @[to_additive "Negation on an `add_group` is a permutation of the underlying type.", simps apply {fully_applied := ff}] protected def inv : perm G := inv_involutive.to_perm _ variable {G} @[simp, to_additive] lemma inv_symm : (equiv.inv G).symm = equiv.inv G := rfl end has_involutive_neg end equiv
df64ac1169eee251b174377da036e6bba4fe8363
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/order/conditionally_complete_lattice.lean
9c226ae06dd2694e377962070744c6f9eabb788b
[ "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
49,710
lean
/- Copyright (c) 2018 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 data.set.intervals.ord_connected /-! # Theory of conditionally complete lattices. A conditionally complete lattice is a lattice in which every non-empty bounded subset s has a least upper bound and a greatest lower bound, denoted below by Sup s and Inf s. Typical examples are real, nat, int with their usual orders. The theory is very comparable to the theory of complete lattices, except that suitable boundedness and nonemptiness assumptions have to be added to most statements. We introduce two predicates bdd_above and bdd_below to express this boundedness, prove their basic properties, and then go on to prove most useful properties of Sup and Inf in conditionally complete lattices. To differentiate the statements between complete lattices and conditionally complete lattices, we prefix Inf and Sup in the statements by c, giving cInf and cSup. For instance, Inf_le is a statement in complete lattices ensuring Inf s ≤ x, while cInf_le is the same statement in conditionally complete lattices with an additional assumption that s is bounded below. -/ set_option old_structure_cmd true open set variables {α β : Type*} {ι : Sort*} section /-! Extension of Sup and Inf from a preorder `α` to `with_top α` and `with_bot α` -/ open_locale classical noncomputable instance {α : Type*} [preorder α] [has_Sup α] : has_Sup (with_top α) := ⟨λ S, if ⊤ ∈ S then ⊤ else if bdd_above (coe ⁻¹' S : set α) then ↑(Sup (coe ⁻¹' S : set α)) else ⊤⟩ noncomputable instance {α : Type*} [has_Inf α] : has_Inf (with_top α) := ⟨λ S, if S ⊆ {⊤} then ⊤ else ↑(Inf (coe ⁻¹' S : set α))⟩ noncomputable instance {α : Type*} [has_Sup α] : has_Sup (with_bot α) := ⟨(@with_top.has_Inf (order_dual α) _).Inf⟩ noncomputable instance {α : Type*} [preorder α] [has_Inf α] : has_Inf (with_bot α) := ⟨(@with_top.has_Sup (order_dual α) _ _).Sup⟩ @[simp] theorem with_top.cInf_empty {α : Type*} [has_Inf α] : Inf (∅ : set (with_top α)) = ⊤ := if_pos $ set.empty_subset _ @[simp] theorem with_bot.cSup_empty {α : Type*} [has_Sup α] : Sup (∅ : set (with_bot α)) = ⊥ := if_pos $ set.empty_subset _ end -- section /-- A conditionally complete lattice is a lattice in which every nonempty subset which is bounded above has a supremum, and every nonempty subset which is bounded below has an infimum. Typical examples are real numbers or natural numbers. To differentiate the statements from the corresponding statements in (unconditional) complete lattices, we prefix Inf and Sup by a c everywhere. The same statements should hold in both worlds, sometimes with additional assumptions of nonemptiness or boundedness.-/ class conditionally_complete_lattice (α : Type*) extends lattice α, has_Sup α, has_Inf α := (le_cSup : ∀s a, bdd_above s → a ∈ s → a ≤ Sup s) (cSup_le : ∀ s a, set.nonempty s → a ∈ upper_bounds s → Sup s ≤ a) (cInf_le : ∀s a, bdd_below s → a ∈ s → Inf s ≤ a) (le_cInf : ∀s a, set.nonempty s → a ∈ lower_bounds s → a ≤ Inf s) /-- A conditionally complete linear order is a linear order in which every nonempty subset which is bounded above has a supremum, and every nonempty subset which is bounded below has an infimum. Typical examples are real numbers or natural numbers. To differentiate the statements from the corresponding statements in (unconditional) complete linear orders, we prefix Inf and Sup by a c everywhere. The same statements should hold in both worlds, sometimes with additional assumptions of nonemptiness or boundedness.-/ class conditionally_complete_linear_order (α : Type*) extends conditionally_complete_lattice α, linear_order α /-- A conditionally complete linear order with `bot` is a linear order with least element, in which every nonempty subset which is bounded above has a supremum, and every nonempty subset (necessarily bounded below) has an infimum. A typical example is the natural numbers. To differentiate the statements from the corresponding statements in (unconditional) complete linear orders, we prefix Inf and Sup by a c everywhere. The same statements should hold in both worlds, sometimes with additional assumptions of nonemptiness or boundedness.-/ class conditionally_complete_linear_order_bot (α : Type*) extends conditionally_complete_linear_order α, order_bot α := (cSup_empty : Sup ∅ = ⊥) /- A complete lattice is a conditionally complete lattice, as there are no restrictions on the properties of Inf and Sup in a complete lattice.-/ @[priority 100] -- see Note [lower instance priority] instance conditionally_complete_lattice_of_complete_lattice [complete_lattice α]: conditionally_complete_lattice α := { le_cSup := by intros; apply le_Sup; assumption, cSup_le := by intros; apply Sup_le; assumption, cInf_le := by intros; apply Inf_le; assumption, le_cInf := by intros; apply le_Inf; assumption, ..‹complete_lattice α› } @[priority 100] -- see Note [lower instance priority] instance conditionally_complete_linear_order_of_complete_linear_order [complete_linear_order α]: conditionally_complete_linear_order α := { ..conditionally_complete_lattice_of_complete_lattice, .. ‹complete_linear_order α› } section open_locale classical /-- A well founded linear order is conditionally complete, with a bottom element. -/ @[reducible] noncomputable def well_founded.conditionally_complete_linear_order_with_bot {α : Type*} [i : linear_order α] (h : well_founded ((<) : α → α → Prop)) (c : α) (hc : c = h.min set.univ ⟨c, mem_univ c⟩) : conditionally_complete_linear_order_bot α := { sup := max, le_sup_left := le_max_left, le_sup_right := le_max_right, sup_le := λ a b c, max_le, inf := min, inf_le_left := min_le_left, inf_le_right := min_le_right, le_inf := λ a b c, le_min, Inf := λ s, if hs : s.nonempty then h.min s hs else c, cInf_le := begin assume s a hs has, have s_ne : s.nonempty := ⟨a, has⟩, simpa [s_ne] using not_lt.1 (h.not_lt_min s s_ne has), end, le_cInf := begin assume s a hs has, simp only [hs, dif_pos], exact has (h.min_mem s hs), end, Sup := λ s, if hs : (upper_bounds s).nonempty then h.min _ hs else c, le_cSup := begin assume s a hs has, have h's : (upper_bounds s).nonempty := hs, simp only [h's, dif_pos], exact h.min_mem _ h's has, end, cSup_le := begin assume s a hs has, have h's : (upper_bounds s).nonempty := ⟨a, has⟩, simp only [h's, dif_pos], simpa using h.not_lt_min _ h's has, end, bot := c, bot_le := λ x, by convert not_lt.1 (h.not_lt_min set.univ ⟨c, mem_univ c⟩ (mem_univ x)), cSup_empty := begin have : (set.univ : set α).nonempty := ⟨c, mem_univ c⟩, simp only [this, dif_pos, upper_bounds_empty], exact hc.symm end, .. i } end section order_dual instance (α : Type*) [conditionally_complete_lattice α] : conditionally_complete_lattice (order_dual α) := { le_cSup := @conditionally_complete_lattice.cInf_le α _, cSup_le := @conditionally_complete_lattice.le_cInf α _, le_cInf := @conditionally_complete_lattice.cSup_le α _, cInf_le := @conditionally_complete_lattice.le_cSup α _, ..order_dual.has_Inf α, ..order_dual.has_Sup α, ..order_dual.lattice α } instance (α : Type*) [conditionally_complete_linear_order α] : conditionally_complete_linear_order (order_dual α) := { ..order_dual.conditionally_complete_lattice α, ..order_dual.linear_order α } end order_dual section conditionally_complete_lattice variables [conditionally_complete_lattice α] {s t : set α} {a b : α} theorem le_cSup (h₁ : bdd_above s) (h₂ : a ∈ s) : a ≤ Sup s := conditionally_complete_lattice.le_cSup s a h₁ h₂ theorem cSup_le (h₁ : s.nonempty) (h₂ : ∀b∈s, b ≤ a) : Sup s ≤ a := conditionally_complete_lattice.cSup_le s a h₁ h₂ theorem cInf_le (h₁ : bdd_below s) (h₂ : a ∈ s) : Inf s ≤ a := conditionally_complete_lattice.cInf_le s a h₁ h₂ theorem le_cInf (h₁ : s.nonempty) (h₂ : ∀b∈s, a ≤ b) : a ≤ Inf s := conditionally_complete_lattice.le_cInf s a h₁ h₂ theorem le_cSup_of_le (_ : bdd_above s) (hb : b ∈ s) (h : a ≤ b) : a ≤ Sup s := le_trans h (le_cSup ‹bdd_above s› hb) theorem cInf_le_of_le (_ : bdd_below s) (hb : b ∈ s) (h : b ≤ a) : Inf s ≤ a := le_trans (cInf_le ‹bdd_below s› hb) h theorem cSup_le_cSup (_ : bdd_above t) (_ : s.nonempty) (h : s ⊆ t) : Sup s ≤ Sup t := cSup_le ‹_› (assume (a) (ha : a ∈ s), le_cSup ‹bdd_above t› (h ha)) theorem cInf_le_cInf (_ : bdd_below t) (_ : s.nonempty) (h : s ⊆ t) : Inf t ≤ Inf s := le_cInf ‹_› (assume (a) (ha : a ∈ s), cInf_le ‹bdd_below t› (h ha)) lemma is_lub_cSup (ne : s.nonempty) (H : bdd_above s) : is_lub s (Sup s) := ⟨assume x, le_cSup H, assume x, cSup_le ne⟩ lemma is_lub_csupr [nonempty ι] {f : ι → α} (H : bdd_above (range f)) : is_lub (range f) (⨆ i, f i) := is_lub_cSup (range_nonempty f) H lemma is_lub_csupr_set {f : β → α} {s : set β} (H : bdd_above (f '' s)) (Hne : s.nonempty) : is_lub (f '' s) (⨆ i : s, f i) := by { rw ← Sup_image', exact is_lub_cSup (Hne.image _) H } lemma is_glb_cInf (ne : s.nonempty) (H : bdd_below s) : is_glb s (Inf s) := ⟨assume x, cInf_le H, assume x, le_cInf ne⟩ lemma is_glb_cinfi [nonempty ι] {f : ι → α} (H : bdd_below (range f)) : is_glb (range f) (⨅ i, f i) := is_glb_cInf (range_nonempty f) H lemma is_glb_cinfi_set {f : β → α} {s : set β} (H : bdd_below (f '' s)) (Hne : s.nonempty) : is_glb (f '' s) (⨅ i : s, f i) := @is_lub_csupr_set (order_dual α) _ _ _ _ H Hne lemma is_lub.cSup_eq (H : is_lub s a) (ne : s.nonempty) : Sup s = a := (is_lub_cSup ne ⟨a, H.1⟩).unique H lemma is_lub.csupr_eq [nonempty ι] {f : ι → α} (H : is_lub (range f) a) : (⨆ i, f i) = a := H.cSup_eq (range_nonempty f) lemma is_lub.csupr_set_eq {s : set β} {f : β → α} (H : is_lub (f '' s) a) (Hne : s.nonempty) : (⨆ i : s, f i) = a := is_lub.cSup_eq (image_eq_range f s ▸ H) (image_eq_range f s ▸ Hne.image f) /-- A greatest element of a set is the supremum of this set. -/ lemma is_greatest.cSup_eq (H : is_greatest s a) : Sup s = a := H.is_lub.cSup_eq H.nonempty lemma is_greatest.Sup_mem (H : is_greatest s a) : Sup s ∈ s := H.cSup_eq.symm ▸ H.1 lemma is_glb.cInf_eq (H : is_glb s a) (ne : s.nonempty) : Inf s = a := (is_glb_cInf ne ⟨a, H.1⟩).unique H lemma is_glb.cinfi_eq [nonempty ι] {f : ι → α} (H : is_glb (range f) a) : (⨅ i, f i) = a := H.cInf_eq (range_nonempty f) lemma is_glb.cinfi_set_eq {s : set β} {f : β → α} (H : is_glb (f '' s) a) (Hne : s.nonempty) : (⨅ i : s, f i) = a := is_glb.cInf_eq (image_eq_range f s ▸ H) (image_eq_range f s ▸ Hne.image f) /-- A least element of a set is the infimum of this set. -/ lemma is_least.cInf_eq (H : is_least s a) : Inf s = a := H.is_glb.cInf_eq H.nonempty lemma is_least.Inf_mem (H : is_least s a) : Inf s ∈ s := H.cInf_eq.symm ▸ H.1 lemma subset_Icc_cInf_cSup (hb : bdd_below s) (ha : bdd_above s) : s ⊆ Icc (Inf s) (Sup s) := λ x hx, ⟨cInf_le hb hx, le_cSup ha hx⟩ theorem cSup_le_iff (hb : bdd_above s) (ne : s.nonempty) : Sup s ≤ a ↔ (∀b ∈ s, b ≤ a) := is_lub_le_iff (is_lub_cSup ne hb) theorem le_cInf_iff (hb : bdd_below s) (ne : s.nonempty) : a ≤ Inf s ↔ (∀b ∈ s, a ≤ b) := le_is_glb_iff (is_glb_cInf ne hb) lemma cSup_lower_bounds_eq_cInf {s : set α} (h : bdd_below s) (hs : s.nonempty) : Sup (lower_bounds s) = Inf s := (is_lub_cSup h $ hs.mono $ λ x hx y hy, hy hx).unique (is_glb_cInf hs h).is_lub lemma cInf_upper_bounds_eq_cSup {s : set α} (h : bdd_above s) (hs : s.nonempty) : Inf (upper_bounds s) = Sup s := (is_glb_cInf h $ hs.mono $ λ x hx y hy, hy hx).unique (is_lub_cSup hs h).is_glb lemma not_mem_of_lt_cInf {x : α} {s : set α} (h : x < Inf s) (hs : bdd_below s) : x ∉ s := λ hx, lt_irrefl _ (h.trans_le (cInf_le hs hx)) lemma not_mem_of_cSup_lt {x : α} {s : set α} (h : Sup s < x) (hs : bdd_above s) : x ∉ s := @not_mem_of_lt_cInf (order_dual α) _ x s h hs /--Introduction rule to prove that `b` is the supremum of `s`: it suffices to check that `b` is larger than all elements of `s`, and that this is not the case of any `w<b`. See `Sup_eq_of_forall_le_of_forall_lt_exists_gt` for a version in complete lattices. -/ theorem cSup_eq_of_forall_le_of_forall_lt_exists_gt (_ : s.nonempty) (_ : ∀a∈s, a ≤ b) (H : ∀w, w < b → (∃a∈s, w < a)) : Sup s = b := have bdd_above s := ⟨b, by assumption⟩, have (Sup s < b) ∨ (Sup s = b) := lt_or_eq_of_le (cSup_le ‹_› ‹∀a∈s, a ≤ b›), have ¬(Sup s < b) := assume: Sup s < b, let ⟨a, _, _⟩ := (H (Sup s) ‹Sup s < b›) in /- a ∈ s, Sup s < a-/ have Sup s < Sup s := lt_of_lt_of_le ‹Sup s < a› (le_cSup ‹bdd_above s› ‹a ∈ s›), show false, by finish [lt_irrefl (Sup s)], show Sup s = b, by finish /--Introduction rule to prove that `b` is the infimum of `s`: it suffices to check that `b` is smaller than all elements of `s`, and that this is not the case of any `w>b`. See `Inf_eq_of_forall_ge_of_forall_gt_exists_lt` for a version in complete lattices. -/ theorem cInf_eq_of_forall_ge_of_forall_gt_exists_lt (_ : s.nonempty) (_ : ∀a∈s, b ≤ a) (H : ∀w, b < w → (∃a∈s, a < w)) : Inf s = b := @cSup_eq_of_forall_le_of_forall_lt_exists_gt (order_dual α) _ _ _ ‹_› ‹_› ‹_› /--b < Sup s when there is an element a in s with b < a, when s is bounded above. This is essentially an iff, except that the assumptions for the two implications are slightly different (one needs boundedness above for one direction, nonemptiness and linear order for the other one), so we formulate separately the two implications, contrary to the complete_lattice case.-/ lemma lt_cSup_of_lt (_ : bdd_above s) (_ : a ∈ s) (_ : b < a) : b < Sup s := lt_of_lt_of_le ‹b < a› (le_cSup ‹bdd_above s› ‹a ∈ s›) /--Inf s < b when there is an element a in s with a < b, when s is bounded below. This is essentially an iff, except that the assumptions for the two implications are slightly different (one needs boundedness below for one direction, nonemptiness and linear order for the other one), so we formulate separately the two implications, contrary to the complete_lattice case.-/ lemma cInf_lt_of_lt (_ : bdd_below s) (_ : a ∈ s) (_ : a < b) : Inf s < b := @lt_cSup_of_lt (order_dual α) _ _ _ _ ‹_› ‹_› ‹_› /-- If all elements of a nonempty set `s` are less than or equal to all elements of a nonempty set `t`, then there exists an element between these sets. -/ lemma exists_between_of_forall_le (sne : s.nonempty) (tne : t.nonempty) (hst : ∀ (x ∈ s) (y ∈ t), x ≤ y) : (upper_bounds s ∩ lower_bounds t).nonempty := ⟨Inf t, λ x hx, le_cInf tne $ hst x hx, λ y hy, cInf_le (sne.mono hst) hy⟩ /--The supremum of a singleton is the element of the singleton-/ @[simp] theorem cSup_singleton (a : α) : Sup {a} = a := is_greatest_singleton.cSup_eq /--The infimum of a singleton is the element of the singleton-/ @[simp] theorem cInf_singleton (a : α) : Inf {a} = a := is_least_singleton.cInf_eq /--If a set is bounded below and above, and nonempty, its infimum is less than or equal to its supremum.-/ theorem cInf_le_cSup (hb : bdd_below s) (ha : bdd_above s) (ne : s.nonempty) : Inf s ≤ Sup s := is_glb_le_is_lub (is_glb_cInf ne hb) (is_lub_cSup ne ha) ne /--The sup of a union of two sets is the max of the suprema of each subset, under the assumptions that all sets are bounded above and nonempty.-/ theorem cSup_union (hs : bdd_above s) (sne : s.nonempty) (ht : bdd_above t) (tne : t.nonempty) : Sup (s ∪ t) = Sup s ⊔ Sup t := ((is_lub_cSup sne hs).union (is_lub_cSup tne ht)).cSup_eq sne.inl /--The inf of a union of two sets is the min of the infima of each subset, under the assumptions that all sets are bounded below and nonempty.-/ theorem cInf_union (hs : bdd_below s) (sne : s.nonempty) (ht : bdd_below t) (tne : t.nonempty) : Inf (s ∪ t) = Inf s ⊓ Inf t := @cSup_union (order_dual α) _ _ _ hs sne ht tne /--The supremum of an intersection of two sets is bounded by the minimum of the suprema of each set, if all sets are bounded above and nonempty.-/ theorem cSup_inter_le (_ : bdd_above s) (_ : bdd_above t) (hst : (s ∩ t).nonempty) : Sup (s ∩ t) ≤ Sup s ⊓ Sup t := begin apply cSup_le hst, simp only [le_inf_iff, and_imp, set.mem_inter_eq], intros b _ _, split, apply le_cSup ‹bdd_above s› ‹b ∈ s›, apply le_cSup ‹bdd_above t› ‹b ∈ t› end /--The infimum of an intersection of two sets is bounded below by the maximum of the infima of each set, if all sets are bounded below and nonempty.-/ theorem le_cInf_inter (_ : bdd_below s) (_ : bdd_below t) (hst : (s ∩ t).nonempty) : Inf s ⊔ Inf t ≤ Inf (s ∩ t) := @cSup_inter_le (order_dual α) _ _ _ ‹_› ‹_› hst /-- The supremum of insert a s is the maximum of a and the supremum of s, if s is nonempty and bounded above.-/ theorem cSup_insert (hs : bdd_above s) (sne : s.nonempty) : Sup (insert a s) = a ⊔ Sup s := ((is_lub_cSup sne hs).insert a).cSup_eq (insert_nonempty a s) /-- The infimum of insert a s is the minimum of a and the infimum of s, if s is nonempty and bounded below.-/ theorem cInf_insert (hs : bdd_below s) (sne : s.nonempty) : Inf (insert a s) = a ⊓ Inf s := @cSup_insert (order_dual α) _ _ _ hs sne @[simp] lemma cInf_Icc (h : a ≤ b) : Inf (Icc a b) = a := (is_glb_Icc h).cInf_eq (nonempty_Icc.2 h) @[simp] lemma cInf_Ici : Inf (Ici a) = a := is_least_Ici.cInf_eq @[simp] lemma cInf_Ico (h : a < b) : Inf (Ico a b) = a := (is_glb_Ico h).cInf_eq (nonempty_Ico.2 h) @[simp] lemma cInf_Ioc [densely_ordered α] (h : a < b) : Inf (Ioc a b) = a := (is_glb_Ioc h).cInf_eq (nonempty_Ioc.2 h) @[simp] lemma cInf_Ioi [no_top_order α] [densely_ordered α] : Inf (Ioi a) = a := cInf_eq_of_forall_ge_of_forall_gt_exists_lt nonempty_Ioi (λ _, le_of_lt) (λ w hw, by simpa using exists_between hw) @[simp] lemma cInf_Ioo [densely_ordered α] (h : a < b) : Inf (Ioo a b) = a := (is_glb_Ioo h).cInf_eq (nonempty_Ioo.2 h) @[simp] lemma cSup_Icc (h : a ≤ b) : Sup (Icc a b) = b := (is_lub_Icc h).cSup_eq (nonempty_Icc.2 h) @[simp] lemma cSup_Ico [densely_ordered α] (h : a < b) : Sup (Ico a b) = b := (is_lub_Ico h).cSup_eq (nonempty_Ico.2 h) @[simp] lemma cSup_Iic : Sup (Iic a) = a := is_greatest_Iic.cSup_eq @[simp] lemma cSup_Iio [no_bot_order α] [densely_ordered α] : Sup (Iio a) = a := cSup_eq_of_forall_le_of_forall_lt_exists_gt nonempty_Iio (λ _, le_of_lt) (λ w hw, by simpa [and_comm] using exists_between hw) @[simp] lemma cSup_Ioc (h : a < b) : Sup (Ioc a b) = b := (is_lub_Ioc h).cSup_eq (nonempty_Ioc.2 h) @[simp] lemma cSup_Ioo [densely_ordered α] (h : a < b) : Sup (Ioo a b) = b := (is_lub_Ioo h).cSup_eq (nonempty_Ioo.2 h) /--The indexed supremum of a function is bounded above by a uniform bound-/ lemma csupr_le [nonempty ι] {f : ι → α} {c : α} (H : ∀x, f x ≤ c) : supr f ≤ c := cSup_le (range_nonempty f) (by rwa forall_range_iff) /--The indexed supremum of a function is bounded below by the value taken at one point-/ lemma le_csupr {f : ι → α} (H : bdd_above (range f)) (c : ι) : f c ≤ supr f := le_cSup H (mem_range_self _) lemma le_csupr_of_le {f : ι → α} (H : bdd_above (range f)) (c : ι) (h : a ≤ f c) : a ≤ supr f := le_trans h (le_csupr H c) /--The indexed supremum of two functions are comparable if the functions are pointwise comparable-/ lemma csupr_le_csupr {f g : ι → α} (B : bdd_above (range g)) (H : ∀x, f x ≤ g x) : supr f ≤ supr g := begin casesI is_empty_or_nonempty ι, { rw [supr_of_empty', supr_of_empty'] }, { exact csupr_le (λ x, le_csupr_of_le B x (H x)) }, end /--The indexed infimum of two functions are comparable if the functions are pointwise comparable-/ lemma cinfi_le_cinfi {f g : ι → α} (B : bdd_below (range f)) (H : ∀x, f x ≤ g x) : infi f ≤ infi g := @csupr_le_csupr (order_dual α) _ _ _ _ B H /--The indexed minimum of a function is bounded below by a uniform lower bound-/ lemma le_cinfi [nonempty ι] {f : ι → α} {c : α} (H : ∀x, c ≤ f x) : c ≤ infi f := @csupr_le (order_dual α) _ _ _ _ _ H /--The indexed infimum of a function is bounded above by the value taken at one point-/ lemma cinfi_le {f : ι → α} (H : bdd_below (range f)) (c : ι) : infi f ≤ f c := @le_csupr (order_dual α) _ _ _ H c lemma cinfi_le_of_le {f : ι → α} (H : bdd_below (range f)) (c : ι) (h : f c ≤ a) : infi f ≤ a := @le_csupr_of_le (order_dual α) _ _ _ _ H c h @[simp] theorem csupr_const [hι : nonempty ι] {a : α} : (⨆ b:ι, a) = a := by rw [supr, range_const, cSup_singleton] @[simp] theorem cinfi_const [hι : nonempty ι] {a : α} : (⨅ b:ι, a) = a := @csupr_const (order_dual α) _ _ _ _ theorem supr_unique [unique ι] {s : ι → α} : (⨆ i, s i) = s (default ι) := have ∀ i, s i = s (default ι) := λ i, congr_arg s (unique.eq_default i), by simp only [this, csupr_const] theorem infi_unique [unique ι] {s : ι → α} : (⨅ i, s i) = s (default ι) := @supr_unique (order_dual α) _ _ _ _ @[simp] theorem supr_unit {f : unit → α} : (⨆ x, f x) = f () := by { convert supr_unique, apply_instance } @[simp] theorem infi_unit {f : unit → α} : (⨅ x, f x) = f () := @supr_unit (order_dual α) _ _ @[simp] lemma csupr_pos {p : Prop} {f : p → α} (hp : p) : (⨆ h : p, f h) = f hp := by haveI := unique_prop hp; exact supr_unique @[simp] lemma cinfi_pos {p : Prop} {f : p → α} (hp : p) : (⨅ h : p, f h) = f hp := @csupr_pos (order_dual α) _ _ _ hp lemma csupr_set {s : set β} {f : β → α} : (⨆ x : s, f x) = Sup (f '' s) := begin rw supr, congr, ext, rw [mem_image, mem_range, set_coe.exists], simp_rw [subtype.coe_mk, exists_prop], end lemma cinfi_set {s : set β} {f : β → α} : (⨅ x : s, f x) = Inf (f '' s) := @csupr_set (order_dual α) _ _ _ _ /--Introduction rule to prove that `b` is the supremum of `f`: it suffices to check that `b` is larger than `f i` for all `i`, and that this is not the case of any `w<b`. See `supr_eq_of_forall_le_of_forall_lt_exists_gt` for a version in complete lattices. -/ theorem csupr_eq_of_forall_le_of_forall_lt_exists_gt [nonempty ι] {f : ι → α} (h₁ : ∀ i, f i ≤ b) (h₂ : ∀ w, w < b → (∃ i, w < f i)) : (⨆ (i : ι), f i) = b := cSup_eq_of_forall_le_of_forall_lt_exists_gt (range_nonempty f) (forall_range_iff.mpr h₁) (λ w hw, exists_range_iff.mpr $ h₂ w hw) /--Introduction rule to prove that `b` is the infimum of `f`: it suffices to check that `b` is smaller than `f i` for all `i`, and that this is not the case of any `w>b`. See `infi_eq_of_forall_ge_of_forall_gt_exists_lt` for a version in complete lattices. -/ theorem cinfi_eq_of_forall_ge_of_forall_gt_exists_lt [nonempty ι] {f : ι → α} (h₁ : ∀ i, b ≤ f i) (h₂ : ∀ w, b < w → (∃ i, f i < w)) : (⨅ (i : ι), f i) = b := @csupr_eq_of_forall_le_of_forall_lt_exists_gt (order_dual α) _ _ _ _ ‹_› ‹_› ‹_› /-- Nested intervals lemma: if `f` is a monotone sequence, `g` is an antitone sequence, and `f n ≤ g n` for all `n`, then `⨆ n, f n` belongs to all the intervals `[f n, g n]`. -/ lemma monotone.csupr_mem_Inter_Icc_of_antitone [nonempty β] [semilattice_sup β] {f g : β → α} (hf : monotone f) (hg : antitone g) (h : f ≤ g) : (⨆ n, f n) ∈ ⋂ n, Icc (f n) (g n) := begin inhabit β, refine mem_Inter.2 (λ n, ⟨le_csupr ⟨g $ default β, forall_range_iff.2 $ λ m, _⟩ _, csupr_le $ λ m, _⟩); exact hf.forall_le_of_antitone hg h _ _ end /-- Nested intervals lemma: if `[f n, g n]` is an antitone sequence of nonempty closed intervals, then `⨆ n, f n` belongs to all the intervals `[f n, g n]`. -/ lemma csupr_mem_Inter_Icc_of_antitone_Icc [nonempty β] [semilattice_sup β] {f g : β → α} (h : antitone (λ n, Icc (f n) (g n))) (h' : ∀ n, f n ≤ g n) : (⨆ n, f n) ∈ ⋂ n, Icc (f n) (g n) := monotone.csupr_mem_Inter_Icc_of_antitone (λ m n hmn, ((Icc_subset_Icc_iff (h' n)).1 (h hmn)).1) (λ m n hmn, ((Icc_subset_Icc_iff (h' n)).1 (h hmn)).2) h' lemma finset.nonempty.sup'_eq_cSup_image {s : finset β} (hs : s.nonempty) (f : β → α) : s.sup' hs f = Sup (f '' s) := eq_of_forall_ge_iff $ λ a, by simp [cSup_le_iff (s.finite_to_set.image f).bdd_above (hs.to_set.image f)] lemma finset.nonempty.sup'_id_eq_cSup {s : finset α} (hs : s.nonempty) : s.sup' hs id = Sup s := by rw [hs.sup'_eq_cSup_image, image_id] end conditionally_complete_lattice instance pi.conditionally_complete_lattice {ι : Type*} {α : Π i : ι, Type*} [Π i, conditionally_complete_lattice (α i)] : conditionally_complete_lattice (Π i, α i) := { le_cSup := λ s f ⟨g, hg⟩ hf i, le_cSup ⟨g i, set.forall_range_iff.2 $ λ ⟨f', hf'⟩, hg hf' i⟩ ⟨⟨f, hf⟩, rfl⟩, cSup_le := λ s f hs hf i, cSup_le (by haveI := hs.to_subtype; apply range_nonempty) $ λ b ⟨⟨g, hg⟩, hb⟩, hb ▸ hf hg i, cInf_le := λ s f ⟨g, hg⟩ hf i, cInf_le ⟨g i, set.forall_range_iff.2 $ λ ⟨f', hf'⟩, hg hf' i⟩ ⟨⟨f, hf⟩, rfl⟩, le_cInf := λ s f hs hf i, le_cInf (by haveI := hs.to_subtype; apply range_nonempty) $ λ b ⟨⟨g, hg⟩, hb⟩, hb ▸ hf hg i, .. pi.lattice, .. pi.has_Sup, .. pi.has_Inf } section conditionally_complete_linear_order variables [conditionally_complete_linear_order α] {s t : set α} {a b : α} lemma finset.nonempty.cSup_eq_max' {s : finset α} (h : s.nonempty) : Sup ↑s = s.max' h := eq_of_forall_ge_iff $ λ a, (cSup_le_iff s.bdd_above h.to_set).trans (s.max'_le_iff h).symm lemma finset.nonempty.cInf_eq_min' {s : finset α} (h : s.nonempty) : Inf ↑s = s.min' h := @finset.nonempty.cSup_eq_max' (order_dual α) _ s h lemma finset.nonempty.cSup_mem {s : finset α} (h : s.nonempty) : Sup (s : set α) ∈ s := by { rw h.cSup_eq_max', exact s.max'_mem _ } lemma finset.nonempty.cInf_mem {s : finset α} (h : s.nonempty) : Inf (s : set α) ∈ s := @finset.nonempty.cSup_mem (order_dual α) _ _ h lemma set.nonempty.cSup_mem (h : s.nonempty) (hs : finite s) : Sup s ∈ s := by { lift s to finset α using hs, exact finset.nonempty.cSup_mem h } lemma set.nonempty.cInf_mem (h : s.nonempty) (hs : finite s) : Inf s ∈ s := @set.nonempty.cSup_mem (order_dual α) _ _ h hs lemma set.finite.cSup_lt_iff (hs : finite s) (h : s.nonempty) : Sup s < a ↔ ∀ x ∈ s, x < a := ⟨λ h x hx, (le_cSup hs.bdd_above hx).trans_lt h, λ H, H _ $ h.cSup_mem hs⟩ lemma set.finite.lt_cInf_iff (hs : finite s) (h : s.nonempty) : a < Inf s ↔ ∀ x ∈ s, a < x := @set.finite.cSup_lt_iff (order_dual α) _ _ _ hs h /-- When b < Sup s, there is an element a in s with b < a, if s is nonempty and the order is a linear order. -/ lemma exists_lt_of_lt_cSup (hs : s.nonempty) (hb : b < Sup s) : ∃a∈s, b < a := begin classical, contrapose! hb, exact cSup_le hs hb end /-- Indexed version of the above lemma `exists_lt_of_lt_cSup`. When `b < supr f`, there is an element `i` such that `b < f i`. -/ lemma exists_lt_of_lt_csupr [nonempty ι] {f : ι → α} (h : b < supr f) : ∃i, b < f i := let ⟨_, ⟨i, rfl⟩, h⟩ := exists_lt_of_lt_cSup (range_nonempty f) h in ⟨i, h⟩ /--When Inf s < b, there is an element a in s with a < b, if s is nonempty and the order is a linear order.-/ lemma exists_lt_of_cInf_lt (hs : s.nonempty) (hb : Inf s < b) : ∃a∈s, a < b := @exists_lt_of_lt_cSup (order_dual α) _ _ _ hs hb /-- Indexed version of the above lemma `exists_lt_of_cInf_lt` When `infi f < a`, there is an element `i` such that `f i < a`. -/ lemma exists_lt_of_cinfi_lt [nonempty ι] {f : ι → α} (h : infi f < a) : (∃i, f i < a) := @exists_lt_of_lt_csupr (order_dual α) _ _ _ _ _ h /--Introduction rule to prove that b is the supremum of s: it suffices to check that 1) b is an upper bound 2) every other upper bound b' satisfies b ≤ b'.-/ theorem cSup_eq_of_is_forall_le_of_forall_le_imp_ge (_ : s.nonempty) (h_is_ub : ∀ a ∈ s, a ≤ b) (h_b_le_ub : ∀ub, (∀ a ∈ s, a ≤ ub) → (b ≤ ub)) : Sup s = b := le_antisymm (show Sup s ≤ b, from cSup_le ‹s.nonempty› h_is_ub) (show b ≤ Sup s, from h_b_le_ub _ $ assume a, le_cSup ⟨b, h_is_ub⟩) open function variables [is_well_order α (<)] lemma Inf_eq_argmin_on (hs : s.nonempty) : Inf s = argmin_on id (@is_well_order.wf α (<) _) s hs := is_least.cInf_eq ⟨argmin_on_mem _ _ _ _, λ a ha, argmin_on_le id _ _ ha⟩ lemma is_least_Inf (hs : s.nonempty) : is_least s (Inf s) := by { rw Inf_eq_argmin_on hs, exact ⟨argmin_on_mem _ _ _ _, λ a ha, argmin_on_le id _ _ ha⟩ } lemma le_cInf_iff' (hs : s.nonempty) : b ≤ Inf s ↔ b ∈ lower_bounds s := le_is_glb_iff (is_least_Inf hs).is_glb lemma Inf_mem (hs : s.nonempty) : Inf s ∈ s := (is_least_Inf hs).1 end conditionally_complete_linear_order /-! ### Lemmas about a conditionally complete linear order with bottom element In this case we have `Sup ∅ = ⊥`, so we can drop some `nonempty`/`set.nonempty` assumptions. -/ section conditionally_complete_linear_order_bot variables [conditionally_complete_linear_order_bot α] lemma cSup_empty : (Sup ∅ : α) = ⊥ := conditionally_complete_linear_order_bot.cSup_empty lemma csupr_of_empty [is_empty ι] (f : ι → α) : (⨆ i, f i) = ⊥ := by rw [supr_of_empty', cSup_empty] @[simp] lemma csupr_false (f : false → α) : (⨆ i, f i) = ⊥ := csupr_of_empty f lemma is_lub_cSup' {s : set α} (hs : bdd_above s) : is_lub s (Sup s) := begin rcases eq_empty_or_nonempty s with (rfl|hne), { simp only [cSup_empty, is_lub_empty] }, { exact is_lub_cSup hne hs } end lemma cSup_le_iff' {s : set α} (hs : bdd_above s) {a : α} : Sup s ≤ a ↔ ∀ x ∈ s, x ≤ a := is_lub_le_iff (is_lub_cSup' hs) lemma cSup_le' {s : set α} {a : α} (h : a ∈ upper_bounds s) : Sup s ≤ a := (cSup_le_iff' ⟨a, h⟩).2 h lemma exists_lt_of_lt_cSup' {s : set α} {a : α} (h : a < Sup s) : ∃ b ∈ s, a < b := by { contrapose! h, exact cSup_le' h } lemma csupr_le_iff' {f : ι → α} (h : bdd_above (range f)) {a : α} : (⨆ i, f i) ≤ a ↔ ∀ i, f i ≤ a := (cSup_le_iff' h).trans forall_range_iff lemma csupr_le' {f : ι → α} {a : α} (h : ∀ i, f i ≤ a) : (⨆ i, f i) ≤ a := cSup_le' $ forall_range_iff.2 h lemma exists_lt_of_lt_csupr' {f : ι → α} {a : α} (h : a < ⨆ i, f i) : ∃ i, a < f i := by { contrapose! h, exact csupr_le' h } end conditionally_complete_linear_order_bot namespace with_top open_locale classical variables [conditionally_complete_linear_order_bot α] /-- The Sup of a non-empty set is its least upper bound for a conditionally complete lattice with a top. -/ lemma is_lub_Sup' {β : Type*} [conditionally_complete_lattice β] {s : set (with_top β)} (hs : s.nonempty) : is_lub s (Sup s) := begin split, { show ite _ _ _ ∈ _, split_ifs, { intros _ _, exact le_top }, { rintro (⟨⟩|a) ha, { contradiction }, apply some_le_some.2, exact le_cSup h_1 ha }, { intros _ _, exact le_top } }, { show ite _ _ _ ∈ _, split_ifs, { rintro (⟨⟩|a) ha, { exact _root_.le_refl _ }, { exact false.elim (not_top_le_coe a (ha h)) } }, { rintro (⟨⟩|b) hb, { exact le_top }, refine some_le_some.2 (cSup_le _ _), { rcases hs with ⟨⟨⟩|b, hb⟩, { exact absurd hb h }, { exact ⟨b, hb⟩ } }, { intros a ha, exact some_le_some.1 (hb ha) } }, { rintro (⟨⟩|b) hb, { exact _root_.le_refl _ }, { exfalso, apply h_1, use b, intros a ha, exact some_le_some.1 (hb ha) } } } end lemma is_lub_Sup (s : set (with_top α)) : is_lub s (Sup s) := begin cases s.eq_empty_or_nonempty with hs hs, { rw hs, show is_lub ∅ (ite _ _ _), split_ifs, { cases h }, { rw [preimage_empty, cSup_empty], exact is_lub_empty }, { exfalso, apply h_1, use ⊥, rintro a ⟨⟩ } }, exact is_lub_Sup' hs, end /-- The Inf of a bounded-below set is its greatest lower bound for a conditionally complete lattice with a top. -/ lemma is_glb_Inf' {β : Type*} [conditionally_complete_lattice β] {s : set (with_top β)} (hs : bdd_below s) : is_glb s (Inf s) := begin split, { show ite _ _ _ ∈ _, split_ifs, { intros a ha, exact top_le_iff.2 (set.mem_singleton_iff.1 (h ha)) }, { rintro (⟨⟩|a) ha, { exact le_top }, refine some_le_some.2 (cInf_le _ ha), rcases hs with ⟨⟨⟩|b, hb⟩, { exfalso, apply h, intros c hc, rw [mem_singleton_iff, ←top_le_iff], exact hb hc }, use b, intros c hc, exact some_le_some.1 (hb hc) } }, { show ite _ _ _ ∈ _, split_ifs, { intros _ _, exact le_top }, { rintro (⟨⟩|a) ha, { exfalso, apply h, intros b hb, exact set.mem_singleton_iff.2 (top_le_iff.1 (ha hb)) }, { refine some_le_some.2 (le_cInf _ _), { classical, contrapose! h, rintros (⟨⟩|a) ha, { exact mem_singleton ⊤ }, { exact (h ⟨a, ha⟩).elim }}, { intros b hb, rw ←some_le_some, exact ha hb } } } } end lemma is_glb_Inf (s : set (with_top α)) : is_glb s (Inf s) := begin by_cases hs : bdd_below s, { exact is_glb_Inf' hs }, { exfalso, apply hs, use ⊥, intros _ _, exact bot_le }, end noncomputable instance : complete_linear_order (with_top α) := { Sup := Sup, le_Sup := assume s, (is_lub_Sup s).1, Sup_le := assume s, (is_lub_Sup s).2, Inf := Inf, le_Inf := assume s, (is_glb_Inf s).2, Inf_le := assume s, (is_glb_Inf s).1, .. with_top.linear_order, ..with_top.lattice, ..with_top.order_top, ..with_top.order_bot } lemma coe_Sup {s : set α} (hb : bdd_above s) : (↑(Sup s) : with_top α) = (⨆a∈s, ↑a) := begin cases s.eq_empty_or_nonempty with hs hs, { rw [hs, cSup_empty], simp only [set.mem_empty_eq, supr_bot, supr_false], refl }, apply le_antisymm, { refine (coe_le_iff.2 $ assume b hb, cSup_le hs $ assume a has, coe_le_coe.1 $ hb ▸ _), exact (le_supr_of_le a $ le_supr_of_le has $ _root_.le_refl _) }, { exact (supr_le $ assume a, supr_le $ assume ha, coe_le_coe.2 $ le_cSup hb ha) } end lemma coe_Inf {s : set α} (hs : s.nonempty) : (↑(Inf s) : with_top α) = (⨅a∈s, ↑a) := let ⟨x, hx⟩ := hs in have (⨅a∈s, ↑a : with_top α) ≤ x, from infi_le_of_le x $ infi_le_of_le hx $ _root_.le_refl _, let ⟨r, r_eq, hr⟩ := le_coe_iff.1 this in le_antisymm (le_infi $ assume a, le_infi $ assume ha, coe_le_coe.2 $ cInf_le (order_bot.bdd_below s) ha) begin refine (r_eq.symm ▸ coe_le_coe.2 $ le_cInf hs $ assume a has, coe_le_coe.1 $ _), refine (r_eq ▸ infi_le_of_le a _), exact (infi_le_of_le has $ _root_.le_refl _), end end with_top namespace monotone variables [preorder α] [conditionally_complete_lattice β] {f : α → β} (h_mono : monotone f) /-! A monotone function into a conditionally complete lattice preserves the ordering properties of `Sup` and `Inf`. -/ lemma le_cSup_image {s : set α} {c : α} (hcs : c ∈ s) (h_bdd : bdd_above s) : f c ≤ Sup (f '' s) := le_cSup (map_bdd_above h_mono h_bdd) (mem_image_of_mem f hcs) lemma cSup_image_le {s : set α} (hs : s.nonempty) {B : α} (hB: B ∈ upper_bounds s) : Sup (f '' s) ≤ f B := cSup_le (nonempty.image f hs) (h_mono.mem_upper_bounds_image hB) lemma cInf_image_le {s : set α} {c : α} (hcs : c ∈ s) (h_bdd : bdd_below s) : Inf (f '' s) ≤ f c := @le_cSup_image (order_dual α) (order_dual β) _ _ _ (λ x y hxy, h_mono hxy) _ _ hcs h_bdd lemma le_cInf_image {s : set α} (hs : s.nonempty) {B : α} (hB: B ∈ lower_bounds s) : f B ≤ Inf (f '' s) := @cSup_image_le (order_dual α) (order_dual β) _ _ _ (λ x y hxy, h_mono hxy) _ hs _ hB end monotone namespace galois_connection variables {γ : Type*} [conditionally_complete_lattice α] [conditionally_complete_lattice β] [nonempty ι] {l : α → β} {u : β → α} lemma l_cSup (gc : galois_connection l u) {s : set α} (hne : s.nonempty) (hbdd : bdd_above s) : l (Sup s) = ⨆ x : s, l x := eq.symm $ is_lub.csupr_set_eq (gc.is_lub_l_image $ is_lub_cSup hne hbdd) hne lemma l_cSup' (gc : galois_connection l u) {s : set α} (hne : s.nonempty) (hbdd : bdd_above s) : l (Sup s) = Sup (l '' s) := by rw [gc.l_cSup hne hbdd, csupr_set] lemma l_csupr (gc : galois_connection l u) {f : ι → α} (hf : bdd_above (range f)) : l (⨆ i, f i) = ⨆ i, l (f i) := by rw [supr, gc.l_cSup (range_nonempty _) hf, supr_range'] lemma l_csupr_set (gc : galois_connection l u) {s : set γ} {f : γ → α} (hf : bdd_above (f '' s)) (hne : s.nonempty) : l (⨆ i : s, f i) = ⨆ i : s, l (f i) := by { haveI := hne.to_subtype, rw image_eq_range at hf, exact gc.l_csupr hf } lemma u_cInf (gc : galois_connection l u) {s : set β} (hne : s.nonempty) (hbdd : bdd_below s) : u (Inf s) = ⨅ x : s, u x := gc.dual.l_cSup hne hbdd lemma u_cInf' (gc : galois_connection l u) {s : set β} (hne : s.nonempty) (hbdd : bdd_below s) : u (Inf s) = Inf (u '' s) := gc.dual.l_cSup' hne hbdd lemma u_cinfi (gc : galois_connection l u) {f : ι → β} (hf : bdd_below (range f)) : u (⨅ i, f i) = ⨅ i, u (f i) := gc.dual.l_csupr hf lemma u_cinfi_set (gc : galois_connection l u) {s : set γ} {f : γ → β} (hf : bdd_below (f '' s)) (hne : s.nonempty) : u (⨅ i : s, f i) = ⨅ i : s, u (f i) := gc.dual.l_csupr_set hf hne end galois_connection namespace order_iso variables {γ : Type*} [conditionally_complete_lattice α] [conditionally_complete_lattice β] [nonempty ι] lemma map_cSup (e : α ≃o β) {s : set α} (hne : s.nonempty) (hbdd : bdd_above s) : e (Sup s) = ⨆ x : s, e x := e.to_galois_connection.l_cSup hne hbdd lemma map_cSup' (e : α ≃o β) {s : set α} (hne : s.nonempty) (hbdd : bdd_above s) : e (Sup s) = Sup (e '' s) := e.to_galois_connection.l_cSup' hne hbdd lemma map_csupr (e : α ≃o β) {f : ι → α} (hf : bdd_above (range f)) : e (⨆ i, f i) = ⨆ i, e (f i) := e.to_galois_connection.l_csupr hf lemma map_csupr_set (e : α ≃o β) {s : set γ} {f : γ → α} (hf : bdd_above (f '' s)) (hne : s.nonempty) : e (⨆ i : s, f i) = ⨆ i : s, e (f i) := e.to_galois_connection.l_csupr_set hf hne lemma map_cInf (e : α ≃o β) {s : set α} (hne : s.nonempty) (hbdd : bdd_below s) : e (Inf s) = ⨅ x : s, e x := e.dual.map_cSup hne hbdd lemma map_cInf' (e : α ≃o β) {s : set α} (hne : s.nonempty) (hbdd : bdd_below s) : e (Inf s) = Inf (e '' s) := e.dual.map_cSup' hne hbdd lemma map_cinfi (e : α ≃o β) {f : ι → α} (hf : bdd_below (range f)) : e (⨅ i, f i) = ⨅ i, e (f i) := e.dual.map_csupr hf lemma map_cinfi_set (e : α ≃o β) {s : set γ} {f : γ → α} (hf : bdd_below (f '' s)) (hne : s.nonempty) : e (⨅ i : s, f i) = ⨅ i : s, e (f i) := e.dual.map_csupr_set hf hne end order_iso /-! ### Relation between `Sup` / `Inf` and `finset.sup'` / `finset.inf'` Like the `Sup` of a `conditionally_complete_lattice`, `finset.sup'` also requires the set to be non-empty. As a result, we can translate between the two. -/ namespace finset lemma sup'_eq_cSup_image [conditionally_complete_lattice β] (s : finset α) (H) (f : α → β) : s.sup' H f = Sup (f '' s) := begin apply le_antisymm, { refine (finset.sup'_le _ _ $ λ a ha, _), refine le_cSup ⟨s.sup' H f, _⟩ ⟨a, ha, rfl⟩, rintros i ⟨j, hj, rfl⟩, exact finset.le_sup' _ hj }, { apply cSup_le ((coe_nonempty.mpr H).image _), rintros _ ⟨a, ha, rfl⟩, exact finset.le_sup' _ ha, } end lemma inf'_eq_cInf_image [conditionally_complete_lattice β] (s : finset α) (H) (f : α → β) : s.inf' H f = Inf (f '' s) := @sup'_eq_cSup_image _ (order_dual β) _ _ _ _ lemma sup'_id_eq_cSup [conditionally_complete_lattice α] (s : finset α) (H) : s.sup' H id = Sup s := by rw [sup'_eq_cSup_image s H, set.image_id] lemma inf'_id_eq_cInf [conditionally_complete_lattice α] (s : finset α) (H) : s.inf' H id = Inf s := @sup'_id_eq_cSup (order_dual α) _ _ _ end finset section with_top_bot /-! ### Complete lattice structure on `with_top (with_bot α)` If `α` is a `conditionally_complete_lattice`, then we show that `with_top α` and `with_bot α` also inherit the structure of conditionally complete lattices. Furthermore, we show that `with_top (with_bot α)` naturally inherits the structure of a complete lattice. Note that for α a conditionally complete lattice, `Sup` and `Inf` both return junk values for sets which are empty or unbounded. The extension of `Sup` to `with_top α` fixes the unboundedness problem and the extension to `with_bot α` fixes the problem with the empty set. This result can be used to show that the extended reals [-∞, ∞] are a complete lattice. -/ open_locale classical /-- Adding a top element to a conditionally complete lattice gives a conditionally complete lattice -/ noncomputable instance with_top.conditionally_complete_lattice {α : Type*} [conditionally_complete_lattice α] : conditionally_complete_lattice (with_top α) := { le_cSup := λ S a hS haS, (with_top.is_lub_Sup' ⟨a, haS⟩).1 haS, cSup_le := λ S a hS haS, (with_top.is_lub_Sup' hS).2 haS, cInf_le := λ S a hS haS, (with_top.is_glb_Inf' hS).1 haS, le_cInf := λ S a hS haS, (with_top.is_glb_Inf' ⟨a, haS⟩).2 haS, ..with_top.lattice, ..with_top.has_Sup, ..with_top.has_Inf } /-- Adding a bottom element to a conditionally complete lattice gives a conditionally complete lattice -/ noncomputable instance with_bot.conditionally_complete_lattice {α : Type*} [conditionally_complete_lattice α] : conditionally_complete_lattice (with_bot α) := { le_cSup := (@with_top.conditionally_complete_lattice (order_dual α) _).cInf_le, cSup_le := (@with_top.conditionally_complete_lattice (order_dual α) _).le_cInf, cInf_le := (@with_top.conditionally_complete_lattice (order_dual α) _).le_cSup, le_cInf := (@with_top.conditionally_complete_lattice (order_dual α) _).cSup_le, ..with_bot.lattice, ..with_bot.has_Sup, ..with_bot.has_Inf } /-- Adding a bottom and a top to a conditionally complete lattice gives a bounded lattice-/ noncomputable instance with_top.with_bot.bounded_lattice {α : Type*} [conditionally_complete_lattice α] : bounded_lattice (with_top (with_bot α)) := { ..with_top.order_bot, ..with_top.order_top, ..conditionally_complete_lattice.to_lattice _ } noncomputable instance with_top.with_bot.complete_lattice {α : Type*} [conditionally_complete_lattice α] : complete_lattice (with_top (with_bot α)) := { le_Sup := λ S a haS, (with_top.is_lub_Sup' ⟨a, haS⟩).1 haS, Sup_le := λ S a ha, begin cases S.eq_empty_or_nonempty with h, { show ite _ _ _ ≤ a, split_ifs, { rw h at h_1, cases h_1 }, { convert bot_le, convert with_bot.cSup_empty, rw h, refl }, { exfalso, apply h_2, use ⊥, rw h, rintro b ⟨⟩ } }, { refine (with_top.is_lub_Sup' h).2 ha } end, Inf_le := λ S a haS, show ite _ _ _ ≤ a, begin split_ifs, { cases a with a, exact _root_.le_refl _, cases (h haS); tauto }, { cases a, { exact le_top }, { apply with_top.some_le_some.2, refine cInf_le _ haS, use ⊥, intros b hb, exact bot_le } } end, le_Inf := λ S a haS, (with_top.is_glb_Inf' ⟨a, haS⟩).2 haS, ..with_top.has_Inf, ..with_top.has_Sup, ..with_top.with_bot.bounded_lattice } noncomputable instance with_top.with_bot.complete_linear_order {α : Type*} [conditionally_complete_linear_order α] : complete_linear_order (with_top (with_bot α)) := { .. with_top.with_bot.complete_lattice, .. with_top.linear_order } end with_top_bot section subtype variables (s : set α) /-! ### Subtypes of conditionally complete linear orders In this section we give conditions on a subset of a conditionally complete linear order, to ensure that the subtype is itself conditionally complete. We check that an `ord_connected` set satisfies these conditions. TODO There are several possible variants; the `conditionally_complete_linear_order` could be changed to `conditionally_complete_linear_order_bot` or `complete_linear_order`. -/ open_locale classical section has_Sup variables [has_Sup α] /-- `has_Sup` structure on a nonempty subset `s` of an object with `has_Sup`. This definition is non-canonical (it uses `default s`); it should be used only as here, as an auxiliary instance in the construction of the `conditionally_complete_linear_order` structure. -/ noncomputable def subset_has_Sup [inhabited s] : has_Sup s := {Sup := λ t, if ht : Sup (coe '' t : set α) ∈ s then ⟨Sup (coe '' t : set α), ht⟩ else default s} local attribute [instance] subset_has_Sup @[simp] lemma subset_Sup_def [inhabited s] : @Sup s _ = λ t, if ht : Sup (coe '' t : set α) ∈ s then ⟨Sup (coe '' t : set α), ht⟩ else default s := rfl lemma subset_Sup_of_within [inhabited s] {t : set s} (h : Sup (coe '' t : set α) ∈ s) : Sup (coe '' t : set α) = (@Sup s _ t : α) := by simp [dif_pos h] end has_Sup section has_Inf variables [has_Inf α] /-- `has_Inf` structure on a nonempty subset `s` of an object with `has_Inf`. This definition is non-canonical (it uses `default s`); it should be used only as here, as an auxiliary instance in the construction of the `conditionally_complete_linear_order` structure. -/ noncomputable def subset_has_Inf [inhabited s] : has_Inf s := {Inf := λ t, if ht : Inf (coe '' t : set α) ∈ s then ⟨Inf (coe '' t : set α), ht⟩ else default s} local attribute [instance] subset_has_Inf @[simp] lemma subset_Inf_def [inhabited s] : @Inf s _ = λ t, if ht : Inf (coe '' t : set α) ∈ s then ⟨Inf (coe '' t : set α), ht⟩ else default s := rfl lemma subset_Inf_of_within [inhabited s] {t : set s} (h : Inf (coe '' t : set α) ∈ s) : Inf (coe '' t : set α) = (@Inf s _ t : α) := by simp [dif_pos h] end has_Inf variables [conditionally_complete_linear_order α] local attribute [instance] subset_has_Sup local attribute [instance] subset_has_Inf /-- For a nonempty subset of a conditionally complete linear order to be a conditionally complete linear order, it suffices that it contain the `Sup` of all its nonempty bounded-above subsets, and the `Inf` of all its nonempty bounded-below subsets. -/ noncomputable def subset_conditionally_complete_linear_order [inhabited s] (h_Sup : ∀ {t : set s} (ht : t.nonempty) (h_bdd : bdd_above t), Sup (coe '' t : set α) ∈ s) (h_Inf : ∀ {t : set s} (ht : t.nonempty) (h_bdd : bdd_below t), Inf (coe '' t : set α) ∈ s) : conditionally_complete_linear_order s := { le_cSup := begin rintros t c h_bdd hct, -- The following would be a more natural way to finish, but gives a "deep recursion" error: -- simpa [subset_Sup_of_within (h_Sup t)] using -- (strict_mono_coe s).monotone.le_cSup_image hct h_bdd, have := (subtype.mono_coe s).le_cSup_image hct h_bdd, rwa subset_Sup_of_within s (h_Sup ⟨c, hct⟩ h_bdd) at this, end, cSup_le := begin rintros t B ht hB, have := (subtype.mono_coe s).cSup_image_le ht hB, rwa subset_Sup_of_within s (h_Sup ht ⟨B, hB⟩) at this, end, le_cInf := begin intros t B ht hB, have := (subtype.mono_coe s).le_cInf_image ht hB, rwa subset_Inf_of_within s (h_Inf ht ⟨B, hB⟩) at this, end, cInf_le := begin rintros t c h_bdd hct, have := (subtype.mono_coe s).cInf_image_le hct h_bdd, rwa subset_Inf_of_within s (h_Inf ⟨c, hct⟩ h_bdd) at this, end, ..subset_has_Sup s, ..subset_has_Inf s, ..distrib_lattice.to_lattice s, ..(infer_instance : linear_order s) } section ord_connected /-- The `Sup` function on a nonempty `ord_connected` set `s` in a conditionally complete linear order takes values within `s`, for all nonempty bounded-above subsets of `s`. -/ lemma Sup_within_of_ord_connected {s : set α} [hs : ord_connected s] ⦃t : set s⦄ (ht : t.nonempty) (h_bdd : bdd_above t) : Sup (coe '' t : set α) ∈ s := begin obtain ⟨c, hct⟩ : ∃ c, c ∈ t := ht, obtain ⟨B, hB⟩ : ∃ B, B ∈ upper_bounds t := h_bdd, refine hs.out c.2 B.2 ⟨_, _⟩, { exact (subtype.mono_coe s).le_cSup_image hct ⟨B, hB⟩ }, { exact (subtype.mono_coe s).cSup_image_le ⟨c, hct⟩ hB }, end /-- The `Inf` function on a nonempty `ord_connected` set `s` in a conditionally complete linear order takes values within `s`, for all nonempty bounded-below subsets of `s`. -/ lemma Inf_within_of_ord_connected {s : set α} [hs : ord_connected s] ⦃t : set s⦄ (ht : t.nonempty) (h_bdd : bdd_below t) : Inf (coe '' t : set α) ∈ s := begin obtain ⟨c, hct⟩ : ∃ c, c ∈ t := ht, obtain ⟨B, hB⟩ : ∃ B, B ∈ lower_bounds t := h_bdd, refine hs.out B.2 c.2 ⟨_, _⟩, { exact (subtype.mono_coe s).le_cInf_image ⟨c, hct⟩ hB }, { exact (subtype.mono_coe s).cInf_image_le hct ⟨B, hB⟩ }, end /-- A nonempty `ord_connected` set in a conditionally complete linear order is naturally a conditionally complete linear order. -/ noncomputable instance ord_connected_subset_conditionally_complete_linear_order [inhabited s] [ord_connected s] : conditionally_complete_linear_order s := subset_conditionally_complete_linear_order s Sup_within_of_ord_connected Inf_within_of_ord_connected end ord_connected end subtype
abe9c28c243bdb42fec2b87d762d75d3421b7421
6065973b1fa7bbacba932011c9e2f32bf7bdd6c1
/src/data/set/basic.lean
3eec61aa88ab153dd31546663863eb555337c3fe
[ "Apache-2.0" ]
permissive
khmacdonald/mathlib
90a0fa2222369fa69ed2fbfb841b74d2bdfd66cb
3669cb35c578441812ad30fd967d21a94b6f387e
refs/heads/master
1,675,863,801,090
1,609,761,876,000
1,609,761,876,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
95,211
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ import logic.unique import order.boolean_algebra /-! # Basic properties of sets Sets in Lean are homogeneous; all their elements have the same type. Sets whose elements have type `X` are thus defined as `set X := X → Prop`. Note that this function need not be decidable. The definition is in the core library. This file provides some basic definitions related to sets and functions not present in the core library, as well as extra lemmas for functions in the core library (empty set, univ, union, intersection, insert, singleton, set-theoretic difference, complement, and powerset). Note that a set is a term, not a type. There is a coersion from `set α` to `Type*` sending `s` to the corresponding subtype `↥s`. See also the file `set_theory/zfc.lean`, which contains an encoding of ZFC set theory in Lean. ## Main definitions Notation used here: - `f : α → β` is a function, - `s : set α` and `s₁ s₂ : set α` are subsets of `α` - `t : set β` is a subset of `β`. Definitions in the file: * `strict_subset s₁ s₂ : Prop` : the predicate `s₁ ⊆ s₂` but `s₁ ≠ s₂`. * `nonempty s : Prop` : the predicate `s ≠ ∅`. Note that this is the preferred way to express the fact that `s` has an element (see the Implementation Notes). * `preimage f t : set α` : the preimage f⁻¹(t) (written `f ⁻¹' t` in Lean) of a subset of β. * `subsingleton s : Prop` : the predicate saying that `s` has at most one element. * `range f : set β` : the image of `univ` under `f`. Also works for `{p : Prop} (f : p → α)` (unlike `image`) * `prod s t : set (α × β)` : the subset `s × t`. * `inclusion s₁ s₂ : ↥s₁ → ↥s₂` : the map `↥s₁ → ↥s₂` induced by an inclusion `s₁ ⊆ s₂`. ## Notation * `f ⁻¹' t` for `preimage f t` * `f '' s` for `image f s` * `sᶜ` for the complement of `s` ## Implementation notes * `s.nonempty` is to be preferred to `s ≠ ∅` or `∃ x, x ∈ s`. It has the advantage that the `s.nonempty` dot notation can be used. * For `s : set α`, do not use `subtype s`. Instead use `↥s` or `(s : Type*)` or `s`. ## Tags set, sets, subset, subsets, image, preimage, pre-image, range, union, intersection, insert, singleton, complement, powerset -/ /-! ### Set coercion to a type -/ open function universe variables u v w x run_cmd do e ← tactic.get_env, tactic.set_env $ e.mk_protected `set.compl namespace set variable {α : Type*} instance : has_le (set α) := ⟨(⊆)⟩ instance : has_lt (set α) := ⟨λ s t, s ≤ t ∧ ¬t ≤ s⟩ -- `⊂` is not defined until further down instance {α : Type*} : boolean_algebra (set α) := { sup := (∪), le := (≤), lt := (<), inf := (∩), bot := ∅, compl := set.compl, top := univ, sdiff := (\), .. (infer_instance : boolean_algebra (α → Prop)) } @[simp] lemma top_eq_univ : (⊤ : set α) = univ := rfl @[simp] lemma bot_eq_empty : (⊥ : set α) = ∅ := rfl @[simp] lemma sup_eq_union (s t : set α) : s ⊔ t = s ∪ t := rfl @[simp] lemma inf_eq_inter (s t : set α) : s ⊓ t = s ∩ t := rfl @[simp] lemma le_eq_subset (s t : set α) : s ≤ t = (s ⊆ t) := rfl /-! `set.lt_eq_ssubset` is defined further down -/ /-- Coercion from a set to the corresponding subtype. -/ instance {α : Type*} : has_coe_to_sort (set α) := ⟨_, λ s, {x // x ∈ s}⟩ end set section set_coe variables {α : Type u} theorem set.set_coe_eq_subtype (s : set α) : coe_sort.{(u+1) (u+2)} s = {x // x ∈ s} := rfl @[simp] theorem set_coe.forall {s : set α} {p : s → Prop} : (∀ x : s, p x) ↔ (∀ x (h : x ∈ s), p ⟨x, h⟩) := subtype.forall @[simp] theorem set_coe.exists {s : set α} {p : s → Prop} : (∃ x : s, p x) ↔ (∃ x (h : x ∈ s), p ⟨x, h⟩) := subtype.exists theorem set_coe.exists' {s : set α} {p : Π x, x ∈ s → Prop} : (∃ x (h : x ∈ s), p x h) ↔ (∃ x : s, p x x.2) := (@set_coe.exists _ _ $ λ x, p x.1 x.2).symm theorem set_coe.forall' {s : set α} {p : Π x, x ∈ s → Prop} : (∀ x (h : x ∈ s), p x h) ↔ (∀ x : s, p x x.2) := (@set_coe.forall _ _ $ λ x, p x.1 x.2).symm @[simp] theorem set_coe_cast : ∀ {s t : set α} (H' : s = t) (H : @eq (Type u) s t) (x : s), cast H x = ⟨x.1, H' ▸ x.2⟩ | s _ rfl _ ⟨x, h⟩ := rfl theorem set_coe.ext {s : set α} {a b : s} : (↑a : α) = ↑b → a = b := subtype.eq theorem set_coe.ext_iff {s : set α} {a b : s} : (↑a : α) = ↑b ↔ a = b := iff.intro set_coe.ext (assume h, h ▸ rfl) end set_coe /-- See also `subtype.prop` -/ lemma subtype.mem {α : Type*} {s : set α} (p : s) : (p : α) ∈ s := p.prop lemma eq.subset {α} {s t : set α} : s = t → s ⊆ t := by { rintro rfl x hx, exact hx } namespace set variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {a : α} {s t : set α} instance : inhabited (set α) := ⟨∅⟩ @[ext] theorem ext {a b : set α} (h : ∀ x, x ∈ a ↔ x ∈ b) : a = b := funext (assume x, propext (h x)) theorem ext_iff {s t : set α} : s = t ↔ ∀ x, x ∈ s ↔ x ∈ t := ⟨λ h x, by rw h, ext⟩ @[trans] theorem mem_of_mem_of_subset {x : α} {s t : set α} (hx : x ∈ s) (h : s ⊆ t) : x ∈ t := h hx /-! ### Lemmas about `mem` and `set_of` -/ @[simp] theorem mem_set_of_eq {a : α} {p : α → Prop} : a ∈ {a | p a} = p a := rfl theorem nmem_set_of_eq {a : α} {P : α → Prop} : a ∉ {a : α | P a} = ¬ P a := rfl @[simp] theorem set_of_mem_eq {s : set α} : {x | x ∈ s} = s := rfl theorem set_of_set {s : set α} : set_of s = s := rfl lemma set_of_app_iff {p : α → Prop} {x : α} : { x | p x } x ↔ p x := iff.rfl theorem mem_def {a : α} {s : set α} : a ∈ s ↔ s a := iff.rfl instance decidable_mem (s : set α) [H : decidable_pred s] : ∀ a, decidable (a ∈ s) := H instance decidable_set_of (p : α → Prop) [H : decidable_pred p] : decidable_pred {a | p a} := H @[simp] theorem set_of_subset_set_of {p q : α → Prop} : {a | p a} ⊆ {a | q a} ↔ (∀a, p a → q a) := iff.rfl @[simp] lemma sep_set_of {p q : α → Prop} : {a ∈ {a | p a } | q a} = {a | p a ∧ q a} := rfl lemma set_of_and {p q : α → Prop} : {a | p a ∧ q a} = {a | p a} ∩ {a | q a} := rfl lemma set_of_or {p q : α → Prop} : {a | p a ∨ q a} = {a | p a} ∪ {a | q a} := rfl /-! ### Lemmas about subsets -/ -- TODO(Jeremy): write a tactic to unfold specific instances of generic notation? theorem subset_def {s t : set α} : (s ⊆ t) = ∀ x, x ∈ s → x ∈ t := rfl @[refl] theorem subset.refl (a : set α) : a ⊆ a := assume x, id theorem subset.rfl {s : set α} : s ⊆ s := subset.refl s @[trans] theorem subset.trans {a b c : set α} (ab : a ⊆ b) (bc : b ⊆ c) : a ⊆ c := assume x h, bc (ab h) @[trans] theorem mem_of_eq_of_mem {x y : α} {s : set α} (hx : x = y) (h : y ∈ s) : x ∈ s := hx.symm ▸ h theorem subset.antisymm {a b : set α} (h₁ : a ⊆ b) (h₂ : b ⊆ a) : a = b := ext (λ x, iff.intro (λ ina, h₁ ina) (λ inb, h₂ inb)) theorem subset.antisymm_iff {a b : set α} : a = b ↔ a ⊆ b ∧ b ⊆ a := ⟨λ e, e ▸ ⟨subset.refl _, subset.refl _⟩, λ ⟨h₁, h₂⟩, subset.antisymm h₁ h₂⟩ -- an alternative name theorem eq_of_subset_of_subset {a b : set α} (h₁ : a ⊆ b) (h₂ : b ⊆ a) : a = b := subset.antisymm h₁ h₂ theorem mem_of_subset_of_mem {s₁ s₂ : set α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := assume h₁ h₂, h₁ h₂ theorem not_subset : (¬ s ⊆ t) ↔ ∃a ∈ s, a ∉ t := by simp [subset_def, not_forall] /-! ### Definition of strict subsets `s ⊂ t` and basic properties. -/ instance : has_ssubset (set α) := ⟨(<)⟩ @[simp] lemma lt_eq_ssubset (s t : set α) : s < t = (s ⊂ t) := rfl theorem ssubset_def : (s ⊂ t) = (s ⊆ t ∧ ¬ (t ⊆ s)) := rfl theorem eq_or_ssubset_of_subset (h : s ⊆ t) : s = t ∨ s ⊂ t := classical.by_cases (λ H : t ⊆ s, or.inl $ subset.antisymm h H) (λ H, or.inr ⟨h, H⟩) lemma exists_of_ssubset {s t : set α} (h : s ⊂ t) : (∃x∈t, x ∉ s) := not_subset.1 h.2 lemma ssubset_iff_subset_ne {s t : set α} : s ⊂ t ↔ s ⊆ t ∧ s ≠ t := by split; simp [set.ssubset_def, ne.def, set.subset.antisymm_iff] {contextual := tt} lemma ssubset_iff_of_subset {s t : set α} (h : s ⊆ t) : s ⊂ t ↔ ∃ x ∈ t, x ∉ s := ⟨exists_of_ssubset, λ ⟨x, hxt, hxs⟩, ⟨h, λ h, hxs $ h hxt⟩⟩ theorem not_mem_empty (x : α) : ¬ (x ∈ (∅ : set α)) := assume h : x ∈ ∅, h @[simp] theorem not_not_mem : ¬ (a ∉ s) ↔ a ∈ s := by { classical, exact not_not } /-! ### Non-empty sets -/ /-- The property `s.nonempty` expresses the fact that the set `s` is not empty. It should be used in theorem assumptions instead of `∃ x, x ∈ s` or `s ≠ ∅` as it gives access to a nice API thanks to the dot notation. -/ protected def nonempty (s : set α) : Prop := ∃ x, x ∈ s lemma nonempty_def : s.nonempty ↔ ∃ x, x ∈ s := iff.rfl lemma nonempty_of_mem {x} (h : x ∈ s) : s.nonempty := ⟨x, h⟩ theorem nonempty.not_subset_empty : s.nonempty → ¬(s ⊆ ∅) | ⟨x, hx⟩ hs := hs hx theorem nonempty.ne_empty : s.nonempty → s ≠ ∅ | ⟨x, hx⟩ hs := by { rw hs at hx, exact hx } /-- Extract a witness from `s.nonempty`. This function might be used instead of case analysis on the argument. Note that it makes a proof depend on the `classical.choice` axiom. -/ protected noncomputable def nonempty.some (h : s.nonempty) : α := classical.some h protected lemma nonempty.some_mem (h : s.nonempty) : h.some ∈ s := classical.some_spec h lemma nonempty.mono (ht : s ⊆ t) (hs : s.nonempty) : t.nonempty := hs.imp ht lemma nonempty_of_not_subset (h : ¬s ⊆ t) : (s \ t).nonempty := let ⟨x, xs, xt⟩ := not_subset.1 h in ⟨x, xs, xt⟩ lemma nonempty_of_ssubset (ht : s ⊂ t) : (t \ s).nonempty := nonempty_of_not_subset ht.2 lemma nonempty.of_diff (h : (s \ t).nonempty) : s.nonempty := h.imp $ λ _, and.left lemma nonempty_of_ssubset' (ht : s ⊂ t) : t.nonempty := (nonempty_of_ssubset ht).of_diff lemma nonempty.inl (hs : s.nonempty) : (s ∪ t).nonempty := hs.imp $ λ _, or.inl lemma nonempty.inr (ht : t.nonempty) : (s ∪ t).nonempty := ht.imp $ λ _, or.inr @[simp] lemma union_nonempty : (s ∪ t).nonempty ↔ s.nonempty ∨ t.nonempty := exists_or_distrib lemma nonempty.left (h : (s ∩ t).nonempty) : s.nonempty := h.imp $ λ _, and.left lemma nonempty.right (h : (s ∩ t).nonempty) : t.nonempty := h.imp $ λ _, and.right lemma nonempty_inter_iff_exists_right : (s ∩ t).nonempty ↔ ∃ x : t, ↑x ∈ s := ⟨λ ⟨x, xs, xt⟩, ⟨⟨x, xt⟩, xs⟩, λ ⟨⟨x, xt⟩, xs⟩, ⟨x, xs, xt⟩⟩ lemma nonempty_inter_iff_exists_left : (s ∩ t).nonempty ↔ ∃ x : s, ↑x ∈ t := ⟨λ ⟨x, xs, xt⟩, ⟨⟨x, xs⟩, xt⟩, λ ⟨⟨x, xt⟩, xs⟩, ⟨x, xt, xs⟩⟩ lemma nonempty_iff_univ_nonempty : nonempty α ↔ (univ : set α).nonempty := ⟨λ ⟨x⟩, ⟨x, trivial⟩, λ ⟨x, _⟩, ⟨x⟩⟩ @[simp] lemma univ_nonempty : ∀ [h : nonempty α], (univ : set α).nonempty | ⟨x⟩ := ⟨x, trivial⟩ lemma nonempty.to_subtype (h : s.nonempty) : nonempty s := nonempty_subtype.2 h instance [nonempty α] : nonempty (set.univ : set α) := set.univ_nonempty.to_subtype @[simp] lemma nonempty_insert (a : α) (s : set α) : (insert a s).nonempty := ⟨a, or.inl rfl⟩ lemma nonempty_of_nonempty_subtype [nonempty s] : s.nonempty := nonempty_subtype.mp ‹_› /-! ### Lemmas about the empty set -/ theorem empty_def : (∅ : set α) = {x | false} := rfl @[simp] theorem mem_empty_eq (x : α) : x ∈ (∅ : set α) = false := rfl @[simp] theorem set_of_false : {a : α | false} = ∅ := rfl theorem eq_empty_iff_forall_not_mem {s : set α} : s = ∅ ↔ ∀ x, x ∉ s := by simp [ext_iff] @[simp] theorem empty_subset (s : set α) : ∅ ⊆ s := assume x, assume h, false.elim h theorem subset_empty_iff {s : set α} : s ⊆ ∅ ↔ s = ∅ := by simp [subset.antisymm_iff] theorem eq_empty_of_subset_empty {s : set α} : s ⊆ ∅ → s = ∅ := subset_empty_iff.1 theorem eq_empty_of_not_nonempty (h : ¬nonempty α) (s : set α) : s = ∅ := eq_empty_of_subset_empty $ λ x hx, h ⟨x⟩ lemma not_nonempty_iff_eq_empty {s : set α} : ¬s.nonempty ↔ s = ∅ := by simp only [set.nonempty, eq_empty_iff_forall_not_mem, not_exists] lemma empty_not_nonempty : ¬(∅ : set α).nonempty := not_nonempty_iff_eq_empty.2 rfl lemma eq_empty_or_nonempty (s : set α) : s = ∅ ∨ s.nonempty := classical.by_cases or.inr (λ h, or.inl $ not_nonempty_iff_eq_empty.1 h) theorem ne_empty_iff_nonempty : s ≠ ∅ ↔ s.nonempty := (not_congr not_nonempty_iff_eq_empty.symm).trans not_not theorem subset_eq_empty {s t : set α} (h : t ⊆ s) (e : s = ∅) : t = ∅ := subset_empty_iff.1 $ e ▸ h theorem ball_empty_iff {p : α → Prop} : (∀ x ∈ (∅ : set α), p x) ↔ true := by simp [iff_def] /-! ### Universal set. In Lean `@univ α` (or `univ : set α`) is the set that contains all elements of type `α`. Mathematically it is the same as `α` but it has a different type. -/ @[simp] theorem set_of_true : {x : α | true} = univ := rfl @[simp] theorem mem_univ (x : α) : x ∈ @univ α := trivial theorem empty_ne_univ [h : nonempty α] : (∅ : set α) ≠ univ := by simp [ext_iff] @[simp] theorem subset_univ (s : set α) : s ⊆ univ := λ x H, trivial theorem univ_subset_iff {s : set α} : univ ⊆ s ↔ s = univ := by simp [subset.antisymm_iff] theorem eq_univ_of_univ_subset {s : set α} : univ ⊆ s → s = univ := univ_subset_iff.1 theorem eq_univ_iff_forall {s : set α} : s = univ ↔ ∀ x, x ∈ s := by simp [ext_iff] theorem eq_univ_of_forall {s : set α} : (∀ x, x ∈ s) → s = univ := eq_univ_iff_forall.2 lemma eq_univ_of_subset {s t : set α} (h : s ⊆ t) (hs : s = univ) : t = univ := eq_univ_of_univ_subset $ hs ▸ h @[simp] lemma univ_eq_empty_iff : (univ : set α) = ∅ ↔ ¬ nonempty α := eq_empty_iff_forall_not_mem.trans ⟨λ H ⟨x⟩, H x trivial, λ H x _, H ⟨x⟩⟩ lemma exists_mem_of_nonempty (α) : ∀ [nonempty α], ∃x:α, x ∈ (univ : set α) | ⟨x⟩ := ⟨x, trivial⟩ instance univ_decidable : decidable_pred (@set.univ α) := λ x, is_true trivial /-- `diagonal α` is the subset of `α × α` consisting of all pairs of the form `(a, a)`. -/ def diagonal (α : Type*) : set (α × α) := {p | p.1 = p.2} @[simp] lemma mem_diagonal {α : Type*} (x : α) : (x, x) ∈ diagonal α := by simp [diagonal] /-! ### Lemmas about union -/ theorem union_def {s₁ s₂ : set α} : s₁ ∪ s₂ = {a | a ∈ s₁ ∨ a ∈ s₂} := rfl theorem mem_union_left {x : α} {a : set α} (b : set α) : x ∈ a → x ∈ a ∪ b := or.inl theorem mem_union_right {x : α} {b : set α} (a : set α) : x ∈ b → x ∈ a ∪ b := or.inr theorem mem_or_mem_of_mem_union {x : α} {a b : set α} (H : x ∈ a ∪ b) : x ∈ a ∨ x ∈ b := H theorem mem_union.elim {x : α} {a b : set α} {P : Prop} (H₁ : x ∈ a ∪ b) (H₂ : x ∈ a → P) (H₃ : x ∈ b → P) : P := or.elim H₁ H₂ H₃ theorem mem_union (x : α) (a b : set α) : x ∈ a ∪ b ↔ x ∈ a ∨ x ∈ b := iff.rfl @[simp] theorem mem_union_eq (x : α) (a b : set α) : x ∈ a ∪ b = (x ∈ a ∨ x ∈ b) := rfl @[simp] theorem union_self (a : set α) : a ∪ a = a := ext (assume x, or_self _) @[simp] theorem union_empty (a : set α) : a ∪ ∅ = a := ext (assume x, or_false _) @[simp] theorem empty_union (a : set α) : ∅ ∪ a = a := ext (assume x, false_or _) theorem union_comm (a b : set α) : a ∪ b = b ∪ a := ext (assume x, or.comm) theorem union_assoc (a b c : set α) : (a ∪ b) ∪ c = a ∪ (b ∪ c) := ext (assume x, or.assoc) instance union_is_assoc : is_associative (set α) (∪) := ⟨union_assoc⟩ instance union_is_comm : is_commutative (set α) (∪) := ⟨union_comm⟩ theorem union_left_comm (s₁ s₂ s₃ : set α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := by finish theorem union_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := by finish theorem union_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∪ t = t := by finish [subset_def, ext_iff, iff_def] theorem union_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∪ t = s := by finish [subset_def, ext_iff, iff_def] @[simp] theorem subset_union_left (s t : set α) : s ⊆ s ∪ t := λ x, or.inl @[simp] theorem subset_union_right (s t : set α) : t ⊆ s ∪ t := λ x, or.inr theorem union_subset {s t r : set α} (sr : s ⊆ r) (tr : t ⊆ r) : s ∪ t ⊆ r := by finish [subset_def, union_def] @[simp] theorem union_subset_iff {s t u : set α} : s ∪ t ⊆ u ↔ s ⊆ u ∧ t ⊆ u := by finish [iff_def, subset_def] theorem union_subset_union {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ s₂) (h₂ : t₁ ⊆ t₂) : s₁ ∪ t₁ ⊆ s₂ ∪ t₂ := by finish [subset_def] theorem union_subset_union_left {s₁ s₂ : set α} (t) (h : s₁ ⊆ s₂) : s₁ ∪ t ⊆ s₂ ∪ t := union_subset_union h (by refl) theorem union_subset_union_right (s) {t₁ t₂ : set α} (h : t₁ ⊆ t₂) : s ∪ t₁ ⊆ s ∪ t₂ := union_subset_union (by refl) h lemma subset_union_of_subset_left {s t : set α} (h : s ⊆ t) (u : set α) : s ⊆ t ∪ u := subset.trans h (subset_union_left t u) lemma subset_union_of_subset_right {s u : set α} (h : s ⊆ u) (t : set α) : s ⊆ t ∪ u := subset.trans h (subset_union_right t u) @[simp] theorem union_empty_iff {s t : set α} : s ∪ t = ∅ ↔ s = ∅ ∧ t = ∅ := ⟨by finish [ext_iff], by finish [ext_iff]⟩ /-! ### Lemmas about intersection -/ theorem inter_def {s₁ s₂ : set α} : s₁ ∩ s₂ = {a | a ∈ s₁ ∧ a ∈ s₂} := rfl theorem mem_inter_iff (x : α) (a b : set α) : x ∈ a ∩ b ↔ x ∈ a ∧ x ∈ b := iff.rfl @[simp] theorem mem_inter_eq (x : α) (a b : set α) : x ∈ a ∩ b = (x ∈ a ∧ x ∈ b) := rfl theorem mem_inter {x : α} {a b : set α} (ha : x ∈ a) (hb : x ∈ b) : x ∈ a ∩ b := ⟨ha, hb⟩ theorem mem_of_mem_inter_left {x : α} {a b : set α} (h : x ∈ a ∩ b) : x ∈ a := h.left theorem mem_of_mem_inter_right {x : α} {a b : set α} (h : x ∈ a ∩ b) : x ∈ b := h.right @[simp] theorem inter_self (a : set α) : a ∩ a = a := ext (assume x, and_self _) @[simp] theorem inter_empty (a : set α) : a ∩ ∅ = ∅ := ext (assume x, and_false _) @[simp] theorem empty_inter (a : set α) : ∅ ∩ a = ∅ := ext (assume x, false_and _) theorem inter_comm (a b : set α) : a ∩ b = b ∩ a := ext (assume x, and.comm) theorem inter_assoc (a b c : set α) : (a ∩ b) ∩ c = a ∩ (b ∩ c) := ext (assume x, and.assoc) instance inter_is_assoc : is_associative (set α) (∩) := ⟨inter_assoc⟩ instance inter_is_comm : is_commutative (set α) (∩) := ⟨inter_comm⟩ theorem inter_left_comm (s₁ s₂ s₃ : set α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := by finish theorem inter_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := by finish @[simp] theorem inter_subset_left (s t : set α) : s ∩ t ⊆ s := λ x H, and.left H @[simp] theorem inter_subset_right (s t : set α) : s ∩ t ⊆ t := λ x H, and.right H theorem subset_inter {s t r : set α} (rs : r ⊆ s) (rt : r ⊆ t) : r ⊆ s ∩ t := by finish [subset_def, inter_def] @[simp] theorem subset_inter_iff {s t r : set α} : r ⊆ s ∩ t ↔ r ⊆ s ∧ r ⊆ t := ⟨λ h, ⟨subset.trans h (inter_subset_left _ _), subset.trans h (inter_subset_right _ _)⟩, λ ⟨h₁, h₂⟩, subset_inter h₁ h₂⟩ @[simp] theorem inter_univ (a : set α) : a ∩ univ = a := ext (assume x, and_true _) @[simp] theorem univ_inter (a : set α) : univ ∩ a = a := ext (assume x, true_and _) theorem inter_subset_inter_left {s t : set α} (u : set α) (H : s ⊆ t) : s ∩ u ⊆ t ∩ u := by finish [subset_def] theorem inter_subset_inter_right {s t : set α} (u : set α) (H : s ⊆ t) : u ∩ s ⊆ u ∩ t := by finish [subset_def] theorem inter_subset_inter {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) : s₁ ∩ s₂ ⊆ t₁ ∩ t₂ := by finish [subset_def] theorem inter_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∩ t = s := by finish [subset_def, ext_iff, iff_def] theorem inter_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∩ t = t := by finish [subset_def, ext_iff, iff_def] theorem subset_iff_inter_eq_self {s t : set α} : s ⊆ t ↔ s ∩ t = s := ⟨λ h, inter_eq_self_of_subset_left h, λ h x h1, set.mem_of_mem_inter_right (by {rw h, exact h1})⟩ lemma inter_compl_nonempty_iff {s t : set α} : (s ∩ tᶜ).nonempty ↔ ¬ s ⊆ t := begin split, { rintros ⟨x ,xs, xt⟩ sub, exact xt (sub xs) }, { intros h, rcases not_subset.mp h with ⟨x, xs, xt⟩, exact ⟨x, xs, xt⟩ } end theorem union_inter_cancel_left {s t : set α} : (s ∪ t) ∩ s = s := by finish [ext_iff, iff_def] theorem union_inter_cancel_right {s t : set α} : (s ∪ t) ∩ t = t := by finish [ext_iff, iff_def] /-! ### Distributivity laws -/ theorem inter_distrib_left (s t u : set α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := ext (assume x, and_or_distrib_left) theorem inter_distrib_right (s t u : set α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := ext (assume x, or_and_distrib_right) theorem union_distrib_left (s t u : set α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := ext (assume x, or_and_distrib_left) theorem union_distrib_right (s t u : set α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := ext (assume x, and_or_distrib_right) /-! ### Lemmas about `insert` `insert α s` is the set `{α} ∪ s`. -/ theorem insert_def (x : α) (s : set α) : insert x s = { y | y = x ∨ y ∈ s } := rfl @[simp] theorem subset_insert (x : α) (s : set α) : s ⊆ insert x s := assume y ys, or.inr ys theorem mem_insert (x : α) (s : set α) : x ∈ insert x s := or.inl rfl theorem mem_insert_of_mem {x : α} {s : set α} (y : α) : x ∈ s → x ∈ insert y s := or.inr theorem eq_or_mem_of_mem_insert {x a : α} {s : set α} : x ∈ insert a s → x = a ∨ x ∈ s := id theorem mem_of_mem_insert_of_ne {x a : α} {s : set α} (xin : x ∈ insert a s) : x ≠ a → x ∈ s := by finish [insert_def] @[simp] theorem mem_insert_iff {x a : α} {s : set α} : x ∈ insert a s ↔ (x = a ∨ x ∈ s) := iff.rfl @[simp] theorem insert_eq_of_mem {a : α} {s : set α} (h : a ∈ s) : insert a s = s := by finish [ext_iff, iff_def] lemma ne_insert_of_not_mem {s : set α} (t : set α) {a : α} (h : a ∉ s) : s ≠ insert a t := by { contrapose! h, simp [h] } theorem insert_subset : insert a s ⊆ t ↔ (a ∈ t ∧ s ⊆ t) := by simp [subset_def, or_imp_distrib, forall_and_distrib] theorem insert_subset_insert (h : s ⊆ t) : insert a s ⊆ insert a t := assume a', or.imp_right (@h a') theorem ssubset_iff_insert {s t : set α} : s ⊂ t ↔ ∃ a ∉ s, insert a s ⊆ t := begin simp only [insert_subset, exists_and_distrib_right, ssubset_def, not_subset], simp only [exists_prop, and_comm] end theorem ssubset_insert {s : set α} {a : α} (h : a ∉ s) : s ⊂ insert a s := ssubset_iff_insert.2 ⟨a, h, subset.refl _⟩ theorem insert_comm (a b : α) (s : set α) : insert a (insert b s) = insert b (insert a s) := by { ext, simp [or.left_comm] } theorem insert_union : insert a s ∪ t = insert a (s ∪ t) := by { ext, simp [or.comm, or.left_comm] } @[simp] theorem union_insert : s ∪ insert a t = insert a (s ∪ t) := by { ext, simp [or.comm, or.left_comm] } theorem insert_nonempty (a : α) (s : set α) : (insert a s).nonempty := ⟨a, mem_insert a s⟩ instance (a : α) (s : set α) : nonempty (insert a s : set α) := (insert_nonempty a s).to_subtype lemma insert_inter (x : α) (s t : set α) : insert x (s ∩ t) = insert x s ∩ insert x t := by { ext y, simp [←or_and_distrib_left] } -- useful in proofs by induction theorem forall_of_forall_insert {P : α → Prop} {a : α} {s : set α} (h : ∀ x, x ∈ insert a s → P x) : ∀ x, x ∈ s → P x := by finish theorem forall_insert_of_forall {P : α → Prop} {a : α} {s : set α} (h : ∀ x, x ∈ s → P x) (ha : P a) : ∀ x, x ∈ insert a s → P x := by finish theorem bex_insert_iff {P : α → Prop} {a : α} {s : set α} : (∃ x ∈ insert a s, P x) ↔ (∃ x ∈ s, P x) ∨ P a := by finish [iff_def] theorem ball_insert_iff {P : α → Prop} {a : α} {s : set α} : (∀ x ∈ insert a s, P x) ↔ P a ∧ (∀x ∈ s, P x) := by finish [iff_def] /-! ### Lemmas about singletons -/ theorem singleton_def (a : α) : ({a} : set α) = insert a ∅ := (insert_emptyc_eq _).symm @[simp] theorem mem_singleton_iff {a b : α} : a ∈ ({b} : set α) ↔ a = b := iff.rfl @[simp] lemma set_of_eq_eq_singleton {a : α} : {n | n = a} = {a} := set.ext $ λ n, (set.mem_singleton_iff).symm -- TODO: again, annotation needed @[simp] theorem mem_singleton (a : α) : a ∈ ({a} : set α) := by finish theorem eq_of_mem_singleton {x y : α} (h : x ∈ ({y} : set α)) : x = y := by finish @[simp] theorem singleton_eq_singleton_iff {x y : α} : {x} = ({y} : set α) ↔ x = y := by finish [ext_iff, iff_def] theorem mem_singleton_of_eq {x y : α} (H : x = y) : x ∈ ({y} : set α) := by finish theorem insert_eq (x : α) (s : set α) : insert x s = ({x} : set α) ∪ s := by finish [ext_iff, or_comm] @[simp] theorem pair_eq_singleton (a : α) : ({a, a} : set α) = {a} := by finish theorem pair_comm (a b : α) : ({a, b} : set α) = {b, a} := ext $ λ x, or_comm _ _ @[simp] theorem singleton_nonempty (a : α) : ({a} : set α).nonempty := ⟨a, rfl⟩ @[simp] theorem singleton_subset_iff {a : α} {s : set α} : {a} ⊆ s ↔ a ∈ s := ⟨λh, h (by simp), λh b e, by { rw [mem_singleton_iff] at e, simp [*] }⟩ theorem set_compr_eq_eq_singleton {a : α} : {b | b = a} = {a} := by { ext, simp } @[simp] theorem singleton_union : {a} ∪ s = insert a s := rfl @[simp] theorem union_singleton : s ∪ {a} = insert a s := by rw [union_comm, singleton_union] @[simp] theorem singleton_inter_eq_empty : {a} ∩ s = ∅ ↔ a ∉ s := by simp [eq_empty_iff_forall_not_mem] @[simp] theorem inter_singleton_eq_empty : s ∩ {a} = ∅ ↔ a ∉ s := by rw [inter_comm, singleton_inter_eq_empty] @[simp] theorem singleton_inter_nonempty : ({a} ∩ s).nonempty ↔ a ∈ s := by rw [← ne_empty_iff_nonempty, ne.def, singleton_inter_eq_empty, not_not] @[simp] theorem inter_singleton_nonempty : (s ∩ {a}).nonempty ↔ a ∈ s := by rw [inter_comm, singleton_inter_nonempty] lemma nmem_singleton_empty {s : set α} : s ∉ ({∅} : set (set α)) ↔ s.nonempty := by rw [mem_singleton_iff, ← ne.def, ne_empty_iff_nonempty] instance unique_singleton (a : α) : unique ↥({a} : set α) := { default := ⟨a, mem_singleton a⟩, uniq := begin intros x, apply subtype.ext, apply eq_of_mem_singleton (subtype.mem x), end} lemma eq_singleton_iff_unique_mem {s : set α} {a : α} : s = {a} ↔ a ∈ s ∧ ∀ x ∈ s, x = a := by simp [ext_iff, @iff_def (_ ∈ s), forall_and_distrib, and_comm] lemma eq_singleton_iff_nonempty_unique_mem {s : set α} {a : α} : s = {a} ↔ s.nonempty ∧ ∀ x ∈ s, x = a := begin split, { intros h, subst h, simp, }, { rintros ⟨hne, h_uniq⟩, rw eq_singleton_iff_unique_mem, refine ⟨_, h_uniq⟩, rw ← h_uniq hne.some hne.some_spec, apply hne.some_spec, }, end /-! ### Lemmas about sets defined as `{x ∈ s | p x}`. -/ theorem mem_sep {s : set α} {p : α → Prop} {x : α} (xs : x ∈ s) (px : p x) : x ∈ {x ∈ s | p x} := ⟨xs, px⟩ @[simp] theorem sep_mem_eq {s t : set α} : {x ∈ s | x ∈ t} = s ∩ t := rfl @[simp] theorem mem_sep_eq {s : set α} {p : α → Prop} {x : α} : x ∈ {x ∈ s | p x} = (x ∈ s ∧ p x) := rfl theorem mem_sep_iff {s : set α} {p : α → Prop} {x : α} : x ∈ {x ∈ s | p x} ↔ x ∈ s ∧ p x := iff.rfl theorem eq_sep_of_subset {s t : set α} (ssubt : s ⊆ t) : s = {x ∈ t | x ∈ s} := by finish [ext_iff, iff_def, subset_def] theorem sep_subset (s : set α) (p : α → Prop) : {x ∈ s | p x} ⊆ s := assume x, and.left theorem forall_not_of_sep_empty {s : set α} {p : α → Prop} (h : {x ∈ s | p x} = ∅) : ∀ x ∈ s, ¬ p x := by finish [ext_iff] @[simp] lemma sep_univ {α} {p : α → Prop} : {a ∈ (univ : set α) | p a} = {a | p a} := by { ext, simp } @[simp] lemma subset_singleton_iff {α : Type*} {s : set α} {x : α} : s ⊆ {x} ↔ ∀ y ∈ s, y = x := iff.rfl /-! ### Lemmas about complement -/ theorem mem_compl {s : set α} {x : α} (h : x ∉ s) : x ∈ sᶜ := h lemma compl_set_of {α} (p : α → Prop) : {a | p a}ᶜ = { a | ¬ p a } := rfl theorem not_mem_of_mem_compl {s : set α} {x : α} (h : x ∈ sᶜ) : x ∉ s := h @[simp] theorem mem_compl_eq (s : set α) (x : α) : x ∈ sᶜ = (x ∉ s) := rfl theorem mem_compl_iff (s : set α) (x : α) : x ∈ sᶜ ↔ x ∉ s := iff.rfl @[simp] theorem inter_compl_self (s : set α) : s ∩ sᶜ = ∅ := by finish [ext_iff] @[simp] theorem compl_inter_self (s : set α) : sᶜ ∩ s = ∅ := by finish [ext_iff] @[simp] theorem compl_empty : (∅ : set α)ᶜ = univ := by finish [ext_iff] @[simp] theorem compl_union (s t : set α) : (s ∪ t)ᶜ = sᶜ ∩ tᶜ := by finish [ext_iff] local attribute [simp] -- Will be generalized to lattices in `compl_compl'` theorem compl_compl (s : set α) : sᶜᶜ = s := by finish [ext_iff] -- ditto theorem compl_inter (s t : set α) : (s ∩ t)ᶜ = sᶜ ∪ tᶜ := by finish [ext_iff] @[simp] theorem compl_univ : (univ : set α)ᶜ = ∅ := by finish [ext_iff] lemma compl_empty_iff {s : set α} : sᶜ = ∅ ↔ s = univ := by { split, intro h, rw [←compl_compl s, h, compl_empty], intro h, rw [h, compl_univ] } lemma compl_univ_iff {s : set α} : sᶜ = univ ↔ s = ∅ := by rw [←compl_empty_iff, compl_compl] lemma nonempty_compl {s : set α} : sᶜ.nonempty ↔ s ≠ univ := ne_empty_iff_nonempty.symm.trans $ not_congr $ compl_empty_iff lemma mem_compl_singleton_iff {a x : α} : x ∈ ({a} : set α)ᶜ ↔ x ≠ a := not_iff_not_of_iff mem_singleton_iff lemma compl_singleton_eq (a : α) : ({a} : set α)ᶜ = {x | x ≠ a} := ext $ λ x, mem_compl_singleton_iff theorem union_eq_compl_compl_inter_compl (s t : set α) : s ∪ t = (sᶜ ∩ tᶜ)ᶜ := by simp [compl_inter, compl_compl] theorem inter_eq_compl_compl_union_compl (s t : set α) : s ∩ t = (sᶜ ∪ tᶜ)ᶜ := by simp [compl_compl] @[simp] theorem union_compl_self (s : set α) : s ∪ sᶜ = univ := by finish [ext_iff] @[simp] theorem compl_union_self (s : set α) : sᶜ ∪ s = univ := by finish [ext_iff] theorem compl_comp_compl : compl ∘ compl = @id (set α) := funext compl_compl theorem compl_subset_comm {s t : set α} : sᶜ ⊆ t ↔ tᶜ ⊆ s := by haveI := classical.prop_decidable; exact forall_congr (λ a, not_imp_comm) lemma compl_subset_compl {s t : set α} : sᶜ ⊆ tᶜ ↔ t ⊆ s := by rw [compl_subset_comm, compl_compl] theorem compl_subset_iff_union {s t : set α} : sᶜ ⊆ t ↔ s ∪ t = univ := iff.symm $ eq_univ_iff_forall.trans $ forall_congr $ λ a, by haveI := classical.prop_decidable; exact or_iff_not_imp_left theorem subset_compl_comm {s t : set α} : s ⊆ tᶜ ↔ t ⊆ sᶜ := forall_congr $ λ a, imp_not_comm theorem subset_compl_iff_disjoint {s t : set α} : s ⊆ tᶜ ↔ s ∩ t = ∅ := iff.trans (forall_congr $ λ a, and_imp.symm) subset_empty_iff lemma subset_compl_singleton_iff {a : α} {s : set α} : s ⊆ {a}ᶜ ↔ a ∉ s := by { rw subset_compl_comm, simp } theorem inter_subset (a b c : set α) : a ∩ b ⊆ c ↔ a ⊆ bᶜ ∪ c := begin classical, split, { intros h x xa, by_cases h' : x ∈ b, simp [h ⟨xa, h'⟩], simp [h'] }, intros h x, rintro ⟨xa, xb⟩, cases h xa, contradiction, assumption end /-! ### Lemmas about set difference -/ theorem diff_eq (s t : set α) : s \ t = s ∩ tᶜ := rfl @[simp] theorem mem_diff {s t : set α} (x : α) : x ∈ s \ t ↔ x ∈ s ∧ x ∉ t := iff.rfl theorem mem_diff_of_mem {s t : set α} {x : α} (h1 : x ∈ s) (h2 : x ∉ t) : x ∈ s \ t := ⟨h1, h2⟩ theorem mem_of_mem_diff {s t : set α} {x : α} (h : x ∈ s \ t) : x ∈ s := h.left theorem not_mem_of_mem_diff {s t : set α} {x : α} (h : x ∈ s \ t) : x ∉ t := h.right theorem diff_eq_compl_inter {s t : set α} : s \ t = tᶜ ∩ s := by rw [diff_eq, inter_comm] theorem nonempty_diff {s t : set α} : (s \ t).nonempty ↔ ¬ (s ⊆ t) := ⟨λ ⟨x, xs, xt⟩, not_subset.2 ⟨x, xs, xt⟩, λ h, let ⟨x, xs, xt⟩ := not_subset.1 h in ⟨x, xs, xt⟩⟩ theorem union_diff_cancel' {s t u : set α} (h₁ : s ⊆ t) (h₂ : t ⊆ u) : t ∪ (u \ s) = u := by finish [ext_iff, iff_def, subset_def] theorem union_diff_cancel {s t : set α} (h : s ⊆ t) : s ∪ (t \ s) = t := union_diff_cancel' (subset.refl s) h theorem union_diff_cancel_left {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ s = t := by finish [ext_iff, iff_def, subset_def] theorem union_diff_cancel_right {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ t = s := by finish [ext_iff, iff_def, subset_def] theorem union_diff_left {s t : set α} : (s ∪ t) \ s = t \ s := by finish [ext_iff, iff_def] theorem union_diff_right {s t : set α} : (s ∪ t) \ t = s \ t := by finish [ext_iff, iff_def] theorem union_diff_distrib {s t u : set α} : (s ∪ t) \ u = s \ u ∪ t \ u := inter_distrib_right _ _ _ theorem inter_union_distrib_left {s t u : set α} : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := set.ext $ λ _, and_or_distrib_left theorem inter_union_distrib_right {s t u : set α} : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := set.ext $ λ _, and_or_distrib_right theorem union_inter_distrib_left {s t u : set α} : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := set.ext $ λ _, or_and_distrib_left theorem union_inter_distrib_right {s t u : set α} : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := set.ext $ λ _, or_and_distrib_right theorem inter_diff_assoc (a b c : set α) : (a ∩ b) \ c = a ∩ (b \ c) := inter_assoc _ _ _ theorem inter_diff_self (a b : set α) : a ∩ (b \ a) = ∅ := by finish [ext_iff] theorem inter_union_diff (s t : set α) : (s ∩ t) ∪ (s \ t) = s := by finish [ext_iff, iff_def] theorem inter_union_compl (s t : set α) : (s ∩ t) ∪ (s ∩ tᶜ) = s := inter_union_diff _ _ theorem diff_subset (s t : set α) : s \ t ⊆ s := by finish [subset_def] theorem diff_subset_diff {s₁ s₂ t₁ t₂ : set α} : s₁ ⊆ s₂ → t₂ ⊆ t₁ → s₁ \ t₁ ⊆ s₂ \ t₂ := by finish [subset_def] theorem diff_subset_diff_left {s₁ s₂ t : set α} (h : s₁ ⊆ s₂) : s₁ \ t ⊆ s₂ \ t := diff_subset_diff h (by refl) theorem diff_subset_diff_right {s t u : set α} (h : t ⊆ u) : s \ u ⊆ s \ t := diff_subset_diff (subset.refl s) h theorem compl_eq_univ_diff (s : set α) : sᶜ = univ \ s := by finish [ext_iff] @[simp] lemma empty_diff (s : set α) : (∅ \ s : set α) = ∅ := eq_empty_of_subset_empty $ assume x ⟨hx, _⟩, hx theorem diff_eq_empty {s t : set α} : s \ t = ∅ ↔ s ⊆ t := ⟨assume h x hx, classical.by_contradiction $ assume : x ∉ t, show x ∈ (∅ : set α), from h ▸ ⟨hx, this⟩, assume h, eq_empty_of_subset_empty $ assume x ⟨hx, hnx⟩, hnx $ h hx⟩ @[simp] theorem diff_empty {s : set α} : s \ ∅ = s := ext $ assume x, ⟨assume ⟨hx, _⟩, hx, assume h, ⟨h, not_false⟩⟩ theorem diff_diff {u : set α} : s \ t \ u = s \ (t ∪ u) := ext $ by simp [not_or_distrib, and.comm, and.left_comm] -- the following statement contains parentheses to help the reader lemma diff_diff_comm {s t u : set α} : (s \ t) \ u = (s \ u) \ t := by simp_rw [diff_diff, union_comm] lemma diff_subset_iff {s t u : set α} : s \ t ⊆ u ↔ s ⊆ t ∪ u := ⟨assume h x xs, classical.by_cases or.inl (assume nxt, or.inr (h ⟨xs, nxt⟩)), assume h x ⟨xs, nxt⟩, or.resolve_left (h xs) nxt⟩ lemma subset_diff_union (s t : set α) : s ⊆ (s \ t) ∪ t := by rw [union_comm, ←diff_subset_iff] @[simp] lemma diff_singleton_subset_iff {x : α} {s t : set α} : s \ {x} ⊆ t ↔ s ⊆ insert x t := by { rw [←union_singleton, union_comm], apply diff_subset_iff } lemma subset_diff_singleton {x : α} {s t : set α} (h : s ⊆ t) (hx : x ∉ s) : s ⊆ t \ {x} := subset_inter h $ subset_compl_comm.1 $ singleton_subset_iff.2 hx lemma subset_insert_diff_singleton (x : α) (s : set α) : s ⊆ insert x (s \ {x}) := by rw [←diff_singleton_subset_iff] lemma diff_subset_comm {s t u : set α} : s \ t ⊆ u ↔ s \ u ⊆ t := by rw [diff_subset_iff, diff_subset_iff, union_comm] lemma diff_inter {s t u : set α} : s \ (t ∩ u) = (s \ t) ∪ (s \ u) := ext $ λ x, by simp [not_and_distrib, and_or_distrib_left] lemma diff_inter_diff {s t u : set α} : s \ t ∩ (s \ u) = s \ (t ∪ u) := by { ext x, simp only [mem_inter_eq, mem_union_eq, mem_diff, not_or_distrib, and.left_comm, and.assoc, and_self_left] } lemma diff_compl : s \ tᶜ = s ∩ t := by rw [diff_eq, compl_compl] lemma diff_diff_right {s t u : set α} : s \ (t \ u) = (s \ t) ∪ (s ∩ u) := by rw [diff_eq t u, diff_inter, diff_compl] @[simp] theorem insert_diff_of_mem (s) (h : a ∈ t) : insert a s \ t = s \ t := by { ext, split; simp [or_imp_distrib, h] {contextual := tt} } theorem insert_diff_of_not_mem (s) (h : a ∉ t) : insert a s \ t = insert a (s \ t) := begin classical, ext x, by_cases h' : x ∈ t, { have : x ≠ a, { assume H, rw H at h', exact h h' }, simp [h, h', this] }, { simp [h, h'] } end lemma insert_diff_self_of_not_mem {a : α} {s : set α} (h : a ∉ s) : insert a s \ {a} = s := by { ext, simp [and_iff_left_of_imp (λ hx : x ∈ s, show x ≠ a, from λ hxa, h $ hxa ▸ hx)] } theorem union_diff_self {s t : set α} : s ∪ (t \ s) = s ∪ t := by finish [ext_iff, iff_def] theorem diff_union_self {s t : set α} : (s \ t) ∪ t = s ∪ t := by rw [union_comm, union_diff_self, union_comm] theorem diff_inter_self {a b : set α} : (b \ a) ∩ a = ∅ := by { ext, by simp [iff_def] {contextual:=tt} } theorem diff_inter_self_eq_diff {s t : set α} : s \ (t ∩ s) = s \ t := by { ext, simp [iff_def] {contextual := tt} } theorem diff_self_inter {s t : set α} : s \ (s ∩ t) = s \ t := by rw [inter_comm, diff_inter_self_eq_diff] theorem diff_eq_self {s t : set α} : s \ t = s ↔ t ∩ s ⊆ ∅ := by finish [ext_iff, iff_def, subset_def] @[simp] theorem diff_singleton_eq_self {a : α} {s : set α} (h : a ∉ s) : s \ {a} = s := diff_eq_self.2 $ by simp [singleton_inter_eq_empty.2 h] @[simp] theorem insert_diff_singleton {a : α} {s : set α} : insert a (s \ {a}) = insert a s := by simp [insert_eq, union_diff_self, -union_singleton, -singleton_union] @[simp] lemma diff_self {s : set α} : s \ s = ∅ := by { ext, simp } lemma diff_diff_cancel_left {s t : set α} (h : s ⊆ t) : t \ (t \ s) = s := by simp only [diff_diff_right, diff_self, inter_eq_self_of_subset_right h, empty_union] lemma mem_diff_singleton {x y : α} {s : set α} : x ∈ s \ {y} ↔ (x ∈ s ∧ x ≠ y) := iff.rfl lemma mem_diff_singleton_empty {s : set α} {t : set (set α)} : s ∈ t \ {∅} ↔ (s ∈ t ∧ s.nonempty) := mem_diff_singleton.trans $ and_congr iff.rfl ne_empty_iff_nonempty /-! ### Powerset -/ theorem mem_powerset {x s : set α} (h : x ⊆ s) : x ∈ powerset s := h theorem subset_of_mem_powerset {x s : set α} (h : x ∈ powerset s) : x ⊆ s := h @[simp] theorem mem_powerset_iff (x s : set α) : x ∈ powerset s ↔ x ⊆ s := iff.rfl theorem powerset_inter (s t : set α) : 𝒫 (s ∩ t) = 𝒫 s ∩ 𝒫 t := ext $ λ u, subset_inter_iff @[simp] theorem powerset_mono : 𝒫 s ⊆ 𝒫 t ↔ s ⊆ t := ⟨λ h, h (subset.refl s), λ h u hu, subset.trans hu h⟩ theorem monotone_powerset : monotone (powerset : set α → set (set α)) := λ s t, powerset_mono.2 @[simp] theorem powerset_nonempty : (𝒫 s).nonempty := ⟨∅, empty_subset s⟩ @[simp] theorem powerset_empty : 𝒫 (∅ : set α) = {∅} := ext $ λ s, subset_empty_iff /-! ### Inverse image -/ /-- The preimage of `s : set β` by `f : α → β`, written `f ⁻¹' s`, is the set of `x : α` such that `f x ∈ s`. -/ def preimage {α : Type u} {β : Type v} (f : α → β) (s : set β) : set α := {x | f x ∈ s} infix ` ⁻¹' `:80 := preimage section preimage variables {f : α → β} {g : β → γ} @[simp] theorem preimage_empty : f ⁻¹' ∅ = ∅ := rfl @[simp] theorem mem_preimage {s : set β} {a : α} : (a ∈ f ⁻¹' s) ↔ (f a ∈ s) := iff.rfl lemma preimage_congr {f g : α → β} {s : set β} (h : ∀ (x : α), f x = g x) : f ⁻¹' s = g ⁻¹' s := by { congr' with x, apply_assumption } theorem preimage_mono {s t : set β} (h : s ⊆ t) : f ⁻¹' s ⊆ f ⁻¹' t := assume x hx, h hx @[simp] theorem preimage_univ : f ⁻¹' univ = univ := rfl theorem subset_preimage_univ {s : set α} : s ⊆ f ⁻¹' univ := subset_univ _ @[simp] theorem preimage_inter {s t : set β} : f ⁻¹' (s ∩ t) = f ⁻¹' s ∩ f ⁻¹' t := rfl @[simp] theorem preimage_union {s t : set β} : f ⁻¹' (s ∪ t) = f ⁻¹' s ∪ f ⁻¹' t := rfl @[simp] theorem preimage_compl {s : set β} : f ⁻¹' sᶜ = (f ⁻¹' s)ᶜ := rfl @[simp] theorem preimage_diff (f : α → β) (s t : set β) : f ⁻¹' (s \ t) = f ⁻¹' s \ f ⁻¹' t := rfl @[simp] theorem preimage_set_of_eq {p : α → Prop} {f : β → α} : f ⁻¹' {a | p a} = {a | p (f a)} := rfl @[simp] theorem preimage_id {s : set α} : id ⁻¹' s = s := rfl @[simp] theorem preimage_id' {s : set α} : (λ x, x) ⁻¹' s = s := rfl theorem preimage_const_of_mem {b : β} {s : set β} (h : b ∈ s) : (λ (x : α), b) ⁻¹' s = univ := eq_univ_of_forall $ λ x, h theorem preimage_const_of_not_mem {b : β} {s : set β} (h : b ∉ s) : (λ (x : α), b) ⁻¹' s = ∅ := eq_empty_of_subset_empty $ λ x hx, h hx theorem preimage_const (b : β) (s : set β) [decidable (b ∈ s)] : (λ (x : α), b) ⁻¹' s = if b ∈ s then univ else ∅ := by { split_ifs with hb hb, exacts [preimage_const_of_mem hb, preimage_const_of_not_mem hb] } theorem preimage_comp {s : set γ} : (g ∘ f) ⁻¹' s = f ⁻¹' (g ⁻¹' s) := rfl lemma preimage_preimage {g : β → γ} {f : α → β} {s : set γ} : f ⁻¹' (g ⁻¹' s) = (λ x, g (f x)) ⁻¹' s := preimage_comp.symm theorem eq_preimage_subtype_val_iff {p : α → Prop} {s : set (subtype p)} {t : set α} : s = subtype.val ⁻¹' t ↔ (∀x (h : p x), (⟨x, h⟩ : subtype p) ∈ s ↔ x ∈ t) := ⟨assume s_eq x h, by { rw [s_eq], simp }, assume h, ext $ λ ⟨x, hx⟩, by simp [h]⟩ lemma preimage_coe_coe_diagonal {α : Type*} (s : set α) : (prod.map coe coe) ⁻¹' (diagonal α) = diagonal s := begin ext ⟨⟨x, x_in⟩, ⟨y, y_in⟩⟩, simp [set.diagonal], end end preimage /-! ### Image of a set under a function -/ section image infix ` '' `:80 := image theorem mem_image_iff_bex {f : α → β} {s : set α} {y : β} : y ∈ f '' s ↔ ∃ x (_ : x ∈ s), f x = y := bex_def.symm theorem mem_image_eq (f : α → β) (s : set α) (y: β) : y ∈ f '' s = ∃ x, x ∈ s ∧ f x = y := rfl @[simp] theorem mem_image (f : α → β) (s : set α) (y : β) : y ∈ f '' s ↔ ∃ x, x ∈ s ∧ f x = y := iff.rfl lemma image_eta (f : α → β) : f '' s = (λ x, f x) '' s := rfl theorem mem_image_of_mem (f : α → β) {x : α} {a : set α} (h : x ∈ a) : f x ∈ f '' a := ⟨_, h, rfl⟩ theorem mem_image_of_injective {f : α → β} {a : α} {s : set α} (hf : injective f) : f a ∈ f '' s ↔ a ∈ s := iff.intro (assume ⟨b, hb, eq⟩, (hf eq) ▸ hb) (assume h, mem_image_of_mem _ h) theorem ball_image_iff {f : α → β} {s : set α} {p : β → Prop} : (∀ y ∈ f '' s, p y) ↔ (∀ x ∈ s, p (f x)) := by simp theorem ball_image_of_ball {f : α → β} {s : set α} {p : β → Prop} (h : ∀ x ∈ s, p (f x)) : ∀ y ∈ f '' s, p y := ball_image_iff.2 h theorem bex_image_iff {f : α → β} {s : set α} {p : β → Prop} : (∃ y ∈ f '' s, p y) ↔ (∃ x ∈ s, p (f x)) := by simp theorem mem_image_elim {f : α → β} {s : set α} {C : β → Prop} (h : ∀ (x : α), x ∈ s → C (f x)) : ∀{y : β}, y ∈ f '' s → C y | ._ ⟨a, a_in, rfl⟩ := h a a_in theorem mem_image_elim_on {f : α → β} {s : set α} {C : β → Prop} {y : β} (h_y : y ∈ f '' s) (h : ∀ (x : α), x ∈ s → C (f x)) : C y := mem_image_elim h h_y @[congr] lemma image_congr {f g : α → β} {s : set α} (h : ∀a∈s, f a = g a) : f '' s = g '' s := by safe [ext_iff, iff_def] /-- A common special case of `image_congr` -/ lemma image_congr' {f g : α → β} {s : set α} (h : ∀ (x : α), f x = g x) : f '' s = g '' s := image_congr (λx _, h x) theorem image_comp (f : β → γ) (g : α → β) (a : set α) : (f ∘ g) '' a = f '' (g '' a) := subset.antisymm (ball_image_of_ball $ assume a ha, mem_image_of_mem _ $ mem_image_of_mem _ ha) (ball_image_of_ball $ ball_image_of_ball $ assume a ha, mem_image_of_mem _ ha) /-- A variant of `image_comp`, useful for rewriting -/ lemma image_image (g : β → γ) (f : α → β) (s : set α) : g '' (f '' s) = (λ x, g (f x)) '' s := (image_comp g f s).symm /-- Image is monotone with respect to `⊆`. See `set.monotone_image` for the statement in terms of `≤`. -/ theorem image_subset {a b : set α} (f : α → β) (h : a ⊆ b) : f '' a ⊆ f '' b := by finish [subset_def, mem_image_eq] theorem image_union (f : α → β) (s t : set α) : f '' (s ∪ t) = f '' s ∪ f '' t := by finish [ext_iff, iff_def, mem_image_eq] @[simp] theorem image_empty (f : α → β) : f '' ∅ = ∅ := by { ext, simp } lemma image_inter_subset (f : α → β) (s t : set α) : f '' (s ∩ t) ⊆ f '' s ∩ f '' t := subset_inter (image_subset _ $ inter_subset_left _ _) (image_subset _ $ inter_subset_right _ _) theorem image_inter_on {f : α → β} {s t : set α} (h : ∀x∈t, ∀y∈s, f x = f y → x = y) : f '' s ∩ f '' t = f '' (s ∩ t) := subset.antisymm (assume b ⟨⟨a₁, ha₁, h₁⟩, ⟨a₂, ha₂, h₂⟩⟩, have a₂ = a₁, from h _ ha₂ _ ha₁ (by simp *), ⟨a₁, ⟨ha₁, this ▸ ha₂⟩, h₁⟩) (image_inter_subset _ _ _) theorem image_inter {f : α → β} {s t : set α} (H : injective f) : f '' s ∩ f '' t = f '' (s ∩ t) := image_inter_on (assume x _ y _ h, H h) theorem image_univ_of_surjective {ι : Type*} {f : ι → β} (H : surjective f) : f '' univ = univ := eq_univ_of_forall $ by { simpa [image] } @[simp] theorem image_singleton {f : α → β} {a : α} : f '' {a} = {f a} := by { ext, simp [image, eq_comm] } @[simp] theorem nonempty.image_const {s : set α} (hs : s.nonempty) (a : β) : (λ _, a) '' s = {a} := ext $ λ x, ⟨λ ⟨y, _, h⟩, h ▸ mem_singleton _, λ h, (eq_of_mem_singleton h).symm ▸ hs.imp (λ y hy, ⟨hy, rfl⟩)⟩ @[simp] lemma image_eq_empty {α β} {f : α → β} {s : set α} : f '' s = ∅ ↔ s = ∅ := by { simp only [eq_empty_iff_forall_not_mem], exact ⟨λ H a ha, H _ ⟨_, ha, rfl⟩, λ H b ⟨_, ha, _⟩, H _ ha⟩ } -- TODO(Jeremy): there is an issue with - t unfolding to compl t theorem mem_compl_image (t : set α) (S : set (set α)) : t ∈ compl '' S ↔ tᶜ ∈ S := begin suffices : ∀ x, xᶜ = t ↔ tᶜ = x, { simp [this] }, intro x, split; { intro e, subst e, simp } end /-- A variant of `image_id` -/ @[simp] lemma image_id' (s : set α) : (λx, x) '' s = s := by { ext, simp } theorem image_id (s : set α) : id '' s = s := by simp theorem compl_compl_image (S : set (set α)) : compl '' (compl '' S) = S := by rw [← image_comp, compl_comp_compl, image_id] theorem image_insert_eq {f : α → β} {a : α} {s : set α} : f '' (insert a s) = insert (f a) (f '' s) := by { ext, simp [and_or_distrib_left, exists_or_distrib, eq_comm, or_comm, and_comm] } theorem image_pair (f : α → β) (a b : α) : f '' {a, b} = {f a, f b} := by simp only [image_insert_eq, image_singleton] theorem image_subset_preimage_of_inverse {f : α → β} {g : β → α} (I : left_inverse g f) (s : set α) : f '' s ⊆ g ⁻¹' s := λ b ⟨a, h, e⟩, e ▸ ((I a).symm ▸ h : g (f a) ∈ s) theorem preimage_subset_image_of_inverse {f : α → β} {g : β → α} (I : left_inverse g f) (s : set β) : f ⁻¹' s ⊆ g '' s := λ b h, ⟨f b, h, I b⟩ theorem image_eq_preimage_of_inverse {f : α → β} {g : β → α} (h₁ : left_inverse g f) (h₂ : right_inverse g f) : image f = preimage g := funext $ λ s, subset.antisymm (image_subset_preimage_of_inverse h₁ s) (preimage_subset_image_of_inverse h₂ s) theorem mem_image_iff_of_inverse {f : α → β} {g : β → α} {b : β} {s : set α} (h₁ : left_inverse g f) (h₂ : right_inverse g f) : b ∈ f '' s ↔ g b ∈ s := by rw image_eq_preimage_of_inverse h₁ h₂; refl theorem image_compl_subset {f : α → β} {s : set α} (H : injective f) : f '' sᶜ ⊆ (f '' s)ᶜ := subset_compl_iff_disjoint.2 $ by simp [image_inter H] theorem subset_image_compl {f : α → β} {s : set α} (H : surjective f) : (f '' s)ᶜ ⊆ f '' sᶜ := compl_subset_iff_union.2 $ by { rw ← image_union, simp [image_univ_of_surjective H] } theorem image_compl_eq {f : α → β} {s : set α} (H : bijective f) : f '' sᶜ = (f '' s)ᶜ := subset.antisymm (image_compl_subset H.1) (subset_image_compl H.2) theorem subset_image_diff (f : α → β) (s t : set α) : f '' s \ f '' t ⊆ f '' (s \ t) := begin rw [diff_subset_iff, ← image_union, union_diff_self], exact image_subset f (subset_union_right t s) end theorem image_diff {f : α → β} (hf : injective f) (s t : set α) : f '' (s \ t) = f '' s \ f '' t := subset.antisymm (subset.trans (image_inter_subset _ _ _) $ inter_subset_inter_right _ $ image_compl_subset hf) (subset_image_diff f s t) lemma nonempty.image (f : α → β) {s : set α} : s.nonempty → (f '' s).nonempty | ⟨x, hx⟩ := ⟨f x, mem_image_of_mem f hx⟩ lemma nonempty.of_image {f : α → β} {s : set α} : (f '' s).nonempty → s.nonempty | ⟨y, x, hx, _⟩ := ⟨x, hx⟩ @[simp] lemma nonempty_image_iff {f : α → β} {s : set α} : (f '' s).nonempty ↔ s.nonempty := ⟨nonempty.of_image, λ h, h.image f⟩ instance (f : α → β) (s : set α) [nonempty s] : nonempty (f '' s) := (set.nonempty.image f nonempty_of_nonempty_subtype).to_subtype /-- image and preimage are a Galois connection -/ @[simp] theorem image_subset_iff {s : set α} {t : set β} {f : α → β} : f '' s ⊆ t ↔ s ⊆ f ⁻¹' t := ball_image_iff theorem image_preimage_subset (f : α → β) (s : set β) : f '' (f ⁻¹' s) ⊆ s := image_subset_iff.2 (subset.refl _) theorem subset_preimage_image (f : α → β) (s : set α) : s ⊆ f ⁻¹' (f '' s) := λ x, mem_image_of_mem f theorem preimage_image_eq {f : α → β} (s : set α) (h : injective f) : f ⁻¹' (f '' s) = s := subset.antisymm (λ x ⟨y, hy, e⟩, h e ▸ hy) (subset_preimage_image f s) theorem image_preimage_eq {f : α → β} (s : set β) (h : surjective f) : f '' (f ⁻¹' s) = s := subset.antisymm (image_preimage_subset f s) (λ x hx, let ⟨y, e⟩ := h x in ⟨y, (e.symm ▸ hx : f y ∈ s), e⟩) lemma preimage_eq_preimage {f : β → α} (hf : surjective f) : f ⁻¹' s = f ⁻¹' t ↔ s = t := iff.intro (assume eq, by rw [← image_preimage_eq s hf, ← image_preimage_eq t hf, eq]) (assume eq, eq ▸ rfl) lemma image_inter_preimage (f : α → β) (s : set α) (t : set β) : f '' (s ∩ f ⁻¹' t) = f '' s ∩ t := begin apply subset.antisymm, { calc f '' (s ∩ f ⁻¹' t) ⊆ f '' s ∩ (f '' (f⁻¹' t)) : image_inter_subset _ _ _ ... ⊆ f '' s ∩ t : inter_subset_inter_right _ (image_preimage_subset f t) }, { rintros _ ⟨⟨x, h', rfl⟩, h⟩, exact ⟨x, ⟨h', h⟩, rfl⟩ } end lemma image_preimage_inter (f : α → β) (s : set α) (t : set β) : f '' (f ⁻¹' t ∩ s) = t ∩ f '' s := by simp only [inter_comm, image_inter_preimage] @[simp] lemma image_inter_nonempty_iff {f : α → β} {s : set α} {t : set β} : (f '' s ∩ t).nonempty ↔ (s ∩ f ⁻¹' t).nonempty := by rw [←image_inter_preimage, nonempty_image_iff] lemma image_diff_preimage {f : α → β} {s : set α} {t : set β} : f '' (s \ f ⁻¹' t) = f '' s \ t := by simp_rw [diff_eq, ← preimage_compl, image_inter_preimage] theorem compl_image : image (compl : set α → set α) = preimage compl := image_eq_preimage_of_inverse compl_compl compl_compl theorem compl_image_set_of {p : set α → Prop} : compl '' {s | p s} = {s | p sᶜ} := congr_fun compl_image p theorem inter_preimage_subset (s : set α) (t : set β) (f : α → β) : s ∩ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∩ t) := λ x h, ⟨mem_image_of_mem _ h.left, h.right⟩ theorem union_preimage_subset (s : set α) (t : set β) (f : α → β) : s ∪ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∪ t) := λ x h, or.elim h (λ l, or.inl $ mem_image_of_mem _ l) (λ r, or.inr r) theorem subset_image_union (f : α → β) (s : set α) (t : set β) : f '' (s ∪ f ⁻¹' t) ⊆ f '' s ∪ t := image_subset_iff.2 (union_preimage_subset _ _ _) lemma preimage_subset_iff {A : set α} {B : set β} {f : α → β} : f⁻¹' B ⊆ A ↔ (∀ a : α, f a ∈ B → a ∈ A) := iff.rfl lemma image_eq_image {f : α → β} (hf : injective f) : f '' s = f '' t ↔ s = t := iff.symm $ iff.intro (assume eq, eq ▸ rfl) $ assume eq, by rw [← preimage_image_eq s hf, ← preimage_image_eq t hf, eq] lemma image_subset_image_iff {f : α → β} (hf : injective f) : f '' s ⊆ f '' t ↔ s ⊆ t := begin refine (iff.symm $ iff.intro (image_subset f) $ assume h, _), rw [← preimage_image_eq s hf, ← preimage_image_eq t hf], exact preimage_mono h end lemma prod_quotient_preimage_eq_image [s : setoid α] (g : quotient s → β) {h : α → β} (Hh : h = g ∘ quotient.mk) (r : set (β × β)) : {x : quotient s × quotient s | (g x.1, g x.2) ∈ r} = (λ a : α × α, (⟦a.1⟧, ⟦a.2⟧)) '' ((λ a : α × α, (h a.1, h a.2)) ⁻¹' r) := Hh.symm ▸ set.ext (λ ⟨a₁, a₂⟩, ⟨quotient.induction_on₂ a₁ a₂ (λ a₁ a₂ h, ⟨(a₁, a₂), h, rfl⟩), λ ⟨⟨b₁, b₂⟩, h₁, h₂⟩, show (g a₁, g a₂) ∈ r, from have h₃ : ⟦b₁⟧ = a₁ ∧ ⟦b₂⟧ = a₂ := prod.ext_iff.1 h₂, h₃.1 ▸ h₃.2 ▸ h₁⟩) /-- Restriction of `f` to `s` factors through `s.image_factorization f : s → f '' s`. -/ def image_factorization (f : α → β) (s : set α) : s → f '' s := λ p, ⟨f p.1, mem_image_of_mem f p.2⟩ lemma image_factorization_eq {f : α → β} {s : set α} : subtype.val ∘ image_factorization f s = f ∘ subtype.val := funext $ λ p, rfl lemma surjective_onto_image {f : α → β} {s : set α} : surjective (image_factorization f s) := λ ⟨_, ⟨a, ha, rfl⟩⟩, ⟨⟨a, ha⟩, rfl⟩ end image /-! ### Subsingleton -/ /-- A set `s` is a `subsingleton`, if it has at most one element. -/ protected def subsingleton (s : set α) : Prop := ∀ ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s), x = y lemma subsingleton.mono (ht : t.subsingleton) (hst : s ⊆ t) : s.subsingleton := λ x hx y hy, ht (hst hx) (hst hy) lemma subsingleton.image (hs : s.subsingleton) (f : α → β) : (f '' s).subsingleton := λ _ ⟨x, hx, Hx⟩ _ ⟨y, hy, Hy⟩, Hx ▸ Hy ▸ congr_arg f (hs hx hy) lemma subsingleton.eq_singleton_of_mem (hs : s.subsingleton) {x:α} (hx : x ∈ s) : s = {x} := ext $ λ y, ⟨λ hy, (hs hx hy) ▸ mem_singleton _, λ hy, (eq_of_mem_singleton hy).symm ▸ hx⟩ lemma subsingleton_empty : (∅ : set α).subsingleton := λ x, false.elim lemma subsingleton_singleton {a} : ({a} : set α).subsingleton := λ x hx y hy, (eq_of_mem_singleton hx).symm ▸ (eq_of_mem_singleton hy).symm ▸ rfl lemma subsingleton.eq_empty_or_singleton (hs : s.subsingleton) : s = ∅ ∨ ∃ x, s = {x} := s.eq_empty_or_nonempty.elim or.inl (λ ⟨x, hx⟩, or.inr ⟨x, hs.eq_singleton_of_mem hx⟩) lemma subsingleton_univ [subsingleton α] : (univ : set α).subsingleton := λ x hx y hy, subsingleton.elim x y /-- `s`, coerced to a type, is a subsingleton type if and only if `s` is a subsingleton set. -/ @[simp, norm_cast] lemma subsingleton_coe (s : set α) : subsingleton s ↔ s.subsingleton := begin split, { refine λ h, (λ a ha b hb, _), exact set_coe.ext_iff.2 (@subsingleton.elim s h ⟨a, ha⟩ ⟨b, hb⟩) }, { exact λ h, subsingleton.intro (λ a b, set_coe.ext (h a.property b.property)) } end /-- `s` is a subsingleton, if its image of an injective function is. -/ theorem subsingleton_of_image {α β : Type*} {f : α → β} (hf : function.injective f) (s : set α) (hs : subsingleton (f '' s)) : subsingleton s := subsingleton.intro $ λ ⟨a, ha⟩ ⟨b, hb⟩, subtype.ext $ hf (by {simpa using @subsingleton.elim _ hs ⟨f a, ⟨a, ha, rfl⟩⟩ ⟨f b, ⟨b, hb, rfl⟩⟩}) theorem univ_eq_true_false : univ = ({true, false} : set Prop) := eq.symm $ eq_univ_of_forall $ classical.cases (by simp) (by simp) /-! ### Lemmas about range of a function. -/ section range variables {f : ι → α} open function /-- Range of a function. This function is more flexible than `f '' univ`, as the image requires that the domain is in Type and not an arbitrary Sort. -/ def range (f : ι → α) : set α := {x | ∃y, f y = x} @[simp] theorem mem_range {x : α} : x ∈ range f ↔ ∃ y, f y = x := iff.rfl @[simp] theorem mem_range_self (i : ι) : f i ∈ range f := ⟨i, rfl⟩ theorem forall_range_iff {p : α → Prop} : (∀ a ∈ range f, p a) ↔ (∀ i, p (f i)) := by simp theorem exists_range_iff {p : α → Prop} : (∃ a ∈ range f, p a) ↔ (∃ i, p (f i)) := by simp lemma exists_range_iff' {p : α → Prop} : (∃ a, a ∈ range f ∧ p a) ↔ ∃ i, p (f i) := by simpa only [exists_prop] using exists_range_iff theorem range_iff_surjective : range f = univ ↔ surjective f := eq_univ_iff_forall alias range_iff_surjective ↔ _ function.surjective.range_eq @[simp] theorem range_id : range (@id α) = univ := range_iff_surjective.2 surjective_id theorem is_compl_range_inl_range_inr : is_compl (range $ @sum.inl α β) (range sum.inr) := ⟨by { rintro y ⟨⟨x₁, rfl⟩, ⟨x₂, _⟩⟩, cc }, by { rintro (x|y) -; [left, right]; exact mem_range_self _ }⟩ @[simp] theorem range_inl_union_range_inr : range (sum.inl : α → α ⊕ β) ∪ range sum.inr = univ := is_compl_range_inl_range_inr.sup_eq_top @[simp] theorem range_inl_inter_range_inr : range (sum.inl : α → α ⊕ β) ∩ range sum.inr = ∅ := is_compl_range_inl_range_inr.inf_eq_bot @[simp] theorem range_inr_union_range_inl : range (sum.inr : β → α ⊕ β) ∪ range sum.inl = univ := is_compl_range_inl_range_inr.symm.sup_eq_top @[simp] theorem range_inr_inter_range_inl : range (sum.inr : β → α ⊕ β) ∩ range sum.inl = ∅ := is_compl_range_inl_range_inr.symm.inf_eq_bot @[simp] theorem preimage_inl_range_inr : sum.inl ⁻¹' range (sum.inr : β → α ⊕ β) = ∅ := by { ext, simp } @[simp] theorem preimage_inr_range_inl : sum.inr ⁻¹' range (sum.inl : α → α ⊕ β) = ∅ := by { ext, simp } @[simp] theorem range_quot_mk (r : α → α → Prop) : range (quot.mk r) = univ := (surjective_quot_mk r).range_eq @[simp] theorem image_univ {ι : Type*} {f : ι → β} : f '' univ = range f := by { ext, simp [image, range] } theorem image_subset_range {ι : Type*} (f : ι → β) (s : set ι) : f '' s ⊆ range f := by rw ← image_univ; exact image_subset _ (subset_univ _) theorem range_comp (g : α → β) (f : ι → α) : range (g ∘ f) = g '' range f := subset.antisymm (forall_range_iff.mpr $ assume i, mem_image_of_mem g (mem_range_self _)) (ball_image_iff.mpr $ forall_range_iff.mpr mem_range_self) theorem range_subset_iff {s : set α} : range f ⊆ s ↔ ∀ y, f y ∈ s := forall_range_iff lemma range_comp_subset_range (f : α → β) (g : β → γ) : range (g ∘ f) ⊆ range g := by rw range_comp; apply image_subset_range lemma range_nonempty_iff_nonempty : (range f).nonempty ↔ nonempty ι := ⟨λ ⟨y, x, hxy⟩, ⟨x⟩, λ ⟨x⟩, ⟨f x, mem_range_self x⟩⟩ lemma range_nonempty [h : nonempty ι] (f : ι → α) : (range f).nonempty := range_nonempty_iff_nonempty.2 h @[simp] lemma range_eq_empty {f : ι → α} : range f = ∅ ↔ ¬ nonempty ι := not_nonempty_iff_eq_empty.symm.trans $ not_congr range_nonempty_iff_nonempty instance [nonempty ι] (f : ι → α) : nonempty (range f) := (range_nonempty f).to_subtype @[simp] lemma image_union_image_compl_eq_range (f : α → β) : (f '' s) ∪ (f '' sᶜ) = range f := by rw [← image_union, ← image_univ, ← union_compl_self] theorem image_preimage_eq_inter_range {f : α → β} {t : set β} : f '' (f ⁻¹' t) = t ∩ range f := ext $ assume x, ⟨assume ⟨x, hx, heq⟩, heq ▸ ⟨hx, mem_range_self _⟩, assume ⟨hx, ⟨y, h_eq⟩⟩, h_eq ▸ mem_image_of_mem f $ show y ∈ f ⁻¹' t, by simp [preimage, h_eq, hx]⟩ lemma image_preimage_eq_of_subset {f : α → β} {s : set β} (hs : s ⊆ range f) : f '' (f ⁻¹' s) = s := by rw [image_preimage_eq_inter_range, inter_eq_self_of_subset_left hs] lemma image_preimage_eq_iff {f : α → β} {s : set β} : f '' (f ⁻¹' s) = s ↔ s ⊆ range f := ⟨by { intro h, rw [← h], apply image_subset_range }, image_preimage_eq_of_subset⟩ lemma preimage_subset_preimage_iff {s t : set α} {f : β → α} (hs : s ⊆ range f) : f ⁻¹' s ⊆ f ⁻¹' t ↔ s ⊆ t := begin split, { intros h x hx, rcases hs hx with ⟨y, rfl⟩, exact h hx }, intros h x, apply h end lemma preimage_eq_preimage' {s t : set α} {f : β → α} (hs : s ⊆ range f) (ht : t ⊆ range f) : f ⁻¹' s = f ⁻¹' t ↔ s = t := begin split, { intro h, apply subset.antisymm, rw [←preimage_subset_preimage_iff hs, h], rw [←preimage_subset_preimage_iff ht, h] }, rintro rfl, refl end @[simp] theorem preimage_inter_range {f : α → β} {s : set β} : f ⁻¹' (s ∩ range f) = f ⁻¹' s := set.ext $ λ x, and_iff_left ⟨x, rfl⟩ @[simp] theorem preimage_range_inter {f : α → β} {s : set β} : f ⁻¹' (range f ∩ s) = f ⁻¹' s := by rw [inter_comm, preimage_inter_range] theorem preimage_image_preimage {f : α → β} {s : set β} : f ⁻¹' (f '' (f ⁻¹' s)) = f ⁻¹' s := by rw [image_preimage_eq_inter_range, preimage_inter_range] @[simp] theorem quot_mk_range_eq [setoid α] : range (λx : α, ⟦x⟧) = univ := range_iff_surjective.2 quot.exists_rep lemma range_const_subset {c : α} : range (λx:ι, c) ⊆ {c} := range_subset_iff.2 $ λ x, rfl @[simp] lemma range_const : ∀ [nonempty ι] {c : α}, range (λx:ι, c) = {c} | ⟨x⟩ c := subset.antisymm range_const_subset $ assume y hy, (mem_singleton_iff.1 hy).symm ▸ mem_range_self x lemma diagonal_eq_range {α : Type*} : diagonal α = range (λ x, (x, x)) := by { ext ⟨x, y⟩, simp [diagonal, eq_comm] } theorem preimage_singleton_nonempty {f : α → β} {y : β} : (f ⁻¹' {y}).nonempty ↔ y ∈ range f := iff.rfl theorem preimage_singleton_eq_empty {f : α → β} {y : β} : f ⁻¹' {y} = ∅ ↔ y ∉ range f := not_nonempty_iff_eq_empty.symm.trans $ not_congr preimage_singleton_nonempty lemma range_subset_singleton {f : ι → α} {x : α} : range f ⊆ {x} ↔ f = const ι x := by simp [range_subset_iff, funext_iff, mem_singleton] lemma image_compl_preimage {f : α → β} {s : set β} : f '' ((f ⁻¹' s)ᶜ) = range f \ s := by rw [compl_eq_univ_diff, image_diff_preimage, image_univ] @[simp] theorem range_sigma_mk {β : α → Type*} (a : α) : range (sigma.mk a : β a → Σ a, β a) = sigma.fst ⁻¹' {a} := begin apply subset.antisymm, { rintros _ ⟨b, rfl⟩, simp }, { rintros ⟨x, y⟩ (rfl|_), exact mem_range_self y } end /-- Any map `f : ι → β` factors through a map `range_factorization f : ι → range f`. -/ def range_factorization (f : ι → β) : ι → range f := λ i, ⟨f i, mem_range_self i⟩ lemma range_factorization_eq {f : ι → β} : subtype.val ∘ range_factorization f = f := funext $ λ i, rfl lemma surjective_onto_range : surjective (range_factorization f) := λ ⟨_, ⟨i, rfl⟩⟩, ⟨i, rfl⟩ lemma image_eq_range (f : α → β) (s : set α) : f '' s = range (λ(x : s), f x) := by { ext, split, rintro ⟨x, h1, h2⟩, exact ⟨⟨x, h1⟩, h2⟩, rintro ⟨⟨x, h1⟩, h2⟩, exact ⟨x, h1, h2⟩ } @[simp] lemma sum.elim_range {α β γ : Type*} (f : α → γ) (g : β → γ) : range (sum.elim f g) = range f ∪ range g := by simp [set.ext_iff, mem_range] lemma range_ite_subset' {p : Prop} [decidable p] {f g : α → β} : range (if p then f else g) ⊆ range f ∪ range g := begin by_cases h : p, {rw if_pos h, exact subset_union_left _ _}, {rw if_neg h, exact subset_union_right _ _} end lemma range_ite_subset {p : α → Prop} [decidable_pred p] {f g : α → β} : range (λ x, if p x then f x else g x) ⊆ range f ∪ range g := begin rw range_subset_iff, intro x, by_cases h : p x, simp [if_pos h, mem_union, mem_range_self], simp [if_neg h, mem_union, mem_range_self] end @[simp] lemma preimage_range (f : α → β) : f ⁻¹' (range f) = univ := eq_univ_of_forall mem_range_self /-- The range of a function from a `unique` type contains just the function applied to its single value. -/ lemma range_unique [h : unique ι] : range f = {f $ default ι} := begin ext x, rw mem_range, split, { rintros ⟨i, hi⟩, rw h.uniq i at hi, exact hi ▸ mem_singleton _ }, { exact λ h, ⟨default ι, h.symm⟩ } end lemma range_diff_image_subset (f : α → β) (s : set α) : range f \ f '' s ⊆ f '' sᶜ := λ y ⟨⟨x, h₁⟩, h₂⟩, ⟨x, λ h, h₂ ⟨x, h, h₁⟩, h₁⟩ lemma range_diff_image {f : α → β} (H : injective f) (s : set α) : range f \ f '' s = f '' sᶜ := subset.antisymm (range_diff_image_subset f s) $ λ y ⟨x, hx, hy⟩, hy ▸ ⟨mem_range_self _, λ ⟨x', hx', eq⟩, hx $ H eq ▸ hx'⟩ end range /-- The set `s` is pairwise `r` if `r x y` for all *distinct* `x y ∈ s`. -/ def pairwise_on (s : set α) (r : α → α → Prop) := ∀ x ∈ s, ∀ y ∈ s, x ≠ y → r x y theorem pairwise_on.mono {s t : set α} {r} (h : t ⊆ s) (hp : pairwise_on s r) : pairwise_on t r := λ x xt y yt, hp x (h xt) y (h yt) theorem pairwise_on.mono' {s : set α} {r r' : α → α → Prop} (H : ∀ a b, r a b → r' a b) (hp : pairwise_on s r) : pairwise_on s r' := λ x xs y ys h, H _ _ (hp x xs y ys h) /-- If and only if `f` takes pairwise equal values on `s`, there is some value it takes everywhere on `s`. -/ lemma pairwise_on_eq_iff_exists_eq [nonempty β] (s : set α) (f : α → β) : (pairwise_on s (λ x y, f x = f y)) ↔ ∃ z, ∀ x ∈ s, f x = z := begin split, { intro h, rcases eq_empty_or_nonempty s with rfl | ⟨x, hx⟩, { exact ⟨classical.arbitrary β, λ x hx, false.elim hx⟩ }, { use f x, intros y hy, by_cases hyx : y = x, { rw hyx }, { exact h y hy x hx hyx } } }, { rintros ⟨z, hz⟩ x hx y hy hne, rw [hz x hx, hz y hy] } end end set open set namespace function variables {ι : Sort*} {α : Type*} {β : Type*} {f : α → β} lemma surjective.preimage_injective (hf : surjective f) : injective (preimage f) := assume s t, (preimage_eq_preimage hf).1 lemma injective.preimage_image (hf : injective f) (s : set α) : f ⁻¹' (f '' s) = s := preimage_image_eq s hf lemma injective.preimage_surjective (hf : injective f) : surjective (preimage f) := by { intro s, use f '' s, rw hf.preimage_image } lemma surjective.image_preimage (hf : surjective f) (s : set β) : f '' (f ⁻¹' s) = s := image_preimage_eq s hf lemma surjective.image_surjective (hf : surjective f) : surjective (image f) := by { intro s, use f ⁻¹' s, rw hf.image_preimage } lemma injective.image_injective (hf : injective f) : injective (image f) := by { intros s t h, rw [←preimage_image_eq s hf, ←preimage_image_eq t hf, h] } lemma surjective.preimage_subset_preimage_iff {s t : set β} (hf : surjective f) : f ⁻¹' s ⊆ f ⁻¹' t ↔ s ⊆ t := by { apply preimage_subset_preimage_iff, rw [hf.range_eq], apply subset_univ } lemma surjective.range_comp {ι' : Sort*} {f : ι → ι'} (hf : surjective f) (g : ι' → α) : range (g ∘ f) = range g := ext $ λ y, (@surjective.exists _ _ _ hf (λ x, g x = y)).symm lemma injective.nonempty_apply_iff {f : set α → set β} (hf : injective f) (h2 : f ∅ = ∅) {s : set α} : (f s).nonempty ↔ s.nonempty := by rw [← ne_empty_iff_nonempty, ← h2, ← ne_empty_iff_nonempty, hf.ne_iff] end function open function /-! ### Image and preimage on subtypes -/ namespace subtype variable {α : Type*} lemma coe_image {p : α → Prop} {s : set (subtype p)} : coe '' s = {x | ∃h : p x, (⟨x, h⟩ : subtype p) ∈ s} := set.ext $ assume a, ⟨assume ⟨⟨a', ha'⟩, in_s, h_eq⟩, h_eq ▸ ⟨ha', in_s⟩, assume ⟨ha, in_s⟩, ⟨⟨a, ha⟩, in_s, rfl⟩⟩ lemma range_coe {s : set α} : range (coe : s → α) = s := by { rw ← set.image_univ, simp [-set.image_univ, coe_image] } /-- A variant of `range_coe`. Try to use `range_coe` if possible. This version is useful when defining a new type that is defined as the subtype of something. In that case, the coercion doesn't fire anymore. -/ lemma range_val {s : set α} : range (subtype.val : s → α) = s := range_coe /-- We make this the simp lemma instead of `range_coe`. The reason is that if we write for `s : set α` the function `coe : s → α`, then the inferred implicit arguments of `coe` are `coe α (λ x, x ∈ s)`. -/ @[simp] lemma range_coe_subtype {p : α → Prop} : range (coe : subtype p → α) = {x | p x} := range_coe @[simp] lemma coe_preimage_self (s : set α) : (coe : s → α) ⁻¹' s = univ := by rw [← preimage_range (coe : s → α), range_coe] lemma range_val_subtype {p : α → Prop} : range (subtype.val : subtype p → α) = {x | p x} := range_coe theorem coe_image_subset (s : set α) (t : set s) : coe '' t ⊆ s := λ x ⟨y, yt, yvaleq⟩, by rw ←yvaleq; exact y.property theorem coe_image_univ (s : set α) : (coe : s → α) '' set.univ = s := image_univ.trans range_coe @[simp] theorem image_preimage_coe (s t : set α) : (coe : s → α) '' (coe ⁻¹' t) = t ∩ s := image_preimage_eq_inter_range.trans $ congr_arg _ range_coe theorem image_preimage_val (s t : set α) : (subtype.val : s → α) '' (subtype.val ⁻¹' t) = t ∩ s := image_preimage_coe s t theorem preimage_coe_eq_preimage_coe_iff {s t u : set α} : ((coe : s → α) ⁻¹' t = coe ⁻¹' u) ↔ t ∩ s = u ∩ s := begin rw [←image_preimage_coe, ←image_preimage_coe], split, { intro h, rw h }, intro h, exact coe_injective.image_injective h end theorem preimage_val_eq_preimage_val_iff (s t u : set α) : ((subtype.val : s → α) ⁻¹' t = subtype.val ⁻¹' u) ↔ (t ∩ s = u ∩ s) := preimage_coe_eq_preimage_coe_iff lemma exists_set_subtype {t : set α} (p : set α → Prop) : (∃(s : set t), p (coe '' s)) ↔ ∃(s : set α), s ⊆ t ∧ p s := begin split, { rintro ⟨s, hs⟩, refine ⟨coe '' s, _, hs⟩, convert image_subset_range _ _, rw [range_coe] }, rintro ⟨s, hs₁, hs₂⟩, refine ⟨coe ⁻¹' s, _⟩, rw [image_preimage_eq_of_subset], exact hs₂, rw [range_coe], exact hs₁ end lemma preimage_coe_nonempty {s t : set α} : ((coe : s → α) ⁻¹' t).nonempty ↔ (s ∩ t).nonempty := by rw [inter_comm, ← image_preimage_coe, nonempty_image_iff] lemma preimage_coe_eq_empty {s t : set α} : (coe : s → α) ⁻¹' t = ∅ ↔ s ∩ t = ∅ := by simp only [← not_nonempty_iff_eq_empty, preimage_coe_nonempty] @[simp] lemma preimage_coe_compl (s : set α) : (coe : s → α) ⁻¹' sᶜ = ∅ := preimage_coe_eq_empty.2 (inter_compl_self s) @[simp] lemma preimage_coe_compl' (s : set α) : (coe : sᶜ → α) ⁻¹' s = ∅ := preimage_coe_eq_empty.2 (compl_inter_self s) end subtype namespace set /-! ### Lemmas about cartesian product of sets -/ section prod variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables {s s₁ s₂ : set α} {t t₁ t₂ : set β} /-- The cartesian product `prod s t` is the set of `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def prod (s : set α) (t : set β) : set (α × β) := {p | p.1 ∈ s ∧ p.2 ∈ t} lemma prod_eq (s : set α) (t : set β) : s.prod t = prod.fst ⁻¹' s ∩ prod.snd ⁻¹' t := rfl theorem mem_prod_eq {p : α × β} : p ∈ s.prod t = (p.1 ∈ s ∧ p.2 ∈ t) := rfl @[simp] theorem mem_prod {p : α × β} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[simp] theorem prod_mk_mem_set_prod_eq {a : α} {b : β} : (a, b) ∈ s.prod t = (a ∈ s ∧ b ∈ t) := rfl lemma mk_mem_prod {a : α} {b : β} (a_in : a ∈ s) (b_in : b ∈ t) : (a, b) ∈ s.prod t := ⟨a_in, b_in⟩ theorem prod_mono {s₁ s₂ : set α} {t₁ t₂ : set β} (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) : s₁.prod t₁ ⊆ s₂.prod t₂ := assume x ⟨h₁, h₂⟩, ⟨hs h₁, ht h₂⟩ lemma prod_subset_iff {P : set (α × β)} : (s.prod t ⊆ P) ↔ ∀ (x ∈ s) (y ∈ t), (x, y) ∈ P := ⟨λ h _ xin _ yin, h (mk_mem_prod xin yin), λ h ⟨_, _⟩ pin, h _ pin.1 _ pin.2⟩ lemma forall_prod_set {p : α × β → Prop} : (∀ x ∈ s.prod t, p x) ↔ ∀ (x ∈ s) (y ∈ t), p (x, y) := prod_subset_iff lemma exists_prod_set {p : α × β → Prop} : (∃ x ∈ s.prod t, p x) ↔ ∃ (x ∈ s) (y ∈ t), p (x, y) := by simp [and_assoc] @[simp] theorem prod_empty : s.prod ∅ = (∅ : set (α × β)) := by { ext, simp } @[simp] theorem empty_prod : set.prod ∅ t = (∅ : set (α × β)) := by { ext, simp } @[simp] theorem univ_prod_univ : (@univ α).prod (@univ β) = univ := by { ext ⟨x, y⟩, simp } lemma univ_prod {t : set β} : set.prod (univ : set α) t = prod.snd ⁻¹' t := by simp [prod_eq] lemma prod_univ {s : set α} : set.prod s (univ : set β) = prod.fst ⁻¹' s := by simp [prod_eq] @[simp] theorem singleton_prod {a : α} : set.prod {a} t = prod.mk a '' t := by { ext ⟨x, y⟩, simp [and.left_comm, eq_comm] } @[simp] theorem prod_singleton {b : β} : s.prod {b} = (λ a, (a, b)) '' s := by { ext ⟨x, y⟩, simp [and.left_comm, eq_comm] } theorem singleton_prod_singleton {a : α} {b : β} : set.prod {a} {b} = ({(a, b)} : set (α × β)) := by simp @[simp] theorem union_prod : (s₁ ∪ s₂).prod t = s₁.prod t ∪ s₂.prod t := by { ext ⟨x, y⟩, simp [or_and_distrib_right] } @[simp] theorem prod_union : s.prod (t₁ ∪ t₂) = s.prod t₁ ∪ s.prod t₂ := by { ext ⟨x, y⟩, simp [and_or_distrib_left] } theorem prod_inter_prod : s₁.prod t₁ ∩ s₂.prod t₂ = (s₁ ∩ s₂).prod (t₁ ∩ t₂) := by { ext ⟨x, y⟩, simp [and_assoc, and.left_comm] } theorem insert_prod {a : α} : (insert a s).prod t = (prod.mk a '' t) ∪ s.prod t := by { ext ⟨x, y⟩, simp [image, iff_def, or_imp_distrib, imp.swap] {contextual := tt} } theorem prod_insert {b : β} : s.prod (insert b t) = ((λa, (a, b)) '' s) ∪ s.prod t := by { ext ⟨x, y⟩, simp [image, iff_def, or_imp_distrib, imp.swap] {contextual := tt} } theorem prod_preimage_eq {f : γ → α} {g : δ → β} : (preimage f s).prod (preimage g t) = preimage (λp, (f p.1, g p.2)) (s.prod t) := rfl @[simp] lemma mk_preimage_prod_left {y : β} (h : y ∈ t) : (λ x, (x, y)) ⁻¹' s.prod t = s := by { ext x, simp [h] } @[simp] lemma mk_preimage_prod_right {x : α} (h : x ∈ s) : prod.mk x ⁻¹' s.prod t = t := by { ext y, simp [h] } @[simp] lemma mk_preimage_prod_left_eq_empty {y : β} (hy : y ∉ t) : (λ x, (x, y)) ⁻¹' s.prod t = ∅ := by { ext z, simp [hy] } @[simp] lemma mk_preimage_prod_right_eq_empty {x : α} (hx : x ∉ s) : prod.mk x ⁻¹' s.prod t = ∅ := by { ext z, simp [hx] } lemma mk_preimage_prod_left_eq_if {y : β} [decidable_pred (∈ t)] : (λ x, (x, y)) ⁻¹' s.prod t = if y ∈ t then s else ∅ := by { split_ifs; simp [h] } lemma mk_preimage_prod_right_eq_if {x : α} [decidable_pred (∈ s)] : prod.mk x ⁻¹' s.prod t = if x ∈ s then t else ∅ := by { split_ifs; simp [h] } theorem image_swap_eq_preimage_swap : image (@prod.swap α β) = preimage prod.swap := image_eq_preimage_of_inverse prod.swap_left_inverse prod.swap_right_inverse theorem preimage_swap_prod {s : set α} {t : set β} : prod.swap ⁻¹' t.prod s = s.prod t := by { ext ⟨x, y⟩, simp [and_comm] } theorem image_swap_prod : prod.swap '' t.prod s = s.prod t := by rw [image_swap_eq_preimage_swap, preimage_swap_prod] theorem prod_image_image_eq {m₁ : α → γ} {m₂ : β → δ} : (image m₁ s).prod (image m₂ t) = image (λp:α×β, (m₁ p.1, m₂ p.2)) (s.prod t) := ext $ by simp [-exists_and_distrib_right, exists_and_distrib_right.symm, and.left_comm, and.assoc, and.comm] theorem prod_range_range_eq {α β γ δ} {m₁ : α → γ} {m₂ : β → δ} : (range m₁).prod (range m₂) = range (λp:α×β, (m₁ p.1, m₂ p.2)) := ext $ by simp [range] theorem prod_range_univ_eq {α β γ} {m₁ : α → γ} : (range m₁).prod (univ : set β) = range (λp:α×β, (m₁ p.1, p.2)) := ext $ by simp [range] theorem prod_univ_range_eq {α β δ} {m₂ : β → δ} : (univ : set α).prod (range m₂) = range (λp:α×β, (p.1, m₂ p.2)) := ext $ by simp [range] theorem nonempty.prod : s.nonempty → t.nonempty → (s.prod t).nonempty | ⟨x, hx⟩ ⟨y, hy⟩ := ⟨(x, y), ⟨hx, hy⟩⟩ theorem nonempty.fst : (s.prod t).nonempty → s.nonempty | ⟨p, hp⟩ := ⟨p.1, hp.1⟩ theorem nonempty.snd : (s.prod t).nonempty → t.nonempty | ⟨p, hp⟩ := ⟨p.2, hp.2⟩ theorem prod_nonempty_iff : (s.prod t).nonempty ↔ s.nonempty ∧ t.nonempty := ⟨λ h, ⟨h.fst, h.snd⟩, λ h, nonempty.prod h.1 h.2⟩ theorem prod_eq_empty_iff : s.prod t = ∅ ↔ (s = ∅ ∨ t = ∅) := by simp only [not_nonempty_iff_eq_empty.symm, prod_nonempty_iff, not_and_distrib] lemma prod_sub_preimage_iff {W : set γ} {f : α × β → γ} : s.prod t ⊆ f ⁻¹' W ↔ ∀ a b, a ∈ s → b ∈ t → f (a, b) ∈ W := by simp [subset_def] lemma fst_image_prod_subset (s : set α) (t : set β) : prod.fst '' (s.prod t) ⊆ s := λ _ h, let ⟨_, ⟨h₂, _⟩, h₁⟩ := (set.mem_image _ _ _).1 h in h₁ ▸ h₂ lemma prod_subset_preimage_fst (s : set α) (t : set β) : s.prod t ⊆ prod.fst ⁻¹' s := image_subset_iff.1 (fst_image_prod_subset s t) lemma fst_image_prod (s : set β) {t : set α} (ht : t.nonempty) : prod.fst '' (s.prod t) = s := set.subset.antisymm (fst_image_prod_subset _ _) $ λ y y_in, let ⟨x, x_in⟩ := ht in ⟨(y, x), ⟨y_in, x_in⟩, rfl⟩ lemma snd_image_prod_subset (s : set α) (t : set β) : prod.snd '' (s.prod t) ⊆ t := λ _ h, let ⟨_, ⟨_, h₂⟩, h₁⟩ := (set.mem_image _ _ _).1 h in h₁ ▸ h₂ lemma prod_subset_preimage_snd (s : set α) (t : set β) : s.prod t ⊆ prod.snd ⁻¹' t := image_subset_iff.1 (snd_image_prod_subset s t) lemma snd_image_prod {s : set α} (hs : s.nonempty) (t : set β) : prod.snd '' (s.prod t) = t := set.subset.antisymm (snd_image_prod_subset _ _) $ λ y y_in, let ⟨x, x_in⟩ := hs in ⟨(x, y), ⟨x_in, y_in⟩, rfl⟩ /-- A product set is included in a product set if and only factors are included, or a factor of the first set is empty. -/ lemma prod_subset_prod_iff : (s.prod t ⊆ s₁.prod t₁) ↔ (s ⊆ s₁ ∧ t ⊆ t₁) ∨ (s = ∅) ∨ (t = ∅) := begin classical, cases (s.prod t).eq_empty_or_nonempty with h h, { simp [h, prod_eq_empty_iff.1 h] }, { have st : s.nonempty ∧ t.nonempty, by rwa [prod_nonempty_iff] at h, split, { assume H : s.prod t ⊆ s₁.prod t₁, have h' : s₁.nonempty ∧ t₁.nonempty := prod_nonempty_iff.1 (h.mono H), refine or.inl ⟨_, _⟩, show s ⊆ s₁, { have := image_subset (prod.fst : α × β → α) H, rwa [fst_image_prod _ st.2, fst_image_prod _ h'.2] at this }, show t ⊆ t₁, { have := image_subset (prod.snd : α × β → β) H, rwa [snd_image_prod st.1, snd_image_prod h'.1] at this } }, { assume H, simp only [st.1.ne_empty, st.2.ne_empty, or_false] at H, exact prod_mono H.1 H.2 } } end end prod /-! ### Lemmas about set-indexed products of sets -/ section pi variables {ι : Type*} {α : ι → Type*} {s : set ι} {t t₁ t₂ : Π i, set (α i)} /-- Given an index set `i` and a family of sets `s : Π i, set (α i)`, `pi i s` is the set of dependent functions `f : Πa, π a` such that `f a` belongs to `π a` whenever `a ∈ i`. -/ def pi (s : set ι) (t : Π i, set (α i)) : set (Π i, α i) := { f | ∀i ∈ s, f i ∈ t i } @[simp] lemma mem_pi {f : Π i, α i} : f ∈ s.pi t ↔ ∀ i ∈ s, f i ∈ t i := by refl @[simp] lemma mem_univ_pi {f : Π i, α i} : f ∈ pi univ t ↔ ∀ i, f i ∈ t i := by simp @[simp] lemma empty_pi (s : Π i, set (α i)) : pi ∅ s = univ := by { ext, simp [pi] } lemma pi_mono (h : ∀ i ∈ s, t₁ i ⊆ t₂ i) : pi s t₁ ⊆ pi s t₂ := λ x hx i hi, (h i hi $ hx i hi) lemma pi_eq_empty {i : ι} (hs : i ∈ s) (ht : t i = ∅) : s.pi t = ∅ := by { ext f, simp only [mem_empty_eq, not_forall, iff_false, mem_pi, not_imp], exact ⟨i, hs, by simp [ht]⟩ } lemma univ_pi_eq_empty {i : ι} (ht : t i = ∅) : pi univ t = ∅ := pi_eq_empty (mem_univ i) ht lemma pi_nonempty_iff : (s.pi t).nonempty ↔ ∀ i, ∃ x, i ∈ s → x ∈ t i := by simp [classical.skolem, set.nonempty] lemma univ_pi_nonempty_iff : (pi univ t).nonempty ↔ ∀ i, (t i).nonempty := by simp [classical.skolem, set.nonempty] lemma pi_eq_empty_iff : s.pi t = ∅ ↔ ∃ i, (α i → false) ∨ (i ∈ s ∧ t i = ∅) := begin rw [← not_nonempty_iff_eq_empty, pi_nonempty_iff], push_neg, apply exists_congr, intro i, split, { intro h, by_cases hα : nonempty (α i), { cases hα with x, refine or.inr ⟨(h x).1, by simp [eq_empty_iff_forall_not_mem, h]⟩ }, { exact or.inl (λ x, hα ⟨x⟩) }}, { rintro (h|h) x, exfalso, exact h x, simp [h] } end lemma univ_pi_eq_empty_iff : pi univ t = ∅ ↔ ∃ i, t i = ∅ := by simp [← not_nonempty_iff_eq_empty, univ_pi_nonempty_iff] @[simp] lemma insert_pi (i : ι) (s : set ι) (t : Π i, set (α i)) : pi (insert i s) t = (eval i ⁻¹' t i) ∩ pi s t := by { ext, simp [pi, or_imp_distrib, forall_and_distrib] } @[simp] lemma singleton_pi (i : ι) (t : Π i, set (α i)) : pi {i} t = (eval i ⁻¹' t i) := by { ext, simp [pi] } lemma pi_if {p : ι → Prop} [h : decidable_pred p] (s : set ι) (t₁ t₂ : Π i, set (α i)) : pi s (λ i, if p i then t₁ i else t₂ i) = pi {i ∈ s | p i} t₁ ∩ pi {i ∈ s | ¬ p i} t₂ := begin ext f, split, { assume h, split; { rintros i ⟨his, hpi⟩, simpa [*] using h i } }, { rintros ⟨ht₁, ht₂⟩ i his, by_cases p i; simp * at * } end open_locale classical lemma eval_image_pi {i : ι} (hs : i ∈ s) (ht : (s.pi t).nonempty) : eval i '' s.pi t = t i := begin ext x, rcases ht with ⟨f, hf⟩, split, { rintro ⟨g, hg, rfl⟩, exact hg i hs }, { intro hg, refine ⟨update f i x, _, by simp⟩, intros j hj, by_cases hji : j = i, { subst hji, simp [hg] }, { rw [mem_pi] at hf, simp [hji, hf, hj] }}, end @[simp] lemma eval_image_univ_pi {i : ι} (ht : (pi univ t).nonempty) : (λ f : Π i, α i, f i) '' pi univ t = t i := eval_image_pi (mem_univ i) ht lemma update_preimage_pi {i : ι} {f : Π i, α i} (hi : i ∈ s) (hf : ∀ j ∈ s, j ≠ i → f j ∈ t j) : (update f i) ⁻¹' s.pi t = t i := begin ext x, split, { intro h, convert h i hi, simp }, { intros hx j hj, by_cases h : j = i, { cases h, simpa }, { rw [update_noteq h], exact hf j hj h }} end lemma update_preimage_univ_pi {i : ι} {f : Π i, α i} (hf : ∀ j ≠ i, f j ∈ t j) : (update f i) ⁻¹' pi univ t = t i := update_preimage_pi (mem_univ i) (λ j _, hf j) lemma subset_pi_eval_image (s : set ι) (u : set (Π i, α i)) : u ⊆ pi s (λ i, eval i '' u) := λ f hf i hi, ⟨f, hf, rfl⟩ end pi /-! ### Lemmas about `inclusion`, the injection of subtypes induced by `⊆` -/ section inclusion variable {α : Type*} /-- `inclusion` is the "identity" function between two subsets `s` and `t`, where `s ⊆ t` -/ def inclusion {s t : set α} (h : s ⊆ t) : s → t := λ x : s, (⟨x, h x.2⟩ : t) @[simp] lemma inclusion_self {s : set α} (x : s) : inclusion (set.subset.refl _) x = x := by { cases x, refl } @[simp] lemma inclusion_right {s t : set α} (h : s ⊆ t) (x : t) (m : (x : α) ∈ s) : inclusion h ⟨x, m⟩ = x := by { cases x, refl } @[simp] lemma inclusion_inclusion {s t u : set α} (hst : s ⊆ t) (htu : t ⊆ u) (x : s) : inclusion htu (inclusion hst x) = inclusion (set.subset.trans hst htu) x := by { cases x, refl } @[simp] lemma coe_inclusion {s t : set α} (h : s ⊆ t) (x : s) : (inclusion h x : α) = (x : α) := rfl lemma inclusion_injective {s t : set α} (h : s ⊆ t) : function.injective (inclusion h) | ⟨_, _⟩ ⟨_, _⟩ := subtype.ext_iff_val.2 ∘ subtype.ext_iff_val.1 lemma eq_of_inclusion_surjective {s t : set α} {h : s ⊆ t} (h_surj : function.surjective (inclusion h)) : s = t := begin apply set.subset.antisymm h, intros x hx, cases h_surj ⟨x, hx⟩ with y key, rw [←subtype.coe_mk x hx, ←key, coe_inclusion], exact subtype.mem y, end lemma range_inclusion {s t : set α} (h : s ⊆ t) : range (inclusion h) = {x : t | (x:α) ∈ s} := by { ext ⟨x, hx⟩, simp [inclusion] } end inclusion /-! ### Injectivity and surjectivity lemmas for image and preimage -/ section image_preimage variables {α : Type u} {β : Type v} {f : α → β} @[simp] lemma preimage_injective : injective (preimage f) ↔ surjective f := begin refine ⟨λ h y, _, surjective.preimage_injective⟩, obtain ⟨x, hx⟩ : (f ⁻¹' {y}).nonempty, { rw [h.nonempty_apply_iff preimage_empty], apply singleton_nonempty }, exact ⟨x, hx⟩ end @[simp] lemma preimage_surjective : surjective (preimage f) ↔ injective f := begin refine ⟨λ h x x' hx, _, injective.preimage_surjective⟩, cases h {x} with s hs, have := mem_singleton x, rwa [← hs, mem_preimage, hx, ← mem_preimage, hs, mem_singleton_iff, eq_comm] at this end @[simp] lemma image_surjective : surjective (image f) ↔ surjective f := begin refine ⟨λ h y, _, surjective.image_surjective⟩, cases h {y} with s hs, have := mem_singleton y, rw [← hs] at this, rcases this with ⟨x, h1x, h2x⟩, exact ⟨x, h2x⟩ end @[simp] lemma image_injective : injective (image f) ↔ injective f := begin refine ⟨λ h x x' hx, _, injective.image_injective⟩, rw [← singleton_eq_singleton_iff], apply h, rw [image_singleton, image_singleton, hx] end end image_preimage /-! ### Lemmas about images of binary and ternary functions -/ section n_ary_image variables {α β γ δ ε : Type*} {f f' : α → β → γ} {g g' : α → β → γ → δ} variables {s s' : set α} {t t' : set β} {u u' : set γ} {a a' : α} {b b' : β} {c c' : γ} {d d' : δ} /-- The image of a binary function `f : α → β → γ` as a function `set α → set β → set γ`. Mathematically this should be thought of as the image of the corresponding function `α × β → γ`. -/ def image2 (f : α → β → γ) (s : set α) (t : set β) : set γ := {c | ∃ a b, a ∈ s ∧ b ∈ t ∧ f a b = c } lemma mem_image2_eq : c ∈ image2 f s t = ∃ a b, a ∈ s ∧ b ∈ t ∧ f a b = c := rfl @[simp] lemma mem_image2 : c ∈ image2 f s t ↔ ∃ a b, a ∈ s ∧ b ∈ t ∧ f a b = c := iff.rfl lemma mem_image2_of_mem (h1 : a ∈ s) (h2 : b ∈ t) : f a b ∈ image2 f s t := ⟨a, b, h1, h2, rfl⟩ lemma mem_image2_iff (hf : injective2 f) : f a b ∈ image2 f s t ↔ a ∈ s ∧ b ∈ t := ⟨ by { rintro ⟨a', b', ha', hb', h⟩, rcases hf h with ⟨rfl, rfl⟩, exact ⟨ha', hb'⟩ }, λ ⟨ha, hb⟩, mem_image2_of_mem ha hb⟩ /-- image2 is monotone with respect to `⊆`. -/ lemma image2_subset (hs : s ⊆ s') (ht : t ⊆ t') : image2 f s t ⊆ image2 f s' t' := by { rintro _ ⟨a, b, ha, hb, rfl⟩, exact mem_image2_of_mem (hs ha) (ht hb) } lemma forall_image2_iff {p : γ → Prop} : (∀ z ∈ image2 f s t, p z) ↔ ∀ (x ∈ s) (y ∈ t), p (f x y) := ⟨λ h x hx y hy, h _ ⟨x, y, hx, hy, rfl⟩, λ h z ⟨x, y, hx, hy, hz⟩, hz ▸ h x hx y hy⟩ @[simp] lemma image2_subset_iff {u : set γ} : image2 f s t ⊆ u ↔ ∀ (x ∈ s) (y ∈ t), f x y ∈ u := forall_image2_iff lemma image2_union_left : image2 f (s ∪ s') t = image2 f s t ∪ image2 f s' t := begin ext c, split, { rintros ⟨a, b, h1a|h2a, hb, rfl⟩;[left, right]; exact ⟨_, _, ‹_›, ‹_›, rfl⟩ }, { rintro (⟨_, _, _, _, rfl⟩|⟨_, _, _, _, rfl⟩); refine ⟨_, _, _, ‹_›, rfl⟩; simp [mem_union, *] } end lemma image2_union_right : image2 f s (t ∪ t') = image2 f s t ∪ image2 f s t' := begin ext c, split, { rintros ⟨a, b, ha, h1b|h2b, rfl⟩;[left, right]; exact ⟨_, _, ‹_›, ‹_›, rfl⟩ }, { rintro (⟨_, _, _, _, rfl⟩|⟨_, _, _, _, rfl⟩); refine ⟨_, _, ‹_›, _, rfl⟩; simp [mem_union, *] } end @[simp] lemma image2_empty_left : image2 f ∅ t = ∅ := ext $ by simp @[simp] lemma image2_empty_right : image2 f s ∅ = ∅ := ext $ by simp lemma image2_inter_subset_left : image2 f (s ∩ s') t ⊆ image2 f s t ∩ image2 f s' t := by { rintro _ ⟨a, b, ⟨h1a, h2a⟩, hb, rfl⟩, split; exact ⟨_, _, ‹_›, ‹_›, rfl⟩ } lemma image2_inter_subset_right : image2 f s (t ∩ t') ⊆ image2 f s t ∩ image2 f s t' := by { rintro _ ⟨a, b, ha, ⟨h1b, h2b⟩, rfl⟩, split; exact ⟨_, _, ‹_›, ‹_›, rfl⟩ } @[simp] lemma image2_singleton_left : image2 f {a} t = f a '' t := ext $ λ x, by simp @[simp] lemma image2_singleton_right : image2 f s {b} = (λ a, f a b) '' s := ext $ λ x, by simp lemma image2_singleton : image2 f {a} {b} = {f a b} := by simp @[congr] lemma image2_congr (h : ∀ (a ∈ s) (b ∈ t), f a b = f' a b) : image2 f s t = image2 f' s t := by { ext, split; rintro ⟨a, b, ha, hb, rfl⟩; refine ⟨a, b, ha, hb, by rw h a ha b hb⟩ } /-- A common special case of `image2_congr` -/ lemma image2_congr' (h : ∀ a b, f a b = f' a b) : image2 f s t = image2 f' s t := image2_congr (λ a _ b _, h a b) /-- The image of a ternary function `f : α → β → γ → δ` as a function `set α → set β → set γ → set δ`. Mathematically this should be thought of as the image of the corresponding function `α × β × γ → δ`. -/ def image3 (g : α → β → γ → δ) (s : set α) (t : set β) (u : set γ) : set δ := {d | ∃ a b c, a ∈ s ∧ b ∈ t ∧ c ∈ u ∧ g a b c = d } @[simp] lemma mem_image3 : d ∈ image3 g s t u ↔ ∃ a b c, a ∈ s ∧ b ∈ t ∧ c ∈ u ∧ g a b c = d := iff.rfl @[congr] lemma image3_congr (h : ∀ (a ∈ s) (b ∈ t) (c ∈ u), g a b c = g' a b c) : image3 g s t u = image3 g' s t u := by { ext x, split; rintro ⟨a, b, c, ha, hb, hc, rfl⟩; refine ⟨a, b, c, ha, hb, hc, by rw h a ha b hb c hc⟩ } /-- A common special case of `image3_congr` -/ lemma image3_congr' (h : ∀ a b c, g a b c = g' a b c) : image3 g s t u = image3 g' s t u := image3_congr (λ a _ b _ c _, h a b c) lemma image2_image2_left (f : δ → γ → ε) (g : α → β → δ) : image2 f (image2 g s t) u = image3 (λ a b c, f (g a b) c) s t u := begin ext, split, { rintro ⟨_, c, ⟨a, b, ha, hb, rfl⟩, hc, rfl⟩, refine ⟨a, b, c, ha, hb, hc, rfl⟩ }, { rintro ⟨a, b, c, ha, hb, hc, rfl⟩, refine ⟨_, c, ⟨a, b, ha, hb, rfl⟩, hc, rfl⟩ } end lemma image2_image2_right (f : α → δ → ε) (g : β → γ → δ) : image2 f s (image2 g t u) = image3 (λ a b c, f a (g b c)) s t u := begin ext, split, { rintro ⟨a, _, ha, ⟨b, c, hb, hc, rfl⟩, rfl⟩, refine ⟨a, b, c, ha, hb, hc, rfl⟩ }, { rintro ⟨a, b, c, ha, hb, hc, rfl⟩, refine ⟨a, _, ha, ⟨b, c, hb, hc, rfl⟩, rfl⟩ } end lemma image2_assoc {ε'} {f : δ → γ → ε} {g : α → β → δ} {f' : α → ε' → ε} {g' : β → γ → ε'} (h_assoc : ∀ a b c, f (g a b) c = f' a (g' b c)) : image2 f (image2 g s t) u = image2 f' s (image2 g' t u) := by simp only [image2_image2_left, image2_image2_right, h_assoc] lemma image_image2 (f : α → β → γ) (g : γ → δ) : g '' image2 f s t = image2 (λ a b, g (f a b)) s t := begin ext, split, { rintro ⟨_, ⟨a, b, ha, hb, rfl⟩, rfl⟩, refine ⟨a, b, ha, hb, rfl⟩ }, { rintro ⟨a, b, ha, hb, rfl⟩, refine ⟨_, ⟨a, b, ha, hb, rfl⟩, rfl⟩ } end lemma image2_image_left (f : γ → β → δ) (g : α → γ) : image2 f (g '' s) t = image2 (λ a b, f (g a) b) s t := begin ext, split, { rintro ⟨_, b, ⟨a, ha, rfl⟩, hb, rfl⟩, refine ⟨a, b, ha, hb, rfl⟩ }, { rintro ⟨a, b, ha, hb, rfl⟩, refine ⟨_, b, ⟨a, ha, rfl⟩, hb, rfl⟩ } end lemma image2_image_right (f : α → γ → δ) (g : β → γ) : image2 f s (g '' t) = image2 (λ a b, f a (g b)) s t := begin ext, split, { rintro ⟨a, _, ha, ⟨b, hb, rfl⟩, rfl⟩, refine ⟨a, b, ha, hb, rfl⟩ }, { rintro ⟨a, b, ha, hb, rfl⟩, refine ⟨a, _, ha, ⟨b, hb, rfl⟩, rfl⟩ } end lemma image2_swap (f : α → β → γ) (s : set α) (t : set β) : image2 f s t = image2 (λ a b, f b a) t s := by { ext, split; rintro ⟨a, b, ha, hb, rfl⟩; refine ⟨b, a, hb, ha, rfl⟩ } @[simp] lemma image2_left (h : t.nonempty) : image2 (λ x y, x) s t = s := by simp [nonempty_def.mp h, ext_iff] @[simp] lemma image2_right (h : s.nonempty) : image2 (λ x y, y) s t = t := by simp [nonempty_def.mp h, ext_iff] @[simp] lemma image_prod (f : α → β → γ) : (λ x : α × β, f x.1 x.2) '' s.prod t = image2 f s t := set.ext $ λ a, ⟨ by { rintros ⟨_, _, rfl⟩, exact ⟨_, _, (mem_prod.mp ‹_›).1, (mem_prod.mp ‹_›).2, rfl⟩ }, by { rintros ⟨_, _, _, _, rfl⟩, exact ⟨(_, _), mem_prod.mpr ⟨‹_›, ‹_›⟩, rfl⟩ }⟩ lemma nonempty.image2 (hs : s.nonempty) (ht : t.nonempty) : (image2 f s t).nonempty := by { cases hs with a ha, cases ht with b hb, exact ⟨f a b, ⟨a, b, ha, hb, rfl⟩⟩ } end n_ary_image end set namespace subsingleton variables {α : Type*} [subsingleton α] lemma eq_univ_of_nonempty {s : set α} : s.nonempty → s = univ := λ ⟨x, hx⟩, eq_univ_of_forall $ λ y, subsingleton.elim x y ▸ hx @[elab_as_eliminator] lemma set_cases {p : set α → Prop} (h0 : p ∅) (h1 : p univ) (s) : p s := s.eq_empty_or_nonempty.elim (λ h, h.symm ▸ h0) $ λ h, (eq_univ_of_nonempty h).symm ▸ h1 end subsingleton
53a94a2e502a5bb3be67cac9c42325751beeb7f2
cf39355caa609c0f33405126beee2739aa3cb77e
/library/init/data/char/default.lean
52f887199f3938cb58826cdf9c4e8fc68e99f26e
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
245
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.data.char.basic init.data.char.lemmas init.data.char.classes
927eef837e3ac379ca3ac968c432a39bd129a373
e031d1fbf8353b338e3189e0d9aec3adb5bb0512
/src/data/malcev.lean
968241f55a76de291eb611444326fe406b0385ec
[ "Apache-2.0" ]
permissive
UniversalAlgebra/lean-ualib
e64431a70007a835b1dd933d66be04ffca118601
ab9cbddbb5bdf1eeac4b0d5994bd6cad2a3665d4
refs/heads/master
1,584,931,281,084
1,558,364,533,000
1,558,364,533,000
140,986,567
6
0
Apache-2.0
1,532,718,578,000
1,531,613,794,000
Lean
UTF-8
Lean
false
false
252
lean
class has_ternary_op (α : Type*) := (ternary_op : α → α → α → α) class has_malcev_op (α : Type*) extends has_ternary_op α := (malcev_op_left : ∀ x y : α, ternary_op x y y = x) (malcev_op_right : ∀ x y : α, ternary_op y y x = x)
85973883f4ce6ebb338269547ce3394e020aae79
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/test/lint_simp_nf.lean
6bdc26e2d635ccdf4b07d2f990e52baea82805a8
[ "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
941
lean
import tactic.lint def f : ℕ → ℕ := default _ def c : ℕ := default _ def d : ℕ := default _ @[simp] lemma c_eq_d : c = d := rfl -- The following lemma never applies when using simp, because c is first rewritten to d @[simp] lemma f_c : f c = 0 := rfl example : f c = 0 := begin simp, guard_target f d = 0, -- does not apply f_c refl end open tactic run_cmd do decl ← get_decl ``f_c, res ← linter.simp_nf.test decl, -- linter complains guard $ res.is_some -- also works with `coe_to_fun` structure morphism := (f : ℕ → ℕ) instance : has_coe_to_fun morphism (λ _, ℕ → ℕ):= ⟨morphism.f⟩ def h : morphism := ⟨default _⟩ -- Also never applies @[simp] lemma h_c : h c = 0 := rfl example : h c = 0 := begin simp, guard_target h d = 0, -- does not apply h_c refl end open tactic run_cmd do decl ← get_decl ``h_c, res ← linter.simp_nf.test decl, -- linter complains guard $ res.is_some
d60b9b45e06859ae58b97908bf5452b7ca8e9ebd
2bafba05c98c1107866b39609d15e849a4ca2bb8
/src/week_2/Part_B_subgroups.lean
d0a94146b4eaa1ac02ccf58d8a0c86da416b3bcc
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/formalising-mathematics
b54c83c94b5c315024ff09997fcd6b303892a749
7cf1d51c27e2038d2804561d63c74711924044a1
refs/heads/master
1,651,267,046,302
1,638,888,459,000
1,638,888,459,000
331,592,375
284
24
Apache-2.0
1,669,593,705,000
1,611,224,849,000
Lean
UTF-8
Lean
false
false
17,544
lean
/- Change the below line to import week_2.kb_solutions.Part_A_groups_solutions (once the solutions are posted) if you want to get rid of the warning -/ import week_2.Part_A_groups /-! ## Subgroups We define the structure `subgroup G`, whose terms are subgroups of `G`. A subgroup of `G` is implemented as a subset of `G` closed under `1`, `*` and `⁻¹`. -/ namespace xena /-- A subgroup of a group G is a subset containing 1 and closed under multiplication and inverse. -/ structure subgroup (G : Type) [group G] := (carrier : set G) (one_mem' : (1 : G) ∈ carrier) (mul_mem' {x y} : x ∈ carrier → y ∈ carrier → x * y ∈ carrier) (inv_mem' {x} : x ∈ carrier → x⁻¹ ∈ carrier) /- At this point, here's what we have. A term `H` of type `subgroup G`, written `H : subgroup G`, is a *quadruple*. To give a term `H : subgroup G` is to give the following four things: 1) `H.carrier` (a subset of `G`), 2) `H.one_mem'` (a proof that `1 ∈ H.carrier`), 3) `H.mul_mem'` (a proof that `H` is closed under multiplication) 4) `H.inv_mem'` (a proof that `H` is closed under inverses). Note in particular that Lean, being super-pedantic, *distinguishes* between the subgroup `H` and the subset `H.carrier`. One is a subgroup, one is a subset. When we get going we will start by setting up some infrastructure so that this difference will be hard to notice. Note also that if `x` is in the subgroup `H` of `H` then the _type_ of `x` is still `G`, and `x ∈ carrier` is a Proposition. Note also that `x : carrier` doesn't make sense (`carrier` is a term, not a type, rather counterintuitively). -/ namespace subgroup open xena.group -- let G be a group and let H and K be subgroups variables {G : Type} [group G] (H J K : subgroup G) /- This `h ∈ H.carrier` notation kind of stinks. I don't want to write `H.carrier` everywhere, because I want to be able to identify the subgroup `H` with its underlying subset `H.carrier`. Note that these things are not _equal_, firstly because `H` contains the proof that `H.carrier` is a subgroup, and secondly because these terms have different types! `H` has type `subgroup G` and `H.carrier` has type `set G`. Let's start by sorting this out. -/ -- If `x : G` and `H : subgroup G` then let's define the notation -- `x ∈ H` to mean `x ∈ H.carrier` instance : has_mem G (subgroup G) := ⟨λ m H, m ∈ H.carrier⟩ -- Let's also define a "coercion", a.k.a. an "invisible map" -- from subgroups of `G` to subsets of `G`, sending `H` to `H.carrier`. -- The map is not completely invisible -- it's a little ↑. So -- if you see `↑H` in the future, it means the subset `H.carrier` by definition. instance : has_coe (subgroup G) (set G) := ⟨λ H, H.carrier⟩ -- `λ` is just computer science notation for ↦ (mapsto); so -- `λ H, H.carrier` is the function `H ↦ H.carrier`. -- Let's check we have this working, and also tell the simplifier that we -- would rather talk about `g ∈ H` than any other way of saying it. /-- `g` is in the underlying subset of `H` iff `g ∈ H`. -/ @[simp] lemma mem_carrier {g : G} : g ∈ H.carrier ↔ g ∈ H := begin -- true by definition refl end /-- `g` is in `H` considered as a subset of `G`, iff `g` is in `H` considered as subgroup of `G`. -/ @[simp] lemma mem_coe {g : G} : g ∈ (↑H : set G) ↔ g ∈ H := begin -- true by definition refl end -- Now let's define theorems without the `'`s in, which use this -- more natural notation /-- A subgroup contains the group's 1. -/ theorem one_mem : (1 : G) ∈ H := begin apply H.one_mem', end /-- A subgroup is closed under multiplication. -/ theorem mul_mem {x y : G} : x ∈ H → y ∈ H → x * y ∈ H := begin -- what do you think? sorry end /-- A subgroup is closed under inverse -/ theorem inv_mem {x : G} : x ∈ H → x⁻¹ ∈ H := begin -- what do you think? sorry end /- So here are the three theorems which you need to remember about subgroups. Say `H : subgroup G`. Then: `H.one_mem : (1 : G) ∈ H` `H.mul_mem {x y : G} : x ∈ H → y ∈ H → x * y ∈ H` `H.inv_mem {x : G} : x ∈ H → x⁻¹ ∈ H` These now look like the way a mathematician would write things. Now let's start to prove basic theorems about subgroups (or, as a the computer scientists would say, make a basic _interface_ or _API_ for subgroups), using this sensible notation. Here's an example; let's prove `x ∈ H ↔ x⁻¹ ∈ H`. Let's put the more complicated expression on the left hand side of the `↔` though, because then we can make it a `simp` lemma. -/ -- Remember that `xena.group.inv_inv x` is the statement that `x⁻¹⁻¹ = x` @[simp] theorem inv_mem_iff {x : G} : x⁻¹ ∈ H ↔ x ∈ H := begin sorry, end -- We could prove a bunch more theorems here. Let's just do one more. -- Let's show that if x and xy are in H then so is y. theorem mem_of_mem_mul_mem {x y : G} (hx : x ∈ H) (hxy : x * y ∈ H) : y ∈ H := begin sorry, end /- Subgroups are extensional objects (like most things in mathematics): two subgroups are equal if they have the same underlying subset, and also if they have the same underlying elements. Let's prove variants of this triviality now. The first one is rather un-mathematical: it takes a subgroup apart into its pieces. I'll see if you can do the other two! -/ /-- Two subgroups are equal if the underlying subsets are equal. -/ theorem ext' {H K : subgroup G} (h : H.carrier = K.carrier) : H = K := begin -- first take H and K apart cases H, -- H now broken up into its underlying 3-tuple. cases K, -- and now it must be obvious, so let's see if the simplifier can do it. simp * at *, -- it can! end -- here's a variant. You can prove it using `ext'`. /-- Two subgroups are equal if and only if the underlying subsets are equal. -/ theorem ext'_iff {H K : subgroup G} : H.carrier = K.carrier ↔ H = K := begin sorry, end -- to do this next one, first apply the `ext'` theorem we just proved, -- and then use the `ext` tactic (which works on sets) /-- Two subgroups are equal if they have the same elements. -/ @[ext] theorem ext {H K : subgroup G} (h : ∀ x, x ∈ H ↔ x ∈ K) : H = K := begin sorry, end /- We tagged that theorem with `ext`, so now the `ext` tactic works on subgroups too: if you ever have a goal of proving that two subgroups are equal, you can use the `ext` tactic to reduce to showing that they have the same elements. -/ /- ## The lattice structure on subgroups Subgroups of a group form what is known as a *lattice*. This is a partially ordered set with a sensible notion of max and min. We partially order subgroups by saying `H ≤ K` if `H.carrier ⊆ K.carrier`. Subgroups even have a good notion of an infinite Sup and Inf (the Inf of a bunch of subgroups is just their intersection; their Sup is the subgroup generated by their union). This combinatorial structure (a partially ordered set with good finite and infinite notions of Sup and Inf) is called a "complete lattice", and Lean has this structure inbuilt into it. We will construct a complete lattice structure on `subgroup G`. We start by defining a relation ≤ on the type of subgroups of a group. We say H ≤ K iff H.carrier ⊆ K.carrier . -/ /-- If `H` and `K` are subgroups of `G`, we write `H ≤ K` to mean `H.carrier ⊆ K.carrier` -/ instance : has_le (subgroup G) := ⟨λ H K, H.carrier ⊆ K.carrier⟩ -- useful to restate the definition so we can `rw` it lemma le_def : H ≤ K ↔ H.carrier ⊆ K.carrier := begin -- true by definition refl end -- another useful variant lemma le_iff : H ≤ K ↔ ∀ g, g ∈ H → g ∈ K := begin -- true by definition refl, end -- Now let's check the axioms for a partial order. -- These are not hard, they just reduce immediately to the -- fact that ⊆ is a partial order @[refl] lemma le_refl : H ≤ H := begin rw le_def, -- Lean knows ⊆ is reflexive so the sneaky `refl` which Lean tries after `rw` -- has closed the goal! end lemma le_antisymm : H ≤ K → K ≤ H → H = K := begin rw [le_def, le_def, ← ext'_iff], -- now this is antisymmetry of ⊆, which Lean knows exact set.subset.antisymm, end @[trans] lemma le_trans : H ≤ J → J ≤ K → H ≤ K := begin rw [le_def, le_def, le_def], -- now this is transitivity of ⊆, which Lean knows exact set.subset.trans, end -- We've made `subgroup G` into a partial order! instance : partial_order (subgroup G) := { le := (≤), le_refl := le_refl, le_antisymm := le_antisymm, le_trans := le_trans } /- ### intersections Let's prove that the intersection of two subgroups is a subgroup. In Lean this is a definition: given two subgroups, we define a new subgroup whose underlying subset is the intersection of the subsets, and then prove the axioms. -/ /-- The intersection of two subgroups is also a subgroup -/ def inf (H K : subgroup G) : subgroup G := { carrier := H.carrier ∩ K.carrier, -- the carrier is the intersection one_mem' := begin -- recall that x ∈ Y ∩ Z is _by definition_ x ∈ Y ∧ x ∈ Z, so you can `split` this. sorry, end, mul_mem' := begin sorry, end, inv_mem' := begin sorry, end } -- Notation for `inf` in computer science circles is ⊓ . instance : has_inf (subgroup G) := ⟨inf⟩ /-- The underlying set of the inf of two subgroups is just their intersection -/ lemma inf_def (H K : subgroup G) : (H ⊓ K : set G) = (H : set G) ∩ K := begin -- true by definition refl end /- ## Subgroup generated by a subset. To do the sup of two subgroups is harder, because we don't just take the union, we need to then look at the subgroup generated by this union (e.g. the union of the x and y axes in ℝ² is not a subgroup). So we need to have a machine to spit out the subgroup of `G` generated by a subset `S : set G`. There are two completely different ways to do this. The first is a "top-down" approach. We could define the subgroup generated by `S` to be the intersection of all the subgroups of `G` that contain `S`. The second is a "bottom-up" approach. We could define the subgroup generated by `S` "by induction" (or more precisely by recursion), saying that `S` is in the subgroup, 1 is in the subgroup, the product of two things in the subgroup is in the subgroup, the inverse of something in the subgroup is in the subgroup, and that's it. Both methods come out rather nicely in Lean. Let's do the first one. We are going to be using a bunch of theorems about "bounded intersections", a.k.a. `set.bInter`. We will soon get tired of writing `set.blah` so let's `open set` so that we can skip it. -/ open set /- Here is the API for `set.bInter` (or `bInter`, as we can now call it): Notation: `⋂` (type with `\I`) If `X : set (subgroup G)`, i.e. if `X` is a set of subgroups of `G`, then `⋂ K ∈ X, (K : set G)` means "take the intersection of the underlying subsets". -- mem_bInter_iff says you're in the intersection iff you're in -- all the things you're intersecting. Very useful for rewriting. `mem_bInter_iff : (g ∈ ⋂ (K ∈ S), f K) ↔ (∀ K, K ∈ s → g ∈ f K)` -- mem_bInter is just the one way implication. Very useful for `apply`ing. `mem_bInter : (∀ K, K ∈ s → g ∈ f K) → (g ∈ ⋂ (K ∈ S), f K)` -/ /- We will consider the closure of a set as the intersect of all subgroups containing the set -/ /-- The Inf of a set of subgroups of G is their intersection. -/ def Inf (X : set (subgroup G)) : subgroup G := { carrier := ⋂ K ∈ X, (K : set G), -- carrier is the intersection of the underlying sets one_mem' := begin sorry, end, mul_mem' := begin sorry, end, inv_mem' := begin sorry, end, } /-- The *closure* of a subset `S` of `G` is the `Inf` of the subgroups of `G` which contain `S`. -/ def closure (S : set G) : subgroup G := Inf {H : subgroup G | S ⊆ H} -- we can restate mem_bInter_iff using our new "closure" language: lemma mem_closure_iff {S : set G} {x : G} : x ∈ closure S ↔ ∀ H : subgroup G, S ⊆ H → x ∈ H := mem_bInter_iff /- There is an underlying abstraction here, which you may not know about. A "closure operator" in mathematics https://en.wikipedia.org/wiki/Closure_operator is something mapping subsets of a set X to subsets of X, and satisfying three axioms: 1) `subset_closure : S ⊆ closure S` 2) `closure_mono : (S ⊆ T) → (closure S ⊆ closure T)` 3) `closure_closure : closure (closure S) = closure S` It works for closure in topological spaces, and it works here too. It also works for algebraic closures of fields, and there are several other places in mathematics where it shows up. This idea, of "abstracting" and axiomatising a phenomenon which shows up in more than one place, is really key in Lean. Let's prove these three lemmas in the case where `X = G` and `closure S` is the subgroup generated by `S`. Here are some things you might find helpful. Remember `mem_coe : g ∈ ↑H ↔ g ∈ H` `mem_carrier : g ∈ H.carrier ↔ g ∈ H` There's `mem_closure_iff : x ∈ closure S ↔ ∀ (H : subgroup G), S ⊆ ↑H → x ∈ H` (`closure S` is a subgroup so you might need to use `mem_coe` or `mem_carrier` first) For subsets there's `subset.trans : X ⊆ Y → Y ⊆ Z → X ⊆ Z` You might find `le_antisymm : H ≤ K → K ≤ H → H = K` from above useful -/ /- Reminder: X ⊆ Y means `∀ g, g ∈ X → g ∈ Y` and it's definitional, so you can just start this with `intro g`. -/ lemma subset_closure (S : set G) : S ⊆ ↑(closure S) := begin sorry, end -- It's useful to know `subset.trans : X ⊆ Y → Y ⊆ Z → X ⊆ Z` lemma closure_mono {S T : set G} (hST : S ⊆ T) : closure S ≤ closure T := begin sorry, end -- not one of the axioms, but sometimes handy lemma closure_le (S : set G) (H : subgroup G) : closure S ≤ H ↔ S ⊆ ↑H := begin sorry, end -- You can start this one by applying `le_antisymm`, lemma closure_closure (S : set G) : closure S = closure (closure S) := begin sorry, end -- This shows that every subgroup is the closure of something, namely its -- underlying subset. lemma closure_self {H : subgroup G} : closure ↑H = H := begin sorry, end /- Recall the second proposed construction of the subgroup closure of a subset `S`; it is the smallest subgroup `H` of `G` such that `S ⊆ H` and which contains `1` and is closed under `*` and `⁻¹`. This inductive constuction (which we did not make) comes with a so-called "recursor": if we have a true/false statement `p g` attached to each element `g` of G with the following properties: 1) `p s` is true for all `s ∈ S`, 2) `p 1` is true, 3) If `p x` and `p y` then `p (x * y)`, 4) If `p x` then `p x⁻¹` Then `p` is true on all of `closure S`. If we had made an inductive definition of `closure S` then this would have been true by definition! We used another definition, so we will have to prove it ourselves. -/ /-- An induction principle for closures. -/ lemma closure_induction {p : G → Prop} {S : set G} (HS : ∀ x ∈ S, p x) (H1 : p 1) (Hmul : ∀ x y, p x → p y → p (x * y)) (Hinv : ∀ x, p x → p x⁻¹) : -- conclusion after colon ∀ x, x ∈ closure S → p x := begin -- the subset of G where `p` is true is a subgroup. Let's call it H let H : subgroup G := { carrier := p, one_mem' := H1, mul_mem' := Hmul, inv_mem' := Hinv }, -- The goal is just that closure S ≤ H, by definition. change closure S ≤ H, -- Our hypothesis HS is just that S ⊆ ↑H, by definition change S ⊆ ↑H at HS, -- I think you can take it from here! sorry, end /- Finally we prove that the `closure` and `coe` maps form a `galois_insertion`. This is another abstraction, it generalises `galois_connection`, which is something that shows up all over the place (algebraic geometry, Galois theory etc). See https://en.wikipedia.org/wiki/Galois_connection A partial order can be considered as a category, with Hom(A,B) having one element if A ≤ B and no elements otherwise. A Galois connection between two partial orders is just a pair of adjoint functors between the categories. Adjointness in our case is `S ⊆ ↑H ↔ closure S ≤ H`. The reason it's an insertion and not just a connection is that if you start with a subgroup, take the underlying subset, and then look at the subgroup generated by that set, you get back to where you started. So it's like one of the adjoint functors being a forgetful functor. -/ def gi : galois_insertion (closure : set G → subgroup G) (coe : subgroup G → set G) := { choice := λ S _, closure S, gc := closure_le, le_l_u := λ H, subset_closure (H : set G), choice_eq := λ _ _, rfl } /- One use of this abstraction is that now we can pull back the complete lattice structure on `set G` to get a complete lattice structure on `subgroup G`. -/ instance : complete_lattice (subgroup G) := {.. galois_insertion.lift_complete_lattice gi} /- We just proved loads of lemmas about Infs and Sups of subgroups automatically, and have access to a ton more because the `complete_lattice` structure in Lean has a big API. See for example https://leanprover-community.github.io/mathlib_docs/order/complete_lattice.html#complete_lattice All those theorems are now true for subgroups. None are particularly hard to prove, but the point is that we don't now have to prove any of them ourselves. -/ end subgroup end xena /- Further work: `bot` and `top` (would have to explain the API for `singleton` and `univ`) -/
1f105431f61c716e480ddcaa9d717bcf2f307c78
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/convex/extrema.lean
416e8a5a1fdcdb3043e0620f5c82bf006b9d32ab
[ "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
4,318
lean
/- Copyright (c) 2020 Frédéric Dupuis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Frédéric Dupuis -/ import analysis.convex.function import topology.algebra.affine import topology.local_extr import topology.metric_space.basic /-! # Minima and maxima of convex functions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We show that if a function `f : E → β` is convex, then a local minimum is also a global minimum, and likewise for concave functions. -/ variables {E β : Type*} [add_comm_group E] [topological_space E] [module ℝ E] [topological_add_group E] [has_continuous_smul ℝ E] [ordered_add_comm_group β] [module ℝ β] [ordered_smul ℝ β] {s : set E} open set filter function open_locale classical topology /-- Helper lemma for the more general case: `is_min_on.of_is_local_min_on_of_convex_on`. -/ lemma is_min_on.of_is_local_min_on_of_convex_on_Icc {f : ℝ → β} {a b : ℝ} (a_lt_b : a < b) (h_local_min : is_local_min_on f (Icc a b) a) (h_conv : convex_on ℝ (Icc a b) f) : is_min_on f (Icc a b) a := begin rintro c hc, dsimp only [mem_set_of_eq], rw [is_local_min_on, nhds_within_Icc_eq_nhds_within_Ici a_lt_b] at h_local_min, rcases hc.1.eq_or_lt with rfl|a_lt_c, { exact le_rfl }, have H₁ : ∀ᶠ y in 𝓝[>] a, f a ≤ f y, from h_local_min.filter_mono (nhds_within_mono _ Ioi_subset_Ici_self), have H₂ : ∀ᶠ y in 𝓝[>] a, y ∈ Ioc a c, from Ioc_mem_nhds_within_Ioi (left_mem_Ico.2 a_lt_c), rcases (H₁.and H₂).exists with ⟨y, hfy, hy_ac⟩, rcases (convex.mem_Ioc a_lt_c).mp hy_ac with ⟨ya, yc, ya₀, yc₀, yac, rfl⟩, suffices : ya • f a + yc • f a ≤ ya • f a + yc • f c, from (smul_le_smul_iff_of_pos yc₀).1 (le_of_add_le_add_left this), calc ya • f a + yc • f a = f a : by rw [← add_smul, yac, one_smul] ... ≤ f (ya * a + yc * c) : hfy ... ≤ ya • f a + yc • f c : h_conv.2 (left_mem_Icc.2 a_lt_b.le) hc ya₀ yc₀.le yac end /-- A local minimum of a convex function is a global minimum, restricted to a set `s`. -/ lemma is_min_on.of_is_local_min_on_of_convex_on {f : E → β} {a : E} (a_in_s : a ∈ s) (h_localmin : is_local_min_on f s a) (h_conv : convex_on ℝ s f) : is_min_on f s a := begin intros x x_in_s, let g : ℝ →ᵃ[ℝ] E := affine_map.line_map a x, have hg0 : g 0 = a := affine_map.line_map_apply_zero a x, have hg1 : g 1 = x := affine_map.line_map_apply_one a x, have hgc : continuous g, from affine_map.line_map_continuous, have h_maps : maps_to g (Icc 0 1) s, { simpa only [maps_to', ← segment_eq_image_line_map] using h_conv.1.segment_subset a_in_s x_in_s }, have fg_local_min_on : is_local_min_on (f ∘ g) (Icc 0 1) 0, { rw ← hg0 at h_localmin, exact h_localmin.comp_continuous_on h_maps hgc.continuous_on (left_mem_Icc.2 zero_le_one) }, have fg_min_on : is_min_on (f ∘ g) (Icc 0 1 : set ℝ) 0, { refine is_min_on.of_is_local_min_on_of_convex_on_Icc one_pos fg_local_min_on _, exact (h_conv.comp_affine_map g).subset h_maps (convex_Icc 0 1) }, simpa only [hg0, hg1, comp_app, mem_set_of_eq] using fg_min_on (right_mem_Icc.2 zero_le_one) end /-- A local maximum of a concave function is a global maximum, restricted to a set `s`. -/ lemma is_max_on.of_is_local_max_on_of_concave_on {f : E → β} {a : E} (a_in_s : a ∈ s) (h_localmax: is_local_max_on f s a) (h_conc : concave_on ℝ s f) : is_max_on f s a := @is_min_on.of_is_local_min_on_of_convex_on _ βᵒᵈ _ _ _ _ _ _ _ _ s f a a_in_s h_localmax h_conc /-- A local minimum of a convex function is a global minimum. -/ lemma is_min_on.of_is_local_min_of_convex_univ {f : E → β} {a : E} (h_local_min : is_local_min f a) (h_conv : convex_on ℝ univ f) : ∀ x, f a ≤ f x := λ x, (is_min_on.of_is_local_min_on_of_convex_on (mem_univ a) (h_local_min.on univ) h_conv) (mem_univ x) /-- A local maximum of a concave function is a global maximum. -/ lemma is_max_on.of_is_local_max_of_convex_univ {f : E → β} {a : E} (h_local_max : is_local_max f a) (h_conc : concave_on ℝ univ f) : ∀ x, f x ≤ f a := @is_min_on.of_is_local_min_of_convex_univ _ βᵒᵈ _ _ _ _ _ _ _ _ f a h_local_max h_conc
42ac248083c989dafb8f5bc81c8397196625be51
8e6cad62ec62c6c348e5faaa3c3f2079012bdd69
/src/group_theory/subgroup.lean
74de473bc083cdbc9fd303fc4b12b10c8d310a3a
[ "Apache-2.0" ]
permissive
benjamindavidson/mathlib
8cc81c865aa8e7cf4462245f58d35ae9a56b150d
fad44b9f670670d87c8e25ff9cdf63af87ad731e
refs/heads/master
1,679,545,578,362
1,615,343,014,000
1,615,343,014,000
312,926,983
0
0
Apache-2.0
1,615,360,301,000
1,605,399,418,000
Lean
UTF-8
Lean
false
false
57,954
lean
/- Copyright (c) 2020 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import group_theory.submonoid import algebra.group.conj import algebra.pointwise import order.modular_lattice /-! # Subgroups This file defines multiplicative and additive subgroups as an extension of submonoids, in a bundled form (unbundled subgroups are in `deprecated/subgroups.lean`). We prove subgroups of a group form a complete lattice, and results about images and preimages of subgroups under group homomorphisms. The bundled subgroups use bundled monoid homomorphisms. There are also theorems about the subgroups generated by an element or a subset of a group, defined both inductively and as the infimum of the set of subgroups containing a given element/subset. Special thanks goes to Amelia Livingston and Yury Kudryashov for their help and inspiration. ## Main definitions Notation used here: - `G N` are `group`s - `A` is an `add_group` - `H K` are `subgroup`s of `G` or `add_subgroup`s of `A` - `x` is an element of type `G` or type `A` - `f g : N →* G` are group homomorphisms - `s k` are sets of elements of type `G` Definitions in the file: * `subgroup G` : the type of subgroups of a group `G` * `add_subgroup A` : the type of subgroups of an additive group `A` * `complete_lattice (subgroup G)` : the subgroups of `G` form a complete lattice * `subgroup.closure k` : the minimal subgroup that includes the set `k` * `subgroup.subtype` : the natural group homomorphism from a subgroup of group `G` to `G` * `subgroup.gi` : `closure` forms a Galois insertion with the coercion to set * `subgroup.comap H f` : the preimage of a subgroup `H` along the group homomorphism `f` is also a subgroup * `subgroup.map f H` : the image of a subgroup `H` along the group homomorphism `f` is also a subgroup * `subgroup.prod H K` : the product of subgroups `H`, `K` of groups `G`, `N` respectively, `H × K` is a subgroup of `G × N` * `monoid_hom.range f` : the range of the group homomorphism `f` is a subgroup * `monoid_hom.ker f` : the kernel of a group homomorphism `f` is the subgroup of elements `x : G` such that `f x = 1` * `monoid_hom.eq_locus f g` : given group homomorphisms `f`, `g`, the elements of `G` such that `f x = g x` form a subgroup of `G` ## Implementation notes Subgroup inclusion is denoted `≤` rather than `⊆`, although `∈` is defined as membership of a subgroup's underlying set. ## Tags subgroup, subgroups -/ open_locale big_operators variables {G : Type*} [group G] variables {A : Type*} [add_group A] set_option old_structure_cmd true /-- A subgroup of a group `G` is a subset containing 1, closed under multiplication and closed under multiplicative inverse. -/ structure subgroup (G : Type*) [group G] extends submonoid G := (inv_mem' {x} : x ∈ carrier → x⁻¹ ∈ carrier) /-- An additive subgroup of an additive group `G` is a subset containing 0, closed under addition and additive inverse. -/ structure add_subgroup (G : Type*) [add_group G] extends add_submonoid G:= (neg_mem' {x} : x ∈ carrier → -x ∈ carrier) attribute [to_additive] subgroup attribute [to_additive add_subgroup.to_add_submonoid] subgroup.to_submonoid /-- Reinterpret a `subgroup` as a `submonoid`. -/ add_decl_doc subgroup.to_submonoid /-- Reinterpret an `add_subgroup` as an `add_submonoid`. -/ add_decl_doc add_subgroup.to_add_submonoid /-- Map from subgroups of group `G` to `add_subgroup`s of `additive G`. -/ def subgroup.to_add_subgroup {G : Type*} [group G] (H : subgroup G) : add_subgroup (additive G) := { neg_mem' := H.inv_mem', .. submonoid.to_add_submonoid H.to_submonoid} /-- Map from `add_subgroup`s of `additive G` to subgroups of `G`. -/ def subgroup.of_add_subgroup {G : Type*} [group G] (H : add_subgroup (additive G)) : subgroup G := { inv_mem' := H.neg_mem', .. submonoid.of_add_submonoid H.to_add_submonoid} /-- Map from `add_subgroup`s of `add_group G` to subgroups of `multiplicative G`. -/ def add_subgroup.to_subgroup {G : Type*} [add_group G] (H : add_subgroup G) : subgroup (multiplicative G) := { inv_mem' := H.neg_mem', .. add_submonoid.to_submonoid H.to_add_submonoid} /-- Map from subgroups of `multiplicative G` to `add_subgroup`s of `add_group G`. -/ def add_subgroup.of_subgroup {G : Type*} [add_group G] (H : subgroup (multiplicative G)) : add_subgroup G := { neg_mem' := H.inv_mem', .. add_submonoid.of_submonoid H.to_submonoid } /-- Subgroups of group `G` are isomorphic to additive subgroups of `additive G`. -/ def subgroup.add_subgroup_equiv (G : Type*) [group G] : subgroup G ≃ add_subgroup (additive G) := { to_fun := subgroup.to_add_subgroup, inv_fun := subgroup.of_add_subgroup, left_inv := λ x, by cases x; refl, right_inv := λ x, by cases x; refl } namespace subgroup @[to_additive] instance : has_coe (subgroup G) (set G) := { coe := subgroup.carrier } @[simp, to_additive] lemma coe_to_submonoid (K : subgroup G) : (K.to_submonoid : set G) = K := rfl @[to_additive] instance : has_mem G (subgroup G) := ⟨λ m K, m ∈ (K : set G)⟩ @[to_additive] instance : has_coe_to_sort (subgroup G) := ⟨_, λ G, (G : Type*)⟩ @[simp, norm_cast, to_additive] lemma mem_coe {K : subgroup G} {g : G} : g ∈ (K : set G) ↔ g ∈ K := iff.rfl @[simp, norm_cast, to_additive] lemma coe_coe (K : subgroup G) : ↥(K : set G) = K := rfl -- note that `to_additive` transfers the `simp` attribute over but not the `norm_cast` attribute attribute [norm_cast] add_subgroup.mem_coe attribute [norm_cast] add_subgroup.coe_coe @[to_additive] instance (K : subgroup G) [d : decidable_pred K.carrier] [fintype G] : fintype K := show fintype {g : G // g ∈ K.carrier}, from infer_instance end subgroup @[to_additive] protected lemma subgroup.exists {K : subgroup G} {p : K → Prop} : (∃ x : K, p x) ↔ ∃ x ∈ K, p ⟨x, ‹x ∈ K›⟩ := set_coe.exists @[to_additive] protected lemma subgroup.forall {K : subgroup G} {p : K → Prop} : (∀ x : K, p x) ↔ ∀ x ∈ K, p ⟨x, ‹x ∈ K›⟩ := set_coe.forall namespace subgroup variables (H K : subgroup G) /-- Copy of a subgroup with a new `carrier` equal to the old one. Useful to fix definitional equalities.-/ @[to_additive "Copy of an additive subgroup with a new `carrier` equal to the old one. Useful to fix definitional equalities"] protected def copy (K : subgroup G) (s : set G) (hs : s = K) : subgroup G := { carrier := s, one_mem' := hs.symm ▸ K.one_mem', mul_mem' := hs.symm ▸ K.mul_mem', inv_mem' := hs.symm ▸ K.inv_mem' } /- Two subgroups are equal if the underlying set are the same. -/ @[to_additive "Two `add_group`s are equal if the underlying subsets are equal."] theorem ext' {H K : subgroup G} (h : (H : set G) = K) : H = K := by { cases H, cases K, congr, exact h } /- Two subgroups are equal if and only if the underlying subsets are equal. -/ @[to_additive "Two `add_subgroup`s are equal if and only if the underlying subsets are equal."] protected theorem ext'_iff {H K : subgroup G} : H = K ↔ (H : set G) = K := ⟨λ h, h ▸ rfl, ext'⟩ /-- Two subgroups are equal if they have the same elements. -/ @[ext, to_additive "Two `add_subgroup`s are equal if they have the same elements."] theorem ext {H K : subgroup G} (h : ∀ x, x ∈ H ↔ x ∈ K) : H = K := ext' $ set.ext h attribute [ext] add_subgroup.ext /-- A subgroup contains the group's 1. -/ @[to_additive "An `add_subgroup` contains the group's 0."] theorem one_mem : (1 : G) ∈ H := H.one_mem' /-- A subgroup is closed under multiplication. -/ @[to_additive "An `add_subgroup` is closed under addition."] theorem mul_mem {x y : G} : x ∈ H → y ∈ H → x * y ∈ H := λ hx hy, H.mul_mem' hx hy /-- A subgroup is closed under inverse. -/ @[to_additive "An `add_subgroup` is closed under inverse."] theorem inv_mem {x : G} : x ∈ H → x⁻¹ ∈ H := λ hx, H.inv_mem' hx /-- A subgroup is closed under division. -/ @[to_additive "An `add_subgroup` is closed under subtraction."] theorem div_mem {x y : G} (hx : x ∈ H) (hy : y ∈ H) : x / y ∈ H := by simpa only [div_eq_mul_inv] using H.mul_mem' hx (H.inv_mem' hy) @[simp, to_additive] theorem inv_mem_iff {x : G} : x⁻¹ ∈ H ↔ x ∈ H := ⟨λ h, inv_inv x ▸ H.inv_mem h, H.inv_mem⟩ @[to_additive] lemma mul_mem_cancel_right {x y : G} (h : x ∈ H) : y * x ∈ H ↔ y ∈ H := ⟨λ hba, by simpa using H.mul_mem hba (H.inv_mem h), λ hb, H.mul_mem hb h⟩ @[to_additive] lemma mul_mem_cancel_left {x y : G} (h : x ∈ H) : x * y ∈ H ↔ y ∈ H := ⟨λ hab, by simpa using H.mul_mem (H.inv_mem h) hab, H.mul_mem h⟩ /-- Product of a list of elements in a subgroup is in the subgroup. -/ @[to_additive "Sum of a list of elements in an `add_subgroup` is in the `add_subgroup`."] lemma list_prod_mem {l : list G} : (∀ x ∈ l, x ∈ K) → l.prod ∈ K := K.to_submonoid.list_prod_mem /-- Product of a multiset of elements in a subgroup of a `comm_group` is in the subgroup. -/ @[to_additive "Sum of a multiset of elements in an `add_subgroup` of an `add_comm_group` is in the `add_subgroup`."] lemma multiset_prod_mem {G} [comm_group G] (K : subgroup G) (g : multiset G) : (∀ a ∈ g, a ∈ K) → g.prod ∈ K := K.to_submonoid.multiset_prod_mem g /-- Product of elements of a subgroup of a `comm_group` indexed by a `finset` is in the subgroup. -/ @[to_additive "Sum of elements in an `add_subgroup` of an `add_comm_group` indexed by a `finset` is in the `add_subgroup`."] lemma prod_mem {G : Type*} [comm_group G] (K : subgroup G) {ι : Type*} {t : finset ι} {f : ι → G} (h : ∀ c ∈ t, f c ∈ K) : ∏ c in t, f c ∈ K := K.to_submonoid.prod_mem h lemma pow_mem {x : G} (hx : x ∈ K) : ∀ n : ℕ, x ^ n ∈ K := K.to_submonoid.pow_mem hx lemma gpow_mem {x : G} (hx : x ∈ K) : ∀ n : ℤ, x ^ n ∈ K | (int.of_nat n) := pow_mem _ hx n | -[1+ n] := K.inv_mem $ K.pow_mem hx n.succ /-- Construct a subgroup from a nonempty set that is closed under division. -/ @[to_additive "Construct a subgroup from a nonempty set that is closed under subtraction"] def of_div (s : set G) (hsn : s.nonempty) (hs : ∀ x y ∈ s, x * y⁻¹ ∈ s) : subgroup G := have one_mem : (1 : G) ∈ s, from let ⟨x, hx⟩ := hsn in by simpa using hs x x hx hx, have inv_mem : ∀ x, x ∈ s → x⁻¹ ∈ s, from λ x hx, by simpa using hs 1 x one_mem hx, { carrier := s, one_mem' := one_mem, inv_mem' := inv_mem, mul_mem' := λ x y hx hy, by simpa using hs x y⁻¹ hx (inv_mem y hy) } /-- A subgroup of a group inherits a multiplication. -/ @[to_additive "An `add_subgroup` of an `add_group` inherits an addition."] instance has_mul : has_mul H := H.to_submonoid.has_mul /-- A subgroup of a group inherits a 1. -/ @[to_additive "An `add_subgroup` of an `add_group` inherits a zero."] instance has_one : has_one H := H.to_submonoid.has_one /-- A subgroup of a group inherits an inverse. -/ @[to_additive "A `add_subgroup` of a `add_group` inherits an inverse."] instance has_inv : has_inv H := ⟨λ a, ⟨a⁻¹, H.inv_mem a.2⟩⟩ /-- A subgroup of a group inherits a division -/ @[to_additive "An `add_subgroup` of an `add_group` inherits a subtraction."] instance has_div : has_div H := ⟨λ a b, ⟨a / b, H.div_mem a.2 b.2⟩⟩ @[simp, norm_cast, to_additive] lemma coe_mul (x y : H) : (↑(x * y) : G) = ↑x * ↑y := rfl @[simp, norm_cast, to_additive] lemma coe_one : ((1 : H) : G) = 1 := rfl @[simp, norm_cast, to_additive] lemma coe_inv (x : H) : ↑(x⁻¹ : H) = (x⁻¹ : G) := rfl @[simp, norm_cast, to_additive] lemma coe_mk (x : G) (hx : x ∈ H) : ((⟨x, hx⟩ : H) : G) = x := rfl attribute [norm_cast] add_subgroup.coe_add add_subgroup.coe_zero add_subgroup.coe_neg add_subgroup.coe_mk /-- A subgroup of a group inherits a group structure. -/ @[to_additive "An `add_subgroup` of an `add_group` inherits an `add_group` structure."] instance to_group {G : Type*} [group G] (H : subgroup G) : group H := subtype.coe_injective.group_div _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) /-- A subgroup of a `comm_group` is a `comm_group`. -/ @[to_additive "An `add_subgroup` of an `add_comm_group` is an `add_comm_group`."] instance to_comm_group {G : Type*} [comm_group G] (H : subgroup G) : comm_group H := subtype.coe_injective.comm_group_div _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) /-- A subgroup of an `ordered_comm_group` is an `ordered_comm_group`. -/ @[to_additive "An `add_subgroup` of an `add_ordered_comm_group` is an `add_ordered_comm_group`."] instance to_ordered_comm_group {G : Type*} [ordered_comm_group G] (H : subgroup G) : ordered_comm_group H := subtype.coe_injective.ordered_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) /-- A subgroup of a `linear_ordered_comm_group` is a `linear_ordered_comm_group`. -/ @[to_additive "An `add_subgroup` of a `linear_ordered_add_comm_group` is a `linear_ordered_add_comm_group`."] instance to_linear_ordered_comm_group {G : Type*} [linear_ordered_comm_group G] (H : subgroup G) : linear_ordered_comm_group H := subtype.coe_injective.linear_ordered_comm_group _ rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) /-- The natural group hom from a subgroup of group `G` to `G`. -/ @[to_additive "The natural group hom from an `add_subgroup` of `add_group` `G` to `G`."] def subtype : H →* G := ⟨coe, rfl, λ _ _, rfl⟩ @[simp, to_additive] theorem coe_subtype : ⇑H.subtype = coe := rfl @[simp, norm_cast] lemma coe_pow (x : H) (n : ℕ) : ((x ^ n : H) : G) = x ^ n := coe_subtype H ▸ monoid_hom.map_pow _ _ _ @[simp, norm_cast] lemma coe_gpow (x : H) (n : ℤ) : ((x ^ n : H) : G) = x ^ n := coe_subtype H ▸ monoid_hom.map_gpow _ _ _ @[to_additive] instance : has_le (subgroup G) := ⟨λ H K, ∀ ⦃x⦄, x ∈ H → x ∈ K⟩ @[to_additive] lemma le_def {H K : subgroup G} : H ≤ K ↔ ∀ ⦃x : G⦄, x ∈ H → x ∈ K := iff.rfl @[simp, to_additive] lemma coe_subset_coe {H K : subgroup G} : (H : set G) ⊆ K ↔ H ≤ K := iff.rfl /-- The inclusion homomorphism from a subgroup `H` contained in `K` to `K`. -/ @[to_additive "The inclusion homomorphism from a additive subgroup `H` contained in `K` to `K`."] def inclusion {H K : subgroup G} (h : H ≤ K) : H →* K := monoid_hom.mk' (λ x, ⟨x, h x.prop⟩) (λ ⟨a, ha⟩ ⟨b, hb⟩, rfl) @[simp, to_additive] lemma coe_inclusion {H K : subgroup G} {h : H ≤ K} (a : H) : (inclusion h a : G) = a := by { cases a, simp only [inclusion, coe_mk, monoid_hom.coe_mk'] } @[simp, to_additive] lemma subtype_comp_inclusion {H K : subgroup G} (hH : H ≤ K) : K.subtype.comp (inclusion hH) = H.subtype := by { ext, simp } @[to_additive] instance : partial_order (subgroup G) := { le := (≤), .. partial_order.lift (coe : subgroup G → set G) (λ a b, ext') } /-- The subgroup `G` of the group `G`. -/ @[to_additive "The `add_subgroup G` of the `add_group G`."] instance : has_top (subgroup G) := ⟨{ inv_mem' := λ _ _, set.mem_univ _ , .. (⊤ : submonoid G) }⟩ /-- The trivial subgroup `{1}` of an group `G`. -/ @[to_additive "The trivial `add_subgroup` `{0}` of an `add_group` `G`."] instance : has_bot (subgroup G) := ⟨{ inv_mem' := λ _, by simp *, .. (⊥ : submonoid G) }⟩ @[to_additive] instance : inhabited (subgroup G) := ⟨⊥⟩ @[simp, to_additive] lemma mem_bot {x : G} : x ∈ (⊥ : subgroup G) ↔ x = 1 := iff.rfl @[simp, to_additive] lemma mem_top (x : G) : x ∈ (⊤ : subgroup G) := set.mem_univ x @[simp, to_additive] lemma coe_top : ((⊤ : subgroup G) : set G) = set.univ := rfl @[simp, to_additive] lemma coe_bot : ((⊥ : subgroup G) : set G) = {1} := rfl @[to_additive] lemma eq_bot_iff_forall : H = ⊥ ↔ ∀ x ∈ H, x = (1 : G) := begin split, { intros h x x_in, rwa [h, mem_bot] at x_in }, { intros h, ext x, rw mem_bot, exact ⟨h x, by { rintros rfl, exact H.one_mem }⟩ }, end @[to_additive] lemma eq_top_of_card_eq [fintype H] [fintype G] (h : fintype.card H = fintype.card G) : H = ⊤ := begin classical, rw fintype.card_congr (equiv.refl _) at h, -- this swaps the fintype instance to classical change fintype.card H.carrier = _ at h, unfreezingI { cases H with S hS1 hS2 hS3, }, have : S = set.univ, { suffices : S.to_finset = finset.univ, { rwa [←set.to_finset_univ, set.to_finset_inj] at this, }, apply finset.eq_univ_of_card, rwa set.to_finset_card }, change (⟨_, _, _, _⟩ : subgroup G) = ⟨_, _, _, _⟩, congr', end @[to_additive] lemma nontrivial_iff_exists_ne_one (H : subgroup G) : nontrivial H ↔ ∃ x ∈ H, x ≠ (1:G) := begin split, { introI h, rcases exists_ne (1 : H) with ⟨⟨h, h_in⟩, h_ne⟩, use [h, h_in], intro hyp, apply h_ne, simpa [hyp] }, { rintros ⟨x, x_in, hx⟩, apply nontrivial_of_ne (⟨x, x_in⟩ : H) 1, intro hyp, apply hx, simpa [has_one.one] using hyp }, end /-- A subgroup is either the trivial subgroup or nontrivial. -/ @[to_additive] lemma bot_or_nontrivial (H : subgroup G) : H = ⊥ ∨ nontrivial H := begin classical, by_cases h : ∀ x ∈ H, x = (1 : G), { left, exact H.eq_bot_iff_forall.mpr h }, { right, push_neg at h, simpa [nontrivial_iff_exists_ne_one] using h }, end /-- A subgroup is either the trivial subgroup or contains a nonzero element. -/ @[to_additive] lemma bot_or_exists_ne_one (H : subgroup G) : H = ⊥ ∨ ∃ x ∈ H, x ≠ (1:G) := begin convert H.bot_or_nontrivial, rw nontrivial_iff_exists_ne_one end /-- The inf of two subgroups is their intersection. -/ @[to_additive "The inf of two `add_subgroups`s is their intersection."] instance : has_inf (subgroup G) := ⟨λ H₁ H₂, { inv_mem' := λ _ ⟨hx, hx'⟩, ⟨H₁.inv_mem hx, H₂.inv_mem hx'⟩, .. H₁.to_submonoid ⊓ H₂.to_submonoid }⟩ @[simp, to_additive] lemma coe_inf (p p' : subgroup G) : ((p ⊓ p' : subgroup G) : set G) = p ∩ p' := rfl @[simp, to_additive] lemma mem_inf {p p' : subgroup G} {x : G} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl @[to_additive] instance : has_Inf (subgroup G) := ⟨λ s, { inv_mem' := λ x hx, set.mem_bInter $ λ i h, i.inv_mem (by apply set.mem_bInter_iff.1 hx i h), .. (⨅ S ∈ s, subgroup.to_submonoid S).copy (⋂ S ∈ s, ↑S) (by simp) }⟩ @[simp, to_additive] lemma coe_Inf (H : set (subgroup G)) : ((Inf H : subgroup G) : set G) = ⋂ s ∈ H, ↑s := rfl attribute [norm_cast] coe_Inf add_subgroup.coe_Inf @[simp, to_additive] lemma mem_Inf {S : set (subgroup G)} {x : G} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_bInter_iff @[to_additive] lemma mem_infi {ι : Sort*} {S : ι → subgroup G} {x : G} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by simp only [infi, mem_Inf, set.forall_range_iff] @[simp, to_additive] lemma coe_infi {ι : Sort*} {S : ι → subgroup G} : (↑(⨅ i, S i) : set G) = ⋂ i, S i := by simp only [infi, coe_Inf, set.bInter_range] attribute [norm_cast] coe_infi add_subgroup.coe_infi /-- Subgroups of a group form a complete lattice. -/ @[to_additive "The `add_subgroup`s of an `add_group` form a complete lattice."] instance : complete_lattice (subgroup G) := { bot := (⊥), bot_le := λ S x hx, (mem_bot.1 hx).symm ▸ S.one_mem, top := (⊤), le_top := λ S x hx, mem_top x, inf := (⊓), le_inf := λ a b c ha hb x hx, ⟨ha hx, hb hx⟩, inf_le_left := λ a b x, and.left, inf_le_right := λ a b x, and.right, .. complete_lattice_of_Inf (subgroup G) $ λ s, is_glb.of_image (λ H K, show (H : set G) ≤ K ↔ H ≤ K, from coe_subset_coe) is_glb_binfi } @[to_additive] lemma mem_sup_left {S T : subgroup G} : ∀ {x : G}, x ∈ S → x ∈ S ⊔ T := show S ≤ S ⊔ T, from le_sup_left @[to_additive] lemma mem_sup_right {S T : subgroup G} : ∀ {x : G}, x ∈ T → x ∈ S ⊔ T := show T ≤ S ⊔ T, from le_sup_right @[to_additive] lemma mem_supr_of_mem {ι : Type*} {S : ι → subgroup G} (i : ι) : ∀ {x : G}, x ∈ S i → x ∈ supr S := show S i ≤ supr S, from le_supr _ _ @[to_additive] lemma mem_Sup_of_mem {S : set (subgroup G)} {s : subgroup G} (hs : s ∈ S) : ∀ {x : G}, x ∈ s → x ∈ Sup S := show s ≤ Sup S, from le_Sup hs @[to_additive] lemma subsingleton_iff : subsingleton G ↔ subsingleton (subgroup G) := ⟨ λ h, by exactI ⟨λ x y, subgroup.ext $ λ i, subsingleton.elim 1 i ▸ by simp [subgroup.one_mem]⟩, λ h, by exactI ⟨λ x y, have ∀ i : G, i = 1 := λ i, mem_bot.mp $ subsingleton.elim (⊤ : subgroup G) ⊥ ▸ mem_top i, (this x).trans (this y).symm⟩⟩ @[to_additive] lemma nontrivial_iff : nontrivial G ↔ nontrivial (subgroup G) := not_iff_not.mp ( (not_nontrivial_iff_subsingleton.trans subsingleton_iff).trans not_nontrivial_iff_subsingleton.symm) @[to_additive] instance [subsingleton G] : unique (subgroup G) := ⟨⟨⊥⟩, λ a, @subsingleton.elim _ (subsingleton_iff.mp ‹_›) a _⟩ @[to_additive] instance [nontrivial G] : nontrivial (subgroup G) := nontrivial_iff.mp ‹_› /-- The `subgroup` generated by a set. -/ @[to_additive "The `add_subgroup` generated by a set"] def closure (k : set G) : subgroup G := Inf {K | k ⊆ K} variable {k : set G} @[to_additive] lemma mem_closure {x : G} : x ∈ closure k ↔ ∀ K : subgroup G, k ⊆ K → x ∈ K := mem_Inf /-- The subgroup generated by a set includes the set. -/ @[simp, to_additive "The `add_subgroup` generated by a set includes the set."] lemma subset_closure : k ⊆ closure k := λ x hx, mem_closure.2 $ λ K hK, hK hx open set /-- A subgroup `K` includes `closure k` if and only if it includes `k`. -/ @[simp, to_additive "An additive subgroup `K` includes `closure k` if and only if it includes `k`"] lemma closure_le : closure k ≤ K ↔ k ⊆ K := ⟨subset.trans subset_closure, λ h, Inf_le h⟩ @[to_additive] lemma closure_eq_of_le (h₁ : k ⊆ K) (h₂ : K ≤ closure k) : closure k = K := le_antisymm ((closure_le $ K).2 h₁) h₂ /-- An induction principle for closure membership. If `p` holds for `1` and all elements of `k`, and is preserved under multiplication and inverse, then `p` holds for all elements of the closure of `k`. -/ @[to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all elements of `k`, and is preserved under addition and isvers, then `p` holds for all elements of the additive closure of `k`."] lemma closure_induction {p : G → Prop} {x} (h : x ∈ closure k) (Hk : ∀ x ∈ k, p x) (H1 : p 1) (Hmul : ∀ x y, p x → p y → p (x * y)) (Hinv : ∀ x, p x → p x⁻¹) : p x := (@closure_le _ _ ⟨p, H1, Hmul, Hinv⟩ _).2 Hk h attribute [elab_as_eliminator] subgroup.closure_induction add_subgroup.closure_induction /-- An induction principle on elements of the subtype `subgroup.closure`. If `p` holds for `1` and all elements of `k`, and is preserved under multiplication and inverse, then `p` holds for all elements `x : closure k`. The difference with `subgroup.closure_induction` is that this acts on the subtype. -/ @[to_additive "An induction principle on elements of the subtype `add_subgroup.closure`. If `p` holds for `0` and all elements of `k`, and is preserved under addition and negation, then `p` holds for all elements `x : closure k`. The difference with `add_subgroup.closure_induction` is that this acts on the subtype."] lemma closure_induction' (k : set G) {p : closure k → Prop} (Hk : ∀ x (h : x ∈ k), p ⟨x, subset_closure h⟩) (H1 : p 1) (Hmul : ∀ x y, p x → p y → p (x * y)) (Hinv : ∀ x, p x → p x⁻¹) (x : closure k) : p x := subtype.rec_on x $ λ x hx, begin refine exists.elim _ (λ (hx : x ∈ closure k) (hc : p ⟨x, hx⟩), hc), exact closure_induction hx (λ x hx, ⟨subset_closure hx, Hk x hx⟩) ⟨one_mem _, H1⟩ (λ x y hx hy, exists.elim hx $ λ hx' hx, exists.elim hy $ λ hy' hy, ⟨mul_mem _ hx' hy', Hmul _ _ hx hy⟩) (λ x hx, exists.elim hx $ λ hx' hx, ⟨inv_mem _ hx', Hinv _ hx⟩), end attribute [elab_as_eliminator] subgroup.closure_induction' add_subgroup.closure_induction' variable (G) /-- `closure` forms a Galois insertion with the coercion to set. -/ @[to_additive "`closure` forms a Galois insertion with the coercion to set."] protected def gi : galois_insertion (@closure G _) coe := { choice := λ s _, closure s, gc := λ s t, @closure_le _ _ t s, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variable {G} /-- Subgroup closure of a set is monotone in its argument: if `h ⊆ k`, then `closure h ≤ closure k`. -/ @[to_additive "Additive subgroup closure of a set is monotone in its argument: if `h ⊆ k`, then `closure h ≤ closure k`"] lemma closure_mono ⦃h k : set G⦄ (h' : h ⊆ k) : closure h ≤ closure k := (subgroup.gi G).gc.monotone_l h' /-- Closure of a subgroup `K` equals `K`. -/ @[simp, to_additive "Additive closure of an additive subgroup `K` equals `K`"] lemma closure_eq : closure (K : set G) = K := (subgroup.gi G).l_u_eq K @[simp, to_additive] lemma closure_empty : closure (∅ : set G) = ⊥ := (subgroup.gi G).gc.l_bot @[simp, to_additive] lemma closure_univ : closure (univ : set G) = ⊤ := @coe_top G _ ▸ closure_eq ⊤ @[to_additive] lemma closure_union (s t : set G) : closure (s ∪ t) = closure s ⊔ closure t := (subgroup.gi G).gc.l_sup @[to_additive] lemma closure_Union {ι} (s : ι → set G) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (subgroup.gi G).gc.l_supr @[to_additive] lemma closure_eq_bot_iff (G : Type*) [group G] (S : set G) : closure S = ⊥ ↔ S ⊆ {1} := by { rw [← le_bot_iff], exact closure_le _} /-- The subgroup generated by an element of a group equals the set of integer number powers of the element. -/ lemma mem_closure_singleton {x y : G} : y ∈ closure ({x} : set G) ↔ ∃ n : ℤ, x ^ n = y := begin refine ⟨λ hy, closure_induction hy _ _ _ _, λ ⟨n, hn⟩, hn ▸ gpow_mem _ (subset_closure $ mem_singleton x) n⟩, { intros y hy, rw [eq_of_mem_singleton hy], exact ⟨1, gpow_one x⟩ }, { exact ⟨0, rfl⟩ }, { rintros _ _ ⟨n, rfl⟩ ⟨m, rfl⟩, exact ⟨n + m, gpow_add x n m⟩ }, rintros _ ⟨n, rfl⟩, exact ⟨-n, gpow_neg x n⟩ end lemma closure_singleton_one : closure ({1} : set G) = ⊥ := by simp [eq_bot_iff_forall, mem_closure_singleton] @[to_additive] lemma mem_supr_of_directed {ι} [hι : nonempty ι] {K : ι → subgroup G} (hK : directed (≤) K) {x : G} : x ∈ (supr K : subgroup G) ↔ ∃ i, x ∈ K i := begin refine ⟨_, λ ⟨i, hi⟩, (le_def.1 $ le_supr K i) hi⟩, suffices : x ∈ closure (⋃ i, (K i : set G)) → ∃ i, x ∈ K i, by simpa only [closure_Union, closure_eq (K _)] using this, refine (λ hx, closure_induction hx (λ _, mem_Union.1) _ _ _), { exact hι.elim (λ i, ⟨i, (K i).one_mem⟩) }, { rintros x y ⟨i, hi⟩ ⟨j, hj⟩, rcases hK i j with ⟨k, hki, hkj⟩, exact ⟨k, (K k).mul_mem (hki hi) (hkj hj)⟩ }, rintros _ ⟨i, hi⟩, exact ⟨i, inv_mem (K i) hi⟩ end @[to_additive] lemma coe_supr_of_directed {ι} [nonempty ι] {S : ι → subgroup G} (hS : directed (≤) S) : ((⨆ i, S i : subgroup G) : set G) = ⋃ i, ↑(S i) := set.ext $ λ x, by simp [mem_supr_of_directed hS] @[to_additive] lemma mem_Sup_of_directed_on {K : set (subgroup G)} (Kne : K.nonempty) (hK : directed_on (≤) K) {x : G} : x ∈ Sup K ↔ ∃ s ∈ K, x ∈ s := begin haveI : nonempty K := Kne.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed hK.directed_coe, set_coe.exists, subtype.coe_mk] end variables {N : Type*} [group N] {P : Type*} [group P] /-- The preimage of a subgroup along a monoid homomorphism is a subgroup. -/ @[to_additive "The preimage of an `add_subgroup` along an `add_monoid` homomorphism is an `add_subgroup`."] def comap {N : Type*} [group N] (f : G →* N) (H : subgroup N) : subgroup G := { carrier := (f ⁻¹' H), inv_mem' := λ a ha, show f a⁻¹ ∈ H, by rw f.map_inv; exact H.inv_mem ha, .. H.to_submonoid.comap f } @[simp, to_additive] lemma coe_comap (K : subgroup N) (f : G →* N) : (K.comap f : set G) = f ⁻¹' K := rfl @[simp, to_additive] lemma mem_comap {K : subgroup N} {f : G →* N} {x : G} : x ∈ K.comap f ↔ f x ∈ K := iff.rfl @[to_additive] lemma comap_comap (K : subgroup P) (g : N →* P) (f : G →* N) : (K.comap g).comap f = K.comap (g.comp f) := rfl /-- The image of a subgroup along a monoid homomorphism is a subgroup. -/ @[to_additive "The image of an `add_subgroup` along an `add_monoid` homomorphism is an `add_subgroup`."] def map (f : G →* N) (H : subgroup G) : subgroup N := { carrier := (f '' H), inv_mem' := by { rintros _ ⟨x, hx, rfl⟩, exact ⟨x⁻¹, H.inv_mem hx, f.map_inv x⟩ }, .. H.to_submonoid.map f } @[simp, to_additive] lemma coe_map (f : G →* N) (K : subgroup G) : (K.map f : set N) = f '' K := rfl @[simp, to_additive] lemma mem_map {f : G →* N} {K : subgroup G} {y : N} : y ∈ K.map f ↔ ∃ x ∈ K, f x = y := mem_image_iff_bex @[to_additive] lemma map_map (g : N →* P) (f : G →* N) : (K.map f).map g = K.map (g.comp f) := ext' $ image_image _ _ _ @[to_additive] lemma map_le_iff_le_comap {f : G →* N} {K : subgroup G} {H : subgroup N} : K.map f ≤ H ↔ K ≤ H.comap f := image_subset_iff @[to_additive] lemma gc_map_comap (f : G →* N) : galois_connection (map f) (comap f) := λ _ _, map_le_iff_le_comap @[to_additive] lemma map_sup (H K : subgroup G) (f : G →* N) : (H ⊔ K).map f = H.map f ⊔ K.map f := (gc_map_comap f).l_sup @[to_additive] lemma map_supr {ι : Sort*} (f : G →* N) (s : ι → subgroup G) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr @[to_additive] lemma comap_inf (H K : subgroup N) (f : G →* N) : (H ⊓ K).comap f = H.comap f ⊓ K.comap f := (gc_map_comap f).u_inf @[to_additive] lemma comap_infi {ι : Sort*} (f : G →* N) (s : ι → subgroup N) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[simp, to_additive] lemma map_bot (f : G →* N) : (⊥ : subgroup G).map f = ⊥ := (gc_map_comap f).l_bot @[simp, to_additive] lemma comap_top (f : G →* N) : (⊤ : subgroup N).comap f = ⊤ := (gc_map_comap f).u_top @[to_additive] lemma map_eq_bot_iff {G' : Type*} [group G'] {f : G →* G'} (hf : function.injective f) (H : subgroup G) : H.map f = ⊥ ↔ H = ⊥ := begin split, { rw [eq_bot_iff_forall, eq_bot_iff_forall], intros h x hx, have hfx : f x = 1 := h (f x) ⟨x, hx, rfl⟩, exact hf (show f x = f 1, by simp only [hfx, monoid_hom.map_one]), }, { intros h, rw [h, map_bot], }, end @[simp, to_additive] lemma comap_subtype_inf_left {H K : subgroup G} : comap H.subtype (H ⊓ K) = comap H.subtype K := ext $ λ x, and_iff_right_of_imp (λ _, x.prop) @[simp, to_additive] lemma comap_subtype_inf_right {H K : subgroup G} : comap K.subtype (H ⊓ K) = comap K.subtype H := ext $ λ x, and_iff_left_of_imp (λ _, x.prop) /-- Given `subgroup`s `H`, `K` of groups `G`, `N` respectively, `H × K` as a subgroup of `G × N`. -/ @[to_additive prod "Given `add_subgroup`s `H`, `K` of `add_group`s `A`, `B` respectively, `H × K` as an `add_subgroup` of `A × B`."] def prod (H : subgroup G) (K : subgroup N) : subgroup (G × N) := { inv_mem' := λ _ hx, ⟨H.inv_mem' hx.1, K.inv_mem' hx.2⟩, .. submonoid.prod H.to_submonoid K.to_submonoid} @[to_additive coe_prod] lemma coe_prod (H : subgroup G) (K : subgroup N) : (H.prod K : set (G × N)) = (H : set G).prod (K : set N) := rfl @[to_additive mem_prod] lemma mem_prod {H : subgroup G} {K : subgroup N} {p : G × N} : p ∈ H.prod K ↔ p.1 ∈ H ∧ p.2 ∈ K := iff.rfl @[to_additive prod_mono] lemma prod_mono : ((≤) ⇒ (≤) ⇒ (≤)) (@prod G _ N _) (@prod G _ N _) := λ s s' hs t t' ht, set.prod_mono hs ht @[to_additive prod_mono_right] lemma prod_mono_right (K : subgroup G) : monotone (λ t : subgroup N, K.prod t) := prod_mono (le_refl K) @[to_additive prod_mono_left] lemma prod_mono_left (H : subgroup N) : monotone (λ K : subgroup G, K.prod H) := λ s₁ s₂ hs, prod_mono hs (le_refl H) @[to_additive prod_top] lemma prod_top (K : subgroup G) : K.prod (⊤ : subgroup N) = K.comap (monoid_hom.fst G N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst] @[to_additive top_prod] lemma top_prod (H : subgroup N) : (⊤ : subgroup G).prod H = H.comap (monoid_hom.snd G N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd] @[simp, to_additive top_prod_top] lemma top_prod_top : (⊤ : subgroup G).prod (⊤ : subgroup N) = ⊤ := (top_prod _).trans $ comap_top _ @[to_additive] lemma bot_prod_bot : (⊥ : subgroup G).prod (⊥ : subgroup N) = ⊥ := ext' $ by simp [coe_prod, prod.one_eq_mk] /-- Product of subgroups is isomorphic to their product as groups. -/ @[to_additive prod_equiv "Product of additive subgroups is isomorphic to their product as additive groups"] def prod_equiv (H : subgroup G) (K : subgroup N) : H.prod K ≃* H × K := { map_mul' := λ x y, rfl, .. equiv.set.prod ↑H ↑K } /-- A subgroup is normal if whenever `n ∈ H`, then `g * n * g⁻¹ ∈ H` for every `g : G` -/ structure normal : Prop := (conj_mem : ∀ n, n ∈ H → ∀ g : G, g * n * g⁻¹ ∈ H) attribute [class] normal end subgroup namespace add_subgroup /-- An add_subgroup is normal if whenever `n ∈ H`, then `g + n - g ∈ H` for every `g : G` -/ structure normal (H : add_subgroup A) : Prop := (conj_mem [] : ∀ n, n ∈ H → ∀ g : A, g + n + -g ∈ H) attribute [to_additive add_subgroup.normal] subgroup.normal attribute [class] normal end add_subgroup namespace subgroup variables {H K : subgroup G} @[priority 100, to_additive] instance normal_of_comm {G : Type*} [comm_group G] (H : subgroup G) : H.normal := ⟨by simp [mul_comm, mul_left_comm]⟩ namespace normal variable (nH : H.normal) @[to_additive] lemma mem_comm {a b : G} (h : a * b ∈ H) : b * a ∈ H := have a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ H, from nH.conj_mem (a * b) h a⁻¹, by simpa @[to_additive] lemma mem_comm_iff {a b : G} : a * b ∈ H ↔ b * a ∈ H := ⟨nH.mem_comm, nH.mem_comm⟩ end normal @[priority 100, to_additive] instance bot_normal : normal (⊥ : subgroup G) := ⟨by simp⟩ @[priority 100, to_additive] instance top_normal : normal (⊤ : subgroup G) := ⟨λ _ _, mem_top⟩ variable (G) /-- The center of a group `G` is the set of elements that commute with everything in `G` -/ @[to_additive "The center of a group `G` is the set of elements that commute with everything in `G`"] def center : subgroup G := { carrier := {z | ∀ g, g * z = z * g}, one_mem' := by simp, mul_mem' := λ a b (ha : ∀ g, g * a = a * g) (hb : ∀ g, g * b = b * g) g, by assoc_rw [ha, hb g], inv_mem' := λ a (ha : ∀ g, g * a = a * g) g, by rw [← inv_inj, mul_inv_rev, inv_inv, ← ha, mul_inv_rev, inv_inv] } variable {G} @[to_additive] lemma mem_center_iff {z : G} : z ∈ center G ↔ ∀ g, g * z = z * g := iff.rfl @[priority 100, to_additive] instance center_normal : (center G).normal := ⟨begin assume n hn g h, assoc_rw [hn (h * g), hn g], simp end⟩ variables {G} (H) /-- The `normalizer` of `H` is the smallest subgroup of `G` inside which `H` is normal. -/ @[to_additive "The `normalizer` of `H` is the smallest subgroup of `G` inside which `H` is normal."] def normalizer : subgroup G := { carrier := {g : G | ∀ n, n ∈ H ↔ g * n * g⁻¹ ∈ H}, one_mem' := by simp, mul_mem' := λ a b (ha : ∀ n, n ∈ H ↔ a * n * a⁻¹ ∈ H) (hb : ∀ n, n ∈ H ↔ b * n * b⁻¹ ∈ H) n, by { rw [hb, ha], simp [mul_assoc] }, inv_mem' := λ a (ha : ∀ n, n ∈ H ↔ a * n * a⁻¹ ∈ H) n, by { rw [ha (a⁻¹ * n * a⁻¹⁻¹)], simp [mul_assoc] } } -- variant for sets. -- TODO should this replace `normalizer`? /-- The `set_normalizer` of `S` is the subgroup of `G` whose elements satisfy `g*S*g⁻¹=S` -/ @[to_additive "The `set_normalizer` of `S` is the subgroup of `G` whose elements satisfy `g+S-g=S`."] def set_normalizer (S : set G) : subgroup G := { carrier := {g : G | ∀ n, n ∈ S ↔ g * n * g⁻¹ ∈ S}, one_mem' := by simp, mul_mem' := λ a b (ha : ∀ n, n ∈ S ↔ a * n * a⁻¹ ∈ S) (hb : ∀ n, n ∈ S ↔ b * n * b⁻¹ ∈ S) n, by { rw [hb, ha], simp [mul_assoc] }, inv_mem' := λ a (ha : ∀ n, n ∈ S ↔ a * n * a⁻¹ ∈ S) n, by { rw [ha (a⁻¹ * n * a⁻¹⁻¹)], simp [mul_assoc] } } variable {H} @[to_additive] lemma mem_normalizer_iff {g : G} : g ∈ normalizer H ↔ ∀ n, n ∈ H ↔ g * n * g⁻¹ ∈ H := iff.rfl @[to_additive] lemma le_normalizer : H ≤ normalizer H := λ x xH n, by rw [H.mul_mem_cancel_right (H.inv_mem xH), H.mul_mem_cancel_left xH] @[priority 100, to_additive] instance normal_in_normalizer : (H.comap H.normalizer.subtype).normal := ⟨λ x xH g, by simpa using (g.2 x).1 xH⟩ open_locale classical @[to_additive] lemma le_normalizer_of_normal [hK : (H.comap K.subtype).normal] (HK : H ≤ K) : K ≤ H.normalizer := λ x hx y, ⟨λ yH, hK.conj_mem ⟨y, HK yH⟩ yH ⟨x, hx⟩, λ yH, by simpa [mem_comap, mul_assoc] using hK.conj_mem ⟨x * y * x⁻¹, HK yH⟩ yH ⟨x⁻¹, K.inv_mem hx⟩⟩ end subgroup namespace group variables {s : set G} /-- Given an element `a`, `conjugates a` is the set of conjugates. -/ def conjugates (a : G) : set G := {b | is_conj a b} lemma mem_conjugates_self {a : G} : a ∈ conjugates a := is_conj_refl _ /-- Given a set `s`, `conjugates_of_set s` is the set of all conjugates of the elements of `s`. -/ def conjugates_of_set (s : set G) : set G := ⋃ a ∈ s, conjugates a lemma mem_conjugates_of_set_iff {x : G} : x ∈ conjugates_of_set s ↔ ∃ a ∈ s, is_conj a x := set.mem_bUnion_iff theorem subset_conjugates_of_set : s ⊆ conjugates_of_set s := λ (x : G) (h : x ∈ s), mem_conjugates_of_set_iff.2 ⟨x, h, is_conj_refl _⟩ theorem conjugates_of_set_mono {s t : set G} (h : s ⊆ t) : conjugates_of_set s ⊆ conjugates_of_set t := set.bUnion_subset_bUnion_left h lemma conjugates_subset_normal {N : subgroup G} [tn : N.normal] {a : G} (h : a ∈ N) : conjugates a ⊆ N := by { rintros a ⟨c, rfl⟩, exact tn.conj_mem a h c } theorem conjugates_of_set_subset {s : set G} {N : subgroup G} [N.normal] (h : s ⊆ N) : conjugates_of_set s ⊆ N := set.bUnion_subset (λ x H, conjugates_subset_normal (h H)) /-- The set of conjugates of `s` is closed under conjugation. -/ lemma conj_mem_conjugates_of_set {x c : G} : x ∈ conjugates_of_set s → (c * x * c⁻¹ ∈ conjugates_of_set s) := λ H, begin rcases (mem_conjugates_of_set_iff.1 H) with ⟨a,h₁,h₂⟩, exact mem_conjugates_of_set_iff.2 ⟨a, h₁, is_conj_trans h₂ ⟨c,rfl⟩⟩, end end group namespace subgroup open group variable {s : set G} /-- The normal closure of a set `s` is the subgroup closure of all the conjugates of elements of `s`. It is the smallest normal subgroup containing `s`. -/ def normal_closure (s : set G) : subgroup G := closure (conjugates_of_set s) theorem conjugates_of_set_subset_normal_closure : conjugates_of_set s ⊆ normal_closure s := subset_closure theorem subset_normal_closure : s ⊆ normal_closure s := set.subset.trans subset_conjugates_of_set conjugates_of_set_subset_normal_closure theorem le_normal_closure {H : subgroup G} : H ≤ normal_closure ↑H := λ _ h, subset_normal_closure h /-- The normal closure of `s` is a normal subgroup. -/ instance normal_closure_normal : (normal_closure s).normal := ⟨λ n h g, begin refine subgroup.closure_induction h (λ x hx, _) _ (λ x y ihx ihy, _) (λ x ihx, _), { exact (conjugates_of_set_subset_normal_closure (conj_mem_conjugates_of_set hx)) }, { simpa using (normal_closure s).one_mem }, { rw ← conj_mul, exact mul_mem _ ihx ihy }, { rw ← conj_inv, exact inv_mem _ ihx } end⟩ /-- The normal closure of `s` is the smallest normal subgroup containing `s`. -/ theorem normal_closure_le_normal {N : subgroup G} [N.normal] (h : s ⊆ N) : normal_closure s ≤ N := begin assume a w, refine closure_induction w (λ x hx, _) _ (λ x y ihx ihy, _) (λ x ihx, _), { exact (conjugates_of_set_subset h hx) }, { exact subgroup.one_mem _ }, { exact subgroup.mul_mem _ ihx ihy }, { exact subgroup.inv_mem _ ihx } end lemma normal_closure_subset_iff {N : subgroup G} [N.normal] : s ⊆ N ↔ normal_closure s ≤ N := ⟨normal_closure_le_normal, set.subset.trans (subset_normal_closure)⟩ theorem normal_closure_mono {s t : set G} (h : s ⊆ t) : normal_closure s ≤ normal_closure t := normal_closure_le_normal (set.subset.trans h subset_normal_closure) theorem normal_closure_eq_infi : normal_closure s = ⨅ (N : subgroup G) [normal N] (hs : s ⊆ N), N := le_antisymm (le_infi (λ N, le_infi (λ hN, by exactI le_infi (normal_closure_le_normal)))) (infi_le_of_le (normal_closure s) (infi_le_of_le (by apply_instance) (infi_le_of_le subset_normal_closure (le_refl _)))) @[simp] theorem normal_closure_eq_self (H : subgroup G) [H.normal] : normal_closure ↑H = H := le_antisymm (normal_closure_le_normal rfl.subset) (le_normal_closure) @[simp] theorem normal_closure_idempotent : normal_closure ↑(normal_closure s) = normal_closure s := normal_closure_eq_self _ theorem closure_le_normal_closure {s : set G} : closure s ≤ normal_closure s := by simp only [subset_normal_closure, closure_le] @[simp] theorem normal_closure_closure_eq_normal_closure {s : set G} : normal_closure ↑(closure s) = normal_closure s := le_antisymm (normal_closure_le_normal closure_le_normal_closure) (normal_closure_mono subset_closure) end subgroup namespace add_subgroup open set lemma gsmul_mem (H : add_subgroup A) {x : A} (hx : x ∈ H) : ∀ n : ℤ, gsmul n x ∈ H | (int.of_nat n) := add_submonoid.nsmul_mem H.to_add_submonoid hx n | -[1+ n] := H.neg_mem' $ H.add_mem hx $ add_submonoid.nsmul_mem H.to_add_submonoid hx n /-- The `add_subgroup` generated by an element of an `add_group` equals the set of natural number multiples of the element. -/ lemma mem_closure_singleton {x y : A} : y ∈ closure ({x} : set A) ↔ ∃ n : ℤ, gsmul n x = y := begin refine ⟨λ hy, closure_induction hy _ _ _ _, λ ⟨n, hn⟩, hn ▸ gsmul_mem _ (subset_closure $ mem_singleton x) n⟩, { intros y hy, rw [eq_of_mem_singleton hy], exact ⟨1, one_gsmul x⟩ }, { exact ⟨0, rfl⟩ }, { rintros _ _ ⟨n, rfl⟩ ⟨m, rfl⟩, exact ⟨n + m, add_gsmul x n m⟩ }, { rintros _ ⟨n, rfl⟩, refine ⟨-n, neg_gsmul x n⟩ } end lemma closure_singleton_zero : closure ({0} : set A) = ⊥ := by simp [eq_bot_iff_forall, mem_closure_singleton] variable (H : add_subgroup A) @[simp] lemma coe_smul (x : H) (n : ℕ) : ((nsmul n x : H) : A) = nsmul n x := coe_subtype H ▸ add_monoid_hom.map_nsmul _ _ _ @[simp] lemma coe_gsmul (x : H) (n : ℤ) : ((n •ℤ x : H) : A) = n •ℤ x := coe_subtype H ▸ add_monoid_hom.map_gsmul _ _ _ attribute [to_additive add_subgroup.coe_smul] subgroup.coe_pow attribute [to_additive add_subgroup.coe_gsmul] subgroup.coe_gpow end add_subgroup namespace monoid_hom variables {N : Type*} {P : Type*} [group N] [group P] (K : subgroup G) open subgroup /-- The range of a monoid homomorphism from a group is a subgroup. -/ @[to_additive "The range of an `add_monoid_hom` from an `add_group` is an `add_subgroup`."] def range (f : G →* N) : subgroup N := subgroup.copy ((⊤ : subgroup G).map f) (set.range f) (by simp [set.ext_iff]) @[to_additive] instance decidable_mem_range (f : G →* N) [fintype G] [decidable_eq N] : decidable_pred (λ x, x ∈ f.range) := λ x, fintype.decidable_exists_fintype @[simp, to_additive] lemma coe_range (f : G →* N) : (f.range : set N) = set.range f := rfl @[simp, to_additive] lemma mem_range {f : G →* N} {y : N} : y ∈ f.range ↔ ∃ x, f x = y := iff.rfl @[to_additive] lemma range_eq_map (f : G →* N) : f.range = (⊤ : subgroup G).map f := by ext; simp /-- The canonical surjective group homomorphism `G →* f(G)` induced by a group homomorphism `G →* N`. -/ @[to_additive "The canonical surjective `add_group` homomorphism `G →+ f(G)` induced by a group homomorphism `G →+ N`."] def to_range (f : G →* N) : G →* f.range := monoid_hom.mk' (λ g, ⟨f g, ⟨g, rfl⟩⟩) $ λ a b, by {ext, exact f.map_mul' _ _} @[to_additive] lemma map_range (g : N →* P) (f : G →* N) : f.range.map g = (g.comp f).range := by rw [range_eq_map, range_eq_map]; exact (⊤ : subgroup G).map_map g f @[to_additive] lemma range_top_iff_surjective {N} [group N] {f : G →* N} : f.range = (⊤ : subgroup N) ↔ function.surjective f := subgroup.ext'_iff.trans $ iff.trans (by rw [coe_range, coe_top]) set.range_iff_surjective /-- The range of a surjective monoid homomorphism is the whole of the codomain. -/ @[to_additive "The range of a surjective `add_monoid` homomorphism is the whole of the codomain."] lemma range_top_of_surjective {N} [group N] (f : G →* N) (hf : function.surjective f) : f.range = (⊤ : subgroup N) := range_top_iff_surjective.2 hf /-- Restriction of a group hom to a subgroup of the domain. -/ @[to_additive "Restriction of an `add_group` hom to an `add_subgroup` of the domain."] def restrict (f : G →* N) (H : subgroup G) : H →* N := f.comp H.subtype @[simp, to_additive] lemma restrict_apply {H : subgroup G} (f : G →* N) (x : H) : f.restrict H x = f (x : G) := rfl /-- Restriction of a group hom to a subgroup of the codomain. -/ @[to_additive "Restriction of an `add_group` hom to an `add_subgroup` of the codomain."] def cod_restrict (f : G →* N) (S : subgroup N) (h : ∀ x, f x ∈ S) : G →* S := { to_fun := λ n, ⟨f n, h n⟩, map_one' := subtype.eq f.map_one, map_mul' := λ x y, subtype.eq (f.map_mul x y) } /-- The multiplicative kernel of a monoid homomorphism is the subgroup of elements `x : G` such that `f x = 1` -/ @[to_additive "The additive kernel of an `add_monoid` homomorphism is the `add_subgroup` of elements such that `f x = 0`"] def ker (f : G →* N) := (⊥ : subgroup N).comap f @[to_additive] lemma mem_ker (f : G →* N) {x : G} : x ∈ f.ker ↔ f x = 1 := iff.rfl @[to_additive] lemma comap_ker (g : N →* P) (f : G →* N) : g.ker.comap f = (g.comp f).ker := rfl @[to_additive] lemma to_range_ker (f : G →* N) : ker (to_range f) = ker f := begin ext, change (⟨f x, _⟩ : range f) = ⟨1, _⟩ ↔ f x = 1, simp only [], end /-- The subgroup of elements `x : G` such that `f x = g x` -/ @[to_additive "The additive subgroup of elements `x : G` such that `f x = g x`"] def eq_locus (f g : G →* N) : subgroup G := { inv_mem' := λ x (hx : f x = g x), show f x⁻¹ = g x⁻¹, by rw [f.map_inv, g.map_inv, hx], .. eq_mlocus f g} /-- If two monoid homomorphisms are equal on a set, then they are equal on its subgroup closure. -/ @[to_additive] lemma eq_on_closure {f g : G →* N} {s : set G} (h : set.eq_on f g s) : set.eq_on f g (closure s) := show closure s ≤ f.eq_locus g, from (closure_le _).2 h @[to_additive] lemma eq_of_eq_on_top {f g : G →* N} (h : set.eq_on f g (⊤ : subgroup G)) : f = g := ext $ λ x, h trivial @[to_additive] lemma eq_of_eq_on_dense {s : set G} (hs : closure s = ⊤) {f g : G →* N} (h : s.eq_on f g) : f = g := eq_of_eq_on_top $ hs ▸ eq_on_closure h @[to_additive] lemma gclosure_preimage_le (f : G →* N) (s : set N) : closure (f ⁻¹' s) ≤ (closure s).comap f := (closure_le _).2 $ λ x hx, by rw [mem_coe, mem_comap]; exact subset_closure hx /-- The image under a monoid homomorphism of the subgroup generated by a set equals the subgroup generated by the image of the set. -/ @[to_additive "The image under an `add_monoid` hom of the `add_subgroup` generated by a set equals the `add_subgroup` generated by the image of the set."] lemma map_closure (f : G →* N) (s : set G) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image f s) (gclosure_preimage_le _ _)) ((closure_le _).2 $ set.image_subset _ subset_closure) end monoid_hom namespace monoid_hom variables {G₁ G₂ G₃ : Type*} [group G₁] [group G₂] [group G₃] variables (f : G₁ →* G₂) /-- `lift_of_surjective f hf g hg` is the unique group homomorphism `φ` * such that `φ.comp f = g` (`lift_of_surjective_comp`), * where `f : G₁ →+* G₂` is surjective (`hf`), * and `g : G₂ →+* G₃` satisfies `hg : f.ker ≤ g.ker`. See `lift_of_surjective_eq` for the uniqueness lemma. ``` G₁. | \ f | \ g | \ v \⌟ G₂----> G₃ ∃!φ ``` -/ @[to_additive "`lift_of_surjective f hf g hg` is the unique additive group homomorphism `φ` * such that `φ.comp f = g` (`lift_of_surjective_comp`), * where `f : G₁ →+* G₂` is surjective (`hf`), * and `g : G₂ →+* G₃` satisfies `hg : f.ker ≤ g.ker`. See `lift_of_surjective_eq` for the uniqueness lemma. ``` G₁. | \\ f | \\ g | \\ v \\⌟ G₂----> G₃ ∃!φ ```"] noncomputable def lift_of_surjective (hf : function.surjective f) (g : G₁ →* G₃) (hg : f.ker ≤ g.ker) : G₂ →* G₃ := { to_fun := λ b, g (classical.some (hf b)), map_one' := hg (classical.some_spec (hf 1)), map_mul' := begin intros x y, rw [← g.map_mul, ← mul_inv_eq_one, ← g.map_inv, ← g.map_mul, ← g.mem_ker], apply hg, rw [f.mem_ker, f.map_mul, f.map_inv, mul_inv_eq_one, f.map_mul], simp only [classical.some_spec (hf _)], end } @[simp, to_additive] lemma lift_of_surjective_comp_apply (hf : function.surjective f) (g : G₁ →* G₃) (hg : f.ker ≤ g.ker) (x : G₁) : (f.lift_of_surjective hf g hg) (f x) = g x := begin dsimp [lift_of_surjective], rw [← mul_inv_eq_one, ← g.map_inv, ← g.map_mul, ← g.mem_ker], apply hg, rw [f.mem_ker, f.map_mul, f.map_inv, mul_inv_eq_one], simp only [classical.some_spec (hf _)], end @[simp, to_additive] lemma lift_of_surjective_comp (hf : function.surjective f) (g : G₁ →* G₃) (hg : f.ker ≤ g.ker) : (f.lift_of_surjective hf g hg).comp f = g := by { ext, simp only [comp_apply, lift_of_surjective_comp_apply] } @[to_additive] lemma eq_lift_of_surjective (hf : function.surjective f) (g : G₁ →* G₃) (hg : f.ker ≤ g.ker) (h : G₂ →* G₃) (hh : h.comp f = g) : h = (f.lift_of_surjective hf g hg) := begin ext b, rcases hf b with ⟨a, rfl⟩, simp only [← comp_apply, hh, f.lift_of_surjective_comp], end end monoid_hom variables {N : Type*} [group N] -- Here `H.normal` is an explicit argument so we can use dot notation with `comap`. @[to_additive] lemma subgroup.normal.comap {H : subgroup N} (hH : H.normal) (f : G →* N) : (H.comap f).normal := ⟨λ _, by simp [subgroup.mem_comap, hH.conj_mem] {contextual := tt}⟩ @[priority 100, to_additive] instance subgroup.normal_comap {H : subgroup N} [nH : H.normal] (f : G →* N) : (H.comap f).normal := nH.comap _ @[priority 100, to_additive] instance monoid_hom.normal_ker (f : G →* N) : f.ker.normal := by rw [monoid_hom.ker]; apply_instance @[priority 100, to_additive] instance subgroup.normal_inf (H N : subgroup G) [hN : N.normal] : ((H ⊓ N).comap H.subtype).normal := ⟨λ x hx g, begin simp only [subgroup.mem_inf, coe_subtype, subgroup.mem_comap] at hx, simp only [subgroup.coe_mul, subgroup.mem_inf, coe_subtype, subgroup.coe_inv, subgroup.mem_comap], exact ⟨H.mul_mem (H.mul_mem g.2 hx.1) (H.inv_mem g.2), hN.1 x hx.2 g⟩, end⟩ namespace subgroup /-- The subgroup generated by an element. -/ def gpowers (g : G) : subgroup G := subgroup.copy (gpowers_hom G g).range (set.range ((^) g : ℤ → G)) rfl @[simp] lemma mem_gpowers (g : G) : g ∈ gpowers g := ⟨1, gpow_one _⟩ lemma gpowers_eq_closure (g : G) : gpowers g = closure {g} := by { ext, exact mem_closure_singleton.symm } @[simp] lemma range_gpowers_hom (g : G) : (gpowers_hom G g).range = gpowers g := rfl lemma gpowers_subset {a : G} {K : subgroup G} (h : a ∈ K) : gpowers a ≤ K := λ x hx, match x, hx with _, ⟨i, rfl⟩ := K.gpow_mem h i end end subgroup namespace add_subgroup /-- The subgroup generated by an element. -/ def gmultiples (a : A) : add_subgroup A := add_subgroup.copy (gmultiples_hom A a).range (set.range ((•ℤ a) : ℤ → A)) rfl @[simp] lemma mem_gmultiples (a : A) : a ∈ gmultiples a := ⟨1, one_gsmul _⟩ lemma gmultiples_eq_closure (a : A) : gmultiples a = closure {a} := by { ext, exact mem_closure_singleton.symm } @[simp] lemma range_gmultiples_hom (a : A) : (gmultiples_hom A a).range = gmultiples a := rfl lemma gmultiples_subset {a : A} {B : add_subgroup A} (h : a ∈ B) : gmultiples a ≤ B := @subgroup.gpowers_subset (multiplicative A) _ _ (B.to_subgroup) h attribute [to_additive add_subgroup.gmultiples] subgroup.gpowers attribute [to_additive add_subgroup.mem_gmultiples] subgroup.mem_gpowers attribute [to_additive add_subgroup.gmultiples_eq_closure] subgroup.gpowers_eq_closure attribute [to_additive add_subgroup.range_gmultiples_hom] subgroup.range_gpowers_hom attribute [to_additive add_subgroup.gmultiples_subset] subgroup.gpowers_subset end add_subgroup namespace mul_equiv variables {H K : subgroup G} /-- Makes the identity isomorphism from a proof two subgroups of a multiplicative group are equal. -/ @[to_additive "Makes the identity additive isomorphism from a proof two subgroups of an additive group are equal."] def subgroup_congr (h : H = K) : H ≃* K := { map_mul' := λ _ _, rfl, ..equiv.set_congr $ subgroup.ext'_iff.1 h } end mul_equiv -- TODO : ↥(⊤ : subgroup H) ≃* H ? namespace subgroup variables {C : Type*} [comm_group C] {s t : subgroup C} {x : C} @[to_additive] lemma mem_sup : x ∈ s ⊔ t ↔ ∃ (y ∈ s) (z ∈ t), y * z = x := ⟨λ h, begin rw [← closure_eq s, ← closure_eq t, ← closure_union] at h, apply closure_induction h, { rintro y (h | h), { exact ⟨y, h, 1, t.one_mem, by simp⟩ }, { exact ⟨1, s.one_mem, y, h, by simp⟩ } }, { exact ⟨1, s.one_mem, 1, ⟨t.one_mem, mul_one 1⟩⟩ }, { rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨y₂, hy₂, z₂, hz₂, rfl⟩, exact ⟨_, mul_mem _ hy₁ hy₂, _, mul_mem _ hz₁ hz₂, by simp [mul_assoc]; cc⟩ }, { rintro _ ⟨y, hy, z, hz, rfl⟩, exact ⟨_, inv_mem _ hy, _, inv_mem _ hz, mul_comm z y ▸ (mul_inv_rev z y).symm⟩ } end, by rintro ⟨y, hy, z, hz, rfl⟩; exact mul_mem _ ((le_sup_left : s ≤ s ⊔ t) hy) ((le_sup_right : t ≤ s ⊔ t) hz)⟩ @[to_additive] lemma mem_sup' : x ∈ s ⊔ t ↔ ∃ (y : s) (z : t), (y:C) * z = x := mem_sup.trans $ by simp only [subgroup.exists, coe_mk] @[to_additive] instance : is_modular_lattice (subgroup C) := ⟨λ x y z xz a ha, begin rw [mem_inf, mem_sup] at ha, rcases ha with ⟨⟨b, hb, c, hc, rfl⟩, haz⟩, rw mem_sup, refine ⟨b, hb, c, mem_inf.2 ⟨hc, _⟩, rfl⟩, rw ← inv_mul_cancel_left b c, apply z.mul_mem (z.inv_mem (xz hb)) haz, end⟩ end subgroup section pointwise namespace subgroup @[to_additive] lemma closure_mul_le (S T : set G) : closure (S * T) ≤ closure S ⊔ closure T := Inf_le $ λ x ⟨s, t, hs, ht, hx⟩, hx ▸ (closure S ⊔ closure T).mul_mem (le_def.mp le_sup_left $ subset_closure hs) (le_def.mp le_sup_right $ subset_closure ht) @[to_additive] lemma sup_eq_closure (H K : subgroup G) : H ⊔ K = closure (H * K) := le_antisymm (sup_le (λ h hh, subset_closure ⟨h, 1, hh, K.one_mem, mul_one h⟩) (λ k hk, subset_closure ⟨1, k, H.one_mem, hk, one_mul k⟩)) (by conv_rhs { rw [← closure_eq H, ← closure_eq K] }; apply closure_mul_le) @[to_additive] private def mul_normal_aux (H N : subgroup G) [hN : N.normal] : subgroup G := { carrier := (H : set G) * N, one_mem' := ⟨1, 1, H.one_mem, N.one_mem, by rw mul_one⟩, mul_mem' := λ a b ⟨h, n, hh, hn, ha⟩ ⟨h', n', hh', hn', hb⟩, ⟨h * h', h'⁻¹ * n * h' * n', H.mul_mem hh hh', N.mul_mem (by simpa using hN.conj_mem _ hn h'⁻¹) hn', by simp [← ha, ← hb, mul_assoc]⟩, inv_mem' := λ x ⟨h, n, hh, hn, hx⟩, ⟨h⁻¹, h * n⁻¹ * h⁻¹, H.inv_mem hh, hN.conj_mem _ (N.inv_mem hn) h, by rw [mul_assoc h, inv_mul_cancel_left, ← hx, mul_inv_rev]⟩ } /-- The carrier of `H ⊔ N` is just `↑H * ↑N` (pointwise set product) when `N` is normal. -/ @[to_additive "The carrier of `H ⊔ N` is just `↑H + ↑N` (pointwise set addition) when `N` is normal."] lemma mul_normal (H N : subgroup G) [N.normal] : (↑(H ⊔ N) : set G) = H * N := set.subset.antisymm (show H ⊔ N ≤ mul_normal_aux H N, by { rw sup_eq_closure, apply Inf_le _, dsimp, refl }) ((sup_eq_closure H N).symm ▸ subset_closure) @[to_additive] private def normal_mul_aux (N H : subgroup G) [hN : N.normal] : subgroup G := { carrier := (N : set G) * H, one_mem' := ⟨1, 1, N.one_mem, H.one_mem, by rw mul_one⟩, mul_mem' := λ a b ⟨n, h, hn, hh, ha⟩ ⟨n', h', hn', hh', hb⟩, ⟨n * (h * n' * h⁻¹), h * h', N.mul_mem hn (hN.conj_mem _ hn' _), H.mul_mem hh hh', by simp [← ha, ← hb, mul_assoc]⟩, inv_mem' := λ x ⟨n, h, hn, hh, hx⟩, ⟨h⁻¹ * n⁻¹ * h, h⁻¹, by simpa using hN.conj_mem _ (N.inv_mem hn) h⁻¹, H.inv_mem hh, by rw [mul_inv_cancel_right, ← mul_inv_rev, hx]⟩ } /-- The carrier of `N ⊔ H` is just `↑N * ↑H` (pointwise set product) when `N` is normal. -/ @[to_additive "The carrier of `N ⊔ H` is just `↑N + ↑H` (pointwise set addition) when `N` is normal."] lemma normal_mul (N H : subgroup G) [N.normal] : (↑(N ⊔ H) : set G) = N * H := set.subset.antisymm (show N ⊔ H ≤ normal_mul_aux N H, by { rw sup_eq_closure, apply Inf_le _, dsimp, refl }) ((sup_eq_closure N H).symm ▸ subset_closure) end subgroup end pointwise
7ab7e0d011697ad389e95ea5a1fd26ca192855b5
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/category/Module/limits.lean
75a31d41ec65ac32771ab6898f7ff9d6c5b44035
[ "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
6,567
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 algebra.category.Module.basic import algebra.category.Group.limits import algebra.direct_limit /-! # The category of R-modules has all limits Further, these limits are preserved by the forgetful functor --- that is, the underlying types are just the limits in the category of types. -/ open category_theory open category_theory.limits universes u v noncomputable theory namespace Module variables {R : Type u} [ring R] variables {J : Type v} [small_category J] instance add_comm_group_obj (F : J ⥤ Module.{v} R) (j) : add_comm_group ((F ⋙ forget (Module R)).obj j) := by { change add_comm_group (F.obj j), apply_instance } instance module_obj (F : J ⥤ Module.{v} R) (j) : module R ((F ⋙ forget (Module R)).obj j) := by { change module R (F.obj j), apply_instance } /-- The flat sections of a functor into `Module R` form a submodule of all sections. -/ def sections_submodule (F : J ⥤ Module R) : submodule R (Π j, F.obj j) := { carrier := (F ⋙ forget (Module R)).sections, smul_mem' := λ r s sh j j' f, begin simp only [forget_map_eq_coe, functor.comp_map, pi.smul_apply, linear_map.map_smul], dsimp [functor.sections] at sh, rw sh f, end, ..(AddGroup.sections_add_subgroup (F ⋙ forget₂ (Module R) AddCommGroup.{v} ⋙ forget₂ AddCommGroup AddGroup.{v})) } -- Adding the following instance speeds up `limit_module` noticeably, -- by preventing a bad unfold of `limit_add_comm_group`. instance limit_add_comm_monoid (F : J ⥤ Module R) : add_comm_monoid (types.limit_cone (F ⋙ forget (Module.{v} R))).X := show add_comm_monoid (sections_submodule F), by apply_instance instance limit_add_comm_group (F : J ⥤ Module R) : add_comm_group (types.limit_cone (F ⋙ forget (Module.{v} R))).X := show add_comm_group (sections_submodule F), by apply_instance instance limit_module (F : J ⥤ Module R) : module R (types.limit_cone (F ⋙ forget (Module.{v} R))).X := show module R (sections_submodule F), by apply_instance /-- `limit.π (F ⋙ forget Ring) j` as a `ring_hom`. -/ def limit_π_linear_map (F : J ⥤ Module R) (j) : (types.limit_cone (F ⋙ forget (Module.{v} R))).X →ₗ[R] (F ⋙ forget (Module R)).obj j := { to_fun := (types.limit_cone (F ⋙ forget (Module R))).π.app j, map_smul' := λ x y, rfl, map_add' := λ x y, rfl } namespace has_limits -- The next two definitions are used in the construction of `has_limits (Module R)`. -- After that, the limits should be constructed using the generic limits API, -- e.g. `limit F`, `limit.cone F`, and `limit.is_limit F`. /-- Construction of a limit cone in `Module R`. (Internal use only; use the limits API.) -/ def limit_cone (F : J ⥤ Module R) : cone F := { X := Module.of R (types.limit_cone (F ⋙ forget _)).X, π := { app := limit_π_linear_map F, naturality' := λ j j' f, linear_map.coe_injective ((types.limit_cone (F ⋙ forget _)).π.naturality f) } } /-- Witness that the limit cone in `Module R` is a limit cone. (Internal use only; use the limits API.) -/ def limit_cone_is_limit (F : J ⥤ Module R) : is_limit (limit_cone F) := begin refine is_limit.of_faithful (forget (Module R)) (types.limit_cone_is_limit _) (λ s, ⟨_, _, _⟩) (λ s, rfl); tidy end end has_limits open has_limits /-- The category of R-modules has all limits. -/ @[irreducible] instance has_limits : has_limits (Module.{v} R) := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, has_limit.mk { cone := limit_cone F, is_limit := limit_cone_is_limit F } } } /-- An auxiliary declaration to speed up typechecking. -/ def forget₂_AddCommGroup_preserves_limits_aux (F : J ⥤ Module R) : is_limit ((forget₂ (Module R) AddCommGroup).map_cone (limit_cone F)) := AddCommGroup.limit_cone_is_limit (F ⋙ forget₂ (Module R) AddCommGroup) /-- The forgetful functor from R-modules to abelian groups preserves all limits. -/ instance forget₂_AddCommGroup_preserves_limits : preserves_limits (forget₂ (Module R) AddCommGroup.{v}) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, preserves_limit_of_preserves_limit_cone (limit_cone_is_limit F) (forget₂_AddCommGroup_preserves_limits_aux F) } } /-- The forgetful functor from R-modules to types preserves all limits. -/ instance forget_preserves_limits : preserves_limits (forget (Module R)) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ F, preserves_limit_of_preserves_limit_cone (limit_cone_is_limit F) (types.limit_cone_is_limit (F ⋙ forget _)) } } section direct_limit open module variables {ι : Type v} variables [dec_ι : decidable_eq ι] [directed_order ι] variables (G : ι → Type v) variables [Π i, add_comm_group (G i)] [Π i, module R (G i)] variables (f : Π i j, i ≤ j → G i →ₗ[R] G j) [module.directed_system G f] /-- The diagram (in the sense of `category_theory`) of an unbundled `direct_limit` of modules. -/ @[simps] def direct_limit_diagram : ι ⥤ Module R := { obj := λ i, Module.of R (G i), map := λ i j hij, f i j hij.le, map_id' := λ i, by { ext x, apply module.directed_system.map_self }, map_comp' := λ i j k hij hjk, begin ext x, symmetry, apply module.directed_system.map_map end } variables [decidable_eq ι] /-- The `cocone` on `direct_limit_diagram` corresponding to the unbundled `direct_limit` of modules. In `direct_limit_is_colimit` we show that it is a colimit cocone. -/ @[simps] def direct_limit_cocone : cocone (direct_limit_diagram G f) := { X := Module.of R $ direct_limit G f, ι := { app := module.direct_limit.of R ι G f, naturality' := λ i j hij, by { ext x, exact direct_limit.of_f } } } /-- The unbundled `direct_limit` of modules is a colimit in the sense of `category_theory`. -/ @[simps] def direct_limit_is_colimit [nonempty ι] : is_colimit (direct_limit_cocone G f) := { desc := λ s, direct_limit.lift R ι G f s.ι.app $ λ i j h x, by { rw [←s.w (hom_of_le h)], refl }, fac' := λ s i, begin ext x, dsimp, exact direct_limit.lift_of s.ι.app _ x, end, uniq' := λ s m h, begin have : s.ι.app = λ i, linear_map.comp m (direct_limit.of R ι (λ i, G i) (λ i j H, f i j H) i), { funext i, rw ← h, refl }, ext x, simp only [this], apply module.direct_limit.lift_unique end } end direct_limit end Module
27455f5368fef8c8c528a12b0f4f3d19532e12ed
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/meta/rb_map_auto.lean
6a4e003bfedaf8bcc068bdfb4fe0fd3deaa4d988
[]
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
684
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Robert Y. Lewis -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.option.defs import Mathlib.data.list.defs import Mathlib.PostPort namespace Mathlib /-! # rb_map This file defines additional operations on native rb_maps and rb_sets. These structures are defined in core in `init.meta.rb_map`. They are meta objects, and are generally the most efficient dictionary structures to use for pure metaprogramming right now. -/ namespace native /-! ### Declarations about `rb_set` -/ namespace rb_set end Mathlib
33fddfebc7cd517c23714b3023121d5aa8123b0f
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/topology/sequences.lean
23aa7710aac56f824ace4ab28384afa8565aa1af
[ "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
18,782
lean
/- Copyright (c) 2018 Jan-David Salchow. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jan-David Salchow, Patrick Massot -/ import topology.subset_properties import topology.metric_space.basic /-! # Sequences in topological spaces In this file we define sequences in topological spaces and show how they are related to filters and the topology. In particular, we * define the sequential closure of a set and prove that it's contained in the closure, * define a type class "sequential_space" in which closure and sequential closure agree, * define sequential continuity and show that it coincides with continuity in sequential spaces, * provide an instance that shows that every first-countable (and in particular metric) space is a sequential space. * define sequential compactness, prove that compactness implies sequential compactness in first countable spaces, and prove they are equivalent for uniform spaces having a countable uniformity basis (in particular metric spaces). -/ open set filter open_locale topological_space variables {α : Type*} {β : Type*} local notation f ` ⟶ ` limit := tendsto f at_top (𝓝 limit) /-! ### Sequential closures, sequential continuity, and sequential spaces. -/ section topological_space variables [topological_space α] [topological_space β] /-- A sequence converges in the sence of topological spaces iff the associated statement for filter holds. -/ lemma topological_space.seq_tendsto_iff {x : ℕ → α} {limit : α} : tendsto x at_top (𝓝 limit) ↔ ∀ U : set α, limit ∈ U → is_open U → ∃ N, ∀ n ≥ N, (x n) ∈ U := (at_top_basis.tendsto_iff (nhds_basis_opens limit)).trans $ by simp only [and_imp, exists_prop, true_and, set.mem_Ici, ge_iff_le, id] /-- The sequential closure of a subset M ⊆ α of a topological space α is the set of all p ∈ α which arise as limit of sequences in M. -/ def sequential_closure (M : set α) : set α := {p | ∃ x : ℕ → α, (∀ n : ℕ, x n ∈ M) ∧ (x ⟶ p)} lemma subset_sequential_closure (M : set α) : M ⊆ sequential_closure M := assume p (_ : p ∈ M), show p ∈ sequential_closure M, from ⟨λ n, p, assume n, ‹p ∈ M›, tendsto_const_nhds⟩ /-- A set `s` is sequentially closed if for any converging sequence `x n` of elements of `s`, the limit belongs to `s` as well. -/ def is_seq_closed (s : set α) : Prop := s = sequential_closure s /-- A convenience lemma for showing that a set is sequentially closed. -/ lemma is_seq_closed_of_def {A : set α} (h : ∀(x : ℕ → α) (p : α), (∀ n : ℕ, x n ∈ A) → (x ⟶ p) → p ∈ A) : is_seq_closed A := show A = sequential_closure A, from subset.antisymm (subset_sequential_closure A) (show ∀ p, p ∈ sequential_closure A → p ∈ A, from (assume p ⟨x, _, _⟩, show p ∈ A, from h x p ‹∀ n : ℕ, ((x n) ∈ A)› ‹(x ⟶ p)›)) /-- The sequential closure of a set is contained in the closure of that set. The converse is not true. -/ lemma sequential_closure_subset_closure (M : set α) : sequential_closure M ⊆ closure M := assume p ⟨x, xM, xp⟩, mem_closure_of_tendsto xp (univ_mem' xM) /-- A set is sequentially closed if it is closed. -/ lemma is_seq_closed_of_is_closed (M : set α) (_ : is_closed M) : is_seq_closed M := suffices sequential_closure M ⊆ M, from set.eq_of_subset_of_subset (subset_sequential_closure M) this, calc sequential_closure M ⊆ closure M : sequential_closure_subset_closure M ... = M : is_closed.closure_eq ‹is_closed M› /-- The limit of a convergent sequence in a sequentially closed set is in that set.-/ lemma mem_of_is_seq_closed {A : set α} (_ : is_seq_closed A) {x : ℕ → α} (_ : ∀ n, x n ∈ A) {limit : α} (_ : (x ⟶ limit)) : limit ∈ A := have limit ∈ sequential_closure A, from show ∃ x : ℕ → α, (∀ n : ℕ, x n ∈ A) ∧ (x ⟶ limit), from ⟨x, ‹∀ n, x n ∈ A›, ‹(x ⟶ limit)›⟩, eq.subst (eq.symm ‹is_seq_closed A›) ‹limit ∈ sequential_closure A› /-- The limit of a convergent sequence in a closed set is in that set.-/ lemma mem_of_is_closed_sequential {A : set α} (_ : is_closed A) {x : ℕ → α} (_ : ∀ n, x n ∈ A) {limit : α} (_ : x ⟶ limit) : limit ∈ A := mem_of_is_seq_closed (is_seq_closed_of_is_closed A ‹is_closed A›) ‹∀ n, x n ∈ A› ‹(x ⟶ limit)› /-- A sequential space is a space in which 'sequences are enough to probe the topology'. This can be formalised by demanding that the sequential closure and the closure coincide. The following statements show that other topological properties can be deduced from sequences in sequential spaces. -/ class sequential_space (α : Type*) [topological_space α] : Prop := (sequential_closure_eq_closure : ∀ M : set α, sequential_closure M = closure M) /-- In a sequential space, a set is closed iff it's sequentially closed. -/ lemma is_seq_closed_iff_is_closed [sequential_space α] {M : set α} : is_seq_closed M ↔ is_closed M := iff.intro (assume _, closure_eq_iff_is_closed.mp (eq.symm (calc M = sequential_closure M : by assumption ... = closure M : sequential_space.sequential_closure_eq_closure M))) (is_seq_closed_of_is_closed M) /-- In a sequential space, a point belongs to the closure of a set iff it is a limit of a sequence taking values in this set. -/ lemma mem_closure_iff_seq_limit [sequential_space α] {s : set α} {a : α} : a ∈ closure s ↔ ∃ x : ℕ → α, (∀ n : ℕ, x n ∈ s) ∧ (x ⟶ a) := by { rw ← sequential_space.sequential_closure_eq_closure, exact iff.rfl } /-- A function between topological spaces is sequentially continuous if it commutes with limit of convergent sequences. -/ def sequentially_continuous (f : α → β) : Prop := ∀ (x : ℕ → α), ∀ {limit : α}, (x ⟶ limit) → (f∘x ⟶ f limit) /- A continuous function is sequentially continuous. -/ lemma continuous.to_sequentially_continuous {f : α → β} (_ : continuous f) : sequentially_continuous f := assume x limit (_ : x ⟶ limit), have tendsto f (𝓝 limit) (𝓝 (f limit)), from continuous.tendsto ‹continuous f› limit, show (f ∘ x) ⟶ (f limit), from tendsto.comp this ‹(x ⟶ limit)› /-- In a sequential space, continuity and sequential continuity coincide. -/ lemma continuous_iff_sequentially_continuous {f : α → β} [sequential_space α] : continuous f ↔ sequentially_continuous f := iff.intro (assume _, ‹continuous f›.to_sequentially_continuous) (assume : sequentially_continuous f, show continuous f, from suffices h : ∀ {A : set β}, is_closed A → is_seq_closed (f ⁻¹' A), from continuous_iff_is_closed.mpr (assume A _, is_seq_closed_iff_is_closed.mp $ h ‹is_closed A›), assume A (_ : is_closed A), is_seq_closed_of_def $ assume (x : ℕ → α) p (_ : ∀ n, f (x n) ∈ A) (_ : x ⟶ p), have (f ∘ x) ⟶ (f p), from ‹sequentially_continuous f› x ‹(x ⟶ p)›, show f p ∈ A, from mem_of_is_closed_sequential ‹is_closed A› ‹∀ n, f (x n) ∈ A› ‹(f∘x ⟶ f p)›) end topological_space namespace topological_space namespace first_countable_topology variables [topological_space α] [first_countable_topology α] /-- Every first-countable space is sequential. -/ @[priority 100] -- see Note [lower instance priority] instance : sequential_space α := ⟨show ∀ M, sequential_closure M = closure M, from assume M, suffices closure M ⊆ sequential_closure M, from set.subset.antisymm (sequential_closure_subset_closure M) this, -- For every p ∈ closure M, we need to construct a sequence x in M that converges to p: assume (p : α) (hp : p ∈ closure M), -- Since we are in a first-countable space, the neighborhood filter around `p` has a decreasing -- basis `U` indexed by `ℕ`. let ⟨U, hU⟩ := (nhds_generated_countable p).exists_antimono_basis in -- Since `p ∈ closure M`, there is an element in each `M ∩ U i` have hp : ∀ (i : ℕ), ∃ (y : α), y ∈ M ∧ y ∈ U i, by simpa using (mem_closure_iff_nhds_basis hU.1).mp hp, begin -- The axiom of (countable) choice builds our sequence from the later fact choose u hu using hp, rw forall_and_distrib at hu, -- It clearly takes values in `M` use [u, hu.1], -- and converges to `p` because the basis is decreasing. apply hU.tendsto hu.2, end⟩ end first_countable_topology end topological_space section seq_compact open topological_space topological_space.first_countable_topology variables [topological_space α] /-- A set `s` is sequentially compact if every sequence taking values in `s` has a converging subsequence. -/ def is_seq_compact (s : set α) := ∀ ⦃u : ℕ → α⦄, (∀ n, u n ∈ s) → ∃ (x ∈ s) (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (u ∘ φ) at_top (𝓝 x) /-- A space `α` is sequentially compact if every sequence in `α` has a converging subsequence. -/ class seq_compact_space (α : Type*) [topological_space α] : Prop := (seq_compact_univ : is_seq_compact (univ : set α)) lemma is_seq_compact.subseq_of_frequently_in {s : set α} (hs : is_seq_compact s) {u : ℕ → α} (hu : ∃ᶠ n in at_top, u n ∈ s) : ∃ (x ∈ s) (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (u ∘ φ) at_top (𝓝 x) := let ⟨ψ, hψ, huψ⟩ := extraction_of_frequently_at_top hu, ⟨x, x_in, φ, hφ, h⟩ := hs huψ in ⟨x, x_in, ψ ∘ φ, hψ.comp hφ, h⟩ lemma seq_compact_space.tendsto_subseq [seq_compact_space α] (u : ℕ → α) : ∃ x (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (u ∘ φ) at_top (𝓝 x) := let ⟨x, _, φ, mono, h⟩ := seq_compact_space.seq_compact_univ (by simp : ∀ n, u n ∈ univ) in ⟨x, φ, mono, h⟩ section first_countable_topology variables [first_countable_topology α] open topological_space.first_countable_topology lemma is_compact.is_seq_compact {s : set α} (hs : is_compact s) : is_seq_compact s := λ u u_in, let ⟨x, x_in, hx⟩ := @hs (map u at_top) _ (le_principal_iff.mpr (univ_mem' u_in : _)) in ⟨x, x_in, tendsto_subseq hx⟩ lemma is_compact.tendsto_subseq' {s : set α} {u : ℕ → α} (hs : is_compact s) (hu : ∃ᶠ n in at_top, u n ∈ s) : ∃ (x ∈ s) (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (u ∘ φ) at_top (𝓝 x) := hs.is_seq_compact.subseq_of_frequently_in hu lemma is_compact.tendsto_subseq {s : set α} {u : ℕ → α} (hs : is_compact s) (hu : ∀ n, u n ∈ s) : ∃ (x ∈ s) (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (u ∘ φ) at_top (𝓝 x) := hs.is_seq_compact hu @[priority 100] -- see Note [lower instance priority] instance first_countable_topology.seq_compact_of_compact [compact_space α] : seq_compact_space α := ⟨compact_univ.is_seq_compact⟩ lemma compact_space.tendsto_subseq [compact_space α] (u : ℕ → α) : ∃ x (φ : ℕ → ℕ), strict_mono φ ∧ tendsto (u ∘ φ) at_top (𝓝 x) := seq_compact_space.tendsto_subseq u end first_countable_topology end seq_compact section uniform_space_seq_compact open_locale uniformity open uniform_space prod variables [uniform_space β] {s : set β} lemma lebesgue_number_lemma_seq {ι : Type*} {c : ι → set β} (hs : is_seq_compact s) (hc₁ : ∀ i, is_open (c i)) (hc₂ : s ⊆ ⋃ i, c i) (hU : is_countably_generated (𝓤 β)) : ∃ V ∈ 𝓤 β, symmetric_rel V ∧ ∀ x ∈ s, ∃ i, ball x V ⊆ c i := begin classical, obtain ⟨V, hV, Vsymm⟩ : ∃ V : ℕ → set (β × β), (𝓤 β).has_antimono_basis (λ _, true) V ∧ ∀ n, swap ⁻¹' V n = V n, from uniform_space.has_seq_basis hU, clear hU, suffices : ∃ n, ∀ x ∈ s, ∃ i, ball x (V n) ⊆ c i, { cases this with n hn, exact ⟨V n, hV.to_has_basis.mem_of_mem trivial, Vsymm n, hn⟩ }, by_contradiction H, obtain ⟨x, x_in, hx⟩ : ∃ x : ℕ → β, (∀ n, x n ∈ s) ∧ ∀ n i, ¬ ball (x n) (V n) ⊆ c i, { push_neg at H, choose x hx using H, exact ⟨x, forall_and_distrib.mp hx⟩ }, clear H, obtain ⟨x₀, x₀_in, φ, φ_mono, hlim⟩ : ∃ (x₀ ∈ s) (φ : ℕ → ℕ), strict_mono φ ∧ (x ∘ φ ⟶ x₀), from hs x_in, clear hs, obtain ⟨i₀, x₀_in⟩ : ∃ i₀, x₀ ∈ c i₀, { rcases hc₂ x₀_in with ⟨_, ⟨i₀, rfl⟩, x₀_in_c⟩, exact ⟨i₀, x₀_in_c⟩ }, clear hc₂, obtain ⟨n₀, hn₀⟩ : ∃ n₀, ball x₀ (V n₀) ⊆ c i₀, { rcases (nhds_basis_uniformity hV.to_has_basis).mem_iff.mp (is_open_iff_mem_nhds.mp (hc₁ i₀) _ x₀_in) with ⟨n₀, _, h⟩, use n₀, rwa ← ball_eq_of_symmetry (Vsymm n₀) at h }, clear hc₁, obtain ⟨W, W_in, hWW⟩ : ∃ W ∈ 𝓤 β, W ○ W ⊆ V n₀, from comp_mem_uniformity_sets (hV.to_has_basis.mem_of_mem trivial), obtain ⟨N, x_φ_N_in, hVNW⟩ : ∃ N, x (φ N) ∈ ball x₀ W ∧ V (φ N) ⊆ W, { obtain ⟨N₁, h₁⟩ : ∃ N₁, ∀ n ≥ N₁, x (φ n) ∈ ball x₀ W, from tendsto_at_top'.mp hlim _ (mem_nhds_left x₀ W_in), obtain ⟨N₂, h₂⟩ : ∃ N₂, V (φ N₂) ⊆ W, { rcases hV.to_has_basis.mem_iff.mp W_in with ⟨N, _, hN⟩, use N, exact subset.trans (hV.decreasing trivial trivial $ φ_mono.id_le _) hN }, have : φ N₂ ≤ φ (max N₁ N₂), from φ_mono.le_iff_le.mpr (le_max_right _ _), exact ⟨max N₁ N₂, h₁ _ (le_max_left _ _), trans (hV.decreasing trivial trivial this) h₂⟩ }, suffices : ball (x (φ N)) (V (φ N)) ⊆ c i₀, from hx (φ N) i₀ this, calc ball (x $ φ N) (V $ φ N) ⊆ ball (x $ φ N) W : preimage_mono hVNW ... ⊆ ball x₀ (V n₀) : ball_subset_of_comp_subset x_φ_N_in hWW ... ⊆ c i₀ : hn₀, end lemma is_seq_compact.totally_bounded (h : is_seq_compact s) : totally_bounded s := begin classical, apply totally_bounded_of_forall_symm, unfold is_seq_compact at h, contrapose! h, rcases h with ⟨V, V_in, V_symm, h⟩, simp_rw [not_subset] at h, have : ∀ (t : set β), finite t → ∃ a, a ∈ s ∧ a ∉ ⋃ y ∈ t, ball y V, { intros t ht, obtain ⟨a, a_in, H⟩ : ∃ a ∈ s, ∀ (x : β), x ∈ t → (x, a) ∉ V, by simpa [ht] using h t, use [a, a_in], intro H', obtain ⟨x, x_in, hx⟩ := mem_bUnion_iff.mp H', exact H x x_in hx }, cases seq_of_forall_finite_exists this with u hu, clear h this, simp [forall_and_distrib] at hu, cases hu with u_in hu, use [u, u_in], clear u_in, intros x x_in φ, intros hφ huφ, obtain ⟨N, hN⟩ : ∃ N, ∀ p q, p ≥ N → q ≥ N → (u (φ p), u (φ q)) ∈ V, from huφ.cauchy_seq.mem_entourage V_in, specialize hN N (N+1) (le_refl N) (nat.le_succ N), specialize hu (φ $ N+1) (φ N) (hφ $ lt_add_one N), exact hu hN, end protected lemma is_seq_compact.is_compact (h : is_countably_generated $ 𝓤 β) (hs : is_seq_compact s) : is_compact s := begin classical, rw is_compact_iff_finite_subcover, intros ι U Uop s_sub, rcases lebesgue_number_lemma_seq hs Uop s_sub h with ⟨V, V_in, Vsymm, H⟩, rcases totally_bounded_iff_subset.mp hs.totally_bounded V V_in with ⟨t,t_sub, tfin, ht⟩, have : ∀ x : t, ∃ (i : ι), ball x.val V ⊆ U i, { rintros ⟨x, x_in⟩, exact H x (t_sub x_in) }, choose i hi using this, haveI : fintype t := tfin.fintype, use finset.image i finset.univ, transitivity ⋃ y ∈ t, ball y V, { intros x x_in, specialize ht x_in, rw mem_bUnion_iff at *, simp_rw ball_eq_of_symmetry Vsymm, exact ht }, { apply bUnion_subset_bUnion, intros x x_in, exact ⟨i ⟨x, x_in⟩, finset.mem_image_of_mem _ (finset.mem_univ _), hi ⟨x, x_in⟩⟩ }, end protected lemma uniform_space.compact_iff_seq_compact (h : is_countably_generated $ 𝓤 β) : is_compact s ↔ is_seq_compact s := begin haveI := uniform_space.first_countable_topology h, exact ⟨λ H, H.is_seq_compact, λ H, H.is_compact h⟩ end lemma uniform_space.compact_space_iff_seq_compact_space (H : is_countably_generated $ 𝓤 β) : compact_space β ↔ seq_compact_space β := have key : is_compact univ ↔ is_seq_compact univ := uniform_space.compact_iff_seq_compact H, ⟨λ ⟨h⟩, ⟨key.mp h⟩, λ ⟨h⟩, ⟨key.mpr h⟩⟩ end uniform_space_seq_compact section metric_seq_compact variables [metric_space β] {s : set β} open metric /-- A version of Bolzano-Weistrass: in a metric space, is_compact s ↔ is_seq_compact s -/ lemma metric.compact_iff_seq_compact : is_compact s ↔ is_seq_compact s := uniform_space.compact_iff_seq_compact emetric.uniformity_has_countable_basis /-- A version of Bolzano-Weistrass: in a proper metric space (eg. $ℝ^n$), every bounded sequence has a converging subsequence. This version assumes only that the sequence is frequently in some bounded set. -/ lemma tendsto_subseq_of_frequently_bounded [proper_space β] (hs : bounded s) {u : ℕ → β} (hu : ∃ᶠ n in at_top, u n ∈ s) : ∃ b ∈ closure s, ∃ φ : ℕ → ℕ, strict_mono φ ∧ tendsto (u ∘ φ) at_top (𝓝 b) := begin have hcs : is_compact (closure s) := compact_iff_closed_bounded.mpr ⟨is_closed_closure, bounded_closure_of_bounded hs⟩, replace hcs : is_seq_compact (closure s), by rwa metric.compact_iff_seq_compact at hcs, have hu' : ∃ᶠ n in at_top, u n ∈ closure s, { apply frequently.mono hu, intro n, apply subset_closure }, exact hcs.subseq_of_frequently_in hu', end /-- A version of Bolzano-Weistrass: in a proper metric space (eg. $ℝ^n$), every bounded sequence has a converging subsequence. -/ lemma tendsto_subseq_of_bounded [proper_space β] (hs : bounded s) {u : ℕ → β} (hu : ∀ n, u n ∈ s) : ∃ b ∈ closure s, ∃ φ : ℕ → ℕ, strict_mono φ ∧ tendsto (u ∘ φ) at_top (𝓝 b) := tendsto_subseq_of_frequently_bounded hs $ frequently_of_forall hu lemma metric.compact_space_iff_seq_compact_space : compact_space β ↔ seq_compact_space β := uniform_space.compact_space_iff_seq_compact_space emetric.uniformity_has_countable_basis lemma seq_compact.lebesgue_number_lemma_of_metric {ι : Type*} {c : ι → set β} (hs : is_seq_compact s) (hc₁ : ∀ i, is_open (c i)) (hc₂ : s ⊆ ⋃ i, c i) : ∃ δ > 0, ∀ x ∈ s, ∃ i, ball x δ ⊆ c i := begin rcases lebesgue_number_lemma_seq hs hc₁ hc₂ emetric.uniformity_has_countable_basis with ⟨V, V_in, _, hV⟩, rcases uniformity_basis_dist.mem_iff.mp V_in with ⟨δ, δ_pos, h⟩, use [δ, δ_pos], intros x x_in, rcases hV x x_in with ⟨i, hi⟩, use i, have := ball_mono h x, rw ball_eq_ball' at this, exact subset.trans this hi, end end metric_seq_compact
cee3ef5ae178bd41906f3c2a2a235ff903dac9cc
82e44445c70db0f03e30d7be725775f122d72f3e
/src/topology/algebra/group_completion.lean
9e5c6fc538d430a9b199d313ff3e652779eae54c
[ "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
4,882
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 Completion of topological groups: -/ import topology.uniform_space.completion import topology.algebra.uniform_group noncomputable theory universes u v section group open uniform_space Cauchy filter set variables {α : Type u} [uniform_space α] instance [has_zero α] : has_zero (completion α) := ⟨(0 : α)⟩ instance [has_neg α] : has_neg (completion α) := ⟨completion.map (λa, -a : α → α)⟩ instance [has_add α] : has_add (completion α) := ⟨completion.map₂ (+)⟩ instance [has_sub α] : has_sub (completion α) := ⟨completion.map₂ has_sub.sub⟩ -- TODO: switch sides once #1103 is fixed @[norm_cast] lemma uniform_space.completion.coe_zero [has_zero α] : ((0 : α) : completion α) = 0 := rfl end group namespace uniform_space.completion section uniform_add_group open uniform_space uniform_space.completion variables {α : Type*} [uniform_space α] [add_group α] [uniform_add_group α] @[norm_cast] lemma coe_neg (a : α) : ((- a : α) : completion α) = - a := (map_coe uniform_continuous_neg a).symm @[norm_cast] lemma coe_sub (a b : α) : ((a - b : α) : completion α) = a - b := (map₂_coe_coe a b has_sub.sub uniform_continuous_sub).symm @[norm_cast] lemma coe_add (a b : α) : ((a + b : α) : completion α) = a + b := (map₂_coe_coe a b (+) uniform_continuous_add).symm instance : add_monoid (completion α) := { zero_add := assume a, completion.induction_on a (is_closed_eq (continuous_map₂ continuous_const continuous_id) continuous_id) (assume a, show 0 + (a : completion α) = a, by rw_mod_cast zero_add), add_zero := assume a, completion.induction_on a (is_closed_eq (continuous_map₂ continuous_id continuous_const) continuous_id) (assume a, show (a : completion α) + 0 = a, by rw_mod_cast add_zero), add_assoc := assume a b c, completion.induction_on₃ a b c (is_closed_eq (continuous_map₂ (continuous_map₂ continuous_fst (continuous_fst.comp continuous_snd)) (continuous_snd.comp continuous_snd)) (continuous_map₂ continuous_fst (continuous_map₂ (continuous_fst.comp continuous_snd) (continuous_snd.comp continuous_snd)))) (assume a b c, show (a : completion α) + b + c = a + (b + c), by repeat { rw_mod_cast add_assoc }), .. completion.has_zero, .. completion.has_neg, ..completion.has_add, .. completion.has_sub } instance : sub_neg_monoid (completion α) := { sub_eq_add_neg := λ a b, completion.induction_on₂ a b (is_closed_eq (continuous_map₂ continuous_fst continuous_snd) (continuous_map₂ continuous_fst (continuous_map.comp continuous_snd))) (λ a b, by exact_mod_cast congr_arg coe (sub_eq_add_neg a b)), .. completion.add_monoid, .. completion.has_neg, .. completion.has_sub } instance : add_group (completion α) := { add_left_neg := assume a, completion.induction_on a (is_closed_eq (continuous_map₂ completion.continuous_map continuous_id) continuous_const) (assume a, show - (a : completion α) + a = 0, by { rw_mod_cast add_left_neg, refl }), .. completion.sub_neg_monoid } instance : uniform_add_group (completion α) := ⟨uniform_continuous_map₂ has_sub.sub⟩ lemma is_add_group_hom_coe : is_add_group_hom (coe : α → completion α) := { map_add := coe_add } variables {β : Type v} [uniform_space β] [add_group β] [uniform_add_group β] lemma is_add_group_hom_extension [complete_space β] [separated_space β] {f : α → β} (hfg : is_add_group_hom f) (hf : continuous f) : is_add_group_hom (completion.extension f) := have hf : uniform_continuous f, from uniform_continuous_of_continuous hfg hf, { map_add := assume a b, completion.induction_on₂ a b (is_closed_eq (continuous_extension.comp continuous_add) ((continuous_extension.comp continuous_fst).add (continuous_extension.comp continuous_snd))) (λ a b, by rw_mod_cast [extension_coe hf, extension_coe hf, extension_coe hf, hfg.map_add]) } lemma is_add_group_hom_map {f : α → β} (hfa : is_add_group_hom f) (hf : continuous f) : is_add_group_hom (completion.map f) := @is_add_group_hom_extension _ _ _ _ _ _ _ _ _ _ _ (is_add_group_hom.comp hfa is_add_group_hom_coe) ((continuous_coe _).comp hf) instance {α : Type u} [uniform_space α] [add_comm_group α] [uniform_add_group α] : add_comm_group (completion α) := { add_comm := assume a b, completion.induction_on₂ a b (is_closed_eq (continuous_map₂ continuous_fst continuous_snd) (continuous_map₂ continuous_snd continuous_fst)) (assume x y, by { change ↑x + ↑y = ↑y + ↑x, rw [← coe_add, ← coe_add, add_comm]}), .. completion.add_group } end uniform_add_group end uniform_space.completion
e84cc2f43684ea0a400a8602c10a723d75840840
947b78d97130d56365ae2ec264df196ce769371a
/src/Lean/Elab/Command.lean
3cd6c267a65521f5ead75c47389001ec73b1a84c
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
25,055
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 -/ import Lean.Elab.Alias import Lean.Elab.Log import Lean.Elab.ResolveName import Lean.Elab.Term import Lean.Elab.Binders import Lean.Elab.SyntheticMVars import Lean.Elab.DeclModifiers namespace Lean namespace Elab namespace Command structure Scope := (kind : String) (header : String) (opts : Options := {}) (currNamespace : Name := Name.anonymous) (openDecls : List OpenDecl := []) (levelNames : List Name := []) (varDecls : Array Syntax := #[]) instance Scope.inhabited : Inhabited Scope := ⟨{ kind := "", header := "" }⟩ structure State := (env : Environment) (messages : MessageLog := {}) (scopes : List Scope := [{ kind := "root", header := "" }]) (nextMacroScope : Nat := firstFrontendMacroScope + 1) (maxRecDepth : Nat) (nextInstIdx : Nat := 1) -- for generating anonymous instance names (ngen : NameGenerator := {}) instance State.inhabited : Inhabited State := ⟨{ env := arbitrary _, maxRecDepth := 0 }⟩ def mkState (env : Environment) (messages : MessageLog := {}) (opts : Options := {}) : State := { env := env, messages := messages, scopes := [{ kind := "root", header := "", opts := opts }], maxRecDepth := getMaxRecDepth opts } structure Context := (fileName : String) (fileMap : FileMap) (currRecDepth : Nat := 0) (cmdPos : String.Pos := 0) (macroStack : MacroStack := []) (currMacroScope : MacroScope := firstFrontendMacroScope) (ref : Syntax := Syntax.missing) abbrev CommandElabCoreM (ε) := ReaderT Context $ StateRefT State $ EIO ε abbrev CommandElabM := CommandElabCoreM Exception abbrev CommandElab := Syntax → CommandElabM Unit instance : MonadEnv CommandElabM := { getEnv := do s ← get; pure s.env, modifyEnv := fun f => modify fun s => { s with env := f s.env } } instance : MonadOptions CommandElabM := { getOptions := do s ← get; pure s.scopes.head!.opts } protected def getRef : CommandElabM Syntax := do ctx ← read; pure ctx.ref instance : AddMessageContext CommandElabM := { addMessageContext := addMessageContextPartial } instance : Ref CommandElabM := { getRef := Command.getRef, withRef := fun α ref x => adaptReader (fun (ctx : Context) => { ctx with ref := ref }) x } instance : AddErrorMessageContext CommandElabM := { add := fun ref msg => do ctx ← read; let ref := getBetterRef ref ctx.macroStack; msg ← addMessageContext msg; msg ← addMacroStack msg ctx.macroStack; pure (ref, msg) } def mkMessageAux (ctx : Context) (ref : Syntax) (msgData : MessageData) (severity : MessageSeverity) : Message := mkMessageCore ctx.fileName ctx.fileMap msgData severity (ref.getPos.getD ctx.cmdPos) private def mkCoreContext (ctx : Context) (s : State) : Core.Context := let scope := s.scopes.head!; { options := scope.opts, currRecDepth := ctx.currRecDepth, maxRecDepth := s.maxRecDepth, ref := ctx.ref } def liftCoreM {α} (x : CoreM α) : CommandElabM α := do s ← get; ctx ← read; let Eα := Except Exception α; let x : CoreM Eα := catch (do a ← x; pure $ Except.ok a) (fun ex => pure $ Except.error ex); let x : EIO Exception (Eα × Core.State) := (ReaderT.run x (mkCoreContext ctx s)).run { env := s.env, ngen := s.ngen }; (ea, coreS) ← liftM x; modify fun s => { s with env := coreS.env, ngen := coreS.ngen }; match ea with | Except.ok a => pure a | Except.error e => throw e private def ioErrorToMessage (ctx : Context) (ref : Syntax) (err : IO.Error) : Message := let ref := getBetterRef ref ctx.macroStack; mkMessageAux ctx ref (toString err) MessageSeverity.error @[inline] def liftEIO {α} (x : EIO Exception α) : CommandElabM α := liftM x @[inline] def liftIO {α} (x : IO α) : CommandElabM α := do ctx ← read; liftEIO $ adaptExcept (fun (ex : IO.Error) => Exception.error ctx.ref ex.toString) x instance : MonadIO CommandElabM := { liftIO := fun α => liftIO } def getScope : CommandElabM Scope := do s ← get; pure s.scopes.head! def getCurrNamespace : CommandElabM Name := do scope ← getScope; pure scope.currNamespace def getOpenDecls : CommandElabM (List OpenDecl) := do scope ← getScope; pure scope.openDecls instance CommandElabM.monadLog : MonadLog CommandElabM := { getRef := getRef, getFileMap := do ctx ← read; pure ctx.fileMap, getFileName := do ctx ← read; pure ctx.fileName, logMessage := fun msg => do currNamespace ← getCurrNamespace; openDecls ← getOpenDecls; let msg := { msg with data := MessageData.withNamingContext { currNamespace := currNamespace, openDecls := openDecls } msg.data }; modify $ fun s => { s with messages := s.messages.add msg } } protected def getCurrMacroScope : CommandElabM Nat := do ctx ← read; pure ctx.currMacroScope protected def getMainModule : CommandElabM Name := do env ← getEnv; pure env.mainModule @[inline] protected def withFreshMacroScope {α} (x : CommandElabM α) : CommandElabM α := do fresh ← modifyGet (fun st => (st.nextMacroScope, { st with nextMacroScope := st.nextMacroScope + 1 })); adaptReader (fun (ctx : Context) => { ctx with currMacroScope := fresh }) x instance CommandElabM.MonadQuotation : MonadQuotation CommandElabM := { getCurrMacroScope := Command.getCurrMacroScope, getMainModule := Command.getMainModule, withFreshMacroScope := @Command.withFreshMacroScope } unsafe def mkCommandElabAttribute : IO (KeyedDeclsAttribute CommandElab) := mkElabAttribute CommandElab `Lean.Elab.Command.commandElabAttribute `builtinCommandElab `commandElab `Lean.Parser.Command `Lean.Elab.Command.CommandElab "command" @[init mkCommandElabAttribute] constant commandElabAttribute : KeyedDeclsAttribute CommandElab := arbitrary _ private def elabCommandUsing (s : State) (stx : Syntax) : List CommandElab → CommandElabM Unit | [] => do throwError ("unexpected syntax" ++ MessageData.nest 2 (Format.line ++ stx)) | (elabFn::elabFns) => catchInternalId unsupportedSyntaxExceptionId (elabFn stx) (fun _ => do set s; elabCommandUsing elabFns) /- Elaborate `x` with `stx` on the macro stack -/ @[inline] def withMacroExpansion {α} (beforeStx afterStx : Syntax) (x : CommandElabM α) : CommandElabM α := adaptReader (fun (ctx : Context) => { ctx with macroStack := { before := beforeStx, after := afterStx } :: ctx.macroStack }) x instance : MonadMacroAdapter CommandElabM := { getCurrMacroScope := getCurrMacroScope, getNextMacroScope := do s ← get; pure s.nextMacroScope, setNextMacroScope := fun next => modify $ fun s => { s with nextMacroScope := next } } instance : MonadRecDepth CommandElabM := { withRecDepth := fun α d x => adaptReader (fun (ctx : Context) => { ctx with currRecDepth := d }) x, getRecDepth := do ctx ← read; pure ctx.currRecDepth, getMaxRecDepth := do s ← get; pure s.maxRecDepth } @[inline] def withLogging (x : CommandElabM Unit) : CommandElabM Unit := catch x (fun ex => match ex with | Exception.error _ _ => logException ex | Exception.internal id => if id == abortExceptionId then pure () else do idName ← liftIO $ id.getName; logError ("internal exception " ++ toString idName)) partial def elabCommand : Syntax → CommandElabM Unit | stx => withRef stx $ withIncRecDepth $ withFreshMacroScope $ withLogging $ match stx with | Syntax.node k args => if k == nullKind then -- list of commands => elaborate in order -- The parser will only ever return a single command at a time, but syntax quotations can return multiple ones args.forM elabCommand else do trace `Elab.step fun _ => stx; s ← get; stxNew? ← catchInternalId unsupportedSyntaxExceptionId (do newStx ← adaptMacro (getMacros s.env) stx; pure (some newStx)) (fun ex => pure none); match stxNew? with | some stxNew => withMacroExpansion stx stxNew $ elabCommand stxNew | _ => do let table := (commandElabAttribute.ext.getState s.env).table; let k := stx.getKind; match table.find? k with | some elabFns => elabCommandUsing s stx elabFns | none => throwError ("elaboration function for '" ++ toString k ++ "' has not been implemented") | _ => throwError "unexpected command" /-- Adapt a syntax transformation to a regular, command-producing elaborator. -/ def adaptExpander (exp : Syntax → CommandElabM Syntax) : CommandElab := fun stx => do stx' ← exp stx; withMacroExpansion stx stx' $ elabCommand stx' private def getVarDecls (s : State) : Array Syntax := s.scopes.head!.varDecls instance CommandElabM.inhabited {α} : Inhabited (CommandElabM α) := ⟨throw $ arbitrary _⟩ private def mkMetaContext : Meta.Context := { config := { foApprox := true, ctxApprox := true, quasiPatternApprox := true } } private def mkTermContext (ctx : Context) (s : State) (declName? : Option Name) : Term.Context := let scope := s.scopes.head!; { macroStack := ctx.macroStack, fileName := ctx.fileName, fileMap := ctx.fileMap, currMacroScope := ctx.currMacroScope, currNamespace := scope.currNamespace, levelNames := scope.levelNames, openDecls := scope.openDecls, declName? := declName? } /-- Auxiliary function for `liftMetaM` -/ private def mkMessageAux (ref : Syntax) (ctx : Context) (msgData : MessageData) (severity : MessageSeverity) : Message := let pos := ref.getPos.getD 0; mkMessageCore ctx.fileName ctx.fileMap msgData severity pos private def addTraceAsMessages (ctx : Context) (log : MessageLog) (traceState : TraceState) : MessageLog := traceState.traces.foldl (fun (log : MessageLog) traceElem => let ref := replaceRef traceElem.ref ctx.ref; let pos := ref.getPos.getD 0; log.add (mkMessageCore ctx.fileName ctx.fileMap traceElem.msg MessageSeverity.information pos)) log def liftTermElabM {α} (declName? : Option Name) (x : TermElabM α) : CommandElabM α := do ctx ← read; s ← get; let scope := s.scopes.head!; -- We execute `x` with an empty message log. Thus, `x` cannot modify/view messages produced by previous commands. -- This is useful for implementing `runTermElabM` where we use `Term.resetMessageLog` let messages := s.messages; let x : MetaM _ := (observing x).run (mkTermContext ctx s declName?) { messages := {} }; let x : CoreM _ := x.run mkMetaContext {}; let x : EIO _ _ := x.run (mkCoreContext ctx s) { env := s.env, ngen := s.ngen, nextMacroScope := s.nextMacroScope }; (((ea, termS), _), coreS) ← liftEIO x; modify fun s => { s with env := coreS.env, messages := addTraceAsMessages ctx (messages ++ termS.messages) coreS.traceState, nextMacroScope := coreS.nextMacroScope, ngen := coreS.ngen }; match ea with | Except.ok a => pure a | Except.error ex => throw ex @[inline] def runTermElabM {α} (declName? : Option Name) (elabFn : Array Expr → TermElabM α) : CommandElabM α := do s ← get; liftTermElabM declName? -- We don't want to store messages produced when elaborating `(getVarDecls s)` because they have already been saved when we elaborated the `variable`(s) command. -- So, we use `Term.resetMessageLog`. (Term.elabBinders (getVarDecls s) (fun xs => do Term.resetMessageLog; elabFn xs)) @[inline] def catchExceptions (x : CommandElabM Unit) : CommandElabCoreM Empty Unit := fun ctx ref => EIO.catchExceptions (withLogging x ctx ref) (fun _ => pure ()) private def addScope (kind : String) (header : String) (newNamespace : Name) : CommandElabM Unit := modify $ fun s => { s with env := s.env.registerNamespace newNamespace, scopes := { s.scopes.head! with kind := kind, header := header, currNamespace := newNamespace } :: s.scopes } private def addScopes (kind : String) (updateNamespace : Bool) : Name → CommandElabM Unit | Name.anonymous => pure () | Name.str p header _ => do addScopes p; currNamespace ← getCurrNamespace; addScope kind header (if updateNamespace then currNamespace ++ header else currNamespace) | _ => throwError "invalid scope" private def addNamespace (header : Name) : CommandElabM Unit := addScopes "namespace" true header @[builtinCommandElab «namespace»] def elabNamespace : CommandElab := fun stx => match_syntax stx with | `(namespace $n) => addNamespace n.getId | _ => throwUnsupportedSyntax @[builtinCommandElab «section»] def elabSection : CommandElab := fun stx => match_syntax stx with | `(section $header:ident) => addScopes "section" false header.getId | `(section) => do currNamespace ← getCurrNamespace; addScope "section" "" currNamespace | _ => throwUnsupportedSyntax def getScopes : CommandElabM (List Scope) := do s ← get; pure s.scopes private def checkAnonymousScope : List Scope → Bool | { header := "", .. } :: _ => true | _ => false private def checkEndHeader : Name → List Scope → Bool | Name.anonymous, _ => true | Name.str p s _, { header := h, .. } :: scopes => h == s && checkEndHeader p scopes | _, _ => false @[builtinCommandElab «end»] def elabEnd : CommandElab := fun stx => do let header? := (stx.getArg 1).getOptionalIdent?; let endSize := match header? with | none => 1 | some n => n.getNumParts; scopes ← getScopes; if endSize < scopes.length then modify $ fun s => { s with scopes := s.scopes.drop endSize } else do { -- we keep "root" scope modify $ fun s => { s with scopes := s.scopes.drop (s.scopes.length - 1) }; throwError "invalid 'end', insufficient scopes" }; match header? with | none => unless (checkAnonymousScope scopes) $ throwError "invalid 'end', name is missing" | some header => unless (checkEndHeader header scopes) $ throwError "invalid 'end', name mismatch" @[inline] def withNamespace {α} (ns : Name) (elabFn : CommandElabM α) : CommandElabM α := do addNamespace ns; a ← elabFn; modify $ fun s => { s with scopes := s.scopes.drop ns.getNumParts }; pure a @[specialize] def modifyScope (f : Scope → Scope) : CommandElabM Unit := modify $ fun s => { s with scopes := match s.scopes with | h::t => f h :: t | [] => unreachable! } def getLevelNames : CommandElabM (List Name) := do scope ← getScope; pure scope.levelNames def addUnivLevel (idStx : Syntax) : CommandElabM Unit := withRef idStx do let id := idStx.getId; levelNames ← getLevelNames; if levelNames.elem id then throwAlreadyDeclaredUniverseLevel id else modifyScope $ fun scope => { scope with levelNames := id :: scope.levelNames } partial def elabChoiceAux (cmds : Array Syntax) : Nat → CommandElabM Unit | i => if h : i < cmds.size then let cmd := cmds.get ⟨i, h⟩; catchInternalId unsupportedSyntaxExceptionId (elabCommand cmd) (fun ex => elabChoiceAux (i+1)) else throwUnsupportedSyntax @[builtinCommandElab choice] def elbChoice : CommandElab := fun stx => elabChoiceAux stx.getArgs 0 @[builtinCommandElab «universe»] def elabUniverse : CommandElab := fun n => do addUnivLevel (n.getArg 1) @[builtinCommandElab «universes»] def elabUniverses : CommandElab := fun n => do let idsStx := n.getArg 1; idsStx.forArgsM addUnivLevel @[builtinCommandElab «init_quot»] def elabInitQuot : CommandElab := fun stx => do env ← getEnv; match env.addDecl Declaration.quotDecl with | Except.ok env => setEnv env | Except.error ex => do opts ← getOptions; throwError (ex.toMessageData opts) def logUnknownDecl (declName : Name) : CommandElabM Unit := logError ("unknown declaration '" ++ toString declName ++ "'") def resolveNamespace (id : Name) : CommandElabM Name := do env ← getEnv; currNamespace ← getCurrNamespace; openDecls ← getOpenDecls; match Elab.resolveNamespace env currNamespace openDecls id with | some ns => pure ns | none => throwError ("unknown namespace '" ++ id ++ "'") @[builtinCommandElab «export»] def elabExport : CommandElab := fun stx => do -- `stx` is of the form (Command.export "export" <namespace> "(" (null <ids>*) ")") let id := stx.getIdAt 1; ns ← resolveNamespace id; currNamespace ← getCurrNamespace; when (ns == currNamespace) $ throwError "invalid 'export', self export"; env ← getEnv; let ids := (stx.getArg 3).getArgs; aliases ← ids.foldlM (fun (aliases : List (Name × Name)) (idStx : Syntax) => do { let id := idStx.getId; let declName := ns ++ id; if env.contains declName then pure $ (currNamespace ++ id, declName) :: aliases else do withRef idStx $ logUnknownDecl declName; pure aliases }) []; modify $ fun s => { s with env := aliases.foldl (fun env p => addAlias env p.1 p.2) s.env } def addOpenDecl (d : OpenDecl) : CommandElabM Unit := modifyScope $ fun scope => { scope with openDecls := d :: scope.openDecls } def elabOpenSimple (n : SyntaxNode) : CommandElabM Unit := -- `open` id+ let nss := n.getArg 0; nss.forArgsM $ fun ns => do ns ← resolveNamespace ns.getId; addOpenDecl (OpenDecl.simple ns []) -- `open` id `(` id+ `)` def elabOpenOnly (n : SyntaxNode) : CommandElabM Unit := do let ns := n.getIdAt 0; ns ← resolveNamespace ns; let ids := n.getArg 2; ids.forArgsM $ fun idStx => do let id := idStx.getId; let declName := ns ++ id; env ← getEnv; if env.contains declName then addOpenDecl (OpenDecl.explicit id declName) else withRef idStx $ logUnknownDecl declName -- `open` id `hiding` id+ def elabOpenHiding (n : SyntaxNode) : CommandElabM Unit := do let ns := n.getIdAt 0; ns ← resolveNamespace ns; let idsStx := n.getArg 2; env ← getEnv; ids : List Name ← idsStx.foldArgsM (fun idStx ids => do let id := idStx.getId; let declName := ns ++ id; if env.contains declName then pure (id::ids) else do withRef idStx $ logUnknownDecl declName; pure ids) []; addOpenDecl (OpenDecl.simple ns ids) -- `open` id `renaming` sepBy (id `->` id) `,` def elabOpenRenaming (n : SyntaxNode) : CommandElabM Unit := do let ns := n.getIdAt 0; ns ← resolveNamespace ns; let rs := (n.getArg 2); rs.forSepArgsM $ fun stx => do let fromId := stx.getIdAt 0; let toId := stx.getIdAt 2; let declName := ns ++ fromId; env ← getEnv; if env.contains declName then addOpenDecl (OpenDecl.explicit toId declName) else withRef stx $ logUnknownDecl declName @[builtinCommandElab «open»] def elabOpen : CommandElab := fun n => do let body := (n.getArg 1).asNode; let k := body.getKind; if k == `Lean.Parser.Command.openSimple then elabOpenSimple body else if k == `Lean.Parser.Command.openOnly then elabOpenOnly body else if k == `Lean.Parser.Command.openHiding then elabOpenHiding body else elabOpenRenaming body @[builtinCommandElab «variable»] def elabVariable : CommandElab := fun n => do -- `variable` bracketedBinder let binder := n.getArg 1; -- Try to elaborate `binder` for sanity checking runTermElabM none $ fun _ => Term.elabBinder binder $ fun _ => pure (); modifyScope $ fun scope => { scope with varDecls := scope.varDecls.push binder } @[builtinCommandElab «variables»] def elabVariables : CommandElab := fun n => do -- `variables` bracketedBinder+ let binders := (n.getArg 1).getArgs; -- Try to elaborate `binders` for sanity checking runTermElabM none $ fun _ => Term.elabBinders binders $ fun _ => pure (); modifyScope $ fun scope => { scope with varDecls := scope.varDecls ++ binders } open Meta @[builtinCommandElab Lean.Parser.Command.check] def elabCheck : CommandElab := fun stx => do let term := stx.getArg 1; withoutModifyingEnv $ runTermElabM (some `_check) $ fun _ => do e ← Term.elabTerm term none; Term.synthesizeSyntheticMVarsNoPostponing; type ← inferType e; logInfo (e ++ " : " ++ type); pure () def hasNoErrorMessages : CommandElabM Bool := do s ← get; pure $ !s.messages.hasErrors def failIfSucceeds (x : CommandElabM Unit) : CommandElabM Unit := do let resetMessages : CommandElabM MessageLog := do { s ← get; let messages := s.messages; modify $ fun s => { s with messages := {} }; pure messages }; let restoreMessages (prevMessages : MessageLog) : CommandElabM Unit := do { modify $ fun s => { s with messages := prevMessages ++ s.messages.errorsToWarnings } }; prevMessages ← resetMessages; succeeded ← finally (catch (do x; hasNoErrorMessages) (fun ex => match ex with | Exception.error _ _ => do logException ex; pure false | Exception.internal id => do logError "internal"; pure false)) -- TODO: improve `logError "internal"` (restoreMessages prevMessages); when succeeded $ throwError "unexpected success" @[builtinCommandElab «check_failure»] def elabCheckFailure : CommandElab := fun stx => failIfSucceeds $ elabCheck stx unsafe def elabEvalUnsafe : CommandElab := fun stx => do let ref := stx; let term := stx.getArg 1; let n := `_eval; ctx ← read; let addAndCompile (value : Expr) : TermElabM Unit := do { type ← inferType value; let decl := Declaration.defnDecl { name := n, lparams := [], type := type, value := value, hints := ReducibilityHints.opaque, isUnsafe := true }; Term.ensureNoUnassignedMVars decl; addAndCompile decl }; let elabMetaEval : CommandElabM Unit := runTermElabM (some n) fun _ => do { e ← Term.elabTerm term none; Term.synthesizeSyntheticMVarsNoPostponing; e ← withLocalDeclD `env (mkConst `Lean.Environment) fun env => withLocalDeclD `opts (mkConst `Lean.Options) fun opts => do { e ← mkAppM `Lean.runMetaEval #[env, opts, e]; mkLambdaFVars #[env, opts] e }; env ← getEnv; opts ← getOptions; act ← finally (do addAndCompile e; evalConst (Environment → Options → IO (String × Except IO.Error Environment)) n) (setEnv env); (out, res) ← MonadIO.liftIO $ act env opts; -- we execute `act` using the environment logInfo out; match res with | Except.error e => throwError e.toString | Except.ok env => do setEnv env; pure () }; let elabEval : CommandElabM Unit := runTermElabM (some n) fun _ => do { -- fall back to non-meta eval if MetaHasEval hasn't been defined yet -- modify e to `runEval e` e ← Term.elabTerm term none; Term.synthesizeSyntheticMVarsNoPostponing; e ← mkAppM `Lean.runEval #[e]; env ← getEnv; act ← finally (do addAndCompile e; evalConst (IO (String × Except IO.Error Unit)) n ) (setEnv env); (out, res) ← MonadIO.liftIO act; logInfo out; match res with | Except.error e => throwError e.toString | Except.ok _ => pure () }; env ← getEnv; if env.contains `Lean.MetaHasEval then do elabMetaEval else elabEval @[builtinCommandElab «eval», implementedBy elabEvalUnsafe] constant elabEval : CommandElab := arbitrary _ @[builtinCommandElab «synth»] def elabSynth : CommandElab := fun stx => do let term := stx.getArg 1; withoutModifyingEnv $ runTermElabM `_synth_cmd $ fun _ => do inst ← Term.elabTerm term none; Term.synthesizeSyntheticMVarsNoPostponing; inst ← instantiateMVars inst; val ← synthInstance inst; logInfo val; pure () def setOption (optionName : Name) (val : DataValue) : CommandElabM Unit := do decl ← liftIO $ getOptionDecl optionName; unless (decl.defValue.sameCtor val) $ throwError "type mismatch at set_option"; modifyScope $ fun scope => { scope with opts := scope.opts.insert optionName val }; match optionName, val with | `maxRecDepth, DataValue.ofNat max => modify $ fun s => { s with maxRecDepth := max } | _, _ => pure () @[builtinCommandElab «set_option»] def elabSetOption : CommandElab := fun stx => do let optionName := stx.getIdAt 1; let val := stx.getArg 2; match val.isStrLit? with | some str => setOption optionName (DataValue.ofString str) | none => match val.isNatLit? with | some num => setOption optionName (DataValue.ofNat num) | none => match val with | Syntax.atom _ "true" => setOption optionName (DataValue.ofBool true) | Syntax.atom _ "false" => setOption optionName (DataValue.ofBool false) | _ => logErrorAt val ("unexpected set_option value " ++ toString val) @[builtinMacro Lean.Parser.Command.«in»] def expandInCmd : Macro := fun stx => do let cmd₁ := stx.getArg 0; let cmd₂ := stx.getArg 2; `(section $cmd₁:command $cmd₂:command end) def expandDeclId (declId : Syntax) (modifiers : Modifiers) : CommandElabM ExpandDeclIdResult := do currNamespace ← getCurrNamespace; currLevelNames ← getLevelNames; Lean.Elab.expandDeclId currNamespace currLevelNames declId modifiers end Command end Elab end Lean
b4ac475a0601ce3027ea63ed4138cd2b0c71a765
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/hott/hit/set_quotient.hlean
9b75b648ef7e9e4fbcc3aa3740bf8b6c4f2a4a0c
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
5,763
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Declaration of set-quotients, i.e. quotient of a mere relation which is then set-truncated. -/ import function algebra.relation types.trunc types.eq hit.quotient open eq is_trunc trunc quotient equiv namespace set_quotient section parameters {A : Type} (R : A → A → Prop) -- set-quotients are just set-truncations of (type) quotients definition set_quotient : Type := trunc 0 (quotient R) parameter {R} definition class_of (a : A) : set_quotient := tr (class_of _ a) definition eq_of_rel {a a' : A} (H : R a a') : class_of a = class_of a' := ap tr (eq_of_rel _ H) theorem is_set_set_quotient [instance] : is_set set_quotient := begin unfold set_quotient, exact _ end protected definition rec {P : set_quotient → Type} [Pt : Πaa, is_set (P aa)] (Pc : Π(a : A), P (class_of a)) (Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a =[eq_of_rel H] Pc a') (x : set_quotient) : P x := begin apply (@trunc.rec_on _ _ P x), { intro x', apply Pt}, { intro y, induction y, { apply Pc}, { exact pathover_of_pathover_ap P tr (Pp H)}} end protected definition rec_on [reducible] {P : set_quotient → Type} (x : set_quotient) [Pt : Πaa, is_set (P aa)] (Pc : Π(a : A), P (class_of a)) (Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a =[eq_of_rel H] Pc a') : P x := rec Pc Pp x theorem rec_eq_of_rel {P : set_quotient → Type} [Pt : Πaa, is_set (P aa)] (Pc : Π(a : A), P (class_of a)) (Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a =[eq_of_rel H] Pc a') {a a' : A} (H : R a a') : apdo (rec Pc Pp) (eq_of_rel H) = Pp H := !is_set.elimo protected definition elim {P : Type} [Pt : is_set P] (Pc : A → P) (Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a = Pc a') (x : set_quotient) : P := rec Pc (λa a' H, pathover_of_eq (Pp H)) x protected definition elim_on [reducible] {P : Type} (x : set_quotient) [Pt : is_set P] (Pc : A → P) (Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a = Pc a') : P := elim Pc Pp x theorem elim_eq_of_rel {P : Type} [Pt : is_set P] (Pc : A → P) (Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a = Pc a') {a a' : A} (H : R a a') : ap (elim Pc Pp) (eq_of_rel H) = Pp H := begin apply eq_of_fn_eq_fn_inv !(pathover_constant (eq_of_rel H)), rewrite [▸*,-apdo_eq_pathover_of_eq_ap,↑elim,rec_eq_of_rel], end protected definition rec_prop {P : set_quotient → Type} [Pt : Πaa, is_prop (P aa)] (Pc : Π(a : A), P (class_of a)) (x : set_quotient) : P x := rec Pc (λa a' H, !is_prop.elimo) x protected definition elim_prop {P : Type} [Pt : is_prop P] (Pc : A → P) (x : set_quotient) : P := elim Pc (λa a' H, !is_prop.elim) x end end set_quotient attribute set_quotient.class_of [constructor] attribute set_quotient.rec set_quotient.elim [unfold 7] [recursor 7] attribute set_quotient.rec_on set_quotient.elim_on [unfold 4] open sigma relation function namespace set_quotient variables {A B C : Type} (R : A → A → Prop) (S : B → B → Prop) (T : C → C → Prop) definition is_surjective_class_of : is_surjective (class_of : A → set_quotient R) := λx, set_quotient.rec_on x (λa, tr (fiber.mk a idp)) (λa a' r, !is_prop.elimo) /- non-dependent universal property -/ definition set_quotient_arrow_equiv (B : Type) [H : is_set B] : (set_quotient R → B) ≃ (Σ(f : A → B), Π(a a' : A), R a a' → f a = f a') := begin fapply equiv.MK, { intro f, exact ⟨λa, f (class_of a), λa a' r, ap f (eq_of_rel r)⟩}, { intro v x, induction v with f p, exact set_quotient.elim_on x f p}, { intro v, induction v with f p, esimp, apply ap (sigma.mk f), apply eq_of_homotopy3, intro a a' r, apply elim_eq_of_rel}, { intro f, apply eq_of_homotopy, intro x, refine set_quotient.rec_on x _ _: esimp, intro a, reflexivity, intro a a' r, apply eq_pathover, apply hdeg_square, apply elim_eq_of_rel} end protected definition code [unfold 4] (a : A) (x : set_quotient R) [H : is_equivalence R] : Prop := set_quotient.elim_on x (R a) begin intros a' a'' H1, refine to_inv !trunctype_eq_equiv _, esimp, apply ua, apply equiv_of_is_prop, { intro H2, exact is_transitive.trans R H2 H1}, { intro H2, apply is_transitive.trans R H2, exact is_symmetric.symm R H1} end protected definition encode {a : A} {x : set_quotient R} (p : class_of a = x) [H : is_equivalence R] : set_quotient.code R a x := begin induction p, esimp, apply is_reflexive.refl R end definition rel_of_eq {a a' : A} (p : class_of a = class_of a' :> set_quotient R) [H : is_equivalence R] : R a a' := set_quotient.encode R p variables {R S T} definition quotient_unary_map [unfold 7] (f : A → B) (H : Π{a a'} (r : R a a'), S (f a) (f a')) : set_quotient R → set_quotient S := set_quotient.elim (class_of ∘ f) (λa a' r, eq_of_rel (H r)) definition quotient_binary_map [unfold 10 11] (f : A → B → C) (H : Π{a a'} (r : R a a') {b b'} (s : S b b'), T (f a b) (f a' b')) [HR : is_reflexive R] [HS : is_reflexive S] : set_quotient R → set_quotient S → set_quotient T := begin refine set_quotient.elim _ _, { intro a, refine set_quotient.elim _ _, { intro b, exact class_of (f a b)}, { intro b b' s, apply eq_of_rel, apply H, apply is_reflexive.refl R, exact s}}, { intro a a' r, apply eq_of_homotopy, refine set_quotient.rec _ _, { intro b, esimp, apply eq_of_rel, apply H, exact r, apply is_reflexive.refl S}, { intro b b' s, apply eq_pathover, esimp, apply is_set.elims}} end end set_quotient
49f6cad5e39482051e81ac952ae5d343ab8a64d9
e30ff3aabdac29f8ea40ad76887544d0f9be9018
/ircbot/modules/default.lean
9d14ec0af5018831e29db7382fd9858847de5c7c
[]
no_license
forked-from-1kasper/leanbot
bdef0efa3e4d0eb75b06c1707fb4e35086bb57fa
c61c8c7fdad7b05877e0d232719ce23d2999557f
refs/heads/master
1,651,846,081,986
1,646,404,009,000
1,646,404,009,000
127,132,795
12
1
null
1,605,183,650,000
1,522,237,998,000
Lean
UTF-8
Lean
false
false
105
lean
import ircbot.modules.ping_pong ircbot.modules.print_date import ircbot.modules.help ircbot.modules.admin
44ba7dbce69ea085ba612d67d1182c21c05d16be
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/logic/relator.lean
225cb3b08f536b28d670ba6240c8b6b3092230b9
[ "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
3,500
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 Relator for functions, pairs, sums, and lists. -/ import logic.basic namespace relator universes u₁ u₂ v₁ v₂ /- TODO(johoelzl): * should we introduce relators of datatypes as recursive function or as inductive predicate? For now we stick to the recursor approach. * relation lift for datatypes, Π, Σ, set, and subtype types * proof composition and identity laws * implement method to derive relators from datatype -/ section variables {α : Sort u₁} {β : Sort u₂} {γ : Sort v₁} {δ : Sort v₂} variables (R : α → β → Prop) (S : γ → δ → Prop) def lift_fun (f : α → γ) (g : β → δ) : Prop := ∀⦃a b⦄, R a b → S (f a) (g b) infixr ⇒ := lift_fun end section variables {α : Type u₁} {β : Type u₂} (R : α → β → Prop) def right_total : Prop := ∀ b, ∃ a, R a b def left_total : Prop := ∀ a, ∃ b, R a b def bi_total : Prop := left_total R ∧ right_total R def left_unique : Prop := ∀ ⦃a b c⦄, R a c → R b c → a = b def right_unique : Prop := ∀ ⦃a b c⦄, R a b → R a c → b = c def bi_unique : Prop := left_unique R ∧ right_unique R variable {R} lemma right_total.rel_forall (h : right_total R) : ((R ⇒ implies) ⇒ implies) (λp, ∀i, p i) (λq, ∀i, q i) := assume p q Hrel H b, exists.elim (h b) (assume a Rab, Hrel Rab (H _)) lemma left_total.rel_exists (h : left_total R) : ((R ⇒ implies) ⇒ implies) (λp, ∃i, p i) (λq, ∃i, q i) := assume p q Hrel ⟨a, pa⟩, (h a).imp $ λ b Rab, Hrel Rab pa lemma bi_total.rel_forall (h : bi_total R) : ((R ⇒ iff) ⇒ iff) (λp, ∀i, p i) (λq, ∀i, q i) := assume p q Hrel, ⟨assume H b, exists.elim (h.right b) (assume a Rab, (Hrel Rab).mp (H _)), assume H a, exists.elim (h.left a) (assume b Rab, (Hrel Rab).mpr (H _))⟩ lemma bi_total.rel_exists (h : bi_total R) : ((R ⇒ iff) ⇒ iff) (λp, ∃i, p i) (λq, ∃i, q i) := assume p q Hrel, ⟨assume ⟨a, pa⟩, (h.left a).imp $ λ b Rab, (Hrel Rab).1 pa, assume ⟨b, qb⟩, (h.right b).imp $ λ a Rab, (Hrel Rab).2 qb⟩ lemma left_unique_of_rel_eq {eq' : β → β → Prop} (he : (R ⇒ (R ⇒ iff)) eq eq') : left_unique R := λ a b c (ac : R a c) (bc : R b c), (he ac bc).mpr ((he bc bc).mp rfl) end lemma rel_imp : (iff ⇒ (iff ⇒ iff)) implies implies := assume p q h r s l, imp_congr h l lemma rel_not : (iff ⇒ iff) not not := assume p q h, not_congr h lemma bi_total_eq {α : Type u₁} : relator.bi_total (@eq α) := { left := λ a, ⟨a, rfl⟩, right := λ a, ⟨a, rfl⟩ } variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables {r : α → β → Prop} {p : β → γ → Prop} {q : γ → δ → Prop} lemma left_unique.flip (h : left_unique r) : right_unique (flip r) := λ a b c h₁ h₂, h h₁ h₂ lemma rel_and : ((↔) ⇒ (↔) ⇒ (↔)) (∧) (∧) := assume a b h₁ c d h₂, and_congr h₁ h₂ lemma rel_or : ((↔) ⇒ (↔) ⇒ (↔)) (∨) (∨) := assume a b h₁ c d h₂, or_congr h₁ h₂ lemma rel_iff : ((↔) ⇒ (↔) ⇒ (↔)) (↔) (↔) := assume a b h₁ c d h₂, iff_congr h₁ h₂ lemma rel_eq {r : α → β → Prop} (hr : bi_unique r) : (r ⇒ r ⇒ (↔)) (=) (=) := assume a b h₁ c d h₂, iff.intro begin intro h, subst h, exact hr.right h₁ h₂ end begin intro h, subst h, exact hr.left h₁ h₂ end end relator
f8e242b313b1e4383189e741478f5b0acfb55098
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/number_theory/padics/padic_norm.lean
6b5c5cd89fc6896779e6772f5a51f8ce07a2a225
[ "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
13,049
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import number_theory.padics.padic_val /-! # p-adic norm > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the `p`-adic norm on `ℚ`. The `p`-adic valuation on `ℚ` is the difference of the multiplicities of `p` in the numerator and denominator of `q`. This function obeys the standard properties of a valuation, with the appropriate assumptions on `p`. The valuation induces a norm on `ℚ`. This norm is a nonarchimedean absolute value. It takes values in {0} ∪ {1/p^k | k ∈ ℤ}. ## Notations This file uses the local notation `/.` for `rat.mk`. ## Implementation notes Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically by taking `[fact p.prime]` as a type class argument. ## References * [F. Q. Gouvêa, *p-adic numbers*][gouvea1997] * [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019] * <https://en.wikipedia.org/wiki/P-adic_number> ## Tags p-adic, p adic, padic, norm, valuation -/ /-- If `q ≠ 0`, the `p`-adic norm of a rational `q` is `p ^ -padic_val_rat p q`. If `q = 0`, the `p`-adic norm of `q` is `0`. -/ def padic_norm (p : ℕ) (q : ℚ) : ℚ := if q = 0 then 0 else (p : ℚ) ^ -padic_val_rat p q namespace padic_norm open padic_val_rat variables {p : ℕ} /-- Unfolds the definition of the `p`-adic norm of `q` when `q ≠ 0`. -/ @[simp] protected lemma eq_zpow_of_nonzero {q : ℚ} (hq : q ≠ 0) : padic_norm p q = p ^ -padic_val_rat p q := by simp [hq, padic_norm] /-- The `p`-adic norm is nonnegative. -/ protected lemma nonneg (q : ℚ) : 0 ≤ padic_norm p q := if hq : q = 0 then by simp [hq, padic_norm] else begin unfold padic_norm; split_ifs, apply zpow_nonneg, exact_mod_cast nat.zero_le _ end /-- The `p`-adic norm of `0` is `0`. -/ @[simp] protected lemma zero : padic_norm p 0 = 0 := by simp [padic_norm] /-- The `p`-adic norm of `1` is `1`. -/ @[simp] protected lemma one : padic_norm p 1 = 1 := by simp [padic_norm] /-- The `p`-adic norm of `p` is `p⁻¹` if `p > 1`. See also `padic_norm.padic_norm_p_of_prime` for a version assuming `p` is prime. -/ lemma padic_norm_p (hp : 1 < p) : padic_norm p p = p⁻¹ := by simp [padic_norm, (pos_of_gt hp).ne', padic_val_nat.self hp] /-- The `p`-adic norm of `p` is `p⁻¹` if `p` is prime. See also `padic_norm.padic_norm_p` for a version assuming `1 < p`. -/ @[simp] lemma padic_norm_p_of_prime [fact p.prime] : padic_norm p p = p⁻¹ := padic_norm_p $ nat.prime.one_lt (fact.out _) /-- The `p`-adic norm of `q` is `1` if `q` is prime and not equal to `p`. -/ lemma padic_norm_of_prime_of_ne {q : ℕ} [p_prime : fact p.prime] [q_prime : fact q.prime] (neq : p ≠ q) : padic_norm p q = 1 := begin have p : padic_val_rat p q = 0, { exact_mod_cast @padic_val_nat_primes p q p_prime q_prime neq }, simp [padic_norm, p, q_prime.1.1, q_prime.1.ne_zero], end /-- The `p`-adic norm of `p` is less than `1` if `1 < p`. See also `padic_norm.padic_norm_p_lt_one_of_prime` for a version assuming `p` is prime. -/ lemma padic_norm_p_lt_one (hp : 1 < p) : padic_norm p p < 1 := begin rw [padic_norm_p hp, inv_lt_one_iff], exact_mod_cast (or.inr hp) end /-- The `p`-adic norm of `p` is less than `1` if `p` is prime. See also `padic_norm.padic_norm_p_lt_one` for a version assuming `1 < p`. -/ lemma padic_norm_p_lt_one_of_prime [fact p.prime] : padic_norm p p < 1 := padic_norm_p_lt_one $ nat.prime.one_lt (fact.out _) /-- `padic_norm p q` takes discrete values `p ^ -z` for `z : ℤ`. -/ protected theorem values_discrete {q : ℚ} (hq : q ≠ 0) : ∃ z : ℤ, padic_norm p q = p ^ -z := ⟨ (padic_val_rat p q), by simp [padic_norm, hq] ⟩ /-- `padic_norm p` is symmetric. -/ @[simp] protected lemma neg (q : ℚ) : padic_norm p (-q) = padic_norm p q := if hq : q = 0 then by simp [hq] else by simp [padic_norm, hq] variable [hp : fact p.prime] include hp /-- If `q ≠ 0`, then `padic_norm p q ≠ 0`. -/ protected lemma nonzero {q : ℚ} (hq : q ≠ 0) : padic_norm p q ≠ 0 := begin rw padic_norm.eq_zpow_of_nonzero hq, apply zpow_ne_zero_of_ne_zero, exact_mod_cast ne_of_gt hp.1.pos end /-- If the `p`-adic norm of `q` is 0, then `q` is `0`. -/ lemma zero_of_padic_norm_eq_zero {q : ℚ} (h : padic_norm p q = 0) : q = 0 := begin apply by_contradiction, intro hq, unfold padic_norm at h, rw if_neg hq at h, apply absurd h, apply zpow_ne_zero_of_ne_zero, exact_mod_cast hp.1.ne_zero end /-- The `p`-adic norm is multiplicative. -/ @[simp] protected theorem mul (q r : ℚ) : padic_norm p (q * r) = padic_norm p q * padic_norm p r := if hq : q = 0 then by simp [hq] else if hr : r = 0 then by simp [hr] else have q * r ≠ 0, from mul_ne_zero hq hr, have (p : ℚ) ≠ 0, by simp [hp.1.ne_zero], by simp [padic_norm, *, padic_val_rat.mul, zpow_add₀ this, mul_comm] /-- The `p`-adic norm respects division. -/ @[simp] protected theorem div (q r : ℚ) : padic_norm p (q / r) = padic_norm p q / padic_norm p r := if hr : r = 0 then by simp [hr] else eq_div_of_mul_eq (padic_norm.nonzero hr) (by rw [←padic_norm.mul, div_mul_cancel _ hr]) /-- The `p`-adic norm of an integer is at most `1`. -/ protected theorem of_int (z : ℤ) : padic_norm p z ≤ 1 := if hz : z = 0 then by simp [hz, zero_le_one] else begin unfold padic_norm, rw [if_neg _], { refine zpow_le_one_of_nonpos _ _, { exact_mod_cast le_of_lt hp.1.one_lt }, { rw [padic_val_rat.of_int, neg_nonpos], norm_cast, simp }}, exact_mod_cast hz, end private lemma nonarchimedean_aux {q r : ℚ} (h : padic_val_rat p q ≤ padic_val_rat p r) : padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) := have hnqp : padic_norm p q ≥ 0, from padic_norm.nonneg _, have hnrp : padic_norm p r ≥ 0, from padic_norm.nonneg _, if hq : q = 0 then by simp [hq, max_eq_right hnrp, le_max_right] else if hr : r = 0 then by simp [hr, max_eq_left hnqp, le_max_left] else if hqr : q + r = 0 then le_trans (by simpa [hqr] using hnqp) (le_max_left _ _) else begin unfold padic_norm, split_ifs, apply le_max_iff.2, left, apply zpow_le_of_le, { exact_mod_cast le_of_lt hp.1.one_lt }, { apply neg_le_neg, have : padic_val_rat p q = min (padic_val_rat p q) (padic_val_rat p r), from (min_eq_left h).symm, rw this, apply min_le_padic_val_rat_add; assumption } end /-- The `p`-adic norm is nonarchimedean: the norm of `p + q` is at most the max of the norm of `p` and the norm of `q`. -/ protected theorem nonarchimedean {q r : ℚ} : padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) := begin wlog hle : (padic_val_rat p q) ≤ (padic_val_rat p r) generalizing q r, { rw [add_comm, max_comm], exact this (le_of_not_le hle) }, exact nonarchimedean_aux hle end /-- The `p`-adic norm respects the triangle inequality: the norm of `p + q` is at most the norm of `p` plus the norm of `q`. -/ theorem triangle_ineq (q r : ℚ) : padic_norm p (q + r) ≤ padic_norm p q + padic_norm p r := calc padic_norm p (q + r) ≤ max (padic_norm p q) (padic_norm p r) : padic_norm.nonarchimedean ... ≤ padic_norm p q + padic_norm p r : max_le_add_of_nonneg (padic_norm.nonneg _) (padic_norm.nonneg _) /-- The `p`-adic norm of a difference is at most the max of each component. Restates the archimedean property of the `p`-adic norm. -/ protected theorem sub {q r : ℚ} : padic_norm p (q - r) ≤ max (padic_norm p q) (padic_norm p r) := by rw [sub_eq_add_neg, ←padic_norm.neg r]; apply padic_norm.nonarchimedean /-- If the `p`-adic norms of `q` and `r` are different, then the norm of `q + r` is equal to the max of the norms of `q` and `r`. -/ lemma add_eq_max_of_ne {q r : ℚ} (hne : padic_norm p q ≠ padic_norm p r) : padic_norm p (q + r) = max (padic_norm p q) (padic_norm p r) := begin wlog hlt : (padic_norm p r) < (padic_norm p q), { rw [add_comm, max_comm], exact this hne.symm (hne.lt_or_lt.resolve_right hlt) }, have : padic_norm p q ≤ max (padic_norm p (q + r)) (padic_norm p r), from calc padic_norm p q = padic_norm p (q + r - r) : by congr; ring ... ≤ max (padic_norm p (q + r)) (padic_norm p (-r)) : padic_norm.nonarchimedean ... = max (padic_norm p (q + r)) (padic_norm p r) : by simp, have hnge : padic_norm p r ≤ padic_norm p (q + r), { apply le_of_not_gt, intro hgt, rw max_eq_right_of_lt hgt at this, apply not_lt_of_ge this, assumption }, have : padic_norm p q ≤ padic_norm p (q + r), by rwa [max_eq_left hnge] at this, apply _root_.le_antisymm, { apply padic_norm.nonarchimedean }, { rwa max_eq_left_of_lt hlt } end /-- The `p`-adic norm is an absolute value: positive-definite and multiplicative, satisfying the triangle inequality. -/ instance : is_absolute_value (padic_norm p) := { abv_nonneg := padic_norm.nonneg, abv_eq_zero := λ _, ⟨zero_of_padic_norm_eq_zero, λ hx, by simpa only [hx]⟩, abv_add := padic_norm.triangle_ineq, abv_mul := padic_norm.mul } lemma dvd_iff_norm_le {n : ℕ} {z : ℤ} : ↑(p ^ n) ∣ z ↔ padic_norm p z ≤ p ^ (-n : ℤ) := begin unfold padic_norm, split_ifs with hz, { norm_cast at hz, have : 0 ≤ (p ^ n : ℚ), {apply pow_nonneg, exact_mod_cast le_of_lt hp.1.pos }, simp [hz, this] }, { rw [zpow_le_iff_le, neg_le_neg_iff, padic_val_rat.of_int, padic_val_int.of_ne_one_ne_zero hp.1.ne_one _], { norm_cast, rw [← part_enat.coe_le_coe, part_enat.coe_get, ← multiplicity.pow_dvd_iff_le_multiplicity], simp }, { exact_mod_cast hz }, { exact_mod_cast hp.1.one_lt } } end /-- The `p`-adic norm of an integer `m` is one iff `p` doesn't divide `m`. -/ lemma int_eq_one_iff (m : ℤ) : padic_norm p m = 1 ↔ ¬ (p : ℤ) ∣ m := begin nth_rewrite 1 ← pow_one p, simp only [dvd_iff_norm_le, int.cast_coe_nat, nat.cast_one, zpow_neg, zpow_one, not_le], split, { intro h, rw [h, inv_lt_one_iff_of_pos]; norm_cast, { exact nat.prime.one_lt (fact.out _), }, { exact nat.prime.pos (fact.out _), }, }, { simp only [padic_norm], split_ifs, { rw [inv_lt_zero, ← nat.cast_zero, nat.cast_lt], intro h, exact (nat.not_lt_zero p h).elim, }, { have : 1 < (p : ℚ) := by norm_cast; exact (nat.prime.one_lt (fact.out _ : nat.prime p)), rw [← zpow_neg_one, zpow_lt_iff_lt this], have : 0 ≤ padic_val_rat p m, simp only [of_int, nat.cast_nonneg], intro h, rw [← zpow_zero (p : ℚ), zpow_inj]; linarith, } }, end lemma int_lt_one_iff (m : ℤ) : padic_norm p m < 1 ↔ (p : ℤ) ∣ m := begin rw [← not_iff_not, ← int_eq_one_iff, eq_iff_le_not_lt], simp only [padic_norm.of_int, true_and], end lemma of_nat (m : ℕ) : padic_norm p m ≤ 1 := padic_norm.of_int (m : ℤ) /-- The `p`-adic norm of a natural `m` is one iff `p` doesn't divide `m`. -/ lemma nat_eq_one_iff (m : ℕ) : padic_norm p m = 1 ↔ ¬ p ∣ m := by simp only [←int.coe_nat_dvd, ←int_eq_one_iff, int.cast_coe_nat] lemma nat_lt_one_iff (m : ℕ) : padic_norm p m < 1 ↔ p ∣ m := by simp only [←int.coe_nat_dvd, ←int_lt_one_iff, int.cast_coe_nat] open_locale big_operators lemma sum_lt {α : Type*} {F : α → ℚ} {t : ℚ} {s : finset α} : s.nonempty → (∀ i ∈ s, padic_norm p (F i) < t) → padic_norm p (∑ i in s, F i) < t := begin classical, refine s.induction_on (by { rintro ⟨-, ⟨⟩⟩, }) _, rintro a S haS IH - ht, by_cases hs : S.nonempty, { rw finset.sum_insert haS, exact lt_of_le_of_lt padic_norm.nonarchimedean (max_lt (ht a (finset.mem_insert_self a S)) (IH hs (λ b hb, ht b (finset.mem_insert_of_mem hb)))), }, { simp * at *, }, end lemma sum_le {α : Type*} {F : α → ℚ} {t : ℚ} {s : finset α} : s.nonempty → (∀ i ∈ s, padic_norm p (F i) ≤ t) → padic_norm p (∑ i in s, F i) ≤ t := begin classical, refine s.induction_on (by { rintro ⟨-, ⟨⟩⟩, }) _, rintro a S haS IH - ht, by_cases hs : S.nonempty, { rw finset.sum_insert haS, exact padic_norm.nonarchimedean.trans (max_le (ht a (finset.mem_insert_self a S)) (IH hs (λ b hb, ht b (finset.mem_insert_of_mem hb)))), }, { simp * at *, }, end lemma sum_lt' {α : Type*} {F : α → ℚ} {t : ℚ} {s : finset α} (hF : ∀ i ∈ s, padic_norm p (F i) < t) (ht : 0 < t) : padic_norm p (∑ i in s, F i) < t := begin obtain rfl | hs := finset.eq_empty_or_nonempty s, { simp [ht], }, { exact sum_lt hs hF, }, end lemma sum_le' {α : Type*} {F : α → ℚ} {t : ℚ} {s : finset α} (hF : ∀ i ∈ s, padic_norm p (F i) ≤ t) (ht : 0 ≤ t) : padic_norm p (∑ i in s, F i) ≤ t := begin obtain rfl | hs := finset.eq_empty_or_nonempty s, { simp [ht], }, { exact sum_le hs hF, }, end end padic_norm
d63152eeed1c1c60090e9676aa4f0c9e34267fc3
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/test/conv.lean
fe990ba4b0c179b80227a3dc792db06d75b56408
[ "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
1,207
lean
/- Copyright (c) 2018 Keeley Hoek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Keeley Hoek -/ import tactic.converter.interactive import tactic.ring example : 0 + 0 = 0 := begin conv_lhs {erw [add_zero]} end example : 0 + 0 = 0 := begin conv_lhs {simp} end example : 0 = 0 + 0 := begin conv_rhs {simp} end -- Example with ring discharging the goal example : 22 + 7 * 4 + 3 * 8 = 0 + 7 * 4 + 46 := begin conv { ring, }, end -- Example with ring failing to discharge, to normalizing the goal example : (22 + 7 * 4 + 3 * 8 = 0 + 7 * 4 + 47) = (74 = 75) := begin conv { ring_nf, }, end -- Example with ring discharging the goal example (x : ℕ) : 22 + 7 * x + 3 * 8 = 0 + 7 * x + 46 := begin conv { ring, }, end -- Example with ring failing to discharge, to normalizing the goal example (x : ℕ) : (22 + 7 * x + 3 * 8 = 0 + 7 * x + 46 + 1) = (7 * x + 46 = 7 * x + 47) := begin conv { ring_nf, }, end -- norm_num examples: example : 22 + 7 * 4 + 3 * 8 = 74 := begin conv { norm_num, }, end example (x : ℕ) : 22 + 7 * x + 3 * 8 = 7 * x + 46 := begin simp [add_comm, add_left_comm], conv { norm_num, }, end
baef20e879d0ca07b52c262b15058f5c789ef454
63abd62053d479eae5abf4951554e1064a4c45b4
/src/group_theory/submonoid/operations.lean
58a2d41ed53808a952a38d951359de7c0155d908
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
21,391
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, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard, Amelia Livingston, Yury Kudryashov -/ import group_theory.submonoid.basic import data.equiv.mul_add import algebra.group.prod import algebra.group.inj_surj /-! # Operations on `submonoid`s In this file we define various operations on `submonoid`s and `monoid_hom`s. ## Main definitions ### Conversion between multiplicative and additive definitions * `submonoid.to_add_submonoid`, `submonoid.of_add_submonoid`, `add_submonoid.to_submonoid`, `add_submonoid.of_submonoid`: convert between multiplicative and additive submonoids of `M`, `multiplicative M`, and `additive M`. * `submonoid.add_submonoid_equiv`: equivalence between `submonoid M` and `add_submonoid (additive M)`. ### (Commutative) monoid structure on a submonoid * `submonoid.to_monoid`, `submonoid.to_comm_monoid`: a submonoid inherits a (commutative) monoid structure. ### Operations on submonoids * `submonoid.comap`: preimage of a submonoid under a monoid homomorphism as a submonoid of the domain; * `submonoid.map`: image of a submonoid under a monoid homomorphism as a submonoid of the codomain; * `submonoid.prod`: product of two submonoids `s : submonoid M` and `t : submonoid N` as a submonoid of `M × N`; ### Monoid homomorphisms between submonoid * `submonoid.subtype`: embedding of a submonoid into the ambient monoid. * `submonoid.inclusion`: given two submonoids `S`, `T` such that `S ≤ T`, `S.inclusion T` is the inclusion of `S` into `T` as a monoid homomorphism; * `mul_equiv.submonoid_congr`: converts a proof of `S = T` into a monoid isomorphism between `S` and `T`. * `submonoid.prod_equiv`: monoid isomorphism between `s.prod t` and `s × t`; ### Operations on `monoid_hom`s * `monoid_hom.mrange`: range of a monoid homomorphism as a submonoid of the codomain; * `monoid_hom.mrestrict`: restrict a monoid homomorphism to a submonoid; * `monoid_hom.cod_mrestrict`: restrict the codomain of a monoid homomorphism to a submonoid; * `monoid_hom.mrange_restrict`: restrict a monoid homomorphism to its range; ## Tags submonoid, range, product, map, comap -/ variables {M N P : Type*} [monoid M] [monoid N] [monoid P] (S : submonoid M) /-! ### Conversion to/from `additive`/`multiplicative` -/ /-- Map from submonoids of monoid `M` to `add_submonoid`s of `additive M`. -/ def submonoid.to_add_submonoid {M : Type*} [monoid M] (S : submonoid M) : add_submonoid (additive M) := { carrier := S.carrier, zero_mem' := S.one_mem', add_mem' := S.mul_mem' } /-- Map from `add_submonoid`s of `additive M` to submonoids of `M`. -/ def submonoid.of_add_submonoid {M : Type*} [monoid M] (S : add_submonoid (additive M)) : submonoid M := { carrier := S.carrier, one_mem' := S.zero_mem', mul_mem' := S.add_mem' } /-- Map from `add_submonoid`s of `add_monoid M` to submonoids of `multiplicative M`. -/ def add_submonoid.to_submonoid {M : Type*} [add_monoid M] (S : add_submonoid M) : submonoid (multiplicative M) := { carrier := S.carrier, one_mem' := S.zero_mem', mul_mem' := S.add_mem' } /-- Map from submonoids of `multiplicative M` to `add_submonoid`s of `add_monoid M`. -/ def add_submonoid.of_submonoid {M : Type*} [add_monoid M] (S : submonoid (multiplicative M)) : add_submonoid M := { carrier := S.carrier, zero_mem' := S.one_mem', add_mem' := S.mul_mem' } /-- Submonoids of monoid `M` are isomorphic to additive submonoids of `additive M`. -/ def submonoid.add_submonoid_equiv (M : Type*) [monoid M] : submonoid M ≃ add_submonoid (additive M) := { to_fun := submonoid.to_add_submonoid, inv_fun := submonoid.of_add_submonoid, left_inv := λ x, by cases x; refl, right_inv := λ x, by cases x; refl } namespace submonoid open set /-! ### `comap` and `map` -/ /-- The preimage of a submonoid along a monoid homomorphism is a submonoid. -/ @[to_additive "The preimage of an `add_submonoid` along an `add_monoid` homomorphism is an `add_submonoid`."] def comap (f : M →* N) (S : submonoid N) : submonoid M := { carrier := (f ⁻¹' S), one_mem' := show f 1 ∈ S, by rw f.map_one; exact S.one_mem, mul_mem' := λ a b ha hb, show f (a * b) ∈ S, by rw f.map_mul; exact S.mul_mem ha hb } @[simp, to_additive] lemma coe_comap (S : submonoid N) (f : M →* N) : (S.comap f : set M) = f ⁻¹' S := rfl @[simp, to_additive] lemma mem_comap {S : submonoid N} {f : M →* N} {x : M} : x ∈ S.comap f ↔ f x ∈ S := iff.rfl @[to_additive] lemma comap_comap (S : submonoid P) (g : N →* P) (f : M →* N) : (S.comap g).comap f = S.comap (g.comp f) := rfl @[to_additive, simp] lemma comap_id (S : submonoid P) : S.comap (monoid_hom.id _) = S := ext (by simp) /-- The image of a submonoid along a monoid homomorphism is a submonoid. -/ @[to_additive "The image of an `add_submonoid` along an `add_monoid` homomorphism is an `add_submonoid`."] def map (f : M →* N) (S : submonoid M) : submonoid N := { carrier := (f '' S), one_mem' := ⟨1, S.one_mem, f.map_one⟩, mul_mem' := begin rintros _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩, exact ⟨x * y, S.mul_mem hx hy, by rw f.map_mul; refl⟩ end } @[simp, to_additive] lemma coe_map (f : M →* N) (S : submonoid M) : (S.map f : set N) = f '' S := rfl @[simp, to_additive] lemma mem_map {f : M →* N} {S : submonoid M} {y : N} : y ∈ S.map f ↔ ∃ x ∈ S, f x = y := mem_image_iff_bex @[to_additive] lemma mem_map_of_mem {f : M →* N} {S : submonoid M} (x : S) : f x ∈ S.map f := mem_image_of_mem f x.2 @[to_additive] lemma map_map (g : N →* P) (f : M →* N) : (S.map f).map g = S.map (g.comp f) := ext' $ image_image _ _ _ @[to_additive] lemma map_le_iff_le_comap {f : M →* N} {S : submonoid M} {T : submonoid N} : S.map f ≤ T ↔ S ≤ T.comap f := image_subset_iff @[to_additive] lemma gc_map_comap (f : M →* N) : galois_connection (map f) (comap f) := λ S T, map_le_iff_le_comap @[to_additive] lemma map_le_of_le_comap {T : submonoid N} {f : M →* N} : S ≤ T.comap f → S.map f ≤ T := (gc_map_comap f).l_le @[to_additive] lemma le_comap_of_map_le {T : submonoid N} {f : M →* N} : S.map f ≤ T → S ≤ T.comap f := (gc_map_comap f).le_u @[to_additive] lemma le_comap_map {f : M →* N} : S ≤ (S.map f).comap f := (gc_map_comap f).le_u_l _ @[to_additive] lemma map_comap_le {S : submonoid N} {f : M →* N} : (S.comap f).map f ≤ S := (gc_map_comap f).l_u_le _ @[to_additive] lemma monotone_map {f : M →* N} : monotone (map f) := (gc_map_comap f).monotone_l @[to_additive] lemma monotone_comap {f : M →* N} : monotone (comap f) := (gc_map_comap f).monotone_u @[simp, to_additive] lemma map_comap_map {f : M →* N} : ((S.map f).comap f).map f = S.map f := congr_fun ((gc_map_comap f).l_u_l_eq_l) _ @[simp, to_additive] lemma comap_map_comap {S : submonoid N} {f : M →* N} : ((S.comap f).map f).comap f = S.comap f := congr_fun ((gc_map_comap f).u_l_u_eq_u) _ @[to_additive] lemma map_sup (S T : submonoid M) (f : M →* N) : (S ⊔ T).map f = S.map f ⊔ T.map f := (gc_map_comap f).l_sup @[to_additive] lemma map_supr {ι : Sort*} (f : M →* N) (s : ι → submonoid M) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr @[to_additive] lemma comap_inf (S T : submonoid N) (f : M →* N) : (S ⊓ T).comap f = S.comap f ⊓ T.comap f := (gc_map_comap f).u_inf @[to_additive] lemma comap_infi {ι : Sort*} (f : M →* N) (s : ι → submonoid N) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[simp, to_additive] lemma map_bot (f : M →* N) : (⊥ : submonoid M).map f = ⊥ := (gc_map_comap f).l_bot @[simp, to_additive] lemma comap_top (f : M →* N) : (⊤ : submonoid N).comap f = ⊤ := (gc_map_comap f).u_top @[simp, to_additive] lemma map_id (S : submonoid M) : S.map (monoid_hom.id M) = S := ext (λ x, ⟨λ ⟨_, h, rfl⟩, h, λ h, ⟨_, h, rfl⟩⟩) section galois_coinsertion variables {ι : Type*} {f : M →* N} (hf : function.injective f) include hf /-- `map f` and `comap f` form a `galois_coinsertion` when `f` is injective. -/ def gci_map_comap : galois_coinsertion (map f) (comap f) := (gc_map_comap f).to_galois_coinsertion (λ S x, by simp [mem_comap, mem_map, hf.eq_iff]) lemma comap_map_eq_of_injective (S : submonoid M) : (S.map f).comap f = S := (gci_map_comap hf).u_l_eq _ lemma comap_surjective_of_injective : function.surjective (comap f) := (gci_map_comap hf).u_surjective lemma map_injective_of_injective : function.injective (map f) := (gci_map_comap hf).l_injective lemma comap_inf_map_of_injective (S T : submonoid M) : (S.map f ⊓ T.map f).comap f = S ⊓ T := (gci_map_comap hf).u_inf_l _ _ lemma comap_infi_map_of_injective (S : ι → submonoid M) : (⨅ i, (S i).map f).comap f = infi S := (gci_map_comap hf).u_infi_l _ lemma comap_sup_map_of_injective (S T : submonoid M) : (S.map f ⊔ T.map f).comap f = S ⊔ T := (gci_map_comap hf).u_sup_l _ _ lemma comap_supr_map_of_injective (S : ι → submonoid M) : (⨆ i, (S i).map f).comap f = supr S := (gci_map_comap hf).u_supr_l _ lemma map_le_map_iff_of_injective {S T : submonoid M} : S.map f ≤ T.map f ↔ S ≤ T := (gci_map_comap hf).l_le_l_iff lemma map_strict_mono_of_injective : strict_mono (map f) := (gci_map_comap hf).strict_mono_l end galois_coinsertion section galois_insertion variables {ι : Type*} {f : M →* N} (hf : function.surjective f) include hf /-- `map f` and `comap f` form a `galois_insertion` when `f` is surjective. -/ def gi_map_comap : galois_insertion (map f) (comap f) := (gc_map_comap f).to_galois_insertion (λ S x h, let ⟨y, hy⟩ := hf x in mem_map.2 ⟨y, by simp [hy, h]⟩) lemma map_comap_eq_of_surjective (S : submonoid N) : (S.comap f).map f = S := (gi_map_comap hf).l_u_eq _ lemma map_surjective_of_surjective : function.surjective (map f) := (gi_map_comap hf).l_surjective lemma comap_injective_of_surjective : function.injective (comap f) := (gi_map_comap hf).u_injective lemma map_inf_comap_of_surjective (S T : submonoid N) : (S.comap f ⊓ T.comap f).map f = S ⊓ T := (gi_map_comap hf).l_inf_u _ _ lemma map_infi_comap_of_surjective (S : ι → submonoid N) : (⨅ i, (S i).comap f).map f = infi S := (gi_map_comap hf).l_infi_u _ lemma map_sup_comap_of_surjective (S T : submonoid N) : (S.comap f ⊔ T.comap f).map f = S ⊔ T := (gi_map_comap hf).l_sup_u _ _ lemma map_supr_comap_of_surjective (S : ι → submonoid N) : (⨆ i, (S i).comap f).map f = supr S := (gi_map_comap hf).l_supr_u _ lemma comap_le_comap_iff_of_surjective {S T : submonoid N} : S.comap f ≤ T.comap f ↔ S ≤ T := (gi_map_comap hf).u_le_u_iff lemma comap_strict_mono_of_surjective : strict_mono (comap f) := (gi_map_comap hf).strict_mono_u end galois_insertion /-- A submonoid of a monoid inherits a multiplication. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits an addition."] instance has_mul : has_mul S := ⟨λ a b, ⟨a.1 * b.1, S.mul_mem a.2 b.2⟩⟩ /-- A submonoid of a monoid inherits a 1. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits a zero."] instance has_one : has_one S := ⟨⟨_, S.one_mem⟩⟩ @[simp, to_additive] lemma coe_mul (x y : S) : (↑(x * y) : M) = ↑x * ↑y := rfl @[simp, to_additive] lemma coe_one : ((1 : S) : M) = 1 := rfl attribute [norm_cast] coe_mul coe_one attribute [norm_cast] add_submonoid.coe_add add_submonoid.coe_zero /-- A submonoid of a monoid inherits a monoid structure. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits an `add_monoid` structure."] instance to_monoid {M : Type*} [monoid M] (S : submonoid M) : monoid S := S.coe_injective.monoid coe rfl (λ _ _, rfl) /-- A submonoid of a `comm_monoid` is a `comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `add_comm_monoid` is an `add_comm_monoid`."] instance to_comm_monoid {M} [comm_monoid M] (S : submonoid M) : comm_monoid S := S.coe_injective.comm_monoid coe rfl (λ _ _, rfl) /-- The natural monoid hom from a submonoid of monoid `M` to `M`. -/ @[to_additive "The natural monoid hom from an `add_submonoid` of `add_monoid` `M` to `M`."] def subtype : S →* M := ⟨coe, rfl, λ _ _, rfl⟩ @[simp, to_additive] theorem coe_subtype : ⇑S.subtype = coe := rfl /-- Given `submonoid`s `s`, `t` of monoids `M`, `N` respectively, `s × t` as a submonoid of `M × N`. -/ @[to_additive prod "Given `add_submonoid`s `s`, `t` of `add_monoid`s `A`, `B` respectively, `s × t` as an `add_submonoid` of `A × B`."] def prod (s : submonoid M) (t : submonoid N) : submonoid (M × N) := { carrier := (s : set M).prod t, one_mem' := ⟨s.one_mem, t.one_mem⟩, mul_mem' := λ p q hp hq, ⟨s.mul_mem hp.1 hq.1, t.mul_mem hp.2 hq.2⟩ } @[to_additive coe_prod] lemma coe_prod (s : submonoid M) (t : submonoid N) : (s.prod t : set (M × N)) = (s : set M).prod (t : set N) := rfl @[to_additive mem_prod] lemma mem_prod {s : submonoid M} {t : submonoid N} {p : M × N} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[to_additive prod_mono] lemma prod_mono {s₁ s₂ : submonoid M} {t₁ t₂ : submonoid N} (hs : s₁ ≤ s₂) (ht : t₁ ≤ t₂) : s₁.prod t₁ ≤ s₂.prod t₂ := set.prod_mono hs ht @[to_additive prod_top] lemma prod_top (s : submonoid M) : s.prod (⊤ : submonoid N) = s.comap (monoid_hom.fst M N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst] @[to_additive top_prod] lemma top_prod (s : submonoid N) : (⊤ : submonoid M).prod s = s.comap (monoid_hom.snd M N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd] @[simp, to_additive top_prod_top] lemma top_prod_top : (⊤ : submonoid M).prod (⊤ : submonoid N) = ⊤ := (top_prod _).trans $ comap_top _ @[to_additive] lemma bot_prod_bot : (⊥ : submonoid M).prod (⊥ : submonoid N) = ⊥ := ext' $ by simp [coe_prod, prod.one_eq_mk] /-- The product of submonoids is isomorphic to their product as monoids. -/ @[to_additive prod_equiv "The product of additive submonoids is isomorphic to their product as additive monoids"] def prod_equiv (s : submonoid M) (t : submonoid N) : s.prod t ≃* s × t := { map_mul' := λ x y, rfl, .. equiv.set.prod ↑s ↑t } open monoid_hom @[to_additive] lemma map_inl (s : submonoid M) : s.map (inl M N) = s.prod ⊥ := ext $ λ p, ⟨λ ⟨x, hx, hp⟩, hp ▸ ⟨hx, set.mem_singleton 1⟩, λ ⟨hps, hp1⟩, ⟨p.1, hps, prod.ext rfl $ (set.eq_of_mem_singleton hp1).symm⟩⟩ @[to_additive] lemma map_inr (s : submonoid N) : s.map (inr M N) = prod ⊥ s := ext $ λ p, ⟨λ ⟨x, hx, hp⟩, hp ▸ ⟨set.mem_singleton 1, hx⟩, λ ⟨hp1, hps⟩, ⟨p.2, hps, prod.ext (set.eq_of_mem_singleton hp1).symm rfl⟩⟩ @[simp, to_additive prod_bot_sup_bot_prod] lemma prod_bot_sup_bot_prod (s : submonoid M) (t : submonoid N) : (s.prod ⊥) ⊔ (prod ⊥ t) = s.prod t := le_antisymm (sup_le (prod_mono (le_refl s) bot_le) (prod_mono bot_le (le_refl t))) $ assume p hp, prod.fst_mul_snd p ▸ mul_mem _ ((le_sup_left : s.prod ⊥ ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨hp.1, set.mem_singleton 1⟩) ((le_sup_right : prod ⊥ t ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨set.mem_singleton 1, hp.2⟩) end submonoid namespace monoid_hom open submonoid /-- The range of a monoid homomorphism is a submonoid. -/ @[to_additive "The range of an `add_monoid_hom` is an `add_submonoid`."] def mrange (f : M →* N) : submonoid N := (⊤ : submonoid M).map f @[simp, to_additive] lemma coe_mrange (f : M →* N) : (f.mrange : set N) = set.range f := set.image_univ @[simp, to_additive] lemma mem_mrange {f : M →* N} {y : N} : y ∈ f.mrange ↔ ∃ x, f x = y := by simp [mrange] @[to_additive] lemma map_mrange (g : N →* P) (f : M →* N) : f.mrange.map g = (g.comp f).mrange := (⊤ : submonoid M).map_map g f @[to_additive] lemma mrange_top_iff_surjective {N} [monoid N] {f : M →* N} : f.mrange = (⊤ : submonoid N) ↔ function.surjective f := submonoid.ext'_iff.trans $ iff.trans (by rw [coe_mrange, coe_top]) set.range_iff_surjective /-- The range of a surjective monoid hom is the whole of the codomain. -/ @[to_additive "The range of a surjective `add_monoid` hom is the whole of the codomain."] lemma mrange_top_of_surjective {N} [monoid N] (f : M →* N) (hf : function.surjective f) : f.mrange = (⊤ : submonoid N) := mrange_top_iff_surjective.2 hf @[to_additive] lemma mrange_eq_map (f : M →* N) : f.mrange = map f ⊤ := rfl @[to_additive] lemma mclosure_preimage_le (f : M →* N) (s : set N) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 $ λ x hx, mem_coe.2 $ mem_comap.2 $ subset_closure hx /-- The image under a monoid hom of the submonoid generated by a set equals the submonoid generated by the image of the set. -/ @[to_additive "The image under an `add_monoid` hom of the `add_submonoid` generated by a set equals the `add_submonoid` generated by the image of the set."] lemma map_mclosure (f : M →* N) (s : set M) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _) (mclosure_preimage_le _ _)) (closure_le.2 $ set.image_subset _ subset_closure) /-- Restriction of a monoid hom to a submonoid of the domain. -/ @[to_additive "Restriction of an add_monoid hom to an `add_submonoid` of the domain."] def mrestrict {N : Type*} [monoid N] (f : M →* N) (S : submonoid M) : S →* N := f.comp S.subtype @[simp, to_additive] lemma mrestrict_apply {N : Type*} [monoid N] (f : M →* N) (x : S) : f.mrestrict S x = f x := rfl /-- Restriction of a monoid hom to a submonoid of the codomain. -/ @[to_additive "Restriction of an `add_monoid` hom to an `add_submonoid` of the codomain."] def cod_mrestrict (f : M →* N) (S : submonoid N) (h : ∀ x, f x ∈ S) : M →* S := { to_fun := λ n, ⟨f n, h n⟩, map_one' := subtype.eq f.map_one, map_mul' := λ x y, subtype.eq (f.map_mul x y) } /-- Restriction of a monoid hom to its range interpreted as a submonoid. -/ @[to_additive "Restriction of an `add_monoid` hom to its range interpreted as a submonoid."] def mrange_restrict {N} [monoid N] (f : M →* N) : M →* f.mrange := f.cod_mrestrict f.mrange $ λ x, ⟨x, submonoid.mem_top x, rfl⟩ @[simp, to_additive] lemma coe_mrange_restrict {N} [monoid N] (f : M →* N) (x : M) : (f.mrange_restrict x : N) = f x := rfl end monoid_hom namespace submonoid open monoid_hom @[to_additive] lemma mrange_inl : (inl M N).mrange = prod ⊤ ⊥ := map_inl ⊤ @[to_additive] lemma mrange_inr : (inr M N).mrange = prod ⊥ ⊤ := map_inr ⊤ @[to_additive] lemma mrange_inl' : (inl M N).mrange = comap (snd M N) ⊥ := mrange_inl.trans (top_prod _) @[to_additive] lemma mrange_inr' : (inr M N).mrange = comap (fst M N) ⊥ := mrange_inr.trans (prod_top _) @[simp, to_additive] lemma mrange_fst : (fst M N).mrange = ⊤ := (fst M N).mrange_top_of_surjective $ @prod.fst_surjective _ _ ⟨1⟩ @[simp, to_additive] lemma mrange_snd : (snd M N).mrange = ⊤ := (snd M N).mrange_top_of_surjective $ @prod.snd_surjective _ _ ⟨1⟩ @[simp, to_additive] lemma mrange_inl_sup_mrange_inr : (inl M N).mrange ⊔ (inr M N).mrange = ⊤ := by simp only [mrange_inl, mrange_inr, prod_bot_sup_bot_prod, top_prod_top] /-- The monoid hom associated to an inclusion of submonoids. -/ @[to_additive "The `add_monoid` hom associated to an inclusion of submonoids."] def inclusion {S T : submonoid M} (h : S ≤ T) : S →* T := S.subtype.cod_mrestrict _ (λ x, h x.2) @[simp, to_additive] lemma range_subtype (s : submonoid M) : s.subtype.mrange = s := ext' $ (coe_mrange _).trans $ subtype.range_coe @[to_additive] lemma eq_bot_iff_forall : S = ⊥ ↔ ∀ x ∈ S, x = (1 : M) := begin split, { intros h x x_in, rwa [h, mem_bot] at x_in }, { intros h, ext x, rw mem_bot, exact ⟨h x, by { rintros rfl, exact S.one_mem }⟩ }, end @[to_additive] lemma nontrivial_iff_exists_ne_one (S : submonoid M) : nontrivial S ↔ ∃ x ∈ S, x ≠ (1:M) := begin split, { introI h, rcases exists_ne (1 : S) with ⟨⟨h, h_in⟩, h_ne⟩, use [h, h_in], intro hyp, apply h_ne, simpa [hyp] }, { rintros ⟨x, x_in, hx⟩, apply nontrivial_of_ne (⟨x, x_in⟩ : S) 1, intro hyp, apply hx, simpa [has_one.one] using hyp }, end /-- A submonoid is either the trivial submonoid or nontrivial. -/ @[to_additive] lemma bot_or_nontrivial (S : submonoid M) : S = ⊥ ∨ nontrivial S := begin classical, by_cases h : ∀ x ∈ S, x = (1 : M), { left, exact S.eq_bot_iff_forall.mpr h }, { right, push_neg at h, simpa [nontrivial_iff_exists_ne_one] using h }, end /-- A submonoid is either the trivial submonoid or contains a nonzero element. -/ @[to_additive] lemma bot_or_exists_ne_one (S : submonoid M) : S = ⊥ ∨ ∃ x ∈ S, x ≠ (1:M) := begin convert S.bot_or_nontrivial, rw nontrivial_iff_exists_ne_one end end submonoid namespace mul_equiv variables {S} {T : submonoid M} /-- Makes the identity isomorphism from a proof that two submonoids of a multiplicative monoid are equal. -/ @[to_additive "Makes the identity additive isomorphism from a proof two submonoids of an additive monoid are equal."] def submonoid_congr (h : S = T) : S ≃* T := { map_mul' := λ _ _, rfl, ..equiv.set_congr $ submonoid.ext'_iff.1 h } end mul_equiv
a608cec1f463402ff0a44c6fb1c2c812966f972a
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/dsimplify2.lean
26deea6ed7a052540d164ffc8645c06971e10d84
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
418
lean
open tactic def f : nat → nat := λ x, x + 10 @[simp] lemma f_lemma (x : nat) : f x = x + 10 := rfl example (p : nat → Prop) (a : nat) (h : p (a + 10)) : p (f a) := by do t ← target, S ← simp_lemmas.mk_default, new_t ← dsimplify (λ e, failed) (λ e, do new_e ← S^.drewrite e, return (new_e, tt)) t, expected ← to_expr ```(p (a + 10)), guard (new_t = expected), change new_t, assumption
f2da4f756211eee9f20463f74eafb498329210f9
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/Process.lean
863a22350e85c2e3920d9016595d290994de81fe
[ "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
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
1,731
lean
-- open IO.Process def usingIO {α} (x : IO α) : IO α := x #eval usingIO do let child ← spawn { cmd := "sh", args := #["-c", "exit 1"] }; child.wait #eval usingIO do let child ← spawn { cmd := "sh", args := #["-c", "echo hi!"] }; child.wait #eval usingIO do let child ← spawn { cmd := "sh", args := #["-c", "echo ho!"], stdout := Stdio.piped }; discard $ child.wait; child.stdout.readToEnd #eval usingIO do let child ← spawn { cmd := "head", args := #["-n1"], stdin := Stdio.piped, stdout := Stdio.piped }; child.stdin.putStrLn "hu!"; child.stdin.flush; discard $ child.wait; child.stdout.readToEnd #eval usingIO do let child ← spawn { cmd := "true", stdin := Stdio.piped }; discard $ child.wait; child.stdin.putStrLn "ha!"; child.stdin.flush <|> IO.println "flush of broken pipe failed" #eval usingIO do -- produce enough output to fill both pipes on all platforms let out ← output { cmd := "sh", args := #["-c", "printf '%100000s' >& 2; printf '%100001s'"] }; IO.println out.stdout.length; IO.println out.stderr.length #eval usingIO do -- With a non-empty stdin, cat would wait on input forever let child ← spawn { cmd := "sh", args := #["-c", "cat"], stdin := Stdio.null }; child.wait #eval usingIO do let child ← spawn { cmd := "sh", args := #["-c", "echo nullStdout"], stdout := Stdio.null }; child.wait #eval usingIO do let child ← spawn { cmd := "sh", args := #["-c", "echo nullStderr >& 2"], stderr := Stdio.null }; child.wait #eval usingIO do let lean ← IO.Process.spawn { cmd := "lean", args := #["--stdin"] stdin := IO.Process.Stdio.piped } let (stdin, lean) ← lean.takeStdin stdin.putStr "#exit\n" lean.wait
c85bd1e667119647688ea2f458e963cbedb72c4b
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/04_Quantifiers_and_Equality.org.25.lean
773e9bf91dc7ace7347d3ad58dd8197b61cb8c74
[]
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
5,174
lean
/- page 57 -/ import standard open classical variables (A : Type) (p q : A → Prop) variable r : Prop example : (∃ x : A, r) → r := λ H, exists.elim H (λ x Hr, Hr) example (a : A) : r → (∃ x : A, r) := λ Hr, exists.intro a Hr example : (∃ x, p x ∧ r) ↔ (∃ x, p x) ∧ r := iff.intro (assume H, obtain (w : A) (Hw : p w ∧ r), from H, and.intro (exists.intro w (and.left Hw)) (and.right Hw)) (assume H, obtain (w : A) (Hw : p w), from and.left H, exists.intro w (and.intro Hw (and.right H))) example : (∃ x, p x ∨ q x) ↔ (∃ x, p x) ∨ (∃ x, q x) := iff.intro (assume H, obtain (w : A) (Hw : p w ∨ q w), from H, or.elim Hw (λ Hp, or.inl (exists.intro w Hp)) (λ Hq, or.inr (exists.intro w Hq))) (assume H, or.elim H (assume Hp : Exists p, obtain w Hw, from Hp, exists.intro w (or.inl Hw)) (assume Hq : Exists q, obtain w Hw, from Hq, exists.intro w (or.inr Hw))) -- nonconstructive (←) example : (∀ x, p x) ↔ ¬ (∃ x, ¬ p x) := iff.intro (assume H H', obtain (w : A) (Hnw : ¬ p w), from H', absurd (H w) Hnw) (assume H, take x : A, by_contradiction (assume Hnx : ¬ p x, absurd (exists.intro x Hnx) H)) -- nonconstructive example : (∃ x, p x) ↔ ¬ (∀ x, ¬ p x) := iff.intro (assume H : ∃ x, p x, assume H' : ∀ x, ¬ p x, obtain (w : A) (Hpw : p w), from H, have Hnpw : ¬ p w, from H' w, absurd Hpw Hnpw) (assume H : ¬ (∀ x, ¬ p x), by_contradiction (assume H1 : ¬ Exists p, have H2 : ∀ x, ¬ p x, from take x, assume H3 : p x, have H4 : ∃ x, p x, from exists.intro x H3, show false, from H1 H4, show false, from H H2)) example : (¬ ∃ x, p x) ↔ (∀ x, ¬ p x) := iff.intro (assume H : ¬ ∃ x, p x, take x : A, assume Hpx : p x, have H' : ∃ x, p x, from exists.intro x Hpx, absurd H' H) (assume H : ∀ x, ¬ p x, assume H' : ∃ x, p x, obtain (w : A) (Pw : p w), from H', absurd Pw (H w)) -- nonconstructive (→) example : (¬ ∀ x, p x) ↔ (∃ x, ¬ p x) := iff.intro (assume H₁ : ¬ ∀ x, p x, by_contradiction (assume H₂ : ¬ ∃ x, ¬ p x, have H₃ : ∀ x, p x, from take x : A, or.elim (em (p x)) (λ Hpx, Hpx) (λ Hnpx, absurd (exists.intro x Hnpx) H₂), absurd H₃ H₁)) (assume H₁ : ∃ x, ¬ p x, assume H₂ : ∀ x, p x, obtain (w : A) (Hnpw : ¬ p w), from H₁, absurd (H₂ w) Hnpw) example : (∀ x, p x → r) ↔ (∃ x, p x) → r := iff.intro (assume H₁ : ∀ x, p x → r, assume H₂ : ∃ x, p x, obtain (w : A) (Hpw : p w), from H₂, show r, from H₁ w Hpw) (assume H : (∃ x, p x) → r, take x : A, assume Hpx : p x, show r, from H (exists.intro x Hpx)) -- nonconstructive (←) example (a : A) : (∃ x, p x → r) ↔ (∀ x, p x) → r := iff.intro (assume H₁ : ∃ x, p x → r, assume H₂ : ∀ x, p x, obtain (w : A) (Pw : p w → r), from H₁, Pw (H₂ w)) (assume H₁ : (∀ x, p x) → r, by_cases (assume H₂ : ∀ x, p x, exists.intro a (assume Hpa : p a, H₁ H₂)) (assume H₂ : ¬ ∀ x, p x, by_contradiction (assume H₃ : ¬ ∃ x, p x → r, have H₄ : ∀ x, p x, from take x, by_contradiction assume Hnpx : ¬ p x, have H₅ : ∃ x, p x → r, from exists.intro x (assume Hpx : p x, absurd Hpx Hnpx), absurd H₅ H₃, absurd H₄ H₂))) -- nonconstructive (←) example (a : A) : (∃ x, r → p x) ↔ (r → ∃ x, p x) := iff.intro (assume H : ∃ x, r → p x, assume Hr : r, obtain (x : A) (Px : r → p x), from H, exists.intro x (Px Hr)) (assume H : r → ∃ x, p x, or.elim (em r) (assume Hr : r, obtain (x : A) (Px : p x), from H Hr, exists.intro x (assume Hr, Px)) (assume Hnr : ¬ r, exists.intro a (assume Hr : r, absurd Hr Hnr)))
950340515d8eadbfb37f3129f0f0bb30d9b151ff
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/inner_product_space/euclidean_dist.lean
7cc795527ac2c2758e9c5971cf8b4945a62fa5ba
[ "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
4,969
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.inner_product_space.calculus import analysis.inner_product_space.pi_L2 /-! # Euclidean distance on a finite dimensional space > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. When we define a smooth bump function on a normed space, it is useful to have a smooth distance on the space. Since the default distance is not guaranteed to be smooth, we define `to_euclidean` to be an equivalence between a finite dimensional topological vector space and the standard Euclidean space of the same dimension. Then we define `euclidean.dist x y = dist (to_euclidean x) (to_euclidean y)` and provide some definitions (`euclidean.ball`, `euclidean.closed_ball`) and simple lemmas about this distance. This way we hide the usage of `to_euclidean` behind an API. -/ open_locale topology open set variables {E : Type*} [add_comm_group E] [topological_space E] [topological_add_group E] [t2_space E] [module ℝ E] [has_continuous_smul ℝ E] [finite_dimensional ℝ E] noncomputable theory open finite_dimensional /-- If `E` is a finite dimensional space over `ℝ`, then `to_euclidean` is a continuous `ℝ`-linear equivalence between `E` and the Euclidean space of the same dimension. -/ def to_euclidean : E ≃L[ℝ] euclidean_space ℝ (fin $ finrank ℝ E) := continuous_linear_equiv.of_finrank_eq finrank_euclidean_space_fin.symm namespace euclidean /-- If `x` and `y` are two points in a finite dimensional space over `ℝ`, then `euclidean.dist x y` is the distance between these points in the metric defined by some inner product space structure on `E`. -/ def dist (x y : E) : ℝ := dist (to_euclidean x) (to_euclidean y) /-- Closed ball w.r.t. the euclidean distance. -/ def closed_ball (x : E) (r : ℝ) : set E := {y | dist y x ≤ r} /-- Open ball w.r.t. the euclidean distance. -/ def ball (x : E) (r : ℝ) : set E := {y | dist y x < r} lemma ball_eq_preimage (x : E) (r : ℝ) : ball x r = to_euclidean ⁻¹' (metric.ball (to_euclidean x) r) := rfl lemma closed_ball_eq_preimage (x : E) (r : ℝ) : closed_ball x r = to_euclidean ⁻¹' (metric.closed_ball (to_euclidean x) r) := rfl lemma ball_subset_closed_ball {x : E} {r : ℝ} : ball x r ⊆ closed_ball x r := λ y (hy : _ < _), le_of_lt hy lemma is_open_ball {x : E} {r : ℝ} : is_open (ball x r) := metric.is_open_ball.preimage to_euclidean.continuous lemma mem_ball_self {x : E} {r : ℝ} (hr : 0 < r) : x ∈ ball x r := metric.mem_ball_self hr lemma closed_ball_eq_image (x : E) (r : ℝ) : closed_ball x r = to_euclidean.symm '' metric.closed_ball (to_euclidean x) r := by rw [to_euclidean.image_symm_eq_preimage, closed_ball_eq_preimage] lemma is_compact_closed_ball {x : E} {r : ℝ} : is_compact (closed_ball x r) := begin rw closed_ball_eq_image, exact (is_compact_closed_ball _ _).image to_euclidean.symm.continuous end lemma is_closed_closed_ball {x : E} {r : ℝ} : is_closed (closed_ball x r) := is_compact_closed_ball.is_closed lemma closure_ball (x : E) {r : ℝ} (h : r ≠ 0) : closure (ball x r) = closed_ball x r := by rw [ball_eq_preimage, ← to_euclidean.preimage_closure, closure_ball (to_euclidean x) h, closed_ball_eq_preimage] lemma exists_pos_lt_subset_ball {R : ℝ} {s : set E} {x : E} (hR : 0 < R) (hs : is_closed s) (h : s ⊆ ball x R) : ∃ r ∈ Ioo 0 R, s ⊆ ball x r := begin rw [ball_eq_preimage, ← image_subset_iff] at h, rcases exists_pos_lt_subset_ball hR (to_euclidean.is_closed_image.2 hs) h with ⟨r, hr, hsr⟩, exact ⟨r, hr, image_subset_iff.1 hsr⟩ end lemma nhds_basis_closed_ball {x : E} : (𝓝 x).has_basis (λ r : ℝ, 0 < r) (closed_ball x) := begin rw [to_euclidean.to_homeomorph.nhds_eq_comap x], exact metric.nhds_basis_closed_ball.comap _ end lemma closed_ball_mem_nhds {x : E} {r : ℝ} (hr : 0 < r) : closed_ball x r ∈ 𝓝 x := nhds_basis_closed_ball.mem_of_mem hr lemma nhds_basis_ball {x : E} : (𝓝 x).has_basis (λ r : ℝ, 0 < r) (ball x) := begin rw [to_euclidean.to_homeomorph.nhds_eq_comap x], exact metric.nhds_basis_ball.comap _ end lemma ball_mem_nhds {x : E} {r : ℝ} (hr : 0 < r) : ball x r ∈ 𝓝 x := nhds_basis_ball.mem_of_mem hr end euclidean variables {F : Type*} [normed_add_comm_group F] [normed_space ℝ F] {G : Type*} [normed_add_comm_group G] [normed_space ℝ G] [finite_dimensional ℝ G] {f g : F → G} {n : ℕ∞} lemma cont_diff.euclidean_dist (hf : cont_diff ℝ n f) (hg : cont_diff ℝ n g) (h : ∀ x, f x ≠ g x) : cont_diff ℝ n (λ x, euclidean.dist (f x) (g x)) := begin simp only [euclidean.dist], apply @cont_diff.dist ℝ, exacts [(@to_euclidean G _ _ _ _ _ _ _).cont_diff.comp hf, (@to_euclidean G _ _ _ _ _ _ _).cont_diff.comp hg, λ x, to_euclidean.injective.ne (h x)] end
49b90317a0863af8086594fd1cf32045bd4baaa2
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/cases_crash1.lean
33a29b32b84149b46ec0be086f261318e8fd56de
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
349
lean
open tactic axiom Sorry : ∀ A : Type, A inductive Enum : Type | ea | eb | ec | ed attribute [instance] noncomputable definition Enum_dec_eq : decidable_eq Enum := by do a ← intro `a, cases a, b ← intro `b, cases b, right >> reflexivity, try (left >> intro `H >>= cases), repeat $ intros >> mk_const `Sorry >>= apply
4992a9e3fc00264398510299e4ee7ce07e001596
efce24474b28579aba3272fdb77177dc2b11d7aa
/src/category_theory/simplex.lean
7949464d1fcb245cbe7275f8dd6a5b252989f0fb
[ "Apache-2.0" ]
permissive
rwbarton/lean-homotopy-theory
cff499f24268d60e1c546e7c86c33f58c62888ed
39e1b4ea1ed1b0eca2f68bc64162dde6a6396dee
refs/heads/lean-3.4.2
1,622,711,883,224
1,598,550,958,000
1,598,550,958,000
136,023,667
12
6
Apache-2.0
1,573,187,573,000
1,528,116,262,000
Lean
UTF-8
Lean
false
false
3,643
lean
import data.fin order.basic tactic.split_ifs import category_theory.base open category_theory def simplex_category := ℕ local notation `Δ` := simplex_category local notation `[`n`]` := fin (n + 1) instance : has_coe_to_sort (Δ) := {S := Type, coe := λ n, [n]} /- Nota bene: We will abuse the notation (n : Δ) to denote what a mathematician would call [n]. -/ /- This defines the set of monotone maps. The best solution is probably to rename `monotone` to `is_monotone, so that we can use `monotone` instead of the following `order_preserving_map`. -/ def order_preserving_map (m n : Δ) := {f : m → n // monotone f} instance {m n : Δ} : has_coe_to_fun (order_preserving_map m n) := { F := λ _, m → n, coe := λ f, f.val } instance : category Δ := { hom := order_preserving_map, --λ m n : Δ, {f : m → n // monotone f}, id := λ X, ⟨id, monotone_id⟩, comp := λ _ _ _ f g, ⟨g.val ∘ f.val, g.2.comp f.2⟩ } namespace simplex_category protected lemma hom_eq2 {m n : Δ} {f g : m ⟶ n} : f = g ↔ f.val = g.val := by cases f; cases g; simp variables {n : Δ} /-- The i-th face map from [n] to [n+1] -/ def δ (i : [n+1]) : n ⟶ ((n + 1) : ℕ) := ⟨λ a, if h : i.val ≤ a.val then a.succ else a.cast_succ, begin intros a b H, dsimp, split_ifs with ha hb, { show a.succ.val ≤ b.succ.val, simpa using nat.succ_le_succ H }, { exfalso, exact hb (nat.le_trans ha H) }, { show a.val ≤ b.succ.val, simpa using nat.le_trans H (nat.le_succ b) }, { exact H } end⟩ /-- The i-th degeneracy map from [n+1] to [n] -/ def σ (i : [n]) : @has_hom.hom Δ _ ((n + 1) : ℕ) n := ⟨λ a, if h : a.val ≤ i.val then ⟨a.val, lt_of_le_of_lt h i.is_lt⟩ else ⟨a.val.pred, (nat.sub_lt_right_iff_lt_add (lt_of_le_of_lt i.val.zero_le (not_le.mp h))).mpr a.is_lt⟩, begin intros a b H, dsimp, split_ifs with ha hb, { exact H }, { simp at hb, have hb' : i.val ≤ nat.pred b.val := begin rw ←nat.pred_succ i.val, exact nat.pred_le_pred hb end, exact nat.le_trans ha hb' }, { exfalso, exact ha (nat.le_trans H h) }, { exact nat.pred_le_pred H } end⟩ lemma simplicial_identity₁ {i j : [n+1]} (H : i ≤ j) : δ i ≫ δ j.succ = δ j ≫ δ i.cast_succ := begin rw simplex_category.hom_eq2, dsimp [category_struct.comp, function.comp, δ], funext a, by_cases hja : (j.val ≤ a.val), { have hja' : ((fin.succ j).val ≤ (fin.succ a).val) := by simp; exact nat.succ_le_succ hja, have hia : ((fin.cast_succ i).val ≤ (fin.succ a).val) := by simp; exact nat.le_trans H (nat.le_trans hja (nat.le_succ a.val)), erw [dif_pos hja, dif_pos (nat.le_trans H hja), dif_pos hja', dif_pos hia] }, { rw [dif_neg hja], by_cases hia : (i.val ≤ a.val), { have hia' : ((fin.cast_succ i).val ≤ (fin.cast_succ a).val) := hia, have hja' : ¬(j.succ.val ≤ a.succ.val) := by simp at *; exact nat.succ_le_succ hja, erw [dif_pos hia, dif_pos hia', dif_neg hja'], apply fin.eq_of_veq, simp }, { have hja' : ¬(j.succ.val ≤ a.cast_succ.val) := by simp at *; exact nat.le_trans hja (nat.le_succ j.val), have hia' : ¬((fin.cast_succ i).val ≤ (fin.cast_succ a).val) := by unfold fin.cast_succ; exact hia, erw [dif_neg hia, dif_neg hja', dif_neg hia'] } } end -- lemma simplicial_identity₂ {i : [n+1]} {j : [n]} (H : i ≤ j.raise) : δ i.raise ≫ σ j.succ = σ j ≫ δ i := sorry -- lemma simplicial_identity₃ -- lemma simplicial_identity₄ -- lemma simplicial_identity₅ end simplex_category
c5198dbc41804c3c4b92f61f8e7675cf723a4d51
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/moduleOf.lean
3aba1981e29a27084f85b52ea30d300a81130e7e
[ "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
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
429
lean
import Lean def f (x : Nat) := x + x open Lean def tst : MetaM Unit := do IO.println (← findModuleOf? `HAdd.hAdd) IO.println (← findModuleOf? `Lean.Core.CoreM) IO.println (← findModuleOf? `Lean.Elab.Term.elabTerm) IO.println (← findModuleOf? `Std.HashMap.insert) IO.println (← findModuleOf? `tst) IO.println (← findModuleOf? `f) IO.println (← findModuleOf? `foo) -- Error: unknown 'foo' #eval tst
0eb5ecc94838bcb91332ce511ad445d673e1bcd4
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/data/analysis/topology.lean
4223f3adaf0e2ead3d6eea1dc3a4d3867e2d6c8a
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
8,452
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Computational realization of topological spaces (experimental). -/ import topology.bases data.analysis.filter open set open filter (hiding realizer) open_locale topological_space /-- A `ctop α σ` is a realization of a topology (basis) on `α`, represented by a type `σ` together with operations for the top element and the intersection operation. -/ structure ctop (α σ : Type*) := (f : σ → set α) (top : α → σ) (top_mem : ∀ x : α, x ∈ f (top x)) (inter : Π a b (x : α), x ∈ f a ∩ f b → σ) (inter_mem : ∀ a b x h, x ∈ f (inter a b x h)) (inter_sub : ∀ a b x h, f (inter a b x h) ⊆ f a ∩ f b) variables {α : Type*} {β : Type*} {σ : Type*} {τ : Type*} namespace ctop section variables (F : ctop α σ) instance : has_coe_to_fun (ctop α σ) := ⟨_, ctop.f⟩ @[simp] theorem coe_mk (f T h₁ I h₂ h₃ a) : (@ctop.mk α σ f T h₁ I h₂ h₃) a = f a := rfl /-- Map a ctop to an equivalent representation type. -/ def of_equiv (E : σ ≃ τ) : ctop α σ → ctop α τ | ⟨f, T, h₁, I, h₂, h₃⟩ := { f := λ a, f (E.symm a), top := λ x, E (T x), top_mem := λ x, by simpa using h₁ x, inter := λ a b x h, E (I (E.symm a) (E.symm b) x h), inter_mem := λ a b x h, by simpa using h₂ (E.symm a) (E.symm b) x h, inter_sub := λ a b x h, by simpa using h₃ (E.symm a) (E.symm b) x h } @[simp] theorem of_equiv_val (E : σ ≃ τ) (F : ctop α σ) (a : τ) : F.of_equiv E a = F (E.symm a) := by cases F; refl end /-- Every `ctop` is a topological space. -/ def to_topsp (F : ctop α σ) : topological_space α := topological_space.generate_from (set.range F.f) theorem to_topsp_is_topological_basis (F : ctop α σ) : @topological_space.is_topological_basis _ F.to_topsp (set.range F.f) := ⟨λ u ⟨a, e₁⟩ v ⟨b, e₂⟩, e₁ ▸ e₂ ▸ λ x h, ⟨_, ⟨_, rfl⟩, F.inter_mem a b x h, F.inter_sub a b x h⟩, eq_univ_iff_forall.2 $ λ x, ⟨_, ⟨_, rfl⟩, F.top_mem x⟩, rfl⟩ @[simp] theorem mem_nhds_to_topsp (F : ctop α σ) {s : set α} {a : α} : s ∈ @nhds _ F.to_topsp a ↔ ∃ b, a ∈ F b ∧ F b ⊆ s := (@topological_space.mem_nhds_of_is_topological_basis _ F.to_topsp _ _ _ F.to_topsp_is_topological_basis).trans $ ⟨λ ⟨_, ⟨x, rfl⟩, h⟩, ⟨x, h⟩, λ ⟨x, h⟩, ⟨_, ⟨x, rfl⟩, h⟩⟩ end ctop /-- A `ctop` realizer for the topological space `T` is a `ctop` which generates `T`. -/ structure ctop.realizer (α) [T : topological_space α] := (σ : Type*) (F : ctop α σ) (eq : F.to_topsp = T) open ctop protected def ctop.to_realizer (F : ctop α σ) : @ctop.realizer _ F.to_topsp := @ctop.realizer.mk _ F.to_topsp σ F rfl namespace ctop.realizer protected theorem is_basis [T : topological_space α] (F : realizer α) : topological_space.is_topological_basis (set.range F.F.f) := by have := to_topsp_is_topological_basis F.F; rwa F.eq at this protected theorem mem_nhds [T : topological_space α] (F : realizer α) {s : set α} {a : α} : s ∈ 𝓝 a ↔ ∃ b, a ∈ F.F b ∧ F.F b ⊆ s := by have := mem_nhds_to_topsp F.F; rwa F.eq at this theorem is_open_iff [topological_space α] (F : realizer α) {s : set α} : is_open s ↔ ∀ a ∈ s, ∃ b, a ∈ F.F b ∧ F.F b ⊆ s := is_open_iff_mem_nhds.trans $ ball_congr $ λ a h, F.mem_nhds theorem is_closed_iff [topological_space α] (F : realizer α) {s : set α} : is_closed s ↔ ∀ a, (∀ b, a ∈ F.F b → ∃ z, z ∈ F.F b ∩ s) → a ∈ s := F.is_open_iff.trans $ forall_congr $ λ a, show (a ∉ s → (∃ (b : F.σ), a ∈ F.F b ∧ ∀ z ∈ F.F b, z ∉ s)) ↔ _, by haveI := classical.prop_decidable; rw [not_imp_comm]; simp [not_exists, not_and, not_forall, and_comm] theorem mem_interior_iff [topological_space α] (F : realizer α) {s : set α} {a : α} : a ∈ interior s ↔ ∃ b, a ∈ F.F b ∧ F.F b ⊆ s := mem_interior_iff_mem_nhds.trans F.mem_nhds protected theorem is_open [topological_space α] (F : realizer α) (s : F.σ) : is_open (F.F s) := is_open_iff_nhds.2 $ λ a m, by simpa using F.mem_nhds.2 ⟨s, m, subset.refl _⟩ theorem ext' [T : topological_space α] {σ : Type*} {F : ctop α σ} (H : ∀ a s, s ∈ 𝓝 a ↔ ∃ b, a ∈ F b ∧ F b ⊆ s) : F.to_topsp = T := topological_space_eq $ funext $ λ s, begin have : ∀ T s, @topological_space.is_open _ T s ↔ _ := @is_open_iff_mem_nhds α, rw [this, this], apply congr_arg (λ f : α → filter α, ∀ a ∈ s, s ∈ f a), funext a, apply filter_eq, apply set.ext, intro x, rw [mem_nhds_to_topsp, H] end theorem ext [T : topological_space α] {σ : Type*} {F : ctop α σ} (H₁ : ∀ a, is_open (F a)) (H₂ : ∀ a s, s ∈ 𝓝 a → ∃ b, a ∈ F b ∧ F b ⊆ s) : F.to_topsp = T := ext' $ λ a s, ⟨H₂ a s, λ ⟨b, h₁, h₂⟩, mem_nhds_sets_iff.2 ⟨_, h₂, H₁ _, h₁⟩⟩ variable [topological_space α] protected def id : realizer α := ⟨{x:set α // is_open x}, { f := subtype.val, top := λ _, ⟨univ, is_open_univ⟩, top_mem := mem_univ, inter := λ ⟨x, h₁⟩ ⟨y, h₂⟩ a h₃, ⟨_, is_open_inter h₁ h₂⟩, inter_mem := λ ⟨x, h₁⟩ ⟨y, h₂⟩ a, id, inter_sub := λ ⟨x, h₁⟩ ⟨y, h₂⟩ a h₃, subset.refl _ }, ext subtype.property $ λ x s h, let ⟨t, h, o, m⟩ := mem_nhds_sets_iff.1 h in ⟨⟨t, o⟩, m, h⟩⟩ def of_equiv (F : realizer α) (E : F.σ ≃ τ) : realizer α := ⟨τ, F.F.of_equiv E, ext' (λ a s, F.mem_nhds.trans $ ⟨λ ⟨s, h⟩, ⟨E s, by simpa using h⟩, λ ⟨t, h⟩, ⟨E.symm t, by simpa using h⟩⟩)⟩ @[simp] theorem of_equiv_σ (F : realizer α) (E : F.σ ≃ τ) : (F.of_equiv E).σ = τ := rfl @[simp] theorem of_equiv_F (F : realizer α) (E : F.σ ≃ τ) (s : τ) : (F.of_equiv E).F s = F.F (E.symm s) := by delta of_equiv; simp protected def nhds (F : realizer α) (a : α) : (𝓝 a).realizer := ⟨{s : F.σ // a ∈ F.F s}, { f := λ s, F.F s.1, pt := ⟨_, F.F.top_mem a⟩, inf := λ ⟨x, h₁⟩ ⟨y, h₂⟩, ⟨_, F.F.inter_mem x y a ⟨h₁, h₂⟩⟩, inf_le_left := λ ⟨x, h₁⟩ ⟨y, h₂⟩ z h, (F.F.inter_sub x y a ⟨h₁, h₂⟩ h).1, inf_le_right := λ ⟨x, h₁⟩ ⟨y, h₂⟩ z h, (F.F.inter_sub x y a ⟨h₁, h₂⟩ h).2 }, filter_eq $ set.ext $ λ x, ⟨λ ⟨⟨s, as⟩, h⟩, mem_nhds_sets_iff.2 ⟨_, h, F.is_open _, as⟩, λ h, let ⟨s, h, as⟩ := F.mem_nhds.1 h in ⟨⟨s, h⟩, as⟩⟩⟩ @[simp] theorem nhds_σ (m : α → β) (F : realizer α) (a : α) : (F.nhds a).σ = {s : F.σ // a ∈ F.F s} := rfl @[simp] theorem nhds_F (m : α → β) (F : realizer α) (a : α) (s) : (F.nhds a).F s = F.F s.1 := rfl theorem tendsto_nhds_iff {m : β → α} {f : filter β} (F : f.realizer) (R : realizer α) {a : α} : tendsto m f (𝓝 a) ↔ ∀ t, a ∈ R.F t → ∃ s, ∀ x ∈ F.F s, m x ∈ R.F t := (F.tendsto_iff _ (R.nhds a)).trans subtype.forall end ctop.realizer structure locally_finite.realizer [topological_space α] (F : realizer α) (f : β → set α) := (bas : ∀ a, {s // a ∈ F.F s}) (sets : ∀ x:α, fintype {i | (f i ∩ F.F (bas x)).nonempty}) theorem locally_finite.realizer.to_locally_finite [topological_space α] {F : realizer α} {f : β → set α} (R : locally_finite.realizer F f) : locally_finite f := λ a, ⟨_, F.mem_nhds.2 ⟨(R.bas a).1, (R.bas a).2, subset.refl _⟩, ⟨R.sets a⟩⟩ theorem locally_finite_iff_exists_realizer [topological_space α] (F : realizer α) {f : β → set α} : locally_finite f ↔ nonempty (locally_finite.realizer F f) := ⟨λ h, let ⟨g, h₁⟩ := classical.axiom_of_choice h, ⟨g₂, h₂⟩ := classical.axiom_of_choice (λ x, show ∃ (b : F.σ), x ∈ (F.F) b ∧ (F.F) b ⊆ g x, from let ⟨h, h'⟩ := h₁ x in F.mem_nhds.1 h) in ⟨⟨λ x, ⟨g₂ x, (h₂ x).1⟩, λ x, finite.fintype $ let ⟨h, h'⟩ := h₁ x in finite_subset h' $ λ i hi, hi.mono (inter_subset_inter_right _ (h₂ x).2)⟩⟩, λ ⟨R⟩, R.to_locally_finite⟩ def compact.realizer [topological_space α] (R : realizer α) (s : set α) := ∀ {f : filter α} (F : f.realizer) (x : F.σ), f ≠ ⊥ → F.F x ⊆ s → {a // a∈s ∧ 𝓝 a ⊓ f ≠ ⊥}
6234240cf58f3c082d8775e9d857b2418ad736d4
4a092885406df4e441e9bb9065d9405dacb94cd8
/src/adic_space.lean
e4fa5af62357834e9a698959f458517a1de7a83f
[ "Apache-2.0" ]
permissive
semorrison/lean-perfectoid-spaces
78c1572cedbfae9c3e460d8aaf91de38616904d8
bb4311dff45791170bcb1b6a983e2591bee88a19
refs/heads/master
1,588,841,765,494
1,554,805,620,000
1,554,805,620,000
180,353,546
0
1
null
1,554,809,880,000
1,554,809,880,000
null
UTF-8
Lean
false
false
2,207
lean
import data.nat.prime import algebra.group_power import topology.algebra.ring import topology.opens import for_mathlib.prime import for_mathlib.is_cover import continuous_valuations import Spa import Huber_pair /- An adic space is... Vpre: p76 -/ universe u open nat function open topological_space structure 𝓥pre (X : Type*) [topological_space X] -- := --(𝓞X : presheaf of rings) --(complete : 𝓞X U is a complete topological ring) --(local : stalks are local) --(val : valuation on each stalk with support the max ideal) /- We denote by 𝓥pre the category of tuples X = (X, O X , (v x ) x∈X ), where (a) X is a topological space, (b) 𝓞_X is a presheaf of complete topological rings on X such that the stalk 𝓞_X,x of 𝓞_X (considered as a presheaf of rings) is a local ring, (c) v_x is an equivalence class of valuations on the stalk 𝓞_X,x such that supp(v_x) is the maximal ideal of 𝓞_X,x . Wedhorn p76 shows how Spa(A) gives an object of this for A a Huber pair -/ --definition affinoid_adic_space (A : Huber_pair) : 𝓥pre := sorry -- unwritten -- it's a full subcat of 𝓥pre class preadic_space (X : Type*) extends topological_space X -- not logically necessary but should be easy instance (A : Huber_pair) : preadic_space (Spa A) := sorry -- attribute [class] _root_.is_open instance preadic_space_restriction {X : Type*} [preadic_space X] {U : opens X} : preadic_space U := sorry -- unwritten class adic_space (X : Type*) extends preadic_space X -- note Wedhorn remark 8.19; being a sheaf of top rings involves a topological condition -- a preadic_space_equiv is just an isom in 𝓥pre, or an isomorphism of preadic spaces. -- unwritten structure preadic_space_equiv (X Y : Type*) [AX : preadic_space X] [AY : preadic_space Y] extends equiv X Y definition is_preadic_space_equiv (X Y : Type*) [AX : preadic_space X] [AY : preadic_space Y] := nonempty (preadic_space_equiv X Y) definition preadic_space_pullback {X : Type*} [preadic_space X] (U : set X) := {x : X // x ∈ U} instance pullback_is_preadic_space {X : Type*} [preadic_space X] (U : set X) : preadic_space (preadic_space_pullback U) := sorry -- notation `is_open` := _root_.is_open
7af806f19888b354b50f82e5fa5a8fb114f15a96
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/analysis/normed_space/finite_dimension.lean
2ad9470056959d33b3de0033e688eae636c4d23a
[ "Apache-2.0" ]
permissive
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
22,000
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.normed_space.operator_norm import analysis.normed_space.add_torsor import topology.bases import linear_algebra.finite_dimensional import tactic.omega /-! # Finite dimensional normed spaces over complete fields Over a complete nondiscrete field, in finite dimension, all norms are equivalent and all linear maps are continuous. Moreover, a finite-dimensional subspace is always complete and closed. ## Main results: * `linear_map.continuous_of_finite_dimensional` : a linear map on a finite-dimensional space over a complete field is continuous. * `finite_dimensional.complete` : a finite-dimensional space over a complete field is complete. This is not registered as an instance, as the field would be an unknown metavariable in typeclass resolution. * `submodule.closed_of_finite_dimensional` : a finite-dimensional subspace over a complete field is closed * `finite_dimensional.proper` : a finite-dimensional space over a proper field is proper. This is not registered as an instance, as the field would be an unknown metavariable in typeclass resolution. It is however registered as an instance for `𝕜 = ℝ` and `𝕜 = ℂ`. As properness implies completeness, there is no need to also register `finite_dimensional.complete` on `ℝ` or `ℂ`. ## Implementation notes The fact that all norms are equivalent is not written explicitly, as it would mean having two norms on a single space, which is not the way type classes work. However, if one has a finite-dimensional vector space `E` with a norm, and a copy `E'` of this type with another norm, then the identities from `E` to `E'` and from `E'`to `E` are continuous thanks to `linear_map.continuous_of_finite_dimensional`. This gives the desired norm equivalence. -/ universes u v w x open set finite_dimensional topological_space open_locale classical big_operators noncomputable theory /-- A linear map on `ι → 𝕜` (where `ι` is a fintype) is continuous -/ lemma linear_map.continuous_on_pi {ι : Type w} [fintype ι] {𝕜 : Type u} [normed_field 𝕜] {E : Type v} [add_comm_group E] [vector_space 𝕜 E] [topological_space E] [topological_add_group E] [topological_vector_space 𝕜 E] (f : (ι → 𝕜) →ₗ[𝕜] E) : continuous f := begin -- for the proof, write `f` in the standard basis, and use that each coordinate is a continuous -- function. have : (f : (ι → 𝕜) → E) = (λx, ∑ i : ι, x i • (f (λj, if i = j then 1 else 0))), by { ext x, exact f.pi_apply_eq_sum_univ x }, rw this, refine continuous_finset_sum _ (λi hi, _), exact (continuous_apply i).smul continuous_const end section complete_field variables {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {E : Type v} [normed_group E] [normed_space 𝕜 E] {F : Type w} [normed_group F] [normed_space 𝕜 F] {F' : Type x} [add_comm_group F'] [vector_space 𝕜 F'] [topological_space F'] [topological_add_group F'] [topological_vector_space 𝕜 F'] [complete_space 𝕜] /-- In finite dimension over a complete field, the canonical identification (in terms of a basis) with `𝕜^n` together with its sup norm is continuous. This is the nontrivial part in the fact that all norms are equivalent in finite dimension. This statement is superceded by the fact that every linear map on a finite-dimensional space is continuous, in `linear_map.continuous_of_finite_dimensional`. -/ lemma continuous_equiv_fun_basis {ι : Type v} [fintype ι] (ξ : ι → E) (hξ : is_basis 𝕜 ξ) : continuous hξ.equiv_fun := begin unfreezingI { induction hn : fintype.card ι with n IH generalizing ι E }, { apply linear_map.continuous_of_bound _ 0 (λx, _), have : hξ.equiv_fun x = 0, by { ext i, exact (fintype.card_eq_zero_iff.1 hn i).elim }, change ∥hξ.equiv_fun x∥ ≤ 0 * ∥x∥, rw this, simp [norm_nonneg] }, { haveI : finite_dimensional 𝕜 E := of_fintype_basis hξ, -- first step: thanks to the inductive assumption, any n-dimensional subspace is equivalent -- to a standard space of dimension n, hence it is complete and therefore closed. have H₁ : ∀s : submodule 𝕜 E, findim 𝕜 s = n → is_closed (s : set E), { assume s s_dim, rcases exists_is_basis_finite 𝕜 s with ⟨b, b_basis, b_finite⟩, letI : fintype b := finite.fintype b_finite, have U : uniform_embedding b_basis.equiv_fun.symm.to_equiv, { have : fintype.card b = n, by { rw ← s_dim, exact (findim_eq_card_basis b_basis).symm }, have : continuous b_basis.equiv_fun := IH (subtype.val : b → s) b_basis this, exact b_basis.equiv_fun.symm.uniform_embedding (linear_map.continuous_on_pi _) this }, have : is_complete (s : set E), from complete_space_coe_iff_is_complete.1 ((complete_space_congr U).1 (by apply_instance)), exact this.is_closed }, -- second step: any linear form is continuous, as its kernel is closed by the first step have H₂ : ∀f : E →ₗ[𝕜] 𝕜, continuous f, { assume f, have : findim 𝕜 f.ker = n ∨ findim 𝕜 f.ker = n.succ, { have Z := f.findim_range_add_findim_ker, rw [findim_eq_card_basis hξ, hn] at Z, have : findim 𝕜 f.range = 0 ∨ findim 𝕜 f.range = 1, { have I : ∀(k : ℕ), k ≤ 1 ↔ k = 0 ∨ k = 1, by omega manual, have : findim 𝕜 f.range ≤ findim 𝕜 𝕜 := submodule.findim_le _, rwa [findim_of_field, I] at this }, cases this, { rw this at Z, right, simpa using Z }, { left, rw [this, add_comm, nat.add_one] at Z, exact nat.succ.inj Z } }, have : is_closed (f.ker : set E), { cases this, { exact H₁ _ this }, { have : f.ker = ⊤, by { apply eq_top_of_findim_eq, rw [findim_eq_card_basis hξ, hn, this] }, simp [this] } }, exact linear_map.continuous_iff_is_closed_ker.2 this }, -- third step: applying the continuity to the linear form corresponding to a coefficient in the -- basis decomposition, deduce that all such coefficients are controlled in terms of the norm have : ∀i:ι, ∃C, 0 ≤ C ∧ ∀(x:E), ∥hξ.equiv_fun x i∥ ≤ C * ∥x∥, { assume i, let f : E →ₗ[𝕜] 𝕜 := (linear_map.proj i).comp hξ.equiv_fun, let f' : E →L[𝕜] 𝕜 := { cont := H₂ f, ..f }, exact ⟨∥f'∥, norm_nonneg _, λx, continuous_linear_map.le_op_norm f' x⟩ }, -- fourth step: combine the bound on each coefficient to get a global bound and the continuity choose C0 hC0 using this, let C := ∑ i, C0 i, have C_nonneg : 0 ≤ C := finset.sum_nonneg (λi hi, (hC0 i).1), have C0_le : ∀i, C0 i ≤ C := λi, finset.single_le_sum (λj hj, (hC0 j).1) (finset.mem_univ _), apply linear_map.continuous_of_bound _ C (λx, _), rw pi_norm_le_iff, { exact λi, le_trans ((hC0 i).2 x) (mul_le_mul_of_nonneg_right (C0_le i) (norm_nonneg _)) }, { exact mul_nonneg C_nonneg (norm_nonneg _) } } end /-- Any linear map on a finite dimensional space over a complete field is continuous. -/ theorem linear_map.continuous_of_finite_dimensional [finite_dimensional 𝕜 E] (f : E →ₗ[𝕜] F') : continuous f := begin -- for the proof, go to a model vector space `b → 𝕜` thanks to `continuous_equiv_fun_basis`, and -- argue that all linear maps there are continuous. rcases exists_is_basis_finite 𝕜 E with ⟨b, b_basis, b_finite⟩, letI : fintype b := finite.fintype b_finite, have A : continuous b_basis.equiv_fun := continuous_equiv_fun_basis _ b_basis, have B : continuous (f.comp (b_basis.equiv_fun.symm : (b → 𝕜) →ₗ[𝕜] E)) := linear_map.continuous_on_pi _, have : continuous ((f.comp (b_basis.equiv_fun.symm : (b → 𝕜) →ₗ[𝕜] E)) ∘ b_basis.equiv_fun) := B.comp A, convert this, ext x, dsimp, rw linear_equiv.symm_apply_apply end theorem affine_map.continuous_of_finite_dimensional {PE PF : Type*} [metric_space PE] [normed_add_torsor E PE] [metric_space PF] [normed_add_torsor F PF] [finite_dimensional 𝕜 E] (f : PE →ᵃ[𝕜] PF) : continuous f := affine_map.continuous_linear_iff.1 f.linear.continuous_of_finite_dimensional /-- The continuous linear map induced by a linear map on a finite dimensional space -/ def linear_map.to_continuous_linear_map [finite_dimensional 𝕜 E] (f : E →ₗ[𝕜] F') : E →L[𝕜] F' := { cont := f.continuous_of_finite_dimensional, ..f } /-- The continuous linear equivalence induced by a linear equivalence on a finite dimensional space. -/ def linear_equiv.to_continuous_linear_equiv [finite_dimensional 𝕜 E] (e : E ≃ₗ[𝕜] F) : E ≃L[𝕜] F := { continuous_to_fun := e.to_linear_map.continuous_of_finite_dimensional, continuous_inv_fun := begin haveI : finite_dimensional 𝕜 F := e.finite_dimensional, exact e.symm.to_linear_map.continuous_of_finite_dimensional end, ..e } /-- Two finite-dimensional normed spaces are continuously linearly equivalent if they have the same (finite) dimension. -/ theorem finite_dimensional.nonempty_continuous_linear_equiv_of_findim_eq [finite_dimensional 𝕜 E] [finite_dimensional 𝕜 F] (cond : findim 𝕜 E = findim 𝕜 F) : nonempty (E ≃L[𝕜] F) := (nonempty_linear_equiv_of_findim_eq cond).map linear_equiv.to_continuous_linear_equiv /-- Two finite-dimensional normed spaces are continuously linearly equivalent if and only if they have the same (finite) dimension. -/ theorem finite_dimensional.nonempty_continuous_linear_equiv_iff_findim_eq [finite_dimensional 𝕜 E] [finite_dimensional 𝕜 F] : nonempty (E ≃L[𝕜] F) ↔ findim 𝕜 E = findim 𝕜 F := ⟨ λ ⟨h⟩, h.to_linear_equiv.findim_eq, λ h, finite_dimensional.nonempty_continuous_linear_equiv_of_findim_eq h ⟩ /-- A continuous linear equivalence between two finite-dimensional normed spaces of the same (finite) dimension. -/ def continuous_linear_equiv.of_findim_eq [finite_dimensional 𝕜 E] [finite_dimensional 𝕜 F] (cond : findim 𝕜 E = findim 𝕜 F) : E ≃L[𝕜] F := (linear_equiv.of_findim_eq E F cond).to_continuous_linear_equiv variables {ι : Type*} [fintype ι] /-- Construct a continuous linear map given the value at a finite basis. -/ def is_basis.constrL {v : ι → E} (hv : is_basis 𝕜 v) (f : ι → F) : E →L[𝕜] F := by haveI : finite_dimensional 𝕜 E := finite_dimensional.of_fintype_basis hv; exact (hv.constr f).to_continuous_linear_map @[simp, norm_cast] lemma is_basis.coe_constrL {v : ι → E} (hv : is_basis 𝕜 v) (f : ι → F) : (hv.constrL f : E →ₗ[𝕜] F) = hv.constr f := rfl /-- The continuous linear equivalence between a vector space over `𝕜` with a finite basis and functions from its basis indexing type to `𝕜`. -/ def is_basis.equiv_funL {v : ι → E} (hv : is_basis 𝕜 v) : E ≃L[𝕜] (ι → 𝕜) := { continuous_to_fun := begin haveI : finite_dimensional 𝕜 E := finite_dimensional.of_fintype_basis hv, apply linear_map.continuous_of_finite_dimensional, end, continuous_inv_fun := begin change continuous hv.equiv_fun.symm.to_fun, apply linear_map.continuous_of_finite_dimensional, end, ..hv.equiv_fun } @[simp] lemma is_basis.constrL_apply {v : ι → E} (hv : is_basis 𝕜 v) (f : ι → F) (e : E) : (hv.constrL f) e = ∑ i, (hv.equiv_fun e i) • f i := hv.constr_apply_fintype _ _ @[simp] lemma is_basis.constrL_basis {v : ι → E} (hv : is_basis 𝕜 v) (f : ι → F) (i : ι) : (hv.constrL f) (v i) = f i := constr_basis _ lemma is_basis.sup_norm_le_norm {v : ι → E} (hv : is_basis 𝕜 v) : ∃ C > (0 : ℝ), ∀ e : E, ∑ i, ∥hv.equiv_fun e i∥ ≤ C * ∥e∥ := begin set φ := hv.equiv_funL.to_continuous_linear_map, set C := ∥φ∥ * (fintype.card ι), use [max C 1, lt_of_lt_of_le (zero_lt_one) (le_max_right C 1)], intros e, calc ∑ i, ∥φ e i∥ ≤ ∑ i : ι, ∥φ e∥ : by { apply finset.sum_le_sum, exact λ i hi, norm_le_pi_norm (φ e) i } ... = ∥φ e∥*(fintype.card ι) : by simpa only [mul_comm, finset.sum_const, nsmul_eq_mul] ... ≤ ∥φ∥ * ∥e∥ * (fintype.card ι) : mul_le_mul_of_nonneg_right (φ.le_op_norm e) (fintype.card ι).cast_nonneg ... = ∥φ∥ * (fintype.card ι) * ∥e∥ : by ring ... ≤ max C 1 * ∥e∥ : mul_le_mul_of_nonneg_right (le_max_left _ _) (norm_nonneg _) end lemma is_basis.op_norm_le {ι : Type*} [fintype ι] {v : ι → E} (hv : is_basis 𝕜 v) : ∃ C > (0 : ℝ), ∀ {u : E →L[𝕜] F} {M : ℝ}, 0 ≤ M → (∀ i, ∥u (v i)∥ ≤ M) → ∥u∥ ≤ C*M := begin obtain ⟨C, C_pos, hC⟩ : ∃ C > (0 : ℝ), ∀ (e : E), ∑ i, ∥hv.equiv_fun e i∥ ≤ C * ∥e∥, from hv.sup_norm_le_norm, use [C, C_pos], intros u M hM hu, apply u.op_norm_le_bound (mul_nonneg (le_of_lt C_pos) hM), intros e, calc ∥u e∥ = ∥u (∑ i, hv.equiv_fun e i • v i)∥ : by conv_lhs { rw ← hv.equiv_fun_total e } ... = ∥∑ i, (hv.equiv_fun e i) • (u $ v i)∥ : by simp [u.map_sum, linear_map.map_smul] ... ≤ ∑ i, ∥(hv.equiv_fun e i) • (u $ v i)∥ : norm_sum_le _ _ ... = ∑ i, ∥hv.equiv_fun e i∥ * ∥u (v i)∥ : by simp only [norm_smul] ... ≤ ∑ i, ∥hv.equiv_fun e i∥ * M : finset.sum_le_sum (λ i hi, mul_le_mul_of_nonneg_left (hu i) (norm_nonneg _)) ... = (∑ i, ∥hv.equiv_fun e i∥) * M : finset.sum_mul.symm ... ≤ C * ∥e∥ * M : mul_le_mul_of_nonneg_right (hC e) hM ... = C * M * ∥e∥ : by ring end instance [finite_dimensional 𝕜 E] [second_countable_topology F] : second_countable_topology (E →L[𝕜] F) := begin set d := finite_dimensional.findim 𝕜 E, suffices : ∀ ε > (0 : ℝ), ∃ n : (E →L[𝕜] F) → fin d → ℕ, ∀ (f g : E →L[𝕜] F), n f = n g → dist f g ≤ ε, from metric.second_countable_of_countable_discretization (λ ε ε_pos, ⟨fin d → ℕ, by apply_instance, this ε ε_pos⟩), intros ε ε_pos, obtain ⟨u : ℕ → F, hu : dense_range u⟩ := exists_dense_seq F, obtain ⟨v : fin d → E, hv : is_basis 𝕜 v⟩ := finite_dimensional.fin_basis 𝕜 E, obtain ⟨C : ℝ, C_pos : 0 < C, hC : ∀ {φ : E →L[𝕜] F} {M : ℝ}, 0 ≤ M → (∀ i, ∥φ (v i)∥ ≤ M) → ∥φ∥ ≤ C * M⟩ := hv.op_norm_le, have h_2C : 0 < 2*C := mul_pos zero_lt_two C_pos, have hε2C : 0 < ε/(2*C) := div_pos ε_pos h_2C, have : ∀ φ : E →L[𝕜] F, ∃ n : fin d → ℕ, ∥φ - (hv.constrL $ u ∘ n)∥ ≤ ε/2, { intros φ, have : ∀ i, ∃ n, ∥φ (v i) - u n∥ ≤ ε/(2*C), { simp only [norm_sub_rev], intro i, have : φ (v i) ∈ closure (range u) := hu _, obtain ⟨n, hn⟩ : ∃ n, ∥u n - φ (v i)∥ < ε / (2 * C), { rw mem_closure_iff_nhds_basis metric.nhds_basis_ball at this, specialize this (ε/(2*C)) hε2C, simpa [dist_eq_norm] }, exact ⟨n, le_of_lt hn⟩ }, choose n hn using this, use n, replace hn : ∀ i : fin d, ∥(φ - (hv.constrL $ u ∘ n)) (v i)∥ ≤ ε / (2 * C), by simp [hn], have : C * (ε / (2 * C)) = ε/2, { rw [eq_div_iff (two_ne_zero : (2 : ℝ) ≠ 0), mul_comm, ← mul_assoc, mul_div_cancel' _ (ne_of_gt h_2C)] }, specialize hC (le_of_lt hε2C) hn, rwa this at hC }, choose n hn using this, set Φ := λ φ : E →L[𝕜] F, (hv.constrL $ u ∘ (n φ)), change ∀ z, dist z (Φ z) ≤ ε/2 at hn, use n, intros x y hxy, calc dist x y ≤ dist x (Φ x) + dist (Φ x) y : dist_triangle _ _ _ ... = dist x (Φ x) + dist y (Φ y) : by simp [Φ, hxy, dist_comm] ... ≤ ε : by linarith [hn x, hn y] end /-- Any finite-dimensional vector space over a complete field is complete. We do not register this as an instance to avoid an instance loop when trying to prove the completeness of `𝕜`, and the search for `𝕜` as an unknown metavariable. Declare the instance explicitly when needed. -/ variables (𝕜 E) lemma finite_dimensional.complete [finite_dimensional 𝕜 E] : complete_space E := begin rcases exists_is_basis_finite 𝕜 E with ⟨b, b_basis, b_finite⟩, letI : fintype b := finite.fintype b_finite, have : uniform_embedding b_basis.equiv_fun.symm := linear_equiv.uniform_embedding _ (linear_map.continuous_of_finite_dimensional _) (linear_map.continuous_of_finite_dimensional _), change uniform_embedding b_basis.equiv_fun.symm.to_equiv at this, exact (complete_space_congr this).1 (by apply_instance) end variables {𝕜 E} /-- A finite-dimensional subspace is complete. -/ lemma submodule.complete_of_finite_dimensional (s : submodule 𝕜 E) [finite_dimensional 𝕜 s] : is_complete (s : set E) := complete_space_coe_iff_is_complete.1 (finite_dimensional.complete 𝕜 s) /-- A finite-dimensional subspace is closed. -/ lemma submodule.closed_of_finite_dimensional (s : submodule 𝕜 E) [finite_dimensional 𝕜 s] : is_closed (s : set E) := s.complete_of_finite_dimensional.is_closed lemma continuous_linear_map.exists_right_inverse_of_surjective [finite_dimensional 𝕜 F] (f : E →L[𝕜] F) (hf : f.range = ⊤) : ∃ g : F →L[𝕜] E, f.comp g = continuous_linear_map.id 𝕜 F := let ⟨g, hg⟩ := (f : E →ₗ[𝕜] F).exists_right_inverse_of_surjective hf in ⟨g.to_continuous_linear_map, continuous_linear_map.ext $ linear_map.ext_iff.1 hg⟩ lemma closed_embedding_smul_left {c : E} (hc : c ≠ 0) : closed_embedding (λ x : 𝕜, x • c) := begin haveI : finite_dimensional 𝕜 (submodule.span 𝕜 {c}) := finite_dimensional.span_of_finite 𝕜 (finite_singleton c), have m1 : closed_embedding (coe : submodule.span 𝕜 {c} → E) := (submodule.span 𝕜 {c}).closed_of_finite_dimensional.closed_embedding_subtype_coe, have m2 : closed_embedding (linear_equiv.to_span_nonzero_singleton 𝕜 E c hc : 𝕜 → submodule.span 𝕜 {c}) := (continuous_linear_equiv.to_span_nonzero_singleton 𝕜 c hc).to_homeomorph.closed_embedding, exact m1.comp m2 end /- `smul` is a closed map in the first argument. -/ lemma is_closed_map_smul_left (c : E) : is_closed_map (λ x : 𝕜, x • c) := begin by_cases hc : c = 0, { simp_rw [hc, smul_zero], exact is_closed_map_const }, { exact (closed_embedding_smul_left hc).is_closed_map } end end complete_field section proper_field variables (𝕜 : Type u) [nondiscrete_normed_field 𝕜] (E : Type v) [normed_group E] [normed_space 𝕜 E] [proper_space 𝕜] /-- Any finite-dimensional vector space over a proper field is proper. We do not register this as an instance to avoid an instance loop when trying to prove the properness of `𝕜`, and the search for `𝕜` as an unknown metavariable. Declare the instance explicitly when needed. -/ lemma finite_dimensional.proper [finite_dimensional 𝕜 E] : proper_space E := begin rcases exists_is_basis_finite 𝕜 E with ⟨b, b_basis, b_finite⟩, letI : fintype b := finite.fintype b_finite, let e := b_basis.equiv_fun, let f : E →L[𝕜] (b → 𝕜) := { cont := linear_map.continuous_of_finite_dimensional _, ..e.to_linear_map }, refine metric.proper_image_of_proper e.symm (linear_map.continuous_of_finite_dimensional _) _ (∥f∥) (λx y, _), { exact equiv.range_eq_univ e.symm.to_equiv }, { have A : e (e.symm x) = x := linear_equiv.apply_symm_apply _ _, have B : e (e.symm y) = y := linear_equiv.apply_symm_apply _ _, conv_lhs { rw [← A, ← B] }, change dist (f (e.symm x)) (f (e.symm y)) ≤ ∥f∥ * dist (e.symm x) (e.symm y), unfreezingI { exact f.lipschitz.dist_le_mul _ _ } } end end proper_field /- Over the real numbers, we can register the previous statement as an instance as it will not cause problems in instance resolution since the properness of `ℝ` is already known. -/ instance finite_dimensional.proper_real (E : Type u) [normed_group E] [normed_space ℝ E] [finite_dimensional ℝ E] : proper_space E := finite_dimensional.proper ℝ E attribute [instance, priority 900] finite_dimensional.proper_real /-- In a finite dimensional vector space over `ℝ`, the series `∑ x, ∥f x∥` is unconditionally summable if and only if the series `∑ x, f x` is unconditionally summable. One implication holds in any complete normed space, while the other holds only in finite dimensional spaces. -/ lemma summable_norm_iff {α E : Type*} [normed_group E] [normed_space ℝ E] [finite_dimensional ℝ E] {f : α → E} : summable (λ x, ∥f x∥) ↔ summable f := begin refine ⟨summable_of_summable_norm, λ hf, _⟩, -- First we use a finite basis to reduce the problem to the case `E = fin N → ℝ` suffices : ∀ {N : ℕ} {g : α → fin N → ℝ}, summable g → summable (λ x, ∥g x∥), { rcases fin_basis ℝ E with ⟨v, hv⟩, set e := hv.equiv_funL, have : summable (λ x, ∥e (f x)∥) := this (e.summable.2 hf), refine summable_of_norm_bounded _ (this.mul_left ↑(nnnorm (e.symm : (fin (findim ℝ E) → ℝ) →L[ℝ] E))) (λ i, _), simpa using (e.symm : (fin (findim ℝ E) → ℝ) →L[ℝ] E).le_op_norm (e $ f i) }, unfreezingI { clear_dependent E }, -- Now we deal with `g : α → fin N → ℝ` intros N g hg, have : ∀ i, summable (λ x, ∥g x i∥) := λ i, (pi.summable.1 hg i).abs, refine summable_of_norm_bounded _ (summable_sum (λ i (hi : i ∈ finset.univ), this i)) (λ x, _), rw [norm_norm, pi_norm_le_iff], { refine λ i, finset.single_le_sum (λ i hi, _) (finset.mem_univ i), exact norm_nonneg (g x i) }, { exact finset.sum_nonneg (λ _ _, norm_nonneg _) } end
4e67b634f0a46e8804e4c1eac383dc6e888b100a
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/order/max.lean
465f1fd7e27811c57b342b6f864fec65e661b8ff
[ "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
10,280
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Yury Kudryashov, Yaël Dillies -/ import order.synonym /-! # Minimal/maximal and bottom/top elements This file defines predicates for elements to be minimal/maximal or bottom/top and typeclasses saying that there are no such elements. ## Predicates * `is_bot`: An element is *bottom* if all elements are greater than it. * `is_top`: An element is *top* if all elements are less than it. * `is_min`: An element is *minimal* if no element is strictly less than it. * `is_max`: An element is *maximal* if no element is strictly greater than it. See also `is_bot_iff_is_min` and `is_top_iff_is_max` for the equivalences in a (co)directed order. ## Typeclasses * `no_bot_order`: An order without bottom elements. * `no_top_order`: An order without top elements. * `no_min_order`: An order without minimal elements. * `no_max_order`: An order without maximal elements. -/ open order_dual variables {α β : Type*} /-- Order without bottom elements. -/ class no_bot_order (α : Type*) [has_le α] : Prop := (exists_not_ge (a : α) : ∃ b, ¬ a ≤ b) /-- Order without top elements. -/ class no_top_order (α : Type*) [has_le α] : Prop := (exists_not_le (a : α) : ∃ b, ¬ b ≤ a) /-- Order without minimal elements. Sometimes called coinitial or dense. -/ class no_min_order (α : Type*) [has_lt α] : Prop := (exists_lt (a : α) : ∃ b, b < a) /-- Order without maximal elements. Sometimes called cofinal. -/ class no_max_order (α : Type*) [has_lt α] : Prop := (exists_gt (a : α) : ∃ b, a < b) export no_bot_order (exists_not_ge) export no_top_order (exists_not_le) export no_min_order (exists_lt) export no_max_order (exists_gt) instance nonempty_lt [has_lt α] [no_min_order α] (a : α) : nonempty {x // x < a} := nonempty_subtype.2 (exists_lt a) instance nonempty_gt [has_lt α] [no_max_order α] (a : α) : nonempty {x // a < x} := nonempty_subtype.2 (exists_gt a) instance order_dual.no_bot_order (α : Type*) [has_le α] [no_top_order α] : no_bot_order αᵒᵈ := ⟨λ a, @exists_not_le α _ _ a⟩ instance order_dual.no_top_order (α : Type*) [has_le α] [no_bot_order α] : no_top_order αᵒᵈ := ⟨λ a, @exists_not_ge α _ _ a⟩ instance order_dual.no_min_order (α : Type*) [has_lt α] [no_max_order α] : no_min_order αᵒᵈ := ⟨λ a, @exists_gt α _ _ a⟩ instance order_dual.no_max_order (α : Type*) [has_lt α] [no_min_order α] : no_max_order αᵒᵈ := ⟨λ a, @exists_lt α _ _ a⟩ @[priority 100] -- See note [lower instance priority] instance no_min_order.to_no_bot_order (α : Type*) [preorder α] [no_min_order α] : no_bot_order α := ⟨λ a, (exists_lt a).imp $ λ _, not_le_of_lt⟩ @[priority 100] -- See note [lower instance priority] instance no_max_order.to_no_top_order (α : Type*) [preorder α] [no_max_order α] : no_top_order α := ⟨λ a, (exists_gt a).imp $ λ _, not_le_of_lt⟩ theorem no_min_order.not_acc [has_lt α] [no_min_order α] (a : α) : ¬ acc (<) a := λ h, acc.rec_on h $ λ x _, (exists_lt x).rec_on theorem no_max_order.not_acc [has_lt α] [no_max_order α] (a : α) : ¬ acc (>) a := λ h, acc.rec_on h $ λ x _, (exists_gt x).rec_on section has_le variables [has_le α] {a b : α} /-- `a : α` is a bottom element of `α` if it is less than or equal to any other element of `α`. This predicate is roughly an unbundled version of `order_bot`, except that a preorder may have several bottom elements. When `α` is linear, this is useful to make a case disjunction on `no_min_order α` within a proof. -/ def is_bot (a : α) : Prop := ∀ b, a ≤ b /-- `a : α` is a top element of `α` if it is greater than or equal to any other element of `α`. This predicate is roughly an unbundled version of `order_bot`, except that a preorder may have several top elements. When `α` is linear, this is useful to make a case disjunction on `no_max_order α` within a proof. -/ def is_top (a : α) : Prop := ∀ b, b ≤ a /-- `a` is a minimal element of `α` if no element is strictly less than it. We spell it without `<` to avoid having to convert between `≤` and `<`. Instead, `is_min_iff_forall_not_lt` does the conversion. -/ def is_min (a : α) : Prop := ∀ ⦃b⦄, b ≤ a → a ≤ b /-- `a` is a maximal element of `α` if no element is strictly greater than it. We spell it without `<` to avoid having to convert between `≤` and `<`. Instead, `is_max_iff_forall_not_lt` does the conversion. -/ def is_max (a : α) : Prop := ∀ ⦃b⦄, a ≤ b → b ≤ a @[simp] lemma not_is_bot [no_bot_order α] (a : α) : ¬is_bot a := λ h, let ⟨b, hb⟩ := exists_not_ge a in hb $ h _ @[simp] lemma not_is_top [no_top_order α] (a : α) : ¬is_top a := λ h, let ⟨b, hb⟩ := exists_not_le a in hb $ h _ protected lemma is_bot.is_min (h : is_bot a) : is_min a := λ b _, h b protected lemma is_top.is_max (h : is_top a) : is_max a := λ b _, h b @[simp] lemma is_bot_to_dual_iff : is_bot (to_dual a) ↔ is_top a := iff.rfl @[simp] lemma is_top_to_dual_iff : is_top (to_dual a) ↔ is_bot a := iff.rfl @[simp] lemma is_min_to_dual_iff : is_min (to_dual a) ↔ is_max a := iff.rfl @[simp] lemma is_max_to_dual_iff : is_max (to_dual a) ↔ is_min a := iff.rfl @[simp] lemma is_bot_of_dual_iff {a : αᵒᵈ} : is_bot (of_dual a) ↔ is_top a := iff.rfl @[simp] lemma is_top_of_dual_iff {a : αᵒᵈ} : is_top (of_dual a) ↔ is_bot a := iff.rfl @[simp] lemma is_min_of_dual_iff {a : αᵒᵈ} : is_min (of_dual a) ↔ is_max a := iff.rfl @[simp] lemma is_max_of_dual_iff {a : αᵒᵈ} : is_max (of_dual a) ↔ is_min a := iff.rfl alias is_bot_to_dual_iff ↔ _ is_top.to_dual alias is_top_to_dual_iff ↔ _ is_bot.to_dual alias is_min_to_dual_iff ↔ _ is_max.to_dual alias is_max_to_dual_iff ↔ _ is_min.to_dual alias is_bot_of_dual_iff ↔ _ is_top.of_dual alias is_top_of_dual_iff ↔ _ is_bot.of_dual alias is_min_of_dual_iff ↔ _ is_max.of_dual alias is_max_of_dual_iff ↔ _ is_min.of_dual end has_le section preorder variables [preorder α] {a b : α} lemma is_bot.mono (ha : is_bot a) (h : b ≤ a) : is_bot b := λ c, h.trans $ ha _ lemma is_top.mono (ha : is_top a) (h : a ≤ b) : is_top b := λ c, (ha _).trans h lemma is_min.mono (ha : is_min a) (h : b ≤ a) : is_min b := λ c hc, h.trans $ ha $ hc.trans h lemma is_max.mono (ha : is_max a) (h : a ≤ b) : is_max b := λ c hc, (ha $ h.trans hc).trans h lemma is_min.not_lt (h : is_min a) : ¬ b < a := λ hb, hb.not_le $ h hb.le lemma is_max.not_lt (h : is_max a) : ¬ a < b := λ hb, hb.not_le $ h hb.le @[simp] lemma not_is_min_of_lt (h : b < a) : ¬ is_min a := λ ha, ha.not_lt h @[simp] lemma not_is_max_of_lt (h : a < b) : ¬ is_max a := λ ha, ha.not_lt h alias not_is_min_of_lt ← has_lt.lt.not_is_min alias not_is_max_of_lt ← has_lt.lt.not_is_max lemma is_min_iff_forall_not_lt : is_min a ↔ ∀ b, ¬ b < a := ⟨λ h _, h.not_lt, λ h b hba, of_not_not $ λ hab, h _ $ hba.lt_of_not_le hab⟩ lemma is_max_iff_forall_not_lt : is_max a ↔ ∀ b, ¬ a < b := ⟨λ h _, h.not_lt, λ h b hba, of_not_not $ λ hab, h _ $ hba.lt_of_not_le hab⟩ @[simp] lemma not_is_min_iff : ¬ is_min a ↔ ∃ b, b < a := by simp_rw [lt_iff_le_not_le, is_min, not_forall, exists_prop] @[simp] lemma not_is_max_iff : ¬ is_max a ↔ ∃ b, a < b := by simp_rw [lt_iff_le_not_le, is_max, not_forall, exists_prop] @[simp] lemma not_is_min [no_min_order α] (a : α) : ¬ is_min a := not_is_min_iff.2 $ exists_lt a @[simp] lemma not_is_max [no_max_order α] (a : α) : ¬ is_max a := not_is_max_iff.2 $ exists_gt a namespace subsingleton variable [subsingleton α] protected lemma is_bot (a : α) : is_bot a := λ _, (subsingleton.elim _ _).le protected lemma is_top (a : α) : is_top a := λ _, (subsingleton.elim _ _).le protected lemma is_min (a : α) : is_min a := (subsingleton.is_bot _).is_min protected lemma is_max (a : α) : is_max a := (subsingleton.is_top _).is_max end subsingleton end preorder section partial_order variables [partial_order α] {a b : α} protected lemma is_min.eq_of_le (ha : is_min a) (h : b ≤ a) : b = a := h.antisymm $ ha h protected lemma is_min.eq_of_ge (ha : is_min a) (h : b ≤ a) : a = b := h.antisymm' $ ha h protected lemma is_max.eq_of_le (ha : is_max a) (h : a ≤ b) : a = b := h.antisymm $ ha h protected lemma is_max.eq_of_ge (ha : is_max a) (h : a ≤ b) : b = a := h.antisymm' $ ha h end partial_order section prod variables [preorder α] [preorder β] {a a₁ a₂ : α} {b b₁ b₂ : β} {x y : α × β} lemma is_bot.prod_mk (ha : is_bot a) (hb : is_bot b) : is_bot (a, b) := λ c, ⟨ha _, hb _⟩ lemma is_top.prod_mk (ha : is_top a) (hb : is_top b) : is_top (a, b) := λ c, ⟨ha _, hb _⟩ lemma is_min.prod_mk (ha : is_min a) (hb : is_min b) : is_min (a, b) := λ c hc, ⟨ha hc.1, hb hc.2⟩ lemma is_max.prod_mk (ha : is_max a) (hb : is_max b) : is_max (a, b) := λ c hc, ⟨ha hc.1, hb hc.2⟩ lemma is_bot.fst (hx : is_bot x) : is_bot x.1 := λ c, (hx (c, x.2)).1 lemma is_bot.snd (hx : is_bot x) : is_bot x.2 := λ c, (hx (x.1, c)).2 lemma is_top.fst (hx : is_top x) : is_top x.1 := λ c, (hx (c, x.2)).1 lemma is_top.snd (hx : is_top x) : is_top x.2 := λ c, (hx (x.1, c)).2 lemma is_min.fst (hx : is_min x) : is_min x.1 := λ c hc, (hx $ show (c, x.2) ≤ x, from (and_iff_left le_rfl).2 hc).1 lemma is_min.snd (hx : is_min x) : is_min x.2 := λ c hc, (hx $ show (x.1, c) ≤ x, from (and_iff_right le_rfl).2 hc).2 lemma is_max.fst (hx : is_max x) : is_max x.1 := λ c hc, (hx $ show x ≤ (c, x.2), from (and_iff_left le_rfl).2 hc).1 lemma is_max.snd (hx : is_max x) : is_max x.2 := λ c hc, (hx $ show x ≤ (x.1, c), from (and_iff_right le_rfl).2 hc).2 lemma prod.is_bot_iff : is_bot x ↔ is_bot x.1 ∧ is_bot x.2 := ⟨λ hx, ⟨hx.fst, hx.snd⟩, λ h, h.1.prod_mk h.2⟩ lemma prod.is_top_iff : is_top x ↔ is_top x.1 ∧ is_top x.2 := ⟨λ hx, ⟨hx.fst, hx.snd⟩, λ h, h.1.prod_mk h.2⟩ lemma prod.is_min_iff : is_min x ↔ is_min x.1 ∧ is_min x.2 := ⟨λ hx, ⟨hx.fst, hx.snd⟩, λ h, h.1.prod_mk h.2⟩ lemma prod.is_max_iff : is_max x ↔ is_max x.1 ∧ is_max x.2 := ⟨λ hx, ⟨hx.fst, hx.snd⟩, λ h, h.1.prod_mk h.2⟩ end prod
9539aa07f42f865208a2b0bd3691186ce576ce8e
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/lean/run/Reid1.lean
15f0dbfda47176421a85e10bf41d8eec70bab324
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
701
lean
structure ConstantFunction (α β : Type) := (f : α → β) (h : ∀ a₁ a₂, f a₁ = f a₂) instance constFunCoe {α β : Type} : CoeFun (ConstantFunction α β) (fun _ => α → β) := { coe := fun c => c.f } def myFun {α : Type} : ConstantFunction α (Option α) := { f := fun a => none, h := fun a₁ a₂ => rfl } def myFun' (α : Type) : ConstantFunction α (Option α) := { f := fun a => none, h := fun a₁ a₂ => rfl } new_frontend set_option pp.all true #check myFun 3 -- works #check @myFun Nat 3 -- works #check myFun' _ 3 -- works #check myFun' Nat 3 -- works variable (c : ConstantFunction Nat Nat) #check c 3 -- works #check (fun c => c 3) myFun -- works
714347f4cbabc488fcf673be0918aac0cab5904c
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/hom/group_action.lean
8c086115d579ee30868ed4f12a210f40e640899c
[ "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
13,913
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.basic import algebra.module.basic /-! # Equivariant homomorphisms > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## 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`. The above types have corresponding classes: * `smul_hom_class F M X Y` states that `F` is a type of bundled `X → Y` homs preserving scalar multiplication by `M` * `distrib_mul_action_hom_class F M A B` states that `F` is a type of bundled `A → B` homs preserving the additive monoid structure and scalar multiplication by `M` * `mul_semiring_action_hom_class F M R S` states that `F` is a type of bundled `R → S` homs preserving the ring structure and scalar multiplication by `M` ## Notations * `X →[M] Y` is `mul_action_hom M X Y`. * `A →+[M] B` is `distrib_mul_action_hom M A B`. * `R →+*[M] S` is `mul_semiring_action_hom M R S`. -/ assert_not_exists submonoid variables (M' : Type*) variables (X : Type*) [has_smul M' X] variables (Y : Type*) [has_smul M' Y] variables (Z : Type*) [has_smul M' Z] variables (M : Type*) [monoid M] 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] set_option old_structure_cmd true /-- Equivariant functions. -/ @[nolint has_nonempty_instance] structure mul_action_hom := (to_fun : X → Y) (map_smul' : ∀ (m : M') (x : X), to_fun (m • x) = m • to_fun x) notation (name := mul_action_hom) X ` →[`:25 M:25 `] `:0 Y:0 := mul_action_hom M X Y /-- `smul_hom_class F M X Y` states that `F` is a type of morphisms preserving scalar multiplication by `M`. You should extend this class when you extend `mul_action_hom`. -/ class smul_hom_class (F : Type*) (M X Y : out_param $ Type*) [has_smul M X] [has_smul M Y] extends fun_like F X (λ _, Y) := (map_smul : ∀ (f : F) (c : M) (x : X), f (c • x) = c • f x) -- `M` becomes a metavariable but it's an `out_param` so it's not a problem. attribute [nolint dangerous_instance] smul_hom_class.to_fun_like export smul_hom_class (map_smul) attribute [simp] map_smul namespace mul_action_hom instance : has_coe_to_fun (X →[M'] Y) (λ _, X → Y) := ⟨mul_action_hom.to_fun⟩ instance : smul_hom_class (X →[M'] Y) M' X Y := { coe := mul_action_hom.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', map_smul := mul_action_hom.map_smul' } variables {M M' X Y} protected lemma map_smul (f : X →[M'] Y) (m : M') (x : X) : f (m • x) = m • f x := map_smul _ _ _ @[ext] theorem ext : ∀ {f g : X →[M'] Y}, (∀ x, f x = g x) → f = g := fun_like.ext theorem ext_iff {f g : X →[M'] Y} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff protected lemma congr_fun {f g : X →[M'] Y} (h : f = g) (x : X) : f x = g x := fun_like.congr_fun h _ variables (M 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 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] variables {A B} /-- The inverse of a bijective equivariant map is equivariant. -/ @[simps] def inverse (f : A →[M] B) (g : B → A) (h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) : B →[M] A := { to_fun := g, map_smul' := λ m x, calc g (m • x) = g (m • (f (g x))) : by rw h₂ ... = g (f (m • (g x))) : by rw f.map_smul ... = m • g x : by rw h₁, } end mul_action_hom /-- Equivariant additive monoid homomorphisms. -/ 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 /-- `distrib_mul_action_hom_class F M A B` states that `F` is a type of morphisms preserving the additive monoid structure and scalar multiplication by `M`. You should extend this class when you extend `distrib_mul_action_hom`. -/ class distrib_mul_action_hom_class (F : Type*) (M A B : out_param $ Type*) [monoid M] [add_monoid A] [add_monoid B] [distrib_mul_action M A] [distrib_mul_action M B] extends smul_hom_class F M A B, add_monoid_hom_class F A B -- `M` becomes a metavariable but it's an `out_param` so it's not a problem. attribute [nolint dangerous_instance] distrib_mul_action_hom_class.to_add_monoid_hom_class 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) (λ _, A → B) := ⟨to_fun⟩ instance : distrib_mul_action_hom_class (A →+[M] B) M A B := { coe := distrib_mul_action_hom.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', map_smul := distrib_mul_action_hom.map_smul', map_zero := distrib_mul_action_hom.map_zero', map_add := distrib_mul_action_hom.map_add' } variables {M A B} @[simp] lemma to_fun_eq_coe (f : A →+[M] B) : f.to_fun = ⇑f := rfl @[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 := fun_like.ext theorem ext_iff {f g : A →+[M] B} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff protected lemma congr_fun {f g : A →+[M] B} (h : f = g) (x : A) : f x = g x := fun_like.congr_fun h _ lemma to_mul_action_hom_injective {f g : A →+[M] B} (h : (f : A →[M] B) = (g : A →[M] B)) : f = g := by { ext a, exact mul_action_hom.congr_fun h a, } lemma to_add_monoid_hom_injective {f g : A →+[M] B} (h : (f : A →+ B) = (g : A →+ B)) : f = g := by { ext a, exact add_monoid_hom.congr_fun h a, } protected lemma map_zero (f : A →+[M] B) : f 0 = 0 := map_zero _ protected lemma map_add (f : A →+[M] B) (x y : A) : f (x + y) = f x + f y := map_add _ _ _ protected lemma map_neg (f : A' →+[M] B') (x : A') : f (-x) = -f x := map_neg _ _ protected lemma map_sub (f : A' →+[M] B') (x y : A') : f (x - y) = f x - f y := map_sub _ _ _ protected lemma map_smul (f : A →+[M] B) (m : M) (x : A) : f (m • x) = m • f x := map_smul _ _ _ 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} instance : has_zero (A →+[M] B) := ⟨{ map_smul' := by simp, .. (0 : A →+ B) }⟩ instance : has_one (A →+[M] A) := ⟨distrib_mul_action_hom.id M⟩ @[simp] lemma coe_zero : ((0 : A →+[M] B) : A → B) = 0 := rfl @[simp] lemma coe_one : ((1 : A →+[M] A) : A → A) = id := rfl lemma zero_apply (a : A) : (0 : A →+[M] B) a = 0 := rfl lemma one_apply (a : A) : (1 : A →+[M] A) a = a := rfl instance : inhabited (A →+[M] B) := ⟨0⟩ /-- 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] /-- The inverse of a bijective `distrib_mul_action_hom` is a `distrib_mul_action_hom`. -/ @[simps] def inverse (f : A →+[M] B) (g : B → A) (h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) : B →+[M] A := { to_fun := g, .. (f : A →+ B).inverse g h₁ h₂, .. (f : A →[M] B).inverse g h₁ h₂ } section semiring variables {R M'} [add_monoid M'] [distrib_mul_action R M'] @[ext] lemma ext_ring {f g : R →+[R] M'} (h : f 1 = g 1) : f = g := by { ext x, rw [← mul_one x, ← smul_eq_mul R, f.map_smul, g.map_smul, h], } lemma ext_ring_iff {f g : R →+[R] M'} : f = g ↔ f 1 = g 1 := ⟨λ h, h ▸ rfl, ext_ring⟩ end semiring end distrib_mul_action_hom /-- Equivariant ring homomorphisms. -/ @[nolint has_nonempty_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 /-- `mul_semiring_action_hom_class F M R S` states that `F` is a type of morphisms preserving the ring structure and scalar multiplication by `M`. You should extend this class when you extend `mul_semiring_action_hom`. -/ class mul_semiring_action_hom_class (F : Type*) (M R S : out_param $ Type*) [monoid M] [semiring R] [semiring S] [distrib_mul_action M R] [distrib_mul_action M S] extends distrib_mul_action_hom_class F M R S, ring_hom_class F R S -- `M` becomes a metavariable but it's an `out_param` so it's not a problem. attribute [nolint dangerous_instance] mul_semiring_action_hom_class.to_ring_hom_class 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) (λ _, R → S) := ⟨λ c, c.to_fun⟩ instance : mul_semiring_action_hom_class (R →+*[M] S) M R S := { coe := mul_semiring_action_hom.to_fun, coe_injective' := λ f g h, by cases f; cases g; congr', map_smul := mul_semiring_action_hom.map_smul', map_zero := mul_semiring_action_hom.map_zero', map_add := mul_semiring_action_hom.map_add', map_one := mul_semiring_action_hom.map_one', map_mul := mul_semiring_action_hom.map_mul' } 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 := fun_like.ext theorem ext_iff {f g : R →+*[M] S} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff protected lemma map_zero (f : R →+*[M] S) : f 0 = 0 := map_zero _ protected lemma map_add (f : R →+*[M] S) (x y : R) : f (x + y) = f x + f y := map_add _ _ _ protected lemma map_neg (f : R' →+*[M] S') (x : R') : f (-x) = -f x := map_neg _ _ protected lemma map_sub (f : R' →+*[M] S') (x y : R') : f (x - y) = f x - f y := map_sub _ _ _ protected lemma map_one (f : R →+*[M] S) : f 1 = 1 := map_one _ protected lemma map_mul (f : R →+*[M] S) (x y : R) : f (x * y) = f x * f y := map_mul _ _ _ protected lemma map_smul (f : R →+*[M] S) (m : M) (x : R) : f (m • x) = m • f x := map_smul _ _ _ 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] end mul_semiring_action_hom
70ba4efd3ebb0c0cb27c463327f244e103576768
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/test/lint_coe_to_fun.lean
ed4ee01642e51d33242f1cb5dd7fc6e8e4a7d986
[ "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,433
lean
import tactic.lint -- see Note [function coercion] structure equiv (α β : Sort*) := (to_fun : α → β) (inv_fun : β → α) instance {α β} : has_coe_to_fun (equiv α β) := ⟨λ _, α → β, equiv.to_fun⟩ structure sparkling_equiv (α β) extends equiv α β instance {α β} : has_coe (sparkling_equiv α β) (equiv α β) := ⟨sparkling_equiv.to_equiv⟩ -- should complain open tactic run_cmd do decl ← get_decl ``sparkling_equiv, res ← linter.has_coe_to_fun.test decl, -- linter complains guard res.is_some instance {α β} : has_coe_to_fun (sparkling_equiv α β) := ⟨λ _, α → β, λ f, f.to_equiv.to_fun⟩ -- prima! run_cmd do decl ← get_decl ``sparkling_equiv, res ← linter.has_coe_to_fun.test decl, -- linter doesn't complain guard res.is_none -- Test support for type-class parameters in the `has_coe_to_fun` instance. namespace with_tc_param structure equiv (α β : Sort*) := (to_fun : α → β) (inv_fun : β → α) instance {α β} [nonempty α] : has_coe_to_fun (equiv α β) := ⟨λ _, α → β, equiv.to_fun⟩ structure sparkling_equiv (α β) [nonempty α] extends equiv α β instance {α β} [nonempty α] : has_coe (sparkling_equiv α β) (equiv α β) := ⟨sparkling_equiv.to_equiv⟩ -- should complain open tactic run_cmd do decl ← get_decl ``sparkling_equiv, res ← linter.has_coe_to_fun.test decl, -- linter complains guard res.is_some end with_tc_param
a9753448ab795af0bcffa84681215190610f8b26
d8820d2c92be8052d13f9c8f8c483a6e15c5f566
/src/Stuff_for_reference/learning.lean
4e5853b2b70848a16c13adf484129ae6de2d5d44
[]
no_license
JasonKYi/M4000x_LEAN_formalisation
4a19b84f6d0fe2e214485b8532e21cd34996c4b1
6e99793f2fcbe88596e27644f430e46aa2a464df
refs/heads/master
1,599,755,414,708
1,589,494,604,000
1,589,494,604,000
221,759,483
8
1
null
1,589,494,605,000
1,573,755,201,000
Lean
UTF-8
Lean
false
false
4,327
lean
import data.real.basic -- Tactics -- revert is kinda the inverse of intro example (x : ℕ) : x = x := begin revert x, -- goal is ⊢ ∀ (x : ℕ), x = x intro y, -- goal is y : ℕ ⊢ y = y reflexivity end -- arbitary expressions can be generalized using genralize example : 3 = 3 := begin generalize : 3 = x, -- goal is x : ℕ ⊢ x = x, revert x, -- goal is ⊢ ∀ (x : ℕ), x = x intro y, reflexivity end -- use this when confused -- set_option pp.notation false -- shows what simp did -- set_option trace.simplify.rewrite true example (p q : Prop) : p → q → p := λ hp, λ hq, hp example (p q r s : Prop) (h₁ : q → r) (h₂ : p → q) : p → r := λ hp : p, h₁ (h₂ hp) example (p q r s : Prop) (h₁ : q → r) (h₂ : p → q) : p → r := assume hp : p, show r, from h₁ (h₂ hp) example (p q : Prop) : p → q → p ∧ q := assume hp : p, assume hq : q, show p ∧ q, from and.intro hp hq example (p q : Prop) : p → q → p ∧ q := λ hp : p, λ hq : q, ⟨hp, hq⟩ -- or we can use and.intro hp hq example (p q : Prop) (h : p ∧ q) : p := h.left -- or and.elim_left h example (p q : Prop) : p ∧ q → q ∧ p := λ h : p ∧ q, and.intro h.right h.left example (p q : Prop) : p → p ∨ q := λ hp : p, or.intro_left q hp example (p q : Prop) : p ∨ q → q ∨ p := λ h : p ∨ q, or.elim h (λ hp : p, show q ∨ p, from or.inr hp) (λ hq : q, show q ∨ p, from or.inl hq) example (p q : Prop) : (p → q) → ¬ q → ¬ p := λ h : p → q, λ hnq : ¬ q, λ hp : p, show false, from hnq (h hp) example (p q r : Prop) : ¬ p → q → (q → p) → r := assume hnp : ¬ p, assume hq : q, assume h : q → p, absurd (h hq) hnp example (p q : Prop) : p ∧ q ↔ q ∧ p := iff.intro (assume hpq : p ∧ q, ⟨hpq.right, hpq.left⟩) (assume hqp : q ∧ p, ⟨hqp.right, hqp.left⟩) inductive names : Type | Johanson : names | Kristian : names | Marlin : names | Maria : names | NoOne : names namespace names #check Maria def age_of_person (person : names) : ℕ := names.cases_on person 18 19 18 23 99999 #reduce age_of_person Maria def next_person (person : names) : names := names.rec_on person Kristian Marlin Maria NoOne Johanson end names namespace hidden inductive nat | zero : nat | succ (n : nat) : nat namespace nat def add : nat → nat → nat | m zero := m | m (succ n) := succ (add m n) notation m ` + ` n := add m n def mul : nat → nat → nat | m zero := zero | m (succ n) := add (mul m n) m notation m ` × ` n := mul m n theorem add_zero (x : nat) : x + zero = x := rfl theorem add_succ (x y : nat) : x + succ y = succ (x + y) := rfl theorem add_assoc (x y z : nat) : (x + y) + z = x + (y + z) := begin induction z with k hk, repeat {rw add_zero}, repeat {rw add_succ}, rw hk end theorem zero_add (x : nat) : zero + x = x := begin induction x with k hk, rw add_zero, rw [add_succ, hk] end theorem succ_add (x y : nat) : (succ x) + y = succ (x + y) := begin induction y with k hk, repeat {rw add_zero}, rw [add_succ, hk, add_succ] end theorem add_comm (x y : nat) : x + y = y + x := begin induction y with k hk, rw [add_zero, zero_add], rw [add_succ, succ_add, hk] end end nat universes u v inductive sum (α : Type u) (β : Type v) | inl {} : α → sum | inr {} : β → sum variable {α : Type u} end hidden example (F G H : Type → Prop) (h : ∀ x : Type, (F x → (¬ G x → H x))) : ∀ x : Type, (F x → (G x ∨ H x)) := begin intros x hF, -- So I'm fixing x and supposing F x is true. We now need to show G x ∨ H x cases classical.em (G x) with hG hG, -- By excluded middle (I assume we can use classical logic since we are not cs?) either G x is true or its false left, from hG, -- If G x is true than G x ∨ H x is true so we are done! right, from h x hF hG -- If G x is false then as from assumption (h : ∀ x : Type, (F x → (¬ G x → H x))), we have not G x implies H x is true so G x or H x is true! end example (F G H : Type → Prop) (h : ∀ x : Type, (F x → (¬ G x → H x))) : ∀ x : Type, (F x → (G x ∨ H x)) := begin finish end
d05bb657d3bcce84bd6f90f6215b2a568ac9cfc9
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/limits/preserves/shapes/pullbacks.lean
7b7086075200bc60dc08bda6296bd27d60426805
[ "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
11,021
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Andrew Yang -/ import category_theory.limits.shapes.pullbacks import category_theory.limits.preserves.basic /-! # Preserving pullbacks Constructions to relate the notions of preserving pullbacks and reflecting pullbacks to concrete pullback cones. In particular, we show that `pullback_comparison G f g` is an isomorphism iff `G` preserves the pullback of `f` and `g`. The dual is also given. ## TODO * Generalise to wide pullbacks -/ noncomputable theory universes v₁ v₂ u₁ u₂ open category_theory category_theory.category category_theory.limits namespace category_theory.limits section pullback variables {C : Type u₁} [category.{v₁} C] variables {D : Type u₂} [category.{v₂} D] variables (G : C ⥤ D) variables {W X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} {h : W ⟶ X} {k : W ⟶ Y} (comm : h ≫ f = k ≫ g) /-- The map of a pullback cone is a limit iff the fork consisting of the mapped morphisms is a limit. This essentially lets us commute `pullback_cone.mk` with `functor.map_cone`. -/ def is_limit_map_cone_pullback_cone_equiv : is_limit (G.map_cone (pullback_cone.mk h k comm)) ≃ is_limit (pullback_cone.mk (G.map h) (G.map k) (by simp only [← G.map_comp, comm]) : pullback_cone (G.map f) (G.map g)) := (is_limit.whisker_equivalence_equiv walking_cospan_equiv.{v₂ v₁}).trans $ (is_limit.postcompose_hom_equiv (diagram_iso_cospan.{v₂} _) _).symm.trans $ is_limit.equiv_iso_limit $ cones.ext (iso.refl _) $ (by rintro (_|_|_); dsimp; simpa only [category.comp_id, category.id_comp, ← G.map_comp]) /-- The property of preserving pullbacks expressed in terms of binary fans. -/ def is_limit_pullback_cone_map_of_is_limit [preserves_limit (cospan f g) G] (l : is_limit (pullback_cone.mk h k comm)) : is_limit (pullback_cone.mk (G.map h) (G.map k) _) := is_limit_map_cone_pullback_cone_equiv G comm (preserves_limit.preserves l) /-- The property of reflecting pullbacks expressed in terms of binary fans. -/ def is_limit_of_is_limit_pullback_cone_map [reflects_limit (cospan f g) G] (l : is_limit (pullback_cone.mk (G.map h) (G.map k) _)) : is_limit (pullback_cone.mk h k comm) := reflects_limit.reflects ((is_limit_map_cone_pullback_cone_equiv G comm).symm l) variables (f g) [preserves_limit (cospan f g) G] /-- If `G` preserves pullbacks and `C` has them, then the pullback cone constructed of the mapped morphisms of the pullback cone is a limit. -/ def is_limit_of_has_pullback_of_preserves_limit [has_pullback f g] : is_limit (pullback_cone.mk (G.map pullback.fst) (G.map pullback.snd) _) := is_limit_pullback_cone_map_of_is_limit G _ (pullback_is_pullback f g) /-- If `F` preserves the pullback of `f, g`, it also preserves the pullback of `g, f`. -/ def preserves_pullback_symmetry : preserves_limit (cospan g f) G := { preserves := λ c hc, begin apply (is_limit.whisker_equivalence_equiv walking_cospan_equiv.{v₂ v₁}).symm.to_fun, apply (is_limit.postcompose_hom_equiv (diagram_iso_cospan.{v₂} _) _).to_fun, apply is_limit.of_iso_limit _ (pullback_cone.iso_mk _).symm, apply pullback_cone.flip_is_limit, apply (is_limit_map_cone_pullback_cone_equiv _ _).to_fun, { apply_with preserves_limit.preserves { instances := ff }, { dsimp [walking_cospan_equiv], apply_instance }, apply pullback_cone.flip_is_limit, apply is_limit.of_iso_limit _ (pullback_cone.iso_mk _), exact (is_limit.postcompose_hom_equiv (diagram_iso_cospan.{v₁} _) _).inv_fun hc }, { exact (c.π.naturality walking_cospan.hom.inr).symm.trans (c.π.naturality walking_cospan.hom.inl : _) } end } variables [has_pullback f g] [has_pullback (G.map f) (G.map g)] /-- If `G` preserves the pullback of `(f,g)`, then the pullback comparison map for `G` at `(f,g)` is an isomorphism. -/ def preserves_pullback.iso : G.obj (pullback f g) ≅ pullback (G.map f) (G.map g) := is_limit.cone_point_unique_up_to_iso (is_limit_of_has_pullback_of_preserves_limit G f g) (limit.is_limit _) @[reassoc] lemma preserves_pullback.iso_hom_fst : (preserves_pullback.iso G f g).hom ≫ pullback.fst = G.map pullback.fst := by simp [preserves_pullback.iso] @[reassoc] lemma preserves_pullback.iso_hom_snd : (preserves_pullback.iso G f g).hom ≫ pullback.snd = G.map pullback.snd := by simp [preserves_pullback.iso] @[simp, reassoc] lemma preserves_pullback.iso_inv_fst : (preserves_pullback.iso G f g).inv ≫ G.map pullback.fst = pullback.fst := by simp [preserves_pullback.iso, iso.inv_comp_eq] @[simp, reassoc] lemma preserves_pullback.iso_inv_snd : (preserves_pullback.iso G f g).inv ≫ G.map pullback.snd = pullback.snd := by simp [preserves_pullback.iso, iso.inv_comp_eq] end pullback section pushout variables {C : Type u₁} [category.{v₁} C] variables {D : Type u₂} [category.{v₂} D] variables (G : C ⥤ D) variables {W X Y Z : C} {h : X ⟶ Z} {k : Y ⟶ Z} {f : W ⟶ X} {g : W ⟶ Y} (comm : f ≫ h = g ≫ k) /-- The map of a pushout cocone is a colimit iff the cofork consisting of the mapped morphisms is a colimit. This essentially lets us commute `pushout_cocone.mk` with `functor.map_cocone`. -/ def is_colimit_map_cocone_pushout_cocone_equiv : is_colimit (G.map_cocone (pushout_cocone.mk h k comm)) ≃ is_colimit (pushout_cocone.mk (G.map h) (G.map k) (by simp only [← G.map_comp, comm]) : pushout_cocone (G.map f) (G.map g)) := (is_colimit.whisker_equivalence_equiv walking_span_equiv.{v₂ v₁}).trans $ (is_colimit.precompose_hom_equiv (diagram_iso_span.{v₂} _).symm _).symm.trans $ is_colimit.equiv_iso_colimit $ cocones.ext (iso.refl _) $ (by rintro (_|_|_); dsimp; simpa only [category.comp_id, category.id_comp, ← G.map_comp]) /-- The property of preserving pushouts expressed in terms of binary cofans. -/ def is_colimit_pushout_cocone_map_of_is_colimit [preserves_colimit (span f g) G] (l : is_colimit (pushout_cocone.mk h k comm)) : is_colimit (pushout_cocone.mk (G.map h) (G.map k) _) := is_colimit_map_cocone_pushout_cocone_equiv G comm (preserves_colimit.preserves l) /-- The property of reflecting pushouts expressed in terms of binary cofans. -/ def is_colimit_of_is_colimit_pushout_cocone_map [reflects_colimit (span f g) G] (l : is_colimit (pushout_cocone.mk (G.map h) (G.map k) _)) : is_colimit (pushout_cocone.mk h k comm) := reflects_colimit.reflects ((is_colimit_map_cocone_pushout_cocone_equiv G comm).symm l) variables (f g) [preserves_colimit (span f g) G] /-- If `G` preserves pushouts and `C` has them, then the pushout cocone constructed of the mapped morphisms of the pushout cocone is a colimit. -/ def is_colimit_of_has_pushout_of_preserves_colimit [has_pushout f g] : is_colimit (pushout_cocone.mk (G.map pushout.inl) (G.map pushout.inr) _) := is_colimit_pushout_cocone_map_of_is_colimit G _ (pushout_is_pushout f g) /-- If `F` preserves the pushout of `f, g`, it also preserves the pushout of `g, f`. -/ def preserves_pushout_symmetry : preserves_colimit (span g f) G := { preserves := λ c hc, begin apply (is_colimit.whisker_equivalence_equiv walking_span_equiv.{v₂ v₁}).symm.to_fun, apply (is_colimit.precompose_hom_equiv (diagram_iso_span.{v₂} _).symm _).to_fun, apply is_colimit.of_iso_colimit _ (pushout_cocone.iso_mk _).symm, apply pushout_cocone.flip_is_colimit, apply (is_colimit_map_cocone_pushout_cocone_equiv _ _).to_fun, { apply_with preserves_colimit.preserves { instances := ff }, { dsimp [walking_span_equiv], apply_instance }, apply pushout_cocone.flip_is_colimit, apply is_colimit.of_iso_colimit _ (pushout_cocone.iso_mk _), exact (is_colimit.precompose_hom_equiv (diagram_iso_span.{v₁} _) _).inv_fun hc }, { exact (c.ι.naturality walking_span.hom.snd).trans (c.ι.naturality walking_span.hom.fst).symm } end } variables [has_pushout f g] [has_pushout (G.map f) (G.map g)] /-- If `G` preserves the pushout of `(f,g)`, then the pushout comparison map for `G` at `(f,g)` is an isomorphism. -/ def preserves_pushout.iso : pushout (G.map f) (G.map g) ≅ G.obj (pushout f g) := is_colimit.cocone_point_unique_up_to_iso (colimit.is_colimit _) (is_colimit_of_has_pushout_of_preserves_colimit G f g) @[reassoc] lemma preserves_pushout.inl_iso_hom : pushout.inl ≫ (preserves_pushout.iso G f g).hom = G.map pushout.inl := by { delta preserves_pushout.iso, simp } @[reassoc] lemma preserves_pushout.inr_iso_hom : pushout.inr ≫ (preserves_pushout.iso G f g).hom = G.map pushout.inr := by { delta preserves_pushout.iso, simp } @[simp, reassoc] lemma preserves_pushout.inl_iso_inv : G.map pushout.inl ≫ (preserves_pushout.iso G f g).inv = pushout.inl := by simp [preserves_pushout.iso, iso.comp_inv_eq] @[simp, reassoc] lemma preserves_pushout.inr_iso_inv : G.map pushout.inr ≫ (preserves_pushout.iso G f g).inv = pushout.inr := by simp [preserves_pushout.iso, iso.comp_inv_eq] end pushout section variables {C : Type u₁} [category.{v₁} C] variables {D : Type u₂} [category.{v₁} D] variables (G : C ⥤ D) section pullback variables {X Y Z : C} {f : X ⟶ Z} {g : Y ⟶ Z} variables [has_pullback f g] [has_pullback (G.map f) (G.map g)] /-- If the pullback comparison map for `G` at `(f,g)` is an isomorphism, then `G` preserves the pullback of `(f,g)`. -/ def preserves_pullback.of_iso_comparison [i : is_iso (pullback_comparison G f g)] : preserves_limit (cospan f g) G := begin apply preserves_limit_of_preserves_limit_cone (pullback_is_pullback f g), apply (is_limit_map_cone_pullback_cone_equiv _ _).symm _, apply is_limit.of_point_iso (limit.is_limit (cospan (G.map f) (G.map g))), apply i, end variable [preserves_limit (cospan f g) G] @[simp] lemma preserves_pullback.iso_hom : (preserves_pullback.iso G f g).hom = pullback_comparison G f g := rfl instance : is_iso (pullback_comparison G f g) := begin rw ← preserves_pullback.iso_hom, apply_instance end end pullback section pushout variables {X Y Z : C} {f : X ⟶ Y} {g : X ⟶ Z} variables [has_pushout f g] [has_pushout (G.map f) (G.map g)] /-- If the pushout comparison map for `G` at `(f,g)` is an isomorphism, then `G` preserves the pushout of `(f,g)`. -/ def preserves_pushout.of_iso_comparison [i : is_iso (pushout_comparison G f g)] : preserves_colimit (span f g) G := begin apply preserves_colimit_of_preserves_colimit_cocone (pushout_is_pushout f g), apply (is_colimit_map_cocone_pushout_cocone_equiv _ _).symm _, apply is_colimit.of_point_iso (colimit.is_colimit (span (G.map f) (G.map g))), apply i, end variable [preserves_colimit (span f g) G] @[simp] lemma preserves_pushout.iso_hom : (preserves_pushout.iso G f g).hom = pushout_comparison G f g := rfl instance : is_iso (pushout_comparison G f g) := begin rw ← preserves_pushout.iso_hom, apply_instance end end pushout end end category_theory.limits
b03ef1e389f6961e3bd3f4631081d8a43495453a
9dc8cecdf3c4634764a18254e94d43da07142918
/src/dynamics/periodic_pts.lean
499fd5bfb137fc354e5a734a41fbe6a30c81eff8
[ "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
23,301
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import algebra.hom.iterate import data.list.cycle import data.nat.prime import dynamics.fixed_points.basic /-! # Periodic points A point `x : α` is a periodic point of `f : α → α` of period `n` if `f^[n] x = x`. ## Main definitions * `is_periodic_pt f n x` : `x` is a periodic point of `f` of period `n`, i.e. `f^[n] x = x`. We do not require `n > 0` in the definition. * `pts_of_period f n` : the set `{x | is_periodic_pt f n x}`. Note that `n` is not required to be the minimal period of `x`. * `periodic_pts f` : the set of all periodic points of `f`. * `minimal_period f x` : the minimal period of a point `x` under an endomorphism `f` or zero if `x` is not a periodic point of `f`. * `orbit f x`: the cycle `[x, f x, f (f x), ...]` for a periodic point. ## Main statements We provide “dot syntax”-style operations on terms of the form `h : is_periodic_pt f n x` including arithmetic operations on `n` and `h.map (hg : semiconj_by g f f')`. We also prove that `f` is bijective on each set `pts_of_period f n` and on `periodic_pts f`. Finally, we prove that `x` is a periodic point of `f` of period `n` if and only if `minimal_period f x | n`. ## References * https://en.wikipedia.org/wiki/Periodic_point -/ open set namespace function variables {α : Type*} {β : Type*} {f fa : α → α} {fb : β → β} {x y : α} {m n : ℕ} /-- A point `x` is a periodic point of `f : α → α` of period `n` if `f^[n] x = x`. Note that we do not require `0 < n` in this definition. Many theorems about periodic points need this assumption. -/ def is_periodic_pt (f : α → α) (n : ℕ) (x : α) := is_fixed_pt (f^[n]) x /-- A fixed point of `f` is a periodic point of `f` of any prescribed period. -/ lemma is_fixed_pt.is_periodic_pt (hf : is_fixed_pt f x) (n : ℕ) : is_periodic_pt f n x := hf.iterate n /-- For the identity map, all points are periodic. -/ lemma is_periodic_id (n : ℕ) (x : α) : is_periodic_pt id n x := (is_fixed_pt_id x).is_periodic_pt n /-- Any point is a periodic point of period `0`. -/ lemma is_periodic_pt_zero (f : α → α) (x : α) : is_periodic_pt f 0 x := is_fixed_pt_id x namespace is_periodic_pt instance [decidable_eq α] {f : α → α} {n : ℕ} {x : α} : decidable (is_periodic_pt f n x) := is_fixed_pt.decidable protected lemma is_fixed_pt (hf : is_periodic_pt f n x) : is_fixed_pt (f^[n]) x := hf protected lemma map (hx : is_periodic_pt fa n x) {g : α → β} (hg : semiconj g fa fb) : is_periodic_pt fb n (g x) := hx.map (hg.iterate_right n) lemma apply_iterate (hx : is_periodic_pt f n x) (m : ℕ) : is_periodic_pt f n (f^[m] x) := hx.map $ commute.iterate_self f m protected lemma apply (hx : is_periodic_pt f n x) : is_periodic_pt f n (f x) := hx.apply_iterate 1 protected lemma add (hn : is_periodic_pt f n x) (hm : is_periodic_pt f m x) : is_periodic_pt f (n + m) x := by { rw [is_periodic_pt, iterate_add], exact hn.comp hm } lemma left_of_add (hn : is_periodic_pt f (n + m) x) (hm : is_periodic_pt f m x) : is_periodic_pt f n x := by { rw [is_periodic_pt, iterate_add] at hn, exact hn.left_of_comp hm } lemma right_of_add (hn : is_periodic_pt f (n + m) x) (hm : is_periodic_pt f n x) : is_periodic_pt f m x := by { rw add_comm at hn, exact hn.left_of_add hm } protected lemma sub (hm : is_periodic_pt f m x) (hn : is_periodic_pt f n x) : is_periodic_pt f (m - n) x := begin cases le_total n m with h h, { refine left_of_add _ hn, rwa [tsub_add_cancel_of_le h] }, { rw [tsub_eq_zero_iff_le.mpr h], apply is_periodic_pt_zero } end protected lemma mul_const (hm : is_periodic_pt f m x) (n : ℕ) : is_periodic_pt f (m * n) x := by simp only [is_periodic_pt, iterate_mul, hm.is_fixed_pt.iterate n] protected lemma const_mul (hm : is_periodic_pt f m x) (n : ℕ) : is_periodic_pt f (n * m) x := by simp only [mul_comm n, hm.mul_const n] lemma trans_dvd (hm : is_periodic_pt f m x) {n : ℕ} (hn : m ∣ n) : is_periodic_pt f n x := let ⟨k, hk⟩ := hn in hk.symm ▸ hm.mul_const k protected lemma iterate (hf : is_periodic_pt f n x) (m : ℕ) : is_periodic_pt (f^[m]) n x := begin rw [is_periodic_pt, ← iterate_mul, mul_comm, iterate_mul], exact hf.is_fixed_pt.iterate m end lemma comp {g : α → α} (hco : commute f g) (hf : is_periodic_pt f n x) (hg : is_periodic_pt g n x) : is_periodic_pt (f ∘ g) n x := by { rw [is_periodic_pt, hco.comp_iterate], exact hf.comp hg } lemma comp_lcm {g : α → α} (hco : commute f g) (hf : is_periodic_pt f m x) (hg : is_periodic_pt g n x) : is_periodic_pt (f ∘ g) (nat.lcm m n) x := (hf.trans_dvd $ nat.dvd_lcm_left _ _).comp hco (hg.trans_dvd $ nat.dvd_lcm_right _ _) lemma left_of_comp {g : α → α} (hco : commute f g) (hfg : is_periodic_pt (f ∘ g) n x) (hg : is_periodic_pt g n x) : is_periodic_pt f n x := begin rw [is_periodic_pt, hco.comp_iterate] at hfg, exact hfg.left_of_comp hg end lemma iterate_mod_apply (h : is_periodic_pt f n x) (m : ℕ) : f^[m % n] x = (f^[m] x) := by conv_rhs { rw [← nat.mod_add_div m n, iterate_add_apply, (h.mul_const _).eq] } protected lemma mod (hm : is_periodic_pt f m x) (hn : is_periodic_pt f n x) : is_periodic_pt f (m % n) x := (hn.iterate_mod_apply m).trans hm protected lemma gcd (hm : is_periodic_pt f m x) (hn : is_periodic_pt f n x) : is_periodic_pt f (m.gcd n) x := begin revert hm hn, refine nat.gcd.induction m n (λ n h0 hn, _) (λ m n hm ih hm hn, _), { rwa [nat.gcd_zero_left], }, { rw [nat.gcd_rec], exact ih (hn.mod hm) hm } end /-- If `f` sends two periodic points `x` and `y` of the same positive period to the same point, then `x = y`. For a similar statement about points of different periods see `eq_of_apply_eq`. -/ lemma eq_of_apply_eq_same (hx : is_periodic_pt f n x) (hy : is_periodic_pt f n y) (hn : 0 < n) (h : f x = f y) : x = y := by rw [← hx.eq, ← hy.eq, ← iterate_pred_comp_of_pos f hn, comp_app, h] /-- If `f` sends two periodic points `x` and `y` of positive periods to the same point, then `x = y`. -/ lemma eq_of_apply_eq (hx : is_periodic_pt f m x) (hy : is_periodic_pt f n y) (hm : 0 < m) (hn : 0 < n) (h : f x = f y) : x = y := (hx.mul_const n).eq_of_apply_eq_same (hy.const_mul m) (mul_pos hm hn) h end is_periodic_pt /-- The set of periodic points of a given (possibly non-minimal) period. -/ def pts_of_period (f : α → α) (n : ℕ) : set α := {x : α | is_periodic_pt f n x} @[simp] lemma mem_pts_of_period : x ∈ pts_of_period f n ↔ is_periodic_pt f n x := iff.rfl lemma semiconj.maps_to_pts_of_period {g : α → β} (h : semiconj g fa fb) (n : ℕ) : maps_to g (pts_of_period fa n) (pts_of_period fb n) := (h.iterate_right n).maps_to_fixed_pts lemma bij_on_pts_of_period (f : α → α) {n : ℕ} (hn : 0 < n) : bij_on f (pts_of_period f n) (pts_of_period f n) := ⟨(commute.refl f).maps_to_pts_of_period n, λ x hx y hy hxy, hx.eq_of_apply_eq_same hy hn hxy, λ x hx, ⟨f^[n.pred] x, hx.apply_iterate _, by rw [← comp_app f, comp_iterate_pred_of_pos f hn, hx.eq]⟩⟩ lemma directed_pts_of_period_pnat (f : α → α) : directed (⊆) (λ n : ℕ+, pts_of_period f n) := λ m n, ⟨m * n, λ x hx, hx.mul_const n, λ x hx, hx.const_mul m⟩ /-- The set of periodic points of a map `f : α → α`. -/ def periodic_pts (f : α → α) : set α := {x : α | ∃ n > 0, is_periodic_pt f n x} lemma mk_mem_periodic_pts (hn : 0 < n) (hx : is_periodic_pt f n x) : x ∈ periodic_pts f := ⟨n, hn, hx⟩ lemma mem_periodic_pts : x ∈ periodic_pts f ↔ ∃ n > 0, is_periodic_pt f n x := iff.rfl lemma is_periodic_pt_of_mem_periodic_pts_of_is_periodic_pt_iterate (hx : x ∈ periodic_pts f) (hm : is_periodic_pt f m (f^[n] x)) : is_periodic_pt f m x := begin rcases hx with ⟨r, hr, hr'⟩, convert (hm.apply_iterate ((n / r + 1) * r - n)).eq, suffices : n ≤ (n / r + 1) * r, { rw [←iterate_add_apply, nat.sub_add_cancel this, iterate_mul, (hr'.iterate _).eq] }, rw [add_mul, one_mul], exact (nat.lt_div_mul_add hr).le end variable (f) lemma bUnion_pts_of_period : (⋃ n > 0, pts_of_period f n) = periodic_pts f := set.ext $ λ x, by simp [mem_periodic_pts] lemma Union_pnat_pts_of_period : (⋃ n : ℕ+, pts_of_period f n) = periodic_pts f := supr_subtype.trans $ bUnion_pts_of_period f lemma bij_on_periodic_pts : bij_on f (periodic_pts f) (periodic_pts f) := Union_pnat_pts_of_period f ▸ bij_on_Union_of_directed (directed_pts_of_period_pnat f) (λ i, bij_on_pts_of_period f i.pos) variable {f} lemma semiconj.maps_to_periodic_pts {g : α → β} (h : semiconj g fa fb) : maps_to g (periodic_pts fa) (periodic_pts fb) := λ x ⟨n, hn, hx⟩, ⟨n, hn, hx.map h⟩ open_locale classical noncomputable theory /-- Minimal period of a point `x` under an endomorphism `f`. If `x` is not a periodic point of `f`, then `minimal_period f x = 0`. -/ def minimal_period (f : α → α) (x : α) := if h : x ∈ periodic_pts f then nat.find h else 0 lemma is_periodic_pt_minimal_period (f : α → α) (x : α) : is_periodic_pt f (minimal_period f x) x := begin delta minimal_period, split_ifs with hx, { exact (nat.find_spec hx).snd }, { exact is_periodic_pt_zero f x } end @[simp] lemma iterate_minimal_period : f^[minimal_period f x] x = x := is_periodic_pt_minimal_period f x @[simp] lemma iterate_add_minimal_period_eq : f^[n + minimal_period f x] x = (f^[n] x) := by { rw iterate_add_apply, congr, exact is_periodic_pt_minimal_period f x } @[simp] lemma iterate_mod_minimal_period_eq : f^[n % minimal_period f x] x = (f^[n] x) := (is_periodic_pt_minimal_period f x).iterate_mod_apply n lemma minimal_period_pos_of_mem_periodic_pts (hx : x ∈ periodic_pts f) : 0 < minimal_period f x := by simp only [minimal_period, dif_pos hx, (nat.find_spec hx).fst.lt] lemma minimal_period_eq_zero_of_nmem_periodic_pts (hx : x ∉ periodic_pts f) : minimal_period f x = 0 := by simp only [minimal_period, dif_neg hx] lemma is_periodic_pt.minimal_period_pos (hn : 0 < n) (hx : is_periodic_pt f n x) : 0 < minimal_period f x := minimal_period_pos_of_mem_periodic_pts $ mk_mem_periodic_pts hn hx lemma minimal_period_pos_iff_mem_periodic_pts : 0 < minimal_period f x ↔ x ∈ periodic_pts f := ⟨not_imp_not.1 $ λ h, by simp only [minimal_period, dif_neg h, lt_irrefl 0, not_false_iff], minimal_period_pos_of_mem_periodic_pts⟩ lemma minimal_period_eq_zero_iff_nmem_periodic_pts : minimal_period f x = 0 ↔ x ∉ periodic_pts f := by rw [←minimal_period_pos_iff_mem_periodic_pts, not_lt, nonpos_iff_eq_zero] lemma is_periodic_pt.minimal_period_le (hn : 0 < n) (hx : is_periodic_pt f n x) : minimal_period f x ≤ n := begin rw [minimal_period, dif_pos (mk_mem_periodic_pts hn hx)], exact nat.find_min' (mk_mem_periodic_pts hn hx) ⟨hn, hx⟩ end lemma minimal_period_apply_iterate (hx : x ∈ periodic_pts f) (n : ℕ) : minimal_period f (f^[n] x) = minimal_period f x := begin apply (is_periodic_pt.minimal_period_le (minimal_period_pos_of_mem_periodic_pts hx) _).antisymm ((is_periodic_pt_of_mem_periodic_pts_of_is_periodic_pt_iterate hx (is_periodic_pt_minimal_period f _)).minimal_period_le (minimal_period_pos_of_mem_periodic_pts _)), { exact (is_periodic_pt_minimal_period f x).apply_iterate n, }, { rcases hx with ⟨m, hm, hx⟩, exact ⟨m, hm, hx.apply_iterate n⟩ } end lemma minimal_period_apply (hx : x ∈ periodic_pts f) : minimal_period f (f x) = minimal_period f x := minimal_period_apply_iterate hx 1 lemma le_of_lt_minimal_period_of_iterate_eq {m n : ℕ} (hm : m < minimal_period f x) (hmn : f^[m] x = (f^[n] x)) : m ≤ n := begin by_contra' hmn', rw [←nat.add_sub_of_le hmn'.le, add_comm, iterate_add_apply] at hmn, exact ((is_periodic_pt.minimal_period_le (tsub_pos_of_lt hmn') (is_periodic_pt_of_mem_periodic_pts_of_is_periodic_pt_iterate (minimal_period_pos_iff_mem_periodic_pts.1 ((zero_le m).trans_lt hm)) hmn)).trans (nat.sub_le m n)).not_lt hm end lemma eq_of_lt_minimal_period_of_iterate_eq {m n : ℕ} (hm : m < minimal_period f x) (hn : n < minimal_period f x) (hmn : f^[m] x = (f^[n] x)) : m = n := (le_of_lt_minimal_period_of_iterate_eq hm hmn).antisymm (le_of_lt_minimal_period_of_iterate_eq hn hmn.symm) lemma eq_iff_lt_minimal_period_of_iterate_eq {m n : ℕ} (hm : m < minimal_period f x) (hn : n < minimal_period f x) : f^[m] x = (f^[n] x) ↔ m = n := ⟨eq_of_lt_minimal_period_of_iterate_eq hm hn, congr_arg _⟩ lemma minimal_period_id : minimal_period id x = 1 := ((is_periodic_id _ _ ).minimal_period_le nat.one_pos).antisymm (nat.succ_le_of_lt ((is_periodic_id _ _ ).minimal_period_pos nat.one_pos)) lemma is_fixed_point_iff_minimal_period_eq_one : minimal_period f x = 1 ↔ is_fixed_pt f x := begin refine ⟨λ h, _, λ h, _⟩, { rw ← iterate_one f, refine function.is_periodic_pt.is_fixed_pt _, rw ← h, exact is_periodic_pt_minimal_period f x }, { exact ((h.is_periodic_pt 1).minimal_period_le nat.one_pos).antisymm (nat.succ_le_of_lt ((h.is_periodic_pt 1).minimal_period_pos nat.one_pos)) } end lemma is_periodic_pt.eq_zero_of_lt_minimal_period (hx : is_periodic_pt f n x) (hn : n < minimal_period f x) : n = 0 := eq.symm $ (eq_or_lt_of_le $ n.zero_le).resolve_right $ λ hn0, not_lt.2 (hx.minimal_period_le hn0) hn lemma not_is_periodic_pt_of_pos_of_lt_minimal_period : ∀ {n : ℕ} (n0 : n ≠ 0) (hn : n < minimal_period f x), ¬ is_periodic_pt f n x | 0 n0 _ := (n0 rfl).elim | (n + 1) _ hn := λ hp, nat.succ_ne_zero _ (hp.eq_zero_of_lt_minimal_period hn) lemma is_periodic_pt.minimal_period_dvd (hx : is_periodic_pt f n x) : minimal_period f x ∣ n := (eq_or_lt_of_le $ n.zero_le).elim (λ hn0, hn0 ▸ dvd_zero _) $ λ hn0, nat.dvd_iff_mod_eq_zero.2 $ (hx.mod $ is_periodic_pt_minimal_period f x).eq_zero_of_lt_minimal_period $ nat.mod_lt _ $ hx.minimal_period_pos hn0 lemma is_periodic_pt_iff_minimal_period_dvd : is_periodic_pt f n x ↔ minimal_period f x ∣ n := ⟨is_periodic_pt.minimal_period_dvd, λ h, (is_periodic_pt_minimal_period f x).trans_dvd h⟩ open nat lemma minimal_period_eq_minimal_period_iff {g : β → β} {y : β} : minimal_period f x = minimal_period g y ↔ ∀ n, is_periodic_pt f n x ↔ is_periodic_pt g n y := by simp_rw [is_periodic_pt_iff_minimal_period_dvd, dvd_right_iff_eq] lemma minimal_period_eq_prime {p : ℕ} [hp : fact p.prime] (hper : is_periodic_pt f p x) (hfix : ¬ is_fixed_pt f x) : minimal_period f x = p := (hp.out.eq_one_or_self_of_dvd _ (hper.minimal_period_dvd)).resolve_left (mt is_fixed_point_iff_minimal_period_eq_one.1 hfix) lemma minimal_period_eq_prime_pow {p k : ℕ} [hp : fact p.prime] (hk : ¬ is_periodic_pt f (p ^ k) x) (hk1 : is_periodic_pt f (p ^ (k + 1)) x) : minimal_period f x = p ^ (k + 1) := begin apply nat.eq_prime_pow_of_dvd_least_prime_pow hp.out; rwa ← is_periodic_pt_iff_minimal_period_dvd end lemma commute.minimal_period_of_comp_dvd_lcm {g : α → α} (h : function.commute f g) : minimal_period (f ∘ g) x ∣ nat.lcm (minimal_period f x) (minimal_period g x) := begin rw [← is_periodic_pt_iff_minimal_period_dvd], exact (is_periodic_pt_minimal_period f x).comp_lcm h (is_periodic_pt_minimal_period g x) end lemma commute.minimal_period_of_comp_dvd_mul {g : α → α} (h : function.commute f g) : minimal_period (f ∘ g) x ∣ (minimal_period f x) * (minimal_period g x) := dvd_trans h.minimal_period_of_comp_dvd_lcm (lcm_dvd_mul _ _) lemma commute.minimal_period_of_comp_eq_mul_of_coprime {g : α → α} (h : function.commute f g) (hco : coprime (minimal_period f x) (minimal_period g x)) : minimal_period (f ∘ g) x = (minimal_period f x) * (minimal_period g x) := begin apply dvd_antisymm (h.minimal_period_of_comp_dvd_mul), suffices : ∀ {f g : α → α}, commute f g → coprime (minimal_period f x) (minimal_period g x) → minimal_period f x ∣ minimal_period (f ∘ g) x, from hco.mul_dvd_of_dvd_of_dvd (this h hco) (h.comp_eq.symm ▸ this h.symm hco.symm), clear hco h f g, intros f g h hco, refine hco.dvd_of_dvd_mul_left (is_periodic_pt.left_of_comp h _ _).minimal_period_dvd, { exact (is_periodic_pt_minimal_period _ _).const_mul _ }, { exact (is_periodic_pt_minimal_period _ _).mul_const _ } end private lemma minimal_period_iterate_eq_div_gcd_aux (h : 0 < gcd (minimal_period f x) n) : minimal_period (f ^[n]) x = minimal_period f x / nat.gcd (minimal_period f x) n := begin apply nat.dvd_antisymm, { apply is_periodic_pt.minimal_period_dvd, rw [is_periodic_pt, is_fixed_pt, ← iterate_mul, ← nat.mul_div_assoc _ (gcd_dvd_left _ _), mul_comm, nat.mul_div_assoc _ (gcd_dvd_right _ _), mul_comm, iterate_mul], exact (is_periodic_pt_minimal_period f x).iterate _ }, { apply coprime.dvd_of_dvd_mul_right (coprime_div_gcd_div_gcd h), apply dvd_of_mul_dvd_mul_right h, rw [nat.div_mul_cancel (gcd_dvd_left _ _), mul_assoc, nat.div_mul_cancel (gcd_dvd_right _ _), mul_comm], apply is_periodic_pt.minimal_period_dvd, rw [is_periodic_pt, is_fixed_pt, iterate_mul], exact is_periodic_pt_minimal_period _ _ } end lemma minimal_period_iterate_eq_div_gcd (h : n ≠ 0) : minimal_period (f ^[n]) x = minimal_period f x / nat.gcd (minimal_period f x) n := minimal_period_iterate_eq_div_gcd_aux $ gcd_pos_of_pos_right _ (nat.pos_of_ne_zero h) lemma minimal_period_iterate_eq_div_gcd' (h : x ∈ periodic_pts f) : minimal_period (f ^[n]) x = minimal_period f x / nat.gcd (minimal_period f x) n := minimal_period_iterate_eq_div_gcd_aux $ gcd_pos_of_pos_left n (minimal_period_pos_iff_mem_periodic_pts.mpr h) /-- The orbit of a periodic point `x` of `f` is the cycle `[x, f x, f (f x), ...]`. Its length is the minimal period of `x`. If `x` is not a periodic point, then this is the empty (aka nil) cycle. -/ def periodic_orbit (f : α → α) (x : α) : cycle α := (list.range (minimal_period f x)).map (λ n, f^[n] x) /-- The definition of a periodic orbit, in terms of `list.map`. -/ lemma periodic_orbit_def (f : α → α) (x : α) : periodic_orbit f x = (list.range (minimal_period f x)).map (λ n, f^[n] x) := rfl /-- The definition of a periodic orbit, in terms of `cycle.map`. -/ lemma periodic_orbit_eq_cycle_map (f : α → α) (x : α) : periodic_orbit f x = (list.range (minimal_period f x) : cycle ℕ).map (λ n, f^[n] x) := rfl @[simp] lemma periodic_orbit_length : (periodic_orbit f x).length = minimal_period f x := by rw [periodic_orbit, cycle.length_coe, list.length_map, list.length_range] @[simp] lemma periodic_orbit_eq_nil_iff_not_periodic_pt : periodic_orbit f x = cycle.nil ↔ x ∉ periodic_pts f := by { simp [periodic_orbit], exact minimal_period_eq_zero_iff_nmem_periodic_pts } lemma periodic_orbit_eq_nil_of_not_periodic_pt (h : x ∉ periodic_pts f) : periodic_orbit f x = cycle.nil := periodic_orbit_eq_nil_iff_not_periodic_pt.2 h @[simp] lemma mem_periodic_orbit_iff (hx : x ∈ periodic_pts f) : y ∈ periodic_orbit f x ↔ ∃ n, f^[n] x = y := begin simp only [periodic_orbit, cycle.mem_coe_iff, list.mem_map, list.mem_range], use λ ⟨a, ha, ha'⟩, ⟨a, ha'⟩, rintro ⟨n, rfl⟩, use [n % minimal_period f x, mod_lt _ (minimal_period_pos_of_mem_periodic_pts hx)], rw iterate_mod_minimal_period_eq end @[simp] lemma iterate_mem_periodic_orbit (hx : x ∈ periodic_pts f) (n : ℕ) : f^[n] x ∈ periodic_orbit f x := (mem_periodic_orbit_iff hx).2 ⟨n, rfl⟩ @[simp] lemma self_mem_periodic_orbit (hx : x ∈ periodic_pts f) : x ∈ periodic_orbit f x := iterate_mem_periodic_orbit hx 0 lemma nodup_periodic_orbit : (periodic_orbit f x).nodup := begin rw [periodic_orbit, cycle.nodup_coe_iff, list.nodup_map_iff_inj_on (list.nodup_range _)], intros m hm n hn hmn, rw list.mem_range at hm hn, rwa eq_iff_lt_minimal_period_of_iterate_eq hm hn at hmn end lemma periodic_orbit_apply_iterate_eq (hx : x ∈ periodic_pts f) (n : ℕ) : periodic_orbit f (f^[n] x) = periodic_orbit f x := eq.symm $ cycle.coe_eq_coe.2 $ ⟨n, begin apply list.ext_le _ (λ m _ _, _), { simp [minimal_period_apply_iterate hx] }, { rw list.nth_le_rotate _ n m, simp [iterate_add_apply] } end⟩ lemma periodic_orbit_apply_eq (hx : x ∈ periodic_pts f) : periodic_orbit f (f x) = periodic_orbit f x := periodic_orbit_apply_iterate_eq hx 1 theorem periodic_orbit_chain (r : α → α → Prop) {f : α → α} {x : α} : (periodic_orbit f x).chain r ↔ ∀ n < minimal_period f x, r (f^[n] x) (f^[n+1] x) := begin by_cases hx : x ∈ periodic_pts f, { have hx' := minimal_period_pos_of_mem_periodic_pts hx, have hM := nat.sub_add_cancel (succ_le_iff.2 hx'), rw [periodic_orbit, ←cycle.map_coe, cycle.chain_map, ←hM, cycle.chain_range_succ], refine ⟨_, λ H, ⟨_, λ m hm, H _ (hm.trans (nat.lt_succ_self _))⟩⟩, { rintro ⟨hr, H⟩ n hn, cases eq_or_lt_of_le (lt_succ_iff.1 hn) with hM' hM', { rwa [hM', hM, iterate_minimal_period] }, { exact H _ hM' } }, { rw iterate_zero_apply, nth_rewrite 2 ←@iterate_minimal_period α f x, nth_rewrite 1 ←hM, exact H _ (nat.lt_succ_self _) } }, { rw [periodic_orbit_eq_nil_of_not_periodic_pt hx, minimal_period_eq_zero_of_nmem_periodic_pts hx], simp } end theorem periodic_orbit_chain' (r : α → α → Prop) {f : α → α} {x : α} (hx : x ∈ periodic_pts f) : (periodic_orbit f x).chain r ↔ ∀ n, r (f^[n] x) (f^[n+1] x) := begin rw periodic_orbit_chain r, refine ⟨λ H n, _, λ H n _, H n⟩, rw [iterate_succ_apply, ←iterate_mod_minimal_period_eq], nth_rewrite 1 ←iterate_mod_minimal_period_eq, rw [←iterate_succ_apply, minimal_period_apply hx], exact H _ (mod_lt _ (minimal_period_pos_of_mem_periodic_pts hx)) end end function namespace mul_action open function variables {α β : Type*} [group α] [mul_action α β] {a : α} {b : β} @[to_additive] lemma pow_smul_eq_iff_minimal_period_dvd {n : ℕ} : a ^ n • b = b ↔ function.minimal_period ((•) a) b ∣ n := by rw [←is_periodic_pt_iff_minimal_period_dvd, is_periodic_pt, is_fixed_pt, smul_iterate] @[to_additive] lemma zpow_smul_eq_iff_minimal_period_dvd {n : ℤ} : a ^ n • b = b ↔ (function.minimal_period ((•) a) b : ℤ) ∣ n := begin cases n, { rw [int.of_nat_eq_coe, zpow_coe_nat, int.coe_nat_dvd, pow_smul_eq_iff_minimal_period_dvd] }, { rw [int.neg_succ_of_nat_coe, zpow_neg, zpow_coe_nat, inv_smul_eq_iff, eq_comm, dvd_neg, int.coe_nat_dvd, pow_smul_eq_iff_minimal_period_dvd] }, end variables (a b) @[simp, to_additive] lemma pow_smul_mod_minimal_period (n : ℕ) : a ^ (n % function.minimal_period ((•) a) b) • b = a ^ n • b := by conv_rhs { rw [← nat.mod_add_div n (minimal_period ((•) a) b), pow_add, mul_smul, pow_smul_eq_iff_minimal_period_dvd.mpr (dvd_mul_right _ _)] } @[simp, to_additive] lemma zpow_smul_mod_minimal_period (n : ℤ) : a ^ (n % (function.minimal_period ((•) a) b : ℤ)) • b = a ^ n • b := by conv_rhs { rw [← int.mod_add_div n (minimal_period ((•) a) b), zpow_add, mul_smul, zpow_smul_eq_iff_minimal_period_dvd.mpr (dvd_mul_right _ _)] } end mul_action
8166bf6c395b574fe0fbea6d1f0958409b2ab486
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/topology/metric_space/gromov_hausdorff_realized.lean
491e31341e7072cc57a8f238b0040a80eaf02180
[ "Apache-2.0" ]
permissive
anrddh/mathlib
6a374da53c7e3a35cb0298b0cd67824efef362b4
a4266a01d2dcb10de19369307c986d038c7bb6a6
refs/heads/master
1,656,710,827,909
1,589,560,456,000
1,589,560,456,000
264,271,800
0
0
Apache-2.0
1,589,568,062,000
1,589,568,061,000
null
UTF-8
Lean
false
false
26,971
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sébastien Gouëzel Construction of a good coupling between nonempty compact metric spaces, minimizing their Hausdorff distance. This construction is instrumental to study the Gromov-Hausdorff distance between nonempty compact metric spaces -/ import topology.metric_space.gluing import topology.metric_space.hausdorff_distance noncomputable theory open_locale classical open_locale topological_space universes u v w open classical set function topological_space filter metric quotient open bounded_continuous_function open sum (inl inr) local attribute [instance] metric_space_sum namespace Gromov_Hausdorff section Gromov_Hausdorff_realized /- This section shows that the Gromov-Hausdorff distance is realized. For this, we consider candidate distances on the disjoint union α ⊕ β of two compact nonempty metric spaces, almost realizing the Gromov-Hausdorff distance, and show that they form a compact family by applying Arzela-Ascoli theorem. The existence of a minimizer follows. -/ section definitions variables (α : Type u) (β : Type v) [metric_space α] [compact_space α] [nonempty α] [metric_space β] [compact_space β] [nonempty β] @[reducible] private def prod_space_fun : Type* := ((α ⊕ β) × (α ⊕ β)) → ℝ @[reducible] private def Cb : Type* := bounded_continuous_function ((α ⊕ β) × (α ⊕ β)) ℝ private def max_var : nnreal := 2 * ⟨diam (univ : set α), diam_nonneg⟩ + 1 + 2 * ⟨diam (univ : set β), diam_nonneg⟩ private lemma one_le_max_var : 1 ≤ max_var α β := calc (1 : real) = 2 * 0 + 1 + 2 * 0 : by simp ... ≤ 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β) : by apply_rules [add_le_add, mul_le_mul_of_nonneg_left, diam_nonneg, diam_nonneg]; norm_num /-- The set of functions on α ⊕ β that are candidates distances to realize the minimum of the Hausdorff distances between α and β in a coupling -/ def candidates : set (prod_space_fun α β) := {f | (((((∀x y : α, f (sum.inl x, sum.inl y) = dist x y) ∧ (∀x y : β, f (sum.inr x, sum.inr y) = dist x y)) ∧ (∀x y, f (x, y) = f (y, x))) ∧ (∀x y z, f (x, z) ≤ f (x, y) + f (y, z))) ∧ (∀x, f (x, x) = 0)) ∧ (∀x y, f (x, y) ≤ max_var α β) } /-- Version of the set of candidates in bounded_continuous_functions, to apply Arzela-Ascoli -/ private def candidates_b : set (Cb α β) := {f : Cb α β | f.val ∈ candidates α β} end definitions --section section constructions variables {α : Type u} {β : Type v} [metric_space α] [compact_space α] [nonempty α] [metric_space β] [compact_space β] [nonempty β] {f : prod_space_fun α β} {x y z t : α ⊕ β} local attribute [instance, priority 10] inhabited_of_nonempty' private lemma max_var_bound : dist x y ≤ max_var α β := calc dist x y ≤ diam (univ : set (α ⊕ β)) : dist_le_diam_of_mem (bounded_of_compact compact_univ) (mem_univ _) (mem_univ _) ... = diam (inl '' (univ : set α) ∪ inr '' (univ : set β)) : by apply congr_arg; ext x y z; cases x; simp [mem_univ, mem_range_self] ... ≤ diam (inl '' (univ : set α)) + dist (inl (default α)) (inr (default β)) + diam (inr '' (univ : set β)) : diam_union (mem_image_of_mem _ (mem_univ _)) (mem_image_of_mem _ (mem_univ _)) ... = diam (univ : set α) + (dist (default α) (default α) + 1 + dist (default β) (default β)) + diam (univ : set β) : by { rw [isometry_on_inl.diam_image, isometry_on_inr.diam_image], refl } ... = 1 * diam (univ : set α) + 1 + 1 * diam (univ : set β) : by simp ... ≤ 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β) : begin apply_rules [add_le_add, mul_le_mul_of_nonneg_right, diam_nonneg, diam_nonneg, le_refl], norm_num, norm_num end private lemma candidates_symm (fA : f ∈ candidates α β) : f (x, y) = f (y ,x) := fA.1.1.1.2 x y private lemma candidates_triangle (fA : f ∈ candidates α β) : f (x, z) ≤ f (x, y) + f (y, z) := fA.1.1.2 x y z private lemma candidates_refl (fA : f ∈ candidates α β) : f (x, x) = 0 := fA.1.2 x private lemma candidates_nonneg (fA : f ∈ candidates α β) : 0 ≤ f (x, y) := begin have : 0 ≤ 2 * f (x, y) := calc 0 = f (x, x) : (candidates_refl fA).symm ... ≤ f (x, y) + f (y, x) : candidates_triangle fA ... = f (x, y) + f (x, y) : by rw [candidates_symm fA] ... = 2 * f (x, y) : by ring, by linarith end private lemma candidates_dist_inl (fA : f ∈ candidates α β) (x y: α) : f (inl x, inl y) = dist x y := fA.1.1.1.1.1 x y private lemma candidates_dist_inr (fA : f ∈ candidates α β) (x y : β) : f (inr x, inr y) = dist x y := fA.1.1.1.1.2 x y private lemma candidates_le_max_var (fA : f ∈ candidates α β) : f (x, y) ≤ max_var α β := fA.2 x y /-- candidates are bounded by max_var α β -/ private lemma candidates_dist_bound (fA : f ∈ candidates α β) : ∀ {x y : α ⊕ β}, f (x, y) ≤ max_var α β * dist x y | (inl x) (inl y) := calc f (inl x, inl y) = dist x y : candidates_dist_inl fA x y ... = dist (inl x) (inl y) : by { rw @sum.dist_eq α β, refl } ... = 1 * dist (inl x) (inl y) : by simp ... ≤ max_var α β * dist (inl x) (inl y) : mul_le_mul_of_nonneg_right (one_le_max_var α β) dist_nonneg | (inl x) (inr y) := calc f (inl x, inr y) ≤ max_var α β : candidates_le_max_var fA ... = max_var α β * 1 : by simp ... ≤ max_var α β * dist (inl x) (inr y) : mul_le_mul_of_nonneg_left sum.one_dist_le (le_trans (zero_le_one) (one_le_max_var α β)) | (inr x) (inl y) := calc f (inr x, inl y) ≤ max_var α β : candidates_le_max_var fA ... = max_var α β * 1 : by simp ... ≤ max_var α β * dist (inl x) (inr y) : mul_le_mul_of_nonneg_left sum.one_dist_le (le_trans (zero_le_one) (one_le_max_var α β)) | (inr x) (inr y) := calc f (inr x, inr y) = dist x y : candidates_dist_inr fA x y ... = dist (inr x) (inr y) : by { rw @sum.dist_eq α β, refl } ... = 1 * dist (inr x) (inr y) : by simp ... ≤ max_var α β * dist (inr x) (inr y) : mul_le_mul_of_nonneg_right (one_le_max_var α β) dist_nonneg /-- Technical lemma to prove that candidates are Lipschitz -/ private lemma candidates_lipschitz_aux (fA : f ∈ candidates α β) : f (x, y) - f (z, t) ≤ 2 * max_var α β * dist (x, y) (z, t) := calc f (x, y) - f(z, t) ≤ f (x, t) + f (t, y) - f (z, t) : add_le_add_right (candidates_triangle fA) _ ... ≤ (f (x, z) + f (z, t) + f(t, y)) - f (z, t) : add_le_add_right (add_le_add_right (candidates_triangle fA) _ ) _ ... = f (x, z) + f (t, y) : by simp [sub_eq_add_neg] ... ≤ max_var α β * dist x z + max_var α β * dist t y : add_le_add (candidates_dist_bound fA) (candidates_dist_bound fA) ... ≤ max_var α β * max (dist x z) (dist t y) + max_var α β * max (dist x z) (dist t y) : begin apply add_le_add, apply mul_le_mul_of_nonneg_left (le_max_left (dist x z) (dist t y)) (le_trans zero_le_one (one_le_max_var α β)), apply mul_le_mul_of_nonneg_left (le_max_right (dist x z) (dist t y)) (le_trans zero_le_one (one_le_max_var α β)), end ... = 2 * max_var α β * max (dist x z) (dist y t) : by { simp [dist_comm], ring } ... = 2 * max_var α β * dist (x, y) (z, t) : by refl /-- Candidates are Lipschitz -/ private lemma candidates_lipschitz (fA : f ∈ candidates α β) : lipschitz_with (2 * max_var α β) f := begin apply lipschitz_with.of_dist_le_mul, rintros ⟨x, y⟩ ⟨z, t⟩, rw real.dist_eq, apply abs_le_of_le_of_neg_le, { exact candidates_lipschitz_aux fA }, { have : -(f (x, y) - f (z, t)) = f (z, t) - f (x, y), by ring, rw [this, dist_comm], exact candidates_lipschitz_aux fA } end /-- candidates give rise to elements of bounded_continuous_functions -/ def candidates_b_of_candidates (f : prod_space_fun α β) (fA : f ∈ candidates α β) : Cb α β := bounded_continuous_function.mk_of_compact f (candidates_lipschitz fA).continuous lemma candidates_b_of_candidates_mem (f : prod_space_fun α β) (fA : f ∈ candidates α β) : candidates_b_of_candidates f fA ∈ candidates_b α β := fA /-- The distance on α ⊕ β is a candidate -/ private lemma dist_mem_candidates : (λp : (α ⊕ β) × (α ⊕ β), dist p.1 p.2) ∈ candidates α β := begin simp only [candidates, dist_comm, forall_const, and_true, add_comm, eq_self_iff_true, and_self, sum.forall, set.mem_set_of_eq, dist_self], repeat { split <|> exact (λa y z, dist_triangle_left _ _ _) <|> exact (λx y, by refl) <|> exact (λx y, max_var_bound) } end def candidates_b_dist (α : Type u) (β : Type v) [metric_space α] [compact_space α] [inhabited α] [metric_space β] [compact_space β] [inhabited β] : Cb α β := candidates_b_of_candidates _ dist_mem_candidates lemma candidates_b_dist_mem_candidates_b : candidates_b_dist α β ∈ candidates_b α β := candidates_b_of_candidates_mem _ _ private lemma candidates_b_nonempty : (candidates_b α β).nonempty := ⟨_, candidates_b_dist_mem_candidates_b⟩ /-- To apply Arzela-Ascoli, we need to check that the set of candidates is closed and equicontinuous. Equicontinuity follows from the Lipschitz control, we check closedness -/ private lemma closed_candidates_b : is_closed (candidates_b α β) := begin have I1 : ∀x y, is_closed {f : Cb α β | f (inl x, inl y) = dist x y} := λx y, is_closed_eq continuous_evalx continuous_const, have I2 : ∀x y, is_closed {f : Cb α β | f (inr x, inr y) = dist x y } := λx y, is_closed_eq continuous_evalx continuous_const, have I3 : ∀x y, is_closed {f : Cb α β | f (x, y) = f (y, x)} := λx y, is_closed_eq continuous_evalx continuous_evalx, have I4 : ∀x y z, is_closed {f : Cb α β | f (x, z) ≤ f (x, y) + f (y, z)} := λx y z, is_closed_le continuous_evalx (continuous_evalx.add continuous_evalx), have I5 : ∀x, is_closed {f : Cb α β | f (x, x) = 0} := λx, is_closed_eq continuous_evalx continuous_const, have I6 : ∀x y, is_closed {f : Cb α β | f (x, y) ≤ max_var α β} := λx y, is_closed_le continuous_evalx continuous_const, have : candidates_b α β = (⋂x y, {f : Cb α β | f ((@inl α β x), (@inl α β y)) = dist x y}) ∩ (⋂x y, {f : Cb α β | f ((@inr α β x), (@inr α β y)) = dist x y}) ∩ (⋂x y, {f : Cb α β | f (x, y) = f (y, x)}) ∩ (⋂x y z, {f : Cb α β | f (x, z) ≤ f (x, y) + f (y, z)}) ∩ (⋂x, {f : Cb α β | f (x, x) = 0}) ∩ (⋂x y, {f : Cb α β | f (x, y) ≤ max_var α β}) := begin ext, unfold candidates_b, unfold candidates, simp [-sum.forall], refl end, rw this, repeat { apply is_closed_inter _ _ <|> apply is_closed_Inter _ <|> apply I1 _ _ <|> apply I2 _ _ <|> apply I3 _ _ <|> apply I4 _ _ _ <|> apply I5 _ <|> apply I6 _ _ <|> assume x }, end /-- Compactness of candidates (in bounded_continuous_functions) follows -/ private lemma compact_candidates_b : compact (candidates_b α β) := begin refine arzela_ascoli₂ (Icc 0 (max_var α β)) compact_Icc (candidates_b α β) closed_candidates_b _ _, { rintros f ⟨x1, x2⟩ hf, simp only [set.mem_Icc], exact ⟨candidates_nonneg hf, candidates_le_max_var hf⟩ }, { refine equicontinuous_of_continuity_modulus (λt, 2 * max_var α β * t) _ _ _, { have : tendsto (λ (t : ℝ), 2 * (max_var α β : ℝ) * t) (𝓝 0) (𝓝 (2 * max_var α β * 0)) := tendsto_const_nhds.mul tendsto_id, simpa using this }, { assume x y f hf, exact (candidates_lipschitz hf).dist_le_mul _ _ } } end /-- We will then choose the candidate minimizing the Hausdorff distance. Except that we are not in a metric space setting, so we need to define our custom version of Hausdorff distance, called HD, and prove its basic properties. -/ def HD (f : Cb α β) := max (supr (λx:α, infi (λy:β, f (inl x, inr y)))) (supr (λy:β, infi (λx:α, f (inl x, inr y)))) /- We will show that HD is continuous on bounded_continuous_functions, to deduce that its minimum on the compact set candidates_b is attained. Since it is defined in terms of infimum and supremum on ℝ, which is only conditionnally complete, we will need all the time to check that the defining sets are bounded below or above. This is done in the next few technical lemmas -/ lemma HD_below_aux1 {f : Cb α β} (C : ℝ) {x : α} : bdd_below (range (λ (y : β), f (inl x, inr y) + C)) := let ⟨cf, hcf⟩ := (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 in ⟨cf + C, forall_range_iff.2 (λi, add_le_add_right ((λx, hcf (mem_range_self x)) _) _)⟩ private lemma HD_bound_aux1 (f : Cb α β) (C : ℝ) : bdd_above (range (λ (x : α), infi (λy:β, f (inl x, inr y) + C))) := begin rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).2 with ⟨Cf, hCf⟩, refine ⟨Cf + C, forall_range_iff.2 (λx, _)⟩, calc infi (λy:β, f (inl x, inr y) + C) ≤ f (inl x, inr (default β)) + C : cinfi_le (HD_below_aux1 C) (default β) ... ≤ Cf + C : add_le_add ((λx, hCf (mem_range_self x)) _) (le_refl _) end lemma HD_below_aux2 {f : Cb α β} (C : ℝ) {y : β} : bdd_below (range (λ (x : α), f (inl x, inr y) + C)) := let ⟨cf, hcf⟩ := (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 in ⟨cf + C, forall_range_iff.2 (λi, add_le_add_right ((λx, hcf (mem_range_self x)) _) _)⟩ private lemma HD_bound_aux2 (f : Cb α β) (C : ℝ) : bdd_above (range (λ (y : β), infi (λx:α, f (inl x, inr y) + C))) := begin rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).2 with ⟨Cf, hCf⟩, refine ⟨Cf + C, forall_range_iff.2 (λy, _)⟩, calc infi (λx:α, f (inl x, inr y) + C) ≤ f (inl (default α), inr y) + C : cinfi_le (HD_below_aux2 C) (default α) ... ≤ Cf + C : add_le_add ((λx, hCf (mem_range_self x)) _) (le_refl _) end /-- Explicit bound on HD (dist). This means that when looking for minimizers it will be sufficient to look for functions with HD(f) bounded by this bound. -/ lemma HD_candidates_b_dist_le : HD (candidates_b_dist α β) ≤ diam (univ : set α) + 1 + diam (univ : set β) := begin refine max_le (csupr_le (λx, _)) (csupr_le (λy, _)), { have A : infi (λy:β, candidates_b_dist α β (inl x, inr y)) ≤ candidates_b_dist α β (inl x, inr (default β)) := cinfi_le (by simpa using HD_below_aux1 0) (default β), have B : dist (inl x) (inr (default β)) ≤ diam (univ : set α) + 1 + diam (univ : set β) := calc dist (inl x) (inr (default β)) = dist x (default α) + 1 + dist (default β) (default β) : rfl ... ≤ diam (univ : set α) + 1 + diam (univ : set β) : begin apply add_le_add (add_le_add _ (le_refl _)), exact dist_le_diam_of_mem (bounded_of_compact (compact_univ)) (mem_univ _) (mem_univ _), exact dist_le_diam_of_mem (bounded_of_compact (compact_univ)) (mem_univ _) (mem_univ _) end, exact le_trans A B }, { have A : infi (λx:α, candidates_b_dist α β (inl x, inr y)) ≤ candidates_b_dist α β (inl (default α), inr y) := cinfi_le (by simpa using HD_below_aux2 0) (default α), have B : dist (inl (default α)) (inr y) ≤ diam (univ : set α) + 1 + diam (univ : set β) := calc dist (inl (default α)) (inr y) = dist (default α) (default α) + 1 + dist (default β) y : rfl ... ≤ diam (univ : set α) + 1 + diam (univ : set β) : begin apply add_le_add (add_le_add _ (le_refl _)), exact dist_le_diam_of_mem (bounded_of_compact (compact_univ)) (mem_univ _) (mem_univ _), exact dist_le_diam_of_mem (bounded_of_compact (compact_univ)) (mem_univ _) (mem_univ _) end, exact le_trans A B }, end /- To check that HD is continuous, we check that it is Lipschitz. As HD is a max, we prove separately inequalities controlling the two terms (relying too heavily on copy-paste...) -/ private lemma HD_lipschitz_aux1 (f g : Cb α β) : supr (λx:α, infi (λy:β, f (inl x, inr y))) ≤ supr (λx:α, infi (λy:β, g (inl x, inr y))) + dist f g := begin rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 with ⟨cg, hcg⟩, have Hcg : ∀x, cg ≤ g x := λx, hcg (mem_range_self x), rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 with ⟨cf, hcf⟩, have Hcf : ∀x, cf ≤ f x := λx, hcf (mem_range_self x), -- prove the inequality but with `dist f g` inside, by using inequalities comparing -- supr to supr and infi to infi have Z : supr (λx:α, infi (λy:β, f (inl x, inr y))) ≤ supr (λx:α, infi (λy:β, g (inl x, inr y) + dist f g)) := csupr_le_csupr (HD_bound_aux1 _ (dist f g)) (λx, cinfi_le_cinfi ⟨cf, forall_range_iff.2(λi, Hcf _)⟩ (λy, coe_le_coe_add_dist)), -- move the `dist f g` out of the infimum and the supremum, arguing that continuous monotone maps -- (here the addition of `dist f g`) preserve infimum and supremum have E1 : ∀x, infi (λy:β, g (inl x, inr y)) + dist f g = infi ((λz, z + dist f g) ∘ (λy:β, (g (inl x, inr y)))), { assume x, refine cinfi_of_cinfi_of_monotone_of_continuous (_ : continuous (λ (z : ℝ), z + dist f g)) _ _, { exact continuous_id.add continuous_const }, { assume x y hx, simpa }, { show bdd_below (range (λ (y : β), g (inl x, inr y))), from ⟨cg, forall_range_iff.2(λi, Hcg _)⟩ } }, have E2 : supr (λx:α, infi (λy:β, g (inl x, inr y))) + dist f g = supr ((λz, z + dist f g) ∘ (λx:α, infi (λy:β, g (inl x, inr y)))), { refine csupr_of_csupr_of_monotone_of_continuous (_ : continuous (λ (z : ℝ), z + dist f g)) _ _, { exact continuous_id.add continuous_const }, { assume x y hx, simpa }, { by simpa using HD_bound_aux1 _ 0 } }, -- deduce the result from the above two steps simpa [E2, E1, function.comp] end private lemma HD_lipschitz_aux2 (f g : Cb α β) : supr (λy:β, infi (λx:α, f (inl x, inr y))) ≤ supr (λy:β, infi (λx:α, g (inl x, inr y))) + dist f g := begin rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 with ⟨cg, hcg⟩, have Hcg : ∀x, cg ≤ g x := λx, hcg (mem_range_self x), rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 with ⟨cf, hcf⟩, have Hcf : ∀x, cf ≤ f x := λx, hcf (mem_range_self x), -- prove the inequality but with `dist f g` inside, by using inequalities comparing -- supr to supr and infi to infi have Z : supr (λy:β, infi (λx:α, f (inl x, inr y))) ≤ supr (λy:β, infi (λx:α, g (inl x, inr y) + dist f g)) := csupr_le_csupr (HD_bound_aux2 _ (dist f g)) (λy, cinfi_le_cinfi ⟨cf, forall_range_iff.2(λi, Hcf _)⟩ (λy, coe_le_coe_add_dist)), -- move the `dist f g` out of the infimum and the supremum, arguing that continuous monotone maps -- (here the addition of `dist f g`) preserve infimum and supremum have E1 : ∀y, infi (λx:α, g (inl x, inr y)) + dist f g = infi ((λz, z + dist f g) ∘ (λx:α, (g (inl x, inr y)))), { assume y, refine cinfi_of_cinfi_of_monotone_of_continuous (_ : continuous (λ (z : ℝ), z + dist f g)) _ _, { exact continuous_id.add continuous_const }, { assume x y hx, simpa }, { show bdd_below (range (λx:α, g (inl x, inr y))), from ⟨cg, forall_range_iff.2(λi, Hcg _)⟩ } }, have E2 : supr (λy:β, infi (λx:α, g (inl x, inr y))) + dist f g = supr ((λz, z + dist f g) ∘ (λy:β, infi (λx:α, g (inl x, inr y)))), { refine csupr_of_csupr_of_monotone_of_continuous (_ : continuous (λ (z : ℝ), z + dist f g)) _ _, { exact continuous_id.add continuous_const }, { assume x y hx, simpa }, { by simpa using HD_bound_aux2 _ 0 } }, -- deduce the result from the above two steps simpa [E2, E1, function.comp] end private lemma HD_lipschitz_aux3 (f g : Cb α β) : HD f ≤ HD g + dist f g := max_le (le_trans (HD_lipschitz_aux1 f g) (add_le_add_right (le_max_left _ _) _)) (le_trans (HD_lipschitz_aux2 f g) (add_le_add_right (le_max_right _ _) _)) /-- Conclude that HD, being Lipschitz, is continuous -/ private lemma HD_continuous : continuous (HD : Cb α β → ℝ) := lipschitz_with.continuous (lipschitz_with.of_le_add HD_lipschitz_aux3) end constructions --section section consequences variables (α : Type u) (β : Type v) [metric_space α] [compact_space α] [nonempty α] [metric_space β] [compact_space β] [nonempty β] /- Now that we have proved that the set of candidates is compact, and that HD is continuous, we can finally select a candidate minimizing HD. This will be the candidate realizing the optimal coupling. -/ private lemma exists_minimizer : ∃f ∈ candidates_b α β, ∀g ∈ candidates_b α β, HD f ≤ HD g := compact_candidates_b.exists_forall_le candidates_b_nonempty HD_continuous.continuous_on private definition optimal_GH_dist : Cb α β := classical.some (exists_minimizer α β) private lemma optimal_GH_dist_mem_candidates_b : optimal_GH_dist α β ∈ candidates_b α β := by cases (classical.some_spec (exists_minimizer α β)); assumption private lemma HD_optimal_GH_dist_le (g : Cb α β) (hg : g ∈ candidates_b α β) : HD (optimal_GH_dist α β) ≤ HD g := let ⟨Z1, Z2⟩ := classical.some_spec (exists_minimizer α β) in Z2 g hg /-- With the optimal candidate, construct a premetric space structure on α ⊕ β, on which the predistance is given by the candidate. Then, we will identify points at 0 predistance to obtain a genuine metric space -/ def premetric_optimal_GH_dist : premetric_space (α ⊕ β) := { dist := λp q, optimal_GH_dist α β (p, q), dist_self := λx, candidates_refl (optimal_GH_dist_mem_candidates_b α β), dist_comm := λx y, candidates_symm (optimal_GH_dist_mem_candidates_b α β), dist_triangle := λx y z, candidates_triangle (optimal_GH_dist_mem_candidates_b α β) } local attribute [instance] premetric_optimal_GH_dist premetric.dist_setoid /-- A metric space which realizes the optimal coupling between α and β -/ @[derive [metric_space]] definition optimal_GH_coupling : Type* := premetric.metric_quot (α ⊕ β) /-- Injection of α in the optimal coupling between α and β -/ def optimal_GH_injl (x : α) : optimal_GH_coupling α β := ⟦inl x⟧ /-- The injection of α in the optimal coupling between α and β is an isometry. -/ lemma isometry_optimal_GH_injl : isometry (optimal_GH_injl α β) := begin refine isometry_emetric_iff_metric.2 (λx y, _), change dist ⟦inl x⟧ ⟦inl y⟧ = dist x y, exact candidates_dist_inl (optimal_GH_dist_mem_candidates_b α β) _ _, end /-- Injection of β in the optimal coupling between α and β -/ def optimal_GH_injr (y : β) : optimal_GH_coupling α β := ⟦inr y⟧ /-- The injection of β in the optimal coupling between α and β is an isometry. -/ lemma isometry_optimal_GH_injr : isometry (optimal_GH_injr α β) := begin refine isometry_emetric_iff_metric.2 (λx y, _), change dist ⟦inr x⟧ ⟦inr y⟧ = dist x y, exact candidates_dist_inr (optimal_GH_dist_mem_candidates_b α β) _ _, end /-- The optimal coupling between two compact spaces α and β is still a compact space -/ instance compact_space_optimal_GH_coupling : compact_space (optimal_GH_coupling α β) := ⟨begin have : (univ : set (optimal_GH_coupling α β)) = (optimal_GH_injl α β '' univ) ∪ (optimal_GH_injr α β '' univ), { refine subset.antisymm (λxc hxc, _) (subset_univ _), rcases quotient.exists_rep xc with ⟨x, hx⟩, cases x; rw ← hx, { have : ⟦inl x⟧ = optimal_GH_injl α β x := rfl, rw this, exact mem_union_left _ (mem_image_of_mem _ (mem_univ _)) }, { have : ⟦inr x⟧ = optimal_GH_injr α β x := rfl, rw this, exact mem_union_right _ (mem_image_of_mem _ (mem_univ _)) } }, rw this, exact (compact_univ.image (isometry_optimal_GH_injl α β).continuous).union (compact_univ.image (isometry_optimal_GH_injr α β).continuous) end⟩ /-- For any candidate f, HD(f) is larger than or equal to the Hausdorff distance in the optimal coupling. This follows from the fact that HD of the optimal candidate is exactly the Hausdorff distance in the optimal coupling, although we only prove here the inequality we need. -/ lemma Hausdorff_dist_optimal_le_HD {f} (h : f ∈ candidates_b α β) : Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ HD f := begin refine le_trans (le_of_forall_le_of_dense (λr hr, _)) (HD_optimal_GH_dist_le α β f h), have A : ∀ x ∈ range (optimal_GH_injl α β), ∃ y ∈ range (optimal_GH_injr α β), dist x y ≤ r, { assume x hx, rcases mem_range.1 hx with ⟨z, hz⟩, rw ← hz, have I1 : supr (λx:α, infi (λy:β, optimal_GH_dist α β (inl x, inr y))) < r := lt_of_le_of_lt (le_max_left _ _) hr, have I2 : infi (λy:β, optimal_GH_dist α β (inl z, inr y)) ≤ supr (λx:α, infi (λy:β, optimal_GH_dist α β (inl x, inr y))) := le_cSup (by simpa using HD_bound_aux1 _ 0) (mem_range_self _), have I : infi (λy:β, optimal_GH_dist α β (inl z, inr y)) < r := lt_of_le_of_lt I2 I1, rcases exists_lt_of_cInf_lt (range_nonempty _) I with ⟨r', r'range, hr'⟩, rcases mem_range.1 r'range with ⟨z', hz'⟩, existsi [optimal_GH_injr α β z', mem_range_self _], have : (optimal_GH_dist α β) (inl z, inr z') ≤ r := begin rw hz', exact le_of_lt hr' end, exact this }, refine Hausdorff_dist_le_of_mem_dist _ A _, { rcases exists_mem_of_nonempty α with ⟨xα, _⟩, have : optimal_GH_injl α β xα ∈ range (optimal_GH_injl α β) := mem_range_self _, rcases A _ this with ⟨y, yrange, hy⟩, exact le_trans dist_nonneg hy }, { assume y hy, rcases mem_range.1 hy with ⟨z, hz⟩, rw ← hz, have I1 : supr (λy:β, infi (λx:α, optimal_GH_dist α β (inl x, inr y))) < r := lt_of_le_of_lt (le_max_right _ _) hr, have I2 : infi (λx:α, optimal_GH_dist α β (inl x, inr z)) ≤ supr (λy:β, infi (λx:α, optimal_GH_dist α β (inl x, inr y))) := le_cSup (by simpa using HD_bound_aux2 _ 0) (mem_range_self _), have I : infi (λx:α, optimal_GH_dist α β (inl x, inr z)) < r := lt_of_le_of_lt I2 I1, rcases exists_lt_of_cInf_lt (range_nonempty _) I with ⟨r', r'range, hr'⟩, rcases mem_range.1 r'range with ⟨z', hz'⟩, existsi [optimal_GH_injl α β z', mem_range_self _], have : (optimal_GH_dist α β) (inl z', inr z) ≤ r := begin rw hz', exact le_of_lt hr' end, rw dist_comm, exact this } end end consequences /- We are done with the construction of the optimal coupling -/ end Gromov_Hausdorff_realized end Gromov_Hausdorff
f89dd5c9c56172e22852fb84c037b3bd6c5b9387
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/instances/ereal.lean
3148a9199d1abf8dc8d3b47eb8c74f7ea6aed869
[ "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
13,251
lean
/- Copyright (c) 2021 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 data.real.ereal import topology.algebra.order.monotone_continuity import topology.instances.ennreal /-! # Topological structure on `ereal` We endow `ereal` with the order topology, and prove basic properties of this topology. ## Main results * `coe : ℝ → ereal` is an open embedding * `coe : ℝ≥0∞ → ereal` is an embedding * The addition on `ereal` is continuous except at `(⊥, ⊤)` and at `(⊤, ⊥)`. * Negation is a homeomorphism on `ereal`. ## Implementation Most proofs are adapted from the corresponding proofs on `ℝ≥0∞`. -/ noncomputable theory open classical set filter metric topological_space open_locale classical topological_space ennreal nnreal big_operators filter variables {α : Type*} [topological_space α] namespace ereal instance : topological_space ereal := preorder.topology ereal instance : order_topology ereal := ⟨rfl⟩ instance : t2_space ereal := by apply_instance instance : second_countable_topology ereal := ⟨begin refine ⟨⋃ (q : ℚ), {{a : ereal | a < (q : ℝ)}, {a : ereal | ((q : ℝ) : ereal) < a}}, countable_Union (λ a, (countable_singleton _).insert _), _⟩, refine le_antisymm (le_generate_from $ by simp [or_imp_distrib, is_open_lt', is_open_gt'] {contextual := tt}) _, apply le_generate_from (λ s h, _), rcases h with ⟨a, hs | hs⟩; [ rw show s = ⋃q∈{q:ℚ | a < (q : ℝ)}, {b | ((q : ℝ) : ereal) < b}, by { ext x, simpa only [hs, exists_prop, mem_Union] using lt_iff_exists_rat_btwn }, rw show s = ⋃q∈{q:ℚ | ((q : ℝ) : ereal) < a}, {b | b < ((q : ℝ) : ereal)}, by { ext x, simpa only [hs, and_comm, exists_prop, mem_Union] using lt_iff_exists_rat_btwn }]; { apply is_open_Union, intro q, apply is_open_Union, intro hq, apply generate_open.basic, exact mem_Union.2 ⟨q, by simp⟩ }, end⟩ /-! ### Real coercion -/ lemma embedding_coe : embedding (coe : ℝ → ereal) := ⟨⟨begin refine le_antisymm _ _, { rw [@order_topology.topology_eq_generate_intervals ereal _, ← coinduced_le_iff_le_induced], refine le_generate_from (assume s ha, _), rcases ha with ⟨a, rfl | rfl⟩, show is_open {b : ℝ | a < ↑b}, { induction a using ereal.rec, { simp only [is_open_univ, bot_lt_coe, set_of_true] }, { simp only [ereal.coe_lt_coe_iff], exact is_open_Ioi }, { simp only [set_of_false, is_open_empty, not_top_lt] } }, show is_open {b : ℝ | ↑b < a}, { induction a using ereal.rec, { simp only [not_lt_bot, set_of_false, is_open_empty] }, { simp only [ereal.coe_lt_coe_iff], exact is_open_Iio }, { simp only [is_open_univ, coe_lt_top, set_of_true] } } }, { rw [@order_topology.topology_eq_generate_intervals ℝ _], refine le_generate_from (assume s ha, _), rcases ha with ⟨a, rfl | rfl⟩, exact ⟨Ioi a, is_open_Ioi, by simp [Ioi]⟩, exact ⟨Iio a, is_open_Iio, by simp [Iio]⟩ } end⟩, assume a b, by simp only [imp_self, ereal.coe_eq_coe_iff]⟩ lemma open_embedding_coe : open_embedding (coe : ℝ → ereal) := ⟨embedding_coe, begin convert @is_open_Ioo ereal _ _ _ ⊥ ⊤, ext x, induction x using ereal.rec, { simp only [left_mem_Ioo, mem_range, coe_ne_bot, exists_false, not_false_iff] }, { simp only [mem_range_self, mem_Ioo, bot_lt_coe, coe_lt_top, and_self] }, { simp only [mem_range, right_mem_Ioo, exists_false, coe_ne_top] } end⟩ @[norm_cast] lemma tendsto_coe {α : Type*} {f : filter α} {m : α → ℝ} {a : ℝ} : tendsto (λ a, (m a : ereal)) f (𝓝 ↑a) ↔ tendsto m f (𝓝 a) := embedding_coe.tendsto_nhds_iff.symm lemma _root_.continuous_coe_real_ereal : continuous (coe : ℝ → ereal) := embedding_coe.continuous lemma continuous_coe_iff {f : α → ℝ} : continuous (λa, (f a : ereal)) ↔ continuous f := embedding_coe.continuous_iff.symm lemma nhds_coe {r : ℝ} : 𝓝 (r : ereal) = (𝓝 r).map coe := (open_embedding_coe.map_nhds_eq r).symm lemma nhds_coe_coe {r p : ℝ} : 𝓝 ((r : ereal), (p : ereal)) = (𝓝 (r, p)).map (λp:ℝ × ℝ, (p.1, p.2)) := ((open_embedding_coe.prod open_embedding_coe).map_nhds_eq (r, p)).symm lemma tendsto_to_real {a : ereal} (ha : a ≠ ⊤) (h'a : a ≠ ⊥) : tendsto ereal.to_real (𝓝 a) (𝓝 a.to_real) := begin lift a to ℝ using and.intro ha h'a, rw [nhds_coe, tendsto_map'_iff], exact tendsto_id end lemma continuous_on_to_real : continuous_on ereal.to_real ({⊥, ⊤} : set ereal).compl := λ a ha, continuous_at.continuous_within_at (tendsto_to_real (by { simp [not_or_distrib] at ha, exact ha.2 }) (by { simp [not_or_distrib] at ha, exact ha.1 })) /-- The set of finite `ereal` numbers is homeomorphic to `ℝ`. -/ def ne_bot_top_homeomorph_real : ({⊥, ⊤} : set ereal).compl ≃ₜ ℝ := { continuous_to_fun := continuous_on_iff_continuous_restrict.1 continuous_on_to_real, continuous_inv_fun := continuous_subtype_mk _ continuous_coe_real_ereal, .. ne_top_bot_equiv_real } /-! ### ennreal coercion -/ lemma embedding_coe_ennreal : embedding (coe : ℝ≥0∞ → ereal) := ⟨⟨begin refine le_antisymm _ _, { rw [@order_topology.topology_eq_generate_intervals ereal _, ← coinduced_le_iff_le_induced], refine le_generate_from (assume s ha, _), rcases ha with ⟨a, rfl | rfl⟩, show is_open {b : ℝ≥0∞ | a < ↑b}, { induction a using ereal.rec with x, { simp only [is_open_univ, bot_lt_coe_ennreal, set_of_true] }, { rcases le_or_lt 0 x with h|h, { have : (x : ereal) = ((id ⟨x, h⟩ : ℝ≥0) : ℝ≥0∞) := rfl, rw this, simp only [id.def, coe_ennreal_lt_coe_ennreal_iff], exact is_open_Ioi, }, { have : ∀ (y : ℝ≥0∞), (x : ereal) < y := λ y, (ereal.coe_lt_coe_iff.2 h).trans_le (coe_ennreal_nonneg _), simp only [this, is_open_univ, set_of_true] } }, { simp only [set_of_false, is_open_empty, not_top_lt] } }, show is_open {b : ℝ≥0∞ | ↑b < a}, { induction a using ereal.rec with x, { simp only [not_lt_bot, set_of_false, is_open_empty] }, { rcases le_or_lt 0 x with h|h, { have : (x : ereal) = ((id ⟨x, h⟩ : ℝ≥0) : ℝ≥0∞) := rfl, rw this, simp only [id.def, coe_ennreal_lt_coe_ennreal_iff], exact is_open_Iio, }, { convert is_open_empty, apply eq_empty_iff_forall_not_mem.2 (λ y hy, lt_irrefl (x : ereal) _), exact ((ereal.coe_lt_coe_iff.2 h).trans_le (coe_ennreal_nonneg y)).trans hy } }, { simp only [← coe_ennreal_top, coe_ennreal_lt_coe_ennreal_iff], exact is_open_Iio } } }, { rw [@order_topology.topology_eq_generate_intervals ℝ≥0∞ _], refine le_generate_from (assume s ha, _), rcases ha with ⟨a, rfl | rfl⟩, exact ⟨Ioi a, is_open_Ioi, by simp [Ioi]⟩, exact ⟨Iio a, is_open_Iio, by simp [Iio]⟩ } end⟩, assume a b, by simp only [imp_self, coe_ennreal_eq_coe_ennreal_iff]⟩ @[norm_cast] lemma tendsto_coe_ennreal {α : Type*} {f : filter α} {m : α → ℝ≥0∞} {a : ℝ≥0∞} : tendsto (λ a, (m a : ereal)) f (𝓝 ↑a) ↔ tendsto m f (𝓝 a) := embedding_coe_ennreal.tendsto_nhds_iff.symm lemma _root_.continuous_coe_ennreal_ereal : continuous (coe : ℝ≥0∞ → ereal) := embedding_coe_ennreal.continuous lemma continuous_coe_ennreal_iff {f : α → ℝ≥0∞} : continuous (λa, (f a : ereal)) ↔ continuous f := embedding_coe_ennreal.continuous_iff.symm /-! ### Neighborhoods of infinity -/ lemma nhds_top : 𝓝 (⊤ : ereal) = ⨅ a ≠ ⊤, 𝓟 (Ioi a) := nhds_top_order.trans $ by simp [lt_top_iff_ne_top, Ioi] lemma nhds_top' : 𝓝 (⊤ : ereal) = ⨅ a : ℝ, 𝓟 (Ioi a) := begin rw [nhds_top], apply le_antisymm, { exact infi_mono' (λ x, ⟨x, by simp⟩) }, { refine le_infi (λ r, le_infi (λ hr, _)), induction r using ereal.rec, { exact (infi_le _ 0).trans (by simp) }, { exact infi_le _ _ }, { simpa using hr, } } end lemma mem_nhds_top_iff {s : set ereal} : s ∈ 𝓝 (⊤ : ereal) ↔ ∃ (y : ℝ), Ioi (y : ereal) ⊆ s := begin rw [nhds_top', mem_infi_of_directed], { refl }, exact λ x y, ⟨max x y, by simp [le_refl], by simp [le_refl]⟩, end lemma tendsto_nhds_top_iff_real {α : Type*} {m : α → ereal} {f : filter α} : tendsto m f (𝓝 ⊤) ↔ ∀ x : ℝ, ∀ᶠ a in f, ↑x < m a := by simp only [nhds_top', mem_Ioi, tendsto_infi, tendsto_principal] lemma nhds_bot : 𝓝 (⊥ : ereal) = ⨅ a ≠ ⊥, 𝓟 (Iio a) := nhds_bot_order.trans $ by simp [bot_lt_iff_ne_bot] lemma nhds_bot' : 𝓝 (⊥ : ereal) = ⨅ a : ℝ, 𝓟 (Iio a) := begin rw [nhds_bot], apply le_antisymm, { exact infi_mono' (λ x, ⟨x, by simp⟩) }, { refine le_infi (λ r, le_infi (λ hr, _)), induction r using ereal.rec, { simpa using hr }, { exact infi_le _ _ }, { exact (infi_le _ 0).trans (by simp) } } end lemma mem_nhds_bot_iff {s : set ereal} : s ∈ 𝓝 (⊥ : ereal) ↔ ∃ (y : ℝ), Iio (y : ereal) ⊆ s := begin rw [nhds_bot', mem_infi_of_directed], { refl }, exact λ x y, ⟨min x y, by simp [le_refl], by simp [le_refl]⟩, end lemma tendsto_nhds_bot_iff_real {α : Type*} {m : α → ereal} {f : filter α} : tendsto m f (𝓝 ⊥) ↔ ∀ x : ℝ, ∀ᶠ a in f, m a < x := by simp only [nhds_bot', mem_Iio, tendsto_infi, tendsto_principal] /-! ### Continuity of addition -/ lemma continuous_at_add_coe_coe (a b :ℝ) : continuous_at (λ (p : ereal × ereal), p.1 + p.2) (a, b) := by simp only [continuous_at, nhds_coe_coe, ← coe_add, tendsto_map'_iff, (∘), tendsto_coe, tendsto_add] lemma continuous_at_add_top_coe (a : ℝ) : continuous_at (λ (p : ereal × ereal), p.1 + p.2) (⊤, a) := begin simp only [continuous_at, tendsto_nhds_top_iff_real, top_add, nhds_prod_eq], assume r, rw eventually_prod_iff, refine ⟨λ z, ((r - (a - 1): ℝ) : ereal) < z, Ioi_mem_nhds (coe_lt_top _), λ z, ((a - 1 : ℝ) : ereal) < z, Ioi_mem_nhds (by simp [zero_lt_one]), λ x hx y hy, _⟩, dsimp, convert add_lt_add hx hy, simp, end lemma continuous_at_add_coe_top (a : ℝ) : continuous_at (λ (p : ereal × ereal), p.1 + p.2) (a, ⊤) := begin change continuous_at ((λ (p : ereal × ereal), p.2 + p.1) ∘ prod.swap) (a, ⊤), apply continuous_at.comp _ continuous_swap.continuous_at, simp_rw add_comm, exact continuous_at_add_top_coe a end lemma continuous_at_add_top_top : continuous_at (λ (p : ereal × ereal), p.1 + p.2) (⊤, ⊤) := begin simp only [continuous_at, tendsto_nhds_top_iff_real, top_add, nhds_prod_eq], assume r, rw eventually_prod_iff, refine ⟨λ z, (r : ereal) < z, Ioi_mem_nhds (coe_lt_top _), λ z, ((0 : ℝ) : ereal) < z, Ioi_mem_nhds (by simp [zero_lt_one]), λ x hx y hy, _⟩, dsimp, convert add_lt_add hx hy, simp, end lemma continuous_at_add_bot_coe (a : ℝ) : continuous_at (λ (p : ereal × ereal), p.1 + p.2) (⊥, a) := begin simp only [continuous_at, tendsto_nhds_bot_iff_real, nhds_prod_eq, bot_add_coe], assume r, rw eventually_prod_iff, refine ⟨λ z, z < ((r - (a + 1): ℝ) : ereal), Iio_mem_nhds (bot_lt_coe _), λ z, z < ((a + 1 : ℝ) : ereal), Iio_mem_nhds (by simp [-coe_add, zero_lt_one]), λ x hx y hy, _⟩, convert add_lt_add hx hy, rw sub_add_cancel, end lemma continuous_at_add_coe_bot (a : ℝ) : continuous_at (λ (p : ereal × ereal), p.1 + p.2) (a, ⊥) := begin change continuous_at ((λ (p : ereal × ereal), p.2 + p.1) ∘ prod.swap) (a, ⊥), apply continuous_at.comp _ continuous_swap.continuous_at, simp_rw add_comm, exact continuous_at_add_bot_coe a end lemma continuous_at_add_bot_bot : continuous_at (λ (p : ereal × ereal), p.1 + p.2) (⊥, ⊥) := begin simp only [continuous_at, tendsto_nhds_bot_iff_real, nhds_prod_eq, bot_add_bot], assume r, rw eventually_prod_iff, refine ⟨λ z, z < r, Iio_mem_nhds (bot_lt_coe _), λ z, z < 0, Iio_mem_nhds (bot_lt_coe _), λ x hx y hy, _⟩, dsimp, convert add_lt_add hx hy, simp end /-- The addition on `ereal` is continuous except where it doesn't make sense (i.e., at `(⊥, ⊤)` and at `(⊤, ⊥)`). -/ lemma continuous_at_add {p : ereal × ereal} (h : p.1 ≠ ⊤ ∨ p.2 ≠ ⊥) (h' : p.1 ≠ ⊥ ∨ p.2 ≠ ⊤) : continuous_at (λ (p : ereal × ereal), p.1 + p.2) p := begin rcases p with ⟨x, y⟩, induction x using ereal.rec; induction y using ereal.rec, { exact continuous_at_add_bot_bot }, { exact continuous_at_add_bot_coe _ }, { simpa using h' }, { exact continuous_at_add_coe_bot _ }, { exact continuous_at_add_coe_coe _ _ }, { exact continuous_at_add_coe_top _ }, { simpa using h }, { exact continuous_at_add_top_coe _ }, { exact continuous_at_add_top_top }, end /-! ### Negation-/ /-- Negation on `ereal` as a homeomorphism -/ def neg_homeo : ereal ≃ₜ ereal := neg_order_iso.to_homeomorph lemma continuous_neg : continuous (λ (x : ereal), -x) := neg_homeo.continuous end ereal
84f539a99785b79d733bd43201fd772f764c38e8
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/coe.lean
f2414a799f9f8cbc253d9c8fad042ded3c7ec6de
[]
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
7,718
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.PrePort import Mathlib.Lean3Lib.init.data.list.basic import Mathlib.Lean3Lib.init.data.subtype.basic import Mathlib.Lean3Lib.init.data.prod universes u v l u₁ u₂ u₃ ua₁ ua₂ ub₁ ub₂ ua ub namespace Mathlib /- The elaborator tries to insert coercions automatically. Only instances of has_coe type class are considered in the process. Lean also provides a "lifting" operator: ↑a. It uses all instances of has_lift type class. Every has_coe instance is also a has_lift instance. We recommend users only use has_coe for coercions that do not produce a lot of ambiguity. All coercions and lifts can be identified with the constant coe. We use the has_coe_to_fun type class for encoding coercions from a type to a function space. We use the has_coe_to_sort type class for encoding coercions from a type to a sort. -/ /-- Can perform a lifting operation `↑a`. -/ class has_lift (a : Sort u) (b : Sort v) where lift : a → b /-- Auxiliary class that contains the transitive closure of has_lift. -/ class has_lift_t (a : Sort u) (b : Sort v) where lift : a → b class has_coe (a : Sort u) (b : Sort v) where coe : a → b /-- Auxiliary class that contains the transitive closure of has_coe. -/ class has_coe_t (a : Sort u) (b : Sort v) where coe : a → b class has_coe_to_fun (a : Sort u) where F : a → Sort v coe : (x : a) → F x class has_coe_to_sort (a : Sort u) where S : Sort v coe : a → S def lift {a : Sort u} {b : Sort v} [has_lift a b] : a → b := has_lift.lift def lift_t {a : Sort u} {b : Sort v} [has_lift_t a b] : a → b := has_lift_t.lift def coe_b {a : Sort u} {b : Sort v} [has_coe a b] : a → b := has_coe.coe def coe_t {a : Sort u} {b : Sort v} [has_coe_t a b] : a → b := has_coe_t.coe def coe_fn_b {a : Sort u} [has_coe_to_fun a] (x : a) : has_coe_to_fun.F x := has_coe_to_fun.coe /- User level coercion operators -/ def coe {a : Sort u} {b : Sort v} [has_lift_t a b] : a → b := lift_t def coe_fn {a : Sort u} [has_coe_to_fun a] (x : a) : has_coe_to_fun.F x := has_coe_to_fun.coe def coe_sort {a : Sort u} [has_coe_to_sort a] : a → has_coe_to_sort.S a := has_coe_to_sort.coe prefix:1024 "↑" => Mathlib.coe prefix:1024 "⇑" => Mathlib.coe_fn prefix:1024 "↥" => Mathlib.coe_sort /- Notation -/ /- Transitive closure for has_lift, has_coe, has_coe_to_fun -/ protected instance lift_trans {a : Sort u₁} {b : Sort u₂} {c : Sort u₃} [has_lift_t b c] [has_lift a b] : has_lift_t a c := has_lift_t.mk fun (x : a) => lift_t (lift x) protected instance lift_base {a : Sort u} {b : Sort v} [has_lift a b] : has_lift_t a b := has_lift_t.mk lift protected instance coe_trans {a : Sort u₁} {b : Sort u₂} {c : Sort u₃} [has_coe_t b c] [has_coe a b] : has_coe_t a c := has_coe_t.mk fun (x : a) => coe_t (coe_b x) protected instance coe_base {a : Sort u} {b : Sort v} [has_coe a b] : has_coe_t a b := has_coe_t.mk coe_b /- We add this instance directly into has_coe_t to avoid non-termination. Suppose coe_option had type (has_coe a (option a)). Then, we can loop when searching a coercion from α to β (has_coe_t α β) 1- coe_trans at (has_coe_t α β) (has_coe α ?b₁) and (has_coe_t ?b₁ c) 2- coe_option at (has_coe α ?b₁) ?b₁ := option α 3- coe_trans at (has_coe_t (option α) β) (has_coe (option α) ?b₂) and (has_coe_t ?b₂ β) 4- coe_option at (has_coe (option α) ?b₂) ?b₂ := option (option α)) ... -/ protected instance coe_option {a : Type u} : has_coe_t a (Option a) := has_coe_t.mk fun (x : a) => some x /- Auxiliary transitive closure for has_coe which does not contain instances such as coe_option. They would produce non-termination when combined with coe_fn_trans and coe_sort_trans. -/ class has_coe_t_aux (a : Sort u) (b : Sort v) where coe : a → b protected instance coe_trans_aux {a : Sort u₁} {b : Sort u₂} {c : Sort u₃} [has_coe_t_aux b c] [has_coe a b] : has_coe_t_aux a c := has_coe_t_aux.mk fun (x : a) => has_coe_t_aux.coe (coe_b x) protected instance coe_base_aux {a : Sort u} {b : Sort v} [has_coe a b] : has_coe_t_aux a b := has_coe_t_aux.mk coe_b protected instance coe_fn_trans {a : Sort u₁} {b : Sort u₂} [has_coe_to_fun b] [has_coe_t_aux a b] : has_coe_to_fun a := has_coe_to_fun.mk (fun (x : a) => has_coe_to_fun.F (has_coe_t_aux.coe x)) fun (x : a) => ⇑(has_coe_t_aux.coe x) protected instance coe_sort_trans {a : Sort u₁} {b : Sort u₂} [has_coe_to_sort b] [has_coe_t_aux a b] : has_coe_to_sort a := has_coe_to_sort.mk (has_coe_to_sort.S b) fun (x : a) => ↥(has_coe_t_aux.coe x) /- Every coercion is also a lift -/ protected instance coe_to_lift {a : Sort u} {b : Sort v} [has_coe_t a b] : has_lift_t a b := has_lift_t.mk coe_t /- basic coercions -/ protected instance coe_bool_to_Prop : has_coe Bool Prop := has_coe.mk fun (y : Bool) => y = tt /- Tactics such as the simplifier only unfold reducible constants when checking whether two terms are definitionally equal or a term is a proposition. The motivation is performance. In particular, when simplifying `p -> q`, the tactic `simp` only visits `p` if it can establish that it is a proposition. Thus, we mark the following instance as @[reducible], otherwise `simp` will not visit `↑p` when simplifying `↑p -> q`. -/ protected instance coe_sort_bool : has_coe_to_sort Bool := has_coe_to_sort.mk Prop fun (y : Bool) => y = tt protected instance coe_decidable_eq (x : Bool) : Decidable ↑x := (fun (this : Decidable (x = tt)) => this) (bool.decidable_eq x tt) protected instance coe_subtype {a : Sort u} {p : a → Prop} : has_coe (Subtype fun (x : a) => p x) a := has_coe.mk subtype.val /- basic lifts -/ /- Remark: we can't use [has_lift_t a₂ a₁] since it will produce non-termination whenever a type class resolution problem does not have a solution. -/ protected instance lift_fn {a₁ : Sort ua₁} {a₂ : Sort ua₂} {b₁ : Sort ub₁} {b₂ : Sort ub₂} [has_lift a₂ a₁] [has_lift_t b₁ b₂] : has_lift (a₁ → b₁) (a₂ → b₂) := has_lift.mk fun (f : a₁ → b₁) (x : a₂) => ↑(f ↑x) protected instance lift_fn_range {a : Sort ua} {b₁ : Sort ub₁} {b₂ : Sort ub₂} [has_lift_t b₁ b₂] : has_lift (a → b₁) (a → b₂) := has_lift.mk fun (f : a → b₁) (x : a) => ↑(f x) protected instance lift_fn_dom {a₁ : Sort ua₁} {a₂ : Sort ua₂} {b : Sort ub} [has_lift a₂ a₁] : has_lift (a₁ → b) (a₂ → b) := has_lift.mk fun (f : a₁ → b) (x : a₂) => f ↑x protected instance lift_pair {a₁ : Type ua₁} {a₂ : Type ub₂} {b₁ : Type ub₁} {b₂ : Type ub₂} [has_lift_t a₁ a₂] [has_lift_t b₁ b₂] : has_lift (a₁ × b₁) (a₂ × b₂) := has_lift.mk fun (p : a₁ × b₁) => prod.cases_on p fun (x : a₁) (y : b₁) => (↑x, ↑y) protected instance lift_pair₁ {a₁ : Type ua₁} {a₂ : Type ua₂} {b : Type ub} [has_lift_t a₁ a₂] : has_lift (a₁ × b) (a₂ × b) := has_lift.mk fun (p : a₁ × b) => prod.cases_on p fun (x : a₁) (y : b) => (↑x, y) protected instance lift_pair₂ {a : Type ua} {b₁ : Type ub₁} {b₂ : Type ub₂} [has_lift_t b₁ b₂] : has_lift (a × b₁) (a × b₂) := has_lift.mk fun (p : a × b₁) => prod.cases_on p fun (x : a) (y : b₁) => (x, ↑y) protected instance lift_list {a : Type u} {b : Type v} [has_lift_t a b] : has_lift (List a) (List b) := has_lift.mk fun (l : List a) => list.map coe l
c4f7e09eaf5b0700a910b7261e6786d4be6bc1ff
367134ba5a65885e863bdc4507601606690974c1
/src/data/set/intervals/unordered_interval.lean
4ccb3b39bc29a3556f294c84ff6f34327b39933e
[ "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
8,179
lean
/- Copyright (c) 2020 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import order.bounds import data.set.intervals.image_preimage /-! # Intervals without endpoints ordering In any decidable linear order `α`, we define the set of elements lying between two elements `a` and `b` as `Icc (min a b) (max a b)`. `Icc a b` requires the assumption `a ≤ b` to be meaningful, which is sometimes inconvenient. The interval as defined in this file is always the set of things lying between `a` and `b`, regardless of the relative order of `a` and `b`. For real numbers, `Icc (min a b) (max a b)` is the same as `segment a b`. ## Notation We use the localized notation `[a, b]` for `interval a b`. One can open the locale `interval` to make the notation available. -/ universe u namespace set section linear_order variables {α : Type u} [linear_order α] {a a₁ a₂ b b₁ b₂ x : α} /-- `interval a b` is the set of elements lying between `a` and `b`, with `a` and `b` included. -/ def interval (a b : α) := Icc (min a b) (max a b) localized "notation `[`a `, ` b `]` := interval a b" in interval @[simp] lemma interval_of_le (h : a ≤ b) : [a, b] = Icc a b := by rw [interval, min_eq_left h, max_eq_right h] @[simp] lemma interval_of_ge (h : b ≤ a) : [a, b] = Icc b a := by { rw [interval, min_eq_right h, max_eq_left h] } lemma interval_swap (a b : α) : [a, b] = [b, a] := or.elim (le_total a b) (by simp {contextual := tt}) (by simp {contextual := tt}) lemma interval_of_lt (h : a < b) : [a, b] = Icc a b := interval_of_le (le_of_lt h) lemma interval_of_gt (h : b < a) : [a, b] = Icc b a := interval_of_ge (le_of_lt h) lemma interval_of_not_le (h : ¬ a ≤ b) : [a, b] = Icc b a := interval_of_gt (lt_of_not_ge h) lemma interval_of_not_ge (h : ¬ b ≤ a) : [a, b] = Icc a b := interval_of_lt (lt_of_not_ge h) @[simp] lemma interval_self : [a, a] = {a} := set.ext $ by simp [le_antisymm_iff, and_comm] @[simp] lemma nonempty_interval : set.nonempty [a, b] := by { simp only [interval, min_le_iff, le_max_iff, nonempty_Icc], left, left, refl } @[simp] lemma left_mem_interval : a ∈ [a, b] := by { rw [interval, mem_Icc], exact ⟨min_le_left _ _, le_max_left _ _⟩ } @[simp] lemma right_mem_interval : b ∈ [a, b] := by { rw interval_swap, exact left_mem_interval } lemma Icc_subset_interval : Icc a b ⊆ [a, b] := by { assume x h, rwa interval_of_le, exact le_trans h.1 h.2 } lemma Icc_subset_interval' : Icc b a ⊆ [a, b] := by { rw interval_swap, apply Icc_subset_interval } lemma mem_interval_of_le (ha : a ≤ x) (hb : x ≤ b) : x ∈ [a, b] := Icc_subset_interval ⟨ha, hb⟩ lemma mem_interval_of_ge (hb : b ≤ x) (ha : x ≤ a) : x ∈ [a, b] := Icc_subset_interval' ⟨hb, ha⟩ lemma not_mem_interval_of_lt {c : α} (ha : c < a) (hb : c < b) : c ∉ interval a b := not_mem_Icc_of_lt $ lt_min_iff.mpr ⟨ha, hb⟩ lemma not_mem_interval_of_gt {c : α} (ha : a < c) (hb : b < c) : c ∉ interval a b := not_mem_Icc_of_gt $ max_lt_iff.mpr ⟨ha, hb⟩ lemma interval_subset_interval (h₁ : a₁ ∈ [a₂, b₂]) (h₂ : b₁ ∈ [a₂, b₂]) : [a₁, b₁] ⊆ [a₂, b₂] := Icc_subset_Icc (le_min h₁.1 h₂.1) (max_le h₁.2 h₂.2) lemma interval_subset_interval_iff_mem : [a₁, b₁] ⊆ [a₂, b₂] ↔ a₁ ∈ [a₂, b₂] ∧ b₁ ∈ [a₂, b₂] := iff.intro (λh, ⟨h left_mem_interval, h right_mem_interval⟩) (λ h, interval_subset_interval h.1 h.2) lemma interval_subset_interval_iff_le : [a₁, b₁] ⊆ [a₂, b₂] ↔ min a₂ b₂ ≤ min a₁ b₁ ∧ max a₁ b₁ ≤ max a₂ b₂ := by { rw [interval, interval, Icc_subset_Icc_iff], exact min_le_max } lemma interval_subset_interval_right (h : x ∈ [a, b]) : [x, b] ⊆ [a, b] := interval_subset_interval h right_mem_interval lemma interval_subset_interval_left (h : x ∈ [a, b]) : [a, x] ⊆ [a, b] := interval_subset_interval left_mem_interval h lemma bdd_below_bdd_above_iff_subset_interval (s : set α) : bdd_below s ∧ bdd_above s ↔ ∃ a b, s ⊆ [a, b] := begin rw [bdd_below_bdd_above_iff_subset_Icc], split, { rintro ⟨a, b, h⟩, exact ⟨a, b, λ x hx, Icc_subset_interval (h hx)⟩ }, { rintro ⟨a, b, h⟩, exact ⟨min a b, max a b, h⟩ } end end linear_order open_locale interval section ordered_add_comm_group variables {α : Type u} [linear_ordered_add_comm_group α] (a b c x y : α) @[simp] lemma preimage_const_add_interval : (λ x, a + x) ⁻¹' [b, c] = [b - a, c - a] := by simp only [interval, preimage_const_add_Icc, min_sub_sub_right, max_sub_sub_right] @[simp] lemma preimage_add_const_interval : (λ x, x + a) ⁻¹' [b, c] = [b - a, c - a] := by simpa only [add_comm] using preimage_const_add_interval a b c @[simp] lemma preimage_neg_interval : -([a, b]) = [-a, -b] := by simp only [interval, preimage_neg_Icc, min_neg_neg, max_neg_neg] @[simp] lemma preimage_sub_const_interval : (λ x, x - a) ⁻¹' [b, c] = [b + a, c + a] := by simp [sub_eq_add_neg] @[simp] lemma preimage_const_sub_interval : (λ x, a - x) ⁻¹' [b, c] = [a - b, a - c] := by { rw [interval, interval, preimage_const_sub_Icc], simp only [sub_eq_add_neg, min_add_add_left, max_add_add_left, min_neg_neg, max_neg_neg], } @[simp] lemma image_const_add_interval : (λ x, a + x) '' [b, c] = [a + b, a + c] := by simp [add_comm] @[simp] lemma image_add_const_interval : (λ x, x + a) '' [b, c] = [b + a, c + a] := by simp @[simp] lemma image_const_sub_interval : (λ x, a - x) '' [b, c] = [a - b, a - c] := by simp [sub_eq_add_neg, image_comp (λ x, a + x) (λ x, -x)] @[simp] lemma image_sub_const_interval : (λ x, x - a) '' [b, c] = [b - a, c - a] := by simp [sub_eq_add_neg, add_comm] lemma image_neg_interval : has_neg.neg '' [a, b] = [-a, -b] := by simp variables {a b c x y} /-- If `[x, y]` is a subinterval of `[a, b]`, then the distance between `x` and `y` is less than or equal to that of `a` and `b` -/ lemma abs_sub_le_of_subinterval (h : [x, y] ⊆ [a, b]) : abs (y - x) ≤ abs (b - a) := begin rw [← max_sub_min_eq_abs, ← max_sub_min_eq_abs], rw [interval_subset_interval_iff_le] at h, exact sub_le_sub h.2 h.1, end /-- If `x ∈ [a, b]`, then the distance between `a` and `x` is less than or equal to that of `a` and `b` -/ lemma abs_sub_left_of_mem_interval (h : x ∈ [a, b]) : abs (x - a) ≤ abs (b - a) := abs_sub_le_of_subinterval (interval_subset_interval_left h) /-- If `x ∈ [a, b]`, then the distance between `x` and `b` is less than or equal to that of `a` and `b` -/ lemma abs_sub_right_of_mem_interval (h : x ∈ [a, b]) : abs (b - x) ≤ abs (b - a) := abs_sub_le_of_subinterval (interval_subset_interval_right h) end ordered_add_comm_group section linear_ordered_field variables {k : Type u} [linear_ordered_field k] {a : k} @[simp] lemma preimage_mul_const_interval (ha : a ≠ 0) (b c : k) : (λ x, x * a) ⁻¹' [b, c] = [b / a, c / a] := (lt_or_gt_of_ne ha).elim (λ ha, by simp [interval, ha, ha.le, min_div_div_right_of_nonpos, max_div_div_right_of_nonpos]) (λ (ha : 0 < a), by simp [interval, ha, ha.le, min_div_div_right, max_div_div_right]) @[simp] lemma preimage_const_mul_interval (ha : a ≠ 0) (b c : k) : (λ x, a * x) ⁻¹' [b, c] = [b / a, c / a] := by simp only [← preimage_mul_const_interval ha, mul_comm] @[simp] lemma preimage_div_const_interval (ha : a ≠ 0) (b c : k) : (λ x, x / a) ⁻¹' [b, c] = [b * a, c * a] := (preimage_mul_const_interval (inv_ne_zero ha) _ _).trans $ by simp [div_eq_mul_inv] @[simp] lemma image_mul_const_interval (a b c : k) : (λ x, x * a) '' [b, c] = [b * a, c * a] := if ha : a = 0 then by simp [ha] else calc (λ x, x * a) '' [b, c] = (λ x, x / a) ⁻¹' [b, c] : (units.mk0 a ha).mul_right.image_eq_preimage _ ... = [b * a, c * a] : preimage_div_const_interval ha _ _ @[simp] lemma image_const_mul_interval (a b c : k) : (λ x, a * x) '' [b, c] = [a * b, a * c] := by simpa only [mul_comm] using image_mul_const_interval a b c @[simp] lemma image_div_const_interval (a b c : k) : (λ x, x / a) '' [b, c] = [b / a, c / a] := image_mul_const_interval _ _ _ end linear_ordered_field end set
c946499b1c65c0607a04074559406a9ea3d54433
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/order/basic.lean
68736eac7514bc525b31a4d760ec18a615aa4715
[ "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
37,053
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Mario Carneiro -/ import data.prod.basic import data.subtype /-! # Basic definitions about `≤` and `<` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > https://github.com/leanprover-community/mathlib4/pull/556 > Any changes to this file require a corresponding PR to mathlib4. This file proves basic results about orders, provides extensive dot notation, defines useful order classes and allows to transfer order instances. ## Type synonyms * `order_dual α` : A type synonym reversing the meaning of all inequalities, with notation `αᵒᵈ`. * `as_linear_order α`: A type synonym to promote `partial_order α` to `linear_order α` using `is_total α (≤)`. ### Transfering orders - `order.preimage`, `preorder.lift`: Transfers a (pre)order on `β` to an order on `α` using a function `f : α → β`. - `partial_order.lift`, `linear_order.lift`: Transfers a partial (resp., linear) order on `β` to a partial (resp., linear) order on `α` using an injective function `f`. ### Extra class * `has_sup`: type class for the `⊔` notation * `has_inf`: type class for the `⊓` notation * `has_compl`: type class for the `ᶜ` notation * `densely_ordered`: An order with no gap, i.e. for any two elements `a < b` there exists `c` such that `a < c < b`. ## Notes `≤` and `<` are highly favored over `≥` and `>` in mathlib. The reason is that we can formulate all lemmas using `≤`/`<`, and `rw` has trouble unifying `≤` and `≥`. Hence choosing one direction spares us useless duplication. This is enforced by a linter. See Note [nolint_ge] for more infos. Dot notation is particularly useful on `≤` (`has_le.le`) and `<` (`has_lt.lt`). To that end, we provide many aliases to dot notation-less lemmas. For example, `le_trans` is aliased with `has_le.le.trans` and can be used to construct `hab.trans hbc : a ≤ c` when `hab : a ≤ b`, `hbc : b ≤ c`, `lt_of_le_of_lt` is aliased as `has_le.le.trans_lt` and can be used to construct `hab.trans hbc : a < c` when `hab : a ≤ b`, `hbc : b < c`. ## TODO - expand module docs - automatic construction of dual definitions / theorems ## Tags preorder, order, partial order, poset, linear order, chain -/ open function universes u v w variables {ι : Type*} {α : Type u} {β : Type v} {γ : Type w} {π : ι → Type*} {r : α → α → Prop} section preorder variables [preorder α] {a b c : α} lemma le_trans' : b ≤ c → a ≤ b → a ≤ c := flip le_trans lemma lt_trans' : b < c → a < b → a < c := flip lt_trans lemma lt_of_le_of_lt' : b ≤ c → a < b → a < c := flip lt_of_lt_of_le lemma lt_of_lt_of_le' : b < c → a ≤ b → a < c := flip lt_of_le_of_lt end preorder section partial_order variables [partial_order α] {a b : α} lemma ge_antisymm : a ≤ b → b ≤ a → b = a := flip le_antisymm lemma lt_of_le_of_ne' : a ≤ b → b ≠ a → a < b := λ h₁ h₂, lt_of_le_of_ne h₁ h₂.symm lemma ne.lt_of_le : a ≠ b → a ≤ b → a < b := flip lt_of_le_of_ne lemma ne.lt_of_le' : b ≠ a → a ≤ b → a < b := flip lt_of_le_of_ne' end partial_order attribute [simp] le_refl attribute [ext] has_le alias le_trans ← has_le.le.trans alias le_trans' ← has_le.le.trans' alias lt_of_le_of_lt ← has_le.le.trans_lt alias lt_of_le_of_lt' ← has_le.le.trans_lt' alias le_antisymm ← has_le.le.antisymm alias ge_antisymm ← has_le.le.antisymm' alias lt_of_le_of_ne ← has_le.le.lt_of_ne alias lt_of_le_of_ne' ← has_le.le.lt_of_ne' alias lt_of_le_not_le ← has_le.le.lt_of_not_le alias lt_or_eq_of_le ← has_le.le.lt_or_eq alias decidable.lt_or_eq_of_le ← has_le.le.lt_or_eq_dec alias le_of_lt ← has_lt.lt.le alias lt_trans ← has_lt.lt.trans alias lt_trans' ← has_lt.lt.trans' alias lt_of_lt_of_le ← has_lt.lt.trans_le alias lt_of_lt_of_le' ← has_lt.lt.trans_le' alias ne_of_lt ← has_lt.lt.ne alias lt_asymm ← has_lt.lt.asymm has_lt.lt.not_lt alias le_of_eq ← eq.le attribute [nolint decidable_classical] has_le.le.lt_or_eq_dec section variables [preorder α] {a b c : α} /-- A version of `le_refl` where the argument is implicit -/ lemma le_rfl : a ≤ a := le_refl a @[simp] lemma lt_self_iff_false (x : α) : x < x ↔ false := ⟨lt_irrefl x, false.elim⟩ lemma le_of_le_of_eq (hab : a ≤ b) (hbc : b = c) : a ≤ c := hab.trans hbc.le lemma le_of_eq_of_le (hab : a = b) (hbc : b ≤ c) : a ≤ c := hab.le.trans hbc lemma lt_of_lt_of_eq (hab : a < b) (hbc : b = c) : a < c := hab.trans_le hbc.le lemma lt_of_eq_of_lt (hab : a = b) (hbc : b < c) : a < c := hab.le.trans_lt hbc lemma le_of_le_of_eq' : b ≤ c → a = b → a ≤ c := flip le_of_eq_of_le lemma le_of_eq_of_le' : b = c → a ≤ b → a ≤ c := flip le_of_le_of_eq lemma lt_of_lt_of_eq' : b < c → a = b → a < c := flip lt_of_eq_of_lt lemma lt_of_eq_of_lt' : b = c → a < b → a < c := flip lt_of_lt_of_eq alias le_of_le_of_eq ← has_le.le.trans_eq alias le_of_le_of_eq' ← has_le.le.trans_eq' alias lt_of_lt_of_eq ← has_lt.lt.trans_eq alias lt_of_lt_of_eq' ← has_lt.lt.trans_eq' alias le_of_eq_of_le ← eq.trans_le alias le_of_eq_of_le' ← eq.trans_ge alias lt_of_eq_of_lt ← eq.trans_lt alias lt_of_eq_of_lt' ← eq.trans_gt end namespace eq variables [preorder α] {x y z : α} /-- If `x = y` then `y ≤ x`. Note: this lemma uses `y ≤ x` instead of `x ≥ y`, because `le` is used almost exclusively in mathlib. -/ protected lemma ge (h : x = y) : y ≤ x := h.symm.le lemma not_lt (h : x = y) : ¬ x < y := λ h', h'.ne h lemma not_gt (h : x = y) : ¬ y < x := h.symm.not_lt end eq namespace has_le.le @[nolint ge_or_gt] -- see Note [nolint_ge] protected lemma ge [has_le α] {x y : α} (h : x ≤ y) : y ≥ x := h section partial_order variables [partial_order α] {a b : α} lemma lt_iff_ne (h : a ≤ b) : a < b ↔ a ≠ b := ⟨λ h, h.ne, h.lt_of_ne⟩ lemma gt_iff_ne (h : a ≤ b) : a < b ↔ b ≠ a := ⟨λ h, h.ne.symm, h.lt_of_ne'⟩ lemma not_lt_iff_eq (h : a ≤ b) : ¬ a < b ↔ a = b := h.lt_iff_ne.not_left lemma not_gt_iff_eq (h : a ≤ b) : ¬ a < b ↔ b = a := h.gt_iff_ne.not_left lemma le_iff_eq (h : a ≤ b) : b ≤ a ↔ b = a := ⟨λ h', h'.antisymm h, eq.le⟩ lemma ge_iff_eq (h : a ≤ b) : b ≤ a ↔ a = b := ⟨h.antisymm, eq.ge⟩ end partial_order lemma lt_or_le [linear_order α] {a b : α} (h : a ≤ b) (c : α) : a < c ∨ c ≤ b := (lt_or_ge a c).imp id $ λ hc, le_trans hc h lemma le_or_lt [linear_order α] {a b : α} (h : a ≤ b) (c : α) : a ≤ c ∨ c < b := (le_or_gt a c).imp id $ λ hc, lt_of_lt_of_le hc h lemma le_or_le [linear_order α] {a b : α} (h : a ≤ b) (c : α) : a ≤ c ∨ c ≤ b := (h.le_or_lt c).elim or.inl (λ h, or.inr $ le_of_lt h) end has_le.le namespace has_lt.lt @[nolint ge_or_gt] -- see Note [nolint_ge] protected lemma gt [has_lt α] {x y : α} (h : x < y) : y > x := h protected lemma false [preorder α] {x : α} : x < x → false := lt_irrefl x lemma ne' [preorder α] {x y : α} (h : x < y) : y ≠ x := h.ne.symm lemma lt_or_lt [linear_order α] {x y : α} (h : x < y) (z : α) : x < z ∨ z < y := (lt_or_ge z y).elim or.inr (λ hz, or.inl $ h.trans_le hz) end has_lt.lt @[nolint ge_or_gt] -- see Note [nolint_ge] protected lemma ge.le [has_le α] {x y : α} (h : x ≥ y) : y ≤ x := h @[nolint ge_or_gt] -- see Note [nolint_ge] protected lemma gt.lt [has_lt α] {x y : α} (h : x > y) : y < x := h @[nolint ge_or_gt] -- see Note [nolint_ge] theorem ge_of_eq [preorder α] {a b : α} (h : a = b) : a ≥ b := h.ge @[simp, nolint ge_or_gt] -- see Note [nolint_ge] lemma ge_iff_le [has_le α] {a b : α} : a ≥ b ↔ b ≤ a := iff.rfl @[simp, nolint ge_or_gt] -- see Note [nolint_ge] lemma gt_iff_lt [has_lt α] {a b : α} : a > b ↔ b < a := iff.rfl lemma not_le_of_lt [preorder α] {a b : α} (h : a < b) : ¬ b ≤ a := (le_not_le_of_lt h).right alias not_le_of_lt ← has_lt.lt.not_le lemma not_lt_of_le [preorder α] {a b : α} (h : a ≤ b) : ¬ b < a := λ hba, hba.not_le h alias not_lt_of_le ← has_le.le.not_lt lemma ne_of_not_le [preorder α] {a b : α} (h : ¬ a ≤ b) : a ≠ b := λ hab, h (le_of_eq hab) -- See Note [decidable namespace] protected lemma decidable.le_iff_eq_or_lt [partial_order α] [@decidable_rel α (≤)] {a b : α} : a ≤ b ↔ a = b ∨ a < b := decidable.le_iff_lt_or_eq.trans or.comm lemma le_iff_eq_or_lt [partial_order α] {a b : α} : a ≤ b ↔ a = b ∨ a < b := le_iff_lt_or_eq.trans or.comm lemma lt_iff_le_and_ne [partial_order α] {a b : α} : a < b ↔ a ≤ b ∧ a ≠ b := ⟨λ h, ⟨le_of_lt h, ne_of_lt h⟩, λ ⟨h1, h2⟩, h1.lt_of_ne h2⟩ -- See Note [decidable namespace] protected lemma decidable.eq_iff_le_not_lt [partial_order α] [@decidable_rel α (≤)] {a b : α} : a = b ↔ a ≤ b ∧ ¬ a < b := ⟨λ h, ⟨h.le, h ▸ lt_irrefl _⟩, λ ⟨h₁, h₂⟩, h₁.antisymm $ decidable.by_contradiction $ λ h₃, h₂ (h₁.lt_of_not_le h₃)⟩ lemma eq_iff_le_not_lt [partial_order α] {a b : α} : a = b ↔ a ≤ b ∧ ¬ a < b := by haveI := classical.dec; exact decidable.eq_iff_le_not_lt lemma eq_or_lt_of_le [partial_order α] {a b : α} (h : a ≤ b) : a = b ∨ a < b := h.lt_or_eq.symm lemma eq_or_gt_of_le [partial_order α] {a b : α} (h : a ≤ b) : b = a ∨ a < b := h.lt_or_eq.symm.imp eq.symm id alias decidable.eq_or_lt_of_le ← has_le.le.eq_or_lt_dec alias eq_or_lt_of_le ← has_le.le.eq_or_lt alias eq_or_gt_of_le ← has_le.le.eq_or_gt attribute [nolint decidable_classical] has_le.le.eq_or_lt_dec lemma eq_of_le_of_not_lt [partial_order α] {a b : α} (hab : a ≤ b) (hba : ¬ a < b) : a = b := hab.eq_or_lt.resolve_right hba lemma eq_of_ge_of_not_gt [partial_order α] {a b : α} (hab : a ≤ b) (hba : ¬ a < b) : b = a := (hab.eq_or_lt.resolve_right hba).symm alias eq_of_le_of_not_lt ← has_le.le.eq_of_not_lt alias eq_of_ge_of_not_gt ← has_le.le.eq_of_not_gt lemma ne.le_iff_lt [partial_order α] {a b : α} (h : a ≠ b) : a ≤ b ↔ a < b := ⟨λ h', lt_of_le_of_ne h' h, λ h, h.le⟩ lemma ne.not_le_or_not_le [partial_order α] {a b : α} (h : a ≠ b) : ¬ a ≤ b ∨ ¬ b ≤ a := not_and_distrib.1 $ le_antisymm_iff.not.1 h -- See Note [decidable namespace] protected lemma decidable.ne_iff_lt_iff_le [partial_order α] [decidable_eq α] {a b : α} : (a ≠ b ↔ a < b) ↔ a ≤ b := ⟨λ h, decidable.by_cases le_of_eq (le_of_lt ∘ h.mp), λ h, ⟨lt_of_le_of_ne h, ne_of_lt⟩⟩ @[simp] lemma ne_iff_lt_iff_le [partial_order α] {a b : α} : (a ≠ b ↔ a < b) ↔ a ≤ b := by haveI := classical.dec; exact decidable.ne_iff_lt_iff_le -- Variant of `min_def` with the branches reversed. lemma min_def' [linear_order α] (a b : α) : min a b = if b ≤ a then b else a := begin rw [min_def], rcases lt_trichotomy a b with lt | eq | gt, { rw [if_pos lt.le, if_neg (not_le.mpr lt)], }, { rw [if_pos eq.le, if_pos eq.ge, eq], }, { rw [if_neg (not_le.mpr gt), if_pos gt.le], } end -- Variant of `min_def` with the branches reversed. -- This is sometimes useful as it used to be the default. lemma max_def' [linear_order α] (a b : α) : max a b = if b ≤ a then a else b := begin rw [max_def], rcases lt_trichotomy a b with lt | eq | gt, { rw [if_pos lt.le, if_neg (not_le.mpr lt)], }, { rw [if_pos eq.le, if_pos eq.ge, eq], }, { rw [if_neg (not_le.mpr gt), if_pos gt.le], } end lemma lt_of_not_le [linear_order α] {a b : α} (h : ¬ b ≤ a) : a < b := ((le_total _ _).resolve_right h).lt_of_not_le h lemma lt_iff_not_le [linear_order α] {x y : α} : x < y ↔ ¬ y ≤ x := ⟨not_le_of_lt, lt_of_not_le⟩ lemma ne.lt_or_lt [linear_order α] {x y : α} (h : x ≠ y) : x < y ∨ y < x := lt_or_gt_of_ne h /-- A version of `ne_iff_lt_or_gt` with LHS and RHS reversed. -/ @[simp] lemma lt_or_lt_iff_ne [linear_order α] {x y : α} : x < y ∨ y < x ↔ x ≠ y := ne_iff_lt_or_gt.symm lemma not_lt_iff_eq_or_lt [linear_order α] {a b : α} : ¬ a < b ↔ a = b ∨ b < a := not_lt.trans $ decidable.le_iff_eq_or_lt.trans $ or_congr eq_comm iff.rfl lemma exists_ge_of_linear [linear_order α] (a b : α) : ∃ c, a ≤ c ∧ b ≤ c := match le_total a b with | or.inl h := ⟨_, h, le_rfl⟩ | or.inr h := ⟨_, le_rfl, h⟩ end lemma lt_imp_lt_of_le_imp_le {β} [linear_order α] [preorder β] {a b : α} {c d : β} (H : a ≤ b → c ≤ d) (h : d < c) : b < a := lt_of_not_le $ λ h', (H h').not_lt h lemma le_imp_le_iff_lt_imp_lt {β} [linear_order α] [linear_order β] {a b : α} {c d : β} : (a ≤ b → c ≤ d) ↔ (d < c → b < a) := ⟨lt_imp_lt_of_le_imp_le, le_imp_le_of_lt_imp_lt⟩ lemma lt_iff_lt_of_le_iff_le' {β} [preorder α] [preorder β] {a b : α} {c d : β} (H : a ≤ b ↔ c ≤ d) (H' : b ≤ a ↔ d ≤ c) : b < a ↔ d < c := lt_iff_le_not_le.trans $ (and_congr H' (not_congr H)).trans lt_iff_le_not_le.symm lemma lt_iff_lt_of_le_iff_le {β} [linear_order α] [linear_order β] {a b : α} {c d : β} (H : a ≤ b ↔ c ≤ d) : b < a ↔ d < c := not_le.symm.trans $ (not_congr H).trans $ not_le lemma le_iff_le_iff_lt_iff_lt {β} [linear_order α] [linear_order β] {a b : α} {c d : β} : (a ≤ b ↔ c ≤ d) ↔ (b < a ↔ d < c) := ⟨lt_iff_lt_of_le_iff_le, λ H, not_lt.symm.trans $ (not_congr H).trans $ not_lt⟩ lemma eq_of_forall_le_iff [partial_order α] {a b : α} (H : ∀ c, c ≤ a ↔ c ≤ b) : a = b := ((H _).1 le_rfl).antisymm ((H _).2 le_rfl) lemma le_of_forall_le [preorder α] {a b : α} (H : ∀ c, c ≤ a → c ≤ b) : a ≤ b := H _ le_rfl lemma le_of_forall_le' [preorder α] {a b : α} (H : ∀ c, a ≤ c → b ≤ c) : b ≤ a := H _ le_rfl lemma le_of_forall_lt [linear_order α] {a b : α} (H : ∀ c, c < a → c < b) : a ≤ b := le_of_not_lt $ λ h, lt_irrefl _ (H _ h) lemma forall_lt_iff_le [linear_order α] {a b : α} : (∀ ⦃c⦄, c < a → c < b) ↔ a ≤ b := ⟨le_of_forall_lt, λ h c hca, lt_of_lt_of_le hca h⟩ lemma le_of_forall_lt' [linear_order α] {a b : α} (H : ∀ c, a < c → b < c) : b ≤ a := le_of_not_lt $ λ h, lt_irrefl _ (H _ h) lemma forall_lt_iff_le' [linear_order α] {a b : α} : (∀ ⦃c⦄, a < c → b < c) ↔ b ≤ a := ⟨le_of_forall_lt', λ h c hac, lt_of_le_of_lt h hac⟩ lemma eq_of_forall_ge_iff [partial_order α] {a b : α} (H : ∀ c, a ≤ c ↔ b ≤ c) : a = b := ((H _).2 le_rfl).antisymm ((H _).1 le_rfl) lemma eq_of_forall_lt_iff [linear_order α] {a b : α} (h : ∀ c, c < a ↔ c < b) : a = b := (le_of_forall_lt $ λ _, (h _).1).antisymm $ le_of_forall_lt $ λ _, (h _).2 lemma eq_of_forall_gt_iff [linear_order α] {a b : α} (h : ∀ c, a < c ↔ b < c) : a = b := (le_of_forall_lt' $ λ _, (h _).2).antisymm $ le_of_forall_lt' $ λ _, (h _).1 /-- A symmetric relation implies two values are equal, when it implies they're less-equal. -/ lemma rel_imp_eq_of_rel_imp_le [partial_order β] (r : α → α → Prop) [is_symm α r] {f : α → β} (h : ∀ a b, r a b → f a ≤ f b) {a b : α} : r a b → f a = f b := λ hab, le_antisymm (h a b hab) (h b a $ symm hab) /-- monotonicity of `≤` with respect to `→` -/ lemma le_implies_le_of_le_of_le {a b c d : α} [preorder α] (hca : c ≤ a) (hbd : b ≤ d) : a ≤ b → c ≤ d := λ hab, (hca.trans hab).trans hbd @[ext] lemma preorder.to_has_le_injective {α : Type*} : function.injective (@preorder.to_has_le α) := λ A B h, begin cases A, cases B, injection h with h_le, have : A_lt = B_lt, { funext a b, dsimp [(≤)] at A_lt_iff_le_not_le B_lt_iff_le_not_le h_le, simp [A_lt_iff_le_not_le, B_lt_iff_le_not_le, h_le], }, congr', end @[ext] lemma partial_order.to_preorder_injective {α : Type*} : function.injective (@partial_order.to_preorder α) := λ A B h, by { cases A, cases B, injection h, congr' } @[ext] lemma linear_order.to_partial_order_injective {α : Type*} : function.injective (@linear_order.to_partial_order α) := begin intros A B h, cases A, cases B, injection h, obtain rfl : A_le = B_le := ‹_›, obtain rfl : A_lt = B_lt := ‹_›, obtain rfl : A_decidable_le = B_decidable_le := subsingleton.elim _ _, obtain rfl : A_max = B_max := A_max_def.trans B_max_def.symm, obtain rfl : A_min = B_min := A_min_def.trans B_min_def.symm, congr end theorem preorder.ext {α} {A B : preorder α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := by { ext x y, exact H x y } theorem partial_order.ext {α} {A B : partial_order α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := by { ext x y, exact H x y } theorem linear_order.ext {α} {A B : linear_order α} (H : ∀ x y : α, (by haveI := A; exact x ≤ y) ↔ x ≤ y) : A = B := by { ext x y, exact H x y } /-- Given a relation `R` on `β` and a function `f : α → β`, the preimage relation on `α` is defined by `x ≤ y ↔ f x ≤ f y`. It is the unique relation on `α` making `f` a `rel_embedding` (assuming `f` is injective). -/ @[simp] def order.preimage {α β} (f : α → β) (s : β → β → Prop) (x y : α) : Prop := s (f x) (f y) infix ` ⁻¹'o `:80 := order.preimage /-- The preimage of a decidable order is decidable. -/ instance order.preimage.decidable {α β} (f : α → β) (s : β → β → Prop) [H : decidable_rel s] : decidable_rel (f ⁻¹'o s) := λ x y, H _ _ /-! ### Order dual -/ /-- Type synonym to equip a type with the dual order: `≤` means `≥` and `<` means `>`. `αᵒᵈ` is notation for `order_dual α`. -/ def order_dual (α : Type*) : Type* := α notation α `ᵒᵈ`:std.prec.max_plus := order_dual α namespace order_dual instance (α : Type*) [h : nonempty α] : nonempty αᵒᵈ := h instance (α : Type*) [h : subsingleton α] : subsingleton αᵒᵈ := h instance (α : Type*) [has_le α] : has_le αᵒᵈ := ⟨λ x y : α, y ≤ x⟩ instance (α : Type*) [has_lt α] : has_lt αᵒᵈ := ⟨λ x y : α, y < x⟩ instance (α : Type*) [preorder α] : preorder αᵒᵈ := { le_refl := le_refl, le_trans := λ a b c hab hbc, hbc.trans hab, lt_iff_le_not_le := λ _ _, lt_iff_le_not_le, .. order_dual.has_le α, .. order_dual.has_lt α } instance (α : Type*) [partial_order α] : partial_order αᵒᵈ := { le_antisymm := λ a b hab hba, @le_antisymm α _ a b hba hab, .. order_dual.preorder α } instance (α : Type*) [linear_order α] : linear_order αᵒᵈ := { le_total := λ a b : α, le_total b a, decidable_le := (infer_instance : decidable_rel (λ a b : α, b ≤ a)), decidable_lt := (infer_instance : decidable_rel (λ a b : α, b < a)), min := @max α _, max := @min α _, min_def := funext₂ $ @max_def' α _, max_def := funext₂ $ @min_def' α _, .. order_dual.partial_order α } instance : Π [inhabited α], inhabited αᵒᵈ := id theorem preorder.dual_dual (α : Type*) [H : preorder α] : order_dual.preorder αᵒᵈ = H := preorder.ext $ λ _ _, iff.rfl theorem partial_order.dual_dual (α : Type*) [H : partial_order α] : order_dual.partial_order αᵒᵈ = H := partial_order.ext $ λ _ _, iff.rfl theorem linear_order.dual_dual (α : Type*) [H : linear_order α] : order_dual.linear_order αᵒᵈ = H := linear_order.ext $ λ _ _, iff.rfl end order_dual /-! ### `has_compl` -/ /-- Set / lattice complement -/ @[notation_class] class has_compl (α : Type*) := (compl : α → α) export has_compl (compl) postfix `ᶜ`:(max+1) := compl instance Prop.has_compl : has_compl Prop := ⟨not⟩ instance pi.has_compl {ι : Type u} {α : ι → Type v} [∀ i, has_compl (α i)] : has_compl (Π i, α i) := ⟨λ x i, (x i)ᶜ⟩ lemma pi.compl_def {ι : Type u} {α : ι → Type v} [∀ i, has_compl (α i)] (x : Π i, α i) : xᶜ = λ i, (x i)ᶜ := rfl @[simp] lemma pi.compl_apply {ι : Type u} {α : ι → Type v} [∀ i, has_compl (α i)] (x : Π i, α i) (i : ι) : xᶜ i = (x i)ᶜ := rfl instance is_irrefl.compl (r) [is_irrefl α r] : is_refl α rᶜ := ⟨@irrefl α r _⟩ instance is_refl.compl (r) [is_refl α r] : is_irrefl α rᶜ := ⟨λ a, not_not_intro (refl a)⟩ /-! ### Order instances on the function space -/ instance pi.has_le {ι : Type u} {α : ι → Type v} [∀ i, has_le (α i)] : has_le (Π i, α i) := { le := λ x y, ∀ i, x i ≤ y i } lemma pi.le_def {ι : Type u} {α : ι → Type v} [∀ i, has_le (α i)] {x y : Π i, α i} : x ≤ y ↔ ∀ i, x i ≤ y i := iff.rfl instance pi.preorder {ι : Type u} {α : ι → Type v} [∀ i, preorder (α i)] : preorder (Π i, α i) := { le_refl := λ a i, le_refl (a i), le_trans := λ a b c h₁ h₂ i, le_trans (h₁ i) (h₂ i), ..pi.has_le } lemma pi.lt_def {ι : Type u} {α : ι → Type v} [∀ i, preorder (α i)] {x y : Π i, α i} : x < y ↔ x ≤ y ∧ ∃ i, x i < y i := by simp [lt_iff_le_not_le, pi.le_def] {contextual := tt} instance pi.partial_order [Π i, partial_order (π i)] : partial_order (Π i, π i) := { le_antisymm := λ f g h1 h2, funext $ λ b, (h1 b).antisymm (h2 b), ..pi.preorder } section pi /-- A function `a` is strongly less than a function `b` if `a i < b i` for all `i`. -/ def strong_lt [Π i, has_lt (π i)] (a b : Π i, π i) : Prop := ∀ i, a i < b i local infix ` ≺ `:50 := strong_lt variables [Π i, preorder (π i)] {a b c : Π i, π i} lemma le_of_strong_lt (h : a ≺ b) : a ≤ b := λ i, (h _).le lemma lt_of_strong_lt [nonempty ι] (h : a ≺ b) : a < b := by { inhabit ι, exact pi.lt_def.2 ⟨le_of_strong_lt h, default, h _⟩ } lemma strong_lt_of_strong_lt_of_le (hab : a ≺ b) (hbc : b ≤ c) : a ≺ c := λ i, (hab _).trans_le $ hbc _ lemma strong_lt_of_le_of_strong_lt (hab : a ≤ b) (hbc : b ≺ c) : a ≺ c := λ i, (hab _).trans_lt $ hbc _ alias le_of_strong_lt ← strong_lt.le alias lt_of_strong_lt ← strong_lt.lt alias strong_lt_of_strong_lt_of_le ← strong_lt.trans_le alias strong_lt_of_le_of_strong_lt ← has_le.le.trans_strong_lt end pi section function variables [decidable_eq ι] [Π i, preorder (π i)] {x y : Π i, π i} {i : ι} {a b : π i} lemma le_update_iff : x ≤ function.update y i a ↔ x i ≤ a ∧ ∀ j ≠ i, x j ≤ y j := function.forall_update_iff _ (λ j z, x j ≤ z) lemma update_le_iff : function.update x i a ≤ y ↔ a ≤ y i ∧ ∀ j ≠ i, x j ≤ y j := function.forall_update_iff _ (λ j z, z ≤ y j) lemma update_le_update_iff : function.update x i a ≤ function.update y i b ↔ a ≤ b ∧ ∀ j ≠ i, x j ≤ y j := by simp [update_le_iff] {contextual := tt} @[simp] lemma le_update_self_iff : x ≤ update x i a ↔ x i ≤ a := by simp [le_update_iff] @[simp] lemma update_le_self_iff : update x i a ≤ x ↔ a ≤ x i := by simp [update_le_iff] @[simp] lemma lt_update_self_iff : x < update x i a ↔ x i < a := by simp [lt_iff_le_not_le] @[simp] lemma update_lt_self_iff : update x i a < x ↔ a < x i := by simp [lt_iff_le_not_le] end function instance pi.has_sdiff {ι : Type u} {α : ι → Type v} [∀ i, has_sdiff (α i)] : has_sdiff (Π i, α i) := ⟨λ x y i, x i \ y i⟩ lemma pi.sdiff_def {ι : Type u} {α : ι → Type v} [∀ i, has_sdiff (α i)] (x y : Π i, α i) : (x \ y) = λ i, x i \ y i := rfl @[simp] lemma pi.sdiff_apply {ι : Type u} {α : ι → Type v} [∀ i, has_sdiff (α i)] (x y : Π i, α i) (i : ι) : (x \ y) i = x i \ y i := rfl namespace function variables [preorder α] [nonempty β] {a b : α} @[simp] lemma const_le_const : const β a ≤ const β b ↔ a ≤ b := by simp [pi.le_def] @[simp] lemma const_lt_const : const β a < const β b ↔ a < b := by simpa [pi.lt_def] using le_of_lt end function /-! ### `min`/`max` recursors -/ section min_max_rec variables [linear_order α] {p : α → Prop} {x y : α} lemma min_rec (hx : x ≤ y → p x) (hy : y ≤ x → p y) : p (min x y) := (le_total x y).rec (λ h, (min_eq_left h).symm.subst (hx h)) (λ h, (min_eq_right h).symm.subst (hy h)) lemma max_rec (hx : y ≤ x → p x) (hy : x ≤ y → p y) : p (max x y) := @min_rec αᵒᵈ _ _ _ _ hx hy lemma min_rec' (p : α → Prop) (hx : p x) (hy : p y) : p (min x y) := min_rec (λ _, hx) (λ _, hy) lemma max_rec' (p : α → Prop) (hx : p x) (hy : p y) : p (max x y) := max_rec (λ _, hx) (λ _, hy) lemma min_def_lt (x y : α) : min x y = if x < y then x else y := begin rw [min_comm, min_def, ← ite_not], simp only [not_le], end lemma max_def_lt (x y : α) : max x y = if x < y then y else x := begin rw [max_comm, max_def, ← ite_not], simp only [not_le], end end min_max_rec /-! ### `has_sup` and `has_inf` -/ /-- Typeclass for the `⊔` (`\lub`) notation -/ @[notation_class] class has_sup (α : Type u) := (sup : α → α → α) /-- Typeclass for the `⊓` (`\glb`) notation -/ @[notation_class] class has_inf (α : Type u) := (inf : α → α → α) infix ` ⊔ ` := has_sup.sup infix ` ⊓ ` := has_inf.inf /-! ### Lifts of order instances -/ /-- Transfer a `preorder` on `β` to a `preorder` on `α` using a function `f : α → β`. See note [reducible non-instances]. -/ @[reducible] def preorder.lift {α β} [preorder β] (f : α → β) : preorder α := { le := λ x y, f x ≤ f y, le_refl := λ a, le_rfl, le_trans := λ a b c, le_trans, lt := λ x y, f x < f y, lt_iff_le_not_le := λ a b, lt_iff_le_not_le } /-- Transfer a `partial_order` on `β` to a `partial_order` on `α` using an injective function `f : α → β`. See note [reducible non-instances]. -/ @[reducible] def partial_order.lift {α β} [partial_order β] (f : α → β) (inj : injective f) : partial_order α := { le_antisymm := λ a b h₁ h₂, inj (h₁.antisymm h₂), .. preorder.lift f } /-- Transfer a `linear_order` on `β` to a `linear_order` on `α` using an injective function `f : α → β`. This version takes `[has_sup α]` and `[has_inf α]` as arguments, then uses them for `max` and `min` fields. See `linear_order.lift'` for a version that autogenerates `min` and `max` fields. See note [reducible non-instances]. -/ @[reducible] def linear_order.lift {α β} [linear_order β] [has_sup α] [has_inf α] (f : α → β) (inj : injective f) (hsup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (hinf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) : linear_order α := { le_total := λ x y, le_total (f x) (f y), decidable_le := λ x y, (infer_instance : decidable (f x ≤ f y)), decidable_lt := λ x y, (infer_instance : decidable (f x < f y)), decidable_eq := λ x y, decidable_of_iff (f x = f y) inj.eq_iff, min := (⊓), max := (⊔), min_def := by { ext x y, apply inj, rw [hinf, min_def, min_default, apply_ite f], refl }, max_def := by { ext x y, apply inj, rw [hsup, max_def, max_default, apply_ite f], refl }, .. partial_order.lift f inj } /-- Transfer a `linear_order` on `β` to a `linear_order` on `α` using an injective function `f : α → β`. This version autogenerates `min` and `max` fields. See `linear_order.lift` for a version that takes `[has_sup α]` and `[has_inf α]`, then uses them as `max` and `min`. See note [reducible non-instances]. -/ @[reducible] def linear_order.lift' {α β} [linear_order β] (f : α → β) (inj : injective f) : linear_order α := @linear_order.lift α β _ ⟨λ x y, if f x ≤ f y then y else x⟩ ⟨λ x y, if f x ≤ f y then x else y⟩ f inj (λ x y, (apply_ite f _ _ _).trans (max_def _ _).symm) (λ x y, (apply_ite f _ _ _).trans (min_def _ _).symm) /-! ### Subtype of an order -/ namespace subtype instance [has_le α] {p : α → Prop} : has_le (subtype p) := ⟨λ x y, (x : α) ≤ y⟩ instance [has_lt α] {p : α → Prop} : has_lt (subtype p) := ⟨λ x y, (x : α) < y⟩ @[simp] lemma mk_le_mk [has_le α] {p : α → Prop} {x y : α} {hx : p x} {hy : p y} : (⟨x, hx⟩ : subtype p) ≤ ⟨y, hy⟩ ↔ x ≤ y := iff.rfl @[simp] lemma mk_lt_mk [has_lt α] {p : α → Prop} {x y : α} {hx : p x} {hy : p y} : (⟨x, hx⟩ : subtype p) < ⟨y, hy⟩ ↔ x < y := iff.rfl @[simp, norm_cast] lemma coe_le_coe [has_le α] {p : α → Prop} {x y : subtype p} : (x : α) ≤ y ↔ x ≤ y := iff.rfl @[simp, norm_cast] lemma coe_lt_coe [has_lt α] {p : α → Prop} {x y : subtype p} : (x : α) < y ↔ x < y := iff.rfl instance [preorder α] (p : α → Prop) : preorder (subtype p) := preorder.lift (coe : subtype p → α) instance partial_order [partial_order α] (p : α → Prop) : partial_order (subtype p) := partial_order.lift coe subtype.coe_injective instance decidable_le [preorder α] [h : @decidable_rel α (≤)] {p : α → Prop} : @decidable_rel (subtype p) (≤) := λ a b, h a b instance decidable_lt [preorder α] [h : @decidable_rel α (<)] {p : α → Prop} : @decidable_rel (subtype p) (<) := λ a b, h a b /-- A subtype of a linear order is a linear order. We explicitly give the proofs of decidable equality and decidable order in order to ensure the decidability instances are all definitionally equal. -/ instance [linear_order α] (p : α → Prop) : linear_order (subtype p) := @linear_order.lift (subtype p) _ _ ⟨λ x y, ⟨max x y, max_rec' _ x.2 y.2⟩⟩ ⟨λ x y, ⟨min x y, min_rec' _ x.2 y.2⟩⟩ coe subtype.coe_injective (λ _ _, rfl) (λ _ _, rfl) end subtype /-! ### Pointwise order on `α × β` The lexicographic order is defined in `data.prod.lex`, and the instances are available via the type synonym `α ×ₗ β = α × β`. -/ namespace prod instance (α : Type u) (β : Type v) [has_le α] [has_le β] : has_le (α × β) := ⟨λ p q, p.1 ≤ q.1 ∧ p.2 ≤ q.2⟩ lemma le_def [has_le α] [has_le β] {x y : α × β} : x ≤ y ↔ x.1 ≤ y.1 ∧ x.2 ≤ y.2 := iff.rfl @[simp] lemma mk_le_mk [has_le α] [has_le β] {x₁ x₂ : α} {y₁ y₂ : β} : (x₁, y₁) ≤ (x₂, y₂) ↔ x₁ ≤ x₂ ∧ y₁ ≤ y₂ := iff.rfl @[simp] lemma swap_le_swap [has_le α] [has_le β] {x y : α × β} : x.swap ≤ y.swap ↔ x ≤ y := and_comm _ _ section preorder variables [preorder α] [preorder β] {a a₁ a₂ : α} {b b₁ b₂ : β} {x y : α × β} instance (α : Type u) (β : Type v) [preorder α] [preorder β] : preorder (α × β) := { le_refl := λ ⟨a, b⟩, ⟨le_refl a, le_refl b⟩, le_trans := λ ⟨a, b⟩ ⟨c, d⟩ ⟨e, f⟩ ⟨hac, hbd⟩ ⟨hce, hdf⟩, ⟨le_trans hac hce, le_trans hbd hdf⟩, .. prod.has_le α β } @[simp] lemma swap_lt_swap : x.swap < y.swap ↔ x < y := and_congr swap_le_swap (not_congr swap_le_swap) lemma mk_le_mk_iff_left : (a₁, b) ≤ (a₂, b) ↔ a₁ ≤ a₂ := and_iff_left le_rfl lemma mk_le_mk_iff_right : (a, b₁) ≤ (a, b₂) ↔ b₁ ≤ b₂ := and_iff_right le_rfl lemma mk_lt_mk_iff_left : (a₁, b) < (a₂, b) ↔ a₁ < a₂ := lt_iff_lt_of_le_iff_le' mk_le_mk_iff_left mk_le_mk_iff_left lemma mk_lt_mk_iff_right : (a, b₁) < (a, b₂) ↔ b₁ < b₂ := lt_iff_lt_of_le_iff_le' mk_le_mk_iff_right mk_le_mk_iff_right lemma lt_iff : x < y ↔ x.1 < y.1 ∧ x.2 ≤ y.2 ∨ x.1 ≤ y.1 ∧ x.2 < y.2 := begin refine ⟨λ h, _, _⟩, { by_cases h₁ : y.1 ≤ x.1, { exact or.inr ⟨h.1.1, h.1.2.lt_of_not_le $ λ h₂, h.2 ⟨h₁, h₂⟩⟩ }, { exact or.inl ⟨h.1.1.lt_of_not_le h₁, h.1.2⟩ } }, { rintro (⟨h₁, h₂⟩ | ⟨h₁, h₂⟩), { exact ⟨⟨h₁.le, h₂⟩, λ h, h₁.not_le h.1⟩ }, { exact ⟨⟨h₁, h₂.le⟩, λ h, h₂.not_le h.2⟩ } } end @[simp] lemma mk_lt_mk : (a₁, b₁) < (a₂, b₂) ↔ a₁ < a₂ ∧ b₁ ≤ b₂ ∨ a₁ ≤ a₂ ∧ b₁ < b₂ := lt_iff end preorder /-- The pointwise partial order on a product. (The lexicographic ordering is defined in order/lexicographic.lean, and the instances are available via the type synonym `α ×ₗ β = α × β`.) -/ instance (α : Type u) (β : Type v) [partial_order α] [partial_order β] : partial_order (α × β) := { le_antisymm := λ ⟨a, b⟩ ⟨c, d⟩ ⟨hac, hbd⟩ ⟨hca, hdb⟩, prod.ext (hac.antisymm hca) (hbd.antisymm hdb), .. prod.preorder α β } end prod /-! ### Additional order classes -/ /-- An order is dense if there is an element between any pair of distinct comparable elements. -/ class densely_ordered (α : Type u) [has_lt α] : Prop := (dense : ∀ a₁ a₂ : α, a₁ < a₂ → ∃ a, a₁ < a ∧ a < a₂) lemma exists_between [has_lt α] [densely_ordered α] : ∀ {a₁ a₂ : α}, a₁ < a₂ → ∃ a, a₁ < a ∧ a < a₂ := densely_ordered.dense instance order_dual.densely_ordered (α : Type u) [has_lt α] [densely_ordered α] : densely_ordered αᵒᵈ := ⟨λ a₁ a₂ ha, (@exists_between α _ _ _ _ ha).imp $ λ a, and.symm⟩ @[simp] lemma densely_ordered_order_dual [has_lt α] : densely_ordered αᵒᵈ ↔ densely_ordered α := ⟨by { convert @order_dual.densely_ordered αᵒᵈ _, casesI ‹has_lt α›, refl }, @order_dual.densely_ordered α _⟩ instance [preorder α] [preorder β] [densely_ordered α] [densely_ordered β] : densely_ordered (α × β) := ⟨λ a b, begin simp_rw prod.lt_iff, rintro (⟨h₁, h₂⟩ | ⟨h₁, h₂⟩), { obtain ⟨c, ha, hb⟩ := exists_between h₁, exact ⟨(c, _), or.inl ⟨ha, h₂⟩, or.inl ⟨hb, le_rfl⟩⟩ }, { obtain ⟨c, ha, hb⟩ := exists_between h₂, exact ⟨(_, c), or.inr ⟨h₁, ha⟩, or.inr ⟨le_rfl, hb⟩⟩ } end⟩ instance {α : ι → Type*} [Π i, preorder (α i)] [Π i, densely_ordered (α i)] : densely_ordered (Π i, α i) := ⟨λ a b, begin classical, simp_rw pi.lt_def, rintro ⟨hab, i, hi⟩, obtain ⟨c, ha, hb⟩ := exists_between hi, exact ⟨a.update i c, ⟨le_update_iff.2 ⟨ha.le, λ _ _, le_rfl⟩, i, by rwa update_same⟩, update_le_iff.2 ⟨hb.le, λ _ _, hab _⟩, i, by rwa update_same⟩, end⟩ lemma le_of_forall_le_of_dense [linear_order α] [densely_ordered α] {a₁ a₂ : α} (h : ∀ a, a₂ < a → a₁ ≤ a) : a₁ ≤ a₂ := le_of_not_gt $ λ ha, let ⟨a, ha₁, ha₂⟩ := exists_between ha in lt_irrefl a $ lt_of_lt_of_le ‹a < a₁› (h _ ‹a₂ < a›) lemma eq_of_le_of_forall_le_of_dense [linear_order α] [densely_ordered α] {a₁ a₂ : α} (h₁ : a₂ ≤ a₁) (h₂ : ∀ a, a₂ < a → a₁ ≤ a) : a₁ = a₂ := le_antisymm (le_of_forall_le_of_dense h₂) h₁ lemma le_of_forall_ge_of_dense [linear_order α] [densely_ordered α] {a₁ a₂ : α} (h : ∀ a₃ < a₁, a₃ ≤ a₂) : a₁ ≤ a₂ := le_of_not_gt $ λ ha, let ⟨a, ha₁, ha₂⟩ := exists_between ha in lt_irrefl a $ lt_of_le_of_lt (h _ ‹a < a₁›) ‹a₂ < a› lemma eq_of_le_of_forall_ge_of_dense [linear_order α] [densely_ordered α] {a₁ a₂ : α} (h₁ : a₂ ≤ a₁) (h₂ : ∀ a₃ < a₁, a₃ ≤ a₂) : a₁ = a₂ := (le_of_forall_ge_of_dense h₂).antisymm h₁ lemma dense_or_discrete [linear_order α] (a₁ a₂ : α) : (∃ a, a₁ < a ∧ a < a₂) ∨ ((∀ a, a₁ < a → a₂ ≤ a) ∧ (∀ a < a₂, a ≤ a₁)) := or_iff_not_imp_left.2 $ λ h, ⟨λ a ha₁, le_of_not_gt $ λ ha₂, h ⟨a, ha₁, ha₂⟩, λ a ha₂, le_of_not_gt $ λ ha₁, h ⟨a, ha₁, ha₂⟩⟩ namespace punit variables (a b : punit.{u+1}) instance : linear_order punit := by refine_struct { le := λ _ _, true, lt := λ _ _, false, max := λ _ _, star, min := λ _ _, star, decidable_eq := punit.decidable_eq, decidable_le := λ _ _, decidable.true, decidable_lt := λ _ _, decidable.false }; intros; trivial <|> simp only [eq_iff_true_of_subsingleton, not_true, and_false] <|> exact or.inl trivial lemma max_eq : max a b = star := rfl lemma min_eq : min a b = star := rfl @[simp] protected lemma le : a ≤ b := trivial @[simp] lemma not_lt : ¬ a < b := not_false instance : densely_ordered punit := ⟨λ _ _, false.elim⟩ end punit section prop /-- Propositions form a complete boolean algebra, where the `≤` relation is given by implication. -/ instance Prop.has_le : has_le Prop := ⟨(→)⟩ @[simp] lemma le_Prop_eq : ((≤) : Prop → Prop → Prop) = (→) := rfl lemma subrelation_iff_le {r s : α → α → Prop} : subrelation r s ↔ r ≤ s := iff.rfl instance Prop.partial_order : partial_order Prop := { le_refl := λ _, id, le_trans := λ a b c f g, g ∘ f, le_antisymm := λ a b Hab Hba, propext ⟨Hab, Hba⟩, ..Prop.has_le } end prop variables {s : β → β → Prop} {t : γ → γ → Prop} /-! ### Linear order from a total partial order -/ /-- Type synonym to create an instance of `linear_order` from a `partial_order` and `is_total α (≤)` -/ def as_linear_order (α : Type u) := α instance {α} [inhabited α] : inhabited (as_linear_order α) := ⟨ (default : α) ⟩ noncomputable instance as_linear_order.linear_order {α} [partial_order α] [is_total α (≤)] : linear_order (as_linear_order α) := { le_total := @total_of α (≤) _, decidable_le := classical.dec_rel _, .. (_ : partial_order α) }
57f82de7b2ade58e0a69b3c905ab874bc0f1d2c5
b7f22e51856f4989b970961f794f1c435f9b8f78
/hott/algebra/trunc_group.hlean
9ea7e91fcb303db57f0c97026350c206c9fe065e
[ "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
2,733
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn truncating an ∞-group to a group -/ import hit.trunc algebra.group open eq is_trunc trunc namespace algebra section parameters (n : trunc_index) {A : Type} (mul : A → A → A) (inv : A → A) (one : A) (mul_assoc : ∀a b c, mul (mul a b) c = mul a (mul b c)) (one_mul : ∀a, mul one a = a) (mul_one : ∀a, mul a one = a) (mul_left_inv : ∀a, mul (inv a) a = one) local abbreviation G := trunc n A include mul definition trunc_mul [unfold 9 10] (g h : G) : G := begin induction g with p, induction h with q, exact tr (mul p q) end omit mul include inv definition trunc_inv [unfold 9] (g : G) : G := begin induction g with p, exact tr (inv p) end omit inv include one definition trunc_one [constructor] : G := tr one local notation 1 := trunc_one local postfix ⁻¹ := trunc_inv local infix * := trunc_mul parameters {mul} {inv} {one} omit one include mul_assoc theorem trunc_mul_assoc (g₁ g₂ g₃ : G) : g₁ * g₂ * g₃ = g₁ * (g₂ * g₃) := begin induction g₁ with p₁, induction g₂ with p₂, induction g₃ with p₃, exact ap tr !mul_assoc, end omit mul_assoc include one_mul theorem trunc_one_mul (g : G) : 1 * g = g := begin induction g with p, exact ap tr !one_mul end omit one_mul include mul_one theorem trunc_mul_one (g : G) : g * 1 = g := begin induction g with p, exact ap tr !mul_one end omit mul_one include mul_left_inv theorem trunc_mul_left_inv (g : G) : g⁻¹ * g = 1 := begin induction g with p, exact ap tr !mul_left_inv end omit mul_left_inv theorem trunc_mul_comm (mul_comm : ∀a b, mul a b = mul b a) (g h : G) : g * h = h * g := begin induction g with p, induction h with q, exact ap tr !mul_comm end parameters (mul) (inv) (one) definition trunc_group [constructor] : group (trunc 0 A) := ⦃group, mul := algebra.trunc_mul 0 mul, mul_assoc := algebra.trunc_mul_assoc 0 mul_assoc, one := algebra.trunc_one 0 one, one_mul := algebra.trunc_one_mul 0 one_mul, mul_one := algebra.trunc_mul_one 0 mul_one, inv := algebra.trunc_inv 0 inv, mul_left_inv := algebra.trunc_mul_left_inv 0 mul_left_inv, is_set_carrier := _⦄ definition trunc_comm_group [constructor] (mul_comm : ∀a b, mul a b = mul b a) : comm_group (trunc 0 A) := ⦃comm_group, trunc_group, mul_comm := algebra.trunc_mul_comm 0 mul_comm⦄ end end algebra
476b753b0c68547fbf8b15ec324d3545a50af24e
b2e508d02500f1512e1618150413e6be69d9db10
/src/measure_theory/integration.lean
c167da87c57eda8ccafef6cd636321e64aea5f92
[ "Apache-2.0" ]
permissive
callum-sutton/mathlib
c3788f90216e9cd43eeffcb9f8c9f959b3b01771
afd623825a3ac6bfbcc675a9b023edad3f069e89
refs/heads/master
1,591,371,888,053
1,560,990,690,000
1,560,990,690,000
192,476,045
0
0
Apache-2.0
1,568,941,843,000
1,560,837,965,000
Lean
UTF-8
Lean
false
false
46,262
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Johannes Hölzl Lebesgue integral on `ennreal`. We define simple functions and show that each Borel measurable function on `ennreal` can be approximated by a sequence of simple functions. -/ import algebra.pi_instances measure_theory.measure_space measure_theory.borel_space noncomputable theory open lattice set filter local attribute [instance] classical.prop_decidable section sequence_of_directed variables {α : Type*} {β : Type*} [encodable α] [inhabited α] open encodable noncomputable def sequence_of_directed (r : β → β → Prop) (f : α → β) (hf : directed r f) : ℕ → α | 0 := default α | (n + 1) := let p := sequence_of_directed n in match decode α n with | none := p | (some a) := classical.some (hf p a) end lemma monotone_sequence_of_directed [partial_order β] (f : α → β) (hf : directed (≤) f) : monotone (f ∘ sequence_of_directed (≤) f hf) := monotone_of_monotone_nat $ assume n, begin dsimp [sequence_of_directed], generalize eq : sequence_of_directed (≤) f hf n = p, cases h : decode α n with a, { refl }, { exact (classical.some_spec (hf p a)).1 } end lemma le_sequence_of_directed [partial_order β] (f : α → β) (hf : directed (≤) f) (a : α) : f a ≤ f (sequence_of_directed (≤) f hf (encode a + 1)) := begin simp [sequence_of_directed, -add_comm, encodek], exact (classical.some_spec (hf _ a)).2 end end sequence_of_directed namespace measure_theory variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} structure {u v} simple_func (α : Type u) [measurable_space α] (β : Type v) := (to_fun : α → β) (measurable_sn : ∀ x, is_measurable (to_fun ⁻¹' {x})) (finite : (set.range to_fun).finite) local infixr ` →ₛ `:25 := simple_func namespace simple_func section measurable variables [measurable_space α] instance has_coe_to_fun : has_coe_to_fun (α →ₛ β) := ⟨_, to_fun⟩ @[extensionality] theorem ext {f g : α →ₛ β} (H : ∀ a, f a = g a) : f = g := by cases f; cases g; congr; exact funext H protected def range (f : α →ₛ β) := f.finite.to_finset @[simp] theorem mem_range {f : α →ₛ β} {b} : b ∈ f.range ↔ ∃ a, f a = b := finite.mem_to_finset def const (α) {β} [measurable_space α] (b : β) : α →ₛ β := ⟨λ a, b, λ x, is_measurable.const _, finite_subset (set.finite_singleton b) $ by rintro _ ⟨a, rfl⟩; simp⟩ @[simp] theorem const_apply (a : α) (b : β) : (const α b) a = b := rfl lemma range_const (α) [measurable_space α] [ne : nonempty α] (b : β) : (const α b).range = {b} := begin ext b', simp [mem_range], exact ⟨assume ⟨_, h⟩, h.symm, assume h, ne.elim $ λa, ⟨a, h.symm⟩⟩ end lemma is_measurable_cut (p : α → β → Prop) (f : α →ₛ β) (h : ∀b, is_measurable {a | p a b}) : is_measurable {a | p a (f a)} := begin rw (_ : {a | p a (f a)} = ⋃ b ∈ set.range f, {a | p a b} ∩ f ⁻¹' {b}), { exact is_measurable.bUnion (countable_finite f.finite) (λ b _, is_measurable.inter (h b) (f.measurable_sn _)) }, ext a, simp, exact ⟨λ h, ⟨_, ⟨a, rfl⟩, h, rfl⟩, λ ⟨_, ⟨a', rfl⟩, h', e⟩, e.symm ▸ h'⟩ end theorem preimage_measurable (f : α →ₛ β) (s) : is_measurable (f ⁻¹' s) := is_measurable_cut (λ _ b, b ∈ s) f (λ b, by simp [is_measurable.const]) theorem measurable [measurable_space β] (f : α →ₛ β) : measurable f := λ s _, preimage_measurable f s def ite {s : set α} (hs : is_measurable s) (f g : α →ₛ β) : α →ₛ β := ⟨λ a, if a ∈ s then f a else g a, λ x, by letI : measurable_space β := ⊤; exact measurable.if hs f.measurable g.measurable _ trivial, finite_subset (finite_union f.finite g.finite) begin rintro _ ⟨a, rfl⟩, by_cases a ∈ s; simp [h], exacts [or.inl ⟨_, rfl⟩, or.inr ⟨_, rfl⟩] end⟩ @[simp] theorem ite_apply {s : set α} (hs : is_measurable s) (f g : α →ₛ β) (a) : ite hs f g a = if a ∈ s then f a else g a := rfl def bind (f : α →ₛ β) (g : β → α →ₛ γ) : α →ₛ γ := ⟨λa, g (f a) a, λ c, is_measurable_cut (λa b, g b a ∈ ({c} : set γ)) f (λ b, (g b).measurable_sn c), finite_subset (finite_bUnion f.finite (λ b, (g b).finite)) $ by rintro _ ⟨a, rfl⟩; simp; exact ⟨_, ⟨a, rfl⟩, _, rfl⟩⟩ @[simp] theorem bind_apply (f : α →ₛ β) (g : β → α →ₛ γ) (a) : f.bind g a = g (f a) a := rfl def restrict [has_zero β] (f : α →ₛ β) (s : set α) : α →ₛ β := if hs : is_measurable s then ite hs f (const α 0) else const α 0 @[simp] theorem restrict_apply [has_zero β] (f : α →ₛ β) {s : set α} (hs : is_measurable s) (a) : restrict f s a = if a ∈ s then f a else 0 := by unfold_coes; simp [restrict, hs]; apply ite_apply hs theorem restrict_preimage [has_zero β] (f : α →ₛ β) {s : set α} (hs : is_measurable s) {t : set β} (ht : (0:β) ∉ t) : restrict f s ⁻¹' t = s ∩ f ⁻¹' t := by ext a; dsimp; rw [restrict_apply]; by_cases a ∈ s; simp [h, hs, ht] def map (g : β → γ) (f : α →ₛ β) : α →ₛ γ := bind f (const α ∘ g) @[simp] theorem map_apply (g : β → γ) (f : α →ₛ β) (a) : f.map g a = g (f a) := rfl theorem map_map (g : β → γ) (h: γ → δ) (f : α →ₛ β) : (f.map g).map h = f.map (h ∘ g) := rfl theorem coe_map (g : β → γ) (f : α →ₛ β) : (f.map g : α → γ) = g ∘ f := rfl @[simp] theorem range_map (g : β → γ) (f : α →ₛ β) : (f.map g).range = f.range.image g := begin ext c, simp [mem_range], split, { rintros ⟨a, rfl⟩, exact ⟨f a, ⟨_, rfl⟩, rfl⟩ }, { rintros ⟨_, ⟨a, rfl⟩, rfl⟩, exact ⟨_, rfl⟩ } end def seq (f : α →ₛ (β → γ)) (g : α →ₛ β) : α →ₛ γ := f.bind (λf, g.map f) def pair (f : α →ₛ β) (g : α →ₛ γ) : α →ₛ (β × γ) := (f.map prod.mk).seq g @[simp] lemma pair_apply (f : α →ₛ β) (g : α →ₛ γ) (a) : pair f g a = (f a, g a) := rfl theorem bind_const (f : α →ₛ β) : f.bind (const α) = f := by ext; simp instance [has_zero β] : has_zero (α →ₛ β) := ⟨const α 0⟩ instance [has_add β] : has_add (α →ₛ β) := ⟨λf g, (f.map (+)).seq g⟩ instance [has_mul β] : has_mul (α →ₛ β) := ⟨λf g, (f.map (*)).seq g⟩ instance [has_sup β] : has_sup (α →ₛ β) := ⟨λf g, (f.map (⊔)).seq g⟩ instance [has_inf β] : has_inf (α →ₛ β) := ⟨λf g, (f.map (⊓)).seq g⟩ instance [has_le β] : has_le (α →ₛ β) := ⟨λf g, ∀a, f a ≤ g a⟩ @[simp] lemma sup_apply [has_sup β] (f g : α →ₛ β) (a : α) : (f ⊔ g) a = f a ⊔ g a := rfl @[simp] lemma mul_apply [has_mul β] (f g : α →ₛ β) (a : α) : (f * g) a = f a * g a := rfl lemma add_apply [has_add β] (f g : α →ₛ β) (a : α) : (f + g) a = f a + g a := rfl lemma add_eq_map₂ [has_add β] (f g : α →ₛ β) : f + g = (pair f g).map (λp:β×β, p.1 + p.2) := rfl lemma sup_eq_map₂ [has_sup β] (f g : α →ₛ β) : f ⊔ g = (pair f g).map (λp:β×β, p.1 ⊔ p.2) := rfl lemma const_mul_eq_map [has_mul β] (f : α →ₛ β) (b : β) : const α b * f = f.map (λa, b * a) := rfl instance [add_monoid β] : add_monoid (α →ₛ β) := { add := (+), zero := 0, add_assoc := assume f g h, ext (assume a, add_assoc _ _ _), zero_add := assume f, ext (assume a, zero_add _), add_zero := assume f, ext (assume a, add_zero _) } instance [semiring β] [add_monoid β] : has_scalar β (α →ₛ β) := ⟨λb f, f.map (λa, b * a)⟩ instance [preorder β] : preorder (α →ₛ β) := { le_refl := λf a, le_refl _, le_trans := λf g h hfg hgh a, le_trans (hfg _) (hgh a), .. simple_func.has_le } instance [partial_order β] : partial_order (α →ₛ β) := { le_antisymm := assume f g hfg hgf, ext $ assume a, le_antisymm (hfg a) (hgf a), .. simple_func.preorder } instance [order_bot β] : order_bot (α →ₛ β) := { bot := const α ⊥, bot_le := λf a, bot_le, .. simple_func.partial_order } instance [order_top β] : order_top (α →ₛ β) := { top := const α⊤, le_top := λf a, le_top, .. simple_func.partial_order } instance [semilattice_inf β] : semilattice_inf (α →ₛ β) := { inf := (⊓), inf_le_left := assume f g a, inf_le_left, inf_le_right := assume f g a, inf_le_right, le_inf := assume f g h hfh hgh a, le_inf (hfh a) (hgh a), .. simple_func.partial_order } instance [semilattice_sup β] : semilattice_sup (α →ₛ β) := { sup := (⊔), le_sup_left := assume f g a, le_sup_left, le_sup_right := assume f g a, le_sup_right, sup_le := assume f g h hfh hgh a, sup_le (hfh a) (hgh a), .. simple_func.partial_order } instance [semilattice_sup_bot β] : semilattice_sup_bot (α →ₛ β) := { .. simple_func.lattice.semilattice_sup,.. simple_func.lattice.order_bot } instance [lattice β] : lattice (α →ₛ β) := { .. simple_func.lattice.semilattice_sup,.. simple_func.lattice.semilattice_inf } instance [bounded_lattice β] : bounded_lattice (α →ₛ β) := { .. simple_func.lattice.lattice, .. simple_func.lattice.order_bot, .. simple_func.lattice.order_top } lemma finset_sup_apply [semilattice_sup_bot β] {f : γ → α →ₛ β} (s : finset γ) (a : α) : s.sup f a = s.sup (λc, f c a) := begin refine finset.induction_on s rfl _, assume a s hs ih, rw [finset.sup_insert, finset.sup_insert, sup_apply, ih] end section approx section variables [topological_space β] [semilattice_sup_bot β] [has_zero β] def approx (i : ℕ → β) (f : α → β) (n : ℕ) : α →ₛ β := (finset.range n).sup (λk, restrict (const α (i k)) {a:α | i k ≤ f a}) lemma approx_apply [ordered_topology β] {i : ℕ → β} {f : α → β} {n : ℕ} (a : α) (hf : _root_.measurable f) : (approx i f n : α →ₛ β) a = (finset.range n).sup (λk, if i k ≤ f a then i k else 0) := begin dsimp only [approx], rw [finset_sup_apply], congr, funext k, rw [restrict_apply], refl, exact (hf.preimage $ is_measurable_of_is_closed $ is_closed_ge' _) end lemma monotone_approx (i : ℕ → β) (f : α → β) : monotone (approx i f) := assume n m h, finset.sup_mono $ finset.range_subset.2 h lemma approx_comp [ordered_topology β] [measurable_space γ] {i : ℕ → β} {f : γ → β} {g : α → γ} {n : ℕ} (a : α) (hf : _root_.measurable f) (hg : _root_.measurable g) : (approx i (f ∘ g) n : α →ₛ β) a = (approx i f n : γ →ₛ β) (g a) := by rw [approx_apply _ hf, approx_apply _ (hf.comp hg)] end lemma supr_approx_apply [topological_space β] [complete_lattice β] [ordered_topology β] [has_zero β] (i : ℕ → β) (f : α → β) (a : α) (hf : _root_.measurable f) (h_zero : (0 : β) = ⊥): (⨆n, (approx i f n : α →ₛ β) a) = (⨆k (h : i k ≤ f a), i k) := begin refine le_antisymm (supr_le $ assume n, _) (supr_le $ assume k, supr_le $ assume hk, _), { rw [approx_apply a hf, h_zero], refine finset.sup_le (assume k hk, _), split_ifs, exact le_supr_of_le k (le_supr _ h), exact bot_le }, { refine le_supr_of_le (k+1) _, rw [approx_apply a hf], have : k ∈ finset.range (k+1) := finset.mem_range.2 (nat.lt_succ_self _), refine le_trans (le_of_eq _) (finset.le_sup this), rw [if_pos hk] } end end approx section eapprox def ennreal_rat_embed (n : ℕ) : ennreal := nnreal.of_real ((encodable.decode ℚ n).get_or_else (0 : ℚ)) lemma ennreal_rat_embed_encode (q : ℚ) (hq : 0 ≤ q) : ennreal_rat_embed (encodable.encode q) = nnreal.of_real q := by rw [ennreal_rat_embed, encodable.encodek]; refl def eapprox : (α → ennreal) → ℕ → α →ₛ ennreal := approx ennreal_rat_embed lemma monotone_eapprox (f : α → ennreal) : monotone (eapprox f) := monotone_approx _ f lemma supr_eapprox_apply (f : α → ennreal) (hf : _root_.measurable f) (a : α) : (⨆n, (eapprox f n : α →ₛ ennreal) a) = f a := begin rw [eapprox, supr_approx_apply ennreal_rat_embed f a hf rfl], refine le_antisymm (supr_le $ assume i, supr_le $ assume hi, hi) (le_of_not_gt _), assume h, rcases ennreal.lt_iff_exists_rat_btwn.1 h with ⟨q, hq, lt_q, q_lt⟩, have : (nnreal.of_real q : ennreal) ≤ (⨆ (k : ℕ) (h : ennreal_rat_embed k ≤ f a), ennreal_rat_embed k), { refine le_supr_of_le (encodable.encode q) _, rw [ennreal_rat_embed_encode q hq], refine le_supr_of_le (le_of_lt q_lt) _, exact le_refl _ }, exact lt_irrefl _ (lt_of_le_of_lt this lt_q) end lemma eapprox_comp [measurable_space γ] {f : γ → ennreal} {g : α → γ} {n : ℕ} (hf : _root_.measurable f) (hg : _root_.measurable g) : (eapprox (f ∘ g) n : α → ennreal) = (eapprox f n : γ →ₛ ennreal) ∘ g := funext $ assume a, approx_comp a hf hg end eapprox end measurable section measure variables [measure_space α] def integral (f : α →ₛ ennreal) : ennreal := f.range.sum (λ x, x * volume (f ⁻¹' {x})) -- TODO: slow simp proofs lemma map_integral (g : β → ennreal) (f : α →ₛ β) : (f.map g).integral = f.range.sum (λ x, g x * volume (f ⁻¹' {x})) := begin simp only [integral, coe_map, range_map], refine finset.sum_image' _ (assume b hb, _), rcases mem_range.1 hb with ⟨a, rfl⟩, let s' := f.range.filter (λb, g b = g (f a)), have : g ∘ ⇑f ⁻¹' {g (f a)} = (⋃b∈s', ⇑f ⁻¹' {b}), { ext a', simp, split, { assume eq, exact ⟨⟨_, rfl⟩, eq⟩ }, { rintros ⟨_, eq⟩, exact eq } }, calc g (f a) * volume (g ∘ ⇑f ⁻¹' {g (f a)}) = g (f a) * volume (⋃b∈s', ⇑f ⁻¹' {b}) : by rw [this] ... = g (f a) * s'.sum (λb, volume (f ⁻¹' {b})) : begin rw [volume_bUnion_finset], { simp [pairwise_on, (on)], rintros b a₀ rfl eq₀ b a₁ rfl eq₁ ne a ⟨h₁, h₂⟩, simp at h₁ h₂, rw [← h₁, h₂] at ne, exact ne rfl }, exact assume a ha, preimage_measurable _ _ end ... = s'.sum (λb, g (f a) * volume (f ⁻¹' {b})) : by rw [finset.mul_sum] ... = s'.sum (λb, g b * volume (f ⁻¹' {b})) : finset.sum_congr rfl $ by simp {contextual := tt} end lemma zero_integral : (0 : α →ₛ ennreal).integral = 0 := begin refine (finset.sum_eq_zero_iff_of_nonneg $ assume _ _, zero_le _).2 _, assume r hr, rcases mem_range.1 hr with ⟨a, rfl⟩, exact zero_mul _ end lemma add_integral (f g : α →ₛ ennreal) : (f + g).integral = f.integral + g.integral := calc (f + g).integral = (pair f g).range.sum (λx, x.1 * volume (pair f g ⁻¹' {x}) + x.2 * volume (pair f g ⁻¹' {x})) : by rw [add_eq_map₂, map_integral]; exact finset.sum_congr rfl (assume a ha, add_mul _ _ _) ... = (pair f g).range.sum (λx, x.1 * volume (pair f g ⁻¹' {x})) + (pair f g).range.sum (λx, x.2 * volume (pair f g ⁻¹' {x})) : by rw [finset.sum_add_distrib] ... = ((pair f g).map prod.fst).integral + ((pair f g).map prod.snd).integral : by rw [map_integral, map_integral] ... = integral f + integral g : rfl lemma const_mul_integral (f : α →ₛ ennreal) (x : ennreal) : (const α x * f).integral = x * f.integral := calc (f.map (λa, x * a)).integral = f.range.sum (λr, x * r * volume (f ⁻¹' {r})) : by rw [map_integral] ... = f.range.sum (λr, x * (r * volume (f ⁻¹' {r}))) : finset.sum_congr rfl (assume a ha, mul_assoc _ _ _) ... = x * f.integral : finset.mul_sum.symm lemma mem_restrict_range [has_zero β] {r : β} {s : set α} {f : α →ₛ β} (hs : is_measurable s) : r ∈ (restrict f s).range ↔ (r = 0 ∧ s ≠ univ) ∨ (∃a∈s, f a = r) := begin simp only [mem_range, restrict_apply, hs], split, { rintros ⟨a, ha⟩, split_ifs at ha, { exact or.inr ⟨a, h, ha⟩ }, { exact or.inl ⟨ha.symm, assume eq, h $ eq.symm ▸ trivial⟩ } }, { rintros (⟨rfl, h⟩ | ⟨a, ha, rfl⟩), { have : ¬ ∀a, a ∈ s := assume this, h $ eq_univ_of_forall this, rcases not_forall.1 this with ⟨a, ha⟩, refine ⟨a, _⟩, rw [if_neg ha] }, { refine ⟨a, _⟩, rw [if_pos ha] } } end lemma restrict_preimage' {r : ennreal} {s : set α} (f : α →ₛ ennreal) (hs : is_measurable s) (hr : r ≠ 0): (restrict f s) ⁻¹' {r} = (f ⁻¹' {r} ∩ s) := begin ext a, by_cases a ∈ s; simp [hs, h, hr.symm] end lemma restrict_integral (f : α →ₛ ennreal) (s : set α) (hs : is_measurable s) : (restrict f s).integral = f.range.sum (λr, r * volume (f ⁻¹' {r} ∩ s)) := begin refine finset.sum_bij_ne_zero (λr _ _, r) _ _ _ _, { assume r hr, rcases (mem_restrict_range hs).1 hr with ⟨rfl, h⟩ | ⟨a, ha, rfl⟩, { simp }, { assume _, exact mem_range.2 ⟨a, rfl⟩ } }, { assume a b _ _ _ _ h, exact h }, { assume r hr, by_cases r0 : r = 0, { simp [r0] }, assume h0, rcases mem_range.1 hr with ⟨a, rfl⟩, have : f ⁻¹' {f a} ∩ s ≠ ∅, { assume h, simpa [h] using h0 }, rcases ne_empty_iff_exists_mem.1 this with ⟨a', eq', ha'⟩, refine ⟨_, (mem_restrict_range hs).2 (or.inr ⟨a', ha', _⟩), _, rfl⟩, { simpa using eq' }, { rwa [restrict_preimage' _ hs r0] } }, { assume r hr ne, by_cases r = 0, { simp [h] }, rw [restrict_preimage' _ hs h] } end lemma restrict_const_integral (c : ennreal) (s : set α) (hs : is_measurable s) : (restrict (const α c) s).integral = c * volume s := have (@const α ennreal _ c) ⁻¹' {c} = univ, begin refine eq_univ_of_forall (assume a, _), simp, end, calc (restrict (const α c) s).integral = c * volume ((const α c) ⁻¹' {c} ∩ s) : begin rw [restrict_integral (const α c) s hs], refine finset.sum_eq_single c _ _, { assume r hr, rcases mem_range.1 hr with ⟨a, rfl⟩, contradiction }, { by_cases nonempty α, { assume ne, rcases h with ⟨a⟩, exfalso, exact ne (mem_range.2 ⟨a, rfl⟩) }, { assume empty, have : (@const α ennreal _ c) ⁻¹' {c} ∩ s = ∅, { ext a, exfalso, exact h ⟨a⟩ }, simp only [this, volume_empty, mul_zero] } } end ... = c * volume s : by rw [this, univ_inter] lemma integral_sup_le (f g : α →ₛ ennreal) : f.integral ⊔ g.integral ≤ (f ⊔ g).integral := calc f.integral ⊔ g.integral = ((pair f g).map prod.fst).integral ⊔ ((pair f g).map prod.snd).integral : rfl ... ≤ (pair f g).range.sum (λx, (x.1 ⊔ x.2) * volume (pair f g ⁻¹' {x})) : begin rw [map_integral, map_integral], refine sup_le _ _; refine finset.sum_le_sum' (λ a _, canonically_ordered_semiring.mul_le_mul _ (le_refl _)), exact le_sup_left, exact le_sup_right end ... = (f ⊔ g).integral : by rw [sup_eq_map₂, map_integral] lemma integral_le_integral (f g : α →ₛ ennreal) (h : f ≤ g) : f.integral ≤ g.integral := calc f.integral ≤ f.integral ⊔ g.integral : le_sup_left ... ≤ (f ⊔ g).integral : integral_sup_le _ _ ... = g.integral : by rw [sup_of_le_right h] lemma integral_congr (f g : α →ₛ ennreal) (h : {a | f a = g a} ∈ (@measure_space.μ α _).a_e) : f.integral = g.integral := show ((pair f g).map prod.fst).integral = ((pair f g).map prod.snd).integral, from begin rw [map_integral, map_integral], refine finset.sum_congr rfl (assume p hp, _), rcases mem_range.1 hp with ⟨a, rfl⟩, by_cases eq : f a = g a, { dsimp only [pair_apply], rw eq }, { have : volume ((pair f g) ⁻¹' {(f a, g a)}) = 0, { refine volume_mono_null (assume a' ha', _) h, simp at ha', show f a' ≠ g a', rwa [ha'.1, ha'.2] }, simp [this] } end lemma integral_map {β} [measure_space β] (f : α →ₛ ennreal) (g : β →ₛ ennreal) (m : α → β) (hm : _root_.measurable m) (eq : ∀a:α, f a = g (m a)) (h : ∀s:set β, is_measurable s → volume s = volume (m ⁻¹' s)) : f.integral = g.integral := have f_eq : (f : α → ennreal) = g ∘ m := funext eq, have vol_f : ∀r, volume (f ⁻¹' {r}) = volume (g ⁻¹' {r}), by { assume r, rw [h, f_eq, preimage_comp], exact measurable_sn _ _ }, begin simp [integral, vol_f], refine finset.sum_subset _ _, { simp [finset.subset_iff, f_eq], rintros r a rfl, exact ⟨_, rfl⟩ }, { assume r hrg hrf, rw [simple_func.mem_range, not_exists] at hrf, have : f ⁻¹' {r} = ∅ := set.eq_empty_of_subset_empty (assume a, by simpa using hrf a), simp [(vol_f _).symm, this] } end end measure end simple_func section lintegral open simple_func variable [measure_space α] /-- The lower Lebesgue integral -/ def lintegral (f : α → ennreal) : ennreal := ⨆ (s : α →ₛ ennreal) (hf : f ≥ s), s.integral notation `∫⁻` binders `, ` r:(scoped f, lintegral f) := r theorem simple_func.lintegral_eq_integral (f : α →ₛ ennreal) : (∫⁻ a, f a) = f.integral := le_antisymm (supr_le $ assume s, supr_le $ assume hs, integral_le_integral _ _ hs) (le_supr_of_le f $ le_supr_of_le (le_refl f) $ le_refl _) lemma lintegral_le_lintegral (f g : α → ennreal) (h : f ≤ g) : (∫⁻ a, f a) ≤ (∫⁻ a, g a) := supr_le_supr $ assume s, supr_le $ assume hs, le_supr_of_le (le_trans hs h) (le_refl _) lemma lintegral_eq_nnreal (f : α → ennreal) : (∫⁻ a, f a) = (⨆ (s : α →ₛ nnreal) (hf : f ≥ s.map (coe : nnreal → ennreal)), (s.map (coe : nnreal → ennreal)).integral) := begin let c : nnreal → ennreal := coe, refine le_antisymm (supr_le $ assume s, supr_le $ assume hs, _) (supr_le $ assume s, supr_le $ assume hs, le_supr_of_le (s.map c) $ le_supr _ hs), by_cases {a | s a ≠ ⊤} ∈ (@measure_space.μ α _).a_e, { have : f ≥ (s.map ennreal.to_nnreal).map c := le_trans (assume a, ennreal.coe_to_nnreal_le_self) hs, refine le_supr_of_le (s.map ennreal.to_nnreal) (le_supr_of_le this (le_of_eq $ integral_congr _ _ _)), exact filter.mem_sets_of_superset h (assume a ha, (ennreal.coe_to_nnreal ha).symm) }, { have h_vol_s : volume {a : α | s a = ⊤} ≠ 0, { simp [measure.a_e, set.compl_set_of] at h, assumption }, let n : ℕ → (α →ₛ nnreal) := λn, restrict (const α (n : nnreal)) (s ⁻¹' {⊤}), have n_le_s : ∀i, (n i).map c ≤ s, { assume i a, dsimp [n, c], rw [restrict_apply _ (s.preimage_measurable _)], split_ifs with ha, { simp at ha, exact ha.symm ▸ le_top }, { exact zero_le _ } }, have approx_s : ∀ (i : ℕ), ↑i * volume {a : α | s a = ⊤} ≤ integral (map c (n i)), { assume i, have : {a : α | s a = ⊤} = s ⁻¹' {⊤}, { ext a, simp }, rw [this, ← restrict_const_integral _ _ (s.preimage_measurable _)], { refine integral_le_integral _ _ (assume a, le_of_eq _), simp [n, c, restrict_apply, s.preimage_measurable], split_ifs; simp [ennreal.coe_nat] }, }, calc s.integral ≤ ⊤ : le_top ... = (⨆i:ℕ, (i : ennreal) * volume {a | s a = ⊤}) : by rw [← ennreal.supr_mul, ennreal.supr_coe_nat, ennreal.top_mul, if_neg h_vol_s] ... ≤ (⨆i, ((n i).map c).integral) : supr_le_supr approx_s ... ≤ ⨆ (s : α →ₛ nnreal) (hf : f ≥ s.map c), (s.map c).integral : have ∀i, ((n i).map c : α → ennreal) ≤ f := assume i, le_trans (n_le_s i) hs, (supr_le $ assume i, le_supr_of_le (n i) (le_supr (λh, ((n i).map c).integral) (this i))) } end /-- Monotone convergence theorem -- somtimes called Beppo-Levi convergence. See `lintegral_supr_directed` for a more general form. -/ theorem lintegral_supr {f : ℕ → α → ennreal} (hf : ∀n, measurable (f n)) (h_mono : monotone f) : (∫⁻ a, ⨆n, f n a) = (⨆n, ∫⁻ a, f n a) := let c : nnreal → ennreal := coe in let F (a:α) := ⨆n, f n a in have hF : measurable F := measurable.supr hf, show (∫⁻ a, F a) = (⨆n, ∫⁻ a, f n a), begin refine le_antisymm _ _, { rw [lintegral_eq_nnreal], refine supr_le (assume s, supr_le (assume hsf, _)), refine ennreal.le_of_forall_lt_one_mul_lt (assume a ha, _), rcases ennreal.lt_iff_exists_coe.1 ha with ⟨r, rfl, ha⟩, have ha : r < 1 := ennreal.coe_lt_coe.1 ha, let rs := s.map (λa, r * a), have eq_rs : (const α r : α →ₛ ennreal) * map c s = rs.map c, { ext1 a, exact ennreal.coe_mul.symm }, have eq : ∀p, (rs.map c) ⁻¹' {p} = (⋃n, (rs.map c) ⁻¹' {p} ∩ {a | p ≤ f n a}), { assume p, rw [← inter_Union_left, ← inter_univ ((map c rs) ⁻¹' {p})] {occs := occurrences.pos [1]}, refine set.ext (assume x, and_congr_right $ assume hx, (true_iff _).2 _), by_cases p_eq : p = 0, { simp [p_eq] }, simp at hx, subst hx, have : r * s x ≠ 0, { rwa [(≠), ← ennreal.coe_eq_zero] }, have : s x ≠ 0, { refine mt _ this, assume h, rw [h, mul_zero] }, have : (rs.map c) x < ⨆ (n : ℕ), f n x, { refine lt_of_lt_of_le (ennreal.coe_lt_coe.2 (_)) (hsf x), suffices : r * s x < 1 * s x, simpa [rs], exact mul_lt_mul_of_pos_right ha (zero_lt_iff_ne_zero.2 this) }, rcases lt_supr_iff.1 this with ⟨i, hi⟩, exact mem_Union.2 ⟨i, le_of_lt hi⟩ }, have mono : ∀r:ennreal, monotone (λn, (rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}), { assume r i j h, refine inter_subset_inter (subset.refl _) _, assume x hx, exact le_trans hx (h_mono h x) }, have h_meas : ∀n, is_measurable {a : α | ⇑(map c rs) a ≤ f n a} := assume n, measurable_le (simple_func.measurable _) (hf n), calc (r:ennreal) * integral (s.map c) = (rs.map c).range.sum (λr, r * volume ((rs.map c) ⁻¹' {r})) : by rw [← const_mul_integral, integral, eq_rs] ... ≤ (rs.map c).range.sum (λr, r * volume (⋃n, (rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a})) : le_of_eq (finset.sum_congr rfl $ assume x hx, by rw ← eq) ... ≤ (rs.map c).range.sum (λr, (⨆n, r * volume ((rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a}))) : le_of_eq (finset.sum_congr rfl $ assume x hx, begin rw [volume, measure_Union_eq_supr_nat _ (mono x), ennreal.mul_supr], { assume i, refine is_measurable.inter ((rs.map c).preimage_measurable _) _, refine (hf i).preimage _, exact is_measurable_of_is_closed (is_closed_ge' _) } end) ... ≤ ⨆n, (rs.map c).range.sum (λr, r * volume ((rs.map c) ⁻¹' {r} ∩ {a | r ≤ f n a})) : begin refine le_of_eq _, rw [ennreal.finset_sum_supr_nat], assume p i j h, exact canonically_ordered_semiring.mul_le_mul (le_refl _) (volume_mono $ mono p h) end ... ≤ (⨆n:ℕ, ((rs.map c).restrict {a | (rs.map c) a ≤ f n a}).integral) : begin refine supr_le_supr (assume n, _), rw [restrict_integral _ _ (h_meas n)], { refine le_of_eq (finset.sum_congr rfl $ assume r hr, _), congr' 2, ext a, refine and_congr_right _, simp {contextual := tt} } end ... ≤ (⨆n, ∫⁻ a, f n a) : begin refine supr_le_supr (assume n, _), rw [← simple_func.lintegral_eq_integral], refine lintegral_le_lintegral _ _ (assume a, _), dsimp, rw [restrict_apply], split_ifs; simp, simpa using h, exact h_meas n end }, { exact supr_le (assume n, lintegral_le_lintegral _ _ $ assume a, le_supr _ n) } end lemma lintegral_eq_supr_eapprox_integral {f : α → ennreal} (hf : measurable f) : (∫⁻ a, f a) = (⨆n, (eapprox f n).integral) := calc (∫⁻ a, f a) = (∫⁻ a, ⨆n, (eapprox f n : α → ennreal) a) : by congr; ext a; rw [supr_eapprox_apply f hf] ... = (⨆n, ∫⁻ a, (eapprox f n : α → ennreal) a) : begin rw [lintegral_supr], { assume n, exact (eapprox f n).measurable }, { assume i j h, exact (monotone_eapprox f h) } end ... = (⨆n, (eapprox f n).integral) : by congr; ext n; rw [(eapprox f n).lintegral_eq_integral] lemma lintegral_add {f g : α → ennreal} (hf : measurable f) (hg : measurable g) : (∫⁻ a, f a + g a) = (∫⁻ a, f a) + (∫⁻ a, g a) := calc (∫⁻ a, f a + g a) = (∫⁻ a, (⨆n, (eapprox f n : α → ennreal) a) + (⨆n, (eapprox g n : α → ennreal) a)) : by congr; funext a; rw [supr_eapprox_apply f hf, supr_eapprox_apply g hg] ... = (∫⁻ a, (⨆n, (eapprox f n + eapprox g n : α → ennreal) a)) : begin congr, funext a, rw [ennreal.supr_add_supr_of_monotone], { refl }, { assume i j h, exact monotone_eapprox _ h a }, { assume i j h, exact monotone_eapprox _ h a }, end ... = (⨆n, (eapprox f n).integral + (eapprox g n).integral) : begin rw [lintegral_supr], { congr, funext n, rw [← simple_func.add_integral, ← simple_func.lintegral_eq_integral], refl }, { assume n, exact measurable_add (eapprox f n).measurable (eapprox g n).measurable }, { assume i j h a, exact add_le_add' (monotone_eapprox _ h _) (monotone_eapprox _ h _) } end ... = (⨆n, (eapprox f n).integral) + (⨆n, (eapprox g n).integral) : by refine (ennreal.supr_add_supr_of_monotone _ _).symm; { assume i j h, exact simple_func.integral_le_integral _ _ (monotone_eapprox _ h) } ... = (∫⁻ a, f a) + (∫⁻ a, g a) : by rw [lintegral_eq_supr_eapprox_integral hf, lintegral_eq_supr_eapprox_integral hg] @[simp] lemma lintegral_zero : (∫⁻ a:α, 0) = 0 := show (∫⁻ a:α, (0 : α →ₛ ennreal) a) = 0, by rw [simple_func.lintegral_eq_integral, zero_integral] lemma lintegral_finset_sum (s : finset β) {f : β → α → ennreal} (hf : ∀b, measurable (f b)) : (∫⁻ a, s.sum (λb, f b a)) = s.sum (λb, ∫⁻ a, f b a) := begin refine finset.induction_on s _ _, { simp }, { assume a s has ih, simp [has], rw [lintegral_add (hf _) (measurable_finset_sum s hf), ih] } end lemma lintegral_const_mul (r : ennreal) {f : α → ennreal} (hf : measurable f) : (∫⁻ a, r * f a) = r * (∫⁻ a, f a) := calc (∫⁻ a, r * f a) = (∫⁻ a, (⨆n, (const α r * eapprox f n) a)) : by congr; funext a; rw [← supr_eapprox_apply f hf, ennreal.mul_supr]; refl ... = (⨆n, r * (eapprox f n).integral) : begin rw [lintegral_supr], { congr, funext n, rw [← simple_func.const_mul_integral, ← simple_func.lintegral_eq_integral] }, { assume n, exact simple_func.measurable _ }, { assume i j h a, exact canonically_ordered_semiring.mul_le_mul (le_refl _) (monotone_eapprox _ h _) } end ... = r * (∫⁻ a, f a) : by rw [← ennreal.mul_supr, lintegral_eq_supr_eapprox_integral hf] lemma lintegral_supr_const (r : ennreal) {s : set α} (hs : is_measurable s) : (∫⁻ a, ⨆(h : a ∈ s), r) = r * volume s := begin rw [← restrict_const_integral r s hs, ← (restrict (const α r) s).lintegral_eq_integral], congr; ext a; by_cases a ∈ s; simp [h, hs] end lemma lintegral_le_lintegral_ae {f g : α → ennreal} (h : ∀ₘ a, f a ≤ g a) : (∫⁻ a, f a) ≤ (∫⁻ a, g a) := begin rcases exists_is_measurable_superset_of_measure_eq_zero h with ⟨t, hts, ht, ht0⟩, have : - t ∈ (@measure_space.μ α _).a_e, { rw [measure.mem_a_e_iff, lattice.neg_neg, ht0] }, refine (supr_le $ assume s, supr_le $ assume hfs, le_supr_of_le (s.restrict (- t)) $ le_supr_of_le _ _), { assume a, by_cases a ∈ t; simp [h, simple_func.restrict_apply, ht.compl], exact le_trans (hfs a) (by_contradiction $ assume hnfg, h (hts hnfg)) }, { refine le_of_eq (s.integral_congr _ _), filter_upwards [this], refine assume a hnt, _, by_cases hat : a ∈ t; simp [hat, ht.compl], exact (hnt hat).elim } end lemma lintegral_congr_ae {f g : α → ennreal} (h : ∀ₘ a, f a = g a) : (∫⁻ a, f a) = (∫⁻ a, g a) := le_antisymm (lintegral_le_lintegral_ae $ by filter_upwards [h] assume a h, le_of_eq h) (lintegral_le_lintegral_ae $ by filter_upwards [h] assume a h, le_of_eq h.symm) lemma lintegral_eq_zero_iff {f : α → ennreal} (hf : measurable f) : lintegral f = 0 ↔ (∀ₘ a, f a = 0) := begin refine iff.intro (assume h, _) (assume h, _), { have : ∀n:ℕ, ∀ₘ a, f a < n⁻¹, { assume n, have : is_measurable {a : α | f a ≥ n⁻¹ }, { exact hf _ (is_measurable_of_is_closed $ is_closed_ge' _) }, have : (n : ennreal)⁻¹ * volume {a | f a ≥ n⁻¹ } = 0, { rw [← simple_func.restrict_const_integral _ _ this, ← le_zero_iff_eq, ← simple_func.lintegral_eq_integral], refine le_trans (lintegral_le_lintegral _ _ _) (le_of_eq h), assume a, by_cases h : (n : ennreal)⁻¹ ≤ f a; simp [h, (≥), this] }, rw [ennreal.mul_eq_zero, ennreal.inv_eq_zero] at this, simpa [ennreal.nat_ne_top, all_ae_iff] using this }, filter_upwards [all_ae_all_iff.2 this], dsimp, assume a ha, by_contradiction h, rcases ennreal.exists_inv_nat_lt h with ⟨n, hn⟩, exact (lt_irrefl _ $ lt_trans hn $ ha n).elim }, { calc lintegral f = lintegral (λa:α, 0) : lintegral_congr_ae h ... = 0 : lintegral_zero } end /-- Weaker version of the monotone convergence theorem-/ lemma lintegral_supr_ae {f : ℕ → α → ennreal} (hf : ∀n, measurable (f n)) (h_mono : ∀n, ∀ₘ a, f n a ≤ f n.succ a) : (∫⁻ a, ⨆n, f n a) = (⨆n, ∫⁻ a, f n a) := let ⟨s, hs⟩ := exists_is_measurable_superset_of_measure_eq_zero (all_ae_iff.1 (all_ae_all_iff.2 h_mono)) in let g := λ n a, if a ∈ s then 0 else f n a in have g_eq_f : ∀ₘ a, ∀n, g n a = f n a, begin have := hs.2.2, rw [← compl_compl s] at this, filter_upwards [(measure.mem_a_e_iff (-s)).2 this] assume a ha n, if_neg ha end, calc (∫⁻ a, ⨆n, f n a) = (∫⁻ a, ⨆n, g n a) : lintegral_congr_ae begin filter_upwards [g_eq_f], assume a ha, congr, funext, exact (ha n).symm end ... = ⨆n, (∫⁻ a, g n a) : lintegral_supr (assume n, measurable.if hs.2.1 measurable_const (hf n)) (monotone_of_monotone_nat $ assume n a, classical.by_cases (assume h : a ∈ s, by simp [g, if_pos h]) (assume h : a ∉ s, begin simp only [g, if_neg h], have := hs.1, rw subset_def at this, have := mt (this a) h, simp only [not_not, mem_set_of_eq] at this, exact this n end)) ... = ⨆n, (∫⁻ a, f n a) : begin congr, funext, apply lintegral_congr_ae, filter_upwards [g_eq_f] assume a ha, ha n end lemma lintegral_sub {f g : α → ennreal} (hf : measurable f) (hg : measurable g) (hg_fin : lintegral g < ⊤) (h_le : ∀ₘ a, g a ≤ f a) : (∫⁻ a, f a - g a) = (∫⁻ a, f a) - (∫⁻ a, g a) := begin rw [← ennreal.add_right_inj hg_fin, ennreal.sub_add_cancel_of_le (lintegral_le_lintegral_ae h_le), ← lintegral_add (ennreal.measurable_sub hf hg) hg], show (∫⁻ (a : α), f a - g a + g a) = ∫⁻ (a : α), f a, apply lintegral_congr_ae, filter_upwards [h_le], simp only [add_comm, mem_set_of_eq], assume a ha, exact ennreal.add_sub_cancel_of_le ha end /-- Monotone convergence theorem for nonincreasing sequences of functions -/ lemma lintegral_infi_ae {f : ℕ → α → ennreal} (h_meas : ∀n, measurable (f n)) (h_mono : ∀n:ℕ, ∀ₘ a, f n.succ a ≤ f n a) (h_fin : lintegral (f 0) < ⊤): (∫⁻ a, ⨅n, f n a) = (⨅n, ∫⁻ a, f n a) := have fn_le_f0 : (∫⁻ a, ⨅n, f n a) ≤ lintegral (f 0), from lintegral_le_lintegral _ _ (assume a, infi_le_of_le 0 (le_refl _)), have fn_le_f0' : (⨅n, ∫⁻ a, f n a) ≤ lintegral (f 0), from infi_le_of_le 0 (le_refl _), (ennreal.sub_left_inj h_fin fn_le_f0 fn_le_f0').1 $ show lintegral (f 0) - (∫⁻ a, ⨅n, f n a) = lintegral (f 0) - (⨅n, ∫⁻ a, f n a), from calc lintegral (f 0) - (∫⁻ a, ⨅n, f n a) = ∫⁻ a, f 0 a - ⨅n, f n a : (lintegral_sub (h_meas 0) (measurable.infi h_meas) (calc (∫⁻ a, ⨅n, f n a) ≤ lintegral (f 0) : lintegral_le_lintegral _ _ (assume a, infi_le _ _) ... < ⊤ : h_fin ) (all_ae_of_all $ assume a, infi_le _ _)).symm ... = ∫⁻ a, ⨆n, f 0 a - f n a : congr rfl (funext (assume a, ennreal.sub_infi)) ... = ⨆n, ∫⁻ a, f 0 a - f n a : lintegral_supr_ae (assume n, ennreal.measurable_sub (h_meas 0) (h_meas n)) (assume n, by filter_upwards [h_mono n] assume a ha, ennreal.sub_le_sub (le_refl _) ha) ... = ⨆n, lintegral (f 0) - ∫⁻ a, f n a : have h_mono : ∀ₘ a, ∀n:ℕ, f n.succ a ≤ f n a := all_ae_all_iff.2 h_mono, have h_mono : ∀n, ∀ₘa, f n a ≤ f 0 a := assume n, begin filter_upwards [h_mono], simp only [mem_set_of_eq], assume a, assume h, induction n with n ih, {exact le_refl _}, {exact le_trans (h n) ih} end, congr rfl (funext $ assume n, lintegral_sub (h_meas _) (h_meas _) (calc (∫⁻ a, f n a) ≤ ∫⁻ a, f 0 a : lintegral_le_lintegral_ae $ h_mono n ... < ⊤ : h_fin) (h_mono n)) ... = lintegral (f 0) - (⨅n, ∫⁻ a, f n a) : ennreal.sub_infi.symm /-- Known as Fatou's lemma -/ lemma lintegral_liminf_le {f : ℕ → α → ennreal} (h_meas : ∀n, measurable (f n)) : (∫⁻ a, liminf at_top (λ n, f n a)) ≤ liminf at_top (λ n, lintegral (f n)) := calc (∫⁻ a, liminf at_top (λ n, f n a)) = ∫⁻ a, ⨆n:ℕ, ⨅i≥n, f i a : congr rfl (funext (assume a, liminf_eq_supr_infi_of_nat)) ... = ⨆n:ℕ, ∫⁻ a, ⨅i≥n, f i a : lintegral_supr begin assume n, apply measurable.infi, assume i, by_cases h : i ≥ n, {convert h_meas i, simp [h]}, {convert measurable_const, simp [h]} end begin assume n m hnm a, simp only [le_infi_iff], assume i hi, refine infi_le_of_le i (infi_le_of_le (le_trans hnm hi) (le_refl _)) end ... ≤ ⨆n:ℕ, ⨅i≥n, lintegral (f i) : supr_le_supr $ assume n, le_infi $ assume i, le_infi $ assume hi, lintegral_le_lintegral _ _ $ assume a, infi_le_of_le i $ infi_le_of_le hi $ le_refl _ ... = liminf at_top (λ n, lintegral (f n)) : liminf_eq_supr_infi_of_nat.symm lemma limsup_lintegral_le {f : ℕ → α → ennreal} {g : α → ennreal} (hf_meas : ∀ n, measurable (f n)) (hg_meas : measurable g) (h_bound : ∀n, ∀ₘa, f n a ≤ g a) (h_fin : lintegral g < ⊤) : limsup at_top (λn, lintegral (f n)) ≤ ∫⁻ a, limsup at_top (λn, f n a) := calc limsup at_top (λn, lintegral (f n)) = ⨅n:ℕ, ⨆i≥n, lintegral (f i) : limsup_eq_infi_supr_of_nat ... ≤ ⨅n:ℕ, ∫⁻ a, ⨆i≥n, f i a : infi_le_infi $ assume n, supr_le $ assume i, supr_le $ assume hi, lintegral_le_lintegral _ _ $ assume a, le_supr_of_le i $ le_supr_of_le hi (le_refl _) ... = ∫⁻ a, ⨅n:ℕ, ⨆i≥n, f i a : (lintegral_infi_ae (assume n, @measurable.supr _ _ _ _ _ _ _ _ _ (λ i a, supr (λ (h : i ≥ n), f i a)) (assume i, measurable.supr_Prop (hf_meas i))) (assume n, all_ae_of_all $ assume a, begin simp only [supr_le_iff], assume i hi, refine le_supr_of_le i _, rw [supr_pos _], exact le_refl _, exact nat.le_of_succ_le hi end ) (lt_of_le_of_lt (lintegral_le_lintegral_ae begin filter_upwards [all_ae_all_iff.2 h_bound], simp only [supr_le_iff, mem_set_of_eq], assume a ha i hi, exact ha i end ) h_fin)).symm ... = ∫⁻ a, limsup at_top (λn, f n a) : lintegral_congr_ae $ all_ae_of_all $ assume a, limsup_eq_infi_supr_of_nat.symm /-- Dominated convergence theorem for nonnegative functions -/ lemma dominated_convergence_nn {F : ℕ → α → ennreal} {f : α → ennreal} {g : α → ennreal} (hF_meas : ∀n, measurable (F n)) (hf_meas : measurable f) (hg_meas : measurable g) (h_bound : ∀n, ∀ₘ a, F n a ≤ g a) (h_fin : lintegral g < ⊤) (h_lim : ∀ₘ a, tendsto (λ n, F n a) at_top (nhds (f a))) : tendsto (λn, lintegral (F n)) at_top (nhds (lintegral f)) := begin have limsup_le_lintegral := calc limsup at_top (λ (n : ℕ), lintegral (F n)) ≤ ∫⁻ (a : α), limsup at_top (λn, F n a) : limsup_lintegral_le hF_meas hg_meas h_bound h_fin ... = lintegral f : lintegral_congr_ae $ by filter_upwards [h_lim] assume a h, limsup_eq_of_tendsto at_top_ne_bot h, have lintegral_le_liminf := calc lintegral f = ∫⁻ (a : α), liminf at_top (λ (n : ℕ), F n a) : lintegral_congr_ae $ by filter_upwards [h_lim] assume a h, (liminf_eq_of_tendsto at_top_ne_bot h).symm ... ≤ liminf at_top (λ n, lintegral (F n)) : lintegral_liminf_le hF_meas, have liminf_eq_limsup := le_antisymm (liminf_le_limsup (map_ne_bot at_top_ne_bot)) (le_trans limsup_le_lintegral lintegral_le_liminf), have liminf_eq_lintegral : liminf at_top (λ n, lintegral (F n)) = lintegral f := le_antisymm (by convert limsup_le_lintegral) lintegral_le_liminf, have limsup_eq_lintegral : limsup at_top (λ n, lintegral (F n)) = lintegral f := le_antisymm limsup_le_lintegral begin convert lintegral_le_liminf, exact liminf_eq_limsup.symm end, exact tendsto_of_liminf_eq_limsup ⟨liminf_eq_lintegral, limsup_eq_lintegral⟩ end section open encodable /-- Monotone convergence for a suprema over a directed family and indexed by an encodable type -/ theorem lintegral_supr_directed [encodable β] {f : β → α → ennreal} (hf : ∀b, measurable (f b)) (h_directed : directed (≤) f) : (∫⁻ a, ⨆b, f b a) = (⨆b, ∫⁻ a, f b a) := begin by_cases hβ : ¬ nonempty β, { have : ∀f : β → ennreal, (⨆(b : β), f b) = 0 := assume f, supr_eq_bot.2 (assume b, (hβ ⟨b⟩).elim), simp [this] }, cases of_not_not hβ with b, haveI iβ : inhabited β := ⟨b⟩, clear hβ b, have : ∀a, (⨆ b, f b a) = (⨆ n, f (sequence_of_directed (≤) f h_directed n) a), { assume a, refine le_antisymm (supr_le $ assume b, _) (supr_le $ assume n, le_supr (λn, f n a) _), exact le_supr_of_le (encode b + 1) (le_sequence_of_directed f h_directed b a) }, calc (∫⁻ a, ⨆ b, f b a) = (∫⁻ a, ⨆ n, f (sequence_of_directed (≤) f h_directed n) a) : by simp only [this] ... = (⨆ n, ∫⁻ a, f (sequence_of_directed (≤) f h_directed n) a) : lintegral_supr (assume n, hf _) (monotone_sequence_of_directed f h_directed) ... = (⨆ b, ∫⁻ a, f b a) : begin refine le_antisymm (supr_le $ assume n, _) (supr_le $ assume b, _), { exact le_supr (λb, lintegral (f b)) _ }, { exact le_supr_of_le (encode b + 1) (lintegral_le_lintegral _ _ $ le_sequence_of_directed f h_directed b) } end end end lemma lintegral_tsum [encodable β] {f : β → α → ennreal} (hf : ∀i, measurable (f i)) : (∫⁻ a, ∑ i, f i a) = (∑ i, ∫⁻ a, f i a) := begin simp only [ennreal.tsum_eq_supr_sum], rw [lintegral_supr_directed], { simp [lintegral_finset_sum _ hf] }, { assume b, exact measurable_finset_sum _ hf }, { assume s t, use [s ∪ t], split, exact assume a, finset.sum_le_sum_of_subset (finset.subset_union_left _ _), exact assume a, finset.sum_le_sum_of_subset (finset.subset_union_right _ _) } end end lintegral namespace measure def integral [measurable_space α] (m : measure α) (f : α → ennreal) : ennreal := @lintegral α { μ := m } f variables [measurable_space α] {m : measure α} @[simp] lemma integral_zero : m.integral (λa, 0) = 0 := @lintegral_zero α { μ := m } lemma integral_map [measurable_space β] {f : β → ennreal} {g : α → β} (hf : measurable f) (hg : measurable g) : (map g m).integral f = m.integral (f ∘ g) := begin rw [integral, integral, lintegral_eq_supr_eapprox_integral, lintegral_eq_supr_eapprox_integral], { congr, funext n, symmetry, apply simple_func.integral_map, { exact hg }, { assume a, exact congr_fun (simple_func.eapprox_comp hf hg) a }, { assume s hs, exact map_apply hg hs } }, exact hf.comp hg, assumption end lemma integral_dirac (a : α) {f : α → ennreal} (hf : measurable f) : (dirac a).integral f = f a := have ∀f:α →ₛ ennreal, @simple_func.integral α {μ := dirac a} f = f a, begin assume f, have : ∀r, @volume α { μ := dirac a } (⇑f ⁻¹' {r}) = ⨆ h : f a = r, 1, { assume r, transitivity, apply dirac_apply, apply simple_func.measurable_sn, refine supr_congr_Prop _ _; simp }, transitivity, apply finset.sum_eq_single (f a), { assume b hb h, simp [this, ne.symm h], }, { assume h, simp at h, exact (h a rfl).elim }, { rw [this], simp } end, begin rw [integral, lintegral_eq_supr_eapprox_integral], { simp [this, simple_func.supr_eapprox_apply f hf] }, assumption end def with_density (m : measure α) (f : α → ennreal) : measure α := if hf : measurable f then measure.of_measurable (λs hs, m.integral (λa, ⨆(h : a ∈ s), f a)) (by simp) begin assume s hs hd, have : ∀a, (⨆ (h : a ∈ ⋃i, s i), f a) = (∑i, (⨆ (h : a ∈ s i), f a)), { assume a, by_cases ha : ∃j, a ∈ s j, { rcases ha with ⟨j, haj⟩, have : ∀i, a ∈ s i ↔ j = i := assume i, iff.intro (assume hai, by_contradiction $ assume hij, hd j i hij ⟨haj, hai⟩) (by rintros rfl; assumption), simp [this, ennreal.tsum_supr_eq] }, { have : ∀i, ¬ a ∈ s i, { simpa using ha }, simp [this] } }, simp only [this], apply lintegral_tsum, { assume i, simp [supr_eq_if], exact measurable.if (hs i) hf measurable_const } end else 0 lemma with_density_apply {m : measure α} {f : α → ennreal} {s : set α} (hf : measurable f) (hs : is_measurable s) : m.with_density f s = m.integral (λa, ⨆(h : a ∈ s), f a) := by rw [with_density, dif_pos hf]; exact measure.of_measurable_apply s hs end measure end measure_theory
3e9c6c7f5039c55ec1f679bd343ae8e421c39b92
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/continued_fractions/terminated_stable_auto.lean
9ac90dea9346218295bf9cca5dbad2a35b617912
[]
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,099
lean
/- Copyright (c) 2020 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.continued_fractions.translations import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Stabilisation of gcf Computations Under Termination ## Summary We show that the continuants and convergents of a gcf stabilise once the gcf terminates. -/ namespace generalized_continued_fraction /-- If a gcf terminated at position `n`, it also terminated at `m ≥ n`.-/ theorem terminated_stable {K : Type u_1} {g : generalized_continued_fraction K} {n : ℕ} {m : ℕ} (n_le_m : n ≤ m) (terminated_at_n : terminated_at g n) : terminated_at g m := seq.terminated_stable (s g) n_le_m terminated_at_n theorem continuants_aux_stable_step_of_terminated {K : Type u_1} {g : generalized_continued_fraction K} {n : ℕ} [division_ring K] (terminated_at_n : terminated_at g n) : continuants_aux g (n + bit0 1) = continuants_aux g (n + 1) := sorry theorem continuants_aux_stable_of_terminated {K : Type u_1} {g : generalized_continued_fraction K} {n : ℕ} {m : ℕ} [division_ring K] (succ_n_le_m : n + 1 ≤ m) (terminated_at_n : terminated_at g n) : continuants_aux g m = continuants_aux g (n + 1) := sorry theorem convergents'_aux_stable_step_of_terminated {K : Type u_1} {n : ℕ} [division_ring K] {s : seq (pair K)} (terminated_at_n : seq.terminated_at s n) : convergents'_aux s (n + 1) = convergents'_aux s n := sorry theorem convergents'_aux_stable_of_terminated {K : Type u_1} {n : ℕ} {m : ℕ} [division_ring K] {s : seq (pair K)} (n_le_m : n ≤ m) (terminated_at_n : seq.terminated_at s n) : convergents'_aux s m = convergents'_aux s n := sorry theorem continuants_stable_of_terminated {K : Type u_1} {g : generalized_continued_fraction K} {n : ℕ} {m : ℕ} [division_ring K] (n_le_m : n ≤ m) (terminated_at_n : terminated_at g n) : continuants g m = continuants g n := sorry theorem numerators_stable_of_terminated {K : Type u_1} {g : generalized_continued_fraction K} {n : ℕ} {m : ℕ} [division_ring K] (n_le_m : n ≤ m) (terminated_at_n : terminated_at g n) : numerators g m = numerators g n := sorry theorem denominators_stable_of_terminated {K : Type u_1} {g : generalized_continued_fraction K} {n : ℕ} {m : ℕ} [division_ring K] (n_le_m : n ≤ m) (terminated_at_n : terminated_at g n) : denominators g m = denominators g n := sorry theorem convergents_stable_of_terminated {K : Type u_1} {g : generalized_continued_fraction K} {n : ℕ} {m : ℕ} [division_ring K] (n_le_m : n ≤ m) (terminated_at_n : terminated_at g n) : convergents g m = convergents g n := sorry theorem convergents'_stable_of_terminated {K : Type u_1} {g : generalized_continued_fraction K} {n : ℕ} {m : ℕ} [division_ring K] (n_le_m : n ≤ m) (terminated_at_n : terminated_at g n) : convergents' g m = convergents' g n := sorry end Mathlib
7a0ba47d6d9c9a8442627642fbea64c7899aaa69
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/list/dedup.lean
4b42facffb4c63b2ed44ed3d8937fbe20aa4ba7c
[ "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
5,699
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.nodup /-! # Erasure of duplicates in a list > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file proves basic results about `list.dedup` (definition in `data.list.defs`). `dedup l` returns `l` without its duplicates. It keeps the earliest (that is, rightmost) occurrence of each. ## Tags duplicate, multiplicity, nodup, `nub` -/ universes u namespace list variables {α : Type u} [decidable_eq α] @[simp] theorem dedup_nil : dedup [] = ([] : list α) := rfl theorem dedup_cons_of_mem' {a : α} {l : list α} (h : a ∈ dedup l) : dedup (a :: l) = dedup l := pw_filter_cons_of_neg $ by simpa only [forall_mem_ne] using h theorem dedup_cons_of_not_mem' {a : α} {l : list α} (h : a ∉ dedup l) : dedup (a :: l) = a :: dedup l := pw_filter_cons_of_pos $ by simpa only [forall_mem_ne] using h @[simp] theorem mem_dedup {a : α} {l : list α} : a ∈ dedup l ↔ a ∈ l := by simpa only [dedup, forall_mem_ne, not_not] using not_congr (@forall_mem_pw_filter α (≠) _ (λ x y z xz, not_and_distrib.1 $ mt (λ h, eq.trans h.1 h.2) xz) a l) @[simp] theorem dedup_cons_of_mem {a : α} {l : list α} (h : a ∈ l) : dedup (a :: l) = dedup l := dedup_cons_of_mem' $ mem_dedup.2 h @[simp] theorem dedup_cons_of_not_mem {a : α} {l : list α} (h : a ∉ l) : dedup (a :: l) = a :: dedup l := dedup_cons_of_not_mem' $ mt mem_dedup.1 h theorem dedup_sublist : ∀ (l : list α), dedup l <+ l := pw_filter_sublist theorem dedup_subset : ∀ (l : list α), dedup l ⊆ l := pw_filter_subset theorem subset_dedup (l : list α) : l ⊆ dedup l := λ a, mem_dedup.2 theorem nodup_dedup : ∀ l : list α, nodup (dedup l) := pairwise_pw_filter theorem head_dedup [inhabited α] (l : list α) : l.dedup.head = if l.head ∈ l.tail then l.tail.dedup.head else l.head := match l with | [] := rfl | (a :: l) := by { by_cases ha : a ∈ l; simp [ha, list.dedup_cons_of_mem] } end theorem tail_dedup [inhabited α] (l : list α) : l.dedup.tail = if l.head ∈ l.tail then l.tail.dedup.tail else l.tail.dedup := match l with | [] := rfl | (a :: l) := by { by_cases ha : a ∈ l; simp [ha, list.dedup_cons_of_mem] } end theorem dedup_eq_self {l : list α} : dedup l = l ↔ nodup l := pw_filter_eq_self theorem dedup_eq_cons (l : list α) (a : α) (l' : list α) : l.dedup = a :: l' ↔ a ∈ l ∧ a ∉ l' ∧ l.dedup.tail = l' := begin refine ⟨λ h, _, λ h, _⟩, { refine ⟨mem_dedup.1 (h.symm ▸ mem_cons_self _ _), λ ha, _, by rw [h, tail_cons]⟩, have : count a l.dedup ≤ 1 := nodup_iff_count_le_one.1 (nodup_dedup l) a, rw [h, count_cons_self, add_le_iff_nonpos_left] at this, exact (not_le_of_lt (count_pos.2 ha) this) }, { have := @cons_head_tail α ⟨a⟩ _ (ne_nil_of_mem (mem_dedup.2 h.1)), have hal : a ∈ l.dedup := mem_dedup.2 h.1, rw [← this, mem_cons_iff, or_iff_not_imp_right] at hal, exact this ▸ h.2.2.symm ▸ (cons_eq_cons.2 ⟨(hal (h.2.2.symm ▸ h.2.1)).symm, rfl⟩) } end @[simp] theorem dedup_eq_nil (l : list α) : l.dedup = [] ↔ l = [] := begin induction l with a l hl, { exact iff.rfl }, { by_cases h : a ∈ l, { simp only [list.dedup_cons_of_mem h, hl, list.ne_nil_of_mem h] }, { simp only [list.dedup_cons_of_not_mem h, list.cons_ne_nil] } } end protected lemma nodup.dedup {l : list α} (h : l.nodup) : l.dedup = l := list.dedup_eq_self.2 h @[simp] theorem dedup_idempotent {l : list α} : dedup (dedup l) = dedup l := pw_filter_idempotent theorem dedup_append (l₁ l₂ : list α) : dedup (l₁ ++ l₂) = l₁ ∪ dedup l₂ := begin induction l₁ with a l₁ IH, {refl}, rw [cons_union, ← IH], show dedup (a :: (l₁ ++ l₂)) = insert a (dedup (l₁ ++ l₂)), by_cases a ∈ dedup (l₁ ++ l₂); [ rw [dedup_cons_of_mem' h, insert_of_mem h], rw [dedup_cons_of_not_mem' h, insert_of_not_mem h]] end lemma replicate_dedup {x : α} : ∀ {k}, k ≠ 0 → (replicate k x).dedup = [x] | 0 h := (h rfl).elim | 1 _ := rfl | (n+2) _ := by rw [replicate_succ, dedup_cons_of_mem (mem_replicate.2 ⟨n.succ_ne_zero, rfl⟩), replicate_dedup n.succ_ne_zero] lemma count_dedup (l : list α) (a : α) : l.dedup.count a = if a ∈ l then 1 else 0 := by simp_rw [count_eq_of_nodup $ nodup_dedup l, mem_dedup] /-- Summing the count of `x` over a list filtered by some `p` is just `countp` applied to `p` -/ lemma sum_map_count_dedup_filter_eq_countp (p : α → Prop) [decidable_pred p] (l : list α) : ((l.dedup.filter p).map $ λ x, l.count x).sum = l.countp p := begin induction l with a as h, { simp }, { simp_rw [list.countp_cons, list.count_cons', list.sum_map_add], congr' 1, { refine trans _ h, by_cases ha : a ∈ as, { simp [dedup_cons_of_mem ha] }, { simp only [dedup_cons_of_not_mem ha, list.filter], split_ifs with hp; simp [list.map_cons, list.sum_cons, list.count_eq_zero.2 ha, zero_add] } }, { by_cases hp : p a, { refine trans (sum_map_eq_nsmul_single a _ (λ _ h _, by simp [h])) _, simp [hp, count_dedup] }, { refine trans (list.sum_eq_zero $ λ n hn, _) (by simp [hp]), obtain ⟨a', ha'⟩ := list.mem_map.1 hn, simp only [(λ h, hp (h ▸ (list.mem_filter.1 ha'.1).2) : a' ≠ a), if_false] at ha', exact ha'.2.symm } } }, end lemma sum_map_count_dedup_eq_length (l : list α) : (l.dedup.map $ λ x, l.count x).sum = l.length := by simpa using sum_map_count_dedup_filter_eq_countp (λ _, true) l end list
ec01db03692a9ebfaeaeee67a0ea9c5a3874bafc
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/sheaves/sheafify.lean
3c707fbc5aa5ef650814d889dfdd4efb9dbe551a
[ "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
4,414
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 topology.sheaves.local_predicate import topology.sheaves.stalks /-! # Sheafification of `Type` valued presheaves We construct the sheafification of a `Type` valued presheaf, as the subsheaf of dependent functions into the stalks consisting of functions which are locally germs. We show that the stalks of the sheafification are isomorphic to the original stalks, via `stalk_to_fiber` which evaluates a germ of a dependent function at a point. We construct a morphism `to_sheafify` from a presheaf to (the underlying presheaf of) its sheafification, given by sending a section to its collection of germs. ## Future work Show that the map induced on stalks by `to_sheafify` is the inverse of `stalk_to_fiber`. Show sheafification is a functor from presheaves to sheaves, and that it is the left adjoint of the forgetful functor, following <https://stacks.math.columbia.edu/tag/007X>. -/ universes v noncomputable theory open Top open opposite open topological_space variables {X : Top.{v}} (F : presheaf (Type v) X) namespace Top.presheaf namespace sheafify /-- The prelocal predicate on functions into the stalks, asserting that the function is equal to a germ. -/ def is_germ : prelocal_predicate (λ x, F.stalk x) := { pred := λ U f, ∃ (g : F.obj (op U)), ∀ x : U, f x = F.germ x g, res := λ V U i f ⟨g, p⟩, ⟨F.map i.op g, λ x, (p (i x)).trans (F.germ_res_apply _ _ _).symm⟩, } /-- The local predicate on functions into the stalks, asserting that the function is locally equal to a germ. -/ def is_locally_germ : local_predicate (λ x, F.stalk x) := (is_germ F).sheafify end sheafify /-- The sheafification of a `Type` valued presheaf, defined as the functions into the stalks which are locally equal to germs. -/ def sheafify : sheaf (Type v) X := subsheaf_to_Types (sheafify.is_locally_germ F) /-- The morphism from a presheaf to its sheafification, sending each section to its germs. (This forms the unit of the adjunction.) -/ def to_sheafify : F ⟶ F.sheafify.1 := { app := λ U f, ⟨λ x, F.germ x f, prelocal_predicate.sheafify_of ⟨f, λ x, rfl⟩⟩, naturality' := λ U U' f, by { ext x ⟨u, m⟩, exact germ_res_apply F f.unop ⟨u, m⟩ x } } /-- The natural morphism from the stalk of the sheafification to the original stalk. In `sheafify_stalk_iso` we show this is an isomorphism. -/ def stalk_to_fiber (x : X) : F.sheafify.presheaf.stalk x ⟶ F.stalk x := stalk_to_fiber (sheafify.is_locally_germ F) x lemma stalk_to_fiber_surjective (x : X) : function.surjective (F.stalk_to_fiber x) := begin apply stalk_to_fiber_surjective, intro t, obtain ⟨U, m, s, rfl⟩ := F.germ_exist _ t, { use ⟨U, m⟩, fsplit, { exact λ y, F.germ y s, }, { exact ⟨prelocal_predicate.sheafify_of ⟨s, (λ _, rfl)⟩, rfl⟩, }, }, end lemma stalk_to_fiber_injective (x : X) : function.injective (F.stalk_to_fiber x) := begin apply stalk_to_fiber_injective, intros, rcases hU ⟨x, U.2⟩ with ⟨U', mU, iU, gU, wU⟩, rcases hV ⟨x, V.2⟩ with ⟨V', mV, iV, gV, wV⟩, have wUx := wU ⟨x, mU⟩, dsimp at wUx, erw wUx at e, clear wUx, have wVx := wV ⟨x, mV⟩, dsimp at wVx, erw wVx at e, clear wVx, rcases F.germ_eq x mU mV gU gV e with ⟨W, mW, iU', iV', e'⟩, dsimp at e', use ⟨W ⊓ (U' ⊓ V'), ⟨mW, mU, mV⟩⟩, refine ⟨_, _, _⟩, { change W ⊓ (U' ⊓ V') ⟶ U.obj, exact (opens.inf_le_right _ _) ≫ (opens.inf_le_left _ _) ≫ iU, }, { change W ⊓ (U' ⊓ V') ⟶ V.obj, exact (opens.inf_le_right _ _) ≫ (opens.inf_le_right _ _) ≫ iV, }, { intro w, dsimp, specialize wU ⟨w.1, w.2.2.1⟩, dsimp at wU, specialize wV ⟨w.1, w.2.2.2⟩, dsimp at wV, erw [wU, ←F.germ_res iU' ⟨w, w.2.1⟩, wV, ←F.germ_res iV' ⟨w, w.2.1⟩, category_theory.types_comp_apply, category_theory.types_comp_apply, e'] }, end /-- The isomorphism betweeen a stalk of the sheafification and the original stalk. -/ def sheafify_stalk_iso (x : X) : F.sheafify.presheaf.stalk x ≅ F.stalk x := (equiv.of_bijective _ ⟨stalk_to_fiber_injective _ _, stalk_to_fiber_surjective _ _⟩).to_iso -- PROJECT functoriality, and that sheafification is the left adjoint of the forgetful functor. end Top.presheaf
4d87dd2e2d36ac0a8e2c228aae989c59e0f58461
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/hide_cmd1.lean
2a9f912d9df36ee85f3d9291ab4d37da1fd636dc
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
1,034
lean
section tst1 open nat #check succ hide succ #check succ -- Error end tst1 section tst2 open nat hide succ def succ := tt #check succ -- Should not fail, it is not ambiguous since the alias nat.succ has been hidden end tst2 section tst3 open nat section nested hide zero #check zero -- Error end nested #check zero -- Should work, the scope of the previous `hide` is the section nested end tst3 namespace tst4 open nat namespace nested hide zero #check zero -- Error end nested #check zero -- Should work, the scope of the previous `hide` is the namespace nested end tst4 section tst5 hide nat.succ -- Error, we can only hide aliases for now end tst5 section tst6 #check @is_true -- is_true is an alias for decidable.is_true hide is_true #check @is_true -- Error end tst6 #check @is_true def is_true := tt #check is_true -- Error, is_true is now ambiguous hide is_true #check is_true -- should work, is_true : bool section tst7 open list #check map #check foldl hide map foldl #check map -- Error #check foldl -- Error end tst7
3c4809f414408320b3fe3754385ef274019b706c
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/test/induction.lean
947cc28fdfd57ec079e620be03a78f185f428803
[ "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
38,076
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, auto.not_forall_eq, auto.not_not_eq] 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
2dc83e13eefb429e643d797560d1d427470e584e
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/data/list/sorted.lean
bc07eaa1d006e1e245d48ae095714c9189671ac9
[ "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
5,753
lean
/- Copyright (c) 2015 Leonardo de Moura. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import data.list.comb data.list.perm namespace list variable {A : Type} variable (R : A → A → Prop) inductive locally_sorted : list A → Prop := | base0 : locally_sorted [] | base : ∀ a, locally_sorted [a] | step : ∀ {a b l}, R a b → locally_sorted (b::l) → locally_sorted (a::b::l) inductive hd_rel (a : A) : list A → Prop := | base : hd_rel a [] | step : ∀ {b} (l), R a b → hd_rel a (b::l) inductive sorted : list A → Prop := | base : sorted [] | step : ∀ {a : A} {l : list A}, hd_rel R a l → sorted l → sorted (a::l) variable {R} lemma hd_rel_inv : ∀ {a b l}, hd_rel R a (b::l) → R a b := begin intros a b l h, cases h, assumption end lemma sorted_inv : ∀ {a l}, sorted R (a::l) → hd_rel R a l ∧ sorted R l := begin intros a l h, cases h, split, repeat assumption end lemma sorted.rect_on {P : list A → Type} : ∀ {l}, sorted R l → P [] → (∀ a l, sorted R l → P l → hd_rel R a l → P (a::l)) → P l | [] s h₁ h₂ := h₁ | (a::l) s h₁ h₂ := have hd_rel R a l, from and.left (sorted_inv s), have sorted R l, from and.right (sorted_inv s), have P l, from sorted.rect_on this h₁ h₂, h₂ a l `sorted R l` `P l` `hd_rel R a l` lemma sorted_singleton (a : A) : sorted R [a] := sorted.step !hd_rel.base !sorted.base lemma sorted_of_locally_sorted : ∀ {l}, locally_sorted R l → sorted R l | [] h := !sorted.base | [a] h := !sorted_singleton | (a::b::l) (locally_sorted.step h₁ h₂) := have sorted R (b::l), from sorted_of_locally_sorted h₂, sorted.step (hd_rel.step _ h₁) this lemma locally_sorted_of_sorted : ∀ {l}, sorted R l → locally_sorted R l | [] h := !locally_sorted.base0 | [a] h := !locally_sorted.base | (a::b::l) (sorted.step (hd_rel.step _ h₁) h₂) := have locally_sorted R (b::l), from locally_sorted_of_sorted h₂, locally_sorted.step h₁ this lemma locally_sorted_eq_sorted : @locally_sorted = @sorted := funext (λ A, funext (λ R, funext (λ l, propext (iff.intro sorted_of_locally_sorted locally_sorted_of_sorted)))) variable (R) inductive strongly_sorted : list A → Prop := | base : strongly_sorted [] | step : ∀ {a l}, all l (R a) → strongly_sorted l → strongly_sorted (a::l) variable {R} lemma sorted_of_strongly_sorted : ∀ {l}, strongly_sorted R l → sorted R l | [] h := !sorted.base | [a] h := !sorted_singleton | (a::b::l) (strongly_sorted.step h₁ h₂) := have hd_rel R a (b::l), from hd_rel.step _ (of_all_cons h₁), have sorted R (b::l), from sorted_of_strongly_sorted h₂, sorted.step `hd_rel R a (b::l)` `sorted R (b::l)` lemma sorted_extends (trans : transitive R) : ∀ {a l}, sorted R (a::l) → all l (R a) | a [] h := !all_nil | a (b::l) h := have hd_rel R a (b::l), from and.left (sorted_inv h), have R a b, from hd_rel_inv this, have all l (R b), from sorted_extends (and.right (sorted_inv h)), all_of_forall (take x, suppose x ∈ b::l, or.elim (eq_or_mem_of_mem_cons this) (suppose x = b, by subst x; assumption) (suppose x ∈ l, have R b x, from of_mem_of_all this `all l (R b)`, trans `R a b` `R b x`)) theorem strongly_sorted_of_sorted_of_transitive (trans : transitive R) : ∀ {l}, sorted R l → strongly_sorted R l | [] h := !strongly_sorted.base | (a::l) h := have sorted R l, from and.right (sorted_inv h), have strongly_sorted R l, from strongly_sorted_of_sorted_of_transitive this, have all l (R a), from sorted_extends trans h, strongly_sorted.step `all l (R a)` `strongly_sorted R l` open perm lemma eq_of_sorted_of_perm (tr : transitive R) (anti : anti_symmetric R) : ∀ {l₁ l₂ : list A}, l₁ ~ l₂ → sorted R l₁ → sorted R l₂ → l₁ = l₂ | [] [] h₁ h₂ h₃ := rfl | (a₁::l₁) [] h₁ h₂ h₃ := absurd (perm.symm h₁) !not_perm_nil_cons | [] (a₂::l₂) h₁ h₂ h₃ := absurd h₁ !not_perm_nil_cons | (a::l₁) l₂ h₁ h₂ h₃ := have aux : ∀ {t}, l₂ = a::t → a::l₁ = l₂, from take t, suppose l₂ = a::t, have l₁ ~ t, by rewrite [this at h₁]; apply perm_cons_inv h₁, have sorted R l₁, from and.right (sorted_inv h₂), have sorted R t, by rewrite [`l₂ = a::t` at h₃]; exact and.right (sorted_inv h₃), have l₁ = t, from eq_of_sorted_of_perm `l₁ ~ t` `sorted R l₁` `sorted R t`, show a :: l₁ = l₂, by rewrite [`l₂ = a::t`, this], have a ∈ l₂, from mem_perm h₁ !mem_cons, obtain s t (e₁ : l₂ = s ++ (a::t)), from mem_split this, begin cases s with b s, { have l₂ = a::t, by exact e₁, exact aux this }, { have e₁ : l₂ = b::(s++(a::t)), by exact e₁, have b ∈ l₂, by rewrite e₁; apply mem_cons, have hall₂ : all (s++(a::t)) (R b), begin rewrite [e₁ at h₃], apply sorted_extends tr h₃ end, have a ∈ s++(a::t), from mem_append_right _ !mem_cons, have R b a, from of_mem_of_all this hall₂, have b ∈ a::l₁, from mem_perm (perm.symm h₁) `b ∈ l₂`, have hall₁ : all l₁ (R a), from sorted_extends tr h₂, apply or.elim (eq_or_mem_of_mem_cons `b ∈ a::l₁`), suppose b = a, by rewrite this at e₁; exact aux e₁, suppose b ∈ l₁, have R a b, from of_mem_of_all this hall₁, have b = a, from anti `R b a` `R a b`, by rewrite this at e₁; exact aux e₁ } end end list
dd93d47136a6c447f472e24267a2db748a8a303a
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/category_theory/conj.lean
a3df2bd9d3853daa39e98cbefff4fabe5eb06225
[ "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
5,465
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import category_theory.endomorphism import algebra.group_power /-! # Conjugate morphisms by isomorphisms An isomorphism `α : X ≅ Y` defines - a monoid isomorphism `conj : End X ≃* End Y` by `α.conj f = α.inv ≫ f ≫ α.hom`; - a group isomorphism `conj_Aut : Aut X ≃* Aut Y` by `α.conj_Aut f = α.symm ≪≫ f ≪≫ α`. For completeness, we also define `hom_congr : (X ≅ X₁) → (Y ≅ Y₁) → (X ⟶ Y) ≃ (X₁ ⟶ Y₁)`, cf. `equiv.arrow_congr`. -/ universes v u namespace category_theory namespace iso variables {C : Type u} [category.{v} C] /-- If `X` is isomorphic to `X₁` and `Y` is isomorphic to `Y₁`, then there is a natural bijection between `X ⟶ Y` and `X₁ ⟶ Y₁`. See also `equiv.arrow_congr`. -/ def hom_congr {X Y X₁ Y₁ : C} (α : X ≅ X₁) (β : Y ≅ Y₁) : (X ⟶ Y) ≃ (X₁ ⟶ Y₁) := { to_fun := λ f, α.inv ≫ f ≫ β.hom, inv_fun := λ f, α.hom ≫ f ≫ β.inv, left_inv := λ f, show α.hom ≫ (α.inv ≫ f ≫ β.hom) ≫ β.inv = f, by rw [category.assoc, category.assoc, β.hom_inv_id, α.hom_inv_id_assoc, category.comp_id], right_inv := λ f, show α.inv ≫ (α.hom ≫ f ≫ β.inv) ≫ β.hom = f, by rw [category.assoc, category.assoc, β.inv_hom_id, α.inv_hom_id_assoc, category.comp_id] } @[simp] lemma hom_congr_apply {X Y X₁ Y₁ : C} (α : X ≅ X₁) (β : Y ≅ Y₁) (f : X ⟶ Y) : α.hom_congr β f = α.inv ≫ f ≫ β.hom := rfl lemma hom_congr_comp {X Y Z X₁ Y₁ Z₁ : C} (α : X ≅ X₁) (β : Y ≅ Y₁) (γ : Z ≅ Z₁) (f : X ⟶ Y) (g : Y ⟶ Z) : α.hom_congr γ (f ≫ g) = α.hom_congr β f ≫ β.hom_congr γ g := by simp @[simp] lemma hom_congr_refl {X Y : C} (f : X ⟶ Y) : (iso.refl X).hom_congr (iso.refl Y) f = f := by simp @[simp] lemma hom_congr_trans {X₁ Y₁ X₂ Y₂ X₃ Y₃ : C} (α₁ : X₁ ≅ X₂) (β₁ : Y₁ ≅ Y₂) (α₂ : X₂ ≅ X₃) (β₂ : Y₂ ≅ Y₃) (f : X₁ ⟶ Y₁) : (α₁ ≪≫ α₂).hom_congr (β₁ ≪≫ β₂) f = (α₁.hom_congr β₁).trans (α₂.hom_congr β₂) f := by simp @[simp] lemma hom_congr_symm {X₁ Y₁ X₂ Y₂ : C} (α : X₁ ≅ X₂) (β : Y₁ ≅ Y₂) : (α.hom_congr β).symm = α.symm.hom_congr β.symm := rfl variables {X Y : C} (α : X ≅ Y) /-- An isomorphism between two objects defines a monoid isomorphism between their monoid of endomorphisms. -/ def conj : End X ≃* End Y := { map_mul' := λ f g, hom_congr_comp α α α g f, .. hom_congr α α } lemma conj_apply (f : End X) : α.conj f = α.inv ≫ f ≫ α.hom := rfl @[simp] lemma conj_comp (f g : End X) : α.conj (f ≫ g) = (α.conj f) ≫ (α.conj g) := α.conj.map_mul g f @[simp] lemma conj_id : α.conj (𝟙 X) = 𝟙 Y := α.conj.map_one @[simp] lemma refl_conj (f : End X) : (iso.refl X).conj f = f := by rw [conj_apply, iso.refl_inv, iso.refl_hom, category.id_comp, category.comp_id] @[simp] lemma trans_conj {Z : C} (β : Y ≅ Z) (f : End X) : (α ≪≫ β).conj f = β.conj (α.conj f) := hom_congr_trans α α β β f @[simp] lemma symm_self_conj (f : End X) : α.symm.conj (α.conj f) = f := by rw [← trans_conj, α.self_symm_id, refl_conj] @[simp] lemma self_symm_conj (f : End Y) : α.conj (α.symm.conj f) = f := α.symm.symm_self_conj f @[simp] lemma conj_pow (f : End X) (n : ℕ) : α.conj (f^n) = (α.conj f)^n := α.conj.to_monoid_hom.map_pow f n /-- `conj` defines a group isomorphisms between groups of automorphisms -/ def conj_Aut : Aut X ≃* Aut Y := (Aut.units_End_equiv_Aut X).symm.trans $ (units.map_equiv α.conj).trans $ Aut.units_End_equiv_Aut Y lemma conj_Aut_apply (f : Aut X) : α.conj_Aut f = α.symm ≪≫ f ≪≫ α := by cases f; cases α; ext; refl @[simp] lemma conj_Aut_hom (f : Aut X) : (α.conj_Aut f).hom = α.conj f.hom := rfl @[simp] lemma trans_conj_Aut {Z : C} (β : Y ≅ Z) (f : Aut X) : (α ≪≫ β).conj_Aut f = β.conj_Aut (α.conj_Aut f) := by simp only [conj_Aut_apply, iso.trans_symm, iso.trans_assoc] @[simp] lemma conj_Aut_mul (f g : Aut X) : α.conj_Aut (f * g) = α.conj_Aut f * α.conj_Aut g := α.conj_Aut.map_mul f g @[simp] lemma conj_Aut_trans (f g : Aut X) : α.conj_Aut (f ≪≫ g) = α.conj_Aut f ≪≫ α.conj_Aut g := conj_Aut_mul α g f @[simp] lemma conj_Aut_pow (f : Aut X) (n : ℕ) : α.conj_Aut (f^n) = (α.conj_Aut f)^n := α.conj_Aut.to_monoid_hom.map_pow f n @[simp] lemma conj_Aut_zpow (f : Aut X) (n : ℤ) : α.conj_Aut (f^n) = (α.conj_Aut f)^n := α.conj_Aut.to_monoid_hom.map_zpow f n end iso namespace functor universes v₁ u₁ variables {C : Type u} [category.{v} C] {D : Type u₁} [category.{v₁} D] (F : C ⥤ D) lemma map_hom_congr {X Y X₁ Y₁ : C} (α : X ≅ X₁) (β : Y ≅ Y₁) (f : X ⟶ Y) : F.map (iso.hom_congr α β f) = iso.hom_congr (F.map_iso α) (F.map_iso β) (F.map f) := by simp lemma map_conj {X Y : C} (α : X ≅ Y) (f : End X) : F.map (α.conj f) = (F.map_iso α).conj (F.map f) := map_hom_congr F α α f lemma map_conj_Aut (F : C ⥤ D) {X Y : C} (α : X ≅ Y) (f : Aut X) : F.map_iso (α.conj_Aut f) = (F.map_iso α).conj_Aut (F.map_iso f) := by ext; simp only [map_iso_hom, iso.conj_Aut_hom, F.map_conj] -- alternative proof: by simp only [iso.conj_Aut_apply, F.map_iso_trans, F.map_iso_symm] end functor end category_theory
6ce736ea4648072f2c167ca8bdae7fa9360f4f9c
f0050a24971f1b7315211eec6e02a8eee618ce6b
/src/hoare/basic.lean
82ac8d88d1335b7b52a6904b42b61b66d7566adc
[ "Apache-2.0" ]
permissive
ttowncompiled/excaLibur
9574f2acb0bafcd59f59423b63a62c15442ea5f7
7d8371bf998012d4f8c49d3fe2bf540e2517c688
refs/heads/master
1,670,710,035,703
1,599,670,584,000
1,599,670,584,000
292,720,923
0
0
null
null
null
null
UTF-8
Lean
false
false
14,594
lean
/- Copyright (c) Ian Riley. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ian Riley -/ import common big_step.basic def partial_hoare (P : scope → Prop) (S : stmt) (Q : scope → Prop) : Prop := ∀ (s t : scope), P s → (S, s) ⟹ t → Q t notation `{* ` P : 1 ` *} ` S : 1 ` {* ` Q : 1 ` *}` := partial_hoare P S Q /- Instructions for how to use partial_hoare and its notation A partial Hoare triple is used to represent the pre- and post-condition of an instruction or composition of instructions. A partial Hoare can be constructed with the following notation. {* P *} S {* Q *} P is the pre-condition and Q is the post condition of statement S (from common.stmt). It states that when stmt S is executed with pre-condition P, it will either (a) terminate with post-condition Q or (b) not terminate. These Hoare triples are referred to as partial Hoare triples precisely because a stmt S executed with pre-condition P may not terminate. This partial Hoare triple is implemented using big_step semantics, so it is used to construct proofs of properties of a program which executes synchronously/deterministically. Given any big_step (S, s) ⟹ t, there is a corresponding partial Hoare triple {* P s *} S {* Q t *} Note that P and Q are both defined with the type scope → Prop. Thus, the pre-condition P is defined over the scope s in which stmt S is executed while the post-condition Q is defined over the scope t which results from the execution of stmt S in scope s. This construction of a partial Hoare triple from big_step semantics allows us to more easily reason over the properties of statement S. See documentation in big_step.basic for more information on big_step semantics. See documentation in common.basic for more information on specifying program properties. -/ namespace partial_hoare /- Sequent: Skip ___________ {P} skip {P} -/ lemma skip_intro {P : scope → Prop} : {* P *} stmt.skip {* P *} := begin intros s t hP hst, cases hst, exact hP, end /- Sequent: Assign __________________ , {P[a/x]} x := a {P} where P[a/x] means "the scope P where the proposition of a is substituted into the predicate x." If there is no predicate x, then one is created. -/ lemma assign_intro (P : scope → Prop) {x : string} {a : scope → Prop} : {* λ (s : scope), P (s{x ↦ a s}) *} stmt.assign x a {* P *} := begin intros s t hP hst, cases hst, exact hP, end /- Sequent: {P} S {Q} {Q} T {R} Comp _____________________ {P} S ;; T {R} -/ lemma comp_intro {P Q R : scope → Prop} {S T : stmt} (hS : {* P *} S {* Q *}) (hT : {* Q *} T {* R *}) : {* P *} S ;; T {* R *} := begin intros s t hP hst, cases hst, apply hT hst_t, { apply hS s, { exact hP, }, { exact hst_hS, } }, { exact hst_hT, } end /- Sequent: {P ∧ b} S {Q} {P ∧ ¬ b} T {Q} If-Then-Else _______________________________ {P} if b then S else T {Q} -/ lemma ite_intro {b P Q : scope → Prop} {S T : stmt} (hS : {* λ (s : scope), P s ∧ b s *} S {* Q *}) (hT : {* λ (s : scope), P s ∧ ¬ b s *} T {* Q *}) : {* P *} stmt.ite b S T {* Q *} := begin intros s t hP hst, cases hst, { apply hS, { exact and.intro hP hst_hcond, }, { exact hst_hbody, } }, { apply hT, { exact and.intro hP hst_hcond, }, { exact hst_hbody, } } end /- Sequent: {I ∧ b} S {I} While __________________________ {I} while b do S {I ∧ ¬ b} -/ lemma while_intro (I : scope → Prop) {b : scope → Prop} {S : stmt} (hS : {* λ (s : scope), I s ∧ b s *} S {* I *}) : {* I *} stmt.while b S {* λ (s : scope), I s ∧ ¬ b s *} := begin intros s t hP, generalize hws : (stmt.while b S, s) = ws, intro hst, induction hst generalizing s; cases hws, { apply hst_ih_hrest hst_t, { apply hS hst_s, { exact and.intro hP hst_hcond, }, { exact hst_hbody, } }, { refl, } }, { exact and.intro hP hst_hcond, } end /- Sequent: P → (v₀ s) ∧ (v₁ s) P → P[(σ s)] {P[(σ s)]} F {Q} Call ________________________________________________________ , {P} call f v₀ v₁ σ F {Q} where P[(σ s)] means "the scope P injected with the predicates of (σ s)." The predicates of (σ s) include input (read-only) parameters, in-out (read-write) paramters, local variables. These predicates (except for those predicates relating to v₁) must use a distinct naming convention, so that predicates of s are not overwritten. Return statements that return a local variable must be reformulated as an in-out parameter. -- σ \s -/ lemma call_intro {v₀ v₁ P Q : scope → Prop} {f : string} {F : stmt} {σ : scope → scope} (hargs : ∀ (s : scope), P s → (v₀ s ∧ v₁ s)) (hP : ∀ (s : scope), P s → P (σ s)) (hF : {* λ (s : scope), P (σ s) *} F {* Q *}) : {* P *} stmt.call f v₀ v₁ σ F {* Q *} := begin intros s t hP' hF', cases hF', apply hF (σ s), { apply hP (σ s), apply hP s, exact hP', }, { exact hF'_hF, } end /- Sequent: P' → P {P} S {Q} Q → Q' Consequence __________________________ {P'} S {Q'} -/ lemma consequence {P P' Q Q' : scope → Prop} {S : stmt} (hP : ∀ (s : scope), P' s → P s) (hS : {* P *} S {* Q *}) (hQ : ∀ (s : scope), Q s → Q' s) : {* P' *} S {* Q' *} := begin intros s t hP' hst, apply hQ t, apply hS, { apply hP s, exact hP', }, { exact hst, } end /- Sequent: P' → P {P} S {Q} Consequence-Left _________________ {P'} S {Q} -/ lemma consequence_left (P' : scope → Prop) {P Q : scope → Prop} {S : stmt} (hP : ∀ (s : scope), P' s → P s) (hS : {* P *} S {* Q *}) : {* P' *} S {* Q *} := consequence hP hS (by cc) /- Sequent: {P} S {Q} Q → Q' Consequence-Right __________________ {P} S {Q'} -/ lemma consequence_right (Q : scope → Prop) {P Q' : scope → Prop} {S : stmt} (hS : {* P *} S {* Q *}) (hQ : ∀ (s : scope), Q s → Q' s) : {* P *} S {* Q' *} := consequence (by cc) hS hQ /- Sequent: P → Q Skip' ____________ {P} skip {Q} -/ lemma skip_intro' {P Q : scope → Prop} (hP : ∀ (s : scope), P s → Q s) : {* P *} stmt.skip {* Q *} := consequence hP skip_intro (by cc) /- Sequent: P → Q[a/x] Assign' _____________ , {P} x := a {Q} where Q[a/x] means "the scope Q where the proposition of a is substituted into the predicate x." If there is no predicate x, then one is created. -/ lemma assign_intro' {P Q : scope → Prop} {x : string} {a : scope → Prop} (hP : ∀ (s : scope), P s → Q (s{x ↦ a s})) : {* P *} stmt.assign x a {* Q *} := consequence hP (assign_intro Q) (by cc) /- Sequent: P' → P {P} S {Q} {Q} T {R} R → R' Comp' ______________________________________ {P'} S ;; T {R'} -/ lemma comp_intro' {P P' Q R R' : scope → Prop} {S T : stmt} (hP : ∀ (s : scope), P' s → P s) (hS : {* P *} S {* Q *}) (hT : {* Q *} T {* R *}) (hR : ∀ (s : scope), R s → R' s) : {* P' *} S ;; T {* R' *} := begin apply consequence_left, { exact hP, }, { intros s t hP₂ hst, cases hst, apply hR, apply hT, { apply hS, { exact hP₂, }, { exact hst_hS, } }, { exact hst_hT, } } end /- Sequent: P' → P {P} S {Q} {Q} T {R} Comp-Left _____________________________ {P'} S ;; T {R} -/ lemma comp_intro_left (P' : scope → Prop) {P Q R : scope → Prop} {S T : stmt} (hP : ∀ (s : scope), P' s → P s) (hS : {* P *} S {* Q *}) (hT : {* Q *} T {* R *}) : {* P' *} S ;; T {* R *} := consequence_left P' hP (comp_intro hS hT) /- Sequent: {P} S {Q} {Q} T {R} R → R' Comp-Right ______________________________ {P} S ;; T {R'} -/ lemma comp_intro_right (R : scope → Prop) {P Q R' : scope → Prop} {S T : stmt} (hS : {* P *} S {* Q *}) (hT : {* Q *} T {* R *}) (hR : ∀ (s : scope), R s → R' s) : {* P *} S ;; T {* R' *} := consequence_right R (comp_intro hS hT) hR /- Sequent: P → b {P} S {Q} If-Then-Else-True __________________________ {P} if b then S else T {Q} -/ lemma ite_true_intro {b P Q : scope → Prop} {S T : stmt} (hP : ∀ (s : scope), P s → b s) (hS : {* P *} S {* Q *}) : {* P *} stmt.ite b S T {* Q *} := begin intros s t hP' hst, apply hS, { exact hP', }, { cases hst, { exact hst_hbody, }, { exfalso, apply hst_hcond, apply hP, exact hP', } } end /- Sequent: P → ¬ b {P} T {Q} If-Then-Else-False __________________________ {P} if b then S else T {Q} -/ lemma ite_false_intro {b P Q : scope → Prop} {S T : stmt} (hP : ∀ (s : scope), P s → ¬ b s) (hT : {* P *} T {* Q *}) : {* P *} stmt.ite b S T {* Q *} := begin intros s t hP' hst, apply hT, { exact hP', }, { cases hst, { exfalso, apply hP s hP', exact hst_hcond, }, { exact hst_hbody, } } end /- Sequent: P → I {I ∧ b} S {I} I ∧ ¬ b → Q While-Invariant ___________________________________ {P} while b do S {Q} -/ lemma while_invariant {b P Q : scope → Prop} {S : stmt} (I : scope → Prop) (hP : ∀ (s : scope), P s → I s) (hS : {* λ (s : scope), I s ∧ b s *} S {* I *}) (hQ : ∀ (s : scope), ¬ b s → I s → Q s) : {* P *} stmt.while b S {* Q *} := begin apply consequence, { exact hP, }, { apply while_intro I hS, }, { intros s h, apply hQ, { exact h.right, }, { exact h.left, } } end /- Sequent: {P ∧ b} S ;; while b do S {Q} P ∧ ¬ b → Q While-Right ___________________________________________ {P} while b do S {Q} -/ lemma while_right {b P Q : scope → Prop} {S : stmt} (hS : {* λ (s : scope), P s ∧ b s *} S ;; stmt.while b S {* Q *}) (hQ : ∀ (s : scope), ¬ b s → P s → Q s) : {* P *} stmt.while b S {* Q *} := begin intros s t hP hst, cases hst, { apply hS, { exact and.intro hP hst_hcond, }, { apply big_step.comp hst_hbody hst_hrest, } }, { apply hQ s hst_hcond hP, } end /- Sequent: P → b {P} S ;; while b do S {Q} While-Unwind-Right _________________________________ {P} while b do S {Q} -/ lemma while_unwind_right {b P Q : scope → Prop} {S : stmt} (hP : ∀ (s : scope), P s → b s) (hS : {* P *} S ;; stmt.while b S {* Q *}) : {* P *} stmt.while b S {* Q *} := begin intros s t hP' hst, cases hst, { apply hS, { exact hP', }, { apply big_step.comp hst_hbody hst_hrest, } }, { exfalso, apply hst_hcond, apply hP s hP', } end /- Sequent: P → ¬ b While-False ____________________ {P} while b do S {P} -/ lemma while_false_intro {b P : scope → Prop} {S : stmt} (hP : ∀ (s : scope), P s → ¬ b s) : {* P *} stmt.while b S {* P *} := begin intros s t hP' hst, cases hst, { exfalso, apply hP s hP', exact hst_hcond, }, { exact hP', } end /- Sequent: Q Assign-Left ___________________ , {Q[a/x]} x := a {Q} where Q[a/x] means "the scope Q where the proposition of a is substituted into the predicate x." If there is no predicate x, then one is created. -/ lemma assign_intro_left (Q : scope → Prop) {x : string} {a : scope → Prop} : {* λ (s : scope), ∃ (t₀ : Prop), Q (s{x ↦ t₀}) ∧ t₀ = a s *} stmt.assign x a {* Q *} := begin apply assign_intro', intros s hP, cases hP, rw ← hP_h.right, exact hP_h.left end /- Sequent: P Assign-Right ___________________ , {P} x := a {P[a/x]} where P[a/x] means "the scope P where the proposition of a is substituted into the predicate x." If there is no predicate x, then one is created. -/ lemma assign_intro_right (P : scope → Prop) {x : string} {a : scope → Prop} : {* P *} stmt.assign x a {* λ (s : scope), ∃ (t₀ : Prop), P (s{x ↦ t₀}) ∧ s x = a (s{x ↦ t₀}) *} := begin apply assign_intro', intros s hP, apply exists.intro (s x), apply and.intro, { rw (scope.update_squash x (s x) (a s) s), rw (scope.update_id x s), exact hP }, { rw (scope.update_apply x (a s) s), rw (scope.update_squash x (s x) (a s) s), rw (scope.update_id x s), } end end partial_hoare
921fb41a057222f485afdf07d64c3ec797fe438f
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/measure_theory/measure/content.lean
b477cc4e45904b5a1e843a1fddfb5c94bd6e7a8b
[ "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
17,010
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import measure_theory.measure.measure_space import measure_theory.measure.regular import topology.opens import topology.compacts /-! # Contents In this file we work with *contents*. A content `λ` is a function from a certain class of subsets (such as the compact subsets) to `ℝ≥0` that is * additive: If `K₁` and `K₂` are disjoint sets in the domain of `λ`, then `λ(K₁ ∪ K₂) = λ(K₁) + λ(K₂)`; * subadditive: If `K₁` and `K₂` are in the domain of `λ`, then `λ(K₁ ∪ K₂) ≤ λ(K₁) + λ(K₂)`; * monotone: If `K₁ ⊆ K₂` are in the domain of `λ`, then `λ(K₁) ≤ λ(K₂)`. We show that: * Given a content `λ` on compact sets, let us define a function `λ*` on open sets, by letting `λ* U` be the supremum of `λ K` for `K` included in `U`. This is a countably subadditive map that vanishes at `∅`. In Halmos (1950) this is called the *inner content* `λ*` of `λ`, and formalized as `inner_content`. * Given an inner content, we define an outer measure `μ*`, by letting `μ* E` be the infimum of `λ* U` over the open sets `U` containing `E`. This is indeed an outer measure. It is formalized as `outer_measure`. * Restricting this outer measure to Borel sets gives a regular measure `μ`. We define bundled contents as `content`. In this file we only work on contents on compact sets, and inner contents on open sets, and both contents and inner contents map into the extended nonnegative reals. However, in other applications other choices can be made, and it is not a priori clear what the best interface should be. ## Main definitions For `μ : content G`, we define * `μ.inner_content` : the inner content associated to `μ`. * `μ.outer_measure` : the outer measure associated to `μ`. * `μ.measure` : the Borel measure associated to `μ`. We prove that, on a locally compact space, the measure `μ.measure` is regular. ## References * Paul Halmos (1950), Measure Theory, §53 * <https://en.wikipedia.org/wiki/Content_(measure_theory)> -/ universes u v w noncomputable theory open set topological_space open_locale nnreal ennreal namespace measure_theory variables {G : Type w} [topological_space G] /-- A content is an additive function on compact sets taking values in `ℝ≥0`. It is a device from which one can define a measure. -/ structure content (G : Type w) [topological_space G] := (to_fun : compacts G → ℝ≥0) (mono' : ∀ (K₁ K₂ : compacts G), K₁.1 ⊆ K₂.1 → to_fun K₁ ≤ to_fun K₂) (sup_disjoint' : ∀ (K₁ K₂ : compacts G), disjoint K₁.1 K₂.1 → to_fun (K₁ ⊔ K₂) = to_fun K₁ + to_fun K₂) (sup_le' : ∀ (K₁ K₂ : compacts G), to_fun (K₁ ⊔ K₂) ≤ to_fun K₁ + to_fun K₂) instance : inhabited (content G) := ⟨{ to_fun := λ K, 0, mono' := by simp, sup_disjoint' := by simp, sup_le' := by simp }⟩ /-- Although the `to_fun` field of a content takes values in `ℝ≥0`, we register a coercion to functions taking values in `ℝ≥0∞` as most constructions below rely on taking suprs and infs, which is more convenient in a complete lattice, and aim at constructing a measure. -/ instance : has_coe_to_fun (content G) (λ _, compacts G → ℝ≥0∞) := ⟨λ μ s, μ.to_fun s⟩ namespace content variable (μ : content G) lemma apply_eq_coe_to_fun (K : compacts G) : μ K = μ.to_fun K := rfl lemma mono (K₁ K₂ : compacts G) (h : K₁.1 ⊆ K₂.1) : μ K₁ ≤ μ K₂ := by simp [apply_eq_coe_to_fun, μ.mono' _ _ h] lemma sup_disjoint (K₁ K₂ : compacts G) (h : disjoint K₁.1 K₂.1) : μ (K₁ ⊔ K₂) = μ K₁ + μ K₂ := by simp [apply_eq_coe_to_fun, μ.sup_disjoint' _ _ h] lemma sup_le (K₁ K₂ : compacts G) : μ (K₁ ⊔ K₂) ≤ μ K₁ + μ K₂ := by { simp only [apply_eq_coe_to_fun], norm_cast, exact μ.sup_le' _ _ } lemma lt_top (K : compacts G) : μ K < ∞ := ennreal.coe_lt_top lemma empty : μ ⊥ = 0 := begin have := μ.sup_disjoint' ⊥ ⊥, simpa [apply_eq_coe_to_fun] using this, end /-- Constructing the inner content of a content. From a content defined on the compact sets, we obtain a function defined on all open sets, by taking the supremum of the content of all compact subsets. -/ def inner_content (U : opens G) : ℝ≥0∞ := ⨆ (K : compacts G) (h : K.1 ⊆ U), μ K lemma le_inner_content (K : compacts G) (U : opens G) (h2 : K.1 ⊆ U) : μ K ≤ μ.inner_content U := le_supr_of_le K $ le_supr _ h2 lemma inner_content_le (U : opens G) (K : compacts G) (h2 : (U : set G) ⊆ K.1) : μ.inner_content U ≤ μ K := bsupr_le $ λ K' hK', μ.mono _ _ (subset.trans hK' h2) lemma inner_content_of_is_compact {K : set G} (h1K : is_compact K) (h2K : is_open K) : μ.inner_content ⟨K, h2K⟩ = μ ⟨K, h1K⟩ := le_antisymm (bsupr_le $ λ K' hK', μ.mono _ ⟨K, h1K⟩ hK') (μ.le_inner_content _ _ subset.rfl) lemma inner_content_empty : μ.inner_content ∅ = 0 := begin refine le_antisymm _ (zero_le _), rw ←μ.empty, refine bsupr_le (λ K hK, _), have : K = ⊥, { ext1, rw [subset_empty_iff.mp hK, compacts.bot_val] }, rw this, refl' end /-- This is "unbundled", because that it required for the API of `induced_outer_measure`. -/ lemma inner_content_mono ⦃U V : set G⦄ (hU : is_open U) (hV : is_open V) (h2 : U ⊆ V) : μ.inner_content ⟨U, hU⟩ ≤ μ.inner_content ⟨V, hV⟩ := supr_le_supr $ λ K, supr_le_supr_const $ λ hK, subset.trans hK h2 lemma inner_content_exists_compact {U : opens G} (hU : μ.inner_content U ≠ ∞) {ε : ℝ≥0} (hε : ε ≠ 0) : ∃ K : compacts G, K.1 ⊆ U ∧ μ.inner_content U ≤ μ K + ε := begin have h'ε := ennreal.coe_ne_zero.2 hε, cases le_or_lt (μ.inner_content U) ε, { exact ⟨⊥, empty_subset _, le_add_left h⟩ }, have := ennreal.sub_lt_self hU h.ne_bot h'ε, conv at this {to_rhs, rw inner_content }, simp only [lt_supr_iff] at this, rcases this with ⟨U, h1U, h2U⟩, refine ⟨U, h1U, _⟩, rw [← tsub_le_iff_right], exact le_of_lt h2U end /-- The inner content of a supremum of opens is at most the sum of the individual inner contents. -/ lemma inner_content_Sup_nat [t2_space G] (U : ℕ → opens G) : μ.inner_content (⨆ (i : ℕ), U i) ≤ ∑' (i : ℕ), μ.inner_content (U i) := begin have h3 : ∀ (t : finset ℕ) (K : ℕ → compacts G), μ (t.sup K) ≤ t.sum (λ i, μ (K i)), { intros t K, refine finset.induction_on t _ _, { simp only [μ.empty, nonpos_iff_eq_zero, finset.sum_empty, finset.sup_empty], }, { intros n s hn ih, rw [finset.sup_insert, finset.sum_insert hn], exact le_trans (μ.sup_le _ _) (add_le_add_left ih _) }}, refine bsupr_le (λ K hK, _), rcases is_compact.elim_finite_subcover K.2 _ (λ i, (U i).prop) _ with ⟨t, ht⟩, swap, { convert hK, rw [opens.supr_def, subtype.coe_mk] }, rcases K.2.finite_compact_cover t (coe ∘ U) (λ i _, (U _).prop) (by simp only [ht]) with ⟨K', h1K', h2K', h3K'⟩, let L : ℕ → compacts G := λ n, ⟨K' n, h1K' n⟩, convert le_trans (h3 t L) _, { ext1, simp only [h3K', compacts.finset_sup_val, finset.sup_eq_supr, set.supr_eq_Union] }, refine le_trans (finset.sum_le_sum _) (ennreal.sum_le_tsum t), intros i hi, refine le_trans _ (le_supr _ (L i)), refine le_trans _ (le_supr _ (h2K' i)), refl' end /-- The inner content of a union of sets is at most the sum of the individual inner contents. This is the "unbundled" version of `inner_content_Sup_nat`. It required for the API of `induced_outer_measure`. -/ lemma inner_content_Union_nat [t2_space G] ⦃U : ℕ → set G⦄ (hU : ∀ (i : ℕ), is_open (U i)) : μ.inner_content ⟨⋃ (i : ℕ), U i, is_open_Union hU⟩ ≤ ∑' (i : ℕ), μ.inner_content ⟨U i, hU i⟩ := by { have := μ.inner_content_Sup_nat (λ i, ⟨U i, hU i⟩), rwa [opens.supr_def] at this } lemma inner_content_comap (f : G ≃ₜ G) (h : ∀ ⦃K : compacts G⦄, μ (K.map f f.continuous) = μ K) (U : opens G) : μ.inner_content (U.comap f.continuous) = μ.inner_content U := begin refine supr_congr _ ((compacts.equiv f).surjective) _, intro K, refine supr_congr_Prop image_subset_iff _, intro hK, simp only [equiv.coe_fn_mk, subtype.mk_eq_mk, ennreal.coe_eq_coe, compacts.equiv], apply h, end @[to_additive] lemma is_mul_left_invariant_inner_content [group G] [topological_group G] (h : ∀ (g : G) {K : compacts G}, μ (K.map _ $ continuous_mul_left g) = μ K) (g : G) (U : opens G) : μ.inner_content (U.comap $ continuous_mul_left g) = μ.inner_content U := by convert μ.inner_content_comap (homeomorph.mul_left g) (λ K, h g) U @[to_additive] lemma inner_content_pos_of_is_mul_left_invariant [t2_space G] [group G] [topological_group G] (h3 : ∀ (g : G) {K : compacts G}, μ (K.map _ $ continuous_mul_left g) = μ K) (K : compacts G) (hK : μ K ≠ 0) (U : opens G) (hU : (U : set G).nonempty) : 0 < μ.inner_content U := begin have : (interior (U : set G)).nonempty, rwa [U.prop.interior_eq], rcases compact_covered_by_mul_left_translates K.2 this with ⟨s, hs⟩, suffices : μ K ≤ s.card * μ.inner_content U, { exact (ennreal.mul_pos_iff.mp $ hK.bot_lt.trans_le this).2 }, have : K.1 ⊆ ↑⨆ (g ∈ s), U.comap $ continuous_mul_left g, { simpa only [opens.supr_def, opens.coe_comap, subtype.coe_mk] }, refine (μ.le_inner_content _ _ this).trans _, refine (rel_supr_sum (μ.inner_content) (μ.inner_content_empty) (≤) (μ.inner_content_Sup_nat) _ _).trans _, simp only [μ.is_mul_left_invariant_inner_content h3, finset.sum_const, nsmul_eq_mul, le_refl] end lemma inner_content_mono' ⦃U V : set G⦄ (hU : is_open U) (hV : is_open V) (h2 : U ⊆ V) : μ.inner_content ⟨U, hU⟩ ≤ μ.inner_content ⟨V, hV⟩ := supr_le_supr $ λ K, supr_le_supr_const $ λ hK, subset.trans hK h2 /-- Extending a content on compact sets to an outer measure on all sets. -/ protected def outer_measure : outer_measure G := induced_outer_measure (λ U hU, μ.inner_content ⟨U, hU⟩) is_open_empty μ.inner_content_empty variables [t2_space G] lemma outer_measure_opens (U : opens G) : μ.outer_measure U = μ.inner_content U := induced_outer_measure_eq' (λ _, is_open_Union) μ.inner_content_Union_nat μ.inner_content_mono U.2 lemma outer_measure_of_is_open (U : set G) (hU : is_open U) : μ.outer_measure U = μ.inner_content ⟨U, hU⟩ := μ.outer_measure_opens ⟨U, hU⟩ lemma outer_measure_le (U : opens G) (K : compacts G) (hUK : (U : set G) ⊆ K.1) : μ.outer_measure U ≤ μ K := (μ.outer_measure_opens U).le.trans $ μ.inner_content_le U K hUK lemma le_outer_measure_compacts (K : compacts G) : μ K ≤ μ.outer_measure K.1 := begin rw [content.outer_measure, induced_outer_measure_eq_infi], { exact le_infi (λ U, le_infi $ λ hU, le_infi $ μ.le_inner_content K ⟨U, hU⟩) }, { exact μ.inner_content_Union_nat }, { exact μ.inner_content_mono } end lemma outer_measure_eq_infi (A : set G) : μ.outer_measure A = ⨅ (U : set G) (hU : is_open U) (h : A ⊆ U), μ.inner_content ⟨U, hU⟩ := induced_outer_measure_eq_infi _ μ.inner_content_Union_nat μ.inner_content_mono A lemma outer_measure_interior_compacts (K : compacts G) : μ.outer_measure (interior K.1) ≤ μ K := le_trans (le_of_eq $ μ.outer_measure_opens (opens.interior K.1)) (μ.inner_content_le _ _ interior_subset) lemma outer_measure_exists_compact {U : opens G} (hU : μ.outer_measure U ≠ ∞) {ε : ℝ≥0} (hε : ε ≠ 0) : ∃ K : compacts G, K.1 ⊆ U ∧ μ.outer_measure U ≤ μ.outer_measure K.1 + ε := begin rw [μ.outer_measure_opens] at hU ⊢, rcases μ.inner_content_exists_compact hU hε with ⟨K, h1K, h2K⟩, exact ⟨K, h1K, le_trans h2K $ add_le_add_right (μ.le_outer_measure_compacts K) _⟩, end lemma outer_measure_exists_open {A : set G} (hA : μ.outer_measure A ≠ ∞) {ε : ℝ≥0} (hε : ε ≠ 0) : ∃ U : opens G, A ⊆ U ∧ μ.outer_measure U ≤ μ.outer_measure A + ε := begin rcases induced_outer_measure_exists_set _ _ μ.inner_content_mono hA (ennreal.coe_ne_zero.2 hε) with ⟨U, hU, h2U, h3U⟩, exact ⟨⟨U, hU⟩, h2U, h3U⟩, swap, exact μ.inner_content_Union_nat end lemma outer_measure_preimage (f : G ≃ₜ G) (h : ∀ ⦃K : compacts G⦄, μ (K.map f f.continuous) = μ K) (A : set G) : μ.outer_measure (f ⁻¹' A) = μ.outer_measure A := begin refine induced_outer_measure_preimage _ μ.inner_content_Union_nat μ.inner_content_mono _ (λ s, f.is_open_preimage) _, intros s hs, convert μ.inner_content_comap f h ⟨s, hs⟩ end lemma outer_measure_lt_top_of_is_compact [locally_compact_space G] {K : set G} (hK : is_compact K) : μ.outer_measure K < ∞ := begin rcases exists_compact_superset hK with ⟨F, h1F, h2F⟩, calc μ.outer_measure K ≤ μ.outer_measure (interior F) : outer_measure.mono' _ h2F ... ≤ μ ⟨F, h1F⟩ : by apply μ.outer_measure_le ⟨interior F, is_open_interior⟩ ⟨F, h1F⟩ interior_subset ... < ⊤ : μ.lt_top _ end @[to_additive] lemma is_mul_left_invariant_outer_measure [group G] [topological_group G] (h : ∀ (g : G) {K : compacts G}, μ (K.map _ $ continuous_mul_left g) = μ K) (g : G) (A : set G) : μ.outer_measure ((λ h, g * h) ⁻¹' A) = μ.outer_measure A := by convert μ.outer_measure_preimage (homeomorph.mul_left g) (λ K, h g) A lemma outer_measure_caratheodory (A : set G) : μ.outer_measure.caratheodory.measurable_set' A ↔ ∀ (U : opens G), μ.outer_measure (U ∩ A) + μ.outer_measure (U \ A) ≤ μ.outer_measure U := begin dsimp [opens], rw subtype.forall, apply induced_outer_measure_caratheodory, apply inner_content_Union_nat, apply inner_content_mono' end @[to_additive] lemma outer_measure_pos_of_is_mul_left_invariant [group G] [topological_group G] (h3 : ∀ (g : G) {K : compacts G}, μ (K.map _ $ continuous_mul_left g) = μ K) (K : compacts G) (hK : μ K ≠ 0) {U : set G} (h1U : is_open U) (h2U : U.nonempty) : 0 < μ.outer_measure U := by { convert μ.inner_content_pos_of_is_mul_left_invariant h3 K hK ⟨U, h1U⟩ h2U, exact μ.outer_measure_opens ⟨U, h1U⟩ } variables [S : measurable_space G] [borel_space G] include S /-- For the outer measure coming from a content, all Borel sets are measurable. -/ lemma borel_le_caratheodory : S ≤ μ.outer_measure.caratheodory := begin rw [@borel_space.measurable_eq G _ _], refine measurable_space.generate_from_le _, intros U hU, rw μ.outer_measure_caratheodory, intro U', rw μ.outer_measure_of_is_open ((U' : set G) ∩ U) (is_open.inter U'.prop hU), simp only [inner_content, supr_subtype'], rw [opens.coe_mk], haveI : nonempty {L : compacts G // L.1 ⊆ U' ∩ U} := ⟨⟨⊥, empty_subset _⟩⟩, rw [ennreal.supr_add], refine supr_le _, rintro ⟨L, hL⟩, simp only [subset_inter_iff] at hL, have : ↑U' \ U ⊆ U' \ L.1 := diff_subset_diff_right hL.2, refine le_trans (add_le_add_left (μ.outer_measure.mono' this) _) _, rw μ.outer_measure_of_is_open (↑U' \ L.1) (is_open.sdiff U'.2 L.2.is_closed), simp only [inner_content, supr_subtype'], rw [opens.coe_mk], haveI : nonempty {M : compacts G // M.1 ⊆ ↑U' \ L.1} := ⟨⟨⊥, empty_subset _⟩⟩, rw [ennreal.add_supr], refine supr_le _, rintro ⟨M, hM⟩, simp only [subset_diff] at hM, have : (L ⊔ M).1 ⊆ U', { simp only [union_subset_iff, compacts.sup_val, hM, hL, and_self] }, rw μ.outer_measure_of_is_open ↑U' U'.2, refine le_trans (ge_of_eq _) (μ.le_inner_content _ _ this), exact μ.sup_disjoint _ _ hM.2.symm, end /-- The measure induced by the outer measure coming from a content, on the Borel sigma-algebra. -/ protected def measure : measure G := μ.outer_measure.to_measure μ.borel_le_caratheodory lemma measure_apply {s : set G} (hs : measurable_set s) : μ.measure s = μ.outer_measure s := to_measure_apply _ _ hs /-- In a locally compact space, any measure constructed from a content is regular. -/ instance regular [locally_compact_space G] : μ.measure.regular := begin haveI : μ.measure.outer_regular, { refine ⟨λ A hA r (hr : _ < _), _⟩, rw [μ.measure_apply hA, outer_measure_eq_infi] at hr, simp only [infi_lt_iff] at hr, rcases hr with ⟨U, hUo, hAU, hr⟩, rw [← μ.outer_measure_of_is_open U hUo, ← μ.measure_apply hUo.measurable_set] at hr, exact ⟨U, hAU, hUo, hr⟩ }, split, { intros K hK, rw [measure_apply _ hK.measurable_set], exact μ.outer_measure_lt_top_of_is_compact hK }, { intros U hU r hr, rw [measure_apply _ hU.measurable_set, μ.outer_measure_of_is_open U hU] at hr, simp only [inner_content, lt_supr_iff] at hr, rcases hr with ⟨K, hKU, hr⟩, refine ⟨K.1, hKU, K.2, hr.trans_le _⟩, exact (μ.le_outer_measure_compacts K).trans (le_to_measure_apply _ _ _) }, end end content end measure_theory
8bf4c25db3572f67b1dfc2ea42b68282eacd64cc
7b02c598aa57070b4cf4fbfe2416d0479220187f
/move_to_lib.hlean
59cd841b6e4f5edca810873212290d47bfc0c2af
[ "Apache-2.0" ]
permissive
jdchristensen/Spectral
50d4f0ddaea1484d215ef74be951da6549de221d
6ded2b94d7ae07c4098d96a68f80a9cd3d433eb8
refs/heads/master
1,611,555,010,649
1,496,724,191,000
1,496,724,191,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
20,605
hlean
-- definitions, theorems and attributes which should be moved to files in the HoTT library import homotopy.sphere2 homotopy.cofiber homotopy.wedge hit.prop_trunc hit.set_quotient eq2 open eq nat int susp pointed pmap sigma is_equiv equiv fiber algebra trunc trunc_index pi group is_trunc function sphere unit sum prod bool namespace eq definition eq.rec_to {A : Type} {a₀ : A} {P : Π⦃a₁⦄, a₀ = a₁ → Type} {a₁ : A} (p₀ : a₀ = a₁) (H : P p₀) ⦃a₂ : A⦄ (p : a₀ = a₂) : P p := begin induction p₀, induction p, exact H end definition eq.rec_to2 {A : Type} {P : Π⦃a₀ a₁⦄, a₀ = a₁ → Type} {a₀ a₀' a₁' : A} (p' : a₀' = a₁') (p₀ : a₀ = a₀') (H : P p') ⦃a₁ : A⦄ (p : a₀ = a₁) : P p := begin induction p₀, induction p', induction p, exact H end definition eq.rec_right_inv {A : Type} (f : A ≃ A) {P : Π⦃a₀ a₁⦄, f a₀ = a₁ → Type} (H : Πa, P (right_inv f a)) ⦃a₀ a₁ : A⦄ (p : f a₀ = a₁) : P p := begin revert a₀ p, refine equiv_rect f⁻¹ᵉ _ _, intro a₀ p, exact eq.rec_to (right_inv f a₀) (H a₀) p, end definition eq.rec_equiv {A B : Type} {a₀ : A} (f : A ≃ B) {P : Π{a₁}, f a₀ = f a₁ → Type} (H : P (idpath (f a₀))) ⦃a₁ : A⦄ (p : f a₀ = f a₁) : P p := begin assert qr : Σ(q : a₀ = a₁), ap f q = p, { exact ⟨eq_of_fn_eq_fn f p, ap_eq_of_fn_eq_fn' f p⟩ }, cases qr with q r, apply transport P r, induction q, exact H end definition eq.rec_equiv_symm {A B : Type} {a₁ : A} (f : A ≃ B) {P : Π{a₀}, f a₀ = f a₁ → Type} (H : P (idpath (f a₁))) ⦃a₀ : A⦄ (p : f a₀ = f a₁) : P p := begin assert qr : Σ(q : a₀ = a₁), ap f q = p, { exact ⟨eq_of_fn_eq_fn f p, ap_eq_of_fn_eq_fn' f p⟩ }, cases qr with q r, apply transport P r, induction q, exact H end definition eq.rec_equiv_to_same {A B : Type} {a₀ : A} (f : A ≃ B) {P : Π{a₁}, f a₀ = f a₁ → Type} ⦃a₁' : A⦄ (p' : f a₀ = f a₁') (H : P p') ⦃a₁ : A⦄ (p : f a₀ = f a₁) : P p := begin revert a₁' p' H a₁ p, refine eq.rec_equiv f _, exact eq.rec_equiv f end definition eq.rec_equiv_to {A A' B : Type} {a₀ : A} (f : A ≃ B) (g : A' ≃ B) {P : Π{a₁}, f a₀ = g a₁ → Type} ⦃a₁' : A'⦄ (p' : f a₀ = g a₁') (H : P p') ⦃a₁ : A'⦄ (p : f a₀ = g a₁) : P p := begin assert qr : Σ(q : g⁻¹ (f a₀) = a₁), (right_inv g (f a₀))⁻¹ ⬝ ap g q = p, { exact ⟨eq_of_fn_eq_fn g (right_inv g (f a₀) ⬝ p), whisker_left _ (ap_eq_of_fn_eq_fn' g _) ⬝ !inv_con_cancel_left⟩ }, assert q'r' : Σ(q' : g⁻¹ (f a₀) = a₁'), (right_inv g (f a₀))⁻¹ ⬝ ap g q' = p', { exact ⟨eq_of_fn_eq_fn g (right_inv g (f a₀) ⬝ p'), whisker_left _ (ap_eq_of_fn_eq_fn' g _) ⬝ !inv_con_cancel_left⟩ }, induction qr with q r, induction q'r' with q' r', induction q, induction q', induction r, induction r', exact H end definition eq.rec_grading {A A' B : Type} {a : A} (f : A ≃ B) (g : A' ≃ B) {P : Π{b}, f a = b → Type} {a' : A'} (p' : f a = g a') (H : P p') ⦃b : B⦄ (p : f a = b) : P p := begin revert b p, refine equiv_rect g _ _, exact eq.rec_equiv_to f g p' H end definition eq.rec_grading_unbased {A B B' C : Type} (f : A ≃ B) (g : B ≃ C) (h : B' ≃ C) {P : Π{b c}, g b = c → Type} {a' : A} {b' : B'} (p' : g (f a') = h b') (H : P p') ⦃b : B⦄ ⦃c : C⦄ (q : f a' = b) (p : g b = c) : P p := begin induction q, exact eq.rec_grading (f ⬝e g) h p' H p end -- definition homotopy_group_homomorphism_pinv (n : ℕ) {A B : Type*} (f : A ≃* B) : -- π→g[n+1] f⁻¹ᵉ* ~ (homotopy_group_isomorphism_of_pequiv n f)⁻¹ᵍ := -- begin -- -- refine ptrunc_functor_phomotopy 0 !apn_pinv ⬝hty _, -- -- intro x, esimp, -- end -- definition natural_square_tr_eq {A B : Type} {a a' : A} {f g : A → B} -- (p : f ~ g) (q : a = a') : natural_square p q = square_of_pathover (apd p q) := -- idp end eq open eq namespace trunc -- TODO: redefine loopn_ptrunc_pequiv definition apn_ptrunc_functor (n : ℕ₋₂) (k : ℕ) {A B : Type*} (f : A →* B) : Ω→[k] (ptrunc_functor (n+k) f) ∘* (loopn_ptrunc_pequiv n k A)⁻¹ᵉ* ~* (loopn_ptrunc_pequiv n k B)⁻¹ᵉ* ∘* ptrunc_functor n (Ω→[k] f) := begin revert n, induction k with k IH: intro n, { reflexivity }, { exact sorry } end definition ptrunc_pequiv_natural [constructor] (n : ℕ₋₂) {A B : Type*} (f : A →* B) [is_trunc n A] [is_trunc n B] : f ∘* ptrunc_pequiv n A ~* ptrunc_pequiv n B ∘* ptrunc_functor n f := begin fapply phomotopy.mk, { intro a, induction a with a, reflexivity }, { refine !idp_con ⬝ _ ⬝ !idp_con⁻¹, refine !ap_compose'⁻¹ ⬝ _, apply ap_id } end definition ptr_natural [constructor] (n : ℕ₋₂) {A B : Type*} (f : A →* B) : ptrunc_functor n f ∘* ptr n A ~* ptr n B ∘* f := begin fapply phomotopy.mk, { intro a, reflexivity }, { reflexivity } end definition ptrunc_elim_pcompose (n : ℕ₋₂) {A B C : Type*} (g : B →* C) (f : A →* B) [is_trunc n B] [is_trunc n C] : ptrunc.elim n (g ∘* f) ~* g ∘* ptrunc.elim n f := begin fapply phomotopy.mk, { intro a, induction a with a, reflexivity }, { apply idp_con } end end trunc namespace sigma -- definition sigma_pathover_equiv_of_is_prop {A : Type} {B : A → Type} {C : Πa, B a → Type} -- {a a' : A} {p : a = a'} {b : B a} {b' : B a'} {c : C a b} {c' : C a' b'} -- [Πa b, is_prop (C a b)] : ⟨b, c⟩ =[p] ⟨b', c'⟩ ≃ b =[p] b' := -- begin -- fapply equiv.MK, -- { exact pathover_pr1 }, -- { intro q, induction q, apply pathover_idp_of_eq, exact sigma_eq idp !is_prop.elimo }, -- { intro q, induction q, -- have c = c', from !is_prop.elim, induction this, -- rewrite [▸*, is_prop_elimo_self (C a) c] }, -- { esimp, generalize ⟨b, c⟩, intro x q, } -- end --rexact @(ap pathover_pr1) _ idpo _, end sigma open sigma namespace group -- definition is_equiv_isomorphism -- some extra instances for type class inference -- definition is_mul_hom_comm_homomorphism [instance] {G G' : AbGroup} (φ : G →g G') -- : @is_mul_hom G G' (@ab_group.to_group _ (AbGroup.struct G)) -- (@ab_group.to_group _ (AbGroup.struct G')) φ := -- homomorphism.struct φ -- definition is_mul_hom_comm_homomorphism1 [instance] {G G' : AbGroup} (φ : G →g G') -- : @is_mul_hom G G' _ -- (@ab_group.to_group _ (AbGroup.struct G')) φ := -- homomorphism.struct φ -- definition is_mul_hom_comm_homomorphism2 [instance] {G G' : AbGroup} (φ : G →g G') -- : @is_mul_hom G G' (@ab_group.to_group _ (AbGroup.struct G)) _ φ := -- homomorphism.struct φ end group open group namespace function variables {A B : Type} {f f' : A → B} definition homotopy_group_isomorphism_of_is_embedding (n : ℕ) [H : is_succ n] {A B : Type*} (f : A →* B) [H2 : is_embedding f] : πg[n] A ≃g πg[n] B := begin apply isomorphism.mk (homotopy_group_homomorphism n f), induction H with n, apply is_equiv_of_equiv_of_homotopy (ptrunc_pequiv_ptrunc 0 (loopn_pequiv_loopn_of_is_embedding (n+1) f)), exact sorry end end function open function namespace is_conn open unit trunc_index nat is_trunc pointed.ops definition is_conn_fun_compose {n : ℕ₋₂} {A B C : Type} (g : B → C) (f : A → B) (H : is_conn_fun n g) (K : is_conn_fun n f) : is_conn_fun n (g ∘ f) := sorry end is_conn namespace misc open is_conn /- move! -/ open sigma.ops pointed definition merely_constant {A B : Type} (f : A → B) : Type := Σb, Πa, merely (f a = b) definition merely_constant_pmap {A B : Type*} {f : A →* B} (H : merely_constant f) (a : A) : merely (f a = pt) := tconcat (tconcat (H.2 a) (tinverse (H.2 pt))) (tr (respect_pt f)) definition merely_constant_of_is_conn {A B : Type*} (f : A →* B) [is_conn 0 A] : merely_constant f := ⟨pt, is_conn.elim -1 _ (tr (respect_pt f))⟩ open sigma definition component [constructor] (A : Type*) : Type* := pType.mk (Σ(a : A), merely (pt = a)) ⟨pt, tr idp⟩ lemma is_conn_component [instance] (A : Type*) : is_conn 0 (component A) := is_contr.mk (tr pt) begin intro x, induction x with x, induction x with a p, induction p with p, induction p, reflexivity end definition component_incl [constructor] (A : Type*) : component A →* A := pmap.mk pr1 idp definition is_embedding_component_incl [instance] (A : Type*) : is_embedding (component_incl A) := is_embedding_pr1 _ definition component_intro [constructor] {A B : Type*} (f : A →* B) (H : merely_constant f) : A →* component B := begin fapply pmap.mk, { intro a, refine ⟨f a, _⟩, exact tinverse (merely_constant_pmap H a) }, exact subtype_eq !respect_pt end definition component_functor [constructor] {A B : Type*} (f : A →* B) : component A →* component B := component_intro (f ∘* component_incl A) !merely_constant_of_is_conn -- definition component_elim [constructor] {A B : Type*} (f : A →* B) (H : merely_constant f) : -- A →* component B := -- begin -- fapply pmap.mk, -- { intro a, refine ⟨f a, _⟩, exact tinverse (merely_constant_pmap H a) }, -- exact subtype_eq !respect_pt -- end definition loop_component (A : Type*) : Ω (component A) ≃* Ω A := loop_pequiv_loop_of_is_embedding (component_incl A) lemma loopn_component (n : ℕ) (A : Type*) : Ω[n+1] (component A) ≃* Ω[n+1] A := !loopn_succ_in ⬝e* loopn_pequiv_loopn n (loop_component A) ⬝e* !loopn_succ_in⁻¹ᵉ* -- lemma fundamental_group_component (A : Type*) : π₁ (component A) ≃g π₁ A := -- isomorphism_of_equiv (trunc_equiv_trunc 0 (loop_component A)) _ lemma homotopy_group_component (n : ℕ) (A : Type*) : πg[n+1] (component A) ≃g πg[n+1] A := homotopy_group_isomorphism_of_is_embedding (n+1) (component_incl A) definition is_trunc_component [instance] (n : ℕ₋₂) (A : Type*) [is_trunc n A] : is_trunc n (component A) := begin apply @is_trunc_sigma, intro a, cases n with n, { apply is_contr_of_inhabited_prop, exact tr !is_prop.elim }, { apply is_trunc_succ_of_is_prop }, end definition ptrunc_component' (n : ℕ₋₂) (A : Type*) : ptrunc (n.+2) (component A) ≃* component (ptrunc (n.+2) A) := begin fapply pequiv.MK, { exact ptrunc.elim (n.+2) (component_functor !ptr) }, { intro x, cases x with x p, induction x with a, refine tr ⟨a, _⟩, note q := trunc_functor -1 !tr_eq_tr_equiv p, exact trunc_trunc_equiv_left _ !minus_one_le_succ q }, { exact sorry }, { exact sorry } end definition ptrunc_component (n : ℕ₋₂) (A : Type*) : ptrunc n (component A) ≃* component (ptrunc n A) := begin cases n with n, exact sorry, cases n with n, exact sorry, exact ptrunc_component' n A end definition pfiber_pequiv_component_of_is_contr [constructor] {A B : Type*} (f : A →* B) [is_contr B] /- extra condition, something like trunc_functor 0 f is an embedding -/ : pfiber f ≃* component A := sorry end misc namespace category definition precategory_Group.{u} [instance] [constructor] : precategory.{u+1 u} Group := begin fapply precategory.mk, { exact λG H, G →g H }, { exact _ }, { exact λG H K ψ φ, ψ ∘g φ }, { exact λG, gid G }, { intros, apply homomorphism_eq, esimp }, { intros, apply homomorphism_eq, esimp }, { intros, apply homomorphism_eq, esimp } end definition precategory_AbGroup.{u} [instance] [constructor] : precategory.{u+1 u} AbGroup := begin fapply precategory.mk, { exact λG H, G →g H }, { exact _ }, { exact λG H K ψ φ, ψ ∘g φ }, { exact λG, gid G }, { intros, apply homomorphism_eq, esimp }, { intros, apply homomorphism_eq, esimp }, { intros, apply homomorphism_eq, esimp } end open iso definition Group_is_iso_of_is_equiv {G H : Group} (φ : G →g H) (H : is_equiv (group_fun φ)) : is_iso φ := begin fconstructor, { exact (isomorphism.mk φ H)⁻¹ᵍ }, { apply homomorphism_eq, rexact left_inv φ }, { apply homomorphism_eq, rexact right_inv φ } end definition Group_is_equiv_of_is_iso {G H : Group} (φ : G ⟶ H) (Hφ : is_iso φ) : is_equiv (group_fun φ) := begin fapply adjointify, { exact group_fun φ⁻¹ʰ }, { note p := right_inverse φ, exact ap010 group_fun p }, { note p := left_inverse φ, exact ap010 group_fun p } end definition Group_iso_equiv (G H : Group) : (G ≅ H) ≃ (G ≃g H) := begin fapply equiv.MK, { intro φ, induction φ with φ φi, constructor, exact Group_is_equiv_of_is_iso φ _ }, { intro v, induction v with φ φe, constructor, exact Group_is_iso_of_is_equiv φ _ }, { intro v, induction v with φ φe, apply isomorphism_eq, reflexivity }, { intro φ, induction φ with φ φi, apply iso_eq, reflexivity } end definition Group_props.{u} {A : Type.{u}} (v : (A → A → A) × (A → A) × A) : Prop.{u} := begin induction v with m v, induction v with i o, fapply trunctype.mk, { exact is_set A × (Πa, m a o = a) × (Πa, m o a = a) × (Πa b c, m (m a b) c = m a (m b c)) × (Πa, m (i a) a = o) }, { apply is_trunc_of_imp_is_trunc, intro v, induction v with H v, have is_prop (Πa, m a o = a), from _, have is_prop (Πa, m o a = a), from _, have is_prop (Πa b c, m (m a b) c = m a (m b c)), from _, have is_prop (Πa, m (i a) a = o), from _, apply is_trunc_prod } end definition Group.sigma_char2.{u} : Group.{u} ≃ Σ(A : Type.{u}) (v : (A → A → A) × (A → A) × A), Group_props v := begin fapply equiv.MK, { intro G, refine ⟨G, _⟩, induction G with G g, induction g with m s ma o om mo i mi, repeat (fconstructor; do 2 try assumption), }, { intro v, induction v with x v, induction v with y v, repeat induction y with x y, repeat induction v with x v, constructor, fconstructor, repeat assumption }, { intro v, induction v with x v, induction v with y v, repeat induction y with x y, repeat induction v with x v, reflexivity }, { intro v, repeat induction v with x v, reflexivity }, end open is_trunc section local attribute group.to_has_mul group.to_has_inv [coercion] theorem inv_eq_of_mul_eq {A : Type} (G H : group A) (p : @mul A G ~2 @mul A H) : @inv A G ~ @inv A H := begin have foo : Π(g : A), @inv A G g = (@inv A G g * g) * @inv A H g, from λg, !mul_inv_cancel_right⁻¹, cases G with Gs Gm Gh1 G1 Gh2 Gh3 Gi Gh4, cases H with Hs Hm Hh1 H1 Hh2 Hh3 Hi Hh4, change Gi ~ Hi, intro g, have p' : Gm ~2 Hm, from p, calc Gi g = Hm (Hm (Gi g) g) (Hi g) : foo ... = Hm (Gm (Gi g) g) (Hi g) : by rewrite p' ... = Hm G1 (Hi g) : by rewrite Gh4 ... = Gm G1 (Hi g) : by rewrite p' ... = Hi g : Gh2 end theorem one_eq_of_mul_eq {A : Type} (G H : group A) (p : @mul A (group.to_has_mul G) ~2 @mul A (group.to_has_mul H)) : @one A (group.to_has_one G) = @one A (group.to_has_one H) := begin cases G with Gm Gs Gh1 G1 Gh2 Gh3 Gi Gh4, cases H with Hm Hs Hh1 H1 Hh2 Hh3 Hi Hh4, exact (Hh2 G1)⁻¹ ⬝ (p H1 G1)⁻¹ ⬝ Gh3 H1, end end open prod.ops definition group_of_Group_props.{u} {A : Type.{u}} {m : A → A → A} {i : A → A} {o : A} (H : Group_props (m, (i, o))) : group A := ⦃group, mul := m, inv := i, one := o, is_set_carrier := H.1, mul_one := H.2.1, one_mul := H.2.2.1, mul_assoc := H.2.2.2.1, mul_left_inv := H.2.2.2.2⦄ theorem Group_eq_equiv_lemma2 {A : Type} {m m' : A → A → A} {i i' : A → A} {o o' : A} (H : Group_props (m, (i, o))) (H' : Group_props (m', (i', o'))) : (m, (i, o)) = (m', (i', o')) ≃ (m ~2 m') := begin have is_set A, from pr1 H, apply equiv_of_is_prop, { intro p, exact apd100 (eq_pr1 p)}, { intro p, apply prod_eq (eq_of_homotopy2 p), apply prod_eq: esimp [Group_props] at *; esimp, { apply eq_of_homotopy, exact inv_eq_of_mul_eq (group_of_Group_props H) (group_of_Group_props H') p }, { exact one_eq_of_mul_eq (group_of_Group_props H) (group_of_Group_props H') p }} end open sigma.ops theorem Group_eq_equiv_lemma {G H : Group} (p : (Group.sigma_char2 G).1 = (Group.sigma_char2 H).1) : ((Group.sigma_char2 G).2 =[p] (Group.sigma_char2 H).2) ≃ (is_mul_hom (equiv_of_eq (proof p qed : Group.carrier G = Group.carrier H))) := begin refine !sigma_pathover_equiv_of_is_prop ⬝e _, induction G with G g, induction H with H h, esimp [Group.sigma_char2] at p, induction p, refine !pathover_idp ⬝e _, induction g with s m ma o om mo i mi, induction h with σ μ μa ε εμ με ι μι, exact Group_eq_equiv_lemma2 (Group.sigma_char2 (Group.mk G (group.mk s m ma o om mo i mi))).2.2 (Group.sigma_char2 (Group.mk G (group.mk σ μ μa ε εμ με ι μι))).2.2 end definition isomorphism.sigma_char (G H : Group) : (G ≃g H) ≃ Σ(e : G ≃ H), is_mul_hom e := begin fapply equiv.MK, { intro φ, exact ⟨equiv_of_isomorphism φ, to_respect_mul φ⟩ }, { intro v, induction v with e p, exact isomorphism_of_equiv e p }, { intro v, induction v with e p, induction e, reflexivity }, { intro φ, induction φ with φ H, induction φ, reflexivity }, end definition Group_eq_equiv (G H : Group) : G = H ≃ (G ≃g H) := begin refine (eq_equiv_fn_eq_of_equiv Group.sigma_char2 G H) ⬝e _, refine !sigma_eq_equiv ⬝e _, refine sigma_equiv_sigma_right Group_eq_equiv_lemma ⬝e _, transitivity (Σ(e : (Group.sigma_char2 G).1 ≃ (Group.sigma_char2 H).1), @is_mul_hom _ _ _ _ (to_fun e)), apply sigma_ua, exact !isomorphism.sigma_char⁻¹ᵉ end definition to_fun_Group_eq_equiv {G H : Group} (p : G = H) : Group_eq_equiv G H p ~ isomorphism_of_eq p := begin induction p, reflexivity end definition Group_eq2 {G H : Group} {p q : G = H} (r : isomorphism_of_eq p ~ isomorphism_of_eq q) : p = q := begin apply eq_of_fn_eq_fn (Group_eq_equiv G H), apply isomorphism_eq, intro g, refine to_fun_Group_eq_equiv p g ⬝ r g ⬝ (to_fun_Group_eq_equiv q g)⁻¹, end definition Group_eq_equiv_Group_iso (G₁ G₂ : Group) : G₁ = G₂ ≃ G₁ ≅ G₂ := Group_eq_equiv G₁ G₂ ⬝e (Group_iso_equiv G₁ G₂)⁻¹ᵉ definition category_Group.{u} : category Group.{u} := category.mk precategory_Group begin intro G H, apply is_equiv_of_equiv_of_homotopy (Group_eq_equiv_Group_iso G H), intro p, induction p, fapply iso_eq, apply homomorphism_eq, reflexivity end definition category_AbGroup : category AbGroup := category.mk precategory_AbGroup sorry definition Grp.{u} [constructor] : Category := category.Mk Group.{u} category_Group definition AbGrp [constructor] : Category := category.Mk AbGroup category_AbGroup end category namespace sphere -- definition constant_sphere_map_sphere {n m : ℕ} (H : n < m) (f : S* n →* S* m) : -- f ~* pconst (S* n) (S* m) := -- begin -- assert H : is_contr (Ω[n] (S* m)), -- { apply homotopy_group_sphere_le, }, -- apply phomotopy_of_eq, -- apply eq_of_fn_eq_fn !psphere_pmap_pequiv, -- apply @is_prop.elim -- end end sphere section injective_surjective open trunc fiber image /- do we want to prove this without funext before we move it? -/ variables {A B C : Type} (f : A → B) definition is_embedding_factor [is_set A] [is_set B] (g : B → C) (h : A → C) (H : g ∘ f ~ h) : is_embedding h → is_embedding f := begin induction H using homotopy.rec_on_idp, intro E, fapply is_embedding_of_is_injective, intro x y p, fapply @is_injective_of_is_embedding _ _ _ E _ _ (ap g p) end definition is_surjective_factor (g : B → C) (h : A → C) (H : g ∘ f ~ h) : is_surjective h → is_surjective g := begin induction H using homotopy.rec_on_idp, intro S, intro c, note p := S c, induction p, apply tr, fapply fiber.mk, exact f a, exact p end end injective_surjective
2f3ea9f4269249fa7ab79881123fcbada9c0cf14
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1184.lean
296c38a738c123905784f40ea771c924a3790c4e
[ "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
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
159
lean
@[simp] theorem one_le_of_lt (h: n < m) : 1 ≤ m := Nat.lt_of_le_of_lt (Nat.zero_le _) h example (h: n < m) : 1 ≤ m := by simp (disch := assumption) [h]
71071bab97421e585ff369c28da72ce8ede4e879
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/stage0/src/Init/Classical.lean
29ed50bb2fc36c1ab1c3d2b65a1b3d4a55a56f55
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,392
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Core import Init.NotationExtra universes u v /- Classical reasoning support -/ namespace Classical axiom choice {α : Sort u} : Nonempty α → α noncomputable def indefiniteDescription {α : Sort u} (p : α → Prop) (h : ∃ x, p x) : {x // p x} := choice <| let ⟨x, px⟩ := h; ⟨⟨x, px⟩⟩ noncomputable def choose {α : Sort u} {p : α → Prop} (h : ∃ x, p x) : α := (indefiniteDescription p h).val theorem chooseSpec {α : Sort u} {p : α → Prop} (h : ∃ x, p x) : p (choose h) := (indefiniteDescription p h).property /- Diaconescu's theorem: excluded middle from choice, Function extensionality and propositional extensionality. -/ theorem em (p : Prop) : p ∨ ¬p := let U (x : Prop) : Prop := x = True ∨ p; let V (x : Prop) : Prop := x = False ∨ p; have exU : ∃ x, U x from ⟨True, Or.inl rfl⟩; have exV : ∃ x, V x from ⟨False, Or.inl rfl⟩; let u : Prop := choose exU; let v : Prop := choose exV; have uDef : U u from chooseSpec exU; have vDef : V v from chooseSpec exV; have notUvOrP : u ≠ v ∨ p from match uDef, vDef with | Or.inr h, _ => Or.inr h | _, Or.inr h => Or.inr h | Or.inl hut, Or.inl hvf => have hne : u ≠ v from hvf.symm ▸ hut.symm ▸ trueNeFalse Or.inl hne have pImpliesUv : p → u = v from fun hp => have hpred : U = V from funext fun x => have hl : (x = True ∨ p) → (x = False ∨ p) from fun a => Or.inr hp; have hr : (x = False ∨ p) → (x = True ∨ p) from fun a => Or.inr hp; show (x = True ∨ p) = (x = False ∨ p) from propext (Iff.intro hl hr); have h₀ : ∀ exU exV, @choose _ U exU = @choose _ V exV from hpred ▸ fun exU exV => rfl; show u = v from h₀ ..; match notUvOrP with | Or.inl hne => Or.inr (mt pImpliesUv hne) | Or.inr h => Or.inl h theorem existsTrueOfNonempty {α : Sort u} : Nonempty α → ∃ x : α, True | ⟨x⟩ => ⟨x, trivial⟩ noncomputable def inhabitedOfNonempty {α : Sort u} (h : Nonempty α) : Inhabited α := ⟨choice h⟩ noncomputable def inhabitedOfExists {α : Sort u} {p : α → Prop} (h : ∃ x, p x) : Inhabited α := inhabitedOfNonempty (Exists.elim h (fun w hw => ⟨w⟩)) /- all propositions are Decidable -/ noncomputable scoped instance (priority := low) propDecidable (a : Prop) : Decidable a := choice <| match em a with | Or.inl h => ⟨isTrue h⟩ | Or.inr h => ⟨isFalse h⟩ noncomputable def decidableInhabited (a : Prop) : Inhabited (Decidable a) where default := inferInstance noncomputable def typeDecidableEq (α : Sort u) : DecidableEq α := fun x y => inferInstance noncomputable def typeDecidable (α : Sort u) : PSum α (α → False) := match (propDecidable (Nonempty α)) with | (isTrue hp) => PSum.inl (@arbitrary _ (inhabitedOfNonempty hp)) | (isFalse hn) => PSum.inr (fun a => absurd (Nonempty.intro a) hn) noncomputable def strongIndefiniteDescription {α : Sort u} (p : α → Prop) (h : Nonempty α) : {x : α // (∃ y : α, p y) → p x} := @dite _ (∃ x : α, p x) (propDecidable _) (fun (hp : ∃ x : α, p x) => show {x : α // (∃ y : α, p y) → p x} from let xp := indefiniteDescription _ hp; ⟨xp.val, fun h' => xp.property⟩) (fun hp => ⟨choice h, fun h => absurd h hp⟩) /- the Hilbert epsilon Function -/ noncomputable def epsilon {α : Sort u} [h : Nonempty α] (p : α → Prop) : α := (strongIndefiniteDescription p h).val theorem epsilonSpecAux {α : Sort u} (h : Nonempty α) (p : α → Prop) : (∃ y, p y) → p (@epsilon α h p) := (strongIndefiniteDescription p h).property theorem epsilonSpec {α : Sort u} {p : α → Prop} (hex : ∃ y, p y) : p (@epsilon α (nonemptyOfExists hex) p) := epsilonSpecAux (nonemptyOfExists hex) p hex theorem epsilonSingleton {α : Sort u} (x : α) : @epsilon α ⟨x⟩ (fun y => y = x) = x := @epsilonSpec α (fun y => y = x) ⟨x, rfl⟩ /- the axiom of choice -/ theorem axiomOfChoice {α : Sort u} {β : α → Sort v} {r : ∀ x, β x → Prop} (h : ∀ x, ∃ y, r x y) : ∃ (f : ∀ x, β x), ∀ x, r x (f x) := ⟨_, fun x => chooseSpec (h x)⟩ theorem skolem {α : Sort u} {b : α → Sort v} {p : ∀ x, b x → Prop} : (∀ x, ∃ y, p x y) ↔ ∃ (f : ∀ x, b x), ∀ x, p x (f x) := ⟨axiomOfChoice, fun ⟨f, hw⟩ (x) => ⟨f x, hw x⟩⟩ theorem propComplete (a : Prop) : a = True ∨ a = False := by cases em a with | inl _ => apply Or.inl; apply propext; apply Iff.intro; { intros; apply True.intro }; { intro; assumption } | inr hn => apply Or.inr; apply propext; apply Iff.intro; { intro h; exact hn h }; { intro h; apply False.elim h } -- this supercedes byCases in Decidable theorem byCases {p q : Prop} (hpq : p → q) (hnpq : ¬p → q) : q := Decidable.byCases (dec := propDecidable _) hpq hnpq -- this supercedes byContradiction in Decidable theorem byContradiction {p : Prop} (h : ¬p → False) : p := Decidable.byContradiction (dec := propDecidable _) h macro "byCases" h:ident ":" e:term : tactic => `(cases em $e:term with | Or.inl $h:ident => _ | Or.inr $h:ident => _) end Classical
b44728c6fad13bdaefb5f9af0316c31f7295313a
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/src/Lean/Meta/Basic.lean
832b388f71f4f30c1e74bf86371c41b11fda0465
[ "Apache-2.0" ]
permissive
banksonian/lean4
3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc
78da6b3aa2840693eea354a41e89fc5b212a5011
refs/heads/master
1,673,703,624,165
1,605,123,551,000
1,605,123,551,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
44,664
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 -/ import Lean.Data.LOption import Lean.Environment import Lean.Class import Lean.ReducibilityAttrs import Lean.Util.Trace import Lean.Util.RecDepth import Lean.Util.PPExt import Lean.Compiler.InlineAttrs import Lean.Meta.TransparencyMode import Lean.Meta.DiscrTreeTypes import Lean.Eval import Lean.CoreM /- This module provides four (mutually dependent) goodies that are needed for building the elaborator and tactic frameworks. 1- Weak head normal form computation with support for metavariables and transparency modes. 2- Definitionally equality checking with support for metavariables (aka unification modulo definitional equality). 3- Type inference. 4- Type class resolution. They are packed into the MetaM monad. -/ namespace Lean.Meta builtin_initialize isDefEqStuckExceptionId : InternalExceptionId ← registerInternalExceptionId `isDefEqStuck structure Config := (foApprox : Bool := false) (ctxApprox : Bool := false) (quasiPatternApprox : Bool := false) /- When `constApprox` is set to true, we solve `?m t =?= c` using `?m := fun _ => c` when `?m t` is not a higher-order pattern and `c` is not an application as -/ (constApprox : Bool := false) /- When the following flag is set, `isDefEq` throws the exeption `Exeption.isDefEqStuck` whenever it encounters a constraint `?m ... =?= t` where `?m` is read only. This feature is useful for type class resolution where we may want to notify the caller that the TC problem may be solveable later after it assigns `?m`. -/ (isDefEqStuckEx : Bool := false) (transparency : TransparencyMode := TransparencyMode.default) /- If zetaNonDep == false, then non dependent let-decls are not zeta expanded. -/ (zetaNonDep : Bool := true) /- When `trackZeta == true`, we store zetaFVarIds all free variables that have been zeta-expanded. -/ (trackZeta : Bool := false) structure ParamInfo := (implicit : Bool := false) (instImplicit : Bool := false) (hasFwdDeps : Bool := false) (backDeps : Array Nat := #[]) instance : Inhabited ParamInfo := ⟨{}⟩ def ParamInfo.isExplicit (p : ParamInfo) : Bool := !p.implicit && p.instImplicit structure FunInfo := (paramInfo : Array ParamInfo := #[]) (resultDeps : Array Nat := #[]) structure InfoCacheKey := (transparency : TransparencyMode) (expr : Expr) (nargs? : Option Nat) namespace InfoCacheKey instance : Inhabited InfoCacheKey := ⟨⟨arbitrary _, arbitrary _, arbitrary _⟩⟩ instance : Hashable InfoCacheKey := ⟨fun ⟨transparency, expr, nargs⟩ => mixHash (hash transparency) $ mixHash (hash expr) (hash nargs)⟩ instance : BEq InfoCacheKey := ⟨fun ⟨t₁, e₁, n₁⟩ ⟨t₂, e₂, n₂⟩ => t₁ == t₂ && n₁ == n₂ && e₁ == e₂⟩ end InfoCacheKey open Std (PersistentArray PersistentHashMap) abbrev SynthInstanceCache := PersistentHashMap Expr (Option Expr) structure Cache := (inferType : PersistentExprStructMap Expr := {}) (funInfo : PersistentHashMap InfoCacheKey FunInfo := {}) (synthInstance : SynthInstanceCache := {}) (whnfDefault : PersistentExprStructMap Expr := {}) -- cache for closed terms and `TransparencyMode.default` (whnfAll : PersistentExprStructMap Expr := {}) -- cache for closed terms and `TransparencyMode.all` structure PostponedEntry := (lhs : Level) (rhs : Level) structure State := (mctx : MetavarContext := {}) (cache : Cache := {}) /- When `trackZeta == true`, then any let-decl free variable that is zeta expansion performed by `MetaM` is stored in `zetaFVarIds`. -/ (zetaFVarIds : NameSet := {}) (postponed : PersistentArray PostponedEntry := {}) instance : Inhabited State := ⟨{}⟩ structure Context := (config : Config := {}) (lctx : LocalContext := {}) (localInstances : LocalInstances := #[]) abbrev MetaM := ReaderT Context $ StateRefT State CoreM instance : MonadIO MetaM := { liftIO := fun x => liftM (liftIO x : CoreM _) } instance {α} : Inhabited (MetaM α) := { default := fun _ _ => arbitrary _ } instance : MonadLCtx MetaM := { getLCtx := do pure (← read).lctx } instance : MonadMCtx MetaM := { getMCtx := do pure (← get).mctx, modifyMCtx := fun f => modify fun s => { s with mctx := f s.mctx } } instance : AddMessageContext MetaM := { addMessageContext := addMessageContextFull } @[inline] def MetaM.run {α} (x : MetaM α) (ctx : Context := {}) (s : State := {}) : CoreM (α × State) := x ctx $.run s @[inline] def MetaM.run' {α} (x : MetaM α) (ctx : Context := {}) (s : State := {}) : CoreM α := Prod.fst <$> x.run ctx s @[inline] def MetaM.toIO {α} (x : MetaM α) (ctxCore : Core.Context) (sCore : Core.State) (ctx : Context := {}) (s : State := {}) : IO (α × Core.State × State) := do let ((a, s), sCore) ← (x.run ctx s).toIO ctxCore sCore pure (a, sCore, s) instance {α} [MetaEval α] : MetaEval (MetaM α) := ⟨fun env opts x _ => MetaEval.eval env opts x.run' true⟩ protected def throwIsDefEqStuck {α} : MetaM α := throw $ Exception.internal isDefEqStuckExceptionId builtin_initialize registerTraceClass `Meta registerTraceClass `Meta.debug @[inline] def liftMetaM {α m} [MonadLiftT MetaM m] (x : MetaM α) : m α := liftM x @[inline] def mapMetaM {m} [MonadControlT MetaM m] [Monad m] (f : forall {α}, MetaM α → MetaM α) {α} (x : m α) : m α := controlAt MetaM fun runInBase => f $ runInBase x @[inline] def map1MetaM {β m} [MonadControlT MetaM m] [Monad m] (f : forall {α}, (β → MetaM α) → MetaM α) {α} (k : β → m α) : m α := controlAt MetaM fun runInBase => f fun b => runInBase $ k b @[inline] def map2MetaM {β γ m} [MonadControlT MetaM m] [Monad m] (f : forall {α}, (β → γ → MetaM α) → MetaM α) {α} (k : β → γ → m α) : m α := controlAt MetaM fun runInBase => f fun b c => runInBase $ k b c section Methods variables {m : Type → Type} [MonadLiftT MetaM m] variables {n : Type → Type} [MonadControlT MetaM n] [Monad n] def getLocalInstances : m LocalInstances := liftMetaM do pure (← read).localInstances def getConfig : m Config := liftMetaM do pure (← read).config def setMCtx (mctx : MetavarContext) : m Unit := liftMetaM $ modify fun s => { s with mctx := mctx } def resetZetaFVarIds : m Unit := liftMetaM $ modify fun s => { s with zetaFVarIds := {} } def getZetaFVarIds : m NameSet := liftMetaM do pure (← get).zetaFVarIds def getPostponed : m (PersistentArray PostponedEntry) := liftMetaM do pure (← get).postponed def setPostponed (postponed : PersistentArray PostponedEntry) : m Unit := liftMetaM $ modify fun s => { s with postponed := postponed } @[inline] def modifyPostponed (f : PersistentArray PostponedEntry → PersistentArray PostponedEntry) : m Unit := liftMetaM $ modify fun s => { s with postponed := f s.postponed } builtin_initialize whnfRef : IO.Ref (Expr → MetaM Expr) ← IO.mkRef fun _ => throwError "whnf implementation was not set" builtin_initialize inferTypeRef : IO.Ref (Expr → MetaM Expr) ← IO.mkRef fun _ => throwError "inferType implementation was not set" builtin_initialize isExprDefEqAuxRef : IO.Ref (Expr → Expr → MetaM Bool) ← IO.mkRef fun _ _ => throwError "isDefEq implementation was not set" builtin_initialize synthPendingRef : IO.Ref (MVarId → MetaM Bool) ← IO.mkRef fun _ => pure false def whnf (e : Expr) : m Expr := liftMetaM $ withIncRecDepth do (← liftIO whnfRef.get) e def whnfForall [Monad m] (e : Expr) : m Expr := do let e' ← whnf e if e'.isForall then pure e' else pure e def inferType (e : Expr) : m Expr := liftMetaM $ withIncRecDepth do (← liftIO inferTypeRef.get) e protected def isExprDefEqAux (t s : Expr) : MetaM Bool := withIncRecDepth do (← liftIO isExprDefEqAuxRef.get) t s protected def synthPending (mvarId : MVarId) : MetaM Bool := withIncRecDepth do (← liftIO synthPendingRef.get) mvarId private def mkFreshExprMVarAtCore (mvarId : MVarId) (lctx : LocalContext) (localInsts : LocalInstances) (type : Expr) (kind : MetavarKind) (userName : Name) (numScopeArgs : Nat) : MetaM Expr := do modifyMCtx fun mctx => mctx.addExprMVarDecl mvarId userName lctx localInsts type kind numScopeArgs; pure $ mkMVar mvarId def mkFreshExprMVarAt (lctx : LocalContext) (localInsts : LocalInstances) (type : Expr) (kind : MetavarKind := MetavarKind.natural) (userName : Name := Name.anonymous) (numScopeArgs : Nat := 0) : m Expr := liftMetaM do let mvarId ← mkFreshId mkFreshExprMVarAtCore mvarId lctx localInsts type kind userName numScopeArgs def mkFreshLevelMVar : m Level := liftMetaM do let mvarId ← mkFreshId modifyMCtx fun mctx => mctx.addLevelMVarDecl mvarId; pure $ mkLevelMVar mvarId private def mkFreshExprMVarCore (type : Expr) (kind : MetavarKind) (userName : Name) : MetaM Expr := do let lctx ← getLCtx let localInsts ← getLocalInstances mkFreshExprMVarAt lctx localInsts type kind userName private def mkFreshExprMVarImpl (type? : Option Expr) (kind : MetavarKind) (userName : Name) : MetaM Expr := match type? with | some type => mkFreshExprMVarCore type kind userName | none => do let u ← mkFreshLevelMVar let type ← mkFreshExprMVarCore (mkSort u) MetavarKind.natural Name.anonymous mkFreshExprMVarCore type kind userName def mkFreshExprMVar (type? : Option Expr) (kind := MetavarKind.natural) (userName := Name.anonymous) : m Expr := liftMetaM $ mkFreshExprMVarImpl type? kind userName def mkFreshTypeMVar (kind := MetavarKind.natural) (userName := Name.anonymous) : m Expr := liftMetaM do let u ← mkFreshLevelMVar mkFreshExprMVar (mkSort u) kind userName /- Low-level version of `MkFreshExprMVar` which allows users to create/reserve a `mvarId` using `mkFreshId`, and then later create the metavar using this method. -/ private def mkFreshExprMVarWithIdCore (mvarId : MVarId) (type : Expr) (kind : MetavarKind := MetavarKind.natural) (userName : Name := Name.anonymous) (numScopeArgs : Nat := 0) : m Expr := liftMetaM do let lctx ← getLCtx let localInsts ← getLocalInstances mkFreshExprMVarAtCore mvarId lctx localInsts type kind userName numScopeArgs def mkFreshExprMVarWithIdImpl (mvarId : MVarId) (type? : Option Expr) (kind : MetavarKind) (userName : Name) : MetaM Expr := match type? with | some type => mkFreshExprMVarWithIdCore mvarId type kind userName | none => do let u ← mkFreshLevelMVar let type ← mkFreshExprMVar (mkSort u) mkFreshExprMVarWithIdCore mvarId type kind userName def mkFreshExprMVarWithId (mvarId : MVarId) (type? : Option Expr := none) (kind : MetavarKind := MetavarKind.natural) (userName := Name.anonymous) : m Expr := liftMetaM $ mkFreshExprMVarWithIdImpl mvarId type? kind userName def shouldReduceAll : MetaM Bool := liftMetaM do return (← read).config.transparency == TransparencyMode.all def shouldReduceReducibleOnly : m Bool := liftMetaM do return (← read).config.transparency == TransparencyMode.reducible def getTransparency : m TransparencyMode := liftMetaM do return (← read).config.transparency def getMVarDecl (mvarId : MVarId) : m MetavarDecl := liftMetaM do let mctx ← getMCtx match mctx.findDecl? mvarId with | some d => pure d | none => throwError! "unknown metavariable '{mkMVar mvarId}'" def setMVarKind (mvarId : MVarId) (kind : MetavarKind) : m Unit := liftMetaM do modifyMCtx fun mctx => mctx.setMVarKind mvarId kind def isReadOnlyExprMVar (mvarId : MVarId) : m Bool := liftMetaM do let mvarDecl ← getMVarDecl mvarId let mctx ← getMCtx pure $ mvarDecl.depth != mctx.depth def isReadOnlyOrSyntheticOpaqueExprMVar (mvarId : MVarId) : m Bool := liftMetaM do let mvarDecl ← getMVarDecl mvarId match mvarDecl.kind with | MetavarKind.syntheticOpaque => pure true | _ => do let mctx ← getMCtx pure $ mvarDecl.depth != mctx.depth def isReadOnlyLevelMVar (mvarId : MVarId) : m Bool := liftMetaM do let mctx ← getMCtx match mctx.findLevelDepth? mvarId with | some depth => pure $ depth != mctx.depth | _ => throwError! "unknown universe metavariable '{mkLevelMVar mvarId}'" def renameMVar (mvarId : MVarId) (newUserName : Name) : m Unit := liftMetaM do modifyMCtx fun mctx => mctx.renameMVar mvarId newUserName def isExprMVarAssigned (mvarId : MVarId) : m Bool := liftMetaM do let mctx ← getMCtx pure $ mctx.isExprAssigned mvarId def getExprMVarAssignment? (mvarId : MVarId) : m (Option Expr) := liftMetaM do let mctx ← getMCtx pure (mctx.getExprAssignment? mvarId) def assignExprMVar (mvarId : MVarId) (val : Expr) : m Unit := liftMetaM do modifyMCtx fun mctx => mctx.assignExpr mvarId val def isDelayedAssigned (mvarId : MVarId) : m Bool := liftMetaM do return (← getMCtx).isDelayedAssigned mvarId def getDelayedAssignment? (mvarId : MVarId) : m (Option DelayedMetavarAssignment) := liftMetaM do return (← getMCtx).getDelayedAssignment? mvarId def hasAssignableMVar (e : Expr) : m Bool := liftMetaM do return (← getMCtx).hasAssignableMVar e def throwUnknownFVar {α} (fvarId : FVarId) : MetaM α := throwError! "unknown free variable '{mkFVar fvarId}'" def findLocalDecl? (fvarId : FVarId) : m (Option LocalDecl) := liftMetaM do return (← getLCtx).find? fvarId def getLocalDecl (fvarId : FVarId) : m LocalDecl := liftMetaM do match (← getLCtx).find? fvarId with | some d => pure d | none => throwUnknownFVar fvarId def getFVarLocalDecl (fvar : Expr) : m LocalDecl := liftMetaM do getLocalDecl fvar.fvarId! def getLocalDeclFromUserName (userName : Name) : m LocalDecl := liftMetaM do match (← getLCtx).findFromUserName? userName with | some d => pure d | none => throwError! "unknown local declaration '{userName}'" def instantiateLevelMVarsImp (u : Level) : MetaM Level := MetavarContext.instantiateLevelMVars u def instantiateLevelMVars (u : Level) : m Level := liftMetaM do instantiateLevelMVarsImp u def instantiateMVarsImp (e : Expr) : MetaM Expr := (MetavarContext.instantiateExprMVars e).run def instantiateMVars (e : Expr) : m Expr := liftMetaM do instantiateMVarsImp e def instantiateLocalDeclMVars (localDecl : LocalDecl) : m LocalDecl := liftMetaM do match localDecl with | LocalDecl.cdecl idx id n type bi => let type ← instantiateMVars type pure $ LocalDecl.cdecl idx id n type bi | LocalDecl.ldecl idx id n type val nonDep => let type ← instantiateMVars type let val ← instantiateMVars val pure $ LocalDecl.ldecl idx id n type val nonDep @[inline] private def liftMkBindingM {α} (x : MetavarContext.MkBindingM α) : MetaM α := do match x (← getLCtx) { mctx := (← getMCtx), ngen := (← getNGen) } with | EStateM.Result.ok e newS => do setNGen newS.ngen; setMCtx newS.mctx; pure e | EStateM.Result.error (MetavarContext.MkBinding.Exception.revertFailure mctx lctx toRevert decl) newS => do setMCtx newS.mctx; setNGen newS.ngen; throwError "failed to create binder due to failure when reverting variable dependencies" def mkForallFVars (xs : Array Expr) (e : Expr) : m Expr := liftMetaM do if xs.isEmpty then pure e else liftMkBindingM $ MetavarContext.mkForall xs e def mkLambdaFVars (xs : Array Expr) (e : Expr) : m Expr := liftMetaM do if xs.isEmpty then pure e else liftMkBindingM $ MetavarContext.mkLambda xs e def mkLetFVars (xs : Array Expr) (e : Expr) : m Expr := mkLambdaFVars xs e def mkArrow (d b : Expr) : m Expr := liftMetaM do let n ← mkFreshUserName `x return Lean.mkForall n BinderInfo.default d b def mkForallUsedOnly (xs : Array Expr) (e : Expr) : m (Expr × Nat) := liftMetaM do if xs.isEmpty then pure (e, 0) else liftMkBindingM $ MetavarContext.mkForallUsedOnly xs e def elimMVarDeps (xs : Array Expr) (e : Expr) (preserveOrder : Bool := false) : m Expr := liftMetaM do if xs.isEmpty then pure e else liftMkBindingM $ MetavarContext.elimMVarDeps xs e preserveOrder @[inline] def withConfig {α} (f : Config → Config) : n α → n α := mapMetaM $ withReader (fun ctx => { ctx with config := f ctx.config }) @[inline] def withTrackingZeta {α} (x : n α) : n α := withConfig (fun cfg => { cfg with trackZeta := true }) x @[inline] def withTransparency {α} (mode : TransparencyMode) : n α → n α := mapMetaM $ withConfig (fun config => { config with transparency := mode }) @[inline] def withReducible {α} (x : n α) : n α := withTransparency TransparencyMode.reducible x @[inline] def withAtLeastTransparency {α} (mode : TransparencyMode) (x : n α) : n α := withConfig (fun config => let oldMode := config.transparency let mode := if oldMode.lt mode then mode else oldMode { config with transparency := mode }) x def getConst? (constName : Name) : MetaM (Option ConstantInfo) := do let env ← getEnv match env.find? constName with | some (info@(ConstantInfo.thmInfo _)) => if (← shouldReduceAll) then pure (some info) else pure none | some (info@(ConstantInfo.defnInfo _)) => if (← shouldReduceReducibleOnly) then if (← isReducible constName) then pure (some info) else pure none else pure (some info) | some info => pure (some info) | none => throwUnknownConstant constName def getConstNoEx? (constName : Name) : MetaM (Option ConstantInfo) := do let env ← getEnv match env.find? constName with | some (info@(ConstantInfo.thmInfo _)) => if (← shouldReduceAll) then pure (some info) else pure none | some (info@(ConstantInfo.defnInfo _)) => if (← shouldReduceReducibleOnly) then if (← isReducible constName) then pure (some info) else pure none else pure (some info) | some info => pure (some info) | none => pure none /-- Save cache, execute `x`, restore cache -/ @[inline] private def savingCacheImpl {α} (x : MetaM α) : MetaM α := do let s ← get let savedCache := s.cache try x finally modify fun s => { s with cache := savedCache } @[inline] def savingCache {α} : n α → n α := mapMetaM savingCacheImpl private def isClassQuickConst? (constName : Name) : MetaM (LOption Name) := do let env ← getEnv if isClass env constName then pure (LOption.some constName) else match (← getConst? constName) with | some _ => pure LOption.undef | none => pure LOption.none private partial def isClassQuick? : Expr → MetaM (LOption Name) | Expr.bvar _ _ => pure LOption.none | Expr.lit _ _ => pure LOption.none | Expr.fvar _ _ => pure LOption.none | Expr.sort _ _ => pure LOption.none | Expr.lam _ _ _ _ => pure LOption.none | Expr.letE _ _ _ _ _ => pure LOption.undef | Expr.proj _ _ _ _ => pure LOption.undef | Expr.forallE _ _ b _ => isClassQuick? b | Expr.mdata _ e _ => isClassQuick? e | Expr.const n _ _ => isClassQuickConst? n | Expr.mvar mvarId _ => do match (← getExprMVarAssignment? mvarId) with | some val => isClassQuick? val | none => pure LOption.none | Expr.app f _ _ => match f.getAppFn with | Expr.const n _ _ => isClassQuickConst? n | Expr.lam _ _ _ _ => pure LOption.undef | _ => pure LOption.none def saveAndResetSynthInstanceCache : MetaM SynthInstanceCache := do let s ← get let savedSythInstance := s.cache.synthInstance modify fun s => { s with cache := { s.cache with synthInstance := {} } } pure savedSythInstance def restoreSynthInstanceCache (cache : SynthInstanceCache) : MetaM Unit := modify fun s => { s with cache := { s.cache with synthInstance := cache } } @[inline] private def resettingSynthInstanceCacheImpl {α} (x : MetaM α) : MetaM α := do let savedSythInstance ← saveAndResetSynthInstanceCache try x finally restoreSynthInstanceCache savedSythInstance /-- Reset `synthInstance` cache, execute `x`, and restore cache -/ @[inline] def resettingSynthInstanceCache {α} : n α → n α := mapMetaM resettingSynthInstanceCacheImpl @[inline] def resettingSynthInstanceCacheWhen {α} (b : Bool) (x : n α) : n α := if b then resettingSynthInstanceCache x else x private def withNewLocalInstanceImp {α} (className : Name) (fvar : Expr) (k : MetaM α) : MetaM α := do let localDecl ← getFVarLocalDecl fvar /- Recall that we use `auxDecl` binderInfo when compiling recursive declarations. -/ match localDecl.binderInfo with | BinderInfo.auxDecl => k | _ => resettingSynthInstanceCache $ withReader (fun ctx => { ctx with localInstances := ctx.localInstances.push { className := className, fvar := fvar } }) k /-- Add entry `{ className := className, fvar := fvar }` to localInstances, and then execute continuation `k`. It resets the type class cache using `resettingSynthInstanceCache`. -/ def withNewLocalInstance {α} (className : Name) (fvar : Expr) : n α → n α := mapMetaM $ withNewLocalInstanceImp className fvar /-- `withNewLocalInstances isClassExpensive fvars j k` updates the vector or local instances using free variables `fvars[j] ... fvars.back`, and execute `k`. - `isClassExpensive` is defined later. - The type class chache is reset whenever a new local instance is found. - `isClassExpensive` uses `whnf` which depends (indirectly) on the set of local instances. Thus, each new local instance requires a new `resettingSynthInstanceCache`. -/ @[specialize] private partial def withNewLocalInstancesImp {α} (isClassExpensive? : Expr → MetaM (Option Name)) (fvars : Array Expr) (i : Nat) (k : MetaM α) : MetaM α := do if h : i < fvars.size then let fvar := fvars.get ⟨i, h⟩ let decl ← getFVarLocalDecl fvar match (← isClassQuick? decl.type) with | LOption.none => withNewLocalInstancesImp isClassExpensive? fvars (i+1) k | LOption.undef => match (← isClassExpensive? decl.type) with | none => withNewLocalInstancesImp isClassExpensive? fvars (i+1) k | some c => withNewLocalInstance c fvar $ withNewLocalInstancesImp isClassExpensive? fvars (i+1) k | LOption.some c => withNewLocalInstance c fvar $ withNewLocalInstancesImp isClassExpensive? fvars (i+1) k else k private def fvarsSizeLtMaxFVars (fvars : Array Expr) (maxFVars? : Option Nat) : Bool := match maxFVars? with | some maxFVars => fvars.size < maxFVars | none => true /-- `forallTelescopeAux whnf k lctx fvars j type` Remarks: - `lctx` is the `MetaM` local context exteded with the declaration for `fvars`. - `type` is the type we are computing the telescope for. It contains only dangling bound variables in the range `[j, fvars.size)` - if `reducing? == true` and `type` is not `forallE`, we use `whnf`. - when `type` is not a `forallE` nor it can't be reduced to one, we excute the continuation `k`. Here is an example that demonstrates the `reducing?`. Suppose we have ``` abbrev StateM s a := s -> Prod a s ``` Now, assume we are trying to build the telescope for ``` forall (x : Nat), StateM Int Bool ``` if `reducing? == true`, the function executes `k #[(x : Nat) (s : Int)] Bool`. if `reducing? == false`, the function executes `k #[(x : Nat)] (StateM Int Bool)` if `maxFVars?` is `some max`, then we interrupt the telescope construction when `fvars.size == max` -/ @[specialize] private partial def forallTelescopeReducingAuxAux {α} (isClassExpensive? : Expr → MetaM (Option Name)) (reducing? : Bool) (maxFVars? : Option Nat) (type : Expr) (k : Array Expr → Expr → MetaM α) : MetaM α := do let rec process (lctx : LocalContext) (fvars : Array Expr) (j : Nat) (type : Expr) : MetaM α := do match type with | Expr.forallE n d b c => if fvarsSizeLtMaxFVars fvars maxFVars? then let d := d.instantiateRevRange j fvars.size fvars let fvarId ← mkFreshId let lctx := lctx.mkLocalDecl fvarId n d c.binderInfo let fvar := mkFVar fvarId let fvars := fvars.push fvar process lctx fvars j b else let type := type.instantiateRevRange j fvars.size fvars; withReader (fun ctx => { ctx with lctx := lctx }) do withNewLocalInstancesImp isClassExpensive? fvars j do k fvars type | _ => let type := type.instantiateRevRange j fvars.size fvars; withReader (fun ctx => { ctx with lctx := lctx }) do withNewLocalInstancesImp isClassExpensive? fvars j do if reducing? && fvarsSizeLtMaxFVars fvars maxFVars? then let newType ← whnf type if newType.isForall then process lctx fvars fvars.size newType else k fvars type else k fvars type process (← getLCtx) #[] 0 type /- We need this auxiliary definition because it depends on `isClassExpensive`, and `isClassExpensive` depends on it. -/ @[specialize] private def forallTelescopeReducingAux {α} (isClassExpensive? : Expr → MetaM (Option Name)) (type : Expr) (maxFVars? : Option Nat) (k : Array Expr → Expr → MetaM α) : MetaM α := do match maxFVars? with | some 0 => k #[] type | _ => do let newType ← whnf type if newType.isForall then forallTelescopeReducingAuxAux isClassExpensive? true maxFVars? newType k else k #[] type private partial def isClassExpensive? : Expr → MetaM (Option Name) | type => withReducible $ -- when testing whether a type is a type class, we only unfold reducible constants. forallTelescopeReducingAux isClassExpensive? type none fun xs type => do match type.getAppFn with | Expr.const c _ _ => do let env ← getEnv pure $ if isClass env c then some c else none | _ => pure none private def isClassImp? (type : Expr) : MetaM (Option Name) := do match (← isClassQuick? type) with | LOption.none => pure none | LOption.some c => pure (some c) | LOption.undef => isClassExpensive? type def isClass? (type : Expr) : m (Option Name) := liftMetaM do try isClassImp? type catch _ => pure none private def withNewLocalInstancesImpAux {α} (fvars : Array Expr) (j : Nat) : n α → n α := mapMetaM $ withNewLocalInstancesImp isClassExpensive? fvars j def withNewLocalInstances {α} (fvars : Array Expr) (j : Nat) : n α → n α := mapMetaM $ withNewLocalInstancesImpAux fvars j private def forallTelescopeImp {α} (type : Expr) (k : Array Expr → Expr → MetaM α) : MetaM α := do forallTelescopeReducingAuxAux isClassExpensive? false none type k /-- Given `type` of the form `forall xs, A`, execute `k xs A`. This combinator will declare local declarations, create free variables for them, execute `k` with updated local context, and make sure the cache is restored after executing `k`. -/ def forallTelescope {α} (type : Expr) (k : Array Expr → Expr → n α) : n α := map2MetaM (fun k => forallTelescopeImp type k) k @[noinline] private def forallTelescopeReducingImp {α} (type : Expr) (k : Array Expr → Expr → MetaM α) : MetaM α := forallTelescopeReducingAux isClassExpensive? type none k /-- Similar to `forallTelescope`, but given `type` of the form `forall xs, A`, it reduces `A` and continues bulding the telescope if it is a `forall`. -/ def forallTelescopeReducing {α} (type : Expr) (k : Array Expr → Expr → n α) : n α := map2MetaM (fun k => forallTelescopeReducingImp type k) k @[noinline] private def forallBoundedTelescopeImp {α} (type : Expr) (maxFVars? : Option Nat) (k : Array Expr → Expr → MetaM α) : MetaM α := forallTelescopeReducingAux isClassExpensive? type maxFVars? k /-- Similar to `forallTelescopeReducing`, stops constructing the telescope when it reaches size `maxFVars`. -/ def forallBoundedTelescope {α} (type : Expr) (maxFVars? : Option Nat) (k : Array Expr → Expr → n α) : n α := map2MetaM (fun k => forallBoundedTelescopeImp type maxFVars? k) k /-- Similar to `forallTelescopeAuxAux` but for lambda and let expressions. -/ private partial def lambdaTelescopeAux {α} (k : Array Expr → Expr → MetaM α) : Bool → LocalContext → Array Expr → Nat → Expr → MetaM α | consumeLet, lctx, fvars, j, Expr.lam n d b c => do let d := d.instantiateRevRange j fvars.size fvars let fvarId ← mkFreshId let lctx := lctx.mkLocalDecl fvarId n d c.binderInfo let fvar := mkFVar fvarId lambdaTelescopeAux k consumeLet lctx (fvars.push fvar) j b | true, lctx, fvars, j, Expr.letE n t v b _ => do let t := t.instantiateRevRange j fvars.size fvars let v := v.instantiateRevRange j fvars.size fvars let fvarId ← mkFreshId let lctx := lctx.mkLetDecl fvarId n t v let fvar := mkFVar fvarId lambdaTelescopeAux k true lctx (fvars.push fvar) j b | _, lctx, fvars, j, e => let e := e.instantiateRevRange j fvars.size fvars; withReader (fun ctx => { ctx with lctx := lctx }) do withNewLocalInstancesImp isClassExpensive? fvars j do k fvars e private partial def lambdaTelescopeImp {α} (e : Expr) (consumeLet : Bool) (k : Array Expr → Expr → MetaM α) : MetaM α := do let rec process (consumeLet : Bool) (lctx : LocalContext) (fvars : Array Expr) (j : Nat) (e : Expr) : MetaM α := do match consumeLet, e with | _, Expr.lam n d b c => let d := d.instantiateRevRange j fvars.size fvars let fvarId ← mkFreshId let lctx := lctx.mkLocalDecl fvarId n d c.binderInfo let fvar := mkFVar fvarId process consumeLet lctx (fvars.push fvar) j b | true, Expr.letE n t v b _ => do let t := t.instantiateRevRange j fvars.size fvars let v := v.instantiateRevRange j fvars.size fvars let fvarId ← mkFreshId let lctx := lctx.mkLetDecl fvarId n t v let fvar := mkFVar fvarId process true lctx (fvars.push fvar) j b | _, e => let e := e.instantiateRevRange j fvars.size fvars withReader (fun ctx => { ctx with lctx := lctx }) do withNewLocalInstancesImp isClassExpensive? fvars j do k fvars e process consumeLet (← getLCtx) #[] 0 e /-- Similar to `forallTelescope` but for lambda and let expressions. -/ def lambdaLetTelescope {α} (type : Expr) (k : Array Expr → Expr → n α) : n α := map2MetaM (fun k => lambdaTelescopeImp type true k) k /-- Similar to `forallTelescope` but for lambda expressions. -/ def lambdaTelescope {α} (type : Expr) (k : Array Expr → Expr → n α) : n α := map2MetaM (fun k => lambdaTelescopeImp type false k) k def getParamNamesImp (declName : Name) : MetaM (Array Name) := do let cinfo ← getConstInfo declName forallTelescopeReducing cinfo.type fun xs _ => do xs.mapM fun x => do let localDecl ← getLocalDecl x.fvarId! pure localDecl.userName /-- Return the parameter names for the givel global declaration. -/ def getParamNames (declName : Name) : m (Array Name) := liftMetaM $ getParamNamesImp declName -- `kind` specifies the metavariable kind for metavariables not corresponding to instance implicit `[ ... ]` arguments. private partial def forallMetaTelescopeReducingAux (e : Expr) (reducing : Bool) (maxMVars? : Option Nat) (kind : MetavarKind) : MetaM (Array Expr × Array BinderInfo × Expr) := let rec process (mvars : Array Expr) (bis : Array BinderInfo) (j : Nat) (type : Expr) : MetaM (Array Expr × Array BinderInfo × Expr) := do match type with | Expr.forallE n d b c => let cont : Unit → MetaM (Array Expr × Array BinderInfo × Expr) := fun _ => do let d := d.instantiateRevRange j mvars.size mvars let k := if c.binderInfo.isInstImplicit then MetavarKind.synthetic else kind let mvar ← mkFreshExprMVar d k n let mvars := mvars.push mvar let bis := bis.push c.binderInfo process mvars bis j b match maxMVars? with | none => cont () | some maxMVars => if mvars.size < maxMVars then cont () else let type := type.instantiateRevRange j mvars.size mvars; pure (mvars, bis, type) | _ => let type := type.instantiateRevRange j mvars.size mvars; if reducing then do let newType ← whnf type; if newType.isForall then process mvars bis mvars.size newType else pure (mvars, bis, type) else pure (mvars, bis, type) process #[] #[] 0 e /-- Similar to `forallTelescope`, but creates metavariables instead of free variables. -/ def forallMetaTelescope (e : Expr) (kind := MetavarKind.natural) : m (Array Expr × Array BinderInfo × Expr) := liftMetaM $ forallMetaTelescopeReducingAux e (reducing := false) (maxMVars? := none) kind /-- Similar to `forallTelescopeReducing`, but creates metavariables instead of free variables. -/ def forallMetaTelescopeReducing (e : Expr) (maxMVars? : Option Nat := none) (kind := MetavarKind.natural) : m (Array Expr × Array BinderInfo × Expr) := liftMetaM $ forallMetaTelescopeReducingAux e (reducing := true) maxMVars? kind /-- Similar to `forallMetaTelescopeReducingAux` but for lambda expressions. -/ private partial def lambdaMetaTelescopeImp (e : Expr) (maxMVars? : Option Nat) : MetaM (Array Expr × Array BinderInfo × Expr) := let rec process (mvars : Array Expr) (bis : Array BinderInfo) (j : Nat) (type : Expr) : MetaM (Array Expr × Array BinderInfo × Expr) := do let finalize : Unit → MetaM (Array Expr × Array BinderInfo × Expr) := fun _ => do let type := type.instantiateRevRange j mvars.size mvars pure (mvars, bis, type) let cont : Unit → MetaM (Array Expr × Array BinderInfo × Expr) := fun _ => do match type with | Expr.lam n d b c => let d := d.instantiateRevRange j mvars.size mvars let mvar ← mkFreshExprMVar d let mvars := mvars.push mvar let bis := bis.push c.binderInfo process mvars bis j b | _ => finalize () match maxMVars? with | none => cont () | some maxMVars => if mvars.size < maxMVars then cont () else finalize () process #[] #[] 0 e /-- Similar to `forallMetaTelescope` but for lambda expressions. -/ partial def lambdaMetaTelescope (e : Expr) (maxMVars? : Option Nat := none) : m (Array Expr × Array BinderInfo × Expr) := liftMetaM $ lambdaMetaTelescopeImp e maxMVars? private def withNewFVar {α} (fvar fvarType : Expr) (k : Expr → MetaM α) : MetaM α := do match (← isClass? fvarType) with | none => k fvar | some c => withNewLocalInstance c fvar $ k fvar private def withLocalDeclImp {α} (n : Name) (bi : BinderInfo) (type : Expr) (k : Expr → MetaM α) : MetaM α := do let fvarId ← mkFreshId let ctx ← read let lctx := ctx.lctx.mkLocalDecl fvarId n type bi let fvar := mkFVar fvarId withReader (fun ctx => { ctx with lctx := lctx }) do withNewFVar fvar type k def withLocalDecl {α} (name : Name) (bi : BinderInfo) (type : Expr) (k : Expr → n α) : n α := map1MetaM (fun k => withLocalDeclImp name bi type k) k def withLocalDeclD {α} (name : Name) (type : Expr) (k : Expr → n α) : n α := withLocalDecl name BinderInfo.default type k private def withLetDeclImp {α} (n : Name) (type : Expr) (val : Expr) (k : Expr → MetaM α) : MetaM α := do let fvarId ← mkFreshId let ctx ← read let lctx := ctx.lctx.mkLetDecl fvarId n type val let fvar := mkFVar fvarId withReader (fun ctx => { ctx with lctx := lctx }) do withNewFVar fvar type k def withLetDecl {α} (name : Name) (type : Expr) (val : Expr) (k : Expr → n α) : n α := map1MetaM (fun k => withLetDeclImp name type val k) k private def withExistingLocalDeclsImp {α} (decls : List LocalDecl) (k : MetaM α) : MetaM α := do let ctx ← read let numLocalInstances := ctx.localInstances.size let lctx := decls.foldl (fun (lctx : LocalContext) decl => lctx.addDecl decl) ctx.lctx withReader (fun ctx => { ctx with lctx := lctx }) do let newLocalInsts ← decls.foldlM (fun (newlocalInsts : Array LocalInstance) (decl : LocalDecl) => (do { match (← isClass? decl.type) with | none => pure newlocalInsts | some c => pure $ newlocalInsts.push { className := c, fvar := decl.toExpr } } : MetaM _)) ctx.localInstances; if newLocalInsts.size == numLocalInstances then k else resettingSynthInstanceCache $ withReader (fun ctx => { ctx with localInstances := newLocalInsts }) k def withExistingLocalDecls {α} (decls : List LocalDecl) : n α → n α := mapMetaM $ withExistingLocalDeclsImp decls private def withNewMCtxDepthImp {α} (x : MetaM α) : MetaM α := do let s ← get let savedMCtx := s.mctx modifyMCtx fun mctx => mctx.incDepth try x finally setMCtx savedMCtx /-- Save cache and `MetavarContext`, bump the `MetavarContext` depth, execute `x`, and restore saved data. -/ def withNewMCtxDepth {α} : n α → n α := mapMetaM withNewMCtxDepthImp private def withLocalContextImp {α} (lctx : LocalContext) (localInsts : LocalInstances) (x : MetaM α) : MetaM α := do let localInstsCurr ← getLocalInstances withReader (fun ctx => { ctx with lctx := lctx, localInstances := localInsts }) do if localInsts == localInstsCurr then x else resettingSynthInstanceCache x def withLCtx {α} (lctx : LocalContext) (localInsts : LocalInstances) : n α → n α := mapMetaM $ withLocalContextImp lctx localInsts private def withMVarContextImp {α} (mvarId : MVarId) (x : MetaM α) : MetaM α := do let mvarDecl ← getMVarDecl mvarId withLocalContextImp mvarDecl.lctx mvarDecl.localInstances x /-- Execute `x` using the given metavariable `LocalContext` and `LocalInstances`. The type class resolution cache is flushed when executing `x` if its `LocalInstances` are different from the current ones. -/ def withMVarContext {α} (mvarId : MVarId) : n α → n α := mapMetaM $ withMVarContextImp mvarId private def withMCtxImp {α} (mctx : MetavarContext) (x : MetaM α) : MetaM α := do let mctx' ← getMCtx setMCtx mctx try x finally setMCtx mctx' def withMCtx {α} (mctx : MetavarContext) : n α → n α := mapMetaM $ withMCtxImp mctx @[inline] private def approxDefEqImp {α} (x : MetaM α) : MetaM α := withConfig (fun config => { config with foApprox := true, ctxApprox := true, quasiPatternApprox := true}) x /-- Execute `x` using approximate unification: `foApprox`, `ctxApprox` and `quasiPatternApprox`. -/ @[inline] def approxDefEq {α} : n α → n α := mapMetaM approxDefEqImp @[inline] private def fullApproxDefEqImp {α} (x : MetaM α) : MetaM α := withConfig (fun config => { config with foApprox := true, ctxApprox := true, quasiPatternApprox := true, constApprox := true }) x /-- Similar to `approxDefEq`, but uses all available approximations. We don't use `constApprox` by default at `approxDefEq` because it often produces undesirable solution for monadic code. For example, suppose we have `pure (x > 0)` which has type `?m Prop`. We also have the goal `[Pure ?m]`. Now, assume the expected type is `IO Bool`. Then, the unification constraint `?m Prop =?= IO Bool` could be solved as `?m := fun _ => IO Bool` using `constApprox`, but this spurious solution would generate a failure when we try to solve `[Pure (fun _ => IO Bool)]` -/ @[inline] def fullApproxDefEq {α} : n α → n α := mapMetaM fullApproxDefEqImp def normalizeLevel (u : Level) : m Level := liftMetaM do let u ← instantiateLevelMVars u; pure u.normalize def assignLevelMVar (mvarId : MVarId) (u : Level) : m Unit := liftMetaM do modifyMCtx fun mctx => mctx.assignLevel mvarId u def whnfD [MonadLiftT MetaM n] (e : Expr) : n Expr := withTransparency TransparencyMode.default $ whnf e def setInlineAttribute (declName : Name) (kind := Compiler.InlineAttributeKind.inline): m Unit := liftMetaM do let env ← getEnv match Compiler.setInlineAttribute env declName kind with | Except.ok env => setEnv env | Except.error msg => throwError msg private partial def instantiateForallAux (ps : Array Expr) (i : Nat) (e : Expr) : MetaM Expr := do if h : i < ps.size then let p := ps.get ⟨i, h⟩ let e ← whnf e match e with | Expr.forallE _ _ b _ => instantiateForallAux ps (i+1) (b.instantiate1 p) | _ => throwError "invalid instantiateForall, too many parameters" else pure e /- Given `e` of the form `forall (a_1 : A_1) ... (a_n : A_n), B[a_1, ..., a_n]` and `p_1 : A_1, ... p_n : A_n`, return `B[p_1, ..., p_n]`. -/ def instantiateForall (e : Expr) (ps : Array Expr) : m Expr := liftMetaM $ instantiateForallAux ps 0 e private partial def instantiateLambdaAux (ps : Array Expr) (i : Nat) (e : Expr) : MetaM Expr := do if h : i < ps.size then let p := ps.get ⟨i, h⟩ let e ← whnf e match e with | Expr.lam _ _ b _ => instantiateLambdaAux ps (i+1) (b.instantiate1 p) | _ => throwError "invalid instantiateLambda, too many parameters" else pure e /- Given `e` of the form `fun (a_1 : A_1) ... (a_n : A_n) => t[a_1, ..., a_n]` and `p_1 : A_1, ... p_n : A_n`, return `t[p_1, ..., p_n]`. It uses `whnf` to reduce `e` if it is not a lambda -/ def instantiateLambda (e : Expr) (ps : Array Expr) : m Expr := liftMetaM $ instantiateLambdaAux ps 0 e /-- Return true iff `e` depends on the free variable `fvarId` -/ def dependsOn (e : Expr) (fvarId : FVarId) : m Bool := liftMetaM do let mctx ← getMCtx pure $ mctx.exprDependsOn e fvarId def ppExprImp (e : Expr) : MetaM Format := do let env ← getEnv let mctx ← getMCtx let lctx ← getLCtx let opts ← getOptions liftIO $ Lean.ppExpr { env := env, mctx := mctx, lctx := lctx, opts := opts } e def ppExpr (e : Expr) : m Format := liftMetaM $ ppExprImp e @[inline] protected def orelse {α} (x y : MetaM α) : MetaM α := do let env ← getEnv let mctx ← getMCtx try x catch _ => setEnv env; setMCtx mctx; y instance {α} : OrElse (MetaM α) := ⟨Meta.orelse⟩ @[inline] private def orelseMergeErrorsImp {α} (x y : MetaM α) (mergeRef : Syntax → Syntax → Syntax := fun r₁ r₂ => r₁) (mergeMsg : MessageData → MessageData → MessageData := fun m₁ m₂ => m₁ ++ Format.line ++ m₂) : MetaM α := do let env ← getEnv let mctx ← getMCtx try x catch ex => setEnv env setMCtx mctx match ex with | Exception.error ref₁ m₁ => try y catch | Exception.error ref₂ m₂ => throw $ Exception.error (mergeRef ref₁ ref₂) (mergeMsg m₁ m₂) | ex => throw ex | ex => throw ex /-- Similar to `orelse`, but merge errors. Note that internal errors are not caught. The default `mergeRef` uses the `ref` (position information) for the first message. The default `mergeMsg` combines error messages using `Format.line ++ Format.line` as a separator. -/ @[inline] def orelseMergeErrors {α m} [MonadControlT MetaM m] [Monad m] (x y : m α) (mergeRef : Syntax → Syntax → Syntax := fun r₁ r₂ => r₁) (mergeMsg : MessageData → MessageData → MessageData := fun m₁ m₂ => m₁ ++ Format.line ++ Format.line ++ m₂) : m α := do controlAt MetaM fun runInBase => orelseMergeErrorsImp (runInBase x) (runInBase y) mergeRef mergeMsg /-- Execute `x`, and apply `f` to the produced error message -/ def mapErrorImp {α} (x : MetaM α) (f : MessageData → MessageData) : MetaM α := do try x catch | Exception.error ref msg => throw $ Exception.error ref $ f msg | ex => throw ex @[inline] def mapError {α m} [MonadControlT MetaM m] [Monad m] (x : m α) (f : MessageData → MessageData) : m α := controlAt MetaM fun runInBase => mapErrorImp (runInBase x) f /-- `commitWhenSome? x` executes `x` and keep modifications when it returns `some a`. -/ @[specialize] def commitWhenSome? {α} (x? : MetaM (Option α)) : MetaM (Option α) := do let env ← getEnv let mctx ← getMCtx try match (← x?) with | some a => pure (some a) | none => setEnv env setMCtx mctx pure none catch ex => setEnv env setMCtx mctx throw ex end Methods end Meta export Meta (MetaM) end Lean